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 |
+ |
int wrongformat = 0; /* input in another format? */ |
81 |
+ |
|
82 |
+ |
GC revgc; /* graphics context with GXinvert */ |
83 |
+ |
|
84 |
|
XRASTER *ourras; /* our stored image */ |
85 |
|
unsigned char *ourdata; /* our image data */ |
86 |
|
|
143 |
|
default: |
144 |
|
goto userr; |
145 |
|
} |
146 |
+ |
else if (argv[i][0] == '=') |
147 |
+ |
geometry = argv[i]; |
148 |
|
else |
149 |
|
break; |
150 |
|
|
151 |
< |
if (argc-i == 1) { |
151 |
> |
if (i == argc-1) { |
152 |
|
fname = argv[i]; |
153 |
|
fin = fopen(fname, "r"); |
154 |
|
if (fin == NULL) { |
155 |
|
sprintf(errmsg, "can't open file \"%s\"", fname); |
156 |
|
quiterr(errmsg); |
157 |
|
} |
158 |
< |
} |
158 |
> |
} else if (i != argc) |
159 |
> |
goto userr; |
160 |
|
/* get header */ |
161 |
< |
getheader(fin, headline); |
161 |
> |
getheader(fin, headline, NULL); |
162 |
|
/* get picture dimensions */ |
163 |
< |
if (fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR)) |
164 |
< |
quiterr("bad picture size"); |
163 |
> |
if (wrongformat || fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR)) |
164 |
> |
quiterr("bad picture format"); |
165 |
|
/* set view parameters */ |
166 |
|
if (gotview && setview(&ourview) != NULL) |
167 |
|
gotview = 0; |
185 |
|
{ |
186 |
|
static char *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL}; |
187 |
|
register char **an; |
188 |
+ |
char fmt[32]; |
189 |
|
|
190 |
< |
if (!strncmp(s, "EXPOSURE=", 9)) |
191 |
< |
exposure *= atof(s+9); |
192 |
< |
else |
190 |
> |
if (isexpos(s)) |
191 |
> |
exposure *= exposval(s); |
192 |
> |
else if (isformat(s)) { |
193 |
> |
formatval(fmt, s); |
194 |
> |
wrongformat = strcmp(fmt, COLRFMT); |
195 |
> |
} else |
196 |
|
for (an = altname; *an != NULL; an++) |
197 |
|
if (!strncmp(*an, s, strlen(*an))) { |
198 |
< |
if (sscanview(&ourview, s+strlen(*an)) == 0) |
198 |
> |
if (sscanview(&ourview, s+strlen(*an)) > 0) |
199 |
|
gotview++; |
200 |
|
return; |
201 |
|
} |
204 |
|
|
205 |
|
init() /* get data and open window */ |
206 |
|
{ |
207 |
+ |
XSetWindowAttributes ourwinattr; |
208 |
|
XSizeHints oursizhints; |
209 |
|
register int i; |
210 |
|
|
217 |
|
} |
218 |
|
if ((thedisplay = XOpenDisplay(NULL)) == NULL) |
219 |
|
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"); |
220 |
|
if (maxcolors == 0) { /* get number of available colors */ |
221 |
< |
maxcolors = 1<<DisplayPlanes(thedisplay,ourscreen); |
221 |
> |
i = DisplayPlanes(thedisplay,ourscreen); |
222 |
> |
maxcolors = i > 8 ? 256 : 1<<i; |
223 |
|
if (maxcolors > 4) maxcolors -= 2; |
224 |
|
} |
225 |
+ |
/* store image */ |
226 |
+ |
getras(); |
227 |
+ |
/* open window */ |
228 |
+ |
ourwinattr.border_pixel = ourblack; |
229 |
+ |
ourwinattr.background_pixel = ourwhite; |
230 |
+ |
wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH, |
231 |
+ |
0, InputOutput, ourras->visual, |
232 |
+ |
CWBackPixel|CWBorderPixel, &ourwinattr); |
233 |
+ |
if (wind == 0) |
234 |
+ |
quiterr("can't create window"); |
235 |
+ |
width = xmax; |
236 |
+ |
height = ymax; |
237 |
|
fontid = XLoadFont(thedisplay, FONTNAME); |
238 |
|
if (fontid == 0) |
239 |
|
quiterr("can't get font"); |
240 |
|
XSetFont(thedisplay, ourgc, fontid); |
241 |
+ |
revgc = XCreateGC(thedisplay, wind, 0, 0); |
242 |
+ |
XSetFunction(thedisplay, revgc, GXinvert); |
243 |
|
XStoreName(thedisplay, wind, fname == NULL ? progname : fname); |
244 |
|
XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay, |
245 |
|
XC_diamond_cross)); |
246 |
|
if (geometry != NULL) { |
247 |
|
bzero((char *)&oursizhints, sizeof(oursizhints)); |
248 |
|
i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y, |
249 |
< |
&oursizhints.width, &oursizhints.height); |
250 |
< |
if (i & (XValue|YValue) == (XValue|YValue)) { |
249 |
> |
(unsigned *)&oursizhints.width, |
250 |
> |
(unsigned *)&oursizhints.height); |
251 |
> |
if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue)) |
252 |
> |
oursizhints.flags |= USSize; |
253 |
> |
else { |
254 |
> |
oursizhints.width = xmax; |
255 |
> |
oursizhints.height = ymax; |
256 |
> |
oursizhints.flags |= PSize; |
257 |
> |
} |
258 |
> |
if ((i&(XValue|YValue)) == (XValue|YValue)) { |
259 |
|
oursizhints.flags |= USPosition; |
260 |
|
if (i & XNegative) |
261 |
< |
oursizhints.x += DisplayWidth(thedisplay,ourscreen)-1; |
261 |
> |
oursizhints.x += DisplayWidth(thedisplay, |
262 |
> |
ourscreen)-1-oursizhints.width-2*BORWIDTH; |
263 |
|
if (i & YNegative) |
264 |
< |
oursizhints.y += DisplayHeight(thedisplay,ourscreen)-1; |
264 |
> |
oursizhints.y += DisplayHeight(thedisplay, |
265 |
> |
ourscreen)-1-oursizhints.height-2*BORWIDTH; |
266 |
|
} |
230 |
– |
if (i & (WidthValue|HeightValue) == (WidthValue|HeightValue)) |
231 |
– |
oursizhints.flags |= USSize; |
267 |
|
XSetNormalHints(thedisplay, wind, &oursizhints); |
268 |
|
} |
269 |
< |
/* store image */ |
270 |
< |
getras(); |
271 |
< |
XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask| |
237 |
< |
StructureNotifyMask|ButtonMotionMask| |
238 |
< |
KeyPressMask|ExposureMask); |
269 |
> |
XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask |
270 |
> |
|ButtonMotionMask|StructureNotifyMask |
271 |
> |
|KeyPressMask|ExposureMask); |
272 |
|
XMapWindow(thedisplay, wind); |
273 |
|
return; |
274 |
|
memerr: |
304 |
|
getras() /* get raster file */ |
305 |
|
{ |
306 |
|
colormap ourmap; |
274 |
– |
unsigned char rmap[256], gmap[256], bmap[256]; |
307 |
|
XVisualInfo vinfo; |
276 |
– |
register int i; |
308 |
|
|
309 |
|
if (maxcolors <= 2) { /* monochrome */ |
310 |
|
ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8)); |
315 |
|
if (ourras == NULL) |
316 |
|
goto fail; |
317 |
|
getmono(); |
318 |
< |
} else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo)) { |
318 |
> |
} else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo) |
319 |
> |
/* kludge for DirectColor */ |
320 |
> |
|| XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) { |
321 |
|
ourdata = (unsigned char *)malloc(xmax*ymax*3); |
322 |
|
if (ourdata == NULL) |
323 |
|
goto fail; |
338 |
|
biq(dither,maxcolors,1,ourmap); |
339 |
|
else |
340 |
|
ciq(dither,maxcolors,1,ourmap); |
341 |
< |
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) |
341 |
> |
if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0) |
342 |
|
goto fail; |
343 |
|
} |
344 |
|
return; |
345 |
|
fail: |
346 |
< |
quit("could not create raster image"); |
346 |
> |
quiterr("could not create raster image"); |
347 |
|
} |
348 |
|
|
349 |
|
|
387 |
|
} |
388 |
|
|
389 |
|
|
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 |
– |
|
390 |
|
docom(ekey) /* execute command */ |
391 |
|
XKeyPressedEvent *ekey; |
392 |
|
{ |
417 |
|
sprintf(buf, "%.3f", intens(cval)/exposure); |
418 |
|
break; |
419 |
|
case 'l': /* luminance */ |
420 |
< |
sprintf(buf, "%.0fn", bright(cval)*683.0/exposure); |
420 |
> |
sprintf(buf, "%.0fn", luminance(cval)/exposure); |
421 |
|
break; |
422 |
|
case 'c': /* color */ |
423 |
|
comp = pow(2.0, (double)scale); |
427 |
|
colval(cval,BLU)*comp); |
428 |
|
break; |
429 |
|
} |
430 |
< |
XDrawImageString(thedisplay, wind, ourgc, box.xmin, box.ymin, |
431 |
< |
buf, strlen(buf)); |
430 |
> |
XDrawImageString(thedisplay, wind, ourgc, |
431 |
> |
box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); |
432 |
|
return(0); |
433 |
|
case 'i': /* identify (contour) */ |
434 |
|
if (ourras->pixels == NULL) |
439 |
|
cvx.red = random() & 65535; |
440 |
|
cvx.green = random() & 65535; |
441 |
|
cvx.blue = random() & 65535; |
442 |
< |
XStoreColors(thedisplay, ourras->cmap, &cvx, 1); |
442 |
> |
cvx.flags = DoRed|DoGreen|DoBlue; |
443 |
> |
XStoreColor(thedisplay, ourras->cmap, &cvx); |
444 |
|
return(0); |
445 |
|
case 'p': /* position */ |
446 |
|
sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff); |
452 |
|
XBell(thedisplay, 0); |
453 |
|
return(-1); |
454 |
|
} |
455 |
< |
viewray(rorg, rdir, &ourview, |
455 |
> |
if (viewray(rorg, rdir, &ourview, |
456 |
|
(ekey->x-xoff+.5)/xmax, |
457 |
< |
(ymax-1-ekey->y+yoff+.5)/ymax); |
457 |
> |
(ymax-1-ekey->y+yoff+.5)/ymax) < 0) |
458 |
> |
return(-1); |
459 |
|
printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); |
460 |
|
printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); |
461 |
|
fflush(stdout); |
469 |
|
scale_rcolors(ourras, pow(2.0, (double)n)); |
470 |
|
scale += n; |
471 |
|
sprintf(buf, "%+d", scale); |
472 |
< |
XDrawImageString(thedisplay, wind, ourgc, box.xmin, box.ymin, |
473 |
< |
buf, strlen(buf)); |
472 |
> |
XDrawImageString(thedisplay, wind, ourgc, |
473 |
> |
box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); |
474 |
|
XFlush(thedisplay); |
475 |
|
free(ourdata); |
476 |
|
free_raster(ourras); |
489 |
|
redraw(box.xmin, box.ymin, box.xsiz, box.ysiz); |
490 |
|
return(0); |
491 |
|
default: |
492 |
< |
XBell(0); |
492 |
> |
XBell(thedisplay, 0); |
493 |
|
return(-1); |
494 |
|
} |
495 |
|
} |
496 |
|
|
497 |
|
|
498 |
< |
moveimage(ep) /* shift the image */ |
499 |
< |
XButtonPressedEvent *ep; |
498 |
> |
moveimage(ebut) /* shift the image */ |
499 |
> |
XButtonPressedEvent *ebut; |
500 |
|
{ |
501 |
< |
XButtonPressedEvent eb; |
501 |
> |
union { |
502 |
> |
XEvent u; |
503 |
> |
XButtonReleasedEvent b; |
504 |
> |
XPointerMovedEvent m; |
505 |
> |
} e; |
506 |
> |
int mxo, myo; |
507 |
|
|
508 |
< |
XMaskEvent(thedisplay, ButtonReleaseMask, &eb); |
509 |
< |
xoff += eb.x - ep->x; |
510 |
< |
yoff += eb.y - ep->y; |
508 |
> |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); |
509 |
> |
while (e.u.type == MotionNotify) { |
510 |
> |
mxo = e.m.x; |
511 |
> |
myo = e.m.y; |
512 |
> |
revline(ebut->x, ebut->y, mxo, myo); |
513 |
> |
revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, |
514 |
> |
xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); |
515 |
> |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); |
516 |
> |
revline(ebut->x, ebut->y, mxo, myo); |
517 |
> |
revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, |
518 |
> |
xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); |
519 |
> |
} |
520 |
> |
xoff += e.b.x - ebut->x; |
521 |
> |
yoff += e.b.y - ebut->y; |
522 |
|
XClearWindow(thedisplay, wind); |
523 |
|
redraw(0, 0, width, height); |
486 |
– |
return(0); |
524 |
|
} |
525 |
|
|
526 |
|
|
528 |
|
XButtonPressedEvent *ebut; |
529 |
|
{ |
530 |
|
union { |
531 |
< |
XEvent e; |
531 |
> |
XEvent u; |
532 |
|
XButtonReleasedEvent b; |
533 |
|
XPointerMovedEvent m; |
534 |
|
} e; |
535 |
|
|
536 |
< |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.e); |
537 |
< |
while (e.e.type == ButtonMotionMask) { |
536 |
> |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); |
537 |
> |
while (e.u.type == MotionNotify) { |
538 |
|
revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y); |
539 |
< |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, |
503 |
< |
&e.e); |
539 |
> |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); |
540 |
|
revbox(ebut->x, ebut->y, box.xmin, box.ymin); |
541 |
|
} |
542 |
|
box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x); |
559 |
|
revbox(x0, y0, x1, y1) /* draw box with reversed lines */ |
560 |
|
int x0, y0, x1, y1; |
561 |
|
{ |
562 |
< |
static GC mygc = 0; |
562 |
> |
revline(x0, y0, x1, y0); |
563 |
> |
revline(x0, y1, x1, y1); |
564 |
> |
revline(x0, y0, x0, y1); |
565 |
> |
revline(x1, y0, x1, y1); |
566 |
> |
} |
567 |
|
|
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 */ |
568 |
|
|
539 |
– |
|
569 |
|
avgbox(clr) /* average color over current box */ |
570 |
|
COLOR clr; |
571 |
|
{ |
611 |
|
register unsigned char *dp; |
612 |
|
register int x, err; |
613 |
|
int y; |
585 |
– |
rgbpixel *inline; |
614 |
|
short *cerr; |
615 |
|
|
616 |
< |
if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL |
617 |
< |
|| (cerr = (short *)calloc(xmax,sizeof(short))) == NULL) |
590 |
< |
quit("out of memory in getmono"); |
616 |
> |
if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL) |
617 |
> |
quiterr("out of memory in getmono"); |
618 |
|
dp = ourdata - 1; |
619 |
|
for (y = 0; y < ymax; y++) { |
620 |
< |
picreadline3(y, inline); |
620 |
> |
if (getscan(y) < 0) |
621 |
> |
quiterr("seek error in getmono"); |
622 |
> |
normcolrs(scanline, xmax, scale); |
623 |
|
err = 0; |
624 |
|
for (x = 0; x < xmax; x++) { |
625 |
|
if (!(x&7)) |
626 |
|
*++dp = 0; |
627 |
< |
err += rgb_bright(&inline[x]) + cerr[x]; |
627 |
> |
err += normbright(scanline[x]) + cerr[x]; |
628 |
|
if (err > 127) |
629 |
|
err -= 255; |
630 |
|
else |
631 |
< |
*dp |= 1<<(x&07); |
631 |
> |
*dp |= 1<<(7-(x&07)); |
632 |
|
cerr[x] = err >>= 1; |
633 |
|
} |
634 |
|
} |
606 |
– |
free((char *)inline); |
635 |
|
free((char *)cerr); |
636 |
|
} |
637 |
|
|
639 |
|
getfull() /* get full (24-bit) data */ |
640 |
|
{ |
641 |
|
int y; |
642 |
< |
|
643 |
< |
for (y = 0; y < ymax; y++) |
644 |
< |
picreadline3(y, (rgbpixel *)(ourdata+y*xmax*3)); |
642 |
> |
register unsigned char *dp; |
643 |
> |
register int x; |
644 |
> |
/* set gamma correction */ |
645 |
> |
setcolrgam(gamcor); |
646 |
> |
/* read and convert file */ |
647 |
> |
dp = ourdata; |
648 |
> |
for (y = 0; y < ymax; y++) { |
649 |
> |
if (getscan(y) < 0) |
650 |
> |
quiterr("seek error in getfull"); |
651 |
> |
if (scale) |
652 |
> |
shiftcolrs(scanline, xmax, scale); |
653 |
> |
colrs_gambs(scanline, xmax); |
654 |
> |
for (x = 0; x < xmax; x++) { |
655 |
> |
*dp++ = scanline[x][RED]; |
656 |
> |
*dp++ = scanline[x][GRN]; |
657 |
> |
*dp++ = scanline[x][BLU]; |
658 |
> |
} |
659 |
> |
} |
660 |
|
} |
661 |
|
|
662 |
|
|
695 |
|
if (scanpos == NULL || scanpos[y] == -1) |
696 |
|
return(-1); |
697 |
|
if (fseek(fin, scanpos[y], 0) == -1) |
698 |
< |
quit("fseek error"); |
698 |
> |
quiterr("fseek error"); |
699 |
|
cury = y; |
700 |
|
} else if (scanpos != NULL) |
701 |
|
scanpos[y] = ftell(fin); |
741 |
|
register int i, val; |
742 |
|
|
743 |
|
for (i = 0; i < 256; i++) { |
744 |
< |
val = pow(i/256.0, 1.0/gamcor) * 256.0; |
744 |
> |
val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; |
745 |
|
map[0][i] = map[1][i] = map[2][i] = val; |
746 |
|
} |
747 |
|
} |