1 /* $Id: tif_aux.c,v 1.20.2.3 2010-06-09 21:15:27 bfriesen 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 _TIFFCheckRealloc(TIFF
* tif
, tdata_t buffer
,
38 size_t nmemb
, size_t elem_size
, const char* what
)
41 tsize_t bytes
= nmemb
* elem_size
;
44 * XXX: Check for integer overflow.
46 if (nmemb
&& elem_size
&& bytes
/ elem_size
== nmemb
)
47 cp
= _TIFFrealloc(buffer
, bytes
);
50 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
51 "Failed to allocate memory for %s "
52 "(%ld elements of %ld bytes each)",
53 what
,(long) nmemb
, (long) elem_size
);
59 _TIFFCheckMalloc(TIFF
* tif
, size_t nmemb
, size_t elem_size
, const char* what
)
61 return _TIFFCheckRealloc(tif
, NULL
, nmemb
, elem_size
, what
);
65 TIFFDefaultTransferFunction(TIFFDirectory
* td
)
67 uint16
**tf
= td
->td_transferfunction
;
70 tf
[0] = tf
[1] = tf
[2] = 0;
71 if (td
->td_bitspersample
>= sizeof(tsize_t
) * 8 - 2)
74 n
= 1<<td
->td_bitspersample
;
75 nbytes
= n
* sizeof (uint16
);
76 if (!(tf
[0] = (uint16
*)_TIFFmalloc(nbytes
)))
79 for (i
= 1; i
< n
; i
++) {
80 double t
= (double)i
/((double) n
-1.);
81 tf
[0][i
] = (uint16
)floor(65535.*pow(t
, 2.2) + .5);
84 if (td
->td_samplesperpixel
- td
->td_extrasamples
> 1) {
85 if (!(tf
[1] = (uint16
*)_TIFFmalloc(nbytes
)))
87 _TIFFmemcpy(tf
[1], tf
[0], nbytes
);
88 if (!(tf
[2] = (uint16
*)_TIFFmalloc(nbytes
)))
90 _TIFFmemcpy(tf
[2], tf
[0], nbytes
);
101 tf
[0] = tf
[1] = tf
[2] = 0;
106 TIFFDefaultRefBlackWhite(TIFFDirectory
* td
)
110 if (!(td
->td_refblackwhite
= (float *)_TIFFmalloc(6*sizeof (float))))
112 if (td
->td_photometric
== PHOTOMETRIC_YCBCR
) {
114 * YCbCr (Class Y) images must have the ReferenceBlackWhite
115 * tag set. Fix the broken images, which lacks that tag.
117 td
->td_refblackwhite
[0] = 0.0F
;
118 td
->td_refblackwhite
[1] = td
->td_refblackwhite
[3] =
119 td
->td_refblackwhite
[5] = 255.0F
;
120 td
->td_refblackwhite
[2] = td
->td_refblackwhite
[4] = 128.0F
;
123 * Assume RGB (Class R)
125 for (i
= 0; i
< 3; i
++) {
126 td
->td_refblackwhite
[2*i
+0] = 0;
127 td
->td_refblackwhite
[2*i
+1] =
128 (float)((1L<<td
->td_bitspersample
)-1L);
135 * Like TIFFGetField, but return any default
136 * value if the tag is not present in the directory.
138 * NB: We use the value in the directory, rather than
139 * explcit values so that defaults exist only one
140 * place in the library -- in TIFFDefaultDirectory.
143 TIFFVGetFieldDefaulted(TIFF
* tif
, ttag_t tag
, va_list ap
)
145 TIFFDirectory
*td
= &tif
->tif_dir
;
147 if (TIFFVGetField(tif
, tag
, ap
))
150 case TIFFTAG_SUBFILETYPE
:
151 *va_arg(ap
, uint32
*) = td
->td_subfiletype
;
153 case TIFFTAG_BITSPERSAMPLE
:
154 *va_arg(ap
, uint16
*) = td
->td_bitspersample
;
156 case TIFFTAG_THRESHHOLDING
:
157 *va_arg(ap
, uint16
*) = td
->td_threshholding
;
159 case TIFFTAG_FILLORDER
:
160 *va_arg(ap
, uint16
*) = td
->td_fillorder
;
162 case TIFFTAG_ORIENTATION
:
163 *va_arg(ap
, uint16
*) = td
->td_orientation
;
165 case TIFFTAG_SAMPLESPERPIXEL
:
166 *va_arg(ap
, uint16
*) = td
->td_samplesperpixel
;
168 case TIFFTAG_ROWSPERSTRIP
:
169 *va_arg(ap
, uint32
*) = td
->td_rowsperstrip
;
171 case TIFFTAG_MINSAMPLEVALUE
:
172 *va_arg(ap
, uint16
*) = td
->td_minsamplevalue
;
174 case TIFFTAG_MAXSAMPLEVALUE
:
175 *va_arg(ap
, uint16
*) = td
->td_maxsamplevalue
;
177 case TIFFTAG_PLANARCONFIG
:
178 *va_arg(ap
, uint16
*) = td
->td_planarconfig
;
180 case TIFFTAG_RESOLUTIONUNIT
:
181 *va_arg(ap
, uint16
*) = td
->td_resolutionunit
;
183 case TIFFTAG_PREDICTOR
:
185 TIFFPredictorState
* sp
= (TIFFPredictorState
*) tif
->tif_data
;
186 *va_arg(ap
, uint16
*) = (uint16
) sp
->predictor
;
189 case TIFFTAG_DOTRANGE
:
190 *va_arg(ap
, uint16
*) = 0;
191 *va_arg(ap
, uint16
*) = (1<<td
->td_bitspersample
)-1;
194 *va_arg(ap
, uint16
*) = INKSET_CMYK
;
196 case TIFFTAG_NUMBEROFINKS
:
197 *va_arg(ap
, uint16
*) = 4;
199 case TIFFTAG_EXTRASAMPLES
:
200 *va_arg(ap
, uint16
*) = td
->td_extrasamples
;
201 *va_arg(ap
, uint16
**) = td
->td_sampleinfo
;
203 case TIFFTAG_MATTEING
:
204 *va_arg(ap
, uint16
*) =
205 (td
->td_extrasamples
== 1 &&
206 td
->td_sampleinfo
[0] == EXTRASAMPLE_ASSOCALPHA
);
208 case TIFFTAG_TILEDEPTH
:
209 *va_arg(ap
, uint32
*) = td
->td_tiledepth
;
211 case TIFFTAG_DATATYPE
:
212 *va_arg(ap
, uint16
*) = td
->td_sampleformat
-1;
214 case TIFFTAG_SAMPLEFORMAT
:
215 *va_arg(ap
, uint16
*) = td
->td_sampleformat
;
217 case TIFFTAG_IMAGEDEPTH
:
218 *va_arg(ap
, uint32
*) = td
->td_imagedepth
;
220 case TIFFTAG_YCBCRCOEFFICIENTS
:
222 /* defaults are from CCIR Recommendation 601-1 */
223 static float ycbcrcoeffs
[] = { 0.299f
, 0.587f
, 0.114f
};
224 *va_arg(ap
, float **) = ycbcrcoeffs
;
227 case TIFFTAG_YCBCRSUBSAMPLING
:
228 *va_arg(ap
, uint16
*) = td
->td_ycbcrsubsampling
[0];
229 *va_arg(ap
, uint16
*) = td
->td_ycbcrsubsampling
[1];
231 case TIFFTAG_YCBCRPOSITIONING
:
232 *va_arg(ap
, uint16
*) = td
->td_ycbcrpositioning
;
234 case TIFFTAG_WHITEPOINT
:
236 static float whitepoint
[2];
238 /* TIFF 6.0 specification tells that it is no default
239 value for the WhitePoint, but AdobePhotoshop TIFF
240 Technical Note tells that it should be CIE D50. */
241 whitepoint
[0] = D50_X0
/ (D50_X0
+ D50_Y0
+ D50_Z0
);
242 whitepoint
[1] = D50_Y0
/ (D50_X0
+ D50_Y0
+ D50_Z0
);
243 *va_arg(ap
, float **) = whitepoint
;
246 case TIFFTAG_TRANSFERFUNCTION
:
247 if (!td
->td_transferfunction
[0] &&
248 !TIFFDefaultTransferFunction(td
)) {
249 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
, "No space for \"TransferFunction\" tag");
252 *va_arg(ap
, uint16
**) = td
->td_transferfunction
[0];
253 if (td
->td_samplesperpixel
- td
->td_extrasamples
> 1) {
254 *va_arg(ap
, uint16
**) = td
->td_transferfunction
[1];
255 *va_arg(ap
, uint16
**) = td
->td_transferfunction
[2];
258 case TIFFTAG_REFERENCEBLACKWHITE
:
259 if (!td
->td_refblackwhite
&& !TIFFDefaultRefBlackWhite(td
))
261 *va_arg(ap
, float **) = td
->td_refblackwhite
;
268 * Like TIFFGetField, but return any default
269 * value if the tag is not present in the directory.
272 TIFFGetFieldDefaulted(TIFF
* tif
, ttag_t tag
, ...)
278 ok
= TIFFVGetFieldDefaulted(tif
, tag
, ap
);
283 /* vim: set ts=8 sts=8 sw=8 noet: */