tiffsplit.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* $Id: tiffsplit.c,v 1.22 2011-10-22 17:03:01 bfriesen Exp $ */
  2. /*
  3. * Copyright (c) 1992-1997 Sam Leffler
  4. * Copyright (c) 1992-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 "tiffio.h"
  30. #ifndef HAVE_GETOPT
  31. extern int getopt(int, char**, char*);
  32. #endif
  33. #define CopyField(tag, v) \
  34. if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
  35. #define CopyField2(tag, v1, v2) \
  36. if (TIFFGetField(in, tag, &v1, &v2)) TIFFSetField(out, tag, v1, v2)
  37. #define CopyField3(tag, v1, v2, v3) \
  38. if (TIFFGetField(in, tag, &v1, &v2, &v3)) TIFFSetField(out, tag, v1, v2, v3)
  39. #define PATH_LENGTH 8192
  40. static const char TIFF_SUFFIX[] = ".tif";
  41. static char fname[PATH_LENGTH];
  42. static int tiffcp(TIFF*, TIFF*);
  43. static void newfilename(void);
  44. static int cpStrips(TIFF*, TIFF*);
  45. static int cpTiles(TIFF*, TIFF*);
  46. int
  47. main(int argc, char* argv[])
  48. {
  49. TIFF *in, *out;
  50. if (argc < 2) {
  51. fprintf(stderr, "%s\n\n", TIFFGetVersion());
  52. fprintf(stderr, "usage: tiffsplit input.tif [prefix]\n");
  53. return (-3);
  54. }
  55. if (argc > 2) {
  56. strncpy(fname, argv[2], sizeof(fname));
  57. fname[sizeof(fname) - 1] = '\0';
  58. }
  59. in = TIFFOpen(argv[1], "r");
  60. if (in != NULL) {
  61. do {
  62. size_t path_len;
  63. char *path;
  64. newfilename();
  65. path_len = strlen(fname) + sizeof(TIFF_SUFFIX);
  66. path = (char *) _TIFFmalloc(path_len);
  67. strncpy(path, fname, path_len);
  68. path[path_len - 1] = '\0';
  69. strncat(path, TIFF_SUFFIX, path_len - strlen(path) - 1);
  70. out = TIFFOpen(path, TIFFIsBigEndian(in)?"wb":"wl");
  71. _TIFFfree(path);
  72. if (out == NULL)
  73. return (-2);
  74. if (!tiffcp(in, out))
  75. return (-1);
  76. TIFFClose(out);
  77. } while (TIFFReadDirectory(in));
  78. (void) TIFFClose(in);
  79. }
  80. return (0);
  81. }
  82. static void
  83. newfilename(void)
  84. {
  85. static int first = 1;
  86. static long lastTurn;
  87. static long fnum;
  88. static short defname;
  89. static char *fpnt;
  90. if (first) {
  91. if (fname[0]) {
  92. fpnt = fname + strlen(fname);
  93. defname = 0;
  94. } else {
  95. fname[0] = 'x';
  96. fpnt = fname + 1;
  97. defname = 1;
  98. }
  99. first = 0;
  100. }
  101. #define MAXFILES 17576
  102. if (fnum == MAXFILES) {
  103. if (!defname || fname[0] == 'z') {
  104. fprintf(stderr, "tiffsplit: too many files.\n");
  105. exit(1);
  106. }
  107. fname[0]++;
  108. fnum = 0;
  109. }
  110. if (fnum % 676 == 0) {
  111. if (fnum != 0) {
  112. /*
  113. * advance to next letter every 676 pages
  114. * condition for 'z'++ will be covered above
  115. */
  116. fpnt[0]++;
  117. } else {
  118. /*
  119. * set to 'a' if we are on the very first file
  120. */
  121. fpnt[0] = 'a';
  122. }
  123. /*
  124. * set the value of the last turning point
  125. */
  126. lastTurn = fnum;
  127. }
  128. /*
  129. * start from 0 every 676 times (provided by lastTurn)
  130. * this keeps us within a-z boundaries
  131. */
  132. fpnt[1] = (char)((fnum - lastTurn) / 26) + 'a';
  133. /*
  134. * cycle last letter every file, from a-z, then repeat
  135. */
  136. fpnt[2] = (char)(fnum % 26) + 'a';
  137. fnum++;
  138. }
  139. static int
  140. tiffcp(TIFF* in, TIFF* out)
  141. {
  142. uint16 bitspersample, samplesperpixel, compression, shortv, *shortav;
  143. uint32 w, l;
  144. float floatv;
  145. char *stringv;
  146. uint32 longv;
  147. CopyField(TIFFTAG_SUBFILETYPE, longv);
  148. CopyField(TIFFTAG_TILEWIDTH, w);
  149. CopyField(TIFFTAG_TILELENGTH, l);
  150. CopyField(TIFFTAG_IMAGEWIDTH, w);
  151. CopyField(TIFFTAG_IMAGELENGTH, l);
  152. CopyField(TIFFTAG_BITSPERSAMPLE, bitspersample);
  153. CopyField(TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
  154. CopyField(TIFFTAG_COMPRESSION, compression);
  155. if (compression == COMPRESSION_JPEG) {
  156. uint32 count = 0;
  157. void *table = NULL;
  158. if (TIFFGetField(in, TIFFTAG_JPEGTABLES, &count, &table)
  159. && count > 0 && table) {
  160. TIFFSetField(out, TIFFTAG_JPEGTABLES, count, table);
  161. }
  162. }
  163. CopyField(TIFFTAG_PHOTOMETRIC, shortv);
  164. CopyField(TIFFTAG_PREDICTOR, shortv);
  165. CopyField(TIFFTAG_THRESHHOLDING, shortv);
  166. CopyField(TIFFTAG_FILLORDER, shortv);
  167. CopyField(TIFFTAG_ORIENTATION, shortv);
  168. CopyField(TIFFTAG_MINSAMPLEVALUE, shortv);
  169. CopyField(TIFFTAG_MAXSAMPLEVALUE, shortv);
  170. CopyField(TIFFTAG_XRESOLUTION, floatv);
  171. CopyField(TIFFTAG_YRESOLUTION, floatv);
  172. CopyField(TIFFTAG_GROUP3OPTIONS, longv);
  173. CopyField(TIFFTAG_GROUP4OPTIONS, longv);
  174. CopyField(TIFFTAG_RESOLUTIONUNIT, shortv);
  175. CopyField(TIFFTAG_PLANARCONFIG, shortv);
  176. CopyField(TIFFTAG_ROWSPERSTRIP, longv);
  177. CopyField(TIFFTAG_XPOSITION, floatv);
  178. CopyField(TIFFTAG_YPOSITION, floatv);
  179. CopyField(TIFFTAG_IMAGEDEPTH, longv);
  180. CopyField(TIFFTAG_TILEDEPTH, longv);
  181. CopyField(TIFFTAG_SAMPLEFORMAT, shortv);
  182. CopyField2(TIFFTAG_EXTRASAMPLES, shortv, shortav);
  183. { uint16 *red, *green, *blue;
  184. CopyField3(TIFFTAG_COLORMAP, red, green, blue);
  185. }
  186. { uint16 shortv2;
  187. CopyField2(TIFFTAG_PAGENUMBER, shortv, shortv2);
  188. }
  189. CopyField(TIFFTAG_ARTIST, stringv);
  190. CopyField(TIFFTAG_IMAGEDESCRIPTION, stringv);
  191. CopyField(TIFFTAG_MAKE, stringv);
  192. CopyField(TIFFTAG_MODEL, stringv);
  193. CopyField(TIFFTAG_SOFTWARE, stringv);
  194. CopyField(TIFFTAG_DATETIME, stringv);
  195. CopyField(TIFFTAG_HOSTCOMPUTER, stringv);
  196. CopyField(TIFFTAG_PAGENAME, stringv);
  197. CopyField(TIFFTAG_DOCUMENTNAME, stringv);
  198. CopyField(TIFFTAG_BADFAXLINES, longv);
  199. CopyField(TIFFTAG_CLEANFAXDATA, longv);
  200. CopyField(TIFFTAG_CONSECUTIVEBADFAXLINES, longv);
  201. CopyField(TIFFTAG_FAXRECVPARAMS, longv);
  202. CopyField(TIFFTAG_FAXRECVTIME, longv);
  203. CopyField(TIFFTAG_FAXSUBADDRESS, stringv);
  204. CopyField(TIFFTAG_FAXDCS, stringv);
  205. if (TIFFIsTiled(in))
  206. return (cpTiles(in, out));
  207. else
  208. return (cpStrips(in, out));
  209. }
  210. static int
  211. cpStrips(TIFF* in, TIFF* out)
  212. {
  213. tmsize_t bufsize = TIFFStripSize(in);
  214. unsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize);
  215. if (buf) {
  216. tstrip_t s, ns = TIFFNumberOfStrips(in);
  217. uint64 *bytecounts;
  218. if (!TIFFGetField(in, TIFFTAG_STRIPBYTECOUNTS, &bytecounts)) {
  219. fprintf(stderr, "tiffsplit: strip byte counts are missing\n");
  220. return (0);
  221. }
  222. for (s = 0; s < ns; s++) {
  223. if (bytecounts[s] > (uint64)bufsize) {
  224. buf = (unsigned char *)_TIFFrealloc(buf, (tmsize_t)bytecounts[s]);
  225. if (!buf)
  226. return (0);
  227. bufsize = (tmsize_t)bytecounts[s];
  228. }
  229. if (TIFFReadRawStrip(in, s, buf, (tmsize_t)bytecounts[s]) < 0 ||
  230. TIFFWriteRawStrip(out, s, buf, (tmsize_t)bytecounts[s]) < 0) {
  231. _TIFFfree(buf);
  232. return (0);
  233. }
  234. }
  235. _TIFFfree(buf);
  236. return (1);
  237. }
  238. return (0);
  239. }
  240. static int
  241. cpTiles(TIFF* in, TIFF* out)
  242. {
  243. tmsize_t bufsize = TIFFTileSize(in);
  244. unsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize);
  245. if (buf) {
  246. ttile_t t, nt = TIFFNumberOfTiles(in);
  247. uint64 *bytecounts;
  248. if (!TIFFGetField(in, TIFFTAG_TILEBYTECOUNTS, &bytecounts)) {
  249. fprintf(stderr, "tiffsplit: tile byte counts are missing\n");
  250. return (0);
  251. }
  252. for (t = 0; t < nt; t++) {
  253. if (bytecounts[t] > (uint64) bufsize) {
  254. buf = (unsigned char *)_TIFFrealloc(buf, (tmsize_t)bytecounts[t]);
  255. if (!buf)
  256. return (0);
  257. bufsize = (tmsize_t)bytecounts[t];
  258. }
  259. if (TIFFReadRawTile(in, t, buf, (tmsize_t)bytecounts[t]) < 0 ||
  260. TIFFWriteRawTile(out, t, buf, (tmsize_t)bytecounts[t]) < 0) {
  261. _TIFFfree(buf);
  262. return (0);
  263. }
  264. }
  265. _TIFFfree(buf);
  266. return (1);
  267. }
  268. return (0);
  269. }
  270. /* vim: set ts=8 sts=8 sw=8 noet: */
  271. /*
  272. * Local Variables:
  273. * mode: c
  274. * c-basic-offset: 8
  275. * fill-column: 78
  276. * End:
  277. */