1 |
< |
/* Copyright (c) 1993 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1997 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
21 |
|
|
22 |
|
#include "standard.h" |
23 |
|
|
24 |
+ |
#include <signal.h> |
25 |
|
#include <X11/Xlib.h> |
26 |
|
#include <X11/cursorfont.h> |
27 |
|
#include <X11/Xutil.h> |
28 |
|
#include <X11/Xatom.h> |
29 |
|
|
30 |
< |
#include "color.h" |
30 |
> |
#undef NOPROTO |
31 |
> |
#define NOPROTO 1 |
32 |
> |
#include "tonemap.h" |
33 |
|
#include "view.h" |
34 |
|
#include "x11raster.h" |
35 |
|
#include "random.h" |
36 |
|
#include "resolu.h" |
37 |
|
|
38 |
+ |
#ifndef int4 |
39 |
+ |
#define int4 int /* most int's are 32-bit */ |
40 |
+ |
#endif |
41 |
+ |
|
42 |
|
#define FONTNAME "8x13" /* text font we'll use */ |
43 |
|
|
44 |
|
#define CTRL(c) ((c)-'@') |
55 |
|
#define redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras) |
56 |
|
|
57 |
|
double gamcor = 2.2; /* gamma correction */ |
58 |
+ |
char *gamstr = NULL; /* gamma value override */ |
59 |
|
|
60 |
|
int dither = 1; /* dither colors? */ |
61 |
|
int fast = 0; /* keep picture in Pixmap? */ |
72 |
|
int xoff = 0; /* x image offset */ |
73 |
|
int yoff = 0; /* y image offset */ |
74 |
|
|
75 |
+ |
int parent = 0; /* number of children, -1 if child */ |
76 |
+ |
int sequential = 0; /* display images in sequence */ |
77 |
+ |
|
78 |
+ |
char *tout = "od"; /* output of 't' command */ |
79 |
+ |
int tinterv = 0; /* interval between mouse reports */ |
80 |
+ |
|
81 |
+ |
int tmflags = -1; /* tone mapping flags (-1 for none) */ |
82 |
+ |
|
83 |
|
VIEW ourview = STDVIEW; /* image view parameters */ |
84 |
|
int gotview = 0; /* got parameters from file */ |
85 |
|
|
86 |
|
COLR *scanline; /* scan line buffer */ |
87 |
+ |
TMbright *lscan; /* encoded luminance scanline */ |
88 |
+ |
BYTE *cscan; /* encoded chroma scanline */ |
89 |
+ |
BYTE *pscan; /* compute pixel scanline */ |
90 |
|
|
91 |
|
RESOLU inpres; /* input resolution and ordering */ |
92 |
|
int xmax, ymax; /* picture dimensions */ |
121 |
|
|
122 |
|
char errmsg[128]; |
123 |
|
|
124 |
< |
extern BYTE clrtab[256][3]; /* global color map */ |
124 |
> |
BYTE clrtab[256][3]; /* global color map */ |
125 |
|
|
126 |
|
extern long ftell(); |
127 |
|
|
128 |
+ |
extern char *getenv(); |
129 |
+ |
|
130 |
|
Display *thedisplay; |
131 |
|
Atom closedownAtom, wmProtocolsAtom; |
132 |
|
|
133 |
+ |
int sigrecv; |
134 |
|
|
135 |
+ |
int onsig() { sigrecv++; } |
136 |
+ |
|
137 |
+ |
|
138 |
|
main(argc, argv) |
139 |
|
int argc; |
140 |
|
char *argv[]; |
141 |
|
{ |
117 |
– |
extern char *getenv(); |
118 |
– |
char *gv; |
142 |
|
int headline(); |
143 |
|
int i; |
144 |
+ |
int pid; |
145 |
|
|
146 |
|
progname = argv[0]; |
123 |
– |
if ((gv = getenv("GAMMA")) != NULL) |
124 |
– |
gamcor = atof(gv); |
147 |
|
|
148 |
|
for (i = 1; i < argc; i++) |
149 |
|
if (argv[i][0] == '-') |
150 |
|
switch (argv[i][1]) { |
151 |
< |
case 'c': |
151 |
> |
case 'c': /* number of colors */ |
152 |
|
maxcolors = atoi(argv[++i]); |
153 |
|
break; |
154 |
< |
case 'b': |
154 |
> |
case 'b': /* greyscale only */ |
155 |
|
greyscale = !greyscale; |
156 |
|
break; |
157 |
< |
case 'm': |
157 |
> |
case 'm': /* monochrome */ |
158 |
> |
greyscale = 1; |
159 |
|
maxcolors = 2; |
160 |
|
break; |
161 |
< |
case 'd': |
161 |
> |
case 'd': /* display or dither */ |
162 |
|
if (argv[i][2] == 'i') |
163 |
|
dispname = argv[++i]; |
164 |
|
else |
165 |
|
dither = !dither; |
166 |
|
break; |
167 |
< |
case 'f': |
167 |
> |
case 'f': /* save pixmap */ |
168 |
|
fast = !fast; |
169 |
|
break; |
170 |
< |
case 'e': |
171 |
< |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
170 |
> |
case 's': /* one at a time */ |
171 |
> |
sequential = !sequential; |
172 |
> |
break; |
173 |
> |
case 'o': /* 't' output */ |
174 |
> |
tout = argv[i]+2; |
175 |
> |
break; |
176 |
> |
case 't': /* msec interval */ |
177 |
> |
tinterv = atoi(argv[++i]); |
178 |
> |
break; |
179 |
> |
case 'e': /* exposure comp. */ |
180 |
> |
i++; |
181 |
> |
if (!strcmp(argv[i], "auto")) { |
182 |
> |
tmflags = TM_F_CAMERA; |
183 |
> |
break; |
184 |
> |
} |
185 |
> |
if (!strcmp(argv[i], "human")) { |
186 |
> |
tmflags = TM_F_HUMAN; |
187 |
> |
break; |
188 |
> |
} |
189 |
> |
if (argv[i][0] != '+' && argv[i][0] != '-') |
190 |
|
goto userr; |
191 |
< |
scale = atoi(argv[++i]); |
191 |
> |
scale = atoi(argv[i]); |
192 |
|
break; |
193 |
< |
case 'g': |
193 |
> |
case 'g': /* gamma comp. */ |
194 |
|
if (argv[i][2] == 'e') |
195 |
|
geometry = argv[++i]; |
196 |
|
else |
197 |
< |
gamcor = atof(argv[++i]); |
197 |
> |
gamstr = argv[++i]; |
198 |
|
break; |
199 |
|
default: |
200 |
|
goto userr; |
204 |
|
else |
205 |
|
break; |
206 |
|
|
207 |
< |
if (i == argc-1) { |
207 |
> |
if (i > argc) |
208 |
> |
goto userr; |
209 |
> |
while (i < argc-1) { |
210 |
> |
sigrecv = 0; |
211 |
> |
signal(SIGCONT, onsig); |
212 |
> |
if ((pid=fork()) == 0) { /* a child for each picture */ |
213 |
> |
parent = -1; |
214 |
> |
break; |
215 |
> |
} |
216 |
> |
if (pid < 0) |
217 |
> |
quiterr("fork failed"); |
218 |
> |
parent++; |
219 |
> |
while (!sigrecv) |
220 |
> |
pause(); /* wait for wake-up call */ |
221 |
> |
i++; |
222 |
> |
} |
223 |
> |
if (i < argc) { /* open picture file */ |
224 |
|
fname = argv[i]; |
225 |
|
fin = fopen(fname, "r"); |
226 |
< |
if (fin == NULL) { |
227 |
< |
sprintf(errmsg, "cannot open file \"%s\"", fname); |
228 |
< |
quiterr(errmsg); |
172 |
< |
} |
173 |
< |
} else if (i != argc) |
174 |
< |
goto userr; |
226 |
> |
if (fin == NULL) |
227 |
> |
quiterr("cannot open picture file"); |
228 |
> |
} |
229 |
|
/* get header */ |
230 |
|
getheader(fin, headline, NULL); |
231 |
|
/* get picture dimensions */ |
245 |
|
getevent(); /* main loop */ |
246 |
|
userr: |
247 |
|
fprintf(stderr, |
248 |
< |
"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic\n", |
248 |
> |
"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] pic ..\n", |
249 |
|
progname); |
250 |
|
exit(1); |
251 |
|
} |
258 |
|
|
259 |
|
if (isexpos(s)) |
260 |
|
exposure *= exposval(s); |
261 |
< |
else if (isformat(s)) { |
208 |
< |
formatval(fmt, s); |
261 |
> |
else if (formatval(fmt, s)) |
262 |
|
wrongformat = strcmp(fmt, COLRFMT); |
263 |
< |
} else if (isview(s) && sscanview(&ourview, s) > 0) |
263 |
> |
else if (isview(s) && sscanview(&ourview, s) > 0) |
264 |
|
gotview++; |
265 |
|
} |
266 |
|
|
294 |
|
name += i+1; |
295 |
|
if ((thedisplay = XOpenDisplay(dispname)) == NULL) |
296 |
|
quiterr("cannot open display"); |
297 |
+ |
/* set gamma value */ |
298 |
+ |
if (gamstr == NULL) /* get it from the X server */ |
299 |
+ |
gamstr = XGetDefault(thedisplay, "radiance", "gamma"); |
300 |
+ |
if (gamstr == NULL) /* get it from the environment */ |
301 |
+ |
gamstr = getenv("DISPLAY_GAMMA"); |
302 |
+ |
if (gamstr != NULL) |
303 |
+ |
gamcor = atof(gamstr); |
304 |
|
/* get best visual for default screen */ |
305 |
|
getbestvis(); |
306 |
|
/* store image */ |
327 |
|
} |
328 |
|
} |
329 |
|
/* open window */ |
330 |
+ |
i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel; |
331 |
|
ourwinattr.border_pixel = ourwhite; |
332 |
|
ourwinattr.background_pixel = ourblack; |
333 |
< |
ourwinattr.colormap = XCreateColormap(thedisplay, ourroot, |
334 |
< |
ourvis.visual, AllocNone); |
333 |
> |
if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) { |
334 |
> |
ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual); |
335 |
> |
i |= CWColormap; |
336 |
> |
} |
337 |
|
ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask| |
338 |
|
ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask; |
339 |
|
ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross); |
340 |
|
wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y, |
341 |
|
xszhints.width, xszhints.height, BORWIDTH, |
342 |
< |
ourvis.depth, InputOutput, ourvis.visual, CWEventMask| |
343 |
< |
CWCursor|CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); |
342 |
> |
ourvis.depth, InputOutput, ourvis.visual, |
343 |
> |
i, &ourwinattr); |
344 |
|
if (wind == 0) |
345 |
|
quiterr("cannot create window"); |
346 |
|
width = xmax; |
347 |
|
height = ymax; |
348 |
< |
xgcv.foreground = ourblack; |
286 |
< |
xgcv.background = ourwhite; |
348 |
> |
/* prepare graphics drawing context */ |
349 |
|
if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0) |
350 |
|
quiterr("cannot get font"); |
351 |
+ |
xgcv.foreground = ourblack; |
352 |
+ |
xgcv.background = ourwhite; |
353 |
|
ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground| |
354 |
|
GCFont, &xgcv); |
355 |
|
xgcv.function = GXinvert; |
378 |
|
XSetWMProtocols(thedisplay, wind, &closedownAtom, 1); |
379 |
|
|
380 |
|
XMapWindow(thedisplay, wind); |
317 |
– |
return; |
381 |
|
} /* end of init */ |
382 |
|
|
383 |
|
|
384 |
|
quiterr(err) /* print message and exit */ |
385 |
|
char *err; |
386 |
|
{ |
387 |
< |
if (err != NULL) { |
388 |
< |
fprintf(stderr, "%s: %s\n", progname, err); |
389 |
< |
exit(1); |
387 |
> |
register int es; |
388 |
> |
int cs; |
389 |
> |
|
390 |
> |
if (es = err != NULL) |
391 |
> |
fprintf(stderr, "%s: %s: %s\n", progname, |
392 |
> |
fname==NULL?"<stdin>":fname, err); |
393 |
> |
if (thedisplay != NULL) |
394 |
> |
XCloseDisplay(thedisplay); |
395 |
> |
if (parent < 0 & sigrecv == 0) |
396 |
> |
kill(getppid(), SIGCONT); |
397 |
> |
while (parent > 0 && wait(&cs) != -1) { /* wait for any children */ |
398 |
> |
if (es == 0) |
399 |
> |
es = cs>>8 & 0xff; |
400 |
> |
parent--; |
401 |
|
} |
402 |
< |
exit(0); |
402 |
> |
exit(es); |
403 |
|
} |
404 |
|
|
405 |
|
|
540 |
|
goto fail; |
541 |
|
getmono(); |
542 |
|
} else if (ourvis.class == TrueColor | ourvis.class == DirectColor) { |
543 |
< |
ourdata = (unsigned char *)malloc(4*xmax*ymax); |
543 |
> |
ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax); |
544 |
|
if (ourdata == NULL) |
545 |
|
goto fail; |
546 |
< |
ourras = make_raster(thedisplay, &ourvis, 32, |
546 |
> |
ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8, |
547 |
|
ourdata, xmax, ymax, 32); |
548 |
|
if (ourras == NULL) |
549 |
|
goto fail; |
556 |
|
xmax, ymax, 8); |
557 |
|
if (ourras == NULL) |
558 |
|
goto fail; |
559 |
< |
if (greyscale | ourvis.class == StaticGray) |
559 |
> |
if (greyscale) |
560 |
|
getgrey(); |
561 |
|
else |
562 |
|
getmapped(); |
586 |
|
map_rcolors(ourras, wind); |
587 |
|
if (fast) |
588 |
|
make_rpixmap(ourras, wind); |
589 |
+ |
if (!sequential & parent < 0 & sigrecv == 0) { |
590 |
+ |
kill(getppid(), SIGCONT); |
591 |
+ |
sigrecv--; |
592 |
+ |
} |
593 |
|
break; |
594 |
|
case UnmapNotify: |
595 |
|
if (!fast) |
602 |
|
case ButtonPress: |
603 |
|
if (xev.xbutton.state & (ShiftMask|ControlMask)) |
604 |
|
moveimage(&xev.xbutton); |
527 |
– |
else if (xev.xbutton.button == Button2) |
528 |
– |
traceray(xev.xbutton.x, xev.xbutton.y); |
605 |
|
else |
606 |
< |
getbox(&xev.xbutton); |
606 |
> |
switch (xev.xbutton.button) { |
607 |
> |
case Button1: |
608 |
> |
getbox(&xev.xbutton); |
609 |
> |
break; |
610 |
> |
case Button2: |
611 |
> |
traceray(xev.xbutton.x, xev.xbutton.y); |
612 |
> |
break; |
613 |
> |
case Button3: |
614 |
> |
trackrays(&xev.xbutton); |
615 |
> |
break; |
616 |
> |
} |
617 |
|
break; |
618 |
< |
case ClientMessage: |
618 |
> |
case ClientMessage: |
619 |
|
if ((xev.xclient.message_type == wmProtocolsAtom) && |
620 |
|
(xev.xclient.data.l[0] == closedownAtom)) |
621 |
|
quiterr(NULL); |
624 |
|
} |
625 |
|
|
626 |
|
|
627 |
< |
traceray(xpos, ypos) /* print ray corresponding to pixel */ |
627 |
> |
traceray(xpos, ypos) /* print requested pixel data */ |
628 |
|
int xpos, ypos; |
629 |
|
{ |
630 |
+ |
extern char *index(); |
631 |
|
FLOAT hv[2]; |
632 |
|
FVECT rorg, rdir; |
633 |
+ |
COLOR cval; |
634 |
+ |
register char *cp; |
635 |
|
|
636 |
< |
if (!gotview) { /* no view, no can do */ |
637 |
< |
XBell(thedisplay, 0); |
638 |
< |
return(-1); |
639 |
< |
} |
636 |
> |
box.xmin = xpos; box.xsiz = 1; |
637 |
> |
box.ymin = ypos; box.ysiz = 1; |
638 |
> |
avgbox(cval); |
639 |
> |
scalecolor(cval, 1./exposure); |
640 |
|
pix2loc(hv, &inpres, xpos-xoff, ypos-yoff); |
641 |
< |
if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) |
642 |
< |
return(-1); |
643 |
< |
printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); |
644 |
< |
printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); |
641 |
> |
if (!gotview || viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) |
642 |
> |
rorg[0] = rorg[1] = rorg[2] = |
643 |
> |
rdir[0] = rdir[1] = rdir[2] = 0.; |
644 |
> |
|
645 |
> |
for (cp = tout; *cp; cp++) /* print what they asked for */ |
646 |
> |
switch (*cp) { |
647 |
> |
case 'o': /* origin */ |
648 |
> |
printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); |
649 |
> |
break; |
650 |
> |
case 'd': /* direction */ |
651 |
> |
printf("%e %e %e ", rdir[0], rdir[1], rdir[2]); |
652 |
> |
break; |
653 |
> |
case 'v': /* radiance value */ |
654 |
> |
printf("%e %e %e ", colval(cval,RED), |
655 |
> |
colval(cval,GRN), colval(cval,BLU)); |
656 |
> |
break; |
657 |
> |
case 'l': /* luminance */ |
658 |
> |
printf("%e ", luminance(cval)); |
659 |
> |
break; |
660 |
> |
case 'p': /* pixel position */ |
661 |
> |
printf("%d %d ", (int)(hv[0]*inpres.xr), |
662 |
> |
(int)(hv[1]*inpres.yr)); |
663 |
> |
break; |
664 |
> |
} |
665 |
> |
putchar('\n'); |
666 |
|
fflush(stdout); |
667 |
|
return(0); |
668 |
|
} |
737 |
|
case '@': /* adaptation level */ |
738 |
|
if (avgbox(cval) == -1) |
739 |
|
return(-1); |
740 |
< |
comp = com=='@' |
741 |
< |
? 59.2/pow(1.219+pow(luminance(cval)/exposure,.4),2.5)/exposure |
742 |
< |
: .5/bright(cval) ; |
740 |
> |
comp = bright(cval); |
741 |
> |
if (comp < 1e-20) { |
742 |
> |
XBell(thedisplay, 0); |
743 |
> |
return(-1); |
744 |
> |
} |
745 |
> |
if (com == '@') |
746 |
> |
comp = 106./exposure/ |
747 |
> |
pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5); |
748 |
> |
else |
749 |
> |
comp = .5/comp; |
750 |
|
comp = log(comp)/.69315 - scale; |
751 |
|
n = comp < 0 ? comp-.5 : comp+.5 ; /* round */ |
752 |
< |
if (n == 0) |
753 |
< |
return(0); |
754 |
< |
scale_rcolors(ourras, pow(2.0, (double)n)); |
752 |
> |
if (tmflags != -1) |
753 |
> |
tmflags = -1; /* turn off tone mapping */ |
754 |
> |
else { |
755 |
> |
if (n == 0) /* else check if any change */ |
756 |
> |
return(0); |
757 |
> |
scale_rcolors(ourras, pow(2.0, (double)n)); |
758 |
> |
} |
759 |
|
scale += n; |
760 |
|
sprintf(buf, "%+d", scale); |
761 |
|
XDrawImageString(thedisplay, wind, ourgc, |
774 |
|
make_rpixmap(ourras, wind); |
775 |
|
redraw(0, 0, width, height); |
776 |
|
return(0); |
777 |
+ |
case 'f': /* turn on fast redraw */ |
778 |
+ |
fast = 1; |
779 |
+ |
make_rpixmap(ourras, wind); |
780 |
+ |
return(0); |
781 |
+ |
case 'F': /* turn off fast redraw */ |
782 |
+ |
fast = 0; |
783 |
+ |
free_rpixmap(ourras); |
784 |
+ |
return(0); |
785 |
|
case '0': /* recenter origin */ |
786 |
|
if (xoff == 0 & yoff == 0) |
787 |
|
return(0); |
852 |
|
} |
853 |
|
|
854 |
|
|
855 |
+ |
trackrays(ebut) /* trace rays as mouse moves */ |
856 |
+ |
XButtonPressedEvent *ebut; |
857 |
+ |
{ |
858 |
+ |
XEvent e; |
859 |
+ |
unsigned long lastrept; |
860 |
+ |
|
861 |
+ |
traceray(ebut->x, ebut->y); |
862 |
+ |
lastrept = ebut->time; |
863 |
+ |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e); |
864 |
+ |
while (e.type == MotionNotify) { |
865 |
+ |
if (e.xmotion.time >= lastrept + tinterv) { |
866 |
+ |
traceray(e.xmotion.x, e.xmotion.y); |
867 |
+ |
lastrept = e.xmotion.time; |
868 |
+ |
} |
869 |
+ |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e); |
870 |
+ |
} |
871 |
+ |
} |
872 |
+ |
|
873 |
+ |
|
874 |
|
revbox(x0, y0, x1, y1) /* draw box with reversed lines */ |
875 |
|
int x0, y0, x1, y1; |
876 |
|
{ |
929 |
|
} |
930 |
|
|
931 |
|
|
932 |
+ |
make_tonemap() /* initialize tone mapping */ |
933 |
+ |
{ |
934 |
+ |
int y; |
935 |
+ |
|
936 |
+ |
if (tmflags != -1 && fname == NULL) { |
937 |
+ |
fprintf(stderr, "%s: cannot adjust tone of standard input\n", |
938 |
+ |
progname); |
939 |
+ |
tmflags = -1; |
940 |
+ |
} |
941 |
+ |
if (tmflags == -1) { |
942 |
+ |
setcolrcor(pow, 1.0/gamcor); |
943 |
+ |
return; |
944 |
+ |
} |
945 |
+ |
if (greyscale) |
946 |
+ |
tmflags |= TM_F_BW; |
947 |
+ |
/* initialize tm library */ |
948 |
+ |
if (tmInit(tmflags, stdprims, gamcor) == NULL) |
949 |
+ |
goto memerr; |
950 |
+ |
if (tmSetSpace(stdprims, WHTEFFICACY/exposure)) |
951 |
+ |
goto tmerr; |
952 |
+ |
/* allocate encoding buffers */ |
953 |
+ |
if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL) |
954 |
+ |
goto memerr; |
955 |
+ |
if (tmflags & TM_F_BW) { |
956 |
+ |
cscan = TM_NOCHROM; |
957 |
+ |
if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL) |
958 |
+ |
goto memerr; |
959 |
+ |
} else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax)) == NULL) |
960 |
+ |
goto memerr; |
961 |
+ |
/* compute picture histogram */ |
962 |
+ |
for (y = 0; y < ymax; y++) { |
963 |
+ |
getscan(y); |
964 |
+ |
if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax)) |
965 |
+ |
goto tmerr; |
966 |
+ |
if (tmAddHisto(lscan, xmax, 1)) |
967 |
+ |
goto tmerr; |
968 |
+ |
} |
969 |
+ |
/* compute tone mapping */ |
970 |
+ |
if (tmComputeMapping(gamcor, 0., 0.)) |
971 |
+ |
goto tmerr; |
972 |
+ |
free((char *)lscan); |
973 |
+ |
return; |
974 |
+ |
memerr: |
975 |
+ |
quiterr("out of memory in make_tonemap"); |
976 |
+ |
tmerr: |
977 |
+ |
quiterr("tone mapping error"); |
978 |
+ |
} |
979 |
+ |
|
980 |
+ |
|
981 |
+ |
tmap_colrs(scn, len) /* apply tone mapping to scanline */ |
982 |
+ |
register COLR *scn; |
983 |
+ |
int len; |
984 |
+ |
{ |
985 |
+ |
register BYTE *ps; |
986 |
+ |
|
987 |
+ |
if (tmflags == -1) { |
988 |
+ |
if (scale) |
989 |
+ |
shiftcolrs(scn, len, scale); |
990 |
+ |
colrs_gambs(scn, len); |
991 |
+ |
return; |
992 |
+ |
} |
993 |
+ |
if (len > xmax) |
994 |
+ |
quiterr("code error 1 in tmap_colrs"); |
995 |
+ |
if (tmCvColrs(lscan, cscan, scn, len)) |
996 |
+ |
goto tmerr; |
997 |
+ |
if (tmMapPixels(pscan, lscan, cscan, len)) |
998 |
+ |
goto tmerr; |
999 |
+ |
ps = pscan; |
1000 |
+ |
if (tmflags & TM_F_BW) |
1001 |
+ |
while (len--) { |
1002 |
+ |
scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++; |
1003 |
+ |
scn[0][EXP] = COLXS; |
1004 |
+ |
scn++; |
1005 |
+ |
} |
1006 |
+ |
else |
1007 |
+ |
while (len--) { |
1008 |
+ |
scn[0][RED] = *ps++; |
1009 |
+ |
scn[0][GRN] = *ps++; |
1010 |
+ |
scn[0][BLU] = *ps++; |
1011 |
+ |
scn[0][EXP] = COLXS; |
1012 |
+ |
scn++; |
1013 |
+ |
} |
1014 |
+ |
return; |
1015 |
+ |
tmerr: |
1016 |
+ |
quiterr("tone mapping error"); |
1017 |
+ |
} |
1018 |
+ |
|
1019 |
+ |
|
1020 |
+ |
done_tonemap() /* clean up after tone mapping is done */ |
1021 |
+ |
{ |
1022 |
+ |
if (tmflags == -1 || tmTop == NULL) |
1023 |
+ |
return; |
1024 |
+ |
tmDone(tmTop); /* clear old mapping */ |
1025 |
+ |
free((char *)lscan); /* free memory */ |
1026 |
+ |
free((char *)pscan); |
1027 |
+ |
} |
1028 |
+ |
|
1029 |
+ |
|
1030 |
|
getmono() /* get monochrome data */ |
1031 |
|
{ |
1032 |
|
register unsigned char *dp; |
1114 |
|
getfull() /* get full (24-bit) data */ |
1115 |
|
{ |
1116 |
|
int y; |
1117 |
< |
register unsigned long *dp; |
1117 |
> |
register unsigned int4 *dp; |
1118 |
|
register int x; |
1119 |
< |
/* set gamma correction */ |
1120 |
< |
setcolrgam(gamcor); |
1119 |
> |
/* initialize tone mapping */ |
1120 |
> |
make_tonemap(); |
1121 |
|
/* read and convert file */ |
1122 |
< |
dp = (unsigned long *)ourdata; |
1122 |
> |
dp = (unsigned int4 *)ourdata; |
1123 |
|
for (y = 0; y < ymax; y++) { |
1124 |
|
getscan(y); |
1125 |
|
add2icon(y, scanline); |
1126 |
< |
if (scale) |
881 |
< |
shiftcolrs(scanline, xmax, scale); |
882 |
< |
colrs_gambs(scanline, xmax); |
1126 |
> |
tmap_colrs(scanline, xmax); |
1127 |
|
if (ourras->image->blue_mask & 1) |
1128 |
|
for (x = 0; x < xmax; x++) |
1129 |
< |
*dp++ = scanline[x][RED] << 16 | |
1130 |
< |
scanline[x][GRN] << 8 | |
1131 |
< |
scanline[x][BLU] ; |
1129 |
> |
*dp++ = (unsigned int4)scanline[x][RED] << 16 | |
1130 |
> |
(unsigned int4)scanline[x][GRN] << 8 | |
1131 |
> |
(unsigned int4)scanline[x][BLU] ; |
1132 |
|
else |
1133 |
|
for (x = 0; x < xmax; x++) |
1134 |
< |
*dp++ = scanline[x][RED] | |
1135 |
< |
scanline[x][GRN] << 8 | |
1136 |
< |
scanline[x][BLU] << 16 ; |
1134 |
> |
*dp++ = (unsigned int4)scanline[x][RED] | |
1135 |
> |
(unsigned int4)scanline[x][GRN] << 8 | |
1136 |
> |
(unsigned int4)scanline[x][BLU] << 16 ; |
1137 |
|
} |
1138 |
+ |
done_tonemap(); |
1139 |
|
} |
1140 |
|
|
1141 |
|
|
1144 |
|
int y; |
1145 |
|
register unsigned char *dp; |
1146 |
|
register int x; |
1147 |
< |
/* set gamma correction */ |
1148 |
< |
setcolrgam(gamcor); |
1147 |
> |
/* initialize tone mapping */ |
1148 |
> |
make_tonemap(); |
1149 |
|
/* read and convert file */ |
1150 |
|
dp = ourdata; |
1151 |
|
for (y = 0; y < ymax; y++) { |
1152 |
|
getscan(y); |
1153 |
|
add2icon(y, scanline); |
1154 |
< |
if (scale) |
910 |
< |
shiftcolrs(scanline, xmax, scale); |
911 |
< |
for (x = 0; x < xmax; x++) |
912 |
< |
scanline[x][GRN] = normbright(scanline[x]); |
913 |
< |
colrs_gambs(scanline, xmax); |
1154 |
> |
tmap_colrs(scanline, xmax); |
1155 |
|
if (maxcolors < 256) |
1156 |
|
for (x = 0; x < xmax; x++) |
1157 |
< |
*dp++ = ((long)scanline[x][GRN] * |
1157 |
> |
*dp++ = ((int4)scanline[x][GRN] * |
1158 |
|
maxcolors + maxcolors/2) >> 8; |
1159 |
|
else |
1160 |
|
for (x = 0; x < xmax; x++) |
1162 |
|
} |
1163 |
|
for (x = 0; x < maxcolors; x++) |
1164 |
|
clrtab[x][RED] = clrtab[x][GRN] = |
1165 |
< |
clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors; |
1165 |
> |
clrtab[x][BLU] = ((int4)x*256 + 128)/maxcolors; |
1166 |
> |
done_tonemap(); |
1167 |
|
} |
1168 |
|
|
1169 |
|
|
1170 |
|
getmapped() /* get color-mapped data */ |
1171 |
|
{ |
1172 |
|
int y; |
1173 |
< |
/* set gamma correction */ |
1174 |
< |
setcolrgam(gamcor); |
1173 |
> |
/* make sure we can do it first */ |
1174 |
> |
if (fname == NULL) |
1175 |
> |
quiterr("cannot map colors from standard input"); |
1176 |
> |
/* initialize tone mapping */ |
1177 |
> |
make_tonemap(); |
1178 |
|
/* make histogram */ |
1179 |
< |
new_histo(); |
1179 |
> |
if (new_histo((int4)xmax*ymax) == -1) |
1180 |
> |
quiterr("cannot initialize histogram"); |
1181 |
|
for (y = 0; y < ymax; y++) { |
1182 |
|
if (getscan(y) < 0) |
1183 |
< |
quiterr("seek error in getmapped"); |
1183 |
> |
break; |
1184 |
|
add2icon(y, scanline); |
1185 |
< |
if (scale) |
940 |
< |
shiftcolrs(scanline, xmax, scale); |
941 |
< |
colrs_gambs(scanline, xmax); |
1185 |
> |
tmap_colrs(scanline, xmax); |
1186 |
|
cnt_colrs(scanline, xmax); |
1187 |
|
} |
1188 |
|
/* map pixels */ |
1189 |
|
if (!new_clrtab(maxcolors)) |
1190 |
|
quiterr("cannot create color map"); |
1191 |
|
for (y = 0; y < ymax; y++) { |
1192 |
< |
if (getscan(y) < 0) |
1193 |
< |
quiterr("seek error in getmapped"); |
950 |
< |
if (scale) |
951 |
< |
shiftcolrs(scanline, xmax, scale); |
952 |
< |
colrs_gambs(scanline, xmax); |
1192 |
> |
getscan(y); |
1193 |
> |
tmap_colrs(scanline, xmax); |
1194 |
|
if (dither) |
1195 |
|
dith_colrs(ourdata+y*xmax, scanline, xmax); |
1196 |
|
else |
1197 |
|
map_colrs(ourdata+y*xmax, scanline, xmax); |
1198 |
|
} |
1199 |
+ |
done_tonemap(); |
1200 |
|
} |
1201 |
|
|
1202 |
|
|
1231 |
|
getscan(y) |
1232 |
|
int y; |
1233 |
|
{ |
1234 |
+ |
static int trunced = -1; /* truncated file? */ |
1235 |
+ |
skipit: |
1236 |
+ |
if (trunced >= 0 && y >= trunced) { |
1237 |
+ |
bzero(scanline, xmax*sizeof(COLR)); |
1238 |
+ |
return(-1); |
1239 |
+ |
} |
1240 |
|
if (y != cury) { |
1241 |
|
if (scanpos == NULL || scanpos[y] == -1) |
1242 |
|
return(-1); |
1246 |
|
} else if (scanpos != NULL && scanpos[y] == -1) |
1247 |
|
scanpos[y] = ftell(fin); |
1248 |
|
|
1249 |
< |
if (freadcolrs(scanline, xmax, fin) < 0) |
1250 |
< |
quiterr("read error"); |
1251 |
< |
|
1249 |
> |
if (freadcolrs(scanline, xmax, fin) < 0) { |
1250 |
> |
fprintf(stderr, "%s: %s: unfinished picture\n", |
1251 |
> |
progname, fname==NULL?"<stdin>":fname); |
1252 |
> |
trunced = y; |
1253 |
> |
goto skipit; |
1254 |
> |
} |
1255 |
|
cury++; |
1256 |
|
return(0); |
1257 |
|
} |