unify reading of YCbCr Tiffs

This commit is contained in:
Konstantin Kopachev 2021-01-11 23:28:58 -08:00 committed by Eric Soroos
parent 4c2dfadf26
commit 4dd288c66c

View File

@ -213,24 +213,34 @@ ImagingLibTiffInit(ImagingCodecState state, int fp, uint32 offset) {
} }
int int
_decodeStripYCbCr(Imaging im, ImagingCodecState state, TIFF *tiff) { _decodeYCbCr(Imaging im, ImagingCodecState state, TIFF *tiff) {
// To avoid dealing with YCbCr subsampling, let libtiff handle it // To avoid dealing with YCbCr subsampling, let libtiff handle it
// Use a TIFFRGBAImage wrapping the tiff image, and let libtiff handle // Use a TIFFRGBAImage wrapping the tiff image, and let libtiff handle
// all of the conversion. Metadata read from the TIFFRGBAImage could // all of the conversion. Metadata read from the TIFFRGBAImage could
// be different from the metadata that the base tiff returns. // be different from the metadata that the base tiff returns.
INT32 strip_row; INT32 current_row;
UINT8 *new_data; UINT8 *new_data;
UINT32 rows_per_strip, row_byte_size, rows_to_read; UINT32 rows_per_block, row_byte_size, rows_to_read;
int ret; int ret;
TIFFRGBAImage img; TIFFRGBAImage img;
char emsg[1024] = ""; char emsg[1024] = "";
ret = TIFFGetFieldDefaulted(tiff, TIFFTAG_ROWSPERSTRIP, &rows_per_strip); // Since using TIFFRGBAImage* functions, we can read whole tiff into rastrr in one call
if (ret != 1) { // Let's select smaller block size. Multiplying image width by (tile length OR rows per strip)
rows_per_strip = state->ysize; // gives us manageable block size in pixels
if (TIFFIsTiled(tiff)) {
ret = TIFFGetFieldDefaulted(tiff, TIFFTAG_TILELENGTH, &rows_per_block);
} }
TRACE(("RowsPerStrip: %u \n", rows_per_strip)); else {
ret = TIFFGetFieldDefaulted(tiff, TIFFTAG_ROWSPERSTRIP, &rows_per_block);
}
if (ret != 1) {
rows_per_block = state->ysize;
}
TRACE(("RowsPerBlock: %u \n", rows_per_block));
if (!(TIFFRGBAImageOK(tiff, emsg) && TIFFRGBAImageBegin(&img, tiff, 0, emsg))) { if (!(TIFFRGBAImageOK(tiff, emsg) && TIFFRGBAImageBegin(&img, tiff, 0, emsg))) {
TRACE(("Decode error, msg: %s", emsg)); TRACE(("Decode error, msg: %s", emsg));
@ -263,14 +273,14 @@ _decodeStripYCbCr(Imaging im, ImagingCodecState state, TIFF *tiff) {
row_byte_size = img.width * 4; row_byte_size = img.width * 4;
/* overflow check for realloc */ /* overflow check for realloc */
if (INT_MAX / row_byte_size < rows_per_strip) { if (INT_MAX / row_byte_size < rows_per_block) {
state->errcode = IMAGING_CODEC_MEMORY; state->errcode = IMAGING_CODEC_MEMORY;
goto decodeycbcr_err; goto decodeycbcr_err;
} }
state->bytes = rows_per_strip * row_byte_size; state->bytes = rows_per_block * row_byte_size;
TRACE(("StripSize: %d \n", state->bytes)); TRACE(("BlockSize: %d \n", state->bytes));
/* realloc to fit whole strip */ /* realloc to fit whole strip */
/* malloc check above */ /* malloc check above */
@ -282,9 +292,9 @@ _decodeStripYCbCr(Imaging im, ImagingCodecState state, TIFF *tiff) {
state->buffer = new_data; state->buffer = new_data;
for (; state->y < state->ysize; state->y += rows_per_strip) { for (; state->y < state->ysize; state->y += rows_per_block) {
img.row_offset = state->y; img.row_offset = state->y;
rows_to_read = min(rows_per_strip, img.height - state->y); rows_to_read = min(rows_per_block, img.height - state->y);
if (!TIFFRGBAImageGet(&img, (UINT32 *)state->buffer, img.width, rows_to_read)) { if (!TIFFRGBAImageGet(&img, (UINT32 *)state->buffer, img.width, rows_to_read)) {
TRACE(("Decode Error, y: %d\n", state->y)); TRACE(("Decode Error, y: %d\n", state->y));
@ -299,19 +309,19 @@ _decodeStripYCbCr(Imaging im, ImagingCodecState state, TIFF *tiff) {
TRACE(("Decoded strip for row %d \n", state->y)); TRACE(("Decoded strip for row %d \n", state->y));
// iterate over each row in the strip and stuff data into image // iterate over each row in the strip and stuff data into image
for (strip_row = 0; for (current_row = 0;
strip_row < min((INT32)rows_per_strip, state->ysize - state->y); current_row < min((INT32)rows_per_block, state->ysize - state->y);
strip_row++) { current_row++) {
TRACE(("Writing data into line %d ; \n", state->y + strip_row)); TRACE(("Writing data into line %d ; \n", state->y + current_row));
// UINT8 * bbb = state->buffer + strip_row * (state->bytes / // UINT8 * bbb = state->buffer + current_row * (state->bytes /
// rows_per_strip); TRACE(("chars: %x %x %x %x\n", ((UINT8 *)bbb)[0], // rows_per_block); TRACE(("chars: %x %x %x %x\n", ((UINT8 *)bbb)[0],
// ((UINT8 *)bbb)[1], ((UINT8 *)bbb)[2], ((UINT8 *)bbb)[3])); // ((UINT8 *)bbb)[1], ((UINT8 *)bbb)[2], ((UINT8 *)bbb)[3]));
state->shuffle( state->shuffle(
(UINT8 *)im->image[state->y + state->yoff + strip_row] + (UINT8 *)im->image[state->y + state->yoff + current_row] +
state->xoff * im->pixelsize, state->xoff * im->pixelsize,
state->buffer + strip_row * row_byte_size, state->buffer + current_row * row_byte_size,
state->xsize); state->xsize);
} }
} }
@ -525,180 +535,151 @@ ImagingLibTiffDecode(
isYCbCr = 0; isYCbCr = 0;
} }
// YCbCr data is read as RGB by libtiff and we don't need to worry about planar storage in that case if (isYCbCr) {
// if number of bands is 1, there is no difference with contig case _decodeYCbCr(im, state, tiff);
if (planarconfig == PLANARCONFIG_SEPARATE &&
im->bands > 1 &&
!isYCbCr) {
uint16 bits_per_sample = 8;
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;
goto decode_err;
}
planes = im->bands;
// 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);
} else {
unpackers[0] = state->shuffle;
} }
else {
// YCbCr data is read as RGB by libtiff and we don't need to worry about planar storage in that case
// if number of bands is 1, there is no difference with contig case
if (planarconfig == PLANARCONFIG_SEPARATE &&
im->bands > 1 &&
!isYCbCr) {
if (TIFFIsTiled(tiff)) { uint16 bits_per_sample = 8;
INT32 x, y, tile_y;
UINT32 tile_width, tile_length, current_tile_length, current_line,
current_tile_width, row_byte_size;
UINT8 *new_data;
TIFFGetField(tiff, TIFFTAG_TILEWIDTH, &tile_width); TIFFGetFieldDefaulted(tiff, TIFFTAG_BITSPERSAMPLE, &bits_per_sample);
TIFFGetField(tiff, TIFFTAG_TILELENGTH, &tile_length); if (bits_per_sample != 8 && bits_per_sample != 16) {
TRACE(("Invalid value for bits per sample: %d\n", bits_per_sample));
/* overflow check for row_byte_size calculation */
if ((UINT32)INT_MAX / state->bits < tile_width) {
state->errcode = IMAGING_CODEC_MEMORY;
goto decode_err;
}
if (isYCbCr) {
row_byte_size = tile_width * 4;
/* sanity check, we use this value in shuffle below */
if (im->pixelsize != 4) {
state->errcode = IMAGING_CODEC_BROKEN; state->errcode = IMAGING_CODEC_BROKEN;
goto decode_err; goto decode_err;
} }
planes = im->bands;
// 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);
} else { } else {
// We could use TIFFTileSize, but for YCbCr data it returns subsampled data size unpackers[0] = state->shuffle;
}
if (TIFFIsTiled(tiff)) {
INT32 x, y, tile_y;
UINT32 tile_width, tile_length, current_tile_length, current_line,
current_tile_width, row_byte_size;
UINT8 *new_data;
TIFFGetField(tiff, TIFFTAG_TILEWIDTH, &tile_width);
TIFFGetField(tiff, TIFFTAG_TILELENGTH, &tile_length);
/* overflow check for row_byte_size calculation */
if ((UINT32)INT_MAX / state->bits < tile_width) {
state->errcode = IMAGING_CODEC_MEMORY;
goto decode_err;
}
// We could use TIFFTileSize, but for YCbCr data it returns subsampled data
// size
row_byte_size = (tile_width * state->bits / planes + 7) / 8; row_byte_size = (tile_width * state->bits / planes + 7) / 8;
}
/* overflow check for realloc */ /* overflow check for realloc */
if (INT_MAX / row_byte_size < tile_length) { if (INT_MAX / row_byte_size < tile_length) {
state->errcode = IMAGING_CODEC_MEMORY; state->errcode = IMAGING_CODEC_MEMORY;
goto decode_err; goto decode_err;
} }
state->bytes = row_byte_size * tile_length; state->bytes = row_byte_size * tile_length;
if (TIFFTileSize(tiff) > state->bytes) { if (TIFFTileSize(tiff) > state->bytes) {
// If the tile size as expected by LibTiff isn't what we're expecting, abort. // If the tile size as expected by LibTiff isn't what we're expecting,
state->errcode = IMAGING_CODEC_MEMORY; // abort.
goto decode_err; state->errcode = IMAGING_CODEC_MEMORY;
} goto decode_err;
}
/* realloc to fit whole tile */ /* realloc to fit whole tile */
/* malloc check above */ /* malloc check above */
new_data = realloc(state->buffer, state->bytes); new_data = realloc(state->buffer, state->bytes);
if (!new_data) { if (!new_data) {
state->errcode = IMAGING_CODEC_MEMORY; state->errcode = IMAGING_CODEC_MEMORY;
goto decode_err; goto decode_err;
} }
state->buffer = new_data; state->buffer = new_data;
TRACE(("TIFFTileSize: %d\n", state->bytes)); TRACE(("TIFFTileSize: %d\n", state->bytes));
for (y = state->yoff; y < state->ysize; y += tile_length) { for (y = state->yoff; y < state->ysize; y += tile_length) {
int plane; int plane;
for (plane = 0; plane < planes; plane++) { for (plane = 0; plane < planes; plane++) {
ImagingShuffler shuffler = unpackers[plane]; ImagingShuffler shuffler = unpackers[plane];
for (x = state->xoff; x < state->xsize; x += tile_width) { for (x = state->xoff; x < state->xsize; x += tile_width) {
/* Sanity Check. Apparently in some cases, the TiffReadRGBA* functions /* Sanity Check. Apparently in some cases, the TiffReadRGBA* functions
have a different view of the size of the tiff than we're getting from have a different view of the size of the tiff than we're getting from
other functions. So, we need to check here. other functions. So, we need to check here.
*/ */
if (!TIFFCheckTile(tiff, x, y, 0, plane)) { if (!TIFFCheckTile(tiff, x, y, 0, plane)) {
TRACE(("Check Tile Error, Tile at %dx%d\n", x, y)); TRACE(("Check Tile Error, Tile at %dx%d\n", x, y));
state->errcode = IMAGING_CODEC_BROKEN;
goto decode_err;
}
if (isYCbCr) {
/* To avoid dealing with YCbCr subsampling, let libtiff handle it */
if (!TIFFReadRGBATile(tiff, x, y, (UINT32 *)state->buffer)) {
TRACE(("Decode Error, Tile at %dx%d\n", x, y));
state->errcode = IMAGING_CODEC_BROKEN; state->errcode = IMAGING_CODEC_BROKEN;
goto decode_err; goto decode_err;
} }
#if WORDS_BIGENDIAN
TIFFSwabArrayOfLong((UINT32 *)state->buffer, tile_width * tile_length);
#endif
} else {
if (TIFFReadTile(tiff, (tdata_t)state->buffer, x, y, 0, plane) == -1) { if (TIFFReadTile(tiff, (tdata_t)state->buffer, x, y, 0, plane) == -1) {
TRACE(("Decode Error, Tile at %dx%d\n", x, y)); TRACE(("Decode Error, Tile at %dx%d\n", x, y));
state->errcode = IMAGING_CODEC_BROKEN; state->errcode = IMAGING_CODEC_BROKEN;
goto decode_err; goto decode_err;
} }
}
TRACE(("Read tile at %dx%d; \n\n", x, y)); TRACE(("Read tile at %dx%d; \n\n", x, y));
current_tile_width = min((INT32) tile_width, state->xsize - x); current_tile_width = min((INT32) tile_width, state->xsize - x);
current_tile_length = min((INT32) tile_length, state->ysize - y); current_tile_length = min((INT32) tile_length, state->ysize - y);
// iterate over each line in the tile and stuff data into image // iterate over each line in the tile and stuff data into image
for (tile_y = 0; tile_y < current_tile_length; tile_y++) { 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)); 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; // 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])); // TRACE(("chars: %x%x%x%x\n", ((UINT8 *)bbb)[0], ((UINT8 *)bbb)[1], ((UINT8 *)bbb)[2], ((UINT8 *)bbb)[3]));
/*
* For some reason the TIFFReadRGBATile() function
* chooses the lower left corner as the origin.
* Vertically mirror by shuffling the scanlines
* backwards
*/
if (isYCbCr) {
current_line = tile_length - tile_y - 1;
} else {
current_line = tile_y; current_line = tile_y;
}
shuffler((UINT8*) im->image[tile_y + y] + x * im->pixelsize, shuffler((UINT8*) im->image[tile_y + y] + x * im->pixelsize,
state->buffer + current_line * row_byte_size, state->buffer + current_line * row_byte_size,
current_tile_width current_tile_width
); );
}
} }
} }
} }
} }
} else { else {
if (!isYCbCr) {
_decodeStrip(im, state, tiff, planes, unpackers); _decodeStrip(im, state, tiff, planes, unpackers);
} }
else {
_decodeStripYCbCr(im, state, tiff);
}
}
if (!state->errcode) { if (!state->errcode) {
// Check if raw mode was RGBa and it was stored on separate planes // Check if raw mode was RGBa and it was stored on separate planes
// so we have to convert it to RGBA // so we have to convert it to RGBA
if (planes > 3 && strcmp(im->mode, "RGBA") == 0) { if (planes > 3 && strcmp(im->mode, "RGBA") == 0) {
uint16 extrasamples; uint16 extrasamples;
uint16* sampleinfo; uint16* sampleinfo;
ImagingShuffler shuffle; ImagingShuffler shuffle;
INT32 y; INT32 y;
TIFFGetFieldDefaulted(tiff, TIFFTAG_EXTRASAMPLES, &extrasamples, &sampleinfo); TIFFGetFieldDefaulted(tiff, TIFFTAG_EXTRASAMPLES, &extrasamples, &sampleinfo);
if (extrasamples >= 1 && if (extrasamples >= 1 &&
(sampleinfo[0] == EXTRASAMPLE_UNSPECIFIED || sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA) (sampleinfo[0] == EXTRASAMPLE_UNSPECIFIED || sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA)
) { ) {
shuffle = ImagingFindUnpacker("RGBA", "RGBa", NULL); shuffle = ImagingFindUnpacker("RGBA", "RGBa", NULL);
for (y = state->yoff; y < state->ysize; y++) { for (y = state->yoff; y < state->ysize; y++) {
UINT8* ptr = (UINT8*) im->image[y + state->yoff] + UINT8* ptr = (UINT8*) im->image[y + state->yoff] +
state->xoff * im->pixelsize; state->xoff * im->pixelsize;
shuffle(ptr, ptr, state->xsize); shuffle(ptr, ptr, state->xsize);
}
} }
} }
} }