44 |
|
#define ourblack BlackPixel(thedisplay,ourscreen) |
45 |
|
#define ourwhite WhitePixel(thedisplay,ourscreen) |
46 |
|
#define ourroot RootWindow(thedisplay,ourscreen) |
47 |
– |
#define ourgc DefaultGC(thedisplay,ourscreen) |
47 |
|
|
48 |
|
#define revline(x0,y0,x1,y1) XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1) |
49 |
|
|
54 |
|
int dither = 1; /* dither colors? */ |
55 |
|
int fast = 0; /* keep picture in Pixmap? */ |
56 |
|
|
57 |
+ |
char *dispname = NULL; /* our display name */ |
58 |
+ |
|
59 |
|
Window wind = 0; /* our output window */ |
60 |
|
Font fontid; /* our font */ |
61 |
|
|
84 |
|
|
85 |
|
int wrongformat = 0; /* input in another format? */ |
86 |
|
|
87 |
+ |
GC ourgc; /* standard graphics context */ |
88 |
|
GC revgc; /* graphics context with GXinvert */ |
89 |
|
|
90 |
< |
XRASTER *ourras; /* our stored image */ |
90 |
> |
int *ourrank; /* our visual class ranking */ |
91 |
> |
XVisualInfo ourvis; /* our visual */ |
92 |
> |
XRASTER *ourras; /* our stored image */ |
93 |
|
unsigned char *ourdata; /* our image data */ |
94 |
|
|
95 |
|
struct { |
135 |
|
maxcolors = 2; |
136 |
|
break; |
137 |
|
case 'd': |
138 |
+ |
if (argv[i][2] == 'i') { |
139 |
+ |
dispname = argv[++i]; |
140 |
+ |
break; |
141 |
+ |
} |
142 |
|
dither = !dither; |
143 |
|
break; |
144 |
|
case 'f': |
167 |
|
fname = argv[i]; |
168 |
|
fin = fopen(fname, "r"); |
169 |
|
if (fin == NULL) { |
170 |
< |
sprintf(errmsg, "can't open file \"%s\"", fname); |
170 |
> |
sprintf(errmsg, "cannot open file \"%s\"", fname); |
171 |
|
quiterr(errmsg); |
172 |
|
} |
173 |
|
} else if (i != argc) |
200 |
|
headline(s) /* get relevant info from header */ |
201 |
|
char *s; |
202 |
|
{ |
195 |
– |
static char *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL}; |
196 |
– |
register char **an; |
203 |
|
char fmt[32]; |
204 |
|
|
205 |
|
if (isexpos(s)) |
207 |
|
else if (isformat(s)) { |
208 |
|
formatval(fmt, s); |
209 |
|
wrongformat = strcmp(fmt, COLRFMT); |
210 |
< |
} else |
211 |
< |
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 |
< |
} |
210 |
> |
} else if (isview(s) && sscanview(&ourview, s) > 0) |
211 |
> |
gotview++; |
212 |
|
} |
213 |
|
|
214 |
|
|
216 |
|
{ |
217 |
|
XWMHints ourxwmhints; |
218 |
|
XSetWindowAttributes ourwinattr; |
219 |
< |
XSizeHints oursizhints; |
220 |
< |
register int i; |
219 |
> |
XSizeHints oursizhints; |
220 |
> |
register int i; |
221 |
|
|
222 |
|
if (fname != NULL) { |
223 |
|
scanpos = (long *)malloc(ymax*sizeof(long)); |
226 |
|
for (i = 0; i < ymax; i++) |
227 |
|
scanpos[i] = -1; |
228 |
|
} |
229 |
< |
if ((thedisplay = XOpenDisplay(NULL)) == NULL) |
230 |
< |
quiterr("can't open display; DISPLAY variable set?"); |
231 |
< |
if (maxcolors == 0) { /* get number of available colors */ |
232 |
< |
i = DisplayPlanes(thedisplay,ourscreen); |
232 |
< |
maxcolors = i > 8 ? 256 : 1<<i; |
233 |
< |
if (maxcolors > 4) maxcolors -= 2; |
234 |
< |
} |
229 |
> |
if ((thedisplay = XOpenDisplay(dispname)) == NULL) |
230 |
> |
quiterr("cannot open display"); |
231 |
> |
/* get best visual for default screen */ |
232 |
> |
getbestvis(); |
233 |
|
/* store image */ |
234 |
|
getras(); |
235 |
|
/* open window */ |
236 |
|
ourwinattr.border_pixel = ourblack; |
237 |
|
ourwinattr.background_pixel = ourwhite; |
238 |
+ |
ourwinattr.colormap = XCreateColormap(thedisplay, ourroot, |
239 |
+ |
ourvis.visual, AllocNone); |
240 |
|
wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH, |
241 |
< |
0, InputOutput, ourras->visual, |
242 |
< |
CWBackPixel|CWBorderPixel, &ourwinattr); |
241 |
> |
ourvis.depth, InputOutput, ourvis.visual, |
242 |
> |
CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); |
243 |
|
if (wind == 0) |
244 |
< |
quiterr("can't create window"); |
244 |
> |
quiterr("cannot create window"); |
245 |
|
width = xmax; |
246 |
|
height = ymax; |
247 |
+ |
ourgc = XCreateGC(thedisplay, wind, 0, 0); |
248 |
+ |
XSetState(thedisplay, ourgc, BlackPixel(thedisplay,ourscreen), |
249 |
+ |
WhitePixel(thedisplay,ourscreen), GXcopy, AllPlanes); |
250 |
+ |
revgc = XCreateGC(thedisplay, wind, 0, 0); |
251 |
+ |
XSetFunction(thedisplay, revgc, GXinvert); |
252 |
|
fontid = XLoadFont(thedisplay, FONTNAME); |
253 |
|
if (fontid == 0) |
254 |
< |
quiterr("can't get font"); |
254 |
> |
quiterr("cannot get font"); |
255 |
|
XSetFont(thedisplay, ourgc, fontid); |
251 |
– |
revgc = XCreateGC(thedisplay, wind, 0, 0); |
252 |
– |
XSetFunction(thedisplay, revgc, GXinvert); |
256 |
|
XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay, |
257 |
|
XC_diamond_cross)); |
258 |
|
XStoreName(thedisplay, wind, fname == NULL ? progname : fname); |
319 |
|
} |
320 |
|
|
321 |
|
|
322 |
+ |
static int |
323 |
+ |
viscmp(v1,v2) /* compare visual to see which is better, descending */ |
324 |
+ |
register XVisualInfo *v1, *v2; |
325 |
+ |
{ |
326 |
+ |
int bad1 = 0, bad2 = 0; |
327 |
+ |
register int *rp; |
328 |
+ |
|
329 |
+ |
if (v1->class == v2->class) { |
330 |
+ |
if (v1->class == TrueColor || v1->class == DirectColor) { |
331 |
+ |
/* prefer 24-bit to 32-bit */ |
332 |
+ |
if (v1->depth == 24 && v2->depth == 32) |
333 |
+ |
return(-1); |
334 |
+ |
if (v1->depth == 32 && v2->depth == 24) |
335 |
+ |
return(1); |
336 |
+ |
return(0); |
337 |
+ |
} |
338 |
+ |
/* don't be too greedy */ |
339 |
+ |
if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth) |
340 |
+ |
return(v1->depth - v2->depth); |
341 |
+ |
return(v2->depth - v1->depth); |
342 |
+ |
} |
343 |
+ |
/* prefer Pseudo when < 24-bit */ |
344 |
+ |
if ((v1->class == TrueColor || v1->class == DirectColor) && |
345 |
+ |
v1->depth < 24) |
346 |
+ |
bad1 = 1; |
347 |
+ |
if ((v2->class == TrueColor || v2->class == DirectColor) && |
348 |
+ |
v2->depth < 24) |
349 |
+ |
bad2 = -1; |
350 |
+ |
if (bad1 | bad2) |
351 |
+ |
return(bad1+bad2); |
352 |
+ |
/* otherwise, use class ranking */ |
353 |
+ |
for (rp = ourrank; *rp != -1; rp++) { |
354 |
+ |
if (v1->class == *rp) |
355 |
+ |
return(-1); |
356 |
+ |
if (v2->class == *rp) |
357 |
+ |
return(1); |
358 |
+ |
} |
359 |
+ |
return(0); |
360 |
+ |
} |
361 |
+ |
|
362 |
+ |
|
363 |
+ |
getbestvis() /* get the best visual for this screen */ |
364 |
+ |
{ |
365 |
+ |
static char vistype[][12] = { |
366 |
+ |
"StaticGray", |
367 |
+ |
"GrayScale", |
368 |
+ |
"StaticColor", |
369 |
+ |
"PseudoColor", |
370 |
+ |
"TrueColor", |
371 |
+ |
"DirectColor" |
372 |
+ |
}; |
373 |
+ |
static int rankings[3][6] = { |
374 |
+ |
{TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1}, |
375 |
+ |
{PseudoColor,GrayScale,-1}, |
376 |
+ |
{PseudoColor,GrayScale,-1} |
377 |
+ |
}; |
378 |
+ |
XVisualInfo *xvi; |
379 |
+ |
int vismatched; |
380 |
+ |
register int i, j; |
381 |
+ |
|
382 |
+ |
if (greyscale) { |
383 |
+ |
ourrank = rankings[2]; |
384 |
+ |
if (maxcolors < 2) maxcolors = 256; |
385 |
+ |
} else if (maxcolors >= 2 && maxcolors <= 256) |
386 |
+ |
ourrank = rankings[1]; |
387 |
+ |
else { |
388 |
+ |
ourrank = rankings[0]; |
389 |
+ |
maxcolors = 256; |
390 |
+ |
} |
391 |
+ |
/* find best visual */ |
392 |
+ |
ourvis.screen = ourscreen; |
393 |
+ |
xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched); |
394 |
+ |
if (xvi == NULL) |
395 |
+ |
quiterr("no visuals for this screen!"); |
396 |
+ |
for (i = 0; i < vismatched; i++) |
397 |
+ |
fprintf(stderr, "Type %s, depth %d\n", vistype[xvi[i].class], xvi[i].depth); |
398 |
+ |
for (i = 0, j = 1; j < vismatched; j++) |
399 |
+ |
if (viscmp(&xvi[i],&xvi[j]) > 0) |
400 |
+ |
i = j; |
401 |
+ |
/* compare to least acceptable */ |
402 |
+ |
for (j = 0; ourrank[j++] != -1; ) |
403 |
+ |
; |
404 |
+ |
ourvis.class = ourrank[--j]; |
405 |
+ |
ourvis.depth = 1; |
406 |
+ |
if (viscmp(&xvi[i],&ourvis) > 0) |
407 |
+ |
quiterr("inadequate visuals on this screen"); |
408 |
+ |
/* OK, we'll use it */ |
409 |
+ |
copystruct(&ourvis, &xvi[i]); |
410 |
+ |
fprintf(stderr, "Selected visual type %s, depth %d\n", vistype[ourvis.class], |
411 |
+ |
ourvis.depth); |
412 |
+ |
if (ourvis.class == GrayScale || ourvis.class == StaticGray) |
413 |
+ |
greyscale = 1; |
414 |
+ |
if (1<<ourvis.depth < maxcolors) |
415 |
+ |
maxcolors = 1<<ourvis.depth; |
416 |
+ |
if (maxcolors > 4) |
417 |
+ |
maxcolors -= 2; |
418 |
+ |
XFree((char *)xvi); |
419 |
+ |
} |
420 |
+ |
|
421 |
+ |
|
422 |
|
getras() /* get raster file */ |
423 |
|
{ |
424 |
|
colormap ourmap; |
428 |
|
ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8)); |
429 |
|
if (ourdata == NULL) |
430 |
|
goto fail; |
431 |
< |
ourras = make_raster(thedisplay, ourscreen, 1, ourdata, |
431 |
> |
ourras = make_raster(thedisplay, &ourvis, 1, ourdata, |
432 |
|
xmax, ymax, 8); |
433 |
|
if (ourras == NULL) |
434 |
|
goto fail; |
435 |
|
getmono(); |
436 |
< |
} else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo) |
437 |
< |
/* kludge for DirectColor */ |
335 |
< |
|| XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) { |
336 |
< |
ourdata = (unsigned char *)malloc(xmax*ymax*3); |
436 |
> |
} else if (ourvis.class == TrueColor || ourvis.class == DirectColor) { |
437 |
> |
ourdata = (unsigned char *)malloc(4*xmax*ymax); |
438 |
|
if (ourdata == NULL) |
439 |
|
goto fail; |
440 |
< |
ourras = make_raster(thedisplay, ourscreen, 24, ourdata, |
441 |
< |
xmax, ymax, 8); |
440 |
> |
ourras = make_raster(thedisplay, &ourvis, 32, |
441 |
> |
ourdata, xmax, ymax, 32); |
442 |
|
if (ourras == NULL) |
443 |
|
goto fail; |
444 |
|
getfull(); |
446 |
|
ourdata = (unsigned char *)malloc(xmax*ymax); |
447 |
|
if (ourdata == NULL) |
448 |
|
goto fail; |
449 |
< |
ourras = make_raster(thedisplay, ourscreen, 8, ourdata, |
449 |
> |
ourras = make_raster(thedisplay, &ourvis, 8, ourdata, |
450 |
|
xmax, ymax, 8); |
451 |
|
if (ourras == NULL) |
452 |
|
goto fail; |
485 |
|
case MapNotify: |
486 |
|
map_rcolors(ourras, wind); |
487 |
|
if (fast) |
488 |
< |
make_rpixmap(ourras); |
488 |
> |
make_rpixmap(ourras, wind); |
489 |
|
break; |
490 |
|
case UnmapNotify: |
491 |
|
unmap_rcolors(ourras); |
817 |
|
getfull() /* get full (24-bit) data */ |
818 |
|
{ |
819 |
|
int y; |
820 |
< |
register unsigned char *dp; |
820 |
> |
register unsigned long *dp; |
821 |
|
register int x; |
822 |
|
/* set gamma correction */ |
823 |
|
setcolrgam(gamcor); |
824 |
|
/* read and convert file */ |
825 |
< |
dp = ourdata; |
825 |
> |
dp = (unsigned long *)ourdata; |
826 |
|
for (y = 0; y < ymax; y++) { |
827 |
|
if (getscan(y) < 0) |
828 |
|
quiterr("seek error in getfull"); |
830 |
|
shiftcolrs(scanline, xmax, scale); |
831 |
|
colrs_gambs(scanline, xmax); |
832 |
|
add2icon(y, scanline); |
833 |
< |
for (x = 0; x < xmax; x++) { |
834 |
< |
*dp++ = scanline[x][RED]; |
835 |
< |
*dp++ = scanline[x][GRN]; |
836 |
< |
*dp++ = scanline[x][BLU]; |
837 |
< |
} |
833 |
> |
if (ourras->image->blue_mask & 1) |
834 |
> |
for (x = 0; x < xmax; x++) |
835 |
> |
*dp++ = scanline[x][RED] << 16 | |
836 |
> |
scanline[x][GRN] << 8 | |
837 |
> |
scanline[x][BLU] ; |
838 |
> |
else |
839 |
> |
for (x = 0; x < xmax; x++) |
840 |
> |
*dp++ = scanline[x][RED] | |
841 |
> |
scanline[x][GRN] << 8 | |
842 |
> |
scanline[x][BLU] << 16 ; |
843 |
|
} |
844 |
|
} |
845 |
|
|