/src/FreeImage/Source/LibTIFF/tif_dir.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 1391 lines · 1071 code · 72 blank · 248 comment · 229 complexity · f20bb1ca16c4e8967aada22a5abf6982 MD5 · raw file

  1. /* $Id: tif_dir.c,v 1.38 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 Tag Get & Set Routines.
  29. * (and also some miscellaneous stuff)
  30. */
  31. #include "tiffiop.h"
  32. /*
  33. * These are used in the backwards compatibility code...
  34. */
  35. #define DATATYPE_VOID 0 /* !untyped data */
  36. #define DATATYPE_INT 1 /* !signed integer data */
  37. #define DATATYPE_UINT 2 /* !unsigned integer data */
  38. #define DATATYPE_IEEEFP 3 /* !IEEE floating point data */
  39. static void
  40. setByteArray(void** vpp, void* vp, size_t nmemb, size_t elem_size)
  41. {
  42. if (*vpp)
  43. _TIFFfree(*vpp), *vpp = 0;
  44. if (vp) {
  45. tsize_t bytes = nmemb * elem_size;
  46. if (elem_size && bytes / elem_size == nmemb)
  47. *vpp = (void*) _TIFFmalloc(bytes);
  48. if (*vpp)
  49. _TIFFmemcpy(*vpp, vp, bytes);
  50. }
  51. }
  52. void _TIFFsetByteArray(void** vpp, void* vp, uint32 n)
  53. { setByteArray(vpp, vp, n, 1); }
  54. void _TIFFsetString(char** cpp, char* cp)
  55. { setByteArray((void**) cpp, (void*) cp, strlen(cp)+1, 1); }
  56. void _TIFFsetNString(char** cpp, char* cp, uint32 n)
  57. { setByteArray((void**) cpp, (void*) cp, n, 1); }
  58. void _TIFFsetShortArray(uint16** wpp, uint16* wp, uint32 n)
  59. { setByteArray((void**) wpp, (void*) wp, n, sizeof (uint16)); }
  60. void _TIFFsetLongArray(uint32** lpp, uint32* lp, uint32 n)
  61. { setByteArray((void**) lpp, (void*) lp, n, sizeof (uint32)); }
  62. void _TIFFsetFloatArray(float** fpp, float* fp, uint32 n)
  63. { setByteArray((void**) fpp, (void*) fp, n, sizeof (float)); }
  64. void _TIFFsetDoubleArray(double** dpp, double* dp, uint32 n)
  65. { setByteArray((void**) dpp, (void*) dp, n, sizeof (double)); }
  66. /*
  67. * Install extra samples information.
  68. */
  69. static int
  70. setExtraSamples(TIFFDirectory* td, va_list ap, uint32* v)
  71. {
  72. /* XXX: Unassociated alpha data == 999 is a known Corel Draw bug, see below */
  73. #define EXTRASAMPLE_COREL_UNASSALPHA 999
  74. uint16* va;
  75. uint32 i;
  76. *v = va_arg(ap, uint32);
  77. if ((uint16) *v > td->td_samplesperpixel)
  78. return 0;
  79. va = va_arg(ap, uint16*);
  80. if (*v > 0 && va == NULL) /* typically missing param */
  81. return 0;
  82. for (i = 0; i < *v; i++) {
  83. if (va[i] > EXTRASAMPLE_UNASSALPHA) {
  84. /*
  85. * XXX: Corel Draw is known to produce incorrect
  86. * ExtraSamples tags which must be patched here if we
  87. * want to be able to open some of the damaged TIFF
  88. * files:
  89. */
  90. if (va[i] == EXTRASAMPLE_COREL_UNASSALPHA)
  91. va[i] = EXTRASAMPLE_UNASSALPHA;
  92. else
  93. return 0;
  94. }
  95. }
  96. td->td_extrasamples = (uint16) *v;
  97. _TIFFsetShortArray(&td->td_sampleinfo, va, td->td_extrasamples);
  98. return 1;
  99. #undef EXTRASAMPLE_COREL_UNASSALPHA
  100. }
  101. static uint32
  102. checkInkNamesString(TIFF* tif, uint32 slen, const char* s)
  103. {
  104. TIFFDirectory* td = &tif->tif_dir;
  105. uint16 i = td->td_samplesperpixel;
  106. if (slen > 0) {
  107. const char* ep = s+slen;
  108. const char* cp = s;
  109. for (; i > 0; i--) {
  110. for (; *cp != '\0'; cp++)
  111. if (cp >= ep)
  112. goto bad;
  113. cp++; /* skip \0 */
  114. }
  115. return (cp-s);
  116. }
  117. bad:
  118. TIFFErrorExt(tif->tif_clientdata, "TIFFSetField",
  119. "%s: Invalid InkNames value; expecting %d names, found %d",
  120. tif->tif_name,
  121. td->td_samplesperpixel,
  122. td->td_samplesperpixel-i);
  123. return (0);
  124. }
  125. static int
  126. _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
  127. {
  128. static const char module[] = "_TIFFVSetField";
  129. TIFFDirectory* td = &tif->tif_dir;
  130. int status = 1;
  131. uint32 v32, i, v;
  132. char* s;
  133. switch (tag) {
  134. case TIFFTAG_SUBFILETYPE:
  135. td->td_subfiletype = va_arg(ap, uint32);
  136. break;
  137. case TIFFTAG_IMAGEWIDTH:
  138. td->td_imagewidth = va_arg(ap, uint32);
  139. break;
  140. case TIFFTAG_IMAGELENGTH:
  141. td->td_imagelength = va_arg(ap, uint32);
  142. break;
  143. case TIFFTAG_BITSPERSAMPLE:
  144. td->td_bitspersample = (uint16) va_arg(ap, int);
  145. /*
  146. * If the data require post-decoding processing to byte-swap
  147. * samples, set it up here. Note that since tags are required
  148. * to be ordered, compression code can override this behaviour
  149. * in the setup method if it wants to roll the post decoding
  150. * work in with its normal work.
  151. */
  152. if (tif->tif_flags & TIFF_SWAB) {
  153. if (td->td_bitspersample == 8)
  154. tif->tif_postdecode = _TIFFNoPostDecode;
  155. else if (td->td_bitspersample == 16)
  156. tif->tif_postdecode = _TIFFSwab16BitData;
  157. else if (td->td_bitspersample == 24)
  158. tif->tif_postdecode = _TIFFSwab24BitData;
  159. else if (td->td_bitspersample == 32)
  160. tif->tif_postdecode = _TIFFSwab32BitData;
  161. else if (td->td_bitspersample == 64)
  162. tif->tif_postdecode = _TIFFSwab64BitData;
  163. else if (td->td_bitspersample == 128) /* two 64's */
  164. tif->tif_postdecode = _TIFFSwab64BitData;
  165. }
  166. break;
  167. case TIFFTAG_COMPRESSION:
  168. v = va_arg(ap, uint32) & 0xffff;
  169. /*
  170. * If we're changing the compression scheme, the notify the
  171. * previous module so that it can cleanup any state it's
  172. * setup.
  173. */
  174. if (TIFFFieldSet(tif, FIELD_COMPRESSION)) {
  175. if (td->td_compression == v)
  176. break;
  177. (*tif->tif_cleanup)(tif);
  178. tif->tif_flags &= ~TIFF_CODERSETUP;
  179. }
  180. /*
  181. * Setup new compression routine state.
  182. */
  183. if( (status = TIFFSetCompressionScheme(tif, v)) != 0 )
  184. td->td_compression = (uint16) v;
  185. else
  186. status = 0;
  187. break;
  188. case TIFFTAG_PHOTOMETRIC:
  189. td->td_photometric = (uint16) va_arg(ap, int);
  190. break;
  191. case TIFFTAG_THRESHHOLDING:
  192. td->td_threshholding = (uint16) va_arg(ap, int);
  193. break;
  194. case TIFFTAG_FILLORDER:
  195. v = va_arg(ap, uint32);
  196. if (v != FILLORDER_LSB2MSB && v != FILLORDER_MSB2LSB)
  197. goto badvalue;
  198. td->td_fillorder = (uint16) v;
  199. break;
  200. case TIFFTAG_ORIENTATION:
  201. v = va_arg(ap, uint32);
  202. if (v < ORIENTATION_TOPLEFT || ORIENTATION_LEFTBOT < v)
  203. goto badvalue;
  204. else
  205. td->td_orientation = (uint16) v;
  206. break;
  207. case TIFFTAG_SAMPLESPERPIXEL:
  208. /* XXX should cross check -- e.g. if pallette, then 1 */
  209. v = va_arg(ap, uint32);
  210. if (v == 0)
  211. goto badvalue;
  212. td->td_samplesperpixel = (uint16) v;
  213. break;
  214. case TIFFTAG_ROWSPERSTRIP:
  215. v32 = va_arg(ap, uint32);
  216. if (v32 == 0)
  217. goto badvalue32;
  218. td->td_rowsperstrip = v32;
  219. if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
  220. td->td_tilelength = v32;
  221. td->td_tilewidth = td->td_imagewidth;
  222. }
  223. break;
  224. case TIFFTAG_MINSAMPLEVALUE:
  225. td->td_minsamplevalue = (uint16) va_arg(ap, int);
  226. break;
  227. case TIFFTAG_MAXSAMPLEVALUE:
  228. td->td_maxsamplevalue = (uint16) va_arg(ap, int);
  229. break;
  230. case TIFFTAG_SMINSAMPLEVALUE:
  231. td->td_sminsamplevalue = va_arg(ap, double);
  232. break;
  233. case TIFFTAG_SMAXSAMPLEVALUE:
  234. td->td_smaxsamplevalue = va_arg(ap, double);
  235. break;
  236. case TIFFTAG_XRESOLUTION:
  237. td->td_xresolution = (float) va_arg(ap, double);
  238. break;
  239. case TIFFTAG_YRESOLUTION:
  240. td->td_yresolution = (float) va_arg(ap, double);
  241. break;
  242. case TIFFTAG_PLANARCONFIG:
  243. v = va_arg(ap, uint32);
  244. if (v != PLANARCONFIG_CONTIG && v != PLANARCONFIG_SEPARATE)
  245. goto badvalue;
  246. td->td_planarconfig = (uint16) v;
  247. break;
  248. case TIFFTAG_XPOSITION:
  249. td->td_xposition = (float) va_arg(ap, double);
  250. break;
  251. case TIFFTAG_YPOSITION:
  252. td->td_yposition = (float) va_arg(ap, double);
  253. break;
  254. case TIFFTAG_RESOLUTIONUNIT:
  255. v = va_arg(ap, uint32);
  256. if (v < RESUNIT_NONE || RESUNIT_CENTIMETER < v)
  257. goto badvalue;
  258. td->td_resolutionunit = (uint16) v;
  259. break;
  260. case TIFFTAG_PAGENUMBER:
  261. td->td_pagenumber[0] = (uint16) va_arg(ap, int);
  262. td->td_pagenumber[1] = (uint16) va_arg(ap, int);
  263. break;
  264. case TIFFTAG_HALFTONEHINTS:
  265. td->td_halftonehints[0] = (uint16) va_arg(ap, int);
  266. td->td_halftonehints[1] = (uint16) va_arg(ap, int);
  267. break;
  268. case TIFFTAG_COLORMAP:
  269. v32 = (uint32)(1L<<td->td_bitspersample);
  270. _TIFFsetShortArray(&td->td_colormap[0], va_arg(ap, uint16*), v32);
  271. _TIFFsetShortArray(&td->td_colormap[1], va_arg(ap, uint16*), v32);
  272. _TIFFsetShortArray(&td->td_colormap[2], va_arg(ap, uint16*), v32);
  273. break;
  274. case TIFFTAG_EXTRASAMPLES:
  275. if (!setExtraSamples(td, ap, &v))
  276. goto badvalue;
  277. break;
  278. case TIFFTAG_MATTEING:
  279. td->td_extrasamples = (uint16) (va_arg(ap, int) != 0);
  280. if (td->td_extrasamples) {
  281. uint16 sv = EXTRASAMPLE_ASSOCALPHA;
  282. _TIFFsetShortArray(&td->td_sampleinfo, &sv, 1);
  283. }
  284. break;
  285. case TIFFTAG_TILEWIDTH:
  286. v32 = va_arg(ap, uint32);
  287. if (v32 % 16) {
  288. if (tif->tif_mode != O_RDONLY)
  289. goto badvalue32;
  290. TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
  291. "Nonstandard tile width %d, convert file", v32);
  292. }
  293. td->td_tilewidth = v32;
  294. tif->tif_flags |= TIFF_ISTILED;
  295. break;
  296. case TIFFTAG_TILELENGTH:
  297. v32 = va_arg(ap, uint32);
  298. if (v32 % 16) {
  299. if (tif->tif_mode != O_RDONLY)
  300. goto badvalue32;
  301. TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
  302. "Nonstandard tile length %d, convert file", v32);
  303. }
  304. td->td_tilelength = v32;
  305. tif->tif_flags |= TIFF_ISTILED;
  306. break;
  307. case TIFFTAG_TILEDEPTH:
  308. v32 = va_arg(ap, uint32);
  309. if (v32 == 0)
  310. goto badvalue32;
  311. td->td_tiledepth = v32;
  312. break;
  313. case TIFFTAG_DATATYPE:
  314. v = va_arg(ap, uint32);
  315. switch (v) {
  316. case DATATYPE_VOID: v = SAMPLEFORMAT_VOID; break;
  317. case DATATYPE_INT: v = SAMPLEFORMAT_INT; break;
  318. case DATATYPE_UINT: v = SAMPLEFORMAT_UINT; break;
  319. case DATATYPE_IEEEFP: v = SAMPLEFORMAT_IEEEFP;break;
  320. default: goto badvalue;
  321. }
  322. td->td_sampleformat = (uint16) v;
  323. break;
  324. case TIFFTAG_SAMPLEFORMAT:
  325. v = va_arg(ap, uint32);
  326. if (v < SAMPLEFORMAT_UINT || SAMPLEFORMAT_COMPLEXIEEEFP < v)
  327. goto badvalue;
  328. td->td_sampleformat = (uint16) v;
  329. /* Try to fix up the SWAB function for complex data. */
  330. if( td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT
  331. && td->td_bitspersample == 32
  332. && tif->tif_postdecode == _TIFFSwab32BitData )
  333. tif->tif_postdecode = _TIFFSwab16BitData;
  334. else if( (td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT
  335. || td->td_sampleformat == SAMPLEFORMAT_COMPLEXIEEEFP)
  336. && td->td_bitspersample == 64
  337. && tif->tif_postdecode == _TIFFSwab64BitData )
  338. tif->tif_postdecode = _TIFFSwab32BitData;
  339. break;
  340. case TIFFTAG_IMAGEDEPTH:
  341. td->td_imagedepth = va_arg(ap, uint32);
  342. break;
  343. case TIFFTAG_SUBIFD:
  344. if ((tif->tif_flags & TIFF_INSUBIFD) == 0) {
  345. td->td_nsubifd = (uint16) va_arg(ap, int);
  346. _TIFFsetLongArray(&td->td_subifd, va_arg(ap, uint32*),
  347. (long) td->td_nsubifd);
  348. } else {
  349. TIFFErrorExt(tif->tif_clientdata, module,
  350. "%s: Sorry, cannot nest SubIFDs",
  351. tif->tif_name);
  352. status = 0;
  353. }
  354. break;
  355. case TIFFTAG_YCBCRPOSITIONING:
  356. td->td_ycbcrpositioning = (uint16) va_arg(ap, int);
  357. break;
  358. case TIFFTAG_YCBCRSUBSAMPLING:
  359. td->td_ycbcrsubsampling[0] = (uint16) va_arg(ap, int);
  360. td->td_ycbcrsubsampling[1] = (uint16) va_arg(ap, int);
  361. break;
  362. case TIFFTAG_TRANSFERFUNCTION:
  363. v = (td->td_samplesperpixel - td->td_extrasamples) > 1 ? 3 : 1;
  364. for (i = 0; i < v; i++)
  365. _TIFFsetShortArray(&td->td_transferfunction[i],
  366. va_arg(ap, uint16*), 1L<<td->td_bitspersample);
  367. break;
  368. case TIFFTAG_REFERENCEBLACKWHITE:
  369. /* XXX should check for null range */
  370. _TIFFsetFloatArray(&td->td_refblackwhite, va_arg(ap, float*), 6);
  371. break;
  372. case TIFFTAG_INKNAMES:
  373. v = va_arg(ap, uint32);
  374. s = va_arg(ap, char*);
  375. v = checkInkNamesString(tif, v, s);
  376. status = v > 0;
  377. if( v > 0 ) {
  378. _TIFFsetNString(&td->td_inknames, s, v);
  379. td->td_inknameslen = v;
  380. }
  381. break;
  382. default: {
  383. TIFFTagValue *tv;
  384. int tv_size, iCustom;
  385. const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
  386. /*
  387. * This can happen if multiple images are open with different
  388. * codecs which have private tags. The global tag information
  389. * table may then have tags that are valid for one file but not
  390. * the other. If the client tries to set a tag that is not valid
  391. * for the image's codec then we'll arrive here. This
  392. * happens, for example, when tiffcp is used to convert between
  393. * compression schemes and codec-specific tags are blindly copied.
  394. */
  395. if(fip == NULL || fip->field_bit != FIELD_CUSTOM) {
  396. TIFFErrorExt(tif->tif_clientdata, module,
  397. "%s: Invalid %stag \"%s\" (not supported by codec)",
  398. tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
  399. fip ? fip->field_name : "Unknown");
  400. status = 0;
  401. break;
  402. }
  403. /*
  404. * Find the existing entry for this custom value.
  405. */
  406. tv = NULL;
  407. for (iCustom = 0; iCustom < td->td_customValueCount; iCustom++) {
  408. if (td->td_customValues[iCustom].info->field_tag == tag) {
  409. tv = td->td_customValues + iCustom;
  410. if (tv->value != NULL) {
  411. _TIFFfree(tv->value);
  412. tv->value = NULL;
  413. }
  414. break;
  415. }
  416. }
  417. /*
  418. * Grow the custom list if the entry was not found.
  419. */
  420. if(tv == NULL) {
  421. TIFFTagValue *new_customValues;
  422. td->td_customValueCount++;
  423. new_customValues = (TIFFTagValue *)
  424. _TIFFrealloc(td->td_customValues,
  425. sizeof(TIFFTagValue) * td->td_customValueCount);
  426. if (!new_customValues) {
  427. TIFFErrorExt(tif->tif_clientdata, module,
  428. "%s: Failed to allocate space for list of custom values",
  429. tif->tif_name);
  430. status = 0;
  431. goto end;
  432. }
  433. td->td_customValues = new_customValues;
  434. tv = td->td_customValues + (td->td_customValueCount - 1);
  435. tv->info = fip;
  436. tv->value = NULL;
  437. tv->count = 0;
  438. }
  439. /*
  440. * Set custom value ... save a copy of the custom tag value.
  441. */
  442. tv_size = _TIFFDataSize(fip->field_type);
  443. if (tv_size == 0) {
  444. status = 0;
  445. TIFFErrorExt(tif->tif_clientdata, module,
  446. "%s: Bad field type %d for \"%s\"",
  447. tif->tif_name, fip->field_type,
  448. fip->field_name);
  449. goto end;
  450. }
  451. if(fip->field_passcount) {
  452. if (fip->field_writecount == TIFF_VARIABLE2)
  453. tv->count = (uint32) va_arg(ap, uint32);
  454. else
  455. tv->count = (int) va_arg(ap, int);
  456. } else if (fip->field_writecount == TIFF_VARIABLE
  457. || fip->field_writecount == TIFF_VARIABLE2)
  458. tv->count = 1;
  459. else if (fip->field_writecount == TIFF_SPP)
  460. tv->count = td->td_samplesperpixel;
  461. else
  462. tv->count = fip->field_writecount;
  463. if (fip->field_type == TIFF_ASCII)
  464. _TIFFsetString((char **)&tv->value, va_arg(ap, char *));
  465. else {
  466. tv->value = _TIFFCheckMalloc(tif, tv_size, tv->count,
  467. "Tag Value");
  468. if (!tv->value) {
  469. status = 0;
  470. goto end;
  471. }
  472. if ((fip->field_passcount
  473. || fip->field_writecount == TIFF_VARIABLE
  474. || fip->field_writecount == TIFF_VARIABLE2
  475. || fip->field_writecount == TIFF_SPP
  476. || tv->count > 1)
  477. && fip->field_tag != TIFFTAG_PAGENUMBER
  478. && fip->field_tag != TIFFTAG_HALFTONEHINTS
  479. && fip->field_tag != TIFFTAG_YCBCRSUBSAMPLING
  480. && fip->field_tag != TIFFTAG_DOTRANGE) {
  481. _TIFFmemcpy(tv->value, va_arg(ap, void *),
  482. tv->count * tv_size);
  483. } else {
  484. /*
  485. * XXX: The following loop required to handle
  486. * TIFFTAG_PAGENUMBER, TIFFTAG_HALFTONEHINTS,
  487. * TIFFTAG_YCBCRSUBSAMPLING and TIFFTAG_DOTRANGE tags.
  488. * These tags are actually arrays and should be passed as
  489. * array pointers to TIFFSetField() function, but actually
  490. * passed as a list of separate values. This behaviour
  491. * must be changed in the future!
  492. */
  493. int i;
  494. char *val = (char *)tv->value;
  495. for (i = 0; i < tv->count; i++, val += tv_size) {
  496. switch (fip->field_type) {
  497. case TIFF_BYTE:
  498. case TIFF_UNDEFINED:
  499. {
  500. uint8 v = (uint8)va_arg(ap, int);
  501. _TIFFmemcpy(val, &v, tv_size);
  502. }
  503. break;
  504. case TIFF_SBYTE:
  505. {
  506. int8 v = (int8)va_arg(ap, int);
  507. _TIFFmemcpy(val, &v, tv_size);
  508. }
  509. break;
  510. case TIFF_SHORT:
  511. {
  512. uint16 v = (uint16)va_arg(ap, int);
  513. _TIFFmemcpy(val, &v, tv_size);
  514. }
  515. break;
  516. case TIFF_SSHORT:
  517. {
  518. int16 v = (int16)va_arg(ap, int);
  519. _TIFFmemcpy(val, &v, tv_size);
  520. }
  521. break;
  522. case TIFF_LONG:
  523. case TIFF_IFD:
  524. {
  525. uint32 v = va_arg(ap, uint32);
  526. _TIFFmemcpy(val, &v, tv_size);
  527. }
  528. break;
  529. case TIFF_SLONG:
  530. {
  531. int32 v = va_arg(ap, int32);
  532. _TIFFmemcpy(val, &v, tv_size);
  533. }
  534. break;
  535. case TIFF_RATIONAL:
  536. case TIFF_SRATIONAL:
  537. case TIFF_FLOAT:
  538. {
  539. float v = (float)va_arg(ap, double);
  540. _TIFFmemcpy(val, &v, tv_size);
  541. }
  542. break;
  543. case TIFF_DOUBLE:
  544. {
  545. double v = va_arg(ap, double);
  546. _TIFFmemcpy(val, &v, tv_size);
  547. }
  548. break;
  549. default:
  550. _TIFFmemset(val, 0, tv_size);
  551. status = 0;
  552. break;
  553. }
  554. }
  555. }
  556. }
  557. }
  558. }
  559. if (status) {
  560. TIFFSetFieldBit(tif, _TIFFFieldWithTag(tif, tag)->field_bit);
  561. tif->tif_flags |= TIFF_DIRTYDIRECT;
  562. }
  563. end:
  564. va_end(ap);
  565. return (status);
  566. badvalue:
  567. TIFFErrorExt(tif->tif_clientdata, module,
  568. "%s: Bad value %d for \"%s\" tag",
  569. tif->tif_name, v,
  570. _TIFFFieldWithTag(tif, tag)->field_name);
  571. va_end(ap);
  572. return (0);
  573. badvalue32:
  574. TIFFErrorExt(tif->tif_clientdata, module,
  575. "%s: Bad value %u for \"%s\" tag",
  576. tif->tif_name, v32,
  577. _TIFFFieldWithTag(tif, tag)->field_name);
  578. va_end(ap);
  579. return (0);
  580. }
  581. /*
  582. * Return 1/0 according to whether or not
  583. * it is permissible to set the tag's value.
  584. * Note that we allow ImageLength to be changed
  585. * so that we can append and extend to images.
  586. * Any other tag may not be altered once writing
  587. * has commenced, unless its value has no effect
  588. * on the format of the data that is written.
  589. */
  590. static int
  591. OkToChangeTag(TIFF* tif, ttag_t tag)
  592. {
  593. const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
  594. if (!fip) { /* unknown tag */
  595. TIFFErrorExt(tif->tif_clientdata, "TIFFSetField", "%s: Unknown %stag %u",
  596. tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", tag);
  597. return (0);
  598. }
  599. if (tag != TIFFTAG_IMAGELENGTH && (tif->tif_flags & TIFF_BEENWRITING) &&
  600. !fip->field_oktochange) {
  601. /*
  602. * Consult info table to see if tag can be changed
  603. * after we've started writing. We only allow changes
  604. * to those tags that don't/shouldn't affect the
  605. * compression and/or format of the data.
  606. */
  607. TIFFErrorExt(tif->tif_clientdata, "TIFFSetField",
  608. "%s: Cannot modify tag \"%s\" while writing",
  609. tif->tif_name, fip->field_name);
  610. return (0);
  611. }
  612. return (1);
  613. }
  614. /*
  615. * Record the value of a field in the
  616. * internal directory structure. The
  617. * field will be written to the file
  618. * when/if the directory structure is
  619. * updated.
  620. */
  621. int
  622. TIFFSetField(TIFF* tif, ttag_t tag, ...)
  623. {
  624. va_list ap;
  625. int status;
  626. va_start(ap, tag);
  627. status = TIFFVSetField(tif, tag, ap);
  628. va_end(ap);
  629. return (status);
  630. }
  631. /*
  632. * Like TIFFSetField, but taking a varargs
  633. * parameter list. This routine is useful
  634. * for building higher-level interfaces on
  635. * top of the library.
  636. */
  637. int
  638. TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
  639. {
  640. return OkToChangeTag(tif, tag) ?
  641. (*tif->tif_tagmethods.vsetfield)(tif, tag, ap) : 0;
  642. }
  643. static int
  644. _TIFFVGetField(TIFF* tif, ttag_t tag, va_list ap)
  645. {
  646. TIFFDirectory* td = &tif->tif_dir;
  647. int ret_val = 1;
  648. switch (tag) {
  649. case TIFFTAG_SUBFILETYPE:
  650. *va_arg(ap, uint32*) = td->td_subfiletype;
  651. break;
  652. case TIFFTAG_IMAGEWIDTH:
  653. *va_arg(ap, uint32*) = td->td_imagewidth;
  654. break;
  655. case TIFFTAG_IMAGELENGTH:
  656. *va_arg(ap, uint32*) = td->td_imagelength;
  657. break;
  658. case TIFFTAG_BITSPERSAMPLE:
  659. *va_arg(ap, uint16*) = td->td_bitspersample;
  660. break;
  661. case TIFFTAG_COMPRESSION:
  662. *va_arg(ap, uint16*) = td->td_compression;
  663. break;
  664. case TIFFTAG_PHOTOMETRIC:
  665. *va_arg(ap, uint16*) = td->td_photometric;
  666. break;
  667. case TIFFTAG_THRESHHOLDING:
  668. *va_arg(ap, uint16*) = td->td_threshholding;
  669. break;
  670. case TIFFTAG_FILLORDER:
  671. *va_arg(ap, uint16*) = td->td_fillorder;
  672. break;
  673. case TIFFTAG_ORIENTATION:
  674. *va_arg(ap, uint16*) = td->td_orientation;
  675. break;
  676. case TIFFTAG_SAMPLESPERPIXEL:
  677. *va_arg(ap, uint16*) = td->td_samplesperpixel;
  678. break;
  679. case TIFFTAG_ROWSPERSTRIP:
  680. *va_arg(ap, uint32*) = td->td_rowsperstrip;
  681. break;
  682. case TIFFTAG_MINSAMPLEVALUE:
  683. *va_arg(ap, uint16*) = td->td_minsamplevalue;
  684. break;
  685. case TIFFTAG_MAXSAMPLEVALUE:
  686. *va_arg(ap, uint16*) = td->td_maxsamplevalue;
  687. break;
  688. case TIFFTAG_SMINSAMPLEVALUE:
  689. *va_arg(ap, double*) = td->td_sminsamplevalue;
  690. break;
  691. case TIFFTAG_SMAXSAMPLEVALUE:
  692. *va_arg(ap, double*) = td->td_smaxsamplevalue;
  693. break;
  694. case TIFFTAG_XRESOLUTION:
  695. *va_arg(ap, float*) = td->td_xresolution;
  696. break;
  697. case TIFFTAG_YRESOLUTION:
  698. *va_arg(ap, float*) = td->td_yresolution;
  699. break;
  700. case TIFFTAG_PLANARCONFIG:
  701. *va_arg(ap, uint16*) = td->td_planarconfig;
  702. break;
  703. case TIFFTAG_XPOSITION:
  704. *va_arg(ap, float*) = td->td_xposition;
  705. break;
  706. case TIFFTAG_YPOSITION:
  707. *va_arg(ap, float*) = td->td_yposition;
  708. break;
  709. case TIFFTAG_RESOLUTIONUNIT:
  710. *va_arg(ap, uint16*) = td->td_resolutionunit;
  711. break;
  712. case TIFFTAG_PAGENUMBER:
  713. *va_arg(ap, uint16*) = td->td_pagenumber[0];
  714. *va_arg(ap, uint16*) = td->td_pagenumber[1];
  715. break;
  716. case TIFFTAG_HALFTONEHINTS:
  717. *va_arg(ap, uint16*) = td->td_halftonehints[0];
  718. *va_arg(ap, uint16*) = td->td_halftonehints[1];
  719. break;
  720. case TIFFTAG_COLORMAP:
  721. *va_arg(ap, uint16**) = td->td_colormap[0];
  722. *va_arg(ap, uint16**) = td->td_colormap[1];
  723. *va_arg(ap, uint16**) = td->td_colormap[2];
  724. break;
  725. case TIFFTAG_STRIPOFFSETS:
  726. case TIFFTAG_TILEOFFSETS:
  727. *va_arg(ap, uint32**) = td->td_stripoffset;
  728. break;
  729. case TIFFTAG_STRIPBYTECOUNTS:
  730. case TIFFTAG_TILEBYTECOUNTS:
  731. *va_arg(ap, uint32**) = td->td_stripbytecount;
  732. break;
  733. case TIFFTAG_MATTEING:
  734. *va_arg(ap, uint16*) =
  735. (td->td_extrasamples == 1 &&
  736. td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
  737. break;
  738. case TIFFTAG_EXTRASAMPLES:
  739. *va_arg(ap, uint16*) = td->td_extrasamples;
  740. *va_arg(ap, uint16**) = td->td_sampleinfo;
  741. break;
  742. case TIFFTAG_TILEWIDTH:
  743. *va_arg(ap, uint32*) = td->td_tilewidth;
  744. break;
  745. case TIFFTAG_TILELENGTH:
  746. *va_arg(ap, uint32*) = td->td_tilelength;
  747. break;
  748. case TIFFTAG_TILEDEPTH:
  749. *va_arg(ap, uint32*) = td->td_tiledepth;
  750. break;
  751. case TIFFTAG_DATATYPE:
  752. switch (td->td_sampleformat) {
  753. case SAMPLEFORMAT_UINT:
  754. *va_arg(ap, uint16*) = DATATYPE_UINT;
  755. break;
  756. case SAMPLEFORMAT_INT:
  757. *va_arg(ap, uint16*) = DATATYPE_INT;
  758. break;
  759. case SAMPLEFORMAT_IEEEFP:
  760. *va_arg(ap, uint16*) = DATATYPE_IEEEFP;
  761. break;
  762. case SAMPLEFORMAT_VOID:
  763. *va_arg(ap, uint16*) = DATATYPE_VOID;
  764. break;
  765. }
  766. break;
  767. case TIFFTAG_SAMPLEFORMAT:
  768. *va_arg(ap, uint16*) = td->td_sampleformat;
  769. break;
  770. case TIFFTAG_IMAGEDEPTH:
  771. *va_arg(ap, uint32*) = td->td_imagedepth;
  772. break;
  773. case TIFFTAG_SUBIFD:
  774. *va_arg(ap, uint16*) = td->td_nsubifd;
  775. *va_arg(ap, uint32**) = td->td_subifd;
  776. break;
  777. case TIFFTAG_YCBCRPOSITIONING:
  778. *va_arg(ap, uint16*) = td->td_ycbcrpositioning;
  779. break;
  780. case TIFFTAG_YCBCRSUBSAMPLING:
  781. *va_arg(ap, uint16*) = td->td_ycbcrsubsampling[0];
  782. *va_arg(ap, uint16*) = td->td_ycbcrsubsampling[1];
  783. break;
  784. case TIFFTAG_TRANSFERFUNCTION:
  785. *va_arg(ap, uint16**) = td->td_transferfunction[0];
  786. if (td->td_samplesperpixel - td->td_extrasamples > 1) {
  787. *va_arg(ap, uint16**) = td->td_transferfunction[1];
  788. *va_arg(ap, uint16**) = td->td_transferfunction[2];
  789. }
  790. break;
  791. case TIFFTAG_REFERENCEBLACKWHITE:
  792. *va_arg(ap, float**) = td->td_refblackwhite;
  793. break;
  794. case TIFFTAG_INKNAMES:
  795. *va_arg(ap, char**) = td->td_inknames;
  796. break;
  797. default:
  798. {
  799. const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
  800. int i;
  801. /*
  802. * This can happen if multiple images are open with different
  803. * codecs which have private tags. The global tag information
  804. * table may then have tags that are valid for one file but not
  805. * the other. If the client tries to get a tag that is not valid
  806. * for the image's codec then we'll arrive here.
  807. */
  808. if( fip == NULL || fip->field_bit != FIELD_CUSTOM )
  809. {
  810. TIFFErrorExt(tif->tif_clientdata, "_TIFFVGetField",
  811. "%s: Invalid %stag \"%s\" "
  812. "(not supported by codec)",
  813. tif->tif_name,
  814. isPseudoTag(tag) ? "pseudo-" : "",
  815. fip ? fip->field_name : "Unknown");
  816. ret_val = 0;
  817. break;
  818. }
  819. /*
  820. * Do we have a custom value?
  821. */
  822. ret_val = 0;
  823. for (i = 0; i < td->td_customValueCount; i++) {
  824. TIFFTagValue *tv = td->td_customValues + i;
  825. if (tv->info->field_tag != tag)
  826. continue;
  827. if (fip->field_passcount) {
  828. if (fip->field_readcount == TIFF_VARIABLE2)
  829. *va_arg(ap, uint32*) = (uint32)tv->count;
  830. else /* Assume TIFF_VARIABLE */
  831. *va_arg(ap, uint16*) = (uint16)tv->count;
  832. *va_arg(ap, void **) = tv->value;
  833. ret_val = 1;
  834. } else {
  835. if ((fip->field_type == TIFF_ASCII
  836. || fip->field_readcount == TIFF_VARIABLE
  837. || fip->field_readcount == TIFF_VARIABLE2
  838. || fip->field_readcount == TIFF_SPP
  839. || tv->count > 1)
  840. && fip->field_tag != TIFFTAG_PAGENUMBER
  841. && fip->field_tag != TIFFTAG_HALFTONEHINTS
  842. && fip->field_tag != TIFFTAG_YCBCRSUBSAMPLING
  843. && fip->field_tag != TIFFTAG_DOTRANGE) {
  844. *va_arg(ap, void **) = tv->value;
  845. ret_val = 1;
  846. } else {
  847. int j;
  848. char *val = (char *)tv->value;
  849. for (j = 0; j < tv->count;
  850. j++, val += _TIFFDataSize(tv->info->field_type)) {
  851. switch (fip->field_type) {
  852. case TIFF_BYTE:
  853. case TIFF_UNDEFINED:
  854. *va_arg(ap, uint8*) =
  855. *(uint8 *)val;
  856. ret_val = 1;
  857. break;
  858. case TIFF_SBYTE:
  859. *va_arg(ap, int8*) =
  860. *(int8 *)val;
  861. ret_val = 1;
  862. break;
  863. case TIFF_SHORT:
  864. *va_arg(ap, uint16*) =
  865. *(uint16 *)val;
  866. ret_val = 1;
  867. break;
  868. case TIFF_SSHORT:
  869. *va_arg(ap, int16*) =
  870. *(int16 *)val;
  871. ret_val = 1;
  872. break;
  873. case TIFF_LONG:
  874. case TIFF_IFD:
  875. *va_arg(ap, uint32*) =
  876. *(uint32 *)val;
  877. ret_val = 1;
  878. break;
  879. case TIFF_SLONG:
  880. *va_arg(ap, int32*) =
  881. *(int32 *)val;
  882. ret_val = 1;
  883. break;
  884. case TIFF_RATIONAL:
  885. case TIFF_SRATIONAL:
  886. case TIFF_FLOAT:
  887. *va_arg(ap, float*) =
  888. *(float *)val;
  889. ret_val = 1;
  890. break;
  891. case TIFF_DOUBLE:
  892. *va_arg(ap, double*) =
  893. *(double *)val;
  894. ret_val = 1;
  895. break;
  896. default:
  897. ret_val = 0;
  898. break;
  899. }
  900. }
  901. }
  902. }
  903. break;
  904. }
  905. }
  906. }
  907. return(ret_val);
  908. }
  909. /*
  910. * Return the value of a field in the
  911. * internal directory structure.
  912. */
  913. int
  914. TIFFGetField(TIFF* tif, ttag_t tag, ...)
  915. {
  916. int status;
  917. va_list ap;
  918. va_start(ap, tag);
  919. status = TIFFVGetField(tif, tag, ap);
  920. va_end(ap);
  921. return (status);
  922. }
  923. /*
  924. * Like TIFFGetField, but taking a varargs
  925. * parameter list. This routine is useful
  926. * for building higher-level interfaces on
  927. * top of the library.
  928. */
  929. int
  930. TIFFVGetField(TIFF* tif, ttag_t tag, va_list ap)
  931. {
  932. const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
  933. return (fip && (isPseudoTag(tag) || TIFFFieldSet(tif, fip->field_bit)) ?
  934. (*tif->tif_tagmethods.vgetfield)(tif, tag, ap) : 0);
  935. }
  936. #define CleanupField(member) { \
  937. if (td->member) { \
  938. _TIFFfree(td->member); \
  939. td->member = 0; \
  940. } \
  941. }
  942. /*
  943. * Release storage associated with a directory.
  944. */
  945. void
  946. TIFFFreeDirectory(TIFF* tif)
  947. {
  948. TIFFDirectory *td = &tif->tif_dir;
  949. int i;
  950. _TIFFmemset(td->td_fieldsset, 0, FIELD_SETLONGS);
  951. CleanupField(td_colormap[0]);
  952. CleanupField(td_colormap[1]);
  953. CleanupField(td_colormap[2]);
  954. CleanupField(td_sampleinfo);
  955. CleanupField(td_subifd);
  956. CleanupField(td_inknames);
  957. CleanupField(td_refblackwhite);
  958. CleanupField(td_transferfunction[0]);
  959. CleanupField(td_transferfunction[1]);
  960. CleanupField(td_transferfunction[2]);
  961. CleanupField(td_stripoffset);
  962. CleanupField(td_stripbytecount);
  963. TIFFClrFieldBit(tif, FIELD_YCBCRSUBSAMPLING);
  964. TIFFClrFieldBit(tif, FIELD_YCBCRPOSITIONING);
  965. /* Cleanup custom tag values */
  966. for( i = 0; i < td->td_customValueCount; i++ ) {
  967. if (td->td_customValues[i].value)
  968. _TIFFfree(td->td_customValues[i].value);
  969. }
  970. td->td_customValueCount = 0;
  971. CleanupField(td_customValues);
  972. }
  973. #undef CleanupField
  974. /*
  975. * Client Tag extension support (from Niles Ritter).
  976. */
  977. static TIFFExtendProc _TIFFextender = (TIFFExtendProc) NULL;
  978. TIFFExtendProc
  979. TIFFSetTagExtender(TIFFExtendProc extender)
  980. {
  981. TIFFExtendProc prev = _TIFFextender;
  982. _TIFFextender = extender;
  983. return (prev);
  984. }
  985. /*
  986. * Setup for a new directory. Should we automatically call
  987. * TIFFWriteDirectory() if the current one is dirty?
  988. *
  989. * The newly created directory will not exist on the file till
  990. * TIFFWriteDirectory(), TIFFFlush() or TIFFClose() is called.
  991. */
  992. int
  993. TIFFCreateDirectory(TIFF* tif)
  994. {
  995. TIFFDefaultDirectory(tif);
  996. tif->tif_diroff = 0;
  997. tif->tif_nextdiroff = 0;
  998. tif->tif_curoff = 0;
  999. tif->tif_row = (uint32) -1;
  1000. tif->tif_curstrip = (tstrip_t) -1;
  1001. return 0;
  1002. }
  1003. /*
  1004. * Setup a default directory structure.
  1005. */
  1006. int
  1007. TIFFDefaultDirectory(TIFF* tif)
  1008. {
  1009. register TIFFDirectory* td = &tif->tif_dir;
  1010. size_t tiffFieldInfoCount;
  1011. const TIFFFieldInfo *tiffFieldInfo =
  1012. _TIFFGetFieldInfo(&tiffFieldInfoCount);
  1013. _TIFFSetupFieldInfo(tif, tiffFieldInfo, tiffFieldInfoCount);
  1014. _TIFFmemset(td, 0, sizeof (*td));
  1015. td->td_fillorder = FILLORDER_MSB2LSB;
  1016. td->td_bitspersample = 1;
  1017. td->td_threshholding = THRESHHOLD_BILEVEL;
  1018. td->td_orientation = ORIENTATION_TOPLEFT;
  1019. td->td_samplesperpixel = 1;
  1020. td->td_rowsperstrip = (uint32) -1;
  1021. td->td_tilewidth = 0;
  1022. td->td_tilelength = 0;
  1023. td->td_tiledepth = 1;
  1024. td->td_stripbytecountsorted = 1; /* Our own arrays always sorted. */
  1025. td->td_resolutionunit = RESUNIT_INCH;
  1026. td->td_sampleformat = SAMPLEFORMAT_UINT;
  1027. td->td_imagedepth = 1;
  1028. td->td_ycbcrsubsampling[0] = 2;
  1029. td->td_ycbcrsubsampling[1] = 2;
  1030. td->td_ycbcrpositioning = YCBCRPOSITION_CENTERED;
  1031. tif->tif_postdecode = _TIFFNoPostDecode;
  1032. tif->tif_foundfield = NULL;
  1033. tif->tif_tagmethods.vsetfield = _TIFFVSetField;
  1034. tif->tif_tagmethods.vgetfield = _TIFFVGetField;
  1035. tif->tif_tagmethods.printdir = NULL;
  1036. /*
  1037. * Give client code a chance to install their own
  1038. * tag extensions & methods, prior to compression overloads.
  1039. */
  1040. if (_TIFFextender)
  1041. (*_TIFFextender)(tif);
  1042. (void) TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
  1043. /*
  1044. * NB: The directory is marked dirty as a result of setting
  1045. * up the default compression scheme. However, this really
  1046. * isn't correct -- we want TIFF_DIRTYDIRECT to be set only
  1047. * if the user does something. We could just do the setup
  1048. * by hand, but it seems better to use the normal mechanism
  1049. * (i.e. TIFFSetField).
  1050. */
  1051. tif->tif_flags &= ~TIFF_DIRTYDIRECT;
  1052. /*
  1053. * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19
  1054. * we clear the ISTILED flag when setting up a new directory.
  1055. * Should we also be clearing stuff like INSUBIFD?
  1056. */
  1057. tif->tif_flags &= ~TIFF_ISTILED;
  1058. /*
  1059. * Clear other directory-specific fields.
  1060. */
  1061. tif->tif_tilesize = -1;
  1062. tif->tif_scanlinesize = -1;
  1063. return (1);
  1064. }
  1065. static int
  1066. TIFFAdvanceDirectory(TIFF* tif, uint32* nextdir, toff_t* off)
  1067. {
  1068. static const char module[] = "TIFFAdvanceDirectory";
  1069. uint16 dircount;
  1070. if (isMapped(tif))
  1071. {
  1072. toff_t poff=*nextdir;
  1073. if (poff+sizeof(uint16) > tif->tif_size)
  1074. {
  1075. TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory count",
  1076. tif->tif_name);
  1077. return (0);
  1078. }
  1079. _TIFFmemcpy(&dircount, tif->tif_base+poff, sizeof (uint16));
  1080. if (tif->tif_flags & TIFF_SWAB)
  1081. TIFFSwabShort(&dircount);
  1082. poff+=sizeof (uint16)+dircount*sizeof (TIFFDirEntry);
  1083. if (off != NULL)
  1084. *off = poff;
  1085. if (((toff_t) (poff+sizeof (uint32))) > tif->tif_size)
  1086. {
  1087. TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory link",
  1088. tif->tif_name);
  1089. return (0);
  1090. }
  1091. _TIFFmemcpy(nextdir, tif->tif_base+poff, sizeof (uint32));
  1092. if (tif->tif_flags & TIFF_SWAB)
  1093. TIFFSwabLong(nextdir);
  1094. return (1);
  1095. }
  1096. else
  1097. {
  1098. if (!SeekOK(tif, *nextdir) ||
  1099. !ReadOK(tif, &dircount, sizeof (uint16))) {
  1100. TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory count",
  1101. tif->tif_name);
  1102. return (0);
  1103. }
  1104. if (tif->tif_flags & TIFF_SWAB)
  1105. TIFFSwabShort(&dircount);
  1106. if (off != NULL)
  1107. *off = TIFFSeekFile(tif,
  1108. dircount*sizeof (TIFFDirEntry), SEEK_CUR);
  1109. else
  1110. (void) TIFFSeekFile(tif,
  1111. dircount*sizeof (TIFFDirEntry), SEEK_CUR);
  1112. if (!ReadOK(tif, nextdir, sizeof (uint32))) {
  1113. TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory link",
  1114. tif->tif_name);
  1115. return (0);
  1116. }
  1117. if (tif->tif_flags & TIFF_SWAB)
  1118. TIFFSwabLong(nextdir);
  1119. return (1);
  1120. }
  1121. }
  1122. /*
  1123. * Count the number of directories in a file.
  1124. */
  1125. tdir_t
  1126. TIFFNumberOfDirectories(TIFF* tif)
  1127. {
  1128. toff_t nextdir = tif->tif_header.tiff_diroff;
  1129. tdir_t n = 0;
  1130. while (nextdir != 0 && TIFFAdvanceDirectory(tif, &nextdir, NULL))
  1131. n++;
  1132. return (n);
  1133. }
  1134. /*
  1135. * Set the n-th directory as the current directory.
  1136. * NB: Directories are numbered starting at 0.
  1137. */
  1138. int
  1139. TIFFSetDirectory(TIFF* tif, tdir_t dirn)
  1140. {
  1141. toff_t nextdir;
  1142. tdir_t n;
  1143. nextdir = tif->tif_header.tiff_diroff;
  1144. for (n = dirn; n > 0 && nextdir != 0; n--)
  1145. if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
  1146. return (0);
  1147. tif->tif_nextdiroff = nextdir;
  1148. /*
  1149. * Set curdir to the actual directory index. The
  1150. * -1 is because TIFFReadDirectory will increment
  1151. * tif_curdir after successfully reading the directory.
  1152. */
  1153. tif->tif_curdir = (dirn - n) - 1;
  1154. /*
  1155. * Reset tif_dirnumber counter and start new list of seen directories.
  1156. * We need this to prevent IFD loops.
  1157. */
  1158. tif->tif_dirnumber = 0;
  1159. return (TIFFReadDirectory(tif));
  1160. }
  1161. /*
  1162. * Set the current directory to be the directory
  1163. * located at the specified file offset. This interface
  1164. * is used mainly to access directories linked with
  1165. * the SubIFD tag (e.g. thumbnail images).
  1166. */
  1167. int
  1168. TIFFSetSubDirectory(TIFF* tif, uint32 diroff)
  1169. {
  1170. tif->tif_nextdiroff = diroff;
  1171. /*
  1172. * Reset tif_dirnumber counter and start new list of seen directories.
  1173. * We need this to prevent IFD loops.
  1174. */
  1175. tif->tif_dirnumber = 0;
  1176. return (TIFFReadDirectory(tif));
  1177. }
  1178. /*
  1179. * Return file offset of the current directory.
  1180. */
  1181. uint32
  1182. TIFFCurrentDirOffset(TIFF* tif)
  1183. {
  1184. return (tif->tif_diroff);
  1185. }
  1186. /*
  1187. * Return an indication of whether or not we are
  1188. * at the last directory in the file.
  1189. */
  1190. int
  1191. TIFFLastDirectory(TIFF* tif)
  1192. {
  1193. return (tif->tif_nextdiroff == 0);
  1194. }
  1195. /*
  1196. * Unlink the specified directory from the directory chain.
  1197. */
  1198. int
  1199. TIFFUnlinkDirectory(TIFF* tif, tdir_t dirn)
  1200. {
  1201. static const char module[] = "TIFFUnlinkDirectory";
  1202. toff_t nextdir;
  1203. toff_t off;
  1204. tdir_t n;
  1205. if (tif->tif_mode == O_RDONLY) {
  1206. TIFFErrorExt(tif->tif_clientdata, module,
  1207. "Can not unlink directory in read-only file");
  1208. return (0);
  1209. }
  1210. /*
  1211. * Go to the directory before the one we want
  1212. * to unlink and nab the offset of the link
  1213. * field we'll need to patch.
  1214. */
  1215. nextdir = tif->tif_header.tiff_diroff;
  1216. off = sizeof (uint16) + sizeof (uint16);
  1217. for (n = dirn-1; n > 0; n--) {
  1218. if (nextdir == 0) {
  1219. TIFFErrorExt(tif->tif_clientdata, module, "Directory %d does not exist", dirn);
  1220. return (0);
  1221. }
  1222. if (!TIFFAdvanceDirectory(tif, &nextdir, &off))
  1223. return (0);
  1224. }
  1225. /*
  1226. * Advance to the directory to be unlinked and fetch
  1227. * the offset of the directory that follows.
  1228. */
  1229. if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
  1230. return (0);
  1231. /*
  1232. * Go back and patch the link field of the preceding
  1233. * directory to point to the offset of the directory
  1234. * that follows.
  1235. */
  1236. (void) TIFFSeekFile(tif, off, SEEK_SET);
  1237. if (tif->tif_flags & TIFF_SWAB)
  1238. TIFFSwabLong(&nextdir);
  1239. if (!WriteOK(tif, &nextdir, sizeof (uint32))) {
  1240. TIFFErrorExt(tif->tif_clientdata, module, "Error writing directory link");
  1241. return (0);
  1242. }
  1243. /*
  1244. * Leave directory state setup safely. We don't have
  1245. * facilities for doing inserting and removing directories,
  1246. * so it's safest to just invalidate everything. This
  1247. * means that the caller can only append to the directory
  1248. * chain.
  1249. */
  1250. (*tif->tif_cleanup)(tif);
  1251. if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
  1252. _TIFFfree(tif->tif_rawdata);
  1253. tif->tif_rawdata = NULL;
  1254. tif->tif_rawcc = 0;
  1255. }
  1256. tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP|TIFF_POSTENCODE);
  1257. TIFFFreeDirectory(tif);
  1258. TIFFDefaultDirectory(tif);
  1259. tif->tif_diroff = 0; /* force link on next write */
  1260. tif->tif_nextdiroff = 0; /* next write must be at end */
  1261. tif->tif_curoff = 0;
  1262. tif->tif_row = (uint32) -1;
  1263. tif->tif_curstrip = (tstrip_t) -1;
  1264. return (1);
  1265. }
  1266. /* [BFC]
  1267. *
  1268. * Author: Bruce Cameron <cameron@petris.com>
  1269. *
  1270. * Set a table of tags that are to be replaced during directory process by the
  1271. * 'IGNORE' state - or return TRUE/FALSE for the requested tag such that
  1272. * 'ReadDirectory' can use the stored information.
  1273. *
  1274. * FIXME: this is never used properly. Should be removed in the future.
  1275. */
  1276. int
  1277. TIFFReassignTagToIgnore (enum TIFFIgnoreSense task, int TIFFtagID)
  1278. {
  1279. static int TIFFignoretags [FIELD_LAST];
  1280. static int tagcount = 0 ;
  1281. int i; /* Loop index */
  1282. int j; /* Loop index */
  1283. switch (task)
  1284. {
  1285. case TIS_STORE:
  1286. if ( tagcount < (FIELD_LAST - 1) )
  1287. {
  1288. for ( j = 0 ; j < tagcount ; ++j )
  1289. { /* Do not add duplicate tag */
  1290. if ( TIFFignoretags [j] == TIFFtagID )
  1291. return (TRUE) ;
  1292. }
  1293. TIFFignoretags [tagcount++] = TIFFtagID ;
  1294. return (TRUE) ;
  1295. }
  1296. break ;
  1297. case TIS_EXTRACT:
  1298. for ( i = 0 ; i < tagcount ; ++i )
  1299. {
  1300. if ( TIFFignoretags [i] == TIFFtagID )
  1301. return (TRUE) ;
  1302. }
  1303. break;
  1304. case TIS_EMPTY:
  1305. tagcount = 0 ; /* Clear the list */
  1306. return (TRUE) ;
  1307. default:
  1308. break;
  1309. }
  1310. return (FALSE);
  1311. }
  1312. /* vim: set ts=8 sts=8 sw=8 noet: */
  1313. /*
  1314. * Local Variables:
  1315. * mode: c
  1316. * c-basic-offset: 8
  1317. * fill-column: 78
  1318. * End:
  1319. */