1 |
< |
/* Copyright (c) 1997 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 |
|
/* |
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 |
|
|
38 |
– |
#ifndef int4 |
39 |
– |
#define int4 int /* most int's are 32-bit */ |
40 |
– |
#endif |
41 |
– |
|
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 |
|
|
77 |
|
char *tout = "od"; /* output of 't' command */ |
78 |
|
int tinterv = 0; /* interval between mouse reports */ |
79 |
|
|
80 |
< |
int tmflags = -1; /* tone mapping flags (-1 for none) */ |
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 */ |
177 |
|
break; |
178 |
|
case 'e': /* exposure comp. */ |
179 |
|
i++; |
180 |
< |
if (!strcmp(argv[i], "auto")) { |
180 |
> |
if (argv[i][0] == 'a') { |
181 |
|
tmflags = TM_F_CAMERA; |
182 |
|
break; |
183 |
|
} |
184 |
< |
if (!strcmp(argv[i], "human")) { |
184 |
> |
if (argv[i][0] == 'h') { |
185 |
|
tmflags = TM_F_HUMAN; |
186 |
|
break; |
187 |
|
} |
250 |
|
} |
251 |
|
|
252 |
|
|
253 |
+ |
int |
254 |
|
headline(s) /* get relevant info from header */ |
255 |
|
char *s; |
256 |
|
{ |
262 |
|
wrongformat = strcmp(fmt, COLRFMT); |
263 |
|
else if (isview(s) && sscanview(&ourview, s) > 0) |
264 |
|
gotview++; |
265 |
+ |
return(0); |
266 |
|
} |
267 |
|
|
268 |
|
|
692 |
|
case '\r': |
693 |
|
case 'l': |
694 |
|
case 'c': /* value */ |
695 |
< |
if (avgbox(cval) == -1) |
695 |
> |
if (!avgbox(cval)) |
696 |
|
return(-1); |
697 |
|
switch (com) { |
698 |
|
case '\n': |
735 |
|
case 't': /* trace */ |
736 |
|
return(traceray(ekey->x, ekey->y)); |
737 |
|
case 'a': /* auto exposure */ |
738 |
< |
if (tmflags == TM_F_CAMERA) |
739 |
< |
return(0); |
738 |
> |
if (fname == NULL) |
739 |
> |
return(-1); |
740 |
|
tmflags = TM_F_CAMERA; |
741 |
|
strcpy(buf, "auto exposure..."); |
742 |
|
goto remap; |
743 |
|
case 'h': /* human response */ |
744 |
< |
if (tmflags == TM_F_HUMAN) |
745 |
< |
return(0); |
744 |
> |
if (fname == NULL) |
745 |
> |
return(-1); |
746 |
|
tmflags = TM_F_HUMAN; |
747 |
|
strcpy(buf, "human exposure..."); |
748 |
|
goto remap; |
749 |
|
case '=': /* adjust exposure */ |
750 |
|
case '@': /* adaptation level */ |
751 |
< |
if (avgbox(cval) == -1) |
751 |
> |
if (!avgbox(cval)) |
752 |
|
return(-1); |
753 |
|
comp = bright(cval); |
754 |
|
if (comp < 1e-20) { |
762 |
|
comp = .5/comp; |
763 |
|
comp = log(comp)/.69315 - scale; |
764 |
|
n = comp < 0 ? comp-.5 : comp+.5 ; /* round */ |
765 |
< |
if (tmflags != -1) |
766 |
< |
tmflags = -1; /* turn off tone mapping */ |
765 |
> |
if (tmflags != TM_F_LINEAR) |
766 |
> |
tmflags = TM_F_LINEAR; /* turn off tone mapping */ |
767 |
|
else { |
768 |
|
if (n == 0) /* else check if any change */ |
769 |
|
return(0); |
895 |
|
} |
896 |
|
|
897 |
|
|
898 |
< |
avgbox(clr) /* average color over current box */ |
899 |
< |
COLOR clr; |
898 |
> |
int |
899 |
> |
colavg(scn, n, cavg) |
900 |
> |
register COLR *scn; |
901 |
> |
register int n; |
902 |
> |
COLOR cavg; |
903 |
|
{ |
904 |
< |
static COLOR lc; |
905 |
< |
static int ll, lr, lt, lb; |
904 |
> |
COLOR col; |
905 |
> |
|
906 |
> |
while (n--) { |
907 |
> |
colr_color(col, scn++); |
908 |
> |
addcolor(cavg, col); |
909 |
> |
} |
910 |
> |
} |
911 |
> |
|
912 |
> |
|
913 |
> |
int |
914 |
> |
avgbox(cavg) /* average color over current box */ |
915 |
> |
COLOR cavg; |
916 |
> |
{ |
917 |
> |
double d; |
918 |
> |
register int rval; |
919 |
> |
|
920 |
> |
setcolor(cavg, 0., 0., 0.); |
921 |
> |
rval = dobox(colavg, (char *)cavg); |
922 |
> |
if (rval > 0) { |
923 |
> |
d = 1./rval; |
924 |
> |
scalecolor(cavg, d); |
925 |
> |
} |
926 |
> |
return(rval); |
927 |
> |
} |
928 |
> |
|
929 |
> |
|
930 |
> |
int |
931 |
> |
dobox(f, p) /* run function over box */ |
932 |
> |
int (*f)(); /* function to call for each subscan */ |
933 |
> |
char *p; /* pointer to private data */ |
934 |
> |
{ |
935 |
|
int left, right, top, bottom; |
936 |
|
int y; |
904 |
– |
double d; |
905 |
– |
COLOR ctmp; |
906 |
– |
register int x; |
937 |
|
|
908 |
– |
setcolor(clr, 0.0, 0.0, 0.0); |
938 |
|
left = box.xmin - xoff; |
939 |
|
right = left + box.xsiz; |
940 |
|
if (left < 0) |
942 |
|
if (right > xmax) |
943 |
|
right = xmax; |
944 |
|
if (left >= right) |
945 |
< |
return(-1); |
945 |
> |
return(0); |
946 |
|
top = box.ymin - yoff; |
947 |
|
bottom = top + box.ysiz; |
948 |
|
if (top < 0) |
950 |
|
if (bottom > ymax) |
951 |
|
bottom = ymax; |
952 |
|
if (top >= bottom) |
924 |
– |
return(-1); |
925 |
– |
if (left == ll && right == lr && top == lt && bottom == lb) { |
926 |
– |
copycolor(clr, lc); |
953 |
|
return(0); |
928 |
– |
} |
954 |
|
for (y = top; y < bottom; y++) { |
955 |
|
if (getscan(y) == -1) |
956 |
|
return(-1); |
957 |
< |
for (x = left; x < right; x++) { |
933 |
< |
colr_color(ctmp, scanline[x]); |
934 |
< |
addcolor(clr, ctmp); |
935 |
< |
} |
957 |
> |
(*f)(scanline+left, right-left, p); |
958 |
|
} |
959 |
< |
d = 1.0/((right-left)*(bottom-top)); |
938 |
< |
scalecolor(clr, d); |
939 |
< |
ll = left; lr = right; lt = top; lb = bottom; |
940 |
< |
copycolor(lc, clr); |
941 |
< |
return(0); |
959 |
> |
return((right-left)*(bottom-top)); |
960 |
|
} |
961 |
|
|
962 |
|
|
963 |
+ |
int |
964 |
+ |
addfix(scn, n) /* add fixation points to histogram */ |
965 |
+ |
COLR *scn; |
966 |
+ |
int n; |
967 |
+ |
{ |
968 |
+ |
if (tmCvColrs(lscan, TM_NOCHROM, scn, n)) |
969 |
+ |
goto tmerr; |
970 |
+ |
if (tmAddHisto(lscan, n, FIXWEIGHT)) |
971 |
+ |
goto tmerr; |
972 |
+ |
return; |
973 |
+ |
tmerr: |
974 |
+ |
quiterr("tone mapping error"); |
975 |
+ |
} |
976 |
+ |
|
977 |
+ |
|
978 |
|
make_tonemap() /* initialize tone mapping */ |
979 |
|
{ |
980 |
< |
int y; |
980 |
> |
int flags, y; |
981 |
|
|
982 |
< |
if (tmflags != -1 && fname == NULL) { |
982 |
> |
if (tmflags != TM_F_LINEAR && fname == NULL) { |
983 |
|
fprintf(stderr, "%s: cannot adjust tone of standard input\n", |
984 |
|
progname); |
985 |
< |
tmflags = -1; |
985 |
> |
tmflags = TM_F_LINEAR; |
986 |
|
} |
987 |
< |
if (tmflags == -1) { |
987 |
> |
if (tmflags == TM_F_LINEAR) { /* linear with clamping */ |
988 |
|
setcolrcor(pow, 1.0/gamcor); |
989 |
|
return; |
990 |
|
} |
991 |
< |
if (greyscale) |
992 |
< |
tmflags |= TM_F_BW; |
993 |
< |
/* initialize tm library */ |
994 |
< |
if (tmInit(tmflags, stdprims, gamcor) == NULL) |
995 |
< |
goto memerr; |
996 |
< |
if (tmSetSpace(stdprims, WHTEFFICACY/exposure)) |
997 |
< |
goto tmerr; |
965 |
< |
/* allocate encoding buffers */ |
966 |
< |
if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL) |
967 |
< |
goto memerr; |
968 |
< |
if (tmflags & TM_F_BW) { |
969 |
< |
cscan = TM_NOCHROM; |
970 |
< |
if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL) |
991 |
> |
flags = tmflags; /* histogram adjustment */ |
992 |
> |
if (greyscale) flags |= TM_F_BW; |
993 |
> |
if (tmTop != NULL) { /* reuse old histogram if one */ |
994 |
> |
tmDone(tmTop); |
995 |
> |
tmTop->flags = flags; |
996 |
> |
} else { /* else initialize */ |
997 |
> |
if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL) |
998 |
|
goto memerr; |
999 |
< |
} else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax)) == NULL) |
1000 |
< |
goto memerr; |
1001 |
< |
/* compute picture histogram */ |
1002 |
< |
for (y = 0; y < ymax; y++) { |
1003 |
< |
getscan(y); |
1004 |
< |
if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax)) |
999 |
> |
if (greyscale) { |
1000 |
> |
cscan = TM_NOCHROM; |
1001 |
> |
if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL) |
1002 |
> |
goto memerr; |
1003 |
> |
} else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax)) |
1004 |
> |
== NULL) |
1005 |
> |
goto memerr; |
1006 |
> |
/* initialize tm library */ |
1007 |
> |
if (tmInit(flags, stdprims, gamcor) == NULL) |
1008 |
> |
goto memerr; |
1009 |
> |
if (tmSetSpace(stdprims, WHTEFFICACY/exposure)) |
1010 |
|
goto tmerr; |
1011 |
< |
if (tmAddHisto(lscan, xmax, 1)) |
1012 |
< |
goto tmerr; |
1011 |
> |
/* compute picture histogram */ |
1012 |
> |
for (y = 0; y < ymax; y++) { |
1013 |
> |
getscan(y); |
1014 |
> |
if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax)) |
1015 |
> |
goto tmerr; |
1016 |
> |
if (tmAddHisto(lscan, xmax, 1)) |
1017 |
> |
goto tmerr; |
1018 |
> |
} |
1019 |
|
} |
1020 |
< |
/* compute tone mapping */ |
1020 |
> |
tmDup(); /* add fixations to duplicate map */ |
1021 |
> |
dobox(addfix, NULL); |
1022 |
> |
/* (re)compute tone mapping */ |
1023 |
|
if (tmComputeMapping(gamcor, 0., 0.)) |
1024 |
|
goto tmerr; |
985 |
– |
free((char *)lscan); |
1025 |
|
return; |
1026 |
|
memerr: |
1027 |
|
quiterr("out of memory in make_tonemap"); |
1036 |
|
{ |
1037 |
|
register BYTE *ps; |
1038 |
|
|
1039 |
< |
if (tmflags == -1) { |
1039 |
> |
if (tmflags == TM_F_LINEAR) { |
1040 |
|
if (scale) |
1041 |
|
shiftcolrs(scn, len, scale); |
1042 |
|
colrs_gambs(scn, len); |
1049 |
|
if (tmMapPixels(pscan, lscan, cscan, len)) |
1050 |
|
goto tmerr; |
1051 |
|
ps = pscan; |
1052 |
< |
if (tmflags & TM_F_BW) |
1052 |
> |
if (greyscale) |
1053 |
|
while (len--) { |
1054 |
|
scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++; |
1055 |
|
scn[0][EXP] = COLXS; |
1069 |
|
} |
1070 |
|
|
1071 |
|
|
1033 |
– |
done_tonemap() /* clean up after tone mapping is done */ |
1034 |
– |
{ |
1035 |
– |
if (tmflags == -1 || tmTop == NULL) |
1036 |
– |
return; |
1037 |
– |
tmDone(tmTop); /* clear old mapping */ |
1038 |
– |
free((char *)lscan); /* free memory */ |
1039 |
– |
free((char *)pscan); |
1040 |
– |
} |
1041 |
– |
|
1042 |
– |
|
1072 |
|
getmono() /* get monochrome data */ |
1073 |
|
{ |
1074 |
|
register unsigned char *dp; |
1177 |
|
(unsigned int4)scanline[x][GRN] << 8 | |
1178 |
|
(unsigned int4)scanline[x][BLU] << 16 ; |
1179 |
|
} |
1151 |
– |
done_tonemap(); |
1180 |
|
} |
1181 |
|
|
1182 |
|
|
1204 |
|
for (x = 0; x < maxcolors; x++) |
1205 |
|
clrtab[x][RED] = clrtab[x][GRN] = |
1206 |
|
clrtab[x][BLU] = ((int4)x*256 + 128)/maxcolors; |
1179 |
– |
done_tonemap(); |
1207 |
|
} |
1208 |
|
|
1209 |
|
|
1236 |
|
else |
1237 |
|
map_colrs(ourdata+y*xmax, scanline, xmax); |
1238 |
|
} |
1212 |
– |
done_tonemap(); |
1239 |
|
} |
1240 |
|
|
1241 |
|
|