| 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 |
|
/* |
| 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 */ |
| 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': |
| 736 |
|
case 't': /* trace */ |
| 737 |
|
return(traceray(ekey->x, ekey->y)); |
| 738 |
|
case 'a': /* auto exposure */ |
| 739 |
< |
if (tmflags == TM_F_CAMERA) |
| 740 |
< |
return(0); |
| 739 |
> |
if (fname == NULL) |
| 740 |
> |
return(-1); |
| 741 |
|
tmflags = TM_F_CAMERA; |
| 742 |
|
strcpy(buf, "auto exposure..."); |
| 743 |
|
goto remap; |
| 744 |
|
case 'h': /* human response */ |
| 745 |
< |
if (tmflags == TM_F_HUMAN) |
| 746 |
< |
return(0); |
| 745 |
> |
if (fname == NULL) |
| 746 |
> |
return(-1); |
| 747 |
|
tmflags = TM_F_HUMAN; |
| 748 |
|
strcpy(buf, "human exposure..."); |
| 749 |
|
goto remap; |
| 750 |
|
case '=': /* adjust exposure */ |
| 751 |
|
case '@': /* adaptation level */ |
| 752 |
< |
if (avgbox(cval) == -1) |
| 752 |
> |
if (!avgbox(cval)) |
| 753 |
|
return(-1); |
| 754 |
|
comp = bright(cval); |
| 755 |
|
if (comp < 1e-20) { |
| 763 |
|
comp = .5/comp; |
| 764 |
|
comp = log(comp)/.69315 - scale; |
| 765 |
|
n = comp < 0 ? comp-.5 : comp+.5 ; /* round */ |
| 766 |
< |
if (tmflags != -1) |
| 767 |
< |
tmflags = -1; /* turn off tone mapping */ |
| 766 |
> |
if (tmflags != TM_F_LINEAR) |
| 767 |
> |
tmflags = TM_F_LINEAR; /* turn off tone mapping */ |
| 768 |
|
else { |
| 769 |
|
if (n == 0) /* else check if any change */ |
| 770 |
|
return(0); |
| 896 |
|
} |
| 897 |
|
|
| 898 |
|
|
| 899 |
< |
avgbox(clr) /* average color over current box */ |
| 900 |
< |
COLOR clr; |
| 899 |
> |
int |
| 900 |
> |
colavg(scn, n, cavg) |
| 901 |
> |
register COLR *scn; |
| 902 |
> |
register int n; |
| 903 |
> |
COLOR cavg; |
| 904 |
|
{ |
| 905 |
< |
static COLOR lc; |
| 906 |
< |
static int ll, lr, lt, lb; |
| 905 |
> |
COLOR col; |
| 906 |
> |
|
| 907 |
> |
while (n--) { |
| 908 |
> |
colr_color(col, scn++); |
| 909 |
> |
addcolor(cavg, col); |
| 910 |
> |
} |
| 911 |
> |
} |
| 912 |
> |
|
| 913 |
> |
|
| 914 |
> |
int |
| 915 |
> |
avgbox(cavg) /* average color over current box */ |
| 916 |
> |
COLOR cavg; |
| 917 |
> |
{ |
| 918 |
> |
double d; |
| 919 |
> |
register int rval; |
| 920 |
> |
|
| 921 |
> |
setcolor(cavg, 0., 0., 0.); |
| 922 |
> |
rval = dobox(colavg, (char *)cavg); |
| 923 |
> |
if (rval > 0) { |
| 924 |
> |
d = 1./rval; |
| 925 |
> |
scalecolor(cavg, d); |
| 926 |
> |
} |
| 927 |
> |
return(rval); |
| 928 |
> |
} |
| 929 |
> |
|
| 930 |
> |
|
| 931 |
> |
int |
| 932 |
> |
dobox(f, p) /* run function over box */ |
| 933 |
> |
int (*f)(); /* function to call for each subscan */ |
| 934 |
> |
char *p; /* pointer to private data */ |
| 935 |
> |
{ |
| 936 |
|
int left, right, top, bottom; |
| 937 |
|
int y; |
| 904 |
– |
double d; |
| 905 |
– |
COLOR ctmp; |
| 906 |
– |
register int x; |
| 938 |
|
|
| 908 |
– |
setcolor(clr, 0.0, 0.0, 0.0); |
| 939 |
|
left = box.xmin - xoff; |
| 940 |
|
right = left + box.xsiz; |
| 941 |
|
if (left < 0) |
| 943 |
|
if (right > xmax) |
| 944 |
|
right = xmax; |
| 945 |
|
if (left >= right) |
| 946 |
< |
return(-1); |
| 946 |
> |
return(0); |
| 947 |
|
top = box.ymin - yoff; |
| 948 |
|
bottom = top + box.ysiz; |
| 949 |
|
if (top < 0) |
| 951 |
|
if (bottom > ymax) |
| 952 |
|
bottom = ymax; |
| 953 |
|
if (top >= bottom) |
| 924 |
– |
return(-1); |
| 925 |
– |
if (left == ll && right == lr && top == lt && bottom == lb) { |
| 926 |
– |
copycolor(clr, lc); |
| 954 |
|
return(0); |
| 928 |
– |
} |
| 955 |
|
for (y = top; y < bottom; y++) { |
| 956 |
|
if (getscan(y) == -1) |
| 957 |
|
return(-1); |
| 958 |
< |
for (x = left; x < right; x++) { |
| 933 |
< |
colr_color(ctmp, scanline[x]); |
| 934 |
< |
addcolor(clr, ctmp); |
| 935 |
< |
} |
| 958 |
> |
(*f)(scanline+left, right-left, p); |
| 959 |
|
} |
| 960 |
< |
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); |
| 960 |
> |
return((right-left)*(bottom-top)); |
| 961 |
|
} |
| 962 |
|
|
| 963 |
|
|
| 964 |
+ |
int |
| 965 |
+ |
addfix(scn, n) /* add fixation points to histogram */ |
| 966 |
+ |
COLR *scn; |
| 967 |
+ |
int n; |
| 968 |
+ |
{ |
| 969 |
+ |
if (tmCvColrs(lscan, TM_NOCHROM, scn, n)) |
| 970 |
+ |
goto tmerr; |
| 971 |
+ |
if (tmAddHisto(lscan, n, FIXWEIGHT)) |
| 972 |
+ |
goto tmerr; |
| 973 |
+ |
return; |
| 974 |
+ |
tmerr: |
| 975 |
+ |
quiterr("tone mapping error"); |
| 976 |
+ |
} |
| 977 |
+ |
|
| 978 |
+ |
|
| 979 |
|
make_tonemap() /* initialize tone mapping */ |
| 980 |
|
{ |
| 981 |
|
int flags, y; |
| 982 |
|
|
| 983 |
< |
if (tmflags != -1 && fname == NULL) { |
| 983 |
> |
if (tmflags != TM_F_LINEAR && fname == NULL) { |
| 984 |
|
fprintf(stderr, "%s: cannot adjust tone of standard input\n", |
| 985 |
|
progname); |
| 986 |
< |
tmflags = -1; |
| 986 |
> |
tmflags = TM_F_LINEAR; |
| 987 |
|
} |
| 988 |
< |
if (tmflags == -1) { |
| 988 |
> |
if (tmflags == TM_F_LINEAR) { /* linear with clamping */ |
| 989 |
|
setcolrcor(pow, 1.0/gamcor); |
| 990 |
|
return; |
| 991 |
|
} |
| 992 |
< |
if (tmTop != NULL) /* already initialized? */ |
| 959 |
< |
return; |
| 960 |
< |
flags = tmflags; |
| 992 |
> |
flags = tmflags; /* histogram adjustment */ |
| 993 |
|
if (greyscale) flags |= TM_F_BW; |
| 994 |
< |
/* initialize tm library */ |
| 995 |
< |
if (tmInit(flags, stdprims, gamcor) == NULL) |
| 996 |
< |
goto memerr; |
| 997 |
< |
if (tmSetSpace(stdprims, WHTEFFICACY/exposure)) |
| 998 |
< |
goto tmerr; |
| 967 |
< |
/* allocate encoding buffers */ |
| 968 |
< |
if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL) |
| 969 |
< |
goto memerr; |
| 970 |
< |
if (greyscale) { |
| 971 |
< |
cscan = TM_NOCHROM; |
| 972 |
< |
if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL) |
| 994 |
> |
if (tmTop != NULL) { /* reuse old histogram if one */ |
| 995 |
> |
tmDone(tmTop); |
| 996 |
> |
tmTop->flags = flags; |
| 997 |
> |
} else { /* else initialize */ |
| 998 |
> |
if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL) |
| 999 |
|
goto memerr; |
| 1000 |
< |
} else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax)) == NULL) |
| 1001 |
< |
goto memerr; |
| 1002 |
< |
/* compute picture histogram */ |
| 1003 |
< |
for (y = 0; y < ymax; y++) { |
| 1004 |
< |
getscan(y); |
| 1005 |
< |
if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax)) |
| 1000 |
> |
if (greyscale) { |
| 1001 |
> |
cscan = TM_NOCHROM; |
| 1002 |
> |
if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL) |
| 1003 |
> |
goto memerr; |
| 1004 |
> |
} else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax)) |
| 1005 |
> |
== NULL) |
| 1006 |
> |
goto memerr; |
| 1007 |
> |
/* initialize tm library */ |
| 1008 |
> |
if (tmInit(flags, stdprims, gamcor) == NULL) |
| 1009 |
> |
goto memerr; |
| 1010 |
> |
if (tmSetSpace(stdprims, WHTEFFICACY/exposure)) |
| 1011 |
|
goto tmerr; |
| 1012 |
< |
if (tmAddHisto(lscan, xmax, 1)) |
| 1013 |
< |
goto tmerr; |
| 1012 |
> |
/* compute picture histogram */ |
| 1013 |
> |
for (y = 0; y < ymax; y++) { |
| 1014 |
> |
getscan(y); |
| 1015 |
> |
if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax)) |
| 1016 |
> |
goto tmerr; |
| 1017 |
> |
if (tmAddHisto(lscan, xmax, 1)) |
| 1018 |
> |
goto tmerr; |
| 1019 |
> |
} |
| 1020 |
|
} |
| 1021 |
< |
/* compute tone mapping */ |
| 1021 |
> |
tmDup(); /* add fixations to duplicate map */ |
| 1022 |
> |
dobox(addfix, NULL); |
| 1023 |
> |
/* (re)compute tone mapping */ |
| 1024 |
|
if (tmComputeMapping(gamcor, 0., 0.)) |
| 1025 |
|
goto tmerr; |
| 1026 |
|
return; |
| 1037 |
|
{ |
| 1038 |
|
register BYTE *ps; |
| 1039 |
|
|
| 1040 |
< |
if (tmflags == -1) { |
| 1040 |
> |
if (tmflags == TM_F_LINEAR) { |
| 1041 |
|
if (scale) |
| 1042 |
|
shiftcolrs(scn, len, scale); |
| 1043 |
|
colrs_gambs(scn, len); |