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 */ |
51 |
|
|
52 |
|
int dither = 1; /* dither colors? */ |
55 |
|
Window wind = 0; /* our output window */ |
56 |
|
Font fontid; /* our font */ |
57 |
|
|
58 |
< |
long maxcolors = 0; /* maximum colors */ |
58 |
> |
int maxcolors = 0; /* maximum colors */ |
59 |
|
int greyscale = 0; /* in grey */ |
60 |
|
|
61 |
|
int scale = 0; /* scalefactor; power of two */ |
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 */ |
184 |
|
static char *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL}; |
185 |
|
register char **an; |
186 |
|
|
187 |
< |
if (!strncmp(s, "EXPOSURE=", 9)) |
188 |
< |
exposure *= atof(s+9); |
187 |
> |
if (isexpos(s)) |
188 |
> |
exposure *= exposval(s); |
189 |
|
else |
190 |
|
for (an = altname; *an != NULL; an++) |
191 |
|
if (!strncmp(*an, s, strlen(*an))) { |
192 |
< |
if (sscanview(&ourview, s+strlen(*an)) == 0) |
192 |
> |
if (sscanview(&ourview, s+strlen(*an)) > 0) |
193 |
|
gotview++; |
194 |
|
return; |
195 |
|
} |
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?"); |
204 |
– |
wind = XCreateSimpleWindow(thedisplay, |
205 |
– |
ourroot, 0, 0, xmax, ymax, BORWIDTH, ourblack, ourwhite); |
206 |
– |
if (wind == 0) |
207 |
– |
quiterr("can't create window"); |
214 |
|
if (maxcolors == 0) { /* get number of available colors */ |
215 |
< |
maxcolors = 1<<DisplayPlanes(thedisplay,ourscreen); |
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)); |
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)) { |
244 |
> |
if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue)) |
245 |
> |
oursizhints.flags |= USSize; |
246 |
> |
else { |
247 |
> |
oursizhints.width = xmax; |
248 |
> |
oursizhints.height = ymax; |
249 |
> |
oursizhints.flags |= PSize; |
250 |
> |
} |
251 |
> |
if ((i&(XValue|YValue)) == (XValue|YValue)) { |
252 |
|
oursizhints.flags |= USPosition; |
253 |
|
if (i & XNegative) |
254 |
< |
oursizhints.x += DisplayWidth(thedisplay,ourscreen)-1; |
254 |
> |
oursizhints.x += DisplayWidth(thedisplay, |
255 |
> |
ourscreen)-1-oursizhints.width-2*BORWIDTH; |
256 |
|
if (i & YNegative) |
257 |
< |
oursizhints.y += DisplayHeight(thedisplay,ourscreen)-1; |
257 |
> |
oursizhints.y += DisplayHeight(thedisplay, |
258 |
> |
ourscreen)-1-oursizhints.height-2*BORWIDTH; |
259 |
|
} |
230 |
– |
if (i & (WidthValue|HeightValue) == (WidthValue|HeightValue)) |
231 |
– |
oursizhints.flags |= USSize; |
260 |
|
XSetNormalHints(thedisplay, wind, &oursizhints); |
261 |
|
} |
262 |
< |
/* store image */ |
263 |
< |
getras(); |
264 |
< |
XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask| |
237 |
< |
StructureNotifyMask|ButtonMotionMask| |
238 |
< |
KeyPressMask|ExposureMask); |
262 |
> |
XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask |
263 |
> |
|ButtonMotionMask|StructureNotifyMask |
264 |
> |
|KeyPressMask|ExposureMask); |
265 |
|
XMapWindow(thedisplay, wind); |
266 |
|
return; |
267 |
|
memerr: |
297 |
|
getras() /* get raster file */ |
298 |
|
{ |
299 |
|
colormap ourmap; |
274 |
– |
unsigned char rmap[256], gmap[256], bmap[256]; |
300 |
|
XVisualInfo vinfo; |
276 |
– |
register int i; |
301 |
|
|
302 |
|
if (maxcolors <= 2) { /* monochrome */ |
303 |
|
ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8)); |
308 |
|
if (ourras == NULL) |
309 |
|
goto fail; |
310 |
|
getmono(); |
311 |
< |
} else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo)) { |
311 |
> |
} else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo) |
312 |
> |
/* kludge for DirectColor */ |
313 |
> |
|| XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) { |
314 |
|
ourdata = (unsigned char *)malloc(xmax*ymax*3); |
315 |
|
if (ourdata == NULL) |
316 |
|
goto fail; |
331 |
|
biq(dither,maxcolors,1,ourmap); |
332 |
|
else |
333 |
|
ciq(dither,maxcolors,1,ourmap); |
334 |
< |
for (i = 0; i < 256; i++) { |
309 |
< |
rmap[i] = ourmap[0][i]; |
310 |
< |
gmap[i] = ourmap[1][i]; |
311 |
< |
bmap[i] = ourmap[2][i]; |
312 |
< |
} |
313 |
< |
if (init_rcolors(ourras, rmap, gmap, bmap) == 0) |
334 |
> |
if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0) |
335 |
|
goto fail; |
336 |
|
} |
337 |
|
return; |
338 |
|
fail: |
339 |
< |
quit("could not create raster image"); |
339 |
> |
quiterr("could not create raster image"); |
340 |
|
} |
341 |
|
|
342 |
|
|
380 |
|
} |
381 |
|
|
382 |
|
|
362 |
– |
redraw(x, y, w, h) /* redraw section of window */ |
363 |
– |
int x, y; |
364 |
– |
int w, h; |
365 |
– |
{ |
366 |
– |
patch_raster(wind,x-xoff,y-yoff,x,y,w,h,ourras); |
367 |
– |
} |
368 |
– |
|
369 |
– |
|
383 |
|
docom(ekey) /* execute command */ |
384 |
|
XKeyPressedEvent *ekey; |
385 |
|
{ |
420 |
|
colval(cval,BLU)*comp); |
421 |
|
break; |
422 |
|
} |
423 |
< |
XDrawImageString(thedisplay, wind, ourgc, box.xmin, box.ymin, |
424 |
< |
buf, strlen(buf)); |
423 |
> |
XDrawImageString(thedisplay, wind, ourgc, |
424 |
> |
box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); |
425 |
|
return(0); |
426 |
|
case 'i': /* identify (contour) */ |
427 |
|
if (ourras->pixels == NULL) |
432 |
|
cvx.red = random() & 65535; |
433 |
|
cvx.green = random() & 65535; |
434 |
|
cvx.blue = random() & 65535; |
435 |
< |
XStoreColors(thedisplay, ourras->cmap, &cvx, 1); |
435 |
> |
cvx.flags = DoRed|DoGreen|DoBlue; |
436 |
> |
XStoreColor(thedisplay, ourras->cmap, &cvx); |
437 |
|
return(0); |
438 |
|
case 'p': /* position */ |
439 |
|
sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff); |
445 |
|
XBell(thedisplay, 0); |
446 |
|
return(-1); |
447 |
|
} |
448 |
< |
viewray(rorg, rdir, &ourview, |
448 |
> |
if (viewray(rorg, rdir, &ourview, |
449 |
|
(ekey->x-xoff+.5)/xmax, |
450 |
< |
(ymax-1-ekey->y+yoff+.5)/ymax); |
450 |
> |
(ymax-1-ekey->y+yoff+.5)/ymax) < 0) |
451 |
> |
return(-1); |
452 |
|
printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); |
453 |
|
printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); |
454 |
|
fflush(stdout); |
462 |
|
scale_rcolors(ourras, pow(2.0, (double)n)); |
463 |
|
scale += n; |
464 |
|
sprintf(buf, "%+d", scale); |
465 |
< |
XDrawImageString(thedisplay, wind, ourgc, box.xmin, box.ymin, |
466 |
< |
buf, strlen(buf)); |
465 |
> |
XDrawImageString(thedisplay, wind, ourgc, |
466 |
> |
box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); |
467 |
|
XFlush(thedisplay); |
468 |
|
free(ourdata); |
469 |
|
free_raster(ourras); |
482 |
|
redraw(box.xmin, box.ymin, box.xsiz, box.ysiz); |
483 |
|
return(0); |
484 |
|
default: |
485 |
< |
XBell(0); |
485 |
> |
XBell(thedisplay, 0); |
486 |
|
return(-1); |
487 |
|
} |
488 |
|
} |
489 |
|
|
490 |
|
|
491 |
< |
moveimage(ep) /* shift the image */ |
492 |
< |
XButtonPressedEvent *ep; |
491 |
> |
moveimage(ebut) /* shift the image */ |
492 |
> |
XButtonPressedEvent *ebut; |
493 |
|
{ |
494 |
< |
XButtonPressedEvent eb; |
494 |
> |
union { |
495 |
> |
XEvent u; |
496 |
> |
XButtonReleasedEvent b; |
497 |
> |
XPointerMovedEvent m; |
498 |
> |
} e; |
499 |
> |
int mxo, myo; |
500 |
|
|
501 |
< |
XMaskEvent(thedisplay, ButtonReleaseMask, &eb); |
502 |
< |
xoff += eb.x - ep->x; |
503 |
< |
yoff += eb.y - ep->y; |
501 |
> |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); |
502 |
> |
while (e.u.type == MotionNotify) { |
503 |
> |
mxo = e.m.x; |
504 |
> |
myo = e.m.y; |
505 |
> |
revline(ebut->x, ebut->y, mxo, myo); |
506 |
> |
revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, |
507 |
> |
xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); |
508 |
> |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); |
509 |
> |
revline(ebut->x, ebut->y, mxo, myo); |
510 |
> |
revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, |
511 |
> |
xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); |
512 |
> |
} |
513 |
> |
xoff += e.b.x - ebut->x; |
514 |
> |
yoff += e.b.y - ebut->y; |
515 |
|
XClearWindow(thedisplay, wind); |
516 |
|
redraw(0, 0, width, height); |
486 |
– |
return(0); |
517 |
|
} |
518 |
|
|
519 |
|
|
521 |
|
XButtonPressedEvent *ebut; |
522 |
|
{ |
523 |
|
union { |
524 |
< |
XEvent e; |
524 |
> |
XEvent u; |
525 |
|
XButtonReleasedEvent b; |
526 |
|
XPointerMovedEvent m; |
527 |
|
} e; |
528 |
|
|
529 |
< |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.e); |
530 |
< |
while (e.e.type == ButtonMotionMask) { |
529 |
> |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); |
530 |
> |
while (e.u.type == MotionNotify) { |
531 |
|
revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y); |
532 |
< |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, |
503 |
< |
&e.e); |
532 |
> |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); |
533 |
|
revbox(ebut->x, ebut->y, box.xmin, box.ymin); |
534 |
|
} |
535 |
|
box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x); |
552 |
|
revbox(x0, y0, x1, y1) /* draw box with reversed lines */ |
553 |
|
int x0, y0, x1, y1; |
554 |
|
{ |
555 |
< |
static GC mygc = 0; |
555 |
> |
revline(x0, y0, x1, y0); |
556 |
> |
revline(x0, y1, x1, y1); |
557 |
> |
revline(x0, y0, x0, y1); |
558 |
> |
revline(x1, y0, x1, y1); |
559 |
> |
} |
560 |
|
|
528 |
– |
if (mygc == 0) { |
529 |
– |
mygc = XCreateGC(thedisplay, wind, 0, 0); |
530 |
– |
XSetPlaneMask(thedisplay, mygc, ~0L); |
531 |
– |
XSetFunction(thedisplay, mygc, GXinvert); |
532 |
– |
} |
533 |
– |
XDrawLine(thedisplay, wind, mygc, x0, y0, x1, y0); |
534 |
– |
XDrawLine(thedisplay, wind, mygc, x0, y1, x1, y1); |
535 |
– |
XDrawLine(thedisplay, wind, mygc, x0, y0, x0, y1); |
536 |
– |
XDrawLine(thedisplay, wind, mygc, x1, y0, x1, y1); |
537 |
– |
} /* end of revbox */ |
561 |
|
|
539 |
– |
|
562 |
|
avgbox(clr) /* average color over current box */ |
563 |
|
COLOR clr; |
564 |
|
{ |
604 |
|
register unsigned char *dp; |
605 |
|
register int x, err; |
606 |
|
int y; |
585 |
– |
rgbpixel *inline; |
607 |
|
short *cerr; |
608 |
|
|
609 |
< |
if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL |
610 |
< |
|| (cerr = (short *)calloc(xmax,sizeof(short))) == NULL) |
590 |
< |
quit("out of memory in getmono"); |
609 |
> |
if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL) |
610 |
> |
quiterr("out of memory in getmono"); |
611 |
|
dp = ourdata - 1; |
612 |
|
for (y = 0; y < ymax; y++) { |
613 |
< |
picreadline3(y, inline); |
613 |
> |
if (getscan(y) < 0) |
614 |
> |
quiterr("seek error in getmono"); |
615 |
> |
normcolrs(scanline, xmax, scale); |
616 |
|
err = 0; |
617 |
|
for (x = 0; x < xmax; x++) { |
618 |
|
if (!(x&7)) |
619 |
|
*++dp = 0; |
620 |
< |
err += rgb_bright(&inline[x]) + cerr[x]; |
620 |
> |
err += normbright(scanline[x]) + cerr[x]; |
621 |
|
if (err > 127) |
622 |
|
err -= 255; |
623 |
|
else |
624 |
< |
*dp |= 1<<(x&07); |
624 |
> |
*dp |= 1<<(7-(x&07)); |
625 |
|
cerr[x] = err >>= 1; |
626 |
|
} |
627 |
|
} |
606 |
– |
free((char *)inline); |
628 |
|
free((char *)cerr); |
629 |
|
} |
630 |
|
|
632 |
|
getfull() /* get full (24-bit) data */ |
633 |
|
{ |
634 |
|
int y; |
635 |
< |
|
636 |
< |
for (y = 0; y < ymax; y++) |
637 |
< |
picreadline3(y, (rgbpixel *)(ourdata+y*xmax*3)); |
635 |
> |
register unsigned char *dp; |
636 |
> |
register int x; |
637 |
> |
/* set gamma correction */ |
638 |
> |
setcolrgam(gamcor); |
639 |
> |
/* read and convert file */ |
640 |
> |
dp = ourdata; |
641 |
> |
for (y = 0; y < ymax; y++) { |
642 |
> |
if (getscan(y) < 0) |
643 |
> |
quiterr("seek error in getfull"); |
644 |
> |
if (scale) |
645 |
> |
shiftcolrs(scanline, xmax, scale); |
646 |
> |
colrs_gambs(scanline, xmax); |
647 |
> |
for (x = 0; x < xmax; x++) { |
648 |
> |
*dp++ = scanline[x][RED]; |
649 |
> |
*dp++ = scanline[x][GRN]; |
650 |
> |
*dp++ = scanline[x][BLU]; |
651 |
> |
} |
652 |
> |
} |
653 |
|
} |
654 |
|
|
655 |
|
|
688 |
|
if (scanpos == NULL || scanpos[y] == -1) |
689 |
|
return(-1); |
690 |
|
if (fseek(fin, scanpos[y], 0) == -1) |
691 |
< |
quit("fseek error"); |
691 |
> |
quiterr("fseek error"); |
692 |
|
cury = y; |
693 |
|
} else if (scanpos != NULL) |
694 |
|
scanpos[y] = ftell(fin); |