| Bsoft 1.9 Bernard's software package

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

tif_aux.c
Go to the documentation of this file.
1 /* $Id: tif_aux.c,v 1.20.2.3 2010-06-09 21:15:27 bfriesen Exp $ */
2
3 /*
4 * Copyright (c) 1991-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6 *
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.
14 *
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.
18 *
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
24 * OF THIS SOFTWARE.
25 */
26
27 /*
28 * TIFF Library.
29 *
30 * Auxiliary Support Routines.
31 */
32 #include "tiffiop.h"
33 #include "tif_predict.h"
34 #include <math.h>
35
36 tdata_t
37 _TIFFCheckRealloc(TIFF* tif, tdata_t buffer,
38 size_t nmemb, size_t elem_size, const char* what)
39 {
40 tdata_t cp = NULL;
41 tsize_t bytes = nmemb * elem_size;
42
43 /*
44 * XXX: Check for integer overflow.
45 */
46 if (nmemb && elem_size && bytes / elem_size == nmemb)
47 cp = _TIFFrealloc(buffer, bytes);
48
49 if (cp == NULL)
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);
54
55 return cp;
56 }
57
58 tdata_t
59 _TIFFCheckMalloc(TIFF* tif, size_t nmemb, size_t elem_size, const char* what)
60 {
61 return _TIFFCheckRealloc(tif, NULL, nmemb, elem_size, what);
62 }
63
64 static int
65 TIFFDefaultTransferFunction(TIFFDirectory* td)
66 {
67 uint16 **tf = td->td_transferfunction;
68 tsize_t i, n, nbytes;
69
70 tf[0] = tf[1] = tf[2] = 0;
71 if (td->td_bitspersample >= sizeof(tsize_t) * 8 - 2)
72 return 0;
73
74 n = 1<<td->td_bitspersample;
75 nbytes = n * sizeof (uint16);
76 if (!(tf[0] = (uint16 *)_TIFFmalloc(nbytes)))
77 return 0;
78 tf[0][0] = 0;
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);
82 }
83
84 if (td->td_samplesperpixel - td->td_extrasamples > 1) {
85 if (!(tf[1] = (uint16 *)_TIFFmalloc(nbytes)))
86 goto bad;
87 _TIFFmemcpy(tf[1], tf[0], nbytes);
88 if (!(tf[2] = (uint16 *)_TIFFmalloc(nbytes)))
89 goto bad;
90 _TIFFmemcpy(tf[2], tf[0], nbytes);
91 }
92 return 1;
93
94 bad:
95 if (tf[0])
96 _TIFFfree(tf[0]);
97 if (tf[1])
98 _TIFFfree(tf[1]);
99 if (tf[2])
100 _TIFFfree(tf[2]);
101 tf[0] = tf[1] = tf[2] = 0;
102 return 0;
103 }
104
105 static int
106 TIFFDefaultRefBlackWhite(TIFFDirectory* td)
107 {
108 int i;
109
110 if (!(td->td_refblackwhite = (float *)_TIFFmalloc(6*sizeof (float))))
111 return 0;
112 if (td->td_photometric == PHOTOMETRIC_YCBCR) {
113 /*
114 * YCbCr (Class Y) images must have the ReferenceBlackWhite
115 * tag set. Fix the broken images, which lacks that tag.
116 */
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;
121 } else {
122 /*
123 * Assume RGB (Class R)
124 */
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);
129 }
130 }
131 return 1;
132 }
133
134 /*
135 * Like TIFFGetField, but return any default
136 * value if the tag is not present in the directory.
137 *
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.
141 */
142 int
143 TIFFVGetFieldDefaulted(TIFF* tif, ttag_t tag, va_list ap)
144 {
145 TIFFDirectory *td = &tif->tif_dir;
146
147 if (TIFFVGetField(tif, tag, ap))
148 return (1);
149 switch (tag) {
150 case TIFFTAG_SUBFILETYPE:
151 *va_arg(ap, uint32 *) = td->td_subfiletype;
152 return (1);
153 case TIFFTAG_BITSPERSAMPLE:
154 *va_arg(ap, uint16 *) = td->td_bitspersample;
155 return (1);
156 case TIFFTAG_THRESHHOLDING:
157 *va_arg(ap, uint16 *) = td->td_threshholding;
158 return (1);
159 case TIFFTAG_FILLORDER:
160 *va_arg(ap, uint16 *) = td->td_fillorder;
161 return (1);
162 case TIFFTAG_ORIENTATION:
163 *va_arg(ap, uint16 *) = td->td_orientation;
164 return (1);
165 case TIFFTAG_SAMPLESPERPIXEL:
166 *va_arg(ap, uint16 *) = td->td_samplesperpixel;
167 return (1);
168 case TIFFTAG_ROWSPERSTRIP:
169 *va_arg(ap, uint32 *) = td->td_rowsperstrip;
170 return (1);
171 case TIFFTAG_MINSAMPLEVALUE:
172 *va_arg(ap, uint16 *) = td->td_minsamplevalue;
173 return (1);
174 case TIFFTAG_MAXSAMPLEVALUE:
175 *va_arg(ap, uint16 *) = td->td_maxsamplevalue;
176 return (1);
177 case TIFFTAG_PLANARCONFIG:
178 *va_arg(ap, uint16 *) = td->td_planarconfig;
179 return (1);
180 case TIFFTAG_RESOLUTIONUNIT:
181 *va_arg(ap, uint16 *) = td->td_resolutionunit;
182 return (1);
183 case TIFFTAG_PREDICTOR:
184 {
185 TIFFPredictorState* sp = (TIFFPredictorState*) tif->tif_data;
186 *va_arg(ap, uint16*) = (uint16) sp->predictor;
187 return 1;
188 }
189 case TIFFTAG_DOTRANGE:
190 *va_arg(ap, uint16 *) = 0;
191 *va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1;
192 return (1);
193 case TIFFTAG_INKSET:
194 *va_arg(ap, uint16 *) = INKSET_CMYK;
195 return 1;
196 case TIFFTAG_NUMBEROFINKS:
197 *va_arg(ap, uint16 *) = 4;
198 return (1);
199 case TIFFTAG_EXTRASAMPLES:
200 *va_arg(ap, uint16 *) = td->td_extrasamples;
201 *va_arg(ap, uint16 **) = td->td_sampleinfo;
202 return (1);
203 case TIFFTAG_MATTEING:
204 *va_arg(ap, uint16 *) =
205 (td->td_extrasamples == 1 &&
206 td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
207 return (1);
208 case TIFFTAG_TILEDEPTH:
209 *va_arg(ap, uint32 *) = td->td_tiledepth;
210 return (1);
211 case TIFFTAG_DATATYPE:
212 *va_arg(ap, uint16 *) = td->td_sampleformat-1;
213 return (1);
214 case TIFFTAG_SAMPLEFORMAT:
215 *va_arg(ap, uint16 *) = td->td_sampleformat;
216 return(1);
217 case TIFFTAG_IMAGEDEPTH:
218 *va_arg(ap, uint32 *) = td->td_imagedepth;
219 return (1);
220 case TIFFTAG_YCBCRCOEFFICIENTS:
221 {
222 /* defaults are from CCIR Recommendation 601-1 */
223 static float ycbcrcoeffs[] = { 0.299f, 0.587f, 0.114f };
224 *va_arg(ap, float **) = ycbcrcoeffs;
225 return 1;
226 }
227 case TIFFTAG_YCBCRSUBSAMPLING:
228 *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0];
229 *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1];
230 return (1);
231 case TIFFTAG_YCBCRPOSITIONING:
232 *va_arg(ap, uint16 *) = td->td_ycbcrpositioning;
233 return (1);
234 case TIFFTAG_WHITEPOINT:
235 {
236 static float whitepoint[2];
237
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;
244 return 1;
245 }
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");
250 return (0);
251 }
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];
256 }
257 return (1);
258 case TIFFTAG_REFERENCEBLACKWHITE:
259 if (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(td))
260 return (0);
261 *va_arg(ap, float **) = td->td_refblackwhite;
262 return (1);
263 }
264 return 0;
265 }
266
267 /*
268 * Like TIFFGetField, but return any default
269 * value if the tag is not present in the directory.
270 */
271 int
272 TIFFGetFieldDefaulted(TIFF* tif, ttag_t tag, ...)
273 {
274 int ok;
275 va_list ap;
276
277 va_start(ap, tag);
278 ok = TIFFVGetFieldDefaulted(tif, tag, ap);
279 va_end(ap);
280 return (ok);
281 }
282
283 /* vim: set ts=8 sts=8 sw=8 noet: */
284 /*
285 * Local Variables:
286 * mode: c
287 * c-basic-offset: 8
288 * fill-column: 78
289 * End:
290 */
D50_Z0
#define D50_Z0
Definition: tiffio.h:125
tsize_t
int32 tsize_t
Definition: tiffio.h:67
TIFFTAG_SUBFILETYPE
#define TIFFTAG_SUBFILETYPE
Definition: tiff.h:157
ttag_t
uint32 ttag_t
Definition: tiffio.h:61
TIFFDirectory::td_threshholding
uint16 td_threshholding
Definition: tif_dir.h:48
tdata_t
void * tdata_t
Definition: tiffio.h:68
TIFFDirectory::td_refblackwhite
float * td_refblackwhite
Definition: tif_dir.h:78
TIFFDirectory::td_imagedepth
uint32 td_imagedepth
Definition: tif_dir.h:41
PHOTOMETRIC_YCBCR
#define PHOTOMETRIC_YCBCR
Definition: tiff.h:206
TIFFTAG_BITSPERSAMPLE
#define TIFFTAG_BITSPERSAMPLE
Definition: tiff.h:167
TIFFPredictorState
Definition: tif_predict.h:38
TIFFTAG_ROWSPERSTRIP
#define TIFFTAG_ROWSPERSTRIP
Definition: tiff.h:236
TIFFDirectory::td_bitspersample
uint16 td_bitspersample
Definition: tif_dir.h:44
TIFFDirectory::td_minsamplevalue
uint16 td_minsamplevalue
Definition: tif_dir.h:53
TIFFTAG_EXTRASAMPLES
#define TIFFTAG_EXTRASAMPLES
Definition: tiff.h:307
TIFFTAG_ORIENTATION
#define TIFFTAG_ORIENTATION
Definition: tiff.h:226
TIFFTAG_DATATYPE
#define TIFFTAG_DATATYPE
Definition: tiff.h:363
TIFFTAG_MINSAMPLEVALUE
#define TIFFTAG_MINSAMPLEVALUE
Definition: tiff.h:238
TIFFTAG_FILLORDER
#define TIFFTAG_FILLORDER
Definition: tiff.h:218
D50_X0
#define D50_X0
Definition: tiffio.h:123
TIFFDirectory::td_transferfunction
uint16 * td_transferfunction[3]
Definition: tif_dir.h:79
TIFFTAG_DOTRANGE
#define TIFFTAG_DOTRANGE
Definition: tiff.h:305
TIFFTAG_THRESHHOLDING
#define TIFFTAG_THRESHHOLDING
Definition: tiff.h:212
TIFFDirectory::td_subfiletype
uint32 td_subfiletype
Definition: tif_dir.h:43
TIFFTAG_YCBCRSUBSAMPLING
#define TIFFTAG_YCBCRSUBSAMPLING
Definition: tiff.h:346
tiff::tif_clientdata
thandle_t tif_clientdata
Definition: tiffiop.h:185
TIFFTAG_REFERENCEBLACKWHITE
#define TIFFTAG_REFERENCEBLACKWHITE
Definition: tiff.h:350
tiffiop.h
TIFFTAG_PLANARCONFIG
#define TIFFTAG_PLANARCONFIG
Definition: tiff.h:242
TIFFTAG_MATTEING
#define TIFFTAG_MATTEING
Definition: tiff.h:362
tiff::tif_name
char * tif_name
Definition: tiffiop.h:103
tiff
Definition: tiffiop.h:102
TIFFTAG_YCBCRCOEFFICIENTS
#define TIFFTAG_YCBCRCOEFFICIENTS
Definition: tiff.h:345
TIFFDirectory::td_extrasamples
uint16 td_extrasamples
Definition: tif_dir.h:62
_TIFFmemcpy
void _TIFFmemcpy(tdata_t d, const tdata_t s, tsize_t c)
Definition: tif_acorn.c:485
INKSET_CMYK
#define INKSET_CMYK
Definition: tiff.h:301
TIFFTAG_YCBCRPOSITIONING
#define TIFFTAG_YCBCRPOSITIONING
Definition: tiff.h:347
TIFFDirectory::td_ycbcrsubsampling
uint16 td_ycbcrsubsampling[2]
Definition: tif_dir.h:75
TIFFDirectory::td_orientation
uint16 td_orientation
Definition: tif_dir.h:50
tif_predict.h
TIFFDirectory::td_sampleinfo
uint16 * td_sampleinfo
Definition: tif_dir.h:63
TIFFTAG_SAMPLESPERPIXEL
#define TIFFTAG_SAMPLESPERPIXEL
Definition: tiff.h:235
uint16
unsigned short uint16
Definition: tiff.h:71
TIFFDirectory::td_rowsperstrip
uint32 td_rowsperstrip
Definition: tif_dir.h:52
_TIFFrealloc
void * _TIFFrealloc(tdata_t p, tsize_t s)
Definition: tif_acorn.c:473
TIFFDirectory::td_maxsamplevalue
uint16 td_maxsamplevalue
Definition: tif_dir.h:53
TIFFGetFieldDefaulted
int TIFFGetFieldDefaulted(TIFF *tif, ttag_t tag,...)
Definition: tif_aux.c:272
TIFFDirectory
Definition: tif_dir.h:36
TIFFDirectory::td_planarconfig
uint16 td_planarconfig
Definition: tif_dir.h:57
TIFFErrorExt
void TIFFErrorExt(thandle_t fd, const char *module, const char *fmt,...)
Definition: tif_error.c:63
TIFFTAG_TRANSFERFUNCTION
#define TIFFTAG_TRANSFERFUNCTION
Definition: tiff.h:276
TIFFTAG_WHITEPOINT
#define TIFFTAG_WHITEPOINT
Definition: tiff.h:285
TIFFTAG_INKSET
#define TIFFTAG_INKSET
Definition: tiff.h:300
TIFFTAG_NUMBEROFINKS
#define TIFFTAG_NUMBEROFINKS
Definition: tiff.h:304
TIFFDirectory::td_samplesperpixel
uint16 td_samplesperpixel
Definition: tif_dir.h:51
TIFFVGetField
int TIFFVGetField(TIFF *tif, ttag_t tag, va_list ap)
Definition: tif_dir.c:969
TIFFTAG_TILEDEPTH
#define TIFFTAG_TILEDEPTH
Definition: tiff.h:365
TIFFTAG_IMAGEDEPTH
#define TIFFTAG_IMAGEDEPTH
Definition: tiff.h:364
TIFFDirectory::td_tiledepth
uint32 td_tiledepth
Definition: tif_dir.h:42
TIFFTAG_RESOLUTIONUNIT
#define TIFFTAG_RESOLUTIONUNIT
Definition: tiff.h:265
TIFFDirectory::td_ycbcrpositioning
uint16 td_ycbcrpositioning
Definition: tif_dir.h:76
tiff::tif_dir
TIFFDirectory tif_dir
Definition: tiffiop.h:133
TIFFTAG_PREDICTOR
#define TIFFTAG_PREDICTOR
Definition: tiff.h:281
_TIFFmalloc
void * _TIFFmalloc(tsize_t s)
Definition: tif_acorn.c:461
TIFFDirectory::td_sampleformat
uint16 td_sampleformat
Definition: tif_dir.h:45
_TIFFCheckMalloc
tdata_t _TIFFCheckMalloc(TIFF *tif, size_t nmemb, size_t elem_size, const char *what)
Definition: tif_aux.c:59
TIFFDirectory::td_resolutionunit
uint16 td_resolutionunit
Definition: tif_dir.h:56
_TIFFfree
void _TIFFfree(tdata_t p)
Definition: tif_acorn.c:467
D50_Y0
#define D50_Y0
Definition: tiffio.h:124
_TIFFCheckRealloc
tdata_t _TIFFCheckRealloc(TIFF *tif, tdata_t buffer, size_t nmemb, size_t elem_size, const char *what)
Definition: tif_aux.c:37
TIFFTAG_SAMPLEFORMAT
#define TIFFTAG_SAMPLEFORMAT
Definition: tiff.h:311
EXTRASAMPLE_ASSOCALPHA
#define EXTRASAMPLE_ASSOCALPHA
Definition: tiff.h:309
tiff::tif_data
tidata_t tif_data
Definition: tiffiop.h:170
TIFFPredictorState::predictor
int predictor
Definition: tif_predict.h:39
TIFFTAG_MAXSAMPLEVALUE
#define TIFFTAG_MAXSAMPLEVALUE
Definition: tiff.h:239
TIFFDirectory::td_photometric
uint16 td_photometric
Definition: tif_dir.h:47
TIFFDirectory::td_fillorder
uint16 td_fillorder
Definition: tif_dir.h:49
TIFFVGetFieldDefaulted
int TIFFVGetFieldDefaulted(TIFF *tif, ttag_t tag, va_list ap)
Definition: tif_aux.c:143