Merge pull request #268 from megabuz/master

Add several TIFF decoders and encoders
This commit is contained in:
Alex Clark ☺ 2013-07-01 09:14:43 -07:00
commit fd98b0cceb
7 changed files with 77 additions and 5 deletions

View File

@ -118,8 +118,13 @@ COMPRESSION_INFO = {
5: "tiff_lzw",
6: "tiff_jpeg", # obsolete
7: "jpeg",
8: "tiff_adobe_deflate",
32771: "tiff_raw_16", # 16-bit padding
32773: "packbits"
32773: "packbits",
32809: "tiff_thunderscan",
32946: "tiff_deflate",
34676: "tiff_sgilog",
34677: "tiff_sgilog24",
}
COMPRESSION_INFO_REV = dict([(v,k) for (k,v) in COMPRESSION_INFO.items()])
@ -746,8 +751,11 @@ class TiffImageFile(ImageFile.ImageFile):
offsets = self.tag[STRIPOFFSETS]
h = getscalar(ROWSPERSTRIP, ysize)
w = self.size[0]
if self._compression in ["tiff_ccitt", "group3",
"group4", "tiff_raw_16"]:
if self._compression in ["tiff_ccitt", "group3", "group4",
"tiff_jpeg", "tiff_adobe_deflate",
"tiff_thunderscan", "tiff_deflate",
"tiff_sgilog", "tiff_sgilog24",
"tiff_raw_16"]:
## if Image.DEBUG:
## print "Activating g4 compression for whole file"
@ -878,8 +886,11 @@ def _save(im, fp, filename):
ifd = ImageFileDirectory(prefix)
compression = im.info.get('compression','raw')
libtiff = compression in ["tiff_ccitt", "group3",
"group4", "tiff_raw_16"]
libtiff = compression in ["tiff_ccitt", "group3", "group4",
"tiff_jpeg", "tiff_adobe_deflate",
"tiff_thunderscan", "tiff_deflate",
"tiff_sgilog", "tiff_sgilog24",
"tiff_raw_16"]
# -- multi-page -- skip TIFF header on subsequent pages
if not libtiff and fp.tell() == 0:

Binary file not shown.

View File

@ -71,3 +71,12 @@ def test_xyres_tiff():
im.tag.tags[Y_RESOLUTION] = (72,)
im._setup()
assert_equal(im.info['dpi'], (72., 72.))
def test_adobe_deflate_tiff():
file = "Tests/images/tiff_adobe_deflate.tif"
im = Image.open(file)
assert_equal(im.mode, "RGB")
assert_equal(im.size, (278, 374))
assert_equal(im.tile[0][:3], ('tiff_adobe_deflate', (0, 0, 278, 374), 0))
assert_no_exception(lambda: im.load())

View File

@ -3313,11 +3313,23 @@ static PyMethodDef functions[] = {
{"tiff_ccitt_decoder", (PyCFunction)PyImaging_LibTiffDecoderNew, 1},
{"group3_decoder", (PyCFunction)PyImaging_LibTiffDecoderNew, 1},
{"group4_decoder", (PyCFunction)PyImaging_LibTiffDecoderNew, 1},
{"tiff_jpeg_decoder", (PyCFunction)PyImaging_LibTiffDecoderNew, 1},
{"tiff_adobe_deflate_decoder", (PyCFunction)PyImaging_LibTiffDecoderNew, 1},
{"tiff_thunderscan_decoder", (PyCFunction)PyImaging_LibTiffDecoderNew, 1},
{"tiff_deflate_decoder", (PyCFunction)PyImaging_LibTiffDecoderNew, 1},
{"tiff_sgilog_decoder", (PyCFunction)PyImaging_LibTiffDecoderNew, 1},
{"tiff_sgilog24_decoder", (PyCFunction)PyImaging_LibTiffDecoderNew, 1},
{"tiff_raw_16_decoder", (PyCFunction)PyImaging_LibTiffDecoderNew, 1},
{"tiff_ccitt_encoder", (PyCFunction)PyImaging_LibTiffEncoderNew, 1},
{"group3_encoder", (PyCFunction)PyImaging_LibTiffEncoderNew, 1},
{"group4_encoder", (PyCFunction)PyImaging_LibTiffEncoderNew, 1},
{"tiff_jpeg_encoder", (PyCFunction)PyImaging_LibTiffEncoderNew, 1},
{"tiff_adobe_deflate_encoder", (PyCFunction)PyImaging_LibTiffEncoderNew, 1},
{"tiff_thunderscan_encoder", (PyCFunction)PyImaging_LibTiffEncoderNew, 1},
{"tiff_deflate_encoder", (PyCFunction)PyImaging_LibTiffEncoderNew, 1},
{"tiff_sgilog_encoder", (PyCFunction)PyImaging_LibTiffEncoderNew, 1},
{"tiff_sgilog24_encoder", (PyCFunction)PyImaging_LibTiffEncoderNew, 1},
{"tiff_raw_16_encoder", (PyCFunction)PyImaging_LibTiffEncoderNew, 1},
#endif
{"msp_decoder", (PyCFunction)PyImaging_MspDecoderNew, 1},

View File

@ -449,6 +449,24 @@ PyImaging_LibTiffDecoderNew(PyObject* self, PyObject* args)
} else if (strcasecmp(compname, "group4") == 0) {
compression = COMPRESSION_CCITTFAX4;
} else if (strcasecmp(compname, "tiff_jpeg") == 0) {
compression = COMPRESSION_OJPEG;
} else if (strcasecmp(compname, "tiff_adobe_deflate") == 0) {
compression = COMPRESSION_ADOBE_DEFLATE;
} else if (strcasecmp(compname, "tiff_thunderscan") == 0) {
compression = COMPRESSION_THUNDERSCAN;
} else if (strcasecmp(compname, "tiff_deflate") == 0) {
compression = COMPRESSION_DEFLATE;
} else if (strcasecmp(compname, "tiff_sgilog") == 0) {
compression = COMPRESSION_SGILOG;
} else if (strcasecmp(compname, "tiff_sgilog24") == 0) {
compression = COMPRESSION_SGILOG24;
} else if (strcasecmp(compname, "tiff_raw_16") == 0) {
compression = COMPRESSION_CCITTRLEW;

View File

@ -719,6 +719,24 @@ PyImaging_LibTiffEncoderNew(PyObject* self, PyObject* args)
} else if (strcasecmp(compname, "group4") == 0) {
compression = COMPRESSION_CCITTFAX4;
} else if (strcasecmp(compname, "tiff_jpeg") == 0) {
compression = COMPRESSION_OJPEG;
} else if (strcasecmp(compname, "tiff_adobe_deflate") == 0) {
compression = COMPRESSION_ADOBE_DEFLATE;
} else if (strcasecmp(compname, "tiff_thunderscan") == 0) {
compression = COMPRESSION_THUNDERSCAN;
} else if (strcasecmp(compname, "tiff_deflate") == 0) {
compression = COMPRESSION_DEFLATE;
} else if (strcasecmp(compname, "tiff_sgilog") == 0) {
compression = COMPRESSION_SGILOG;
} else if (strcasecmp(compname, "tiff_sgilog24") == 0) {
compression = COMPRESSION_SGILOG24;
} else if (strcasecmp(compname, "tiff_raw_16") == 0) {
compression = COMPRESSION_CCITTRLEW;

View File

@ -197,6 +197,10 @@ int ImagingLibTiffDecode(Imaging im, ImagingCodecState state, UINT8* buffer, int
clientstate->flrealloc = 0;
dump_state(clientstate);
TIFFSetWarningHandler(NULL);
TIFFSetWarningHandlerExt(NULL);
if (clientstate->fp) {
TRACE(("Opening using fd: %d\n",clientstate->fp));
lseek(clientstate->fp,0,SEEK_SET); // Sometimes, I get it set to the end.