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 |
|
|
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) { |
161 |
|
getevent(); /* main loop */ |
162 |
|
userr: |
163 |
|
fprintf(stderr, |
164 |
< |
"Usage: %s [=geometry][-b][-m][-d][-f][-c ncolors] file\n", |
164 |
> |
"Usage: %s [=geometry][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", |
165 |
|
progname); |
166 |
|
quit(1); |
167 |
|
} |
170 |
|
headline(s) /* get relevant info from header */ |
171 |
|
char *s; |
172 |
|
{ |
173 |
< |
static char *altname[] = {"rview","rpict","VIEW=",NULL}; |
173 |
> |
static char *altname[] = {"rview","rpict",VIEWSTR,NULL}; |
174 |
|
register char **an; |
175 |
|
|
176 |
|
if (!strncmp(s, "EXPOSURE=", 9)) |
185 |
|
} |
186 |
|
|
187 |
|
|
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 |
– |
|
188 |
|
init() /* get data and open window */ |
189 |
|
{ |
190 |
|
register int i; |
194 |
– |
colormap ourmap; |
191 |
|
OpaqueFrame mainframe; |
192 |
|
char defgeom[32]; |
193 |
|
|
209 |
|
maxcolors = 1<<DisplayPlanes(); |
210 |
|
if (maxcolors > 4) maxcolors -= 2; |
211 |
|
} |
216 |
– |
ourras.width = xmax; |
217 |
– |
ourras.height = ymax; |
212 |
|
/* store image */ |
213 |
< |
if (maxcolors <= 2) { /* monochrome */ |
214 |
< |
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 |
< |
} |
213 |
> |
getras(); |
214 |
> |
|
215 |
|
mainframe.bdrwidth = BORWIDTH; |
216 |
|
mainframe.border = WhitePixmap; |
217 |
|
mainframe.background = BlackPixmap; |
264 |
|
} |
265 |
|
|
266 |
|
|
267 |
+ |
getras() /* get raster file */ |
268 |
+ |
{ |
269 |
+ |
colormap ourmap; |
270 |
+ |
|
271 |
+ |
ourras = (XRASTER *)calloc(1, sizeof(XRASTER)); |
272 |
+ |
if (ourras == NULL) |
273 |
+ |
goto memerr; |
274 |
+ |
ourras->width = xmax; |
275 |
+ |
ourras->height = ymax; |
276 |
+ |
if (maxcolors <= 2) { /* monochrome */ |
277 |
+ |
ourras->data.m = (unsigned short *)malloc(BitmapSize(xmax,ymax)); |
278 |
+ |
if (ourras->data.m == NULL) |
279 |
+ |
goto memerr; |
280 |
+ |
getmono(); |
281 |
+ |
} else { |
282 |
+ |
ourras->data.bz = (unsigned char *)malloc(BZPixmapSize(xmax,ymax)); |
283 |
+ |
if (ourras->data.bz == NULL) |
284 |
+ |
goto memerr; |
285 |
+ |
if (greyscale) |
286 |
+ |
biq(dither,maxcolors,1,ourmap); |
287 |
+ |
else |
288 |
+ |
ciq(dither,maxcolors,1,ourmap); |
289 |
+ |
if (init_rcolors(ourras, ourmap) == 0) |
290 |
+ |
goto memerr; |
291 |
+ |
} |
292 |
+ |
return; |
293 |
+ |
memerr: |
294 |
+ |
quit("out of memory"); |
295 |
+ |
} |
296 |
+ |
|
297 |
+ |
|
298 |
|
getevent() /* process the next event */ |
299 |
|
{ |
300 |
|
WindowInfo info; |
314 |
|
fixwindow(&e); |
315 |
|
break; |
316 |
|
case UnmapWindow: |
317 |
< |
unmap_rcolors(&ourras); |
317 |
> |
unmap_rcolors(ourras); |
318 |
|
break; |
319 |
|
case ButtonPressed: |
320 |
|
if (controlshift(&e)) |
337 |
|
int x, y; |
338 |
|
int w, h; |
339 |
|
{ |
340 |
< |
map_rcolors(&ourras); |
340 |
> |
if (ourras->ncolors && map_rcolors(ourras) == NULL) { |
341 |
> |
fprintf(stderr, "%s: cannot allocate colors\n", progname); |
342 |
> |
return(-1); |
343 |
> |
} |
344 |
|
if (fast) |
345 |
< |
make_rpixmap(&ourras); |
346 |
< |
return(patch_raster(wind,x-xoff,y-yoff,x,y,w,h,&ourras) ? 0 : -1); |
345 |
> |
make_rpixmap(ourras); |
346 |
> |
return(patch_raster(wind,x-xoff,y-yoff,x,y,w,h,ourras) ? 0 : -1); |
347 |
|
} |
348 |
|
|
349 |
|
|
352 |
|
{ |
353 |
|
char buf[80]; |
354 |
|
COLOR cval; |
355 |
+ |
Color cvx; |
356 |
|
char *cp; |
357 |
|
int n; |
358 |
+ |
double comp; |
359 |
|
FVECT rorg, rdir; |
360 |
|
|
361 |
|
cp = XLookupMapping(ekey, &n); |
363 |
|
return(0); |
364 |
|
switch (*cp) { /* interpret command */ |
365 |
|
case 'q': |
366 |
< |
case CTRL(D): /* quiterr */ |
366 |
> |
case CTRL(D): /* quit */ |
367 |
|
quit(0); |
368 |
|
case '\n': |
369 |
|
case '\r': |
374 |
|
switch (*cp) { |
375 |
|
case '\n': |
376 |
|
case '\r': /* radiance */ |
377 |
< |
sprintf(buf, "%-3g", intens(cval)/exposure); |
377 |
> |
sprintf(buf, "%.3f", intens(cval)/exposure); |
378 |
|
break; |
379 |
|
case 'l': /* luminance */ |
380 |
< |
sprintf(buf, "%-3gL", bright(cval)*683.0/exposure); |
380 |
> |
sprintf(buf, "%.0fn", bright(cval)*683.0/exposure); |
381 |
|
break; |
382 |
|
case 'c': /* color */ |
383 |
+ |
comp = pow(2.0, (double)scale); |
384 |
|
sprintf(buf, "(%.2f,%.2f,%.2f)", |
385 |
< |
colval(cval,RED), |
386 |
< |
colval(cval,GRN), |
387 |
< |
colval(cval,BLU)); |
385 |
> |
colval(cval,RED)*comp, |
386 |
> |
colval(cval,GRN)*comp, |
387 |
> |
colval(cval,BLU)*comp); |
388 |
|
break; |
389 |
|
} |
390 |
|
XText(wind, box.xmin, box.ymin, buf, strlen(buf), |
391 |
|
fontid, BlackPixel, WhitePixel); |
392 |
|
return(0); |
393 |
+ |
case 'i': /* identify (contour) */ |
394 |
+ |
if (ourras->pixels == NULL) |
395 |
+ |
return(-1); |
396 |
+ |
n = ourras->data.bz[ekey->x-xoff+BZPixmapSize(xmax,ekey->y-yoff)]; |
397 |
+ |
n = ourras->pmap[n]; |
398 |
+ |
cvx.pixel = ourras->cdefs[n].pixel; |
399 |
+ |
cvx.red = random() & 65535; |
400 |
+ |
cvx.green = random() & 65535; |
401 |
+ |
cvx.blue = random() & 65535; |
402 |
+ |
XStoreColor(&cvx); |
403 |
+ |
return(0); |
404 |
|
case 'p': /* position */ |
405 |
|
sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff); |
406 |
|
XText(wind, ekey->x, ekey->y, buf, strlen(buf), |
417 |
|
printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); |
418 |
|
fflush(stdout); |
419 |
|
return(0); |
420 |
+ |
case '=': /* adjust exposure */ |
421 |
+ |
if (avgbox(cval) == -1) |
422 |
+ |
return(-1); |
423 |
+ |
n = log(.5/bright(cval))/.69315 - scale; /* truncate */ |
424 |
+ |
if (n == 0) |
425 |
+ |
return(0); |
426 |
+ |
scale_rcolors(ourras, pow(2.0, (double)n)); |
427 |
+ |
scale += n; |
428 |
+ |
sprintf(buf, "%+d", scale); |
429 |
+ |
XText(wind, box.xmin, box.ymin, buf, strlen(buf), |
430 |
+ |
fontid, BlackPixel, WhitePixel); |
431 |
+ |
XFlush(); |
432 |
+ |
free_raster(ourras); |
433 |
+ |
getras(); |
434 |
+ |
/* fall through */ |
435 |
|
case CTRL(R): /* redraw */ |
436 |
+ |
case CTRL(L): |
437 |
+ |
unmap_rcolors(ourras); |
438 |
|
XClear(wind); |
439 |
|
return(redraw(0, 0, width, height)); |
440 |
|
case ' ': /* clear */ |
552 |
|
if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL |
553 |
|
|| (cerr = (short *)calloc(xmax,sizeof(short))) == NULL) |
554 |
|
quit("out of memory in getmono"); |
555 |
< |
dp = ourras.data.m - 1; |
555 |
> |
dp = ourras->data.m - 1; |
556 |
|
for (y = 0; y < ymax; y++) { |
557 |
|
picreadline3(y, inline); |
558 |
|
err = 0; |
572 |
|
} |
573 |
|
|
574 |
|
|
575 |
< |
init_rcolors(xr, cmap) /* assign color values */ |
575 |
> |
init_rcolors(xr, cmap) /* (re)assign color values */ |
576 |
|
register XRASTER *xr; |
577 |
|
colormap cmap; |
578 |
|
{ |
603 |
|
} |
604 |
|
|
605 |
|
|
606 |
+ |
scale_rcolors(xr, sf) /* scale color map */ |
607 |
+ |
register XRASTER *xr; |
608 |
+ |
double sf; |
609 |
+ |
{ |
610 |
+ |
register int i; |
611 |
+ |
long maxv; |
612 |
+ |
|
613 |
+ |
if (xr->pixels == NULL) |
614 |
+ |
return; |
615 |
+ |
|
616 |
+ |
sf = pow(sf, 1.0/gamcor); |
617 |
+ |
maxv = 65535/sf; |
618 |
+ |
|
619 |
+ |
for (i = xr->ncolors; i--; ) { |
620 |
+ |
xr->cdefs[i].red = xr->cdefs[i].red > maxv ? |
621 |
+ |
65535 : |
622 |
+ |
xr->cdefs[i].red * sf; |
623 |
+ |
xr->cdefs[i].green = xr->cdefs[i].green > maxv ? |
624 |
+ |
65535 : |
625 |
+ |
xr->cdefs[i].green * sf; |
626 |
+ |
xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ? |
627 |
+ |
65535 : |
628 |
+ |
xr->cdefs[i].blue * sf; |
629 |
+ |
} |
630 |
+ |
XStoreColors(xr->ncolors, xr->cdefs); |
631 |
+ |
} |
632 |
+ |
|
633 |
+ |
|
634 |
|
getscan(y) |
635 |
|
int y; |
636 |
|
{ |
655 |
|
int y; |
656 |
|
register rgbpixel *l3; |
657 |
|
{ |
658 |
< |
register BYTE *l4; |
659 |
< |
register int shift, c; |
587 |
< |
int i; |
588 |
< |
|
658 |
> |
register int i; |
659 |
> |
/* read scanline */ |
660 |
|
if (getscan(y) < 0) |
661 |
|
quiterr("cannot seek for picreadline"); |
662 |
|
/* convert scanline */ |
663 |
< |
for (l4=scanline[0], i=xmax; i--; l4+=4, l3++) { |
664 |
< |
shift = l4[EXP] - COLXS; |
665 |
< |
if (shift >= 8) { |
666 |
< |
l3->r = l3->g = l3->b = 255; |
667 |
< |
} else if (shift <= -8) { |
668 |
< |
l3->r = l3->g = l3->b = 0; |
669 |
< |
} else if (shift > 0) { |
670 |
< |
c = l4[RED] << shift; |
671 |
< |
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 |
< |
} |
663 |
> |
if (scale) |
664 |
> |
for (i = 0; i < xmax; i++) |
665 |
> |
if (scanline[i][EXP]) |
666 |
> |
scanline[i][EXP] += scale; |
667 |
> |
normcolrs(scanline, xmax); |
668 |
> |
for (i = 0; i < xmax; i++) { |
669 |
> |
l3[i].r = scanline[i][RED]; |
670 |
> |
l3[i].g = scanline[i][GRN]; |
671 |
> |
l3[i].b = scanline[i][BLU]; |
672 |
|
} |
673 |
|
} |
674 |
|
|
677 |
|
int y; |
678 |
|
pixel *l; |
679 |
|
{ |
680 |
< |
bcopy(l, ourras.data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1)); |
680 |
> |
bcopy(l, ourras->data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1)); |
681 |
|
} |
682 |
|
|
683 |
|
|
684 |
< |
picreadcm(map) /* do gamcor correction */ |
684 |
> |
picreadcm(map) /* do gamma correction */ |
685 |
|
colormap map; |
686 |
|
{ |
687 |
|
extern double pow(); |
697 |
|
picwritecm(map) /* handled elsewhere */ |
698 |
|
colormap map; |
699 |
|
{ |
700 |
+ |
#ifdef DEBUG |
701 |
+ |
register int i; |
702 |
+ |
|
703 |
+ |
for (i = 0; i < 256; i++) |
704 |
+ |
printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]); |
705 |
+ |
#endif |
706 |
|
} |