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; /* gamma correction */ |
43 |
> |
double gamcor = 2.2; /* gamma correction */ |
44 |
|
|
45 |
|
XRASTER *ourras = NULL; /* our stored raster image */ |
46 |
|
|
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 */ |
72 |
|
|
73 |
|
double exposure = 1.0; /* exposure compensation used */ |
74 |
|
|
75 |
+ |
int wrongformat = 0; /* input in another format */ |
76 |
+ |
|
77 |
|
struct { |
78 |
|
int xmin, ymin, xsiz, ysiz; |
79 |
|
} box = {0, 0, 0, 0}; /* current box */ |
143 |
|
} |
144 |
|
} |
145 |
|
/* get header */ |
146 |
< |
getheader(fin, headline); |
146 |
> |
getheader(fin, headline, NULL); |
147 |
|
/* get picture dimensions */ |
148 |
< |
if (fscanf(fin, "-Y %d +X %d\n", &ymax, &xmax) != 2) |
148 |
> |
if (wrongformat || fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR)) |
149 |
|
quiterr("bad picture size"); |
150 |
|
/* set view parameters */ |
151 |
< |
if (gotview) { |
152 |
< |
ourview.hresolu = xmax; |
149 |
< |
ourview.vresolu = ymax; |
150 |
< |
if (setview(&ourview) != NULL) |
151 |
< |
gotview = 0; |
152 |
< |
} |
151 |
> |
if (gotview && setview(&ourview) != NULL) |
152 |
> |
gotview = 0; |
153 |
|
if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) |
154 |
|
quiterr("out of memory"); |
155 |
|
|
170 |
|
{ |
171 |
|
static char *altname[] = {"rview","rpict",VIEWSTR,NULL}; |
172 |
|
register char **an; |
173 |
+ |
char fmt[32]; |
174 |
|
|
175 |
< |
if (!strncmp(s, "EXPOSURE=", 9)) |
176 |
< |
exposure *= atof(s+9); |
177 |
< |
else |
175 |
> |
if (isexpos(s)) |
176 |
> |
exposure *= exposval(s); |
177 |
> |
else if (isformat(s)) { |
178 |
> |
formatval(fmt, s); |
179 |
> |
wrongformat = strcmp(fmt, COLRFMT); |
180 |
> |
} else |
181 |
|
for (an = altname; *an != NULL; an++) |
182 |
|
if (!strncmp(*an, s, strlen(*an))) { |
183 |
< |
if (sscanview(&ourview, s+strlen(*an)) == 0) |
183 |
> |
if (sscanview(&ourview, s+strlen(*an)) > 0) |
184 |
|
gotview++; |
185 |
|
return; |
186 |
|
} |
187 |
|
} |
188 |
|
|
189 |
|
|
186 |
– |
char * |
187 |
– |
sskip(s) /* skip a word */ |
188 |
– |
register char *s; |
189 |
– |
{ |
190 |
– |
while (isspace(*s)) s++; |
191 |
– |
while (*s && !isspace(*s)) s++; |
192 |
– |
return(s); |
193 |
– |
} |
194 |
– |
|
195 |
– |
|
190 |
|
init() /* get data and open window */ |
191 |
|
{ |
192 |
|
register int i; |
293 |
|
} |
294 |
|
return; |
295 |
|
memerr: |
296 |
< |
quit("out of memory"); |
296 |
> |
quiterr("out of memory"); |
297 |
|
} |
298 |
|
|
299 |
|
|
339 |
|
int x, y; |
340 |
|
int w, h; |
341 |
|
{ |
342 |
< |
if (map_rcolors(ourras) == NULL) { |
342 |
> |
if (ourras->ncolors && map_rcolors(ourras) == NULL) { |
343 |
|
fprintf(stderr, "%s: cannot allocate colors\n", progname); |
344 |
|
return(-1); |
345 |
|
} |
354 |
|
{ |
355 |
|
char buf[80]; |
356 |
|
COLOR cval; |
357 |
+ |
Color cvx; |
358 |
|
char *cp; |
359 |
|
int n; |
360 |
|
double comp; |
365 |
|
return(0); |
366 |
|
switch (*cp) { /* interpret command */ |
367 |
|
case 'q': |
368 |
< |
case CTRL(D): /* quiterr */ |
368 |
> |
case CTRL(D): /* quit */ |
369 |
|
quit(0); |
370 |
|
case '\n': |
371 |
|
case '\r': |
376 |
|
switch (*cp) { |
377 |
|
case '\n': |
378 |
|
case '\r': /* radiance */ |
379 |
< |
sprintf(buf, "%-3g", intens(cval)/exposure); |
379 |
> |
sprintf(buf, "%.3f", intens(cval)/exposure); |
380 |
|
break; |
381 |
|
case 'l': /* luminance */ |
382 |
< |
sprintf(buf, "%-3gL", bright(cval)*683.0/exposure); |
382 |
> |
sprintf(buf, "%.0fL", luminance(cval)/exposure); |
383 |
|
break; |
384 |
|
case 'c': /* color */ |
385 |
|
comp = pow(2.0, (double)scale); |
392 |
|
XText(wind, box.xmin, box.ymin, buf, strlen(buf), |
393 |
|
fontid, BlackPixel, WhitePixel); |
394 |
|
return(0); |
395 |
+ |
case 'i': /* identify (contour) */ |
396 |
+ |
if (ourras->pixels == NULL) |
397 |
+ |
return(-1); |
398 |
+ |
n = ourras->data.bz[ekey->x-xoff+BZPixmapSize(xmax,ekey->y-yoff)]; |
399 |
+ |
n = ourras->pmap[n]; |
400 |
+ |
cvx.pixel = ourras->cdefs[n].pixel; |
401 |
+ |
cvx.red = random() & 65535; |
402 |
+ |
cvx.green = random() & 65535; |
403 |
+ |
cvx.blue = random() & 65535; |
404 |
+ |
XStoreColor(&cvx); |
405 |
+ |
return(0); |
406 |
|
case 'p': /* position */ |
407 |
|
sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff); |
408 |
|
XText(wind, ekey->x, ekey->y, buf, strlen(buf), |
413 |
|
XFeep(0); |
414 |
|
return(-1); |
415 |
|
} |
416 |
< |
rayview(rorg, rdir, &ourview, |
417 |
< |
ekey->x-xoff + .5, ymax-1-ekey->y+yoff + .5); |
416 |
> |
if (viewray(rorg, rdir, &ourview, (ekey->x-xoff+.5)/xmax, |
417 |
> |
(ymax-1-ekey->y+yoff+.5)/ymax) < 0) |
418 |
> |
return(-1); |
419 |
|
printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); |
420 |
|
printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); |
421 |
|
fflush(stdout); |
437 |
|
/* fall through */ |
438 |
|
case CTRL(R): /* redraw */ |
439 |
|
case CTRL(L): |
440 |
+ |
unmap_rcolors(ourras); |
441 |
|
XClear(wind); |
442 |
|
return(redraw(0, 0, width, height)); |
443 |
|
case ' ': /* clear */ |
548 |
|
{ |
549 |
|
register unsigned short *dp; |
550 |
|
register int x, err; |
551 |
< |
int y; |
552 |
< |
rgbpixel *inline; |
551 |
> |
int y, errp; |
552 |
> |
rgbpixel *inl; |
553 |
|
short *cerr; |
554 |
|
|
555 |
< |
if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL |
555 |
> |
if ((inl = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL |
556 |
|
|| (cerr = (short *)calloc(xmax,sizeof(short))) == NULL) |
557 |
< |
quit("out of memory in getmono"); |
557 |
> |
quiterr("out of memory in getmono"); |
558 |
|
dp = ourras->data.m - 1; |
559 |
|
for (y = 0; y < ymax; y++) { |
560 |
< |
picreadline3(y, inline); |
560 |
> |
picreadline3(y, inl); |
561 |
|
err = 0; |
562 |
|
for (x = 0; x < xmax; x++) { |
563 |
|
if (!(x&0xf)) |
564 |
|
*++dp = 0; |
565 |
< |
err += rgb_bright(&inline[x]) + cerr[x]; |
565 |
> |
errp = err; |
566 |
> |
err += rgb_bright(&inl[x]) + cerr[x]; |
567 |
|
if (err > 127) |
568 |
|
err -= 255; |
569 |
|
else |
570 |
|
*dp |= 1<<(x&0xf); |
571 |
< |
cerr[x] = err >>= 1; |
571 |
> |
err /= 3; |
572 |
> |
cerr[x] = err + errp; |
573 |
|
} |
574 |
|
} |
575 |
< |
free((char *)inline); |
575 |
> |
free((char *)inl); |
576 |
|
free((char *)cerr); |
577 |
|
} |
578 |
|
|
613 |
|
double sf; |
614 |
|
{ |
615 |
|
register int i; |
616 |
< |
int maxv; |
616 |
> |
long maxv; |
617 |
|
|
618 |
+ |
if (xr->pixels == NULL) |
619 |
+ |
return; |
620 |
+ |
|
621 |
|
sf = pow(sf, 1.0/gamcor); |
622 |
< |
maxv = (1<<16) / sf; |
622 |
> |
maxv = 65535/sf; |
623 |
|
|
624 |
|
for (i = xr->ncolors; i--; ) { |
625 |
< |
xr->cdefs[i].red = xr->cdefs[i].red >= maxv ? |
626 |
< |
(1<<16)-1 : |
625 |
> |
xr->cdefs[i].red = xr->cdefs[i].red > maxv ? |
626 |
> |
65535 : |
627 |
|
xr->cdefs[i].red * sf; |
628 |
< |
xr->cdefs[i].green = xr->cdefs[i].green >= maxv ? |
629 |
< |
(1<<16)-1 : |
628 |
> |
xr->cdefs[i].green = xr->cdefs[i].green > maxv ? |
629 |
> |
65535 : |
630 |
|
xr->cdefs[i].green * sf; |
631 |
< |
xr->cdefs[i].blue = xr->cdefs[i].blue >= maxv ? |
632 |
< |
(1<<16)-1 : |
631 |
> |
xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ? |
632 |
> |
65535 : |
633 |
|
xr->cdefs[i].blue * sf; |
634 |
|
} |
635 |
< |
if (xr->pixels != NULL) |
623 |
< |
XStoreColors(xr->ncolors, xr->cdefs); |
635 |
> |
XStoreColors(xr->ncolors, xr->cdefs); |
636 |
|
} |
637 |
|
|
638 |
|
|
643 |
|
if (scanpos == NULL || scanpos[y] == -1) |
644 |
|
return(-1); |
645 |
|
if (fseek(fin, scanpos[y], 0) == -1) |
646 |
< |
quit("fseek error"); |
646 |
> |
quiterr("fseek error"); |
647 |
|
cury = y; |
648 |
|
} else if (scanpos != NULL) |
649 |
|
scanpos[y] = ftell(fin); |
660 |
|
int y; |
661 |
|
register rgbpixel *l3; |
662 |
|
{ |
663 |
< |
register BYTE *l4; |
664 |
< |
register int shift, c; |
653 |
< |
int i; |
654 |
< |
|
663 |
> |
register int i; |
664 |
> |
/* read scanline */ |
665 |
|
if (getscan(y) < 0) |
666 |
|
quiterr("cannot seek for picreadline"); |
667 |
|
/* convert scanline */ |
668 |
< |
for (l4=scanline[0], i=xmax; i--; l4+=4, l3++) { |
669 |
< |
shift = l4[EXP] - COLXS + scale; |
670 |
< |
if (shift >= 8) { |
671 |
< |
l3->r = l3->g = l3->b = 255; |
672 |
< |
} else if (shift <= -8) { |
663 |
< |
l3->r = l3->g = l3->b = 0; |
664 |
< |
} else if (shift > 0) { |
665 |
< |
c = l4[RED] << shift; |
666 |
< |
l3->r = c > 255 ? 255 : c; |
667 |
< |
c = l4[GRN] << shift; |
668 |
< |
l3->g = c > 255 ? 255 : c; |
669 |
< |
c = l4[BLU] << shift; |
670 |
< |
l3->b = c > 255 ? 255 : c; |
671 |
< |
} else if (shift < 0) { |
672 |
< |
l3->r = l4[RED] >> -shift; |
673 |
< |
l3->g = l4[GRN] >> -shift; |
674 |
< |
l3->b = l4[BLU] >> -shift; |
675 |
< |
} else { |
676 |
< |
l3->r = l4[RED]; |
677 |
< |
l3->g = l4[GRN]; |
678 |
< |
l3->b = l4[BLU]; |
679 |
< |
} |
668 |
> |
normcolrs(scanline, xmax, scale); |
669 |
> |
for (i = 0; i < xmax; i++) { |
670 |
> |
l3[i].r = scanline[i][RED]; |
671 |
> |
l3[i].g = scanline[i][GRN]; |
672 |
> |
l3[i].b = scanline[i][BLU]; |
673 |
|
} |
674 |
|
} |
675 |
|
|
678 |
|
int y; |
679 |
|
pixel *l; |
680 |
|
{ |
681 |
< |
bcopy(l, ourras->data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1)); |
681 |
> |
bcopy((char *)l, (char *)ourras->data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1)); |
682 |
|
} |
683 |
|
|
684 |
|
|
685 |
< |
picreadcm(map) /* do gamcor correction */ |
685 |
> |
picreadcm(map) /* do gamma correction */ |
686 |
|
colormap map; |
687 |
|
{ |
688 |
|
extern double pow(); |
689 |
|
register int i, val; |
690 |
|
|
691 |
|
for (i = 0; i < 256; i++) { |
692 |
< |
val = pow(i/256.0, 1.0/gamcor) * 256.0; |
692 |
> |
val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; |
693 |
|
map[0][i] = map[1][i] = map[2][i] = val; |
694 |
|
} |
695 |
|
} |