2
0

tif_pdsdirread.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. /* $Header: /cvs/maptools/cvsroot/libtiff/contrib/pds/tif_pdsdirread.c,v 1.4 2010-06-08 18:55:15 bfriesen Exp $ */
  2. /*
  3. * Copyright (c) 1988-1996 Sam Leffler
  4. * Copyright (c) 1991-1996 Silicon Graphics, Inc.
  5. * Copyright (c( 1996 USAF Phillips Laboratory
  6. *
  7. * Permission to use, copy, modify, distribute, and sell this software and
  8. * its documentation for any purpose is hereby granted without fee, provided
  9. * that (i) the above copyright notices and this permission notice appear in
  10. * all copies of the software and related documentation, and (ii) the names of
  11. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  12. * publicity relating to the software without the specific, prior written
  13. * permission of Sam Leffler and Silicon Graphics.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  17. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  18. *
  19. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  20. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  21. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  23. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  24. * OF THIS SOFTWARE.
  25. */
  26. /*
  27. * TIFF Library.
  28. *
  29. * These routines written by Conrad J. Poelman on a single late-night of
  30. * March 20-21, 1996.
  31. *
  32. * The entire purpose of this file is to provide a single external function,
  33. * TIFFReadPrivateDataSubDirectory(). This function is intended for use in reading a
  34. * private subdirectory from a TIFF file into a private structure. The
  35. * actual writing of data into the structure is handled by the setFieldFn(),
  36. * which is passed to TIFFReadPrivateDataSubDirectory() as a parameter. The idea is to
  37. * enable any application wishing to store private subdirectories to do so
  38. * easily using this function, without modifying the TIFF library.
  39. *
  40. * The astute observer will notice that only two functions are at all different
  41. * from the original tif_dirread.c file: TIFFReadPrivateDataSubDirectory() and
  42. * TIFFFetchNormalSubTag(). All the other stuff that makes this file so huge
  43. * is only necessary because all of those functions are declared static in
  44. * tif_dirread.c, so we have to totally duplicate them in order to use them.
  45. *
  46. * Oh, also note the bug fix in TIFFFetchFloat().
  47. *
  48. */
  49. #include "tiffiop.h"
  50. #define IGNORE 0 /* tag placeholder used below */
  51. #if HAVE_IEEEFP
  52. #define TIFFCvtIEEEFloatToNative(tif, n, fp)
  53. #define TIFFCvtIEEEDoubleToNative(tif, n, dp)
  54. #else
  55. extern void TIFFCvtIEEEFloatToNative(TIFF*, uint32, float*);
  56. extern void TIFFCvtIEEEDoubleToNative(TIFF*, uint32, double*);
  57. #endif
  58. static void EstimateStripByteCounts(TIFF*, TIFFDirEntry*, uint16);
  59. static void MissingRequired(TIFF*, const char*);
  60. static int CheckDirCount(TIFF*, TIFFDirEntry*, uint32);
  61. static tsize_t TIFFFetchData(TIFF*, TIFFDirEntry*, char*);
  62. static tsize_t TIFFFetchString(TIFF*, TIFFDirEntry*, char*);
  63. static float TIFFFetchRational(TIFF*, TIFFDirEntry*);
  64. static int TIFFFetchNormalSubTag(TIFF*, TIFFDirEntry*, const TIFFFieldInfo*,
  65. int (*getFieldFn)(TIFF *tif,ttag_t tag,...));
  66. static int TIFFFetchPerSampleShorts(TIFF*, TIFFDirEntry*, int*);
  67. static int TIFFFetchPerSampleAnys(TIFF*, TIFFDirEntry*, double*);
  68. static int TIFFFetchShortArray(TIFF*, TIFFDirEntry*, uint16*);
  69. static int TIFFFetchStripThing(TIFF*, TIFFDirEntry*, long, uint32**);
  70. static int TIFFFetchExtraSamples(TIFF*, TIFFDirEntry*);
  71. static int TIFFFetchRefBlackWhite(TIFF*, TIFFDirEntry*);
  72. static float TIFFFetchFloat(TIFF*, TIFFDirEntry*);
  73. static int TIFFFetchFloatArray(TIFF*, TIFFDirEntry*, float*);
  74. static int TIFFFetchDoubleArray(TIFF*, TIFFDirEntry*, double*);
  75. static int TIFFFetchAnyArray(TIFF*, TIFFDirEntry*, double*);
  76. static int TIFFFetchShortPair(TIFF*, TIFFDirEntry*);
  77. #if STRIPCHOP_SUPPORT
  78. static void ChopUpSingleUncompressedStrip(TIFF*);
  79. #endif
  80. static char *
  81. CheckMalloc(TIFF* tif, tsize_t n, const char* what)
  82. {
  83. char *cp = (char*)_TIFFmalloc(n);
  84. if (cp == NULL)
  85. TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "No space %s", what);
  86. return (cp);
  87. }
  88. /* Just as was done with TIFFWritePrivateDataSubDirectory(), here we implement
  89. TIFFReadPrivateDataSubDirectory() which takes an offset into the TIFF file,
  90. a TIFFFieldInfo structure specifying the types of the various tags,
  91. and a function to use to set individual tags when they are encountered.
  92. The data is read from the file, translated using the TIFF library's
  93. built-in machine-independent conversion functions, and filled into
  94. private subdirectory structure.
  95. This code was written by copying the original TIFFReadDirectory() function
  96. from tif_dirread.c and paring it down to what is needed for this.
  97. It is the caller's responsibility to allocate and initialize the internal
  98. structure that setFieldFn() will be writing into. If this function is being
  99. called more than once before closing the file, the caller also must be
  100. careful to free data in the structure before re-initializing.
  101. It is also the caller's responsibility to verify the presence of
  102. any required fields after reading the directory in.
  103. */
  104. int
  105. TIFFReadPrivateDataSubDirectory(TIFF* tif, toff_t pdir_offset,
  106. TIFFFieldInfo *field_info,
  107. int (*setFieldFn)(TIFF *tif, ttag_t tag, ...))
  108. {
  109. register TIFFDirEntry* dp;
  110. register int n;
  111. register TIFFDirectory* td;
  112. TIFFDirEntry* dir;
  113. int iv;
  114. long v;
  115. double dv;
  116. const TIFFFieldInfo* fip;
  117. int fix;
  118. uint16 dircount;
  119. uint32 nextdiroff;
  120. char* cp;
  121. int diroutoforderwarning = 0;
  122. /* Skipped part about checking for directories or compression data. */
  123. if (!isMapped(tif)) {
  124. if (!SeekOK(tif, pdir_offset)) {
  125. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  126. "Seek error accessing TIFF private subdirectory");
  127. return (0);
  128. }
  129. if (!ReadOK(tif, &dircount, sizeof (uint16))) {
  130. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  131. "Can not read TIFF private subdirectory count");
  132. return (0);
  133. }
  134. if (tif->tif_flags & TIFF_SWAB)
  135. TIFFSwabShort(&dircount);
  136. dir = (TIFFDirEntry *)CheckMalloc(tif,
  137. dircount * sizeof (TIFFDirEntry), "to read TIFF private subdirectory");
  138. if (dir == NULL)
  139. return (0);
  140. if (!ReadOK(tif, dir, dircount*sizeof (TIFFDirEntry))) {
  141. TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Can not read TIFF private subdirectory");
  142. goto bad;
  143. }
  144. /*
  145. * Read offset to next directory for sequential scans.
  146. */
  147. (void) ReadOK(tif, &nextdiroff, sizeof (uint32));
  148. } else {
  149. toff_t off = pdir_offset;
  150. if (off + sizeof (short) > tif->tif_size) {
  151. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  152. "Can not read TIFF private subdirectory count");
  153. return (0);
  154. } else
  155. _TIFFmemcpy(&dircount, tif->tif_base + off, sizeof (uint16));
  156. off += sizeof (uint16);
  157. if (tif->tif_flags & TIFF_SWAB)
  158. TIFFSwabShort(&dircount);
  159. dir = (TIFFDirEntry *)CheckMalloc(tif,
  160. dircount * sizeof (TIFFDirEntry), "to read TIFF private subdirectory");
  161. if (dir == NULL)
  162. return (0);
  163. if (off + dircount*sizeof (TIFFDirEntry) > tif->tif_size) {
  164. TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Can not read TIFF private subdirectory");
  165. goto bad;
  166. } else
  167. _TIFFmemcpy(dir, tif->tif_base + off,
  168. dircount*sizeof (TIFFDirEntry));
  169. off += dircount* sizeof (TIFFDirEntry);
  170. if (off + sizeof (uint32) < tif->tif_size)
  171. _TIFFmemcpy(&nextdiroff, tif->tif_base+off, sizeof (uint32));
  172. }
  173. if (tif->tif_flags & TIFF_SWAB)
  174. TIFFSwabLong(&nextdiroff);
  175. /*
  176. * Setup default value and then make a pass over
  177. * the fields to check type and tag information,
  178. * and to extract info required to size data
  179. * structures. A second pass is made afterwards
  180. * to read in everthing not taken in the first pass.
  181. */
  182. td = &tif->tif_dir;
  183. for (fip = field_info, dp = dir, n = dircount;
  184. n > 0; n--, dp++) {
  185. if (tif->tif_flags & TIFF_SWAB) {
  186. TIFFSwabArrayOfShort(&dp->tdir_tag, 2);
  187. TIFFSwabArrayOfLong(&dp->tdir_count, 2);
  188. }
  189. /*
  190. * Find the field information entry for this tag.
  191. */
  192. /*
  193. * Silicon Beach (at least) writes unordered
  194. * directory tags (violating the spec). Handle
  195. * it here, but be obnoxious (maybe they'll fix it?).
  196. */
  197. if (dp->tdir_tag < fip->field_tag) {
  198. if (!diroutoforderwarning) {
  199. TIFFWarning(tif->tif_name,
  200. "invalid TIFF private subdirectory; tags are not sorted in ascending order");
  201. diroutoforderwarning = 1;
  202. }
  203. fip = field_info; /* O(n^2) */
  204. }
  205. while (fip->field_tag && fip->field_tag < dp->tdir_tag)
  206. fip++;
  207. if (!fip->field_tag || fip->field_tag != dp->tdir_tag) {
  208. TIFFWarning(tif->tif_name,
  209. "unknown field with tag %d (0x%x) in private subdirectory ignored",
  210. dp->tdir_tag, dp->tdir_tag);
  211. dp->tdir_tag = IGNORE;
  212. fip = field_info;/* restart search */
  213. continue;
  214. }
  215. /*
  216. * Null out old tags that we ignore.
  217. */
  218. /* Not implemented yet, since FIELD_IGNORE is specific to
  219. the main directories. Could pass this in too... */
  220. if (0 /* && fip->field_bit == FIELD_IGNORE */) {
  221. ignore:
  222. dp->tdir_tag = IGNORE;
  223. continue;
  224. }
  225. /*
  226. * Check data type.
  227. */
  228. while (dp->tdir_type != (u_short)fip->field_type) {
  229. if (fip->field_type == TIFF_ANY) /* wildcard */
  230. break;
  231. fip++;
  232. if (!fip->field_tag || fip->field_tag != dp->tdir_tag) {
  233. TIFFWarning(tif->tif_name,
  234. "wrong data type %d for \"%s\"; tag ignored",
  235. dp->tdir_type, fip[-1].field_name);
  236. goto ignore;
  237. }
  238. }
  239. /*
  240. * Check count if known in advance.
  241. */
  242. if (fip->field_readcount != TIFF_VARIABLE) {
  243. uint32 expected = (fip->field_readcount == TIFF_SPP) ?
  244. (uint32) td->td_samplesperpixel :
  245. (uint32) fip->field_readcount;
  246. if (!CheckDirCount(tif, dp, expected))
  247. goto ignore;
  248. }
  249. /* Now read in and process data from field. */
  250. if (!TIFFFetchNormalSubTag(tif, dp, fip, setFieldFn))
  251. goto bad;
  252. }
  253. if (dir)
  254. _TIFFfree(dir);
  255. return (1);
  256. bad:
  257. if (dir)
  258. _TIFFfree(dir);
  259. return (0);
  260. }
  261. static void
  262. EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
  263. {
  264. register TIFFDirEntry *dp;
  265. register TIFFDirectory *td = &tif->tif_dir;
  266. uint16 i;
  267. if (td->td_stripbytecount)
  268. _TIFFfree(td->td_stripbytecount);
  269. td->td_stripbytecount = (uint32*)
  270. CheckMalloc(tif, td->td_nstrips * sizeof (uint32),
  271. "for \"StripByteCounts\" array");
  272. if (td->td_compression != COMPRESSION_NONE) {
  273. uint32 space = (uint32)(sizeof (TIFFHeader)
  274. + sizeof (uint16)
  275. + (dircount * sizeof (TIFFDirEntry))
  276. + sizeof (uint32));
  277. toff_t filesize = TIFFGetFileSize(tif);
  278. uint16 n;
  279. /* calculate amount of space used by indirect values */
  280. for (dp = dir, n = dircount; n > 0; n--, dp++) {
  281. uint32 cc = dp->tdir_count*TIFFDataWidth(dp->tdir_type);
  282. if (cc > sizeof (uint32))
  283. space += cc;
  284. }
  285. space = (filesize - space) / td->td_samplesperpixel;
  286. for (i = 0; i < td->td_nstrips; i++)
  287. td->td_stripbytecount[i] = space;
  288. /*
  289. * This gross hack handles the case were the offset to
  290. * the last strip is past the place where we think the strip
  291. * should begin. Since a strip of data must be contiguous,
  292. * it's safe to assume that we've overestimated the amount
  293. * of data in the strip and trim this number back accordingly.
  294. */
  295. i--;
  296. if (td->td_stripoffset[i] + td->td_stripbytecount[i] > filesize)
  297. td->td_stripbytecount[i] =
  298. filesize - td->td_stripoffset[i];
  299. } else {
  300. uint32 rowbytes = TIFFScanlineSize(tif);
  301. uint32 rowsperstrip = td->td_imagelength / td->td_nstrips;
  302. for (i = 0; i < td->td_nstrips; i++)
  303. td->td_stripbytecount[i] = rowbytes*rowsperstrip;
  304. }
  305. TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
  306. if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
  307. td->td_rowsperstrip = td->td_imagelength;
  308. }
  309. static void
  310. MissingRequired(TIFF* tif, const char* tagname)
  311. {
  312. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  313. "TIFF directory is missing required \"%s\" field", tagname);
  314. }
  315. /*
  316. * Check the count field of a directory
  317. * entry against a known value. The caller
  318. * is expected to skip/ignore the tag if
  319. * there is a mismatch.
  320. */
  321. static int
  322. CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count)
  323. {
  324. if (count != dir->tdir_count) {
  325. TIFFWarning(tif->tif_name,
  326. "incorrect count for field \"%s\" (%lu, expecting %lu); tag ignored",
  327. _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name,
  328. dir->tdir_count, count);
  329. return (0);
  330. }
  331. return (1);
  332. }
  333. /*
  334. * Fetch a contiguous directory item.
  335. */
  336. static tsize_t
  337. TIFFFetchData(TIFF* tif, TIFFDirEntry* dir, char* cp)
  338. {
  339. int w = TIFFDataWidth(dir->tdir_type);
  340. tsize_t cc = dir->tdir_count * w;
  341. if (!isMapped(tif)) {
  342. if (!SeekOK(tif, dir->tdir_offset))
  343. goto bad;
  344. if (!ReadOK(tif, cp, cc))
  345. goto bad;
  346. } else {
  347. if (dir->tdir_offset + cc > tif->tif_size)
  348. goto bad;
  349. _TIFFmemcpy(cp, tif->tif_base + dir->tdir_offset, cc);
  350. }
  351. if (tif->tif_flags & TIFF_SWAB) {
  352. switch (dir->tdir_type) {
  353. case TIFF_SHORT:
  354. case TIFF_SSHORT:
  355. TIFFSwabArrayOfShort((uint16*) cp, dir->tdir_count);
  356. break;
  357. case TIFF_LONG:
  358. case TIFF_SLONG:
  359. case TIFF_FLOAT:
  360. TIFFSwabArrayOfLong((uint32*) cp, dir->tdir_count);
  361. break;
  362. case TIFF_RATIONAL:
  363. case TIFF_SRATIONAL:
  364. TIFFSwabArrayOfLong((uint32*) cp, 2*dir->tdir_count);
  365. break;
  366. case TIFF_DOUBLE:
  367. TIFFSwabArrayOfDouble((double*) cp, dir->tdir_count);
  368. break;
  369. }
  370. }
  371. return (cc);
  372. bad:
  373. TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Error fetching data for field \"%s\"",
  374. _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
  375. return ((tsize_t) 0);
  376. }
  377. /*
  378. * Fetch an ASCII item from the file.
  379. */
  380. static tsize_t
  381. TIFFFetchString(TIFF* tif, TIFFDirEntry* dir, char* cp)
  382. {
  383. if (dir->tdir_count <= 4) {
  384. uint32 l = dir->tdir_offset;
  385. if (tif->tif_flags & TIFF_SWAB)
  386. TIFFSwabLong(&l);
  387. _TIFFmemcpy(cp, &l, dir->tdir_count);
  388. return (1);
  389. }
  390. return (TIFFFetchData(tif, dir, cp));
  391. }
  392. /*
  393. * Convert numerator+denominator to float.
  394. */
  395. static int
  396. cvtRational(TIFF* tif, TIFFDirEntry* dir, uint32 num, uint32 denom, float* rv)
  397. {
  398. if (denom == 0) {
  399. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  400. "%s: Rational with zero denominator (num = %lu)",
  401. _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name, num);
  402. return (0);
  403. } else {
  404. if (dir->tdir_type == TIFF_RATIONAL)
  405. *rv = ((float)num / (float)denom);
  406. else
  407. *rv = ((float)(int32)num / (float)(int32)denom);
  408. return (1);
  409. }
  410. }
  411. /*
  412. * Fetch a rational item from the file
  413. * at offset off and return the value
  414. * as a floating point number.
  415. */
  416. static float
  417. TIFFFetchRational(TIFF* tif, TIFFDirEntry* dir)
  418. {
  419. uint32 l[2];
  420. float v;
  421. return (!TIFFFetchData(tif, dir, (char *)l) ||
  422. !cvtRational(tif, dir, l[0], l[1], &v) ? 1.0f : v);
  423. }
  424. /*
  425. * Fetch a single floating point value
  426. * from the offset field and return it
  427. * as a native float.
  428. */
  429. static float
  430. TIFFFetchFloat(TIFF* tif, TIFFDirEntry* dir)
  431. {
  432. /* This appears to be a flagrant bug in the TIFF library, yet I
  433. actually don't understand how it could have ever worked the old
  434. way. Look at the comments in my new code and you'll understand. */
  435. #if (0)
  436. float v = (float)
  437. TIFFExtractData(tif, dir->tdir_type, dir->tdir_offset);
  438. TIFFCvtIEEEFloatToNative(tif, 1, &v);
  439. #else
  440. float v;
  441. /* This is a little bit tricky - if we just cast the uint32 to a float,
  442. C will perform a numerical conversion, which is not what we want.
  443. We want to take the actual bit pattern in the uint32 and interpret
  444. it as a float. Thus we cast a uint32 * into a float * and then
  445. dereference to get v. */
  446. uint32 l = (uint32)
  447. TIFFExtractData(tif, dir->tdir_type, dir->tdir_offset);
  448. v = * (float *) &l;
  449. TIFFCvtIEEEFloatToNative(tif, 1, &v);
  450. #endif
  451. return (v);
  452. }
  453. /*
  454. * Fetch an array of BYTE or SBYTE values.
  455. */
  456. static int
  457. TIFFFetchByteArray(TIFF* tif, TIFFDirEntry* dir, uint16* v)
  458. {
  459. if (dir->tdir_count <= 4) {
  460. /*
  461. * Extract data from offset field.
  462. */
  463. if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) {
  464. switch (dir->tdir_count) {
  465. case 4: v[3] = dir->tdir_offset & 0xff;
  466. case 3: v[2] = (dir->tdir_offset >> 8) & 0xff;
  467. case 2: v[1] = (dir->tdir_offset >> 16) & 0xff;
  468. case 1: v[0] = dir->tdir_offset >> 24;
  469. }
  470. } else {
  471. switch (dir->tdir_count) {
  472. case 4: v[3] = dir->tdir_offset >> 24;
  473. case 3: v[2] = (dir->tdir_offset >> 16) & 0xff;
  474. case 2: v[1] = (dir->tdir_offset >> 8) & 0xff;
  475. case 1: v[0] = dir->tdir_offset & 0xff;
  476. }
  477. }
  478. return (1);
  479. } else
  480. return (TIFFFetchData(tif, dir, (char*) v) != 0); /* XXX */
  481. }
  482. /*
  483. * Fetch an array of SHORT or SSHORT values.
  484. */
  485. static int
  486. TIFFFetchShortArray(TIFF* tif, TIFFDirEntry* dir, uint16* v)
  487. {
  488. if (dir->tdir_count <= 2) {
  489. if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) {
  490. switch (dir->tdir_count) {
  491. case 2: v[1] = dir->tdir_offset & 0xffff;
  492. case 1: v[0] = dir->tdir_offset >> 16;
  493. }
  494. } else {
  495. switch (dir->tdir_count) {
  496. case 2: v[1] = dir->tdir_offset >> 16;
  497. case 1: v[0] = dir->tdir_offset & 0xffff;
  498. }
  499. }
  500. return (1);
  501. } else
  502. return (TIFFFetchData(tif, dir, (char *)v) != 0);
  503. }
  504. /*
  505. * Fetch a pair of SHORT or BYTE values.
  506. */
  507. static int
  508. TIFFFetchShortPair(TIFF* tif, TIFFDirEntry* dir)
  509. {
  510. uint16 v[2];
  511. int ok = 0;
  512. switch (dir->tdir_type) {
  513. case TIFF_SHORT:
  514. case TIFF_SSHORT:
  515. ok = TIFFFetchShortArray(tif, dir, v);
  516. break;
  517. case TIFF_BYTE:
  518. case TIFF_SBYTE:
  519. ok = TIFFFetchByteArray(tif, dir, v);
  520. break;
  521. }
  522. if (ok)
  523. TIFFSetField(tif, dir->tdir_tag, v[0], v[1]);
  524. return (ok);
  525. }
  526. /*
  527. * Fetch an array of LONG or SLONG values.
  528. */
  529. static int
  530. TIFFFetchLongArray(TIFF* tif, TIFFDirEntry* dir, uint32* v)
  531. {
  532. if (dir->tdir_count == 1) {
  533. v[0] = dir->tdir_offset;
  534. return (1);
  535. } else
  536. return (TIFFFetchData(tif, dir, (char*) v) != 0);
  537. }
  538. /*
  539. * Fetch an array of RATIONAL or SRATIONAL values.
  540. */
  541. static int
  542. TIFFFetchRationalArray(TIFF* tif, TIFFDirEntry* dir, float* v)
  543. {
  544. int ok = 0;
  545. uint32* l;
  546. l = (uint32*)CheckMalloc(tif,
  547. dir->tdir_count*TIFFDataWidth(dir->tdir_type),
  548. "to fetch array of rationals");
  549. if (l) {
  550. if (TIFFFetchData(tif, dir, (char *)l)) {
  551. uint32 i;
  552. for (i = 0; i < dir->tdir_count; i++) {
  553. ok = cvtRational(tif, dir,
  554. l[2*i+0], l[2*i+1], &v[i]);
  555. if (!ok)
  556. break;
  557. }
  558. }
  559. _TIFFfree((char *)l);
  560. }
  561. return (ok);
  562. }
  563. /*
  564. * Fetch an array of FLOAT values.
  565. */
  566. static int
  567. TIFFFetchFloatArray(TIFF* tif, TIFFDirEntry* dir, float* v)
  568. {
  569. if (dir->tdir_count == 1) {
  570. v[0] = *(float*) &dir->tdir_offset;
  571. TIFFCvtIEEEFloatToNative(tif, dir->tdir_count, v);
  572. return (1);
  573. } else if (TIFFFetchData(tif, dir, (char*) v)) {
  574. TIFFCvtIEEEFloatToNative(tif, dir->tdir_count, v);
  575. return (1);
  576. } else
  577. return (0);
  578. }
  579. /*
  580. * Fetch an array of DOUBLE values.
  581. */
  582. static int
  583. TIFFFetchDoubleArray(TIFF* tif, TIFFDirEntry* dir, double* v)
  584. {
  585. if (TIFFFetchData(tif, dir, (char*) v)) {
  586. TIFFCvtIEEEDoubleToNative(tif, dir->tdir_count, v);
  587. return (1);
  588. } else
  589. return (0);
  590. }
  591. /*
  592. * Fetch an array of ANY values. The actual values are
  593. * returned as doubles which should be able hold all the
  594. * types. Yes, there really should be an tany_t to avoid
  595. * this potential non-portability ... Note in particular
  596. * that we assume that the double return value vector is
  597. * large enough to read in any fundamental type. We use
  598. * that vector as a buffer to read in the base type vector
  599. * and then convert it in place to double (from end
  600. * to front of course).
  601. */
  602. static int
  603. TIFFFetchAnyArray(TIFF* tif, TIFFDirEntry* dir, double* v)
  604. {
  605. int i;
  606. switch (dir->tdir_type) {
  607. case TIFF_BYTE:
  608. case TIFF_SBYTE:
  609. if (!TIFFFetchByteArray(tif, dir, (uint16*) v))
  610. return (0);
  611. if (dir->tdir_type == TIFF_BYTE) {
  612. uint16* vp = (uint16*) v;
  613. for (i = dir->tdir_count-1; i >= 0; i--)
  614. v[i] = vp[i];
  615. } else {
  616. int16* vp = (int16*) v;
  617. for (i = dir->tdir_count-1; i >= 0; i--)
  618. v[i] = vp[i];
  619. }
  620. break;
  621. case TIFF_SHORT:
  622. case TIFF_SSHORT:
  623. if (!TIFFFetchShortArray(tif, dir, (uint16*) v))
  624. return (0);
  625. if (dir->tdir_type == TIFF_SHORT) {
  626. uint16* vp = (uint16*) v;
  627. for (i = dir->tdir_count-1; i >= 0; i--)
  628. v[i] = vp[i];
  629. } else {
  630. int16* vp = (int16*) v;
  631. for (i = dir->tdir_count-1; i >= 0; i--)
  632. v[i] = vp[i];
  633. }
  634. break;
  635. case TIFF_LONG:
  636. case TIFF_SLONG:
  637. if (!TIFFFetchLongArray(tif, dir, (uint32*) v))
  638. return (0);
  639. if (dir->tdir_type == TIFF_LONG) {
  640. uint32* vp = (uint32*) v;
  641. for (i = dir->tdir_count-1; i >= 0; i--)
  642. v[i] = vp[i];
  643. } else {
  644. int32* vp = (int32*) v;
  645. for (i = dir->tdir_count-1; i >= 0; i--)
  646. v[i] = vp[i];
  647. }
  648. break;
  649. case TIFF_RATIONAL:
  650. case TIFF_SRATIONAL:
  651. if (!TIFFFetchRationalArray(tif, dir, (float*) v))
  652. return (0);
  653. { float* vp = (float*) v;
  654. for (i = dir->tdir_count-1; i >= 0; i--)
  655. v[i] = vp[i];
  656. }
  657. break;
  658. case TIFF_FLOAT:
  659. if (!TIFFFetchFloatArray(tif, dir, (float*) v))
  660. return (0);
  661. { float* vp = (float*) v;
  662. for (i = dir->tdir_count-1; i >= 0; i--)
  663. v[i] = vp[i];
  664. }
  665. break;
  666. case TIFF_DOUBLE:
  667. return (TIFFFetchDoubleArray(tif, dir, (double*) v));
  668. default:
  669. /* TIFF_NOTYPE */
  670. /* TIFF_ASCII */
  671. /* TIFF_UNDEFINED */
  672. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  673. "Cannot read TIFF_ANY type %d for field \"%s\"",
  674. _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
  675. return (0);
  676. }
  677. return (1);
  678. }
  679. /*
  680. * Fetch a tag that is not handled by special case code.
  681. */
  682. /* The standard function TIFFFetchNormalTag() could definitely be replaced
  683. with a simple call to this function, just adding TIFFSetField() as the
  684. last argument. */
  685. static int
  686. TIFFFetchNormalSubTag(TIFF* tif, TIFFDirEntry* dp, const TIFFFieldInfo* fip,
  687. int (*setFieldFn)(TIFF *tif, ttag_t tag, ...))
  688. {
  689. static char mesg[] = "to fetch tag value";
  690. int ok = 0;
  691. if (dp->tdir_count > 1) { /* array of values */
  692. char* cp = NULL;
  693. switch (dp->tdir_type) {
  694. case TIFF_BYTE:
  695. case TIFF_SBYTE:
  696. /* NB: always expand BYTE values to shorts */
  697. cp = CheckMalloc(tif,
  698. dp->tdir_count * sizeof (uint16), mesg);
  699. ok = cp && TIFFFetchByteArray(tif, dp, (uint16*) cp);
  700. break;
  701. case TIFF_SHORT:
  702. case TIFF_SSHORT:
  703. cp = CheckMalloc(tif,
  704. dp->tdir_count * sizeof (uint16), mesg);
  705. ok = cp && TIFFFetchShortArray(tif, dp, (uint16*) cp);
  706. break;
  707. case TIFF_LONG:
  708. case TIFF_SLONG:
  709. cp = CheckMalloc(tif,
  710. dp->tdir_count * sizeof (uint32), mesg);
  711. ok = cp && TIFFFetchLongArray(tif, dp, (uint32*) cp);
  712. break;
  713. case TIFF_RATIONAL:
  714. case TIFF_SRATIONAL:
  715. cp = CheckMalloc(tif,
  716. dp->tdir_count * sizeof (float), mesg);
  717. ok = cp && TIFFFetchRationalArray(tif, dp, (float*) cp);
  718. break;
  719. case TIFF_FLOAT:
  720. cp = CheckMalloc(tif,
  721. dp->tdir_count * sizeof (float), mesg);
  722. ok = cp && TIFFFetchFloatArray(tif, dp, (float*) cp);
  723. break;
  724. case TIFF_DOUBLE:
  725. cp = CheckMalloc(tif,
  726. dp->tdir_count * sizeof (double), mesg);
  727. ok = cp && TIFFFetchDoubleArray(tif, dp, (double*) cp);
  728. break;
  729. case TIFF_ASCII:
  730. case TIFF_UNDEFINED: /* bit of a cheat... */
  731. /*
  732. * Some vendors write strings w/o the trailing
  733. * NULL byte, so always append one just in case.
  734. */
  735. cp = CheckMalloc(tif, dp->tdir_count+1, mesg);
  736. if (ok = (cp && TIFFFetchString(tif, dp, cp)))
  737. cp[dp->tdir_count] = '\0'; /* XXX */
  738. break;
  739. }
  740. if (ok) {
  741. ok = (fip->field_passcount ?
  742. (*setFieldFn)(tif, dp->tdir_tag, dp->tdir_count, cp)
  743. : (*setFieldFn)(tif, dp->tdir_tag, cp));
  744. }
  745. if (cp != NULL)
  746. _TIFFfree(cp);
  747. } else if (CheckDirCount(tif, dp, 1)) { /* singleton value */
  748. switch (dp->tdir_type) {
  749. case TIFF_BYTE:
  750. case TIFF_SBYTE:
  751. case TIFF_SHORT:
  752. case TIFF_SSHORT:
  753. /*
  754. * If the tag is also acceptable as a LONG or SLONG
  755. * then (*setFieldFn) will expect an uint32 parameter
  756. * passed to it (through varargs). Thus, for machines
  757. * where sizeof (int) != sizeof (uint32) we must do
  758. * a careful check here. It's hard to say if this
  759. * is worth optimizing.
  760. *
  761. * NB: We use TIFFFieldWithTag here knowing that
  762. * it returns us the first entry in the table
  763. * for the tag and that that entry is for the
  764. * widest potential data type the tag may have.
  765. */
  766. { TIFFDataType type = fip->field_type;
  767. if (type != TIFF_LONG && type != TIFF_SLONG) {
  768. uint16 v = (uint16)
  769. TIFFExtractData(tif, dp->tdir_type, dp->tdir_offset);
  770. ok = (fip->field_passcount ?
  771. (*setFieldFn)(tif, dp->tdir_tag, 1, &v)
  772. : (*setFieldFn)(tif, dp->tdir_tag, v));
  773. break;
  774. }
  775. }
  776. /* fall thru... */
  777. case TIFF_LONG:
  778. case TIFF_SLONG:
  779. { uint32 v32 =
  780. TIFFExtractData(tif, dp->tdir_type, dp->tdir_offset);
  781. ok = (fip->field_passcount ?
  782. (*setFieldFn)(tif, dp->tdir_tag, 1, &v32)
  783. : (*setFieldFn)(tif, dp->tdir_tag, v32));
  784. }
  785. break;
  786. case TIFF_RATIONAL:
  787. case TIFF_SRATIONAL:
  788. case TIFF_FLOAT:
  789. { float v = (dp->tdir_type == TIFF_FLOAT ?
  790. TIFFFetchFloat(tif, dp)
  791. : TIFFFetchRational(tif, dp));
  792. ok = (fip->field_passcount ?
  793. (*setFieldFn)(tif, dp->tdir_tag, 1, &v)
  794. : (*setFieldFn)(tif, dp->tdir_tag, v));
  795. }
  796. break;
  797. case TIFF_DOUBLE:
  798. { double v;
  799. ok = (TIFFFetchDoubleArray(tif, dp, &v) &&
  800. (fip->field_passcount ?
  801. (*setFieldFn)(tif, dp->tdir_tag, 1, &v)
  802. : (*setFieldFn)(tif, dp->tdir_tag, v))
  803. );
  804. }
  805. break;
  806. case TIFF_ASCII:
  807. case TIFF_UNDEFINED: /* bit of a cheat... */
  808. { char c[2];
  809. if (ok = (TIFFFetchString(tif, dp, c) != 0)) {
  810. c[1] = '\0'; /* XXX paranoid */
  811. ok = (*setFieldFn)(tif, dp->tdir_tag, c);
  812. }
  813. }
  814. break;
  815. }
  816. }
  817. return (ok);
  818. }
  819. /* Everything after this is exactly duplicated from the standard tif_dirread.c
  820. file, necessitated by the fact that they are declared static there so
  821. we can't call them!
  822. */
  823. #define NITEMS(x) (sizeof (x) / sizeof (x[0]))
  824. /*
  825. * Fetch samples/pixel short values for
  826. * the specified tag and verify that
  827. * all values are the same.
  828. */
  829. static int
  830. TIFFFetchPerSampleShorts(TIFF* tif, TIFFDirEntry* dir, int* pl)
  831. {
  832. int samples = tif->tif_dir.td_samplesperpixel;
  833. int status = 0;
  834. if (CheckDirCount(tif, dir, (uint32) samples)) {
  835. uint16 buf[10];
  836. uint16* v = buf;
  837. if (samples > NITEMS(buf))
  838. v = (uint16*) _TIFFmalloc(samples * sizeof (uint16));
  839. if (TIFFFetchShortArray(tif, dir, v)) {
  840. int i;
  841. for (i = 1; i < samples; i++)
  842. if (v[i] != v[0]) {
  843. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  844. "Cannot handle different per-sample values for field \"%s\"",
  845. _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
  846. goto bad;
  847. }
  848. *pl = v[0];
  849. status = 1;
  850. }
  851. bad:
  852. if (v != buf)
  853. _TIFFfree((char*) v);
  854. }
  855. return (status);
  856. }
  857. /*
  858. * Fetch samples/pixel ANY values for
  859. * the specified tag and verify that
  860. * all values are the same.
  861. */
  862. static int
  863. TIFFFetchPerSampleAnys(TIFF* tif, TIFFDirEntry* dir, double* pl)
  864. {
  865. int samples = (int) tif->tif_dir.td_samplesperpixel;
  866. int status = 0;
  867. if (CheckDirCount(tif, dir, (uint32) samples)) {
  868. double buf[10];
  869. double* v = buf;
  870. if (samples > NITEMS(buf))
  871. v = (double*) _TIFFmalloc(samples * sizeof (double));
  872. if (TIFFFetchAnyArray(tif, dir, v)) {
  873. int i;
  874. for (i = 1; i < samples; i++)
  875. if (v[i] != v[0]) {
  876. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  877. "Cannot handle different per-sample values for field \"%s\"",
  878. _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
  879. goto bad;
  880. }
  881. *pl = v[0];
  882. status = 1;
  883. }
  884. bad:
  885. if (v != buf)
  886. _TIFFfree(v);
  887. }
  888. return (status);
  889. }
  890. #undef NITEMS
  891. /*
  892. * Fetch a set of offsets or lengths.
  893. * While this routine says "strips",
  894. * in fact it's also used for tiles.
  895. */
  896. static int
  897. TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, long nstrips, uint32** lpp)
  898. {
  899. register uint32* lp;
  900. int status;
  901. if (!CheckDirCount(tif, dir, (uint32) nstrips))
  902. return (0);
  903. /*
  904. * Allocate space for strip information.
  905. */
  906. if (*lpp == NULL &&
  907. (*lpp = (uint32 *)CheckMalloc(tif,
  908. nstrips * sizeof (uint32), "for strip array")) == NULL)
  909. return (0);
  910. lp = *lpp;
  911. if (dir->tdir_type == (int)TIFF_SHORT) {
  912. /*
  913. * Handle uint16->uint32 expansion.
  914. */
  915. uint16* dp = (uint16*) CheckMalloc(tif,
  916. dir->tdir_count* sizeof (uint16), "to fetch strip tag");
  917. if (dp == NULL)
  918. return (0);
  919. if (status = TIFFFetchShortArray(tif, dir, dp)) {
  920. register uint16* wp = dp;
  921. while (nstrips-- > 0)
  922. *lp++ = *wp++;
  923. }
  924. _TIFFfree((char*) dp);
  925. } else
  926. status = TIFFFetchLongArray(tif, dir, lp);
  927. return (status);
  928. }
  929. #define NITEMS(x) (sizeof (x) / sizeof (x[0]))
  930. /*
  931. * Fetch and set the ExtraSamples tag.
  932. */
  933. static int
  934. TIFFFetchExtraSamples(TIFF* tif, TIFFDirEntry* dir)
  935. {
  936. uint16 buf[10];
  937. uint16* v = buf;
  938. int status;
  939. if (dir->tdir_count > NITEMS(buf))
  940. v = (uint16*) _TIFFmalloc(dir->tdir_count * sizeof (uint16));
  941. if (dir->tdir_type == TIFF_BYTE)
  942. status = TIFFFetchByteArray(tif, dir, v);
  943. else
  944. status = TIFFFetchShortArray(tif, dir, v);
  945. if (status)
  946. status = TIFFSetField(tif, dir->tdir_tag, dir->tdir_count, v);
  947. if (v != buf)
  948. _TIFFfree((char*) v);
  949. return (status);
  950. }
  951. #undef NITEMS
  952. #ifdef COLORIMETRY_SUPPORT
  953. /*
  954. * Fetch and set the RefBlackWhite tag.
  955. */
  956. static int
  957. TIFFFetchRefBlackWhite(TIFF* tif, TIFFDirEntry* dir)
  958. {
  959. static char mesg[] = "for \"ReferenceBlackWhite\" array";
  960. char* cp;
  961. int ok;
  962. if (dir->tdir_type == TIFF_RATIONAL)
  963. return (1/*TIFFFetchNormalTag(tif, dir) just so linker won't complain - this part of the code is never used anyway */);
  964. /*
  965. * Handle LONG's for backward compatibility.
  966. */
  967. cp = CheckMalloc(tif, dir->tdir_count * sizeof (uint32), mesg);
  968. if (ok = (cp && TIFFFetchLongArray(tif, dir, (uint32*) cp))) {
  969. float* fp = (float*)
  970. CheckMalloc(tif, dir->tdir_count * sizeof (float), mesg);
  971. if (ok = (fp != NULL)) {
  972. uint32 i;
  973. for (i = 0; i < dir->tdir_count; i++)
  974. fp[i] = (float)((uint32*) cp)[i];
  975. ok = TIFFSetField(tif, dir->tdir_tag, fp);
  976. _TIFFfree((char*) fp);
  977. }
  978. }
  979. if (cp)
  980. _TIFFfree(cp);
  981. return (ok);
  982. }
  983. #endif
  984. #if STRIPCHOP_SUPPORT
  985. /*
  986. * Replace a single strip (tile) of uncompressed data by
  987. * multiple strips (tiles), each approximately 8Kbytes.
  988. * This is useful for dealing with large images or
  989. * for dealing with machines with a limited amount
  990. * memory.
  991. */
  992. static void
  993. ChopUpSingleUncompressedStrip(TIFF* tif)
  994. {
  995. register TIFFDirectory *td = &tif->tif_dir;
  996. uint32 bytecount = td->td_stripbytecount[0];
  997. uint32 offset = td->td_stripoffset[0];
  998. tsize_t rowbytes = TIFFVTileSize(tif, 1), stripbytes;
  999. tstrip_t strip, nstrips, rowsperstrip;
  1000. uint32* newcounts;
  1001. uint32* newoffsets;
  1002. /*
  1003. * Make the rows hold at least one
  1004. * scanline, but fill 8k if possible.
  1005. */
  1006. if (rowbytes > 8192) {
  1007. stripbytes = rowbytes;
  1008. rowsperstrip = 1;
  1009. } else {
  1010. rowsperstrip = 8192 / rowbytes;
  1011. stripbytes = rowbytes * rowsperstrip;
  1012. }
  1013. /* never increase the number of strips in an image */
  1014. if (rowsperstrip >= td->td_rowsperstrip)
  1015. return;
  1016. nstrips = (tstrip_t) TIFFhowmany(bytecount, stripbytes);
  1017. newcounts = (uint32*) CheckMalloc(tif, nstrips * sizeof (uint32),
  1018. "for chopped \"StripByteCounts\" array");
  1019. newoffsets = (uint32*) CheckMalloc(tif, nstrips * sizeof (uint32),
  1020. "for chopped \"StripOffsets\" array");
  1021. if (newcounts == NULL || newoffsets == NULL) {
  1022. /*
  1023. * Unable to allocate new strip information, give
  1024. * up and use the original one strip information.
  1025. */
  1026. if (newcounts != NULL)
  1027. _TIFFfree(newcounts);
  1028. if (newoffsets != NULL)
  1029. _TIFFfree(newoffsets);
  1030. return;
  1031. }
  1032. /*
  1033. * Fill the strip information arrays with
  1034. * new bytecounts and offsets that reflect
  1035. * the broken-up format.
  1036. */
  1037. for (strip = 0; strip < nstrips; strip++) {
  1038. if (stripbytes > bytecount)
  1039. stripbytes = bytecount;
  1040. newcounts[strip] = stripbytes;
  1041. newoffsets[strip] = offset;
  1042. offset += stripbytes;
  1043. bytecount -= stripbytes;
  1044. }
  1045. /*
  1046. * Replace old single strip info with multi-strip info.
  1047. */
  1048. td->td_stripsperimage = td->td_nstrips = nstrips;
  1049. TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
  1050. _TIFFfree(td->td_stripbytecount);
  1051. _TIFFfree(td->td_stripoffset);
  1052. td->td_stripbytecount = newcounts;
  1053. td->td_stripoffset = newoffsets;
  1054. }
  1055. #endif /* STRIPCHOP_SUPPORT */
  1056. /*
  1057. * Local Variables:
  1058. * mode: c
  1059. * c-basic-offset: 8
  1060. * fill-column: 78
  1061. * End:
  1062. */