From 18d6432036784858ed12f12fa03f144756442d61 Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Thu, 27 Mar 2014 08:35:38 +0000 Subject: [PATCH] Fixed some problems with the reduce option, as well as a number of warnings from GCC. --- PIL/Jpeg2KImagePlugin.py | 6 +++--- libImaging/Incremental.c | 11 ++++++++--- libImaging/Jpeg2KDecode.c | 16 +++++++++++----- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/PIL/Jpeg2KImagePlugin.py b/PIL/Jpeg2KImagePlugin.py index cd72107a8..f57f4a784 100644 --- a/PIL/Jpeg2KImagePlugin.py +++ b/PIL/Jpeg2KImagePlugin.py @@ -169,14 +169,14 @@ class Jpeg2KImageFile(ImageFile.ImageFile): if self.reduce: power = 1 << self.reduce adjust = power >> 1 - self.size = ((self.size[0] + adjust) / power, - (self.size[1] + adjust) / power) + self.size = (int((self.size[0] + adjust) / power), + int((self.size[1] + adjust) / power)) if self.tile: # Update the reduce and layers settings t = self.tile[0] t3 = (t[3][0], self.reduce, self.layers, t[3][3]) - self.tile = [(t[0], t[1], t[2], t3)] + self.tile = [(t[0], (0, 0) + self.size, t[2], t3)] ImageFile.ImageFile.load(self) diff --git a/libImaging/Incremental.c b/libImaging/Incremental.c index 9574d51a8..9e7fb38ec 100644 --- a/libImaging/Incremental.c +++ b/libImaging/Incremental.c @@ -130,6 +130,9 @@ codec_thread(void *ptr) static void flush_stream(ImagingIncrementalCodec codec) { + UINT8 *buffer; + size_t bytes; + /* This is to flush data from the write buffer for a seekable write codec. */ if (codec->read_or_write != INCREMENTAL_CODEC_WRITE @@ -140,8 +143,8 @@ flush_stream(ImagingIncrementalCodec codec) DEBUG("flushing data\n"); - UINT8 *buffer = codec->stream.buffer; - size_t bytes = codec->stream.ptr - codec->stream.buffer; + buffer = codec->stream.buffer; + bytes = codec->stream.ptr - codec->stream.buffer; codec->state->errcode = 0; codec->seekable = INCREMENTAL_CODEC_NOT_SEEKABLE; @@ -645,6 +648,8 @@ off_t ImagingIncrementalCodecSeek(ImagingIncrementalCodec codec, off_t bytes) { + off_t buffered; + DEBUG("seeking (going to %llu bytes)\n", (unsigned long long)bytes); if (codec->stream.fd >= 0) @@ -660,7 +665,7 @@ ImagingIncrementalCodecSeek(ImagingIncrementalCodec codec, return -1; } - off_t buffered = codec->stream.top - codec->stream.buffer; + buffered = codec->stream.top - codec->stream.buffer; if (bytes <= buffered) { DEBUG("seek within buffer\n"); diff --git a/libImaging/Jpeg2KDecode.c b/libImaging/Jpeg2KDecode.c index dd20dc738..6b6176c78 100644 --- a/libImaging/Jpeg2KDecode.c +++ b/libImaging/Jpeg2KDecode.c @@ -230,7 +230,7 @@ j2ku_graya_la(opj_image_t *in, const JPEG2KTILEINFO *tileinfo, const UINT8 *adata = &atiledata[acsiz * y * w]; UINT8 *row = (UINT8 *)im->image[y0 + y] + x0 * 4; for (x = 0; x < w; ++x) { - UINT32 word = 0, aword = 0; + UINT32 word = 0, aword = 0, byte; switch (csiz) { case 1: word = *data++; break; @@ -244,7 +244,7 @@ j2ku_graya_la(opj_image_t *in, const JPEG2KTILEINFO *tileinfo, case 4: aword = *(const UINT32 *)adata; adata += 4; break; } - UINT8 byte = j2ku_shift(offset + word, shift); + byte = j2ku_shift(offset + word, shift); row[0] = row[1] = row[2] = byte; row[3] = j2ku_shift(aoffset + aword, ashift); row += 4; @@ -552,9 +552,7 @@ j2k_decode_entry(Imaging im, ImagingCodecState state, } for (n = 1; n < image->numcomps; ++n) { - /* Check that the sample frequency is uniform */ - if (image->comps[0].dx != image->comps[n].dx - || image->comps[0].dy != image->comps[n].dy) { + if (image->comps[n].dx != 1 || image->comps[n].dy != 1) { state->errcode = IMAGING_CODEC_BROKEN; state->state = J2K_STATE_FAILED; goto quick_exit; @@ -612,6 +610,7 @@ j2k_decode_entry(Imaging im, ImagingCodecState state, for (;;) { JPEG2KTILEINFO tile_info; OPJ_BOOL should_continue; + unsigned correction = (1 << params.cp_reduce) - 1; if (!opj_read_tile_header(codec, stream, @@ -629,6 +628,13 @@ j2k_decode_entry(Imaging im, ImagingCodecState state, if (!should_continue) break; + /* Adjust the tile co-ordinates based on the reduction (OpenJPEG + doesn't do this for us) */ + tile_info.x0 = (tile_info.x0 + correction) >> context->reduce; + tile_info.y0 = (tile_info.y0 + correction) >> context->reduce; + tile_info.x1 = (tile_info.x1 + correction) >> context->reduce; + tile_info.y1 = (tile_info.y1 + correction) >> context->reduce; + if (buffer_size < tile_info.data_size) { UINT8 *new = realloc (state->buffer, tile_info.data_size); if (!new) {