tif_pdsdirwrite.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /* $Header: /cvs/maptools/cvsroot/libtiff/contrib/pds/tif_pdsdirwrite.c,v 1.4 2010-06-08 18:55:15 bfriesen Exp $ */
  2. /* When writing data to TIFF files, it is often useful to store application-
  3. specific data in a private TIFF directory so that the tags don't need to
  4. be registered and won't conflict with other people's user-defined tags.
  5. One needs to have a registered public tag which contains some amount of
  6. raw data. That raw data, however, is interpreted at an independent,
  7. separate, private tiff directory. This file provides some routines which
  8. will be useful for converting that data from its raw binary form into
  9. the proper form for your application.
  10. */
  11. /*
  12. * Copyright (c) 1988-1996 Sam Leffler
  13. * Copyright (c) 1991-1996 Silicon Graphics, Inc.
  14. * Copyright (c( 1996 USAF Phillips Laboratory
  15. *
  16. * Permission to use, copy, modify, distribute, and sell this software and
  17. * its documentation for any purpose is hereby granted without fee, provided
  18. * that (i) the above copyright notices and this permission notice appear in
  19. * all copies of the software and related documentation, and (ii) the names of
  20. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  21. * publicity relating to the software without the specific, prior written
  22. * permission of Sam Leffler and Silicon Graphics.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  26. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  27. *
  28. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  29. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  30. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  31. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  32. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  33. * OF THIS SOFTWARE.
  34. */
  35. /*
  36. * TIFF Library.
  37. *
  38. * These routines written by Conrad J. Poelman on a single late-night of
  39. * March 20-21, 1996.
  40. *
  41. * The entire purpose of this file is to provide a single external function,
  42. * TIFFWritePrivateDataSubDirectory(). This function is intended for use
  43. * in writing a private subdirectory structure into a TIFF file. The
  44. * actual reading of data from the structure is handled by the getFieldFn(),
  45. * which is passed to TIFFWritePrivateDataSubDirectory() as a parameter. The
  46. * idea is to enable any application wishing to read private subdirectories to
  47. * do so easily using this function, without modifying the TIFF library.
  48. *
  49. * The astute observer will notice that only two functions are at all different
  50. * from the original tif_dirwrite.c file: TIFFWritePrivateDataSubDirectory()and
  51. * TIFFWriteNormalSubTag(). All the other stuff that makes this file so huge
  52. * is only necessary because all of those functions are declared static in
  53. * tif_dirwrite.c, so we have to totally duplicate them in order to use them.
  54. *
  55. * Oh, also please note the bug-fix in the routine TIFFWriteNormalSubTag(),
  56. * which equally should be applied to TIFFWriteNormalTag().
  57. *
  58. */
  59. #include "tiffiop.h"
  60. #if HAVE_IEEEFP
  61. #define TIFFCvtNativeToIEEEFloat(tif, n, fp)
  62. #define TIFFCvtNativeToIEEEDouble(tif, n, dp)
  63. #else
  64. extern void TIFFCvtNativeToIEEEFloat(TIFF*, uint32, float*);
  65. extern void TIFFCvtNativeToIEEEDouble(TIFF*, uint32, double*);
  66. #endif
  67. static int TIFFWriteNormalTag(TIFF*, TIFFDirEntry*, const TIFFFieldInfo*);
  68. static int TIFFWriteNormalSubTag(TIFF*, TIFFDirEntry*, const TIFFFieldInfo*,
  69. int (*getFieldFn)(TIFF *tif,ttag_t tag,...));
  70. static void TIFFSetupShortLong(TIFF*, ttag_t, TIFFDirEntry*, uint32);
  71. static int TIFFSetupShortPair(TIFF*, ttag_t, TIFFDirEntry*);
  72. static int TIFFWritePerSampleShorts(TIFF*, ttag_t, TIFFDirEntry*);
  73. static int TIFFWritePerSampleAnys(TIFF*, TIFFDataType, ttag_t, TIFFDirEntry*);
  74. static int TIFFWriteShortTable(TIFF*, ttag_t, TIFFDirEntry*, uint32, uint16**);
  75. static int TIFFWriteShortArray(TIFF*,
  76. TIFFDataType, ttag_t, TIFFDirEntry*, uint32, uint16*);
  77. static int TIFFWriteLongArray(TIFF *,
  78. TIFFDataType, ttag_t, TIFFDirEntry*, uint32, uint32*);
  79. static int TIFFWriteRationalArray(TIFF *,
  80. TIFFDataType, ttag_t, TIFFDirEntry*, uint32, float*);
  81. static int TIFFWriteFloatArray(TIFF *,
  82. TIFFDataType, ttag_t, TIFFDirEntry*, uint32, float*);
  83. static int TIFFWriteDoubleArray(TIFF *,
  84. TIFFDataType, ttag_t, TIFFDirEntry*, uint32, double*);
  85. static int TIFFWriteByteArray(TIFF*, TIFFDirEntry*, char*);
  86. static int TIFFWriteAnyArray(TIFF*,
  87. TIFFDataType, ttag_t, TIFFDirEntry*, uint32, double*);
  88. #ifdef COLORIMETRY_SUPPORT
  89. static int TIFFWriteTransferFunction(TIFF*, TIFFDirEntry*);
  90. #endif
  91. static int TIFFWriteData(TIFF*, TIFFDirEntry*, char*);
  92. static int TIFFLinkDirectory(TIFF*);
  93. #define WriteRationalPair(type, tag1, v1, tag2, v2) { \
  94. if (!TIFFWriteRational(tif, type, tag1, dir, v1)) \
  95. goto bad; \
  96. if (!TIFFWriteRational(tif, type, tag2, dir+1, v2)) \
  97. goto bad; \
  98. dir++; \
  99. }
  100. #define TIFFWriteRational(tif, type, tag, dir, v) \
  101. TIFFWriteRationalArray((tif), (type), (tag), (dir), 1, &(v))
  102. #ifndef TIFFWriteRational
  103. static int TIFFWriteRational(TIFF*,
  104. TIFFDataType, ttag_t, TIFFDirEntry*, float);
  105. #endif
  106. /* This function will write an entire directory to the disk, and return the
  107. offset value indicating where in the file it wrote the beginning of the
  108. directory structure. This is NOT the same as the offset value before
  109. calling this function, because some of the fields may have caused various
  110. data items to be written out BEFORE writing the directory structure.
  111. This code was basically written by ripping of the TIFFWriteDirectory()
  112. code and generalizing it, using RPS's TIFFWritePliIfd() code for
  113. inspiration. My original goal was to make this code general enough that
  114. the original TIFFWriteDirectory() could be rewritten to just call this
  115. function with the appropriate field and field-accessing arguments.
  116. However, now I realize that there's a lot of code that gets executed for
  117. the main, standard TIFF directories that does not apply to special
  118. private subdirectories, so such a reimplementation for the sake of
  119. eliminating redundant or duplicate code is probably not possible,
  120. unless we also pass in a Main flag to indiciate which type of handling
  121. to do, which would be kind of a hack. I've marked those places where I
  122. changed or ripped out code which would have to be re-inserted to
  123. generalize this function. If it can be done in a clean and graceful way,
  124. it would be a great way to generalize the TIFF library. Otherwise, I'll
  125. just leave this code here where it duplicates but remains on top of and
  126. hopefully mostly independent of the main TIFF library.
  127. The caller will probably want to free the sub directory structure after
  128. returning from this call, since otherwise once written out, the user
  129. is likely to forget about it and leave data lying around.
  130. */
  131. toff_t
  132. TIFFWritePrivateDataSubDirectory(TIFF* tif,
  133. uint32 pdir_fieldsset[], int pdir_fields_last,
  134. TIFFFieldInfo *field_info,
  135. int (*getFieldFn)(TIFF *tif, ttag_t tag, ...))
  136. {
  137. uint16 dircount;
  138. uint32 diroff, nextdiroff;
  139. ttag_t tag;
  140. uint32 nfields;
  141. tsize_t dirsize;
  142. char* data;
  143. TIFFDirEntry* dir;
  144. u_long b, *fields, fields_size;
  145. toff_t directory_offset;
  146. TIFFFieldInfo* fip;
  147. /*
  148. * Deleted out all of the encoder flushing and such code from here -
  149. * not necessary for subdirectories.
  150. */
  151. /* Finish writing out any image data. */
  152. TIFFFlushData(tif);
  153. /*
  154. * Size the directory so that we can calculate
  155. * offsets for the data items that aren't kept
  156. * in-place in each field.
  157. */
  158. nfields = 0;
  159. for (b = 0; b <= pdir_fields_last; b++)
  160. if (FieldSet(pdir_fieldsset, b))
  161. /* Deleted code to make size of first 4 tags 2
  162. instead of 1. */
  163. nfields += 1;
  164. dirsize = nfields * sizeof (TIFFDirEntry);
  165. data = (char*) _TIFFmalloc(dirsize);
  166. if (data == NULL) {
  167. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  168. "Cannot write private subdirectory, out of space");
  169. return (0);
  170. }
  171. /*
  172. * Place directory in data section of the file. If there isn't one
  173. * yet, place it at the end of the file. The directory is treated as
  174. * data, so we don't link it into the directory structure at all.
  175. */
  176. if (tif->tif_dataoff == 0)
  177. tif->tif_dataoff =(TIFFSeekFile(tif, (toff_t) 0, SEEK_END)+1) &~ 1;
  178. diroff = tif->tif_dataoff;
  179. tif->tif_dataoff = (toff_t)(
  180. diroff + sizeof (uint16) + dirsize + sizeof (toff_t));
  181. if (tif->tif_dataoff & 1)
  182. tif->tif_dataoff++;
  183. (void) TIFFSeekFile(tif, tif->tif_dataoff, SEEK_SET);
  184. /*tif->tif_curdir++;*/
  185. dir = (TIFFDirEntry*) data;
  186. /*
  187. * Setup external form of directory
  188. * entries and write data items.
  189. */
  190. /*
  191. * We make a local copy of the fieldsset here so that we don't mess
  192. * up the original one when we call ResetFieldBit(). But I'm not sure
  193. * why the original code calls ResetFieldBit(), since we're already
  194. * going through the fields in order...
  195. *
  196. * fields_size is the number of uint32's we will need to hold the
  197. * bit-mask for all of the fields. If our highest field number is
  198. * 100, then we'll need 100 / (8*4)+1 == 4 uint32's to hold the
  199. * fieldset.
  200. *
  201. * Unlike the original code, we allocate fields dynamically based
  202. * on the requested pdir_fields_last value, allowing private
  203. * data subdirectories to contain more than the built-in code's limit
  204. * of 95 tags in a directory.
  205. */
  206. fields_size = pdir_fields_last / (8*sizeof(uint32)) + 1;
  207. fields = _TIFFmalloc(fields_size*sizeof(uint32));
  208. _TIFFmemcpy(fields, pdir_fieldsset, fields_size * sizeof(uint32));
  209. /* Deleted "write out extra samples tag" code here. */
  210. /* Deleted code for checking a billion little special cases for the
  211. * standard TIFF tags. Should add a general mechanism for overloading
  212. * write function for each field, just like Brian kept telling me!!!
  213. */
  214. for (fip = field_info; fip->field_tag; fip++) {
  215. /* Deleted code to check for FIELD_IGNORE!! */
  216. if (/* fip->field_bit == FIELD_IGNORE || */
  217. !FieldSet(fields, fip->field_bit))
  218. continue;
  219. if (!TIFFWriteNormalSubTag(tif, dir, fip, getFieldFn))
  220. goto bad;
  221. dir++;
  222. ResetFieldBit(fields, fip->field_bit);
  223. }
  224. /* Now we've written all of the referenced data, and are about to
  225. write the main directory structure, so grab the tif_dataoff value
  226. now so we can remember where we wrote the directory. */
  227. directory_offset = tif->tif_dataoff;
  228. /*
  229. * Write directory.
  230. */
  231. dircount = (uint16) nfields;
  232. /* Deleted code to link to the next directory - we set it to zero! */
  233. nextdiroff = 0;
  234. if (tif->tif_flags & TIFF_SWAB) {
  235. /*
  236. * The file's byte order is opposite to the
  237. * native machine architecture. We overwrite
  238. * the directory information with impunity
  239. * because it'll be released below after we
  240. * write it to the file. Note that all the
  241. * other tag construction routines assume that
  242. * we do this byte-swapping; i.e. they only
  243. * byte-swap indirect data.
  244. */
  245. for (dir = (TIFFDirEntry*) data; dircount; dir++, dircount--) {
  246. TIFFSwabArrayOfShort(&dir->tdir_tag, 2);
  247. TIFFSwabArrayOfLong(&dir->tdir_count, 2);
  248. }
  249. dircount = (uint16) nfields;
  250. TIFFSwabShort(&dircount);
  251. TIFFSwabLong(&nextdiroff);
  252. }
  253. (void) TIFFSeekFile(tif, tif->tif_dataoff, SEEK_SET);
  254. if (!WriteOK(tif, &dircount, sizeof (dircount))) {
  255. TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Error writing private subdirectory count");
  256. goto bad;
  257. }
  258. if (!WriteOK(tif, data, dirsize)) {
  259. TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Error writing private subdirectory contents");
  260. goto bad;
  261. }
  262. if (!WriteOK(tif, &nextdiroff, sizeof (nextdiroff))) {
  263. TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Error writing private subdirectory link");
  264. goto bad;
  265. }
  266. tif->tif_dataoff += sizeof(dircount) + dirsize + sizeof(nextdiroff);
  267. _TIFFfree(data);
  268. _TIFFfree(fields);
  269. tif->tif_flags &= ~TIFF_DIRTYDIRECT;
  270. #if (0)
  271. /* This stuff commented out because I don't think we want it for
  272. subdirectories, but I could be wrong. */
  273. (*tif->tif_cleanup)(tif);
  274. /*
  275. * Reset directory-related state for subsequent
  276. * directories.
  277. */
  278. TIFFDefaultDirectory(tif);
  279. tif->tif_curoff = 0;
  280. tif->tif_row = (uint32) -1;
  281. tif->tif_curstrip = (tstrip_t) -1;
  282. #endif
  283. return (directory_offset);
  284. bad:
  285. _TIFFfree(data);
  286. _TIFFfree(fields);
  287. return (0);
  288. }
  289. #undef WriteRationalPair
  290. /*
  291. * Process tags that are not special cased.
  292. */
  293. /* The standard function TIFFWriteNormalTag() could definitely be replaced
  294. with a simple call to this function, just adding TIFFGetField() as the
  295. last argument. */
  296. static int
  297. TIFFWriteNormalSubTag(TIFF* tif, TIFFDirEntry* dir, const TIFFFieldInfo* fip,
  298. int (*getFieldFn)(TIFF *tif, ttag_t tag, ...))
  299. {
  300. u_short wc = (u_short) fip->field_writecount;
  301. dir->tdir_tag = fip->field_tag;
  302. dir->tdir_type = (u_short) fip->field_type;
  303. dir->tdir_count = wc;
  304. #define WRITEF(x,y) x(tif, fip->field_type, fip->field_tag, dir, wc, y)
  305. switch (fip->field_type) {
  306. case TIFF_SHORT:
  307. case TIFF_SSHORT:
  308. if (wc > 1) {
  309. uint16* wp;
  310. if (wc == (u_short) TIFF_VARIABLE) {
  311. (*getFieldFn)(tif, fip->field_tag, &wc, &wp);
  312. dir->tdir_count = wc;
  313. } else
  314. (*getFieldFn)(tif, fip->field_tag, &wp);
  315. if (!WRITEF(TIFFWriteShortArray, wp))
  316. return (0);
  317. } else {
  318. uint16 sv;
  319. (*getFieldFn)(tif, fip->field_tag, &sv);
  320. dir->tdir_offset =
  321. TIFFInsertData(tif, dir->tdir_type, sv);
  322. }
  323. break;
  324. case TIFF_LONG:
  325. case TIFF_SLONG:
  326. if (wc > 1) {
  327. uint32* lp;
  328. if (wc == (u_short) TIFF_VARIABLE) {
  329. (*getFieldFn)(tif, fip->field_tag, &wc, &lp);
  330. dir->tdir_count = wc;
  331. } else
  332. (*getFieldFn)(tif, fip->field_tag, &lp);
  333. if (!WRITEF(TIFFWriteLongArray, lp))
  334. return (0);
  335. } else {
  336. /* XXX handle LONG->SHORT conversion */
  337. (*getFieldFn)(tif, fip->field_tag, &dir->tdir_offset);
  338. }
  339. break;
  340. case TIFF_RATIONAL:
  341. case TIFF_SRATIONAL:
  342. if (wc > 1) {
  343. float* fp;
  344. if (wc == (u_short) TIFF_VARIABLE) {
  345. (*getFieldFn)(tif, fip->field_tag, &wc, &fp);
  346. dir->tdir_count = wc;
  347. } else
  348. (*getFieldFn)(tif, fip->field_tag, &fp);
  349. if (!WRITEF(TIFFWriteRationalArray, fp))
  350. return (0);
  351. } else {
  352. float fv;
  353. (*getFieldFn)(tif, fip->field_tag, &fv);
  354. if (!WRITEF(TIFFWriteRationalArray, &fv))
  355. return (0);
  356. }
  357. break;
  358. case TIFF_FLOAT:
  359. if (wc > 1) {
  360. float* fp;
  361. if (wc == (u_short) TIFF_VARIABLE) {
  362. (*getFieldFn)(tif, fip->field_tag, &wc, &fp);
  363. dir->tdir_count = wc;
  364. } else
  365. (*getFieldFn)(tif, fip->field_tag, &fp);
  366. if (!WRITEF(TIFFWriteFloatArray, fp))
  367. return (0);
  368. } else {
  369. float fv;
  370. (*getFieldFn)(tif, fip->field_tag, &fv);
  371. if (!WRITEF(TIFFWriteFloatArray, &fv))
  372. return (0);
  373. }
  374. break;
  375. case TIFF_DOUBLE:
  376. /* Hey - I think this is a bug, or at least a "gross
  377. inconsistency", in the TIFF library. Look at the original
  378. TIFF library code below within the "#if (0) ... #else".
  379. Just from the type of *dp, you can see that this code
  380. expects TIFFGetField() to be handed a double ** for
  381. any TIFF_DOUBLE tag, even for the constant wc==1 case.
  382. This is totally inconsistent with other fields (like
  383. TIFF_FLOAT, above) and is also inconsistent with the
  384. TIFFSetField() function for TIFF_DOUBLEs, which expects
  385. to be passed a single double by value for the wc==1 case.
  386. (See the handling of TIFFFetchNormalTag() in tif_dirread.c
  387. for an example.) Maybe this function was written before
  388. TIFFWriteDoubleArray() was written, not that that's an
  389. excuse. Anyway, the new code below is a trivial modification
  390. of the TIFF_FLOAT code above. The fact that even single
  391. doubles get written out in the data segment and get an
  392. offset value stored is irrelevant here - that is all
  393. handled by TIFFWriteDoubleArray(). */
  394. #if (0)
  395. { double* dp;
  396. if (wc == (u_short) TIFF_VARIABLE) {
  397. (*getFieldFn)(tif, fip->field_tag, &wc, &dp);
  398. dir->tdir_count = wc;
  399. } else
  400. (*getFieldFn)(tif, fip->field_tag, &dp);
  401. TIFFCvtNativeToIEEEDouble(tif, wc, dp);
  402. if (!TIFFWriteData(tif, dir, (char*) dp))
  403. return (0);
  404. }
  405. #else
  406. if (wc > 1) {
  407. double* dp;
  408. if (wc == (u_short) TIFF_VARIABLE) {
  409. (*getFieldFn)(tif, fip->field_tag, &wc, &dp);
  410. dir->tdir_count = wc;
  411. } else
  412. (*getFieldFn)(tif, fip->field_tag, &dp);
  413. if (!WRITEF(TIFFWriteDoubleArray, dp))
  414. return (0);
  415. } else {
  416. double dv;
  417. (*getFieldFn)(tif, fip->field_tag, &dv);
  418. if (!WRITEF(TIFFWriteDoubleArray, &dv))
  419. return (0);
  420. }
  421. #endif
  422. break;
  423. case TIFF_ASCII:
  424. { char* cp;
  425. (*getFieldFn)(tif, fip->field_tag, &cp);
  426. dir->tdir_count = (uint32) (strlen(cp) + 1);
  427. if (!TIFFWriteByteArray(tif, dir, cp))
  428. return (0);
  429. }
  430. break;
  431. case TIFF_UNDEFINED:
  432. { char* cp;
  433. if (wc == (u_short) TIFF_VARIABLE) {
  434. (*getFieldFn)(tif, fip->field_tag, &wc, &cp);
  435. dir->tdir_count = wc;
  436. } else
  437. (*getFieldFn)(tif, fip->field_tag, &cp);
  438. if (!TIFFWriteByteArray(tif, dir, cp))
  439. return (0);
  440. }
  441. break;
  442. }
  443. return (1);
  444. }
  445. #undef WRITEF
  446. /* Everything after this is exactly duplicated from the standard tif_dirwrite.c
  447. file, necessitated by the fact that they are declared static there so
  448. we can't call them!
  449. */
  450. /*
  451. * Setup a directory entry with either a SHORT
  452. * or LONG type according to the value.
  453. */
  454. static void
  455. TIFFSetupShortLong(TIFF* tif, ttag_t tag, TIFFDirEntry* dir, uint32 v)
  456. {
  457. dir->tdir_tag = tag;
  458. dir->tdir_count = 1;
  459. if (v > 0xffffL) {
  460. dir->tdir_type = (short) TIFF_LONG;
  461. dir->tdir_offset = v;
  462. } else {
  463. dir->tdir_type = (short) TIFF_SHORT;
  464. dir->tdir_offset = TIFFInsertData(tif, (int) TIFF_SHORT, v);
  465. }
  466. }
  467. #undef MakeShortDirent
  468. #ifndef TIFFWriteRational
  469. /*
  470. * Setup a RATIONAL directory entry and
  471. * write the associated indirect value.
  472. */
  473. static int
  474. TIFFWriteRational(TIFF* tif,
  475. TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, float v)
  476. {
  477. return (TIFFWriteRationalArray(tif, type, tag, dir, 1, &v));
  478. }
  479. #endif
  480. #define NITEMS(x) (sizeof (x) / sizeof (x[0]))
  481. /*
  482. * Setup a directory entry that references a
  483. * samples/pixel array of SHORT values and
  484. * (potentially) write the associated indirect
  485. * values.
  486. */
  487. static int
  488. TIFFWritePerSampleShorts(TIFF* tif, ttag_t tag, TIFFDirEntry* dir)
  489. {
  490. uint16 buf[10], v;
  491. uint16* w = buf;
  492. int i, status, samples = tif->tif_dir.td_samplesperpixel;
  493. if (samples > NITEMS(buf))
  494. w = (uint16*) _TIFFmalloc(samples * sizeof (uint16));
  495. TIFFGetField(tif, tag, &v);
  496. for (i = 0; i < samples; i++)
  497. w[i] = v;
  498. status = TIFFWriteShortArray(tif, TIFF_SHORT, tag, dir, samples, w);
  499. if (w != buf)
  500. _TIFFfree((char*) w);
  501. return (status);
  502. }
  503. /*
  504. * Setup a directory entry that references a samples/pixel array of ``type''
  505. * values and (potentially) write the associated indirect values. The source
  506. * data from TIFFGetField() for the specified tag must be returned as double.
  507. */
  508. static int
  509. TIFFWritePerSampleAnys(TIFF* tif,
  510. TIFFDataType type, ttag_t tag, TIFFDirEntry* dir)
  511. {
  512. double buf[10], v;
  513. double* w = buf;
  514. int i, status;
  515. int samples = (int) tif->tif_dir.td_samplesperpixel;
  516. if (samples > NITEMS(buf))
  517. w = (double*) _TIFFmalloc(samples * sizeof (double));
  518. TIFFGetField(tif, tag, &v);
  519. for (i = 0; i < samples; i++)
  520. w[i] = v;
  521. status = TIFFWriteAnyArray(tif, type, tag, dir, samples, w);
  522. if (w != buf)
  523. _TIFFfree(w);
  524. return (status);
  525. }
  526. #undef NITEMS
  527. /*
  528. * Setup a pair of shorts that are returned by
  529. * value, rather than as a reference to an array.
  530. */
  531. static int
  532. TIFFSetupShortPair(TIFF* tif, ttag_t tag, TIFFDirEntry* dir)
  533. {
  534. uint16 v[2];
  535. TIFFGetField(tif, tag, &v[0], &v[1]);
  536. return (TIFFWriteShortArray(tif, TIFF_SHORT, tag, dir, 2, v));
  537. }
  538. /*
  539. * Setup a directory entry for an NxM table of shorts,
  540. * where M is known to be 2**bitspersample, and write
  541. * the associated indirect data.
  542. */
  543. static int
  544. TIFFWriteShortTable(TIFF* tif,
  545. ttag_t tag, TIFFDirEntry* dir, uint32 n, uint16** table)
  546. {
  547. uint32 i, off;
  548. dir->tdir_tag = tag;
  549. dir->tdir_type = (short) TIFF_SHORT;
  550. /* XXX -- yech, fool TIFFWriteData */
  551. dir->tdir_count = (uint32) (1L<<tif->tif_dir.td_bitspersample);
  552. off = tif->tif_dataoff;
  553. for (i = 0; i < n; i++)
  554. if (!TIFFWriteData(tif, dir, (char *)table[i]))
  555. return (0);
  556. dir->tdir_count *= n;
  557. dir->tdir_offset = off;
  558. return (1);
  559. }
  560. /*
  561. * Write/copy data associated with an ASCII or opaque tag value.
  562. */
  563. static int
  564. TIFFWriteByteArray(TIFF* tif, TIFFDirEntry* dir, char* cp)
  565. {
  566. if (dir->tdir_count > 4) {
  567. if (!TIFFWriteData(tif, dir, cp))
  568. return (0);
  569. } else
  570. _TIFFmemcpy(&dir->tdir_offset, cp, dir->tdir_count);
  571. return (1);
  572. }
  573. /*
  574. * Setup a directory entry of an array of SHORT
  575. * or SSHORT and write the associated indirect values.
  576. */
  577. static int
  578. TIFFWriteShortArray(TIFF* tif,
  579. TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, uint32 n, uint16* v)
  580. {
  581. dir->tdir_tag = tag;
  582. dir->tdir_type = (short) type;
  583. dir->tdir_count = n;
  584. if (n <= 2) {
  585. if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) {
  586. dir->tdir_offset = (uint32) ((long) v[0] << 16);
  587. if (n == 2)
  588. dir->tdir_offset |= v[1] & 0xffff;
  589. } else {
  590. dir->tdir_offset = v[0] & 0xffff;
  591. if (n == 2)
  592. dir->tdir_offset |= (long) v[1] << 16;
  593. }
  594. return (1);
  595. } else
  596. return (TIFFWriteData(tif, dir, (char*) v));
  597. }
  598. /*
  599. * Setup a directory entry of an array of LONG
  600. * or SLONG and write the associated indirect values.
  601. */
  602. static int
  603. TIFFWriteLongArray(TIFF* tif,
  604. TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, uint32 n, uint32* v)
  605. {
  606. dir->tdir_tag = tag;
  607. dir->tdir_type = (short) type;
  608. dir->tdir_count = n;
  609. if (n == 1) {
  610. dir->tdir_offset = v[0];
  611. return (1);
  612. } else
  613. return (TIFFWriteData(tif, dir, (char*) v));
  614. }
  615. /*
  616. * Setup a directory entry of an array of RATIONAL
  617. * or SRATIONAL and write the associated indirect values.
  618. */
  619. static int
  620. TIFFWriteRationalArray(TIFF* tif,
  621. TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, uint32 n, float* v)
  622. {
  623. uint32 i;
  624. uint32* t;
  625. int status;
  626. dir->tdir_tag = tag;
  627. dir->tdir_type = (short) type;
  628. dir->tdir_count = n;
  629. t = (uint32*) _TIFFmalloc(2*n * sizeof (uint32));
  630. for (i = 0; i < n; i++) {
  631. float fv = v[i];
  632. int sign = 1;
  633. uint32 den;
  634. if (fv < 0) {
  635. if (type == TIFF_RATIONAL) {
  636. TIFFWarning(tif->tif_name,
  637. "\"%s\": Information lost writing value (%g) as (unsigned) RATIONAL",
  638. _TIFFFieldWithTag(tif,tag)->field_name, v);
  639. fv = 0;
  640. } else
  641. fv = -fv, sign = -1;
  642. }
  643. den = 1L;
  644. if (fv > 0) {
  645. while (fv < 1L<<(31-3) && den < 1L<<(31-3))
  646. fv *= 1<<3, den *= 1L<<3;
  647. }
  648. t[2*i+0] = sign * (fv + 0.5);
  649. t[2*i+1] = den;
  650. }
  651. status = TIFFWriteData(tif, dir, (char *)t);
  652. _TIFFfree((char*) t);
  653. return (status);
  654. }
  655. static int
  656. TIFFWriteFloatArray(TIFF* tif,
  657. TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, uint32 n, float* v)
  658. {
  659. dir->tdir_tag = tag;
  660. dir->tdir_type = (short) type;
  661. dir->tdir_count = n;
  662. TIFFCvtNativeToIEEEFloat(tif, n, v);
  663. if (n == 1) {
  664. dir->tdir_offset = *(uint32*) &v[0];
  665. return (1);
  666. } else
  667. return (TIFFWriteData(tif, dir, (char*) v));
  668. }
  669. static int
  670. TIFFWriteDoubleArray(TIFF* tif,
  671. TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, uint32 n, double* v)
  672. {
  673. dir->tdir_tag = tag;
  674. dir->tdir_type = (short) type;
  675. dir->tdir_count = n;
  676. TIFFCvtNativeToIEEEDouble(tif, n, v);
  677. return (TIFFWriteData(tif, dir, (char*) v));
  678. }
  679. /*
  680. * Write an array of ``type'' values for a specified tag (i.e. this is a tag
  681. * which is allowed to have different types, e.g. SMaxSampleType).
  682. * Internally the data values are represented as double since a double can
  683. * hold any of the TIFF tag types (yes, this should really be an abstract
  684. * type tany_t for portability). The data is converted into the specified
  685. * type in a temporary buffer and then handed off to the appropriate array
  686. * writer.
  687. */
  688. static int
  689. TIFFWriteAnyArray(TIFF* tif,
  690. TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, uint32 n, double* v)
  691. {
  692. char buf[10 * sizeof(double)];
  693. char* w = buf;
  694. int i, status = 0;
  695. if (n * TIFFDataWidth(type) > sizeof buf)
  696. w = (char*) _TIFFmalloc(n * TIFFDataWidth(type));
  697. switch (type) {
  698. case TIFF_BYTE:
  699. { unsigned char* bp = (unsigned char*) w;
  700. for (i = 0; i < n; i++)
  701. bp[i] = (unsigned char) v[i];
  702. dir->tdir_tag = tag;
  703. dir->tdir_type = (short) type;
  704. dir->tdir_count = n;
  705. if (!TIFFWriteByteArray(tif, dir, (char*) bp))
  706. goto out;
  707. }
  708. break;
  709. case TIFF_SBYTE:
  710. { signed char* bp = (signed char*) w;
  711. for (i = 0; i < n; i++)
  712. bp[i] = (signed char) v[i];
  713. dir->tdir_tag = tag;
  714. dir->tdir_type = (short) type;
  715. dir->tdir_count = n;
  716. if (!TIFFWriteByteArray(tif, dir, (char*) bp))
  717. goto out;
  718. }
  719. break;
  720. case TIFF_SHORT:
  721. { uint16* bp = (uint16*) w;
  722. for (i = 0; i < n; i++)
  723. bp[i] = (uint16) v[i];
  724. if (!TIFFWriteShortArray(tif, type, tag, dir, n, (uint16*)bp))
  725. goto out;
  726. }
  727. break;
  728. case TIFF_SSHORT:
  729. { int16* bp = (int16*) w;
  730. for (i = 0; i < n; i++)
  731. bp[i] = (int16) v[i];
  732. if (!TIFFWriteShortArray(tif, type, tag, dir, n, (uint16*)bp))
  733. goto out;
  734. }
  735. break;
  736. case TIFF_LONG:
  737. { uint32* bp = (uint32*) w;
  738. for (i = 0; i < n; i++)
  739. bp[i] = (uint32) v[i];
  740. if (!TIFFWriteLongArray(tif, type, tag, dir, n, bp))
  741. goto out;
  742. }
  743. break;
  744. case TIFF_SLONG:
  745. { int32* bp = (int32*) w;
  746. for (i = 0; i < n; i++)
  747. bp[i] = (int32) v[i];
  748. if (!TIFFWriteLongArray(tif, type, tag, dir, n, (uint32*) bp))
  749. goto out;
  750. }
  751. break;
  752. case TIFF_FLOAT:
  753. { float* bp = (float*) w;
  754. for (i = 0; i < n; i++)
  755. bp[i] = (float) v[i];
  756. if (!TIFFWriteFloatArray(tif, type, tag, dir, n, bp))
  757. goto out;
  758. }
  759. break;
  760. case TIFF_DOUBLE:
  761. return (TIFFWriteDoubleArray(tif, type, tag, dir, n, v));
  762. default:
  763. /* TIFF_NOTYPE */
  764. /* TIFF_ASCII */
  765. /* TIFF_UNDEFINED */
  766. /* TIFF_RATIONAL */
  767. /* TIFF_SRATIONAL */
  768. goto out;
  769. }
  770. status = 1;
  771. out:
  772. if (w != buf)
  773. _TIFFfree(w);
  774. return (status);
  775. }
  776. #ifdef COLORIMETRY_SUPPORT
  777. static int
  778. TIFFWriteTransferFunction(TIFF* tif, TIFFDirEntry* dir)
  779. {
  780. TIFFDirectory* td = &tif->tif_dir;
  781. tsize_t n = (1L<<td->td_bitspersample) * sizeof (uint16);
  782. uint16** tf = td->td_transferfunction;
  783. int ncols;
  784. /*
  785. * Check if the table can be written as a single column,
  786. * or if it must be written as 3 columns. Note that we
  787. * write a 3-column tag if there are 2 samples/pixel and
  788. * a single column of data won't suffice--hmm.
  789. */
  790. switch (td->td_samplesperpixel - td->td_extrasamples) {
  791. default: if (_TIFFmemcmp(tf[0], tf[2], n)) { ncols = 3; break; }
  792. case 2: if (_TIFFmemcmp(tf[0], tf[1], n)) { ncols = 3; break; }
  793. case 1: case 0: ncols = 1;
  794. }
  795. return (TIFFWriteShortTable(tif,
  796. TIFFTAG_TRANSFERFUNCTION, dir, ncols, tf));
  797. }
  798. #endif
  799. /*
  800. * Write a contiguous directory item.
  801. */
  802. static int
  803. TIFFWriteData(TIFF* tif, TIFFDirEntry* dir, char* cp)
  804. {
  805. tsize_t cc;
  806. if (tif->tif_flags & TIFF_SWAB) {
  807. switch (dir->tdir_type) {
  808. case TIFF_SHORT:
  809. case TIFF_SSHORT:
  810. TIFFSwabArrayOfShort((uint16*) cp, dir->tdir_count);
  811. break;
  812. case TIFF_LONG:
  813. case TIFF_SLONG:
  814. case TIFF_FLOAT:
  815. TIFFSwabArrayOfLong((uint32*) cp, dir->tdir_count);
  816. break;
  817. case TIFF_RATIONAL:
  818. case TIFF_SRATIONAL:
  819. TIFFSwabArrayOfLong((uint32*) cp, 2*dir->tdir_count);
  820. break;
  821. case TIFF_DOUBLE:
  822. TIFFSwabArrayOfDouble((double*) cp, dir->tdir_count);
  823. break;
  824. }
  825. }
  826. dir->tdir_offset = tif->tif_dataoff;
  827. cc = dir->tdir_count * TIFFDataWidth(dir->tdir_type);
  828. if (SeekOK(tif, dir->tdir_offset) &&
  829. WriteOK(tif, cp, cc)) {
  830. tif->tif_dataoff += (cc + 1) & ~1;
  831. return (1);
  832. }
  833. TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Error writing data for field \"%s\"",
  834. _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
  835. return (0);
  836. }
  837. /*
  838. * Link the current directory into the
  839. * directory chain for the file.
  840. */
  841. static int
  842. TIFFLinkDirectory(TIFF* tif)
  843. {
  844. static const char module[] = "TIFFLinkDirectory";
  845. uint32 nextdir;
  846. uint32 diroff;
  847. tif->tif_diroff = (TIFFSeekFile(tif, (toff_t) 0, SEEK_END)+1) &~ 1;
  848. diroff = (uint32) tif->tif_diroff;
  849. if (tif->tif_flags & TIFF_SWAB)
  850. TIFFSwabLong(&diroff);
  851. #if SUBIFD_SUPPORT
  852. if (tif->tif_flags & TIFF_INSUBIFD) {
  853. (void) TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET);
  854. if (!WriteOK(tif, &diroff, sizeof (diroff))) {
  855. TIFFErrorExt(tif->tif_clientdata, module,
  856. "%s: Error writing SubIFD directory link",
  857. tif->tif_name);
  858. return (0);
  859. }
  860. /*
  861. * Advance to the next SubIFD or, if this is
  862. * the last one configured, revert back to the
  863. * normal directory linkage.
  864. */
  865. if (--tif->tif_nsubifd)
  866. tif->tif_subifdoff += sizeof (diroff);
  867. else
  868. tif->tif_flags &= ~TIFF_INSUBIFD;
  869. return (1);
  870. }
  871. #endif
  872. if (tif->tif_header.tiff_diroff == 0) {
  873. /*
  874. * First directory, overwrite offset in header.
  875. */
  876. tif->tif_header.tiff_diroff = (uint32) tif->tif_diroff;
  877. #define HDROFF(f) ((toff_t) &(((TIFFHeader*) 0)->f))
  878. (void) TIFFSeekFile(tif, HDROFF(tiff_diroff), SEEK_SET);
  879. if (!WriteOK(tif, &diroff, sizeof (diroff))) {
  880. TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Error writing TIFF header");
  881. return (0);
  882. }
  883. return (1);
  884. }
  885. /*
  886. * Not the first directory, search to the last and append.
  887. */
  888. nextdir = tif->tif_header.tiff_diroff;
  889. do {
  890. uint16 dircount;
  891. if (!SeekOK(tif, nextdir) ||
  892. !ReadOK(tif, &dircount, sizeof (dircount))) {
  893. TIFFErrorExt(tif->tif_clientdata, module, "Error fetching directory count");
  894. return (0);
  895. }
  896. if (tif->tif_flags & TIFF_SWAB)
  897. TIFFSwabShort(&dircount);
  898. (void) TIFFSeekFile(tif,
  899. dircount * sizeof (TIFFDirEntry), SEEK_CUR);
  900. if (!ReadOK(tif, &nextdir, sizeof (nextdir))) {
  901. TIFFErrorExt(tif->tif_clientdata, module, "Error fetching directory link");
  902. return (0);
  903. }
  904. if (tif->tif_flags & TIFF_SWAB)
  905. TIFFSwabLong(&nextdir);
  906. } while (nextdir != 0);
  907. (void) TIFFSeekFile(tif, -(toff_t) sizeof (nextdir), SEEK_CUR);
  908. if (!WriteOK(tif, &diroff, sizeof (diroff))) {
  909. TIFFErrorExt(tif->tif_clientdata, module, "Error writing directory link");
  910. return (0);
  911. }
  912. return (1);
  913. }
  914. /*
  915. * Local Variables:
  916. * mode: c
  917. * c-basic-offset: 8
  918. * fill-column: 78
  919. * End:
  920. */