ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tmaptiff.c
(Generate patch)

Comparing ray/src/common/tmaptiff.c (file contents):
Revision 3.1 by greg, Sat Feb 22 02:07:22 2003 UTC vs.
Revision 3.8 by greg, Mon Feb 9 20:48:08 2009 UTC

# Line 7 | Line 7 | static const char      RCSid[] = "$Id$";
7   * Externals declared in tmaptiff.h
8   */
9  
10 < /* ====================================================================
11 < * The Radiance Software License, Version 1.0
12 < *
13 < * Copyright (c) 1990 - 2002 The Regents of the University of California,
14 < * through Lawrence Berkeley National Laboratory.   All rights reserved.
15 < *
16 < * Redistribution and use in source and binary forms, with or without
17 < * modification, are permitted provided that the following conditions
18 < * are met:
19 < *
20 < * 1. Redistributions of source code must retain the above copyright
21 < *         notice, this list of conditions and the following disclaimer.
22 < *
23 < * 2. Redistributions in binary form must reproduce the above copyright
24 < *       notice, this list of conditions and the following disclaimer in
25 < *       the documentation and/or other materials provided with the
26 < *       distribution.
27 < *
28 < * 3. The end-user documentation included with the redistribution,
29 < *           if any, must include the following acknowledgment:
30 < *             "This product includes Radiance software
31 < *                 (http://radsite.lbl.gov/)
32 < *                 developed by the Lawrence Berkeley National Laboratory
33 < *               (http://www.lbl.gov/)."
34 < *       Alternately, this acknowledgment may appear in the software itself,
35 < *       if and wherever such third-party acknowledgments normally appear.
36 < *
37 < * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
38 < *       and "The Regents of the University of California" must
39 < *       not be used to endorse or promote products derived from this
40 < *       software without prior written permission. For written
41 < *       permission, please contact [email protected].
42 < *
43 < * 5. Products derived from this software may not be called "Radiance",
44 < *       nor may "Radiance" appear in their name, without prior written
45 < *       permission of Lawrence Berkeley National Laboratory.
46 < *
47 < * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
48 < * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 < * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50 < * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
51 < * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 < * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53 < * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54 < * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55 < * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56 < * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57 < * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 < * SUCH DAMAGE.
59 < * ====================================================================
60 < *
61 < * This software consists of voluntary contributions made by many
62 < * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
63 < * information on Lawrence Berkeley National Laboratory, please see
64 < * <http://www.lbl.gov/>.
65 < */
10 > #include "copyright.h"
11  
12   #include <stdio.h>
13 < #include "tiffio.h"
13 > #include <stdlib.h>
14   #include "tmprivat.h"
15 + #include "tiffio.h"
16   #include "tmaptiff.h"
17  
18 +                                        /* input cases we handle */
19 + #define TC_LOGLUV32     1
20 + #define TC_LOGLUV24     2
21 + #define TC_LOGL16       3
22 + #define TC_GRYFLOAT     4
23 + #define TC_RGBFLOAT     5
24 + #define TC_GRYSHORT     6
25 + #define TC_RGBSHORT     7
26  
27 + /* figure out what kind of TIFF we have and if we can tone-map it */
28 + static int
29 + getTIFFtype(TIFF *tif)
30 + {
31 +        uint16  comp, phot, pconf;
32 +        uint16  samp_fmt, bits_samp;
33 +        
34 +        TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &pconf);
35 +        if (pconf != PLANARCONFIG_CONTIG)
36 +                return(0);
37 +        TIFFGetFieldDefaulted(tif, TIFFTAG_PHOTOMETRIC, &phot);
38 +        TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &comp);
39 +        TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLEFORMAT, &samp_fmt);
40 +        TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &bits_samp);
41 +        switch (phot) {
42 +        case PHOTOMETRIC_LOGLUV:
43 +                if (comp == COMPRESSION_SGILOG)
44 +                        return(TC_LOGLUV32);
45 +                if (comp == COMPRESSION_SGILOG24)
46 +                        return(TC_LOGLUV24);
47 +                return(0);
48 +        case PHOTOMETRIC_LOGL:
49 +                if (comp == COMPRESSION_SGILOG)
50 +                        return(TC_LOGL16);
51 +                return(0);
52 +        case PHOTOMETRIC_MINISBLACK:
53 +                if (samp_fmt == SAMPLEFORMAT_UINT) {
54 +                        if (bits_samp == 16)
55 +                                return(TC_GRYSHORT);
56 +                        return(0);
57 +                }
58 +                if (samp_fmt == SAMPLEFORMAT_IEEEFP) {
59 +                        if (bits_samp == 8*sizeof(float))
60 +                                return(TC_GRYFLOAT);
61 +                        return(0);
62 +                }
63 +                return(0);
64 +        case PHOTOMETRIC_RGB:
65 +                if (samp_fmt == SAMPLEFORMAT_UINT) {
66 +                        if (bits_samp == 16)
67 +                                return(TC_RGBSHORT);
68 +                        return(0);
69 +                }
70 +                if (samp_fmt == SAMPLEFORMAT_IEEEFP) {
71 +                        if (bits_samp == 8*sizeof(float))
72 +                                return(TC_RGBFLOAT);
73 +                        return(0);
74 +                }
75 +                return(0);
76 +        }
77 +        return(0);
78 + }
79 +
80 + /* load and convert TIFF */
81   int
82 < tmLoadTIFF(lpp, cpp, xp, yp, fname, tp) /* load and convert TIFF */
83 < TMbright        **lpp;
76 < BYTE    **cpp;
77 < int     *xp, *yp;
78 < char    *fname;
79 < TIFF    *tp;
82 > tmLoadTIFF(TMstruct *tms, TMbright **lpp, BYTE **cpp,
83 >                int *xp, int *yp, char *fname, TIFF *tp)
84   {
85          char    *funcName = fname==NULL ? "tmLoadTIFF" : fname;
86 +        RGBPRIMP        inppri = tms->monpri;
87 +        RGBPRIMS        myprims;
88 +        float   *fa;
89          TIFF    *tif;
90          int     err;
91 <        union {uint16 *w; uint32 *l; MEM_PTR p;} sl;
85 <        uint16  comp, phot, pconf;
91 >        union {uint16 *w; uint32 *l; float *f; MEM_PTR p;} sl;
92          uint32  width, height;
93 +        int     tcase;
94          double  stonits;
95          int     y;
96                                          /* check arguments */
97 <        if (tmTop == NULL)
97 >        if (tms == NULL)
98                  returnErr(TM_E_TMINVAL);
99 <        if (lpp == NULL | xp == NULL | yp == NULL |
100 <                        (fname == NULL & tp == NULL))
99 >        if ((lpp == NULL) | (xp == NULL) | (yp == NULL) |
100 >                        ((fname == NULL) & (tp == NULL)))
101                  returnErr(TM_E_ILLEGAL);
102                                          /* check/get TIFF tags */
103          sl.p = NULL; *lpp = NULL;
# Line 98 | Line 105 | TIFF   *tp;
105          err = TM_E_BADFILE;
106          if ((tif = tp) == NULL && (tif = TIFFOpen(fname, "r")) == NULL)
107                  returnErr(TM_E_BADFILE);
108 <        TIFFGetFieldDefaulted(tif, TIFFTAG_PHOTOMETRIC, &phot);
109 <        TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &comp);
108 >        tcase = getTIFFtype(tif);
109 >        if (!tcase)
110 >                goto done;
111          if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width) ||
112                          !TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height))
113                  goto done;
114          *xp = width; *yp = height;
115 <        TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &pconf);
116 <        if (pconf != PLANARCONFIG_CONTIG)
117 <                goto done;
115 >        if (TIFFGetField(tif, TIFFTAG_PRIMARYCHROMATICITIES, &fa)) {
116 >                myprims[RED][CIEX] = fa[0];
117 >                myprims[RED][CIEY] = fa[1];
118 >                myprims[GRN][CIEX] = fa[2];
119 >                myprims[GRN][CIEY] = fa[3];
120 >                myprims[BLU][CIEX] = fa[4];
121 >                myprims[BLU][CIEY] = fa[5];
122 >                myprims[WHT][CIEX] = 1./3.;
123 >                myprims[WHT][CIEY] = 1./3.;
124 >                if (TIFFGetField(tif, TIFFTAG_WHITEPOINT, &fa)) {
125 >                        myprims[WHT][CIEX] = fa[0];
126 >                        myprims[WHT][CIEY] = fa[1];
127 >                }
128 >                inppri = myprims;
129 >        }
130          if (!TIFFGetField(tif, TIFFTAG_STONITS, &stonits))
131                  stonits = 1.;
132 <        if (phot == PHOTOMETRIC_LOGLUV) {
133 <                if (comp != COMPRESSION_SGILOG && comp != COMPRESSION_SGILOG24)
134 <                        goto done;
132 >        switch (tcase) {                /* set up conversion */
133 >        case TC_LOGLUV32:
134 >        case TC_LOGLUV24:
135                  TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_RAW);
136                  sl.l = (uint32 *)malloc(width*sizeof(uint32));
137 <                if (cpp != TM_NOCHROMP) {
138 <                        *cpp = (BYTE *)malloc(width*height*3*sizeof(BYTE));
139 <                        if (*cpp == NULL) {
120 <                                err = TM_E_NOMEM;
121 <                                goto done;
122 <                        }
123 <                }
124 <        } else if (phot == PHOTOMETRIC_LOGL) {
125 <                if (comp != COMPRESSION_SGILOG)
126 <                        goto done;
137 >                tmSetSpace(tms, TM_XYZPRIM, stonits, NULL);
138 >                break;
139 >        case TC_LOGL16:
140                  TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_16BIT);
141                  sl.w = (uint16 *)malloc(width*sizeof(uint16));
142 <        } else
142 >                tmSetSpace(tms, tms->monpri, stonits, NULL);
143 >                break;
144 >        case TC_RGBFLOAT:
145 >                sl.f = (float *)malloc(width*3*sizeof(float));
146 >                tmSetSpace(tms, inppri, stonits, NULL);
147 >                break;
148 >        case TC_GRYFLOAT:
149 >                sl.f = (float *)malloc(width*sizeof(float));
150 >                tmSetSpace(tms, tms->monpri, stonits, NULL);
151 >                break;
152 >        case TC_RGBSHORT:
153 >                sl.w = (uint16 *)malloc(width*3*sizeof(uint16));
154 >                tmSetSpace(tms, inppri, stonits, NULL);
155 >                break;
156 >        case TC_GRYSHORT:
157 >                sl.w = (uint16 *)malloc(width*sizeof(uint16));
158 >                tmSetSpace(tms, tms->monpri, stonits, NULL);
159 >                break;
160 >        default:
161 >                err = TM_E_CODERR1;
162                  goto done;
163 +        }
164          *lpp = (TMbright *)malloc(width*height*sizeof(TMbright));
165 <        if (sl.p == NULL | *lpp == NULL) {
165 >        if ((sl.p == NULL) | (*lpp == NULL)) {
166                  err = TM_E_NOMEM;
167                  goto done;
168          }
169 <                                        /* set input color space */
170 <        tmSetSpace(TM_XYZPRIM, stonits);
169 >        switch (tcase) {                /* allocate color if needed */
170 >        case TC_LOGLUV32:
171 >        case TC_LOGLUV24:
172 >        case TC_RGBFLOAT:
173 >        case TC_RGBSHORT:
174 >                if (cpp == TM_NOCHROMP)
175 >                        break;
176 >                *cpp = (BYTE *)malloc(width*height*3*sizeof(BYTE));
177 >                if (*cpp == NULL) {
178 >                        err = TM_E_NOMEM;
179 >                        goto done;
180 >                }
181 >                break;
182 >        }
183                                          /* read and convert each scanline */
184          for (y = 0; y < height; y++) {
185                  if (TIFFReadScanline(tif, sl.p, y, 0) < 0) {
186                          err = TM_E_BADFILE;
187                          break;
188                  }
189 <                if (phot == PHOTOMETRIC_LOGL)
190 <                        err = tmCvL16(*lpp + y*width, sl.w, width);
191 <                else if (comp == COMPRESSION_SGILOG24)
147 <                        err = tmCvLuv24(*lpp + y*width,
189 >                switch (tcase) {
190 >                case TC_LOGLUV32:
191 >                        err = tmCvLuv32(tms, *lpp + y*width,
192                                  cpp==TM_NOCHROMP ? TM_NOCHROM : *cpp+y*3*width,
193                                          sl.l, width);
194 <                else
195 <                        err = tmCvLuv32(*lpp + y*width,
194 >                        break;
195 >                case TC_LOGLUV24:
196 >                        err = tmCvLuv24(tms, *lpp + y*width,
197                                  cpp==TM_NOCHROMP ? TM_NOCHROM : *cpp+y*3*width,
198                                          sl.l, width);
199 +                        break;
200 +                case TC_LOGL16:
201 +                        err = tmCvL16(tms, *lpp + y*width, sl.w, width);
202 +                        break;
203 +                case TC_RGBFLOAT:
204 +                        err = tmCvColors(tms, *lpp + y*width,
205 +                                cpp==TM_NOCHROMP ? TM_NOCHROM : *cpp+y*3*width,
206 +                                        (COLOR *)sl.f, width);
207 +                        break;
208 +                case TC_GRYFLOAT:
209 +                        err = tmCvGrays(tms, *lpp + y*width, sl.f, width);
210 +                        break;
211 +                case TC_RGBSHORT:
212 +                        err = tmCvRGB48(tms, *lpp + y*width,
213 +                                cpp==TM_NOCHROMP ? TM_NOCHROM : *cpp+y*3*width,
214 +                                        (uint16 (*)[3])sl.w, width, DEFGAM);
215 +                        break;
216 +                case TC_GRYSHORT:
217 +                        err = tmCvGray16(tms, *lpp + y*width,
218 +                                        sl.w, width, DEFGAM);
219 +                        break;
220 +                default:
221 +                        err = TM_E_CODERR1;
222 +                        break;
223 +                }
224                  if (err != TM_E_OK)
225                          break;
226          }
# Line 182 | Line 252 | done:                                  /* clean up */
252   * As in tmMapPicture(), grey values are also returned if flags&TM_F_BW.
253   */
254   int
255 < tmMapTIFF(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname, tp)
256 < BYTE    **psp;
187 < int     *xp, *yp;
188 < int     flags;
189 < RGBPRIMP        monpri;
190 < double  gamval, Lddyn, Ldmax;
191 < char    *fname;
192 < TIFF    *tp;
255 > tmMapTIFF(BYTE **psp, int *xp, int *yp, int flags, RGBPRIMP monpri,
256 >        double gamval, double Lddyn, double Ldmax, char *fname, TIFF *tp)
257   {
258          char    *funcName = fname==NULL ? "tmMapTIFF" : fname;
259 +        TMstruct        *tms;
260          TMbright        *lp;
261          BYTE    *cp;
262          int     err;
263                                          /* check arguments */
264 <        if (psp == NULL | xp == NULL | yp == NULL | monpri == NULL |
265 <                        (fname == NULL & tp == NULL))
264 >        if ((psp == NULL) | (xp == NULL) | (yp == NULL) | (monpri == NULL) |
265 >                        ((fname == NULL) & (tp == NULL)))
266                  returnErr(TM_E_ILLEGAL);
267          if (gamval < MINGAM) gamval = DEFGAM;
268          if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN;
269          if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX;
270          if (flags & TM_F_BW) monpri = stdprims;
271                                          /* initialize tone mapping */
272 <        if (tmInit(flags, monpri, gamval) == NULL)
272 >        if ((tms = tmInit(flags, monpri, gamval)) == NULL)
273                  returnErr(TM_E_NOMEM);
274                                          /* load and convert TIFF */
275          cp = TM_NOCHROM;
276 <        err = tmLoadTIFF(&lp, flags&TM_F_BW ? TM_NOCHROMP : &cp,
276 >        err = tmLoadTIFF(tms, &lp, flags&TM_F_BW ? TM_NOCHROMP : &cp,
277                          xp, yp, fname, tp);
278          if (err != TM_E_OK) {
279 <                tmDone(NULL);
279 >                tmDone(tms);
280                  return(err);
281          }
282          if (cp == TM_NOCHROM) {
283                  *psp = (BYTE *)malloc(*xp * *yp * sizeof(BYTE));
284                  if (*psp == NULL) {
285                          free((MEM_PTR)lp);
286 <                        tmDone(NULL);
286 >                        tmDone(tms);
287                          returnErr(TM_E_NOMEM);
288                  }
289          } else
290                  *psp = cp;
291                                          /* compute color mapping */
292 <        err = tmAddHisto(lp, *xp * *yp, 1);
292 >        err = tmAddHisto(tms, lp, *xp * *yp, 1);
293          if (err != TM_E_OK)
294                  goto done;
295 <        err = tmComputeMapping(gamval, Lddyn, Ldmax);
295 >        err = tmComputeMapping(tms, gamval, Lddyn, Ldmax);
296          if (err != TM_E_OK)
297                  goto done;
298                                          /* map pixels */
299 <        err = tmMapPixels(*psp, lp, cp, *xp * *yp);
299 >        err = tmMapPixels(tms, *psp, lp, cp, *xp * *yp);
300  
301   done:                                   /* clean up */
302          free((MEM_PTR)lp);
303 <        tmDone(NULL);
303 >        tmDone(tms);
304          if (err != TM_E_OK) {           /* free memory on error */
305                  free((MEM_PTR)*psp);
306                  *psp = NULL;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines