1 /* $Id: tif_dirwrite.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 Write Support Routines.
35 #define TIFFCvtNativeToIEEEFloat(tif, n, fp)
36 #define TIFFCvtNativeToIEEEDouble(tif, n, dp)
38 extern void TIFFCvtNativeToIEEEFloat(TIFF*, uint32, float*);
39 extern void TIFFCvtNativeToIEEEDouble(TIFF*, uint32, double*);
42 static int TIFFWriteNormalTag(TIFF*, TIFFDirEntry*, const TIFFFieldInfo*);
43 static void TIFFSetupShortLong(TIFF*, ttag_t, TIFFDirEntry*, uint32);
44 static void TIFFSetupShort(TIFF*, ttag_t, TIFFDirEntry*, uint16);
45 static int TIFFSetupShortPair(TIFF*, ttag_t, TIFFDirEntry*);
46 static int TIFFWritePerSampleShorts(TIFF*, ttag_t, TIFFDirEntry*);
47 static int TIFFWritePerSampleAnys(TIFF*, TIFFDataType, ttag_t, TIFFDirEntry*);
48 static int TIFFWriteShortTable(TIFF*, ttag_t, TIFFDirEntry*, uint32, uint16**);
49 static int TIFFWriteShortArray(TIFF*, TIFFDirEntry*, uint16*);
50 static int TIFFWriteLongArray(TIFF *, TIFFDirEntry*, uint32*);
51 static int TIFFWriteRationalArray(TIFF *, TIFFDirEntry*, float*);
52 static int TIFFWriteFloatArray(TIFF *, TIFFDirEntry*, float*);
53 static int TIFFWriteDoubleArray(TIFF *, TIFFDirEntry*, double*);
54 static int TIFFWriteByteArray(TIFF*, TIFFDirEntry*, char*);
55 static int TIFFWriteAnyArray(TIFF*,
56 TIFFDataType, ttag_t, TIFFDirEntry*, uint32, double*);
57 static int TIFFWriteTransferFunction(TIFF*, TIFFDirEntry*);
58 static int TIFFWriteInkNames(TIFF*, TIFFDirEntry*);
59 static int TIFFWriteData(TIFF*, TIFFDirEntry*, char*);
60 static int TIFFLinkDirectory(TIFF*);
62 #define WriteRationalPair(type, tag1, v1, tag2, v2) { \
63 TIFFWriteRational((tif), (type), (tag1), (dir), (v1)) \
64 TIFFWriteRational((tif), (type), (tag2), (dir)+1, (v2)) \
67 #define TIFFWriteRational(tif, type, tag, dir, v) \
68 (dir)->tdir_tag = (tag); \
69 (dir)->tdir_type = (type); \
70 (dir)->tdir_count = 1; \
71 if (!TIFFWriteRationalArray((tif), (dir), &(v))) \
75 * Write the contents of the current directory
76 * to the specified file. This routine doesn't
77 * handle overwriting a directory with auxiliary
78 * storage that's been changed.
81 _TIFFWriteDirectory(TIFF* tif, int done)
91 unsigned long b, fields[FIELD_SETLONGS];
94 if (tif->tif_mode == O_RDONLY)
97 * Clear write state so that subsequent images with
98 * different characteristics get the right buffers
103 if (tif->tif_flags & TIFF_POSTENCODE) {
104 tif->tif_flags &= ~TIFF_POSTENCODE;
105 if (!(*tif->tif_postencode)(tif)) {
106 TIFFError(tif->tif_name,
107 "Error post-encoding before directory write");
111 (*tif->tif_close)(tif); /* shutdown encoder */
113 * Flush any data that might have been written
114 * by the compression close+cleanup routines.
116 if (tif->tif_rawcc > 0 && !TIFFFlushData1(tif)) {
117 TIFFError(tif->tif_name,
118 "Error flushing data before directory write");
121 if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
122 _TIFFfree(tif->tif_rawdata);
123 tif->tif_rawdata = NULL;
125 tif->tif_rawdatasize = 0;
127 tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP);
132 * Size the directory so that we can calculate
133 * offsets for the data items that aren't kept
134 * in-place in each field.
137 for (b = 0; b <= FIELD_LAST; b++)
138 if (TIFFFieldSet(tif, b) && b != FIELD_CUSTOM)
139 nfields += (b < FIELD_SUBFILETYPE ? 2 : 1);
140 nfields += td->td_customValueCount;
141 dirsize = nfields * sizeof (TIFFDirEntry);
142 data = (char*) _TIFFmalloc(dirsize);
144 TIFFError(tif->tif_name,
145 "Cannot write directory, out of space");
149 * Directory hasn't been placed yet, put
150 * it at the end of the file and link it
151 * into the existing directory structure.
153 if (tif->tif_diroff == 0 && !TIFFLinkDirectory(tif))
155 tif->tif_dataoff = (toff_t)(
156 tif->tif_diroff + sizeof (uint16) + dirsize + sizeof (toff_t));
157 if (tif->tif_dataoff & 1)
159 (void) TIFFSeekFile(tif, tif->tif_dataoff, SEEK_SET);
161 dir = (TIFFDirEntry*) data;
163 * Setup external form of directory
164 * entries and write data items.
166 _TIFFmemcpy(fields, td->td_fieldsset, sizeof (fields));
168 * Write out ExtraSamples tag only if
169 * extra samples are present in the data.
171 if (FieldSet(fields, FIELD_EXTRASAMPLES) && !td->td_extrasamples) {
172 ResetFieldBit(fields, FIELD_EXTRASAMPLES);
174 dirsize -= sizeof (TIFFDirEntry);
176 for (fi = 0, nfi = tif->tif_nfields; nfi > 0; nfi--, fi++) {
177 const TIFFFieldInfo* fip = tif->tif_fieldinfo[fi];
180 ** For custom fields, we test to see if the custom field
181 ** is set or not. For normal fields, we just use the
184 if( fip->field_bit == FIELD_CUSTOM )
186 int ci, is_set = FALSE;
188 for( ci = 0; ci < td->td_customValueCount; ci++ )
189 is_set |= (td->td_customValues[ci].info == fip);
194 else if (!FieldSet(fields, fip->field_bit))
199 ** Handle other fields.
201 switch (fip->field_bit)
203 case FIELD_STRIPOFFSETS:
205 * We use one field bit for both strip and tile
207 * offsets, and so must be careful in selecting
208 * the appropriate field descriptor (so that tags
209 * are written in sorted order).
212 TIFFTAG_TILEOFFSETS : TIFFTAG_STRIPOFFSETS;
213 if (tag != fip->field_tag)
216 dir->tdir_tag = (uint16) tag;
217 dir->tdir_type = (uint16) TIFF_LONG;
218 dir->tdir_count = (uint32) td->td_nstrips;
219 if (!TIFFWriteLongArray(tif, dir, td->td_stripoffset))
222 case FIELD_STRIPBYTECOUNTS:
224 * We use one field bit for both strip and tile
225 * byte counts, and so must be careful in selecting
226 * the appropriate field descriptor (so that tags
227 * are written in sorted order).
230 TIFFTAG_TILEBYTECOUNTS : TIFFTAG_STRIPBYTECOUNTS;
231 if (tag != fip->field_tag)
234 dir->tdir_tag = (uint16) tag;
235 dir->tdir_type = (uint16) TIFF_LONG;
236 dir->tdir_count = (uint32) td->td_nstrips;
237 if (!TIFFWriteLongArray(tif, dir,
238 td->td_stripbytecount))
241 case FIELD_ROWSPERSTRIP:
242 TIFFSetupShortLong(tif, TIFFTAG_ROWSPERSTRIP,
243 dir, td->td_rowsperstrip);
246 if (!TIFFWriteShortTable(tif, TIFFTAG_COLORMAP, dir,
250 case FIELD_IMAGEDIMENSIONS:
251 TIFFSetupShortLong(tif, TIFFTAG_IMAGEWIDTH,
252 dir++, td->td_imagewidth);
253 TIFFSetupShortLong(tif, TIFFTAG_IMAGELENGTH,
254 dir, td->td_imagelength);
256 case FIELD_TILEDIMENSIONS:
257 TIFFSetupShortLong(tif, TIFFTAG_TILEWIDTH,
258 dir++, td->td_tilewidth);
259 TIFFSetupShortLong(tif, TIFFTAG_TILELENGTH,
260 dir, td->td_tilelength);
262 case FIELD_COMPRESSION:
263 TIFFSetupShort(tif, TIFFTAG_COMPRESSION,
264 dir, td->td_compression);
266 case FIELD_PHOTOMETRIC:
267 TIFFSetupShort(tif, TIFFTAG_PHOTOMETRIC,
268 dir, td->td_photometric);
271 WriteRationalPair(TIFF_RATIONAL,
272 TIFFTAG_XPOSITION, td->td_xposition,
273 TIFFTAG_YPOSITION, td->td_yposition);
275 case FIELD_RESOLUTION:
276 WriteRationalPair(TIFF_RATIONAL,
277 TIFFTAG_XRESOLUTION, td->td_xresolution,
278 TIFFTAG_YRESOLUTION, td->td_yresolution);
280 case FIELD_BITSPERSAMPLE:
281 case FIELD_MINSAMPLEVALUE:
282 case FIELD_MAXSAMPLEVALUE:
283 case FIELD_SAMPLEFORMAT:
284 if (!TIFFWritePerSampleShorts(tif, fip->field_tag, dir))
287 case FIELD_SMINSAMPLEVALUE:
288 case FIELD_SMAXSAMPLEVALUE:
289 if (!TIFFWritePerSampleAnys(tif,
290 _TIFFSampleToTagType(tif), fip->field_tag, dir))
293 case FIELD_PAGENUMBER:
294 case FIELD_HALFTONEHINTS:
295 case FIELD_YCBCRSUBSAMPLING:
297 if (!TIFFSetupShortPair(tif, fip->field_tag, dir))
301 if (!TIFFWriteInkNames(tif, dir))
304 case FIELD_TRANSFERFUNCTION:
305 if (!TIFFWriteTransferFunction(tif, dir))
310 * XXX: Always write this field using LONG type
311 * for backward compatibility.
313 dir->tdir_tag = (uint16) fip->field_tag;
314 dir->tdir_type = (uint16) TIFF_LONG;
315 dir->tdir_count = (uint32) td->td_nsubifd;
316 if (!TIFFWriteLongArray(tif, dir, td->td_subifd))
319 * Total hack: if this directory includes a SubIFD
320 * tag then force the next <n> directories to be
321 * written as ``sub directories'' of this one. This
322 * is used to write things like thumbnails and
323 * image masks that one wants to keep out of the
324 * normal directory linkage access mechanism.
326 if (dir->tdir_count > 0) {
327 tif->tif_flags |= TIFF_INSUBIFD;
328 tif->tif_nsubifd = (uint16) dir->tdir_count;
329 if (dir->tdir_count > 1)
330 tif->tif_subifdoff = dir->tdir_offset;
332 tif->tif_subifdoff = (uint32)(
335 + ((char*)&dir->tdir_offset-data));
339 if (!TIFFWriteNormalTag(tif, dir, fip))
345 if( fip->field_bit != FIELD_CUSTOM )
346 ResetFieldBit(fields, fip->field_bit);
352 dircount = (uint16) nfields;
353 diroff = (uint32) tif->tif_nextdiroff;
354 if (tif->tif_flags & TIFF_SWAB) {
356 * The file's byte order is opposite to the
357 * native machine architecture. We overwrite
358 * the directory information with impunity
359 * because it'll be released below after we
360 * write it to the file. Note that all the
361 * other tag construction routines assume that
362 * we do this byte-swapping; i.e. they only
363 * byte-swap indirect data.
365 for (dir = (TIFFDirEntry*) data; dircount; dir++, dircount--) {
366 TIFFSwabArrayOfShort(&dir->tdir_tag, 2);
367 TIFFSwabArrayOfLong(&dir->tdir_count, 2);
369 dircount = (uint16) nfields;
370 TIFFSwabShort(&dircount);
371 TIFFSwabLong(&diroff);
373 (void) TIFFSeekFile(tif, tif->tif_diroff, SEEK_SET);
374 if (!WriteOK(tif, &dircount, sizeof (dircount))) {
375 TIFFError(tif->tif_name, "Error writing directory count");
378 if (!WriteOK(tif, data, dirsize)) {
379 TIFFError(tif->tif_name, "Error writing directory contents");
382 if (!WriteOK(tif, &diroff, sizeof (diroff))) {
383 TIFFError(tif->tif_name, "Error writing directory link");
387 TIFFFreeDirectory(tif);
388 tif->tif_flags &= ~TIFF_DIRTYDIRECT;
389 (*tif->tif_cleanup)(tif);
392 * Reset directory-related state for subsequent
395 TIFFCreateDirectory(tif);
403 #undef WriteRationalPair
406 TIFFWriteDirectory(TIFF* tif)
408 return _TIFFWriteDirectory(tif, TRUE);
412 * Similar to TIFFWriteDirectory(), writes the directory out
413 * but leaves all data structures in memory so that it can be
414 * written again. This will make a partially written TIFF file
415 * readable before it is successfully completed/closed.
418 TIFFCheckpointDirectory(TIFF* tif)
421 /* Setup the strips arrays, if they haven't already been. */
422 if (tif->tif_dir.td_stripoffset == NULL)
423 (void) TIFFSetupStrips(tif);
424 rc = _TIFFWriteDirectory(tif, FALSE);
425 (void) TIFFSetWriteOffset(tif, TIFFSeekFile(tif, 0, SEEK_END));
430 * Process tags that are not special cased.
433 TIFFWriteNormalTag(TIFF* tif, TIFFDirEntry* dir, const TIFFFieldInfo* fip)
435 uint16 wc = (uint16) fip->field_writecount;
438 dir->tdir_tag = (uint16) fip->field_tag;
439 dir->tdir_type = (uint16) fip->field_type;
440 dir->tdir_count = wc;
442 switch (fip->field_type) {
445 if (fip->field_passcount) {
447 if (wc == (uint16) TIFF_VARIABLE2) {
448 TIFFGetField(tif, fip->field_tag, &wc2, &wp);
449 dir->tdir_count = wc2;
450 } else { /* Assume TIFF_VARIABLE */
451 TIFFGetField(tif, fip->field_tag, &wc, &wp);
452 dir->tdir_count = wc;
454 if (!TIFFWriteShortArray(tif, dir, wp))
459 TIFFGetField(tif, fip->field_tag, &sv);
461 TIFFInsertData(tif, dir->tdir_type, sv);
464 TIFFGetField(tif, fip->field_tag, &wp);
465 if (!TIFFWriteShortArray(tif, dir, wp))
473 if (fip->field_passcount) {
475 if (wc == (uint16) TIFF_VARIABLE2) {
476 TIFFGetField(tif, fip->field_tag, &wc2, &lp);
477 dir->tdir_count = wc2;
478 } else { /* Assume TIFF_VARIABLE */
479 TIFFGetField(tif, fip->field_tag, &wc, &lp);
480 dir->tdir_count = wc;
482 if (!TIFFWriteLongArray(tif, dir, lp))
486 /* XXX handle LONG->SHORT conversion */
487 TIFFGetField(tif, fip->field_tag,
491 TIFFGetField(tif, fip->field_tag, &lp);
492 if (!TIFFWriteLongArray(tif, dir, lp))
499 if (fip->field_passcount) {
501 if (wc == (uint16) TIFF_VARIABLE2) {
502 TIFFGetField(tif, fip->field_tag, &wc2, &fp);
503 dir->tdir_count = wc2;
504 } else { /* Assume TIFF_VARIABLE */
505 TIFFGetField(tif, fip->field_tag, &wc, &fp);
506 dir->tdir_count = wc;
508 if (!TIFFWriteRationalArray(tif, dir, fp))
513 TIFFGetField(tif, fip->field_tag, &fv);
514 if (!TIFFWriteRationalArray(tif, dir, &fv))
518 TIFFGetField(tif, fip->field_tag, &fp);
519 if (!TIFFWriteRationalArray(tif, dir, fp))
525 if (fip->field_passcount) {
527 if (wc == (uint16) TIFF_VARIABLE2) {
528 TIFFGetField(tif, fip->field_tag, &wc2, &fp);
529 dir->tdir_count = wc2;
530 } else { /* Assume TIFF_VARIABLE */
531 TIFFGetField(tif, fip->field_tag, &wc, &fp);
532 dir->tdir_count = wc;
534 if (!TIFFWriteFloatArray(tif, dir, fp))
539 TIFFGetField(tif, fip->field_tag, &fv);
540 if (!TIFFWriteFloatArray(tif, dir, &fv))
544 TIFFGetField(tif, fip->field_tag, &fp);
545 if (!TIFFWriteFloatArray(tif, dir, fp))
551 if (fip->field_passcount) {
553 if (wc == (uint16) TIFF_VARIABLE2) {
554 TIFFGetField(tif, fip->field_tag, &wc2, &dp);
555 dir->tdir_count = wc2;
556 } else { /* Assume TIFF_VARIABLE */
557 TIFFGetField(tif, fip->field_tag, &wc, &dp);
558 dir->tdir_count = wc;
560 if (!TIFFWriteDoubleArray(tif, dir, dp))
565 TIFFGetField(tif, fip->field_tag, &dv);
566 if (!TIFFWriteDoubleArray(tif, dir, &dv))
570 TIFFGetField(tif, fip->field_tag, &dp);
571 if (!TIFFWriteDoubleArray(tif, dir, dp))
579 if (fip->field_passcount)
580 TIFFGetField(tif, fip->field_tag, &wc, &cp);
582 TIFFGetField(tif, fip->field_tag, &cp);
584 dir->tdir_count = (uint32) (strlen(cp) + 1);
585 if (!TIFFWriteByteArray(tif, dir, cp))
592 if (fip->field_passcount) {
594 if (wc == (uint16) TIFF_VARIABLE2) {
595 TIFFGetField(tif, fip->field_tag, &wc2, &cp);
596 dir->tdir_count = wc2;
597 } else { /* Assume TIFF_VARIABLE */
598 TIFFGetField(tif, fip->field_tag, &wc, &cp);
599 dir->tdir_count = wc;
601 if (!TIFFWriteByteArray(tif, dir, cp))
606 TIFFGetField(tif, fip->field_tag, &cv);
607 if (!TIFFWriteByteArray(tif, dir, &cv))
611 TIFFGetField(tif, fip->field_tag, &cp);
612 if (!TIFFWriteByteArray(tif, dir, cp))
620 if (wc == (unsigned short) TIFF_VARIABLE) {
621 TIFFGetField(tif, fip->field_tag, &wc, &cp);
622 dir->tdir_count = wc;
623 } else if (wc == (unsigned short) TIFF_VARIABLE2) {
624 TIFFGetField(tif, fip->field_tag, &wc2, &cp);
625 dir->tdir_count = wc2;
627 TIFFGetField(tif, fip->field_tag, &cp);
628 if (!TIFFWriteByteArray(tif, dir, cp))
640 * Setup a directory entry with either a SHORT
641 * or LONG type according to the value.
644 TIFFSetupShortLong(TIFF* tif, ttag_t tag, TIFFDirEntry* dir, uint32 v)
646 dir->tdir_tag = (uint16) tag;
649 dir->tdir_type = (short) TIFF_LONG;
650 dir->tdir_offset = v;
652 dir->tdir_type = (short) TIFF_SHORT;
653 dir->tdir_offset = TIFFInsertData(tif, (int) TIFF_SHORT, v);
658 * Setup a SHORT directory entry
661 TIFFSetupShort(TIFF* tif, ttag_t tag, TIFFDirEntry* dir, uint16 v)
663 dir->tdir_tag = (uint16) tag;
665 dir->tdir_type = (short) TIFF_SHORT;
666 dir->tdir_offset = TIFFInsertData(tif, (int) TIFF_SHORT, v);
668 #undef MakeShortDirent
670 #define NITEMS(x) (sizeof (x) / sizeof (x[0]))
672 * Setup a directory entry that references a
673 * samples/pixel array of SHORT values and
674 * (potentially) write the associated indirect
678 TIFFWritePerSampleShorts(TIFF* tif, ttag_t tag, TIFFDirEntry* dir)
682 int i, status, samples = tif->tif_dir.td_samplesperpixel;
684 if (samples > NITEMS(buf)) {
685 w = (uint16*) _TIFFmalloc(samples * sizeof (uint16));
687 TIFFError(tif->tif_name,
688 "No space to write per-sample shorts");
692 TIFFGetField(tif, tag, &v);
693 for (i = 0; i < samples; i++)
696 dir->tdir_tag = (uint16) tag;
697 dir->tdir_type = (uint16) TIFF_SHORT;
698 dir->tdir_count = samples;
699 status = TIFFWriteShortArray(tif, dir, w);
701 _TIFFfree((char*) w);
706 * Setup a directory entry that references a samples/pixel array of ``type''
707 * values and (potentially) write the associated indirect values. The source
708 * data from TIFFGetField() for the specified tag must be returned as double.
711 TIFFWritePerSampleAnys(TIFF* tif,
712 TIFFDataType type, ttag_t tag, TIFFDirEntry* dir)
717 int samples = (int) tif->tif_dir.td_samplesperpixel;
719 if (samples > NITEMS(buf)) {
720 w = (double*) _TIFFmalloc(samples * sizeof (double));
722 TIFFError(tif->tif_name,
723 "No space to write per-sample values");
727 TIFFGetField(tif, tag, &v);
728 for (i = 0; i < samples; i++)
730 status = TIFFWriteAnyArray(tif, type, tag, dir, samples, w);
738 * Setup a pair of shorts that are returned by
739 * value, rather than as a reference to an array.
742 TIFFSetupShortPair(TIFF* tif, ttag_t tag, TIFFDirEntry* dir)
746 TIFFGetField(tif, tag, &v[0], &v[1]);
748 dir->tdir_tag = (uint16) tag;
749 dir->tdir_type = (uint16) TIFF_SHORT;
751 return (TIFFWriteShortArray(tif, dir, v));
755 * Setup a directory entry for an NxM table of shorts,
756 * where M is known to be 2**bitspersample, and write
757 * the associated indirect data.
760 TIFFWriteShortTable(TIFF* tif,
761 ttag_t tag, TIFFDirEntry* dir, uint32 n, uint16** table)
765 dir->tdir_tag = (uint16) tag;
766 dir->tdir_type = (short) TIFF_SHORT;
767 /* XXX -- yech, fool TIFFWriteData */
768 dir->tdir_count = (uint32) (1L<<tif->tif_dir.td_bitspersample);
769 off = tif->tif_dataoff;
770 for (i = 0; i < n; i++)
771 if (!TIFFWriteData(tif, dir, (char *)table[i]))
773 dir->tdir_count *= n;
774 dir->tdir_offset = off;
779 * Write/copy data associated with an ASCII or opaque tag value.
782 TIFFWriteByteArray(TIFF* tif, TIFFDirEntry* dir, char* cp)
784 if (dir->tdir_count > 4) {
785 if (!TIFFWriteData(tif, dir, cp))
788 _TIFFmemcpy(&dir->tdir_offset, cp, dir->tdir_count);
793 * Setup a directory entry of an array of SHORT
794 * or SSHORT and write the associated indirect values.
797 TIFFWriteShortArray(TIFF* tif, TIFFDirEntry* dir, uint16* v)
799 if (dir->tdir_count <= 2) {
800 if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) {
801 dir->tdir_offset = (uint32) ((long) v[0] << 16);
802 if (dir->tdir_count == 2)
803 dir->tdir_offset |= v[1] & 0xffff;
805 dir->tdir_offset = v[0] & 0xffff;
806 if (dir->tdir_count == 2)
807 dir->tdir_offset |= (long) v[1] << 16;
811 return (TIFFWriteData(tif, dir, (char*) v));
815 * Setup a directory entry of an array of LONG
816 * or SLONG and write the associated indirect values.
819 TIFFWriteLongArray(TIFF* tif, TIFFDirEntry* dir, uint32* v)
821 if (dir->tdir_count == 1) {
822 dir->tdir_offset = v[0];
825 return (TIFFWriteData(tif, dir, (char*) v));
829 * Setup a directory entry of an array of RATIONAL
830 * or SRATIONAL and write the associated indirect values.
833 TIFFWriteRationalArray(TIFF* tif, TIFFDirEntry* dir, float* v)
839 t = (uint32*) _TIFFmalloc(2 * dir->tdir_count * sizeof (uint32));
841 TIFFError(tif->tif_name,
842 "No space to write RATIONAL array");
845 for (i = 0; i < dir->tdir_count; i++) {
851 if (dir->tdir_type == TIFF_RATIONAL) {
852 TIFFWarning(tif->tif_name,
853 "\"%s\": Information lost writing value (%g) as (unsigned) RATIONAL",
854 _TIFFFieldWithTag(tif,dir->tdir_tag)->field_name,
862 while (fv < 1L<<(31-3) && den < 1L<<(31-3))
863 fv *= 1<<3, den *= 1L<<3;
865 t[2*i+0] = (uint32) (sign * (fv + 0.5));
868 status = TIFFWriteData(tif, dir, (char *)t);
869 _TIFFfree((char*) t);
874 TIFFWriteFloatArray(TIFF* tif, TIFFDirEntry* dir, float* v)
876 TIFFCvtNativeToIEEEFloat(tif, dir->tdir_count, v);
877 if (dir->tdir_count == 1) {
878 dir->tdir_offset = *(uint32*) &v[0];
881 return (TIFFWriteData(tif, dir, (char*) v));
885 TIFFWriteDoubleArray(TIFF* tif, TIFFDirEntry* dir, double* v)
887 TIFFCvtNativeToIEEEDouble(tif, n, v);
888 return (TIFFWriteData(tif, dir, (char*) v));
892 * Write an array of ``type'' values for a specified tag (i.e. this is a tag
893 * which is allowed to have different types, e.g. SMaxSampleType).
894 * Internally the data values are represented as double since a double can
895 * hold any of the TIFF tag types (yes, this should really be an abstract
896 * type tany_t for portability). The data is converted into the specified
897 * type in a temporary buffer and then handed off to the appropriate array
901 TIFFWriteAnyArray(TIFF* tif,
902 TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, uint32 n, double* v)
904 char buf[10 * sizeof(double)];
908 if (n * TIFFDataWidth(type) > sizeof buf) {
909 w = (char*) _TIFFmalloc(n * TIFFDataWidth(type));
911 TIFFError(tif->tif_name,
912 "No space to write array");
917 dir->tdir_tag = (uint16) tag;
918 dir->tdir_type = (uint16) type;
924 uint8* bp = (uint8*) w;
925 for (i = 0; i < (int) n; i++)
926 bp[i] = (uint8) v[i];
927 if (!TIFFWriteByteArray(tif, dir, (char*) bp))
933 int8* bp = (int8*) w;
934 for (i = 0; i < (int) n; i++)
936 if (!TIFFWriteByteArray(tif, dir, (char*) bp))
942 uint16* bp = (uint16*) w;
943 for (i = 0; i < (int) n; i++)
944 bp[i] = (uint16) v[i];
945 if (!TIFFWriteShortArray(tif, dir, (uint16*)bp))
951 int16* bp = (int16*) w;
952 for (i = 0; i < (int) n; i++)
953 bp[i] = (int16) v[i];
954 if (!TIFFWriteShortArray(tif, dir, (uint16*)bp))
960 uint32* bp = (uint32*) w;
961 for (i = 0; i < (int) n; i++)
962 bp[i] = (uint32) v[i];
963 if (!TIFFWriteLongArray(tif, dir, bp))
969 int32* bp = (int32*) w;
970 for (i = 0; i < (int) n; i++)
971 bp[i] = (int32) v[i];
972 if (!TIFFWriteLongArray(tif, dir, (uint32*) bp))
978 float* bp = (float*) w;
979 for (i = 0; i < (int) n; i++)
980 bp[i] = (float) v[i];
981 if (!TIFFWriteFloatArray(tif, dir, bp))
986 return (TIFFWriteDoubleArray(tif, dir, v));
1003 TIFFWriteTransferFunction(TIFF* tif, TIFFDirEntry* dir)
1005 TIFFDirectory* td = &tif->tif_dir;
1006 tsize_t n = (1L<<td->td_bitspersample) * sizeof (uint16);
1007 uint16** tf = td->td_transferfunction;
1011 * Check if the table can be written as a single column,
1012 * or if it must be written as 3 columns. Note that we
1013 * write a 3-column tag if there are 2 samples/pixel and
1014 * a single column of data won't suffice--hmm.
1016 switch (td->td_samplesperpixel - td->td_extrasamples) {
1017 default: if (_TIFFmemcmp(tf[0], tf[2], n)) { ncols = 3; break; }
1018 case 2: if (_TIFFmemcmp(tf[0], tf[1], n)) { ncols = 3; break; }
1019 case 1: case 0: ncols = 1;
1021 return (TIFFWriteShortTable(tif,
1022 TIFFTAG_TRANSFERFUNCTION, dir, ncols, tf));
1026 TIFFWriteInkNames(TIFF* tif, TIFFDirEntry* dir)
1028 TIFFDirectory* td = &tif->tif_dir;
1030 dir->tdir_tag = TIFFTAG_INKNAMES;
1031 dir->tdir_type = (short) TIFF_ASCII;
1032 dir->tdir_count = td->td_inknameslen;
1033 return (TIFFWriteByteArray(tif, dir, td->td_inknames));
1037 * Write a contiguous directory item.
1040 TIFFWriteData(TIFF* tif, TIFFDirEntry* dir, char* cp)
1044 if (tif->tif_flags & TIFF_SWAB) {
1045 switch (dir->tdir_type) {
1048 TIFFSwabArrayOfShort((uint16*) cp, dir->tdir_count);
1053 TIFFSwabArrayOfLong((uint32*) cp, dir->tdir_count);
1056 case TIFF_SRATIONAL:
1057 TIFFSwabArrayOfLong((uint32*) cp, 2*dir->tdir_count);
1060 TIFFSwabArrayOfDouble((double*) cp, dir->tdir_count);
1064 dir->tdir_offset = tif->tif_dataoff;
1065 cc = dir->tdir_count * TIFFDataWidth((TIFFDataType) dir->tdir_type);
1066 if (SeekOK(tif, dir->tdir_offset) &&
1067 WriteOK(tif, cp, cc)) {
1068 tif->tif_dataoff += (cc + 1) & ~1;
1071 TIFFError(tif->tif_name, "Error writing data for field \"%s\"",
1072 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
1077 * Similar to TIFFWriteDirectory(), but if the directory has already
1078 * been written once, it is relocated to the end of the file, in case it
1079 * has changed in size. Note that this will result in the loss of the
1080 * previously used directory space.
1084 TIFFRewriteDirectory( TIFF *tif )
1086 static const char module[] = "TIFFRewriteDirectory";
1088 /* We don't need to do anything special if it hasn't been written. */
1089 if( tif->tif_diroff == 0 )
1090 return TIFFWriteDirectory( tif );
1093 ** Find and zero the pointer to this directory, so that TIFFLinkDirectory
1094 ** will cause it to be added after this directories current pre-link.
1097 /* Is it the first directory in the file? */
1098 if (tif->tif_header.tiff_diroff == tif->tif_diroff)
1100 tif->tif_header.tiff_diroff = 0;
1101 tif->tif_diroff = 0;
1103 TIFFSeekFile(tif, (toff_t)(TIFF_MAGIC_SIZE+TIFF_VERSION_SIZE),
1105 if (!WriteOK(tif, &(tif->tif_header.tiff_diroff),
1106 sizeof (tif->tif_diroff)))
1108 TIFFError(tif->tif_name, "Error updating TIFF header");
1114 toff_t nextdir, off;
1116 nextdir = tif->tif_header.tiff_diroff;
1120 if (!SeekOK(tif, nextdir) ||
1121 !ReadOK(tif, &dircount, sizeof (dircount))) {
1122 TIFFError(module, "Error fetching directory count");
1125 if (tif->tif_flags & TIFF_SWAB)
1126 TIFFSwabShort(&dircount);
1127 (void) TIFFSeekFile(tif,
1128 dircount * sizeof (TIFFDirEntry), SEEK_CUR);
1129 if (!ReadOK(tif, &nextdir, sizeof (nextdir))) {
1130 TIFFError(module, "Error fetching directory link");
1133 if (tif->tif_flags & TIFF_SWAB)
1134 TIFFSwabLong(&nextdir);
1135 } while (nextdir != tif->tif_diroff && nextdir != 0);
1136 off = TIFFSeekFile(tif, 0, SEEK_CUR); /* get current offset */
1137 (void) TIFFSeekFile(tif, off - (toff_t)sizeof(nextdir), SEEK_SET);
1138 tif->tif_diroff = 0;
1139 if (!WriteOK(tif, &(tif->tif_diroff), sizeof (nextdir))) {
1140 TIFFError(module, "Error writing directory link");
1146 ** Now use TIFFWriteDirectory() normally.
1149 return TIFFWriteDirectory( tif );
1154 * Link the current directory into the
1155 * directory chain for the file.
1158 TIFFLinkDirectory(TIFF* tif)
1160 static const char module[] = "TIFFLinkDirectory";
1164 tif->tif_diroff = (TIFFSeekFile(tif, (toff_t) 0, SEEK_END)+1) &~ 1;
1165 diroff = tif->tif_diroff;
1166 if (tif->tif_flags & TIFF_SWAB)
1167 TIFFSwabLong(&diroff);
1172 if (tif->tif_flags & TIFF_INSUBIFD) {
1173 (void) TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET);
1174 if (!WriteOK(tif, &diroff, sizeof (diroff))) {
1176 "%s: Error writing SubIFD directory link",
1181 * Advance to the next SubIFD or, if this is
1182 * the last one configured, revert back to the
1183 * normal directory linkage.
1185 if (--tif->tif_nsubifd)
1186 tif->tif_subifdoff += sizeof (diroff);
1188 tif->tif_flags &= ~TIFF_INSUBIFD;
1192 if (tif->tif_header.tiff_diroff == 0) {
1194 * First directory, overwrite offset in header.
1196 tif->tif_header.tiff_diroff = tif->tif_diroff;
1197 (void) TIFFSeekFile(tif,
1198 (toff_t)(TIFF_MAGIC_SIZE+TIFF_VERSION_SIZE),
1200 if (!WriteOK(tif, &diroff, sizeof (diroff))) {
1201 TIFFError(tif->tif_name, "Error writing TIFF header");
1207 * Not the first directory, search to the last and append.
1209 nextdir = tif->tif_header.tiff_diroff;
1213 if (!SeekOK(tif, nextdir) ||
1214 !ReadOK(tif, &dircount, sizeof (dircount))) {
1215 TIFFError(module, "Error fetching directory count");
1218 if (tif->tif_flags & TIFF_SWAB)
1219 TIFFSwabShort(&dircount);
1220 (void) TIFFSeekFile(tif,
1221 dircount * sizeof (TIFFDirEntry), SEEK_CUR);
1222 if (!ReadOK(tif, &nextdir, sizeof (nextdir))) {
1223 TIFFError(module, "Error fetching directory link");
1226 if (tif->tif_flags & TIFF_SWAB)
1227 TIFFSwabLong(&nextdir);
1228 } while (nextdir != 0);
1229 off = TIFFSeekFile(tif, 0, SEEK_CUR); /* get current offset */
1230 (void) TIFFSeekFile(tif, off - (toff_t)sizeof(nextdir), SEEK_SET);
1231 if (!WriteOK(tif, &diroff, sizeof (diroff))) {
1232 TIFFError(module, "Error writing directory link");
1238 /* vim: set ts=8 sts=8 sw=8 noet: */