| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id$";
|
| 3 |
#endif
|
| 4 |
/*
|
| 5 |
* Perform tone mapping on TIFF input.
|
| 6 |
*
|
| 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 |
*/
|
| 66 |
|
| 67 |
#include <stdio.h>
|
| 68 |
#include "tiffio.h"
|
| 69 |
#include "tmprivat.h"
|
| 70 |
#include "tmaptiff.h"
|
| 71 |
|
| 72 |
|
| 73 |
int
|
| 74 |
tmLoadTIFF(lpp, cpp, xp, yp, fname, tp) /* load and convert TIFF */
|
| 75 |
TMbright **lpp;
|
| 76 |
BYTE **cpp;
|
| 77 |
int *xp, *yp;
|
| 78 |
char *fname;
|
| 79 |
TIFF *tp;
|
| 80 |
{
|
| 81 |
char *funcName = fname==NULL ? "tmLoadTIFF" : fname;
|
| 82 |
TIFF *tif;
|
| 83 |
int err;
|
| 84 |
union {uint16 *w; uint32 *l; MEM_PTR p;} sl;
|
| 85 |
uint16 comp, phot, pconf;
|
| 86 |
uint32 width, height;
|
| 87 |
double stonits;
|
| 88 |
int y;
|
| 89 |
/* check arguments */
|
| 90 |
if (tmTop == NULL)
|
| 91 |
returnErr(TM_E_TMINVAL);
|
| 92 |
if (lpp == NULL | xp == NULL | yp == NULL |
|
| 93 |
(fname == NULL & tp == NULL))
|
| 94 |
returnErr(TM_E_ILLEGAL);
|
| 95 |
/* check/get TIFF tags */
|
| 96 |
sl.p = NULL; *lpp = NULL;
|
| 97 |
if (cpp != TM_NOCHROMP) *cpp = TM_NOCHROM;
|
| 98 |
err = TM_E_BADFILE;
|
| 99 |
if ((tif = tp) == NULL && (tif = TIFFOpen(fname, "r")) == NULL)
|
| 100 |
returnErr(TM_E_BADFILE);
|
| 101 |
TIFFGetFieldDefaulted(tif, TIFFTAG_PHOTOMETRIC, &phot);
|
| 102 |
TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &comp);
|
| 103 |
if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width) ||
|
| 104 |
!TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height))
|
| 105 |
goto done;
|
| 106 |
*xp = width; *yp = height;
|
| 107 |
TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &pconf);
|
| 108 |
if (pconf != PLANARCONFIG_CONTIG)
|
| 109 |
goto done;
|
| 110 |
if (!TIFFGetField(tif, TIFFTAG_STONITS, &stonits))
|
| 111 |
stonits = 1.;
|
| 112 |
if (phot == PHOTOMETRIC_LOGLUV) {
|
| 113 |
if (comp != COMPRESSION_SGILOG && comp != COMPRESSION_SGILOG24)
|
| 114 |
goto done;
|
| 115 |
TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_RAW);
|
| 116 |
sl.l = (uint32 *)malloc(width*sizeof(uint32));
|
| 117 |
if (cpp != TM_NOCHROMP) {
|
| 118 |
*cpp = (BYTE *)malloc(width*height*3*sizeof(BYTE));
|
| 119 |
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;
|
| 127 |
TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_16BIT);
|
| 128 |
sl.w = (uint16 *)malloc(width*sizeof(uint16));
|
| 129 |
} else
|
| 130 |
goto done;
|
| 131 |
*lpp = (TMbright *)malloc(width*height*sizeof(TMbright));
|
| 132 |
if (sl.p == NULL | *lpp == NULL) {
|
| 133 |
err = TM_E_NOMEM;
|
| 134 |
goto done;
|
| 135 |
}
|
| 136 |
/* set input color space */
|
| 137 |
tmSetSpace(TM_XYZPRIM, stonits);
|
| 138 |
/* read and convert each scanline */
|
| 139 |
for (y = 0; y < height; y++) {
|
| 140 |
if (TIFFReadScanline(tif, sl.p, y, 0) < 0) {
|
| 141 |
err = TM_E_BADFILE;
|
| 142 |
break;
|
| 143 |
}
|
| 144 |
if (phot == PHOTOMETRIC_LOGL)
|
| 145 |
err = tmCvL16(*lpp + y*width, sl.w, width);
|
| 146 |
else if (comp == COMPRESSION_SGILOG24)
|
| 147 |
err = tmCvLuv24(*lpp + y*width,
|
| 148 |
cpp==TM_NOCHROMP ? TM_NOCHROM : *cpp+y*3*width,
|
| 149 |
sl.l, width);
|
| 150 |
else
|
| 151 |
err = tmCvLuv32(*lpp + y*width,
|
| 152 |
cpp==TM_NOCHROMP ? TM_NOCHROM : *cpp+y*3*width,
|
| 153 |
sl.l, width);
|
| 154 |
if (err != TM_E_OK)
|
| 155 |
break;
|
| 156 |
}
|
| 157 |
done: /* clean up */
|
| 158 |
if (tp == NULL)
|
| 159 |
TIFFClose(tif);
|
| 160 |
if (sl.p != NULL)
|
| 161 |
free(sl.p);
|
| 162 |
if (err != TM_E_OK) { /* free buffers on error */
|
| 163 |
if (*lpp != NULL)
|
| 164 |
free((MEM_PTR)*lpp);
|
| 165 |
*lpp = NULL;
|
| 166 |
if (cpp != TM_NOCHROMP) {
|
| 167 |
if (*cpp != TM_NOCHROM)
|
| 168 |
free((MEM_PTR)*cpp);
|
| 169 |
*cpp = NULL;
|
| 170 |
}
|
| 171 |
*xp = *yp = 0;
|
| 172 |
returnErr(err);
|
| 173 |
}
|
| 174 |
returnOK;
|
| 175 |
}
|
| 176 |
|
| 177 |
|
| 178 |
/*
|
| 179 |
* Load and tone-map a SGILOG TIFF.
|
| 180 |
* Beware of greyscale input -- you must check the PHOTOMETRIC tag to
|
| 181 |
* determine that the returned array contains only grey values, not RGB.
|
| 182 |
* As in tmMapPicture(), grey values are also returned if flags&TM_F_BW.
|
| 183 |
*/
|
| 184 |
int
|
| 185 |
tmMapTIFF(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname, tp)
|
| 186 |
BYTE **psp;
|
| 187 |
int *xp, *yp;
|
| 188 |
int flags;
|
| 189 |
RGBPRIMP monpri;
|
| 190 |
double gamval, Lddyn, Ldmax;
|
| 191 |
char *fname;
|
| 192 |
TIFF *tp;
|
| 193 |
{
|
| 194 |
char *funcName = fname==NULL ? "tmMapTIFF" : fname;
|
| 195 |
TMbright *lp;
|
| 196 |
BYTE *cp;
|
| 197 |
int err;
|
| 198 |
/* check arguments */
|
| 199 |
if (psp == NULL | xp == NULL | yp == NULL | monpri == NULL |
|
| 200 |
(fname == NULL & tp == NULL))
|
| 201 |
returnErr(TM_E_ILLEGAL);
|
| 202 |
if (gamval < MINGAM) gamval = DEFGAM;
|
| 203 |
if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN;
|
| 204 |
if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX;
|
| 205 |
if (flags & TM_F_BW) monpri = stdprims;
|
| 206 |
/* initialize tone mapping */
|
| 207 |
if (tmInit(flags, monpri, gamval) == NULL)
|
| 208 |
returnErr(TM_E_NOMEM);
|
| 209 |
/* load and convert TIFF */
|
| 210 |
cp = TM_NOCHROM;
|
| 211 |
err = tmLoadTIFF(&lp, flags&TM_F_BW ? TM_NOCHROMP : &cp,
|
| 212 |
xp, yp, fname, tp);
|
| 213 |
if (err != TM_E_OK) {
|
| 214 |
tmDone(NULL);
|
| 215 |
return(err);
|
| 216 |
}
|
| 217 |
if (cp == TM_NOCHROM) {
|
| 218 |
*psp = (BYTE *)malloc(*xp * *yp * sizeof(BYTE));
|
| 219 |
if (*psp == NULL) {
|
| 220 |
free((MEM_PTR)lp);
|
| 221 |
tmDone(NULL);
|
| 222 |
returnErr(TM_E_NOMEM);
|
| 223 |
}
|
| 224 |
} else
|
| 225 |
*psp = cp;
|
| 226 |
/* compute color mapping */
|
| 227 |
err = tmAddHisto(lp, *xp * *yp, 1);
|
| 228 |
if (err != TM_E_OK)
|
| 229 |
goto done;
|
| 230 |
err = tmComputeMapping(gamval, Lddyn, Ldmax);
|
| 231 |
if (err != TM_E_OK)
|
| 232 |
goto done;
|
| 233 |
/* map pixels */
|
| 234 |
err = tmMapPixels(*psp, lp, cp, *xp * *yp);
|
| 235 |
|
| 236 |
done: /* clean up */
|
| 237 |
free((MEM_PTR)lp);
|
| 238 |
tmDone(NULL);
|
| 239 |
if (err != TM_E_OK) { /* free memory on error */
|
| 240 |
free((MEM_PTR)*psp);
|
| 241 |
*psp = NULL;
|
| 242 |
*xp = *yp = 0;
|
| 243 |
returnErr(err);
|
| 244 |
}
|
| 245 |
returnOK;
|
| 246 |
}
|