29 |
|
|
30 |
|
#include "pic.h" |
31 |
|
|
32 |
+ |
#include "random.h" |
33 |
+ |
|
34 |
|
#define controlshift(e) (((XButtonEvent *)(e))->detail & (ShiftMask|ControlMask)) |
35 |
|
|
36 |
|
#define FONTNAME "9x15" /* text font we'll use */ |
40 |
|
#define BORWIDTH 5 /* border width */ |
41 |
|
#define BARHEIGHT 25 /* menu bar size */ |
42 |
|
|
43 |
< |
double gamcor = 2.0; /* gamcor correction */ |
43 |
> |
double gamcor = 2.2; /* gamma correction */ |
44 |
|
|
45 |
< |
XRASTER ourras; /* our stored raster image */ |
45 |
> |
XRASTER *ourras = NULL; /* our stored raster image */ |
46 |
|
|
47 |
|
int dither = 1; /* dither colors? */ |
48 |
|
int fast = 0; /* keep picture in Pixmap? */ |
53 |
|
int maxcolors = 0; /* maximum colors */ |
54 |
|
int greyscale = 0; /* in grey */ |
55 |
|
|
56 |
+ |
int scale = 0; /* scalefactor; power of two */ |
57 |
+ |
|
58 |
|
int xoff = 0; /* x image offset */ |
59 |
|
int yoff = 0; /* y image offset */ |
60 |
|
|
61 |
< |
VIEW ourview = STDVIEW(0); /* image view parameters */ |
61 |
> |
VIEW ourview = STDVIEW; /* image view parameters */ |
62 |
|
int gotview = 0; /* got parameters from file */ |
63 |
|
|
64 |
|
COLR *scanline; /* scan line buffer */ |
86 |
|
|
87 |
|
extern char *malloc(), *calloc(); |
88 |
|
|
89 |
< |
extern double atof(); |
89 |
> |
extern double atof(), pow(), log(); |
90 |
|
|
91 |
|
|
92 |
|
main(argc, argv) |
116 |
|
case 'f': |
117 |
|
fast = !fast; |
118 |
|
break; |
119 |
+ |
case 'e': |
120 |
+ |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
121 |
+ |
goto userr; |
122 |
+ |
scale = atoi(argv[++i]); |
123 |
+ |
break; |
124 |
|
case 'g': |
125 |
|
gamcor = atof(argv[++i]); |
126 |
|
break; |
139 |
|
sprintf(errmsg, "can't open file \"%s\"", fname); |
140 |
|
quiterr(errmsg); |
141 |
|
} |
142 |
< |
} else |
134 |
< |
goto userr; |
135 |
< |
|
142 |
> |
} |
143 |
|
/* get header */ |
144 |
|
getheader(fin, headline); |
145 |
|
/* get picture dimensions */ |
146 |
< |
if (fscanf(fin, "-Y %d +X %d\n", &ymax, &xmax) != 2) |
146 |
> |
if (fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR)) |
147 |
|
quiterr("bad picture size"); |
148 |
|
/* set view parameters */ |
149 |
< |
if (gotview) { |
150 |
< |
ourview.hresolu = xmax; |
144 |
< |
ourview.vresolu = ymax; |
145 |
< |
if (setview(&ourview) != NULL) |
146 |
< |
gotview = 0; |
147 |
< |
} |
149 |
> |
if (gotview && setview(&ourview) != NULL) |
150 |
> |
gotview = 0; |
151 |
|
if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) |
152 |
|
quiterr("out of memory"); |
153 |
|
|
157 |
|
getevent(); /* main loop */ |
158 |
|
userr: |
159 |
|
fprintf(stderr, |
160 |
< |
"Usage: %s [=geometry][-b][-m][-d][-f][-c ncolors] file\n", |
160 |
> |
"Usage: %s [=geometry][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", |
161 |
|
progname); |
162 |
|
quit(1); |
163 |
|
} |
166 |
|
headline(s) /* get relevant info from header */ |
167 |
|
char *s; |
168 |
|
{ |
169 |
< |
static char *altname[] = {"rview","rpict","VIEW=",NULL}; |
169 |
> |
static char *altname[] = {"rview","rpict",VIEWSTR,NULL}; |
170 |
|
register char **an; |
171 |
|
|
172 |
< |
if (!strncmp(s, "EXPOSURE=", 9)) |
173 |
< |
exposure *= atof(s+9); |
172 |
> |
if (isexpos(s)) |
173 |
> |
exposure *= exposval(s); |
174 |
|
else |
175 |
|
for (an = altname; *an != NULL; an++) |
176 |
|
if (!strncmp(*an, s, strlen(*an))) { |
177 |
< |
if (sscanview(&ourview, s+strlen(*an)) == 0) |
177 |
> |
if (sscanview(&ourview, s+strlen(*an)) > 0) |
178 |
|
gotview++; |
179 |
|
return; |
180 |
|
} |
181 |
|
} |
182 |
|
|
183 |
|
|
181 |
– |
char * |
182 |
– |
sskip(s) /* skip a word */ |
183 |
– |
register char *s; |
184 |
– |
{ |
185 |
– |
while (isspace(*s)) s++; |
186 |
– |
while (*s && !isspace(*s)) s++; |
187 |
– |
return(s); |
188 |
– |
} |
189 |
– |
|
190 |
– |
|
184 |
|
init() /* get data and open window */ |
185 |
|
{ |
186 |
|
register int i; |
194 |
– |
colormap ourmap; |
187 |
|
OpaqueFrame mainframe; |
188 |
|
char defgeom[32]; |
189 |
|
|
205 |
|
maxcolors = 1<<DisplayPlanes(); |
206 |
|
if (maxcolors > 4) maxcolors -= 2; |
207 |
|
} |
216 |
– |
ourras.width = xmax; |
217 |
– |
ourras.height = ymax; |
208 |
|
/* store image */ |
209 |
< |
if (maxcolors <= 2) { /* monochrome */ |
210 |
< |
ourras.data.m = (unsigned short *)malloc(BitmapSize(xmax,ymax)); |
221 |
< |
if (ourras.data.m == NULL) |
222 |
< |
goto memerr; |
223 |
< |
getmono(); |
224 |
< |
} else { |
225 |
< |
ourras.data.bz = (unsigned char *)malloc(BZPixmapSize(xmax,ymax)); |
226 |
< |
if (ourras.data.bz == NULL) |
227 |
< |
goto memerr; |
228 |
< |
if (greyscale) |
229 |
< |
biq(dither,maxcolors,1,ourmap); |
230 |
< |
else |
231 |
< |
ciq(dither,maxcolors,1,ourmap); |
232 |
< |
if (init_rcolors(&ourras, ourmap) == 0) |
233 |
< |
goto memerr; |
234 |
< |
} |
209 |
> |
getras(); |
210 |
> |
|
211 |
|
mainframe.bdrwidth = BORWIDTH; |
212 |
|
mainframe.border = WhitePixmap; |
213 |
|
mainframe.background = BlackPixmap; |
260 |
|
} |
261 |
|
|
262 |
|
|
263 |
+ |
getras() /* get raster file */ |
264 |
+ |
{ |
265 |
+ |
colormap ourmap; |
266 |
+ |
|
267 |
+ |
ourras = (XRASTER *)calloc(1, sizeof(XRASTER)); |
268 |
+ |
if (ourras == NULL) |
269 |
+ |
goto memerr; |
270 |
+ |
ourras->width = xmax; |
271 |
+ |
ourras->height = ymax; |
272 |
+ |
if (maxcolors <= 2) { /* monochrome */ |
273 |
+ |
ourras->data.m = (unsigned short *)malloc(BitmapSize(xmax,ymax)); |
274 |
+ |
if (ourras->data.m == NULL) |
275 |
+ |
goto memerr; |
276 |
+ |
getmono(); |
277 |
+ |
} else { |
278 |
+ |
ourras->data.bz = (unsigned char *)malloc(BZPixmapSize(xmax,ymax)); |
279 |
+ |
if (ourras->data.bz == NULL) |
280 |
+ |
goto memerr; |
281 |
+ |
if (greyscale) |
282 |
+ |
biq(dither,maxcolors,1,ourmap); |
283 |
+ |
else |
284 |
+ |
ciq(dither,maxcolors,1,ourmap); |
285 |
+ |
if (init_rcolors(ourras, ourmap) == 0) |
286 |
+ |
goto memerr; |
287 |
+ |
} |
288 |
+ |
return; |
289 |
+ |
memerr: |
290 |
+ |
quit("out of memory"); |
291 |
+ |
} |
292 |
+ |
|
293 |
+ |
|
294 |
|
getevent() /* process the next event */ |
295 |
|
{ |
296 |
|
WindowInfo info; |
310 |
|
fixwindow(&e); |
311 |
|
break; |
312 |
|
case UnmapWindow: |
313 |
< |
unmap_rcolors(&ourras); |
313 |
> |
unmap_rcolors(ourras); |
314 |
|
break; |
315 |
|
case ButtonPressed: |
316 |
|
if (controlshift(&e)) |
333 |
|
int x, y; |
334 |
|
int w, h; |
335 |
|
{ |
336 |
< |
map_rcolors(&ourras); |
336 |
> |
if (ourras->ncolors && map_rcolors(ourras) == NULL) { |
337 |
> |
fprintf(stderr, "%s: cannot allocate colors\n", progname); |
338 |
> |
return(-1); |
339 |
> |
} |
340 |
|
if (fast) |
341 |
< |
make_rpixmap(&ourras); |
342 |
< |
return(patch_raster(wind,x-xoff,y-yoff,x,y,w,h,&ourras) ? 0 : -1); |
341 |
> |
make_rpixmap(ourras); |
342 |
> |
return(patch_raster(wind,x-xoff,y-yoff,x,y,w,h,ourras) ? 0 : -1); |
343 |
|
} |
344 |
|
|
345 |
|
|
348 |
|
{ |
349 |
|
char buf[80]; |
350 |
|
COLOR cval; |
351 |
+ |
Color cvx; |
352 |
|
char *cp; |
353 |
|
int n; |
354 |
+ |
double comp; |
355 |
|
FVECT rorg, rdir; |
356 |
|
|
357 |
|
cp = XLookupMapping(ekey, &n); |
359 |
|
return(0); |
360 |
|
switch (*cp) { /* interpret command */ |
361 |
|
case 'q': |
362 |
< |
case CTRL(D): /* quiterr */ |
362 |
> |
case CTRL(D): /* quit */ |
363 |
|
quit(0); |
364 |
|
case '\n': |
365 |
|
case '\r': |
370 |
|
switch (*cp) { |
371 |
|
case '\n': |
372 |
|
case '\r': /* radiance */ |
373 |
< |
sprintf(buf, "%-3g", intens(cval)/exposure); |
373 |
> |
sprintf(buf, "%.3f", intens(cval)/exposure); |
374 |
|
break; |
375 |
|
case 'l': /* luminance */ |
376 |
< |
sprintf(buf, "%-3gL", bright(cval)*683.0/exposure); |
376 |
> |
sprintf(buf, "%.0fn", bright(cval)*683.0/exposure); |
377 |
|
break; |
378 |
|
case 'c': /* color */ |
379 |
+ |
comp = pow(2.0, (double)scale); |
380 |
|
sprintf(buf, "(%.2f,%.2f,%.2f)", |
381 |
< |
colval(cval,RED), |
382 |
< |
colval(cval,GRN), |
383 |
< |
colval(cval,BLU)); |
381 |
> |
colval(cval,RED)*comp, |
382 |
> |
colval(cval,GRN)*comp, |
383 |
> |
colval(cval,BLU)*comp); |
384 |
|
break; |
385 |
|
} |
386 |
|
XText(wind, box.xmin, box.ymin, buf, strlen(buf), |
387 |
|
fontid, BlackPixel, WhitePixel); |
388 |
|
return(0); |
389 |
+ |
case 'i': /* identify (contour) */ |
390 |
+ |
if (ourras->pixels == NULL) |
391 |
+ |
return(-1); |
392 |
+ |
n = ourras->data.bz[ekey->x-xoff+BZPixmapSize(xmax,ekey->y-yoff)]; |
393 |
+ |
n = ourras->pmap[n]; |
394 |
+ |
cvx.pixel = ourras->cdefs[n].pixel; |
395 |
+ |
cvx.red = random() & 65535; |
396 |
+ |
cvx.green = random() & 65535; |
397 |
+ |
cvx.blue = random() & 65535; |
398 |
+ |
XStoreColor(&cvx); |
399 |
+ |
return(0); |
400 |
|
case 'p': /* position */ |
401 |
|
sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff); |
402 |
|
XText(wind, ekey->x, ekey->y, buf, strlen(buf), |
407 |
|
XFeep(0); |
408 |
|
return(-1); |
409 |
|
} |
410 |
< |
rayview(rorg, rdir, &ourview, |
411 |
< |
ekey->x-xoff + .5, ymax-1-ekey->y+yoff + .5); |
410 |
> |
viewray(rorg, rdir, &ourview, (ekey->x-xoff+.5)/xmax, |
411 |
> |
(ymax-1-ekey->y+yoff+.5)/ymax); |
412 |
|
printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); |
413 |
|
printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); |
414 |
|
fflush(stdout); |
415 |
|
return(0); |
416 |
+ |
case '=': /* adjust exposure */ |
417 |
+ |
if (avgbox(cval) == -1) |
418 |
+ |
return(-1); |
419 |
+ |
n = log(.5/bright(cval))/.69315 - scale; /* truncate */ |
420 |
+ |
if (n == 0) |
421 |
+ |
return(0); |
422 |
+ |
scale_rcolors(ourras, pow(2.0, (double)n)); |
423 |
+ |
scale += n; |
424 |
+ |
sprintf(buf, "%+d", scale); |
425 |
+ |
XText(wind, box.xmin, box.ymin, buf, strlen(buf), |
426 |
+ |
fontid, BlackPixel, WhitePixel); |
427 |
+ |
XFlush(); |
428 |
+ |
free_raster(ourras); |
429 |
+ |
getras(); |
430 |
+ |
/* fall through */ |
431 |
|
case CTRL(R): /* redraw */ |
432 |
+ |
case CTRL(L): |
433 |
+ |
unmap_rcolors(ourras); |
434 |
|
XClear(wind); |
435 |
|
return(redraw(0, 0, width, height)); |
436 |
|
case ' ': /* clear */ |
548 |
|
if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL |
549 |
|
|| (cerr = (short *)calloc(xmax,sizeof(short))) == NULL) |
550 |
|
quit("out of memory in getmono"); |
551 |
< |
dp = ourras.data.m - 1; |
551 |
> |
dp = ourras->data.m - 1; |
552 |
|
for (y = 0; y < ymax; y++) { |
553 |
|
picreadline3(y, inline); |
554 |
|
err = 0; |
568 |
|
} |
569 |
|
|
570 |
|
|
571 |
< |
init_rcolors(xr, cmap) /* assign color values */ |
571 |
> |
init_rcolors(xr, cmap) /* (re)assign color values */ |
572 |
|
register XRASTER *xr; |
573 |
|
colormap cmap; |
574 |
|
{ |
599 |
|
} |
600 |
|
|
601 |
|
|
602 |
+ |
scale_rcolors(xr, sf) /* scale color map */ |
603 |
+ |
register XRASTER *xr; |
604 |
+ |
double sf; |
605 |
+ |
{ |
606 |
+ |
register int i; |
607 |
+ |
long maxv; |
608 |
+ |
|
609 |
+ |
if (xr->pixels == NULL) |
610 |
+ |
return; |
611 |
+ |
|
612 |
+ |
sf = pow(sf, 1.0/gamcor); |
613 |
+ |
maxv = 65535/sf; |
614 |
+ |
|
615 |
+ |
for (i = xr->ncolors; i--; ) { |
616 |
+ |
xr->cdefs[i].red = xr->cdefs[i].red > maxv ? |
617 |
+ |
65535 : |
618 |
+ |
xr->cdefs[i].red * sf; |
619 |
+ |
xr->cdefs[i].green = xr->cdefs[i].green > maxv ? |
620 |
+ |
65535 : |
621 |
+ |
xr->cdefs[i].green * sf; |
622 |
+ |
xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ? |
623 |
+ |
65535 : |
624 |
+ |
xr->cdefs[i].blue * sf; |
625 |
+ |
} |
626 |
+ |
XStoreColors(xr->ncolors, xr->cdefs); |
627 |
+ |
} |
628 |
+ |
|
629 |
+ |
|
630 |
|
getscan(y) |
631 |
|
int y; |
632 |
|
{ |
651 |
|
int y; |
652 |
|
register rgbpixel *l3; |
653 |
|
{ |
654 |
< |
register BYTE *l4; |
655 |
< |
register int shift, c; |
587 |
< |
int i; |
588 |
< |
|
654 |
> |
register int i; |
655 |
> |
/* read scanline */ |
656 |
|
if (getscan(y) < 0) |
657 |
|
quiterr("cannot seek for picreadline"); |
658 |
|
/* convert scanline */ |
659 |
< |
for (l4=scanline[0], i=xmax; i--; l4+=4, l3++) { |
660 |
< |
shift = l4[EXP] - COLXS; |
661 |
< |
if (shift >= 8) { |
662 |
< |
l3->r = l3->g = l3->b = 255; |
663 |
< |
} else if (shift <= -8) { |
664 |
< |
l3->r = l3->g = l3->b = 0; |
665 |
< |
} else if (shift > 0) { |
666 |
< |
c = l4[RED] << shift; |
667 |
< |
l3->r = c > 255 ? 255 : c; |
601 |
< |
c = l4[GRN] << shift; |
602 |
< |
l3->g = c > 255 ? 255 : c; |
603 |
< |
c = l4[BLU] << shift; |
604 |
< |
l3->b = c > 255 ? 255 : c; |
605 |
< |
} else if (shift < 0) { |
606 |
< |
l3->r = l4[RED] >> -shift; |
607 |
< |
l3->g = l4[GRN] >> -shift; |
608 |
< |
l3->b = l4[BLU] >> -shift; |
609 |
< |
} else { |
610 |
< |
l3->r = l4[RED]; |
611 |
< |
l3->g = l4[GRN]; |
612 |
< |
l3->b = l4[BLU]; |
613 |
< |
} |
659 |
> |
if (scale) |
660 |
> |
for (i = 0; i < xmax; i++) |
661 |
> |
if (scanline[i][EXP]) |
662 |
> |
scanline[i][EXP] += scale; |
663 |
> |
normcolrs(scanline, xmax); |
664 |
> |
for (i = 0; i < xmax; i++) { |
665 |
> |
l3[i].r = scanline[i][RED]; |
666 |
> |
l3[i].g = scanline[i][GRN]; |
667 |
> |
l3[i].b = scanline[i][BLU]; |
668 |
|
} |
669 |
|
} |
670 |
|
|
673 |
|
int y; |
674 |
|
pixel *l; |
675 |
|
{ |
676 |
< |
bcopy(l, ourras.data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1)); |
676 |
> |
bcopy(l, ourras->data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1)); |
677 |
|
} |
678 |
|
|
679 |
|
|
680 |
< |
picreadcm(map) /* do gamcor correction */ |
680 |
> |
picreadcm(map) /* do gamma correction */ |
681 |
|
colormap map; |
682 |
|
{ |
683 |
|
extern double pow(); |
693 |
|
picwritecm(map) /* handled elsewhere */ |
694 |
|
colormap map; |
695 |
|
{ |
696 |
+ |
#ifdef DEBUG |
697 |
+ |
register int i; |
698 |
+ |
|
699 |
+ |
for (i = 0; i < 256; i++) |
700 |
+ |
printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]); |
701 |
+ |
#endif |
702 |
|
} |