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("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 |
|
} |
174 |
|
headline(s) /* get relevant info from header */ |
175 |
|
char *s; |
176 |
|
{ |
177 |
< |
static char *altname[] = {"rview","rpict","VIEW=",NULL}; |
167 |
< |
register char **an; |
177 |
> |
char fmt[32]; |
178 |
|
|
179 |
< |
if (!strncmp(s, "EXPOSURE=", 9)) |
180 |
< |
exposure *= atof(s+9); |
181 |
< |
else |
182 |
< |
for (an = altname; *an != NULL; an++) |
183 |
< |
if (!strncmp(*an, s, strlen(*an))) { |
184 |
< |
if (sscanview(&ourview, s+strlen(*an)) == 0) |
185 |
< |
gotview++; |
176 |
< |
return; |
177 |
< |
} |
179 |
> |
if (isexpos(s)) |
180 |
> |
exposure *= exposval(s); |
181 |
> |
else if (isformat(s)) { |
182 |
> |
formatval(fmt, s); |
183 |
> |
wrongformat = strcmp(fmt, COLRFMT); |
184 |
> |
} else if (isview(s) && sscanview(&ourview, s) > 0) |
185 |
> |
gotview++; |
186 |
|
} |
187 |
|
|
188 |
|
|
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 |
– |
|
189 |
|
init() /* get data and open window */ |
190 |
|
{ |
191 |
|
register int i; |
194 |
– |
colormap ourmap; |
192 |
|
OpaqueFrame mainframe; |
193 |
|
char defgeom[32]; |
194 |
|
|
210 |
|
maxcolors = 1<<DisplayPlanes(); |
211 |
|
if (maxcolors > 4) maxcolors -= 2; |
212 |
|
} |
216 |
– |
ourras.width = xmax; |
217 |
– |
ourras.height = ymax; |
213 |
|
/* store image */ |
214 |
< |
if (maxcolors <= 2) { /* monochrome */ |
215 |
< |
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 |
< |
} |
214 |
> |
getras(); |
215 |
> |
|
216 |
|
mainframe.bdrwidth = BORWIDTH; |
217 |
|
mainframe.border = WhitePixmap; |
218 |
|
mainframe.background = BlackPixmap; |
265 |
|
} |
266 |
|
|
267 |
|
|
268 |
+ |
getras() /* get raster file */ |
269 |
+ |
{ |
270 |
+ |
colormap ourmap; |
271 |
+ |
|
272 |
+ |
ourras = (XRASTER *)calloc(1, sizeof(XRASTER)); |
273 |
+ |
if (ourras == NULL) |
274 |
+ |
goto memerr; |
275 |
+ |
ourras->width = xmax; |
276 |
+ |
ourras->height = ymax; |
277 |
+ |
if (maxcolors <= 2) { /* monochrome */ |
278 |
+ |
ourras->data.m = (unsigned short *)malloc(BitmapSize(xmax,ymax)); |
279 |
+ |
if (ourras->data.m == NULL) |
280 |
+ |
goto memerr; |
281 |
+ |
getmono(); |
282 |
+ |
} else { |
283 |
+ |
ourras->data.bz = (unsigned char *)malloc(BZPixmapSize(xmax,ymax)); |
284 |
+ |
if (ourras->data.bz == NULL) |
285 |
+ |
goto memerr; |
286 |
+ |
if (greyscale) |
287 |
+ |
biq(dither,maxcolors,1,ourmap); |
288 |
+ |
else |
289 |
+ |
ciq(dither,maxcolors,1,ourmap); |
290 |
+ |
if (init_rcolors(ourras, ourmap) == 0) |
291 |
+ |
goto memerr; |
292 |
+ |
} |
293 |
+ |
return; |
294 |
+ |
memerr: |
295 |
+ |
quiterr("out of memory"); |
296 |
+ |
} |
297 |
+ |
|
298 |
+ |
|
299 |
|
getevent() /* process the next event */ |
300 |
|
{ |
301 |
|
WindowInfo info; |
315 |
|
fixwindow(&e); |
316 |
|
break; |
317 |
|
case UnmapWindow: |
318 |
< |
unmap_rcolors(&ourras); |
318 |
> |
unmap_rcolors(ourras); |
319 |
|
break; |
320 |
|
case ButtonPressed: |
321 |
|
if (controlshift(&e)) |
338 |
|
int x, y; |
339 |
|
int w, h; |
340 |
|
{ |
341 |
< |
map_rcolors(&ourras); |
341 |
> |
if (ourras->ncolors && map_rcolors(ourras) == NULL) { |
342 |
> |
fprintf(stderr, "%s: cannot allocate colors\n", progname); |
343 |
> |
return(-1); |
344 |
> |
} |
345 |
|
if (fast) |
346 |
< |
make_rpixmap(&ourras); |
347 |
< |
return(patch_raster(wind,x-xoff,y-yoff,x,y,w,h,&ourras) ? 0 : -1); |
346 |
> |
make_rpixmap(ourras); |
347 |
> |
return(patch_raster(wind,x-xoff,y-yoff,x,y,w,h,ourras) ? 0 : -1); |
348 |
|
} |
349 |
|
|
350 |
|
|
353 |
|
{ |
354 |
|
char buf[80]; |
355 |
|
COLOR cval; |
356 |
+ |
Color cvx; |
357 |
|
char *cp; |
358 |
|
int n; |
359 |
+ |
double comp; |
360 |
|
FVECT rorg, rdir; |
361 |
|
|
362 |
|
cp = XLookupMapping(ekey, &n); |
364 |
|
return(0); |
365 |
|
switch (*cp) { /* interpret command */ |
366 |
|
case 'q': |
367 |
< |
case CTRL(D): /* quiterr */ |
367 |
> |
case CTRL('D'): /* quit */ |
368 |
|
quit(0); |
369 |
|
case '\n': |
370 |
|
case '\r': |
375 |
|
switch (*cp) { |
376 |
|
case '\n': |
377 |
|
case '\r': /* radiance */ |
378 |
< |
sprintf(buf, "%-3g", intens(cval)/exposure); |
378 |
> |
sprintf(buf, "%.3f", intens(cval)/exposure); |
379 |
|
break; |
380 |
|
case 'l': /* luminance */ |
381 |
< |
sprintf(buf, "%-3gL", bright(cval)*683.0/exposure); |
381 |
> |
sprintf(buf, "%.0fL", luminance(cval)/exposure); |
382 |
|
break; |
383 |
|
case 'c': /* color */ |
384 |
+ |
comp = pow(2.0, (double)scale); |
385 |
|
sprintf(buf, "(%.2f,%.2f,%.2f)", |
386 |
< |
colval(cval,RED), |
387 |
< |
colval(cval,GRN), |
388 |
< |
colval(cval,BLU)); |
386 |
> |
colval(cval,RED)*comp, |
387 |
> |
colval(cval,GRN)*comp, |
388 |
> |
colval(cval,BLU)*comp); |
389 |
|
break; |
390 |
|
} |
391 |
|
XText(wind, box.xmin, box.ymin, buf, strlen(buf), |
392 |
|
fontid, BlackPixel, WhitePixel); |
393 |
|
return(0); |
394 |
+ |
case 'i': /* identify (contour) */ |
395 |
+ |
if (ourras->pixels == NULL) |
396 |
+ |
return(-1); |
397 |
+ |
n = ourras->data.bz[ekey->x-xoff+BZPixmapSize(xmax,ekey->y-yoff)]; |
398 |
+ |
n = ourras->pmap[n]; |
399 |
+ |
cvx.pixel = ourras->cdefs[n].pixel; |
400 |
+ |
cvx.red = random() & 65535; |
401 |
+ |
cvx.green = random() & 65535; |
402 |
+ |
cvx.blue = random() & 65535; |
403 |
+ |
XStoreColor(&cvx); |
404 |
+ |
return(0); |
405 |
|
case 'p': /* position */ |
406 |
|
sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff); |
407 |
|
XText(wind, ekey->x, ekey->y, buf, strlen(buf), |
412 |
|
XFeep(0); |
413 |
|
return(-1); |
414 |
|
} |
415 |
< |
rayview(rorg, rdir, &ourview, |
416 |
< |
ekey->x-xoff + .5, ymax-1-ekey->y+yoff + .5); |
415 |
> |
if (viewray(rorg, rdir, &ourview, (ekey->x-xoff+.5)/xmax, |
416 |
> |
(ymax-1-ekey->y+yoff+.5)/ymax) < 0) |
417 |
> |
return(-1); |
418 |
|
printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); |
419 |
|
printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); |
420 |
|
fflush(stdout); |
421 |
|
return(0); |
422 |
< |
case CTRL(R): /* redraw */ |
422 |
> |
case '=': /* adjust exposure */ |
423 |
> |
if (avgbox(cval) == -1) |
424 |
> |
return(-1); |
425 |
> |
n = log(.5/bright(cval))/.69315 - scale; /* truncate */ |
426 |
> |
if (n == 0) |
427 |
> |
return(0); |
428 |
> |
scale_rcolors(ourras, pow(2.0, (double)n)); |
429 |
> |
scale += n; |
430 |
> |
sprintf(buf, "%+d", scale); |
431 |
> |
XText(wind, box.xmin, box.ymin, buf, strlen(buf), |
432 |
> |
fontid, BlackPixel, WhitePixel); |
433 |
> |
XFlush(); |
434 |
> |
free_raster(ourras); |
435 |
> |
getras(); |
436 |
> |
/* fall through */ |
437 |
> |
case CTRL('R'): /* redraw */ |
438 |
> |
case CTRL('L'): |
439 |
> |
unmap_rcolors(ourras); |
440 |
|
XClear(wind); |
441 |
|
return(redraw(0, 0, width, height)); |
442 |
|
case ' ': /* clear */ |
547 |
|
{ |
548 |
|
register unsigned short *dp; |
549 |
|
register int x, err; |
550 |
< |
int y; |
551 |
< |
rgbpixel *inline; |
550 |
> |
int y, errp; |
551 |
> |
rgbpixel *inl; |
552 |
|
short *cerr; |
553 |
|
|
554 |
< |
if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL |
554 |
> |
if ((inl = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL |
555 |
|
|| (cerr = (short *)calloc(xmax,sizeof(short))) == NULL) |
556 |
< |
quit("out of memory in getmono"); |
557 |
< |
dp = ourras.data.m - 1; |
556 |
> |
quiterr("out of memory in getmono"); |
557 |
> |
dp = ourras->data.m - 1; |
558 |
|
for (y = 0; y < ymax; y++) { |
559 |
< |
picreadline3(y, inline); |
559 |
> |
picreadline3(y, inl); |
560 |
|
err = 0; |
561 |
|
for (x = 0; x < xmax; x++) { |
562 |
|
if (!(x&0xf)) |
563 |
|
*++dp = 0; |
564 |
< |
err += rgb_bright(&inline[x]) + cerr[x]; |
564 |
> |
errp = err; |
565 |
> |
err += rgb_bright(&inl[x]) + cerr[x]; |
566 |
|
if (err > 127) |
567 |
|
err -= 255; |
568 |
|
else |
569 |
|
*dp |= 1<<(x&0xf); |
570 |
< |
cerr[x] = err >>= 1; |
570 |
> |
err /= 3; |
571 |
> |
cerr[x] = err + errp; |
572 |
|
} |
573 |
|
} |
574 |
< |
free((char *)inline); |
574 |
> |
free((char *)inl); |
575 |
|
free((char *)cerr); |
576 |
|
} |
577 |
|
|
578 |
|
|
579 |
< |
init_rcolors(xr, cmap) /* assign color values */ |
579 |
> |
init_rcolors(xr, cmap) /* (re)assign color values */ |
580 |
|
register XRASTER *xr; |
581 |
|
colormap cmap; |
582 |
|
{ |
607 |
|
} |
608 |
|
|
609 |
|
|
610 |
+ |
scale_rcolors(xr, sf) /* scale color map */ |
611 |
+ |
register XRASTER *xr; |
612 |
+ |
double sf; |
613 |
+ |
{ |
614 |
+ |
register int i; |
615 |
+ |
long maxv; |
616 |
+ |
|
617 |
+ |
if (xr->pixels == NULL) |
618 |
+ |
return; |
619 |
+ |
|
620 |
+ |
sf = pow(sf, 1.0/gamcor); |
621 |
+ |
maxv = 65535/sf; |
622 |
+ |
|
623 |
+ |
for (i = xr->ncolors; i--; ) { |
624 |
+ |
xr->cdefs[i].red = xr->cdefs[i].red > maxv ? |
625 |
+ |
65535 : |
626 |
+ |
xr->cdefs[i].red * sf; |
627 |
+ |
xr->cdefs[i].green = xr->cdefs[i].green > maxv ? |
628 |
+ |
65535 : |
629 |
+ |
xr->cdefs[i].green * sf; |
630 |
+ |
xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ? |
631 |
+ |
65535 : |
632 |
+ |
xr->cdefs[i].blue * sf; |
633 |
+ |
} |
634 |
+ |
XStoreColors(xr->ncolors, xr->cdefs); |
635 |
+ |
} |
636 |
+ |
|
637 |
+ |
|
638 |
|
getscan(y) |
639 |
|
int y; |
640 |
|
{ |
642 |
|
if (scanpos == NULL || scanpos[y] == -1) |
643 |
|
return(-1); |
644 |
|
if (fseek(fin, scanpos[y], 0) == -1) |
645 |
< |
quit("fseek error"); |
645 |
> |
quiterr("fseek error"); |
646 |
|
cury = y; |
647 |
< |
} else if (scanpos != NULL) |
647 |
> |
} else if (scanpos != NULL && scanpos[y] == -1) |
648 |
|
scanpos[y] = ftell(fin); |
649 |
|
|
650 |
|
if (freadcolrs(scanline, xmax, fin) < 0) |
659 |
|
int y; |
660 |
|
register rgbpixel *l3; |
661 |
|
{ |
662 |
< |
register BYTE *l4; |
663 |
< |
register int shift, c; |
587 |
< |
int i; |
588 |
< |
|
662 |
> |
register int i; |
663 |
> |
/* read scanline */ |
664 |
|
if (getscan(y) < 0) |
665 |
|
quiterr("cannot seek for picreadline"); |
666 |
|
/* convert scanline */ |
667 |
< |
for (l4=scanline[0], i=xmax; i--; l4+=4, l3++) { |
668 |
< |
shift = l4[EXP] - COLXS; |
669 |
< |
if (shift >= 8) { |
670 |
< |
l3->r = l3->g = l3->b = 255; |
671 |
< |
} 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 |
< |
} |
667 |
> |
normcolrs(scanline, xmax, scale); |
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((char *)l, (char *)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(); |
688 |
|
register int i, val; |
689 |
|
|
690 |
|
for (i = 0; i < 256; i++) { |
691 |
< |
val = pow(i/256.0, 1.0/gamcor) * 256.0; |
691 |
> |
val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; |
692 |
|
map[0][i] = map[1][i] = map[2][i] = val; |
693 |
|
} |
694 |
|
} |
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 |
|
} |