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)) |
198 |
|
init() /* get data and open window */ |
199 |
|
{ |
200 |
|
register int i; |
194 |
– |
colormap ourmap; |
201 |
|
OpaqueFrame mainframe; |
202 |
|
char defgeom[32]; |
203 |
|
|
219 |
|
maxcolors = 1<<DisplayPlanes(); |
220 |
|
if (maxcolors > 4) maxcolors -= 2; |
221 |
|
} |
216 |
– |
ourras.width = xmax; |
217 |
– |
ourras.height = ymax; |
222 |
|
/* store image */ |
223 |
< |
if (maxcolors <= 2) { /* monochrome */ |
224 |
< |
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 |
< |
} |
223 |
> |
getras(); |
224 |
> |
|
225 |
|
mainframe.bdrwidth = BORWIDTH; |
226 |
|
mainframe.border = WhitePixmap; |
227 |
|
mainframe.background = BlackPixmap; |
274 |
|
} |
275 |
|
|
276 |
|
|
277 |
+ |
getras() /* get raster file */ |
278 |
+ |
{ |
279 |
+ |
colormap ourmap; |
280 |
+ |
|
281 |
+ |
ourras = (XRASTER *)calloc(1, sizeof(XRASTER)); |
282 |
+ |
if (ourras == NULL) |
283 |
+ |
goto memerr; |
284 |
+ |
ourras->width = xmax; |
285 |
+ |
ourras->height = ymax; |
286 |
+ |
if (maxcolors <= 2) { /* monochrome */ |
287 |
+ |
ourras->data.m = (unsigned short *)malloc(BitmapSize(xmax,ymax)); |
288 |
+ |
if (ourras->data.m == NULL) |
289 |
+ |
goto memerr; |
290 |
+ |
getmono(); |
291 |
+ |
} else { |
292 |
+ |
ourras->data.bz = (unsigned char *)malloc(BZPixmapSize(xmax,ymax)); |
293 |
+ |
if (ourras->data.bz == NULL) |
294 |
+ |
goto memerr; |
295 |
+ |
if (greyscale) |
296 |
+ |
biq(dither,maxcolors,1,ourmap); |
297 |
+ |
else |
298 |
+ |
ciq(dither,maxcolors,1,ourmap); |
299 |
+ |
if (init_rcolors(ourras, ourmap) == 0) |
300 |
+ |
goto memerr; |
301 |
+ |
} |
302 |
+ |
return; |
303 |
+ |
memerr: |
304 |
+ |
quit("out of memory"); |
305 |
+ |
} |
306 |
+ |
|
307 |
+ |
|
308 |
|
getevent() /* process the next event */ |
309 |
|
{ |
310 |
|
WindowInfo info; |
324 |
|
fixwindow(&e); |
325 |
|
break; |
326 |
|
case UnmapWindow: |
327 |
< |
unmap_rcolors(&ourras); |
327 |
> |
unmap_rcolors(ourras); |
328 |
|
break; |
329 |
|
case ButtonPressed: |
330 |
|
if (controlshift(&e)) |
347 |
|
int x, y; |
348 |
|
int w, h; |
349 |
|
{ |
350 |
< |
map_rcolors(&ourras); |
350 |
> |
if (ourras->ncolors && map_rcolors(ourras) == NULL) { |
351 |
> |
fprintf(stderr, "%s: cannot allocate colors\n", progname); |
352 |
> |
return(-1); |
353 |
> |
} |
354 |
|
if (fast) |
355 |
< |
make_rpixmap(&ourras); |
356 |
< |
return(patch_raster(wind,x-xoff,y-yoff,x,y,w,h,&ourras) ? 0 : -1); |
355 |
> |
make_rpixmap(ourras); |
356 |
> |
return(patch_raster(wind,x-xoff,y-yoff,x,y,w,h,ourras) ? 0 : -1); |
357 |
|
} |
358 |
|
|
359 |
|
|
362 |
|
{ |
363 |
|
char buf[80]; |
364 |
|
COLOR cval; |
365 |
+ |
Color cvx; |
366 |
|
char *cp; |
367 |
|
int n; |
368 |
+ |
double comp; |
369 |
|
FVECT rorg, rdir; |
370 |
|
|
371 |
|
cp = XLookupMapping(ekey, &n); |
373 |
|
return(0); |
374 |
|
switch (*cp) { /* interpret command */ |
375 |
|
case 'q': |
376 |
< |
case CTRL(D): /* quiterr */ |
376 |
> |
case CTRL(D): /* quit */ |
377 |
|
quit(0); |
378 |
|
case '\n': |
379 |
|
case '\r': |
387 |
|
sprintf(buf, "%-3g", intens(cval)/exposure); |
388 |
|
break; |
389 |
|
case 'l': /* luminance */ |
390 |
< |
sprintf(buf, "%-3gL", bright(cval)*683.0/exposure); |
390 |
> |
sprintf(buf, "%-3gn", bright(cval)*683.0/exposure); |
391 |
|
break; |
392 |
|
case 'c': /* color */ |
393 |
+ |
comp = pow(2.0, (double)scale); |
394 |
|
sprintf(buf, "(%.2f,%.2f,%.2f)", |
395 |
< |
colval(cval,RED), |
396 |
< |
colval(cval,GRN), |
397 |
< |
colval(cval,BLU)); |
395 |
> |
colval(cval,RED)*comp, |
396 |
> |
colval(cval,GRN)*comp, |
397 |
> |
colval(cval,BLU)*comp); |
398 |
|
break; |
399 |
|
} |
400 |
|
XText(wind, box.xmin, box.ymin, buf, strlen(buf), |
401 |
|
fontid, BlackPixel, WhitePixel); |
402 |
|
return(0); |
403 |
+ |
case 'i': /* identify (contour) */ |
404 |
+ |
if (ourras->pixels == NULL) |
405 |
+ |
return(-1); |
406 |
+ |
n = ourras->data.bz[ekey->x-xoff+BZPixmapSize(xmax,ekey->y-yoff)]; |
407 |
+ |
n = ourras->pmap[n]; |
408 |
+ |
cvx.pixel = ourras->cdefs[n].pixel; |
409 |
+ |
cvx.red = random() & 65535; |
410 |
+ |
cvx.green = random() & 65535; |
411 |
+ |
cvx.blue = random() & 65535; |
412 |
+ |
XStoreColor(&cvx); |
413 |
+ |
return(0); |
414 |
|
case 'p': /* position */ |
415 |
|
sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff); |
416 |
|
XText(wind, ekey->x, ekey->y, buf, strlen(buf), |
427 |
|
printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); |
428 |
|
fflush(stdout); |
429 |
|
return(0); |
430 |
+ |
case '=': /* adjust exposure */ |
431 |
+ |
if (avgbox(cval) == -1) |
432 |
+ |
return(-1); |
433 |
+ |
n = log(.5/bright(cval))/.69315 - scale; /* truncate */ |
434 |
+ |
if (n == 0) |
435 |
+ |
return(0); |
436 |
+ |
scale_rcolors(ourras, pow(2.0, (double)n)); |
437 |
+ |
scale += n; |
438 |
+ |
sprintf(buf, "%+d", scale); |
439 |
+ |
XText(wind, box.xmin, box.ymin, buf, strlen(buf), |
440 |
+ |
fontid, BlackPixel, WhitePixel); |
441 |
+ |
XFlush(); |
442 |
+ |
free_raster(ourras); |
443 |
+ |
getras(); |
444 |
+ |
/* fall through */ |
445 |
|
case CTRL(R): /* redraw */ |
446 |
+ |
case CTRL(L): |
447 |
+ |
unmap_rcolors(ourras); |
448 |
|
XClear(wind); |
449 |
|
return(redraw(0, 0, width, height)); |
450 |
|
case ' ': /* clear */ |
562 |
|
if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL |
563 |
|
|| (cerr = (short *)calloc(xmax,sizeof(short))) == NULL) |
564 |
|
quit("out of memory in getmono"); |
565 |
< |
dp = ourras.data.m - 1; |
565 |
> |
dp = ourras->data.m - 1; |
566 |
|
for (y = 0; y < ymax; y++) { |
567 |
|
picreadline3(y, inline); |
568 |
|
err = 0; |
582 |
|
} |
583 |
|
|
584 |
|
|
585 |
< |
init_rcolors(xr, cmap) /* assign color values */ |
585 |
> |
init_rcolors(xr, cmap) /* (re)assign color values */ |
586 |
|
register XRASTER *xr; |
587 |
|
colormap cmap; |
588 |
|
{ |
613 |
|
} |
614 |
|
|
615 |
|
|
616 |
+ |
scale_rcolors(xr, sf) /* scale color map */ |
617 |
+ |
register XRASTER *xr; |
618 |
+ |
double sf; |
619 |
+ |
{ |
620 |
+ |
register int i; |
621 |
+ |
long maxv; |
622 |
+ |
|
623 |
+ |
if (xr->pixels == NULL) |
624 |
+ |
return; |
625 |
+ |
|
626 |
+ |
sf = pow(sf, 1.0/gamcor); |
627 |
+ |
maxv = 65535/sf; |
628 |
+ |
|
629 |
+ |
for (i = xr->ncolors; i--; ) { |
630 |
+ |
xr->cdefs[i].red = xr->cdefs[i].red > maxv ? |
631 |
+ |
65535 : |
632 |
+ |
xr->cdefs[i].red * sf; |
633 |
+ |
xr->cdefs[i].green = xr->cdefs[i].green > maxv ? |
634 |
+ |
65535 : |
635 |
+ |
xr->cdefs[i].green * sf; |
636 |
+ |
xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ? |
637 |
+ |
65535 : |
638 |
+ |
xr->cdefs[i].blue * sf; |
639 |
+ |
} |
640 |
+ |
XStoreColors(xr->ncolors, xr->cdefs); |
641 |
+ |
} |
642 |
+ |
|
643 |
+ |
|
644 |
|
getscan(y) |
645 |
|
int y; |
646 |
|
{ |
665 |
|
int y; |
666 |
|
register rgbpixel *l3; |
667 |
|
{ |
668 |
< |
register BYTE *l4; |
669 |
< |
register int shift, c; |
587 |
< |
int i; |
588 |
< |
|
668 |
> |
register int i; |
669 |
> |
/* read scanline */ |
670 |
|
if (getscan(y) < 0) |
671 |
|
quiterr("cannot seek for picreadline"); |
672 |
|
/* convert scanline */ |
673 |
< |
for (l4=scanline[0], i=xmax; i--; l4+=4, l3++) { |
674 |
< |
shift = l4[EXP] - COLXS; |
675 |
< |
if (shift >= 8) { |
676 |
< |
l3->r = l3->g = l3->b = 255; |
677 |
< |
} else if (shift <= -8) { |
678 |
< |
l3->r = l3->g = l3->b = 0; |
679 |
< |
} else if (shift > 0) { |
680 |
< |
c = l4[RED] << shift; |
681 |
< |
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 |
< |
} |
673 |
> |
if (scale != 0) |
674 |
> |
for (i = 0; i < xmax; i++) |
675 |
> |
if (scanline[i][EXP]+scale >= 0) |
676 |
> |
scanline[i][EXP] += scale; |
677 |
> |
normcolrs(scanline, xmax); |
678 |
> |
for (i = 0; i < xmax; i++) { |
679 |
> |
l3[i].r = scanline[i][RED]; |
680 |
> |
l3[i].g = scanline[i][GRN]; |
681 |
> |
l3[i].b = scanline[i][BLU]; |
682 |
|
} |
683 |
|
} |
684 |
|
|
687 |
|
int y; |
688 |
|
pixel *l; |
689 |
|
{ |
690 |
< |
bcopy(l, ourras.data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1)); |
690 |
> |
bcopy(l, ourras->data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1)); |
691 |
|
} |
692 |
|
|
693 |
|
|
694 |
< |
picreadcm(map) /* do gamcor correction */ |
694 |
> |
picreadcm(map) /* do gamma correction */ |
695 |
|
colormap map; |
696 |
|
{ |
697 |
|
extern double pow(); |
707 |
|
picwritecm(map) /* handled elsewhere */ |
708 |
|
colormap map; |
709 |
|
{ |
710 |
+ |
#ifdef DEBUG |
711 |
+ |
register int i; |
712 |
+ |
|
713 |
+ |
for (i = 0; i < 256; i++) |
714 |
+ |
printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]); |
715 |
+ |
#endif |
716 |
|
} |