]> git.saurik.com Git - wxWidgets.git/blob - src/tiff/tif_dirinfo.c
Copy about.htm
[wxWidgets.git] / src / tiff / tif_dirinfo.c
1 /* $Header$ */
2
3 /*
4 * Copyright (c) 1988-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 * Core Directory Tag Support.
31 */
32 #include "tiffiop.h"
33 #include <stdlib.h>
34 #include <assert.h>
35 #include <stdio.h>
36
37 /*
38 * NB: NB: THIS ARRAY IS ASSUMED TO BE SORTED BY TAG.
39 * If a tag can have both LONG and SHORT types
40 * then the LONG must be placed before the SHORT for
41 * writing to work properly.
42 *
43 * NOTE: The second field (field_readcount) and third field (field_writecount)
44 * sometimes use the values TIFF_VARIABLE (-1), TIFF_VARIABLE2 (-3)
45 * and TIFFTAG_SPP (-2). The macros should be used but would throw off
46 * the formatting of the code, so please interprete the -1, -2 and -3
47 * values accordingly.
48 */
49 #ifndef VMS
50 static
51 #endif
52 const TIFFFieldInfo tiffFieldInfo[] = {
53 { TIFFTAG_SUBFILETYPE, 1, 1, TIFF_LONG, FIELD_SUBFILETYPE,
54 TRUE, FALSE, "SubfileType" },
55 /* XXX SHORT for compatibility w/ old versions of the library */
56 { TIFFTAG_SUBFILETYPE, 1, 1, TIFF_SHORT, FIELD_SUBFILETYPE,
57 TRUE, FALSE, "SubfileType" },
58 { TIFFTAG_OSUBFILETYPE, 1, 1, TIFF_SHORT, FIELD_SUBFILETYPE,
59 TRUE, FALSE, "OldSubfileType" },
60 { TIFFTAG_IMAGEWIDTH, 1, 1, TIFF_LONG, FIELD_IMAGEDIMENSIONS,
61 FALSE, FALSE, "ImageWidth" },
62 { TIFFTAG_IMAGEWIDTH, 1, 1, TIFF_SHORT, FIELD_IMAGEDIMENSIONS,
63 FALSE, FALSE, "ImageWidth" },
64 { TIFFTAG_IMAGELENGTH, 1, 1, TIFF_LONG, FIELD_IMAGEDIMENSIONS,
65 TRUE, FALSE, "ImageLength" },
66 { TIFFTAG_IMAGELENGTH, 1, 1, TIFF_SHORT, FIELD_IMAGEDIMENSIONS,
67 TRUE, FALSE, "ImageLength" },
68 /* XXX LONG for compatibility with some broken TIFF writers */
69 { TIFFTAG_BITSPERSAMPLE, -1,-1, TIFF_LONG, FIELD_BITSPERSAMPLE,
70 FALSE, FALSE, "BitsPerSample" },
71 { TIFFTAG_BITSPERSAMPLE, -1,-1, TIFF_SHORT, FIELD_BITSPERSAMPLE,
72 FALSE, FALSE, "BitsPerSample" },
73 /* XXX LONG for compatibility with some broken TIFF writers */
74 { TIFFTAG_COMPRESSION, -1, 1, TIFF_LONG, FIELD_COMPRESSION,
75 FALSE, FALSE, "Compression" },
76 { TIFFTAG_COMPRESSION, -1, 1, TIFF_SHORT, FIELD_COMPRESSION,
77 FALSE, FALSE, "Compression" },
78 /* XXX LONG for compatibility with some broken TIFF writers */
79 { TIFFTAG_PHOTOMETRIC, 1, 1, TIFF_LONG, FIELD_PHOTOMETRIC,
80 FALSE, FALSE, "PhotometricInterpretation" },
81 { TIFFTAG_PHOTOMETRIC, 1, 1, TIFF_SHORT, FIELD_PHOTOMETRIC,
82 FALSE, FALSE, "PhotometricInterpretation" },
83 { TIFFTAG_THRESHHOLDING, 1, 1, TIFF_SHORT, FIELD_THRESHHOLDING,
84 TRUE, FALSE, "Threshholding" },
85 { TIFFTAG_CELLWIDTH, 1, 1, TIFF_SHORT, FIELD_IGNORE,
86 TRUE, FALSE, "CellWidth" },
87 { TIFFTAG_CELLLENGTH, 1, 1, TIFF_SHORT, FIELD_IGNORE,
88 TRUE, FALSE, "CellLength" },
89 { TIFFTAG_FILLORDER, 1, 1, TIFF_SHORT, FIELD_FILLORDER,
90 FALSE, FALSE, "FillOrder" },
91 { TIFFTAG_DOCUMENTNAME, -1,-1, TIFF_ASCII, FIELD_DOCUMENTNAME,
92 TRUE, FALSE, "DocumentName" },
93 { TIFFTAG_IMAGEDESCRIPTION, -1,-1, TIFF_ASCII, FIELD_IMAGEDESCRIPTION,
94 TRUE, FALSE, "ImageDescription" },
95 { TIFFTAG_MAKE, -1,-1, TIFF_ASCII, FIELD_MAKE,
96 TRUE, FALSE, "Make" },
97 { TIFFTAG_MODEL, -1,-1, TIFF_ASCII, FIELD_MODEL,
98 TRUE, FALSE, "Model" },
99 { TIFFTAG_STRIPOFFSETS, -1,-1, TIFF_LONG, FIELD_STRIPOFFSETS,
100 FALSE, FALSE, "StripOffsets" },
101 { TIFFTAG_STRIPOFFSETS, -1,-1, TIFF_SHORT, FIELD_STRIPOFFSETS,
102 FALSE, FALSE, "StripOffsets" },
103 { TIFFTAG_ORIENTATION, 1, 1, TIFF_SHORT, FIELD_ORIENTATION,
104 FALSE, FALSE, "Orientation" },
105 { TIFFTAG_SAMPLESPERPIXEL, 1, 1, TIFF_SHORT, FIELD_SAMPLESPERPIXEL,
106 FALSE, FALSE, "SamplesPerPixel" },
107 { TIFFTAG_ROWSPERSTRIP, 1, 1, TIFF_LONG, FIELD_ROWSPERSTRIP,
108 FALSE, FALSE, "RowsPerStrip" },
109 { TIFFTAG_ROWSPERSTRIP, 1, 1, TIFF_SHORT, FIELD_ROWSPERSTRIP,
110 FALSE, FALSE, "RowsPerStrip" },
111 { TIFFTAG_STRIPBYTECOUNTS, -1,-1, TIFF_LONG, FIELD_STRIPBYTECOUNTS,
112 FALSE, FALSE, "StripByteCounts" },
113 { TIFFTAG_STRIPBYTECOUNTS, -1,-1, TIFF_SHORT, FIELD_STRIPBYTECOUNTS,
114 FALSE, FALSE, "StripByteCounts" },
115 { TIFFTAG_MINSAMPLEVALUE, -2,-1, TIFF_SHORT, FIELD_MINSAMPLEVALUE,
116 TRUE, FALSE, "MinSampleValue" },
117 { TIFFTAG_MAXSAMPLEVALUE, -2,-1, TIFF_SHORT, FIELD_MAXSAMPLEVALUE,
118 TRUE, FALSE, "MaxSampleValue" },
119 { TIFFTAG_XRESOLUTION, 1, 1, TIFF_RATIONAL, FIELD_RESOLUTION,
120 FALSE, FALSE, "XResolution" },
121 { TIFFTAG_YRESOLUTION, 1, 1, TIFF_RATIONAL, FIELD_RESOLUTION,
122 FALSE, FALSE, "YResolution" },
123 { TIFFTAG_PLANARCONFIG, 1, 1, TIFF_SHORT, FIELD_PLANARCONFIG,
124 FALSE, FALSE, "PlanarConfiguration" },
125 { TIFFTAG_PAGENAME, -1,-1, TIFF_ASCII, FIELD_PAGENAME,
126 TRUE, FALSE, "PageName" },
127 { TIFFTAG_XPOSITION, 1, 1, TIFF_RATIONAL, FIELD_POSITION,
128 TRUE, FALSE, "XPosition" },
129 { TIFFTAG_YPOSITION, 1, 1, TIFF_RATIONAL, FIELD_POSITION,
130 TRUE, FALSE, "YPosition" },
131 { TIFFTAG_FREEOFFSETS, -1,-1, TIFF_LONG, FIELD_IGNORE,
132 FALSE, FALSE, "FreeOffsets" },
133 { TIFFTAG_FREEBYTECOUNTS, -1,-1, TIFF_LONG, FIELD_IGNORE,
134 FALSE, FALSE, "FreeByteCounts" },
135 { TIFFTAG_GRAYRESPONSEUNIT, 1, 1, TIFF_SHORT, FIELD_IGNORE,
136 TRUE, FALSE, "GrayResponseUnit" },
137 { TIFFTAG_GRAYRESPONSECURVE,-1,-1, TIFF_SHORT, FIELD_IGNORE,
138 TRUE, FALSE, "GrayResponseCurve" },
139 { TIFFTAG_RESOLUTIONUNIT, 1, 1, TIFF_SHORT, FIELD_RESOLUTIONUNIT,
140 FALSE, FALSE, "ResolutionUnit" },
141 { TIFFTAG_PAGENUMBER, 2, 2, TIFF_SHORT, FIELD_PAGENUMBER,
142 TRUE, FALSE, "PageNumber" },
143 { TIFFTAG_COLORRESPONSEUNIT, 1, 1, TIFF_SHORT, FIELD_IGNORE,
144 TRUE, FALSE, "ColorResponseUnit" },
145 { TIFFTAG_TRANSFERFUNCTION, -1,-1, TIFF_SHORT, FIELD_TRANSFERFUNCTION,
146 TRUE, FALSE, "TransferFunction" },
147 { TIFFTAG_SOFTWARE, -1,-1, TIFF_ASCII, FIELD_CUSTOM,
148 TRUE, FALSE, "Software" },
149 { TIFFTAG_DATETIME, 20,20, TIFF_ASCII, FIELD_DATETIME,
150 TRUE, FALSE, "DateTime" },
151 { TIFFTAG_ARTIST, -1,-1, TIFF_ASCII, FIELD_ARTIST,
152 TRUE, FALSE, "Artist" },
153 { TIFFTAG_HOSTCOMPUTER, -1,-1, TIFF_ASCII, FIELD_HOSTCOMPUTER,
154 TRUE, FALSE, "HostComputer" },
155 { TIFFTAG_WHITEPOINT, 2, 2, TIFF_RATIONAL,FIELD_WHITEPOINT,
156 TRUE, FALSE, "WhitePoint" },
157 { TIFFTAG_PRIMARYCHROMATICITIES,6,6,TIFF_RATIONAL,FIELD_PRIMARYCHROMAS,
158 TRUE, FALSE, "PrimaryChromaticities" },
159 { TIFFTAG_COLORMAP, -1,-1, TIFF_SHORT, FIELD_COLORMAP,
160 TRUE, FALSE, "ColorMap" },
161 { TIFFTAG_HALFTONEHINTS, 2, 2, TIFF_SHORT, FIELD_HALFTONEHINTS,
162 TRUE, FALSE, "HalftoneHints" },
163 { TIFFTAG_TILEWIDTH, 1, 1, TIFF_LONG, FIELD_TILEDIMENSIONS,
164 FALSE, FALSE, "TileWidth" },
165 { TIFFTAG_TILEWIDTH, 1, 1, TIFF_SHORT, FIELD_TILEDIMENSIONS,
166 FALSE, FALSE, "TileWidth" },
167 { TIFFTAG_TILELENGTH, 1, 1, TIFF_LONG, FIELD_TILEDIMENSIONS,
168 FALSE, FALSE, "TileLength" },
169 { TIFFTAG_TILELENGTH, 1, 1, TIFF_SHORT, FIELD_TILEDIMENSIONS,
170 FALSE, FALSE, "TileLength" },
171 { TIFFTAG_TILEOFFSETS, -1, 1, TIFF_LONG, FIELD_STRIPOFFSETS,
172 FALSE, FALSE, "TileOffsets" },
173 { TIFFTAG_TILEBYTECOUNTS, -1, 1, TIFF_LONG, FIELD_STRIPBYTECOUNTS,
174 FALSE, FALSE, "TileByteCounts" },
175 { TIFFTAG_TILEBYTECOUNTS, -1, 1, TIFF_SHORT, FIELD_STRIPBYTECOUNTS,
176 FALSE, FALSE, "TileByteCounts" },
177 { TIFFTAG_SUBIFD, -1,-1, TIFF_LONG, FIELD_SUBIFD,
178 TRUE, TRUE, "SubIFD" },
179 { TIFFTAG_INKSET, 1, 1, TIFF_SHORT, FIELD_INKSET,
180 FALSE, FALSE, "InkSet" },
181 { TIFFTAG_INKNAMES, -1,-1, TIFF_ASCII, FIELD_INKNAMES,
182 TRUE, TRUE, "InkNames" },
183 { TIFFTAG_NUMBEROFINKS, 1, 1, TIFF_SHORT, FIELD_NUMBEROFINKS,
184 TRUE, FALSE, "NumberOfInks" },
185 { TIFFTAG_DOTRANGE, 2, 2, TIFF_SHORT, FIELD_DOTRANGE,
186 FALSE, FALSE, "DotRange" },
187 { TIFFTAG_DOTRANGE, 2, 2, TIFF_BYTE, FIELD_DOTRANGE,
188 FALSE, FALSE, "DotRange" },
189 { TIFFTAG_TARGETPRINTER, -1,-1, TIFF_ASCII, FIELD_TARGETPRINTER,
190 TRUE, FALSE, "TargetPrinter" },
191 { TIFFTAG_EXTRASAMPLES, -1,-1, TIFF_SHORT, FIELD_EXTRASAMPLES,
192 FALSE, FALSE, "ExtraSamples" },
193 /* XXX for bogus Adobe Photoshop v2.5 files */
194 { TIFFTAG_EXTRASAMPLES, -1,-1, TIFF_BYTE, FIELD_EXTRASAMPLES,
195 FALSE, FALSE, "ExtraSamples" },
196 { TIFFTAG_SAMPLEFORMAT, -1,-1, TIFF_SHORT, FIELD_SAMPLEFORMAT,
197 FALSE, FALSE, "SampleFormat" },
198 { TIFFTAG_SMINSAMPLEVALUE, -2,-1, TIFF_ANY, FIELD_SMINSAMPLEVALUE,
199 TRUE, FALSE, "SMinSampleValue" },
200 { TIFFTAG_SMAXSAMPLEVALUE, -2,-1, TIFF_ANY, FIELD_SMAXSAMPLEVALUE,
201 TRUE, FALSE, "SMaxSampleValue" },
202 { TIFFTAG_YCBCRCOEFFICIENTS, 3, 3, TIFF_RATIONAL, FIELD_YCBCRCOEFFICIENTS,
203 FALSE, FALSE, "YCbCrCoefficients" },
204 { TIFFTAG_YCBCRSUBSAMPLING, 2, 2, TIFF_SHORT, FIELD_YCBCRSUBSAMPLING,
205 FALSE, FALSE, "YCbCrSubsampling" },
206 { TIFFTAG_YCBCRPOSITIONING, 1, 1, TIFF_SHORT, FIELD_YCBCRPOSITIONING,
207 FALSE, FALSE, "YCbCrPositioning" },
208 { TIFFTAG_REFERENCEBLACKWHITE,6,6,TIFF_RATIONAL, FIELD_REFBLACKWHITE,
209 TRUE, FALSE, "ReferenceBlackWhite" },
210 /* XXX temporarily accept LONG for backwards compatibility */
211 { TIFFTAG_REFERENCEBLACKWHITE,6,6,TIFF_LONG, FIELD_REFBLACKWHITE,
212 TRUE, FALSE, "ReferenceBlackWhite" },
213 { TIFFTAG_XMLPACKET, -1,-3, TIFF_UNDEFINED, FIELD_XMLPACKET,
214 FALSE, TRUE, "XMLPacket" },
215 /* begin SGI tags */
216 { TIFFTAG_MATTEING, 1, 1, TIFF_SHORT, FIELD_EXTRASAMPLES,
217 FALSE, FALSE, "Matteing" },
218 { TIFFTAG_DATATYPE, -2,-1, TIFF_SHORT, FIELD_SAMPLEFORMAT,
219 FALSE, FALSE, "DataType" },
220 { TIFFTAG_IMAGEDEPTH, 1, 1, TIFF_LONG, FIELD_IMAGEDEPTH,
221 FALSE, FALSE, "ImageDepth" },
222 { TIFFTAG_IMAGEDEPTH, 1, 1, TIFF_SHORT, FIELD_IMAGEDEPTH,
223 FALSE, FALSE, "ImageDepth" },
224 { TIFFTAG_TILEDEPTH, 1, 1, TIFF_LONG, FIELD_TILEDEPTH,
225 FALSE, FALSE, "TileDepth" },
226 { TIFFTAG_TILEDEPTH, 1, 1, TIFF_SHORT, FIELD_TILEDEPTH,
227 FALSE, FALSE, "TileDepth" },
228 /* end SGI tags */
229 /* begin Pixar tags */
230 { TIFFTAG_PIXAR_IMAGEFULLWIDTH, 1, 1, TIFF_LONG, FIELD_IMAGEFULLWIDTH,
231 TRUE, FALSE, "ImageFullWidth" },
232 { TIFFTAG_PIXAR_IMAGEFULLLENGTH, 1, 1, TIFF_LONG, FIELD_IMAGEFULLLENGTH,
233 TRUE, FALSE, "ImageFullLength" },
234 { TIFFTAG_PIXAR_TEXTUREFORMAT, -1,-1, TIFF_ASCII, FIELD_TEXTUREFORMAT,
235 TRUE, FALSE, "TextureFormat" },
236 { TIFFTAG_PIXAR_WRAPMODES, -1,-1, TIFF_ASCII, FIELD_WRAPMODES,
237 TRUE, FALSE, "TextureWrapModes" },
238 { TIFFTAG_PIXAR_FOVCOT, 1, 1, TIFF_FLOAT, FIELD_FOVCOT,
239 TRUE, FALSE, "FieldOfViewCotan" },
240 { TIFFTAG_PIXAR_MATRIX_WORLDTOSCREEN, 16,16, TIFF_FLOAT,
241 FIELD_MATRIX_WORLDTOSCREEN, TRUE, FALSE, "MatrixWorldToScreen" },
242 { TIFFTAG_PIXAR_MATRIX_WORLDTOCAMERA, 16,16, TIFF_FLOAT,
243 FIELD_MATRIX_WORLDTOCAMERA, TRUE, FALSE, "MatrixWorldToCamera" },
244 { TIFFTAG_COPYRIGHT, -1,-1, TIFF_ASCII, FIELD_COPYRIGHT,
245 TRUE, FALSE, "Copyright" },
246 /* end Pixar tags */
247 #ifdef IPTC_SUPPORT
248 #ifdef PHOTOSHOP_SUPPORT
249 { TIFFTAG_RICHTIFFIPTC, -1,-1, TIFF_LONG, FIELD_RICHTIFFIPTC,
250 FALSE, TRUE, "RichTIFFIPTC" },
251 #else
252 { TIFFTAG_RICHTIFFIPTC, -1,-3, TIFF_UNDEFINED, FIELD_RICHTIFFIPTC,
253 FALSE, TRUE, "RichTIFFIPTC" },
254 #endif
255 #endif
256 { TIFFTAG_PHOTOSHOP, -1,-3, TIFF_BYTE, FIELD_PHOTOSHOP,
257 FALSE, TRUE, "Photoshop" },
258 { TIFFTAG_ICCPROFILE, -1,-3, TIFF_UNDEFINED, FIELD_ICCPROFILE,
259 FALSE, TRUE, "ICC Profile" },
260 { TIFFTAG_STONITS, 1, 1, TIFF_DOUBLE, FIELD_STONITS,
261 FALSE, FALSE, "StoNits" },
262 };
263 #define N(a) (sizeof (a) / sizeof (a[0]))
264
265 void
266 _TIFFSetupFieldInfo(TIFF* tif)
267 {
268 if (tif->tif_fieldinfo) {
269 int i;
270
271 for (i = 0; i < tif->tif_nfields; i++)
272 {
273 TIFFFieldInfo *fld = tif->tif_fieldinfo[i];
274 if (fld->field_bit == FIELD_CUSTOM &&
275 strncmp("Tag ", fld->field_name, 4) == 0)
276 {
277 _TIFFfree(fld->field_name);
278 _TIFFfree(fld);
279 }
280 }
281
282 _TIFFfree(tif->tif_fieldinfo);
283 tif->tif_nfields = 0;
284 }
285 _TIFFMergeFieldInfo(tif, tiffFieldInfo, N(tiffFieldInfo));
286 }
287
288 static int
289 tagCompare(const void* a, const void* b)
290 {
291 const TIFFFieldInfo* ta = *(const TIFFFieldInfo**) a;
292 const TIFFFieldInfo* tb = *(const TIFFFieldInfo**) b;
293 /* NB: be careful of return values for 16-bit platforms */
294 if (ta->field_tag != tb->field_tag)
295 return (ta->field_tag < tb->field_tag ? -1 : 1);
296 else
297 return ((int)tb->field_type - (int)ta->field_type);
298 }
299
300 void
301 _TIFFMergeFieldInfo(TIFF* tif, const TIFFFieldInfo info[], int n)
302 {
303 TIFFFieldInfo** tp;
304 int i;
305
306 if (tif->tif_nfields > 0) {
307 tif->tif_fieldinfo = (TIFFFieldInfo**)
308 _TIFFrealloc(tif->tif_fieldinfo,
309 (tif->tif_nfields+n) * sizeof (TIFFFieldInfo*));
310 } else {
311 tif->tif_fieldinfo = (TIFFFieldInfo**)
312 _TIFFmalloc(n * sizeof (TIFFFieldInfo*));
313 }
314 assert(tif->tif_fieldinfo != NULL);
315 tp = &tif->tif_fieldinfo[tif->tif_nfields];
316 for (i = 0; i < n; i++)
317 tp[i] = (TIFFFieldInfo*) &info[i]; /* XXX */
318
319 /* Sort the field info by tag number */
320 qsort(tif->tif_fieldinfo, (size_t) (tif->tif_nfields += n),
321 sizeof (TIFFFieldInfo*), tagCompare);
322 }
323
324 void
325 _TIFFPrintFieldInfo(TIFF* tif, FILE* fd)
326 {
327 int i;
328
329 fprintf(fd, "%s: \n", tif->tif_name);
330 for (i = 0; i < tif->tif_nfields; i++) {
331 const TIFFFieldInfo* fip = tif->tif_fieldinfo[i];
332 fprintf(fd, "field[%2d] %5lu, %2d, %2d, %d, %2d, %5s, %5s, %s\n"
333 , i
334 , (unsigned long) fip->field_tag
335 , fip->field_readcount, fip->field_writecount
336 , fip->field_type
337 , fip->field_bit
338 , fip->field_oktochange ? "TRUE" : "FALSE"
339 , fip->field_passcount ? "TRUE" : "FALSE"
340 , fip->field_name
341 );
342 }
343 }
344
345 /*
346 * Return size of TIFFDataType in bytes
347 */
348 int
349 TIFFDataWidth(TIFFDataType type)
350 {
351 switch(type)
352 {
353 case 0: /* nothing */
354 case 1: /* TIFF_BYTE */
355 case 2: /* TIFF_ASCII */
356 case 6: /* TIFF_SBYTE */
357 case 7: /* TIFF_UNDEFINED */
358 return 1;
359 case 3: /* TIFF_SHORT */
360 case 8: /* TIFF_SSHORT */
361 return 2;
362 case 4: /* TIFF_LONG */
363 case 9: /* TIFF_SLONG */
364 case 11: /* TIFF_FLOAT */
365 case 13: /* TIFF_IFD */
366 return 4;
367 case 5: /* TIFF_RATIONAL */
368 case 10: /* TIFF_SRATIONAL */
369 case 12: /* TIFF_DOUBLE */
370 return 8;
371 default:
372 return 0; /* will return 0 for unknown types */
373 }
374 }
375
376 /*
377 * Return nearest TIFFDataType to the sample type of an image.
378 */
379 TIFFDataType
380 _TIFFSampleToTagType(TIFF* tif)
381 {
382 uint32 bps = TIFFhowmany8(tif->tif_dir.td_bitspersample);
383
384 switch (tif->tif_dir.td_sampleformat) {
385 case SAMPLEFORMAT_IEEEFP:
386 return (bps == 4 ? TIFF_FLOAT : TIFF_DOUBLE);
387 case SAMPLEFORMAT_INT:
388 return (bps <= 1 ? TIFF_SBYTE :
389 bps <= 2 ? TIFF_SSHORT : TIFF_SLONG);
390 case SAMPLEFORMAT_UINT:
391 return (bps <= 1 ? TIFF_BYTE :
392 bps <= 2 ? TIFF_SHORT : TIFF_LONG);
393 case SAMPLEFORMAT_VOID:
394 return (TIFF_UNDEFINED);
395 }
396 /*NOTREACHED*/
397 return (TIFF_UNDEFINED);
398 }
399
400 const TIFFFieldInfo*
401 _TIFFFindFieldInfo(TIFF* tif, ttag_t tag, TIFFDataType dt)
402 {
403 static const TIFFFieldInfo *last = NULL;
404 int i, n;
405
406 if (last && last->field_tag == tag &&
407 (dt == TIFF_ANY || dt == last->field_type))
408 return (last);
409 /* NB: if table gets big, use sorted search (e.g. binary search) */
410 if(dt != TIFF_ANY) {
411 TIFFFieldInfo key = {0, 0, 0, 0, 0, 0, 0, 0};
412 key.field_tag = tag;
413 key.field_type = dt;
414 return((const TIFFFieldInfo *) bsearch(&key,
415 tif->tif_fieldinfo,
416 tif->tif_nfields,
417 sizeof(TIFFFieldInfo),
418 tagCompare));
419 } else for (i = 0, n = tif->tif_nfields; i < n; i++) {
420 const TIFFFieldInfo* fip = tif->tif_fieldinfo[i];
421 if (fip->field_tag == tag &&
422 (dt == TIFF_ANY || fip->field_type == dt))
423 return (last = fip);
424 }
425 return ((const TIFFFieldInfo *)0);
426 }
427
428 const TIFFFieldInfo*
429 _TIFFFieldWithTag(TIFF* tif, ttag_t tag)
430 {
431 const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
432 if (!fip) {
433 TIFFError("TIFFFieldWithTag",
434 "Internal error, unknown tag 0x%x", (u_int) tag);
435 assert(fip != NULL);
436 /*NOTREACHED*/
437 }
438 return (fip);
439 }
440
441 const TIFFFieldInfo*
442 _TIFFFindOrRegisterFieldInfo( TIFF *tif, ttag_t tag, TIFFDataType dt )
443
444 {
445 const TIFFFieldInfo *fld;
446
447 fld = _TIFFFindFieldInfo( tif, tag, dt );
448 if( fld == NULL )
449 {
450 fld = _TIFFCreateAnonFieldInfo( tif, tag, dt );
451 _TIFFMergeFieldInfo( tif, fld, 1 );
452 }
453
454 return fld;
455 }
456
457 TIFFFieldInfo*
458 _TIFFCreateAnonFieldInfo(TIFF *tif, ttag_t tag, TIFFDataType field_type)
459 {
460 TIFFFieldInfo *fld;
461
462 fld = (TIFFFieldInfo *) _TIFFmalloc(sizeof (TIFFFieldInfo));
463 if (fld == NULL)
464 return NULL;
465 _TIFFmemset( fld, 0, sizeof(TIFFFieldInfo) );
466
467 fld->field_tag = tag;
468 fld->field_readcount = TIFF_VARIABLE;
469 fld->field_writecount = TIFF_VARIABLE;
470 fld->field_type = field_type;
471 fld->field_bit = FIELD_CUSTOM;
472 fld->field_oktochange = TRUE;
473 fld->field_passcount = TRUE;
474 fld->field_name = (char *) _TIFFmalloc(32);
475 if (fld->field_name == NULL) {
476 _TIFFfree(fld);
477 return NULL;
478 }
479
480 /* note that this name is a special sign to TIFFClose() and
481 * _TIFFSetupFieldInfo() to free the field
482 */
483 sprintf(fld->field_name, "Tag %d", (int) tag);
484
485 return fld;
486 }