/src/FreeImage/Source/LibTIFF/tif_read.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 750 lines · 517 code · 48 blank · 185 comment · 120 complexity · bb36fa0c041c9264289a4ece6a39999d MD5 · raw file

  1. /* $Id: tif_read.c,v 1.37 2011/04/10 17:14:09 drolon Exp $ */
  2. /*
  3. * Copyright (c) 1988-1997 Sam Leffler
  4. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and
  7. * its documentation for any purpose is hereby granted without fee, provided
  8. * that (i) the above copyright notices and this permission notice appear in
  9. * all copies of the software and related documentation, and (ii) the names of
  10. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11. * publicity relating to the software without the specific, prior written
  12. * permission of Sam Leffler and Silicon Graphics.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  22. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. */
  25. /*
  26. * TIFF Library.
  27. * Scanline-oriented Read Support
  28. */
  29. #include "tiffiop.h"
  30. #include <stdio.h>
  31. int TIFFFillStrip(TIFF*, tstrip_t);
  32. int TIFFFillTile(TIFF*, ttile_t);
  33. static int TIFFStartStrip(TIFF*, tstrip_t);
  34. static int TIFFStartTile(TIFF*, ttile_t);
  35. static int TIFFCheckRead(TIFF*, int);
  36. #define NOSTRIP ((tstrip_t) -1) /* undefined state */
  37. #define NOTILE ((ttile_t) -1) /* undefined state */
  38. /*
  39. * Seek to a random row+sample in a file.
  40. */
  41. static int
  42. TIFFSeek(TIFF* tif, uint32 row, tsample_t sample)
  43. {
  44. register TIFFDirectory *td = &tif->tif_dir;
  45. tstrip_t strip;
  46. if (row >= td->td_imagelength) { /* out of range */
  47. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  48. "%lu: Row out of range, max %lu",
  49. (unsigned long) row,
  50. (unsigned long) td->td_imagelength);
  51. return (0);
  52. }
  53. if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
  54. if (sample >= td->td_samplesperpixel) {
  55. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  56. "%lu: Sample out of range, max %lu",
  57. (unsigned long) sample, (unsigned long) td->td_samplesperpixel);
  58. return (0);
  59. }
  60. strip = sample*td->td_stripsperimage + row/td->td_rowsperstrip;
  61. } else
  62. strip = row / td->td_rowsperstrip;
  63. if (strip != tif->tif_curstrip) { /* different strip, refill */
  64. if (!TIFFFillStrip(tif, strip))
  65. return (0);
  66. } else if (row < tif->tif_row) {
  67. /*
  68. * Moving backwards within the same strip: backup
  69. * to the start and then decode forward (below).
  70. *
  71. * NB: If you're planning on lots of random access within a
  72. * strip, it's better to just read and decode the entire
  73. * strip, and then access the decoded data in a random fashion.
  74. */
  75. if (!TIFFStartStrip(tif, strip))
  76. return (0);
  77. }
  78. if (row != tif->tif_row) {
  79. /*
  80. * Seek forward to the desired row.
  81. */
  82. if (!(*tif->tif_seek)(tif, row - tif->tif_row))
  83. return (0);
  84. tif->tif_row = row;
  85. }
  86. return (1);
  87. }
  88. int
  89. TIFFReadScanline(TIFF* tif, tdata_t buf, uint32 row, tsample_t sample)
  90. {
  91. int e;
  92. if (!TIFFCheckRead(tif, 0))
  93. return (-1);
  94. if( (e = TIFFSeek(tif, row, sample)) != 0) {
  95. /*
  96. * Decompress desired row into user buffer.
  97. */
  98. e = (*tif->tif_decoderow)
  99. (tif, (tidata_t) buf, tif->tif_scanlinesize, sample);
  100. /* we are now poised at the beginning of the next row */
  101. tif->tif_row = row + 1;
  102. if (e)
  103. (*tif->tif_postdecode)(tif, (tidata_t) buf,
  104. tif->tif_scanlinesize);
  105. }
  106. return (e > 0 ? 1 : -1);
  107. }
  108. /*
  109. * Read a strip of data and decompress the specified
  110. * amount into the user-supplied buffer.
  111. */
  112. tsize_t
  113. TIFFReadEncodedStrip(TIFF* tif, tstrip_t strip, tdata_t buf, tsize_t size)
  114. {
  115. TIFFDirectory *td = &tif->tif_dir;
  116. uint32 nrows;
  117. tsize_t stripsize;
  118. tstrip_t sep_strip, strips_per_sep;
  119. if (!TIFFCheckRead(tif, 0))
  120. return (-1);
  121. if (strip >= td->td_nstrips) {
  122. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  123. "%ld: Strip out of range, max %ld",
  124. (long) strip, (long) td->td_nstrips);
  125. return (-1);
  126. }
  127. /*
  128. * Calculate the strip size according to the number of
  129. * rows in the strip (check for truncated last strip on any
  130. * of the separations).
  131. */
  132. if( td->td_rowsperstrip >= td->td_imagelength )
  133. strips_per_sep = 1;
  134. else
  135. strips_per_sep = (td->td_imagelength+td->td_rowsperstrip-1)
  136. / td->td_rowsperstrip;
  137. sep_strip = strip % strips_per_sep;
  138. if (sep_strip != strips_per_sep-1 ||
  139. (nrows = td->td_imagelength % td->td_rowsperstrip) == 0)
  140. nrows = td->td_rowsperstrip;
  141. stripsize = TIFFVStripSize(tif, nrows);
  142. if (size == (tsize_t) -1)
  143. size = stripsize;
  144. else if (size > stripsize)
  145. size = stripsize;
  146. if (TIFFFillStrip(tif, strip)
  147. && (*tif->tif_decodestrip)(tif, (tidata_t) buf, size,
  148. (tsample_t)(strip / td->td_stripsperimage)) > 0 ) {
  149. (*tif->tif_postdecode)(tif, (tidata_t) buf, size);
  150. return (size);
  151. } else
  152. return ((tsize_t) -1);
  153. }
  154. static tsize_t
  155. TIFFReadRawStrip1(TIFF* tif,
  156. tstrip_t strip, tdata_t buf, tsize_t size, const char* module)
  157. {
  158. TIFFDirectory *td = &tif->tif_dir;
  159. assert((tif->tif_flags&TIFF_NOREADRAW)==0);
  160. if (!isMapped(tif)) {
  161. tsize_t cc;
  162. if (!SeekOK(tif, td->td_stripoffset[strip])) {
  163. TIFFErrorExt(tif->tif_clientdata, module,
  164. "%s: Seek error at scanline %lu, strip %lu",
  165. tif->tif_name,
  166. (unsigned long) tif->tif_row, (unsigned long) strip);
  167. return (-1);
  168. }
  169. cc = TIFFReadFile(tif, buf, size);
  170. if (cc != size) {
  171. TIFFErrorExt(tif->tif_clientdata, module,
  172. "%s: Read error at scanline %lu; got %lu bytes, expected %lu",
  173. tif->tif_name,
  174. (unsigned long) tif->tif_row,
  175. (unsigned long) cc,
  176. (unsigned long) size);
  177. return (-1);
  178. }
  179. } else {
  180. if (td->td_stripoffset[strip] + size > tif->tif_size) {
  181. TIFFErrorExt(tif->tif_clientdata, module,
  182. "%s: Read error at scanline %lu, strip %lu; got %lu bytes, expected %lu",
  183. tif->tif_name,
  184. (unsigned long) tif->tif_row,
  185. (unsigned long) strip,
  186. (unsigned long) tif->tif_size - td->td_stripoffset[strip],
  187. (unsigned long) size);
  188. return (-1);
  189. }
  190. _TIFFmemcpy(buf, tif->tif_base + td->td_stripoffset[strip],
  191. size);
  192. }
  193. return (size);
  194. }
  195. /*
  196. * Read a strip of data from the file.
  197. */
  198. tsize_t
  199. TIFFReadRawStrip(TIFF* tif, tstrip_t strip, tdata_t buf, tsize_t size)
  200. {
  201. static const char module[] = "TIFFReadRawStrip";
  202. TIFFDirectory *td = &tif->tif_dir;
  203. /*
  204. * FIXME: butecount should have tsize_t type, but for now libtiff
  205. * defines tsize_t as a signed 32-bit integer and we are losing
  206. * ability to read arrays larger than 2^31 bytes. So we are using
  207. * uint32 instead of tsize_t here.
  208. */
  209. uint32 bytecount;
  210. if (!TIFFCheckRead(tif, 0))
  211. return ((tsize_t) -1);
  212. if (strip >= td->td_nstrips) {
  213. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  214. "%lu: Strip out of range, max %lu",
  215. (unsigned long) strip,
  216. (unsigned long) td->td_nstrips);
  217. return ((tsize_t) -1);
  218. }
  219. if (tif->tif_flags&TIFF_NOREADRAW)
  220. {
  221. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  222. "Compression scheme does not support access to raw uncompressed data");
  223. return ((tsize_t) -1);
  224. }
  225. bytecount = td->td_stripbytecount[strip];
  226. if (bytecount <= 0) {
  227. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  228. "%lu: Invalid strip byte count, strip %lu",
  229. (unsigned long) bytecount, (unsigned long) strip);
  230. return ((tsize_t) -1);
  231. }
  232. if (size != (tsize_t)-1 && (uint32)size < bytecount)
  233. bytecount = size;
  234. return (TIFFReadRawStrip1(tif, strip, buf, bytecount, module));
  235. }
  236. /*
  237. * Read the specified strip and setup for decoding. The data buffer is
  238. * expanded, as necessary, to hold the strip's data.
  239. */
  240. int
  241. TIFFFillStrip(TIFF* tif, tstrip_t strip)
  242. {
  243. static const char module[] = "TIFFFillStrip";
  244. TIFFDirectory *td = &tif->tif_dir;
  245. if ((tif->tif_flags&TIFF_NOREADRAW)==0)
  246. {
  247. /*
  248. * FIXME: butecount should have tsize_t type, but for now
  249. * libtiff defines tsize_t as a signed 32-bit integer and we
  250. * are losing ability to read arrays larger than 2^31 bytes.
  251. * So we are using uint32 instead of tsize_t here.
  252. */
  253. uint32 bytecount = td->td_stripbytecount[strip];
  254. if (bytecount <= 0) {
  255. TIFFErrorExt(tif->tif_clientdata, module,
  256. "%s: Invalid strip byte count %lu, strip %lu",
  257. tif->tif_name, (unsigned long) bytecount,
  258. (unsigned long) strip);
  259. return (0);
  260. }
  261. if (isMapped(tif) &&
  262. (isFillOrder(tif, td->td_fillorder)
  263. || (tif->tif_flags & TIFF_NOBITREV))) {
  264. /*
  265. * The image is mapped into memory and we either don't
  266. * need to flip bits or the compression routine is
  267. * going to handle this operation itself. In this
  268. * case, avoid copying the raw data and instead just
  269. * reference the data from the memory mapped file
  270. * image. This assumes that the decompression
  271. * routines do not modify the contents of the raw data
  272. * buffer (if they try to, the application will get a
  273. * fault since the file is mapped read-only).
  274. */
  275. if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
  276. _TIFFfree(tif->tif_rawdata);
  277. tif->tif_flags &= ~TIFF_MYBUFFER;
  278. /*
  279. * We must check for overflow, potentially causing
  280. * an OOB read. Instead of simple
  281. *
  282. * td->td_stripoffset[strip]+bytecount > tif->tif_size
  283. *
  284. * comparison (which can overflow) we do the following
  285. * two comparisons:
  286. */
  287. if (bytecount > tif->tif_size ||
  288. td->td_stripoffset[strip] > tif->tif_size - bytecount) {
  289. /*
  290. * This error message might seem strange, but
  291. * it's what would happen if a read were done
  292. * instead.
  293. */
  294. TIFFErrorExt(tif->tif_clientdata, module,
  295. "%s: Read error on strip %lu; "
  296. "got %lu bytes, expected %lu",
  297. tif->tif_name, (unsigned long) strip,
  298. (unsigned long) tif->tif_size - td->td_stripoffset[strip],
  299. (unsigned long) bytecount);
  300. tif->tif_curstrip = NOSTRIP;
  301. return (0);
  302. }
  303. tif->tif_rawdatasize = bytecount;
  304. tif->tif_rawdata = tif->tif_base + td->td_stripoffset[strip];
  305. } else {
  306. /*
  307. * Expand raw data buffer, if needed, to hold data
  308. * strip coming from file (perhaps should set upper
  309. * bound on the size of a buffer we'll use?).
  310. */
  311. if (bytecount > (uint32)tif->tif_rawdatasize) {
  312. tif->tif_curstrip = NOSTRIP;
  313. if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
  314. TIFFErrorExt(tif->tif_clientdata,
  315. module,
  316. "%s: Data buffer too small to hold strip %lu",
  317. tif->tif_name,
  318. (unsigned long) strip);
  319. return (0);
  320. }
  321. if (!TIFFReadBufferSetup(tif, 0,
  322. TIFFroundup(bytecount, 1024)))
  323. return (0);
  324. }
  325. if ((uint32)TIFFReadRawStrip1(tif, strip,
  326. (unsigned char *)tif->tif_rawdata,
  327. bytecount, module) != bytecount)
  328. return (0);
  329. if (!isFillOrder(tif, td->td_fillorder) &&
  330. (tif->tif_flags & TIFF_NOBITREV) == 0)
  331. TIFFReverseBits(tif->tif_rawdata, bytecount);
  332. }
  333. }
  334. return (TIFFStartStrip(tif, strip));
  335. }
  336. /*
  337. * Tile-oriented Read Support
  338. * Contributed by Nancy Cam (Silicon Graphics).
  339. */
  340. /*
  341. * Read and decompress a tile of data. The
  342. * tile is selected by the (x,y,z,s) coordinates.
  343. */
  344. tsize_t
  345. TIFFReadTile(TIFF* tif,
  346. tdata_t buf, uint32 x, uint32 y, uint32 z, tsample_t s)
  347. {
  348. if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
  349. return (-1);
  350. return (TIFFReadEncodedTile(tif,
  351. TIFFComputeTile(tif, x, y, z, s), buf, (tsize_t) -1));
  352. }
  353. /*
  354. * Read a tile of data and decompress the specified
  355. * amount into the user-supplied buffer.
  356. */
  357. tsize_t
  358. TIFFReadEncodedTile(TIFF* tif, ttile_t tile, tdata_t buf, tsize_t size)
  359. {
  360. TIFFDirectory *td = &tif->tif_dir;
  361. tsize_t tilesize = tif->tif_tilesize;
  362. if (!TIFFCheckRead(tif, 1))
  363. return (-1);
  364. if (tile >= td->td_nstrips) {
  365. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  366. "%ld: Tile out of range, max %ld",
  367. (long) tile, (unsigned long) td->td_nstrips);
  368. return (-1);
  369. }
  370. if (size == (tsize_t) -1)
  371. size = tilesize;
  372. else if (size > tilesize)
  373. size = tilesize;
  374. if (TIFFFillTile(tif, tile) && (*tif->tif_decodetile)(tif,
  375. (tidata_t) buf, size, (tsample_t)(tile/td->td_stripsperimage))) {
  376. (*tif->tif_postdecode)(tif, (tidata_t) buf, size);
  377. return (size);
  378. } else
  379. return (-1);
  380. }
  381. static tsize_t
  382. TIFFReadRawTile1(TIFF* tif,
  383. ttile_t tile, tdata_t buf, tsize_t size, const char* module)
  384. {
  385. TIFFDirectory *td = &tif->tif_dir;
  386. assert((tif->tif_flags&TIFF_NOREADRAW)==0);
  387. if (!isMapped(tif)) {
  388. tsize_t cc;
  389. if (!SeekOK(tif, td->td_stripoffset[tile])) {
  390. TIFFErrorExt(tif->tif_clientdata, module,
  391. "%s: Seek error at row %ld, col %ld, tile %ld",
  392. tif->tif_name,
  393. (long) tif->tif_row,
  394. (long) tif->tif_col,
  395. (long) tile);
  396. return ((tsize_t) -1);
  397. }
  398. cc = TIFFReadFile(tif, buf, size);
  399. if (cc != size) {
  400. TIFFErrorExt(tif->tif_clientdata, module,
  401. "%s: Read error at row %ld, col %ld; got %lu bytes, expected %lu",
  402. tif->tif_name,
  403. (long) tif->tif_row,
  404. (long) tif->tif_col,
  405. (unsigned long) cc,
  406. (unsigned long) size);
  407. return ((tsize_t) -1);
  408. }
  409. } else {
  410. if (td->td_stripoffset[tile] + size > tif->tif_size) {
  411. TIFFErrorExt(tif->tif_clientdata, module,
  412. "%s: Read error at row %ld, col %ld, tile %ld; got %lu bytes, expected %lu",
  413. tif->tif_name,
  414. (long) tif->tif_row,
  415. (long) tif->tif_col,
  416. (long) tile,
  417. (unsigned long) tif->tif_size - td->td_stripoffset[tile],
  418. (unsigned long) size);
  419. return ((tsize_t) -1);
  420. }
  421. _TIFFmemcpy(buf, tif->tif_base + td->td_stripoffset[tile], size);
  422. }
  423. return (size);
  424. }
  425. /*
  426. * Read a tile of data from the file.
  427. */
  428. tsize_t
  429. TIFFReadRawTile(TIFF* tif, ttile_t tile, tdata_t buf, tsize_t size)
  430. {
  431. static const char module[] = "TIFFReadRawTile";
  432. TIFFDirectory *td = &tif->tif_dir;
  433. /*
  434. * FIXME: butecount should have tsize_t type, but for now libtiff
  435. * defines tsize_t as a signed 32-bit integer and we are losing
  436. * ability to read arrays larger than 2^31 bytes. So we are using
  437. * uint32 instead of tsize_t here.
  438. */
  439. uint32 bytecount;
  440. if (!TIFFCheckRead(tif, 1))
  441. return ((tsize_t) -1);
  442. if (tile >= td->td_nstrips) {
  443. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  444. "%lu: Tile out of range, max %lu",
  445. (unsigned long) tile, (unsigned long) td->td_nstrips);
  446. return ((tsize_t) -1);
  447. }
  448. if (tif->tif_flags&TIFF_NOREADRAW)
  449. {
  450. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  451. "Compression scheme does not support access to raw uncompressed data");
  452. return ((tsize_t) -1);
  453. }
  454. bytecount = td->td_stripbytecount[tile];
  455. if (size != (tsize_t) -1 && (uint32)size < bytecount)
  456. bytecount = size;
  457. return (TIFFReadRawTile1(tif, tile, buf, bytecount, module));
  458. }
  459. /*
  460. * Read the specified tile and setup for decoding. The data buffer is
  461. * expanded, as necessary, to hold the tile's data.
  462. */
  463. int
  464. TIFFFillTile(TIFF* tif, ttile_t tile)
  465. {
  466. static const char module[] = "TIFFFillTile";
  467. TIFFDirectory *td = &tif->tif_dir;
  468. if ((tif->tif_flags&TIFF_NOREADRAW)==0)
  469. {
  470. /*
  471. * FIXME: butecount should have tsize_t type, but for now
  472. * libtiff defines tsize_t as a signed 32-bit integer and we
  473. * are losing ability to read arrays larger than 2^31 bytes.
  474. * So we are using uint32 instead of tsize_t here.
  475. */
  476. uint32 bytecount = td->td_stripbytecount[tile];
  477. if (bytecount <= 0) {
  478. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  479. "%lu: Invalid tile byte count, tile %lu",
  480. (unsigned long) bytecount, (unsigned long) tile);
  481. return (0);
  482. }
  483. if (isMapped(tif) &&
  484. (isFillOrder(tif, td->td_fillorder)
  485. || (tif->tif_flags & TIFF_NOBITREV))) {
  486. /*
  487. * The image is mapped into memory and we either don't
  488. * need to flip bits or the compression routine is
  489. * going to handle this operation itself. In this
  490. * case, avoid copying the raw data and instead just
  491. * reference the data from the memory mapped file
  492. * image. This assumes that the decompression
  493. * routines do not modify the contents of the raw data
  494. * buffer (if they try to, the application will get a
  495. * fault since the file is mapped read-only).
  496. */
  497. if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
  498. _TIFFfree(tif->tif_rawdata);
  499. tif->tif_flags &= ~TIFF_MYBUFFER;
  500. /*
  501. * We must check for overflow, potentially causing
  502. * an OOB read. Instead of simple
  503. *
  504. * td->td_stripoffset[tile]+bytecount > tif->tif_size
  505. *
  506. * comparison (which can overflow) we do the following
  507. * two comparisons:
  508. */
  509. if (bytecount > tif->tif_size ||
  510. td->td_stripoffset[tile] > tif->tif_size - bytecount) {
  511. tif->tif_curtile = NOTILE;
  512. return (0);
  513. }
  514. tif->tif_rawdatasize = bytecount;
  515. tif->tif_rawdata =
  516. tif->tif_base + td->td_stripoffset[tile];
  517. } else {
  518. /*
  519. * Expand raw data buffer, if needed, to hold data
  520. * tile coming from file (perhaps should set upper
  521. * bound on the size of a buffer we'll use?).
  522. */
  523. if (bytecount > (uint32)tif->tif_rawdatasize) {
  524. tif->tif_curtile = NOTILE;
  525. if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
  526. TIFFErrorExt(tif->tif_clientdata,
  527. module,
  528. "%s: Data buffer too small to hold tile %ld",
  529. tif->tif_name,
  530. (long) tile);
  531. return (0);
  532. }
  533. if (!TIFFReadBufferSetup(tif, 0,
  534. TIFFroundup(bytecount, 1024)))
  535. return (0);
  536. }
  537. if ((uint32)TIFFReadRawTile1(tif, tile,
  538. (unsigned char *)tif->tif_rawdata,
  539. bytecount, module) != bytecount)
  540. return (0);
  541. if (!isFillOrder(tif, td->td_fillorder) &&
  542. (tif->tif_flags & TIFF_NOBITREV) == 0)
  543. TIFFReverseBits(tif->tif_rawdata, bytecount);
  544. }
  545. }
  546. return (TIFFStartTile(tif, tile));
  547. }
  548. /*
  549. * Setup the raw data buffer in preparation for
  550. * reading a strip of raw data. If the buffer
  551. * is specified as zero, then a buffer of appropriate
  552. * size is allocated by the library. Otherwise,
  553. * the client must guarantee that the buffer is
  554. * large enough to hold any individual strip of
  555. * raw data.
  556. */
  557. int
  558. TIFFReadBufferSetup(TIFF* tif, tdata_t bp, tsize_t size)
  559. {
  560. static const char module[] = "TIFFReadBufferSetup";
  561. assert((tif->tif_flags&TIFF_NOREADRAW)==0);
  562. if (tif->tif_rawdata) {
  563. if (tif->tif_flags & TIFF_MYBUFFER)
  564. _TIFFfree(tif->tif_rawdata);
  565. tif->tif_rawdata = NULL;
  566. }
  567. if (bp) {
  568. tif->tif_rawdatasize = size;
  569. tif->tif_rawdata = (tidata_t) bp;
  570. tif->tif_flags &= ~TIFF_MYBUFFER;
  571. } else {
  572. tif->tif_rawdatasize = TIFFroundup(size, 1024);
  573. if (tif->tif_rawdatasize > 0)
  574. tif->tif_rawdata = (tidata_t) _TIFFmalloc(tif->tif_rawdatasize);
  575. tif->tif_flags |= TIFF_MYBUFFER;
  576. }
  577. if ((tif->tif_rawdata == NULL) || (tif->tif_rawdatasize == 0)) {
  578. TIFFErrorExt(tif->tif_clientdata, module,
  579. "%s: No space for data buffer at scanline %ld",
  580. tif->tif_name, (long) tif->tif_row);
  581. tif->tif_rawdatasize = 0;
  582. return (0);
  583. }
  584. return (1);
  585. }
  586. /*
  587. * Set state to appear as if a
  588. * strip has just been read in.
  589. */
  590. static int
  591. TIFFStartStrip(TIFF* tif, tstrip_t strip)
  592. {
  593. TIFFDirectory *td = &tif->tif_dir;
  594. if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
  595. if (!(*tif->tif_setupdecode)(tif))
  596. return (0);
  597. tif->tif_flags |= TIFF_CODERSETUP;
  598. }
  599. tif->tif_curstrip = strip;
  600. tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
  601. if (tif->tif_flags&TIFF_NOREADRAW)
  602. {
  603. tif->tif_rawcp = NULL;
  604. tif->tif_rawcc = 0;
  605. }
  606. else
  607. {
  608. tif->tif_rawcp = tif->tif_rawdata;
  609. tif->tif_rawcc = td->td_stripbytecount[strip];
  610. }
  611. return ((*tif->tif_predecode)(tif,
  612. (tsample_t)(strip / td->td_stripsperimage)));
  613. }
  614. /*
  615. * Set state to appear as if a
  616. * tile has just been read in.
  617. */
  618. static int
  619. TIFFStartTile(TIFF* tif, ttile_t tile)
  620. {
  621. TIFFDirectory *td = &tif->tif_dir;
  622. if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
  623. if (!(*tif->tif_setupdecode)(tif))
  624. return (0);
  625. tif->tif_flags |= TIFF_CODERSETUP;
  626. }
  627. tif->tif_curtile = tile;
  628. tif->tif_row =
  629. (tile % TIFFhowmany(td->td_imagewidth, td->td_tilewidth)) *
  630. td->td_tilelength;
  631. tif->tif_col =
  632. (tile % TIFFhowmany(td->td_imagelength, td->td_tilelength)) *
  633. td->td_tilewidth;
  634. if (tif->tif_flags&TIFF_NOREADRAW)
  635. {
  636. tif->tif_rawcp = NULL;
  637. tif->tif_rawcc = 0;
  638. }
  639. else
  640. {
  641. tif->tif_rawcp = tif->tif_rawdata;
  642. tif->tif_rawcc = td->td_stripbytecount[tile];
  643. }
  644. return ((*tif->tif_predecode)(tif,
  645. (tsample_t)(tile/td->td_stripsperimage)));
  646. }
  647. static int
  648. TIFFCheckRead(TIFF* tif, int tiles)
  649. {
  650. if (tif->tif_mode == O_WRONLY) {
  651. TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "File not open for reading");
  652. return (0);
  653. }
  654. if (tiles ^ isTiled(tif)) {
  655. TIFFErrorExt(tif->tif_clientdata, tif->tif_name, tiles ?
  656. "Can not read tiles from a stripped image" :
  657. "Can not read scanlines from a tiled image");
  658. return (0);
  659. }
  660. return (1);
  661. }
  662. void
  663. _TIFFNoPostDecode(TIFF* tif, tidata_t buf, tsize_t cc)
  664. {
  665. (void) tif; (void) buf; (void) cc;
  666. }
  667. void
  668. _TIFFSwab16BitData(TIFF* tif, tidata_t buf, tsize_t cc)
  669. {
  670. (void) tif;
  671. assert((cc & 1) == 0);
  672. TIFFSwabArrayOfShort((uint16*) buf, cc/2);
  673. }
  674. void
  675. _TIFFSwab24BitData(TIFF* tif, tidata_t buf, tsize_t cc)
  676. {
  677. (void) tif;
  678. assert((cc % 3) == 0);
  679. TIFFSwabArrayOfTriples((uint8*) buf, cc/3);
  680. }
  681. void
  682. _TIFFSwab32BitData(TIFF* tif, tidata_t buf, tsize_t cc)
  683. {
  684. (void) tif;
  685. assert((cc & 3) == 0);
  686. TIFFSwabArrayOfLong((uint32*) buf, cc/4);
  687. }
  688. void
  689. _TIFFSwab64BitData(TIFF* tif, tidata_t buf, tsize_t cc)
  690. {
  691. (void) tif;
  692. assert((cc & 7) == 0);
  693. TIFFSwabArrayOfDouble((double*) buf, cc/8);
  694. }
  695. /* vim: set ts=8 sts=8 sw=8 noet: */
  696. /*
  697. * Local Variables:
  698. * mode: c
  699. * c-basic-offset: 8
  700. * fill-column: 78
  701. * End:
  702. */