tif_imageiter.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /* $Header: /cvs/maptools/cvsroot/libtiff/contrib/pds/tif_imageiter.c,v 1.4 2010-06-08 18:55:15 bfriesen Exp $ */
  2. /*
  3. * Copyright (c) 1991-1996 Sam Leffler
  4. * Copyright (c) 1991-1996 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. * Written by Conrad J. Poelman, PL/WSAT, Kirtland AFB, NM on 26 Mar 96.
  29. *
  30. * This file contains code to allow a calling program to "iterate" over each
  31. * pixels in an image as it is read from the file. The iterator takes care of
  32. * reading strips versus (possibly clipped) tiles, decoding the information
  33. * according to the decoding method, and so on, so that calling program can
  34. * ignore those details. The calling program does, however, need to be
  35. * conscious of the type of the pixel data that it is receiving.
  36. *
  37. * For reasons of efficiency, the callback function actually gets called for
  38. * "blocks" of pixels rather than for individual pixels. The format of the
  39. * callback arguments is given below.
  40. *
  41. * This code was taken from TIFFReadRGBAImage() in tif_getimage.c of the original
  42. * TIFF distribution, and simplified and generalized to provide this general
  43. * iteration capability. Those routines could certainly be re-implemented in terms
  44. * of a TIFFImageIter if desired.
  45. *
  46. */
  47. #include "tiffiop.h"
  48. #include "tif_imageiter.h"
  49. #include <assert.h>
  50. #include <stdio.h>
  51. static int gtTileContig(TIFFImageIter*, void *udata, uint32, uint32);
  52. static int gtTileSeparate(TIFFImageIter*, void *udata, uint32, uint32);
  53. static int gtStripContig(TIFFImageIter*, void *udata, uint32, uint32);
  54. static int gtStripSeparate(TIFFImageIter*, void *udata, uint32, uint32);
  55. static const char photoTag[] = "PhotometricInterpretation";
  56. static int
  57. isCCITTCompression(TIFF* tif)
  58. {
  59. uint16 compress;
  60. TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
  61. return (compress == COMPRESSION_CCITTFAX3 ||
  62. compress == COMPRESSION_CCITTFAX4 ||
  63. compress == COMPRESSION_CCITTRLE ||
  64. compress == COMPRESSION_CCITTRLEW);
  65. }
  66. int
  67. TIFFImageIterBegin(TIFFImageIter* img, TIFF* tif, int stop, char emsg[1024])
  68. {
  69. uint16* sampleinfo;
  70. uint16 extrasamples;
  71. uint16 planarconfig;
  72. int colorchannels;
  73. img->tif = tif;
  74. img->stoponerr = stop;
  75. TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
  76. img->alpha = 0;
  77. TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
  78. TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
  79. &extrasamples, &sampleinfo);
  80. if (extrasamples == 1)
  81. switch (sampleinfo[0]) {
  82. case EXTRASAMPLE_ASSOCALPHA: /* data is pre-multiplied */
  83. case EXTRASAMPLE_UNASSALPHA: /* data is not pre-multiplied */
  84. img->alpha = sampleinfo[0];
  85. break;
  86. }
  87. colorchannels = img->samplesperpixel - extrasamples;
  88. TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
  89. if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric)) {
  90. switch (colorchannels) {
  91. case 1:
  92. if (isCCITTCompression(tif))
  93. img->photometric = PHOTOMETRIC_MINISWHITE;
  94. else
  95. img->photometric = PHOTOMETRIC_MINISBLACK;
  96. break;
  97. case 3:
  98. img->photometric = PHOTOMETRIC_RGB;
  99. break;
  100. default:
  101. sprintf(emsg, "Missing needed %s tag", photoTag);
  102. return (0);
  103. }
  104. }
  105. switch (img->photometric) {
  106. case PHOTOMETRIC_PALETTE:
  107. if (!TIFFGetField(tif, TIFFTAG_COLORMAP,
  108. &img->redcmap, &img->greencmap, &img->bluecmap)) {
  109. TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "Missing required \"Colormap\" tag");
  110. return (0);
  111. }
  112. /* fall thru... */
  113. case PHOTOMETRIC_MINISWHITE:
  114. case PHOTOMETRIC_MINISBLACK:
  115. /* This should work now so skip the check - BSR
  116. if (planarconfig == PLANARCONFIG_CONTIG && img->samplesperpixel != 1) {
  117. sprintf(emsg,
  118. "Sorry, can not handle contiguous data with %s=%d, and %s=%d",
  119. photoTag, img->photometric,
  120. "Samples/pixel", img->samplesperpixel);
  121. return (0);
  122. }
  123. */
  124. break;
  125. case PHOTOMETRIC_YCBCR:
  126. if (planarconfig != PLANARCONFIG_CONTIG) {
  127. sprintf(emsg, "Sorry, can not handle YCbCr images with %s=%d",
  128. "Planarconfiguration", planarconfig);
  129. return (0);
  130. }
  131. /* It would probably be nice to have a reality check here. */
  132. { uint16 compress;
  133. TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
  134. if (compress == COMPRESSION_JPEG && planarconfig == PLANARCONFIG_CONTIG) {
  135. /* can rely on libjpeg to convert to RGB */
  136. /* XXX should restore current state on exit */
  137. TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
  138. img->photometric = PHOTOMETRIC_RGB;
  139. }
  140. }
  141. break;
  142. case PHOTOMETRIC_RGB:
  143. if (colorchannels < 3) {
  144. sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
  145. "Color channels", colorchannels);
  146. return (0);
  147. }
  148. break;
  149. case PHOTOMETRIC_SEPARATED: {
  150. uint16 inkset;
  151. TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
  152. if (inkset != INKSET_CMYK) {
  153. sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
  154. "InkSet", inkset);
  155. return (0);
  156. }
  157. if (img->samplesperpixel != 4) {
  158. sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
  159. "Samples/pixel", img->samplesperpixel);
  160. return (0);
  161. }
  162. break;
  163. }
  164. default:
  165. sprintf(emsg, "Sorry, can not handle image with %s=%d",
  166. photoTag, img->photometric);
  167. return (0);
  168. }
  169. TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
  170. TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
  171. TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
  172. switch (img->orientation) {
  173. case ORIENTATION_BOTRIGHT:
  174. case ORIENTATION_RIGHTBOT: /* XXX */
  175. case ORIENTATION_LEFTBOT: /* XXX */
  176. TIFFWarning(TIFFFileName(tif), "using bottom-left orientation");
  177. img->orientation = ORIENTATION_BOTLEFT;
  178. /* fall thru... */
  179. case ORIENTATION_BOTLEFT:
  180. break;
  181. case ORIENTATION_TOPRIGHT:
  182. case ORIENTATION_RIGHTTOP: /* XXX */
  183. case ORIENTATION_LEFTTOP: /* XXX */
  184. default:
  185. TIFFWarning(TIFFFileName(tif), "using top-left orientation");
  186. img->orientation = ORIENTATION_TOPLEFT;
  187. /* fall thru... */
  188. case ORIENTATION_TOPLEFT:
  189. break;
  190. }
  191. img->isContig =
  192. !(planarconfig == PLANARCONFIG_SEPARATE && colorchannels > 1);
  193. if (img->isContig) {
  194. img->get = TIFFIsTiled(tif) ? gtTileContig : gtStripContig;
  195. } else {
  196. img->get = TIFFIsTiled(tif) ? gtTileSeparate : gtStripSeparate;
  197. }
  198. return (1);
  199. }
  200. int
  201. TIFFImageIterGet(TIFFImageIter* img, void *udata, uint32 w, uint32 h)
  202. {
  203. if (img->get == NULL) {
  204. TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No \"get\" routine setup");
  205. return (0);
  206. }
  207. if (img->callback.any == NULL) {
  208. TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
  209. "No \"put\" routine setupl; probably can not handle image format");
  210. return (0);
  211. }
  212. return (*img->get)(img, udata, w, h);
  213. }
  214. TIFFImageIterEnd(TIFFImageIter* img)
  215. {
  216. /* Nothing to free... ? */
  217. }
  218. /*
  219. * Read the specified image into an ABGR-format raster.
  220. */
  221. int
  222. TIFFReadImageIter(TIFF* tif,
  223. uint32 rwidth, uint32 rheight, uint8* raster, int stop)
  224. {
  225. char emsg[1024];
  226. TIFFImageIter img;
  227. int ok;
  228. if (TIFFImageIterBegin(&img, tif, stop, emsg)) {
  229. /* XXX verify rwidth and rheight against width and height */
  230. ok = TIFFImageIterGet(&img, raster, rwidth, img.height);
  231. TIFFImageIterEnd(&img);
  232. } else {
  233. TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), emsg);
  234. ok = 0;
  235. }
  236. return (ok);
  237. }
  238. /*
  239. * Get an tile-organized image that has
  240. * PlanarConfiguration contiguous if SamplesPerPixel > 1
  241. * or
  242. * SamplesPerPixel == 1
  243. */
  244. static int
  245. gtTileContig(TIFFImageIter* img, void *udata, uint32 w, uint32 h)
  246. {
  247. TIFF* tif = img->tif;
  248. ImageIterTileContigRoutine callback = img->callback.contig;
  249. uint16 orientation;
  250. uint32 col, row;
  251. uint32 tw, th;
  252. u_char* buf;
  253. int32 fromskew;
  254. uint32 nrow;
  255. buf = (u_char*) _TIFFmalloc(TIFFTileSize(tif));
  256. if (buf == 0) {
  257. TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
  258. return (0);
  259. }
  260. TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
  261. TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
  262. orientation = img->orientation;
  263. for (row = 0; row < h; row += th) {
  264. nrow = (row + th > h ? h - row : th);
  265. for (col = 0; col < w; col += tw) {
  266. if (TIFFReadTile(tif, buf, col, row, 0, 0) < 0 && img->stoponerr)
  267. break;
  268. if (col + tw > w) {
  269. /*
  270. * Tile is clipped horizontally. Calculate
  271. * visible portion and skewing factors.
  272. */
  273. uint32 npix = w - col;
  274. fromskew = tw - npix;
  275. (*callback)(img, udata, col, row, npix, nrow, fromskew, buf);
  276. } else {
  277. (*callback)(img, udata, col, row, tw, nrow, 0, buf);
  278. }
  279. }
  280. }
  281. _TIFFfree(buf);
  282. return (1);
  283. }
  284. /*
  285. * Get an tile-organized image that has
  286. * SamplesPerPixel > 1
  287. * PlanarConfiguration separated
  288. * We assume that all such images are RGB.
  289. */
  290. static int
  291. gtTileSeparate(TIFFImageIter* img, void *udata, uint32 w, uint32 h)
  292. {
  293. TIFF* tif = img->tif;
  294. ImageIterTileSeparateRoutine callback = img->callback.separate;
  295. uint16 orientation;
  296. uint32 col, row;
  297. uint32 tw, th;
  298. u_char* buf;
  299. u_char* r;
  300. u_char* g;
  301. u_char* b;
  302. u_char* a;
  303. tsize_t tilesize;
  304. int32 fromskew;
  305. int alpha = img->alpha;
  306. uint32 nrow;
  307. tilesize = TIFFTileSize(tif);
  308. buf = (u_char*) _TIFFmalloc(4*tilesize);
  309. if (buf == 0) {
  310. TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
  311. return (0);
  312. }
  313. r = buf;
  314. g = r + tilesize;
  315. b = g + tilesize;
  316. a = b + tilesize;
  317. if (!alpha)
  318. memset(a, 0xff, tilesize);
  319. TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
  320. TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
  321. orientation = img->orientation;
  322. for (row = 0; row < h; row += th) {
  323. nrow = (row + th > h ? h - row : th);
  324. for (col = 0; col < w; col += tw) {
  325. if (TIFFReadTile(tif, r, col, row,0,0) < 0 && img->stoponerr)
  326. break;
  327. if (TIFFReadTile(tif, g, col, row,0,1) < 0 && img->stoponerr)
  328. break;
  329. if (TIFFReadTile(tif, b, col, row,0,2) < 0 && img->stoponerr)
  330. break;
  331. if (alpha && TIFFReadTile(tif,a,col,row,0,3) < 0 && img->stoponerr)
  332. break;
  333. if (col + tw > w) {
  334. /*
  335. * Tile is clipped horizontally. Calculate
  336. * visible portion and skewing factors.
  337. */
  338. uint32 npix = w - col;
  339. fromskew = tw - npix;
  340. (*callback)(img, udata, col, row, npix, nrow, fromskew, r, g, b, a);
  341. } else {
  342. (*callback)(img, udata, col, row, tw, nrow, 0, r, g, b, a);
  343. }
  344. }
  345. }
  346. _TIFFfree(buf);
  347. return (1);
  348. }
  349. /*
  350. * Get a strip-organized image that has
  351. * PlanarConfiguration contiguous if SamplesPerPixel > 1
  352. * or
  353. * SamplesPerPixel == 1
  354. */
  355. static int
  356. gtStripContig(TIFFImageIter* img, void *udata, uint32 w, uint32 h)
  357. {
  358. TIFF* tif = img->tif;
  359. ImageIterTileContigRoutine callback = img->callback.contig;
  360. uint16 orientation;
  361. uint32 row, nrow;
  362. u_char* buf;
  363. uint32 rowsperstrip;
  364. uint32 imagewidth = img->width;
  365. tsize_t scanline;
  366. int32 fromskew;
  367. buf = (u_char*) _TIFFmalloc(TIFFStripSize(tif));
  368. if (buf == 0) {
  369. TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for strip buffer");
  370. return (0);
  371. }
  372. orientation = img->orientation;
  373. TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
  374. scanline = TIFFScanlineSize(tif);
  375. fromskew = (w < imagewidth ? imagewidth - w : 0);
  376. for (row = 0; row < h; row += rowsperstrip) {
  377. nrow = (row + rowsperstrip > h ? h - row : rowsperstrip);
  378. if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 0),
  379. buf, nrow*scanline) < 0 && img->stoponerr)
  380. break;
  381. (*callback)(img, udata, 0, row, w, nrow, fromskew, buf);
  382. }
  383. _TIFFfree(buf);
  384. return (1);
  385. }
  386. /*
  387. * Get a strip-organized image with
  388. * SamplesPerPixel > 1
  389. * PlanarConfiguration separated
  390. * We assume that all such images are RGB.
  391. */
  392. static int
  393. gtStripSeparate(TIFFImageIter* img, void *udata, uint32 w, uint32 h)
  394. {
  395. TIFF* tif = img->tif;
  396. ImageIterTileSeparateRoutine callback = img->callback.separate;
  397. uint16 orientation;
  398. u_char *buf;
  399. u_char *r, *g, *b, *a;
  400. uint32 row, nrow;
  401. tsize_t scanline;
  402. uint32 rowsperstrip;
  403. uint32 imagewidth = img->width;
  404. tsize_t stripsize;
  405. int32 fromskew;
  406. int alpha = img->alpha;
  407. stripsize = TIFFStripSize(tif);
  408. r = buf = (u_char *)_TIFFmalloc(4*stripsize);
  409. if (buf == 0) {
  410. TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
  411. return (0);
  412. }
  413. g = r + stripsize;
  414. b = g + stripsize;
  415. a = b + stripsize;
  416. if (!alpha)
  417. memset(a, 0xff, stripsize);
  418. orientation = img->orientation;
  419. TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
  420. scanline = TIFFScanlineSize(tif);
  421. fromskew = (w < imagewidth ? imagewidth - w : 0);
  422. for (row = 0; row < h; row += rowsperstrip) {
  423. nrow = (row + rowsperstrip > h ? h - row : rowsperstrip);
  424. if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 0),
  425. r, nrow*scanline) < 0 && img->stoponerr)
  426. break;
  427. if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 1),
  428. g, nrow*scanline) < 0 && img->stoponerr)
  429. break;
  430. if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 2),
  431. b, nrow*scanline) < 0 && img->stoponerr)
  432. break;
  433. if (alpha &&
  434. (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 3),
  435. a, nrow*scanline) < 0 && img->stoponerr))
  436. break;
  437. (*callback)(img, udata, 0, row, w, nrow, fromskew, r, g, b, a);
  438. }
  439. _TIFFfree(buf);
  440. return (1);
  441. }
  442. DECLAREContigCallbackFunc(TestContigCallback)
  443. {
  444. printf("Contig Callback called with x = %d, y = %d, w = %d, h = %d, fromskew = %d\n",
  445. x, y, w, h, fromskew);
  446. }
  447. DECLARESepCallbackFunc(TestSepCallback)
  448. {
  449. printf("Sep Callback called with x = %d, y = %d, w = %d, h = %d, fromskew = %d\n",
  450. x, y, w, h, fromskew);
  451. }
  452. #ifdef MAIN
  453. main(int argc, char **argv)
  454. {
  455. char emsg[1024];
  456. TIFFImageIter img;
  457. int ok;
  458. int stop = 1;
  459. TIFF *tif;
  460. unsigned long nx, ny;
  461. unsigned short BitsPerSample, SamplesPerPixel;
  462. int isColorMapped, isPliFile;
  463. unsigned char *ColorMap;
  464. unsigned char *data;
  465. if (argc < 2) {
  466. fprintf(stderr,"usage: %s tiff_file\n",argv[0]);
  467. exit(1);
  468. }
  469. tif = (TIFF *)PLIGetImage(argv[1], (void *) &data, &ColorMap,
  470. &nx, &ny, &BitsPerSample, &SamplesPerPixel,
  471. &isColorMapped, &isPliFile);
  472. if (tif != NULL) {
  473. if (TIFFImageIterBegin(&img, tif, stop, emsg)) {
  474. /* Here need to set data and callback function! */
  475. if (img.isContig) {
  476. img.callback = TestContigCallback;
  477. } else {
  478. img.callback = TestSepCallback;
  479. }
  480. ok = TIFFImageIterGet(&img, NULL, img.width, img.height);
  481. TIFFImageIterEnd(&img);
  482. } else {
  483. TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), emsg);
  484. }
  485. }
  486. }
  487. #endif
  488. /*
  489. * Local Variables:
  490. * mode: c
  491. * c-basic-offset: 8
  492. * fill-column: 78
  493. * End:
  494. */