2013-03-09 07:51:10 +04:00
|
|
|
/*
|
|
|
|
* The Python Imaging Library.
|
|
|
|
* $Id: //modules/pil/libImaging/TiffDecode.c#1 $
|
|
|
|
*
|
|
|
|
* LibTiff-based Group3 and Group4 decoder
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* started modding to use non-private tiff functions to port to libtiff 4.x
|
|
|
|
* eds 3/12/12
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Imaging.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBTIFF
|
|
|
|
|
|
|
|
#ifndef uint
|
|
|
|
#define uint uint32
|
|
|
|
#endif
|
|
|
|
|
2013-03-14 05:37:42 +04:00
|
|
|
#include "TiffDecode.h"
|
2013-03-09 07:51:10 +04:00
|
|
|
|
2020-08-30 05:30:47 +03:00
|
|
|
/* Convert C file descriptor to WinApi HFILE if LibTiff was compiled with tif_win32.c
|
|
|
|
*
|
|
|
|
* This cast is safe, as the top 32-bits of HFILE are guaranteed to be zero,
|
2021-01-03 06:17:51 +03:00
|
|
|
* see
|
|
|
|
* https://docs.microsoft.com/en-us/windows/win32/winprog64/interprocess-communication
|
2020-08-30 05:30:47 +03:00
|
|
|
*/
|
|
|
|
#ifndef USE_WIN32_FILEIO
|
|
|
|
#define fd_to_tiff_fd(fd) (fd)
|
|
|
|
#else
|
|
|
|
#define fd_to_tiff_fd(fd) ((int)_get_osfhandle(fd))
|
|
|
|
#endif
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
void
|
|
|
|
dump_state(const TIFFSTATE *state) {
|
|
|
|
TRACE(
|
|
|
|
("State: Location %u size %d eof %d data: %p ifd: %d\n",
|
|
|
|
(uint)state->loc,
|
|
|
|
(int)state->size,
|
|
|
|
(uint)state->eof,
|
|
|
|
state->data,
|
|
|
|
state->ifd));
|
2013-03-09 07:51:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
procs for TIFFOpenClient
|
|
|
|
*/
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
tsize_t
|
|
|
|
_tiffReadProc(thandle_t hdata, tdata_t buf, tsize_t size) {
|
2018-10-19 21:42:40 +03:00
|
|
|
TIFFSTATE *state = (TIFFSTATE *)hdata;
|
|
|
|
tsize_t to_read;
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2018-10-19 21:42:40 +03:00
|
|
|
TRACE(("_tiffReadProc: %d \n", (int)size));
|
|
|
|
dump_state(state);
|
2013-03-09 07:51:10 +04:00
|
|
|
|
2021-01-08 20:45:42 +03:00
|
|
|
if (state->loc > state->eof) {
|
2021-06-11 11:26:00 +03:00
|
|
|
TIFFError("_tiffReadProc", "Invalid Read at loc %" PRIu64 ", eof: %" PRIu64, state->loc, state->eof);
|
2021-01-08 20:45:42 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2018-10-19 21:42:40 +03:00
|
|
|
to_read = min(size, min(state->size, (tsize_t)state->eof) - (tsize_t)state->loc);
|
|
|
|
TRACE(("to_read: %d\n", (int)to_read));
|
2013-03-09 07:51:10 +04:00
|
|
|
|
2018-10-19 21:42:40 +03:00
|
|
|
_TIFFmemcpy(buf, (UINT8 *)state->data + state->loc, to_read);
|
|
|
|
state->loc += (toff_t)to_read;
|
2013-03-09 07:51:10 +04:00
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
TRACE(("location: %u\n", (uint)state->loc));
|
2018-10-19 21:42:40 +03:00
|
|
|
return to_read;
|
2013-03-09 07:51:10 +04:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
tsize_t
|
|
|
|
_tiffWriteProc(thandle_t hdata, tdata_t buf, tsize_t size) {
|
2018-10-19 21:42:40 +03:00
|
|
|
TIFFSTATE *state = (TIFFSTATE *)hdata;
|
|
|
|
tsize_t to_write;
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2018-10-19 21:42:40 +03:00
|
|
|
TRACE(("_tiffWriteProc: %d \n", (int)size));
|
|
|
|
dump_state(state);
|
2013-03-09 07:51:10 +04:00
|
|
|
|
2018-10-19 21:42:40 +03:00
|
|
|
to_write = min(size, state->size - (tsize_t)state->loc);
|
2021-01-03 06:17:51 +03:00
|
|
|
if (state->flrealloc && size > to_write) {
|
2018-10-19 21:42:40 +03:00
|
|
|
tdata_t new_data;
|
2021-01-03 06:17:51 +03:00
|
|
|
tsize_t newsize = state->size;
|
2018-10-19 21:42:40 +03:00
|
|
|
while (newsize < (size + state->size)) {
|
2021-01-03 06:17:51 +03:00
|
|
|
if (newsize > INT_MAX - 64 * 1024) {
|
2016-03-16 19:01:25 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2021-01-03 06:17:51 +03:00
|
|
|
newsize += 64 * 1024;
|
2018-10-19 21:42:40 +03:00
|
|
|
// newsize*=2; // UNDONE, by 64k chunks?
|
|
|
|
}
|
|
|
|
TRACE(("Reallocing in write to %d bytes\n", (int)newsize));
|
2016-03-16 19:01:25 +03:00
|
|
|
/* malloc check ok, overflow checked above */
|
2018-10-19 21:42:40 +03:00
|
|
|
new_data = realloc(state->data, newsize);
|
|
|
|
if (!new_data) {
|
|
|
|
// fail out
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
state->data = new_data;
|
|
|
|
state->size = newsize;
|
|
|
|
to_write = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE(("to_write: %d\n", (int)to_write));
|
|
|
|
|
|
|
|
_TIFFmemcpy((UINT8 *)state->data + state->loc, buf, to_write);
|
|
|
|
state->loc += (toff_t)to_write;
|
|
|
|
state->eof = max(state->loc, state->eof);
|
|
|
|
|
|
|
|
dump_state(state);
|
|
|
|
return to_write;
|
2013-03-09 07:51:10 +04:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
toff_t
|
|
|
|
_tiffSeekProc(thandle_t hdata, toff_t off, int whence) {
|
2018-10-19 21:42:40 +03:00
|
|
|
TIFFSTATE *state = (TIFFSTATE *)hdata;
|
|
|
|
|
|
|
|
TRACE(("_tiffSeekProc: off: %u whence: %d \n", (uint)off, whence));
|
|
|
|
dump_state(state);
|
|
|
|
switch (whence) {
|
2021-01-03 06:17:51 +03:00
|
|
|
case 0:
|
|
|
|
state->loc = off;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
state->loc += off;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
state->loc = state->eof + off;
|
|
|
|
break;
|
2018-10-19 21:42:40 +03:00
|
|
|
}
|
|
|
|
dump_state(state);
|
|
|
|
return state->loc;
|
2013-03-09 07:51:10 +04:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
int
|
|
|
|
_tiffCloseProc(thandle_t hdata) {
|
2018-10-19 21:42:40 +03:00
|
|
|
TIFFSTATE *state = (TIFFSTATE *)hdata;
|
2013-03-09 07:51:10 +04:00
|
|
|
|
2018-10-19 21:42:40 +03:00
|
|
|
TRACE(("_tiffCloseProc \n"));
|
|
|
|
dump_state(state);
|
2013-03-09 07:51:10 +04:00
|
|
|
|
2018-10-19 21:42:40 +03:00
|
|
|
return 0;
|
2013-03-09 07:51:10 +04:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
toff_t
|
|
|
|
_tiffSizeProc(thandle_t hdata) {
|
2018-10-19 21:42:40 +03:00
|
|
|
TIFFSTATE *state = (TIFFSTATE *)hdata;
|
2013-03-09 07:51:10 +04:00
|
|
|
|
2018-10-19 21:42:40 +03:00
|
|
|
TRACE(("_tiffSizeProc \n"));
|
|
|
|
dump_state(state);
|
2013-03-09 07:51:10 +04:00
|
|
|
|
2018-10-19 21:42:40 +03:00
|
|
|
return (toff_t)state->size;
|
2013-03-09 07:51:10 +04:00
|
|
|
}
|
2018-12-09 08:04:34 +03:00
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
int
|
|
|
|
_tiffMapProc(thandle_t hdata, tdata_t *pbase, toff_t *psize) {
|
2018-10-19 21:42:40 +03:00
|
|
|
TIFFSTATE *state = (TIFFSTATE *)hdata;
|
2013-03-09 07:51:10 +04:00
|
|
|
|
2018-10-19 21:42:40 +03:00
|
|
|
TRACE(("_tiffMapProc input size: %u, data: %p\n", (uint)*psize, *pbase));
|
|
|
|
dump_state(state);
|
2013-03-09 07:51:10 +04:00
|
|
|
|
2018-10-19 21:42:40 +03:00
|
|
|
*pbase = state->data;
|
|
|
|
*psize = state->size;
|
|
|
|
TRACE(("_tiffMapProc returning size: %u, data: %p\n", (uint)*psize, *pbase));
|
|
|
|
return (1);
|
2013-03-09 07:51:10 +04:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
int
|
|
|
|
_tiffNullMapProc(thandle_t hdata, tdata_t *pbase, toff_t *psize) {
|
|
|
|
(void)hdata;
|
|
|
|
(void)pbase;
|
|
|
|
(void)psize;
|
2018-10-19 21:42:40 +03:00
|
|
|
return (0);
|
2013-03-09 07:51:10 +04:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
void
|
|
|
|
_tiffUnmapProc(thandle_t hdata, tdata_t base, toff_t size) {
|
2018-10-19 21:42:40 +03:00
|
|
|
TRACE(("_tiffUnMapProc\n"));
|
2021-01-03 06:17:51 +03:00
|
|
|
(void)hdata;
|
|
|
|
(void)base;
|
|
|
|
(void)size;
|
2013-03-09 07:51:10 +04:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
int
|
2021-04-30 17:51:39 +03:00
|
|
|
ImagingLibTiffInit(ImagingCodecState state, int fp, uint32_t offset) {
|
2018-10-19 21:42:40 +03:00
|
|
|
TIFFSTATE *clientstate = (TIFFSTATE *)state->context;
|
2013-03-09 07:51:10 +04:00
|
|
|
|
|
|
|
TRACE(("initing libtiff\n"));
|
2021-01-03 06:17:51 +03:00
|
|
|
TRACE(("filepointer: %d \n", fp));
|
|
|
|
TRACE(
|
|
|
|
("State: count %d, state %d, x %d, y %d, ystep %d\n",
|
|
|
|
state->count,
|
|
|
|
state->state,
|
|
|
|
state->x,
|
|
|
|
state->y,
|
|
|
|
state->ystep));
|
|
|
|
TRACE(
|
|
|
|
("State: xsize %d, ysize %d, xoff %d, yoff %d \n",
|
|
|
|
state->xsize,
|
|
|
|
state->ysize,
|
|
|
|
state->xoff,
|
|
|
|
state->yoff));
|
2018-10-19 21:42:40 +03:00
|
|
|
TRACE(("State: bits %d, bytes %d \n", state->bits, state->bytes));
|
|
|
|
TRACE(("State: context %p \n", state->context));
|
|
|
|
|
|
|
|
clientstate->loc = 0;
|
|
|
|
clientstate->size = 0;
|
|
|
|
clientstate->data = 0;
|
|
|
|
clientstate->fp = fp;
|
2014-08-21 22:02:01 +04:00
|
|
|
clientstate->ifd = offset;
|
2018-10-19 21:42:40 +03:00
|
|
|
clientstate->eof = 0;
|
2013-03-09 07:51:10 +04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
int
|
2021-04-30 17:51:39 +03:00
|
|
|
_pickUnpackers(Imaging im, ImagingCodecState state, TIFF *tiff, uint16_t planarconfig, ImagingShuffler *unpackers) {
|
2021-01-14 05:33:49 +03:00
|
|
|
// if number of bands is 1, there is no difference with contig case
|
|
|
|
if (planarconfig == PLANARCONFIG_SEPARATE && im->bands > 1) {
|
2021-04-30 17:51:39 +03:00
|
|
|
uint16_t bits_per_sample = 8;
|
2021-01-14 05:33:49 +03:00
|
|
|
|
|
|
|
TIFFGetFieldDefaulted(tiff, TIFFTAG_BITSPERSAMPLE, &bits_per_sample);
|
|
|
|
if (bits_per_sample != 8 && bits_per_sample != 16) {
|
|
|
|
TRACE(("Invalid value for bits per sample: %d\n", bits_per_sample));
|
|
|
|
state->errcode = IMAGING_CODEC_BROKEN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll pick appropriate set of unpackers depending on planar_configuration
|
|
|
|
// It does not matter if data is RGB(A), CMYK or LUV really,
|
|
|
|
// we just copy it plane by plane
|
|
|
|
unpackers[0] = ImagingFindUnpacker("RGBA", bits_per_sample == 16 ? "R;16N" : "R", NULL);
|
|
|
|
unpackers[1] = ImagingFindUnpacker("RGBA", bits_per_sample == 16 ? "G;16N" : "G", NULL);
|
|
|
|
unpackers[2] = ImagingFindUnpacker("RGBA", bits_per_sample == 16 ? "B;16N" : "B", NULL);
|
|
|
|
unpackers[3] = ImagingFindUnpacker("RGBA", bits_per_sample == 16 ? "A;16N" : "A", NULL);
|
|
|
|
|
|
|
|
return im->bands;
|
|
|
|
} else {
|
|
|
|
unpackers[0] = state->shuffle;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
_decodeAsRGBA(Imaging im, ImagingCodecState state, TIFF *tiff) {
|
|
|
|
// To avoid dealing with YCbCr subsampling and other complications, let libtiff handle it
|
2020-11-01 17:16:38 +03:00
|
|
|
// Use a TIFFRGBAImage wrapping the tiff image, and let libtiff handle
|
|
|
|
// all of the conversion. Metadata read from the TIFFRGBAImage could
|
2021-01-03 06:17:51 +03:00
|
|
|
// be different from the metadata that the base tiff returns.
|
2020-11-01 17:16:38 +03:00
|
|
|
|
2021-01-12 10:28:58 +03:00
|
|
|
INT32 current_row;
|
2020-11-01 17:16:38 +03:00
|
|
|
UINT8 *new_data;
|
2021-01-12 10:28:58 +03:00
|
|
|
UINT32 rows_per_block, row_byte_size, rows_to_read;
|
2020-11-01 17:16:38 +03:00
|
|
|
int ret;
|
|
|
|
TIFFRGBAImage img;
|
|
|
|
char emsg[1024] = "";
|
|
|
|
|
2021-01-12 10:28:58 +03:00
|
|
|
// Since using TIFFRGBAImage* functions, we can read whole tiff into rastrr in one call
|
|
|
|
// Let's select smaller block size. Multiplying image width by (tile length OR rows per strip)
|
|
|
|
// gives us manageable block size in pixels
|
|
|
|
if (TIFFIsTiled(tiff)) {
|
|
|
|
ret = TIFFGetFieldDefaulted(tiff, TIFFTAG_TILELENGTH, &rows_per_block);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret = TIFFGetFieldDefaulted(tiff, TIFFTAG_ROWSPERSTRIP, &rows_per_block);
|
|
|
|
}
|
|
|
|
|
2021-03-31 22:04:59 +03:00
|
|
|
if (ret != 1 || rows_per_block==(UINT32)(-1)) {
|
2021-01-12 10:28:58 +03:00
|
|
|
rows_per_block = state->ysize;
|
2020-11-01 17:16:38 +03:00
|
|
|
}
|
2021-01-12 10:28:58 +03:00
|
|
|
|
|
|
|
TRACE(("RowsPerBlock: %u \n", rows_per_block));
|
2018-12-09 08:04:34 +03:00
|
|
|
|
2020-11-01 17:16:38 +03:00
|
|
|
if (!(TIFFRGBAImageOK(tiff, emsg) && TIFFRGBAImageBegin(&img, tiff, 0, emsg))) {
|
|
|
|
TRACE(("Decode error, msg: %s", emsg));
|
|
|
|
state->errcode = IMAGING_CODEC_BROKEN;
|
2020-11-01 19:25:31 +03:00
|
|
|
// nothing to clean up, just return
|
2020-11-01 17:16:38 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2018-12-09 08:04:34 +03:00
|
|
|
|
2020-11-01 17:16:38 +03:00
|
|
|
img.req_orientation = ORIENTATION_TOPLEFT;
|
|
|
|
img.col_offset = 0;
|
2018-12-09 08:04:34 +03:00
|
|
|
|
2020-11-01 17:16:38 +03:00
|
|
|
/* overflow check for row byte size */
|
|
|
|
if (INT_MAX / 4 < img.width) {
|
|
|
|
state->errcode = IMAGING_CODEC_MEMORY;
|
2021-01-14 05:33:49 +03:00
|
|
|
goto decodergba_err;
|
2021-01-03 06:17:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TiffRGBAImages are 32bits/pixel.
|
2020-11-01 17:16:38 +03:00
|
|
|
row_byte_size = img.width * 4;
|
|
|
|
|
|
|
|
/* overflow check for realloc */
|
2021-01-12 10:28:58 +03:00
|
|
|
if (INT_MAX / row_byte_size < rows_per_block) {
|
2020-11-01 17:16:38 +03:00
|
|
|
state->errcode = IMAGING_CODEC_MEMORY;
|
2021-01-14 05:33:49 +03:00
|
|
|
goto decodergba_err;
|
2020-11-01 17:16:38 +03:00
|
|
|
}
|
2018-12-09 08:04:34 +03:00
|
|
|
|
2021-01-12 10:28:58 +03:00
|
|
|
state->bytes = rows_per_block * row_byte_size;
|
2018-12-09 08:04:34 +03:00
|
|
|
|
2021-01-12 10:28:58 +03:00
|
|
|
TRACE(("BlockSize: %d \n", state->bytes));
|
2018-12-09 08:04:34 +03:00
|
|
|
|
2020-11-01 17:16:38 +03:00
|
|
|
/* realloc to fit whole strip */
|
|
|
|
/* malloc check above */
|
2021-01-03 06:17:51 +03:00
|
|
|
new_data = realloc(state->buffer, state->bytes);
|
2020-11-01 17:16:38 +03:00
|
|
|
if (!new_data) {
|
|
|
|
state->errcode = IMAGING_CODEC_MEMORY;
|
2021-01-14 05:33:49 +03:00
|
|
|
goto decodergba_err;
|
2020-11-01 17:16:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
state->buffer = new_data;
|
|
|
|
|
2021-01-12 10:28:58 +03:00
|
|
|
for (; state->y < state->ysize; state->y += rows_per_block) {
|
2021-01-03 06:17:51 +03:00
|
|
|
img.row_offset = state->y;
|
2021-01-12 10:28:58 +03:00
|
|
|
rows_to_read = min(rows_per_block, img.height - state->y);
|
2020-11-01 17:16:38 +03:00
|
|
|
|
2021-01-03 23:35:32 +03:00
|
|
|
if (!TIFFRGBAImageGet(&img, (UINT32 *)state->buffer, img.width, rows_to_read)) {
|
2021-01-03 06:17:51 +03:00
|
|
|
TRACE(("Decode Error, y: %d\n", state->y));
|
2020-11-01 17:16:38 +03:00
|
|
|
state->errcode = IMAGING_CODEC_BROKEN;
|
2021-01-14 05:33:49 +03:00
|
|
|
goto decodergba_err;
|
2018-12-09 08:04:34 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:06:49 +03:00
|
|
|
#if WORDS_BIGENDIAN
|
|
|
|
TIFFSwabArrayOfLong((UINT32 *)state->buffer, img.width * rows_to_read);
|
|
|
|
#endif
|
|
|
|
|
2020-11-01 17:16:38 +03:00
|
|
|
TRACE(("Decoded strip for row %d \n", state->y));
|
|
|
|
|
|
|
|
// iterate over each row in the strip and stuff data into image
|
2021-01-12 10:28:58 +03:00
|
|
|
for (current_row = 0;
|
|
|
|
current_row < min((INT32)rows_per_block, state->ysize - state->y);
|
|
|
|
current_row++) {
|
|
|
|
TRACE(("Writing data into line %d ; \n", state->y + current_row));
|
2020-11-01 17:16:38 +03:00
|
|
|
|
2021-01-12 10:28:58 +03:00
|
|
|
// UINT8 * bbb = state->buffer + current_row * (state->bytes /
|
|
|
|
// rows_per_block); TRACE(("chars: %x %x %x %x\n", ((UINT8 *)bbb)[0],
|
2021-01-03 06:17:51 +03:00
|
|
|
// ((UINT8 *)bbb)[1], ((UINT8 *)bbb)[2], ((UINT8 *)bbb)[3]));
|
2020-11-01 17:16:38 +03:00
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
state->shuffle(
|
2021-01-12 10:28:58 +03:00
|
|
|
(UINT8 *)im->image[state->y + state->yoff + current_row] +
|
2021-01-03 06:17:51 +03:00
|
|
|
state->xoff * im->pixelsize,
|
2021-01-12 10:28:58 +03:00
|
|
|
state->buffer + current_row * row_byte_size,
|
2021-01-03 06:17:51 +03:00
|
|
|
state->xsize);
|
2018-12-09 08:04:34 +03:00
|
|
|
}
|
2020-11-01 17:16:38 +03:00
|
|
|
}
|
2020-11-01 19:25:31 +03:00
|
|
|
|
2021-01-14 05:33:49 +03:00
|
|
|
decodergba_err:
|
2020-11-01 19:25:31 +03:00
|
|
|
TIFFRGBAImageEnd(&img);
|
|
|
|
if (state->errcode != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-11-01 17:16:38 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2018-12-09 08:04:34 +03:00
|
|
|
|
2021-01-14 05:33:49 +03:00
|
|
|
int
|
|
|
|
_decodeTile(Imaging im, ImagingCodecState state, TIFF *tiff, int planes, ImagingShuffler *unpackers) {
|
2021-01-26 07:29:04 +03:00
|
|
|
INT32 x, y, tile_y, current_tile_length, current_tile_width;
|
|
|
|
UINT32 tile_width, tile_length;
|
|
|
|
tsize_t tile_bytes_size, row_byte_size;
|
2021-01-14 05:33:49 +03:00
|
|
|
UINT8 *new_data;
|
|
|
|
|
2021-01-26 07:29:04 +03:00
|
|
|
tile_bytes_size = TIFFTileSize(tiff);
|
2021-01-14 05:33:49 +03:00
|
|
|
|
2021-01-26 07:29:04 +03:00
|
|
|
if (tile_bytes_size == 0) {
|
|
|
|
TRACE(("Decode Error, Can not calculate TileSize\n"));
|
|
|
|
state->errcode = IMAGING_CODEC_BROKEN;
|
2021-01-14 05:33:49 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-01-26 07:29:04 +03:00
|
|
|
row_byte_size = TIFFTileRowSize(tiff);
|
2021-01-14 05:33:49 +03:00
|
|
|
|
2021-01-26 07:29:04 +03:00
|
|
|
if (row_byte_size == 0 || row_byte_size > tile_bytes_size) {
|
|
|
|
TRACE(("Decode Error, Can not calculate TileRowSize\n"));
|
|
|
|
state->errcode = IMAGING_CODEC_BROKEN;
|
2021-01-14 05:33:49 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-01-26 07:29:04 +03:00
|
|
|
/* overflow check for realloc */
|
|
|
|
if (tile_bytes_size > INT_MAX - 1) {
|
2021-01-14 05:33:49 +03:00
|
|
|
state->errcode = IMAGING_CODEC_MEMORY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-01-26 07:29:04 +03:00
|
|
|
TIFFGetField(tiff, TIFFTAG_TILEWIDTH, &tile_width);
|
|
|
|
TIFFGetField(tiff, TIFFTAG_TILELENGTH, &tile_length);
|
|
|
|
|
|
|
|
if (tile_width > INT_MAX || tile_length > INT_MAX) {
|
|
|
|
// state->x and state->y are ints
|
|
|
|
state->errcode = IMAGING_CODEC_MEMORY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-01-26 08:45:57 +03:00
|
|
|
if (tile_bytes_size > ((tile_length * state->bits / planes + 7) / 8) * tile_width) {
|
|
|
|
// If the tile size as expected by LibTiff isn't what we're expecting, abort.
|
|
|
|
// man: TIFFTileSize returns the equivalent size for a tile of data as it would be returned in a
|
|
|
|
// call to TIFFReadTile ...
|
|
|
|
state->errcode = IMAGING_CODEC_BROKEN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
state->bytes = tile_bytes_size;
|
|
|
|
|
2021-01-14 05:33:49 +03:00
|
|
|
TRACE(("TIFFTileSize: %d\n", state->bytes));
|
|
|
|
|
2021-01-26 08:45:57 +03:00
|
|
|
/* realloc to fit whole tile */
|
|
|
|
/* malloc check above */
|
|
|
|
new_data = realloc(state->buffer, state->bytes);
|
|
|
|
if (!new_data) {
|
|
|
|
state->errcode = IMAGING_CODEC_MEMORY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
state->buffer = new_data;
|
|
|
|
|
2021-01-14 05:33:49 +03:00
|
|
|
for (y = state->yoff; y < state->ysize; y += tile_length) {
|
|
|
|
int plane;
|
|
|
|
for (plane = 0; plane < planes; plane++) {
|
|
|
|
ImagingShuffler shuffler = unpackers[plane];
|
|
|
|
for (x = state->xoff; x < state->xsize; x += tile_width) {
|
|
|
|
if (TIFFReadTile(tiff, (tdata_t)state->buffer, x, y, 0, plane) == -1) {
|
|
|
|
TRACE(("Decode Error, Tile at %dx%d\n", x, y));
|
|
|
|
state->errcode = IMAGING_CODEC_BROKEN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE(("Read tile at %dx%d; \n\n", x, y));
|
|
|
|
|
|
|
|
current_tile_width = min((INT32) tile_width, state->xsize - x);
|
|
|
|
current_tile_length = min((INT32) tile_length, state->ysize - y);
|
|
|
|
// iterate over each line in the tile and stuff data into image
|
|
|
|
for (tile_y = 0; tile_y < current_tile_length; tile_y++) {
|
|
|
|
TRACE(("Writing tile data at %dx%d using tile_width: %d; \n", tile_y + y, x, current_tile_width));
|
|
|
|
|
|
|
|
// UINT8 * bbb = state->buffer + tile_y * row_byte_size;
|
|
|
|
// TRACE(("chars: %x%x%x%x\n", ((UINT8 *)bbb)[0], ((UINT8 *)bbb)[1], ((UINT8 *)bbb)[2], ((UINT8 *)bbb)[3]));
|
|
|
|
|
|
|
|
shuffler((UINT8*) im->image[tile_y + y] + x * im->pixelsize,
|
2021-01-26 07:29:04 +03:00
|
|
|
state->buffer + tile_y * row_byte_size,
|
2021-01-14 05:33:49 +03:00
|
|
|
current_tile_width
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
int
|
2021-01-10 21:29:56 +03:00
|
|
|
_decodeStrip(Imaging im, ImagingCodecState state, TIFF *tiff, int planes, ImagingShuffler *unpackers) {
|
2020-12-31 15:01:35 +03:00
|
|
|
INT32 strip_row = 0;
|
2020-11-01 17:16:38 +03:00
|
|
|
UINT8 *new_data;
|
2021-01-26 07:29:04 +03:00
|
|
|
UINT32 rows_per_strip;
|
2020-11-01 17:16:38 +03:00
|
|
|
int ret;
|
2021-04-01 00:17:20 +03:00
|
|
|
tsize_t strip_size, row_byte_size, unpacker_row_byte_size;
|
2020-11-01 17:16:38 +03:00
|
|
|
|
|
|
|
ret = TIFFGetField(tiff, TIFFTAG_ROWSPERSTRIP, &rows_per_strip);
|
2021-01-26 07:29:04 +03:00
|
|
|
if (ret != 1 || rows_per_strip==(UINT32)(-1)) {
|
2020-11-01 17:16:38 +03:00
|
|
|
rows_per_strip = state->ysize;
|
|
|
|
}
|
|
|
|
|
2021-01-26 07:29:04 +03:00
|
|
|
if (rows_per_strip > INT_MAX) {
|
2020-11-01 17:16:38 +03:00
|
|
|
state->errcode = IMAGING_CODEC_MEMORY;
|
|
|
|
return -1;
|
2018-12-09 08:04:34 +03:00
|
|
|
}
|
|
|
|
|
2021-01-26 07:29:04 +03:00
|
|
|
TRACE(("RowsPerStrip: %u\n", rows_per_strip));
|
|
|
|
|
|
|
|
strip_size = TIFFStripSize(tiff);
|
|
|
|
if (strip_size > INT_MAX - 1) {
|
|
|
|
state->errcode = IMAGING_CODEC_MEMORY;
|
|
|
|
return -1;
|
|
|
|
}
|
2021-01-26 08:45:57 +03:00
|
|
|
|
2021-04-01 00:17:20 +03:00
|
|
|
unpacker_row_byte_size = (state->xsize * state->bits / planes + 7) / 8;
|
|
|
|
if (strip_size > (unpacker_row_byte_size * rows_per_strip)) {
|
2021-01-26 08:45:57 +03:00
|
|
|
// If the strip size as expected by LibTiff isn't what we're expecting, abort.
|
|
|
|
// man: TIFFStripSize returns the equivalent size for a strip of data as it would be returned in a
|
|
|
|
// call to TIFFReadEncodedStrip ...
|
|
|
|
state->errcode = IMAGING_CODEC_BROKEN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-01-26 07:29:04 +03:00
|
|
|
state->bytes = strip_size;
|
2020-11-01 17:16:38 +03:00
|
|
|
|
|
|
|
TRACE(("StripSize: %d \n", state->bytes));
|
|
|
|
|
2021-01-26 07:29:04 +03:00
|
|
|
row_byte_size = TIFFScanlineSize(tiff);
|
2020-11-01 17:16:38 +03:00
|
|
|
|
2021-04-01 00:17:20 +03:00
|
|
|
// if the unpacker calculated row size is > row byte size, (at least) the last
|
|
|
|
// row of the strip will have a read buffer overflow.
|
|
|
|
if (row_byte_size == 0 || unpacker_row_byte_size > row_byte_size) {
|
2021-01-26 07:29:04 +03:00
|
|
|
state->errcode = IMAGING_CODEC_BROKEN;
|
2018-12-09 08:04:34 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-01-26 07:29:04 +03:00
|
|
|
TRACE(("RowsByteSize: %u \n", row_byte_size));
|
|
|
|
|
2020-11-01 17:16:38 +03:00
|
|
|
/* realloc to fit whole strip */
|
|
|
|
/* malloc check above */
|
2021-01-03 06:17:51 +03:00
|
|
|
new_data = realloc(state->buffer, state->bytes);
|
2020-11-01 17:16:38 +03:00
|
|
|
if (!new_data) {
|
|
|
|
state->errcode = IMAGING_CODEC_MEMORY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
state->buffer = new_data;
|
|
|
|
|
|
|
|
for (; state->y < state->ysize; state->y += rows_per_strip) {
|
2021-01-12 00:28:23 +03:00
|
|
|
int plane;
|
2020-12-31 15:01:35 +03:00
|
|
|
for (plane = 0; plane < planes; plane++) {
|
|
|
|
ImagingShuffler shuffler = unpackers[plane];
|
2021-03-09 07:20:29 +03:00
|
|
|
if (TIFFReadEncodedStrip(tiff, TIFFComputeStrip(tiff, state->y, plane), (tdata_t)state->buffer, strip_size) == -1) {
|
2020-12-31 15:01:35 +03:00
|
|
|
TRACE(("Decode Error, strip %d\n", TIFFComputeStrip(tiff, state->y, 0)));
|
|
|
|
state->errcode = IMAGING_CODEC_BROKEN;
|
|
|
|
return -1;
|
|
|
|
}
|
2020-11-01 17:16:38 +03:00
|
|
|
|
2020-12-31 15:01:35 +03:00
|
|
|
TRACE(("Decoded strip for row %d \n", state->y));
|
2020-11-01 17:16:38 +03:00
|
|
|
|
2020-12-31 15:01:35 +03:00
|
|
|
// iterate over each row in the strip and stuff data into image
|
|
|
|
for (strip_row = 0;
|
|
|
|
strip_row < min((INT32) rows_per_strip, state->ysize - state->y);
|
|
|
|
strip_row++) {
|
|
|
|
TRACE(("Writing data into line %d ; \n", state->y + strip_row));
|
2020-11-01 17:16:38 +03:00
|
|
|
|
2020-12-31 15:01:35 +03:00
|
|
|
// UINT8 * bbb = state->buffer + strip_row * (state->bytes / rows_per_strip);
|
|
|
|
// TRACE(("chars: %x %x %x %x\n", ((UINT8 *)bbb)[0], ((UINT8 *)bbb)[1], ((UINT8 *)bbb)[2], ((UINT8 *)bbb)[3]));
|
2020-11-01 17:16:38 +03:00
|
|
|
|
2020-12-31 15:01:35 +03:00
|
|
|
shuffler(
|
|
|
|
(UINT8*) im->image[state->y + state->yoff + strip_row] +
|
2021-01-03 06:17:51 +03:00
|
|
|
state->xoff * im->pixelsize,
|
2020-12-31 15:01:35 +03:00
|
|
|
state->buffer + strip_row * row_byte_size,
|
|
|
|
state->xsize);
|
|
|
|
}
|
2020-11-01 17:16:38 +03:00
|
|
|
}
|
|
|
|
}
|
2020-12-31 15:01:35 +03:00
|
|
|
|
2018-12-09 08:04:34 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
int
|
|
|
|
ImagingLibTiffDecode(
|
|
|
|
Imaging im, ImagingCodecState state, UINT8 *buffer, Py_ssize_t bytes) {
|
2018-10-19 07:23:06 +03:00
|
|
|
TIFFSTATE *clientstate = (TIFFSTATE *)state->context;
|
|
|
|
char *filename = "tempfile.tif";
|
2021-05-31 13:18:34 +03:00
|
|
|
char *mode = "rC";
|
2018-10-19 07:23:06 +03:00
|
|
|
TIFF *tiff;
|
2021-04-30 17:51:39 +03:00
|
|
|
uint16_t photometric = 0; // init to not PHOTOMETRIC_YCBCR
|
|
|
|
uint16_t compression;
|
2021-01-14 05:33:49 +03:00
|
|
|
int readAsRGBA = 0;
|
2021-04-30 17:51:39 +03:00
|
|
|
uint16_t planarconfig = 0;
|
2021-01-10 21:29:56 +03:00
|
|
|
int planes = 1;
|
2020-12-31 15:01:35 +03:00
|
|
|
ImagingShuffler unpackers[4];
|
2022-01-18 06:19:43 +03:00
|
|
|
INT32 img_width, img_height;
|
2013-03-09 07:51:10 +04:00
|
|
|
|
2021-01-10 00:45:38 +03:00
|
|
|
memset(unpackers, 0, sizeof(ImagingShuffler) * 4);
|
2020-12-31 16:35:26 +03:00
|
|
|
|
2018-10-19 21:42:40 +03:00
|
|
|
/* buffer is the encoded file, bytes is the length of the encoded file */
|
|
|
|
/* it all ends up in state->buffer, which is a uint8* from Imaging.h */
|
2013-03-09 07:51:10 +04:00
|
|
|
|
|
|
|
TRACE(("in decoder: bytes %d\n", bytes));
|
2021-01-03 06:17:51 +03:00
|
|
|
TRACE(
|
|
|
|
("State: count %d, state %d, x %d, y %d, ystep %d\n",
|
|
|
|
state->count,
|
|
|
|
state->state,
|
|
|
|
state->x,
|
|
|
|
state->y,
|
|
|
|
state->ystep));
|
|
|
|
TRACE(
|
|
|
|
("State: xsize %d, ysize %d, xoff %d, yoff %d \n",
|
|
|
|
state->xsize,
|
|
|
|
state->ysize,
|
|
|
|
state->xoff,
|
|
|
|
state->yoff));
|
2018-10-19 21:42:40 +03:00
|
|
|
TRACE(("State: bits %d, bytes %d \n", state->bits, state->bytes));
|
2021-01-03 06:17:51 +03:00
|
|
|
TRACE(
|
|
|
|
("Buffer: %p: %c%c%c%c\n",
|
|
|
|
buffer,
|
|
|
|
(char)buffer[0],
|
|
|
|
(char)buffer[1],
|
|
|
|
(char)buffer[2],
|
|
|
|
(char)buffer[3]));
|
|
|
|
TRACE(
|
|
|
|
("State->Buffer: %c%c%c%c\n",
|
|
|
|
(char)state->buffer[0],
|
|
|
|
(char)state->buffer[1],
|
|
|
|
(char)state->buffer[2],
|
|
|
|
(char)state->buffer[3]));
|
|
|
|
TRACE(
|
|
|
|
("Image: mode %s, type %d, bands: %d, xsize %d, ysize %d \n",
|
|
|
|
im->mode,
|
|
|
|
im->type,
|
|
|
|
im->bands,
|
|
|
|
im->xsize,
|
|
|
|
im->ysize));
|
|
|
|
TRACE(
|
|
|
|
("Image: image8 %p, image32 %p, image %p, block %p \n",
|
|
|
|
im->image8,
|
|
|
|
im->image32,
|
|
|
|
im->image,
|
|
|
|
im->block));
|
|
|
|
TRACE(("Image: pixelsize: %d, linesize %d \n", im->pixelsize, im->linesize));
|
2018-10-19 21:42:40 +03:00
|
|
|
|
|
|
|
dump_state(clientstate);
|
|
|
|
clientstate->size = bytes;
|
|
|
|
clientstate->eof = clientstate->size;
|
|
|
|
clientstate->loc = 0;
|
|
|
|
clientstate->data = (tdata_t)buffer;
|
|
|
|
clientstate->flrealloc = 0;
|
|
|
|
dump_state(clientstate);
|
2013-07-01 19:48:21 +04:00
|
|
|
|
|
|
|
TIFFSetWarningHandler(NULL);
|
|
|
|
TIFFSetWarningHandlerExt(NULL);
|
|
|
|
|
2018-10-19 21:42:40 +03:00
|
|
|
if (clientstate->fp) {
|
2021-01-03 06:17:51 +03:00
|
|
|
TRACE(("Opening using fd: %d\n", clientstate->fp));
|
|
|
|
lseek(clientstate->fp, 0, SEEK_SET); // Sometimes, I get it set to the end.
|
2020-08-30 05:30:47 +03:00
|
|
|
tiff = TIFFFdOpen(fd_to_tiff_fd(clientstate->fp), filename, mode);
|
2018-10-19 21:42:40 +03:00
|
|
|
} else {
|
|
|
|
TRACE(("Opening from string\n"));
|
2021-01-03 06:17:51 +03:00
|
|
|
tiff = TIFFClientOpen(
|
|
|
|
filename,
|
|
|
|
mode,
|
|
|
|
(thandle_t)clientstate,
|
|
|
|
_tiffReadProc,
|
|
|
|
_tiffWriteProc,
|
|
|
|
_tiffSeekProc,
|
|
|
|
_tiffCloseProc,
|
|
|
|
_tiffSizeProc,
|
|
|
|
_tiffMapProc,
|
|
|
|
_tiffUnmapProc);
|
2018-10-19 21:42:40 +03:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
if (!tiff) {
|
2018-10-19 21:42:40 +03:00
|
|
|
TRACE(("Error, didn't get the tiff\n"));
|
|
|
|
state->errcode = IMAGING_CODEC_BROKEN;
|
|
|
|
return -1;
|
|
|
|
}
|
2013-03-09 07:51:10 +04:00
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
if (clientstate->ifd) {
|
2018-10-19 21:42:40 +03:00
|
|
|
int rv;
|
2021-04-30 17:51:39 +03:00
|
|
|
uint32_t ifdoffset = clientstate->ifd;
|
2018-10-19 21:42:40 +03:00
|
|
|
TRACE(("reading tiff ifd %u\n", ifdoffset));
|
|
|
|
rv = TIFFSetSubDirectory(tiff, ifdoffset);
|
2021-01-03 06:17:51 +03:00
|
|
|
if (!rv) {
|
2018-10-19 21:42:40 +03:00
|
|
|
TRACE(("error in TIFFSetSubDirectory"));
|
2020-11-01 19:25:31 +03:00
|
|
|
goto decode_err;
|
2018-10-19 21:42:40 +03:00
|
|
|
}
|
|
|
|
}
|
2014-08-21 22:02:01 +04:00
|
|
|
|
2021-03-31 22:04:59 +03:00
|
|
|
TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &img_width);
|
|
|
|
TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &img_height);
|
|
|
|
|
|
|
|
if (state->xsize != img_width || state->ysize != img_height) {
|
|
|
|
TRACE(
|
|
|
|
("Inconsistent Image Error: %d =? %d, %d =? %d",
|
|
|
|
state->xsize,
|
|
|
|
img_width,
|
|
|
|
state->ysize,
|
|
|
|
img_height));
|
|
|
|
state->errcode = IMAGING_CODEC_BROKEN;
|
|
|
|
goto decode_err;
|
|
|
|
}
|
|
|
|
|
2020-12-31 15:01:35 +03:00
|
|
|
|
2020-11-01 17:16:38 +03:00
|
|
|
TIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &photometric);
|
2021-01-10 02:05:36 +03:00
|
|
|
TIFFGetField(tiff, TIFFTAG_COMPRESSION, &compression);
|
2020-12-31 15:01:35 +03:00
|
|
|
TIFFGetFieldDefaulted(tiff, TIFFTAG_PLANARCONFIG, &planarconfig);
|
|
|
|
|
2021-01-14 05:33:49 +03:00
|
|
|
// Dealing with YCbCr images is complicated in case if subsampling
|
|
|
|
// Let LibTiff read them as RGBA
|
|
|
|
readAsRGBA = photometric == PHOTOMETRIC_YCBCR;
|
2021-01-10 02:05:36 +03:00
|
|
|
|
2021-01-14 05:33:49 +03:00
|
|
|
if (readAsRGBA && compression == COMPRESSION_JPEG && planarconfig == PLANARCONFIG_CONTIG) {
|
2021-05-19 16:32:00 +03:00
|
|
|
// If using new JPEG compression, let libjpeg do RGB conversion for performance reasons
|
2021-01-10 02:05:36 +03:00
|
|
|
TIFFSetField(tiff, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
|
2021-01-14 05:33:49 +03:00
|
|
|
readAsRGBA = 0;
|
2021-01-10 02:05:36 +03:00
|
|
|
}
|
|
|
|
|
2021-01-14 05:33:49 +03:00
|
|
|
if (readAsRGBA) {
|
|
|
|
_decodeAsRGBA(im, state, tiff);
|
2021-01-12 10:28:58 +03:00
|
|
|
}
|
|
|
|
else {
|
2021-01-14 05:33:49 +03:00
|
|
|
planes = _pickUnpackers(im, state, tiff, planarconfig, unpackers);
|
|
|
|
if (planes <= 0) {
|
|
|
|
goto decode_err;
|
2020-12-31 15:01:35 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 10:28:58 +03:00
|
|
|
if (TIFFIsTiled(tiff)) {
|
2021-01-14 05:33:49 +03:00
|
|
|
_decodeTile(im, state, tiff, planes, unpackers);
|
2018-07-03 03:15:24 +03:00
|
|
|
}
|
2020-12-31 15:01:35 +03:00
|
|
|
else {
|
2021-01-12 10:28:58 +03:00
|
|
|
_decodeStrip(im, state, tiff, planes, unpackers);
|
2018-07-03 03:15:24 +03:00
|
|
|
}
|
2013-03-09 07:51:10 +04:00
|
|
|
|
2021-01-12 10:28:58 +03:00
|
|
|
if (!state->errcode) {
|
|
|
|
// Check if raw mode was RGBa and it was stored on separate planes
|
|
|
|
// so we have to convert it to RGBA
|
|
|
|
if (planes > 3 && strcmp(im->mode, "RGBA") == 0) {
|
2021-04-30 17:51:39 +03:00
|
|
|
uint16_t extrasamples;
|
|
|
|
uint16_t* sampleinfo;
|
2021-01-12 10:28:58 +03:00
|
|
|
ImagingShuffler shuffle;
|
|
|
|
INT32 y;
|
|
|
|
|
|
|
|
TIFFGetFieldDefaulted(tiff, TIFFTAG_EXTRASAMPLES, &extrasamples, &sampleinfo);
|
|
|
|
|
|
|
|
if (extrasamples >= 1 &&
|
|
|
|
(sampleinfo[0] == EXTRASAMPLE_UNSPECIFIED || sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA)
|
|
|
|
) {
|
|
|
|
shuffle = ImagingFindUnpacker("RGBA", "RGBa", NULL);
|
|
|
|
|
|
|
|
for (y = state->yoff; y < state->ysize; y++) {
|
|
|
|
UINT8* ptr = (UINT8*) im->image[y + state->yoff] +
|
|
|
|
state->xoff * im->pixelsize;
|
|
|
|
shuffle(ptr, ptr, state->xsize);
|
|
|
|
}
|
2020-12-31 15:01:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
decode_err:
|
2018-10-19 21:42:40 +03:00
|
|
|
TIFFClose(tiff);
|
|
|
|
TRACE(("Done Decoding, Returning \n"));
|
|
|
|
// Returning -1 here to force ImageFile.load to break, rather than
|
|
|
|
// even think about looping back around.
|
|
|
|
return -1;
|
2013-03-09 07:51:10 +04:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
int
|
|
|
|
ImagingLibTiffEncodeInit(ImagingCodecState state, char *filename, int fp) {
|
2018-10-19 21:42:40 +03:00
|
|
|
// Open the FD or the pointer as a tiff file, for writing.
|
|
|
|
// We may have to do some monkeying around to make this really work.
|
|
|
|
// If we have a fp, then we're good.
|
|
|
|
// If we have a memory string, we're probably going to have to malloc, then
|
|
|
|
// shuffle bytes into the writescanline process.
|
|
|
|
// Going to have to deal with the directory as well.
|
2013-03-09 07:51:10 +04:00
|
|
|
|
2018-10-19 21:42:40 +03:00
|
|
|
TIFFSTATE *clientstate = (TIFFSTATE *)state->context;
|
2021-01-03 06:17:51 +03:00
|
|
|
int bufsize = 64 * 1024;
|
2018-10-19 21:42:40 +03:00
|
|
|
char *mode = "w";
|
2013-03-09 07:51:10 +04:00
|
|
|
|
|
|
|
TRACE(("initing libtiff\n"));
|
2021-01-03 06:17:51 +03:00
|
|
|
TRACE(("Filename %s, filepointer: %d \n", filename, fp));
|
|
|
|
TRACE(
|
|
|
|
("State: count %d, state %d, x %d, y %d, ystep %d\n",
|
|
|
|
state->count,
|
|
|
|
state->state,
|
|
|
|
state->x,
|
|
|
|
state->y,
|
|
|
|
state->ystep));
|
|
|
|
TRACE(
|
|
|
|
("State: xsize %d, ysize %d, xoff %d, yoff %d \n",
|
|
|
|
state->xsize,
|
|
|
|
state->ysize,
|
|
|
|
state->xoff,
|
|
|
|
state->yoff));
|
2018-10-19 21:42:40 +03:00
|
|
|
TRACE(("State: bits %d, bytes %d \n", state->bits, state->bytes));
|
|
|
|
TRACE(("State: context %p \n", state->context));
|
|
|
|
|
|
|
|
clientstate->loc = 0;
|
|
|
|
clientstate->size = 0;
|
2021-01-03 06:17:51 +03:00
|
|
|
clientstate->eof = 0;
|
2018-10-19 21:42:40 +03:00
|
|
|
clientstate->data = 0;
|
|
|
|
clientstate->flrealloc = 0;
|
|
|
|
clientstate->fp = fp;
|
|
|
|
|
|
|
|
state->state = 0;
|
|
|
|
|
|
|
|
if (fp) {
|
2021-01-03 06:17:51 +03:00
|
|
|
TRACE(("Opening using fd: %d for writing \n", clientstate->fp));
|
2020-08-30 05:30:47 +03:00
|
|
|
clientstate->tiff = TIFFFdOpen(fd_to_tiff_fd(clientstate->fp), filename, mode);
|
2018-10-19 21:42:40 +03:00
|
|
|
} else {
|
2021-01-03 06:17:51 +03:00
|
|
|
// malloc a buffer to write the tif, we're going to need to realloc or something
|
|
|
|
// if we need bigger.
|
2018-10-19 21:42:40 +03:00
|
|
|
TRACE(("Opening a buffer for writing \n"));
|
2016-03-16 14:47:18 +03:00
|
|
|
/* malloc check ok, small constant allocation */
|
2018-10-19 21:42:40 +03:00
|
|
|
clientstate->data = malloc(bufsize);
|
|
|
|
clientstate->size = bufsize;
|
2021-01-03 06:17:51 +03:00
|
|
|
clientstate->flrealloc = 1;
|
2013-03-09 07:51:10 +04:00
|
|
|
|
2018-10-19 21:42:40 +03:00
|
|
|
if (!clientstate->data) {
|
|
|
|
TRACE(("Error, couldn't allocate a buffer of size %d\n", bufsize));
|
|
|
|
return 0;
|
|
|
|
}
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
clientstate->tiff = TIFFClientOpen(
|
|
|
|
filename,
|
|
|
|
mode,
|
|
|
|
(thandle_t)clientstate,
|
|
|
|
_tiffReadProc,
|
|
|
|
_tiffWriteProc,
|
|
|
|
_tiffSeekProc,
|
|
|
|
_tiffCloseProc,
|
|
|
|
_tiffSizeProc,
|
|
|
|
_tiffNullMapProc,
|
|
|
|
_tiffUnmapProc); /*force no mmap*/
|
2018-10-19 21:42:40 +03:00
|
|
|
}
|
2013-03-09 07:51:10 +04:00
|
|
|
|
2018-10-19 21:42:40 +03:00
|
|
|
if (!clientstate->tiff) {
|
|
|
|
TRACE(("Error, couldn't open tiff file\n"));
|
|
|
|
return 0;
|
|
|
|
}
|
2013-03-09 07:51:10 +04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
int
|
|
|
|
ImagingLibTiffMergeFieldInfo(
|
|
|
|
ImagingCodecState state, TIFFDataType field_type, int key, int is_var_length) {
|
2019-06-30 21:48:19 +03:00
|
|
|
// Refer to libtiff docs (http://www.simplesystems.org/libtiff/addingtags.html)
|
2018-10-25 11:45:13 +03:00
|
|
|
TIFFSTATE *clientstate = (TIFFSTATE *)state->context;
|
2021-04-30 17:51:39 +03:00
|
|
|
uint32_t n;
|
2018-10-25 11:36:49 +03:00
|
|
|
int status = 0;
|
2018-10-25 11:45:13 +03:00
|
|
|
|
2019-06-30 21:48:19 +03:00
|
|
|
// custom fields added with ImagingLibTiffMergeFieldInfo are only used for
|
|
|
|
// decoding, ignore readcount;
|
2020-12-30 03:57:05 +03:00
|
|
|
int readcount = 1;
|
2019-06-30 21:48:19 +03:00
|
|
|
// we support writing a single value, or a variable number of values
|
|
|
|
int writecount = 1;
|
|
|
|
// whether the first value should encode the number of values.
|
|
|
|
int passcount = 0;
|
|
|
|
|
|
|
|
TIFFFieldInfo info[] = {
|
2021-01-03 06:17:51 +03:00
|
|
|
{key,
|
|
|
|
readcount,
|
|
|
|
writecount,
|
|
|
|
field_type,
|
|
|
|
FIELD_CUSTOM,
|
|
|
|
1,
|
|
|
|
passcount,
|
|
|
|
"CustomField"}};
|
2019-06-30 21:48:19 +03:00
|
|
|
|
|
|
|
if (is_var_length) {
|
|
|
|
info[0].field_writecount = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_var_length && field_type != TIFF_ASCII) {
|
|
|
|
info[0].field_passcount = 1;
|
|
|
|
}
|
|
|
|
|
2018-10-25 11:45:13 +03:00
|
|
|
n = sizeof(info) / sizeof(info[0]);
|
|
|
|
|
2018-10-25 11:36:49 +03:00
|
|
|
// Test for libtiff 4.0 or later, excluding libtiff 3.9.6 and 3.9.7
|
2021-01-03 06:17:51 +03:00
|
|
|
#if TIFFLIB_VERSION >= 20111221 && TIFFLIB_VERSION != 20120218 && \
|
|
|
|
TIFFLIB_VERSION != 20120922
|
2018-10-25 11:36:49 +03:00
|
|
|
status = TIFFMergeFieldInfo(clientstate->tiff, info, n);
|
|
|
|
#else
|
|
|
|
TIFFMergeFieldInfo(clientstate->tiff, info, n);
|
|
|
|
#endif
|
|
|
|
return status;
|
2018-10-25 11:45:13 +03:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
int
|
|
|
|
ImagingLibTiffSetField(ImagingCodecState state, ttag_t tag, ...) {
|
2018-10-19 21:42:40 +03:00
|
|
|
// after tif_dir.c->TIFFSetField.
|
|
|
|
TIFFSTATE *clientstate = (TIFFSTATE *)state->context;
|
|
|
|
va_list ap;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
va_start(ap, tag);
|
|
|
|
status = TIFFVSetField(clientstate->tiff, tag, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return status;
|
2013-03-09 07:51:10 +04:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
int
|
|
|
|
ImagingLibTiffEncode(Imaging im, ImagingCodecState state, UINT8 *buffer, int bytes) {
|
2018-10-19 21:42:40 +03:00
|
|
|
/* One shot encoder. Encode everything to the tiff in the clientstate.
|
|
|
|
If we're running off of a FD, then run once, we're good, everything
|
|
|
|
ends up in the file, we close and we're done.
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2018-10-19 21:42:40 +03:00
|
|
|
If we're going to memory, then we need to write the whole file into memory, then
|
|
|
|
parcel it back out to the pystring buffer bytes at a time.
|
2013-03-09 07:51:10 +04:00
|
|
|
|
2018-10-19 21:42:40 +03:00
|
|
|
*/
|
2013-03-09 07:51:10 +04:00
|
|
|
|
2018-10-19 21:42:40 +03:00
|
|
|
TIFFSTATE *clientstate = (TIFFSTATE *)state->context;
|
|
|
|
TIFF *tiff = clientstate->tiff;
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-03-09 07:51:10 +04:00
|
|
|
TRACE(("in encoder: bytes %d\n", bytes));
|
2021-01-03 06:17:51 +03:00
|
|
|
TRACE(
|
|
|
|
("State: count %d, state %d, x %d, y %d, ystep %d\n",
|
|
|
|
state->count,
|
|
|
|
state->state,
|
|
|
|
state->x,
|
|
|
|
state->y,
|
|
|
|
state->ystep));
|
|
|
|
TRACE(
|
|
|
|
("State: xsize %d, ysize %d, xoff %d, yoff %d \n",
|
|
|
|
state->xsize,
|
|
|
|
state->ysize,
|
|
|
|
state->xoff,
|
|
|
|
state->yoff));
|
2018-10-19 21:42:40 +03:00
|
|
|
TRACE(("State: bits %d, bytes %d \n", state->bits, state->bytes));
|
2021-01-03 06:17:51 +03:00
|
|
|
TRACE(
|
|
|
|
("Buffer: %p: %c%c%c%c\n",
|
|
|
|
buffer,
|
|
|
|
(char)buffer[0],
|
|
|
|
(char)buffer[1],
|
|
|
|
(char)buffer[2],
|
|
|
|
(char)buffer[3]));
|
|
|
|
TRACE(
|
|
|
|
("State->Buffer: %c%c%c%c\n",
|
|
|
|
(char)state->buffer[0],
|
|
|
|
(char)state->buffer[1],
|
|
|
|
(char)state->buffer[2],
|
|
|
|
(char)state->buffer[3]));
|
|
|
|
TRACE(
|
|
|
|
("Image: mode %s, type %d, bands: %d, xsize %d, ysize %d \n",
|
|
|
|
im->mode,
|
|
|
|
im->type,
|
|
|
|
im->bands,
|
|
|
|
im->xsize,
|
|
|
|
im->ysize));
|
|
|
|
TRACE(
|
|
|
|
("Image: image8 %p, image32 %p, image %p, block %p \n",
|
|
|
|
im->image8,
|
|
|
|
im->image32,
|
|
|
|
im->image,
|
|
|
|
im->block));
|
|
|
|
TRACE(("Image: pixelsize: %d, linesize %d \n", im->pixelsize, im->linesize));
|
2018-10-19 21:42:40 +03:00
|
|
|
|
|
|
|
dump_state(clientstate);
|
|
|
|
|
|
|
|
if (state->state == 0) {
|
|
|
|
TRACE(("Encoding line bt line"));
|
2021-01-03 06:17:51 +03:00
|
|
|
while (state->y < state->ysize) {
|
|
|
|
state->shuffle(
|
|
|
|
state->buffer,
|
|
|
|
(UINT8 *)im->image[state->y + state->yoff] +
|
|
|
|
state->xoff * im->pixelsize,
|
|
|
|
state->xsize);
|
|
|
|
|
|
|
|
if (TIFFWriteScanline(
|
2021-04-30 17:51:39 +03:00
|
|
|
tiff, (tdata_t)(state->buffer), (uint32_t)state->y, 0) == -1) {
|
2018-10-19 21:42:40 +03:00
|
|
|
TRACE(("Encode Error, row %d\n", state->y));
|
|
|
|
state->errcode = IMAGING_CODEC_BROKEN;
|
|
|
|
TIFFClose(tiff);
|
2021-01-03 06:17:51 +03:00
|
|
|
if (!clientstate->fp) {
|
2018-10-19 21:42:40 +03:00
|
|
|
free(clientstate->data);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
state->y++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state->y == state->ysize) {
|
2021-01-03 06:17:51 +03:00
|
|
|
state->state = 1;
|
2018-10-19 21:42:40 +03:00
|
|
|
|
|
|
|
TRACE(("Flushing \n"));
|
|
|
|
if (!TIFFFlush(tiff)) {
|
|
|
|
TRACE(("Error flushing the tiff"));
|
|
|
|
// likely reason is memory.
|
|
|
|
state->errcode = IMAGING_CODEC_MEMORY;
|
|
|
|
TIFFClose(tiff);
|
2021-01-03 06:17:51 +03:00
|
|
|
if (!clientstate->fp) {
|
2018-10-19 21:42:40 +03:00
|
|
|
free(clientstate->data);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
TRACE(("Closing \n"));
|
|
|
|
TIFFClose(tiff);
|
|
|
|
// reset the clientstate metadata to use it to read out the buffer.
|
|
|
|
clientstate->loc = 0;
|
2021-01-03 06:17:51 +03:00
|
|
|
clientstate->size = clientstate->eof; // redundant?
|
2018-10-19 21:42:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state->state == 1 && !clientstate->fp) {
|
|
|
|
int read = (int)_tiffReadProc(clientstate, (tdata_t)buffer, (tsize_t)bytes);
|
2021-01-03 06:17:51 +03:00
|
|
|
TRACE(
|
|
|
|
("Buffer: %p: %c%c%c%c\n",
|
|
|
|
buffer,
|
|
|
|
(char)buffer[0],
|
|
|
|
(char)buffer[1],
|
|
|
|
(char)buffer[2],
|
|
|
|
(char)buffer[3]));
|
2018-10-19 21:42:40 +03:00
|
|
|
if (clientstate->loc == clientstate->eof) {
|
|
|
|
TRACE(("Hit EOF, calling an end, freeing data"));
|
|
|
|
state->errcode = IMAGING_CODEC_END;
|
|
|
|
free(clientstate->data);
|
|
|
|
}
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
state->errcode = IMAGING_CODEC_END;
|
|
|
|
return 0;
|
2013-03-09 07:51:10 +04:00
|
|
|
}
|
2018-10-25 11:36:49 +03:00
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
const char *
|
|
|
|
ImagingTiffVersion(void) {
|
2018-10-25 11:36:49 +03:00
|
|
|
return TIFFGetVersion();
|
|
|
|
}
|
|
|
|
|
2013-03-09 07:51:10 +04:00
|
|
|
#endif
|