1 |
< |
/* Copyright (c) 1993 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
4 |
> |
static char SCCSid[] = "$SunId$ SGI"; |
5 |
|
#endif |
6 |
|
|
7 |
|
/* |
27 |
|
#include <X11/Xutil.h> |
28 |
|
#include <X11/Xatom.h> |
29 |
|
|
30 |
+ |
#undef NOPROTO |
31 |
+ |
#define NOPROTO 1 |
32 |
|
#include "color.h" |
33 |
+ |
#include "tonemap.h" |
34 |
|
#include "view.h" |
35 |
|
#include "x11raster.h" |
36 |
|
#include "random.h" |
37 |
|
#include "resolu.h" |
38 |
|
|
36 |
– |
#ifdef __alpha |
37 |
– |
#define int4 int |
38 |
– |
#endif |
39 |
– |
#ifndef int4 |
40 |
– |
#define int4 long |
41 |
– |
#endif |
42 |
– |
|
39 |
|
#define FONTNAME "8x13" /* text font we'll use */ |
40 |
|
|
41 |
|
#define CTRL(c) ((c)-'@') |
44 |
|
|
45 |
|
#define ICONSIZ (8*10) /* maximum icon dimension (even 8) */ |
46 |
|
|
47 |
+ |
#define FIXWEIGHT 20 /* weight to add for fixation points */ |
48 |
+ |
|
49 |
|
#define ourscreen DefaultScreen(thedisplay) |
50 |
|
#define ourroot RootWindow(thedisplay,ourscreen) |
51 |
|
|
54 |
|
#define redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras) |
55 |
|
|
56 |
|
double gamcor = 2.2; /* gamma correction */ |
57 |
+ |
char *gamstr = NULL; /* gamma value override */ |
58 |
|
|
59 |
|
int dither = 1; /* dither colors? */ |
60 |
|
int fast = 0; /* keep picture in Pixmap? */ |
72 |
|
int yoff = 0; /* y image offset */ |
73 |
|
|
74 |
|
int parent = 0; /* number of children, -1 if child */ |
75 |
+ |
int sequential = 0; /* display images in sequence */ |
76 |
|
|
77 |
+ |
char *tout = "od"; /* output of 't' command */ |
78 |
+ |
int tinterv = 0; /* interval between mouse reports */ |
79 |
+ |
|
80 |
+ |
int tmflags = TM_F_LINEAR; /* tone mapping flags */ |
81 |
+ |
|
82 |
|
VIEW ourview = STDVIEW; /* image view parameters */ |
83 |
|
int gotview = 0; /* got parameters from file */ |
84 |
|
|
85 |
|
COLR *scanline; /* scan line buffer */ |
86 |
+ |
TMbright *lscan; /* encoded luminance scanline */ |
87 |
+ |
BYTE *cscan; /* encoded chroma scanline */ |
88 |
+ |
BYTE *pscan; /* compute pixel scanline */ |
89 |
|
|
90 |
|
RESOLU inpres; /* input resolution and ordering */ |
91 |
|
int xmax, ymax; /* picture dimensions */ |
120 |
|
|
121 |
|
char errmsg[128]; |
122 |
|
|
123 |
< |
extern BYTE clrtab[256][3]; /* global color map */ |
123 |
> |
BYTE clrtab[256][3]; /* global color map */ |
124 |
|
|
125 |
|
extern long ftell(); |
126 |
|
|
127 |
+ |
extern char *getenv(); |
128 |
+ |
|
129 |
|
Display *thedisplay; |
130 |
|
Atom closedownAtom, wmProtocolsAtom; |
131 |
|
|
138 |
|
int argc; |
139 |
|
char *argv[]; |
140 |
|
{ |
131 |
– |
extern char *getenv(); |
132 |
– |
char *gv; |
141 |
|
int headline(); |
142 |
|
int i; |
143 |
|
int pid; |
144 |
|
|
145 |
|
progname = argv[0]; |
138 |
– |
if ((gv = getenv("GAMMA")) != NULL) |
139 |
– |
gamcor = atof(gv); |
146 |
|
|
147 |
|
for (i = 1; i < argc; i++) |
148 |
|
if (argv[i][0] == '-') |
149 |
|
switch (argv[i][1]) { |
150 |
< |
case 'c': |
150 |
> |
case 'c': /* number of colors */ |
151 |
|
maxcolors = atoi(argv[++i]); |
152 |
|
break; |
153 |
< |
case 'b': |
153 |
> |
case 'b': /* greyscale only */ |
154 |
|
greyscale = !greyscale; |
155 |
|
break; |
156 |
< |
case 'm': |
156 |
> |
case 'm': /* monochrome */ |
157 |
> |
greyscale = 1; |
158 |
|
maxcolors = 2; |
159 |
|
break; |
160 |
< |
case 'd': |
160 |
> |
case 'd': /* display or dither */ |
161 |
|
if (argv[i][2] == 'i') |
162 |
|
dispname = argv[++i]; |
163 |
|
else |
164 |
|
dither = !dither; |
165 |
|
break; |
166 |
< |
case 'f': |
166 |
> |
case 'f': /* save pixmap */ |
167 |
|
fast = !fast; |
168 |
|
break; |
169 |
< |
case 'e': |
170 |
< |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
169 |
> |
case 's': /* one at a time */ |
170 |
> |
sequential = !sequential; |
171 |
> |
break; |
172 |
> |
case 'o': /* 't' output */ |
173 |
> |
tout = argv[i]+2; |
174 |
> |
break; |
175 |
> |
case 't': /* msec interval */ |
176 |
> |
tinterv = atoi(argv[++i]); |
177 |
> |
break; |
178 |
> |
case 'e': /* exposure comp. */ |
179 |
> |
i++; |
180 |
> |
if (argv[i][0] == 'a') { |
181 |
> |
tmflags = TM_F_CAMERA; |
182 |
> |
break; |
183 |
> |
} |
184 |
> |
if (argv[i][0] == 'h') { |
185 |
> |
tmflags = TM_F_HUMAN; |
186 |
> |
break; |
187 |
> |
} |
188 |
> |
if (argv[i][0] != '+' && argv[i][0] != '-') |
189 |
|
goto userr; |
190 |
< |
scale = atoi(argv[++i]); |
190 |
> |
scale = atoi(argv[i]); |
191 |
|
break; |
192 |
< |
case 'g': |
192 |
> |
case 'g': /* gamma comp. */ |
193 |
|
if (argv[i][2] == 'e') |
194 |
|
geometry = argv[++i]; |
195 |
|
else |
196 |
< |
gamcor = atof(argv[++i]); |
196 |
> |
gamstr = argv[++i]; |
197 |
|
break; |
198 |
|
default: |
199 |
|
goto userr; |
240 |
|
|
241 |
|
init(argc, argv); /* get file and open window */ |
242 |
|
|
218 |
– |
if (parent < 0) { |
219 |
– |
kill(getppid(), SIGCONT); /* signal parent if child */ |
220 |
– |
sigrecv--; |
221 |
– |
} |
243 |
|
for ( ; ; ) |
244 |
|
getevent(); /* main loop */ |
245 |
|
userr: |
246 |
|
fprintf(stderr, |
247 |
< |
"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic ..\n", |
247 |
> |
"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] pic ..\n", |
248 |
|
progname); |
249 |
|
exit(1); |
250 |
|
} |
257 |
|
|
258 |
|
if (isexpos(s)) |
259 |
|
exposure *= exposval(s); |
260 |
< |
else if (isformat(s)) { |
240 |
< |
formatval(fmt, s); |
260 |
> |
else if (formatval(fmt, s)) |
261 |
|
wrongformat = strcmp(fmt, COLRFMT); |
262 |
< |
} else if (isview(s) && sscanview(&ourview, s) > 0) |
262 |
> |
else if (isview(s) && sscanview(&ourview, s) > 0) |
263 |
|
gotview++; |
264 |
|
} |
265 |
|
|
293 |
|
name += i+1; |
294 |
|
if ((thedisplay = XOpenDisplay(dispname)) == NULL) |
295 |
|
quiterr("cannot open display"); |
296 |
+ |
/* set gamma value */ |
297 |
+ |
if (gamstr == NULL) /* get it from the X server */ |
298 |
+ |
gamstr = XGetDefault(thedisplay, "radiance", "gamma"); |
299 |
+ |
if (gamstr == NULL) /* get it from the environment */ |
300 |
+ |
gamstr = getenv("DISPLAY_GAMMA"); |
301 |
+ |
if (gamstr != NULL) |
302 |
+ |
gamcor = atof(gamstr); |
303 |
|
/* get best visual for default screen */ |
304 |
|
getbestvis(); |
305 |
|
/* store image */ |
326 |
|
} |
327 |
|
} |
328 |
|
/* open window */ |
329 |
+ |
i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel; |
330 |
|
ourwinattr.border_pixel = ourwhite; |
331 |
|
ourwinattr.background_pixel = ourblack; |
332 |
< |
ourwinattr.colormap = XCreateColormap(thedisplay, ourroot, |
333 |
< |
ourvis.visual, AllocNone); |
332 |
> |
if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) { |
333 |
> |
ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual); |
334 |
> |
i |= CWColormap; |
335 |
> |
} |
336 |
|
ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask| |
337 |
|
ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask; |
338 |
|
ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross); |
339 |
|
wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y, |
340 |
|
xszhints.width, xszhints.height, BORWIDTH, |
341 |
< |
ourvis.depth, InputOutput, ourvis.visual, CWEventMask| |
342 |
< |
CWCursor|CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); |
341 |
> |
ourvis.depth, InputOutput, ourvis.visual, |
342 |
> |
i, &ourwinattr); |
343 |
|
if (wind == 0) |
344 |
|
quiterr("cannot create window"); |
345 |
|
width = xmax; |
377 |
|
XSetWMProtocols(thedisplay, wind, &closedownAtom, 1); |
378 |
|
|
379 |
|
XMapWindow(thedisplay, wind); |
350 |
– |
return; |
380 |
|
} /* end of init */ |
381 |
|
|
382 |
|
|
389 |
|
if (es = err != NULL) |
390 |
|
fprintf(stderr, "%s: %s: %s\n", progname, |
391 |
|
fname==NULL?"<stdin>":fname, err); |
392 |
< |
if (parent > 0 & wind != 0) { |
393 |
< |
XDestroyWindow(thedisplay, wind); |
394 |
< |
XFlush(thedisplay); |
366 |
< |
} else if (parent < 0 & sigrecv == 0) |
392 |
> |
if (thedisplay != NULL) |
393 |
> |
XCloseDisplay(thedisplay); |
394 |
> |
if (parent < 0 & sigrecv == 0) |
395 |
|
kill(getppid(), SIGCONT); |
396 |
|
while (parent > 0 && wait(&cs) != -1) { /* wait for any children */ |
397 |
|
if (es == 0) |
555 |
|
xmax, ymax, 8); |
556 |
|
if (ourras == NULL) |
557 |
|
goto fail; |
558 |
< |
if (greyscale | ourvis.class == StaticGray) |
558 |
> |
if (greyscale) |
559 |
|
getgrey(); |
560 |
|
else |
561 |
|
getmapped(); |
585 |
|
map_rcolors(ourras, wind); |
586 |
|
if (fast) |
587 |
|
make_rpixmap(ourras, wind); |
588 |
+ |
if (!sequential & parent < 0 & sigrecv == 0) { |
589 |
+ |
kill(getppid(), SIGCONT); |
590 |
+ |
sigrecv--; |
591 |
+ |
} |
592 |
|
break; |
593 |
|
case UnmapNotify: |
594 |
|
if (!fast) |
601 |
|
case ButtonPress: |
602 |
|
if (xev.xbutton.state & (ShiftMask|ControlMask)) |
603 |
|
moveimage(&xev.xbutton); |
572 |
– |
else if (xev.xbutton.button == Button2) |
573 |
– |
traceray(xev.xbutton.x, xev.xbutton.y); |
604 |
|
else |
605 |
< |
getbox(&xev.xbutton); |
605 |
> |
switch (xev.xbutton.button) { |
606 |
> |
case Button1: |
607 |
> |
getbox(&xev.xbutton); |
608 |
> |
break; |
609 |
> |
case Button2: |
610 |
> |
traceray(xev.xbutton.x, xev.xbutton.y); |
611 |
> |
break; |
612 |
> |
case Button3: |
613 |
> |
trackrays(&xev.xbutton); |
614 |
> |
break; |
615 |
> |
} |
616 |
|
break; |
617 |
|
case ClientMessage: |
618 |
|
if ((xev.xclient.message_type == wmProtocolsAtom) && |
623 |
|
} |
624 |
|
|
625 |
|
|
626 |
< |
traceray(xpos, ypos) /* print ray corresponding to pixel */ |
626 |
> |
traceray(xpos, ypos) /* print requested pixel data */ |
627 |
|
int xpos, ypos; |
628 |
|
{ |
629 |
+ |
extern char *index(); |
630 |
|
FLOAT hv[2]; |
631 |
|
FVECT rorg, rdir; |
632 |
+ |
COLOR cval; |
633 |
+ |
register char *cp; |
634 |
|
|
635 |
< |
if (!gotview) { /* no view, no can do */ |
636 |
< |
XBell(thedisplay, 0); |
637 |
< |
return(-1); |
638 |
< |
} |
635 |
> |
box.xmin = xpos; box.xsiz = 1; |
636 |
> |
box.ymin = ypos; box.ysiz = 1; |
637 |
> |
avgbox(cval); |
638 |
> |
scalecolor(cval, 1./exposure); |
639 |
|
pix2loc(hv, &inpres, xpos-xoff, ypos-yoff); |
640 |
< |
if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) |
641 |
< |
return(-1); |
642 |
< |
printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); |
643 |
< |
printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); |
640 |
> |
if (!gotview || viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) |
641 |
> |
rorg[0] = rorg[1] = rorg[2] = |
642 |
> |
rdir[0] = rdir[1] = rdir[2] = 0.; |
643 |
> |
|
644 |
> |
for (cp = tout; *cp; cp++) /* print what they asked for */ |
645 |
> |
switch (*cp) { |
646 |
> |
case 'o': /* origin */ |
647 |
> |
printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); |
648 |
> |
break; |
649 |
> |
case 'd': /* direction */ |
650 |
> |
printf("%e %e %e ", rdir[0], rdir[1], rdir[2]); |
651 |
> |
break; |
652 |
> |
case 'v': /* radiance value */ |
653 |
> |
printf("%e %e %e ", colval(cval,RED), |
654 |
> |
colval(cval,GRN), colval(cval,BLU)); |
655 |
> |
break; |
656 |
> |
case 'l': /* luminance */ |
657 |
> |
printf("%e ", luminance(cval)); |
658 |
> |
break; |
659 |
> |
case 'p': /* pixel position */ |
660 |
> |
printf("%d %d ", (int)(hv[0]*inpres.xr), |
661 |
> |
(int)(hv[1]*inpres.yr)); |
662 |
> |
break; |
663 |
> |
} |
664 |
> |
putchar('\n'); |
665 |
|
fflush(stdout); |
666 |
|
return(0); |
667 |
|
} |
690 |
|
case '\r': |
691 |
|
case 'l': |
692 |
|
case 'c': /* value */ |
693 |
< |
if (avgbox(cval) == -1) |
693 |
> |
if (!avgbox(cval)) |
694 |
|
return(-1); |
695 |
|
switch (com) { |
696 |
|
case '\n': |
732 |
|
return(0); |
733 |
|
case 't': /* trace */ |
734 |
|
return(traceray(ekey->x, ekey->y)); |
735 |
+ |
case 'a': /* auto exposure */ |
736 |
+ |
if (fname == NULL) |
737 |
+ |
return(-1); |
738 |
+ |
tmflags = TM_F_CAMERA; |
739 |
+ |
strcpy(buf, "auto exposure..."); |
740 |
+ |
goto remap; |
741 |
+ |
case 'h': /* human response */ |
742 |
+ |
if (fname == NULL) |
743 |
+ |
return(-1); |
744 |
+ |
tmflags = TM_F_HUMAN; |
745 |
+ |
strcpy(buf, "human exposure..."); |
746 |
+ |
goto remap; |
747 |
|
case '=': /* adjust exposure */ |
748 |
|
case '@': /* adaptation level */ |
749 |
< |
if (avgbox(cval) == -1) |
749 |
> |
if (!avgbox(cval)) |
750 |
|
return(-1); |
751 |
< |
comp = com=='@' |
752 |
< |
? 106./pow(1.219+pow(luminance(cval)/exposure,.4),2.5)/exposure |
753 |
< |
: .5/bright(cval) ; |
751 |
> |
comp = bright(cval); |
752 |
> |
if (comp < 1e-20) { |
753 |
> |
XBell(thedisplay, 0); |
754 |
> |
return(-1); |
755 |
> |
} |
756 |
> |
if (com == '@') |
757 |
> |
comp = 106./exposure/ |
758 |
> |
pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5); |
759 |
> |
else |
760 |
> |
comp = .5/comp; |
761 |
|
comp = log(comp)/.69315 - scale; |
762 |
|
n = comp < 0 ? comp-.5 : comp+.5 ; /* round */ |
763 |
< |
if (n == 0) |
764 |
< |
return(0); |
765 |
< |
scale_rcolors(ourras, pow(2.0, (double)n)); |
763 |
> |
if (tmflags != TM_F_LINEAR) |
764 |
> |
tmflags = TM_F_LINEAR; /* turn off tone mapping */ |
765 |
> |
else { |
766 |
> |
if (n == 0) /* else check if any change */ |
767 |
> |
return(0); |
768 |
> |
scale_rcolors(ourras, pow(2.0, (double)n)); |
769 |
> |
} |
770 |
|
scale += n; |
771 |
|
sprintf(buf, "%+d", scale); |
772 |
+ |
remap: |
773 |
|
XDrawImageString(thedisplay, wind, ourgc, |
774 |
|
box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); |
775 |
|
XFlush(thedisplay); |
864 |
|
} |
865 |
|
|
866 |
|
|
867 |
+ |
trackrays(ebut) /* trace rays as mouse moves */ |
868 |
+ |
XButtonPressedEvent *ebut; |
869 |
+ |
{ |
870 |
+ |
XEvent e; |
871 |
+ |
unsigned long lastrept; |
872 |
+ |
|
873 |
+ |
traceray(ebut->x, ebut->y); |
874 |
+ |
lastrept = ebut->time; |
875 |
+ |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e); |
876 |
+ |
while (e.type == MotionNotify) { |
877 |
+ |
if (e.xmotion.time >= lastrept + tinterv) { |
878 |
+ |
traceray(e.xmotion.x, e.xmotion.y); |
879 |
+ |
lastrept = e.xmotion.time; |
880 |
+ |
} |
881 |
+ |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e); |
882 |
+ |
} |
883 |
+ |
} |
884 |
+ |
|
885 |
+ |
|
886 |
|
revbox(x0, y0, x1, y1) /* draw box with reversed lines */ |
887 |
|
int x0, y0, x1, y1; |
888 |
|
{ |
893 |
|
} |
894 |
|
|
895 |
|
|
896 |
< |
avgbox(clr) /* average color over current box */ |
897 |
< |
COLOR clr; |
896 |
> |
int |
897 |
> |
colavg(scn, n, cavg) |
898 |
> |
register COLR *scn; |
899 |
> |
register int n; |
900 |
> |
COLOR cavg; |
901 |
|
{ |
902 |
< |
static COLOR lc; |
903 |
< |
static int ll, lr, lt, lb; |
902 |
> |
COLOR col; |
903 |
> |
|
904 |
> |
while (n--) { |
905 |
> |
colr_color(col, scn++); |
906 |
> |
addcolor(cavg, col); |
907 |
> |
} |
908 |
> |
} |
909 |
> |
|
910 |
> |
|
911 |
> |
int |
912 |
> |
avgbox(cavg) /* average color over current box */ |
913 |
> |
COLOR cavg; |
914 |
> |
{ |
915 |
> |
double d; |
916 |
> |
register int rval; |
917 |
> |
|
918 |
> |
setcolor(cavg, 0., 0., 0.); |
919 |
> |
rval = dobox(colavg, (char *)cavg); |
920 |
> |
if (rval > 0) { |
921 |
> |
d = 1./rval; |
922 |
> |
scalecolor(cavg, d); |
923 |
> |
} |
924 |
> |
return(rval); |
925 |
> |
} |
926 |
> |
|
927 |
> |
|
928 |
> |
int |
929 |
> |
dobox(f, p) /* run function over box */ |
930 |
> |
int (*f)(); /* function to call for each subscan */ |
931 |
> |
char *p; /* pointer to private data */ |
932 |
> |
{ |
933 |
|
int left, right, top, bottom; |
934 |
|
int y; |
796 |
– |
double d; |
797 |
– |
COLOR ctmp; |
798 |
– |
register int x; |
935 |
|
|
800 |
– |
setcolor(clr, 0.0, 0.0, 0.0); |
936 |
|
left = box.xmin - xoff; |
937 |
|
right = left + box.xsiz; |
938 |
|
if (left < 0) |
940 |
|
if (right > xmax) |
941 |
|
right = xmax; |
942 |
|
if (left >= right) |
943 |
< |
return(-1); |
943 |
> |
return(0); |
944 |
|
top = box.ymin - yoff; |
945 |
|
bottom = top + box.ysiz; |
946 |
|
if (top < 0) |
948 |
|
if (bottom > ymax) |
949 |
|
bottom = ymax; |
950 |
|
if (top >= bottom) |
816 |
– |
return(-1); |
817 |
– |
if (left == ll && right == lr && top == lt && bottom == lb) { |
818 |
– |
copycolor(clr, lc); |
951 |
|
return(0); |
820 |
– |
} |
952 |
|
for (y = top; y < bottom; y++) { |
953 |
|
if (getscan(y) == -1) |
954 |
|
return(-1); |
955 |
< |
for (x = left; x < right; x++) { |
956 |
< |
colr_color(ctmp, scanline[x]); |
957 |
< |
addcolor(clr, ctmp); |
955 |
> |
(*f)(scanline+left, right-left, p); |
956 |
> |
} |
957 |
> |
return((right-left)*(bottom-top)); |
958 |
> |
} |
959 |
> |
|
960 |
> |
|
961 |
> |
int |
962 |
> |
addfix(scn, n) /* add fixation points to histogram */ |
963 |
> |
COLR *scn; |
964 |
> |
int n; |
965 |
> |
{ |
966 |
> |
if (tmCvColrs(lscan, TM_NOCHROM, scn, n)) |
967 |
> |
goto tmerr; |
968 |
> |
if (tmAddHisto(lscan, n, FIXWEIGHT)) |
969 |
> |
goto tmerr; |
970 |
> |
return; |
971 |
> |
tmerr: |
972 |
> |
quiterr("tone mapping error"); |
973 |
> |
} |
974 |
> |
|
975 |
> |
|
976 |
> |
make_tonemap() /* initialize tone mapping */ |
977 |
> |
{ |
978 |
> |
int flags, y; |
979 |
> |
|
980 |
> |
if (tmflags != TM_F_LINEAR && fname == NULL) { |
981 |
> |
fprintf(stderr, "%s: cannot adjust tone of standard input\n", |
982 |
> |
progname); |
983 |
> |
tmflags = TM_F_LINEAR; |
984 |
> |
} |
985 |
> |
if (tmflags == TM_F_LINEAR) { /* linear with clamping */ |
986 |
> |
setcolrcor(pow, 1.0/gamcor); |
987 |
> |
return; |
988 |
> |
} |
989 |
> |
flags = tmflags; /* histogram adjustment */ |
990 |
> |
if (greyscale) flags |= TM_F_BW; |
991 |
> |
if (tmTop != NULL) { /* reuse old histogram if one */ |
992 |
> |
tmDone(tmTop); |
993 |
> |
tmTop->flags = flags; |
994 |
> |
} else { /* else initialize */ |
995 |
> |
if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL) |
996 |
> |
goto memerr; |
997 |
> |
if (greyscale) { |
998 |
> |
cscan = TM_NOCHROM; |
999 |
> |
if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL) |
1000 |
> |
goto memerr; |
1001 |
> |
} else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax)) |
1002 |
> |
== NULL) |
1003 |
> |
goto memerr; |
1004 |
> |
/* initialize tm library */ |
1005 |
> |
if (tmInit(flags, stdprims, gamcor) == NULL) |
1006 |
> |
goto memerr; |
1007 |
> |
if (tmSetSpace(stdprims, WHTEFFICACY/exposure)) |
1008 |
> |
goto tmerr; |
1009 |
> |
/* compute picture histogram */ |
1010 |
> |
for (y = 0; y < ymax; y++) { |
1011 |
> |
getscan(y); |
1012 |
> |
if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax)) |
1013 |
> |
goto tmerr; |
1014 |
> |
if (tmAddHisto(lscan, xmax, 1)) |
1015 |
> |
goto tmerr; |
1016 |
|
} |
1017 |
|
} |
1018 |
< |
d = 1.0/((right-left)*(bottom-top)); |
1019 |
< |
scalecolor(clr, d); |
1020 |
< |
ll = left; lr = right; lt = top; lb = bottom; |
1021 |
< |
copycolor(lc, clr); |
1022 |
< |
return(0); |
1018 |
> |
tmDup(); /* add fixations to duplicate map */ |
1019 |
> |
dobox(addfix, NULL); |
1020 |
> |
/* (re)compute tone mapping */ |
1021 |
> |
if (tmComputeMapping(gamcor, 0., 0.)) |
1022 |
> |
goto tmerr; |
1023 |
> |
return; |
1024 |
> |
memerr: |
1025 |
> |
quiterr("out of memory in make_tonemap"); |
1026 |
> |
tmerr: |
1027 |
> |
quiterr("tone mapping error"); |
1028 |
|
} |
1029 |
|
|
1030 |
|
|
1031 |
+ |
tmap_colrs(scn, len) /* apply tone mapping to scanline */ |
1032 |
+ |
register COLR *scn; |
1033 |
+ |
int len; |
1034 |
+ |
{ |
1035 |
+ |
register BYTE *ps; |
1036 |
+ |
|
1037 |
+ |
if (tmflags == TM_F_LINEAR) { |
1038 |
+ |
if (scale) |
1039 |
+ |
shiftcolrs(scn, len, scale); |
1040 |
+ |
colrs_gambs(scn, len); |
1041 |
+ |
return; |
1042 |
+ |
} |
1043 |
+ |
if (len > xmax) |
1044 |
+ |
quiterr("code error 1 in tmap_colrs"); |
1045 |
+ |
if (tmCvColrs(lscan, cscan, scn, len)) |
1046 |
+ |
goto tmerr; |
1047 |
+ |
if (tmMapPixels(pscan, lscan, cscan, len)) |
1048 |
+ |
goto tmerr; |
1049 |
+ |
ps = pscan; |
1050 |
+ |
if (greyscale) |
1051 |
+ |
while (len--) { |
1052 |
+ |
scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++; |
1053 |
+ |
scn[0][EXP] = COLXS; |
1054 |
+ |
scn++; |
1055 |
+ |
} |
1056 |
+ |
else |
1057 |
+ |
while (len--) { |
1058 |
+ |
scn[0][RED] = *ps++; |
1059 |
+ |
scn[0][GRN] = *ps++; |
1060 |
+ |
scn[0][BLU] = *ps++; |
1061 |
+ |
scn[0][EXP] = COLXS; |
1062 |
+ |
scn++; |
1063 |
+ |
} |
1064 |
+ |
return; |
1065 |
+ |
tmerr: |
1066 |
+ |
quiterr("tone mapping error"); |
1067 |
+ |
} |
1068 |
+ |
|
1069 |
+ |
|
1070 |
|
getmono() /* get monochrome data */ |
1071 |
|
{ |
1072 |
|
register unsigned char *dp; |
1156 |
|
int y; |
1157 |
|
register unsigned int4 *dp; |
1158 |
|
register int x; |
1159 |
< |
/* set gamma correction */ |
1160 |
< |
setcolrgam(gamcor); |
1159 |
> |
/* initialize tone mapping */ |
1160 |
> |
make_tonemap(); |
1161 |
|
/* read and convert file */ |
1162 |
|
dp = (unsigned int4 *)ourdata; |
1163 |
|
for (y = 0; y < ymax; y++) { |
1164 |
|
getscan(y); |
1165 |
|
add2icon(y, scanline); |
1166 |
< |
if (scale) |
934 |
< |
shiftcolrs(scanline, xmax, scale); |
935 |
< |
colrs_gambs(scanline, xmax); |
1166 |
> |
tmap_colrs(scanline, xmax); |
1167 |
|
if (ourras->image->blue_mask & 1) |
1168 |
|
for (x = 0; x < xmax; x++) |
1169 |
< |
*dp++ = scanline[x][RED] << 16 | |
1170 |
< |
scanline[x][GRN] << 8 | |
1171 |
< |
scanline[x][BLU] ; |
1169 |
> |
*dp++ = (unsigned int4)scanline[x][RED] << 16 | |
1170 |
> |
(unsigned int4)scanline[x][GRN] << 8 | |
1171 |
> |
(unsigned int4)scanline[x][BLU] ; |
1172 |
|
else |
1173 |
|
for (x = 0; x < xmax; x++) |
1174 |
< |
*dp++ = scanline[x][RED] | |
1175 |
< |
scanline[x][GRN] << 8 | |
1176 |
< |
scanline[x][BLU] << 16 ; |
1174 |
> |
*dp++ = (unsigned int4)scanline[x][RED] | |
1175 |
> |
(unsigned int4)scanline[x][GRN] << 8 | |
1176 |
> |
(unsigned int4)scanline[x][BLU] << 16 ; |
1177 |
|
} |
1178 |
|
} |
1179 |
|
|
1183 |
|
int y; |
1184 |
|
register unsigned char *dp; |
1185 |
|
register int x; |
1186 |
< |
/* set gamma correction */ |
1187 |
< |
setcolrgam(gamcor); |
1186 |
> |
/* initialize tone mapping */ |
1187 |
> |
make_tonemap(); |
1188 |
|
/* read and convert file */ |
1189 |
|
dp = ourdata; |
1190 |
|
for (y = 0; y < ymax; y++) { |
1191 |
|
getscan(y); |
1192 |
|
add2icon(y, scanline); |
1193 |
< |
if (scale) |
963 |
< |
shiftcolrs(scanline, xmax, scale); |
964 |
< |
for (x = 0; x < xmax; x++) |
965 |
< |
scanline[x][GRN] = normbright(scanline[x]); |
966 |
< |
colrs_gambs(scanline, xmax); |
1193 |
> |
tmap_colrs(scanline, xmax); |
1194 |
|
if (maxcolors < 256) |
1195 |
|
for (x = 0; x < xmax; x++) |
1196 |
< |
*dp++ = ((long)scanline[x][GRN] * |
1196 |
> |
*dp++ = ((int4)scanline[x][GRN] * |
1197 |
|
maxcolors + maxcolors/2) >> 8; |
1198 |
|
else |
1199 |
|
for (x = 0; x < xmax; x++) |
1201 |
|
} |
1202 |
|
for (x = 0; x < maxcolors; x++) |
1203 |
|
clrtab[x][RED] = clrtab[x][GRN] = |
1204 |
< |
clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors; |
1204 |
> |
clrtab[x][BLU] = ((int4)x*256 + 128)/maxcolors; |
1205 |
|
} |
1206 |
|
|
1207 |
|
|
1211 |
|
/* make sure we can do it first */ |
1212 |
|
if (fname == NULL) |
1213 |
|
quiterr("cannot map colors from standard input"); |
1214 |
< |
/* set gamma correction */ |
1215 |
< |
setcolrgam(gamcor); |
1214 |
> |
/* initialize tone mapping */ |
1215 |
> |
make_tonemap(); |
1216 |
|
/* make histogram */ |
1217 |
< |
new_histo(); |
1217 |
> |
if (new_histo((int4)xmax*ymax) == -1) |
1218 |
> |
quiterr("cannot initialize histogram"); |
1219 |
|
for (y = 0; y < ymax; y++) { |
1220 |
|
if (getscan(y) < 0) |
1221 |
|
break; |
1222 |
|
add2icon(y, scanline); |
1223 |
< |
if (scale) |
996 |
< |
shiftcolrs(scanline, xmax, scale); |
997 |
< |
colrs_gambs(scanline, xmax); |
1223 |
> |
tmap_colrs(scanline, xmax); |
1224 |
|
cnt_colrs(scanline, xmax); |
1225 |
|
} |
1226 |
|
/* map pixels */ |
1228 |
|
quiterr("cannot create color map"); |
1229 |
|
for (y = 0; y < ymax; y++) { |
1230 |
|
getscan(y); |
1231 |
< |
if (scale) |
1006 |
< |
shiftcolrs(scanline, xmax, scale); |
1007 |
< |
colrs_gambs(scanline, xmax); |
1231 |
> |
tmap_colrs(scanline, xmax); |
1232 |
|
if (dither) |
1233 |
|
dith_colrs(ourdata+y*xmax, scanline, xmax); |
1234 |
|
else |