23 |
|
|
24 |
|
#include "color.h" |
25 |
|
|
26 |
+ |
#include "resolu.h" |
27 |
+ |
|
28 |
|
#include "xraster.h" |
29 |
|
|
30 |
|
#include "view.h" |
31 |
|
|
32 |
|
#include "pic.h" |
33 |
|
|
34 |
+ |
#include "random.h" |
35 |
+ |
|
36 |
|
#define controlshift(e) (((XButtonEvent *)(e))->detail & (ShiftMask|ControlMask)) |
37 |
|
|
38 |
|
#define FONTNAME "9x15" /* text font we'll use */ |
39 |
|
|
40 |
< |
#define CTRL(c) ('c'-'@') |
40 |
> |
#define CTRL(c) ((c)-'@') |
41 |
|
|
42 |
|
#define BORWIDTH 5 /* border width */ |
43 |
|
#define BARHEIGHT 25 /* menu bar size */ |
44 |
|
|
45 |
< |
double gamcor = 2.0; /* gamcor correction */ |
45 |
> |
double gamcor = 2.2; /* gamma correction */ |
46 |
|
|
47 |
< |
XRASTER ourras; /* our stored raster image */ |
47 |
> |
XRASTER *ourras = NULL; /* our stored raster image */ |
48 |
|
|
49 |
|
int dither = 1; /* dither colors? */ |
50 |
|
int fast = 0; /* keep picture in Pixmap? */ |
55 |
|
int maxcolors = 0; /* maximum colors */ |
56 |
|
int greyscale = 0; /* in grey */ |
57 |
|
|
58 |
+ |
int scale = 0; /* scalefactor; power of two */ |
59 |
+ |
|
60 |
|
int xoff = 0; /* x image offset */ |
61 |
|
int yoff = 0; /* y image offset */ |
62 |
|
|
63 |
< |
VIEW ourview = STDVIEW(0); /* image view parameters */ |
63 |
> |
VIEW ourview = STDVIEW; /* image view parameters */ |
64 |
|
int gotview = 0; /* got parameters from file */ |
65 |
|
|
66 |
|
COLR *scanline; /* scan line buffer */ |
74 |
|
|
75 |
|
double exposure = 1.0; /* exposure compensation used */ |
76 |
|
|
77 |
+ |
int wrongformat = 0; /* input in another format */ |
78 |
+ |
|
79 |
|
struct { |
80 |
|
int xmin, ymin, xsiz, ysiz; |
81 |
|
} box = {0, 0, 0, 0}; /* current box */ |
90 |
|
|
91 |
|
extern char *malloc(), *calloc(); |
92 |
|
|
93 |
< |
extern double atof(); |
93 |
> |
extern double pow(), log(); |
94 |
|
|
95 |
|
|
96 |
|
main(argc, argv) |
97 |
|
int argc; |
98 |
|
char *argv[]; |
99 |
|
{ |
100 |
+ |
extern char *getenv(); |
101 |
+ |
char *gv; |
102 |
|
int headline(); |
103 |
|
int i; |
104 |
|
|
105 |
|
progname = argv[0]; |
106 |
+ |
if ((gv = getenv("DISPLAY_GAMMA")) != NULL) |
107 |
+ |
gamcor = atof(gv); |
108 |
|
|
109 |
|
for (i = 1; i < argc; i++) |
110 |
|
if (argv[i][0] == '-') |
124 |
|
case 'f': |
125 |
|
fast = !fast; |
126 |
|
break; |
127 |
+ |
case 'e': |
128 |
+ |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
129 |
+ |
goto userr; |
130 |
+ |
scale = atoi(argv[++i]); |
131 |
+ |
break; |
132 |
|
case 'g': |
133 |
|
gamcor = atof(argv[++i]); |
134 |
|
break; |
147 |
|
sprintf(errmsg, "can't open file \"%s\"", fname); |
148 |
|
quiterr(errmsg); |
149 |
|
} |
150 |
< |
} else |
134 |
< |
goto userr; |
135 |
< |
|
150 |
> |
} |
151 |
|
/* get header */ |
152 |
< |
getheader(fin, headline); |
152 |
> |
getheader(fin, headline, NULL); |
153 |
|
/* get picture dimensions */ |
154 |
< |
if (fscanf(fin, "-Y %d +X %d\n", &ymax, &xmax) != 2) |
154 |
> |
if (wrongformat || fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR)) |
155 |
|
quiterr("bad picture size"); |
156 |
|
/* set view parameters */ |
157 |
< |
if (gotview) { |
158 |
< |
ourview.hresolu = xmax; |
144 |
< |
ourview.vresolu = ymax; |
145 |
< |
if (setview(&ourview) != NULL) |
146 |
< |
gotview = 0; |
147 |
< |
} |
157 |
> |
if (gotview && setview(&ourview) != NULL) |
158 |
> |
gotview = 0; |
159 |
|
if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) |
160 |
|
quiterr("out of memory"); |
161 |
|
|
165 |
|
getevent(); /* main loop */ |
166 |
|
userr: |
167 |
|
fprintf(stderr, |
168 |
< |
"Usage: %s [=geometry][-b][-m][-d][-f][-c ncolors] file\n", |
168 |
> |
"Usage: %s [=geometry][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", |
169 |
|
progname); |
170 |
|
quit(1); |
171 |
|
} |
172 |
|
|
173 |
|
|
174 |
+ |
int |
175 |
|
headline(s) /* get relevant info from header */ |
176 |
|
char *s; |
177 |
|
{ |
178 |
< |
static char *altname[] = {"rview","rpict","VIEW=",NULL}; |
167 |
< |
register char **an; |
178 |
> |
char fmt[32]; |
179 |
|
|
180 |
< |
if (!strncmp(s, "EXPOSURE=", 9)) |
181 |
< |
exposure *= atof(s+9); |
182 |
< |
else |
183 |
< |
for (an = altname; *an != NULL; an++) |
184 |
< |
if (!strncmp(*an, s, strlen(*an))) { |
185 |
< |
if (sscanview(&ourview, s+strlen(*an)) == 0) |
186 |
< |
gotview++; |
187 |
< |
return; |
177 |
< |
} |
180 |
> |
if (isexpos(s)) |
181 |
> |
exposure *= exposval(s); |
182 |
> |
else if (isformat(s)) { |
183 |
> |
formatval(fmt, s); |
184 |
> |
wrongformat = strcmp(fmt, COLRFMT); |
185 |
> |
} else if (isview(s) && sscanview(&ourview, s) > 0) |
186 |
> |
gotview++; |
187 |
> |
return(0); |
188 |
|
} |
189 |
|
|
190 |
|
|
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 |
– |
|
191 |
|
init() /* get data and open window */ |
192 |
|
{ |
193 |
|
register int i; |
194 |
– |
colormap ourmap; |
194 |
|
OpaqueFrame mainframe; |
195 |
|
char defgeom[32]; |
196 |
|
|
212 |
|
maxcolors = 1<<DisplayPlanes(); |
213 |
|
if (maxcolors > 4) maxcolors -= 2; |
214 |
|
} |
216 |
– |
ourras.width = xmax; |
217 |
– |
ourras.height = ymax; |
215 |
|
/* store image */ |
216 |
< |
if (maxcolors <= 2) { /* monochrome */ |
217 |
< |
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 |
< |
} |
216 |
> |
getras(); |
217 |
> |
|
218 |
|
mainframe.bdrwidth = BORWIDTH; |
219 |
|
mainframe.border = WhitePixmap; |
220 |
|
mainframe.background = BlackPixmap; |
267 |
|
} |
268 |
|
|
269 |
|
|
270 |
+ |
getras() /* get raster file */ |
271 |
+ |
{ |
272 |
+ |
colormap ourmap; |
273 |
+ |
|
274 |
+ |
ourras = (XRASTER *)calloc(1, sizeof(XRASTER)); |
275 |
+ |
if (ourras == NULL) |
276 |
+ |
goto memerr; |
277 |
+ |
ourras->width = xmax; |
278 |
+ |
ourras->height = ymax; |
279 |
+ |
if (maxcolors <= 2) { /* monochrome */ |
280 |
+ |
ourras->data.m = (unsigned short *)malloc(BitmapSize(xmax,ymax)); |
281 |
+ |
if (ourras->data.m == NULL) |
282 |
+ |
goto memerr; |
283 |
+ |
getmono(); |
284 |
+ |
} else { |
285 |
+ |
ourras->data.bz = (unsigned char *)malloc(BZPixmapSize(xmax,ymax)); |
286 |
+ |
if (ourras->data.bz == NULL) |
287 |
+ |
goto memerr; |
288 |
+ |
if (greyscale) |
289 |
+ |
biq(dither,maxcolors,1,ourmap); |
290 |
+ |
else |
291 |
+ |
ciq(dither,maxcolors,1,ourmap); |
292 |
+ |
if (init_rcolors(ourras, ourmap) == 0) |
293 |
+ |
goto memerr; |
294 |
+ |
} |
295 |
+ |
return; |
296 |
+ |
memerr: |
297 |
+ |
quiterr("out of memory"); |
298 |
+ |
} |
299 |
+ |
|
300 |
+ |
|
301 |
|
getevent() /* process the next event */ |
302 |
|
{ |
303 |
|
WindowInfo info; |
317 |
|
fixwindow(&e); |
318 |
|
break; |
319 |
|
case UnmapWindow: |
320 |
< |
unmap_rcolors(&ourras); |
320 |
> |
unmap_rcolors(ourras); |
321 |
|
break; |
322 |
|
case ButtonPressed: |
323 |
|
if (controlshift(&e)) |
340 |
|
int x, y; |
341 |
|
int w, h; |
342 |
|
{ |
343 |
< |
map_rcolors(&ourras); |
343 |
> |
if (ourras->ncolors && map_rcolors(ourras) == NULL) { |
344 |
> |
fprintf(stderr, "%s: cannot allocate colors\n", progname); |
345 |
> |
return(-1); |
346 |
> |
} |
347 |
|
if (fast) |
348 |
< |
make_rpixmap(&ourras); |
349 |
< |
return(patch_raster(wind,x-xoff,y-yoff,x,y,w,h,&ourras) ? 0 : -1); |
348 |
> |
make_rpixmap(ourras); |
349 |
> |
return(patch_raster(wind,x-xoff,y-yoff,x,y,w,h,ourras) ? 0 : -1); |
350 |
|
} |
351 |
|
|
352 |
|
|
355 |
|
{ |
356 |
|
char buf[80]; |
357 |
|
COLOR cval; |
358 |
+ |
Color cvx; |
359 |
|
char *cp; |
360 |
|
int n; |
361 |
+ |
double comp; |
362 |
|
FVECT rorg, rdir; |
363 |
|
|
364 |
|
cp = XLookupMapping(ekey, &n); |
366 |
|
return(0); |
367 |
|
switch (*cp) { /* interpret command */ |
368 |
|
case 'q': |
369 |
< |
case CTRL(D): /* quiterr */ |
369 |
> |
case CTRL('D'): /* quit */ |
370 |
|
quit(0); |
371 |
|
case '\n': |
372 |
|
case '\r': |
377 |
|
switch (*cp) { |
378 |
|
case '\n': |
379 |
|
case '\r': /* radiance */ |
380 |
< |
sprintf(buf, "%-3g", intens(cval)/exposure); |
380 |
> |
sprintf(buf, "%.3f", intens(cval)/exposure); |
381 |
|
break; |
382 |
|
case 'l': /* luminance */ |
383 |
< |
sprintf(buf, "%-3gL", bright(cval)*683.0/exposure); |
383 |
> |
sprintf(buf, "%.0fL", luminance(cval)/exposure); |
384 |
|
break; |
385 |
|
case 'c': /* color */ |
386 |
+ |
comp = pow(2.0, (double)scale); |
387 |
|
sprintf(buf, "(%.2f,%.2f,%.2f)", |
388 |
< |
colval(cval,RED), |
389 |
< |
colval(cval,GRN), |
390 |
< |
colval(cval,BLU)); |
388 |
> |
colval(cval,RED)*comp, |
389 |
> |
colval(cval,GRN)*comp, |
390 |
> |
colval(cval,BLU)*comp); |
391 |
|
break; |
392 |
|
} |
393 |
|
XText(wind, box.xmin, box.ymin, buf, strlen(buf), |
394 |
|
fontid, BlackPixel, WhitePixel); |
395 |
|
return(0); |
396 |
+ |
case 'i': /* identify (contour) */ |
397 |
+ |
if (ourras->pixels == NULL) |
398 |
+ |
return(-1); |
399 |
+ |
n = ourras->data.bz[ekey->x-xoff+BZPixmapSize(xmax,ekey->y-yoff)]; |
400 |
+ |
n = ourras->pmap[n]; |
401 |
+ |
cvx.pixel = ourras->cdefs[n].pixel; |
402 |
+ |
cvx.red = random() & 65535; |
403 |
+ |
cvx.green = random() & 65535; |
404 |
+ |
cvx.blue = random() & 65535; |
405 |
+ |
XStoreColor(&cvx); |
406 |
+ |
return(0); |
407 |
|
case 'p': /* position */ |
408 |
|
sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff); |
409 |
|
XText(wind, ekey->x, ekey->y, buf, strlen(buf), |
414 |
|
XFeep(0); |
415 |
|
return(-1); |
416 |
|
} |
417 |
< |
rayview(rorg, rdir, &ourview, |
418 |
< |
ekey->x-xoff + .5, ymax-1-ekey->y+yoff + .5); |
417 |
> |
if (viewray(rorg, rdir, &ourview, (ekey->x-xoff+.5)/xmax, |
418 |
> |
(ymax-1-ekey->y+yoff+.5)/ymax) < 0) |
419 |
> |
return(-1); |
420 |
|
printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); |
421 |
|
printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); |
422 |
|
fflush(stdout); |
423 |
|
return(0); |
424 |
< |
case CTRL(R): /* redraw */ |
424 |
> |
case '=': /* adjust exposure */ |
425 |
> |
if (avgbox(cval) == -1) |
426 |
> |
return(-1); |
427 |
> |
n = log(.5/bright(cval))/.69315 - scale; /* truncate */ |
428 |
> |
if (n == 0) |
429 |
> |
return(0); |
430 |
> |
scale_rcolors(ourras, pow(2.0, (double)n)); |
431 |
> |
scale += n; |
432 |
> |
sprintf(buf, "%+d", scale); |
433 |
> |
XText(wind, box.xmin, box.ymin, buf, strlen(buf), |
434 |
> |
fontid, BlackPixel, WhitePixel); |
435 |
> |
XFlush(); |
436 |
> |
free_raster(ourras); |
437 |
> |
getras(); |
438 |
> |
/* fall through */ |
439 |
> |
case CTRL('R'): /* redraw */ |
440 |
> |
case CTRL('L'): |
441 |
> |
unmap_rcolors(ourras); |
442 |
|
XClear(wind); |
443 |
|
return(redraw(0, 0, width, height)); |
444 |
|
case ' ': /* clear */ |
549 |
|
{ |
550 |
|
register unsigned short *dp; |
551 |
|
register int x, err; |
552 |
< |
int y; |
553 |
< |
rgbpixel *inline; |
552 |
> |
int y, errp; |
553 |
> |
rgbpixel *inl; |
554 |
|
short *cerr; |
555 |
|
|
556 |
< |
if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL |
556 |
> |
if ((inl = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL |
557 |
|
|| (cerr = (short *)calloc(xmax,sizeof(short))) == NULL) |
558 |
< |
quit("out of memory in getmono"); |
559 |
< |
dp = ourras.data.m - 1; |
558 |
> |
quiterr("out of memory in getmono"); |
559 |
> |
dp = ourras->data.m - 1; |
560 |
|
for (y = 0; y < ymax; y++) { |
561 |
< |
picreadline3(y, inline); |
561 |
> |
picreadline3(y, inl); |
562 |
|
err = 0; |
563 |
|
for (x = 0; x < xmax; x++) { |
564 |
|
if (!(x&0xf)) |
565 |
|
*++dp = 0; |
566 |
< |
err += rgb_bright(&inline[x]) + cerr[x]; |
566 |
> |
errp = err; |
567 |
> |
err += rgb_bright(&inl[x]) + cerr[x]; |
568 |
|
if (err > 127) |
569 |
|
err -= 255; |
570 |
|
else |
571 |
|
*dp |= 1<<(x&0xf); |
572 |
< |
cerr[x] = err >>= 1; |
572 |
> |
err /= 3; |
573 |
> |
cerr[x] = err + errp; |
574 |
|
} |
575 |
|
} |
576 |
< |
free((char *)inline); |
576 |
> |
free((char *)inl); |
577 |
|
free((char *)cerr); |
578 |
|
} |
579 |
|
|
580 |
|
|
581 |
< |
init_rcolors(xr, cmap) /* assign color values */ |
581 |
> |
init_rcolors(xr, cmap) /* (re)assign color values */ |
582 |
|
register XRASTER *xr; |
583 |
|
colormap cmap; |
584 |
|
{ |
609 |
|
} |
610 |
|
|
611 |
|
|
612 |
+ |
scale_rcolors(xr, sf) /* scale color map */ |
613 |
+ |
register XRASTER *xr; |
614 |
+ |
double sf; |
615 |
+ |
{ |
616 |
+ |
register int i; |
617 |
+ |
long maxv; |
618 |
+ |
|
619 |
+ |
if (xr->pixels == NULL) |
620 |
+ |
return; |
621 |
+ |
|
622 |
+ |
sf = pow(sf, 1.0/gamcor); |
623 |
+ |
maxv = 65535/sf; |
624 |
+ |
|
625 |
+ |
for (i = xr->ncolors; i--; ) { |
626 |
+ |
xr->cdefs[i].red = xr->cdefs[i].red > maxv ? |
627 |
+ |
65535 : |
628 |
+ |
xr->cdefs[i].red * sf; |
629 |
+ |
xr->cdefs[i].green = xr->cdefs[i].green > maxv ? |
630 |
+ |
65535 : |
631 |
+ |
xr->cdefs[i].green * sf; |
632 |
+ |
xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ? |
633 |
+ |
65535 : |
634 |
+ |
xr->cdefs[i].blue * sf; |
635 |
+ |
} |
636 |
+ |
XStoreColors(xr->ncolors, xr->cdefs); |
637 |
+ |
} |
638 |
+ |
|
639 |
+ |
|
640 |
|
getscan(y) |
641 |
|
int y; |
642 |
|
{ |
644 |
|
if (scanpos == NULL || scanpos[y] == -1) |
645 |
|
return(-1); |
646 |
|
if (fseek(fin, scanpos[y], 0) == -1) |
647 |
< |
quit("fseek error"); |
647 |
> |
quiterr("fseek error"); |
648 |
|
cury = y; |
649 |
< |
} else if (scanpos != NULL) |
649 |
> |
} else if (scanpos != NULL && scanpos[y] == -1) |
650 |
|
scanpos[y] = ftell(fin); |
651 |
|
|
652 |
|
if (freadcolrs(scanline, xmax, fin) < 0) |
661 |
|
int y; |
662 |
|
register rgbpixel *l3; |
663 |
|
{ |
664 |
< |
register BYTE *l4; |
665 |
< |
register int shift, c; |
587 |
< |
int i; |
588 |
< |
|
664 |
> |
register int i; |
665 |
> |
/* read scanline */ |
666 |
|
if (getscan(y) < 0) |
667 |
|
quiterr("cannot seek for picreadline"); |
668 |
|
/* convert scanline */ |
669 |
< |
for (l4=scanline[0], i=xmax; i--; l4+=4, l3++) { |
670 |
< |
shift = l4[EXP] - COLXS; |
671 |
< |
if (shift >= 8) { |
672 |
< |
l3->r = l3->g = l3->b = 255; |
673 |
< |
} else if (shift <= -8) { |
597 |
< |
l3->r = l3->g = l3->b = 0; |
598 |
< |
} else if (shift > 0) { |
599 |
< |
c = l4[RED] << shift; |
600 |
< |
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 |
< |
} |
669 |
> |
normcolrs(scanline, xmax, scale); |
670 |
> |
for (i = 0; i < xmax; i++) { |
671 |
> |
l3[i].r = scanline[i][RED]; |
672 |
> |
l3[i].g = scanline[i][GRN]; |
673 |
> |
l3[i].b = scanline[i][BLU]; |
674 |
|
} |
675 |
|
} |
676 |
|
|
679 |
|
int y; |
680 |
|
pixel *l; |
681 |
|
{ |
682 |
< |
bcopy(l, ourras.data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1)); |
682 |
> |
bcopy((char *)l, (char *)ourras->data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1)); |
683 |
|
} |
684 |
|
|
685 |
|
|
686 |
< |
picreadcm(map) /* do gamcor correction */ |
686 |
> |
picreadcm(map) /* do gamma correction */ |
687 |
|
colormap map; |
688 |
|
{ |
689 |
|
extern double pow(); |
690 |
|
register int i, val; |
691 |
|
|
692 |
|
for (i = 0; i < 256; i++) { |
693 |
< |
val = pow(i/256.0, 1.0/gamcor) * 256.0; |
693 |
> |
val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; |
694 |
|
map[0][i] = map[1][i] = map[2][i] = val; |
695 |
|
} |
696 |
|
} |
699 |
|
picwritecm(map) /* handled elsewhere */ |
700 |
|
colormap map; |
701 |
|
{ |
702 |
+ |
#ifdef DEBUG |
703 |
+ |
register int i; |
704 |
+ |
|
705 |
+ |
for (i = 0; i < 256; i++) |
706 |
+ |
printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]); |
707 |
+ |
#endif |
708 |
|
} |