1 /* $Id: tif_dir.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
4 * Copyright (c) 1988-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 * Directory Tag Get & Set Routines.
31 * (and also some miscellaneous stuff)
36 * These are used in the backwards compatibility code...
38 #define DATATYPE_VOID 0 /* !untyped data */
39 #define DATATYPE_INT 1 /* !signed integer data */
40 #define DATATYPE_UINT 2 /* !unsigned integer data */
41 #define DATATYPE_IEEEFP 3 /* !IEEE floating point data */
44 setByteArray(void** vpp, void* vp, size_t nmemb, size_t elem_size)
47 _TIFFfree(*vpp), *vpp = 0;
49 tsize_t bytes = nmemb * elem_size;
50 if (elem_size && bytes / elem_size == nmemb)
51 *vpp = (void*) _TIFFmalloc(bytes);
53 _TIFFmemcpy(*vpp, vp, bytes);
56 void _TIFFsetByteArray(void** vpp, void* vp, long n)
57 { setByteArray(vpp, vp, n, 1); }
58 void _TIFFsetString(char** cpp, char* cp)
59 { setByteArray((void**) cpp, (void*) cp, strlen(cp)+1, 1); }
60 void _TIFFsetNString(char** cpp, char* cp, long n)
61 { setByteArray((void**) cpp, (void*) cp, n, 1); }
62 void _TIFFsetShortArray(uint16** wpp, uint16* wp, long n)
63 { setByteArray((void**) wpp, (void*) wp, n, sizeof (uint16)); }
64 void _TIFFsetLongArray(uint32** lpp, uint32* lp, long n)
65 { setByteArray((void**) lpp, (void*) lp, n, sizeof (uint32)); }
66 void _TIFFsetFloatArray(float** fpp, float* fp, long n)
67 { setByteArray((void**) fpp, (void*) fp, n, sizeof (float)); }
68 void _TIFFsetDoubleArray(double** dpp, double* dp, long n)
69 { setByteArray((void**) dpp, (void*) dp, n, sizeof (double)); }
72 * Install extra samples information.
75 setExtraSamples(TIFFDirectory* td, va_list ap, int* v)
81 if ((uint16) *v > td->td_samplesperpixel)
83 va = va_arg(ap, uint16*);
84 if (*v > 0 && va == NULL) /* typically missing param */
86 for (i = 0; i < *v; i++)
87 if (va[i] > EXTRASAMPLE_UNASSALPHA)
89 td->td_extrasamples = (uint16) *v;
90 _TIFFsetShortArray(&td->td_sampleinfo, va, td->td_extrasamples);
95 checkInkNamesString(TIFF* tif, int slen, const char* s)
97 TIFFDirectory* td = &tif->tif_dir;
98 int i = td->td_samplesperpixel;
101 const char* ep = s+slen;
104 for (; *cp != '\0'; cp++)
112 TIFFError("TIFFSetField",
113 "%s: Invalid InkNames value; expecting %d names, found %d",
115 td->td_samplesperpixel,
116 td->td_samplesperpixel-i);
121 _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
123 static const char module[] = "_TIFFVSetField";
125 TIFFDirectory* td = &tif->tif_dir;
133 case TIFFTAG_SUBFILETYPE:
134 td->td_subfiletype = va_arg(ap, uint32);
136 case TIFFTAG_IMAGEWIDTH:
137 td->td_imagewidth = va_arg(ap, uint32);
139 case TIFFTAG_IMAGELENGTH:
140 td->td_imagelength = va_arg(ap, uint32);
142 case TIFFTAG_BITSPERSAMPLE:
143 td->td_bitspersample = (uint16) va_arg(ap, int);
145 * If the data require post-decoding processing
146 * to byte-swap samples, set it up here. Note
147 * that since tags are required to be ordered,
148 * compression code can override this behaviour
149 * in the setup method if it wants to roll the
150 * post decoding work in with its normal work.
152 if (tif->tif_flags & TIFF_SWAB) {
153 if (td->td_bitspersample == 16)
154 tif->tif_postdecode = _TIFFSwab16BitData;
155 else if (td->td_bitspersample == 32)
156 tif->tif_postdecode = _TIFFSwab32BitData;
157 else if (td->td_bitspersample == 64)
158 tif->tif_postdecode = _TIFFSwab64BitData;
161 case TIFFTAG_COMPRESSION:
162 v = va_arg(ap, int) & 0xffff;
164 * If we're changing the compression scheme,
165 * the notify the previous module so that it
166 * can cleanup any state it's setup.
168 if (TIFFFieldSet(tif, FIELD_COMPRESSION)) {
169 if (td->td_compression == v)
171 (*tif->tif_cleanup)(tif);
172 tif->tif_flags &= ~TIFF_CODERSETUP;
175 * Setup new compression routine state.
177 if( (status = TIFFSetCompressionScheme(tif, v)) != 0 )
178 td->td_compression = (uint16) v;
182 case TIFFTAG_PHOTOMETRIC:
183 td->td_photometric = (uint16) va_arg(ap, int);
185 case TIFFTAG_THRESHHOLDING:
186 td->td_threshholding = (uint16) va_arg(ap, int);
188 case TIFFTAG_FILLORDER:
190 if (v != FILLORDER_LSB2MSB && v != FILLORDER_MSB2LSB)
192 td->td_fillorder = (uint16) v;
194 case TIFFTAG_DOCUMENTNAME:
195 _TIFFsetString(&td->td_documentname, va_arg(ap, char*));
198 _TIFFsetString(&td->td_artist, va_arg(ap, char*));
200 case TIFFTAG_DATETIME:
201 _TIFFsetString(&td->td_datetime, va_arg(ap, char*));
203 case TIFFTAG_HOSTCOMPUTER:
204 _TIFFsetString(&td->td_hostcomputer, va_arg(ap, char*));
206 case TIFFTAG_IMAGEDESCRIPTION:
207 _TIFFsetString(&td->td_imagedescription, va_arg(ap, char*));
210 _TIFFsetString(&td->td_make, va_arg(ap, char*));
213 _TIFFsetString(&td->td_model, va_arg(ap, char*));
215 case TIFFTAG_COPYRIGHT:
216 _TIFFsetString(&td->td_copyright, va_arg(ap, char*));
218 case TIFFTAG_ORIENTATION:
220 if (v < ORIENTATION_TOPLEFT || ORIENTATION_LEFTBOT < v) {
221 TIFFWarning(tif->tif_name,
222 "Bad value %ld for \"%s\" tag ignored",
223 v, _TIFFFieldWithTag(tif, tag)->field_name);
225 td->td_orientation = (uint16) v;
227 case TIFFTAG_SAMPLESPERPIXEL:
228 /* XXX should cross check -- e.g. if pallette, then 1 */
232 td->td_samplesperpixel = (uint16) v;
234 case TIFFTAG_ROWSPERSTRIP:
235 v32 = va_arg(ap, uint32);
238 td->td_rowsperstrip = v32;
239 if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
240 td->td_tilelength = v32;
241 td->td_tilewidth = td->td_imagewidth;
244 case TIFFTAG_MINSAMPLEVALUE:
245 td->td_minsamplevalue = (uint16) va_arg(ap, int);
247 case TIFFTAG_MAXSAMPLEVALUE:
248 td->td_maxsamplevalue = (uint16) va_arg(ap, int);
250 case TIFFTAG_SMINSAMPLEVALUE:
251 td->td_sminsamplevalue = (double) va_arg(ap, dblparam_t);
253 case TIFFTAG_SMAXSAMPLEVALUE:
254 td->td_smaxsamplevalue = (double) va_arg(ap, dblparam_t);
256 case TIFFTAG_XRESOLUTION:
257 td->td_xresolution = (float) va_arg(ap, dblparam_t);
259 case TIFFTAG_YRESOLUTION:
260 td->td_yresolution = (float) va_arg(ap, dblparam_t);
262 case TIFFTAG_PLANARCONFIG:
264 if (v != PLANARCONFIG_CONTIG && v != PLANARCONFIG_SEPARATE)
266 td->td_planarconfig = (uint16) v;
268 case TIFFTAG_PAGENAME:
269 _TIFFsetString(&td->td_pagename, va_arg(ap, char*));
271 case TIFFTAG_XPOSITION:
272 td->td_xposition = (float) va_arg(ap, dblparam_t);
274 case TIFFTAG_YPOSITION:
275 td->td_yposition = (float) va_arg(ap, dblparam_t);
277 case TIFFTAG_RESOLUTIONUNIT:
279 if (v < RESUNIT_NONE || RESUNIT_CENTIMETER < v)
281 td->td_resolutionunit = (uint16) v;
283 case TIFFTAG_PAGENUMBER:
284 td->td_pagenumber[0] = (uint16) va_arg(ap, int);
285 td->td_pagenumber[1] = (uint16) va_arg(ap, int);
287 case TIFFTAG_HALFTONEHINTS:
288 td->td_halftonehints[0] = (uint16) va_arg(ap, int);
289 td->td_halftonehints[1] = (uint16) va_arg(ap, int);
291 case TIFFTAG_COLORMAP:
292 v32 = (uint32)(1L<<td->td_bitspersample);
293 _TIFFsetShortArray(&td->td_colormap[0], va_arg(ap, uint16*), v32);
294 _TIFFsetShortArray(&td->td_colormap[1], va_arg(ap, uint16*), v32);
295 _TIFFsetShortArray(&td->td_colormap[2], va_arg(ap, uint16*), v32);
297 case TIFFTAG_EXTRASAMPLES:
298 if (!setExtraSamples(td, ap, &v))
301 case TIFFTAG_MATTEING:
302 td->td_extrasamples = (uint16) (va_arg(ap, int) != 0);
303 if (td->td_extrasamples) {
304 uint16 sv = EXTRASAMPLE_ASSOCALPHA;
305 _TIFFsetShortArray(&td->td_sampleinfo, &sv, 1);
308 case TIFFTAG_TILEWIDTH:
309 v32 = va_arg(ap, uint32);
311 if (tif->tif_mode != O_RDONLY)
313 TIFFWarning(tif->tif_name,
314 "Nonstandard tile width %d, convert file", v32);
316 td->td_tilewidth = v32;
317 tif->tif_flags |= TIFF_ISTILED;
319 case TIFFTAG_TILELENGTH:
320 v32 = va_arg(ap, uint32);
322 if (tif->tif_mode != O_RDONLY)
324 TIFFWarning(tif->tif_name,
325 "Nonstandard tile length %d, convert file", v32);
327 td->td_tilelength = v32;
328 tif->tif_flags |= TIFF_ISTILED;
330 case TIFFTAG_TILEDEPTH:
331 v32 = va_arg(ap, uint32);
334 td->td_tiledepth = v32;
336 case TIFFTAG_DATATYPE:
339 case DATATYPE_VOID: v = SAMPLEFORMAT_VOID; break;
340 case DATATYPE_INT: v = SAMPLEFORMAT_INT; break;
341 case DATATYPE_UINT: v = SAMPLEFORMAT_UINT; break;
342 case DATATYPE_IEEEFP: v = SAMPLEFORMAT_IEEEFP;break;
343 default: goto badvalue;
345 td->td_sampleformat = (uint16) v;
347 case TIFFTAG_SAMPLEFORMAT:
349 if (v < SAMPLEFORMAT_UINT || SAMPLEFORMAT_COMPLEXIEEEFP < v)
351 td->td_sampleformat = (uint16) v;
353 /* Try to fix up the SWAB function for complex data. */
354 if( td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT
355 && td->td_bitspersample == 32
356 && tif->tif_postdecode == _TIFFSwab32BitData )
357 tif->tif_postdecode = _TIFFSwab16BitData;
358 else if( (td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT
359 || td->td_sampleformat == SAMPLEFORMAT_COMPLEXIEEEFP)
360 && td->td_bitspersample == 64
361 && tif->tif_postdecode == _TIFFSwab64BitData )
362 tif->tif_postdecode = _TIFFSwab32BitData;
363 else if( td->td_sampleformat == SAMPLEFORMAT_COMPLEXIEEEFP
364 && td->td_bitspersample == 128
365 && tif->tif_postdecode == NULL )
366 tif->tif_postdecode = _TIFFSwab64BitData;
368 case TIFFTAG_IMAGEDEPTH:
369 td->td_imagedepth = va_arg(ap, uint32);
371 case TIFFTAG_STONITS:
372 d = va_arg(ap, dblparam_t);
377 /* Begin Pixar Tags */
378 case TIFFTAG_PIXAR_IMAGEFULLWIDTH:
379 td->td_imagefullwidth = va_arg(ap, uint32);
381 case TIFFTAG_PIXAR_IMAGEFULLLENGTH:
382 td->td_imagefulllength = va_arg(ap, uint32);
384 case TIFFTAG_PIXAR_TEXTUREFORMAT:
385 _TIFFsetString(&td->td_textureformat, va_arg(ap, char*));
387 case TIFFTAG_PIXAR_WRAPMODES:
388 _TIFFsetString(&td->td_wrapmodes, va_arg(ap, char*));
390 case TIFFTAG_PIXAR_FOVCOT:
391 td->td_fovcot = (float) va_arg(ap, dblparam_t);
393 case TIFFTAG_PIXAR_MATRIX_WORLDTOSCREEN:
394 _TIFFsetFloatArray(&td->td_matrixWorldToScreen,
395 va_arg(ap, float*), 16);
397 case TIFFTAG_PIXAR_MATRIX_WORLDTOCAMERA:
398 _TIFFsetFloatArray(&td->td_matrixWorldToCamera,
399 va_arg(ap, float*), 16);
404 if ((tif->tif_flags & TIFF_INSUBIFD) == 0) {
405 td->td_nsubifd = (uint16) va_arg(ap, int);
406 _TIFFsetLongArray(&td->td_subifd, va_arg(ap, uint32*),
407 (long) td->td_nsubifd);
409 TIFFError(module, "%s: Sorry, cannot nest SubIFDs",
414 case TIFFTAG_YCBCRCOEFFICIENTS:
415 _TIFFsetFloatArray(&td->td_ycbcrcoeffs, va_arg(ap, float*), 3);
417 case TIFFTAG_YCBCRPOSITIONING:
418 td->td_ycbcrpositioning = (uint16) va_arg(ap, int);
420 case TIFFTAG_YCBCRSUBSAMPLING:
421 td->td_ycbcrsubsampling[0] = (uint16) va_arg(ap, int);
422 td->td_ycbcrsubsampling[1] = (uint16) va_arg(ap, int);
424 case TIFFTAG_WHITEPOINT:
425 _TIFFsetFloatArray(&td->td_whitepoint, va_arg(ap, float*), 2);
427 case TIFFTAG_PRIMARYCHROMATICITIES:
428 _TIFFsetFloatArray(&td->td_primarychromas, va_arg(ap, float*), 6);
430 case TIFFTAG_TRANSFERFUNCTION:
431 v = (td->td_samplesperpixel - td->td_extrasamples) > 1 ? 3 : 1;
432 for (i = 0; i < v; i++)
433 _TIFFsetShortArray(&td->td_transferfunction[i],
434 va_arg(ap, uint16*), 1L<<td->td_bitspersample);
436 case TIFFTAG_REFERENCEBLACKWHITE:
437 /* XXX should check for null range */
438 _TIFFsetFloatArray(&td->td_refblackwhite, va_arg(ap, float*), 6);
441 td->td_inkset = (uint16) va_arg(ap, int);
443 case TIFFTAG_DOTRANGE:
444 /* XXX should check for null range */
445 td->td_dotrange[0] = (uint16) va_arg(ap, int);
446 td->td_dotrange[1] = (uint16) va_arg(ap, int);
448 case TIFFTAG_INKNAMES:
450 s = va_arg(ap, char*);
451 i = checkInkNamesString(tif, i, s);
454 _TIFFsetNString(&td->td_inknames, s, i);
455 td->td_inknameslen = i;
458 case TIFFTAG_NUMBEROFINKS:
459 td->td_ninks = (uint16) va_arg(ap, int);
461 case TIFFTAG_TARGETPRINTER:
462 _TIFFsetString(&td->td_targetprinter, va_arg(ap, char*));
464 case TIFFTAG_ICCPROFILE:
465 td->td_profileLength = (uint32) va_arg(ap, uint32);
466 _TIFFsetByteArray(&td->td_profileData, va_arg(ap, void*),
467 td->td_profileLength);
469 case TIFFTAG_PHOTOSHOP:
470 td->td_photoshopLength = (uint32) va_arg(ap, uint32);
471 _TIFFsetByteArray (&td->td_photoshopData, va_arg(ap, void*),
472 td->td_photoshopLength);
474 case TIFFTAG_RICHTIFFIPTC:
475 td->td_richtiffiptcLength = (uint32) va_arg(ap, uint32);
476 _TIFFsetLongArray ((uint32**)&td->td_richtiffiptcData,
478 td->td_richtiffiptcLength);
480 case TIFFTAG_XMLPACKET:
481 td->td_xmlpacketLength = (uint32) va_arg(ap, uint32);
482 _TIFFsetByteArray(&td->td_xmlpacketData, va_arg(ap, void*),
483 td->td_xmlpacketLength);
486 const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
488 int tv_size, iCustom;
491 * This can happen if multiple images are open with
492 * different codecs which have private tags. The
493 * global tag information table may then have tags
494 * that are valid for one file but not the other.
495 * If the client tries to set a tag that is not valid
496 * for the image's codec then we'll arrive here. This
497 * happens, for example, when tiffcp is used to convert
498 * between compression schemes and codec-specific tags
499 * are blindly copied.
501 if( fip == NULL || fip->field_bit != FIELD_CUSTOM )
504 "%s: Invalid %stag \"%s\" (not supported by codec)",
505 tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
506 _TIFFFieldWithTag(tif, tag)->field_name);
512 * Find the existing entry for this custom value.
515 for( iCustom = 0; iCustom < td->td_customValueCount; iCustom++ )
517 if( td->td_customValues[iCustom].info == fip )
519 tv = td->td_customValues + iCustom;
520 if( tv->value != NULL )
521 _TIFFfree( tv->value );
527 * Grow the custom list if the entry was not found.
531 TIFFTagValue *new_customValues;
533 td->td_customValueCount++;
534 new_customValues = (TIFFTagValue *)
535 _TIFFrealloc(td->td_customValues,
536 sizeof(TIFFTagValue) * td->td_customValueCount);
537 if (!new_customValues) {
539 "%s: Failed to allocate space for list of custom values",
545 td->td_customValues = new_customValues;
547 tv = td->td_customValues + (td->td_customValueCount-1);
554 * Set custom value ... save a copy of the custom tag value.
556 switch (fip->field_type) {
558 * XXX: We can't use TIFFDataWidth() to determine the
559 * space needed to store the value. For TIFF_RATIONAL
560 * values TIFFDataWidth() returns 8, but we use 4-byte
561 * float to represent rationals.
590 TIFFError(module, "%s: Bad field type %d for \"%s\"",
591 tif->tif_name, fip->field_type,
597 if(fip->field_passcount)
598 tv->count = (int) va_arg(ap, int);
602 if (fip->field_passcount) {
603 tv->value = _TIFFmalloc(tv_size * tv->count);
608 _TIFFmemcpy(tv->value, (void *) va_arg(ap,void*),
609 tv->count * tv_size);
610 } else if (fip->field_type == TIFF_ASCII) {
611 const char *value = (const char *) va_arg(ap,const char *);
612 tv->count = strlen(value)+1;
613 tv->value = _TIFFmalloc(tv->count);
618 strcpy(tv->value, value);
620 /* not supporting "pass by value" types yet */
622 "%s: Pass by value is not implemented.",
625 tv->value = _TIFFmalloc(tv_size * tv->count);
630 _TIFFmemset(tv->value, 0, tv->count * tv_size);
636 TIFFSetFieldBit(tif, _TIFFFieldWithTag(tif, tag)->field_bit);
637 tif->tif_flags |= TIFF_DIRTYDIRECT;
644 TIFFError(module, "%s: Bad value %d for \"%s\"",
645 tif->tif_name, v, _TIFFFieldWithTag(tif, tag)->field_name);
649 TIFFError(module, "%s: Bad value %ld for \"%s\"",
650 tif->tif_name, v32, _TIFFFieldWithTag(tif, tag)->field_name);
654 TIFFError(module, "%s: Bad value %f for \"%s\"",
655 tif->tif_name, d, _TIFFFieldWithTag(tif, tag)->field_name);
661 * Return 1/0 according to whether or not
662 * it is permissible to set the tag's value.
663 * Note that we allow ImageLength to be changed
664 * so that we can append and extend to images.
665 * Any other tag may not be altered once writing
666 * has commenced, unless its value has no effect
667 * on the format of the data that is written.
670 OkToChangeTag(TIFF* tif, ttag_t tag)
672 const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
673 if (!fip) { /* unknown tag */
674 TIFFError("TIFFSetField", "%s: Unknown %stag %u",
675 tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", tag);
678 if (tag != TIFFTAG_IMAGELENGTH && (tif->tif_flags & TIFF_BEENWRITING) &&
679 !fip->field_oktochange) {
681 * Consult info table to see if tag can be changed
682 * after we've started writing. We only allow changes
683 * to those tags that don't/shouldn't affect the
684 * compression and/or format of the data.
686 TIFFError("TIFFSetField",
687 "%s: Cannot modify tag \"%s\" while writing",
688 tif->tif_name, fip->field_name);
695 * Record the value of a field in the
696 * internal directory structure. The
697 * field will be written to the file
698 * when/if the directory structure is
702 TIFFSetField(TIFF* tif, ttag_t tag, ...)
708 status = TIFFVSetField(tif, tag, ap);
714 * Like TIFFSetField, but taking a varargs
715 * parameter list. This routine is useful
716 * for building higher-level interfaces on
717 * top of the library.
720 TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
722 return OkToChangeTag(tif, tag) ?
723 (*tif->tif_tagmethods.vsetfield)(tif, tag, ap) : 0;
727 _TIFFVGetField(TIFF* tif, ttag_t tag, va_list ap)
729 TIFFDirectory* td = &tif->tif_dir;
733 case TIFFTAG_SUBFILETYPE:
734 *va_arg(ap, uint32*) = td->td_subfiletype;
736 case TIFFTAG_IMAGEWIDTH:
737 *va_arg(ap, uint32*) = td->td_imagewidth;
739 case TIFFTAG_IMAGELENGTH:
740 *va_arg(ap, uint32*) = td->td_imagelength;
742 case TIFFTAG_BITSPERSAMPLE:
743 *va_arg(ap, uint16*) = td->td_bitspersample;
745 case TIFFTAG_COMPRESSION:
746 *va_arg(ap, uint16*) = td->td_compression;
748 case TIFFTAG_PHOTOMETRIC:
749 *va_arg(ap, uint16*) = td->td_photometric;
751 case TIFFTAG_THRESHHOLDING:
752 *va_arg(ap, uint16*) = td->td_threshholding;
754 case TIFFTAG_FILLORDER:
755 *va_arg(ap, uint16*) = td->td_fillorder;
757 case TIFFTAG_DOCUMENTNAME:
758 *va_arg(ap, char**) = td->td_documentname;
761 *va_arg(ap, char**) = td->td_artist;
763 case TIFFTAG_DATETIME:
764 *va_arg(ap, char**) = td->td_datetime;
766 case TIFFTAG_HOSTCOMPUTER:
767 *va_arg(ap, char**) = td->td_hostcomputer;
769 case TIFFTAG_IMAGEDESCRIPTION:
770 *va_arg(ap, char**) = td->td_imagedescription;
773 *va_arg(ap, char**) = td->td_make;
776 *va_arg(ap, char**) = td->td_model;
778 case TIFFTAG_COPYRIGHT:
779 *va_arg(ap, char**) = td->td_copyright;
781 case TIFFTAG_ORIENTATION:
782 *va_arg(ap, uint16*) = td->td_orientation;
784 case TIFFTAG_SAMPLESPERPIXEL:
785 *va_arg(ap, uint16*) = td->td_samplesperpixel;
787 case TIFFTAG_ROWSPERSTRIP:
788 *va_arg(ap, uint32*) = td->td_rowsperstrip;
790 case TIFFTAG_MINSAMPLEVALUE:
791 *va_arg(ap, uint16*) = td->td_minsamplevalue;
793 case TIFFTAG_MAXSAMPLEVALUE:
794 *va_arg(ap, uint16*) = td->td_maxsamplevalue;
796 case TIFFTAG_SMINSAMPLEVALUE:
797 *va_arg(ap, double*) = td->td_sminsamplevalue;
799 case TIFFTAG_SMAXSAMPLEVALUE:
800 *va_arg(ap, double*) = td->td_smaxsamplevalue;
802 case TIFFTAG_XRESOLUTION:
803 *va_arg(ap, float*) = td->td_xresolution;
805 case TIFFTAG_YRESOLUTION:
806 *va_arg(ap, float*) = td->td_yresolution;
808 case TIFFTAG_PLANARCONFIG:
809 *va_arg(ap, uint16*) = td->td_planarconfig;
811 case TIFFTAG_XPOSITION:
812 *va_arg(ap, float*) = td->td_xposition;
814 case TIFFTAG_YPOSITION:
815 *va_arg(ap, float*) = td->td_yposition;
817 case TIFFTAG_PAGENAME:
818 *va_arg(ap, char**) = td->td_pagename;
820 case TIFFTAG_RESOLUTIONUNIT:
821 *va_arg(ap, uint16*) = td->td_resolutionunit;
823 case TIFFTAG_PAGENUMBER:
824 *va_arg(ap, uint16*) = td->td_pagenumber[0];
825 *va_arg(ap, uint16*) = td->td_pagenumber[1];
827 case TIFFTAG_HALFTONEHINTS:
828 *va_arg(ap, uint16*) = td->td_halftonehints[0];
829 *va_arg(ap, uint16*) = td->td_halftonehints[1];
831 case TIFFTAG_COLORMAP:
832 *va_arg(ap, uint16**) = td->td_colormap[0];
833 *va_arg(ap, uint16**) = td->td_colormap[1];
834 *va_arg(ap, uint16**) = td->td_colormap[2];
836 case TIFFTAG_STRIPOFFSETS:
837 case TIFFTAG_TILEOFFSETS:
838 *va_arg(ap, uint32**) = td->td_stripoffset;
840 case TIFFTAG_STRIPBYTECOUNTS:
841 case TIFFTAG_TILEBYTECOUNTS:
842 *va_arg(ap, uint32**) = td->td_stripbytecount;
844 case TIFFTAG_MATTEING:
845 *va_arg(ap, uint16*) =
846 (td->td_extrasamples == 1 &&
847 td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
849 case TIFFTAG_EXTRASAMPLES:
850 *va_arg(ap, uint16*) = td->td_extrasamples;
851 *va_arg(ap, uint16**) = td->td_sampleinfo;
853 case TIFFTAG_TILEWIDTH:
854 *va_arg(ap, uint32*) = td->td_tilewidth;
856 case TIFFTAG_TILELENGTH:
857 *va_arg(ap, uint32*) = td->td_tilelength;
859 case TIFFTAG_TILEDEPTH:
860 *va_arg(ap, uint32*) = td->td_tiledepth;
862 case TIFFTAG_DATATYPE:
863 switch (td->td_sampleformat) {
864 case SAMPLEFORMAT_UINT:
865 *va_arg(ap, uint16*) = DATATYPE_UINT;
867 case SAMPLEFORMAT_INT:
868 *va_arg(ap, uint16*) = DATATYPE_INT;
870 case SAMPLEFORMAT_IEEEFP:
871 *va_arg(ap, uint16*) = DATATYPE_IEEEFP;
873 case SAMPLEFORMAT_VOID:
874 *va_arg(ap, uint16*) = DATATYPE_VOID;
878 case TIFFTAG_SAMPLEFORMAT:
879 *va_arg(ap, uint16*) = td->td_sampleformat;
881 case TIFFTAG_IMAGEDEPTH:
882 *va_arg(ap, uint32*) = td->td_imagedepth;
884 case TIFFTAG_STONITS:
885 *va_arg(ap, double*) = td->td_stonits;
888 *va_arg(ap, uint16*) = td->td_nsubifd;
889 *va_arg(ap, uint32**) = td->td_subifd;
891 case TIFFTAG_YCBCRCOEFFICIENTS:
892 *va_arg(ap, float**) = td->td_ycbcrcoeffs;
894 case TIFFTAG_YCBCRPOSITIONING:
895 *va_arg(ap, uint16*) = td->td_ycbcrpositioning;
897 case TIFFTAG_YCBCRSUBSAMPLING:
898 *va_arg(ap, uint16*) = td->td_ycbcrsubsampling[0];
899 *va_arg(ap, uint16*) = td->td_ycbcrsubsampling[1];
901 case TIFFTAG_WHITEPOINT:
902 *va_arg(ap, float**) = td->td_whitepoint;
904 case TIFFTAG_PRIMARYCHROMATICITIES:
905 *va_arg(ap, float**) = td->td_primarychromas;
907 case TIFFTAG_TRANSFERFUNCTION:
908 *va_arg(ap, uint16**) = td->td_transferfunction[0];
909 if (td->td_samplesperpixel - td->td_extrasamples > 1) {
910 *va_arg(ap, uint16**) = td->td_transferfunction[1];
911 *va_arg(ap, uint16**) = td->td_transferfunction[2];
914 case TIFFTAG_REFERENCEBLACKWHITE:
915 *va_arg(ap, float**) = td->td_refblackwhite;
918 *va_arg(ap, uint16*) = td->td_inkset;
920 case TIFFTAG_DOTRANGE:
921 *va_arg(ap, uint16*) = td->td_dotrange[0];
922 *va_arg(ap, uint16*) = td->td_dotrange[1];
924 case TIFFTAG_INKNAMES:
925 *va_arg(ap, char**) = td->td_inknames;
927 case TIFFTAG_NUMBEROFINKS:
928 *va_arg(ap, uint16*) = td->td_ninks;
930 case TIFFTAG_TARGETPRINTER:
931 *va_arg(ap, char**) = td->td_targetprinter;
933 case TIFFTAG_ICCPROFILE:
934 *va_arg(ap, uint32*) = td->td_profileLength;
935 *va_arg(ap, void**) = td->td_profileData;
937 case TIFFTAG_PHOTOSHOP:
938 *va_arg(ap, uint32*) = td->td_photoshopLength;
939 *va_arg(ap, void**) = td->td_photoshopData;
941 case TIFFTAG_RICHTIFFIPTC:
942 *va_arg(ap, uint32*) = td->td_richtiffiptcLength;
943 *va_arg(ap, void**) = td->td_richtiffiptcData;
945 case TIFFTAG_XMLPACKET:
946 *va_arg(ap, uint32*) = td->td_xmlpacketLength;
947 *va_arg(ap, void**) = td->td_xmlpacketData;
949 /* Begin Pixar Tags */
950 case TIFFTAG_PIXAR_IMAGEFULLWIDTH:
951 *va_arg(ap, uint32*) = td->td_imagefullwidth;
953 case TIFFTAG_PIXAR_IMAGEFULLLENGTH:
954 *va_arg(ap, uint32*) = td->td_imagefulllength;
956 case TIFFTAG_PIXAR_TEXTUREFORMAT:
957 *va_arg(ap, char**) = td->td_textureformat;
959 case TIFFTAG_PIXAR_WRAPMODES:
960 *va_arg(ap, char**) = td->td_wrapmodes;
962 case TIFFTAG_PIXAR_FOVCOT:
963 *va_arg(ap, float*) = td->td_fovcot;
965 case TIFFTAG_PIXAR_MATRIX_WORLDTOSCREEN:
966 *va_arg(ap, float**) = td->td_matrixWorldToScreen;
968 case TIFFTAG_PIXAR_MATRIX_WORLDTOCAMERA:
969 *va_arg(ap, float**) = td->td_matrixWorldToCamera;
975 const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
979 * This can happen if multiple images are open with
980 * different codecs which have private tags. The
981 * global tag information table may then have tags
982 * that are valid for one file but not the other.
983 * If the client tries to get a tag that is not valid
984 * for the image's codec then we'll arrive here.
986 if( fip == NULL || fip->field_bit != FIELD_CUSTOM )
988 TIFFError("_TIFFVGetField",
989 "%s: Invalid %stag \"%s\" (not supported by codec)",
990 tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
991 _TIFFFieldWithTag(tif, tag)->field_name);
997 ** Do we have a custom value?
1000 for (i = 0; i < td->td_customValueCount; i++)
1002 TIFFTagValue *tv = td->td_customValues + i;
1004 if (tv->info->field_tag != tag)
1007 if (fip->field_passcount) {
1008 if (fip->field_readcount == TIFF_VARIABLE2)
1009 *va_arg(ap, uint32*) = (uint32)tv->count;
1010 else /* Assume TIFF_VARIABLE */
1011 *va_arg(ap, uint16*) = (uint16)tv->count;
1012 *va_arg(ap, void **) = tv->value;
1015 } else if (fip->field_type == TIFF_ASCII) {
1016 *va_arg(ap, void **) = tv->value;
1020 TIFFError("_TIFFVGetField",
1021 "%s: Pass by value is not implemented.",
1032 * Return the value of a field in the
1033 * internal directory structure.
1036 TIFFGetField(TIFF* tif, ttag_t tag, ...)
1042 status = TIFFVGetField(tif, tag, ap);
1048 * Like TIFFGetField, but taking a varargs
1049 * parameter list. This routine is useful
1050 * for building higher-level interfaces on
1051 * top of the library.
1054 TIFFVGetField(TIFF* tif, ttag_t tag, va_list ap)
1056 const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
1057 return (fip && (isPseudoTag(tag) || TIFFFieldSet(tif, fip->field_bit)) ?
1058 (*tif->tif_tagmethods.vgetfield)(tif, tag, ap) : 0);
1061 #define CleanupField(member) { \
1063 _TIFFfree(td->member); \
1069 * Release storage associated with a directory.
1072 TIFFFreeDirectory(TIFF* tif)
1074 TIFFDirectory *td = &tif->tif_dir;
1077 CleanupField(td_colormap[0]);
1078 CleanupField(td_colormap[1]);
1079 CleanupField(td_colormap[2]);
1080 CleanupField(td_documentname);
1081 CleanupField(td_artist);
1082 CleanupField(td_datetime);
1083 CleanupField(td_hostcomputer);
1084 CleanupField(td_imagedescription);
1085 CleanupField(td_make);
1086 CleanupField(td_model);
1087 CleanupField(td_copyright);
1088 CleanupField(td_pagename);
1089 CleanupField(td_sampleinfo);
1090 CleanupField(td_subifd);
1091 CleanupField(td_ycbcrcoeffs);
1092 CleanupField(td_inknames);
1093 CleanupField(td_targetprinter);
1094 CleanupField(td_whitepoint);
1095 CleanupField(td_primarychromas);
1096 CleanupField(td_refblackwhite);
1097 CleanupField(td_transferfunction[0]);
1098 CleanupField(td_transferfunction[1]);
1099 CleanupField(td_transferfunction[2]);
1100 CleanupField(td_profileData);
1101 CleanupField(td_photoshopData);
1102 CleanupField(td_richtiffiptcData);
1103 CleanupField(td_xmlpacketData);
1104 CleanupField(td_stripoffset);
1105 CleanupField(td_stripbytecount);
1106 /* Begin Pixar Tags */
1107 CleanupField(td_textureformat);
1108 CleanupField(td_wrapmodes);
1109 CleanupField(td_matrixWorldToScreen);
1110 CleanupField(td_matrixWorldToCamera);
1111 /* End Pixar Tags */
1113 /* Cleanup custom tag values */
1114 for( i = 0; i < td->td_customValueCount; i++ ) {
1115 if (td->td_customValues[i].value)
1116 _TIFFfree(td->td_customValues[i].value);
1119 td->td_customValueCount = 0;
1120 CleanupField(td_customValues);
1125 * Client Tag extension support (from Niles Ritter).
1127 static TIFFExtendProc _TIFFextender = (TIFFExtendProc) NULL;
1130 TIFFSetTagExtender(TIFFExtendProc extender)
1132 TIFFExtendProc prev = _TIFFextender;
1133 _TIFFextender = extender;
1138 * Setup for a new directory. Should we automatically call
1139 * TIFFWriteDirectory() if the current one is dirty?
1141 * The newly created directory will not exist on the file till
1142 * TIFFWriteDirectory(), TIFFFlush() or TIFFClose() is called.
1145 TIFFCreateDirectory(TIFF* tif)
1147 TIFFDefaultDirectory(tif);
1148 tif->tif_diroff = 0;
1149 tif->tif_nextdiroff = 0;
1150 tif->tif_curoff = 0;
1151 tif->tif_row = (uint32) -1;
1152 tif->tif_curstrip = (tstrip_t) -1;
1158 * Setup a default directory structure.
1161 TIFFDefaultDirectory(TIFF* tif)
1163 register TIFFDirectory* td = &tif->tif_dir;
1165 _TIFFSetupFieldInfo(tif);
1166 _TIFFmemset(td, 0, sizeof (*td));
1167 td->td_fillorder = FILLORDER_MSB2LSB;
1168 td->td_bitspersample = 1;
1169 td->td_threshholding = THRESHHOLD_BILEVEL;
1170 td->td_orientation = ORIENTATION_TOPLEFT;
1171 td->td_samplesperpixel = 1;
1172 td->td_rowsperstrip = (uint32) -1;
1173 td->td_tilewidth = 0;
1174 td->td_tilelength = 0;
1175 td->td_tiledepth = 1;
1176 td->td_stripbytecountsorted = 1; /* Our own arrays always sorted. */
1177 td->td_resolutionunit = RESUNIT_INCH;
1178 td->td_sampleformat = SAMPLEFORMAT_UINT;
1179 td->td_imagedepth = 1;
1180 td->td_ycbcrsubsampling[0] = 2;
1181 td->td_ycbcrsubsampling[1] = 2;
1182 td->td_ycbcrpositioning = YCBCRPOSITION_CENTERED;
1183 td->td_inkset = INKSET_CMYK;
1185 tif->tif_postdecode = _TIFFNoPostDecode;
1186 tif->tif_foundfield = NULL;
1187 tif->tif_tagmethods.vsetfield = _TIFFVSetField;
1188 tif->tif_tagmethods.vgetfield = _TIFFVGetField;
1189 tif->tif_tagmethods.printdir = NULL;
1191 * Give client code a chance to install their own
1192 * tag extensions & methods, prior to compression overloads.
1195 (*_TIFFextender)(tif);
1196 (void) TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
1198 * NB: The directory is marked dirty as a result of setting
1199 * up the default compression scheme. However, this really
1200 * isn't correct -- we want TIFF_DIRTYDIRECT to be set only
1201 * if the user does something. We could just do the setup
1202 * by hand, but it seems better to use the normal mechanism
1203 * (i.e. TIFFSetField).
1205 tif->tif_flags &= ~TIFF_DIRTYDIRECT;
1208 * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19
1209 * we clear the ISTILED flag when setting up a new directory.
1210 * Should we also be clearing stuff like INSUBIFD?
1212 tif->tif_flags &= ~TIFF_ISTILED;
1218 TIFFAdvanceDirectory(TIFF* tif, uint32* nextdir, toff_t* off)
1220 static const char module[] = "TIFFAdvanceDirectory";
1224 toff_t poff=*nextdir;
1225 if (poff+sizeof(uint16) > tif->tif_size)
1227 TIFFError(module, "%s: Error fetching directory count",
1231 _TIFFmemcpy(&dircount, tif->tif_base+poff, sizeof (uint16));
1232 if (tif->tif_flags & TIFF_SWAB)
1233 TIFFSwabShort(&dircount);
1234 poff+=sizeof (uint16)+dircount*sizeof (TIFFDirEntry);
1237 if (((toff_t) (poff+sizeof (uint32))) > tif->tif_size)
1239 TIFFError(module, "%s: Error fetching directory link",
1243 _TIFFmemcpy(nextdir, tif->tif_base+poff, sizeof (uint32));
1244 if (tif->tif_flags & TIFF_SWAB)
1245 TIFFSwabLong(nextdir);
1250 if (!SeekOK(tif, *nextdir) ||
1251 !ReadOK(tif, &dircount, sizeof (uint16))) {
1252 TIFFError(module, "%s: Error fetching directory count",
1256 if (tif->tif_flags & TIFF_SWAB)
1257 TIFFSwabShort(&dircount);
1259 *off = TIFFSeekFile(tif,
1260 dircount*sizeof (TIFFDirEntry), SEEK_CUR);
1262 (void) TIFFSeekFile(tif,
1263 dircount*sizeof (TIFFDirEntry), SEEK_CUR);
1264 if (!ReadOK(tif, nextdir, sizeof (uint32))) {
1265 TIFFError(module, "%s: Error fetching directory link",
1269 if (tif->tif_flags & TIFF_SWAB)
1270 TIFFSwabLong(nextdir);
1276 * Count the number of directories in a file.
1279 TIFFNumberOfDirectories(TIFF* tif)
1281 toff_t nextdir = tif->tif_header.tiff_diroff;
1284 while (nextdir != 0 && TIFFAdvanceDirectory(tif, &nextdir, NULL))
1290 * Set the n-th directory as the current directory.
1291 * NB: Directories are numbered starting at 0.
1294 TIFFSetDirectory(TIFF* tif, tdir_t dirn)
1299 nextdir = tif->tif_header.tiff_diroff;
1300 for (n = dirn; n > 0 && nextdir != 0; n--)
1301 if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
1303 tif->tif_nextdiroff = nextdir;
1305 * Set curdir to the actual directory index. The
1306 * -1 is because TIFFReadDirectory will increment
1307 * tif_curdir after successfully reading the directory.
1309 tif->tif_curdir = (dirn - n) - 1;
1311 * Reset tif_dirnumber counter and start new list of seen directories.
1312 * We need this to prevent IFD loops.
1314 tif->tif_dirnumber = 0;
1315 return (TIFFReadDirectory(tif));
1319 * Set the current directory to be the directory
1320 * located at the specified file offset. This interface
1321 * is used mainly to access directories linked with
1322 * the SubIFD tag (e.g. thumbnail images).
1325 TIFFSetSubDirectory(TIFF* tif, uint32 diroff)
1327 tif->tif_nextdiroff = diroff;
1329 * Reset tif_dirnumber counter and start new list of seen directories.
1330 * We need this to prevent IFD loops.
1332 tif->tif_dirnumber = 0;
1333 return (TIFFReadDirectory(tif));
1337 * Return file offset of the current directory.
1340 TIFFCurrentDirOffset(TIFF* tif)
1342 return (tif->tif_diroff);
1346 * Return an indication of whether or not we are
1347 * at the last directory in the file.
1350 TIFFLastDirectory(TIFF* tif)
1352 return (tif->tif_nextdiroff == 0);
1356 * Unlink the specified directory from the directory chain.
1359 TIFFUnlinkDirectory(TIFF* tif, tdir_t dirn)
1361 static const char module[] = "TIFFUnlinkDirectory";
1366 if (tif->tif_mode == O_RDONLY) {
1367 TIFFError(module, "Can not unlink directory in read-only file");
1371 * Go to the directory before the one we want
1372 * to unlink and nab the offset of the link
1373 * field we'll need to patch.
1375 nextdir = tif->tif_header.tiff_diroff;
1376 off = sizeof (uint16) + sizeof (uint16);
1377 for (n = dirn-1; n > 0; n--) {
1379 TIFFError(module, "Directory %d does not exist", dirn);
1382 if (!TIFFAdvanceDirectory(tif, &nextdir, &off))
1386 * Advance to the directory to be unlinked and fetch
1387 * the offset of the directory that follows.
1389 if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
1392 * Go back and patch the link field of the preceding
1393 * directory to point to the offset of the directory
1396 (void) TIFFSeekFile(tif, off, SEEK_SET);
1397 if (tif->tif_flags & TIFF_SWAB)
1398 TIFFSwabLong(&nextdir);
1399 if (!WriteOK(tif, &nextdir, sizeof (uint32))) {
1400 TIFFError(module, "Error writing directory link");
1404 * Leave directory state setup safely. We don't have
1405 * facilities for doing inserting and removing directories,
1406 * so it's safest to just invalidate everything. This
1407 * means that the caller can only append to the directory
1410 (*tif->tif_cleanup)(tif);
1411 if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
1412 _TIFFfree(tif->tif_rawdata);
1413 tif->tif_rawdata = NULL;
1416 tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP|TIFF_POSTENCODE);
1417 TIFFFreeDirectory(tif);
1418 TIFFDefaultDirectory(tif);
1419 tif->tif_diroff = 0; /* force link on next write */
1420 tif->tif_nextdiroff = 0; /* next write must be at end */
1421 tif->tif_curoff = 0;
1422 tif->tif_row = (uint32) -1;
1423 tif->tif_curstrip = (tstrip_t) -1;
1429 * Author: Bruce Cameron <cameron@petris.com>
1431 * Set a table of tags that are to be replaced during directory process by the
1432 * 'IGNORE' state - or return TRUE/FALSE for the requested tag such that
1433 * 'ReadDirectory' can use the stored information.
1435 * FIXME: this is never used properly. Should be removed in the future.
1438 TIFFReassignTagToIgnore (enum TIFFIgnoreSense task, int TIFFtagID)
1440 static int TIFFignoretags [FIELD_LAST];
1441 static int tagcount = 0 ;
1442 int i; /* Loop index */
1443 int j; /* Loop index */
1448 if ( tagcount < (FIELD_LAST - 1) )
1450 for ( j = 0 ; j < tagcount ; ++j )
1451 { /* Do not add duplicate tag */
1452 if ( TIFFignoretags [j] == TIFFtagID )
1455 TIFFignoretags [tagcount++] = TIFFtagID ;
1461 for ( i = 0 ; i < tagcount ; ++i )
1463 if ( TIFFignoretags [i] == TIFFtagID )
1469 tagcount = 0 ; /* Clear the list */
1479 /* vim: set ts=8 sts=8 sw=8 noet: */