1 |
< |
/* Copyright (c) 1997 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1997 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 "tonemap.h" |
33 |
|
#include "view.h" |
34 |
|
#include "x11raster.h" |
47 |
|
|
48 |
|
#define ICONSIZ (8*10) /* maximum icon dimension (even 8) */ |
49 |
|
|
50 |
+ |
#define FIXWEIGHT 20 /* weight to add for fixation points */ |
51 |
+ |
|
52 |
|
#define ourscreen DefaultScreen(thedisplay) |
53 |
|
#define ourroot RootWindow(thedisplay,ourscreen) |
54 |
|
|
80 |
|
char *tout = "od"; /* output of 't' command */ |
81 |
|
int tinterv = 0; /* interval between mouse reports */ |
82 |
|
|
83 |
< |
int tmflags = -1; /* tone mapping flags (-1 for none) */ |
83 |
> |
int tmflags = TM_F_LINEAR; /* tone mapping flags */ |
84 |
|
|
85 |
|
VIEW ourview = STDVIEW; /* image view parameters */ |
86 |
|
int gotview = 0; /* got parameters from file */ |
180 |
|
break; |
181 |
|
case 'e': /* exposure comp. */ |
182 |
|
i++; |
183 |
< |
if (!strcmp(argv[i], "auto")) { |
183 |
> |
if (argv[i][0] == 'a') { |
184 |
|
tmflags = TM_F_CAMERA; |
185 |
|
break; |
186 |
|
} |
187 |
< |
if (!strcmp(argv[i], "human")) { |
187 |
> |
if (argv[i][0] == 'h') { |
188 |
|
tmflags = TM_F_HUMAN; |
189 |
|
break; |
190 |
|
} |
693 |
|
case '\r': |
694 |
|
case 'l': |
695 |
|
case 'c': /* value */ |
696 |
< |
if (avgbox(cval) == -1) |
696 |
> |
if (!avgbox(cval)) |
697 |
|
return(-1); |
698 |
|
switch (com) { |
699 |
|
case '\n': |
735 |
|
return(0); |
736 |
|
case 't': /* trace */ |
737 |
|
return(traceray(ekey->x, ekey->y)); |
738 |
+ |
case 'a': /* auto exposure */ |
739 |
+ |
tmflags = TM_F_CAMERA; |
740 |
+ |
strcpy(buf, "auto exposure..."); |
741 |
+ |
goto remap; |
742 |
+ |
case 'h': /* human response */ |
743 |
+ |
tmflags = TM_F_HUMAN; |
744 |
+ |
strcpy(buf, "human exposure..."); |
745 |
+ |
goto remap; |
746 |
|
case '=': /* adjust exposure */ |
747 |
|
case '@': /* adaptation level */ |
748 |
< |
if (avgbox(cval) == -1) |
748 |
> |
if (!avgbox(cval)) |
749 |
|
return(-1); |
750 |
|
comp = bright(cval); |
751 |
|
if (comp < 1e-20) { |
759 |
|
comp = .5/comp; |
760 |
|
comp = log(comp)/.69315 - scale; |
761 |
|
n = comp < 0 ? comp-.5 : comp+.5 ; /* round */ |
762 |
< |
if (tmflags != -1) |
763 |
< |
tmflags = -1; /* turn off tone mapping */ |
762 |
> |
if (tmflags != TM_F_LINEAR) |
763 |
> |
tmflags = TM_F_LINEAR; /* turn off tone mapping */ |
764 |
|
else { |
765 |
|
if (n == 0) /* else check if any change */ |
766 |
|
return(0); |
768 |
|
} |
769 |
|
scale += n; |
770 |
|
sprintf(buf, "%+d", scale); |
771 |
+ |
remap: |
772 |
|
XDrawImageString(thedisplay, wind, ourgc, |
773 |
|
box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); |
774 |
|
XFlush(thedisplay); |
892 |
|
} |
893 |
|
|
894 |
|
|
895 |
< |
avgbox(clr) /* average color over current box */ |
896 |
< |
COLOR clr; |
895 |
> |
int |
896 |
> |
colavg(scn, n, cavg) |
897 |
> |
register COLR *scn; |
898 |
> |
register int n; |
899 |
> |
COLOR cavg; |
900 |
|
{ |
901 |
< |
static COLOR lc; |
902 |
< |
static int ll, lr, lt, lb; |
901 |
> |
COLOR col; |
902 |
> |
|
903 |
> |
while (n--) { |
904 |
> |
colr_color(col, scn++); |
905 |
> |
addcolor(cavg, col); |
906 |
> |
} |
907 |
> |
} |
908 |
> |
|
909 |
> |
|
910 |
> |
int |
911 |
> |
avgbox(cavg) /* average color over current box */ |
912 |
> |
COLOR cavg; |
913 |
> |
{ |
914 |
> |
double d; |
915 |
> |
register int rval; |
916 |
> |
|
917 |
> |
setcolor(cavg, 0., 0., 0.); |
918 |
> |
rval = dobox(colavg, (char *)cavg); |
919 |
> |
if (rval > 0) { |
920 |
> |
d = 1./rval; |
921 |
> |
scalecolor(cavg, d); |
922 |
> |
} |
923 |
> |
return(rval); |
924 |
> |
} |
925 |
> |
|
926 |
> |
|
927 |
> |
int |
928 |
> |
dobox(f, p) /* run function over box */ |
929 |
> |
int (*f)(); /* function to call for each subscan */ |
930 |
> |
char *p; /* pointer to private data */ |
931 |
> |
{ |
932 |
|
int left, right, top, bottom; |
933 |
|
int y; |
889 |
– |
double d; |
890 |
– |
COLOR ctmp; |
891 |
– |
register int x; |
934 |
|
|
893 |
– |
setcolor(clr, 0.0, 0.0, 0.0); |
935 |
|
left = box.xmin - xoff; |
936 |
|
right = left + box.xsiz; |
937 |
|
if (left < 0) |
939 |
|
if (right > xmax) |
940 |
|
right = xmax; |
941 |
|
if (left >= right) |
942 |
< |
return(-1); |
942 |
> |
return(0); |
943 |
|
top = box.ymin - yoff; |
944 |
|
bottom = top + box.ysiz; |
945 |
|
if (top < 0) |
947 |
|
if (bottom > ymax) |
948 |
|
bottom = ymax; |
949 |
|
if (top >= bottom) |
909 |
– |
return(-1); |
910 |
– |
if (left == ll && right == lr && top == lt && bottom == lb) { |
911 |
– |
copycolor(clr, lc); |
950 |
|
return(0); |
913 |
– |
} |
951 |
|
for (y = top; y < bottom; y++) { |
952 |
|
if (getscan(y) == -1) |
953 |
|
return(-1); |
954 |
< |
for (x = left; x < right; x++) { |
918 |
< |
colr_color(ctmp, scanline[x]); |
919 |
< |
addcolor(clr, ctmp); |
920 |
< |
} |
954 |
> |
(*f)(scanline+left, right-left, p); |
955 |
|
} |
956 |
< |
d = 1.0/((right-left)*(bottom-top)); |
923 |
< |
scalecolor(clr, d); |
924 |
< |
ll = left; lr = right; lt = top; lb = bottom; |
925 |
< |
copycolor(lc, clr); |
926 |
< |
return(0); |
956 |
> |
return((right-left)*(bottom-top)); |
957 |
|
} |
958 |
|
|
959 |
|
|
960 |
+ |
int |
961 |
+ |
addfix(scn, n) /* add fixation points to histogram */ |
962 |
+ |
COLR *scn; |
963 |
+ |
int n; |
964 |
+ |
{ |
965 |
+ |
if (tmCvColrs(lscan, TM_NOCHROM, scn, n)) |
966 |
+ |
goto tmerr; |
967 |
+ |
if (tmAddHisto(lscan, n, FIXWEIGHT)) |
968 |
+ |
goto tmerr; |
969 |
+ |
return; |
970 |
+ |
tmerr: |
971 |
+ |
quiterr("tone mapping error"); |
972 |
+ |
} |
973 |
+ |
|
974 |
+ |
|
975 |
|
make_tonemap() /* initialize tone mapping */ |
976 |
|
{ |
977 |
< |
int y; |
977 |
> |
int flags, y; |
978 |
|
|
979 |
< |
if (tmflags != -1 && fname == NULL) { |
979 |
> |
if (tmflags != TM_F_LINEAR && fname == NULL) { |
980 |
|
fprintf(stderr, "%s: cannot adjust tone of standard input\n", |
981 |
|
progname); |
982 |
< |
tmflags = -1; |
982 |
> |
tmflags = TM_F_LINEAR; |
983 |
|
} |
984 |
< |
if (tmflags == -1) { |
984 |
> |
if (tmflags == TM_F_LINEAR) { /* linear with clamping */ |
985 |
|
setcolrcor(pow, 1.0/gamcor); |
986 |
|
return; |
987 |
|
} |
988 |
< |
if (greyscale) |
989 |
< |
tmflags |= TM_F_BW; |
990 |
< |
/* initialize tm library */ |
991 |
< |
if (tmInit(tmflags, stdprims, gamcor) == NULL) |
992 |
< |
goto memerr; |
993 |
< |
if (tmSetSpace(stdprims, WHTEFFICACY/exposure)) |
994 |
< |
goto tmerr; |
950 |
< |
/* allocate encoding buffers */ |
951 |
< |
if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL) |
952 |
< |
goto memerr; |
953 |
< |
if (tmflags & TM_F_BW) { |
954 |
< |
cscan = TM_NOCHROM; |
955 |
< |
if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL) |
988 |
> |
flags = tmflags; /* histogram adjustment */ |
989 |
> |
if (greyscale) flags |= TM_F_BW; |
990 |
> |
if (tmTop != NULL) { /* reuse old histogram if one */ |
991 |
> |
tmDone(tmTop); |
992 |
> |
tmTop->flags = flags; |
993 |
> |
} else { /* else initialize */ |
994 |
> |
if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL) |
995 |
|
goto memerr; |
996 |
< |
} else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax)) == NULL) |
997 |
< |
goto memerr; |
998 |
< |
/* compute picture histogram */ |
999 |
< |
for (y = 0; y < ymax; y++) { |
1000 |
< |
getscan(y); |
1001 |
< |
if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax)) |
996 |
> |
if (greyscale) { |
997 |
> |
cscan = TM_NOCHROM; |
998 |
> |
if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL) |
999 |
> |
goto memerr; |
1000 |
> |
} else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax)) |
1001 |
> |
== NULL) |
1002 |
> |
goto memerr; |
1003 |
> |
/* initialize tm library */ |
1004 |
> |
if (tmInit(flags, stdprims, gamcor) == NULL) |
1005 |
> |
goto memerr; |
1006 |
> |
if (tmSetSpace(stdprims, WHTEFFICACY/exposure)) |
1007 |
|
goto tmerr; |
1008 |
< |
if (tmAddHisto(lscan, xmax, 1)) |
1009 |
< |
goto tmerr; |
1008 |
> |
/* compute picture histogram */ |
1009 |
> |
for (y = 0; y < ymax; y++) { |
1010 |
> |
getscan(y); |
1011 |
> |
if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax)) |
1012 |
> |
goto tmerr; |
1013 |
> |
if (tmAddHisto(lscan, xmax, 1)) |
1014 |
> |
goto tmerr; |
1015 |
> |
} |
1016 |
|
} |
1017 |
< |
/* compute tone mapping */ |
1017 |
> |
tmDup(); /* add fixations to duplicate map */ |
1018 |
> |
dobox(addfix, NULL); |
1019 |
> |
/* (re)compute tone mapping */ |
1020 |
|
if (tmComputeMapping(gamcor, 0., 0.)) |
1021 |
|
goto tmerr; |
970 |
– |
free((char *)lscan); |
1022 |
|
return; |
1023 |
|
memerr: |
1024 |
|
quiterr("out of memory in make_tonemap"); |
1033 |
|
{ |
1034 |
|
register BYTE *ps; |
1035 |
|
|
1036 |
< |
if (tmflags == -1) { |
1036 |
> |
if (tmflags == TM_F_LINEAR) { |
1037 |
|
if (scale) |
1038 |
|
shiftcolrs(scn, len, scale); |
1039 |
|
colrs_gambs(scn, len); |
1046 |
|
if (tmMapPixels(pscan, lscan, cscan, len)) |
1047 |
|
goto tmerr; |
1048 |
|
ps = pscan; |
1049 |
< |
if (tmflags & TM_F_BW) |
1049 |
> |
if (greyscale) |
1050 |
|
while (len--) { |
1051 |
|
scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++; |
1052 |
|
scn[0][EXP] = COLXS; |
1066 |
|
} |
1067 |
|
|
1068 |
|
|
1018 |
– |
done_tonemap() /* clean up after tone mapping is done */ |
1019 |
– |
{ |
1020 |
– |
if (tmflags == -1 || tmTop == NULL) |
1021 |
– |
return; |
1022 |
– |
tmDone(tmTop); /* clear old mapping */ |
1023 |
– |
free((char *)lscan); /* free memory */ |
1024 |
– |
free((char *)pscan); |
1025 |
– |
} |
1026 |
– |
|
1027 |
– |
|
1069 |
|
getmono() /* get monochrome data */ |
1070 |
|
{ |
1071 |
|
register unsigned char *dp; |
1174 |
|
(unsigned int4)scanline[x][GRN] << 8 | |
1175 |
|
(unsigned int4)scanline[x][BLU] << 16 ; |
1176 |
|
} |
1136 |
– |
done_tonemap(); |
1177 |
|
} |
1178 |
|
|
1179 |
|
|
1201 |
|
for (x = 0; x < maxcolors; x++) |
1202 |
|
clrtab[x][RED] = clrtab[x][GRN] = |
1203 |
|
clrtab[x][BLU] = ((int4)x*256 + 128)/maxcolors; |
1164 |
– |
done_tonemap(); |
1204 |
|
} |
1205 |
|
|
1206 |
|
|
1233 |
|
else |
1234 |
|
map_colrs(ourdata+y*xmax, scanline, xmax); |
1235 |
|
} |
1197 |
– |
done_tonemap(); |
1236 |
|
} |
1237 |
|
|
1238 |
|
|