1 /* $Id: tif_aux.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
4 * Copyright (c) 1991-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
30 * Auxiliary Support Routines.
33 #include "tif_predict.h"
37 TIFFDefaultTransferFunction(TIFFDirectory* td)
39 uint16 **tf = td->td_transferfunction;
42 tf[0] = tf[1] = tf[2] = 0;
43 if (td->td_bitspersample >= sizeof(tsize_t) * 8 - 2)
46 n = 1<<td->td_bitspersample;
47 nbytes = n * sizeof (uint16);
48 if (!(tf[0] = (uint16 *)_TIFFmalloc(nbytes)))
51 for (i = 1; i < n; i++) {
52 double t = (double)i/((double) n-1.);
53 tf[0][i] = (uint16)floor(65535.*pow(t, 2.2) + .5);
56 if (td->td_samplesperpixel - td->td_extrasamples > 1) {
57 if (!(tf[1] = (uint16 *)_TIFFmalloc(nbytes)))
59 _TIFFmemcpy(tf[1], tf[0], nbytes);
60 if (!(tf[2] = (uint16 *)_TIFFmalloc(nbytes)))
62 _TIFFmemcpy(tf[2], tf[0], nbytes);
73 tf[0] = tf[1] = tf[2] = 0;
78 TIFFDefaultRefBlackWhite(TIFFDirectory* td)
82 if (!(td->td_refblackwhite = (float *)_TIFFmalloc(6*sizeof (float))))
84 if (td->td_photometric == PHOTOMETRIC_YCBCR) {
86 * YCbCr (Class Y) images must have the ReferenceBlackWhite
87 * tag set. Fix the broken images, which lacks that tag.
89 td->td_refblackwhite[0] = 0.0F;
90 td->td_refblackwhite[1] = td->td_refblackwhite[3] =
91 td->td_refblackwhite[5] = 255.0F;
92 td->td_refblackwhite[2] = td->td_refblackwhite[4] = 128.0F;
95 * Assume RGB (Class R)
97 for (i = 0; i < 3; i++) {
98 td->td_refblackwhite[2*i+0] = 0;
99 td->td_refblackwhite[2*i+1] =
100 (float)((1L<<td->td_bitspersample)-1L);
107 * Like TIFFGetField, but return any default
108 * value if the tag is not present in the directory.
110 * NB: We use the value in the directory, rather than
111 * explcit values so that defaults exist only one
112 * place in the library -- in TIFFDefaultDirectory.
115 TIFFVGetFieldDefaulted(TIFF* tif, ttag_t tag, va_list ap)
117 TIFFDirectory *td = &tif->tif_dir;
119 if (TIFFVGetField(tif, tag, ap))
122 case TIFFTAG_SUBFILETYPE:
123 *va_arg(ap, uint32 *) = td->td_subfiletype;
125 case TIFFTAG_BITSPERSAMPLE:
126 *va_arg(ap, uint16 *) = td->td_bitspersample;
128 case TIFFTAG_THRESHHOLDING:
129 *va_arg(ap, uint16 *) = td->td_threshholding;
131 case TIFFTAG_FILLORDER:
132 *va_arg(ap, uint16 *) = td->td_fillorder;
134 case TIFFTAG_ORIENTATION:
135 *va_arg(ap, uint16 *) = td->td_orientation;
137 case TIFFTAG_SAMPLESPERPIXEL:
138 *va_arg(ap, uint16 *) = td->td_samplesperpixel;
140 case TIFFTAG_ROWSPERSTRIP:
141 *va_arg(ap, uint32 *) = td->td_rowsperstrip;
143 case TIFFTAG_MINSAMPLEVALUE:
144 *va_arg(ap, uint16 *) = td->td_minsamplevalue;
146 case TIFFTAG_MAXSAMPLEVALUE:
147 *va_arg(ap, uint16 *) = td->td_maxsamplevalue;
149 case TIFFTAG_PLANARCONFIG:
150 *va_arg(ap, uint16 *) = td->td_planarconfig;
152 case TIFFTAG_RESOLUTIONUNIT:
153 *va_arg(ap, uint16 *) = td->td_resolutionunit;
155 case TIFFTAG_PREDICTOR:
157 TIFFPredictorState* sp = (TIFFPredictorState*) tif->tif_data;
158 *va_arg(ap, uint16*) = (uint16) sp->predictor;
161 case TIFFTAG_DOTRANGE:
162 *va_arg(ap, uint16 *) = 0;
163 *va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1;
166 *va_arg(ap, uint16 *) = td->td_inkset;
168 case TIFFTAG_NUMBEROFINKS:
169 *va_arg(ap, uint16 *) = td->td_ninks;
171 case TIFFTAG_EXTRASAMPLES:
172 *va_arg(ap, uint16 *) = td->td_extrasamples;
173 *va_arg(ap, uint16 **) = td->td_sampleinfo;
175 case TIFFTAG_MATTEING:
176 *va_arg(ap, uint16 *) =
177 (td->td_extrasamples == 1 &&
178 td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
180 case TIFFTAG_TILEDEPTH:
181 *va_arg(ap, uint32 *) = td->td_tiledepth;
183 case TIFFTAG_DATATYPE:
184 *va_arg(ap, uint16 *) = td->td_sampleformat-1;
186 case TIFFTAG_SAMPLEFORMAT:
187 *va_arg(ap, uint16 *) = td->td_sampleformat;
189 case TIFFTAG_IMAGEDEPTH:
190 *va_arg(ap, uint32 *) = td->td_imagedepth;
192 case TIFFTAG_YCBCRCOEFFICIENTS:
193 if (!td->td_ycbcrcoeffs) {
194 td->td_ycbcrcoeffs = (float *)
195 _TIFFmalloc(3*sizeof (float));
196 if (!td->td_ycbcrcoeffs)
198 /* defaults are from CCIR Recommendation 601-1 */
199 td->td_ycbcrcoeffs[0] = 0.299f;
200 td->td_ycbcrcoeffs[1] = 0.587f;
201 td->td_ycbcrcoeffs[2] = 0.114f;
203 *va_arg(ap, float **) = td->td_ycbcrcoeffs;
205 case TIFFTAG_YCBCRSUBSAMPLING:
206 *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0];
207 *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1];
209 case TIFFTAG_YCBCRPOSITIONING:
210 *va_arg(ap, uint16 *) = td->td_ycbcrpositioning;
212 case TIFFTAG_WHITEPOINT:
213 if (!td->td_whitepoint) {
214 td->td_whitepoint = (float *)
215 _TIFFmalloc(2 * sizeof (float));
216 if (!td->td_whitepoint)
218 /* TIFF 6.0 specification says that it is no default
219 value for the WhitePoint, but AdobePhotoshop TIFF
220 Technical Note tells that it should be CIE D50. */
221 td->td_whitepoint[0] =
222 D50_X0 / (D50_X0 + D50_Y0 + D50_Z0);
223 td->td_whitepoint[1] =
224 D50_Y0 / (D50_X0 + D50_Y0 + D50_Z0);
226 *va_arg(ap, float **) = td->td_whitepoint;
228 case TIFFTAG_TRANSFERFUNCTION:
229 if (!td->td_transferfunction[0] &&
230 !TIFFDefaultTransferFunction(td)) {
231 TIFFError(tif->tif_name, "No space for \"TransferFunction\" tag");
234 *va_arg(ap, uint16 **) = td->td_transferfunction[0];
235 if (td->td_samplesperpixel - td->td_extrasamples > 1) {
236 *va_arg(ap, uint16 **) = td->td_transferfunction[1];
237 *va_arg(ap, uint16 **) = td->td_transferfunction[2];
240 case TIFFTAG_REFERENCEBLACKWHITE:
241 if (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(td))
243 *va_arg(ap, float **) = td->td_refblackwhite;
250 * Like TIFFGetField, but return any default
251 * value if the tag is not present in the directory.
254 TIFFGetFieldDefaulted(TIFF* tif, ttag_t tag, ...)
260 ok = TIFFVGetFieldDefaulted(tif, tag, ap);
265 /* vim: set ts=8 sts=8 sw=8 noet: */