43 |
|
#define ourroot RootWindow(thedisplay,ourscreen) |
44 |
|
#define ourgc DefaultGC(thedisplay,ourscreen) |
45 |
|
|
46 |
+ |
#define revline(x0,y0,x1,y1) XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1) |
47 |
+ |
|
48 |
|
#define redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras) |
49 |
|
|
50 |
|
double gamcor = 2.2; /* gamma correction */ |
77 |
|
|
78 |
|
double exposure = 1.0; /* exposure compensation used */ |
79 |
|
|
80 |
+ |
GC revgc; /* graphics context with GXinvert */ |
81 |
+ |
|
82 |
|
XRASTER *ourras; /* our stored image */ |
83 |
|
unsigned char *ourdata; /* our image data */ |
84 |
|
|
141 |
|
default: |
142 |
|
goto userr; |
143 |
|
} |
144 |
+ |
else if (argv[i][0] == '=') |
145 |
+ |
geometry = argv[i]; |
146 |
|
else |
147 |
|
break; |
148 |
|
|
149 |
< |
if (argc-i == 1) { |
149 |
> |
if (i == argc-1) { |
150 |
|
fname = argv[i]; |
151 |
|
fin = fopen(fname, "r"); |
152 |
|
if (fin == NULL) { |
153 |
|
sprintf(errmsg, "can't open file \"%s\"", fname); |
154 |
|
quiterr(errmsg); |
155 |
|
} |
156 |
< |
} |
156 |
> |
} else if (i != argc) |
157 |
> |
goto userr; |
158 |
|
/* get header */ |
159 |
|
getheader(fin, headline); |
160 |
|
/* get picture dimensions */ |
198 |
|
|
199 |
|
init() /* get data and open window */ |
200 |
|
{ |
201 |
+ |
XSetWindowAttributes ourwinattr; |
202 |
|
XSizeHints oursizhints; |
203 |
|
register int i; |
204 |
|
|
211 |
|
} |
212 |
|
if ((thedisplay = XOpenDisplay(NULL)) == NULL) |
213 |
|
quiterr("can't open display; DISPLAY variable set?"); |
206 |
– |
wind = XCreateSimpleWindow(thedisplay, |
207 |
– |
ourroot, 0, 0, xmax, ymax, BORWIDTH, ourblack, ourwhite); |
208 |
– |
if (wind == 0) |
209 |
– |
quiterr("can't create window"); |
214 |
|
if (maxcolors == 0) { /* get number of available colors */ |
215 |
|
i = DisplayPlanes(thedisplay,ourscreen); |
216 |
|
maxcolors = i > 8 ? 256 : 1<<i; |
217 |
|
if (maxcolors > 4) maxcolors -= 2; |
218 |
|
} |
219 |
+ |
/* store image */ |
220 |
+ |
getras(); |
221 |
+ |
/* open window */ |
222 |
+ |
ourwinattr.border_pixel = ourblack; |
223 |
+ |
ourwinattr.background_pixel = ourwhite; |
224 |
+ |
wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH, |
225 |
+ |
0, InputOutput, ourras->visual, |
226 |
+ |
CWBackPixel|CWBorderPixel, &ourwinattr); |
227 |
+ |
if (wind == 0) |
228 |
+ |
quiterr("can't create window"); |
229 |
+ |
width = xmax; |
230 |
+ |
height = ymax; |
231 |
|
fontid = XLoadFont(thedisplay, FONTNAME); |
232 |
|
if (fontid == 0) |
233 |
|
quiterr("can't get font"); |
234 |
|
XSetFont(thedisplay, ourgc, fontid); |
235 |
+ |
revgc = XCreateGC(thedisplay, wind, 0, 0); |
236 |
+ |
XSetFunction(thedisplay, revgc, GXinvert); |
237 |
|
XStoreName(thedisplay, wind, fname == NULL ? progname : fname); |
238 |
|
XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay, |
239 |
|
XC_diamond_cross)); |
240 |
|
if (geometry != NULL) { |
241 |
|
bzero((char *)&oursizhints, sizeof(oursizhints)); |
242 |
|
i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y, |
243 |
< |
&oursizhints.width, &oursizhints.height); |
244 |
< |
if (i & (XValue|YValue) == (XValue|YValue)) { |
243 |
> |
(unsigned *)&oursizhints.width, |
244 |
> |
(unsigned *)&oursizhints.height); |
245 |
> |
if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue)) |
246 |
> |
oursizhints.flags |= USSize; |
247 |
> |
else { |
248 |
> |
oursizhints.width = xmax; |
249 |
> |
oursizhints.height = ymax; |
250 |
> |
oursizhints.flags |= PSize; |
251 |
> |
} |
252 |
> |
if ((i&(XValue|YValue)) == (XValue|YValue)) { |
253 |
|
oursizhints.flags |= USPosition; |
254 |
|
if (i & XNegative) |
255 |
< |
oursizhints.x += DisplayWidth(thedisplay,ourscreen)-1; |
255 |
> |
oursizhints.x += DisplayWidth(thedisplay, |
256 |
> |
ourscreen)-1-oursizhints.width-2*BORWIDTH; |
257 |
|
if (i & YNegative) |
258 |
< |
oursizhints.y += DisplayHeight(thedisplay,ourscreen)-1; |
258 |
> |
oursizhints.y += DisplayHeight(thedisplay, |
259 |
> |
ourscreen)-1-oursizhints.height-2*BORWIDTH; |
260 |
|
} |
233 |
– |
if (i & (WidthValue|HeightValue) == (WidthValue|HeightValue)) |
234 |
– |
oursizhints.flags |= USSize; |
261 |
|
XSetNormalHints(thedisplay, wind, &oursizhints); |
262 |
|
} |
237 |
– |
/* store image */ |
238 |
– |
getras(); |
263 |
|
XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask |
264 |
|
|ButtonMotionMask|StructureNotifyMask |
265 |
|
|KeyPressMask|ExposureMask); |
298 |
|
getras() /* get raster file */ |
299 |
|
{ |
300 |
|
colormap ourmap; |
277 |
– |
unsigned char rmap[256], gmap[256], bmap[256]; |
301 |
|
XVisualInfo vinfo; |
279 |
– |
register int i; |
302 |
|
|
303 |
|
if (maxcolors <= 2) { /* monochrome */ |
304 |
|
ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8)); |
309 |
|
if (ourras == NULL) |
310 |
|
goto fail; |
311 |
|
getmono(); |
312 |
< |
} else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo)) { |
312 |
> |
} else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo) |
313 |
> |
/* kludge for DirectColor */ |
314 |
> |
|| XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) { |
315 |
|
ourdata = (unsigned char *)malloc(xmax*ymax*3); |
316 |
|
if (ourdata == NULL) |
317 |
|
goto fail; |
332 |
|
biq(dither,maxcolors,1,ourmap); |
333 |
|
else |
334 |
|
ciq(dither,maxcolors,1,ourmap); |
335 |
< |
for (i = 0; i < 256; i++) { |
312 |
< |
rmap[i] = ourmap[0][i]; |
313 |
< |
gmap[i] = ourmap[1][i]; |
314 |
< |
bmap[i] = ourmap[2][i]; |
315 |
< |
} |
316 |
< |
if (init_rcolors(ourras, rmap, gmap, bmap) == 0) |
335 |
> |
if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0) |
336 |
|
goto fail; |
337 |
|
} |
338 |
|
return; |
339 |
|
fail: |
340 |
< |
quit("could not create raster image"); |
340 |
> |
quiterr("could not create raster image"); |
341 |
|
} |
342 |
|
|
343 |
|
|
411 |
|
sprintf(buf, "%.3f", intens(cval)/exposure); |
412 |
|
break; |
413 |
|
case 'l': /* luminance */ |
414 |
< |
sprintf(buf, "%.0fn", bright(cval)*683.0/exposure); |
414 |
> |
sprintf(buf, "%.0fn", luminance(cval)/exposure); |
415 |
|
break; |
416 |
|
case 'c': /* color */ |
417 |
|
comp = pow(2.0, (double)scale); |
421 |
|
colval(cval,BLU)*comp); |
422 |
|
break; |
423 |
|
} |
424 |
< |
XDrawImageString(thedisplay, wind, ourgc, box.xmin, box.ymin, |
425 |
< |
buf, strlen(buf)); |
424 |
> |
XDrawImageString(thedisplay, wind, ourgc, |
425 |
> |
box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); |
426 |
|
return(0); |
427 |
|
case 'i': /* identify (contour) */ |
428 |
|
if (ourras->pixels == NULL) |
446 |
|
XBell(thedisplay, 0); |
447 |
|
return(-1); |
448 |
|
} |
449 |
< |
viewray(rorg, rdir, &ourview, |
449 |
> |
if (viewray(rorg, rdir, &ourview, |
450 |
|
(ekey->x-xoff+.5)/xmax, |
451 |
< |
(ymax-1-ekey->y+yoff+.5)/ymax); |
451 |
> |
(ymax-1-ekey->y+yoff+.5)/ymax) < 0) |
452 |
> |
return(-1); |
453 |
|
printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); |
454 |
|
printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); |
455 |
|
fflush(stdout); |
463 |
|
scale_rcolors(ourras, pow(2.0, (double)n)); |
464 |
|
scale += n; |
465 |
|
sprintf(buf, "%+d", scale); |
466 |
< |
XDrawImageString(thedisplay, wind, ourgc, box.xmin, box.ymin, |
467 |
< |
buf, strlen(buf)); |
466 |
> |
XDrawImageString(thedisplay, wind, ourgc, |
467 |
> |
box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); |
468 |
|
XFlush(thedisplay); |
469 |
|
free(ourdata); |
470 |
|
free_raster(ourras); |
493 |
|
XButtonPressedEvent *ebut; |
494 |
|
{ |
495 |
|
union { |
496 |
< |
XEvent u; |
497 |
< |
XButtonReleasedEvent b; |
498 |
< |
} e; |
496 |
> |
XEvent u; |
497 |
> |
XButtonReleasedEvent b; |
498 |
> |
XPointerMovedEvent m; |
499 |
> |
} e; |
500 |
> |
int mxo, myo; |
501 |
|
|
502 |
< |
XMaskEvent(thedisplay, ButtonReleaseMask, &e.u); |
502 |
> |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); |
503 |
> |
while (e.u.type == MotionNotify) { |
504 |
> |
mxo = e.m.x; |
505 |
> |
myo = e.m.y; |
506 |
> |
revline(ebut->x, ebut->y, mxo, myo); |
507 |
> |
revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, |
508 |
> |
xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); |
509 |
> |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); |
510 |
> |
revline(ebut->x, ebut->y, mxo, myo); |
511 |
> |
revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, |
512 |
> |
xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); |
513 |
> |
} |
514 |
|
xoff += e.b.x - ebut->x; |
515 |
|
yoff += e.b.y - ebut->y; |
516 |
|
XClearWindow(thedisplay, wind); |
553 |
|
revbox(x0, y0, x1, y1) /* draw box with reversed lines */ |
554 |
|
int x0, y0, x1, y1; |
555 |
|
{ |
556 |
< |
static GC mygc = 0; |
556 |
> |
revline(x0, y0, x1, y0); |
557 |
> |
revline(x0, y1, x1, y1); |
558 |
> |
revline(x0, y0, x0, y1); |
559 |
> |
revline(x1, y0, x1, y1); |
560 |
> |
} |
561 |
|
|
525 |
– |
if (mygc == 0) { |
526 |
– |
mygc = XCreateGC(thedisplay, wind, 0, 0); |
527 |
– |
XSetFunction(thedisplay, mygc, GXinvert); |
528 |
– |
} |
529 |
– |
XDrawLine(thedisplay, wind, mygc, x0, y0, x1, y0); |
530 |
– |
XDrawLine(thedisplay, wind, mygc, x0, y1, x1, y1); |
531 |
– |
XDrawLine(thedisplay, wind, mygc, x0, y0, x0, y1); |
532 |
– |
XDrawLine(thedisplay, wind, mygc, x1, y0, x1, y1); |
533 |
– |
} /* end of revbox */ |
562 |
|
|
535 |
– |
|
563 |
|
avgbox(clr) /* average color over current box */ |
564 |
|
COLOR clr; |
565 |
|
{ |
605 |
|
register unsigned char *dp; |
606 |
|
register int x, err; |
607 |
|
int y; |
581 |
– |
rgbpixel *inline; |
608 |
|
short *cerr; |
609 |
|
|
610 |
< |
if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL |
611 |
< |
|| (cerr = (short *)calloc(xmax,sizeof(short))) == NULL) |
586 |
< |
quit("out of memory in getmono"); |
610 |
> |
if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL) |
611 |
> |
quiterr("out of memory in getmono"); |
612 |
|
dp = ourdata - 1; |
613 |
|
for (y = 0; y < ymax; y++) { |
614 |
< |
picreadline3(y, inline); |
614 |
> |
if (getscan(y) < 0) |
615 |
> |
quiterr("seek error in getmono"); |
616 |
> |
normcolrs(scanline, xmax, scale); |
617 |
|
err = 0; |
618 |
|
for (x = 0; x < xmax; x++) { |
619 |
|
if (!(x&7)) |
620 |
|
*++dp = 0; |
621 |
< |
err += rgb_bright(&inline[x]) + cerr[x]; |
621 |
> |
err += normbright(scanline[x]) + cerr[x]; |
622 |
|
if (err > 127) |
623 |
|
err -= 255; |
624 |
|
else |
625 |
< |
*dp |= 1<<(x&07); |
625 |
> |
*dp |= 1<<(7-(x&07)); |
626 |
|
cerr[x] = err >>= 1; |
627 |
|
} |
628 |
|
} |
602 |
– |
free((char *)inline); |
629 |
|
free((char *)cerr); |
630 |
|
} |
631 |
|
|
633 |
|
getfull() /* get full (24-bit) data */ |
634 |
|
{ |
635 |
|
int y; |
636 |
< |
|
637 |
< |
for (y = 0; y < ymax; y++) |
638 |
< |
picreadline3(y, (rgbpixel *)(ourdata+y*xmax*3)); |
636 |
> |
register unsigned char *dp; |
637 |
> |
register int x; |
638 |
> |
/* set gamma correction */ |
639 |
> |
setcolrgam(gamcor); |
640 |
> |
/* read and convert file */ |
641 |
> |
dp = ourdata; |
642 |
> |
for (y = 0; y < ymax; y++) { |
643 |
> |
if (getscan(y) < 0) |
644 |
> |
quiterr("seek error in getfull"); |
645 |
> |
if (scale) |
646 |
> |
shiftcolrs(scanline, xmax, scale); |
647 |
> |
colrs_gambs(scanline, xmax); |
648 |
> |
for (x = 0; x < xmax; x++) { |
649 |
> |
*dp++ = scanline[x][RED]; |
650 |
> |
*dp++ = scanline[x][GRN]; |
651 |
> |
*dp++ = scanline[x][BLU]; |
652 |
> |
} |
653 |
> |
} |
654 |
|
} |
655 |
|
|
656 |
|
|
689 |
|
if (scanpos == NULL || scanpos[y] == -1) |
690 |
|
return(-1); |
691 |
|
if (fseek(fin, scanpos[y], 0) == -1) |
692 |
< |
quit("fseek error"); |
692 |
> |
quiterr("fseek error"); |
693 |
|
cury = y; |
694 |
|
} else if (scanpos != NULL) |
695 |
|
scanpos[y] = ftell(fin); |
735 |
|
register int i, val; |
736 |
|
|
737 |
|
for (i = 0; i < 256; i++) { |
738 |
< |
val = pow(i/256.0, 1.0/gamcor) * 256.0; |
738 |
> |
val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; |
739 |
|
map[0][i] = map[1][i] = map[2][i] = val; |
740 |
|
} |
741 |
|
} |