1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261 |
- #define PROGNAME "rpng2-win"
- #define LONGNAME "Progressive PNG Viewer for Windows"
- #define VERSION "2.02 of 16 March 2008"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <setjmp.h> /* for jmpbuf declaration in readpng2.h */
- #include <time.h>
- #include <math.h> /* only for PvdM background code */
- #include <windows.h>
- #ifdef __CYGWIN__
- #include <unistd.h>
- #include <termio.h>
- #include <sys/ioctl.h>
- int repl_getch( void )
- {
- char ch;
- int fd = fileno(stdin);
- struct termio old_tty, new_tty;
- ioctl(fd, TCGETA, &old_tty);
- new_tty = old_tty;
- new_tty.c_lflag &= ~(ICANON | ECHO | ISIG);
- ioctl(fd, TCSETA, &new_tty);
- fread(&ch, 1, sizeof(ch), stdin);
- ioctl(fd, TCSETA, &old_tty);
- return ch;
- }
- #define _getch repl_getch
- #else
- #include <conio.h> /* only for _getch() */
- #endif
- #ifndef PI
- # define PI 3.141592653589793238
- #endif
- #define PI_2 (PI*0.5)
- #define INV_PI_360 (360.0 / PI)
- #define MAX(a,b) (a>b?a:b)
- #define MIN(a,b) (a<b?a:b)
- #define CLIP(a,min,max) MAX(min,MIN((a),max))
- #define ABS(a) ((a)<0?-(a):(a))
- #define CLIP8P(c) MAX(0,(MIN((c),255)))
- #define ROUNDF(f) ((int)(f + 0.5))
- #define rgb1_max bg_freq
- #define rgb1_min bg_gray
- #define rgb2_max bg_bsat
- #define rgb2_min bg_brot
-
- #include "readpng2.h"
- #define alpha_composite(composite, fg, alpha, bg) { \
- ush temp = ((ush)(fg)*(ush)(alpha) + \
- (ush)(bg)*(ush)(255 - (ush)(alpha)) + (ush)128); \
- (composite) = (uch)((temp + (temp >> 8)) >> 8); \
- }
- #define INBUFSIZE 4096
- static void rpng2_win_init(void);
- static int rpng2_win_create_window(void);
- static int rpng2_win_load_bg_image(void);
- static void rpng2_win_display_row(ulg row);
- static void rpng2_win_finish_display(void);
- static void rpng2_win_cleanup(void);
- LRESULT CALLBACK rpng2_win_wndproc(HWND, UINT, WPARAM, LPARAM);
- static char titlebar[1024];
- static char *progname = PROGNAME;
- static char *appname = LONGNAME;
- static char *filename;
- static FILE *infile;
- static mainprog_info rpng2_info;
- static uch inbuf[INBUFSIZE];
- static int incount;
- static int pat = 6;
- static int bg_image = 0;
- static int bgscale = 16;
- static ulg bg_rowbytes;
- static uch *bg_data;
- static struct rgb_color {
- uch r, g, b;
- } rgb[] = {
- { 0, 0, 0},
- {255, 255, 255},
- {173, 132, 57},
- { 64, 132, 0},
- {189, 117, 1},
- {253, 249, 1},
- { 0, 0, 255},
- { 0, 0, 120},
- {255, 0, 255},
- { 64, 0, 64},
- {255, 0, 0},
- { 64, 0, 0},
- {255, 127, 0},
- {192, 96, 0},
- { 24, 60, 0},
- { 85, 125, 200}
- };
- static struct background_pattern {
- ush type;
- int rgb1_max, rgb1_min;
- int rgb2_max, rgb2_min;
- } bg[] = {
- {0+8, 2,0, 1,15},
- {0+24, 2,0, 1,0},
- {0+8, 4,5, 0,2},
- {0+8, 4,5, 0,6},
- {0, 7,0, 8,9},
- {0+8, 13,0, 5,14},
- {0+8, 12,0, 10,11},
- {1, 7,0, 8,0},
- {1, 12,0, 11,0},
- {1, 10,0, 7,0},
- {1, 4,0, 5,0},
- {1, 3,0, 0,0},
- {2, 16, 100, 20, 0},
- {2, 18, 100, 10, 2},
- {2, 16, 256, 100, 250},
- {2, 10000, 256, 11, 0}
- };
- static int num_bgpat = sizeof(bg) / sizeof(struct background_pattern);
- static ulg wimage_rowbytes;
- static uch *dib;
- static uch *wimage_data;
- static BITMAPINFOHEADER *bmih;
- static HWND global_hwnd;
- static HINSTANCE global_hInst;
- static int global_showmode;
- int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, PSTR cmd, int showmode)
- {
- char *args[1024];
- char **argv = args;
- char *p, *q, *bgstr = NULL;
- int argc = 0;
- int rc, alen, flen;
- int error = 0;
- int timing = FALSE;
- int have_bg = FALSE;
- double LUT_exponent;
- double CRT_exponent = 2.2;
- double default_display_exponent;
- MSG msg;
-
- global_hInst = hInst;
- global_showmode = showmode;
- filename = (char *)NULL;
- memset(&rpng2_info, 0, sizeof(mainprog_info));
- #ifndef __CYGWIN__
-
- AllocConsole();
- freopen("CONOUT$", "a", stderr);
- freopen("CONOUT$", "a", stdout);
- #endif
-
- #if defined(NeXT)
-
- LUT_exponent = 1.0 / 2.2;
-
- #elif defined(sgi)
- LUT_exponent = 1.0 / 1.7;
-
- infile = fopen("/etc/config/system.glGammaVal", "r");
- if (infile) {
- double sgi_gamma;
- fgets(tmpline, 80, infile);
- fclose(infile);
- sgi_gamma = atof(tmpline);
- if (sgi_gamma > 0.0)
- LUT_exponent = 1.0 / sgi_gamma;
- }
- #elif defined(Macintosh)
- LUT_exponent = 1.8 / 2.61;
-
- #else
- LUT_exponent = 1.0;
- #endif
-
- default_display_exponent = LUT_exponent * CRT_exponent;
-
- if ((p = getenv("SCREEN_GAMMA")) != NULL)
- rpng2_info.display_exponent = atof(p);
- else
- rpng2_info.display_exponent = default_display_exponent;
-
- argv[argc++] = PROGNAME;
- p = cmd;
- for (;;) {
- if (*p == ' ')
- while (*++p == ' ')
- ;
-
- if (*p == '\0')
- break;
- argv[argc++] = q = p;
- while (*q && *q != ' ')
- ++q;
-
- if (*q == '\0')
- break;
- *q = '\0';
- p = q + 1;
- }
- argv[argc] = NULL;
-
- while (*++argv && !error) {
- if (!strncmp(*argv, "-gamma", 2)) {
- if (!*++argv)
- ++error;
- else {
- rpng2_info.display_exponent = atof(*argv);
- if (rpng2_info.display_exponent <= 0.0)
- ++error;
- }
- } else if (!strncmp(*argv, "-bgcolor", 4)) {
- if (!*++argv)
- ++error;
- else {
- bgstr = *argv;
- if (strlen(bgstr) != 7 || bgstr[0] != '#')
- ++error;
- else {
- have_bg = TRUE;
- bg_image = FALSE;
- }
- }
- } else if (!strncmp(*argv, "-bgpat", 4)) {
- if (!*++argv)
- ++error;
- else {
- pat = atoi(*argv) - 1;
- if (pat < 0 || pat >= num_bgpat)
- ++error;
- else {
- bg_image = TRUE;
- have_bg = FALSE;
- }
- }
- } else if (!strncmp(*argv, "-timing", 2)) {
- timing = TRUE;
- } else {
- if (**argv != '-') {
- filename = *argv;
- if (argv[1])
- ++error;
- } else
- ++error;
- }
- }
- if (!filename)
- ++error;
-
- if (error) {
- #ifndef __CYGWIN__
- int ch;
- #endif
- fprintf(stderr, "\n%s %s: %s\n\n", PROGNAME, VERSION, appname);
- readpng2_version_info();
- fprintf(stderr, "\n"
- "Usage: %s [-gamma exp] [-bgcolor bg | -bgpat pat] [-timing]\n"
- " %*s file.png\n\n"
- " exp \ttransfer-function exponent (``gamma'') of the display\n"
- "\t\t system in floating-point format (e.g., ``%.1f''); equal\n"
- "\t\t to the product of the lookup-table exponent (varies)\n"
- "\t\t and the CRT exponent (usually 2.2); must be positive\n"
- " bg \tdesired background color in 7-character hex RGB format\n"
- "\t\t (e.g., ``#ff7700'' for orange: same as HTML colors);\n"
- "\t\t used with transparent images; overrides -bgpat option\n"
- " pat \tdesired background pattern number (1-%d); used with\n"
- "\t\t transparent images; overrides -bgcolor option\n"
- " -timing\tenables delay for every block read, to simulate modem\n"
- "\t\t download of image (~36 Kbps)\n"
- "\nPress Q, Esc or mouse button 1 after image is displayed to quit.\n"
- #ifndef __CYGWIN__
- "Press Q or Esc to quit this usage screen. ",
- #else
- ,
- #endif
- PROGNAME,
- #if (defined(__i386__) || defined(_M_IX86) || defined(__x86_64__)) && \
- !(defined(__CYGWIN__) || defined(__MINGW32__))
- (int)strlen(PROGNAME), " ",
- #endif
- (int)strlen(PROGNAME), " ", default_display_exponent, num_bgpat);
- fflush(stderr);
- #ifndef __CYGWIN__
- do
- ch = _getch();
- while (ch != 'q' && ch != 'Q' && ch != 0x1B);
- #endif
- exit(1);
- }
- if (!(infile = fopen(filename, "rb"))) {
- fprintf(stderr, PROGNAME ": can't open PNG file [%s]\n", filename);
- ++error;
- } else {
- incount = fread(inbuf, 1, INBUFSIZE, infile);
- if (incount < 8 || !readpng2_check_sig(inbuf, 8)) {
- fprintf(stderr, PROGNAME
- ": [%s] is not a PNG file: incorrect signature\n",
- filename);
- ++error;
- } else if ((rc = readpng2_init(&rpng2_info)) != 0) {
- switch (rc) {
- case 2:
- fprintf(stderr, PROGNAME
- ": [%s] has bad IHDR (libpng longjmp)\n", filename);
- break;
- case 4:
- fprintf(stderr, PROGNAME ": insufficient memory\n");
- break;
- default:
- fprintf(stderr, PROGNAME
- ": unknown readpng2_init() error\n");
- break;
- }
- ++error;
- }
- if (error)
- fclose(infile);
- }
- if (error) {
- #ifndef __CYGWIN__
- int ch;
- #endif
- fprintf(stderr, PROGNAME ": aborting.\n");
- #ifndef __CYGWIN__
- do
- ch = _getch();
- while (ch != 'q' && ch != 'Q' && ch != 0x1B);
- #endif
- exit(2);
- } else {
- fprintf(stderr, "\n%s %s: %s\n", PROGNAME, VERSION, appname);
- #ifndef __CYGWIN__
- fprintf(stderr,
- "\n [console window: closing this window will terminate %s]\n\n",
- PROGNAME);
- #endif
- fflush(stderr);
- }
-
- alen = strlen(appname);
- flen = strlen(filename);
- if (alen + flen + 3 > 1023)
- sprintf(titlebar, "%s: ...%s", appname, filename+(alen+flen+6-1023));
- else
- sprintf(titlebar, "%s: %s", appname, filename);
-
- if (have_bg) {
- unsigned r, g, b;
- sscanf(bgstr+1, "%2x%2x%2x", &r, &g, &b);
- rpng2_info.bg_red = (uch)r;
- rpng2_info.bg_green = (uch)g;
- rpng2_info.bg_blue = (uch)b;
- } else
- rpng2_info.need_bgcolor = TRUE;
- rpng2_info.state = kPreInit;
- rpng2_info.mainprog_init = rpng2_win_init;
- rpng2_info.mainprog_display_row = rpng2_win_display_row;
- rpng2_info.mainprog_finish_display = rpng2_win_finish_display;
-
- for (;;) {
- Trace((stderr, "about to call readpng2_decode_data()\n"))
- if (readpng2_decode_data(&rpng2_info, inbuf, incount))
- ++error;
- Trace((stderr, "done with readpng2_decode_data()\n"))
- if (error || incount != INBUFSIZE || rpng2_info.state == kDone) {
- if (rpng2_info.state == kDone) {
- Trace((stderr, "done decoding PNG image\n"))
- } else if (ferror(infile)) {
- fprintf(stderr, PROGNAME
- ": error while reading PNG image file\n");
- exit(3);
- } else if (feof(infile)) {
- fprintf(stderr, PROGNAME ": end of file reached "
- "(unexpectedly) while reading PNG image file\n");
- exit(3);
- } else {
-
- }
- break;
- }
- if (timing)
- Sleep(1000L);
- incount = fread(inbuf, 1, INBUFSIZE, infile);
- }
-
- fclose(infile);
- Trace((stderr, "about to call readpng2_cleanup()\n"))
- readpng2_cleanup(&rpng2_info);
- if (error) {
- fprintf(stderr, PROGNAME ": libpng error while decoding PNG image\n");
- exit(3);
- }
-
- while (GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
-
- Trace((stderr, "about to call rpng2_win_cleanup()\n"))
- rpng2_win_cleanup();
- return msg.wParam;
- }
- static void rpng2_win_init()
- {
- ulg i;
- ulg rowbytes = rpng2_info.rowbytes;
- Trace((stderr, "beginning rpng2_win_init()\n"))
- Trace((stderr, " rowbytes = %d\n", rpng2_info.rowbytes))
- Trace((stderr, " width = %ld\n", rpng2_info.width))
- Trace((stderr, " height = %ld\n", rpng2_info.height))
-
- if (rpng2_info.height > ((size_t)(-1))/rowbytes) {
- fprintf(stderr, PROGNAME ": image_data buffer would be too large\n",
- readpng2_cleanup(&rpng2_info);
- return;
- }
- rpng2_info.image_data = (uch *)malloc(rowbytes * rpng2_info.height);
- if (!rpng2_info.image_data) {
- readpng2_cleanup(&rpng2_info);
- return;
- }
- rpng2_info.row_pointers = (uch **)malloc(rpng2_info.height * sizeof(uch *));
- if (!rpng2_info.row_pointers) {
- free(rpng2_info.image_data);
- rpng2_info.image_data = NULL;
- readpng2_cleanup(&rpng2_info);
- return;
- }
- for (i = 0; i < rpng2_info.height; ++i)
- rpng2_info.row_pointers[i] = rpng2_info.image_data + i*rowbytes;
- if (rpng2_win_create_window()) {
- readpng2_cleanup(&rpng2_info);
- return;
- }
- rpng2_info.state = kWindowInit;
- }
- static int rpng2_win_create_window()
- {
- uch bg_red = rpng2_info.bg_red;
- uch bg_green = rpng2_info.bg_green;
- uch bg_blue = rpng2_info.bg_blue;
- uch *dest;
- int extra_width, extra_height;
- ulg i, j;
- WNDCLASSEX wndclass;
- RECT rect;
- wimage_rowbytes = ((3*rpng2_info.width + 3L) >> 2) << 2;
- if (!(dib = (uch *)malloc(sizeof(BITMAPINFOHEADER) +
- wimage_rowbytes*rpng2_info.height)))
- {
- return 4;
- }
- memset(dib, 0, sizeof(BITMAPINFOHEADER));
- bmih = (BITMAPINFOHEADER *)dib;
- bmih->biSize = sizeof(BITMAPINFOHEADER);
- bmih->biWidth = rpng2_info.width;
- bmih->biHeight = -((long)rpng2_info.height);
- bmih->biPlanes = 1;
- bmih->biBitCount = 24;
- bmih->biCompression = 0;
- wimage_data = dib + sizeof(BITMAPINFOHEADER);
- if (bg_image) {
- memset(wimage_data, 0, wimage_rowbytes*rpng2_info.height);
- } else {
- for (j = 0; j < rpng2_info.height; ++j) {
- dest = wimage_data + j*wimage_rowbytes;
- for (i = rpng2_info.width; i > 0; --i) {
- *dest++ = bg_blue;
- *dest++ = bg_green;
- *dest++ = bg_red;
- }
- }
- }
- memset(&wndclass, 0, sizeof(wndclass));
- wndclass.cbSize = sizeof(wndclass);
- wndclass.style = CS_HREDRAW | CS_VREDRAW;
- wndclass.lpfnWndProc = rpng2_win_wndproc;
- wndclass.hInstance = global_hInst;
- wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- wndclass.hbrBackground = (HBRUSH)GetStockObject(DKGRAY_BRUSH);
- wndclass.lpszMenuName = NULL;
- wndclass.lpszClassName = progname;
- wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- RegisterClassEx(&wndclass);
- extra_width = 2*(GetSystemMetrics(SM_CXBORDER) +
- GetSystemMetrics(SM_CXDLGFRAME));
- extra_height = 2*(GetSystemMetrics(SM_CYBORDER) +
- GetSystemMetrics(SM_CYDLGFRAME)) +
- GetSystemMetrics(SM_CYCAPTION);
- global_hwnd = CreateWindow(progname, titlebar, WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, CW_USEDEFAULT, rpng2_info.width+extra_width,
- rpng2_info.height+extra_height, NULL, NULL, global_hInst, NULL);
- ShowWindow(global_hwnd, global_showmode);
- UpdateWindow(global_hwnd);
- if (bg_image) {
- static const char *msg = "Computing background image...";
- int x, y, len = strlen(msg);
- HDC hdc = GetDC(global_hwnd);
- TEXTMETRIC tm;
- GetTextMetrics(hdc, &tm);
- x = (rpng2_info.width - len*tm.tmAveCharWidth)/2;
- y = (rpng2_info.height - tm.tmHeight)/2;
- SetBkMode(hdc, TRANSPARENT);
- SetTextColor(hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
-
- TextOut(hdc, ((x < 0)? 0 : x), ((y < 0)? 0 : y), msg, len);
- ReleaseDC(global_hwnd, hdc);
- rpng2_win_load_bg_image();
- }
- if (!bg_image) {
- for (j = 0; j < rpng2_info.height; ++j) {
- dest = wimage_data + j*wimage_rowbytes;
- for (i = rpng2_info.width; i > 0; --i) {
- *dest++ = bg_blue;
- *dest++ = bg_green;
- *dest++ = bg_red;
- }
- }
- }
- rect.left = 0L;
- rect.top = 0L;
- rect.right = (LONG)rpng2_info.width;
- rect.bottom = (LONG)rpng2_info.height;
- InvalidateRect(global_hwnd, &rect, FALSE);
- UpdateWindow(global_hwnd);
- return 0;
- }
- static int rpng2_win_load_bg_image()
- {
- uch *src, *dest;
- uch r1, r2, g1, g2, b1, b2;
- uch r1_inv, r2_inv, g1_inv, g2_inv, b1_inv, b2_inv;
- int k, hmax, max;
- int xidx, yidx, yidx_max = (bgscale-1);
- int even_odd_vert, even_odd_horiz, even_odd;
- int invert_gradient2 = (bg[pat].type & 0x08);
- int invert_column;
- ulg i, row;
- bg_rowbytes = 3 * rpng2_info.width;
- bg_data = (uch *)malloc(bg_rowbytes * rpng2_info.height);
- if (!bg_data) {
- fprintf(stderr, PROGNAME
- ": unable to allocate memory for background image\n");
- bg_image = 0;
- return 1;
- }
- if ((bg[pat].type & 0x07) == 0) {
- uch r1_min = rgb[bg[pat].rgb1_min].r;
- uch g1_min = rgb[bg[pat].rgb1_min].g;
- uch b1_min = rgb[bg[pat].rgb1_min].b;
- uch r2_min = rgb[bg[pat].rgb2_min].r;
- uch g2_min = rgb[bg[pat].rgb2_min].g;
- uch b2_min = rgb[bg[pat].rgb2_min].b;
- int r1_diff = rgb[bg[pat].rgb1_max].r - r1_min;
- int g1_diff = rgb[bg[pat].rgb1_max].g - g1_min;
- int b1_diff = rgb[bg[pat].rgb1_max].b - b1_min;
- int r2_diff = rgb[bg[pat].rgb2_max].r - r2_min;
- int g2_diff = rgb[bg[pat].rgb2_max].g - g2_min;
- int b2_diff = rgb[bg[pat].rgb2_max].b - b2_min;
- for (row = 0; row < rpng2_info.height; ++row) {
- yidx = row % bgscale;
- even_odd_vert = (row / bgscale) & 1;
- r1 = r1_min + (r1_diff * yidx) / yidx_max;
- g1 = g1_min + (g1_diff * yidx) / yidx_max;
- b1 = b1_min + (b1_diff * yidx) / yidx_max;
- r1_inv = r1_min + (r1_diff * (yidx_max-yidx)) / yidx_max;
- g1_inv = g1_min + (g1_diff * (yidx_max-yidx)) / yidx_max;
- b1_inv = b1_min + (b1_diff * (yidx_max-yidx)) / yidx_max;
- r2 = r2_min + (r2_diff * yidx) / yidx_max;
- g2 = g2_min + (g2_diff * yidx) / yidx_max;
- b2 = b2_min + (b2_diff * yidx) / yidx_max;
- r2_inv = r2_min + (r2_diff * (yidx_max-yidx)) / yidx_max;
- g2_inv = g2_min + (g2_diff * (yidx_max-yidx)) / yidx_max;
- b2_inv = b2_min + (b2_diff * (yidx_max-yidx)) / yidx_max;
- dest = bg_data + row*bg_rowbytes;
- for (i = 0; i < rpng2_info.width; ++i) {
- even_odd_horiz = (i / bgscale) & 1;
- even_odd = even_odd_vert ^ even_odd_horiz;
- invert_column =
- (even_odd_horiz && (bg[pat].type & 0x10));
- if (even_odd == 0) {
- if (invert_column) {
- *dest++ = r1_inv;
- *dest++ = g1_inv;
- *dest++ = b1_inv;
- } else {
- *dest++ = r1;
- *dest++ = g1;
- *dest++ = b1;
- }
- } else {
- if ((invert_column && invert_gradient2) ||
- (!invert_column && !invert_gradient2))
- {
- *dest++ = r2;
- *dest++ = g2;
- *dest++ = b2;
- } else {
- *dest++ = r2_inv;
- *dest++ = g2_inv;
- *dest++ = b2_inv;
- }
- }
- }
- }
- } else if ((bg[pat].type & 0x07) == 1) {
- hmax = (bgscale-1)/2;
- max = 2*hmax;
- r1 = rgb[bg[pat].rgb1_max].r;
- g1 = rgb[bg[pat].rgb1_max].g;
- b1 = rgb[bg[pat].rgb1_max].b;
- r2 = rgb[bg[pat].rgb2_max].r;
- g2 = rgb[bg[pat].rgb2_max].g;
- b2 = rgb[bg[pat].rgb2_max].b;
- for (row = 0; row < rpng2_info.height; ++row) {
- yidx = row % bgscale;
- if (yidx > hmax)
- yidx = bgscale-1 - yidx;
- dest = bg_data + row*bg_rowbytes;
- for (i = 0; i < rpng2_info.width; ++i) {
- xidx = i % bgscale;
- if (xidx > hmax)
- xidx = bgscale-1 - xidx;
- k = xidx + yidx;
- *dest++ = (k*r1 + (max-k)*r2) / max;
- *dest++ = (k*g1 + (max-k)*g2) / max;
- *dest++ = (k*b1 + (max-k)*b2) / max;
- }
- }
- } else if ((bg[pat].type & 0x07) == 2) {
- uch ch;
- int ii, x, y, hw, hh, grayspot;
- double freq, rotate, saturate, gray, intensity;
- double angle=0.0, aoffset=0.0, maxDist, dist;
- double red=0.0, green=0.0, blue=0.0, hue, s, v, f, p, q, t;
- fprintf(stderr, "%s: computing radial background...",
- PROGNAME);
- fflush(stderr);
- hh = rpng2_info.height / 2;
- hw = rpng2_info.width / 2;
-
- angle = CLIP(angle, 0.0, 360.0);
- grayspot = CLIP(bg[pat].bg_gray, 1, (hh + hw));
- freq = MAX((double)bg[pat].bg_freq, 0.0);
- saturate = (double)bg[pat].bg_bsat * 0.1;
- rotate = (double)bg[pat].bg_brot * 0.1;
- gray = 0.0;
- intensity = 0.0;
- maxDist = (double)((hw*hw) + (hh*hh));
- for (row = 0; row < rpng2_info.height; ++row) {
- y = row - hh;
- dest = bg_data + row*bg_rowbytes;
- for (i = 0; i < rpng2_info.width; ++i) {
- x = i - hw;
- angle = (x == 0)? PI_2 : atan((double)y / (double)x);
- gray = (double)MAX(ABS(y), ABS(x)) / grayspot;
- gray = MIN(1.0, gray);
- dist = (double)((x*x) + (y*y)) / maxDist;
- intensity = cos((angle+(rotate*dist*PI)) * freq) *
- gray * saturate;
- intensity = (MAX(MIN(intensity,1.0),-1.0) + 1.0) * 0.5;
- hue = (angle + PI) * INV_PI_360 + aoffset;
- s = gray * ((double)(ABS(x)+ABS(y)) / (double)(hw + hh));
- s = MIN(MAX(s,0.0), 1.0);
- v = MIN(MAX(intensity,0.0), 1.0);
- if (s == 0.0) {
- ch = (uch)(v * 255.0);
- *dest++ = ch;
- *dest++ = ch;
- *dest++ = ch;
- } else {
- if ((hue < 0.0) || (hue >= 360.0))
- hue -= (((int)(hue / 360.0)) * 360.0);
- hue /= 60.0;
- ii = (int)hue;
- f = hue - (double)ii;
- p = (1.0 - s) * v;
- q = (1.0 - (s * f)) * v;
- t = (1.0 - (s * (1.0 - f))) * v;
- if (ii == 0) { red = v; green = t; blue = p; }
- else if (ii == 1) { red = q; green = v; blue = p; }
- else if (ii == 2) { red = p; green = v; blue = t; }
- else if (ii == 3) { red = p; green = q; blue = v; }
- else if (ii == 4) { red = t; green = p; blue = v; }
- else if (ii == 5) { red = v; green = p; blue = q; }
- *dest++ = (uch)(red * 255.0);
- *dest++ = (uch)(green * 255.0);
- *dest++ = (uch)(blue * 255.0);
- }
- }
- }
- fprintf(stderr, "done.\n");
- fflush(stderr);
- }
- for (row = 0; row < rpng2_info.height; ++row) {
- src = bg_data + row*bg_rowbytes;
- dest = wimage_data + row*wimage_rowbytes;
- for (i = rpng2_info.width; i > 0; --i) {
- r1 = *src++;
- g1 = *src++;
- b1 = *src++;
- *dest++ = b1;
- *dest++ = g1;
- *dest++ = r1;
- }
- }
- return 0;
- }
- static void rpng2_win_display_row(ulg row)
- {
- uch bg_red = rpng2_info.bg_red;
- uch bg_green = rpng2_info.bg_green;
- uch bg_blue = rpng2_info.bg_blue;
- uch *src, *src2=NULL, *dest;
- uch r, g, b, a;
- ulg i;
- static int rows=0;
- static ulg firstrow;
- Trace((stderr, "beginning rpng2_win_display_row()\n"))
- if (rows == 0)
- firstrow = row;
- ++rows;
- src = rpng2_info.image_data + row*rpng2_info.rowbytes;
- if (bg_image)
- src2 = bg_data + row*bg_rowbytes;
- dest = wimage_data + row*wimage_rowbytes;
- if (rpng2_info.channels == 3) {
- for (i = rpng2_info.width; i > 0; --i) {
- r = *src++;
- g = *src++;
- b = *src++;
- *dest++ = b;
- *dest++ = g;
- *dest++ = r;
- }
- } else {
- for (i = rpng2_info.width; i > 0; --i) {
- r = *src++;
- g = *src++;
- b = *src++;
- a = *src++;
- if (bg_image) {
- bg_red = *src2++;
- bg_green = *src2++;
- bg_blue = *src2++;
- }
- if (a == 255) {
- *dest++ = b;
- *dest++ = g;
- *dest++ = r;
- } else if (a == 0) {
- *dest++ = bg_blue;
- *dest++ = bg_green;
- *dest++ = bg_red;
- } else {
-
- alpha_composite(*dest++, b, a, bg_blue);
- alpha_composite(*dest++, g, a, bg_green);
- alpha_composite(*dest++, r, a, bg_red);
- }
- }
- }
- if ((rows & 0xf) == 0 || row == rpng2_info.height-1) {
- RECT rect;
- rect.left = 0L;
- rect.top = (LONG)firstrow;
- rect.right = (LONG)rpng2_info.width;
- rect.bottom = (LONG)row + 1L;
- InvalidateRect(global_hwnd, &rect, FALSE);
- UpdateWindow(global_hwnd);
- rows = 0;
- }
- }
- static void rpng2_win_finish_display()
- {
- Trace((stderr, "beginning rpng2_win_finish_display()\n"))
-
- rpng2_info.state = kDone;
- printf(
- #ifndef __CYGWIN__
- "Done. Press Q, Esc or mouse button 1 (within image window) to quit.\n"
- #else
- "Done. Press mouse button 1 (within image window) to quit.\n"
- #endif
- );
- fflush(stdout);
- }
- static void rpng2_win_cleanup()
- {
- if (bg_image && bg_data) {
- free(bg_data);
- bg_data = NULL;
- }
- if (rpng2_info.image_data) {
- free(rpng2_info.image_data);
- rpng2_info.image_data = NULL;
- }
- if (rpng2_info.row_pointers) {
- free(rpng2_info.row_pointers);
- rpng2_info.row_pointers = NULL;
- }
- if (dib) {
- free(dib);
- dib = NULL;
- }
- }
- LRESULT CALLBACK rpng2_win_wndproc(HWND hwnd, UINT iMsg, WPARAM wP, LPARAM lP)
- {
- HDC hdc;
- PAINTSTRUCT ps;
- int rc;
- switch (iMsg) {
- case WM_CREATE:
-
- return 0;
- case WM_PAINT:
- hdc = BeginPaint(hwnd, &ps);
- rc = StretchDIBits(hdc, 0, 0, rpng2_info.width, rpng2_info.height,
- 0, 0, rpng2_info.width, rpng2_info.height,
- wimage_data, (BITMAPINFO *)bmih,
- 0, SRCCOPY);
- EndPaint(hwnd, &ps);
- return 0;
-
- case WM_CHAR:
- switch (wP) {
- case 'q':
- case 'Q':
- case 0x1B:
- PostQuitMessage(0);
- }
- return 0;
- case WM_LBUTTONDOWN:
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- }
- return DefWindowProc(hwnd, iMsg, wP, lP);
- }
|