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"; |
27 |
|
|
28 |
|
#include "color.h" |
29 |
|
#include "view.h" |
30 |
– |
#include "pic.h" |
30 |
|
#include "x11raster.h" |
31 |
|
#include "random.h" |
32 |
|
#include "resolu.h" |
40 |
|
#define ICONSIZ (8*10) /* maximum icon dimension (even 8) */ |
41 |
|
|
42 |
|
#define ourscreen DefaultScreen(thedisplay) |
44 |
– |
#define ourblack BlackPixel(thedisplay,ourscreen) |
45 |
– |
#define ourwhite WhitePixel(thedisplay,ourscreen) |
43 |
|
#define ourroot RootWindow(thedisplay,ourscreen) |
44 |
|
|
45 |
|
#define revline(x0,y0,x1,y1) XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1) |
54 |
|
char *dispname = NULL; /* our display name */ |
55 |
|
|
56 |
|
Window wind = 0; /* our output window */ |
57 |
+ |
unsigned long ourblack=0, ourwhite=1; /* black and white for this visual */ |
58 |
|
Font fontid; /* our font */ |
59 |
|
|
60 |
|
int maxcolors = 0; /* maximum colors */ |
103 |
|
|
104 |
|
char errmsg[128]; |
105 |
|
|
106 |
+ |
extern BYTE clrtab[256][3]; /* global color map */ |
107 |
+ |
|
108 |
|
extern long ftell(); |
109 |
|
|
110 |
|
extern char *malloc(), *calloc(); |
111 |
|
|
112 |
– |
extern double pow(), log(); |
113 |
– |
|
112 |
|
Display *thedisplay; |
113 |
|
|
114 |
+ |
|
115 |
|
main(argc, argv) |
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 |
< |
if (argv[i][2] == 'i') { |
141 |
> |
if (argv[i][2] == 'i') |
142 |
|
dispname = argv[++i]; |
143 |
< |
break; |
144 |
< |
} |
142 |
< |
dither = !dither; |
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]); |
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 |
|
} |
244 |
|
CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); |
245 |
|
if (wind == 0) |
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, BlackPixel(thedisplay,ourscreen), |
249 |
< |
WhitePixel(thedisplay,ourscreen), GXcopy, AllPlanes); |
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); |
307 |
|
} |
308 |
|
|
309 |
|
|
308 |
– |
eputs(s) |
309 |
– |
char *s; |
310 |
– |
{ |
311 |
– |
fputs(s, stderr); |
312 |
– |
} |
313 |
– |
|
314 |
– |
|
315 |
– |
quit(code) |
316 |
– |
int code; |
317 |
– |
{ |
318 |
– |
exit(code); |
319 |
– |
} |
320 |
– |
|
321 |
– |
|
310 |
|
static int |
311 |
|
viscmp(v1,v2) /* compare visual to see which is better, descending */ |
312 |
|
register XVisualInfo *v1, *v2; |
350 |
|
|
351 |
|
getbestvis() /* get the best visual for this screen */ |
352 |
|
{ |
353 |
+ |
#ifdef DEBUG |
354 |
|
static char vistype[][12] = { |
355 |
|
"StaticGray", |
356 |
|
"GrayScale", |
359 |
|
"TrueColor", |
360 |
|
"DirectColor" |
361 |
|
}; |
362 |
+ |
#endif |
363 |
|
static int rankings[3][6] = { |
364 |
|
{TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1}, |
365 |
< |
{PseudoColor,GrayScale,-1}, |
366 |
< |
{PseudoColor,GrayScale,-1} |
365 |
> |
{PseudoColor,GrayScale,StaticGray,-1}, |
366 |
> |
{PseudoColor,GrayScale,StaticGray,-1} |
367 |
|
}; |
368 |
|
XVisualInfo *xvi; |
369 |
|
int vismatched; |
383 |
|
xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched); |
384 |
|
if (xvi == NULL) |
385 |
|
quiterr("no visuals for this screen!"); |
386 |
< |
for (i = 0; i < vismatched; i++) |
387 |
< |
fprintf(stderr, "Type %s, depth %d\n", vistype[xvi[i].class], xvi[i].depth); |
386 |
> |
#ifdef DEBUG |
387 |
> |
fprintf(stderr, "Supported visuals:\n"); |
388 |
> |
for (i = 0; i < vismatched; i++) |
389 |
> |
fprintf(stderr, "\ttype %s, depth %d\n", |
390 |
> |
vistype[xvi[i].class], xvi[i].depth); |
391 |
> |
#endif |
392 |
|
for (i = 0, j = 1; j < vismatched; j++) |
393 |
|
if (viscmp(&xvi[i],&xvi[j]) > 0) |
394 |
|
i = j; |
401 |
|
quiterr("inadequate visuals on this screen"); |
402 |
|
/* OK, we'll use it */ |
403 |
|
copystruct(&ourvis, &xvi[i]); |
404 |
< |
fprintf(stderr, "Selected visual type %s, depth %d\n", vistype[ourvis.class], |
405 |
< |
ourvis.depth); |
404 |
> |
#ifdef DEBUG |
405 |
> |
fprintf(stderr, "Selected visual type %s, depth %d\n", |
406 |
> |
vistype[ourvis.class], ourvis.depth); |
407 |
> |
#endif |
408 |
> |
/* make appropriate adjustments */ |
409 |
|
if (ourvis.class == GrayScale || ourvis.class == StaticGray) |
410 |
|
greyscale = 1; |
411 |
< |
if (1<<ourvis.depth < maxcolors) |
412 |
< |
maxcolors = 1<<ourvis.depth; |
413 |
< |
if (maxcolors > 4) |
414 |
< |
maxcolors -= 2; |
411 |
> |
if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors) |
412 |
> |
maxcolors = ourvis.colormap_size; |
413 |
> |
if (ourvis.class == StaticGray) { |
414 |
> |
ourblack = 0; |
415 |
> |
ourwhite = 255; |
416 |
> |
} else if (ourvis.class == PseudoColor) { |
417 |
> |
ourblack = BlackPixel(thedisplay,ourscreen); |
418 |
> |
ourwhite = WhitePixel(thedisplay,ourscreen); |
419 |
> |
if ((ourblack|ourwhite) & ~255L) { |
420 |
> |
ourblack = 0; |
421 |
> |
ourwhite = 1; |
422 |
> |
} |
423 |
> |
if (maxcolors > 4) |
424 |
> |
maxcolors -= 2; |
425 |
> |
} else { |
426 |
> |
ourblack = 0; |
427 |
> |
ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask; |
428 |
> |
} |
429 |
|
XFree((char *)xvi); |
430 |
|
} |
431 |
|
|
432 |
|
|
433 |
|
getras() /* get raster file */ |
434 |
|
{ |
424 |
– |
colormap ourmap; |
435 |
|
XVisualInfo vinfo; |
436 |
|
|
437 |
|
if (maxcolors <= 2) { /* monochrome */ |
443 |
|
if (ourras == NULL) |
444 |
|
goto fail; |
445 |
|
getmono(); |
446 |
< |
} else if (ourvis.class == TrueColor || ourvis.class == DirectColor) { |
446 |
> |
} else if (ourvis.class == TrueColor | ourvis.class == DirectColor) { |
447 |
|
ourdata = (unsigned char *)malloc(4*xmax*ymax); |
448 |
|
if (ourdata == NULL) |
449 |
|
goto fail; |
460 |
|
xmax, ymax, 8); |
461 |
|
if (ourras == NULL) |
462 |
|
goto fail; |
463 |
< |
if (greyscale) |
464 |
< |
biq(dither,maxcolors,1,ourmap); |
463 |
> |
if (greyscale | ourvis.class == StaticGray) |
464 |
> |
getgrey(); |
465 |
|
else |
466 |
< |
ciq(dither,maxcolors,1,ourmap); |
467 |
< |
if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0) |
466 |
> |
getmapped(); |
467 |
> |
if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab)) |
468 |
|
goto fail; |
469 |
|
} |
470 |
|
return; |
498 |
|
make_rpixmap(ourras, wind); |
499 |
|
break; |
500 |
|
case UnmapNotify: |
501 |
< |
unmap_rcolors(ourras); |
501 |
> |
if (!fast) |
502 |
> |
unmap_rcolors(ourras); |
503 |
|
break; |
504 |
|
case Expose: |
505 |
|
redraw(e.e.x, e.e.y, e.e.width, e.e.height); |
611 |
|
XClearWindow(thedisplay, wind); |
612 |
|
map_rcolors(ourras, wind); |
613 |
|
if (fast) |
614 |
< |
make_rpixmap(ourras); |
614 |
> |
make_rpixmap(ourras, wind); |
615 |
|
redraw(0, 0, width, height); |
616 |
|
return(0); |
617 |
|
case ' ': /* clear */ |
855 |
|
} |
856 |
|
|
857 |
|
|
858 |
+ |
getgrey() /* get greyscale data */ |
859 |
+ |
{ |
860 |
+ |
int y; |
861 |
+ |
register unsigned char *dp; |
862 |
+ |
register int x; |
863 |
+ |
/* set gamma correction */ |
864 |
+ |
setcolrgam(gamcor); |
865 |
+ |
/* read and convert file */ |
866 |
+ |
dp = ourdata; |
867 |
+ |
for (y = 0; y < ymax; y++) { |
868 |
+ |
if (getscan(y) < 0) |
869 |
+ |
quiterr("seek error in getgrey"); |
870 |
+ |
if (scale) |
871 |
+ |
shiftcolrs(scanline, xmax, scale); |
872 |
+ |
colrs_gambs(scanline, xmax); |
873 |
+ |
add2icon(y, scanline); |
874 |
+ |
if (maxcolors < 256) |
875 |
+ |
for (x = 0; x < xmax; x++) |
876 |
+ |
*dp++ = ((long)normbright(scanline[x]) * |
877 |
+ |
maxcolors + 128) >> 8; |
878 |
+ |
else |
879 |
+ |
for (x = 0; x < xmax; x++) |
880 |
+ |
*dp++ = normbright(scanline[x]); |
881 |
+ |
} |
882 |
+ |
for (x = 0; x < maxcolors; x++) |
883 |
+ |
clrtab[x][RED] = clrtab[x][GRN] = |
884 |
+ |
clrtab[x][BLU] = ((long)x*256+maxcolors/2)/maxcolors; |
885 |
+ |
} |
886 |
+ |
|
887 |
+ |
|
888 |
+ |
getmapped() /* get color-mapped data */ |
889 |
+ |
{ |
890 |
+ |
int y; |
891 |
+ |
/* set gamma correction */ |
892 |
+ |
setcolrgam(gamcor); |
893 |
+ |
/* make histogram */ |
894 |
+ |
new_histo(); |
895 |
+ |
for (y = 0; y < ymax; y++) { |
896 |
+ |
if (getscan(y) < 0) |
897 |
+ |
quiterr("seek error in getmapped"); |
898 |
+ |
if (scale) |
899 |
+ |
shiftcolrs(scanline, xmax, scale); |
900 |
+ |
colrs_gambs(scanline, xmax); |
901 |
+ |
add2icon(y, scanline); |
902 |
+ |
cnt_colrs(scanline, xmax); |
903 |
+ |
} |
904 |
+ |
/* map pixels */ |
905 |
+ |
if (!new_clrtab(maxcolors)) |
906 |
+ |
quiterr("cannot create color map"); |
907 |
+ |
for (y = 0; y < ymax; y++) { |
908 |
+ |
if (getscan(y) < 0) |
909 |
+ |
quiterr("seek error in getmapped"); |
910 |
+ |
if (scale) |
911 |
+ |
shiftcolrs(scanline, xmax, scale); |
912 |
+ |
colrs_gambs(scanline, xmax); |
913 |
+ |
if (dither) |
914 |
+ |
dith_colrs(ourdata+y*xmax, scanline, xmax); |
915 |
+ |
else |
916 |
+ |
map_colrs(ourdata+y*xmax, scanline, xmax); |
917 |
+ |
} |
918 |
+ |
} |
919 |
+ |
|
920 |
+ |
|
921 |
|
scale_rcolors(xr, sf) /* scale color map */ |
922 |
|
register XRASTER *xr; |
923 |
|
double sf; |
955 |
|
if (fseek(fin, scanpos[y], 0) == -1) |
956 |
|
quiterr("fseek error"); |
957 |
|
cury = y; |
958 |
< |
} else if (scanpos != NULL) |
958 |
> |
} else if (scanpos != NULL && scanpos[y] == -1) |
959 |
|
scanpos[y] = ftell(fin); |
960 |
|
|
961 |
|
if (freadcolrs(scanline, xmax, fin) < 0) |
963 |
|
|
964 |
|
cury++; |
965 |
|
return(0); |
892 |
– |
} |
893 |
– |
|
894 |
– |
|
895 |
– |
picreadline3(y, l3) /* read in 3-byte scanline */ |
896 |
– |
int y; |
897 |
– |
register rgbpixel *l3; |
898 |
– |
{ |
899 |
– |
register int i; |
900 |
– |
/* read scanline */ |
901 |
– |
if (getscan(y) < 0) |
902 |
– |
quiterr("cannot seek for picreadline"); |
903 |
– |
/* convert scanline */ |
904 |
– |
normcolrs(scanline, xmax, scale); |
905 |
– |
add2icon(y, scanline); |
906 |
– |
for (i = 0; i < xmax; i++) { |
907 |
– |
l3[i].r = scanline[i][RED]; |
908 |
– |
l3[i].g = scanline[i][GRN]; |
909 |
– |
l3[i].b = scanline[i][BLU]; |
910 |
– |
} |
911 |
– |
} |
912 |
– |
|
913 |
– |
|
914 |
– |
picwriteline(y, l) /* add 8-bit scanline to image */ |
915 |
– |
int y; |
916 |
– |
pixel *l; |
917 |
– |
{ |
918 |
– |
bcopy((char *)l, (char *)ourdata+y*xmax, xmax); |
919 |
– |
} |
920 |
– |
|
921 |
– |
|
922 |
– |
picreadcm(map) /* do gamma correction */ |
923 |
– |
colormap map; |
924 |
– |
{ |
925 |
– |
extern double pow(); |
926 |
– |
register int i, val; |
927 |
– |
|
928 |
– |
for (i = 0; i < 256; i++) { |
929 |
– |
val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; |
930 |
– |
map[0][i] = map[1][i] = map[2][i] = val; |
931 |
– |
} |
932 |
– |
} |
933 |
– |
|
934 |
– |
|
935 |
– |
picwritecm(map) /* handled elsewhere */ |
936 |
– |
colormap map; |
937 |
– |
{ |
938 |
– |
#ifdef DEBUG |
939 |
– |
register int i; |
940 |
– |
|
941 |
– |
for (i = 0; i < 256; i++) |
942 |
– |
printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]); |
943 |
– |
#endif |
966 |
|
} |