2
0

tif_strip.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /* $Id: tif_strip.c,v 1.35 2012-06-06 05:33:55 fwarmerdam Exp $ */
  2. /*
  3. * Copyright (c) 1991-1997 Sam Leffler
  4. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and
  7. * its documentation for any purpose is hereby granted without fee, provided
  8. * that (i) the above copyright notices and this permission notice appear in
  9. * all copies of the software and related documentation, and (ii) the names of
  10. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11. * publicity relating to the software without the specific, prior written
  12. * permission of Sam Leffler and Silicon Graphics.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  22. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. */
  25. /*
  26. * TIFF Library.
  27. *
  28. * Strip-organized Image Support Routines.
  29. */
  30. #include "tiffiop.h"
  31. /*
  32. * Compute which strip a (row,sample) value is in.
  33. */
  34. uint32
  35. TIFFComputeStrip(TIFF* tif, uint32 row, uint16 sample)
  36. {
  37. static const char module[] = "TIFFComputeStrip";
  38. TIFFDirectory *td = &tif->tif_dir;
  39. uint32 strip;
  40. strip = row / td->td_rowsperstrip;
  41. if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
  42. if (sample >= td->td_samplesperpixel) {
  43. TIFFErrorExt(tif->tif_clientdata, module,
  44. "%lu: Sample out of range, max %lu",
  45. (unsigned long) sample, (unsigned long) td->td_samplesperpixel);
  46. return (0);
  47. }
  48. strip += (uint32)sample*td->td_stripsperimage;
  49. }
  50. return (strip);
  51. }
  52. /*
  53. * Compute how many strips are in an image.
  54. */
  55. uint32
  56. TIFFNumberOfStrips(TIFF* tif)
  57. {
  58. TIFFDirectory *td = &tif->tif_dir;
  59. uint32 nstrips;
  60. nstrips = (td->td_rowsperstrip == (uint32) -1 ? 1 :
  61. TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));
  62. if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
  63. nstrips = _TIFFMultiply32(tif, nstrips, (uint32)td->td_samplesperpixel,
  64. "TIFFNumberOfStrips");
  65. return (nstrips);
  66. }
  67. /*
  68. * Compute the # bytes in a variable height, row-aligned strip.
  69. */
  70. uint64
  71. TIFFVStripSize64(TIFF* tif, uint32 nrows)
  72. {
  73. static const char module[] = "TIFFVStripSize64";
  74. TIFFDirectory *td = &tif->tif_dir;
  75. if (nrows==(uint32)(-1))
  76. nrows=td->td_imagelength;
  77. if ((td->td_planarconfig==PLANARCONFIG_CONTIG)&&
  78. (td->td_photometric == PHOTOMETRIC_YCBCR)&&
  79. (!isUpSampled(tif)))
  80. {
  81. /*
  82. * Packed YCbCr data contain one Cb+Cr for every
  83. * HorizontalSampling*VerticalSampling Y values.
  84. * Must also roundup width and height when calculating
  85. * since images that are not a multiple of the
  86. * horizontal/vertical subsampling area include
  87. * YCbCr data for the extended image.
  88. */
  89. uint16 ycbcrsubsampling[2];
  90. uint16 samplingblock_samples;
  91. uint32 samplingblocks_hor;
  92. uint32 samplingblocks_ver;
  93. uint64 samplingrow_samples;
  94. uint64 samplingrow_size;
  95. if(td->td_samplesperpixel!=3)
  96. {
  97. TIFFErrorExt(tif->tif_clientdata,module,
  98. "Invalid td_samplesperpixel value");
  99. return 0;
  100. }
  101. TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,ycbcrsubsampling+0,
  102. ycbcrsubsampling+1);
  103. if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 && ycbcrsubsampling[0] != 4)
  104. ||(ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 && ycbcrsubsampling[1] != 4))
  105. {
  106. TIFFErrorExt(tif->tif_clientdata,module,
  107. "Invalid YCbCr subsampling (%dx%d)",
  108. ycbcrsubsampling[0],
  109. ycbcrsubsampling[1] );
  110. return 0;
  111. }
  112. samplingblock_samples=ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
  113. samplingblocks_hor=TIFFhowmany_32(td->td_imagewidth,ycbcrsubsampling[0]);
  114. samplingblocks_ver=TIFFhowmany_32(nrows,ycbcrsubsampling[1]);
  115. samplingrow_samples=_TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
  116. samplingrow_size=TIFFhowmany8_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module));
  117. return(_TIFFMultiply64(tif,samplingrow_size,samplingblocks_ver,module));
  118. }
  119. else
  120. return(_TIFFMultiply64(tif,nrows,TIFFScanlineSize64(tif),module));
  121. }
  122. tmsize_t
  123. TIFFVStripSize(TIFF* tif, uint32 nrows)
  124. {
  125. static const char module[] = "TIFFVStripSize";
  126. uint64 m;
  127. tmsize_t n;
  128. m=TIFFVStripSize64(tif,nrows);
  129. n=(tmsize_t)m;
  130. if ((uint64)n!=m)
  131. {
  132. TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
  133. n=0;
  134. }
  135. return(n);
  136. }
  137. /*
  138. * Compute the # bytes in a raw strip.
  139. */
  140. uint64
  141. TIFFRawStripSize64(TIFF* tif, uint32 strip)
  142. {
  143. static const char module[] = "TIFFRawStripSize64";
  144. TIFFDirectory* td = &tif->tif_dir;
  145. uint64 bytecount = td->td_stripbytecount[strip];
  146. if (bytecount == 0)
  147. {
  148. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  149. TIFFErrorExt(tif->tif_clientdata, module,
  150. "%I64u: Invalid strip byte count, strip %lu",
  151. (unsigned __int64) bytecount,
  152. (unsigned long) strip);
  153. #else
  154. TIFFErrorExt(tif->tif_clientdata, module,
  155. "%llu: Invalid strip byte count, strip %lu",
  156. (unsigned long long) bytecount,
  157. (unsigned long) strip);
  158. #endif
  159. bytecount = (uint64) -1;
  160. }
  161. return bytecount;
  162. }
  163. tmsize_t
  164. TIFFRawStripSize(TIFF* tif, uint32 strip)
  165. {
  166. static const char module[] = "TIFFRawStripSize";
  167. uint64 m;
  168. tmsize_t n;
  169. m=TIFFRawStripSize64(tif,strip);
  170. if (m==(uint64)(-1))
  171. n=(tmsize_t)(-1);
  172. else
  173. {
  174. n=(tmsize_t)m;
  175. if ((uint64)n!=m)
  176. {
  177. TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
  178. n=0;
  179. }
  180. }
  181. return(n);
  182. }
  183. /*
  184. * Compute the # bytes in a (row-aligned) strip.
  185. *
  186. * Note that if RowsPerStrip is larger than the
  187. * recorded ImageLength, then the strip size is
  188. * truncated to reflect the actual space required
  189. * to hold the strip.
  190. */
  191. uint64
  192. TIFFStripSize64(TIFF* tif)
  193. {
  194. TIFFDirectory* td = &tif->tif_dir;
  195. uint32 rps = td->td_rowsperstrip;
  196. if (rps > td->td_imagelength)
  197. rps = td->td_imagelength;
  198. return (TIFFVStripSize64(tif, rps));
  199. }
  200. tmsize_t
  201. TIFFStripSize(TIFF* tif)
  202. {
  203. static const char module[] = "TIFFStripSize";
  204. uint64 m;
  205. tmsize_t n;
  206. m=TIFFStripSize64(tif);
  207. n=(tmsize_t)m;
  208. if ((uint64)n!=m)
  209. {
  210. TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
  211. n=0;
  212. }
  213. return(n);
  214. }
  215. /*
  216. * Compute a default strip size based on the image
  217. * characteristics and a requested value. If the
  218. * request is <1 then we choose a strip size according
  219. * to certain heuristics.
  220. */
  221. uint32
  222. TIFFDefaultStripSize(TIFF* tif, uint32 request)
  223. {
  224. return (*tif->tif_defstripsize)(tif, request);
  225. }
  226. uint32
  227. _TIFFDefaultStripSize(TIFF* tif, uint32 s)
  228. {
  229. if ((int32) s < 1) {
  230. /*
  231. * If RowsPerStrip is unspecified, try to break the
  232. * image up into strips that are approximately
  233. * STRIP_SIZE_DEFAULT bytes long.
  234. */
  235. uint64 scanlinesize;
  236. uint64 rows;
  237. scanlinesize=TIFFScanlineSize64(tif);
  238. if (scanlinesize==0)
  239. scanlinesize=1;
  240. rows=(uint64)STRIP_SIZE_DEFAULT/scanlinesize;
  241. if (rows==0)
  242. rows=1;
  243. else if (rows>0xFFFFFFFF)
  244. rows=0xFFFFFFFF;
  245. s=(uint32)rows;
  246. }
  247. return (s);
  248. }
  249. /*
  250. * Return the number of bytes to read/write in a call to
  251. * one of the scanline-oriented i/o routines. Note that
  252. * this number may be 1/samples-per-pixel if data is
  253. * stored as separate planes.
  254. * The ScanlineSize in case of YCbCrSubsampling is defined as the
  255. * strip size divided by the strip height, i.e. the size of a pack of vertical
  256. * subsampling lines divided by vertical subsampling. It should thus make
  257. * sense when multiplied by a multiple of vertical subsampling.
  258. */
  259. uint64
  260. TIFFScanlineSize64(TIFF* tif)
  261. {
  262. static const char module[] = "TIFFScanlineSize64";
  263. TIFFDirectory *td = &tif->tif_dir;
  264. uint64 scanline_size;
  265. if (td->td_planarconfig==PLANARCONFIG_CONTIG)
  266. {
  267. if ((td->td_photometric==PHOTOMETRIC_YCBCR)&&
  268. (td->td_samplesperpixel==3)&&
  269. (!isUpSampled(tif)))
  270. {
  271. uint16 ycbcrsubsampling[2];
  272. uint16 samplingblock_samples;
  273. uint32 samplingblocks_hor;
  274. uint64 samplingrow_samples;
  275. uint64 samplingrow_size;
  276. if(td->td_samplesperpixel!=3)
  277. {
  278. TIFFErrorExt(tif->tif_clientdata,module,
  279. "Invalid td_samplesperpixel value");
  280. return 0;
  281. }
  282. TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,
  283. ycbcrsubsampling+0,
  284. ycbcrsubsampling+1);
  285. if (((ycbcrsubsampling[0]!=1)&&(ycbcrsubsampling[0]!=2)&&(ycbcrsubsampling[0]!=4)) ||
  286. ((ycbcrsubsampling[1]!=1)&&(ycbcrsubsampling[1]!=2)&&(ycbcrsubsampling[1]!=4)))
  287. {
  288. TIFFErrorExt(tif->tif_clientdata,module,
  289. "Invalid YCbCr subsampling");
  290. return 0;
  291. }
  292. samplingblock_samples = ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
  293. samplingblocks_hor = TIFFhowmany_32(td->td_imagewidth,ycbcrsubsampling[0]);
  294. samplingrow_samples = _TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
  295. samplingrow_size = TIFFhowmany_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module),8);
  296. scanline_size = (samplingrow_size/ycbcrsubsampling[1]);
  297. }
  298. else
  299. {
  300. uint64 scanline_samples;
  301. scanline_samples=_TIFFMultiply64(tif,td->td_imagewidth,td->td_samplesperpixel,module);
  302. scanline_size=TIFFhowmany_64(_TIFFMultiply64(tif,scanline_samples,td->td_bitspersample,module),8);
  303. }
  304. }
  305. else
  306. scanline_size=TIFFhowmany_64(_TIFFMultiply64(tif,td->td_imagewidth,td->td_bitspersample,module),8);
  307. return(scanline_size);
  308. }
  309. tmsize_t
  310. TIFFScanlineSize(TIFF* tif)
  311. {
  312. static const char module[] = "TIFFScanlineSize";
  313. uint64 m;
  314. tmsize_t n;
  315. m=TIFFScanlineSize64(tif);
  316. n=(tmsize_t)m;
  317. if ((uint64)n!=m)
  318. {
  319. TIFFErrorExt(tif->tif_clientdata,module,"Integer arithmetic overflow");
  320. n=0;
  321. }
  322. return(n);
  323. }
  324. /*
  325. * Return the number of bytes required to store a complete
  326. * decoded and packed raster scanline (as opposed to the
  327. * I/O size returned by TIFFScanlineSize which may be less
  328. * if data is store as separate planes).
  329. */
  330. uint64
  331. TIFFRasterScanlineSize64(TIFF* tif)
  332. {
  333. static const char module[] = "TIFFRasterScanlineSize64";
  334. TIFFDirectory *td = &tif->tif_dir;
  335. uint64 scanline;
  336. scanline = _TIFFMultiply64(tif, td->td_bitspersample, td->td_imagewidth, module);
  337. if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
  338. scanline = _TIFFMultiply64(tif, scanline, td->td_samplesperpixel, module);
  339. return (TIFFhowmany8_64(scanline));
  340. } else
  341. return (_TIFFMultiply64(tif, TIFFhowmany8_64(scanline),
  342. td->td_samplesperpixel, module));
  343. }
  344. tmsize_t
  345. TIFFRasterScanlineSize(TIFF* tif)
  346. {
  347. static const char module[] = "TIFFRasterScanlineSize";
  348. uint64 m;
  349. tmsize_t n;
  350. m=TIFFRasterScanlineSize64(tif);
  351. n=(tmsize_t)m;
  352. if ((uint64)n!=m)
  353. {
  354. TIFFErrorExt(tif->tif_clientdata,module,"Integer arithmetic overflow");
  355. n=0;
  356. }
  357. return(n);
  358. }
  359. /* vim: set ts=8 sts=8 sw=8 noet: */
  360. /*
  361. * Local Variables:
  362. * mode: c
  363. * c-basic-offset: 8
  364. * fill-column: 78
  365. * End:
  366. */