tiff2rgba.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /* $Id: tiff2rgba.c,v 1.19 2011-02-23 21:46:09 fwarmerdam Exp $ */
  2. /*
  3. * Copyright (c) 1991-1997 Sam Leffler
  4. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and
  7. * its documentation for any purpose is hereby granted without fee, provided
  8. * that (i) the above copyright notices and this permission notice appear in
  9. * all copies of the software and related documentation, and (ii) the names of
  10. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11. * publicity relating to the software without the specific, prior written
  12. * permission of Sam Leffler and Silicon Graphics.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  22. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. */
  25. #include "tif_config.h"
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #ifdef HAVE_UNISTD_H
  30. # include <unistd.h>
  31. #endif
  32. #ifdef NEED_LIBPORT
  33. # include "libport.h"
  34. #endif
  35. #include "tiffiop.h"
  36. #include "tiffio.h"
  37. #define streq(a,b) (strcmp(a,b) == 0)
  38. #define CopyField(tag, v) \
  39. if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
  40. #ifndef howmany
  41. #define howmany(x, y) (((x)+((y)-1))/(y))
  42. #endif
  43. #define roundup(x, y) (howmany(x,y)*((uint32)(y)))
  44. uint16 compression = COMPRESSION_PACKBITS;
  45. uint32 rowsperstrip = (uint32) -1;
  46. int process_by_block = 0; /* default is whole image at once */
  47. int no_alpha = 0;
  48. int bigtiff_output = 0;
  49. static int tiffcvt(TIFF* in, TIFF* out);
  50. static void usage(int code);
  51. int
  52. main(int argc, char* argv[])
  53. {
  54. TIFF *in, *out;
  55. int c;
  56. extern int optind;
  57. extern char *optarg;
  58. while ((c = getopt(argc, argv, "c:r:t:bn8")) != -1)
  59. switch (c) {
  60. case 'b':
  61. process_by_block = 1;
  62. break;
  63. case 'c':
  64. if (streq(optarg, "none"))
  65. compression = COMPRESSION_NONE;
  66. else if (streq(optarg, "packbits"))
  67. compression = COMPRESSION_PACKBITS;
  68. else if (streq(optarg, "lzw"))
  69. compression = COMPRESSION_LZW;
  70. else if (streq(optarg, "jpeg"))
  71. compression = COMPRESSION_JPEG;
  72. else if (streq(optarg, "zip"))
  73. compression = COMPRESSION_DEFLATE;
  74. else
  75. usage(-1);
  76. break;
  77. case 'r':
  78. rowsperstrip = atoi(optarg);
  79. break;
  80. case 't':
  81. rowsperstrip = atoi(optarg);
  82. break;
  83. case 'n':
  84. no_alpha = 1;
  85. break;
  86. case '8':
  87. bigtiff_output = 1;
  88. break;
  89. case '?':
  90. usage(0);
  91. /*NOTREACHED*/
  92. }
  93. if (argc - optind < 2)
  94. usage(-1);
  95. out = TIFFOpen(argv[argc-1], bigtiff_output?"w8":"w");
  96. if (out == NULL)
  97. return (-2);
  98. for (; optind < argc-1; optind++) {
  99. in = TIFFOpen(argv[optind], "r");
  100. if (in != NULL) {
  101. do {
  102. if (!tiffcvt(in, out) ||
  103. !TIFFWriteDirectory(out)) {
  104. (void) TIFFClose(out);
  105. (void) TIFFClose(in);
  106. return (1);
  107. }
  108. } while (TIFFReadDirectory(in));
  109. (void) TIFFClose(in);
  110. }
  111. }
  112. (void) TIFFClose(out);
  113. return (0);
  114. }
  115. static int
  116. cvt_by_tile( TIFF *in, TIFF *out )
  117. {
  118. uint32* raster; /* retrieve RGBA image */
  119. uint32 width, height; /* image width & height */
  120. uint32 tile_width, tile_height;
  121. uint32 row, col;
  122. uint32 *wrk_line;
  123. int ok = 1;
  124. TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
  125. TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
  126. if( !TIFFGetField(in, TIFFTAG_TILEWIDTH, &tile_width)
  127. || !TIFFGetField(in, TIFFTAG_TILELENGTH, &tile_height) ) {
  128. TIFFError(TIFFFileName(in), "Source image not tiled");
  129. return (0);
  130. }
  131. TIFFSetField(out, TIFFTAG_TILEWIDTH, tile_width );
  132. TIFFSetField(out, TIFFTAG_TILELENGTH, tile_height );
  133. /*
  134. * Allocate tile buffer
  135. */
  136. raster = (uint32*)_TIFFmalloc(tile_width * tile_height * sizeof (uint32));
  137. if (raster == 0) {
  138. TIFFError(TIFFFileName(in), "No space for raster buffer");
  139. return (0);
  140. }
  141. /*
  142. * Allocate a scanline buffer for swapping during the vertical
  143. * mirroring pass.
  144. */
  145. wrk_line = (uint32*)_TIFFmalloc(tile_width * sizeof (uint32));
  146. if (!wrk_line) {
  147. TIFFError(TIFFFileName(in), "No space for raster scanline buffer");
  148. ok = 0;
  149. }
  150. /*
  151. * Loop over the tiles.
  152. */
  153. for( row = 0; ok && row < height; row += tile_height )
  154. {
  155. for( col = 0; ok && col < width; col += tile_width )
  156. {
  157. uint32 i_row;
  158. /* Read the tile into an RGBA array */
  159. if (!TIFFReadRGBATile(in, col, row, raster)) {
  160. ok = 0;
  161. break;
  162. }
  163. /*
  164. * XXX: raster array has 4-byte unsigned integer type, that is why
  165. * we should rearrange it here.
  166. */
  167. #if HOST_BIGENDIAN
  168. TIFFSwabArrayOfLong(raster, tile_width * tile_height);
  169. #endif
  170. /*
  171. * For some reason the TIFFReadRGBATile() function chooses the
  172. * lower left corner as the origin. Vertically mirror scanlines.
  173. */
  174. for( i_row = 0; i_row < tile_height / 2; i_row++ )
  175. {
  176. uint32 *top_line, *bottom_line;
  177. top_line = raster + tile_width * i_row;
  178. bottom_line = raster + tile_width * (tile_height-i_row-1);
  179. _TIFFmemcpy(wrk_line, top_line, 4*tile_width);
  180. _TIFFmemcpy(top_line, bottom_line, 4*tile_width);
  181. _TIFFmemcpy(bottom_line, wrk_line, 4*tile_width);
  182. }
  183. /*
  184. * Write out the result in a tile.
  185. */
  186. if( TIFFWriteEncodedTile( out,
  187. TIFFComputeTile( out, col, row, 0, 0),
  188. raster,
  189. 4 * tile_width * tile_height ) == -1 )
  190. {
  191. ok = 0;
  192. break;
  193. }
  194. }
  195. }
  196. _TIFFfree( raster );
  197. _TIFFfree( wrk_line );
  198. return ok;
  199. }
  200. static int
  201. cvt_by_strip( TIFF *in, TIFF *out )
  202. {
  203. uint32* raster; /* retrieve RGBA image */
  204. uint32 width, height; /* image width & height */
  205. uint32 row;
  206. uint32 *wrk_line;
  207. int ok = 1;
  208. TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
  209. TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
  210. if( !TIFFGetField(in, TIFFTAG_ROWSPERSTRIP, &rowsperstrip) ) {
  211. TIFFError(TIFFFileName(in), "Source image not in strips");
  212. return (0);
  213. }
  214. TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
  215. /*
  216. * Allocate strip buffer
  217. */
  218. raster = (uint32*)_TIFFmalloc(width * rowsperstrip * sizeof (uint32));
  219. if (raster == 0) {
  220. TIFFError(TIFFFileName(in), "No space for raster buffer");
  221. return (0);
  222. }
  223. /*
  224. * Allocate a scanline buffer for swapping during the vertical
  225. * mirroring pass.
  226. */
  227. wrk_line = (uint32*)_TIFFmalloc(width * sizeof (uint32));
  228. if (!wrk_line) {
  229. TIFFError(TIFFFileName(in), "No space for raster scanline buffer");
  230. ok = 0;
  231. }
  232. /*
  233. * Loop over the strips.
  234. */
  235. for( row = 0; ok && row < height; row += rowsperstrip )
  236. {
  237. int rows_to_write, i_row;
  238. /* Read the strip into an RGBA array */
  239. if (!TIFFReadRGBAStrip(in, row, raster)) {
  240. ok = 0;
  241. break;
  242. }
  243. /*
  244. * XXX: raster array has 4-byte unsigned integer type, that is why
  245. * we should rearrange it here.
  246. */
  247. #if HOST_BIGENDIAN
  248. TIFFSwabArrayOfLong(raster, width * rowsperstrip);
  249. #endif
  250. /*
  251. * Figure out the number of scanlines actually in this strip.
  252. */
  253. if( row + rowsperstrip > height )
  254. rows_to_write = height - row;
  255. else
  256. rows_to_write = rowsperstrip;
  257. /*
  258. * For some reason the TIFFReadRGBAStrip() function chooses the
  259. * lower left corner as the origin. Vertically mirror scanlines.
  260. */
  261. for( i_row = 0; i_row < rows_to_write / 2; i_row++ )
  262. {
  263. uint32 *top_line, *bottom_line;
  264. top_line = raster + width * i_row;
  265. bottom_line = raster + width * (rows_to_write-i_row-1);
  266. _TIFFmemcpy(wrk_line, top_line, 4*width);
  267. _TIFFmemcpy(top_line, bottom_line, 4*width);
  268. _TIFFmemcpy(bottom_line, wrk_line, 4*width);
  269. }
  270. /*
  271. * Write out the result in a strip
  272. */
  273. if( TIFFWriteEncodedStrip( out, row / rowsperstrip, raster,
  274. 4 * rows_to_write * width ) == -1 )
  275. {
  276. ok = 0;
  277. break;
  278. }
  279. }
  280. _TIFFfree( raster );
  281. _TIFFfree( wrk_line );
  282. return ok;
  283. }
  284. /*
  285. * cvt_whole_image()
  286. *
  287. * read the whole image into one big RGBA buffer and then write out
  288. * strips from that. This is using the traditional TIFFReadRGBAImage()
  289. * API that we trust.
  290. */
  291. static int
  292. cvt_whole_image( TIFF *in, TIFF *out )
  293. {
  294. uint32* raster; /* retrieve RGBA image */
  295. uint32 width, height; /* image width & height */
  296. uint32 row;
  297. size_t pixel_count;
  298. TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
  299. TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
  300. pixel_count = width * height;
  301. /* XXX: Check the integer overflow. */
  302. if (!width || !height || pixel_count / width != height) {
  303. TIFFError(TIFFFileName(in),
  304. "Malformed input file; can't allocate buffer for raster of %lux%lu size",
  305. (unsigned long)width, (unsigned long)height);
  306. return 0;
  307. }
  308. rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip);
  309. TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
  310. raster = (uint32*)_TIFFCheckMalloc(in, pixel_count, sizeof(uint32), "raster buffer");
  311. if (raster == 0) {
  312. TIFFError(TIFFFileName(in), "Failed to allocate buffer (%lu elements of %lu each)",
  313. (unsigned long)pixel_count, (unsigned long)sizeof(uint32));
  314. return (0);
  315. }
  316. /* Read the image in one chunk into an RGBA array */
  317. if (!TIFFReadRGBAImageOriented(in, width, height, raster,
  318. ORIENTATION_TOPLEFT, 0)) {
  319. _TIFFfree(raster);
  320. return (0);
  321. }
  322. /*
  323. * XXX: raster array has 4-byte unsigned integer type, that is why
  324. * we should rearrange it here.
  325. */
  326. #if HOST_BIGENDIAN
  327. TIFFSwabArrayOfLong(raster, width * height);
  328. #endif
  329. /*
  330. * Do we want to strip away alpha components?
  331. */
  332. if (no_alpha)
  333. {
  334. size_t count = pixel_count;
  335. unsigned char *src, *dst;
  336. src = dst = (unsigned char *) raster;
  337. while (count > 0)
  338. {
  339. *(dst++) = *(src++);
  340. *(dst++) = *(src++);
  341. *(dst++) = *(src++);
  342. src++;
  343. count--;
  344. }
  345. }
  346. /*
  347. * Write out the result in strips
  348. */
  349. for (row = 0; row < height; row += rowsperstrip)
  350. {
  351. unsigned char * raster_strip;
  352. int rows_to_write;
  353. int bytes_per_pixel;
  354. if (no_alpha)
  355. {
  356. raster_strip = ((unsigned char *) raster) + 3 * row * width;
  357. bytes_per_pixel = 3;
  358. }
  359. else
  360. {
  361. raster_strip = (unsigned char *) (raster + row * width);
  362. bytes_per_pixel = 4;
  363. }
  364. if( row + rowsperstrip > height )
  365. rows_to_write = height - row;
  366. else
  367. rows_to_write = rowsperstrip;
  368. if( TIFFWriteEncodedStrip( out, row / rowsperstrip, raster_strip,
  369. bytes_per_pixel * rows_to_write * width ) == -1 )
  370. {
  371. _TIFFfree( raster );
  372. return 0;
  373. }
  374. }
  375. _TIFFfree( raster );
  376. return 1;
  377. }
  378. static int
  379. tiffcvt(TIFF* in, TIFF* out)
  380. {
  381. uint32 width, height; /* image width & height */
  382. uint16 shortv;
  383. float floatv;
  384. char *stringv;
  385. uint32 longv;
  386. uint16 v[1];
  387. TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
  388. TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
  389. CopyField(TIFFTAG_SUBFILETYPE, longv);
  390. TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);
  391. TIFFSetField(out, TIFFTAG_IMAGELENGTH, height);
  392. TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
  393. TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
  394. TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
  395. CopyField(TIFFTAG_FILLORDER, shortv);
  396. TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  397. if( no_alpha )
  398. TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 3);
  399. else
  400. TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 4);
  401. if( !no_alpha )
  402. {
  403. v[0] = EXTRASAMPLE_ASSOCALPHA;
  404. TIFFSetField(out, TIFFTAG_EXTRASAMPLES, 1, v);
  405. }
  406. CopyField(TIFFTAG_XRESOLUTION, floatv);
  407. CopyField(TIFFTAG_YRESOLUTION, floatv);
  408. CopyField(TIFFTAG_RESOLUTIONUNIT, shortv);
  409. TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  410. TIFFSetField(out, TIFFTAG_SOFTWARE, TIFFGetVersion());
  411. CopyField(TIFFTAG_DOCUMENTNAME, stringv);
  412. if( process_by_block && TIFFIsTiled( in ) )
  413. return( cvt_by_tile( in, out ) );
  414. else if( process_by_block )
  415. return( cvt_by_strip( in, out ) );
  416. else
  417. return( cvt_whole_image( in, out ) );
  418. }
  419. static char* stuff[] = {
  420. "usage: tiff2rgba [-c comp] [-r rows] [-b] [-n] [-8] input... output",
  421. "where comp is one of the following compression algorithms:",
  422. " jpeg\t\tJPEG encoding",
  423. " zip\t\tLempel-Ziv & Welch encoding",
  424. " lzw\t\tLempel-Ziv & Welch encoding",
  425. " packbits\tPackBits encoding",
  426. " none\t\tno compression",
  427. "and the other options are:",
  428. " -r\trows/strip",
  429. " -b (progress by block rather than as a whole image)",
  430. " -n don't emit alpha component.",
  431. " -8 write BigTIFF file instead of ClassicTIFF",
  432. NULL
  433. };
  434. static void
  435. usage(int code)
  436. {
  437. char buf[BUFSIZ];
  438. int i;
  439. setbuf(stderr, buf);
  440. fprintf(stderr, "%s\n\n", TIFFGetVersion());
  441. for (i = 0; stuff[i] != NULL; i++)
  442. fprintf(stderr, "%s\n", stuff[i]);
  443. exit(code);
  444. }
  445. /* vim: set ts=8 sts=8 sw=8 noet: */
  446. /*
  447. * Local Variables:
  448. * mode: c
  449. * c-basic-offset: 8
  450. * fill-column: 78
  451. * End:
  452. */