]>
git.saurik.com Git - wxWidgets.git/blob - src/tiff/tif_dirwrite.c
df120153f20a4b426a630c49e20c62107bfb52cf
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
*,
50 TIFFDataType
, ttag_t
, TIFFDirEntry
*, uint32
, uint16
*);
51 static int TIFFWriteLongArray(TIFF
*,
52 TIFFDataType
, ttag_t
, TIFFDirEntry
*, uint32
, uint32
*);
53 static int TIFFWriteRationalArray(TIFF
*,
54 TIFFDataType
, ttag_t
, TIFFDirEntry
*, uint32
, float*);
55 static int TIFFWriteFloatArray(TIFF
*,
56 TIFFDataType
, ttag_t
, TIFFDirEntry
*, uint32
, float*);
57 static int TIFFWriteDoubleArray(TIFF
*,
58 TIFFDataType
, ttag_t
, TIFFDirEntry
*, uint32
, double*);
59 static int TIFFWriteByteArray(TIFF
*, TIFFDirEntry
*, char*);
60 static int TIFFWriteAnyArray(TIFF
*,
61 TIFFDataType
, ttag_t
, TIFFDirEntry
*, uint32
, double*);
62 #ifdef COLORIMETRY_SUPPORT
63 static int TIFFWriteTransferFunction(TIFF
*, TIFFDirEntry
*);
66 static int TIFFWriteInkNames(TIFF
*, TIFFDirEntry
*);
68 static int TIFFWriteData(TIFF
*, TIFFDirEntry
*, char*);
69 static int TIFFLinkDirectory(TIFF
*);
71 #define WriteRationalPair(type, tag1, v1, tag2, v2) { \
72 if (!TIFFWriteRational(tif, type, tag1, dir, v1)) \
74 if (!TIFFWriteRational(tif, type, tag2, dir+1, v2)) \
78 #define TIFFWriteRational(tif, type, tag, dir, v) \
79 TIFFWriteRationalArray((tif), (type), (tag), (dir), 1, &(v))
80 #ifndef TIFFWriteRational
81 static int TIFFWriteRational(TIFF
*,
82 TIFFDataType
, ttag_t
, TIFFDirEntry
*, float);
86 * Write the contents of the current directory
87 * to the specified file. This routine doesn't
88 * handle overwriting a directory with auxiliary
89 * storage that's been changed.
92 _TIFFWriteDirectory(TIFF
* tif
, int done
)
102 u_long b
, fields
[FIELD_SETLONGS
];
105 if (tif
->tif_mode
== O_RDONLY
)
108 * Clear write state so that subsequent images with
109 * different characteristics get the right buffers
114 if (tif
->tif_flags
& TIFF_POSTENCODE
) {
115 tif
->tif_flags
&= ~TIFF_POSTENCODE
;
116 if (!(*tif
->tif_postencode
)(tif
)) {
117 TIFFError(tif
->tif_name
,
118 "Error post-encoding before directory write");
122 (*tif
->tif_close
)(tif
); /* shutdown encoder */
124 * Flush any data that might have been written
125 * by the compression close+cleanup routines.
127 if (tif
->tif_rawcc
> 0 && !TIFFFlushData1(tif
)) {
128 TIFFError(tif
->tif_name
,
129 "Error flushing data before directory write");
132 if ((tif
->tif_flags
& TIFF_MYBUFFER
) && tif
->tif_rawdata
) {
133 _TIFFfree(tif
->tif_rawdata
);
134 tif
->tif_rawdata
= NULL
;
136 tif
->tif_rawdatasize
= 0;
138 tif
->tif_flags
&= ~(TIFF_BEENWRITING
|TIFF_BUFFERSETUP
);
143 * Size the directory so that we can calculate
144 * offsets for the data items that aren't kept
145 * in-place in each field.
148 for (b
= 0; b
<= FIELD_LAST
; b
++)
149 if (TIFFFieldSet(tif
, b
) && b
!= FIELD_CUSTOM
)
150 nfields
+= (b
< FIELD_SUBFILETYPE
? 2 : 1);
151 nfields
+= td
->td_customValueCount
;
152 dirsize
= nfields
* sizeof (TIFFDirEntry
);
153 data
= (char*) _TIFFmalloc(dirsize
);
155 TIFFError(tif
->tif_name
,
156 "Cannot write directory, out of space");
160 * Directory hasn't been placed yet, put
161 * it at the end of the file and link it
162 * into the existing directory structure.
164 if (tif
->tif_diroff
== 0 && !TIFFLinkDirectory(tif
))
166 tif
->tif_dataoff
= (toff_t
)(
167 tif
->tif_diroff
+ sizeof (uint16
) + dirsize
+ sizeof (toff_t
));
168 if (tif
->tif_dataoff
& 1)
170 (void) TIFFSeekFile(tif
, tif
->tif_dataoff
, SEEK_SET
);
172 dir
= (TIFFDirEntry
*) data
;
174 * Setup external form of directory
175 * entries and write data items.
177 _TIFFmemcpy(fields
, td
->td_fieldsset
, sizeof (fields
));
179 * Write out ExtraSamples tag only if
180 * extra samples are present in the data.
182 if (FieldSet(fields
, FIELD_EXTRASAMPLES
) && !td
->td_extrasamples
) {
183 ResetFieldBit(fields
, FIELD_EXTRASAMPLES
);
185 dirsize
-= sizeof (TIFFDirEntry
);
187 for (fi
= 0, nfi
= tif
->tif_nfields
; nfi
> 0; nfi
--, fi
++) {
188 const TIFFFieldInfo
* fip
= tif
->tif_fieldinfo
[fi
];
191 ** For custom fields, we test to see if the custom field
192 ** is set or not. For normal fields, we just use the
195 if( fip
->field_bit
== FIELD_CUSTOM
)
197 int ci
, is_set
= FALSE
;
199 for( ci
= 0; ci
< td
->td_customValueCount
; ci
++ )
200 is_set
|= (td
->td_customValues
[ci
].info
== fip
);
205 else if (!FieldSet(fields
, fip
->field_bit
))
210 ** Handle other fields.
212 switch (fip
->field_bit
)
214 case FIELD_STRIPOFFSETS
:
216 * We use one field bit for both strip and tile
218 * offsets, and so must be careful in selecting
219 * the appropriate field descriptor (so that tags
220 * are written in sorted order).
223 TIFFTAG_TILEOFFSETS
: TIFFTAG_STRIPOFFSETS
;
224 if (tag
!= fip
->field_tag
)
226 if (!TIFFWriteLongArray(tif
, TIFF_LONG
, tag
, dir
,
227 (uint32
) td
->td_nstrips
, td
->td_stripoffset
))
230 case FIELD_STRIPBYTECOUNTS
:
232 * We use one field bit for both strip and tile
233 * byte counts, and so must be careful in selecting
234 * the appropriate field descriptor (so that tags
235 * are written in sorted order).
238 TIFFTAG_TILEBYTECOUNTS
: TIFFTAG_STRIPBYTECOUNTS
;
239 if (tag
!= fip
->field_tag
)
241 if (!TIFFWriteLongArray(tif
, TIFF_LONG
, tag
, dir
,
242 (uint32
) td
->td_nstrips
, td
->td_stripbytecount
))
245 case FIELD_ROWSPERSTRIP
:
246 TIFFSetupShortLong(tif
, TIFFTAG_ROWSPERSTRIP
,
247 dir
, td
->td_rowsperstrip
);
250 if (!TIFFWriteShortTable(tif
, TIFFTAG_COLORMAP
, dir
,
254 case FIELD_IMAGEDIMENSIONS
:
255 TIFFSetupShortLong(tif
, TIFFTAG_IMAGEWIDTH
,
256 dir
++, td
->td_imagewidth
);
257 TIFFSetupShortLong(tif
, TIFFTAG_IMAGELENGTH
,
258 dir
, td
->td_imagelength
);
260 case FIELD_TILEDIMENSIONS
:
261 TIFFSetupShortLong(tif
, TIFFTAG_TILEWIDTH
,
262 dir
++, td
->td_tilewidth
);
263 TIFFSetupShortLong(tif
, TIFFTAG_TILELENGTH
,
264 dir
, td
->td_tilelength
);
266 case FIELD_COMPRESSION
:
267 TIFFSetupShort(tif
, TIFFTAG_COMPRESSION
,
268 dir
, td
->td_compression
);
270 case FIELD_PHOTOMETRIC
:
271 TIFFSetupShort(tif
, TIFFTAG_PHOTOMETRIC
,
272 dir
, td
->td_photometric
);
275 WriteRationalPair(TIFF_RATIONAL
,
276 TIFFTAG_XPOSITION
, td
->td_xposition
,
277 TIFFTAG_YPOSITION
, td
->td_yposition
);
279 case FIELD_RESOLUTION
:
280 WriteRationalPair(TIFF_RATIONAL
,
281 TIFFTAG_XRESOLUTION
, td
->td_xresolution
,
282 TIFFTAG_YRESOLUTION
, td
->td_yresolution
);
284 case FIELD_BITSPERSAMPLE
:
285 case FIELD_MINSAMPLEVALUE
:
286 case FIELD_MAXSAMPLEVALUE
:
287 case FIELD_SAMPLEFORMAT
:
288 if (!TIFFWritePerSampleShorts(tif
, fip
->field_tag
, dir
))
291 case FIELD_SMINSAMPLEVALUE
:
292 case FIELD_SMAXSAMPLEVALUE
:
293 if (!TIFFWritePerSampleAnys(tif
,
294 _TIFFSampleToTagType(tif
), fip
->field_tag
, dir
))
297 case FIELD_PAGENUMBER
:
298 case FIELD_HALFTONEHINTS
:
300 case FIELD_YCBCRSUBSAMPLING
:
305 if (!TIFFSetupShortPair(tif
, fip
->field_tag
, dir
))
310 if (!TIFFWriteInkNames(tif
, dir
))
314 #ifdef COLORIMETRY_SUPPORT
315 case FIELD_TRANSFERFUNCTION
:
316 if (!TIFFWriteTransferFunction(tif
, dir
))
322 if (!TIFFWriteNormalTag(tif
, dir
, fip
))
325 * Total hack: if this directory includes a SubIFD
326 * tag then force the next <n> directories to be
327 * written as ``sub directories'' of this one. This
328 * is used to write things like thumbnails and
329 * image masks that one wants to keep out of the
330 * normal directory linkage access mechanism.
332 if (dir
->tdir_count
> 0) {
333 tif
->tif_flags
|= TIFF_INSUBIFD
;
334 tif
->tif_nsubifd
= (uint16
) dir
->tdir_count
;
335 if (dir
->tdir_count
> 1)
336 tif
->tif_subifdoff
= dir
->tdir_offset
;
338 tif
->tif_subifdoff
= (uint32
)(
341 + ((char*)&dir
->tdir_offset
-data
));
346 if (!TIFFWriteNormalTag(tif
, dir
, fip
))
352 if( fip
->field_bit
!= FIELD_CUSTOM
)
353 ResetFieldBit(fields
, fip
->field_bit
);
359 dircount
= (uint16
) nfields
;
360 diroff
= (uint32
) tif
->tif_nextdiroff
;
361 if (tif
->tif_flags
& TIFF_SWAB
) {
363 * The file's byte order is opposite to the
364 * native machine architecture. We overwrite
365 * the directory information with impunity
366 * because it'll be released below after we
367 * write it to the file. Note that all the
368 * other tag construction routines assume that
369 * we do this byte-swapping; i.e. they only
370 * byte-swap indirect data.
372 for (dir
= (TIFFDirEntry
*) data
; dircount
; dir
++, dircount
--) {
373 TIFFSwabArrayOfShort(&dir
->tdir_tag
, 2);
374 TIFFSwabArrayOfLong(&dir
->tdir_count
, 2);
376 dircount
= (uint16
) nfields
;
377 TIFFSwabShort(&dircount
);
378 TIFFSwabLong(&diroff
);
380 (void) TIFFSeekFile(tif
, tif
->tif_diroff
, SEEK_SET
);
381 if (!WriteOK(tif
, &dircount
, sizeof (dircount
))) {
382 TIFFError(tif
->tif_name
, "Error writing directory count");
385 if (!WriteOK(tif
, data
, dirsize
)) {
386 TIFFError(tif
->tif_name
, "Error writing directory contents");
389 if (!WriteOK(tif
, &diroff
, sizeof (diroff
))) {
390 TIFFError(tif
->tif_name
, "Error writing directory link");
394 TIFFFreeDirectory(tif
);
395 tif
->tif_flags
&= ~TIFF_DIRTYDIRECT
;
396 (*tif
->tif_cleanup
)(tif
);
399 * Reset directory-related state for subsequent
402 TIFFCreateDirectory(tif
);
410 #undef WriteRationalPair
413 TIFFWriteDirectory(TIFF
* tif
)
415 return _TIFFWriteDirectory(tif
, TRUE
);
419 * Similar to TIFFWriteDirectory(), writes the directory out
420 * but leaves all data structures in memory so that it can be
421 * written again. This will make a partially written TIFF file
422 * readable before it is successfully completed/closed.
425 TIFFCheckpointDirectory(TIFF
* tif
)
428 /* Setup the strips arrays, if they haven't already been. */
429 if (tif
->tif_dir
.td_stripoffset
== NULL
)
430 (void) TIFFSetupStrips(tif
);
431 rc
= _TIFFWriteDirectory(tif
, FALSE
);
432 (void) TIFFSetWriteOffset(tif
, TIFFSeekFile(tif
, 0, SEEK_END
));
437 * Process tags that are not special cased.
440 TIFFWriteNormalTag(TIFF
* tif
, TIFFDirEntry
* dir
, const TIFFFieldInfo
* fip
)
442 u_short wc
= (u_short
) fip
->field_writecount
;
445 dir
->tdir_tag
= (uint16
) fip
->field_tag
;
446 dir
->tdir_type
= (u_short
) fip
->field_type
;
447 dir
->tdir_count
= wc
;
448 #define WRITEF(x,y) x(tif, fip->field_type, fip->field_tag, dir, wc, y)
449 switch (fip
->field_type
) {
454 if (wc
== (u_short
) TIFF_VARIABLE
455 || fip
->field_passcount
)
456 TIFFGetField(tif
, fip
->field_tag
, &wc
, &wp
);
458 TIFFGetField(tif
, fip
->field_tag
, &wp
);
459 if (!WRITEF(TIFFWriteShortArray
, wp
))
462 if (fip
->field_passcount
) {
464 TIFFGetField(tif
, fip
->field_tag
, &wc
, &wp
);
465 if (!WRITEF(TIFFWriteShortArray
, wp
))
469 TIFFGetField(tif
, fip
->field_tag
, &sv
);
471 TIFFInsertData(tif
, dir
->tdir_type
, sv
);
480 if (wc
== (u_short
) TIFF_VARIABLE
481 || fip
->field_passcount
)
482 TIFFGetField(tif
, fip
->field_tag
, &wc
, &lp
);
484 TIFFGetField(tif
, fip
->field_tag
, &lp
);
485 if (!WRITEF(TIFFWriteLongArray
, lp
))
488 if (fip
->field_passcount
) {
490 TIFFGetField(tif
, fip
->field_tag
, &wc
, &lp
);
491 if (!WRITEF(TIFFWriteLongArray
, lp
))
494 /* XXX handle LONG->SHORT conversion */
495 TIFFGetField(tif
, fip
->field_tag
,
504 if (wc
== (u_short
) TIFF_VARIABLE
505 || fip
->field_passcount
)
506 TIFFGetField(tif
, fip
->field_tag
, &wc
, &fp
);
508 TIFFGetField(tif
, fip
->field_tag
, &fp
);
509 if (!WRITEF(TIFFWriteRationalArray
, fp
))
512 if (fip
->field_passcount
) {
514 TIFFGetField(tif
, fip
->field_tag
, &wc
, &fp
);
515 if (!WRITEF(TIFFWriteRationalArray
, fp
))
519 TIFFGetField(tif
, fip
->field_tag
, &fv
);
520 if (!WRITEF(TIFFWriteRationalArray
, &fv
))
528 if (wc
== (u_short
) TIFF_VARIABLE
529 || fip
->field_passcount
)
530 TIFFGetField(tif
, fip
->field_tag
, &wc
, &fp
);
532 TIFFGetField(tif
, fip
->field_tag
, &fp
);
533 if (!WRITEF(TIFFWriteFloatArray
, fp
))
536 if (fip
->field_passcount
) {
538 TIFFGetField(tif
, fip
->field_tag
, &wc
, &fp
);
539 if (!WRITEF(TIFFWriteFloatArray
, fp
))
543 TIFFGetField(tif
, fip
->field_tag
, &fv
);
544 if (!WRITEF(TIFFWriteFloatArray
, &fv
))
552 if (wc
== (u_short
) TIFF_VARIABLE
553 || fip
->field_passcount
)
554 TIFFGetField(tif
, fip
->field_tag
, &wc
, &dp
);
556 TIFFGetField(tif
, fip
->field_tag
, &dp
);
557 if (!WRITEF(TIFFWriteDoubleArray
, dp
))
560 if (fip
->field_passcount
) {
562 TIFFGetField(tif
, fip
->field_tag
, &wc
, &dp
);
563 if (!WRITEF(TIFFWriteDoubleArray
, dp
))
567 TIFFGetField(tif
, fip
->field_tag
, &dv
);
568 if (!WRITEF(TIFFWriteDoubleArray
, &dv
))
575 TIFFGetField(tif
, fip
->field_tag
, &cp
);
576 dir
->tdir_count
= (uint32
) (strlen(cp
) + 1);
577 if (!TIFFWriteByteArray(tif
, dir
, cp
))
582 /* added based on patch request from MARTIN.MCBRIDE.MM@agfa.co.uk,
583 correctness not verified (FW, 99/08) */
588 if (wc
== (u_short
) TIFF_VARIABLE
589 || fip
->field_passcount
) {
590 TIFFGetField(tif
, fip
->field_tag
, &wc
, &cp
);
591 dir
->tdir_count
= wc
;
592 } else if (wc
== (u_short
) TIFF_VARIABLE2
) {
593 TIFFGetField(tif
, fip
->field_tag
, &wc2
, &cp
);
594 dir
->tdir_count
= wc2
;
596 TIFFGetField(tif
, fip
->field_tag
, &cp
);
597 if (!TIFFWriteByteArray(tif
, dir
, cp
))
600 if (fip
->field_passcount
) {
602 TIFFGetField(tif
, fip
->field_tag
, &wc
, &cp
);
603 dir
->tdir_count
= wc
;
604 if (!TIFFWriteByteArray(tif
, dir
, cp
))
608 TIFFGetField(tif
, fip
->field_tag
, &cv
);
609 if (!TIFFWriteByteArray(tif
, dir
, &cv
))
617 if (wc
== (u_short
) TIFF_VARIABLE
) {
618 TIFFGetField(tif
, fip
->field_tag
, &wc
, &cp
);
619 dir
->tdir_count
= wc
;
620 } else if (wc
== (u_short
) TIFF_VARIABLE2
) {
621 TIFFGetField(tif
, fip
->field_tag
, &wc2
, &cp
);
622 dir
->tdir_count
= wc2
;
624 TIFFGetField(tif
, fip
->field_tag
, &cp
);
625 if (!TIFFWriteByteArray(tif
, dir
, cp
))
638 * Setup a directory entry with either a SHORT
639 * or LONG type according to the value.
642 TIFFSetupShortLong(TIFF
* tif
, ttag_t tag
, TIFFDirEntry
* dir
, uint32 v
)
644 dir
->tdir_tag
= (uint16
) tag
;
647 dir
->tdir_type
= (short) TIFF_LONG
;
648 dir
->tdir_offset
= v
;
650 dir
->tdir_type
= (short) TIFF_SHORT
;
651 dir
->tdir_offset
= TIFFInsertData(tif
, (int) TIFF_SHORT
, v
);
656 * Setup a SHORT directory entry
659 TIFFSetupShort(TIFF
* tif
, ttag_t tag
, TIFFDirEntry
* dir
, uint16 v
)
661 dir
->tdir_tag
= (uint16
) tag
;
663 dir
->tdir_type
= (short) TIFF_SHORT
;
664 dir
->tdir_offset
= TIFFInsertData(tif
, (int) TIFF_SHORT
, v
);
666 #undef MakeShortDirent
668 #ifndef TIFFWriteRational
670 * Setup a RATIONAL directory entry and
671 * write the associated indirect value.
674 TIFFWriteRational(TIFF
* tif
,
675 TIFFDataType type
, ttag_t tag
, TIFFDirEntry
* dir
, float v
)
677 return (TIFFWriteRationalArray(tif
, type
, tag
, dir
, 1, &v
));
681 #define NITEMS(x) (sizeof (x) / sizeof (x[0]))
683 * Setup a directory entry that references a
684 * samples/pixel array of SHORT values and
685 * (potentially) write the associated indirect
689 TIFFWritePerSampleShorts(TIFF
* tif
, ttag_t tag
, TIFFDirEntry
* dir
)
693 int i
, status
, samples
= tif
->tif_dir
.td_samplesperpixel
;
695 if (samples
> NITEMS(buf
)) {
696 w
= (uint16
*) _TIFFmalloc(samples
* sizeof (uint16
));
698 TIFFError(tif
->tif_name
,
699 "No space to write per-sample shorts");
703 TIFFGetField(tif
, tag
, &v
);
704 for (i
= 0; i
< samples
; i
++)
706 status
= TIFFWriteShortArray(tif
, TIFF_SHORT
, tag
, dir
, samples
, w
);
708 _TIFFfree((char*) w
);
713 * Setup a directory entry that references a samples/pixel array of ``type''
714 * values and (potentially) write the associated indirect values. The source
715 * data from TIFFGetField() for the specified tag must be returned as double.
718 TIFFWritePerSampleAnys(TIFF
* tif
,
719 TIFFDataType type
, ttag_t tag
, TIFFDirEntry
* dir
)
724 int samples
= (int) tif
->tif_dir
.td_samplesperpixel
;
726 if (samples
> NITEMS(buf
)) {
727 w
= (double*) _TIFFmalloc(samples
* sizeof (double));
729 TIFFError(tif
->tif_name
,
730 "No space to write per-sample values");
734 TIFFGetField(tif
, tag
, &v
);
735 for (i
= 0; i
< samples
; i
++)
737 status
= TIFFWriteAnyArray(tif
, type
, tag
, dir
, samples
, w
);
745 * Setup a pair of shorts that are returned by
746 * value, rather than as a reference to an array.
749 TIFFSetupShortPair(TIFF
* tif
, ttag_t tag
, TIFFDirEntry
* dir
)
753 TIFFGetField(tif
, tag
, &v
[0], &v
[1]);
754 return (TIFFWriteShortArray(tif
, TIFF_SHORT
, tag
, dir
, 2, v
));
758 * Setup a directory entry for an NxM table of shorts,
759 * where M is known to be 2**bitspersample, and write
760 * the associated indirect data.
763 TIFFWriteShortTable(TIFF
* tif
,
764 ttag_t tag
, TIFFDirEntry
* dir
, uint32 n
, uint16
** table
)
768 dir
->tdir_tag
= (uint16
) tag
;
769 dir
->tdir_type
= (short) TIFF_SHORT
;
770 /* XXX -- yech, fool TIFFWriteData */
771 dir
->tdir_count
= (uint32
) (1L<<tif
->tif_dir
.td_bitspersample
);
772 off
= tif
->tif_dataoff
;
773 for (i
= 0; i
< n
; i
++)
774 if (!TIFFWriteData(tif
, dir
, (char *)table
[i
]))
776 dir
->tdir_count
*= n
;
777 dir
->tdir_offset
= off
;
782 * Write/copy data associated with an ASCII or opaque tag value.
785 TIFFWriteByteArray(TIFF
* tif
, TIFFDirEntry
* dir
, char* cp
)
787 if (dir
->tdir_count
> 4) {
788 if (!TIFFWriteData(tif
, dir
, cp
))
791 _TIFFmemcpy(&dir
->tdir_offset
, cp
, dir
->tdir_count
);
796 * Setup a directory entry of an array of SHORT
797 * or SSHORT and write the associated indirect values.
800 TIFFWriteShortArray(TIFF
* tif
,
801 TIFFDataType type
, ttag_t tag
, TIFFDirEntry
* dir
, uint32 n
, uint16
* v
)
803 dir
->tdir_tag
= (uint16
) tag
;
804 dir
->tdir_type
= (short) type
;
807 if (tif
->tif_header
.tiff_magic
== TIFF_BIGENDIAN
) {
808 dir
->tdir_offset
= (uint32
) ((long) v
[0] << 16);
810 dir
->tdir_offset
|= v
[1] & 0xffff;
812 dir
->tdir_offset
= v
[0] & 0xffff;
814 dir
->tdir_offset
|= (long) v
[1] << 16;
818 return (TIFFWriteData(tif
, dir
, (char*) v
));
822 * Setup a directory entry of an array of LONG
823 * or SLONG and write the associated indirect values.
826 TIFFWriteLongArray(TIFF
* tif
,
827 TIFFDataType type
, ttag_t tag
, TIFFDirEntry
* dir
, uint32 n
, uint32
* v
)
829 dir
->tdir_tag
= (uint16
) tag
;
830 dir
->tdir_type
= (short) type
;
833 dir
->tdir_offset
= v
[0];
836 return (TIFFWriteData(tif
, dir
, (char*) v
));
840 * Setup a directory entry of an array of RATIONAL
841 * or SRATIONAL and write the associated indirect values.
844 TIFFWriteRationalArray(TIFF
* tif
,
845 TIFFDataType type
, ttag_t tag
, TIFFDirEntry
* dir
, uint32 n
, float* v
)
851 dir
->tdir_tag
= (uint16
) tag
;
852 dir
->tdir_type
= (short) type
;
854 t
= (uint32
*) _TIFFmalloc(2*n
* sizeof (uint32
));
856 TIFFError(tif
->tif_name
,
857 "No space to write RATIONAL array");
860 for (i
= 0; i
< n
; i
++) {
866 if (type
== TIFF_RATIONAL
) {
867 TIFFWarning(tif
->tif_name
,
868 "\"%s\": Information lost writing value (%g) as (unsigned) RATIONAL",
869 _TIFFFieldWithTag(tif
,tag
)->field_name
, fv
);
876 while (fv
< 1L<<(31-3) && den
< 1L<<(31-3))
877 fv
*= 1<<3, den
*= 1L<<3;
879 t
[2*i
+0] = (uint32
) (sign
* (fv
+ 0.5));
882 status
= TIFFWriteData(tif
, dir
, (char *)t
);
883 _TIFFfree((char*) t
);
888 TIFFWriteFloatArray(TIFF
* tif
,
889 TIFFDataType type
, ttag_t tag
, TIFFDirEntry
* dir
, uint32 n
, float* v
)
891 dir
->tdir_tag
= (uint16
) tag
;
892 dir
->tdir_type
= (short) type
;
894 TIFFCvtNativeToIEEEFloat(tif
, n
, v
);
896 dir
->tdir_offset
= *(uint32
*) &v
[0];
899 return (TIFFWriteData(tif
, dir
, (char*) v
));
903 TIFFWriteDoubleArray(TIFF
* tif
,
904 TIFFDataType type
, ttag_t tag
, TIFFDirEntry
* dir
, uint32 n
, double* v
)
906 dir
->tdir_tag
= (uint16
) tag
;
907 dir
->tdir_type
= (short) type
;
909 TIFFCvtNativeToIEEEDouble(tif
, n
, v
);
910 return (TIFFWriteData(tif
, dir
, (char*) v
));
914 * Write an array of ``type'' values for a specified tag (i.e. this is a tag
915 * which is allowed to have different types, e.g. SMaxSampleType).
916 * Internally the data values are represented as double since a double can
917 * hold any of the TIFF tag types (yes, this should really be an abstract
918 * type tany_t for portability). The data is converted into the specified
919 * type in a temporary buffer and then handed off to the appropriate array
923 TIFFWriteAnyArray(TIFF
* tif
,
924 TIFFDataType type
, ttag_t tag
, TIFFDirEntry
* dir
, uint32 n
, double* v
)
926 char buf
[10 * sizeof(double)];
930 if (n
* TIFFDataWidth(type
) > sizeof buf
) {
931 w
= (char*) _TIFFmalloc(n
* TIFFDataWidth(type
));
933 TIFFError(tif
->tif_name
,
934 "No space to write array");
940 { uint8
* bp
= (uint8
*) w
;
941 for (i
= 0; i
< (int) n
; i
++)
942 bp
[i
] = (uint8
) v
[i
];
943 dir
->tdir_tag
= (uint16
) tag
;
944 dir
->tdir_type
= (short) type
;
946 if (!TIFFWriteByteArray(tif
, dir
, (char*) bp
))
951 { int8
* bp
= (int8
*) w
;
952 for (i
= 0; i
< (int) n
; i
++)
954 dir
->tdir_tag
= (uint16
) tag
;
955 dir
->tdir_type
= (short) type
;
957 if (!TIFFWriteByteArray(tif
, dir
, (char*) bp
))
962 { uint16
* bp
= (uint16
*) w
;
963 for (i
= 0; i
< (int) n
; i
++)
964 bp
[i
] = (uint16
) v
[i
];
965 if (!TIFFWriteShortArray(tif
, type
, tag
, dir
, n
, (uint16
*)bp
))
970 { int16
* bp
= (int16
*) w
;
971 for (i
= 0; i
< (int) n
; i
++)
972 bp
[i
] = (int16
) v
[i
];
973 if (!TIFFWriteShortArray(tif
, type
, tag
, dir
, n
, (uint16
*)bp
))
978 { uint32
* bp
= (uint32
*) w
;
979 for (i
= 0; i
< (int) n
; i
++)
980 bp
[i
] = (uint32
) v
[i
];
981 if (!TIFFWriteLongArray(tif
, type
, tag
, dir
, n
, bp
))
986 { int32
* bp
= (int32
*) w
;
987 for (i
= 0; i
< (int) n
; i
++)
988 bp
[i
] = (int32
) v
[i
];
989 if (!TIFFWriteLongArray(tif
, type
, tag
, dir
, n
, (uint32
*) bp
))
994 { float* bp
= (float*) w
;
995 for (i
= 0; i
< (int) n
; i
++)
996 bp
[i
] = (float) v
[i
];
997 if (!TIFFWriteFloatArray(tif
, type
, tag
, dir
, n
, bp
))
1002 return (TIFFWriteDoubleArray(tif
, type
, tag
, dir
, n
, v
));
1006 /* TIFF_UNDEFINED */
1008 /* TIFF_SRATIONAL */
1018 #ifdef COLORIMETRY_SUPPORT
1020 TIFFWriteTransferFunction(TIFF
* tif
, TIFFDirEntry
* dir
)
1022 TIFFDirectory
* td
= &tif
->tif_dir
;
1023 tsize_t n
= (1L<<td
->td_bitspersample
) * sizeof (uint16
);
1024 uint16
** tf
= td
->td_transferfunction
;
1028 * Check if the table can be written as a single column,
1029 * or if it must be written as 3 columns. Note that we
1030 * write a 3-column tag if there are 2 samples/pixel and
1031 * a single column of data won't suffice--hmm.
1033 switch (td
->td_samplesperpixel
- td
->td_extrasamples
) {
1034 default: if (_TIFFmemcmp(tf
[0], tf
[2], n
)) { ncols
= 3; break; }
1035 case 2: if (_TIFFmemcmp(tf
[0], tf
[1], n
)) { ncols
= 3; break; }
1036 case 1: case 0: ncols
= 1;
1038 return (TIFFWriteShortTable(tif
,
1039 TIFFTAG_TRANSFERFUNCTION
, dir
, ncols
, tf
));
1045 TIFFWriteInkNames(TIFF
* tif
, TIFFDirEntry
* dir
)
1047 TIFFDirectory
* td
= &tif
->tif_dir
;
1049 dir
->tdir_tag
= TIFFTAG_INKNAMES
;
1050 dir
->tdir_type
= (short) TIFF_ASCII
;
1051 dir
->tdir_count
= td
->td_inknameslen
;
1052 return (TIFFWriteByteArray(tif
, dir
, td
->td_inknames
));
1057 * Write a contiguous directory item.
1060 TIFFWriteData(TIFF
* tif
, TIFFDirEntry
* dir
, char* cp
)
1064 if (tif
->tif_flags
& TIFF_SWAB
) {
1065 switch (dir
->tdir_type
) {
1068 TIFFSwabArrayOfShort((uint16
*) cp
, dir
->tdir_count
);
1073 TIFFSwabArrayOfLong((uint32
*) cp
, dir
->tdir_count
);
1076 case TIFF_SRATIONAL
:
1077 TIFFSwabArrayOfLong((uint32
*) cp
, 2*dir
->tdir_count
);
1080 TIFFSwabArrayOfDouble((double*) cp
, dir
->tdir_count
);
1084 dir
->tdir_offset
= tif
->tif_dataoff
;
1085 cc
= dir
->tdir_count
* TIFFDataWidth((TIFFDataType
) dir
->tdir_type
);
1086 if (SeekOK(tif
, dir
->tdir_offset
) &&
1087 WriteOK(tif
, cp
, cc
)) {
1088 tif
->tif_dataoff
+= (cc
+ 1) & ~1;
1091 TIFFError(tif
->tif_name
, "Error writing data for field \"%s\"",
1092 _TIFFFieldWithTag(tif
, dir
->tdir_tag
)->field_name
);
1097 * Similar to TIFFWriteDirectory(), but if the directory has already
1098 * been written once, it is relocated to the end of the file, in case it
1099 * has changed in size. Note that this will result in the loss of the
1100 * previously used directory space.
1104 TIFFRewriteDirectory( TIFF
*tif
)
1106 static const char module[] = "TIFFRewriteDirectory";
1108 /* We don't need to do anything special if it hasn't been written. */
1109 if( tif
->tif_diroff
== 0 )
1110 return TIFFWriteDirectory( tif
);
1113 ** Find and zero the pointer to this directory, so that TIFFLinkDirectory
1114 ** will cause it to be added after this directories current pre-link.
1117 /* Is it the first directory in the file? */
1118 if (tif
->tif_header
.tiff_diroff
== tif
->tif_diroff
)
1120 tif
->tif_header
.tiff_diroff
= 0;
1121 tif
->tif_diroff
= 0;
1123 #if defined(__hpux) && defined(__LP64__)
1124 #define HDROFF(f) ((toff_t)(unsigned long) &(((TIFFHeader*) 0)->f))
1126 #define HDROFF(f) ((toff_t) &(((TIFFHeader*) 0)->f))
1128 TIFFSeekFile(tif
, HDROFF(tiff_diroff
), SEEK_SET
);
1129 if (!WriteOK(tif
, &(tif
->tif_header
.tiff_diroff
),
1130 sizeof (tif
->tif_diroff
)))
1132 TIFFError(tif
->tif_name
, "Error updating TIFF header");
1138 toff_t nextdir
, off
;
1140 nextdir
= tif
->tif_header
.tiff_diroff
;
1144 if (!SeekOK(tif
, nextdir
) ||
1145 !ReadOK(tif
, &dircount
, sizeof (dircount
))) {
1146 TIFFError(module, "Error fetching directory count");
1149 if (tif
->tif_flags
& TIFF_SWAB
)
1150 TIFFSwabShort(&dircount
);
1151 (void) TIFFSeekFile(tif
,
1152 dircount
* sizeof (TIFFDirEntry
), SEEK_CUR
);
1153 if (!ReadOK(tif
, &nextdir
, sizeof (nextdir
))) {
1154 TIFFError(module, "Error fetching directory link");
1157 if (tif
->tif_flags
& TIFF_SWAB
)
1158 TIFFSwabLong(&nextdir
);
1159 } while (nextdir
!= tif
->tif_diroff
&& nextdir
!= 0);
1160 off
= TIFFSeekFile(tif
, 0, SEEK_CUR
); /* get current offset */
1161 (void) TIFFSeekFile(tif
, off
- (toff_t
)sizeof(nextdir
), SEEK_SET
);
1162 tif
->tif_diroff
= 0;
1163 if (!WriteOK(tif
, &(tif
->tif_diroff
), sizeof (nextdir
))) {
1164 TIFFError(module, "Error writing directory link");
1170 ** Now use TIFFWriteDirectory() normally.
1173 return TIFFWriteDirectory( tif
);
1178 * Link the current directory into the
1179 * directory chain for the file.
1182 TIFFLinkDirectory(TIFF
* tif
)
1184 static const char module[] = "TIFFLinkDirectory";
1188 tif
->tif_diroff
= (TIFFSeekFile(tif
, (toff_t
) 0, SEEK_END
)+1) &~ 1;
1189 diroff
= tif
->tif_diroff
;
1190 if (tif
->tif_flags
& TIFF_SWAB
)
1191 TIFFSwabLong(&diroff
);
1193 if (tif
->tif_flags
& TIFF_INSUBIFD
) {
1194 (void) TIFFSeekFile(tif
, tif
->tif_subifdoff
, SEEK_SET
);
1195 if (!WriteOK(tif
, &diroff
, sizeof (diroff
))) {
1197 "%s: Error writing SubIFD directory link",
1202 * Advance to the next SubIFD or, if this is
1203 * the last one configured, revert back to the
1204 * normal directory linkage.
1206 if (--tif
->tif_nsubifd
)
1207 tif
->tif_subifdoff
+= sizeof (diroff
);
1209 tif
->tif_flags
&= ~TIFF_INSUBIFD
;
1213 if (tif
->tif_header
.tiff_diroff
== 0) {
1215 * First directory, overwrite offset in header.
1217 tif
->tif_header
.tiff_diroff
= tif
->tif_diroff
;
1218 #define HDROFF(f) ((toff_t) &(((TIFFHeader*) 0)->f))
1219 (void) TIFFSeekFile(tif
, HDROFF(tiff_diroff
), SEEK_SET
);
1220 if (!WriteOK(tif
, &diroff
, sizeof (diroff
))) {
1221 TIFFError(tif
->tif_name
, "Error writing TIFF header");
1227 * Not the first directory, search to the last and append.
1229 nextdir
= tif
->tif_header
.tiff_diroff
;
1233 if (!SeekOK(tif
, nextdir
) ||
1234 !ReadOK(tif
, &dircount
, sizeof (dircount
))) {
1235 TIFFError(module, "Error fetching directory count");
1238 if (tif
->tif_flags
& TIFF_SWAB
)
1239 TIFFSwabShort(&dircount
);
1240 (void) TIFFSeekFile(tif
,
1241 dircount
* sizeof (TIFFDirEntry
), SEEK_CUR
);
1242 if (!ReadOK(tif
, &nextdir
, sizeof (nextdir
))) {
1243 TIFFError(module, "Error fetching directory link");
1246 if (tif
->tif_flags
& TIFF_SWAB
)
1247 TIFFSwabLong(&nextdir
);
1248 } while (nextdir
!= 0);
1249 off
= TIFFSeekFile(tif
, 0, SEEK_CUR
); /* get current offset */
1250 (void) TIFFSeekFile(tif
, off
- (toff_t
)sizeof(nextdir
), SEEK_SET
);
1251 if (!WriteOK(tif
, &diroff
, sizeof (diroff
))) {
1252 TIFFError(module, "Error writing directory link");