| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
#define PROGNAME "rpng-win" |
| 54 |
#define LONGNAME "Simple PNG Viewer for Windows" |
| 55 |
#define VERSION "1.20 of 28 May 2001" |
| 56 |
|
| 57 |
#include <stdio.h> |
| 58 |
#include <stdlib.h> |
| 59 |
#include <string.h> |
| 60 |
#include <time.h> |
| 61 |
#include <windows.h> |
| 62 |
#include <conio.h> |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
#include "readpng.h" |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
#define alpha_composite(composite, fg, alpha, bg) { \ |
| 75 |
ush temp = ((ush)(fg)*(ush)(alpha) + \ |
| 76 |
(ush)(bg)*(ush)(255 - (ush)(alpha)) + (ush)128); \ |
| 77 |
(composite) = (uch)((temp + (temp >> 8)) >> 8); \ |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
static int rpng_win_create_window(HINSTANCE hInst, int showmode); |
| 83 |
static int rpng_win_display_image(void); |
| 84 |
static void rpng_win_cleanup(void); |
| 85 |
LRESULT CALLBACK rpng_win_wndproc(HWND, UINT, WPARAM, LPARAM); |
| 86 |
|
| 87 |
|
| 88 |
static char titlebar[1024], *window_name = titlebar; |
| 89 |
static char *progname = PROGNAME; |
| 90 |
static char *appname = LONGNAME; |
| 91 |
static char *icon_name = PROGNAME; |
| 92 |
static char *filename; |
| 93 |
static FILE *infile; |
| 94 |
|
| 95 |
static char *bgstr; |
| 96 |
static uch bg_red=0, bg_green=0, bg_blue=0; |
| 97 |
|
| 98 |
static double display_exponent; |
| 99 |
|
| 100 |
static ulg image_width, image_height, image_rowbytes; |
| 101 |
static int image_channels; |
| 102 |
static uch *image_data; |
| 103 |
|
| 104 |
|
| 105 |
static ulg wimage_rowbytes; |
| 106 |
static uch *dib; |
| 107 |
static uch *wimage_data; |
| 108 |
static BITMAPINFOHEADER *bmih; |
| 109 |
|
| 110 |
static HWND global_hwnd; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, PSTR cmd, int showmode) |
| 116 |
{ |
| 117 |
char *args[1024]; |
| 118 |
char *p, *q, **argv = args; |
| 119 |
int argc = 0; |
| 120 |
int rc, alen, flen; |
| 121 |
int error = 0; |
| 122 |
int have_bg = FALSE; |
| 123 |
double LUT_exponent; |
| 124 |
double CRT_exponent = 2.2; |
| 125 |
double default_display_exponent; |
| 126 |
MSG msg; |
| 127 |
|
| 128 |
|
| 129 |
filename = (char *)NULL; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
AllocConsole(); |
| 138 |
freopen("CONOUT$", "a", stderr); |
| 139 |
freopen("CONOUT$", "a", stdout); |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
#if defined(NeXT) |
| 150 |
LUT_exponent = 1.0 / 2.2; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
#elif defined(sgi) |
| 156 |
LUT_exponent = 1.0 / 1.7; |
| 157 |
|
| 158 |
|
| 159 |
infile = fopen("/etc/config/system.glGammaVal", "r"); |
| 160 |
if (infile) { |
| 161 |
double sgi_gamma; |
| 162 |
|
| 163 |
fgets(tmpline, 80, infile); |
| 164 |
fclose(infile); |
| 165 |
sgi_gamma = atof(tmpline); |
| 166 |
if (sgi_gamma > 0.0) |
| 167 |
LUT_exponent = 1.0 / sgi_gamma; |
| 168 |
} |
| 169 |
#elif defined(Macintosh) |
| 170 |
LUT_exponent = 1.8 / 2.61; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
#else |
| 176 |
LUT_exponent = 1.0; |
| 177 |
#endif |
| 178 |
|
| 179 |
|
| 180 |
default_display_exponent = LUT_exponent * CRT_exponent; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
if ((p = getenv("SCREEN_GAMMA")) != NULL) |
| 189 |
display_exponent = atof(p); |
| 190 |
else |
| 191 |
display_exponent = default_display_exponent; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
argv[argc++] = PROGNAME; |
| 199 |
p = cmd; |
| 200 |
for (;;) { |
| 201 |
if (*p == ' ') |
| 202 |
while (*++p == ' ') |
| 203 |
; |
| 204 |
|
| 205 |
if (*p == '\0') |
| 206 |
break; |
| 207 |
argv[argc++] = q = p; |
| 208 |
while (*q && *q != ' ') |
| 209 |
++q; |
| 210 |
|
| 211 |
if (*q == '\0') |
| 212 |
break; |
| 213 |
*q = '\0'; |
| 214 |
p = q + 1; |
| 215 |
} |
| 216 |
argv[argc] = NULL; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
while (*++argv && !error) { |
| 222 |
if (!strncmp(*argv, "-gamma", 2)) { |
| 223 |
if (!*++argv) |
| 224 |
++error; |
| 225 |
else { |
| 226 |
display_exponent = atof(*argv); |
| 227 |
if (display_exponent <= 0.0) |
| 228 |
++error; |
| 229 |
} |
| 230 |
} else if (!strncmp(*argv, "-bgcolor", 2)) { |
| 231 |
if (!*++argv) |
| 232 |
++error; |
| 233 |
else { |
| 234 |
bgstr = *argv; |
| 235 |
if (strlen(bgstr) != 7 || bgstr[0] != '#') |
| 236 |
++error; |
| 237 |
else |
| 238 |
have_bg = TRUE; |
| 239 |
} |
| 240 |
} else { |
| 241 |
if (**argv != '-') { |
| 242 |
filename = *argv; |
| 243 |
if (argv[1]) |
| 244 |
++error; |
| 245 |
} else |
| 246 |
++error; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
if (!filename) { |
| 251 |
++error; |
| 252 |
} else if (!(infile = fopen(filename, "rb"))) { |
| 253 |
fprintf(stderr, PROGNAME ": can't open PNG file [%s]\n", filename); |
| 254 |
++error; |
| 255 |
} else { |
| 256 |
if ((rc = readpng_init(infile, &image_width, &image_height)) != 0) { |
| 257 |
switch (rc) { |
| 258 |
case 1: |
| 259 |
fprintf(stderr, PROGNAME |
| 260 |
": [%s] is not a PNG file: incorrect signature\n", |
| 261 |
filename); |
| 262 |
break; |
| 263 |
case 2: |
| 264 |
fprintf(stderr, PROGNAME |
| 265 |
": [%s] has bad IHDR (libpng longjmp)\n", |
| 266 |
filename); |
| 267 |
break; |
| 268 |
case 4: |
| 269 |
fprintf(stderr, PROGNAME ": insufficient memory\n"); |
| 270 |
break; |
| 271 |
default: |
| 272 |
fprintf(stderr, PROGNAME |
| 273 |
": unknown readpng_init() error\n"); |
| 274 |
break; |
| 275 |
} |
| 276 |
++error; |
| 277 |
} |
| 278 |
if (error) |
| 279 |
fclose(infile); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
if (error) { |
| 286 |
int ch; |
| 287 |
|
| 288 |
fprintf(stderr, "\n%s %s: %s\n\n", PROGNAME, VERSION, appname); |
| 289 |
readpng_version_info(); |
| 290 |
fprintf(stderr, "\n" |
| 291 |
"Usage: %s [-gamma exp] [-bgcolor bg] file.png\n" |
| 292 |
" exp \ttransfer-function exponent (``gamma'') of the display\n" |
| 293 |
"\t\t system in floating-point format (e.g., ``%.1f''); equal\n" |
| 294 |
"\t\t to the product of the lookup-table exponent (varies)\n" |
| 295 |
"\t\t and the CRT exponent (usually 2.2); must be positive\n" |
| 296 |
" bg \tdesired background color in 7-character hex RGB format\n" |
| 297 |
"\t\t (e.g., ``#ff7700'' for orange: same as HTML colors);\n" |
| 298 |
"\t\t used with transparent images\n" |
| 299 |
"\nPress Q, Esc or mouse button 1 after image is displayed to quit.\n" |
| 300 |
"Press Q or Esc to quit this usage screen.\n" |
| 301 |
"\n", PROGNAME, default_display_exponent); |
| 302 |
do |
| 303 |
ch = _getch(); |
| 304 |
while (ch != 'q' && ch != 'Q' && ch != 0x1B); |
| 305 |
exit(1); |
| 306 |
} else { |
| 307 |
fprintf(stderr, "\n%s %s: %s\n", PROGNAME, VERSION, appname); |
| 308 |
fprintf(stderr, |
| 309 |
"\n [console window: closing this window will terminate %s]\n\n", |
| 310 |
PROGNAME); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
alen = strlen(appname); |
| 317 |
flen = strlen(filename); |
| 318 |
if (alen + flen + 3 > 1023) |
| 319 |
sprintf(titlebar, "%s: ...%s", appname, filename+(alen+flen+6-1023)); |
| 320 |
else |
| 321 |
sprintf(titlebar, "%s: %s", appname, filename); |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
if (have_bg) |
| 329 |
sscanf(bgstr+1, "%2x%2x%2x", &bg_red, &bg_green, &bg_blue); |
| 330 |
else if (readpng_get_bgcolor(&bg_red, &bg_green, &bg_blue) > 1) { |
| 331 |
readpng_cleanup(TRUE); |
| 332 |
fprintf(stderr, PROGNAME |
| 333 |
": libpng error while checking for background color\n"); |
| 334 |
exit(2); |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
if (rpng_win_create_window(hInst, showmode)) |
| 342 |
exit(2); |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
Trace((stderr, "calling readpng_get_image()\n")) |
| 348 |
image_data = readpng_get_image(display_exponent, &image_channels, |
| 349 |
&image_rowbytes); |
| 350 |
Trace((stderr, "done with readpng_get_image()\n")) |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
readpng_cleanup(FALSE); |
| 357 |
fclose(infile); |
| 358 |
|
| 359 |
if (!image_data) { |
| 360 |
fprintf(stderr, PROGNAME ": unable to decode PNG image\n"); |
| 361 |
exit(3); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
Trace((stderr, "calling rpng_win_display_image()\n")) |
| 368 |
if (rpng_win_display_image()) { |
| 369 |
free(image_data); |
| 370 |
exit(4); |
| 371 |
} |
| 372 |
Trace((stderr, "done with rpng_win_display_image()\n")) |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
printf( |
| 378 |
"Done. Press Q, Esc or mouse button 1 (within image window) to quit.\n"); |
| 379 |
fflush(stdout); |
| 380 |
|
| 381 |
while (GetMessage(&msg, NULL, 0, 0)) { |
| 382 |
TranslateMessage(&msg); |
| 383 |
DispatchMessage(&msg); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
rpng_win_cleanup(); |
| 390 |
|
| 391 |
return msg.wParam; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
static int rpng_win_create_window(HINSTANCE hInst, int showmode) |
| 399 |
{ |
| 400 |
uch *dest; |
| 401 |
int extra_width, extra_height; |
| 402 |
ulg i, j; |
| 403 |
WNDCLASSEX wndclass; |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
wimage_rowbytes = ((3*image_width + 3L) >> 2) << 2; |
| 412 |
|
| 413 |
if (!(dib = (uch *)malloc(sizeof(BITMAPINFOHEADER) + |
| 414 |
wimage_rowbytes*image_height))) |
| 415 |
{ |
| 416 |
return 4; |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
memset(dib, 0, sizeof(BITMAPINFOHEADER)); |
| 427 |
bmih = (BITMAPINFOHEADER *)dib; |
| 428 |
bmih->biSize = sizeof(BITMAPINFOHEADER); |
| 429 |
bmih->biWidth = image_width; |
| 430 |
bmih->biHeight = -((long)image_height); |
| 431 |
bmih->biPlanes = 1; |
| 432 |
bmih->biBitCount = 24; |
| 433 |
bmih->biCompression = 0; |
| 434 |
wimage_data = dib + sizeof(BITMAPINFOHEADER); |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
for (j = 0; j < image_height; ++j) { |
| 441 |
dest = wimage_data + j*wimage_rowbytes; |
| 442 |
for (i = image_width; i > 0; --i) { |
| 443 |
*dest++ = bg_blue; |
| 444 |
*dest++ = bg_green; |
| 445 |
*dest++ = bg_red; |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
memset(&wndclass, 0, sizeof(wndclass)); |
| 454 |
|
| 455 |
wndclass.cbSize = sizeof(wndclass); |
| 456 |
wndclass.style = CS_HREDRAW | CS_VREDRAW; |
| 457 |
wndclass.lpfnWndProc = rpng_win_wndproc; |
| 458 |
wndclass.hInstance = hInst; |
| 459 |
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); |
| 460 |
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); |
| 461 |
wndclass.hbrBackground = (HBRUSH)GetStockObject(DKGRAY_BRUSH); |
| 462 |
wndclass.lpszMenuName = NULL; |
| 463 |
wndclass.lpszClassName = progname; |
| 464 |
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); |
| 465 |
|
| 466 |
RegisterClassEx(&wndclass); |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
extra_width = 2*(GetSystemMetrics(SM_CXBORDER) + |
| 473 |
GetSystemMetrics(SM_CXDLGFRAME)); |
| 474 |
extra_height = 2*(GetSystemMetrics(SM_CYBORDER) + |
| 475 |
GetSystemMetrics(SM_CYDLGFRAME)) + |
| 476 |
GetSystemMetrics(SM_CYCAPTION); |
| 477 |
|
| 478 |
global_hwnd = CreateWindow(progname, titlebar, WS_OVERLAPPEDWINDOW, |
| 479 |
CW_USEDEFAULT, CW_USEDEFAULT, image_width+extra_width, |
| 480 |
image_height+extra_height, NULL, NULL, hInst, NULL); |
| 481 |
|
| 482 |
ShowWindow(global_hwnd, showmode); |
| 483 |
UpdateWindow(global_hwnd); |
| 484 |
|
| 485 |
return 0; |
| 486 |
|
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
static int rpng_win_display_image() |
| 494 |
{ |
| 495 |
uch *src, *dest; |
| 496 |
uch r, g, b, a; |
| 497 |
ulg i, row, lastrow; |
| 498 |
RECT rect; |
| 499 |
|
| 500 |
|
| 501 |
Trace((stderr, "beginning display loop (image_channels == %d)\n", |
| 502 |
image_channels)) |
| 503 |
Trace((stderr, "(width = %ld, rowbytes = %ld, wimage_rowbytes = %d)\n", |
| 504 |
image_width, image_rowbytes, wimage_rowbytes)) |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
for (lastrow = row = 0; row < image_height; ++row) { |
| 514 |
src = image_data + row*image_rowbytes; |
| 515 |
dest = wimage_data + row*wimage_rowbytes; |
| 516 |
if (image_channels == 3) { |
| 517 |
for (i = image_width; i > 0; --i) { |
| 518 |
r = *src++; |
| 519 |
g = *src++; |
| 520 |
b = *src++; |
| 521 |
*dest++ = b; |
| 522 |
*dest++ = g; |
| 523 |
*dest++ = r; |
| 524 |
} |
| 525 |
} else { |
| 526 |
for (i = image_width; i > 0; --i) { |
| 527 |
r = *src++; |
| 528 |
g = *src++; |
| 529 |
b = *src++; |
| 530 |
a = *src++; |
| 531 |
if (a == 255) { |
| 532 |
*dest++ = b; |
| 533 |
*dest++ = g; |
| 534 |
*dest++ = r; |
| 535 |
} else if (a == 0) { |
| 536 |
*dest++ = bg_blue; |
| 537 |
*dest++ = bg_green; |
| 538 |
*dest++ = bg_red; |
| 539 |
} else { |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
alpha_composite(*dest++, b, a, bg_blue); |
| 545 |
alpha_composite(*dest++, g, a, bg_green); |
| 546 |
alpha_composite(*dest++, r, a, bg_red); |
| 547 |
} |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
if (((row+1) & 0xf) == 0) { |
| 552 |
rect.left = 0L; |
| 553 |
rect.top = (LONG)lastrow; |
| 554 |
rect.right = (LONG)image_width; |
| 555 |
rect.bottom = (LONG)lastrow + 16L; |
| 556 |
InvalidateRect(global_hwnd, &rect, FALSE); |
| 557 |
UpdateWindow(global_hwnd); |
| 558 |
lastrow = row + 1; |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
Trace((stderr, "calling final image-flush routine\n")) |
| 563 |
if (lastrow < image_height) { |
| 564 |
rect.left = 0L; |
| 565 |
rect.top = (LONG)lastrow; |
| 566 |
rect.right = (LONG)image_width; |
| 567 |
rect.bottom = (LONG)image_height; |
| 568 |
InvalidateRect(global_hwnd, &rect, FALSE); |
| 569 |
UpdateWindow(global_hwnd); |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
return 0; |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
static void rpng_win_cleanup() |
| 586 |
{ |
| 587 |
if (image_data) { |
| 588 |
free(image_data); |
| 589 |
image_data = NULL; |
| 590 |
} |
| 591 |
|
| 592 |
if (dib) { |
| 593 |
free(dib); |
| 594 |
dib = NULL; |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
LRESULT CALLBACK rpng_win_wndproc(HWND hwnd, UINT iMsg, WPARAM wP, LPARAM lP) |
| 603 |
{ |
| 604 |
HDC hdc; |
| 605 |
PAINTSTRUCT ps; |
| 606 |
int rc; |
| 607 |
|
| 608 |
switch (iMsg) { |
| 609 |
case WM_CREATE: |
| 610 |
|
| 611 |
return 0; |
| 612 |
|
| 613 |
case WM_PAINT: |
| 614 |
hdc = BeginPaint(hwnd, &ps); |
| 615 |
|
| 616 |
rc = StretchDIBits(hdc, 0, 0, image_width, image_height, |
| 617 |
|
| 618 |
0, 0, image_width, image_height, |
| 619 |
wimage_data, (BITMAPINFO *)bmih, |
| 620 |
|
| 621 |
0, SRCCOPY); |
| 622 |
EndPaint(hwnd, &ps); |
| 623 |
return 0; |
| 624 |
|
| 625 |
|
| 626 |
case WM_CHAR: |
| 627 |
switch (wP) { |
| 628 |
case 'q': |
| 629 |
case 'Q': |
| 630 |
case 0x1B: |
| 631 |
PostQuitMessage(0); |
| 632 |
} |
| 633 |
return 0; |
| 634 |
|
| 635 |
case WM_LBUTTONDOWN: |
| 636 |
case WM_DESTROY: |
| 637 |
PostQuitMessage(0); |
| 638 |
return 0; |
| 639 |
} |
| 640 |
|
| 641 |
return DefWindowProc(hwnd, iMsg, wP, lP); |
| 642 |
} |