PageRenderTime 62ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/FreeImage/Source/LibTIFF/tif_getimage.c

https://bitbucket.org/cabalistic/ogredeps/
C | 2554 lines | 2069 code | 183 blank | 302 comment | 389 complexity | 01a416068600961142938173e5984966 MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause, CPL-1.0, Unlicense, GPL-2.0, GPL-3.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, BSD-2-Clause, LGPL-2.1
  1. /* $Id: tif_getimage.c,v 1.39 2011/04/10 17:14:09 drolon 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. * Read and return a packed RGBA image.
  29. */
  30. #include "tiffiop.h"
  31. #include <stdio.h>
  32. static int gtTileContig(TIFFRGBAImage*, uint32*, uint32, uint32);
  33. static int gtTileSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
  34. static int gtStripContig(TIFFRGBAImage*, uint32*, uint32, uint32);
  35. static int gtStripSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
  36. static int PickContigCase(TIFFRGBAImage*);
  37. static int PickSeparateCase(TIFFRGBAImage*);
  38. static const char photoTag[] = "PhotometricInterpretation";
  39. /*
  40. * Helper constants used in Orientation tag handling
  41. */
  42. #define FLIP_VERTICALLY 0x01
  43. #define FLIP_HORIZONTALLY 0x02
  44. /*
  45. * Color conversion constants. We will define display types here.
  46. */
  47. TIFFDisplay display_sRGB = {
  48. { /* XYZ -> luminance matrix */
  49. { 3.2410F, -1.5374F, -0.4986F },
  50. { -0.9692F, 1.8760F, 0.0416F },
  51. { 0.0556F, -0.2040F, 1.0570F }
  52. },
  53. 100.0F, 100.0F, 100.0F, /* Light o/p for reference white */
  54. 255, 255, 255, /* Pixel values for ref. white */
  55. 1.0F, 1.0F, 1.0F, /* Residual light o/p for black pixel */
  56. 2.4F, 2.4F, 2.4F, /* Gamma values for the three guns */
  57. };
  58. /*
  59. * Check the image to see if TIFFReadRGBAImage can deal with it.
  60. * 1/0 is returned according to whether or not the image can
  61. * be handled. If 0 is returned, emsg contains the reason
  62. * why it is being rejected.
  63. */
  64. int
  65. TIFFRGBAImageOK(TIFF* tif, char emsg[1024])
  66. {
  67. TIFFDirectory* td = &tif->tif_dir;
  68. uint16 photometric;
  69. int colorchannels;
  70. if (!tif->tif_decodestatus) {
  71. sprintf(emsg, "Sorry, requested compression method is not configured");
  72. return (0);
  73. }
  74. switch (td->td_bitspersample) {
  75. case 1:
  76. case 2:
  77. case 4:
  78. case 8:
  79. case 16:
  80. break;
  81. default:
  82. sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
  83. td->td_bitspersample);
  84. return (0);
  85. }
  86. colorchannels = td->td_samplesperpixel - td->td_extrasamples;
  87. if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric)) {
  88. switch (colorchannels) {
  89. case 1:
  90. photometric = PHOTOMETRIC_MINISBLACK;
  91. break;
  92. case 3:
  93. photometric = PHOTOMETRIC_RGB;
  94. break;
  95. default:
  96. sprintf(emsg, "Missing needed %s tag", photoTag);
  97. return (0);
  98. }
  99. }
  100. switch (photometric) {
  101. case PHOTOMETRIC_MINISWHITE:
  102. case PHOTOMETRIC_MINISBLACK:
  103. case PHOTOMETRIC_PALETTE:
  104. if (td->td_planarconfig == PLANARCONFIG_CONTIG
  105. && td->td_samplesperpixel != 1
  106. && td->td_bitspersample < 8 ) {
  107. sprintf(emsg,
  108. "Sorry, can not handle contiguous data with %s=%d, "
  109. "and %s=%d and Bits/Sample=%d",
  110. photoTag, photometric,
  111. "Samples/pixel", td->td_samplesperpixel,
  112. td->td_bitspersample);
  113. return (0);
  114. }
  115. /*
  116. * We should likely validate that any extra samples are either
  117. * to be ignored, or are alpha, and if alpha we should try to use
  118. * them. But for now we won't bother with this.
  119. */
  120. break;
  121. case PHOTOMETRIC_YCBCR:
  122. /*
  123. * TODO: if at all meaningful and useful, make more complete
  124. * support check here, or better still, refactor to let supporting
  125. * code decide whether there is support and what meaningfull
  126. * error to return
  127. */
  128. break;
  129. case PHOTOMETRIC_RGB:
  130. if (colorchannels < 3) {
  131. sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
  132. "Color channels", colorchannels);
  133. return (0);
  134. }
  135. break;
  136. case PHOTOMETRIC_SEPARATED:
  137. {
  138. uint16 inkset;
  139. TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
  140. if (inkset != INKSET_CMYK) {
  141. sprintf(emsg,
  142. "Sorry, can not handle separated image with %s=%d",
  143. "InkSet", inkset);
  144. return 0;
  145. }
  146. if (td->td_samplesperpixel < 4) {
  147. sprintf(emsg,
  148. "Sorry, can not handle separated image with %s=%d",
  149. "Samples/pixel", td->td_samplesperpixel);
  150. return 0;
  151. }
  152. break;
  153. }
  154. case PHOTOMETRIC_LOGL:
  155. if (td->td_compression != COMPRESSION_SGILOG) {
  156. sprintf(emsg, "Sorry, LogL data must have %s=%d",
  157. "Compression", COMPRESSION_SGILOG);
  158. return (0);
  159. }
  160. break;
  161. case PHOTOMETRIC_LOGLUV:
  162. if (td->td_compression != COMPRESSION_SGILOG &&
  163. td->td_compression != COMPRESSION_SGILOG24) {
  164. sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
  165. "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
  166. return (0);
  167. }
  168. if (td->td_planarconfig != PLANARCONFIG_CONTIG) {
  169. sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
  170. "Planarconfiguration", td->td_planarconfig);
  171. return (0);
  172. }
  173. break;
  174. case PHOTOMETRIC_CIELAB:
  175. break;
  176. default:
  177. sprintf(emsg, "Sorry, can not handle image with %s=%d",
  178. photoTag, photometric);
  179. return (0);
  180. }
  181. return (1);
  182. }
  183. void
  184. TIFFRGBAImageEnd(TIFFRGBAImage* img)
  185. {
  186. if (img->Map)
  187. _TIFFfree(img->Map), img->Map = NULL;
  188. if (img->BWmap)
  189. _TIFFfree(img->BWmap), img->BWmap = NULL;
  190. if (img->PALmap)
  191. _TIFFfree(img->PALmap), img->PALmap = NULL;
  192. if (img->ycbcr)
  193. _TIFFfree(img->ycbcr), img->ycbcr = NULL;
  194. if (img->cielab)
  195. _TIFFfree(img->cielab), img->cielab = NULL;
  196. if( img->redcmap ) {
  197. _TIFFfree( img->redcmap );
  198. _TIFFfree( img->greencmap );
  199. _TIFFfree( img->bluecmap );
  200. }
  201. }
  202. static int
  203. isCCITTCompression(TIFF* tif)
  204. {
  205. uint16 compress;
  206. TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
  207. return (compress == COMPRESSION_CCITTFAX3 ||
  208. compress == COMPRESSION_CCITTFAX4 ||
  209. compress == COMPRESSION_CCITTRLE ||
  210. compress == COMPRESSION_CCITTRLEW);
  211. }
  212. int
  213. TIFFRGBAImageBegin(TIFFRGBAImage* img, TIFF* tif, int stop, char emsg[1024])
  214. {
  215. uint16* sampleinfo;
  216. uint16 extrasamples;
  217. uint16 planarconfig;
  218. uint16 compress;
  219. int colorchannels;
  220. uint16 *red_orig, *green_orig, *blue_orig;
  221. int n_color;
  222. /* Initialize to normal values */
  223. img->row_offset = 0;
  224. img->col_offset = 0;
  225. img->redcmap = NULL;
  226. img->greencmap = NULL;
  227. img->bluecmap = NULL;
  228. img->req_orientation = ORIENTATION_BOTLEFT; /* It is the default */
  229. img->tif = tif;
  230. img->stoponerr = stop;
  231. TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
  232. switch (img->bitspersample) {
  233. case 1:
  234. case 2:
  235. case 4:
  236. case 8:
  237. case 16:
  238. break;
  239. default:
  240. sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
  241. img->bitspersample);
  242. return (0);
  243. }
  244. img->alpha = 0;
  245. TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
  246. TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
  247. &extrasamples, &sampleinfo);
  248. if (extrasamples >= 1)
  249. {
  250. switch (sampleinfo[0]) {
  251. case EXTRASAMPLE_UNSPECIFIED: /* Workaround for some images without */
  252. if (img->samplesperpixel > 3) /* correct info about alpha channel */
  253. img->alpha = EXTRASAMPLE_ASSOCALPHA;
  254. break;
  255. case EXTRASAMPLE_ASSOCALPHA: /* data is pre-multiplied */
  256. case EXTRASAMPLE_UNASSALPHA: /* data is not pre-multiplied */
  257. img->alpha = sampleinfo[0];
  258. break;
  259. }
  260. }
  261. #ifdef DEFAULT_EXTRASAMPLE_AS_ALPHA
  262. if( !TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))
  263. img->photometric = PHOTOMETRIC_MINISWHITE;
  264. if( extrasamples == 0
  265. && img->samplesperpixel == 4
  266. && img->photometric == PHOTOMETRIC_RGB )
  267. {
  268. img->alpha = EXTRASAMPLE_ASSOCALPHA;
  269. extrasamples = 1;
  270. }
  271. #endif
  272. colorchannels = img->samplesperpixel - extrasamples;
  273. TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);
  274. TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
  275. if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric)) {
  276. switch (colorchannels) {
  277. case 1:
  278. if (isCCITTCompression(tif))
  279. img->photometric = PHOTOMETRIC_MINISWHITE;
  280. else
  281. img->photometric = PHOTOMETRIC_MINISBLACK;
  282. break;
  283. case 3:
  284. img->photometric = PHOTOMETRIC_RGB;
  285. break;
  286. default:
  287. sprintf(emsg, "Missing needed %s tag", photoTag);
  288. return (0);
  289. }
  290. }
  291. switch (img->photometric) {
  292. case PHOTOMETRIC_PALETTE:
  293. if (!TIFFGetField(tif, TIFFTAG_COLORMAP,
  294. &red_orig, &green_orig, &blue_orig)) {
  295. sprintf(emsg, "Missing required \"Colormap\" tag");
  296. return (0);
  297. }
  298. /* copy the colormaps so we can modify them */
  299. n_color = (1L << img->bitspersample);
  300. img->redcmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
  301. img->greencmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
  302. img->bluecmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
  303. if( !img->redcmap || !img->greencmap || !img->bluecmap ) {
  304. sprintf(emsg, "Out of memory for colormap copy");
  305. return (0);
  306. }
  307. _TIFFmemcpy( img->redcmap, red_orig, n_color * 2 );
  308. _TIFFmemcpy( img->greencmap, green_orig, n_color * 2 );
  309. _TIFFmemcpy( img->bluecmap, blue_orig, n_color * 2 );
  310. /* fall thru... */
  311. case PHOTOMETRIC_MINISWHITE:
  312. case PHOTOMETRIC_MINISBLACK:
  313. if (planarconfig == PLANARCONFIG_CONTIG
  314. && img->samplesperpixel != 1
  315. && img->bitspersample < 8 ) {
  316. sprintf(emsg,
  317. "Sorry, can not handle contiguous data with %s=%d, "
  318. "and %s=%d and Bits/Sample=%d",
  319. photoTag, img->photometric,
  320. "Samples/pixel", img->samplesperpixel,
  321. img->bitspersample);
  322. return (0);
  323. }
  324. break;
  325. case PHOTOMETRIC_YCBCR:
  326. /* It would probably be nice to have a reality check here. */
  327. if (planarconfig == PLANARCONFIG_CONTIG)
  328. /* can rely on libjpeg to convert to RGB */
  329. /* XXX should restore current state on exit */
  330. switch (compress) {
  331. case COMPRESSION_JPEG:
  332. /*
  333. * TODO: when complete tests verify complete desubsampling
  334. * and YCbCr handling, remove use of TIFFTAG_JPEGCOLORMODE in
  335. * favor of tif_getimage.c native handling
  336. */
  337. TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
  338. img->photometric = PHOTOMETRIC_RGB;
  339. break;
  340. default:
  341. /* do nothing */;
  342. break;
  343. }
  344. /*
  345. * TODO: if at all meaningful and useful, make more complete
  346. * support check here, or better still, refactor to let supporting
  347. * code decide whether there is support and what meaningfull
  348. * error to return
  349. */
  350. break;
  351. case PHOTOMETRIC_RGB:
  352. if (colorchannels < 3) {
  353. sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
  354. "Color channels", colorchannels);
  355. return (0);
  356. }
  357. break;
  358. case PHOTOMETRIC_SEPARATED:
  359. {
  360. uint16 inkset;
  361. TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
  362. if (inkset != INKSET_CMYK) {
  363. sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
  364. "InkSet", inkset);
  365. return (0);
  366. }
  367. if (img->samplesperpixel < 4) {
  368. sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
  369. "Samples/pixel", img->samplesperpixel);
  370. return (0);
  371. }
  372. }
  373. break;
  374. case PHOTOMETRIC_LOGL:
  375. if (compress != COMPRESSION_SGILOG) {
  376. sprintf(emsg, "Sorry, LogL data must have %s=%d",
  377. "Compression", COMPRESSION_SGILOG);
  378. return (0);
  379. }
  380. TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
  381. img->photometric = PHOTOMETRIC_MINISBLACK; /* little white lie */
  382. img->bitspersample = 8;
  383. break;
  384. case PHOTOMETRIC_LOGLUV:
  385. if (compress != COMPRESSION_SGILOG && compress != COMPRESSION_SGILOG24) {
  386. sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
  387. "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
  388. return (0);
  389. }
  390. if (planarconfig != PLANARCONFIG_CONTIG) {
  391. sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
  392. "Planarconfiguration", planarconfig);
  393. return (0);
  394. }
  395. TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
  396. img->photometric = PHOTOMETRIC_RGB; /* little white lie */
  397. img->bitspersample = 8;
  398. break;
  399. case PHOTOMETRIC_CIELAB:
  400. break;
  401. default:
  402. sprintf(emsg, "Sorry, can not handle image with %s=%d",
  403. photoTag, img->photometric);
  404. return (0);
  405. }
  406. img->Map = NULL;
  407. img->BWmap = NULL;
  408. img->PALmap = NULL;
  409. img->ycbcr = NULL;
  410. img->cielab = NULL;
  411. TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
  412. TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
  413. TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
  414. img->isContig =
  415. !(planarconfig == PLANARCONFIG_SEPARATE && colorchannels > 1);
  416. if (img->isContig) {
  417. if (!PickContigCase(img)) {
  418. sprintf(emsg, "Sorry, can not handle image");
  419. return 0;
  420. }
  421. } else {
  422. if (!PickSeparateCase(img)) {
  423. sprintf(emsg, "Sorry, can not handle image");
  424. return 0;
  425. }
  426. }
  427. return 1;
  428. }
  429. int
  430. TIFFRGBAImageGet(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
  431. {
  432. if (img->get == NULL) {
  433. TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No \"get\" routine setup");
  434. return (0);
  435. }
  436. if (img->put.any == NULL) {
  437. TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
  438. "No \"put\" routine setupl; probably can not handle image format");
  439. return (0);
  440. }
  441. return (*img->get)(img, raster, w, h);
  442. }
  443. /*
  444. * Read the specified image into an ABGR-format rastertaking in account
  445. * specified orientation.
  446. */
  447. int
  448. TIFFReadRGBAImageOriented(TIFF* tif,
  449. uint32 rwidth, uint32 rheight, uint32* raster,
  450. int orientation, int stop)
  451. {
  452. char emsg[1024] = "";
  453. TIFFRGBAImage img;
  454. int ok;
  455. if (TIFFRGBAImageOK(tif, emsg) && TIFFRGBAImageBegin(&img, tif, stop, emsg)) {
  456. img.req_orientation = orientation;
  457. /* XXX verify rwidth and rheight against width and height */
  458. ok = TIFFRGBAImageGet(&img, raster+(rheight-img.height)*rwidth,
  459. rwidth, img.height);
  460. TIFFRGBAImageEnd(&img);
  461. } else {
  462. TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
  463. ok = 0;
  464. }
  465. return (ok);
  466. }
  467. /*
  468. * Read the specified image into an ABGR-format raster. Use bottom left
  469. * origin for raster by default.
  470. */
  471. int
  472. TIFFReadRGBAImage(TIFF* tif,
  473. uint32 rwidth, uint32 rheight, uint32* raster, int stop)
  474. {
  475. return TIFFReadRGBAImageOriented(tif, rwidth, rheight, raster,
  476. ORIENTATION_BOTLEFT, stop);
  477. }
  478. static int
  479. setorientation(TIFFRGBAImage* img)
  480. {
  481. switch (img->orientation) {
  482. case ORIENTATION_TOPLEFT:
  483. case ORIENTATION_LEFTTOP:
  484. if (img->req_orientation == ORIENTATION_TOPRIGHT ||
  485. img->req_orientation == ORIENTATION_RIGHTTOP)
  486. return FLIP_HORIZONTALLY;
  487. else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
  488. img->req_orientation == ORIENTATION_RIGHTBOT)
  489. return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
  490. else if (img->req_orientation == ORIENTATION_BOTLEFT ||
  491. img->req_orientation == ORIENTATION_LEFTBOT)
  492. return FLIP_VERTICALLY;
  493. else
  494. return 0;
  495. case ORIENTATION_TOPRIGHT:
  496. case ORIENTATION_RIGHTTOP:
  497. if (img->req_orientation == ORIENTATION_TOPLEFT ||
  498. img->req_orientation == ORIENTATION_LEFTTOP)
  499. return FLIP_HORIZONTALLY;
  500. else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
  501. img->req_orientation == ORIENTATION_RIGHTBOT)
  502. return FLIP_VERTICALLY;
  503. else if (img->req_orientation == ORIENTATION_BOTLEFT ||
  504. img->req_orientation == ORIENTATION_LEFTBOT)
  505. return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
  506. else
  507. return 0;
  508. case ORIENTATION_BOTRIGHT:
  509. case ORIENTATION_RIGHTBOT:
  510. if (img->req_orientation == ORIENTATION_TOPLEFT ||
  511. img->req_orientation == ORIENTATION_LEFTTOP)
  512. return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
  513. else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
  514. img->req_orientation == ORIENTATION_RIGHTTOP)
  515. return FLIP_VERTICALLY;
  516. else if (img->req_orientation == ORIENTATION_BOTLEFT ||
  517. img->req_orientation == ORIENTATION_LEFTBOT)
  518. return FLIP_HORIZONTALLY;
  519. else
  520. return 0;
  521. case ORIENTATION_BOTLEFT:
  522. case ORIENTATION_LEFTBOT:
  523. if (img->req_orientation == ORIENTATION_TOPLEFT ||
  524. img->req_orientation == ORIENTATION_LEFTTOP)
  525. return FLIP_VERTICALLY;
  526. else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
  527. img->req_orientation == ORIENTATION_RIGHTTOP)
  528. return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
  529. else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
  530. img->req_orientation == ORIENTATION_RIGHTBOT)
  531. return FLIP_HORIZONTALLY;
  532. else
  533. return 0;
  534. default: /* NOTREACHED */
  535. return 0;
  536. }
  537. }
  538. /*
  539. * Get an tile-organized image that has
  540. * PlanarConfiguration contiguous if SamplesPerPixel > 1
  541. * or
  542. * SamplesPerPixel == 1
  543. */
  544. static int
  545. gtTileContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
  546. {
  547. TIFF* tif = img->tif;
  548. tileContigRoutine put = img->put.contig;
  549. uint32 col, row, y, rowstoread;
  550. uint32 pos;
  551. uint32 tw, th;
  552. unsigned char* buf;
  553. int32 fromskew, toskew;
  554. uint32 nrow;
  555. int ret = 1, flip;
  556. buf = (unsigned char*) _TIFFmalloc(TIFFTileSize(tif));
  557. if (buf == 0) {
  558. TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
  559. return (0);
  560. }
  561. _TIFFmemset(buf, 0, TIFFTileSize(tif));
  562. TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
  563. TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
  564. flip = setorientation(img);
  565. if (flip & FLIP_VERTICALLY) {
  566. y = h - 1;
  567. toskew = -(int32)(tw + w);
  568. }
  569. else {
  570. y = 0;
  571. toskew = -(int32)(tw - w);
  572. }
  573. for (row = 0; row < h; row += nrow)
  574. {
  575. rowstoread = th - (row + img->row_offset) % th;
  576. nrow = (row + rowstoread > h ? h - row : rowstoread);
  577. for (col = 0; col < w; col += tw)
  578. {
  579. if (TIFFReadTile(tif, buf, col+img->col_offset,
  580. row+img->row_offset, 0, 0) < 0 && img->stoponerr)
  581. {
  582. ret = 0;
  583. break;
  584. }
  585. pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif);
  586. if (col + tw > w)
  587. {
  588. /*
  589. * Tile is clipped horizontally. Calculate
  590. * visible portion and skewing factors.
  591. */
  592. uint32 npix = w - col;
  593. fromskew = tw - npix;
  594. (*put)(img, raster+y*w+col, col, y,
  595. npix, nrow, fromskew, toskew + fromskew, buf + pos);
  596. }
  597. else
  598. {
  599. (*put)(img, raster+y*w+col, col, y, tw, nrow, 0, toskew, buf + pos);
  600. }
  601. }
  602. y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
  603. }
  604. _TIFFfree(buf);
  605. if (flip & FLIP_HORIZONTALLY) {
  606. uint32 line;
  607. for (line = 0; line < h; line++) {
  608. uint32 *left = raster + (line * w);
  609. uint32 *right = left + w - 1;
  610. while ( left < right ) {
  611. uint32 temp = *left;
  612. *left = *right;
  613. *right = temp;
  614. left++, right--;
  615. }
  616. }
  617. }
  618. return (ret);
  619. }
  620. /*
  621. * Get an tile-organized image that has
  622. * SamplesPerPixel > 1
  623. * PlanarConfiguration separated
  624. * We assume that all such images are RGB.
  625. */
  626. static int
  627. gtTileSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
  628. {
  629. TIFF* tif = img->tif;
  630. tileSeparateRoutine put = img->put.separate;
  631. uint32 col, row, y, rowstoread;
  632. uint32 pos;
  633. uint32 tw, th;
  634. unsigned char* buf;
  635. unsigned char* p0;
  636. unsigned char* p1;
  637. unsigned char* p2;
  638. unsigned char* pa;
  639. tsize_t tilesize;
  640. int32 fromskew, toskew;
  641. int alpha = img->alpha;
  642. uint32 nrow;
  643. int ret = 1, flip;
  644. tilesize = TIFFTileSize(tif);
  645. buf = (unsigned char*) _TIFFmalloc((alpha?4:3)*tilesize);
  646. if (buf == 0) {
  647. TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
  648. return (0);
  649. }
  650. _TIFFmemset(buf, 0, (alpha?4:3)*tilesize);
  651. p0 = buf;
  652. p1 = p0 + tilesize;
  653. p2 = p1 + tilesize;
  654. pa = (alpha?(p2+tilesize):NULL);
  655. TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
  656. TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
  657. flip = setorientation(img);
  658. if (flip & FLIP_VERTICALLY) {
  659. y = h - 1;
  660. toskew = -(int32)(tw + w);
  661. }
  662. else {
  663. y = 0;
  664. toskew = -(int32)(tw - w);
  665. }
  666. for (row = 0; row < h; row += nrow)
  667. {
  668. rowstoread = th - (row + img->row_offset) % th;
  669. nrow = (row + rowstoread > h ? h - row : rowstoread);
  670. for (col = 0; col < w; col += tw)
  671. {
  672. if (TIFFReadTile(tif, p0, col+img->col_offset,
  673. row+img->row_offset,0,0) < 0 && img->stoponerr)
  674. {
  675. ret = 0;
  676. break;
  677. }
  678. if (TIFFReadTile(tif, p1, col+img->col_offset,
  679. row+img->row_offset,0,1) < 0 && img->stoponerr)
  680. {
  681. ret = 0;
  682. break;
  683. }
  684. if (TIFFReadTile(tif, p2, col+img->col_offset,
  685. row+img->row_offset,0,2) < 0 && img->stoponerr)
  686. {
  687. ret = 0;
  688. break;
  689. }
  690. if (alpha)
  691. {
  692. if (TIFFReadTile(tif,pa,col+img->col_offset,
  693. row+img->row_offset,0,3) < 0 && img->stoponerr)
  694. {
  695. ret = 0;
  696. break;
  697. }
  698. }
  699. pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif);
  700. if (col + tw > w)
  701. {
  702. /*
  703. * Tile is clipped horizontally. Calculate
  704. * visible portion and skewing factors.
  705. */
  706. uint32 npix = w - col;
  707. fromskew = tw - npix;
  708. (*put)(img, raster+y*w+col, col, y,
  709. npix, nrow, fromskew, toskew + fromskew,
  710. p0 + pos, p1 + pos, p2 + pos, (alpha?(pa+pos):NULL));
  711. } else {
  712. (*put)(img, raster+y*w+col, col, y,
  713. tw, nrow, 0, toskew, p0 + pos, p1 + pos, p2 + pos, (alpha?(pa+pos):NULL));
  714. }
  715. }
  716. y += (flip & FLIP_VERTICALLY ?-(int32) nrow : (int32) nrow);
  717. }
  718. if (flip & FLIP_HORIZONTALLY) {
  719. uint32 line;
  720. for (line = 0; line < h; line++) {
  721. uint32 *left = raster + (line * w);
  722. uint32 *right = left + w - 1;
  723. while ( left < right ) {
  724. uint32 temp = *left;
  725. *left = *right;
  726. *right = temp;
  727. left++, right--;
  728. }
  729. }
  730. }
  731. _TIFFfree(buf);
  732. return (ret);
  733. }
  734. /*
  735. * Get a strip-organized image that has
  736. * PlanarConfiguration contiguous if SamplesPerPixel > 1
  737. * or
  738. * SamplesPerPixel == 1
  739. */
  740. static int
  741. gtStripContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
  742. {
  743. TIFF* tif = img->tif;
  744. tileContigRoutine put = img->put.contig;
  745. uint32 row, y, nrow, nrowsub, rowstoread;
  746. uint32 pos;
  747. unsigned char* buf;
  748. uint32 rowsperstrip;
  749. uint16 subsamplinghor,subsamplingver;
  750. uint32 imagewidth = img->width;
  751. tsize_t scanline;
  752. int32 fromskew, toskew;
  753. int ret = 1, flip;
  754. buf = (unsigned char*) _TIFFmalloc(TIFFStripSize(tif));
  755. if (buf == 0) {
  756. TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for strip buffer");
  757. return (0);
  758. }
  759. _TIFFmemset(buf, 0, TIFFStripSize(tif));
  760. flip = setorientation(img);
  761. if (flip & FLIP_VERTICALLY) {
  762. y = h - 1;
  763. toskew = -(int32)(w + w);
  764. } else {
  765. y = 0;
  766. toskew = -(int32)(w - w);
  767. }
  768. TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
  769. TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, &subsamplinghor, &subsamplingver);
  770. scanline = TIFFNewScanlineSize(tif);
  771. fromskew = (w < imagewidth ? imagewidth - w : 0);
  772. for (row = 0; row < h; row += nrow)
  773. {
  774. rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
  775. nrow = (row + rowstoread > h ? h - row : rowstoread);
  776. nrowsub = nrow;
  777. if ((nrowsub%subsamplingver)!=0)
  778. nrowsub+=subsamplingver-nrowsub%subsamplingver;
  779. if (TIFFReadEncodedStrip(tif,
  780. TIFFComputeStrip(tif,row+img->row_offset, 0),
  781. buf,
  782. ((row + img->row_offset)%rowsperstrip + nrowsub) * scanline) < 0
  783. && img->stoponerr)
  784. {
  785. ret = 0;
  786. break;
  787. }
  788. pos = ((row + img->row_offset) % rowsperstrip) * scanline;
  789. (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, buf + pos);
  790. y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
  791. }
  792. if (flip & FLIP_HORIZONTALLY) {
  793. uint32 line;
  794. for (line = 0; line < h; line++) {
  795. uint32 *left = raster + (line * w);
  796. uint32 *right = left + w - 1;
  797. while ( left < right ) {
  798. uint32 temp = *left;
  799. *left = *right;
  800. *right = temp;
  801. left++, right--;
  802. }
  803. }
  804. }
  805. _TIFFfree(buf);
  806. return (ret);
  807. }
  808. /*
  809. * Get a strip-organized image with
  810. * SamplesPerPixel > 1
  811. * PlanarConfiguration separated
  812. * We assume that all such images are RGB.
  813. */
  814. static int
  815. gtStripSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
  816. {
  817. TIFF* tif = img->tif;
  818. tileSeparateRoutine put = img->put.separate;
  819. unsigned char *buf;
  820. unsigned char *p0, *p1, *p2, *pa;
  821. uint32 row, y, nrow, rowstoread;
  822. uint32 pos;
  823. tsize_t scanline;
  824. uint32 rowsperstrip, offset_row;
  825. uint32 imagewidth = img->width;
  826. tsize_t stripsize;
  827. int32 fromskew, toskew;
  828. int alpha = img->alpha;
  829. int ret = 1, flip;
  830. stripsize = TIFFStripSize(tif);
  831. p0 = buf = (unsigned char *)_TIFFmalloc((alpha?4:3)*stripsize);
  832. if (buf == 0) {
  833. TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
  834. return (0);
  835. }
  836. _TIFFmemset(buf, 0, (alpha?4:3)*stripsize);
  837. p1 = p0 + stripsize;
  838. p2 = p1 + stripsize;
  839. pa = (alpha?(p2+stripsize):NULL);
  840. flip = setorientation(img);
  841. if (flip & FLIP_VERTICALLY) {
  842. y = h - 1;
  843. toskew = -(int32)(w + w);
  844. }
  845. else {
  846. y = 0;
  847. toskew = -(int32)(w - w);
  848. }
  849. TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
  850. scanline = TIFFScanlineSize(tif);
  851. fromskew = (w < imagewidth ? imagewidth - w : 0);
  852. for (row = 0; row < h; row += nrow)
  853. {
  854. rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
  855. nrow = (row + rowstoread > h ? h - row : rowstoread);
  856. offset_row = row + img->row_offset;
  857. if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0),
  858. p0, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0
  859. && img->stoponerr)
  860. {
  861. ret = 0;
  862. break;
  863. }
  864. if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1),
  865. p1, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0
  866. && img->stoponerr)
  867. {
  868. ret = 0;
  869. break;
  870. }
  871. if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2),
  872. p2, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0
  873. && img->stoponerr)
  874. {
  875. ret = 0;
  876. break;
  877. }
  878. if (alpha)
  879. {
  880. if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 3),
  881. pa, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0
  882. && img->stoponerr)
  883. {
  884. ret = 0;
  885. break;
  886. }
  887. }
  888. pos = ((row + img->row_offset) % rowsperstrip) * scanline;
  889. (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, p0 + pos, p1 + pos,
  890. p2 + pos, (alpha?(pa+pos):NULL));
  891. y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
  892. }
  893. if (flip & FLIP_HORIZONTALLY) {
  894. uint32 line;
  895. for (line = 0; line < h; line++) {
  896. uint32 *left = raster + (line * w);
  897. uint32 *right = left + w - 1;
  898. while ( left < right ) {
  899. uint32 temp = *left;
  900. *left = *right;
  901. *right = temp;
  902. left++, right--;
  903. }
  904. }
  905. }
  906. _TIFFfree(buf);
  907. return (ret);
  908. }
  909. /*
  910. * The following routines move decoded data returned
  911. * from the TIFF library into rasters filled with packed
  912. * ABGR pixels (i.e. suitable for passing to lrecwrite.)
  913. *
  914. * The routines have been created according to the most
  915. * important cases and optimized. PickContigCase and
  916. * PickSeparateCase analyze the parameters and select
  917. * the appropriate "get" and "put" routine to use.
  918. */
  919. #define REPEAT8(op) REPEAT4(op); REPEAT4(op)
  920. #define REPEAT4(op) REPEAT2(op); REPEAT2(op)
  921. #define REPEAT2(op) op; op
  922. #define CASE8(x,op) \
  923. switch (x) { \
  924. case 7: op; case 6: op; case 5: op; \
  925. case 4: op; case 3: op; case 2: op; \
  926. case 1: op; \
  927. }
  928. #define CASE4(x,op) switch (x) { case 3: op; case 2: op; case 1: op; }
  929. #define NOP
  930. #define UNROLL8(w, op1, op2) { \
  931. uint32 _x; \
  932. for (_x = w; _x >= 8; _x -= 8) { \
  933. op1; \
  934. REPEAT8(op2); \
  935. } \
  936. if (_x > 0) { \
  937. op1; \
  938. CASE8(_x,op2); \
  939. } \
  940. }
  941. #define UNROLL4(w, op1, op2) { \
  942. uint32 _x; \
  943. for (_x = w; _x >= 4; _x -= 4) { \
  944. op1; \
  945. REPEAT4(op2); \
  946. } \
  947. if (_x > 0) { \
  948. op1; \
  949. CASE4(_x,op2); \
  950. } \
  951. }
  952. #define UNROLL2(w, op1, op2) { \
  953. uint32 _x; \
  954. for (_x = w; _x >= 2; _x -= 2) { \
  955. op1; \
  956. REPEAT2(op2); \
  957. } \
  958. if (_x) { \
  959. op1; \
  960. op2; \
  961. } \
  962. }
  963. #define SKEW(r,g,b,skew) { r += skew; g += skew; b += skew; }
  964. #define SKEW4(r,g,b,a,skew) { r += skew; g += skew; b += skew; a+= skew; }
  965. #define A1 (((uint32)0xffL)<<24)
  966. #define PACK(r,g,b) \
  967. ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|A1)
  968. #define PACK4(r,g,b,a) \
  969. ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|((uint32)(a)<<24))
  970. #define W2B(v) (((v)>>8)&0xff)
  971. #define PACKW(r,g,b) \
  972. ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|A1)
  973. #define PACKW4(r,g,b,a) \
  974. ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|((uint32)W2B(a)<<24))
  975. #define DECLAREContigPutFunc(name) \
  976. static void name(\
  977. TIFFRGBAImage* img, \
  978. uint32* cp, \
  979. uint32 x, uint32 y, \
  980. uint32 w, uint32 h, \
  981. int32 fromskew, int32 toskew, \
  982. unsigned char* pp \
  983. )
  984. /*
  985. * 8-bit palette => colormap/RGB
  986. */
  987. DECLAREContigPutFunc(put8bitcmaptile)
  988. {
  989. uint32** PALmap = img->PALmap;
  990. int samplesperpixel = img->samplesperpixel;
  991. (void) y;
  992. while (h-- > 0) {
  993. for (x = w; x-- > 0;)
  994. {
  995. *cp++ = PALmap[*pp][0];
  996. pp += samplesperpixel;
  997. }
  998. cp += toskew;
  999. pp += fromskew;
  1000. }
  1001. }
  1002. /*
  1003. * 4-bit palette => colormap/RGB
  1004. */
  1005. DECLAREContigPutFunc(put4bitcmaptile)
  1006. {
  1007. uint32** PALmap = img->PALmap;
  1008. (void) x; (void) y;
  1009. fromskew /= 2;
  1010. while (h-- > 0) {
  1011. uint32* bw;
  1012. UNROLL2(w, bw = PALmap[*pp++], *cp++ = *bw++);
  1013. cp += toskew;
  1014. pp += fromskew;
  1015. }
  1016. }
  1017. /*
  1018. * 2-bit palette => colormap/RGB
  1019. */
  1020. DECLAREContigPutFunc(put2bitcmaptile)
  1021. {
  1022. uint32** PALmap = img->PALmap;
  1023. (void) x; (void) y;
  1024. fromskew /= 4;
  1025. while (h-- > 0) {
  1026. uint32* bw;
  1027. UNROLL4(w, bw = PALmap[*pp++], *cp++ = *bw++);
  1028. cp += toskew;
  1029. pp += fromskew;
  1030. }
  1031. }
  1032. /*
  1033. * 1-bit palette => colormap/RGB
  1034. */
  1035. DECLAREContigPutFunc(put1bitcmaptile)
  1036. {
  1037. uint32** PALmap = img->PALmap;
  1038. (void) x; (void) y;
  1039. fromskew /= 8;
  1040. while (h-- > 0) {
  1041. uint32* bw;
  1042. UNROLL8(w, bw = PALmap[*pp++], *cp++ = *bw++);
  1043. cp += toskew;
  1044. pp += fromskew;
  1045. }
  1046. }
  1047. /*
  1048. * 8-bit greyscale => colormap/RGB
  1049. */
  1050. DECLAREContigPutFunc(putgreytile)
  1051. {
  1052. int samplesperpixel = img->samplesperpixel;
  1053. uint32** BWmap = img->BWmap;
  1054. (void) y;
  1055. while (h-- > 0) {
  1056. for (x = w; x-- > 0;)
  1057. {
  1058. *cp++ = BWmap[*pp][0];
  1059. pp += samplesperpixel;
  1060. }
  1061. cp += toskew;
  1062. pp += fromskew;
  1063. }
  1064. }
  1065. /*
  1066. * 16-bit greyscale => colormap/RGB
  1067. */
  1068. DECLAREContigPutFunc(put16bitbwtile)
  1069. {
  1070. int samplesperpixel = img->samplesperpixel;
  1071. uint32** BWmap = img->BWmap;
  1072. (void) y;
  1073. while (h-- > 0) {
  1074. uint16 *wp = (uint16 *) pp;
  1075. for (x = w; x-- > 0;)
  1076. {
  1077. /* use high order byte of 16bit value */
  1078. *cp++ = BWmap[*wp >> 8][0];
  1079. pp += 2 * samplesperpixel;
  1080. wp += samplesperpixel;
  1081. }
  1082. cp += toskew;
  1083. pp += fromskew;
  1084. }
  1085. }
  1086. /*
  1087. * 1-bit bilevel => colormap/RGB
  1088. */
  1089. DECLAREContigPutFunc(put1bitbwtile)
  1090. {
  1091. uint32** BWmap = img->BWmap;
  1092. (void) x; (void) y;
  1093. fromskew /= 8;
  1094. while (h-- > 0) {
  1095. uint32* bw;
  1096. UNROLL8(w, bw = BWmap[*pp++], *cp++ = *bw++);
  1097. cp += toskew;
  1098. pp += fromskew;
  1099. }
  1100. }
  1101. /*
  1102. * 2-bit greyscale => colormap/RGB
  1103. */
  1104. DECLAREContigPutFunc(put2bitbwtile)
  1105. {
  1106. uint32** BWmap = img->BWmap;
  1107. (void) x; (void) y;
  1108. fromskew /= 4;
  1109. while (h-- > 0) {
  1110. uint32* bw;
  1111. UNROLL4(w, bw = BWmap[*pp++], *cp++ = *bw++);
  1112. cp += toskew;
  1113. pp += fromskew;
  1114. }
  1115. }
  1116. /*
  1117. * 4-bit greyscale => colormap/RGB
  1118. */
  1119. DECLAREContigPutFunc(put4bitbwtile)
  1120. {
  1121. uint32** BWmap = img->BWmap;
  1122. (void) x; (void) y;
  1123. fromskew /= 2;
  1124. while (h-- > 0) {
  1125. uint32* bw;
  1126. UNROLL2(w, bw = BWmap[*pp++], *cp++ = *bw++);
  1127. cp += toskew;
  1128. pp += fromskew;
  1129. }
  1130. }
  1131. /*
  1132. * 8-bit packed samples, no Map => RGB
  1133. */
  1134. DECLAREContigPutFunc(putRGBcontig8bittile)
  1135. {
  1136. int samplesperpixel = img->samplesperpixel;
  1137. (void) x; (void) y;
  1138. fromskew *= samplesperpixel;
  1139. while (h-- > 0) {
  1140. UNROLL8(w, NOP,
  1141. *cp++ = PACK(pp[0], pp[1], pp[2]);
  1142. pp += samplesperpixel);
  1143. cp += toskew;
  1144. pp += fromskew;
  1145. }
  1146. }
  1147. /*
  1148. * 8-bit packed samples => RGBA w/ associated alpha
  1149. * (known to have Map == NULL)
  1150. */
  1151. DECLAREContigPutFunc(putRGBAAcontig8bittile)
  1152. {
  1153. int samplesperpixel = img->samplesperpixel;
  1154. (void) x; (void) y;
  1155. fromskew *= samplesperpixel;
  1156. while (h-- > 0) {
  1157. UNROLL8(w, NOP,
  1158. *cp++ = PACK4(pp[0], pp[1], pp[2], pp[3]);
  1159. pp += samplesperpixel);
  1160. cp += toskew;
  1161. pp += fromskew;
  1162. }
  1163. }
  1164. /*
  1165. * 8-bit packed samples => RGBA w/ unassociated alpha
  1166. * (known to have Map == NULL)
  1167. */
  1168. DECLAREContigPutFunc(putRGBUAcontig8bittile)
  1169. {
  1170. int samplesperpixel = img->samplesperpixel;
  1171. (void) y;
  1172. fromskew *= samplesperpixel;
  1173. while (h-- > 0) {
  1174. uint32 r, g, b, a;
  1175. for (x = w; x-- > 0;) {
  1176. a = pp[3];
  1177. r = (a*pp[0] + 127) / 255;
  1178. g = (a*pp[1] + 127) / 255;
  1179. b = (a*pp[2] + 127) / 255;
  1180. *cp++ = PACK4(r,g,b,a);
  1181. pp += samplesperpixel;
  1182. }
  1183. cp += toskew;
  1184. pp += fromskew;
  1185. }
  1186. }
  1187. /*
  1188. * 16-bit packed samples => RGB
  1189. */
  1190. DECLAREContigPutFunc(putRGBcontig16bittile)
  1191. {
  1192. int samplesperpixel = img->samplesperpixel;
  1193. uint16 *wp = (uint16 *)pp;
  1194. (void) y;
  1195. fromskew *= samplesperpixel;
  1196. while (h-- > 0) {
  1197. for (x = w; x-- > 0;) {
  1198. *cp++ = PACKW(wp[0],wp[1],wp[2]);
  1199. wp += samplesperpixel;
  1200. }
  1201. cp += toskew;
  1202. wp += fromskew;
  1203. }
  1204. }
  1205. /*
  1206. * 16-bit packed samples => RGBA w/ associated alpha
  1207. * (known to have Map == NULL)
  1208. */
  1209. DECLAREContigPutFunc(putRGBAAcontig16bittile)
  1210. {
  1211. int samplesperpixel = img->samplesperpixel;
  1212. uint16 *wp = (uint16 *)pp;
  1213. (void) y;
  1214. fromskew *= samplesperpixel;
  1215. while (h-- > 0) {
  1216. for (x = w; x-- > 0;) {
  1217. *cp++ = PACKW4(wp[0],wp[1],wp[2],wp[3]);
  1218. wp += samplesperpixel;
  1219. }
  1220. cp += toskew;
  1221. wp += fromskew;
  1222. }
  1223. }
  1224. /*
  1225. * 16-bit packed samples => RGBA w/ unassociated alpha
  1226. * (known to have Map == NULL)
  1227. */
  1228. DECLAREContigPutFunc(putRGBUAcontig16bittile)
  1229. {
  1230. int samplesperpixel = img->samplesperpixel;
  1231. uint16 *wp = (uint16 *)pp;
  1232. (void) y;
  1233. fromskew *= samplesperpixel;
  1234. while (h-- > 0) {
  1235. uint32 r,g,b,a;
  1236. for (x = w; x-- > 0;) {
  1237. a = W2B(wp[3]);
  1238. r = (a*W2B(wp[0]) + 127) / 255;
  1239. g = (a*W2B(wp[1]) + 127) / 255;
  1240. b = (a*W2B(wp[2]) + 127) / 255;
  1241. *cp++ = PACK4(r,g,b,a);
  1242. wp += samplesperpixel;
  1243. }
  1244. cp += toskew;
  1245. wp += fromskew;
  1246. }
  1247. }
  1248. /*
  1249. * 8-bit packed CMYK samples w/o Map => RGB
  1250. *
  1251. * NB: The conversion of CMYK->RGB is *very* crude.
  1252. */
  1253. DECLAREContigPutFunc(putRGBcontig8bitCMYKtile)
  1254. {
  1255. int samplesperpixel = img->samplesperpixel;
  1256. uint16 r, g, b, k;
  1257. (void) x; (void) y;
  1258. fromskew *= samplesperpixel;
  1259. while (h-- > 0) {
  1260. UNROLL8(w, NOP,
  1261. k = 255 - pp[3];
  1262. r = (k*(255-pp[0]))/255;
  1263. g = (k*(255-pp[1]))/255;
  1264. b = (k*(255-pp[2]))/255;
  1265. *cp++ = PACK(r, g, b);
  1266. pp += samplesperpixel);
  1267. cp += toskew;
  1268. pp += fromskew;
  1269. }
  1270. }
  1271. /*
  1272. * 8-bit packed CMYK samples w/Map => RGB
  1273. *
  1274. * NB: The conversion of CMYK->RGB is *very* crude.
  1275. */
  1276. DECLAREContigPutFunc(putRGBcontig8bitCMYKMaptile)
  1277. {
  1278. int samplesperpixel = img->samplesperpixel;
  1279. TIFFRGBValue* Map = img->Map;
  1280. uint16 r, g, b, k;
  1281. (void) y;
  1282. fromskew *= samplesperpixel;
  1283. while (h-- > 0) {
  1284. for (x = w; x-- > 0;) {
  1285. k = 255 - pp[3];
  1286. r = (k*(255-pp[0]))/255;
  1287. g = (k*(255-pp[1]))/255;
  1288. b = (k*(255-pp[2]))/255;
  1289. *cp++ = PACK(Map[r], Map[g], Map[b]);
  1290. pp += samplesperpixel;
  1291. }
  1292. pp += fromskew;
  1293. cp += toskew;
  1294. }
  1295. }
  1296. #define DECLARESepPutFunc(name) \
  1297. static void name(\
  1298. TIFFRGBAImage* img,\
  1299. uint32* cp,\
  1300. uint32 x, uint32 y, \
  1301. uint32 w, uint32 h,\
  1302. int32 fromskew, int32 toskew,\
  1303. unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* a\
  1304. )
  1305. /*
  1306. * 8-bit unpacked samples => RGB
  1307. */
  1308. DECLARESepPutFunc(putRGBseparate8bittile)
  1309. {
  1310. (void) img; (void) x; (void) y; (void) a;
  1311. while (h-- > 0) {
  1312. UNROLL8(w, NOP, *cp++ = PACK(*r++, *g++, *b++));
  1313. SKEW(r, g, b, fromskew);
  1314. cp += toskew;
  1315. }
  1316. }
  1317. /*
  1318. * 8-bit unpacked samples => RGBA w/ associated alpha
  1319. */
  1320. DECLARESepPutFunc(putRGBAAseparate8bittile)
  1321. {
  1322. (void) img; (void) x; (void) y;
  1323. while (h-- > 0) {
  1324. UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));
  1325. SKEW4(r, g, b, a, fromskew);
  1326. cp += toskew;
  1327. }
  1328. }
  1329. /*
  1330. * 8-bit unpacked samples => RGBA w/ unassociated alpha
  1331. */
  1332. DECLARESepPutFunc(putRGBUAseparate8bittile)
  1333. {
  1334. (void) img; (void) y;
  1335. while (h-- > 0) {
  1336. uint32 rv, gv, bv, av;
  1337. for (x = w; x-- > 0;) {
  1338. av = *a++;
  1339. rv = (av* *r++ + 127) / 255;
  1340. gv = (av* *g++ + 127) / 255;
  1341. bv = (av* *b++ + 127) / 255;
  1342. *cp++ = PACK4(rv,gv,bv,av);
  1343. }
  1344. SKEW4(r, g, b, a, fromskew);
  1345. cp += toskew;
  1346. }
  1347. }
  1348. /*
  1349. * 16-bit unpacked samples => RGB
  1350. */
  1351. DECLARESepPutFunc(putRGBseparate16bittile)
  1352. {
  1353. uint16 *wr = (uint16*) r;
  1354. uint16 *wg = (uint16*) g;
  1355. uint16 *wb = (uint16*) b;
  1356. (void) img; (void) y; (void) a;
  1357. while (h-- > 0) {
  1358. for (x = 0; x < w; x++)
  1359. *cp++ = PACKW(*wr++,*wg++,*wb++);
  1360. SKEW(wr, wg, wb, fromskew);
  1361. cp += toskew;
  1362. }
  1363. }
  1364. /*
  1365. * 16-bit unpacked samples => RGBA w/ associated alpha
  1366. */
  1367. DECLARESepPutFunc(putRGBAAseparate16bittile)
  1368. {
  1369. uint16 *wr = (uint16*) r;
  1370. uint16 *wg = (uint16*) g;
  1371. uint16 *wb = (uint16*) b;
  1372. uint16 *wa = (uint16*) a;
  1373. (void) img; (void) y;
  1374. while (h-- > 0) {
  1375. for (x = 0; x < w; x++)
  1376. *cp++ = PACKW4(*wr++,*wg++,*wb++,*wa++);
  1377. SKEW4(wr, wg, wb, wa, fromskew);
  1378. cp += toskew;
  1379. }
  1380. }
  1381. /*
  1382. * 16-bit unpacked samples => RGBA w/ unassociated alpha
  1383. */
  1384. DECLARESepPutFunc(putRGBUAseparate16bittile)
  1385. {
  1386. uint16 *wr = (uint16*) r;
  1387. uint16 *wg = (uint16*) g;
  1388. uint16 *wb = (uint16*) b;
  1389. uint16 *wa = (uint16*) a;
  1390. (void) img; (void) y;
  1391. while (h-- > 0) {
  1392. uint32 r,g,b,a;
  1393. for (x = w; x-- > 0;) {
  1394. a = W2B(*wa++);
  1395. r = (a*W2B(*wr++) + 127) / 255;
  1396. g = (a*W2B(*wg++) + 127) / 255;
  1397. b = (a*W2B(*wb++) + 127) / 255;
  1398. *cp++ = PACK4(r,g,b,a);
  1399. }
  1400. SKEW4(wr, wg, wb, wa, fromskew);
  1401. cp += toskew;
  1402. }
  1403. }
  1404. /*
  1405. * 8-bit packed CIE L*a*b 1976 samples => RGB
  1406. */
  1407. DECLAREContigPutFunc(putcontig8bitCIELab)
  1408. {
  1409. float X, Y, Z;
  1410. uint32 r, g, b;
  1411. (void) y;
  1412. fromskew *= 3;
  1413. while (h-- > 0) {
  1414. for (x = w; x-- > 0;) {
  1415. TIFFCIELabToXYZ(img->cielab,
  1416. (unsigned char)pp[0],
  1417. (signed char)pp[1],
  1418. (signed char)pp[2],
  1419. &X, &Y, &Z);
  1420. TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);
  1421. *cp++ = PACK(r, g, b);
  1422. pp += 3;
  1423. }
  1424. cp += toskew;
  1425. pp += fromskew;
  1426. }
  1427. }
  1428. /*
  1429. * YCbCr -> RGB conversion and packing routines.
  1430. */
  1431. #define YCbCrtoRGB(dst, Y) { \
  1432. uint32 r, g, b; \
  1433. TIFFYCbCrtoRGB(img->ycbcr, (Y), Cb, Cr, &r, &g, &b); \
  1434. dst = PACK(r, g, b); \
  1435. }
  1436. /*
  1437. * 8-bit packed YCbCr samples => RGB
  1438. * This function is generic for different sampling sizes,
  1439. * and can handle blocks sizes that aren't multiples of the
  1440. * sampling size. However, it is substantially less optimized
  1441. * than the specific sampling cases. It is used as a fallback
  1442. * for difficult blocks.
  1443. */
  1444. #ifdef notdef
  1445. static void putcontig8bitYCbCrGenericTile(
  1446. TIFFRGBAImage* img,
  1447. uint32* cp,
  1448. uint32 x, uint32 y,
  1449. uint32 w, uint32 h,
  1450. int32 fromskew, int32 toskew,
  1451. unsigned char* pp,
  1452. int h_group,
  1453. int v_group )
  1454. {
  1455. uint32* cp1 = cp+w+toskew;
  1456. uint32* cp2 = cp1+w+toskew;
  1457. uint32* cp3 = cp2+w+toskew;
  1458. int32 incr = 3*w+4*toskew;
  1459. int32 Cb, Cr;
  1460. int group_size = v_group * h_group + 2;
  1461. (void) y;
  1462. fromskew = (fromskew * group_size) / h_group;
  1463. for( yy = 0; yy < h; yy++ )
  1464. {
  1465. unsigned char *pp_line;
  1466. int y_line_group = yy / v_group;
  1467. int y_remainder = yy - y_line_group * v_group;
  1468. pp_line = pp + v_line_group *
  1469. for( xx = 0; xx < w; xx++ )
  1470. {
  1471. Cb = pp
  1472. }
  1473. }
  1474. for (; h >= 4; h -= 4) {
  1475. x = w>>2;
  1476. do {
  1477. Cb = pp[16];
  1478. Cr = pp[17];
  1479. YCbCrtoRGB(cp [0], pp[ 0]);
  1480. YCbCrtoRGB(cp [1], pp[ 1]);
  1481. YCbCrtoRGB(cp [2], pp[ 2]);
  1482. YCbCrtoRGB(cp [3], pp[ 3]);
  1483. YCbCrtoRGB(cp1[0], pp[ 4]);
  1484. YCbCrtoRGB(cp1[1], pp[ 5]);
  1485. YCbCrtoRGB(cp1[2], pp[ 6]);
  1486. YCbCrtoRGB(cp1[3], pp[ 7]);
  1487. YCbCrtoRGB(cp2[0], pp[ 8]);
  1488. YCbCrtoRGB(cp2[1], pp[ 9]);
  1489. YCbCrtoRGB(cp2[2], pp[10]);
  1490. YCbCrtoRGB(cp2[3], pp[11]);
  1491. YCbCrtoRGB(cp3[0], pp[12]);
  1492. YCbCrtoRGB(cp3[1], pp[13]);
  1493. YCbCrtoRGB(cp3[2], pp[14]);
  1494. YCbCrtoRGB(cp3[3], pp[15]);
  1495. cp += 4, cp1 += 4, cp2 += 4, cp3 += 4;
  1496. pp += 18;
  1497. } while (--x);
  1498. cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
  1499. pp += fromskew;
  1500. }
  1501. }
  1502. #endif
  1503. /*
  1504. * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB
  1505. */
  1506. DECLAREContigPutFunc(putcontig8bitYCbCr44tile)
  1507. {
  1508. uint32* cp1 = cp+w+toskew;
  1509. uint32* cp2 = cp1+w+toskew;
  1510. uint32* cp3 = cp2+w+toskew;
  1511. int32 incr = 3*w+4*toskew;
  1512. (void) y;
  1513. /* adjust fromskew */
  1514. fromskew = (fromskew * 18) / 4;
  1515. if ((h & 3) == 0 && (w & 3) == 0) {
  1516. for (; h >= 4; h -= 4) {
  1517. x = w>>2;
  1518. do {
  1519. int32 Cb = pp[16];
  1520. int32 Cr = pp[17];
  1521. YCbCrtoRGB(cp [0], pp[ 0]);
  1522. YCbCrtoRGB(cp [1], pp[ 1]);
  1523. YCbCrtoRGB(cp [2], pp[ 2]);
  1524. YCbCrtoRGB(cp [3], pp[ 3]);
  1525. YCbCrtoRGB(cp1[0], pp[ 4]);
  1526. YCbCrtoRGB(cp1[1], pp[ 5]);
  1527. YCbCrtoRGB(cp1[2], pp[ 6]);
  1528. YCbCrtoRGB(cp1[3], pp[ 7]);
  1529. YCbCrtoRGB(cp2[0], pp[ 8]);
  1530. YCbCrtoRGB(cp2[1], pp[ 9]);
  1531. YCbCrtoRGB(cp2[2], pp[10]);
  1532. YCbCrtoRGB(cp2[3], pp[11]);
  1533. YCbCrtoRGB(cp3[0], pp[12]);
  1534. YCbCrtoRGB(cp3[1], pp[13]);
  1535. YCbCrtoRGB(cp3[2], pp[14]);
  1536. YCbCrtoRGB(cp3[3], pp[15]);
  1537. cp += 4, cp1 += 4, cp2 += 4, cp3 += 4;
  1538. pp += 18;
  1539. } while (--x);
  1540. cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
  1541. pp += fromskew;
  1542. }
  1543. } else {
  1544. while (h > 0) {
  1545. for (x = w; x > 0;) {
  1546. int32 Cb = pp[16];
  1547. int32 Cr = pp[17];
  1548. switch (x) {
  1549. default:
  1550. switch (h) {
  1551. default: YCbCrtoRGB(cp3[3], pp[15]); /* FALLTHROUGH */
  1552. case 3: YCbCrtoRGB(cp2[3], pp[11]); /* FALLTHROUGH */
  1553. case 2: YCbCrtoRGB(cp1[3], pp[ 7]); /* FALLTHROUGH */
  1554. case 1: YCbCrtoRGB(cp [3], pp[ 3]); /* FALLTHROUGH */
  1555. } /* FALLTHROUGH */
  1556. case 3:
  1557. switch (h) {
  1558. default: YCbCrtoRGB(cp3[2], pp[14]); /* FALLTHROUGH */
  1559. case 3: YCbCrtoRGB(cp2[2], pp[10]); /* FALLTHROUGH */
  1560. case 2: YCbCrtoRGB(cp1[2], pp[ 6]); /* FALLTHROUGH */
  1561. case 1: YCbCrtoRGB(cp [2], pp[ 2]); /* FALLTHROUGH */
  1562. } /* FALLTHROUGH */
  1563. case 2:
  1564. switch (h) {
  1565. default: YCbCrtoRGB(cp3[1], pp[13]); /* FALLTHROUGH */
  1566. case 3: YCbCrtoRGB(cp2[1], pp[ 9]); /* FALLTHROUGH */
  1567. case 2: YCbCrtoRGB(cp1[1], pp[ 5]); /* FALLTHROUGH */
  1568. case 1: YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
  1569. } /* FALLTHROUGH */
  1570. case 1:
  1571. switch (h) {
  1572. default: YCbCrtoRGB(cp3[0], pp[12]); /* FALLTHROUGH */
  1573. case 3: YCbCrtoRGB(cp2[0], pp[ 8]); /* FALLTHROUGH */
  1574. case 2: YCbCrtoRGB(cp1[0], pp[ 4]); /* FALLTHROUGH */
  1575. case 1: YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
  1576. } /* FALLTHROUGH */
  1577. }
  1578. if (x < 4) {
  1579. cp += x; cp1 += x; cp2 += x; cp3 += x;
  1580. x = 0;
  1581. }
  1582. else {
  1583. cp += 4; cp1 += 4; cp2 += 4; cp3 += 4;
  1584. x -= 4;
  1585. }
  1586. pp += 18;
  1587. }
  1588. if (h <= 4)
  1589. break;
  1590. h -= 4;
  1591. cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
  1592. pp += fromskew;
  1593. }
  1594. }
  1595. }
  1596. /*
  1597. * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB
  1598. */
  1599. DECLAREContigPutFunc(putcontig8bitYCbCr42tile)
  1600. {
  1601. uint32* cp1 = cp+w+toskew;
  1602. int32 incr = 2*toskew+w;
  1603. (void) y;
  1604. fromskew = (fromskew * 10) / 4;
  1605. if ((h & 3) == 0 && (w & 1) == 0) {
  1606. for (; h >= 2; h -= 2) {
  1607. x = w>>2;
  1608. do {
  1609. int32 Cb = pp[8];
  1610. int32 Cr = pp[9];
  1611. YCbCrtoRGB(cp [0], pp[0]);
  1612. YCbCrtoRGB(cp [1], pp[1]);
  1613. YCbCrtoRGB(cp [2], pp[2]);
  1614. YCbCrtoRGB(cp [3], pp[3]);
  1615. YCbCrtoRGB(cp1[0], pp[4]);
  1616. YCbCrtoRGB(cp1[1], pp[5]);
  1617. YCbCrtoRGB(cp1[2], pp[6]);
  1618. YCbCrtoRGB(cp1[3], pp[7]);
  1619. cp += 4, cp1 += 4;
  1620. pp += 10;
  1621. } while (--x);
  1622. cp += incr, cp1 += incr;
  1623. pp += fromskew;
  1624. }
  1625. } else {
  1626. while (h > 0) {
  1627. for (x = w; x > 0;) {
  1628. int32 Cb = pp[8];
  1629. int32 Cr = pp[9];
  1630. switch (x) {
  1631. default:
  1632. switch (h) {
  1633. default: YCbCrtoRGB(cp1[3], pp[ 7]); /* FALLTHROUGH */
  1634. case 1: YCbCrtoRGB(cp [3], pp[ 3]); /* FALLTHROUGH */
  1635. } /* FALLTHROUGH */
  1636. case 3:
  1637. switch (h) {
  1638. default: YCbCrtoRGB(cp1[2], pp[ 6]); /* FALLTHROUGH */
  1639. case 1: YCbCrtoRGB(cp [2], pp[ 2]); /* FALLTHROUGH */
  1640. } /* FALLTHROUGH */
  1641. case 2:
  1642. switch (h) {
  1643. default: YCbCrtoRGB(cp1[1], pp[ 5]); /* FALLTHROUGH */
  1644. case 1: YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
  1645. } /* FALLTHROUGH */
  1646. case 1:
  1647. switch (h) {
  1648. default: YCbCrtoRGB(cp1[0], pp[ 4]); /* FALLTHROUGH */
  1649. case 1: YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
  1650. } /* FALLTHROUGH */
  1651. }
  1652. if (x < 4) {
  1653. cp += x; cp1 += x;
  1654. x = 0;
  1655. }
  1656. else {
  1657. cp += 4; cp1 += 4;
  1658. x -= 4;
  1659. }
  1660. pp += 10;
  1661. }
  1662. if (h <= 2)
  1663. break;
  1664. h -= 2;
  1665. cp += incr, cp1 += incr;
  1666. pp += fromskew;
  1667. }
  1668. }
  1669. }
  1670. /*
  1671. * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB
  1672. */
  1673. DECLAREContigPutFunc(putcontig8bitYCbCr41tile)
  1674. {
  1675. (void) y;
  1676. /* XXX adjust fromskew */
  1677. do {
  1678. x = w>>2;
  1679. do {
  1680. int32 Cb = pp[4];
  1681. int32 Cr = pp[5];
  1682. YCbCrtoRGB(cp [0], pp[0]);
  1683. YCbCrtoRGB(cp [1], pp[1]);
  1684. YCbCrtoRGB(cp [2], pp[2]);
  1685. YCbCrtoRGB(cp [3], pp[3]);
  1686. cp += 4;
  1687. pp += 6;
  1688. } while (--x);
  1689. if( (w&3) != 0 )
  1690. {
  1691. int32 Cb = pp[4];
  1692. int32 Cr = pp[5];
  1693. switch( (w&3) ) {
  1694. case 3: YCbCrtoRGB(cp [2], pp[2]);
  1695. case 2: YCbCrtoRGB(cp [1], pp[1]);
  1696. case 1: YCbCrtoRGB(cp [0], pp[0]);
  1697. case 0: break;
  1698. }
  1699. cp += (w&3);
  1700. pp += 6;
  1701. }
  1702. cp += toskew;
  1703. pp += fromskew;
  1704. } while (--h);
  1705. }
  1706. /*
  1707. * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB
  1708. */
  1709. DECLAREContigPutFunc(putcontig8bitYCbCr22tile)
  1710. {
  1711. uint32* cp2;
  1712. int32 incr = 2*toskew+w;
  1713. (void) y;
  1714. fromskew = (fromskew / 2) * 6;
  1715. cp2 = cp+w+toskew;
  1716. while (h>=2) {
  1717. x = w;
  1718. while (x>=2) {
  1719. uint32 Cb = pp[4];
  1720. uint32 Cr = pp[5];
  1721. YCbCrtoRGB(cp[0], pp[0]);
  1722. YCbCrtoRGB(cp[1], pp[1]);
  1723. YCbCrtoRGB(cp2[0], pp[2]);
  1724. YCbCrtoRGB(cp2[1], pp[3]);
  1725. cp += 2;
  1726. cp2 += 2;
  1727. pp += 6;
  1728. x -= 2;
  1729. }
  1730. if (x==1) {
  1731. uint32 Cb = pp[4];
  1732. uint32 Cr = pp[5];
  1733. YCbCrtoRGB(cp[0], pp[0]);
  1734. YCbCrtoRGB(cp2[0], pp[2]);
  1735. cp ++ ;
  1736. cp2 ++ ;
  1737. pp += 6;
  1738. }
  1739. cp += incr;
  1740. cp2 += incr;
  1741. pp += fromskew;
  1742. h-=2;
  1743. }
  1744. if (h==1) {
  1745. x = w;
  1746. while (x>=2) {
  1747. uint32 Cb = pp[4];
  1748. uint32 Cr = pp[5];
  1749. YCbCrtoRGB(cp[0], pp[0]);
  1750. YCbCrtoRGB(cp[1], pp[1]);
  1751. cp += 2;
  1752. cp2 += 2;
  1753. pp += 6;
  1754. x -= 2;
  1755. }
  1756. if (x==1) {
  1757. uint32 Cb = pp[4];
  1758. uint32 Cr = pp[5];
  1759. YCbCrtoRGB(cp[0], pp[0]);
  1760. }
  1761. }
  1762. }
  1763. /*
  1764. * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB
  1765. */
  1766. DECLAREContigPutFunc(putcontig8bitYCbCr21tile)
  1767. {
  1768. (void) y;
  1769. fromskew = (fromskew * 4) / 2;
  1770. do {
  1771. x = w>>1;
  1772. do {
  1773. int32 Cb = pp[2];
  1774. int32 Cr = pp[3];
  1775. YCbCrtoRGB(cp[0], pp[0]);
  1776. YCbCrtoRGB(cp[1], pp[1]);
  1777. cp += 2;
  1778. pp += 4;
  1779. } while (--x);
  1780. if( (w&1) != 0 )
  1781. {
  1782. int32 Cb = pp[2];
  1783. int32 Cr = pp[3];
  1784. YCbCrtoRGB(cp[0], pp[0]);
  1785. cp += 1;
  1786. pp += 4;
  1787. }
  1788. cp += toskew;
  1789. pp += fromskew;
  1790. } while (--h);
  1791. }
  1792. /*
  1793. * 8-bit packed YCbCr samples w/ 1,2 subsampling => RGB
  1794. */
  1795. DECLAREContigPutFunc(putcontig8bitYCbCr12tile)
  1796. {
  1797. uint32* cp2;
  1798. int32 incr = 2*toskew+w;
  1799. (void) y;
  1800. fromskew = (fromskew / 2) * 4;
  1801. cp2 = cp+w+toskew;
  1802. while (h>=2) {
  1803. x = w;
  1804. do {
  1805. uint32 Cb = pp[2];
  1806. uint32 Cr = pp[3];
  1807. YCbCrtoRGB(cp[0], pp[0]);
  1808. YCbCrtoRGB(cp2[0], pp[1]);
  1809. cp ++;
  1810. cp2 ++;
  1811. pp += 4;
  1812. } while (--x);
  1813. cp += incr;
  1814. cp2 += incr;
  1815. pp += fromskew;
  1816. h-=2;
  1817. }
  1818. if (h==1) {
  1819. x = w;
  1820. do {
  1821. uint32 Cb = pp[2];
  1822. uint32 Cr = pp[3];
  1823. YCbCrtoRGB(cp[0], pp[0]);
  1824. cp ++;
  1825. pp += 4;
  1826. } while (--x);
  1827. }
  1828. }
  1829. /*
  1830. * 8-bit packed YCbCr samples w/ no subsampling => RGB
  1831. */
  1832. DECLAREContigPutFunc(putcontig8bitYCbCr11tile)
  1833. {
  1834. (void) y;
  1835. fromskew *= 3;
  1836. do {
  1837. x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */
  1838. do {
  1839. int32 Cb = pp[1];
  1840. int32 Cr = pp[2];
  1841. YCbCrtoRGB(*cp++, pp[0]);
  1842. pp += 3;
  1843. } while (--x);
  1844. cp += toskew;
  1845. pp += fromskew;
  1846. } while (--h);
  1847. }
  1848. /*
  1849. * 8-bit packed YCbCr samples w/ no subsampling => RGB
  1850. */
  1851. DECLARESepPutFunc(putseparate8bitYCbCr11tile)
  1852. {
  1853. (void) y;
  1854. (void) a;
  1855. /* TODO: naming of input vars is still off, change obfuscating declaration inside define, or resolve obfuscation */
  1856. while (h-- > 0) {
  1857. x = w;
  1858. do {
  1859. uint32 dr, dg, db;
  1860. TIFFYCbCrtoRGB(img->ycbcr,*r++,*g++,*b++,&dr,&dg,&db);
  1861. *cp++ = PACK(dr,dg,db);
  1862. } while (--x);
  1863. SKEW(r, g, b, fromskew);
  1864. cp += toskew;
  1865. }
  1866. }
  1867. #undef YCbCrtoRGB
  1868. static int
  1869. initYCbCrConversion(TIFFRGBAImage* img)
  1870. {
  1871. static char module[] = "initYCbCrConversion";
  1872. float *luma, *refBlackWhite;
  1873. if (img->ycbcr == NULL) {
  1874. img->ycbcr = (TIFFYCbCrToRGB*) _TIFFmalloc(
  1875. TIFFroundup(sizeof (TIFFYCbCrToRGB), sizeof (long))
  1876. + 4*256*sizeof (TIFFRGBValue)
  1877. + 2*256*sizeof (int)
  1878. + 3*256*sizeof (int32)
  1879. );
  1880. if (img->ycbcr == NULL) {
  1881. TIFFErrorExt(img->tif->tif_clientdata, module,
  1882. "No space for YCbCr->RGB conversion state");
  1883. return (0);
  1884. }
  1885. }
  1886. TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma);
  1887. TIFFGetFieldDefaulted(img->tif, TIFFTAG_REFERENCEBLACKWHITE,
  1888. &refBlackWhite);
  1889. if (TIFFYCbCrToRGBInit(img->ycbcr, luma, refBlackWhite) < 0)
  1890. return(0);
  1891. return (1);
  1892. }
  1893. static tileContigRoutine
  1894. initCIELabConversion(TIFFRGBAImage* img)
  1895. {
  1896. static char module[] = "initCIELabConversion";
  1897. float *whitePoint;
  1898. float refWhite[3];
  1899. if (!img->cielab) {
  1900. img->cielab = (TIFFCIELabToRGB *)
  1901. _TIFFmalloc(sizeof(TIFFCIELabToRGB));
  1902. if (!img->cielab) {
  1903. TIFFErrorExt(img->tif->tif_clientdata, module,
  1904. "No space for CIE L*a*b*->RGB conversion state.");
  1905. return NULL;
  1906. }
  1907. }
  1908. TIFFGetFieldDefaulted(img->tif, TIFFTAG_WHITEPOINT, &whitePoint);
  1909. refWhite[1] = 100.0F;
  1910. refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1];
  1911. refWhite[2] = (1.0F - whitePoint[0] - whitePoint[1])
  1912. / whitePoint[1] * refWhite[1];
  1913. if (TIFFCIELabToRGBInit(img->cielab, &display_sRGB, refWhite) < 0) {
  1914. TIFFErrorExt(img->tif->tif_clientdata, module,
  1915. "Failed to initialize CIE L*a*b*->RGB conversion state.");
  1916. _TIFFfree(img->cielab);
  1917. return NULL;
  1918. }
  1919. return putcontig8bitCIELab;
  1920. }
  1921. /*
  1922. * Greyscale images with less than 8 bits/sample are handled
  1923. * with a table to avoid lots of shifts and masks. The table
  1924. * is setup so that put*bwtile (below) can retrieve 8/bitspersample
  1925. * pixel values simply by indexing into the table with one
  1926. * number.
  1927. */
  1928. static int
  1929. makebwmap(TIFFRGBAImage* img)
  1930. {
  1931. TIFFRGBValue* Map = img->Map;
  1932. int bitspersample = img->bitspersample;
  1933. int nsamples = 8 / bitspersample;
  1934. int i;
  1935. uint32* p;
  1936. if( nsamples == 0 )
  1937. nsamples = 1;
  1938. img->BWmap = (uint32**) _TIFFmalloc(
  1939. 256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
  1940. if (img->BWmap == NULL) {
  1941. TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for B&W mapping table");
  1942. return (0);
  1943. }
  1944. p = (uint32*)(img->BWmap + 256);
  1945. for (i = 0; i < 256; i++) {
  1946. TIFFRGBValue c;
  1947. img->BWmap[i] = p;
  1948. switch (bitspersample) {
  1949. #define GREY(x) c = Map[x]; *p++ = PACK(c,c,c);
  1950. case 1:
  1951. GREY(i>>7);
  1952. GREY((i>>6)&1);
  1953. GREY((i>>5)&1);
  1954. GREY((i>>4)&1);
  1955. GREY((i>>3)&1);
  1956. GREY((i>>2)&1);
  1957. GREY((i>>1)&1);
  1958. GREY(i&1);
  1959. break;
  1960. case 2:
  1961. GREY(i>>6);
  1962. GREY((i>>4)&3);
  1963. GREY((i>>2)&3);
  1964. GREY(i&3);
  1965. break;
  1966. case 4:
  1967. GREY(i>>4);
  1968. GREY(i&0xf);
  1969. break;
  1970. case 8:
  1971. case 16:
  1972. GREY(i);
  1973. break;
  1974. }
  1975. #undef GREY
  1976. }
  1977. return (1);
  1978. }
  1979. /*
  1980. * Construct a mapping table to convert from the range
  1981. * of the data samples to [0,255] --for display. This
  1982. * process also handles inverting B&W images when needed.
  1983. */
  1984. static int
  1985. setupMap(TIFFRGBAImage* img)
  1986. {
  1987. int32 x, range;
  1988. range = (int32)((1L<<img->bitspersample)-1);
  1989. /* treat 16 bit the same as eight bit */
  1990. if( img->bitspersample == 16 )
  1991. range = (int32) 255;
  1992. img->Map = (TIFFRGBValue*) _TIFFmalloc((range+1) * sizeof (TIFFRGBValue));
  1993. if (img->Map == NULL) {
  1994. TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
  1995. "No space for photometric conversion table");
  1996. return (0);
  1997. }
  1998. if (img->photometric == PHOTOMETRIC_MINISWHITE) {
  1999. for (x = 0; x <= range; x++)
  2000. img->Map[x] = (TIFFRGBValue) (((range - x) * 255) / range);
  2001. } else {
  2002. for (x = 0; x <= range; x++)
  2003. img->Map[x] = (TIFFRGBValue) ((x * 255) / range);
  2004. }
  2005. if (img->bitspersample <= 16 &&
  2006. (img->photometric == PHOTOMETRIC_MINISBLACK ||
  2007. img->photometric == PHOTOMETRIC_MINISWHITE)) {
  2008. /*
  2009. * Use photometric mapping table to construct
  2010. * unpacking tables for samples <= 8 bits.
  2011. */
  2012. if (!makebwmap(img))
  2013. return (0);
  2014. /* no longer need Map, free it */
  2015. _TIFFfree(img->Map), img->Map = NULL;
  2016. }
  2017. return (1);
  2018. }
  2019. static int
  2020. checkcmap(TIFFRGBAImage* img)
  2021. {
  2022. uint16* r = img->redcmap;
  2023. uint16* g = img->greencmap;
  2024. uint16* b = img->bluecmap;
  2025. long n = 1L<<img->bitspersample;
  2026. while (n-- > 0)
  2027. if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
  2028. return (16);
  2029. return (8);
  2030. }
  2031. static void
  2032. cvtcmap(TIFFRGBAImage* img)
  2033. {
  2034. uint16* r = img->redcmap;
  2035. uint16* g = img->greencmap;
  2036. uint16* b = img->bluecmap;
  2037. long i;
  2038. for (i = (1L<<img->bitspersample)-1; i >= 0; i--) {
  2039. #define CVT(x) ((uint16)((x)>>8))
  2040. r[i] = CVT(r[i]);
  2041. g[i] = CVT(g[i]);
  2042. b[i] = CVT(b[i]);
  2043. #undef CVT
  2044. }
  2045. }
  2046. /*
  2047. * Palette images with <= 8 bits/sample are handled
  2048. * with a table to avoid lots of shifts and masks. The table
  2049. * is setup so that put*cmaptile (below) can retrieve 8/bitspersample
  2050. * pixel values simply by indexing into the table with one
  2051. * number.
  2052. */
  2053. static int
  2054. makecmap(TIFFRGBAImage* img)
  2055. {
  2056. int bitspersample = img->bitspersample;
  2057. int nsamples = 8 / bitspersample;
  2058. uint16* r = img->redcmap;
  2059. uint16* g = img->greencmap;
  2060. uint16* b = img->bluecmap;
  2061. uint32 *p;
  2062. int i;
  2063. img->PALmap = (uint32**) _TIFFmalloc(
  2064. 256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
  2065. if (img->PALmap == NULL) {
  2066. TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for Palette mapping table");
  2067. return (0);
  2068. }
  2069. p = (uint32*)(img->PALmap + 256);
  2070. for (i = 0; i < 256; i++) {
  2071. TIFFRGBValue c;
  2072. img->PALmap[i] = p;
  2073. #define CMAP(x) c = (TIFFRGBValue) x; *p++ = PACK(r[c]&0xff, g[c]&0xff, b[c]&0xff);
  2074. switch (bitspersample) {
  2075. case 1:
  2076. CMAP(i>>7);
  2077. CMAP((i>>6)&1);
  2078. CMAP((i>>5)&1);
  2079. CMAP((i>>4)&1);
  2080. CMAP((i>>3)&1);
  2081. CMAP((i>>2)&1);
  2082. CMAP((i>>1)&1);
  2083. CMAP(i&1);
  2084. break;
  2085. case 2:
  2086. CMAP(i>>6);
  2087. CMAP((i>>4)&3);
  2088. CMAP((i>>2)&3);
  2089. CMAP(i&3);
  2090. break;
  2091. case 4:
  2092. CMAP(i>>4);
  2093. CMAP(i&0xf);
  2094. break;
  2095. case 8:
  2096. CMAP(i);
  2097. break;
  2098. }
  2099. #undef CMAP
  2100. }
  2101. return (1);
  2102. }
  2103. /*
  2104. * Construct any mapping table used
  2105. * by the associated put routine.
  2106. */
  2107. static int
  2108. buildMap(TIFFRGBAImage* img)
  2109. {
  2110. switch (img->photometric) {
  2111. case PHOTOMETRIC_RGB:
  2112. case PHOTOMETRIC_YCBCR:
  2113. case PHOTOMETRIC_SEPARATED:
  2114. if (img->bitspersample == 8)
  2115. break;
  2116. /* fall thru... */
  2117. case PHOTOMETRIC_MINISBLACK:
  2118. case PHOTOMETRIC_MINISWHITE:
  2119. if (!setupMap(img))
  2120. return (0);
  2121. break;
  2122. case PHOTOMETRIC_PALETTE:
  2123. /*
  2124. * Convert 16-bit colormap to 8-bit (unless it looks
  2125. * like an old-style 8-bit colormap).
  2126. */
  2127. if (checkcmap(img) == 16)
  2128. cvtcmap(img);
  2129. else
  2130. TIFFWarningExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "Assuming 8-bit colormap");
  2131. /*
  2132. * Use mapping table and colormap to construct
  2133. * unpacking tables for samples < 8 bits.
  2134. */
  2135. if (img->bitspersample <= 8 && !makecmap(img))
  2136. return (0);
  2137. break;
  2138. }
  2139. return (1);
  2140. }
  2141. /*
  2142. * Select the appropriate conversion routine for packed data.
  2143. */
  2144. static int
  2145. PickContigCase(TIFFRGBAImage* img)
  2146. {
  2147. img->get = TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig;
  2148. img->put.contig = NULL;
  2149. switch (img->photometric) {
  2150. case PHOTOMETRIC_RGB:
  2151. switch (img->bitspersample) {
  2152. case 8:
  2153. if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
  2154. img->put.contig = putRGBAAcontig8bittile;
  2155. else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
  2156. {
  2157. img->put.contig = putRGBUAcontig8bittile;
  2158. }
  2159. else
  2160. img->put.contig = putRGBcontig8bittile;
  2161. break;
  2162. case 16:
  2163. if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
  2164. {
  2165. img->put.contig = putRGBAAcontig16bittile;
  2166. }
  2167. else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
  2168. {
  2169. img->put.contig = putRGBUAcontig16bittile;
  2170. }
  2171. else
  2172. {
  2173. img->put.contig = putRGBcontig16bittile;
  2174. }
  2175. break;
  2176. }
  2177. break;
  2178. case PHOTOMETRIC_SEPARATED:
  2179. if (buildMap(img)) {
  2180. if (img->bitspersample == 8) {
  2181. if (!img->Map)
  2182. img->put.contig = putRGBcontig8bitCMYKtile;
  2183. else
  2184. img->put.contig = putRGBcontig8bitCMYKMaptile;
  2185. }
  2186. }
  2187. break;
  2188. case PHOTOMETRIC_PALETTE:
  2189. if (buildMap(img)) {
  2190. switch (img->bitspersample) {
  2191. case 8:
  2192. img->put.contig = put8bitcmaptile;
  2193. break;
  2194. case 4:
  2195. img->put.contig = put4bitcmaptile;
  2196. break;
  2197. case 2:
  2198. img->put.contig = put2bitcmaptile;
  2199. break;
  2200. case 1:
  2201. img->put.contig = put1bitcmaptile;
  2202. break;
  2203. }
  2204. }
  2205. break;
  2206. case PHOTOMETRIC_MINISWHITE:
  2207. case PHOTOMETRIC_MINISBLACK:
  2208. if (buildMap(img)) {
  2209. switch (img->bitspersample) {
  2210. case 16:
  2211. img->put.contig = put16bitbwtile;
  2212. break;
  2213. case 8:
  2214. img->put.contig = putgreytile;
  2215. break;
  2216. case 4:
  2217. img->put.contig = put4bitbwtile;
  2218. break;
  2219. case 2:
  2220. img->put.contig = put2bitbwtile;
  2221. break;
  2222. case 1:
  2223. img->put.contig = put1bitbwtile;
  2224. break;
  2225. }
  2226. }
  2227. break;
  2228. case PHOTOMETRIC_YCBCR:
  2229. if ((img->bitspersample==8) && (img->samplesperpixel==3))
  2230. {
  2231. if (initYCbCrConversion(img)!=0)
  2232. {
  2233. /*
  2234. * The 6.0 spec says that subsampling must be
  2235. * one of 1, 2, or 4, and that vertical subsampling
  2236. * must always be <= horizontal subsampling; so
  2237. * there are only a few possibilities and we just
  2238. * enumerate the cases.
  2239. * Joris: added support for the [1,2] case, nonetheless, to accomodate
  2240. * some OJPEG files
  2241. */
  2242. uint16 SubsamplingHor;
  2243. uint16 SubsamplingVer;
  2244. TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &SubsamplingHor, &SubsamplingVer);
  2245. switch ((SubsamplingHor<<4)|SubsamplingVer) {
  2246. case 0x44:
  2247. img->put.contig = putcontig8bitYCbCr44tile;
  2248. break;
  2249. case 0x42:
  2250. img->put.contig = putcontig8bitYCbCr42tile;
  2251. break;
  2252. case 0x41:
  2253. img->put.contig = putcontig8bitYCbCr41tile;
  2254. break;
  2255. case 0x22:
  2256. img->put.contig = putcontig8bitYCbCr22tile;
  2257. break;
  2258. case 0x21:
  2259. img->put.contig = putcontig8bitYCbCr21tile;
  2260. break;
  2261. case 0x12:
  2262. img->put.contig = putcontig8bitYCbCr12tile;
  2263. break;
  2264. case 0x11:
  2265. img->put.contig = putcontig8bitYCbCr11tile;
  2266. break;
  2267. }
  2268. }
  2269. }
  2270. break;
  2271. case PHOTOMETRIC_CIELAB:
  2272. if (buildMap(img)) {
  2273. if (img->bitspersample == 8)
  2274. img->put.contig = initCIELabConversion(img);
  2275. break;
  2276. }
  2277. }
  2278. return ((img->get!=NULL) && (img->put.contig!=NULL));
  2279. }
  2280. /*
  2281. * Select the appropriate conversion routine for unpacked data.
  2282. *
  2283. * NB: we assume that unpacked single channel data is directed
  2284. * to the "packed routines.
  2285. */
  2286. static int
  2287. PickSeparateCase(TIFFRGBAImage* img)
  2288. {
  2289. img->get = TIFFIsTiled(img->tif) ? gtTileSeparate : gtStripSeparate;
  2290. img->put.separate = NULL;
  2291. switch (img->photometric) {
  2292. case PHOTOMETRIC_RGB:
  2293. switch (img->bitspersample) {
  2294. case 8:
  2295. if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
  2296. img->put.separate = putRGBAAseparate8bittile;
  2297. else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
  2298. {
  2299. img->put.separate = putRGBUAseparate8bittile;
  2300. }
  2301. else
  2302. img->put.separate = putRGBseparate8bittile;
  2303. break;
  2304. case 16:
  2305. if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
  2306. {
  2307. img->put.separate = putRGBAAseparate16bittile;
  2308. }
  2309. else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
  2310. {
  2311. img->put.separate = putRGBUAseparate16bittile;
  2312. }
  2313. else
  2314. {
  2315. img->put.separate = putRGBseparate16bittile;
  2316. }
  2317. break;
  2318. }
  2319. break;
  2320. case PHOTOMETRIC_YCBCR:
  2321. if ((img->bitspersample==8) && (img->samplesperpixel==3))
  2322. {
  2323. if (initYCbCrConversion(img)!=0)
  2324. {
  2325. uint16 hs, vs;
  2326. TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &hs, &vs);
  2327. switch ((hs<<4)|vs) {
  2328. case 0x11:
  2329. img->put.separate = putseparate8bitYCbCr11tile;
  2330. break;
  2331. /* TODO: add other cases here */
  2332. }
  2333. }
  2334. }
  2335. break;
  2336. }
  2337. return ((img->get!=NULL) && (img->put.separate!=NULL));
  2338. }
  2339. /*
  2340. * Read a whole strip off data from the file, and convert to RGBA form.
  2341. * If this is the last strip, then it will only contain the portion of
  2342. * the strip that is actually within the image space. The result is
  2343. * organized in bottom to top form.
  2344. */
  2345. int
  2346. TIFFReadRGBAStrip(TIFF* tif, uint32 row, uint32 * raster )
  2347. {
  2348. char emsg[1024] = "";
  2349. TIFFRGBAImage img;
  2350. int ok;
  2351. uint32 rowsperstrip, rows_to_read;
  2352. if( TIFFIsTiled( tif ) )
  2353. {
  2354. TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
  2355. "Can't use TIFFReadRGBAStrip() with tiled file.");
  2356. return (0);
  2357. }
  2358. TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
  2359. if( (row % rowsperstrip) != 0 )
  2360. {
  2361. TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
  2362. "Row passed to TIFFReadRGBAStrip() must be first in a strip.");
  2363. return (0);
  2364. }
  2365. if (TIFFRGBAImageOK(tif, emsg) && TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
  2366. img.row_offset = row;
  2367. img.col_offset = 0;
  2368. if( row + rowsperstrip > img.height )
  2369. rows_to_read = img.height - row;
  2370. else
  2371. rows_to_read