1 |
< |
/* Copyright (c) 1991 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
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=0, ourwhite=1; /* 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 |
|
|
116 |
|
int argc; |
117 |
|
char *argv[]; |
118 |
|
{ |
119 |
+ |
extern char *getenv(); |
120 |
+ |
char *gv; |
121 |
|
int headline(); |
122 |
|
int i; |
123 |
|
|
124 |
|
progname = argv[0]; |
125 |
+ |
if ((gv = getenv("GAMMA")) != NULL) |
126 |
+ |
gamcor = atof(gv); |
127 |
|
|
128 |
|
for (i = 1; i < argc; i++) |
129 |
|
if (argv[i][0] == '-') |
138 |
|
maxcolors = 2; |
139 |
|
break; |
140 |
|
case 'd': |
141 |
< |
dither = !dither; |
141 |
> |
if (argv[i][2] == 'i') |
142 |
> |
dispname = argv[++i]; |
143 |
> |
else |
144 |
> |
dither = !dither; |
145 |
|
break; |
146 |
|
case 'f': |
147 |
|
fast = !fast; |
152 |
|
scale = atoi(argv[++i]); |
153 |
|
break; |
154 |
|
case 'g': |
155 |
< |
if (!strcmp(argv[i], "-geometry")) |
155 |
> |
if (argv[i][2] == 'e') |
156 |
|
geometry = argv[++i]; |
157 |
|
else |
158 |
|
gamcor = atof(argv[++i]); |
169 |
|
fname = argv[i]; |
170 |
|
fin = fopen(fname, "r"); |
171 |
|
if (fin == NULL) { |
172 |
< |
sprintf(errmsg, "can't open file \"%s\"", fname); |
172 |
> |
sprintf(errmsg, "cannot open file \"%s\"", fname); |
173 |
|
quiterr(errmsg); |
174 |
|
} |
175 |
|
} else if (i != argc) |
193 |
|
getevent(); /* main loop */ |
194 |
|
userr: |
195 |
|
fprintf(stderr, |
196 |
< |
"Usage: %s [-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", |
196 |
> |
"Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", |
197 |
|
progname); |
198 |
|
quit(1); |
199 |
|
} |
202 |
|
headline(s) /* get relevant info from header */ |
203 |
|
char *s; |
204 |
|
{ |
195 |
– |
static char *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL}; |
196 |
– |
register char **an; |
205 |
|
char fmt[32]; |
206 |
|
|
207 |
|
if (isexpos(s)) |
209 |
|
else if (isformat(s)) { |
210 |
|
formatval(fmt, s); |
211 |
|
wrongformat = strcmp(fmt, COLRFMT); |
212 |
< |
} else |
213 |
< |
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 |
< |
} |
212 |
> |
} else if (isview(s) && sscanview(&ourview, s) > 0) |
213 |
> |
gotview++; |
214 |
|
} |
215 |
|
|
216 |
|
|
218 |
|
{ |
219 |
|
XWMHints ourxwmhints; |
220 |
|
XSetWindowAttributes ourwinattr; |
221 |
< |
XSizeHints oursizhints; |
222 |
< |
register int i; |
221 |
> |
XSizeHints oursizhints; |
222 |
> |
register int i; |
223 |
|
|
224 |
|
if (fname != NULL) { |
225 |
|
scanpos = (long *)malloc(ymax*sizeof(long)); |
228 |
|
for (i = 0; i < ymax; i++) |
229 |
|
scanpos[i] = -1; |
230 |
|
} |
231 |
< |
if ((thedisplay = XOpenDisplay(NULL)) == NULL) |
232 |
< |
quiterr("can't open display; DISPLAY variable set?"); |
233 |
< |
if (maxcolors == 0) { /* get number of available colors */ |
234 |
< |
i = DisplayPlanes(thedisplay,ourscreen); |
232 |
< |
maxcolors = i > 8 ? 256 : 1<<i; |
233 |
< |
if (maxcolors > 4) maxcolors -= 2; |
234 |
< |
} |
231 |
> |
if ((thedisplay = XOpenDisplay(dispname)) == NULL) |
232 |
> |
quiterr("cannot open display"); |
233 |
> |
/* get best visual for default screen */ |
234 |
> |
getbestvis(); |
235 |
|
/* store image */ |
236 |
|
getras(); |
237 |
|
/* open window */ |
238 |
|
ourwinattr.border_pixel = ourblack; |
239 |
|
ourwinattr.background_pixel = ourwhite; |
240 |
+ |
ourwinattr.colormap = XCreateColormap(thedisplay, ourroot, |
241 |
+ |
ourvis.visual, AllocNone); |
242 |
|
wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH, |
243 |
< |
0, InputOutput, ourras->visual, |
244 |
< |
CWBackPixel|CWBorderPixel, &ourwinattr); |
243 |
> |
ourvis.depth, InputOutput, ourvis.visual, |
244 |
> |
CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); |
245 |
|
if (wind == 0) |
246 |
< |
quiterr("can't create window"); |
246 |
> |
quiterr("cannot create window"); |
247 |
> |
XFreeColormap(thedisplay, ourwinattr.colormap); |
248 |
|
width = xmax; |
249 |
|
height = ymax; |
250 |
+ |
ourgc = XCreateGC(thedisplay, wind, 0, 0); |
251 |
+ |
XSetState(thedisplay, ourgc, ourblack, ourwhite, GXcopy, AllPlanes); |
252 |
+ |
revgc = XCreateGC(thedisplay, wind, 0, 0); |
253 |
+ |
XSetFunction(thedisplay, revgc, GXinvert); |
254 |
|
fontid = XLoadFont(thedisplay, FONTNAME); |
255 |
|
if (fontid == 0) |
256 |
< |
quiterr("can't get font"); |
256 |
> |
quiterr("cannot get font"); |
257 |
|
XSetFont(thedisplay, ourgc, fontid); |
251 |
– |
revgc = XCreateGC(thedisplay, wind, 0, 0); |
252 |
– |
XSetFunction(thedisplay, revgc, GXinvert); |
258 |
|
XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay, |
259 |
|
XC_diamond_cross)); |
260 |
|
XStoreName(thedisplay, wind, fname == NULL ? progname : fname); |
321 |
|
} |
322 |
|
|
323 |
|
|
324 |
+ |
static int |
325 |
+ |
viscmp(v1,v2) /* compare visual to see which is better, descending */ |
326 |
+ |
register XVisualInfo *v1, *v2; |
327 |
+ |
{ |
328 |
+ |
int bad1 = 0, bad2 = 0; |
329 |
+ |
register int *rp; |
330 |
+ |
|
331 |
+ |
if (v1->class == v2->class) { |
332 |
+ |
if (v1->class == TrueColor || v1->class == DirectColor) { |
333 |
+ |
/* prefer 24-bit to 32-bit */ |
334 |
+ |
if (v1->depth == 24 && v2->depth == 32) |
335 |
+ |
return(-1); |
336 |
+ |
if (v1->depth == 32 && v2->depth == 24) |
337 |
+ |
return(1); |
338 |
+ |
return(0); |
339 |
+ |
} |
340 |
+ |
/* don't be too greedy */ |
341 |
+ |
if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth) |
342 |
+ |
return(v1->depth - v2->depth); |
343 |
+ |
return(v2->depth - v1->depth); |
344 |
+ |
} |
345 |
+ |
/* prefer Pseudo when < 24-bit */ |
346 |
+ |
if ((v1->class == TrueColor || v1->class == DirectColor) && |
347 |
+ |
v1->depth < 24) |
348 |
+ |
bad1 = 1; |
349 |
+ |
if ((v2->class == TrueColor || v2->class == DirectColor) && |
350 |
+ |
v2->depth < 24) |
351 |
+ |
bad2 = -1; |
352 |
+ |
if (bad1 | bad2) |
353 |
+ |
return(bad1+bad2); |
354 |
+ |
/* otherwise, use class ranking */ |
355 |
+ |
for (rp = ourrank; *rp != -1; rp++) { |
356 |
+ |
if (v1->class == *rp) |
357 |
+ |
return(-1); |
358 |
+ |
if (v2->class == *rp) |
359 |
+ |
return(1); |
360 |
+ |
} |
361 |
+ |
return(0); |
362 |
+ |
} |
363 |
+ |
|
364 |
+ |
|
365 |
+ |
getbestvis() /* get the best visual for this screen */ |
366 |
+ |
{ |
367 |
+ |
#ifdef DEBUG |
368 |
+ |
static char vistype[][12] = { |
369 |
+ |
"StaticGray", |
370 |
+ |
"GrayScale", |
371 |
+ |
"StaticColor", |
372 |
+ |
"PseudoColor", |
373 |
+ |
"TrueColor", |
374 |
+ |
"DirectColor" |
375 |
+ |
}; |
376 |
+ |
#endif |
377 |
+ |
static int rankings[3][6] = { |
378 |
+ |
{TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1}, |
379 |
+ |
{PseudoColor,GrayScale,StaticGray,-1}, |
380 |
+ |
{PseudoColor,GrayScale,StaticGray,-1} |
381 |
+ |
}; |
382 |
+ |
XVisualInfo *xvi; |
383 |
+ |
int vismatched; |
384 |
+ |
register int i, j; |
385 |
+ |
|
386 |
+ |
if (greyscale) { |
387 |
+ |
ourrank = rankings[2]; |
388 |
+ |
if (maxcolors < 2) maxcolors = 256; |
389 |
+ |
} else if (maxcolors >= 2 && maxcolors <= 256) |
390 |
+ |
ourrank = rankings[1]; |
391 |
+ |
else { |
392 |
+ |
ourrank = rankings[0]; |
393 |
+ |
maxcolors = 256; |
394 |
+ |
} |
395 |
+ |
/* find best visual */ |
396 |
+ |
ourvis.screen = ourscreen; |
397 |
+ |
xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched); |
398 |
+ |
if (xvi == NULL) |
399 |
+ |
quiterr("no visuals for this screen!"); |
400 |
+ |
#ifdef DEBUG |
401 |
+ |
fprintf(stderr, "Supported visuals:\n"); |
402 |
+ |
for (i = 0; i < vismatched; i++) |
403 |
+ |
fprintf(stderr, "\ttype %s, depth %d\n", |
404 |
+ |
vistype[xvi[i].class], xvi[i].depth); |
405 |
+ |
#endif |
406 |
+ |
for (i = 0, j = 1; j < vismatched; j++) |
407 |
+ |
if (viscmp(&xvi[i],&xvi[j]) > 0) |
408 |
+ |
i = j; |
409 |
+ |
/* compare to least acceptable */ |
410 |
+ |
for (j = 0; ourrank[j++] != -1; ) |
411 |
+ |
; |
412 |
+ |
ourvis.class = ourrank[--j]; |
413 |
+ |
ourvis.depth = 1; |
414 |
+ |
if (viscmp(&xvi[i],&ourvis) > 0) |
415 |
+ |
quiterr("inadequate visuals on this screen"); |
416 |
+ |
/* OK, we'll use it */ |
417 |
+ |
copystruct(&ourvis, &xvi[i]); |
418 |
+ |
#ifdef DEBUG |
419 |
+ |
fprintf(stderr, "Selected visual type %s, depth %d\n", |
420 |
+ |
vistype[ourvis.class], ourvis.depth); |
421 |
+ |
#endif |
422 |
+ |
/* make appropriate adjustments */ |
423 |
+ |
if (ourvis.class == GrayScale || ourvis.class == StaticGray) |
424 |
+ |
greyscale = 1; |
425 |
+ |
if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors) |
426 |
+ |
maxcolors = ourvis.colormap_size; |
427 |
+ |
if (maxcolors > 4) |
428 |
+ |
maxcolors -= 2; |
429 |
+ |
if (ourvis.class == StaticGray) { |
430 |
+ |
ourblack = 0; |
431 |
+ |
ourwhite = 255; |
432 |
+ |
} else if (ourvis.class == PseudoColor) { |
433 |
+ |
ourblack = BlackPixel(thedisplay,ourscreen); |
434 |
+ |
ourwhite = WhitePixel(thedisplay,ourscreen); |
435 |
+ |
if ((ourblack|ourwhite) & ~255L) { |
436 |
+ |
ourblack = 0; |
437 |
+ |
ourwhite = 1; |
438 |
+ |
} |
439 |
+ |
} else { |
440 |
+ |
ourblack = 0; |
441 |
+ |
ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask; |
442 |
+ |
} |
443 |
+ |
XFree((char *)xvi); |
444 |
+ |
} |
445 |
+ |
|
446 |
+ |
|
447 |
|
getras() /* get raster file */ |
448 |
|
{ |
449 |
|
colormap ourmap; |
453 |
|
ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8)); |
454 |
|
if (ourdata == NULL) |
455 |
|
goto fail; |
456 |
< |
ourras = make_raster(thedisplay, ourscreen, 1, ourdata, |
456 |
> |
ourras = make_raster(thedisplay, &ourvis, 1, ourdata, |
457 |
|
xmax, ymax, 8); |
458 |
|
if (ourras == NULL) |
459 |
|
goto fail; |
460 |
|
getmono(); |
461 |
< |
} else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo) |
462 |
< |
/* kludge for DirectColor */ |
335 |
< |
|| XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) { |
336 |
< |
ourdata = (unsigned char *)malloc(xmax*ymax*3); |
461 |
> |
} else if (ourvis.class == TrueColor || ourvis.class == DirectColor) { |
462 |
> |
ourdata = (unsigned char *)malloc(4*xmax*ymax); |
463 |
|
if (ourdata == NULL) |
464 |
|
goto fail; |
465 |
< |
ourras = make_raster(thedisplay, ourscreen, 24, ourdata, |
466 |
< |
xmax, ymax, 8); |
465 |
> |
ourras = make_raster(thedisplay, &ourvis, 32, |
466 |
> |
ourdata, xmax, ymax, 32); |
467 |
|
if (ourras == NULL) |
468 |
|
goto fail; |
469 |
|
getfull(); |
471 |
|
ourdata = (unsigned char *)malloc(xmax*ymax); |
472 |
|
if (ourdata == NULL) |
473 |
|
goto fail; |
474 |
< |
ourras = make_raster(thedisplay, ourscreen, 8, ourdata, |
474 |
> |
ourras = make_raster(thedisplay, &ourvis, 8, ourdata, |
475 |
|
xmax, ymax, 8); |
476 |
|
if (ourras == NULL) |
477 |
|
goto fail; |
478 |
< |
if (greyscale) |
479 |
< |
biq(dither,maxcolors,1,ourmap); |
480 |
< |
else |
481 |
< |
ciq(dither,maxcolors,1,ourmap); |
482 |
< |
if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0) |
483 |
< |
goto fail; |
478 |
> |
if (ourvis.class == StaticGray) |
479 |
> |
getgrey(); |
480 |
> |
else { |
481 |
> |
if (greyscale) |
482 |
> |
biq(dither,maxcolors,1,ourmap); |
483 |
> |
else |
484 |
> |
ciq(dither,maxcolors,1,ourmap); |
485 |
> |
if (init_rcolors(ourras, ourmap[0], |
486 |
> |
ourmap[1], ourmap[2]) == 0) |
487 |
> |
goto fail; |
488 |
> |
} |
489 |
|
} |
490 |
< |
return; |
490 |
> |
return; |
491 |
|
fail: |
492 |
|
quiterr("could not create raster image"); |
493 |
|
} |
515 |
|
case MapNotify: |
516 |
|
map_rcolors(ourras, wind); |
517 |
|
if (fast) |
518 |
< |
make_rpixmap(ourras); |
518 |
> |
make_rpixmap(ourras, wind); |
519 |
|
break; |
520 |
|
case UnmapNotify: |
521 |
< |
unmap_rcolors(ourras); |
521 |
> |
if (!fast) |
522 |
> |
unmap_rcolors(ourras); |
523 |
|
break; |
524 |
|
case Expose: |
525 |
|
redraw(e.e.x, e.e.y, e.e.width, e.e.height); |
551 |
|
com = buf[0]; |
552 |
|
switch (com) { /* interpret command */ |
553 |
|
case 'q': |
554 |
< |
case CTRL(D): /* quit */ |
554 |
> |
case CTRL('D'): /* quit */ |
555 |
|
quit(0); |
556 |
|
case '\n': |
557 |
|
case '\r': |
625 |
|
free_raster(ourras); |
626 |
|
getras(); |
627 |
|
/* fall through */ |
628 |
< |
case CTRL(R): /* redraw */ |
629 |
< |
case CTRL(L): |
628 |
> |
case CTRL('R'): /* redraw */ |
629 |
> |
case CTRL('L'): |
630 |
|
unmap_rcolors(ourras); |
631 |
|
XClearWindow(thedisplay, wind); |
632 |
|
map_rcolors(ourras, wind); |
633 |
|
if (fast) |
634 |
< |
make_rpixmap(ourras); |
634 |
> |
make_rpixmap(ourras, wind); |
635 |
|
redraw(0, 0, width, height); |
636 |
|
return(0); |
637 |
|
case ' ': /* clear */ |
848 |
|
getfull() /* get full (24-bit) data */ |
849 |
|
{ |
850 |
|
int y; |
851 |
+ |
register unsigned long *dp; |
852 |
+ |
register int x; |
853 |
+ |
/* set gamma correction */ |
854 |
+ |
setcolrgam(gamcor); |
855 |
+ |
/* read and convert file */ |
856 |
+ |
dp = (unsigned long *)ourdata; |
857 |
+ |
for (y = 0; y < ymax; y++) { |
858 |
+ |
if (getscan(y) < 0) |
859 |
+ |
quiterr("seek error in getfull"); |
860 |
+ |
if (scale) |
861 |
+ |
shiftcolrs(scanline, xmax, scale); |
862 |
+ |
colrs_gambs(scanline, xmax); |
863 |
+ |
add2icon(y, scanline); |
864 |
+ |
if (ourras->image->blue_mask & 1) |
865 |
+ |
for (x = 0; x < xmax; x++) |
866 |
+ |
*dp++ = scanline[x][RED] << 16 | |
867 |
+ |
scanline[x][GRN] << 8 | |
868 |
+ |
scanline[x][BLU] ; |
869 |
+ |
else |
870 |
+ |
for (x = 0; x < xmax; x++) |
871 |
+ |
*dp++ = scanline[x][RED] | |
872 |
+ |
scanline[x][GRN] << 8 | |
873 |
+ |
scanline[x][BLU] << 16 ; |
874 |
+ |
} |
875 |
+ |
} |
876 |
+ |
|
877 |
+ |
|
878 |
+ |
getgrey() /* get greyscale data */ |
879 |
+ |
{ |
880 |
+ |
int y; |
881 |
|
register unsigned char *dp; |
882 |
|
register int x; |
883 |
|
/* set gamma correction */ |
891 |
|
shiftcolrs(scanline, xmax, scale); |
892 |
|
colrs_gambs(scanline, xmax); |
893 |
|
add2icon(y, scanline); |
894 |
< |
for (x = 0; x < xmax; x++) { |
895 |
< |
*dp++ = scanline[x][RED]; |
896 |
< |
*dp++ = scanline[x][GRN]; |
897 |
< |
*dp++ = scanline[x][BLU]; |
898 |
< |
} |
894 |
> |
if (ourvis.colormap_size < 256) |
895 |
> |
for (x = 0; x < xmax; x++) |
896 |
> |
*dp++ = ((long)normbright(scanline[x]) * |
897 |
> |
ourvis.colormap_size + 128) >> 8; |
898 |
> |
else |
899 |
> |
for (x = 0; x < xmax; x++) |
900 |
> |
*dp++ = normbright(scanline[x]); |
901 |
|
} |
902 |
|
} |
903 |
|
|
939 |
|
if (fseek(fin, scanpos[y], 0) == -1) |
940 |
|
quiterr("fseek error"); |
941 |
|
cury = y; |
942 |
< |
} else if (scanpos != NULL) |
942 |
> |
} else if (scanpos != NULL && scanpos[y] == -1) |
943 |
|
scanpos[y] = ftell(fin); |
944 |
|
|
945 |
|
if (freadcolrs(scanline, xmax, fin) < 0) |
993 |
|
picwritecm(map) /* handled elsewhere */ |
994 |
|
colormap map; |
995 |
|
{ |
996 |
< |
#ifdef DEBUG |
996 |
> |
#if 0 |
997 |
|
register int i; |
998 |
|
|
999 |
|
for (i = 0; i < 256; i++) |