Skip to content

Commit

Permalink
Merge pull request #2 from radarhere/kk-planar-tiffs
Browse files Browse the repository at this point in the history
Fixed macOS exception
  • Loading branch information
kkopachev authored May 26, 2020
2 parents d4c3a90 + efff57b commit 0f35e9b
Showing 1 changed file with 23 additions and 34 deletions.
57 changes: 23 additions & 34 deletions src/libImaging/TiffDecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,26 +287,8 @@ int ImagingLibTiffDecode(Imaging im, ImagingCodecState state, UINT8* buffer, Py_
TIFF *tiff;
uint16 photometric = 0; // init to not PHOTOMETRIC_YCBCR
UINT8 planarconfig;
UINT8 plane, planes = 1;
// We'll pick appropriate set of unpackers depending on planar_configuration
ImagingShuffler single_plane_unpacker[4] = {state->shuffle},
rgba8_unpack[4] = {
// It does not matter if data is RGB(A), CMYK or LUV really,
// we just copy it plane by plane
ImagingFindUnpacker("RGBA", "R", NULL),
ImagingFindUnpacker("RGBA", "G", NULL),
ImagingFindUnpacker("RGBA", "B", NULL),
ImagingFindUnpacker("RGBA", "A", NULL)
},
rgba16_unpack[4] = {
ImagingFindUnpacker("RGBA", "R;16N", NULL),
ImagingFindUnpacker("RGBA", "G;16N", NULL),
ImagingFindUnpacker("RGBA", "B;16N", NULL),
ImagingFindUnpacker("RGBA", "A;16N", NULL)
};

// Default to single plane unpacker
ImagingShuffler (*unpacker)[4] = &single_plane_unpacker;
UINT8 planes = 1;
ImagingShuffler unpackers[4];

/* 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 */
Expand Down Expand Up @@ -377,21 +359,24 @@ int ImagingLibTiffDecode(Imaging im, ImagingCodecState state, UINT8* buffer, Py_
uint16 bps = 8;

TIFFGetFieldDefaulted(tiff, TIFFTAG_BITSPERSAMPLE, &bps);
switch (bps) {
case 8:
unpacker = &rgba8_unpack;
break;
case 16:
unpacker = &rgba16_unpack;
break;
default:
TRACE(("Invalid value for bits per sample: %d\n", bps));
state->errcode = IMAGING_CODEC_BROKEN;
TIFFClose(tiff);
return -1;
if (bps != 8 && bps != 16) {
TRACE(("Invalid value for bits per sample: %d\n", bps));
state->errcode = IMAGING_CODEC_BROKEN;
TIFFClose(tiff);
return -1;
}

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", bps == 16 ? "R;16N" : "R", NULL);
unpackers[1] = ImagingFindUnpacker("RGBA", bps == 16 ? "G;16N" : "G", NULL);
unpackers[2] = ImagingFindUnpacker("RGBA", bps == 16 ? "B;16N" : "B", NULL);
unpackers[3] = ImagingFindUnpacker("RGBA", bps == 16 ? "A;16N" : "A", NULL);
} else {
unpackers[0] = state->shuffle;
}

if (TIFFIsTiled(tiff)) {
Expand Down Expand Up @@ -435,7 +420,9 @@ int ImagingLibTiffDecode(Imaging im, ImagingCodecState state, UINT8* buffer, Py_
TRACE(("TIFFTileSize: %d\n", state->bytes));

for (y = state->yoff; y < state->ysize; y += tile_length) {
UINT8 plane;
for (plane = 0; plane < planes; plane++) {
ImagingShuffler shuffler = unpackers[plane];
for (x = state->xoff; x < state->xsize; x += tile_width) {
if (ReadTile(tiff, x, y, plane, (UINT32*) state->buffer) == -1) {
TRACE(("Decode Error, Tile at %dx%d\n", x, y));
Expand All @@ -455,7 +442,7 @@ int ImagingLibTiffDecode(Imaging im, ImagingCodecState state, UINT8* buffer, Py_
// 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]));

((*unpacker)[plane])((UINT8*) im->image[tile_y + y] + x * im->pixelsize,
shuffler((UINT8*) im->image[tile_y + y] + x * im->pixelsize,
state->buffer + tile_y * row_byte_size,
current_tile_width
);
Expand Down Expand Up @@ -511,7 +498,9 @@ int ImagingLibTiffDecode(Imaging im, ImagingCodecState state, UINT8* buffer, Py_
state->buffer = new_data;

for (; state->y < state->ysize; state->y += rows_per_strip) {
UINT8 plane;
for (plane = 0; plane < planes; plane++) {
ImagingShuffler shuffler = unpackers[plane];
if (ReadStrip(tiff, state->y, plane, (UINT32 *)state->buffer) == -1) {
TRACE(("Decode Error, strip %d\n", TIFFComputeStrip(tiff, state->y, 0)));
state->errcode = IMAGING_CODEC_BROKEN;
Expand All @@ -528,7 +517,7 @@ int ImagingLibTiffDecode(Imaging im, ImagingCodecState state, UINT8* buffer, Py_
// 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]));

((*unpacker)[plane])((UINT8*) im->image[state->y + state->yoff + strip_row] +
shuffler((UINT8*) im->image[state->y + state->yoff + strip_row] +
state->xoff * im->pixelsize,
state->buffer + strip_row * row_byte_size,
state->xsize);
Expand Down

0 comments on commit 0f35e9b

Please sign in to comment.