2
0

tiff2bw.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /* $Id: tiff2bw.c,v 1.15 2010-07-02 12:02:56 dron Exp $ */
  2. /*
  3. * Copyright (c) 1988-1997 Sam Leffler
  4. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and
  7. * its documentation for any purpose is hereby granted without fee, provided
  8. * that (i) the above copyright notices and this permission notice appear in
  9. * all copies of the software and related documentation, and (ii) the names of
  10. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11. * publicity relating to the software without the specific, prior written
  12. * permission of Sam Leffler and Silicon Graphics.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  22. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. */
  25. #include "tif_config.h"
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30. #ifdef HAVE_UNISTD_H
  31. # include <unistd.h>
  32. #endif
  33. #ifdef NEED_LIBPORT
  34. # include "libport.h"
  35. #endif
  36. #include "tiffio.h"
  37. #define streq(a,b) (strcmp((a),(b)) == 0)
  38. #define strneq(a,b,n) (strncmp(a,b,n) == 0)
  39. /* x% weighting -> fraction of full color */
  40. #define PCT(x) (((x)*255+127)/100)
  41. int RED = PCT(30); /* 30% */
  42. int GREEN = PCT(59); /* 59% */
  43. int BLUE = PCT(11); /* 11% */
  44. static void usage(void);
  45. static int processCompressOptions(char*);
  46. static void
  47. compresscontig(unsigned char* out, unsigned char* rgb, uint32 n)
  48. {
  49. register int v, red = RED, green = GREEN, blue = BLUE;
  50. while (n-- > 0) {
  51. v = red*(*rgb++);
  52. v += green*(*rgb++);
  53. v += blue*(*rgb++);
  54. *out++ = v>>8;
  55. }
  56. }
  57. static void
  58. compresssep(unsigned char* out,
  59. unsigned char* r, unsigned char* g, unsigned char* b, uint32 n)
  60. {
  61. register uint32 red = RED, green = GREEN, blue = BLUE;
  62. while (n-- > 0)
  63. *out++ = (unsigned char)
  64. ((red*(*r++) + green*(*g++) + blue*(*b++)) >> 8);
  65. }
  66. static int
  67. checkcmap(TIFF* tif, int n, uint16* r, uint16* g, uint16* b)
  68. {
  69. while (n-- > 0)
  70. if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
  71. return (16);
  72. TIFFWarning(TIFFFileName(tif), "Assuming 8-bit colormap");
  73. return (8);
  74. }
  75. static void
  76. compresspalette(unsigned char* out, unsigned char* data, uint32 n, uint16* rmap, uint16* gmap, uint16* bmap)
  77. {
  78. register int v, red = RED, green = GREEN, blue = BLUE;
  79. while (n-- > 0) {
  80. unsigned int ix = *data++;
  81. v = red*rmap[ix];
  82. v += green*gmap[ix];
  83. v += blue*bmap[ix];
  84. *out++ = v>>8;
  85. }
  86. }
  87. static uint16 compression = (uint16) -1;
  88. static uint16 predictor = 0;
  89. static int jpegcolormode = JPEGCOLORMODE_RGB;
  90. static int quality = 75; /* JPEG quality */
  91. static void cpTags(TIFF* in, TIFF* out);
  92. int
  93. main(int argc, char* argv[])
  94. {
  95. uint32 rowsperstrip = (uint32) -1;
  96. TIFF *in, *out;
  97. uint32 w, h;
  98. uint16 samplesperpixel;
  99. uint16 bitspersample;
  100. uint16 config;
  101. uint16 photometric;
  102. uint16* red;
  103. uint16* green;
  104. uint16* blue;
  105. tsize_t rowsize;
  106. register uint32 row;
  107. register tsample_t s;
  108. unsigned char *inbuf, *outbuf;
  109. char thing[1024];
  110. int c;
  111. extern int optind;
  112. extern char *optarg;
  113. while ((c = getopt(argc, argv, "c:r:R:G:B:")) != -1)
  114. switch (c) {
  115. case 'c': /* compression scheme */
  116. if (!processCompressOptions(optarg))
  117. usage();
  118. break;
  119. case 'r': /* rows/strip */
  120. rowsperstrip = atoi(optarg);
  121. break;
  122. case 'R':
  123. RED = PCT(atoi(optarg));
  124. break;
  125. case 'G':
  126. GREEN = PCT(atoi(optarg));
  127. break;
  128. case 'B':
  129. BLUE = PCT(atoi(optarg));
  130. break;
  131. case '?':
  132. usage();
  133. /*NOTREACHED*/
  134. }
  135. if (argc - optind < 2)
  136. usage();
  137. in = TIFFOpen(argv[optind], "r");
  138. if (in == NULL)
  139. return (-1);
  140. photometric = 0;
  141. TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &photometric);
  142. if (photometric != PHOTOMETRIC_RGB && photometric != PHOTOMETRIC_PALETTE ) {
  143. fprintf(stderr,
  144. "%s: Bad photometric; can only handle RGB and Palette images.\n",
  145. argv[optind]);
  146. return (-1);
  147. }
  148. TIFFGetField(in, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
  149. if (samplesperpixel != 1 && samplesperpixel != 3) {
  150. fprintf(stderr, "%s: Bad samples/pixel %u.\n",
  151. argv[optind], samplesperpixel);
  152. return (-1);
  153. }
  154. TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bitspersample);
  155. if (bitspersample != 8) {
  156. fprintf(stderr,
  157. " %s: Sorry, only handle 8-bit samples.\n", argv[optind]);
  158. return (-1);
  159. }
  160. TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &w);
  161. TIFFGetField(in, TIFFTAG_IMAGELENGTH, &h);
  162. TIFFGetField(in, TIFFTAG_PLANARCONFIG, &config);
  163. out = TIFFOpen(argv[optind+1], "w");
  164. if (out == NULL)
  165. return (-1);
  166. TIFFSetField(out, TIFFTAG_IMAGEWIDTH, w);
  167. TIFFSetField(out, TIFFTAG_IMAGELENGTH, h);
  168. TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
  169. TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1);
  170. TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  171. cpTags(in, out);
  172. if (compression != (uint16) -1) {
  173. TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
  174. switch (compression) {
  175. case COMPRESSION_JPEG:
  176. TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);
  177. TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
  178. break;
  179. case COMPRESSION_LZW:
  180. case COMPRESSION_DEFLATE:
  181. if (predictor != 0)
  182. TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
  183. break;
  184. }
  185. }
  186. TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
  187. sprintf(thing, "B&W version of %s", argv[optind]);
  188. TIFFSetField(out, TIFFTAG_IMAGEDESCRIPTION, thing);
  189. TIFFSetField(out, TIFFTAG_SOFTWARE, "tiff2bw");
  190. outbuf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(out));
  191. TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
  192. TIFFDefaultStripSize(out, rowsperstrip));
  193. #define pack(a,b) ((a)<<8 | (b))
  194. switch (pack(photometric, config)) {
  195. case pack(PHOTOMETRIC_PALETTE, PLANARCONFIG_CONTIG):
  196. case pack(PHOTOMETRIC_PALETTE, PLANARCONFIG_SEPARATE):
  197. TIFFGetField(in, TIFFTAG_COLORMAP, &red, &green, &blue);
  198. /*
  199. * Convert 16-bit colormap to 8-bit (unless it looks
  200. * like an old-style 8-bit colormap).
  201. */
  202. if (checkcmap(in, 1<<bitspersample, red, green, blue) == 16) {
  203. int i;
  204. #define CVT(x) (((x) * 255L) / ((1L<<16)-1))
  205. for (i = (1<<bitspersample)-1; i >= 0; i--) {
  206. red[i] = CVT(red[i]);
  207. green[i] = CVT(green[i]);
  208. blue[i] = CVT(blue[i]);
  209. }
  210. #undef CVT
  211. }
  212. inbuf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(in));
  213. for (row = 0; row < h; row++) {
  214. if (TIFFReadScanline(in, inbuf, row, 0) < 0)
  215. break;
  216. compresspalette(outbuf, inbuf, w, red, green, blue);
  217. if (TIFFWriteScanline(out, outbuf, row, 0) < 0)
  218. break;
  219. }
  220. break;
  221. case pack(PHOTOMETRIC_RGB, PLANARCONFIG_CONTIG):
  222. inbuf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(in));
  223. for (row = 0; row < h; row++) {
  224. if (TIFFReadScanline(in, inbuf, row, 0) < 0)
  225. break;
  226. compresscontig(outbuf, inbuf, w);
  227. if (TIFFWriteScanline(out, outbuf, row, 0) < 0)
  228. break;
  229. }
  230. break;
  231. case pack(PHOTOMETRIC_RGB, PLANARCONFIG_SEPARATE):
  232. rowsize = TIFFScanlineSize(in);
  233. inbuf = (unsigned char *)_TIFFmalloc(3*rowsize);
  234. for (row = 0; row < h; row++) {
  235. for (s = 0; s < 3; s++)
  236. if (TIFFReadScanline(in,
  237. inbuf+s*rowsize, row, s) < 0)
  238. return (-1);
  239. compresssep(outbuf,
  240. inbuf, inbuf+rowsize, inbuf+2*rowsize, w);
  241. if (TIFFWriteScanline(out, outbuf, row, 0) < 0)
  242. break;
  243. }
  244. break;
  245. }
  246. #undef pack
  247. TIFFClose(out);
  248. return (0);
  249. }
  250. static int
  251. processCompressOptions(char* opt)
  252. {
  253. if (streq(opt, "none"))
  254. compression = COMPRESSION_NONE;
  255. else if (streq(opt, "packbits"))
  256. compression = COMPRESSION_PACKBITS;
  257. else if (strneq(opt, "jpeg", 4)) {
  258. char* cp = strchr(opt, ':');
  259. compression = COMPRESSION_JPEG;
  260. while( cp )
  261. {
  262. if (isdigit((int)cp[1]))
  263. quality = atoi(cp+1);
  264. else if (cp[1] == 'r' )
  265. jpegcolormode = JPEGCOLORMODE_RAW;
  266. else
  267. usage();
  268. cp = strchr(cp+1,':');
  269. }
  270. } else if (strneq(opt, "lzw", 3)) {
  271. char* cp = strchr(opt, ':');
  272. if (cp)
  273. predictor = atoi(cp+1);
  274. compression = COMPRESSION_LZW;
  275. } else if (strneq(opt, "zip", 3)) {
  276. char* cp = strchr(opt, ':');
  277. if (cp)
  278. predictor = atoi(cp+1);
  279. compression = COMPRESSION_DEFLATE;
  280. } else
  281. return (0);
  282. return (1);
  283. }
  284. #define CopyField(tag, v) \
  285. if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
  286. #define CopyField2(tag, v1, v2) \
  287. if (TIFFGetField(in, tag, &v1, &v2)) TIFFSetField(out, tag, v1, v2)
  288. #define CopyField3(tag, v1, v2, v3) \
  289. if (TIFFGetField(in, tag, &v1, &v2, &v3)) TIFFSetField(out, tag, v1, v2, v3)
  290. #define CopyField4(tag, v1, v2, v3, v4) \
  291. if (TIFFGetField(in, tag, &v1, &v2, &v3, &v4)) TIFFSetField(out, tag, v1, v2, v3, v4)
  292. static void
  293. cpTag(TIFF* in, TIFF* out, uint16 tag, uint16 count, TIFFDataType type)
  294. {
  295. switch (type) {
  296. case TIFF_SHORT:
  297. if (count == 1) {
  298. uint16 shortv;
  299. CopyField(tag, shortv);
  300. } else if (count == 2) {
  301. uint16 shortv1, shortv2;
  302. CopyField2(tag, shortv1, shortv2);
  303. } else if (count == 4) {
  304. uint16 *tr, *tg, *tb, *ta;
  305. CopyField4(tag, tr, tg, tb, ta);
  306. } else if (count == (uint16) -1) {
  307. uint16 shortv1;
  308. uint16* shortav;
  309. CopyField2(tag, shortv1, shortav);
  310. }
  311. break;
  312. case TIFF_LONG:
  313. { uint32 longv;
  314. CopyField(tag, longv);
  315. }
  316. break;
  317. case TIFF_RATIONAL:
  318. if (count == 1) {
  319. float floatv;
  320. CopyField(tag, floatv);
  321. } else if (count == (uint16) -1) {
  322. float* floatav;
  323. CopyField(tag, floatav);
  324. }
  325. break;
  326. case TIFF_ASCII:
  327. { char* stringv;
  328. CopyField(tag, stringv);
  329. }
  330. break;
  331. case TIFF_DOUBLE:
  332. if (count == 1) {
  333. double doublev;
  334. CopyField(tag, doublev);
  335. } else if (count == (uint16) -1) {
  336. double* doubleav;
  337. CopyField(tag, doubleav);
  338. }
  339. break;
  340. default:
  341. TIFFError(TIFFFileName(in),
  342. "Data type %d is not supported, tag %d skipped.",
  343. tag, type);
  344. }
  345. }
  346. #undef CopyField4
  347. #undef CopyField3
  348. #undef CopyField2
  349. #undef CopyField
  350. static struct cpTag {
  351. uint16 tag;
  352. uint16 count;
  353. TIFFDataType type;
  354. } tags[] = {
  355. { TIFFTAG_SUBFILETYPE, 1, TIFF_LONG },
  356. { TIFFTAG_THRESHHOLDING, 1, TIFF_SHORT },
  357. { TIFFTAG_DOCUMENTNAME, 1, TIFF_ASCII },
  358. { TIFFTAG_IMAGEDESCRIPTION, 1, TIFF_ASCII },
  359. { TIFFTAG_MAKE, 1, TIFF_ASCII },
  360. { TIFFTAG_MODEL, 1, TIFF_ASCII },
  361. { TIFFTAG_MINSAMPLEVALUE, 1, TIFF_SHORT },
  362. { TIFFTAG_MAXSAMPLEVALUE, 1, TIFF_SHORT },
  363. { TIFFTAG_XRESOLUTION, 1, TIFF_RATIONAL },
  364. { TIFFTAG_YRESOLUTION, 1, TIFF_RATIONAL },
  365. { TIFFTAG_PAGENAME, 1, TIFF_ASCII },
  366. { TIFFTAG_XPOSITION, 1, TIFF_RATIONAL },
  367. { TIFFTAG_YPOSITION, 1, TIFF_RATIONAL },
  368. { TIFFTAG_RESOLUTIONUNIT, 1, TIFF_SHORT },
  369. { TIFFTAG_SOFTWARE, 1, TIFF_ASCII },
  370. { TIFFTAG_DATETIME, 1, TIFF_ASCII },
  371. { TIFFTAG_ARTIST, 1, TIFF_ASCII },
  372. { TIFFTAG_HOSTCOMPUTER, 1, TIFF_ASCII },
  373. { TIFFTAG_WHITEPOINT, 2, TIFF_RATIONAL },
  374. { TIFFTAG_PRIMARYCHROMATICITIES,(uint16) -1,TIFF_RATIONAL },
  375. { TIFFTAG_HALFTONEHINTS, 2, TIFF_SHORT },
  376. { TIFFTAG_INKSET, 1, TIFF_SHORT },
  377. { TIFFTAG_DOTRANGE, 2, TIFF_SHORT },
  378. { TIFFTAG_TARGETPRINTER, 1, TIFF_ASCII },
  379. { TIFFTAG_SAMPLEFORMAT, 1, TIFF_SHORT },
  380. { TIFFTAG_YCBCRCOEFFICIENTS, (uint16) -1,TIFF_RATIONAL },
  381. { TIFFTAG_YCBCRSUBSAMPLING, 2, TIFF_SHORT },
  382. { TIFFTAG_YCBCRPOSITIONING, 1, TIFF_SHORT },
  383. { TIFFTAG_REFERENCEBLACKWHITE, (uint16) -1,TIFF_RATIONAL },
  384. { TIFFTAG_EXTRASAMPLES, (uint16) -1, TIFF_SHORT },
  385. { TIFFTAG_SMINSAMPLEVALUE, 1, TIFF_DOUBLE },
  386. { TIFFTAG_SMAXSAMPLEVALUE, 1, TIFF_DOUBLE },
  387. { TIFFTAG_STONITS, 1, TIFF_DOUBLE },
  388. };
  389. #define NTAGS (sizeof (tags) / sizeof (tags[0]))
  390. static void
  391. cpTags(TIFF* in, TIFF* out)
  392. {
  393. struct cpTag *p;
  394. for (p = tags; p < &tags[NTAGS]; p++)
  395. cpTag(in, out, p->tag, p->count, p->type);
  396. }
  397. #undef NTAGS
  398. char* stuff[] = {
  399. "usage: tiff2bw [options] input.tif output.tif",
  400. "where options are:",
  401. " -R % use #% from red channel",
  402. " -G % use #% from green channel",
  403. " -B % use #% from blue channel",
  404. "",
  405. " -r # make each strip have no more than # rows",
  406. "",
  407. " -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding",
  408. " -c zip[:opts] compress output with deflate encoding",
  409. " -c packbits compress output with packbits encoding",
  410. " -c g3[:opts] compress output with CCITT Group 3 encoding",
  411. " -c g4 compress output with CCITT Group 4 encoding",
  412. " -c none use no compression algorithm on output",
  413. "",
  414. "LZW and deflate options:",
  415. " # set predictor value",
  416. "For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
  417. NULL
  418. };
  419. static void
  420. usage(void)
  421. {
  422. char buf[BUFSIZ];
  423. int i;
  424. setbuf(stderr, buf);
  425. fprintf(stderr, "%s\n\n", TIFFGetVersion());
  426. for (i = 0; stuff[i] != NULL; i++)
  427. fprintf(stderr, "%s\n", stuff[i]);
  428. exit(-1);
  429. }
  430. /* vim: set ts=8 sts=8 sw=8 noet: */
  431. /*
  432. * Local Variables:
  433. * mode: c
  434. * c-basic-offset: 8
  435. * fill-column: 78
  436. * End:
  437. */