34 |
|
|
35 |
|
#define FONTNAME "8x13" /* text font we'll use */ |
36 |
|
|
37 |
< |
#define CTRL(c) ('c'-'@') |
37 |
> |
#define CTRL(c) ((c)-'@') |
38 |
|
|
39 |
|
#define BORWIDTH 5 /* border width */ |
40 |
|
|
41 |
|
#define ICONSIZ (8*10) /* maximum icon dimension (even 8) */ |
42 |
|
|
43 |
|
#define ourscreen DefaultScreen(thedisplay) |
44 |
– |
#define ourblack BlackPixel(thedisplay,ourscreen) |
45 |
– |
#define ourwhite WhitePixel(thedisplay,ourscreen) |
44 |
|
#define ourroot RootWindow(thedisplay,ourscreen) |
47 |
– |
#define ourgc DefaultGC(thedisplay,ourscreen) |
45 |
|
|
46 |
|
#define revline(x0,y0,x1,y1) XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1) |
47 |
|
|
52 |
|
int dither = 1; /* dither colors? */ |
53 |
|
int fast = 0; /* keep picture in Pixmap? */ |
54 |
|
|
55 |
+ |
char *dispname = NULL; /* our display name */ |
56 |
+ |
|
57 |
|
Window wind = 0; /* our output window */ |
58 |
+ |
unsigned long ourblack=1, ourwhite=0; /* black and white for this visual */ |
59 |
|
Font fontid; /* our font */ |
60 |
|
|
61 |
|
int maxcolors = 0; /* maximum colors */ |
83 |
|
|
84 |
|
int wrongformat = 0; /* input in another format? */ |
85 |
|
|
86 |
+ |
GC ourgc; /* standard graphics context */ |
87 |
|
GC revgc; /* graphics context with GXinvert */ |
88 |
|
|
89 |
< |
XRASTER *ourras; /* our stored image */ |
89 |
> |
int *ourrank; /* our visual class ranking */ |
90 |
> |
XVisualInfo ourvis; /* our visual */ |
91 |
> |
XRASTER *ourras; /* our stored image */ |
92 |
|
unsigned char *ourdata; /* our image data */ |
93 |
|
|
94 |
|
struct { |
108 |
|
|
109 |
|
extern char *malloc(), *calloc(); |
110 |
|
|
111 |
< |
extern double atof(), pow(), log(); |
111 |
> |
extern double pow(), log(); |
112 |
|
|
113 |
|
Display *thedisplay; |
114 |
|
|
134 |
|
maxcolors = 2; |
135 |
|
break; |
136 |
|
case 'd': |
137 |
< |
dither = !dither; |
137 |
> |
if (argv[i][2] == 'i') |
138 |
> |
dispname = argv[++i]; |
139 |
> |
else |
140 |
> |
dither = !dither; |
141 |
|
break; |
142 |
|
case 'f': |
143 |
|
fast = !fast; |
148 |
|
scale = atoi(argv[++i]); |
149 |
|
break; |
150 |
|
case 'g': |
151 |
< |
if (!strcmp(argv[i], "-geometry")) |
151 |
> |
if (argv[i][2] == 'e') |
152 |
|
geometry = argv[++i]; |
153 |
|
else |
154 |
|
gamcor = atof(argv[++i]); |
165 |
|
fname = argv[i]; |
166 |
|
fin = fopen(fname, "r"); |
167 |
|
if (fin == NULL) { |
168 |
< |
sprintf(errmsg, "can't open file \"%s\"", fname); |
168 |
> |
sprintf(errmsg, "cannot open file \"%s\"", fname); |
169 |
|
quiterr(errmsg); |
170 |
|
} |
171 |
|
} else if (i != argc) |
189 |
|
getevent(); /* main loop */ |
190 |
|
userr: |
191 |
|
fprintf(stderr, |
192 |
< |
"Usage: %s [-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", |
192 |
> |
"Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", |
193 |
|
progname); |
194 |
|
quit(1); |
195 |
|
} |
198 |
|
headline(s) /* get relevant info from header */ |
199 |
|
char *s; |
200 |
|
{ |
195 |
– |
static char *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL}; |
196 |
– |
register char **an; |
201 |
|
char fmt[32]; |
202 |
|
|
203 |
|
if (isexpos(s)) |
205 |
|
else if (isformat(s)) { |
206 |
|
formatval(fmt, s); |
207 |
|
wrongformat = strcmp(fmt, COLRFMT); |
208 |
< |
} else |
209 |
< |
for (an = altname; *an != NULL; an++) |
206 |
< |
if (!strncmp(*an, s, strlen(*an))) { |
207 |
< |
if (sscanview(&ourview, s+strlen(*an)) > 0) |
208 |
< |
gotview++; |
209 |
< |
return; |
210 |
< |
} |
208 |
> |
} else if (isview(s) && sscanview(&ourview, s) > 0) |
209 |
> |
gotview++; |
210 |
|
} |
211 |
|
|
212 |
|
|
214 |
|
{ |
215 |
|
XWMHints ourxwmhints; |
216 |
|
XSetWindowAttributes ourwinattr; |
217 |
< |
XSizeHints oursizhints; |
218 |
< |
register int i; |
217 |
> |
XSizeHints oursizhints; |
218 |
> |
register int i; |
219 |
|
|
220 |
|
if (fname != NULL) { |
221 |
|
scanpos = (long *)malloc(ymax*sizeof(long)); |
224 |
|
for (i = 0; i < ymax; i++) |
225 |
|
scanpos[i] = -1; |
226 |
|
} |
227 |
< |
if ((thedisplay = XOpenDisplay(NULL)) == NULL) |
228 |
< |
quiterr("can't open display; DISPLAY variable set?"); |
229 |
< |
if (maxcolors == 0) { /* get number of available colors */ |
230 |
< |
i = DisplayPlanes(thedisplay,ourscreen); |
232 |
< |
maxcolors = i > 8 ? 256 : 1<<i; |
233 |
< |
if (maxcolors > 4) maxcolors -= 2; |
234 |
< |
} |
227 |
> |
if ((thedisplay = XOpenDisplay(dispname)) == NULL) |
228 |
> |
quiterr("cannot open display"); |
229 |
> |
/* get best visual for default screen */ |
230 |
> |
getbestvis(); |
231 |
|
/* store image */ |
232 |
|
getras(); |
233 |
|
/* open window */ |
234 |
|
ourwinattr.border_pixel = ourblack; |
235 |
|
ourwinattr.background_pixel = ourwhite; |
236 |
+ |
ourwinattr.colormap = XCreateColormap(thedisplay, ourroot, |
237 |
+ |
ourvis.visual, AllocNone); |
238 |
|
wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH, |
239 |
< |
0, InputOutput, ourras->visual, |
240 |
< |
CWBackPixel|CWBorderPixel, &ourwinattr); |
239 |
> |
ourvis.depth, InputOutput, ourvis.visual, |
240 |
> |
CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); |
241 |
|
if (wind == 0) |
242 |
< |
quiterr("can't create window"); |
242 |
> |
quiterr("cannot create window"); |
243 |
|
width = xmax; |
244 |
|
height = ymax; |
245 |
+ |
ourgc = XCreateGC(thedisplay, wind, 0, 0); |
246 |
+ |
XSetState(thedisplay, ourgc, ourblack, ourwhite, GXcopy, AllPlanes); |
247 |
+ |
revgc = XCreateGC(thedisplay, wind, 0, 0); |
248 |
+ |
XSetFunction(thedisplay, revgc, GXinvert); |
249 |
|
fontid = XLoadFont(thedisplay, FONTNAME); |
250 |
|
if (fontid == 0) |
251 |
< |
quiterr("can't get font"); |
251 |
> |
quiterr("cannot get font"); |
252 |
|
XSetFont(thedisplay, ourgc, fontid); |
251 |
– |
revgc = XCreateGC(thedisplay, wind, 0, 0); |
252 |
– |
XSetFunction(thedisplay, revgc, GXinvert); |
253 |
|
XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay, |
254 |
|
XC_diamond_cross)); |
255 |
|
XStoreName(thedisplay, wind, fname == NULL ? progname : fname); |
316 |
|
} |
317 |
|
|
318 |
|
|
319 |
+ |
static int |
320 |
+ |
viscmp(v1,v2) /* compare visual to see which is better, descending */ |
321 |
+ |
register XVisualInfo *v1, *v2; |
322 |
+ |
{ |
323 |
+ |
int bad1 = 0, bad2 = 0; |
324 |
+ |
register int *rp; |
325 |
+ |
|
326 |
+ |
if (v1->class == v2->class) { |
327 |
+ |
if (v1->class == TrueColor || v1->class == DirectColor) { |
328 |
+ |
/* prefer 24-bit to 32-bit */ |
329 |
+ |
if (v1->depth == 24 && v2->depth == 32) |
330 |
+ |
return(-1); |
331 |
+ |
if (v1->depth == 32 && v2->depth == 24) |
332 |
+ |
return(1); |
333 |
+ |
return(0); |
334 |
+ |
} |
335 |
+ |
/* don't be too greedy */ |
336 |
+ |
if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth) |
337 |
+ |
return(v1->depth - v2->depth); |
338 |
+ |
return(v2->depth - v1->depth); |
339 |
+ |
} |
340 |
+ |
/* prefer Pseudo when < 24-bit */ |
341 |
+ |
if ((v1->class == TrueColor || v1->class == DirectColor) && |
342 |
+ |
v1->depth < 24) |
343 |
+ |
bad1 = 1; |
344 |
+ |
if ((v2->class == TrueColor || v2->class == DirectColor) && |
345 |
+ |
v2->depth < 24) |
346 |
+ |
bad2 = -1; |
347 |
+ |
if (bad1 | bad2) |
348 |
+ |
return(bad1+bad2); |
349 |
+ |
/* otherwise, use class ranking */ |
350 |
+ |
for (rp = ourrank; *rp != -1; rp++) { |
351 |
+ |
if (v1->class == *rp) |
352 |
+ |
return(-1); |
353 |
+ |
if (v2->class == *rp) |
354 |
+ |
return(1); |
355 |
+ |
} |
356 |
+ |
return(0); |
357 |
+ |
} |
358 |
+ |
|
359 |
+ |
|
360 |
+ |
getbestvis() /* get the best visual for this screen */ |
361 |
+ |
{ |
362 |
+ |
#ifdef DEBUG |
363 |
+ |
static char vistype[][12] = { |
364 |
+ |
"StaticGray", |
365 |
+ |
"GrayScale", |
366 |
+ |
"StaticColor", |
367 |
+ |
"PseudoColor", |
368 |
+ |
"TrueColor", |
369 |
+ |
"DirectColor" |
370 |
+ |
}; |
371 |
+ |
#endif |
372 |
+ |
static int rankings[3][6] = { |
373 |
+ |
{TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1}, |
374 |
+ |
{PseudoColor,GrayScale,StaticGray,-1}, |
375 |
+ |
{PseudoColor,GrayScale,StaticGray,-1} |
376 |
+ |
}; |
377 |
+ |
XVisualInfo *xvi; |
378 |
+ |
int vismatched; |
379 |
+ |
register int i, j; |
380 |
+ |
|
381 |
+ |
if (greyscale) { |
382 |
+ |
ourrank = rankings[2]; |
383 |
+ |
if (maxcolors < 2) maxcolors = 256; |
384 |
+ |
} else if (maxcolors >= 2 && maxcolors <= 256) |
385 |
+ |
ourrank = rankings[1]; |
386 |
+ |
else { |
387 |
+ |
ourrank = rankings[0]; |
388 |
+ |
maxcolors = 256; |
389 |
+ |
} |
390 |
+ |
/* find best visual */ |
391 |
+ |
ourvis.screen = ourscreen; |
392 |
+ |
xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched); |
393 |
+ |
if (xvi == NULL) |
394 |
+ |
quiterr("no visuals for this screen!"); |
395 |
+ |
#ifdef DEBUG |
396 |
+ |
fprintf(stderr, "Supported visuals:\n"); |
397 |
+ |
for (i = 0; i < vismatched; i++) |
398 |
+ |
fprintf(stderr, "\ttype %s, depth %d\n", |
399 |
+ |
vistype[xvi[i].class], xvi[i].depth); |
400 |
+ |
#endif |
401 |
+ |
for (i = 0, j = 1; j < vismatched; j++) |
402 |
+ |
if (viscmp(&xvi[i],&xvi[j]) > 0) |
403 |
+ |
i = j; |
404 |
+ |
/* compare to least acceptable */ |
405 |
+ |
for (j = 0; ourrank[j++] != -1; ) |
406 |
+ |
; |
407 |
+ |
ourvis.class = ourrank[--j]; |
408 |
+ |
ourvis.depth = 1; |
409 |
+ |
if (viscmp(&xvi[i],&ourvis) > 0) |
410 |
+ |
quiterr("inadequate visuals on this screen"); |
411 |
+ |
/* OK, we'll use it */ |
412 |
+ |
copystruct(&ourvis, &xvi[i]); |
413 |
+ |
#ifdef DEBUG |
414 |
+ |
fprintf(stderr, "Selected visual type %s, depth %d\n", |
415 |
+ |
vistype[ourvis.class], ourvis.depth); |
416 |
+ |
#endif |
417 |
+ |
/* make appropriate adjustments */ |
418 |
+ |
if (ourvis.class == GrayScale || ourvis.class == StaticGray) |
419 |
+ |
greyscale = 1; |
420 |
+ |
if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors) |
421 |
+ |
maxcolors = ourvis.colormap_size; |
422 |
+ |
if (maxcolors > 4) |
423 |
+ |
maxcolors -= 2; |
424 |
+ |
if (ourvis.class == StaticGray) { |
425 |
+ |
ourblack = 0; |
426 |
+ |
ourwhite = 255; |
427 |
+ |
} else if (ourvis.class == PseudoColor) { |
428 |
+ |
ourblack = BlackPixel(thedisplay,ourscreen); |
429 |
+ |
ourwhite = WhitePixel(thedisplay,ourscreen); |
430 |
+ |
} else { |
431 |
+ |
ourblack = 0; |
432 |
+ |
ourwhite = ~0; |
433 |
+ |
} |
434 |
+ |
XFree((char *)xvi); |
435 |
+ |
} |
436 |
+ |
|
437 |
+ |
|
438 |
|
getras() /* get raster file */ |
439 |
|
{ |
440 |
|
colormap ourmap; |
444 |
|
ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8)); |
445 |
|
if (ourdata == NULL) |
446 |
|
goto fail; |
447 |
< |
ourras = make_raster(thedisplay, ourscreen, 1, ourdata, |
447 |
> |
ourras = make_raster(thedisplay, &ourvis, 1, ourdata, |
448 |
|
xmax, ymax, 8); |
449 |
|
if (ourras == NULL) |
450 |
|
goto fail; |
451 |
|
getmono(); |
452 |
< |
} else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo) |
453 |
< |
/* kludge for DirectColor */ |
335 |
< |
|| XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) { |
336 |
< |
ourdata = (unsigned char *)malloc(xmax*ymax*3); |
452 |
> |
} else if (ourvis.class == TrueColor || ourvis.class == DirectColor) { |
453 |
> |
ourdata = (unsigned char *)malloc(4*xmax*ymax); |
454 |
|
if (ourdata == NULL) |
455 |
|
goto fail; |
456 |
< |
ourras = make_raster(thedisplay, ourscreen, 24, ourdata, |
457 |
< |
xmax, ymax, 8); |
456 |
> |
ourras = make_raster(thedisplay, &ourvis, 32, |
457 |
> |
ourdata, xmax, ymax, 32); |
458 |
|
if (ourras == NULL) |
459 |
|
goto fail; |
460 |
|
getfull(); |
462 |
|
ourdata = (unsigned char *)malloc(xmax*ymax); |
463 |
|
if (ourdata == NULL) |
464 |
|
goto fail; |
465 |
< |
ourras = make_raster(thedisplay, ourscreen, 8, ourdata, |
465 |
> |
ourras = make_raster(thedisplay, &ourvis, 8, ourdata, |
466 |
|
xmax, ymax, 8); |
467 |
|
if (ourras == NULL) |
468 |
|
goto fail; |
469 |
< |
if (greyscale) |
470 |
< |
biq(dither,maxcolors,1,ourmap); |
471 |
< |
else |
472 |
< |
ciq(dither,maxcolors,1,ourmap); |
473 |
< |
if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0) |
474 |
< |
goto fail; |
469 |
> |
if (ourvis.class == StaticGray) |
470 |
> |
getgrey(); |
471 |
> |
else { |
472 |
> |
if (greyscale) |
473 |
> |
biq(dither,maxcolors,1,ourmap); |
474 |
> |
else |
475 |
> |
ciq(dither,maxcolors,1,ourmap); |
476 |
> |
if (init_rcolors(ourras, ourmap[0], |
477 |
> |
ourmap[1], ourmap[2]) == 0) |
478 |
> |
goto fail; |
479 |
> |
} |
480 |
|
} |
481 |
< |
return; |
481 |
> |
return; |
482 |
|
fail: |
483 |
|
quiterr("could not create raster image"); |
484 |
|
} |
506 |
|
case MapNotify: |
507 |
|
map_rcolors(ourras, wind); |
508 |
|
if (fast) |
509 |
< |
make_rpixmap(ourras); |
509 |
> |
make_rpixmap(ourras, wind); |
510 |
|
break; |
511 |
|
case UnmapNotify: |
512 |
< |
unmap_rcolors(ourras); |
512 |
> |
if (!fast) |
513 |
> |
unmap_rcolors(ourras); |
514 |
|
break; |
515 |
|
case Expose: |
516 |
|
redraw(e.e.x, e.e.y, e.e.width, e.e.height); |
542 |
|
com = buf[0]; |
543 |
|
switch (com) { /* interpret command */ |
544 |
|
case 'q': |
545 |
< |
case CTRL(D): /* quit */ |
545 |
> |
case CTRL('D'): /* quit */ |
546 |
|
quit(0); |
547 |
|
case '\n': |
548 |
|
case '\r': |
616 |
|
free_raster(ourras); |
617 |
|
getras(); |
618 |
|
/* fall through */ |
619 |
< |
case CTRL(R): /* redraw */ |
620 |
< |
case CTRL(L): |
619 |
> |
case CTRL('R'): /* redraw */ |
620 |
> |
case CTRL('L'): |
621 |
|
unmap_rcolors(ourras); |
622 |
|
XClearWindow(thedisplay, wind); |
623 |
|
map_rcolors(ourras, wind); |
839 |
|
getfull() /* get full (24-bit) data */ |
840 |
|
{ |
841 |
|
int y; |
842 |
+ |
register unsigned long *dp; |
843 |
+ |
register int x; |
844 |
+ |
/* set gamma correction */ |
845 |
+ |
setcolrgam(gamcor); |
846 |
+ |
/* read and convert file */ |
847 |
+ |
dp = (unsigned long *)ourdata; |
848 |
+ |
for (y = 0; y < ymax; y++) { |
849 |
+ |
if (getscan(y) < 0) |
850 |
+ |
quiterr("seek error in getfull"); |
851 |
+ |
if (scale) |
852 |
+ |
shiftcolrs(scanline, xmax, scale); |
853 |
+ |
colrs_gambs(scanline, xmax); |
854 |
+ |
add2icon(y, scanline); |
855 |
+ |
if (ourras->image->blue_mask & 1) |
856 |
+ |
for (x = 0; x < xmax; x++) |
857 |
+ |
*dp++ = scanline[x][RED] << 16 | |
858 |
+ |
scanline[x][GRN] << 8 | |
859 |
+ |
scanline[x][BLU] ; |
860 |
+ |
else |
861 |
+ |
for (x = 0; x < xmax; x++) |
862 |
+ |
*dp++ = scanline[x][RED] | |
863 |
+ |
scanline[x][GRN] << 8 | |
864 |
+ |
scanline[x][BLU] << 16 ; |
865 |
+ |
} |
866 |
+ |
} |
867 |
+ |
|
868 |
+ |
|
869 |
+ |
getgrey() /* get greyscale data */ |
870 |
+ |
{ |
871 |
+ |
int y; |
872 |
|
register unsigned char *dp; |
873 |
|
register int x; |
874 |
|
/* set gamma correction */ |
882 |
|
shiftcolrs(scanline, xmax, scale); |
883 |
|
colrs_gambs(scanline, xmax); |
884 |
|
add2icon(y, scanline); |
885 |
< |
for (x = 0; x < xmax; x++) { |
886 |
< |
*dp++ = scanline[x][RED]; |
887 |
< |
*dp++ = scanline[x][GRN]; |
888 |
< |
*dp++ = scanline[x][BLU]; |
889 |
< |
} |
885 |
> |
if (ourvis.colormap_size < 256) |
886 |
> |
for (x = 0; x < xmax; x++) |
887 |
> |
*dp++ = ((long)normbright(scanline[x]) * |
888 |
> |
ourvis.colormap_size + 128) >> 8; |
889 |
> |
else |
890 |
> |
for (x = 0; x < xmax; x++) |
891 |
> |
*dp++ = normbright(scanline[x]); |
892 |
|
} |
893 |
|
} |
894 |
|
|
984 |
|
picwritecm(map) /* handled elsewhere */ |
985 |
|
colormap map; |
986 |
|
{ |
987 |
< |
#ifdef DEBUG |
987 |
> |
#if 0 |
988 |
|
register int i; |
989 |
|
|
990 |
|
for (i = 0; i < 256; i++) |