/src/FreeImage/Source/LibTIFF/tif_dirwrite.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 1436 lines · 1083 code · 86 blank · 267 comment · 278 complexity · 98c8b22e58be25f2f87d871018ed9488 MD5 · raw file

  1. /* $Id: tif_dirwrite.c,v 1.37 2011/04/10 17:14:09 drolon Exp $ */
  2. /*
  3. * Copyright (c) 1988-1997 Sam Leffler
  4. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and
  7. * its documentation for any purpose is hereby granted without fee, provided
  8. * that (i) the above copyright notices and this permission notice appear in
  9. * all copies of the software and related documentation, and (ii) the names of
  10. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11. * publicity relating to the software without the specific, prior written
  12. * permission of Sam Leffler and Silicon Graphics.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  22. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. */
  25. /*
  26. * TIFF Library.
  27. *
  28. * Directory Write Support Routines.
  29. */
  30. #include "tiffiop.h"
  31. #ifdef HAVE_IEEEFP
  32. # define TIFFCvtNativeToIEEEFloat(tif, n, fp)
  33. # define TIFFCvtNativeToIEEEDouble(tif, n, dp)
  34. #else
  35. extern void TIFFCvtNativeToIEEEFloat(TIFF*, uint32, float*);
  36. extern void TIFFCvtNativeToIEEEDouble(TIFF*, uint32, double*);
  37. #endif
  38. static int TIFFWriteNormalTag(TIFF*, TIFFDirEntry*, const TIFFFieldInfo*);
  39. static void TIFFSetupShortLong(TIFF*, ttag_t, TIFFDirEntry*, uint32);
  40. static void TIFFSetupShort(TIFF*, ttag_t, TIFFDirEntry*, uint16);
  41. static int TIFFSetupBytePair(TIFF*, ttag_t, TIFFDirEntry*);
  42. static int TIFFSetupShortPair(TIFF*, ttag_t, TIFFDirEntry*);
  43. static int TIFFWritePerSampleShorts(TIFF*, ttag_t, TIFFDirEntry*);
  44. static int TIFFWritePerSampleAnys(TIFF*, TIFFDataType, ttag_t, TIFFDirEntry*);
  45. static int TIFFWriteShortTable(TIFF*, ttag_t, TIFFDirEntry*, uint32, uint16**);
  46. static int TIFFWriteShortArray(TIFF*, TIFFDirEntry*, uint16*);
  47. static int TIFFWriteLongArray(TIFF *, TIFFDirEntry*, uint32*);
  48. static int TIFFWriteRationalArray(TIFF *, TIFFDirEntry*, float*);
  49. static int TIFFWriteFloatArray(TIFF *, TIFFDirEntry*, float*);
  50. static int TIFFWriteDoubleArray(TIFF *, TIFFDirEntry*, double*);
  51. static int TIFFWriteByteArray(TIFF*, TIFFDirEntry*, char*);
  52. static int TIFFWriteAnyArray(TIFF*,
  53. TIFFDataType, ttag_t, TIFFDirEntry*, uint32, double*);
  54. static int TIFFWriteTransferFunction(TIFF*, TIFFDirEntry*);
  55. static int TIFFWriteInkNames(TIFF*, TIFFDirEntry*);
  56. static int TIFFWriteData(TIFF*, TIFFDirEntry*, char*);
  57. static int TIFFLinkDirectory(TIFF*);
  58. #define WriteRationalPair(type, tag1, v1, tag2, v2) { \
  59. TIFFWriteRational((tif), (type), (tag1), (dir), (v1)) \
  60. TIFFWriteRational((tif), (type), (tag2), (dir)+1, (v2)) \
  61. (dir)++; \
  62. }
  63. #define TIFFWriteRational(tif, type, tag, dir, v) \
  64. (dir)->tdir_tag = (tag); \
  65. (dir)->tdir_type = (type); \
  66. (dir)->tdir_count = 1; \
  67. if (!TIFFWriteRationalArray((tif), (dir), &(v))) \
  68. goto bad;
  69. /*
  70. * Write the contents of the current directory
  71. * to the specified file. This routine doesn't
  72. * handle overwriting a directory with auxiliary
  73. * storage that's been changed.
  74. */
  75. static int
  76. _TIFFWriteDirectory(TIFF* tif, int done)
  77. {
  78. uint16 dircount;
  79. toff_t diroff;
  80. ttag_t tag;
  81. uint32 nfields;
  82. tsize_t dirsize;
  83. char* data;
  84. TIFFDirEntry* dir;
  85. TIFFDirectory* td;
  86. unsigned long b, fields[FIELD_SETLONGS];
  87. int fi, nfi;
  88. if (tif->tif_mode == O_RDONLY)
  89. return (1);
  90. /*
  91. * Clear write state so that subsequent images with
  92. * different characteristics get the right buffers
  93. * setup for them.
  94. */
  95. if (done)
  96. {
  97. if (tif->tif_flags & TIFF_POSTENCODE) {
  98. tif->tif_flags &= ~TIFF_POSTENCODE;
  99. if (!(*tif->tif_postencode)(tif)) {
  100. TIFFErrorExt(tif->tif_clientdata,
  101. tif->tif_name,
  102. "Error post-encoding before directory write");
  103. return (0);
  104. }
  105. }
  106. (*tif->tif_close)(tif); /* shutdown encoder */
  107. /*
  108. * Flush any data that might have been written
  109. * by the compression close+cleanup routines.
  110. */
  111. if (tif->tif_rawcc > 0
  112. && (tif->tif_flags & TIFF_BEENWRITING) != 0
  113. && !TIFFFlushData1(tif)) {
  114. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  115. "Error flushing data before directory write");
  116. return (0);
  117. }
  118. if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
  119. _TIFFfree(tif->tif_rawdata);
  120. tif->tif_rawdata = NULL;
  121. tif->tif_rawcc = 0;
  122. tif->tif_rawdatasize = 0;
  123. }
  124. tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP);
  125. }
  126. td = &tif->tif_dir;
  127. /*
  128. * Size the directory so that we can calculate
  129. * offsets for the data items that aren't kept
  130. * in-place in each field.
  131. */
  132. nfields = 0;
  133. for (b = 0; b <= FIELD_LAST; b++)
  134. if (TIFFFieldSet(tif, b) && b != FIELD_CUSTOM)
  135. nfields += (b < FIELD_SUBFILETYPE ? 2 : 1);
  136. nfields += td->td_customValueCount;
  137. dirsize = nfields * sizeof (TIFFDirEntry);
  138. data = (char*) _TIFFmalloc(dirsize);
  139. if (data == NULL) {
  140. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  141. "Cannot write directory, out of space");
  142. return (0);
  143. }
  144. /*
  145. * Directory hasn't been placed yet, put
  146. * it at the end of the file and link it
  147. * into the existing directory structure.
  148. */
  149. if (tif->tif_diroff == 0 && !TIFFLinkDirectory(tif))
  150. goto bad;
  151. tif->tif_dataoff = (toff_t)(
  152. tif->tif_diroff + sizeof (uint16) + dirsize + sizeof (toff_t));
  153. if (tif->tif_dataoff & 1)
  154. tif->tif_dataoff++;
  155. (void) TIFFSeekFile(tif, tif->tif_dataoff, SEEK_SET);
  156. tif->tif_curdir++;
  157. dir = (TIFFDirEntry*) data;
  158. /*
  159. * Setup external form of directory
  160. * entries and write data items.
  161. */
  162. _TIFFmemcpy(fields, td->td_fieldsset, sizeof (fields));
  163. /*
  164. * Write out ExtraSamples tag only if
  165. * extra samples are present in the data.
  166. */
  167. if (FieldSet(fields, FIELD_EXTRASAMPLES) && !td->td_extrasamples) {
  168. ResetFieldBit(fields, FIELD_EXTRASAMPLES);
  169. nfields--;
  170. dirsize -= sizeof (TIFFDirEntry);
  171. } /*XXX*/
  172. for (fi = 0, nfi = tif->tif_nfields; nfi > 0; nfi--, fi++) {
  173. const TIFFFieldInfo* fip = tif->tif_fieldinfo[fi];
  174. /*
  175. * For custom fields, we test to see if the custom field
  176. * is set or not. For normal fields, we just use the
  177. * FieldSet test.
  178. */
  179. if( fip->field_bit == FIELD_CUSTOM )
  180. {
  181. int ci, is_set = FALSE;
  182. for( ci = 0; ci < td->td_customValueCount; ci++ )
  183. is_set |= (td->td_customValues[ci].info == fip);
  184. if( !is_set )
  185. continue;
  186. }
  187. else if (!FieldSet(fields, fip->field_bit))
  188. continue;
  189. /*
  190. * Handle other fields.
  191. */
  192. switch (fip->field_bit)
  193. {
  194. case FIELD_STRIPOFFSETS:
  195. /*
  196. * We use one field bit for both strip and tile
  197. * offsets, and so must be careful in selecting
  198. * the appropriate field descriptor (so that tags
  199. * are written in sorted order).
  200. */
  201. tag = isTiled(tif) ?
  202. TIFFTAG_TILEOFFSETS : TIFFTAG_STRIPOFFSETS;
  203. if (tag != fip->field_tag)
  204. continue;
  205. dir->tdir_tag = (uint16) tag;
  206. dir->tdir_type = (uint16) TIFF_LONG;
  207. dir->tdir_count = (uint32) td->td_nstrips;
  208. if (!TIFFWriteLongArray(tif, dir, td->td_stripoffset))
  209. goto bad;
  210. break;
  211. case FIELD_STRIPBYTECOUNTS:
  212. /*
  213. * We use one field bit for both strip and tile
  214. * byte counts, and so must be careful in selecting
  215. * the appropriate field descriptor (so that tags
  216. * are written in sorted order).
  217. */
  218. tag = isTiled(tif) ?
  219. TIFFTAG_TILEBYTECOUNTS : TIFFTAG_STRIPBYTECOUNTS;
  220. if (tag != fip->field_tag)
  221. continue;
  222. dir->tdir_tag = (uint16) tag;
  223. dir->tdir_type = (uint16) TIFF_LONG;
  224. dir->tdir_count = (uint32) td->td_nstrips;
  225. if (!TIFFWriteLongArray(tif, dir, td->td_stripbytecount))
  226. goto bad;
  227. break;
  228. case FIELD_ROWSPERSTRIP:
  229. TIFFSetupShortLong(tif, TIFFTAG_ROWSPERSTRIP,
  230. dir, td->td_rowsperstrip);
  231. break;
  232. case FIELD_COLORMAP:
  233. if (!TIFFWriteShortTable(tif, TIFFTAG_COLORMAP, dir,
  234. 3, td->td_colormap))
  235. goto bad;
  236. break;
  237. case FIELD_IMAGEDIMENSIONS:
  238. TIFFSetupShortLong(tif, TIFFTAG_IMAGEWIDTH,
  239. dir++, td->td_imagewidth);
  240. TIFFSetupShortLong(tif, TIFFTAG_IMAGELENGTH,
  241. dir, td->td_imagelength);
  242. break;
  243. case FIELD_TILEDIMENSIONS:
  244. TIFFSetupShortLong(tif, TIFFTAG_TILEWIDTH,
  245. dir++, td->td_tilewidth);
  246. TIFFSetupShortLong(tif, TIFFTAG_TILELENGTH,
  247. dir, td->td_tilelength);
  248. break;
  249. case FIELD_COMPRESSION:
  250. TIFFSetupShort(tif, TIFFTAG_COMPRESSION,
  251. dir, td->td_compression);
  252. break;
  253. case FIELD_PHOTOMETRIC:
  254. TIFFSetupShort(tif, TIFFTAG_PHOTOMETRIC,
  255. dir, td->td_photometric);
  256. break;
  257. case FIELD_POSITION:
  258. WriteRationalPair(TIFF_RATIONAL,
  259. TIFFTAG_XPOSITION, td->td_xposition,
  260. TIFFTAG_YPOSITION, td->td_yposition);
  261. break;
  262. case FIELD_RESOLUTION:
  263. WriteRationalPair(TIFF_RATIONAL,
  264. TIFFTAG_XRESOLUTION, td->td_xresolution,
  265. TIFFTAG_YRESOLUTION, td->td_yresolution);
  266. break;
  267. case FIELD_BITSPERSAMPLE:
  268. case FIELD_MINSAMPLEVALUE:
  269. case FIELD_MAXSAMPLEVALUE:
  270. case FIELD_SAMPLEFORMAT:
  271. if (!TIFFWritePerSampleShorts(tif, fip->field_tag, dir))
  272. goto bad;
  273. break;
  274. case FIELD_SMINSAMPLEVALUE:
  275. case FIELD_SMAXSAMPLEVALUE:
  276. if (!TIFFWritePerSampleAnys(tif,
  277. _TIFFSampleToTagType(tif), fip->field_tag, dir))
  278. goto bad;
  279. break;
  280. case FIELD_INKNAMES:
  281. if (!TIFFWriteInkNames(tif, dir))
  282. goto bad;
  283. break;
  284. case FIELD_TRANSFERFUNCTION:
  285. if (!TIFFWriteTransferFunction(tif, dir))
  286. goto bad;
  287. break;
  288. case FIELD_SUBIFD:
  289. /*
  290. * XXX: Always write this field using LONG type
  291. * for backward compatibility.
  292. */
  293. dir->tdir_tag = (uint16) fip->field_tag;
  294. dir->tdir_type = (uint16) TIFF_LONG;
  295. dir->tdir_count = (uint32) td->td_nsubifd;
  296. if (!TIFFWriteLongArray(tif, dir, td->td_subifd))
  297. goto bad;
  298. /*
  299. * Total hack: if this directory includes a SubIFD
  300. * tag then force the next <n> directories to be
  301. * written as ``sub directories'' of this one. This
  302. * is used to write things like thumbnails and
  303. * image masks that one wants to keep out of the
  304. * normal directory linkage access mechanism.
  305. */
  306. if (dir->tdir_count > 0) {
  307. tif->tif_flags |= TIFF_INSUBIFD;
  308. tif->tif_nsubifd = (uint16) dir->tdir_count;
  309. if (dir->tdir_count > 1)
  310. tif->tif_subifdoff = dir->tdir_offset;
  311. else
  312. tif->tif_subifdoff = (uint32)(
  313. tif->tif_diroff
  314. + sizeof (uint16)
  315. + ((char*)&dir->tdir_offset-data));
  316. }
  317. break;
  318. default:
  319. /*
  320. * XXX: Should be fixed and removed. See comments
  321. * related to these tags in tif_dir.c.
  322. */
  323. if (fip->field_tag == TIFFTAG_PAGENUMBER
  324. || fip->field_tag == TIFFTAG_HALFTONEHINTS
  325. || fip->field_tag == TIFFTAG_YCBCRSUBSAMPLING
  326. || fip->field_tag == TIFFTAG_DOTRANGE) {
  327. if (fip->field_type == TIFF_BYTE) {
  328. if (!TIFFSetupBytePair(tif, fip->field_tag, dir))
  329. goto bad;
  330. } else if (fip->field_type == TIFF_SHORT) {
  331. if (!TIFFSetupShortPair(tif, fip->field_tag, dir))
  332. goto bad;
  333. }
  334. } else if (!TIFFWriteNormalTag(tif, dir, fip))
  335. goto bad;
  336. break;
  337. }
  338. dir++;
  339. if( fip->field_bit != FIELD_CUSTOM )
  340. ResetFieldBit(fields, fip->field_bit);
  341. }
  342. /*
  343. * Write directory.
  344. */
  345. dircount = (uint16) nfields;
  346. diroff = (uint32) tif->tif_nextdiroff;
  347. if (tif->tif_flags & TIFF_SWAB) {
  348. /*
  349. * The file's byte order is opposite to the
  350. * native machine architecture. We overwrite
  351. * the directory information with impunity
  352. * because it'll be released below after we
  353. * write it to the file. Note that all the
  354. * other tag construction routines assume that
  355. * we do this byte-swapping; i.e. they only
  356. * byte-swap indirect data.
  357. */
  358. for (dir = (TIFFDirEntry*) data; dircount; dir++, dircount--) {
  359. TIFFSwabArrayOfShort(&dir->tdir_tag, 2);
  360. TIFFSwabArrayOfLong(&dir->tdir_count, 2);
  361. }
  362. dircount = (uint16) nfields;
  363. TIFFSwabShort(&dircount);
  364. TIFFSwabLong(&diroff);
  365. }
  366. (void) TIFFSeekFile(tif, tif->tif_diroff, SEEK_SET);
  367. if (!WriteOK(tif, &dircount, sizeof (dircount))) {
  368. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  369. "Error writing directory count");
  370. goto bad;
  371. }
  372. if (!WriteOK(tif, data, dirsize)) {
  373. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  374. "Error writing directory contents");
  375. goto bad;
  376. }
  377. if (!WriteOK(tif, &diroff, sizeof (uint32))) {
  378. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  379. "Error writing directory link");
  380. goto bad;
  381. }
  382. if (done) {
  383. TIFFFreeDirectory(tif);
  384. tif->tif_flags &= ~TIFF_DIRTYDIRECT;
  385. (*tif->tif_cleanup)(tif);
  386. /*
  387. * Reset directory-related state for subsequent
  388. * directories.
  389. */
  390. TIFFCreateDirectory(tif);
  391. }
  392. _TIFFfree(data);
  393. return (1);
  394. bad:
  395. _TIFFfree(data);
  396. return (0);
  397. }
  398. #undef WriteRationalPair
  399. int
  400. TIFFWriteDirectory(TIFF* tif)
  401. {
  402. return _TIFFWriteDirectory(tif, TRUE);
  403. }
  404. /*
  405. * Similar to TIFFWriteDirectory(), writes the directory out
  406. * but leaves all data structures in memory so that it can be
  407. * written again. This will make a partially written TIFF file
  408. * readable before it is successfully completed/closed.
  409. */
  410. int
  411. TIFFCheckpointDirectory(TIFF* tif)
  412. {
  413. int rc;
  414. /* Setup the strips arrays, if they haven't already been. */
  415. if (tif->tif_dir.td_stripoffset == NULL)
  416. (void) TIFFSetupStrips(tif);
  417. rc = _TIFFWriteDirectory(tif, FALSE);
  418. (void) TIFFSetWriteOffset(tif, TIFFSeekFile(tif, 0, SEEK_END));
  419. return rc;
  420. }
  421. static int
  422. _TIFFWriteCustomDirectory(TIFF* tif, toff_t *pdiroff)
  423. {
  424. uint16 dircount;
  425. uint32 nfields;
  426. tsize_t dirsize;
  427. char* data;
  428. TIFFDirEntry* dir;
  429. TIFFDirectory* td;
  430. unsigned long b, fields[FIELD_SETLONGS];
  431. int fi, nfi;
  432. if (tif->tif_mode == O_RDONLY)
  433. return (1);
  434. td = &tif->tif_dir;
  435. /*
  436. * Size the directory so that we can calculate
  437. * offsets for the data items that aren't kept
  438. * in-place in each field.
  439. */
  440. nfields = 0;
  441. for (b = 0; b <= FIELD_LAST; b++)
  442. if (TIFFFieldSet(tif, b) && b != FIELD_CUSTOM)
  443. nfields += (b < FIELD_SUBFILETYPE ? 2 : 1);
  444. nfields += td->td_customValueCount;
  445. dirsize = nfields * sizeof (TIFFDirEntry);
  446. data = (char*) _TIFFmalloc(dirsize);
  447. if (data == NULL) {
  448. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  449. "Cannot write directory, out of space");
  450. return (0);
  451. }
  452. /*
  453. * Put the directory at the end of the file.
  454. */
  455. tif->tif_diroff = (TIFFSeekFile(tif, (toff_t) 0, SEEK_END)+1) &~ 1;
  456. tif->tif_dataoff = (toff_t)(
  457. tif->tif_diroff + sizeof (uint16) + dirsize + sizeof (toff_t));
  458. if (tif->tif_dataoff & 1)
  459. tif->tif_dataoff++;
  460. (void) TIFFSeekFile(tif, tif->tif_dataoff, SEEK_SET);
  461. dir = (TIFFDirEntry*) data;
  462. /*
  463. * Setup external form of directory
  464. * entries and write data items.
  465. */
  466. _TIFFmemcpy(fields, td->td_fieldsset, sizeof (fields));
  467. for (fi = 0, nfi = tif->tif_nfields; nfi > 0; nfi--, fi++) {
  468. const TIFFFieldInfo* fip = tif->tif_fieldinfo[fi];
  469. /*
  470. * For custom fields, we test to see if the custom field
  471. * is set or not. For normal fields, we just use the
  472. * FieldSet test.
  473. */
  474. if( fip->field_bit == FIELD_CUSTOM )
  475. {
  476. int ci, is_set = FALSE;
  477. for( ci = 0; ci < td->td_customValueCount; ci++ )
  478. is_set |= (td->td_customValues[ci].info == fip);
  479. if( !is_set )
  480. continue;
  481. }
  482. else if (!FieldSet(fields, fip->field_bit))
  483. continue;
  484. if( fip->field_bit != FIELD_CUSTOM )
  485. ResetFieldBit(fields, fip->field_bit);
  486. }
  487. /*
  488. * Write directory.
  489. */
  490. dircount = (uint16) nfields;
  491. *pdiroff = (uint32) tif->tif_nextdiroff;
  492. if (tif->tif_flags & TIFF_SWAB) {
  493. /*
  494. * The file's byte order is opposite to the
  495. * native machine architecture. We overwrite
  496. * the directory information with impunity
  497. * because it'll be released below after we
  498. * write it to the file. Note that all the
  499. * other tag construction routines assume that
  500. * we do this byte-swapping; i.e. they only
  501. * byte-swap indirect data.
  502. */
  503. for (dir = (TIFFDirEntry*) data; dircount; dir++, dircount--) {
  504. TIFFSwabArrayOfShort(&dir->tdir_tag, 2);
  505. TIFFSwabArrayOfLong(&dir->tdir_count, 2);
  506. }
  507. dircount = (uint16) nfields;
  508. TIFFSwabShort(&dircount);
  509. TIFFSwabLong(pdiroff);
  510. }
  511. (void) TIFFSeekFile(tif, tif->tif_diroff, SEEK_SET);
  512. if (!WriteOK(tif, &dircount, sizeof (dircount))) {
  513. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  514. "Error writing directory count");
  515. goto bad;
  516. }
  517. if (!WriteOK(tif, data, dirsize)) {
  518. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  519. "Error writing directory contents");
  520. goto bad;
  521. }
  522. if (!WriteOK(tif, pdiroff, sizeof (uint32))) {
  523. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  524. "Error writing directory link");
  525. goto bad;
  526. }
  527. _TIFFfree(data);
  528. return (1);
  529. bad:
  530. _TIFFfree(data);
  531. return (0);
  532. }
  533. int
  534. TIFFWriteCustomDirectory(TIFF* tif, toff_t *pdiroff)
  535. {
  536. return _TIFFWriteCustomDirectory(tif, pdiroff);
  537. }
  538. /*
  539. * Process tags that are not special cased.
  540. */
  541. static int
  542. TIFFWriteNormalTag(TIFF* tif, TIFFDirEntry* dir, const TIFFFieldInfo* fip)
  543. {
  544. uint16 wc = (uint16) fip->field_writecount;
  545. uint32 wc2;
  546. dir->tdir_tag = (uint16) fip->field_tag;
  547. dir->tdir_type = (uint16) fip->field_type;
  548. dir->tdir_count = wc;
  549. switch (fip->field_type) {
  550. case TIFF_SHORT:
  551. case TIFF_SSHORT:
  552. if (fip->field_passcount) {
  553. uint16* wp;
  554. if (wc == (uint16) TIFF_VARIABLE2) {
  555. TIFFGetField(tif, fip->field_tag, &wc2, &wp);
  556. dir->tdir_count = wc2;
  557. } else { /* Assume TIFF_VARIABLE */
  558. TIFFGetField(tif, fip->field_tag, &wc, &wp);
  559. dir->tdir_count = wc;
  560. }
  561. if (!TIFFWriteShortArray(tif, dir, wp))
  562. return 0;
  563. } else {
  564. if (wc == 1) {
  565. uint16 sv;
  566. TIFFGetField(tif, fip->field_tag, &sv);
  567. dir->tdir_offset =
  568. TIFFInsertData(tif, dir->tdir_type, sv);
  569. } else {
  570. uint16* wp;
  571. TIFFGetField(tif, fip->field_tag, &wp);
  572. if (!TIFFWriteShortArray(tif, dir, wp))
  573. return 0;
  574. }
  575. }
  576. break;
  577. case TIFF_LONG:
  578. case TIFF_SLONG:
  579. case TIFF_IFD:
  580. if (fip->field_passcount) {
  581. uint32* lp;
  582. if (wc == (uint16) TIFF_VARIABLE2) {
  583. TIFFGetField(tif, fip->field_tag, &wc2, &lp);
  584. dir->tdir_count = wc2;
  585. } else { /* Assume TIFF_VARIABLE */
  586. TIFFGetField(tif, fip->field_tag, &wc, &lp);
  587. dir->tdir_count = wc;
  588. }
  589. if (!TIFFWriteLongArray(tif, dir, lp))
  590. return 0;
  591. } else {
  592. if (wc == 1) {
  593. /* XXX handle LONG->SHORT conversion */
  594. TIFFGetField(tif, fip->field_tag,
  595. &dir->tdir_offset);
  596. } else {
  597. uint32* lp;
  598. TIFFGetField(tif, fip->field_tag, &lp);
  599. if (!TIFFWriteLongArray(tif, dir, lp))
  600. return 0;
  601. }
  602. }
  603. break;
  604. case TIFF_RATIONAL:
  605. case TIFF_SRATIONAL:
  606. if (fip->field_passcount) {
  607. float* fp;
  608. if (wc == (uint16) TIFF_VARIABLE2) {
  609. TIFFGetField(tif, fip->field_tag, &wc2, &fp);
  610. dir->tdir_count = wc2;
  611. } else { /* Assume TIFF_VARIABLE */
  612. TIFFGetField(tif, fip->field_tag, &wc, &fp);
  613. dir->tdir_count = wc;
  614. }
  615. if (!TIFFWriteRationalArray(tif, dir, fp))
  616. return 0;
  617. } else {
  618. if (wc == 1) {
  619. float fv;
  620. TIFFGetField(tif, fip->field_tag, &fv);
  621. if (!TIFFWriteRationalArray(tif, dir, &fv))
  622. return 0;
  623. } else {
  624. float* fp;
  625. TIFFGetField(tif, fip->field_tag, &fp);
  626. if (!TIFFWriteRationalArray(tif, dir, fp))
  627. return 0;
  628. }
  629. }
  630. break;
  631. case TIFF_FLOAT:
  632. if (fip->field_passcount) {
  633. float* fp;
  634. if (wc == (uint16) TIFF_VARIABLE2) {
  635. TIFFGetField(tif, fip->field_tag, &wc2, &fp);
  636. dir->tdir_count = wc2;
  637. } else { /* Assume TIFF_VARIABLE */
  638. TIFFGetField(tif, fip->field_tag, &wc, &fp);
  639. dir->tdir_count = wc;
  640. }
  641. if (!TIFFWriteFloatArray(tif, dir, fp))
  642. return 0;
  643. } else {
  644. if (wc == 1) {
  645. float fv;
  646. TIFFGetField(tif, fip->field_tag, &fv);
  647. if (!TIFFWriteFloatArray(tif, dir, &fv))
  648. return 0;
  649. } else {
  650. float* fp;
  651. TIFFGetField(tif, fip->field_tag, &fp);
  652. if (!TIFFWriteFloatArray(tif, dir, fp))
  653. return 0;
  654. }
  655. }
  656. break;
  657. case TIFF_DOUBLE:
  658. if (fip->field_passcount) {
  659. double* dp;
  660. if (wc == (uint16) TIFF_VARIABLE2) {
  661. TIFFGetField(tif, fip->field_tag, &wc2, &dp);
  662. dir->tdir_count = wc2;
  663. } else { /* Assume TIFF_VARIABLE */
  664. TIFFGetField(tif, fip->field_tag, &wc, &dp);
  665. dir->tdir_count = wc;
  666. }
  667. if (!TIFFWriteDoubleArray(tif, dir, dp))
  668. return 0;
  669. } else {
  670. if (wc == 1) {
  671. double dv;
  672. TIFFGetField(tif, fip->field_tag, &dv);
  673. if (!TIFFWriteDoubleArray(tif, dir, &dv))
  674. return 0;
  675. } else {
  676. double* dp;
  677. TIFFGetField(tif, fip->field_tag, &dp);
  678. if (!TIFFWriteDoubleArray(tif, dir, dp))
  679. return 0;
  680. }
  681. }
  682. break;
  683. case TIFF_ASCII:
  684. {
  685. char* cp;
  686. if (fip->field_passcount)
  687. {
  688. if( wc == (uint16) TIFF_VARIABLE2 )
  689. TIFFGetField(tif, fip->field_tag, &wc2, &cp);
  690. else
  691. TIFFGetField(tif, fip->field_tag, &wc, &cp);
  692. }
  693. else
  694. TIFFGetField(tif, fip->field_tag, &cp);
  695. dir->tdir_count = (uint32) (strlen(cp) + 1);
  696. if (!TIFFWriteByteArray(tif, dir, cp))
  697. return (0);
  698. }
  699. break;
  700. case TIFF_BYTE:
  701. case TIFF_SBYTE:
  702. if (fip->field_passcount) {
  703. char* cp;
  704. if (wc == (uint16) TIFF_VARIABLE2) {
  705. TIFFGetField(tif, fip->field_tag, &wc2, &cp);
  706. dir->tdir_count = wc2;
  707. } else { /* Assume TIFF_VARIABLE */
  708. TIFFGetField(tif, fip->field_tag, &wc, &cp);
  709. dir->tdir_count = wc;
  710. }
  711. if (!TIFFWriteByteArray(tif, dir, cp))
  712. return 0;
  713. } else {
  714. if (wc == 1) {
  715. char cv;
  716. TIFFGetField(tif, fip->field_tag, &cv);
  717. if (!TIFFWriteByteArray(tif, dir, &cv))
  718. return 0;
  719. } else {
  720. char* cp;
  721. TIFFGetField(tif, fip->field_tag, &cp);
  722. if (!TIFFWriteByteArray(tif, dir, cp))
  723. return 0;
  724. }
  725. }
  726. break;
  727. case TIFF_UNDEFINED:
  728. { char* cp;
  729. if (wc == (unsigned short) TIFF_VARIABLE) {
  730. TIFFGetField(tif, fip->field_tag, &wc, &cp);
  731. dir->tdir_count = wc;
  732. } else if (wc == (unsigned short) TIFF_VARIABLE2) {
  733. TIFFGetField(tif, fip->field_tag, &wc2, &cp);
  734. dir->tdir_count = wc2;
  735. } else
  736. TIFFGetField(tif, fip->field_tag, &cp);
  737. if (!TIFFWriteByteArray(tif, dir, cp))
  738. return (0);
  739. }
  740. break;
  741. case TIFF_NOTYPE:
  742. break;
  743. }
  744. return (1);
  745. }
  746. /*
  747. * Setup a directory entry with either a SHORT
  748. * or LONG type according to the value.
  749. */
  750. static void
  751. TIFFSetupShortLong(TIFF* tif, ttag_t tag, TIFFDirEntry* dir, uint32 v)
  752. {
  753. dir->tdir_tag = (uint16) tag;
  754. dir->tdir_count = 1;
  755. if (v > 0xffffL) {
  756. dir->tdir_type = (short) TIFF_LONG;
  757. dir->tdir_offset = v;
  758. } else {
  759. dir->tdir_type = (short) TIFF_SHORT;
  760. dir->tdir_offset = TIFFInsertData(tif, (int) TIFF_SHORT, v);
  761. }
  762. }
  763. /*
  764. * Setup a SHORT directory entry
  765. */
  766. static void
  767. TIFFSetupShort(TIFF* tif, ttag_t tag, TIFFDirEntry* dir, uint16 v)
  768. {
  769. dir->tdir_tag = (uint16) tag;
  770. dir->tdir_count = 1;
  771. dir->tdir_type = (short) TIFF_SHORT;
  772. dir->tdir_offset = TIFFInsertData(tif, (int) TIFF_SHORT, v);
  773. }
  774. #undef MakeShortDirent
  775. #define NITEMS(x) (sizeof (x) / sizeof (x[0]))
  776. /*
  777. * Setup a directory entry that references a
  778. * samples/pixel array of SHORT values and
  779. * (potentially) write the associated indirect
  780. * values.
  781. */
  782. static int
  783. TIFFWritePerSampleShorts(TIFF* tif, ttag_t tag, TIFFDirEntry* dir)
  784. {
  785. uint16 buf[10], v;
  786. uint16* w = buf;
  787. uint16 i, samples = tif->tif_dir.td_samplesperpixel;
  788. int status;
  789. if (samples > NITEMS(buf)) {
  790. w = (uint16*) _TIFFmalloc(samples * sizeof (uint16));
  791. if (w == NULL) {
  792. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  793. "No space to write per-sample shorts");
  794. return (0);
  795. }
  796. }
  797. TIFFGetField(tif, tag, &v);
  798. for (i = 0; i < samples; i++)
  799. w[i] = v;
  800. dir->tdir_tag = (uint16) tag;
  801. dir->tdir_type = (uint16) TIFF_SHORT;
  802. dir->tdir_count = samples;
  803. status = TIFFWriteShortArray(tif, dir, w);
  804. if (w != buf)
  805. _TIFFfree((char*) w);
  806. return (status);
  807. }
  808. /*
  809. * Setup a directory entry that references a samples/pixel array of ``type''
  810. * values and (potentially) write the associated indirect values. The source
  811. * data from TIFFGetField() for the specified tag must be returned as double.
  812. */
  813. static int
  814. TIFFWritePerSampleAnys(TIFF* tif,
  815. TIFFDataType type, ttag_t tag, TIFFDirEntry* dir)
  816. {
  817. double buf[10], v;
  818. double* w = buf;
  819. uint16 i, samples = tif->tif_dir.td_samplesperpixel;
  820. int status;
  821. if (samples > NITEMS(buf)) {
  822. w = (double*) _TIFFmalloc(samples * sizeof (double));
  823. if (w == NULL) {
  824. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  825. "No space to write per-sample values");
  826. return (0);
  827. }
  828. }
  829. TIFFGetField(tif, tag, &v);
  830. for (i = 0; i < samples; i++)
  831. w[i] = v;
  832. status = TIFFWriteAnyArray(tif, type, tag, dir, samples, w);
  833. if (w != buf)
  834. _TIFFfree(w);
  835. return (status);
  836. }
  837. #undef NITEMS
  838. /*
  839. * Setup a pair of bytes that are returned by
  840. * value, rather than as a reference to an array.
  841. */
  842. static int
  843. TIFFSetupBytePair(TIFF* tif, ttag_t tag, TIFFDirEntry* dir)
  844. {
  845. char v[2];
  846. TIFFGetField(tif, tag, &v[0], &v[1]);
  847. dir->tdir_tag = (uint16) tag;
  848. dir->tdir_type = (uint16) TIFF_BYTE;
  849. dir->tdir_count = 2;
  850. return (TIFFWriteByteArray(tif, dir, v));
  851. }
  852. /*
  853. * Setup a pair of shorts that are returned by
  854. * value, rather than as a reference to an array.
  855. */
  856. static int
  857. TIFFSetupShortPair(TIFF* tif, ttag_t tag, TIFFDirEntry* dir)
  858. {
  859. uint16 v[2];
  860. TIFFGetField(tif, tag, &v[0], &v[1]);
  861. dir->tdir_tag = (uint16) tag;
  862. dir->tdir_type = (uint16) TIFF_SHORT;
  863. dir->tdir_count = 2;
  864. return (TIFFWriteShortArray(tif, dir, v));
  865. }
  866. /*
  867. * Setup a directory entry for an NxM table of shorts,
  868. * where M is known to be 2**bitspersample, and write
  869. * the associated indirect data.
  870. */
  871. static int
  872. TIFFWriteShortTable(TIFF* tif,
  873. ttag_t tag, TIFFDirEntry* dir, uint32 n, uint16** table)
  874. {
  875. uint32 i, off;
  876. dir->tdir_tag = (uint16) tag;
  877. dir->tdir_type = (short) TIFF_SHORT;
  878. /* XXX -- yech, fool TIFFWriteData */
  879. dir->tdir_count = (uint32) (1L<<tif->tif_dir.td_bitspersample);
  880. off = tif->tif_dataoff;
  881. for (i = 0; i < n; i++)
  882. if (!TIFFWriteData(tif, dir, (char *)table[i]))
  883. return (0);
  884. dir->tdir_count *= n;
  885. dir->tdir_offset = off;
  886. return (1);
  887. }
  888. /*
  889. * Write/copy data associated with an ASCII or opaque tag value.
  890. */
  891. static int
  892. TIFFWriteByteArray(TIFF* tif, TIFFDirEntry* dir, char* cp)
  893. {
  894. if (dir->tdir_count <= 4) {
  895. if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) {
  896. dir->tdir_offset = (uint32)cp[0] << 24;
  897. if (dir->tdir_count >= 2)
  898. dir->tdir_offset |= (uint32)cp[1] << 16;
  899. if (dir->tdir_count >= 3)
  900. dir->tdir_offset |= (uint32)cp[2] << 8;
  901. if (dir->tdir_count == 4)
  902. dir->tdir_offset |= cp[3];
  903. } else {
  904. dir->tdir_offset = cp[0];
  905. if (dir->tdir_count >= 2)
  906. dir->tdir_offset |= (uint32) cp[1] << 8;
  907. if (dir->tdir_count >= 3)
  908. dir->tdir_offset |= (uint32) cp[2] << 16;
  909. if (dir->tdir_count == 4)
  910. dir->tdir_offset |= (uint32) cp[3] << 24;
  911. }
  912. return 1;
  913. } else
  914. return TIFFWriteData(tif, dir, cp);
  915. }
  916. /*
  917. * Setup a directory entry of an array of SHORT
  918. * or SSHORT and write the associated indirect values.
  919. */
  920. static int
  921. TIFFWriteShortArray(TIFF* tif, TIFFDirEntry* dir, uint16* v)
  922. {
  923. if (dir->tdir_count <= 2) {
  924. if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) {
  925. dir->tdir_offset = (uint32) v[0] << 16;
  926. if (dir->tdir_count == 2)
  927. dir->tdir_offset |= v[1] & 0xffff;
  928. } else {
  929. dir->tdir_offset = v[0] & 0xffff;
  930. if (dir->tdir_count == 2)
  931. dir->tdir_offset |= (uint32) v[1] << 16;
  932. }
  933. return (1);
  934. } else
  935. return (TIFFWriteData(tif, dir, (char*) v));
  936. }
  937. /*
  938. * Setup a directory entry of an array of LONG
  939. * or SLONG and write the associated indirect values.
  940. */
  941. static int
  942. TIFFWriteLongArray(TIFF* tif, TIFFDirEntry* dir, uint32* v)
  943. {
  944. if (dir->tdir_count == 1) {
  945. dir->tdir_offset = v[0];
  946. return (1);
  947. } else
  948. return (TIFFWriteData(tif, dir, (char*) v));
  949. }
  950. /*
  951. * Setup a directory entry of an array of RATIONAL
  952. * or SRATIONAL and write the associated indirect values.
  953. */
  954. static int
  955. TIFFWriteRationalArray(TIFF* tif, TIFFDirEntry* dir, float* v)
  956. {
  957. uint32 i;
  958. uint32* t;
  959. int status;
  960. t = (uint32*) _TIFFmalloc(2 * dir->tdir_count * sizeof (uint32));
  961. if (t == NULL) {
  962. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  963. "No space to write RATIONAL array");
  964. return (0);
  965. }
  966. for (i = 0; i < dir->tdir_count; i++) {
  967. float fv = v[i];
  968. int sign = 1;
  969. uint32 den;
  970. if (fv < 0) {
  971. if (dir->tdir_type == TIFF_RATIONAL) {
  972. TIFFWarningExt(tif->tif_clientdata,
  973. tif->tif_name,
  974. "\"%s\": Information lost writing value (%g) as (unsigned) RATIONAL",
  975. _TIFFFieldWithTag(tif,dir->tdir_tag)->field_name,
  976. fv);
  977. fv = 0;
  978. } else
  979. fv = -fv, sign = -1;
  980. }
  981. den = 1L;
  982. if (fv > 0) {
  983. while (fv < 1L<<(31-3) && den < 1L<<(31-3))
  984. fv *= 1<<3, den *= 1L<<3;
  985. }
  986. t[2*i+0] = (uint32) (sign * (int32)(fv + 0.5));
  987. t[2*i+1] = den;
  988. }
  989. status = TIFFWriteData(tif, dir, (char *)t);
  990. _TIFFfree((char*) t);
  991. return (status);
  992. }
  993. static int
  994. TIFFWriteFloatArray(TIFF* tif, TIFFDirEntry* dir, float* v)
  995. {
  996. TIFFCvtNativeToIEEEFloat(tif, dir->tdir_count, v);
  997. if (dir->tdir_count == 1) {
  998. dir->tdir_offset = *(uint32*) &v[0];
  999. return (1);
  1000. } else
  1001. return (TIFFWriteData(tif, dir, (char*) v));
  1002. }
  1003. static int
  1004. TIFFWriteDoubleArray(TIFF* tif, TIFFDirEntry* dir, double* v)
  1005. {
  1006. TIFFCvtNativeToIEEEDouble(tif, dir->tdir_count, v);
  1007. return (TIFFWriteData(tif, dir, (char*) v));
  1008. }
  1009. /*
  1010. * Write an array of ``type'' values for a specified tag (i.e. this is a tag
  1011. * which is allowed to have different types, e.g. SMaxSampleType).
  1012. * Internally the data values are represented as double since a double can
  1013. * hold any of the TIFF tag types (yes, this should really be an abstract
  1014. * type tany_t for portability). The data is converted into the specified
  1015. * type in a temporary buffer and then handed off to the appropriate array
  1016. * writer.
  1017. */
  1018. static int
  1019. TIFFWriteAnyArray(TIFF* tif,
  1020. TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, uint32 n, double* v)
  1021. {
  1022. char buf[10 * sizeof(double)];
  1023. char* w = buf;
  1024. int i, status = 0;
  1025. if (n * TIFFDataWidth(type) > sizeof buf) {
  1026. w = (char*) _TIFFmalloc(n * TIFFDataWidth(type));
  1027. if (w == NULL) {
  1028. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  1029. "No space to write array");
  1030. return (0);
  1031. }
  1032. }
  1033. dir->tdir_tag = (uint16) tag;
  1034. dir->tdir_type = (uint16) type;
  1035. dir->tdir_count = n;
  1036. switch (type) {
  1037. case TIFF_BYTE:
  1038. {
  1039. uint8* bp = (uint8*) w;
  1040. for (i = 0; i < (int) n; i++)
  1041. bp[i] = (uint8) v[i];
  1042. if (!TIFFWriteByteArray(tif, dir, (char*) bp))
  1043. goto out;
  1044. }
  1045. break;
  1046. case TIFF_SBYTE:
  1047. {
  1048. int8* bp = (int8*) w;
  1049. for (i = 0; i < (int) n; i++)
  1050. bp[i] = (int8) v[i];
  1051. if (!TIFFWriteByteArray(tif, dir, (char*) bp))
  1052. goto out;
  1053. }
  1054. break;
  1055. case TIFF_SHORT:
  1056. {
  1057. uint16* bp = (uint16*) w;
  1058. for (i = 0; i < (int) n; i++)
  1059. bp[i] = (uint16) v[i];
  1060. if (!TIFFWriteShortArray(tif, dir, (uint16*)bp))
  1061. goto out;
  1062. }
  1063. break;
  1064. case TIFF_SSHORT:
  1065. {
  1066. int16* bp = (int16*) w;
  1067. for (i = 0; i < (int) n; i++)
  1068. bp[i] = (int16) v[i];
  1069. if (!TIFFWriteShortArray(tif, dir, (uint16*)bp))
  1070. goto out;
  1071. }
  1072. break;
  1073. case TIFF_LONG:
  1074. {
  1075. uint32* bp = (uint32*) w;
  1076. for (i = 0; i < (int) n; i++)
  1077. bp[i] = (uint32) v[i];
  1078. if (!TIFFWriteLongArray(tif, dir, bp))
  1079. goto out;
  1080. }
  1081. break;
  1082. case TIFF_SLONG:
  1083. {
  1084. int32* bp = (int32*) w;
  1085. for (i = 0; i < (int) n; i++)
  1086. bp[i] = (int32) v[i];
  1087. if (!TIFFWriteLongArray(tif, dir, (uint32*) bp))
  1088. goto out;
  1089. }
  1090. break;
  1091. case TIFF_FLOAT:
  1092. {
  1093. float* bp = (float*) w;
  1094. for (i = 0; i < (int) n; i++)
  1095. bp[i] = (float) v[i];
  1096. if (!TIFFWriteFloatArray(tif, dir, bp))
  1097. goto out;
  1098. }
  1099. break;
  1100. case TIFF_DOUBLE:
  1101. {
  1102. if( !TIFFWriteDoubleArray(tif, dir, v))
  1103. goto out;
  1104. }
  1105. break;
  1106. default:
  1107. /* TIFF_NOTYPE */
  1108. /* TIFF_ASCII */
  1109. /* TIFF_UNDEFINED */
  1110. /* TIFF_RATIONAL */
  1111. /* TIFF_SRATIONAL */
  1112. goto out;
  1113. }
  1114. status = 1;
  1115. out:
  1116. if (w != buf)
  1117. _TIFFfree(w);
  1118. return (status);
  1119. }
  1120. static int
  1121. TIFFWriteTransferFunction(TIFF* tif, TIFFDirEntry* dir)
  1122. {
  1123. TIFFDirectory* td = &tif->tif_dir;
  1124. tsize_t n = (1L<<td->td_bitspersample) * sizeof (uint16);
  1125. uint16** tf = td->td_transferfunction;
  1126. int ncols;
  1127. /*
  1128. * Check if the table can be written as a single column,
  1129. * or if it must be written as 3 columns. Note that we
  1130. * write a 3-column tag if there are 2 samples/pixel and
  1131. * a single column of data won't suffice--hmm.
  1132. */
  1133. switch (td->td_samplesperpixel - td->td_extrasamples) {
  1134. default: if (_TIFFmemcmp(tf[0], tf[2], n)) { ncols = 3; break; }
  1135. case 2: if (_TIFFmemcmp(tf[0], tf[1], n)) { ncols = 3; break; }
  1136. case 1: case 0: ncols = 1;
  1137. }
  1138. return (TIFFWriteShortTable(tif,
  1139. TIFFTAG_TRANSFERFUNCTION, dir, ncols, tf));
  1140. }
  1141. static int
  1142. TIFFWriteInkNames(TIFF* tif, TIFFDirEntry* dir)
  1143. {
  1144. TIFFDirectory* td = &tif->tif_dir;
  1145. dir->tdir_tag = TIFFTAG_INKNAMES;
  1146. dir->tdir_type = (short) TIFF_ASCII;
  1147. dir->tdir_count = td->td_inknameslen;
  1148. return (TIFFWriteByteArray(tif, dir, td->td_inknames));
  1149. }
  1150. /*
  1151. * Write a contiguous directory item.
  1152. */
  1153. static int
  1154. TIFFWriteData(TIFF* tif, TIFFDirEntry* dir, char* cp)
  1155. {
  1156. tsize_t cc;
  1157. if (tif->tif_flags & TIFF_SWAB) {
  1158. switch (dir->tdir_type) {
  1159. case TIFF_SHORT:
  1160. case TIFF_SSHORT:
  1161. TIFFSwabArrayOfShort((uint16*) cp, dir->tdir_count);
  1162. break;
  1163. case TIFF_LONG:
  1164. case TIFF_SLONG:
  1165. case TIFF_FLOAT:
  1166. TIFFSwabArrayOfLong((uint32*) cp, dir->tdir_count);
  1167. break;
  1168. case TIFF_RATIONAL:
  1169. case TIFF_SRATIONAL:
  1170. TIFFSwabArrayOfLong((uint32*) cp, 2*dir->tdir_count);
  1171. break;
  1172. case TIFF_DOUBLE:
  1173. TIFFSwabArrayOfDouble((double*) cp, dir->tdir_count);
  1174. break;
  1175. }
  1176. }
  1177. dir->tdir_offset = tif->tif_dataoff;
  1178. cc = dir->tdir_count * TIFFDataWidth((TIFFDataType) dir->tdir_type);
  1179. if (SeekOK(tif, dir->tdir_offset) &&
  1180. WriteOK(tif, cp, cc)) {
  1181. tif->tif_dataoff += (cc + 1) & ~1;
  1182. return (1);
  1183. }
  1184. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  1185. "Error writing data for field \"%s\"",
  1186. _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
  1187. return (0);
  1188. }
  1189. /*
  1190. * Similar to TIFFWriteDirectory(), but if the directory has already
  1191. * been written once, it is relocated to the end of the file, in case it
  1192. * has changed in size. Note that this will result in the loss of the
  1193. * previously used directory space.
  1194. */
  1195. int
  1196. TIFFRewriteDirectory( TIFF *tif )
  1197. {
  1198. static const char module[] = "TIFFRewriteDirectory";
  1199. /* We don't need to do anything special if it hasn't been written. */
  1200. if( tif->tif_diroff == 0 )
  1201. return TIFFWriteDirectory( tif );
  1202. /*
  1203. ** Find and zero the pointer to this directory, so that TIFFLinkDirectory
  1204. ** will cause it to be added after this directories current pre-link.
  1205. */
  1206. /* Is it the first directory in the file? */
  1207. if (tif->tif_header.tiff_diroff == tif->tif_diroff)
  1208. {
  1209. tif->tif_header.tiff_diroff = 0;
  1210. tif->tif_diroff = 0;
  1211. TIFFSeekFile(tif, (toff_t)(TIFF_MAGIC_SIZE+TIFF_VERSION_SIZE),
  1212. SEEK_SET);
  1213. if (!WriteOK(tif, &(tif->tif_header.tiff_diroff),
  1214. sizeof (tif->tif_diroff)))
  1215. {
  1216. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  1217. "Error updating TIFF header");
  1218. return (0);
  1219. }
  1220. }
  1221. else
  1222. {
  1223. toff_t nextdir, off;
  1224. nextdir = tif->tif_header.tiff_diroff;
  1225. do {
  1226. uint16 dircount;
  1227. if (!SeekOK(tif, nextdir) ||
  1228. !ReadOK(tif, &dircount, sizeof (dircount))) {
  1229. TIFFErrorExt(tif->tif_clientdata, module,
  1230. "Error fetching directory count");
  1231. return (0);
  1232. }
  1233. if (tif->tif_flags & TIFF_SWAB)
  1234. TIFFSwabShort(&dircount);
  1235. (void) TIFFSeekFile(tif,
  1236. dircount * sizeof (TIFFDirEntry), SEEK_CUR);
  1237. if (!ReadOK(tif, &nextdir, sizeof (nextdir))) {
  1238. TIFFErrorExt(tif->tif_clientdata, module,
  1239. "Error fetching directory link");
  1240. return (0);
  1241. }
  1242. if (tif->tif_flags & TIFF_SWAB)
  1243. TIFFSwabLong(&nextdir);
  1244. } while (nextdir != tif->tif_diroff && nextdir != 0);
  1245. off = TIFFSeekFile(tif, 0, SEEK_CUR); /* get current offset */
  1246. (void) TIFFSeekFile(tif, off - (toff_t)sizeof(nextdir), SEEK_SET);
  1247. tif->tif_diroff = 0;
  1248. if (!WriteOK(tif, &(tif->tif_diroff), sizeof (nextdir))) {
  1249. TIFFErrorExt(tif->tif_clientdata, module,
  1250. "Error writing directory link");
  1251. return (0);
  1252. }
  1253. }
  1254. /*
  1255. ** Now use TIFFWriteDirectory() normally.
  1256. */
  1257. return TIFFWriteDirectory( tif );
  1258. }
  1259. /*
  1260. * Link the current directory into the directory chain for the file.
  1261. */
  1262. static int
  1263. TIFFLinkDirectory(TIFF* tif)
  1264. {
  1265. static const char module[] = "TIFFLinkDirectory";
  1266. toff_t nextdir;
  1267. toff_t diroff, off;
  1268. tif->tif_diroff = (TIFFSeekFile(tif, (toff_t) 0, SEEK_END)+1) &~ 1;
  1269. diroff = tif->tif_diroff;
  1270. if (tif->tif_flags & TIFF_SWAB)
  1271. TIFFSwabLong(&diroff);
  1272. /*
  1273. * Handle SubIFDs
  1274. */
  1275. if (tif->tif_flags & TIFF_INSUBIFD) {
  1276. (void) TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET);
  1277. if (!WriteOK(tif, &diroff, sizeof (diroff))) {
  1278. TIFFErrorExt(tif->tif_clientdata, module,
  1279. "%s: Error writing SubIFD directory link",
  1280. tif->tif_name);
  1281. return (0);
  1282. }
  1283. /*
  1284. * Advance to the next SubIFD or, if this is
  1285. * the last one configured, revert back to the
  1286. * normal directory linkage.
  1287. */
  1288. if (--tif->tif_nsubifd)
  1289. tif->tif_subifdoff += sizeof (diroff);
  1290. else
  1291. tif->tif_flags &= ~TIFF_INSUBIFD;
  1292. return (1);
  1293. }
  1294. if (tif->tif_header.tiff_diroff == 0) {
  1295. /*
  1296. * First directory, overwrite offset in header.
  1297. */
  1298. tif->tif_header.tiff_diroff = tif->tif_diroff;
  1299. (void) TIFFSeekFile(tif,
  1300. (toff_t)(TIFF_MAGIC_SIZE+TIFF_VERSION_SIZE),
  1301. SEEK_SET);
  1302. if (!WriteOK(tif, &diroff, sizeof (diroff))) {
  1303. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  1304. "Error writing TIFF header");
  1305. return (0);
  1306. }
  1307. return (1);
  1308. }
  1309. /*
  1310. * Not the first directory, search to the last and append.
  1311. */
  1312. nextdir = tif->tif_header.tiff_diroff;
  1313. do {
  1314. uint16 dircount;
  1315. if (!SeekOK(tif, nextdir) ||
  1316. !ReadOK(tif, &dircount, sizeof (dircount))) {
  1317. TIFFErrorExt(tif->tif_clientdata, module,
  1318. "Error fetching directory count");
  1319. return (0);
  1320. }
  1321. if (tif->tif_flags & TIFF_SWAB)
  1322. TIFFSwabShort(&dircount);
  1323. (void) TIFFSeekFile(tif,
  1324. dircount * sizeof (TIFFDirEntry), SEEK_CUR);
  1325. if (!ReadOK(tif, &nextdir, sizeof (nextdir))) {
  1326. TIFFErrorExt(tif->tif_clientdata, module,
  1327. "Error fetching directory link");
  1328. return (0);
  1329. }
  1330. if (tif->tif_flags & TIFF_SWAB)
  1331. TIFFSwabLong(&nextdir);
  1332. } while (nextdir != 0);
  1333. off = TIFFSeekFile(tif, 0, SEEK_CUR); /* get current offset */
  1334. (void) TIFFSeekFile(tif, off - (toff_t)sizeof(nextdir), SEEK_SET);
  1335. if (!WriteOK(tif, &diroff, sizeof (diroff))) {
  1336. TIFFErrorExt(tif->tif_clientdata, module,
  1337. "Error writing directory link");
  1338. return (0);
  1339. }
  1340. return (1);
  1341. }
  1342. /* vim: set ts=8 sts=8 sw=8 noet: */
  1343. /*
  1344. * Local Variables:
  1345. * mode: c
  1346. * c-basic-offset: 8
  1347. * fill-column: 78
  1348. * End:
  1349. */