2
0

tif_dir.c 45 KB

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