TiffDecode.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /*
  2. * The Python Imaging Library.
  3. * $Id: //modules/pil/libImaging/TiffDecode.c#1 $
  4. *
  5. * LibTiff-based Group3 and Group4 decoder
  6. *
  7. *
  8. * started modding to use non-private tiff functions to port to libtiff 4.x
  9. * eds 3/12/12
  10. *
  11. */
  12. #include "Imaging.h"
  13. #ifdef HAVE_LIBTIFF
  14. #ifndef uint
  15. #define uint uint32
  16. #endif
  17. #include "TiffDecode.h"
  18. void dump_state(const TIFFSTATE *state){
  19. TRACE(("State: Location %u size %d eof %d data: %p ifd: %d\n", (uint)state->loc,
  20. (int)state->size, (uint)state->eof, state->data, state->ifd));
  21. }
  22. /*
  23. procs for TIFFOpenClient
  24. */
  25. tsize_t _tiffReadProc(thandle_t hdata, tdata_t buf, tsize_t size) {
  26. TIFFSTATE *state = (TIFFSTATE *)hdata;
  27. tsize_t to_read;
  28. TRACE(("_tiffReadProc: %d \n", (int)size));
  29. dump_state(state);
  30. to_read = min(size, min(state->size, (tsize_t)state->eof) - (tsize_t)state->loc);
  31. TRACE(("to_read: %d\n", (int)to_read));
  32. _TIFFmemcpy(buf, (UINT8 *)state->data + state->loc, to_read);
  33. state->loc += (toff_t)to_read;
  34. TRACE( ("location: %u\n", (uint)state->loc));
  35. return to_read;
  36. }
  37. tsize_t _tiffWriteProc(thandle_t hdata, tdata_t buf, tsize_t size) {
  38. TIFFSTATE *state = (TIFFSTATE *)hdata;
  39. tsize_t to_write;
  40. TRACE(("_tiffWriteProc: %d \n", (int)size));
  41. dump_state(state);
  42. to_write = min(size, state->size - (tsize_t)state->loc);
  43. if (state->flrealloc && size>to_write) {
  44. tdata_t new_data;
  45. tsize_t newsize=state->size;
  46. while (newsize < (size + state->size)) {
  47. if (newsize > INT_MAX - 64*1024){
  48. return 0;
  49. }
  50. newsize += 64*1024;
  51. // newsize*=2; // UNDONE, by 64k chunks?
  52. }
  53. TRACE(("Reallocing in write to %d bytes\n", (int)newsize));
  54. /* malloc check ok, overflow checked above */
  55. new_data = realloc(state->data, newsize);
  56. if (!new_data) {
  57. // fail out
  58. return 0;
  59. }
  60. state->data = new_data;
  61. state->size = newsize;
  62. to_write = size;
  63. }
  64. TRACE(("to_write: %d\n", (int)to_write));
  65. _TIFFmemcpy((UINT8 *)state->data + state->loc, buf, to_write);
  66. state->loc += (toff_t)to_write;
  67. state->eof = max(state->loc, state->eof);
  68. dump_state(state);
  69. return to_write;
  70. }
  71. toff_t _tiffSeekProc(thandle_t hdata, toff_t off, int whence) {
  72. TIFFSTATE *state = (TIFFSTATE *)hdata;
  73. TRACE(("_tiffSeekProc: off: %u whence: %d \n", (uint)off, whence));
  74. dump_state(state);
  75. switch (whence) {
  76. case 0:
  77. state->loc = off;
  78. break;
  79. case 1:
  80. state->loc += off;
  81. break;
  82. case 2:
  83. state->loc = state->eof + off;
  84. break;
  85. }
  86. dump_state(state);
  87. return state->loc;
  88. }
  89. int _tiffCloseProc(thandle_t hdata) {
  90. TIFFSTATE *state = (TIFFSTATE *)hdata;
  91. TRACE(("_tiffCloseProc \n"));
  92. dump_state(state);
  93. return 0;
  94. }
  95. toff_t _tiffSizeProc(thandle_t hdata) {
  96. TIFFSTATE *state = (TIFFSTATE *)hdata;
  97. TRACE(("_tiffSizeProc \n"));
  98. dump_state(state);
  99. return (toff_t)state->size;
  100. }
  101. int _tiffMapProc(thandle_t hdata, tdata_t* pbase, toff_t* psize) {
  102. TIFFSTATE *state = (TIFFSTATE *)hdata;
  103. TRACE(("_tiffMapProc input size: %u, data: %p\n", (uint)*psize, *pbase));
  104. dump_state(state);
  105. *pbase = state->data;
  106. *psize = state->size;
  107. TRACE(("_tiffMapProc returning size: %u, data: %p\n", (uint)*psize, *pbase));
  108. return (1);
  109. }
  110. int _tiffNullMapProc(thandle_t hdata, tdata_t* pbase, toff_t* psize) {
  111. (void) hdata; (void) pbase; (void) psize;
  112. return (0);
  113. }
  114. void _tiffUnmapProc(thandle_t hdata, tdata_t base, toff_t size) {
  115. TRACE(("_tiffUnMapProc\n"));
  116. (void) hdata; (void) base; (void) size;
  117. }
  118. int ImagingLibTiffInit(ImagingCodecState state, int fp, uint32 offset) {
  119. TIFFSTATE *clientstate = (TIFFSTATE *)state->context;
  120. TRACE(("initing libtiff\n"));
  121. TRACE(("filepointer: %d \n", fp));
  122. TRACE(("State: count %d, state %d, x %d, y %d, ystep %d\n", state->count, state->state,
  123. state->x, state->y, state->ystep));
  124. TRACE(("State: xsize %d, ysize %d, xoff %d, yoff %d \n", state->xsize, state->ysize,
  125. state->xoff, state->yoff));
  126. TRACE(("State: bits %d, bytes %d \n", state->bits, state->bytes));
  127. TRACE(("State: context %p \n", state->context));
  128. clientstate->loc = 0;
  129. clientstate->size = 0;
  130. clientstate->data = 0;
  131. clientstate->fp = fp;
  132. clientstate->ifd = offset;
  133. clientstate->eof = 0;
  134. return 1;
  135. }
  136. int ReadTile(TIFF* tiff, UINT32 col, UINT32 row, UINT32* buffer) {
  137. uint16 photometric;
  138. TIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &photometric);
  139. // To avoid dealing with YCbCr subsampling, let libtiff handle it
  140. if (photometric == PHOTOMETRIC_YCBCR) {
  141. UINT32 tile_width, tile_height, swap_line_size, i_row;
  142. UINT32* swap_line;
  143. TIFFGetField(tiff, TIFFTAG_TILEWIDTH, &tile_width);
  144. TIFFGetField(tiff, TIFFTAG_TILELENGTH, &tile_height);
  145. swap_line_size = tile_width * sizeof(UINT32);
  146. if (tile_width != swap_line_size / sizeof(UINT32)) {
  147. return -1;
  148. }
  149. /* Read the tile into an RGBA array */
  150. if (!TIFFReadRGBATile(tiff, col, row, buffer)) {
  151. return -1;
  152. }
  153. swap_line = (UINT32*)malloc(swap_line_size);
  154. if (swap_line == NULL) {
  155. return -1;
  156. }
  157. /*
  158. * For some reason the TIFFReadRGBATile() function chooses the
  159. * lower left corner as the origin. Vertically mirror scanlines.
  160. */
  161. for(i_row = 0; i_row < tile_height / 2; i_row++) {
  162. UINT32 *top_line, *bottom_line;
  163. top_line = buffer + tile_width * i_row;
  164. bottom_line = buffer + tile_width * (tile_height - i_row - 1);
  165. memcpy(swap_line, top_line, 4*tile_width);
  166. memcpy(top_line, bottom_line, 4*tile_width);
  167. memcpy(bottom_line, swap_line, 4*tile_width);
  168. }
  169. free(swap_line);
  170. return 0;
  171. }
  172. if (TIFFReadTile(tiff, (tdata_t)buffer, col, row, 0, 0) == -1) {
  173. TRACE(("Decode Error, Tile at %dx%d\n", col, row));
  174. return -1;
  175. }
  176. TRACE(("Successfully read tile at %dx%d; \n\n", col, row));
  177. return 0;
  178. }
  179. int ReadStrip(TIFF* tiff, UINT32 row, UINT32* buffer) {
  180. uint16 photometric;
  181. TIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &photometric);
  182. // To avoid dealing with YCbCr subsampling, let libtiff handle it
  183. if (photometric == PHOTOMETRIC_YCBCR) {
  184. TIFFRGBAImage img;
  185. char emsg[1024] = "";
  186. UINT32 rows_per_strip, rows_to_read;
  187. int ok;
  188. TIFFGetFieldDefaulted(tiff, TIFFTAG_ROWSPERSTRIP, &rows_per_strip);
  189. if ((row % rows_per_strip) != 0) {
  190. TRACE(("Row passed to ReadStrip() must be first in a strip."));
  191. return -1;
  192. }
  193. if (TIFFRGBAImageOK(tiff, emsg) && TIFFRGBAImageBegin(&img, tiff, 0, emsg)) {
  194. TRACE(("Initialized RGBAImage\n"));
  195. img.req_orientation = ORIENTATION_TOPLEFT;
  196. img.row_offset = row;
  197. img.col_offset = 0;
  198. rows_to_read = min(rows_per_strip, img.height - row);
  199. TRACE(("rows to read: %d\n", rows_to_read));
  200. ok = TIFFRGBAImageGet(&img, buffer, img.width, rows_to_read);
  201. TIFFRGBAImageEnd(&img);
  202. } else {
  203. ok = 0;
  204. }
  205. if (ok == 0) {
  206. TRACE(("Decode Error, row %d; msg: %s\n", row, emsg));
  207. return -1;
  208. }
  209. return 0;
  210. }
  211. if (TIFFReadEncodedStrip(tiff, TIFFComputeStrip(tiff, row, 0), (tdata_t)buffer, -1) == -1) {
  212. TRACE(("Decode Error, strip %d\n", TIFFComputeStrip(tiff, row, 0)));
  213. return -1;
  214. }
  215. return 0;
  216. }
  217. int ImagingLibTiffDecode(Imaging im, ImagingCodecState state, UINT8* buffer, Py_ssize_t bytes) {
  218. TIFFSTATE *clientstate = (TIFFSTATE *)state->context;
  219. char *filename = "tempfile.tif";
  220. char *mode = "r";
  221. TIFF *tiff;
  222. /* buffer is the encoded file, bytes is the length of the encoded file */
  223. /* it all ends up in state->buffer, which is a uint8* from Imaging.h */
  224. TRACE(("in decoder: bytes %d\n", bytes));
  225. TRACE(("State: count %d, state %d, x %d, y %d, ystep %d\n", state->count, state->state,
  226. state->x, state->y, state->ystep));
  227. TRACE(("State: xsize %d, ysize %d, xoff %d, yoff %d \n", state->xsize, state->ysize,
  228. state->xoff, state->yoff));
  229. TRACE(("State: bits %d, bytes %d \n", state->bits, state->bytes));
  230. TRACE(("Buffer: %p: %c%c%c%c\n", buffer, (char)buffer[0], (char)buffer[1],(char)buffer[2], (char)buffer[3]));
  231. TRACE(("State->Buffer: %c%c%c%c\n", (char)state->buffer[0], (char)state->buffer[1],(char)state->buffer[2], (char)state->buffer[3]));
  232. TRACE(("Image: mode %s, type %d, bands: %d, xsize %d, ysize %d \n",
  233. im->mode, im->type, im->bands, im->xsize, im->ysize));
  234. TRACE(("Image: image8 %p, image32 %p, image %p, block %p \n",
  235. im->image8, im->image32, im->image, im->block));
  236. TRACE(("Image: pixelsize: %d, linesize %d \n",
  237. im->pixelsize, im->linesize));
  238. dump_state(clientstate);
  239. clientstate->size = bytes;
  240. clientstate->eof = clientstate->size;
  241. clientstate->loc = 0;
  242. clientstate->data = (tdata_t)buffer;
  243. clientstate->flrealloc = 0;
  244. dump_state(clientstate);
  245. TIFFSetWarningHandler(NULL);
  246. TIFFSetWarningHandlerExt(NULL);
  247. if (clientstate->fp) {
  248. TRACE(("Opening using fd: %d\n",clientstate->fp));
  249. lseek(clientstate->fp,0,SEEK_SET); // Sometimes, I get it set to the end.
  250. tiff = TIFFFdOpen(clientstate->fp, filename, mode);
  251. } else {
  252. TRACE(("Opening from string\n"));
  253. tiff = TIFFClientOpen(filename, mode,
  254. (thandle_t) clientstate,
  255. _tiffReadProc, _tiffWriteProc,
  256. _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
  257. _tiffMapProc, _tiffUnmapProc);
  258. }
  259. if (!tiff){
  260. TRACE(("Error, didn't get the tiff\n"));
  261. state->errcode = IMAGING_CODEC_BROKEN;
  262. return -1;
  263. }
  264. if (clientstate->ifd){
  265. int rv;
  266. uint32 ifdoffset = clientstate->ifd;
  267. TRACE(("reading tiff ifd %u\n", ifdoffset));
  268. rv = TIFFSetSubDirectory(tiff, ifdoffset);
  269. if (!rv){
  270. TRACE(("error in TIFFSetSubDirectory"));
  271. return -1;
  272. }
  273. }
  274. if (TIFFIsTiled(tiff)) {
  275. UINT32 x, y, tile_y, row_byte_size;
  276. UINT32 tile_width, tile_length, current_tile_width;
  277. UINT8 *new_data;
  278. TIFFGetField(tiff, TIFFTAG_TILEWIDTH, &tile_width);
  279. TIFFGetField(tiff, TIFFTAG_TILELENGTH, &tile_length);
  280. // We could use TIFFTileSize, but for YCbCr data it returns subsampled data size
  281. row_byte_size = (tile_width * state->bits + 7) / 8;
  282. /* overflow check for realloc */
  283. if (INT_MAX / row_byte_size < tile_length) {
  284. state->errcode = IMAGING_CODEC_MEMORY;
  285. TIFFClose(tiff);
  286. return -1;
  287. }
  288. state->bytes = row_byte_size * tile_length;
  289. /* realloc to fit whole tile */
  290. /* malloc check above */
  291. new_data = realloc (state->buffer, state->bytes);
  292. if (!new_data) {
  293. state->errcode = IMAGING_CODEC_MEMORY;
  294. TIFFClose(tiff);
  295. return -1;
  296. }
  297. state->buffer = new_data;
  298. TRACE(("TIFFTileSize: %d\n", state->bytes));
  299. for (y = state->yoff; y < state->ysize; y += tile_length) {
  300. for (x = state->xoff; x < state->xsize; x += tile_width) {
  301. if (ReadTile(tiff, x, y, (UINT32*) state->buffer) == -1) {
  302. TRACE(("Decode Error, Tile at %dx%d\n", x, y));
  303. state->errcode = IMAGING_CODEC_BROKEN;
  304. TIFFClose(tiff);
  305. return -1;
  306. }
  307. TRACE(("Read tile at %dx%d; \n\n", x, y));
  308. current_tile_width = min(tile_width, state->xsize - x);
  309. // iterate over each line in the tile and stuff data into image
  310. for (tile_y = 0; tile_y < min(tile_length, state->ysize - y); tile_y++) {
  311. TRACE(("Writing tile data at %dx%d using tile_width: %d; \n", tile_y + y, x, current_tile_width));
  312. // UINT8 * bbb = state->buffer + tile_y * row_byte_size;
  313. // TRACE(("chars: %x%x%x%x\n", ((UINT8 *)bbb)[0], ((UINT8 *)bbb)[1], ((UINT8 *)bbb)[2], ((UINT8 *)bbb)[3]));
  314. state->shuffle((UINT8*) im->image[tile_y + y] + x * im->pixelsize,
  315. state->buffer + tile_y * row_byte_size,
  316. current_tile_width
  317. );
  318. }
  319. }
  320. }
  321. } else {
  322. UINT32 strip_row, row_byte_size;
  323. UINT8 *new_data;
  324. UINT32 rows_per_strip;
  325. int ret;
  326. ret = TIFFGetField(tiff, TIFFTAG_ROWSPERSTRIP, &rows_per_strip);
  327. if (ret != 1) {
  328. rows_per_strip = state->ysize;
  329. }
  330. TRACE(("RowsPerStrip: %u \n", rows_per_strip));
  331. // We could use TIFFStripSize, but for YCbCr data it returns subsampled data size
  332. row_byte_size = (state->xsize * state->bits + 7) / 8;
  333. /* overflow check for realloc */
  334. if (INT_MAX / row_byte_size < rows_per_strip) {
  335. state->errcode = IMAGING_CODEC_MEMORY;
  336. TIFFClose(tiff);
  337. return -1;
  338. }
  339. state->bytes = rows_per_strip * row_byte_size;
  340. TRACE(("StripSize: %d \n", state->bytes));
  341. /* realloc to fit whole strip */
  342. /* malloc check above */
  343. new_data = realloc (state->buffer, state->bytes);
  344. if (!new_data) {
  345. state->errcode = IMAGING_CODEC_MEMORY;
  346. TIFFClose(tiff);
  347. return -1;
  348. }
  349. state->buffer = new_data;
  350. for (; state->y < state->ysize; state->y += rows_per_strip) {
  351. if (ReadStrip(tiff, state->y, (UINT32 *)state->buffer) == -1) {
  352. TRACE(("Decode Error, strip %d\n", TIFFComputeStrip(tiff, state->y, 0)));
  353. state->errcode = IMAGING_CODEC_BROKEN;
  354. TIFFClose(tiff);
  355. return -1;
  356. }
  357. TRACE(("Decoded strip for row %d \n", state->y));
  358. // iterate over each row in the strip and stuff data into image
  359. for (strip_row = 0; strip_row < min(rows_per_strip, state->ysize - state->y); strip_row++) {
  360. TRACE(("Writing data into line %d ; \n", state->y + strip_row));
  361. // UINT8 * bbb = state->buffer + strip_row * (state->bytes / rows_per_strip);
  362. // TRACE(("chars: %x %x %x %x\n", ((UINT8 *)bbb)[0], ((UINT8 *)bbb)[1], ((UINT8 *)bbb)[2], ((UINT8 *)bbb)[3]));
  363. state->shuffle((UINT8*) im->image[state->y + state->yoff + strip_row] +
  364. state->xoff * im->pixelsize,
  365. state->buffer + strip_row * row_byte_size,
  366. state->xsize);
  367. }
  368. }
  369. }
  370. TIFFClose(tiff);
  371. TRACE(("Done Decoding, Returning \n"));
  372. // Returning -1 here to force ImageFile.load to break, rather than
  373. // even think about looping back around.
  374. return -1;
  375. }
  376. int ImagingLibTiffEncodeInit(ImagingCodecState state, char *filename, int fp) {
  377. // Open the FD or the pointer as a tiff file, for writing.
  378. // We may have to do some monkeying around to make this really work.
  379. // If we have a fp, then we're good.
  380. // If we have a memory string, we're probably going to have to malloc, then
  381. // shuffle bytes into the writescanline process.
  382. // Going to have to deal with the directory as well.
  383. TIFFSTATE *clientstate = (TIFFSTATE *)state->context;
  384. int bufsize = 64*1024;
  385. char *mode = "w";
  386. TRACE(("initing libtiff\n"));
  387. TRACE(("Filename %s, filepointer: %d \n", filename, fp));
  388. TRACE(("State: count %d, state %d, x %d, y %d, ystep %d\n", state->count, state->state,
  389. state->x, state->y, state->ystep));
  390. TRACE(("State: xsize %d, ysize %d, xoff %d, yoff %d \n", state->xsize, state->ysize,
  391. state->xoff, state->yoff));
  392. TRACE(("State: bits %d, bytes %d \n", state->bits, state->bytes));
  393. TRACE(("State: context %p \n", state->context));
  394. clientstate->loc = 0;
  395. clientstate->size = 0;
  396. clientstate->eof =0;
  397. clientstate->data = 0;
  398. clientstate->flrealloc = 0;
  399. clientstate->fp = fp;
  400. state->state = 0;
  401. if (fp) {
  402. TRACE(("Opening using fd: %d for writing \n",clientstate->fp));
  403. clientstate->tiff = TIFFFdOpen(clientstate->fp, filename, mode);
  404. } else {
  405. // malloc a buffer to write the tif, we're going to need to realloc or something if we need bigger.
  406. TRACE(("Opening a buffer for writing \n"));
  407. /* malloc check ok, small constant allocation */
  408. clientstate->data = malloc(bufsize);
  409. clientstate->size = bufsize;
  410. clientstate->flrealloc=1;
  411. if (!clientstate->data) {
  412. TRACE(("Error, couldn't allocate a buffer of size %d\n", bufsize));
  413. return 0;
  414. }
  415. clientstate->tiff = TIFFClientOpen(filename, mode,
  416. (thandle_t) clientstate,
  417. _tiffReadProc, _tiffWriteProc,
  418. _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
  419. _tiffNullMapProc, _tiffUnmapProc); /*force no mmap*/
  420. }
  421. if (!clientstate->tiff) {
  422. TRACE(("Error, couldn't open tiff file\n"));
  423. return 0;
  424. }
  425. return 1;
  426. }
  427. int ImagingLibTiffMergeFieldInfo(ImagingCodecState state, TIFFDataType field_type, int key, int is_var_length){
  428. // Refer to libtiff docs (http://www.simplesystems.org/libtiff/addingtags.html)
  429. TIFFSTATE *clientstate = (TIFFSTATE *)state->context;
  430. char field_name[10];
  431. uint32 n;
  432. int status = 0;
  433. // custom fields added with ImagingLibTiffMergeFieldInfo are only used for
  434. // decoding, ignore readcount;
  435. int readcount = 0;
  436. // we support writing a single value, or a variable number of values
  437. int writecount = 1;
  438. // whether the first value should encode the number of values.
  439. int passcount = 0;
  440. TIFFFieldInfo info[] = {
  441. { key, readcount, writecount, field_type, FIELD_CUSTOM, 1, passcount, field_name }
  442. };
  443. if (is_var_length) {
  444. info[0].field_writecount = -1;
  445. }
  446. if (is_var_length && field_type != TIFF_ASCII) {
  447. info[0].field_passcount = 1;
  448. }
  449. n = sizeof(info) / sizeof(info[0]);
  450. // Test for libtiff 4.0 or later, excluding libtiff 3.9.6 and 3.9.7
  451. #if TIFFLIB_VERSION >= 20111221 && TIFFLIB_VERSION != 20120218 && TIFFLIB_VERSION != 20120922
  452. status = TIFFMergeFieldInfo(clientstate->tiff, info, n);
  453. #else
  454. TIFFMergeFieldInfo(clientstate->tiff, info, n);
  455. #endif
  456. return status;
  457. }
  458. int ImagingLibTiffSetField(ImagingCodecState state, ttag_t tag, ...){
  459. // after tif_dir.c->TIFFSetField.
  460. TIFFSTATE *clientstate = (TIFFSTATE *)state->context;
  461. va_list ap;
  462. int status;
  463. va_start(ap, tag);
  464. status = TIFFVSetField(clientstate->tiff, tag, ap);
  465. va_end(ap);
  466. return status;
  467. }
  468. int ImagingLibTiffEncode(Imaging im, ImagingCodecState state, UINT8* buffer, int bytes) {
  469. /* One shot encoder. Encode everything to the tiff in the clientstate.
  470. If we're running off of a FD, then run once, we're good, everything
  471. ends up in the file, we close and we're done.
  472. If we're going to memory, then we need to write the whole file into memory, then
  473. parcel it back out to the pystring buffer bytes at a time.
  474. */
  475. TIFFSTATE *clientstate = (TIFFSTATE *)state->context;
  476. TIFF *tiff = clientstate->tiff;
  477. TRACE(("in encoder: bytes %d\n", bytes));
  478. TRACE(("State: count %d, state %d, x %d, y %d, ystep %d\n", state->count, state->state,
  479. state->x, state->y, state->ystep));
  480. TRACE(("State: xsize %d, ysize %d, xoff %d, yoff %d \n", state->xsize, state->ysize,
  481. state->xoff, state->yoff));
  482. TRACE(("State: bits %d, bytes %d \n", state->bits, state->bytes));
  483. TRACE(("Buffer: %p: %c%c%c%c\n", buffer, (char)buffer[0], (char)buffer[1],(char)buffer[2], (char)buffer[3]));
  484. TRACE(("State->Buffer: %c%c%c%c\n", (char)state->buffer[0], (char)state->buffer[1],(char)state->buffer[2], (char)state->buffer[3]));
  485. TRACE(("Image: mode %s, type %d, bands: %d, xsize %d, ysize %d \n",
  486. im->mode, im->type, im->bands, im->xsize, im->ysize));
  487. TRACE(("Image: image8 %p, image32 %p, image %p, block %p \n",
  488. im->image8, im->image32, im->image, im->block));
  489. TRACE(("Image: pixelsize: %d, linesize %d \n",
  490. im->pixelsize, im->linesize));
  491. dump_state(clientstate);
  492. if (state->state == 0) {
  493. TRACE(("Encoding line bt line"));
  494. while(state->y < state->ysize){
  495. state->shuffle(state->buffer,
  496. (UINT8*) im->image[state->y + state->yoff] +
  497. state->xoff * im->pixelsize,
  498. state->xsize);
  499. if (TIFFWriteScanline(tiff, (tdata_t)(state->buffer), (uint32)state->y, 0) == -1) {
  500. TRACE(("Encode Error, row %d\n", state->y));
  501. state->errcode = IMAGING_CODEC_BROKEN;
  502. TIFFClose(tiff);
  503. if (!clientstate->fp){
  504. free(clientstate->data);
  505. }
  506. return -1;
  507. }
  508. state->y++;
  509. }
  510. if (state->y == state->ysize) {
  511. state->state=1;
  512. TRACE(("Flushing \n"));
  513. if (!TIFFFlush(tiff)) {
  514. TRACE(("Error flushing the tiff"));
  515. // likely reason is memory.
  516. state->errcode = IMAGING_CODEC_MEMORY;
  517. TIFFClose(tiff);
  518. if (!clientstate->fp){
  519. free(clientstate->data);
  520. }
  521. return -1;
  522. }
  523. TRACE(("Closing \n"));
  524. TIFFClose(tiff);
  525. // reset the clientstate metadata to use it to read out the buffer.
  526. clientstate->loc = 0;
  527. clientstate->size = clientstate->eof; // redundant?
  528. }
  529. }
  530. if (state->state == 1 && !clientstate->fp) {
  531. int read = (int)_tiffReadProc(clientstate, (tdata_t)buffer, (tsize_t)bytes);
  532. TRACE(("Buffer: %p: %c%c%c%c\n", buffer, (char)buffer[0], (char)buffer[1],(char)buffer[2], (char)buffer[3]));
  533. if (clientstate->loc == clientstate->eof) {
  534. TRACE(("Hit EOF, calling an end, freeing data"));
  535. state->errcode = IMAGING_CODEC_END;
  536. free(clientstate->data);
  537. }
  538. return read;
  539. }
  540. state->errcode = IMAGING_CODEC_END;
  541. return 0;
  542. }
  543. const char*
  544. ImagingTiffVersion(void)
  545. {
  546. return TIFFGetVersion();
  547. }
  548. #endif