merge from master

This commit is contained in:
wiredfool 2013-05-21 21:29:58 -07:00
commit b3752ca053
14 changed files with 550 additions and 345 deletions

BIN
Images/transparent.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

BIN
Images/transparent.webp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

View File

@ -209,6 +209,10 @@ class ImageFile(Image.Image):
if not s: # truncated jpeg if not s: # truncated jpeg
self.tile = [] self.tile = []
# JpegDecode needs to clean things up here either way
# If we don't destroy the decompressor, we have a memory leak.
d.cleanup()
if LOAD_TRUNCATED_IMAGES: if LOAD_TRUNCATED_IMAGES:
break break
else: else:

View File

@ -554,6 +554,7 @@ def _save(im, fp, filename):
info.get("exif", b"") info.get("exif", b"")
) )
# if we optimize, libjpeg needs a buffer big enough to hold the whole image in a shot. # if we optimize, libjpeg needs a buffer big enough to hold the whole image in a shot.
# Guessing on the size, at im.size bytes. (raw pizel size is channels*size, this # Guessing on the size, at im.size bytes. (raw pizel size is channels*size, this
# is a value that's been used in a django patch. # is a value that's been used in a django patch.
@ -561,6 +562,10 @@ def _save(im, fp, filename):
bufsize=0 bufsize=0
if "optimize" in info: if "optimize" in info:
bufsize = im.size[0]*im.size[1] bufsize = im.size[0]*im.size[1]
# The exif info needs to be written as one block, + APP1, + one spare byte.
# Ensure that our buffer is big enough
bufsize = max(ImageFile.MAXBLOCK, bufsize, len(info.get("exif",b"")) + 5 )
ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)], bufsize) ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)], bufsize)

View File

@ -3,29 +3,55 @@ from PIL import ImageFile
from io import BytesIO from io import BytesIO
from PIL import _webp from PIL import _webp
_VALID_WEBP_MODES = {
"RGB": True,
"RGBA": True,
}
_VP8_MODES_BY_IDENTIFIER = {
b"VP8 ": "RGB",
b"VP8X": "RGBA",
}
def _accept(prefix): def _accept(prefix):
return prefix[:4] == b"RIFF" and prefix[8:16] == b"WEBPVP8 " is_riff_file_format = prefix[:4] == b"RIFF"
is_webp_file = prefix[8:12] == b"WEBP"
is_valid_vp8_mode = prefix[12:16] in _VP8_MODES_BY_IDENTIFIER
return is_riff_file_format and is_webp_file and is_valid_vp8_mode
class WebPImageFile(ImageFile.ImageFile): class WebPImageFile(ImageFile.ImageFile):
format = "WEBP" format = "WEBP"
format_description = "WebP image" format_description = "WebP image"
def _open(self): def _open(self):
self.mode = "RGB" data, width, height, self.mode = _webp.WebPDecode(self.fp.read())
data, width, height = _webp.WebPDecodeRGB(self.fp.read())
self.size = width, height self.size = width, height
self.fp = BytesIO(data) self.fp = BytesIO(data)
self.tile = [("raw", (0, 0) + self.size, 0, 'RGB')] self.tile = [("raw", (0, 0) + self.size, 0, self.mode)]
def _save(im, fp, filename): def _save(im, fp, filename):
if im.mode != "RGB": image_mode = im.mode
raise IOError("cannot write mode %s as WEBP" % im.mode) if im.mode not in _VALID_WEBP_MODES:
raise IOError("cannot write mode %s as WEBP" % image_mode)
quality = im.encoderinfo.get("quality", 80) quality = im.encoderinfo.get("quality", 80)
data = _webp.WebPEncodeRGB(im.tobytes(), im.size[0], im.size[1], im.size[0] * 3, float(quality)) data = _webp.WebPEncode(
im.tobytes(),
im.size[0],
im.size[1],
float(quality),
im.mode
)
fp.write(data) fp.write(data)
Image.register_open("WEBP", WebPImageFile, _accept) Image.register_open("WEBP", WebPImageFile, _accept)
Image.register_save("WEBP", _save) Image.register_save("WEBP", _save)

View File

@ -21,8 +21,6 @@ The fork author's goal is to foster active development of PIL through:
Porting your PIL code to Pillow Porting your PIL code to Pillow
------------------------------- -------------------------------
.. Note:: PIL and Pillow currently cannot co-exist in the same environment. If you want to use Pillow, please remove PIL first.
Pillow is a functional drop-in replacement for the Python Imaging Library. To run your existing PIL-compatible code with Pillow, it needs to be modified to import the ``Imaging`` module from the ``PIL`` namespace *instead* of the global namespace. I.e. change:: Pillow is a functional drop-in replacement for the Python Imaging Library. To run your existing PIL-compatible code with Pillow, it needs to be modified to import the ``Imaging`` module from the ``PIL`` namespace *instead* of the global namespace. I.e. change::
import Image import Image
@ -76,6 +74,8 @@ PIL needs you! Please help us maintain the Python Imaging Library here:
Installation Installation
------------ ------------
.. Note:: PIL and Pillow currently cannot co-exist in the same environment. If you want to use Pillow, please remove PIL first.
Platform support Platform support
~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~
@ -134,7 +134,9 @@ Some (most?) of Pillow's features require external libraries.
* **littlecms** provides color management * **littlecms** provides color management
* **libwebp** provides the Webp format. * **libwebp** provides the Webp format.
* Pillow has been tested with version **0.1.3**, which does not read transparent webp files. Version **0.3.0** supports transparency.
If the prerequisites are installed in the standard library locations for your machine (e.g. /usr or /usr/local), no additional configuration should be required. If they are installed in a non-standard location, you may need to configure setuptools to use those locations (i.e. by editing setup.py and/or setup.cfg) If the prerequisites are installed in the standard library locations for your machine (e.g. /usr or /usr/local), no additional configuration should be required. If they are installed in a non-standard location, you may need to configure setuptools to use those locations (i.e. by editing setup.py and/or setup.cfg)

View File

@ -120,6 +120,12 @@ def test_optimize_large_buffer():
im = Image.new("RGB", (4096,4096), 0xff3333) im = Image.new("RGB", (4096,4096), 0xff3333)
im.save(f, format="JPEG", optimize=True) im.save(f, format="JPEG", optimize=True)
def test_large_exif():
#https://github.com/python-imaging/Pillow/issues/148
f = tempfile('temp.jpg')
im = lena()
im.save(f,'JPEG', quality=90, exif=b"1"*65532)
def test_progressive(): def test_progressive():
im1 = roundtrip(lena()) im1 = roundtrip(lena())
im2 = roundtrip(lena(), progressive=1) im2 = roundtrip(lena(), progressive=1)

View File

@ -6,54 +6,108 @@ try:
from PIL import _webp from PIL import _webp
except: except:
skip('webp support not installed') skip('webp support not installed')
def test_read():
""" Can we write a webp without error. Does it have the bits we expect?""" def test_version():
assert_no_exception(lambda: _webp.WebPDecoderVersion())
def test_good_alpha():
assert_equal(_webp.WebPDecoderBuggyAlpha(), 0)
file = "Images/lena.webp"
im = Image.open(file)
assert_equal(im.mode, "RGB")
assert_equal(im.size, (128, 128))
assert_equal(im.format, "WEBP")
assert_no_exception(lambda: im.load())
assert_no_exception(lambda: im.getdata())
orig_bytes = im.tobytes()
def test_read_rgb():
file_path = "Images/lena.webp"
image = Image.open(file_path)
assert_equal(image.mode, "RGB")
assert_equal(image.size, (128, 128))
assert_equal(image.format, "WEBP")
assert_no_exception(lambda: image.load())
assert_no_exception(lambda: image.getdata())
# generated with: dwebp -ppm ../../Images/lena.webp -o lena_webp_bits.ppm # generated with: dwebp -ppm ../../Images/lena.webp -o lena_webp_bits.ppm
target = Image.open('Tests/images/lena_webp_bits.ppm') target = Image.open('Tests/images/lena_webp_bits.ppm')
assert_image_equal(im, target) assert_image_equal(image, target)
def test_write(): def test_write_rgb():
""" Can we write a webp without error. Does it have the bits we expect?""" """
Can we write a RGB mode file to webp without error. Does it have the bits we
file = tempfile("temp.webp") expect?
lena("RGB").save(file) """
im= Image.open(file) temp_file = tempfile("temp.webp")
im.load()
lena("RGB").save(temp_file)
assert_equal(im.mode, "RGB")
assert_equal(im.size, (128, 128)) image = Image.open(temp_file)
assert_equal(im.format, "WEBP") image.load()
assert_no_exception(lambda: im.load())
assert_no_exception(lambda: im.getdata()) assert_equal(image.mode, "RGB")
assert_equal(image.size, (128, 128))
assert_equal(image.format, "WEBP")
assert_no_exception(lambda: image.load())
assert_no_exception(lambda: image.getdata())
# If we're using the exact same version of webp, this test should pass. # If we're using the exact same version of webp, this test should pass.
# but it doesn't if the webp is generated on Ubuntu and tested on Fedora. # but it doesn't if the webp is generated on Ubuntu and tested on Fedora.
# generated with: dwebp -ppm temp.webp -o lena_webp_write.ppm # generated with: dwebp -ppm temp.webp -o lena_webp_write.ppm
#target = Image.open('Tests/images/lena_webp_write.ppm') #target = Image.open('Tests/images/lena_webp_write.ppm')
#assert_image_equal(im, target) #assert_image_equal(image, target)
# This test asserts that the images are similar. If the average pixel difference # This test asserts that the images are similar. If the average pixel difference
# between the two images is less than the epsilon value, then we're going to # between the two images is less than the epsilon value, then we're going to
# accept that it's a reasonable lossy version of the image. The included lena images # accept that it's a reasonable lossy version of the image. The included lena images
# for webp are showing ~16 on Ubuntu, the jpegs are showing ~18. # for webp are showing ~16 on Ubuntu, the jpegs are showing ~18.
target = lena('RGB') target = lena("RGB")
assert_image_similar(im, target, 20.0) assert_image_similar(image, target, 20.0)
def test_write_rgba():
"""
Can we write a RGBA mode file to webp without error. Does it have the bits we
expect?
"""
temp_file = tempfile("temp.webp")
pil_image = Image.new("RGBA", (10, 10), (255, 0, 0, 20))
pil_image.save(temp_file)
if _webp.WebPDecoderBuggyAlpha():
return
image = Image.open(temp_file)
image.load()
assert_equal(image.mode, "RGBA")
assert_equal(image.size, (10, 10))
assert_equal(image.format, "WEBP")
assert_no_exception(image.load)
assert_no_exception(image.getdata)
assert_image_similar(image, pil_image, 1.0)
if _webp.WebPDecoderBuggyAlpha():
skip("Buggy early version of webp installed, not testing transparency")
def test_read_rgba():
# Generated with `cwebp transparent.png -o transparent.webp`
file_path = "Images/transparent.webp"
image = Image.open(file_path)
assert_equal(image.mode, "RGBA")
assert_equal(image.size, (200, 150))
assert_equal(image.format, "WEBP")
assert_no_exception(lambda: image.load())
assert_no_exception(lambda: image.getdata())
orig_bytes = image.tobytes()
target = Image.open('Images/transparent.png')
assert_image_similar(image, target, 20.0)

113
_webp.c
View File

@ -2,73 +2,128 @@
#include "py3.h" #include "py3.h"
#include <webp/encode.h> #include <webp/encode.h>
#include <webp/decode.h> #include <webp/decode.h>
#include <webp/types.h>
PyObject* WebPEncodeRGB_wrapper(PyObject* self, PyObject* args)
PyObject* WebPEncode_wrapper(PyObject* self, PyObject* args)
{ {
PyBytesObject *rgb_string;
int width; int width;
int height; int height;
int stride;
float quality_factor; float quality_factor;
uint8_t *rgb; uint8_t *rgb;
uint8_t *output; uint8_t *output;
char *mode;
Py_ssize_t size; Py_ssize_t size;
size_t ret_size; size_t ret_size;
if (!PyArg_ParseTuple(args, "Siiif", &rgb_string, &width, &height, &stride, &quality_factor)) { if (!PyArg_ParseTuple(args, "s#iifs",(char**)&rgb, &size, &width, &height, &quality_factor, &mode)) {
Py_INCREF(Py_None); Py_RETURN_NONE;
return Py_None;
} }
PyBytes_AsStringAndSize((PyObject *) rgb_string, (char**)&rgb, &size); if (strcmp(mode, "RGBA")==0){
if (size < width * height * 4){
Py_RETURN_NONE;
}
ret_size = WebPEncodeRGBA(rgb, width, height, 4* width, quality_factor, &output);
} else if (strcmp(mode, "RGB")==0){
if (size < width * height * 3){
Py_RETURN_NONE;
}
ret_size = WebPEncodeRGB(rgb, width, height, 3* width, quality_factor, &output);
} else {
Py_RETURN_NONE;
}
if (stride * height > size) {
Py_INCREF(Py_None);
return Py_None;
}
ret_size = WebPEncodeRGB(rgb, width, height, stride, quality_factor, &output);
if (ret_size > 0) { if (ret_size > 0) {
PyObject *ret = PyBytes_FromStringAndSize((char*)output, ret_size); PyObject *ret = PyBytes_FromStringAndSize((char*)output, ret_size);
free(output); free(output);
return ret; return ret;
} }
Py_INCREF(Py_None); Py_RETURN_NONE;
return Py_None;
} }
PyObject* WebPDecodeRGB_wrapper(PyObject* self, PyObject* args)
PyObject* WebPDecode_wrapper(PyObject* self, PyObject* args)
{ {
PyBytesObject *webp_string; PyBytesObject *webp_string;
int width;
int height;
uint8_t *webp; uint8_t *webp;
uint8_t *output;
Py_ssize_t size; Py_ssize_t size;
PyObject *ret; PyObject *ret, *bytes, *pymode;
WebPDecoderConfig config;
VP8StatusCode vp8_status_code = VP8_STATUS_OK;
char* mode = "RGB";
if (!PyArg_ParseTuple(args, "S", &webp_string)) { if (!PyArg_ParseTuple(args, "S", &webp_string)) {
Py_INCREF(Py_None); Py_RETURN_NONE;
return Py_None;
} }
if (!WebPInitDecoderConfig(&config)) {
Py_RETURN_NONE;
}
PyBytes_AsStringAndSize((PyObject *) webp_string, (char**)&webp, &size); PyBytes_AsStringAndSize((PyObject *) webp_string, (char**)&webp, &size);
output = WebPDecodeRGB(webp, size, &width, &height); vp8_status_code = WebPGetFeatures(webp, size, &config.input);
if (vp8_status_code == VP8_STATUS_OK) {
// If we don't set it, we don't get alpha.
// Initialized to MODE_RGB
if (config.input.has_alpha) {
config.output.colorspace = MODE_RGBA;
mode = "RGBA";
}
vp8_status_code = WebPDecode(webp, size, &config);
}
if (vp8_status_code != VP8_STATUS_OK) {
WebPFreeDecBuffer(&config.output);
Py_RETURN_NONE;
}
if (config.output.colorspace < MODE_YUV) {
bytes = PyBytes_FromStringAndSize((char *)config.output.u.RGBA.rgba,
config.output.u.RGBA.size);
} else {
// Skipping YUV for now. Need Test Images.
// UNDONE -- unclear if we'll ever get here if we set mode_rgb*
bytes = PyBytes_FromStringAndSize((char *)config.output.u.YUVA.y,
config.output.u.YUVA.y_size);
}
ret = PyBytes_FromStringAndSize((char*)output, width * height * 3); #if PY_VERSION_HEX >= 0x03000000
free(output); pymode = PyUnicode_FromString(mode);
return Py_BuildValue("Sii", ret, width, height); #else
pymode = PyString_FromString(mode);
#endif
ret = Py_BuildValue("SiiS", bytes, config.output.width,
config.output.height, pymode);
WebPFreeDecBuffer(&config.output);
return ret;
}
// Return the decoder's version number, packed in hexadecimal using 8bits for
// each of major/minor/revision. E.g: v2.5.7 is 0x020507.
PyObject* WebPDecoderVersion_wrapper(PyObject* self, PyObject* args){
return Py_BuildValue("i", WebPGetDecoderVersion());
}
/*
* The version of webp that ships with (0.1.3) Ubuntu 12.04 doesn't handle alpha well.
* Files that are valid with 0.3 are reported as being invalid.
*/
PyObject* WebPDecoderBuggyAlpha_wrapper(PyObject* self, PyObject* args){
return Py_BuildValue("i", WebPGetDecoderVersion()==0x0103);
} }
static PyMethodDef webpMethods[] = static PyMethodDef webpMethods[] =
{ {
{"WebPEncodeRGB", WebPEncodeRGB_wrapper, METH_VARARGS, "WebPEncodeRGB"}, {"WebPEncode", WebPEncode_wrapper, METH_VARARGS, "WebPEncode"},
{"WebPDecodeRGB", WebPDecodeRGB_wrapper, METH_VARARGS, "WebPEncodeRGB"}, {"WebPDecode", WebPDecode_wrapper, METH_VARARGS, "WebPDecode"},
{"WebPDecoderVersion", WebPDecoderVersion_wrapper, METH_VARARGS, "WebPVersion"},
{"WebPDecoderBuggyAlpha", WebPDecoderBuggyAlpha_wrapper, METH_VARARGS, "WebPDecoderBuggyAlpha"},
{NULL, NULL} {NULL, NULL}
}; };
#if PY_VERSION_HEX >= 0x03000000 #if PY_VERSION_HEX >= 0x03000000
PyMODINIT_FUNC PyMODINIT_FUNC
PyInit__webp(void) { PyInit__webp(void) {

240
decode.c
View File

@ -41,13 +41,14 @@
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* Common */ /* Common */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
int (*decode)(Imaging im, ImagingCodecState state, int (*decode)(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes); UINT8* buffer, int bytes);
int (*cleanup)(ImagingCodecState state);
struct ImagingCodecStateInstance state; struct ImagingCodecStateInstance state;
Imaging im; Imaging im;
PyObject* lock; PyObject* lock;
@ -66,21 +67,21 @@ PyImaging_DecoderNew(int contextsize)
decoder = PyObject_New(ImagingDecoderObject, &ImagingDecoderType); decoder = PyObject_New(ImagingDecoderObject, &ImagingDecoderType);
if (decoder == NULL) if (decoder == NULL)
return NULL; return NULL;
/* Clear the decoder state */ /* Clear the decoder state */
memset(&decoder->state, 0, sizeof(decoder->state)); memset(&decoder->state, 0, sizeof(decoder->state));
/* Allocate decoder context */ /* Allocate decoder context */
if (contextsize > 0) { if (contextsize > 0) {
context = (void*) calloc(1, contextsize); context = (void*) calloc(1, contextsize);
if (!context) { if (!context) {
Py_DECREF(decoder); Py_DECREF(decoder);
(void) PyErr_NoMemory(); (void) PyErr_NoMemory();
return NULL; return NULL;
} }
} else } else
context = 0; context = 0;
/* Initialize decoder context */ /* Initialize decoder context */
decoder->state.context = context; decoder->state.context = context;
@ -88,6 +89,9 @@ PyImaging_DecoderNew(int contextsize)
/* Target image */ /* Target image */
decoder->lock = NULL; decoder->lock = NULL;
decoder->im = NULL; decoder->im = NULL;
/* Initialize the cleanup function pointer */
decoder->cleanup = NULL;
return decoder; return decoder;
} }
@ -108,13 +112,27 @@ _decode(ImagingDecoderObject* decoder, PyObject* args)
int bufsize, status; int bufsize, status;
if (!PyArg_ParseTuple(args, PY_ARG_BYTES_LENGTH, &buffer, &bufsize)) if (!PyArg_ParseTuple(args, PY_ARG_BYTES_LENGTH, &buffer, &bufsize))
return NULL; return NULL;
status = decoder->decode(decoder->im, &decoder->state, buffer, bufsize); status = decoder->decode(decoder->im, &decoder->state, buffer, bufsize);
return Py_BuildValue("ii", status, decoder->state.errcode); return Py_BuildValue("ii", status, decoder->state.errcode);
} }
static PyObject*
_decode_cleanup(ImagingDecoderObject* decoder, PyObject* args)
{
int status = 0;
if (decoder->cleanup){
status = decoder->cleanup(&decoder->state);
}
return Py_BuildValue("i", status);
}
extern Imaging PyImaging_AsImaging(PyObject *op); extern Imaging PyImaging_AsImaging(PyObject *op);
static PyObject* static PyObject*
@ -129,10 +147,10 @@ _setimage(ImagingDecoderObject* decoder, PyObject* args)
/* FIXME: should publish the ImagingType descriptor */ /* FIXME: should publish the ImagingType descriptor */
if (!PyArg_ParseTuple(args, "O|(iiii)", &op, &x0, &y0, &x1, &y1)) if (!PyArg_ParseTuple(args, "O|(iiii)", &op, &x0, &y0, &x1, &y1))
return NULL; return NULL;
im = PyImaging_AsImaging(op); im = PyImaging_AsImaging(op);
if (!im) if (!im)
return NULL; return NULL;
decoder->im = im; decoder->im = im;
@ -140,30 +158,30 @@ _setimage(ImagingDecoderObject* decoder, PyObject* args)
/* Setup decoding tile extent */ /* Setup decoding tile extent */
if (x0 == 0 && x1 == 0) { if (x0 == 0 && x1 == 0) {
state->xsize = im->xsize; state->xsize = im->xsize;
state->ysize = im->ysize; state->ysize = im->ysize;
} else { } else {
state->xoff = x0; state->xoff = x0;
state->yoff = y0; state->yoff = y0;
state->xsize = x1 - x0; state->xsize = x1 - x0;
state->ysize = y1 - y0; state->ysize = y1 - y0;
} }
if (state->xsize <= 0 || if (state->xsize <= 0 ||
state->xsize + state->xoff > (int) im->xsize || state->xsize + state->xoff > (int) im->xsize ||
state->ysize <= 0 || state->ysize <= 0 ||
state->ysize + state->yoff > (int) im->ysize) { state->ysize + state->yoff > (int) im->ysize) {
PyErr_SetString(PyExc_ValueError, "tile cannot extend outside image"); PyErr_SetString(PyExc_ValueError, "tile cannot extend outside image");
return NULL; return NULL;
} }
/* Allocate memory buffer (if bits field is set) */ /* Allocate memory buffer (if bits field is set) */
if (state->bits > 0) { if (state->bits > 0) {
if (!state->bytes) if (!state->bytes)
state->bytes = (state->bits * state->xsize+7)/8; state->bytes = (state->bits * state->xsize+7)/8;
state->buffer = (UINT8*) malloc(state->bytes); state->buffer = (UINT8*) malloc(state->bytes);
if (!state->buffer) if (!state->buffer)
return PyErr_NoMemory(); return PyErr_NoMemory();
} }
/* Keep a reference to the image object, to make sure it doesn't /* Keep a reference to the image object, to make sure it doesn't
@ -178,18 +196,19 @@ _setimage(ImagingDecoderObject* decoder, PyObject* args)
static struct PyMethodDef methods[] = { static struct PyMethodDef methods[] = {
{"decode", (PyCFunction)_decode, 1}, {"decode", (PyCFunction)_decode, 1},
{"cleanup", (PyCFunction)_decode_cleanup, 1},
{"setimage", (PyCFunction)_setimage, 1}, {"setimage", (PyCFunction)_setimage, 1},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
static PyTypeObject ImagingDecoderType = { static PyTypeObject ImagingDecoderType = {
PyVarObject_HEAD_INIT(NULL, 0) PyVarObject_HEAD_INIT(NULL, 0)
"ImagingDecoder", /*tp_name*/ "ImagingDecoder", /*tp_name*/
sizeof(ImagingDecoderObject), /*tp_size*/ sizeof(ImagingDecoderObject), /*tp_size*/
0, /*tp_itemsize*/ 0, /*tp_itemsize*/
/* methods */ /* methods */
(destructor)_dealloc, /*tp_dealloc*/ (destructor)_dealloc, /*tp_dealloc*/
0, /*tp_print*/ 0, /*tp_print*/
0, /*tp_getattr*/ 0, /*tp_getattr*/
0, /*tp_setattr*/ 0, /*tp_setattr*/
0, /*tp_compare*/ 0, /*tp_compare*/
@ -227,9 +246,9 @@ get_unpacker(ImagingDecoderObject* decoder, const char* mode,
unpack = ImagingFindUnpacker(mode, rawmode, &bits); unpack = ImagingFindUnpacker(mode, rawmode, &bits);
if (!unpack) { if (!unpack) {
Py_DECREF(decoder); Py_DECREF(decoder);
PyErr_SetString(PyExc_ValueError, "unknown raw mode"); PyErr_SetString(PyExc_ValueError, "unknown raw mode");
return -1; return -1;
} }
decoder->state.shuffle = unpack; decoder->state.shuffle = unpack;
@ -240,7 +259,7 @@ get_unpacker(ImagingDecoderObject* decoder, const char* mode,
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* BIT (packed fields) */ /* BIT (packed fields) */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
PyObject* PyObject*
@ -256,16 +275,16 @@ PyImaging_BitDecoderNew(PyObject* self, PyObject* args)
int ystep = 1; int ystep = 1;
if (!PyArg_ParseTuple(args, "s|iiiii", &mode, &bits, &pad, &fill, if (!PyArg_ParseTuple(args, "s|iiiii", &mode, &bits, &pad, &fill,
&sign, &ystep)) &sign, &ystep))
return NULL; return NULL;
if (strcmp(mode, "F") != 0) { if (strcmp(mode, "F") != 0) {
PyErr_SetString(PyExc_ValueError, "bad image mode"); PyErr_SetString(PyExc_ValueError, "bad image mode");
return NULL; return NULL;
} }
decoder = PyImaging_DecoderNew(sizeof(BITSTATE)); decoder = PyImaging_DecoderNew(sizeof(BITSTATE));
if (decoder == NULL) if (decoder == NULL)
return NULL; return NULL;
decoder->decode = ImagingBitDecode; decoder->decode = ImagingBitDecode;
@ -281,7 +300,7 @@ PyImaging_BitDecoderNew(PyObject* self, PyObject* args)
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* FLI */ /* FLI */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
PyObject* PyObject*
@ -291,7 +310,7 @@ PyImaging_FliDecoderNew(PyObject* self, PyObject* args)
decoder = PyImaging_DecoderNew(0); decoder = PyImaging_DecoderNew(0);
if (decoder == NULL) if (decoder == NULL)
return NULL; return NULL;
decoder->decode = ImagingFliDecode; decoder->decode = ImagingFliDecode;
@ -300,7 +319,7 @@ PyImaging_FliDecoderNew(PyObject* self, PyObject* args)
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* GIF */ /* GIF */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
PyObject* PyObject*
@ -312,16 +331,16 @@ PyImaging_GifDecoderNew(PyObject* self, PyObject* args)
int bits = 8; int bits = 8;
int interlace = 0; int interlace = 0;
if (!PyArg_ParseTuple(args, "s|ii", &mode, &bits, &interlace)) if (!PyArg_ParseTuple(args, "s|ii", &mode, &bits, &interlace))
return NULL; return NULL;
if (strcmp(mode, "L") != 0 && strcmp(mode, "P") != 0) { if (strcmp(mode, "L") != 0 && strcmp(mode, "P") != 0) {
PyErr_SetString(PyExc_ValueError, "bad image mode"); PyErr_SetString(PyExc_ValueError, "bad image mode");
return NULL; return NULL;
} }
decoder = PyImaging_DecoderNew(sizeof(GIFDECODERSTATE)); decoder = PyImaging_DecoderNew(sizeof(GIFDECODERSTATE));
if (decoder == NULL) if (decoder == NULL)
return NULL; return NULL;
decoder->decode = ImagingGifDecode; decoder->decode = ImagingGifDecode;
@ -333,7 +352,7 @@ PyImaging_GifDecoderNew(PyObject* self, PyObject* args)
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* HEX */ /* HEX */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
PyObject* PyObject*
@ -344,14 +363,14 @@ PyImaging_HexDecoderNew(PyObject* self, PyObject* args)
char* mode; char* mode;
char* rawmode; char* rawmode;
if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode)) if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode))
return NULL; return NULL;
decoder = PyImaging_DecoderNew(0); decoder = PyImaging_DecoderNew(0);
if (decoder == NULL) if (decoder == NULL)
return NULL; return NULL;
if (get_unpacker(decoder, mode, rawmode) < 0) if (get_unpacker(decoder, mode, rawmode) < 0)
return NULL; return NULL;
decoder->decode = ImagingHexDecode; decoder->decode = ImagingHexDecode;
@ -360,7 +379,7 @@ PyImaging_HexDecoderNew(PyObject* self, PyObject* args)
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* LZW */ /* LZW */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
PyObject* PyObject*
@ -372,14 +391,14 @@ PyImaging_TiffLzwDecoderNew(PyObject* self, PyObject* args)
char* rawmode; char* rawmode;
int filter = 0; int filter = 0;
if (!PyArg_ParseTuple(args, "ss|i", &mode, &rawmode, &filter)) if (!PyArg_ParseTuple(args, "ss|i", &mode, &rawmode, &filter))
return NULL; return NULL;
decoder = PyImaging_DecoderNew(sizeof(LZWSTATE)); decoder = PyImaging_DecoderNew(sizeof(LZWSTATE));
if (decoder == NULL) if (decoder == NULL)
return NULL; return NULL;
if (get_unpacker(decoder, mode, rawmode) < 0) if (get_unpacker(decoder, mode, rawmode) < 0)
return NULL; return NULL;
decoder->decode = ImagingLzwDecode; decoder->decode = ImagingLzwDecode;
@ -389,7 +408,7 @@ PyImaging_TiffLzwDecoderNew(PyObject* self, PyObject* args)
} }
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* LibTiff */ /* LibTiff */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
#ifdef HAVE_LIBTIFF #ifdef HAVE_LIBTIFF
@ -409,17 +428,17 @@ PyImaging_LibTiffDecoderNew(PyObject* self, PyObject* args)
char* rawmode; char* rawmode;
char* compname; char* compname;
int compression; int compression;
int fp; int fp;
if (! PyArg_ParseTuple(args, "sssi", &mode, &rawmode, &compname, &fp)) if (! PyArg_ParseTuple(args, "sssi", &mode, &rawmode, &compname, &fp))
return NULL; return NULL;
TRACE(("new tiff decoder %s\n", compname)); TRACE(("new tiff decoder %s\n", compname));
/* UNDONE -- we can probably do almost any arbitrary compression here, /* UNDONE -- we can probably do almost any arbitrary compression here,
* since we're effective passing in the whole file in one shot and * since we're effective passing in the whole file in one shot and
* getting back the data row by row. V2 maybe * getting back the data row by row. V2 maybe
*/ */
if (strcasecmp(compname, "tiff_ccitt") == 0) { if (strcasecmp(compname, "tiff_ccitt") == 0) {
compression = COMPRESSION_CCITTRLE; compression = COMPRESSION_CCITTRLE;
@ -459,7 +478,7 @@ PyImaging_LibTiffDecoderNew(PyObject* self, PyObject* args)
#endif #endif
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* MSP */ /* MSP */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
PyObject* PyObject*
@ -469,10 +488,10 @@ PyImaging_MspDecoderNew(PyObject* self, PyObject* args)
decoder = PyImaging_DecoderNew(0); decoder = PyImaging_DecoderNew(0);
if (decoder == NULL) if (decoder == NULL)
return NULL; return NULL;
if (get_unpacker(decoder, "1", "1") < 0) if (get_unpacker(decoder, "1", "1") < 0)
return NULL; return NULL;
decoder->decode = ImagingMspDecode; decoder->decode = ImagingMspDecode;
@ -481,7 +500,7 @@ PyImaging_MspDecoderNew(PyObject* self, PyObject* args)
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* PackBits */ /* PackBits */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
PyObject* PyObject*
@ -492,14 +511,14 @@ PyImaging_PackbitsDecoderNew(PyObject* self, PyObject* args)
char* mode; char* mode;
char* rawmode; char* rawmode;
if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode)) if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode))
return NULL; return NULL;
decoder = PyImaging_DecoderNew(0); decoder = PyImaging_DecoderNew(0);
if (decoder == NULL) if (decoder == NULL)
return NULL; return NULL;
if (get_unpacker(decoder, mode, rawmode) < 0) if (get_unpacker(decoder, mode, rawmode) < 0)
return NULL; return NULL;
decoder->decode = ImagingPackbitsDecode; decoder->decode = ImagingPackbitsDecode;
@ -508,7 +527,7 @@ PyImaging_PackbitsDecoderNew(PyObject* self, PyObject* args)
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* PCD */ /* PCD */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
PyObject* PyObject*
@ -518,11 +537,11 @@ PyImaging_PcdDecoderNew(PyObject* self, PyObject* args)
decoder = PyImaging_DecoderNew(0); decoder = PyImaging_DecoderNew(0);
if (decoder == NULL) if (decoder == NULL)
return NULL; return NULL;
/* Unpack from PhotoYCC to RGB */ /* Unpack from PhotoYCC to RGB */
if (get_unpacker(decoder, "RGB", "YCC;P") < 0) if (get_unpacker(decoder, "RGB", "YCC;P") < 0)
return NULL; return NULL;
decoder->decode = ImagingPcdDecode; decoder->decode = ImagingPcdDecode;
@ -531,7 +550,7 @@ PyImaging_PcdDecoderNew(PyObject* self, PyObject* args)
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* PCX */ /* PCX */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
PyObject* PyObject*
@ -543,14 +562,14 @@ PyImaging_PcxDecoderNew(PyObject* self, PyObject* args)
char* rawmode; char* rawmode;
int stride; int stride;
if (!PyArg_ParseTuple(args, "ssi", &mode, &rawmode, &stride)) if (!PyArg_ParseTuple(args, "ssi", &mode, &rawmode, &stride))
return NULL; return NULL;
decoder = PyImaging_DecoderNew(0); decoder = PyImaging_DecoderNew(0);
if (decoder == NULL) if (decoder == NULL)
return NULL; return NULL;
if (get_unpacker(decoder, mode, rawmode) < 0) if (get_unpacker(decoder, mode, rawmode) < 0)
return NULL; return NULL;
decoder->state.bytes = stride; decoder->state.bytes = stride;
@ -561,7 +580,7 @@ PyImaging_PcxDecoderNew(PyObject* self, PyObject* args)
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* RAW */ /* RAW */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
PyObject* PyObject*
@ -574,14 +593,14 @@ PyImaging_RawDecoderNew(PyObject* self, PyObject* args)
int stride = 0; int stride = 0;
int ystep = 1; int ystep = 1;
if (!PyArg_ParseTuple(args, "ss|ii", &mode, &rawmode, &stride, &ystep)) if (!PyArg_ParseTuple(args, "ss|ii", &mode, &rawmode, &stride, &ystep))
return NULL; return NULL;
decoder = PyImaging_DecoderNew(sizeof(RAWSTATE)); decoder = PyImaging_DecoderNew(sizeof(RAWSTATE));
if (decoder == NULL) if (decoder == NULL)
return NULL; return NULL;
if (get_unpacker(decoder, mode, rawmode) < 0) if (get_unpacker(decoder, mode, rawmode) < 0)
return NULL; return NULL;
decoder->decode = ImagingRawDecode; decoder->decode = ImagingRawDecode;
@ -594,7 +613,7 @@ PyImaging_RawDecoderNew(PyObject* self, PyObject* args)
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* SUN RLE */ /* SUN RLE */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
PyObject* PyObject*
@ -605,14 +624,14 @@ PyImaging_SunRleDecoderNew(PyObject* self, PyObject* args)
char* mode; char* mode;
char* rawmode; char* rawmode;
if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode)) if (!PyArg_ParseTuple(args, "ss", &mode, &rawmode))
return NULL; return NULL;
decoder = PyImaging_DecoderNew(0); decoder = PyImaging_DecoderNew(0);
if (decoder == NULL) if (decoder == NULL)
return NULL; return NULL;
if (get_unpacker(decoder, mode, rawmode) < 0) if (get_unpacker(decoder, mode, rawmode) < 0)
return NULL; return NULL;
decoder->decode = ImagingSunRleDecode; decoder->decode = ImagingSunRleDecode;
@ -621,7 +640,7 @@ PyImaging_SunRleDecoderNew(PyObject* self, PyObject* args)
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* TGA RLE */ /* TGA RLE */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
PyObject* PyObject*
@ -634,14 +653,14 @@ PyImaging_TgaRleDecoderNew(PyObject* self, PyObject* args)
int ystep = 1; int ystep = 1;
int depth = 8; int depth = 8;
if (!PyArg_ParseTuple(args, "ss|ii", &mode, &rawmode, &ystep, &depth)) if (!PyArg_ParseTuple(args, "ss|ii", &mode, &rawmode, &ystep, &depth))
return NULL; return NULL;
decoder = PyImaging_DecoderNew(0); decoder = PyImaging_DecoderNew(0);
if (decoder == NULL) if (decoder == NULL)
return NULL; return NULL;
if (get_unpacker(decoder, mode, rawmode) < 0) if (get_unpacker(decoder, mode, rawmode) < 0)
return NULL; return NULL;
decoder->decode = ImagingTgaRleDecode; decoder->decode = ImagingTgaRleDecode;
@ -653,7 +672,7 @@ PyImaging_TgaRleDecoderNew(PyObject* self, PyObject* args)
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* XBM */ /* XBM */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
PyObject* PyObject*
@ -663,10 +682,10 @@ PyImaging_XbmDecoderNew(PyObject* self, PyObject* args)
decoder = PyImaging_DecoderNew(0); decoder = PyImaging_DecoderNew(0);
if (decoder == NULL) if (decoder == NULL)
return NULL; return NULL;
if (get_unpacker(decoder, "1", "1;R") < 0) if (get_unpacker(decoder, "1", "1;R") < 0)
return NULL; return NULL;
decoder->decode = ImagingXbmDecode; decoder->decode = ImagingXbmDecode;
@ -675,7 +694,7 @@ PyImaging_XbmDecoderNew(PyObject* self, PyObject* args)
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* ZIP */ /* ZIP */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
#ifdef HAVE_LIBZ #ifdef HAVE_LIBZ
@ -691,14 +710,14 @@ PyImaging_ZipDecoderNew(PyObject* self, PyObject* args)
char* rawmode; char* rawmode;
int interlaced = 0; int interlaced = 0;
if (!PyArg_ParseTuple(args, "ss|i", &mode, &rawmode, &interlaced)) if (!PyArg_ParseTuple(args, "ss|i", &mode, &rawmode, &interlaced))
return NULL; return NULL;
decoder = PyImaging_DecoderNew(sizeof(ZIPSTATE)); decoder = PyImaging_DecoderNew(sizeof(ZIPSTATE));
if (decoder == NULL) if (decoder == NULL)
return NULL; return NULL;
if (get_unpacker(decoder, mode, rawmode) < 0) if (get_unpacker(decoder, mode, rawmode) < 0)
return NULL; return NULL;
decoder->decode = ImagingZipDecode; decoder->decode = ImagingZipDecode;
@ -710,7 +729,7 @@ PyImaging_ZipDecoderNew(PyObject* self, PyObject* args)
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* JPEG */ /* JPEG */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
#ifdef HAVE_LIBJPEG #ifdef HAVE_LIBJPEG
@ -718,15 +737,15 @@ PyImaging_ZipDecoderNew(PyObject* self, PyObject* args)
/* We better define this decoder last in this file, so the following /* We better define this decoder last in this file, so the following
undef's won't mess things up for the Imaging library proper. */ undef's won't mess things up for the Imaging library proper. */
#undef HAVE_PROTOTYPES #undef HAVE_PROTOTYPES
#undef HAVE_STDDEF_H #undef HAVE_STDDEF_H
#undef HAVE_STDLIB_H #undef HAVE_STDLIB_H
#undef UINT8 #undef UINT8
#undef UINT16 #undef UINT16
#undef UINT32 #undef UINT32
#undef INT8 #undef INT8
#undef INT16 #undef INT16
#undef INT32 #undef INT32
#include "Jpeg.h" #include "Jpeg.h"
@ -742,19 +761,20 @@ PyImaging_JpegDecoderNew(PyObject* self, PyObject* args)
int draft = 0; int draft = 0;
if (!PyArg_ParseTuple(args, "ssz|ii", &mode, &rawmode, &jpegmode, if (!PyArg_ParseTuple(args, "ssz|ii", &mode, &rawmode, &jpegmode,
&scale, &draft)) &scale, &draft))
return NULL; return NULL;
if (!jpegmode) if (!jpegmode)
jpegmode = ""; jpegmode = "";
decoder = PyImaging_DecoderNew(sizeof(JPEGSTATE)); decoder = PyImaging_DecoderNew(sizeof(JPEGSTATE));
if (decoder == NULL) if (decoder == NULL)
return NULL; return NULL;
if (get_unpacker(decoder, mode, rawmode) < 0) if (get_unpacker(decoder, mode, rawmode) < 0)
return NULL; return NULL;
decoder->decode = ImagingJpegDecode; decoder->decode = ImagingJpegDecode;
decoder->cleanup = ImagingJpegDecodeCleanup;
strncpy(((JPEGSTATE*)decoder->state.context)->rawmode, rawmode, 8); strncpy(((JPEGSTATE*)decoder->state.context)->rawmode, rawmode, 8);
strncpy(((JPEGSTATE*)decoder->state.context)->jpegmode, jpegmode, 8); strncpy(((JPEGSTATE*)decoder->state.context)->jpegmode, jpegmode, 8);

View File

@ -417,6 +417,8 @@ extern int ImagingHexDecode(Imaging im, ImagingCodecState state,
#ifdef HAVE_LIBJPEG #ifdef HAVE_LIBJPEG
extern int ImagingJpegDecode(Imaging im, ImagingCodecState state, extern int ImagingJpegDecode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes); UINT8* buffer, int bytes);
extern int ImagingJpegDecodeCleanup(ImagingCodecState state);
extern int ImagingJpegEncode(Imaging im, ImagingCodecState state, extern int ImagingJpegEncode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes); UINT8* buffer, int bytes);
#endif #endif

View File

@ -24,7 +24,7 @@
#include "Imaging.h" #include "Imaging.h"
#ifdef HAVE_LIBJPEG #ifdef HAVE_LIBJPEG
#undef HAVE_PROTOTYPES #undef HAVE_PROTOTYPES
#undef HAVE_STDLIB_H #undef HAVE_STDLIB_H
@ -39,7 +39,7 @@
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* Suspending input handler */ /* Suspending input handler */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
METHODDEF(void) METHODDEF(void)
@ -61,16 +61,16 @@ skip_input_data(j_decompress_ptr cinfo, long num_bytes)
JPEGSOURCE* source = (JPEGSOURCE*) cinfo->src; JPEGSOURCE* source = (JPEGSOURCE*) cinfo->src;
if (num_bytes > (long) source->pub.bytes_in_buffer) { if (num_bytes > (long) source->pub.bytes_in_buffer) {
/* We need to skip more data than we have in the buffer. /* We need to skip more data than we have in the buffer.
This will force the JPEG library to suspend decoding. */ This will force the JPEG library to suspend decoding. */
source->skip = num_bytes - source->pub.bytes_in_buffer; source->skip = num_bytes - source->pub.bytes_in_buffer;
source->pub.next_input_byte += source->pub.bytes_in_buffer; source->pub.next_input_byte += source->pub.bytes_in_buffer;
source->pub.bytes_in_buffer = 0; source->pub.bytes_in_buffer = 0;
} else { } else {
/* Skip portion of the buffer */ /* Skip portion of the buffer */
source->pub.bytes_in_buffer -= num_bytes; source->pub.bytes_in_buffer -= num_bytes;
source->pub.next_input_byte += num_bytes; source->pub.next_input_byte += num_bytes;
source->skip = 0; source->skip = 0;
} }
} }
@ -93,7 +93,7 @@ jpeg_buffer_src(j_decompress_ptr cinfo, JPEGSOURCE* source)
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* Error handler */ /* Error handler */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
METHODDEF(void) METHODDEF(void)
@ -111,7 +111,7 @@ output(j_common_ptr cinfo)
} }
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* Decoder */ /* Decoder */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
int int
@ -121,23 +121,23 @@ ImagingJpegDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
int ok; int ok;
if (setjmp(context->error.setjmp_buffer)) { if (setjmp(context->error.setjmp_buffer)) {
/* JPEG error handler */ /* JPEG error handler */
jpeg_destroy_decompress(&context->cinfo); jpeg_destroy_decompress(&context->cinfo);
state->errcode = IMAGING_CODEC_BROKEN; state->errcode = IMAGING_CODEC_BROKEN;
return -1; return -1;
} }
if (!state->state) { if (!state->state) {
/* Setup decompression context */ /* Setup decompression context */
context->cinfo.err = jpeg_std_error(&context->error.pub); context->cinfo.err = jpeg_std_error(&context->error.pub);
context->error.pub.error_exit = error; context->error.pub.error_exit = error;
context->error.pub.output_message = output; context->error.pub.output_message = output;
jpeg_create_decompress(&context->cinfo); jpeg_create_decompress(&context->cinfo);
jpeg_buffer_src(&context->cinfo, &context->source); jpeg_buffer_src(&context->cinfo, &context->source);
/* Ready to decode */ /* Ready to decode */
state->state = 1; state->state = 1;
} }
@ -146,115 +146,115 @@ ImagingJpegDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
context->source.pub.bytes_in_buffer = bytes; context->source.pub.bytes_in_buffer = bytes;
if (context->source.skip > 0) { if (context->source.skip > 0) {
skip_input_data(&context->cinfo, context->source.skip); skip_input_data(&context->cinfo, context->source.skip);
if (context->source.skip > 0) if (context->source.skip > 0)
return context->source.pub.next_input_byte - buf; return context->source.pub.next_input_byte - buf;
} }
switch (state->state) { switch (state->state) {
case 1: case 1:
/* Read JPEG header, until we find an image body. */ /* Read JPEG header, until we find an image body. */
do { do {
/* Note that we cannot return unless we have decoded /* Note that we cannot return unless we have decoded
as much data as possible. */ as much data as possible. */
ok = jpeg_read_header(&context->cinfo, FALSE); ok = jpeg_read_header(&context->cinfo, FALSE);
} while (ok == JPEG_HEADER_TABLES_ONLY); } while (ok == JPEG_HEADER_TABLES_ONLY);
if (ok == JPEG_SUSPENDED) if (ok == JPEG_SUSPENDED)
break; break;
/* Decoder settings */ /* Decoder settings */
/* jpegmode indicates whats in the file; if not set, we'll /* jpegmode indicates whats in the file; if not set, we'll
trust the decoder */ trust the decoder */
if (strcmp(context->jpegmode, "L") == 0) if (strcmp(context->jpegmode, "L") == 0)
context->cinfo.jpeg_color_space = JCS_GRAYSCALE; context->cinfo.jpeg_color_space = JCS_GRAYSCALE;
else if (strcmp(context->jpegmode, "RGB") == 0) else if (strcmp(context->jpegmode, "RGB") == 0)
context->cinfo.jpeg_color_space = JCS_RGB; context->cinfo.jpeg_color_space = JCS_RGB;
else if (strcmp(context->jpegmode, "CMYK") == 0) else if (strcmp(context->jpegmode, "CMYK") == 0)
context->cinfo.jpeg_color_space = JCS_CMYK; context->cinfo.jpeg_color_space = JCS_CMYK;
else if (strcmp(context->jpegmode, "YCbCr") == 0) else if (strcmp(context->jpegmode, "YCbCr") == 0)
context->cinfo.jpeg_color_space = JCS_YCbCr; context->cinfo.jpeg_color_space = JCS_YCbCr;
else if (strcmp(context->jpegmode, "YCbCrK") == 0) { else if (strcmp(context->jpegmode, "YCbCrK") == 0) {
context->cinfo.jpeg_color_space = JCS_YCCK; context->cinfo.jpeg_color_space = JCS_YCCK;
} }
/* rawmode indicates what we want from the decoder. if not /* rawmode indicates what we want from the decoder. if not
set, conversions are disabled */ set, conversions are disabled */
if (strcmp(context->rawmode, "L") == 0) if (strcmp(context->rawmode, "L") == 0)
context->cinfo.out_color_space = JCS_GRAYSCALE; context->cinfo.out_color_space = JCS_GRAYSCALE;
else if (strcmp(context->rawmode, "RGB") == 0) else if (strcmp(context->rawmode, "RGB") == 0)
context->cinfo.out_color_space = JCS_RGB; context->cinfo.out_color_space = JCS_RGB;
else if (strcmp(context->rawmode, "CMYK") == 0 || else if (strcmp(context->rawmode, "CMYK") == 0 ||
strcmp(context->rawmode, "CMYK;I") == 0) strcmp(context->rawmode, "CMYK;I") == 0)
context->cinfo.out_color_space = JCS_CMYK; context->cinfo.out_color_space = JCS_CMYK;
else if (strcmp(context->rawmode, "YCbCr") == 0) else if (strcmp(context->rawmode, "YCbCr") == 0)
context->cinfo.out_color_space = JCS_YCbCr; context->cinfo.out_color_space = JCS_YCbCr;
else if (strcmp(context->rawmode, "YCbCrK") == 0) else if (strcmp(context->rawmode, "YCbCrK") == 0)
context->cinfo.out_color_space = JCS_YCCK; context->cinfo.out_color_space = JCS_YCCK;
else { else {
/* Disable decoder conversions */ /* Disable decoder conversions */
context->cinfo.jpeg_color_space = JCS_UNKNOWN; context->cinfo.jpeg_color_space = JCS_UNKNOWN;
context->cinfo.out_color_space = JCS_UNKNOWN; context->cinfo.out_color_space = JCS_UNKNOWN;
} }
if (context->scale > 1) { if (context->scale > 1) {
context->cinfo.scale_num = 1; context->cinfo.scale_num = 1;
context->cinfo.scale_denom = context->scale; context->cinfo.scale_denom = context->scale;
} }
if (context->draft) { if (context->draft) {
context->cinfo.do_fancy_upsampling = FALSE; context->cinfo.do_fancy_upsampling = FALSE;
context->cinfo.dct_method = JDCT_FASTEST; context->cinfo.dct_method = JDCT_FASTEST;
} }
state->state++; state->state++;
/* fall through */ /* fall through */
case 2: case 2:
/* Set things up for decompression (this processes the entire /* Set things up for decompression (this processes the entire
file if necessary to return data line by line) */ file if necessary to return data line by line) */
if (!jpeg_start_decompress(&context->cinfo)) if (!jpeg_start_decompress(&context->cinfo))
break; break;
state->state++; state->state++;
/* fall through */ /* fall through */
case 3: case 3:
/* Decompress a single line of data */ /* Decompress a single line of data */
ok = 1; ok = 1;
while (state->y < state->ysize) { while (state->y < state->ysize) {
ok = jpeg_read_scanlines(&context->cinfo, &state->buffer, 1); ok = jpeg_read_scanlines(&context->cinfo, &state->buffer, 1);
if (ok != 1) if (ok != 1)
break; break;
state->shuffle((UINT8*) im->image[state->y + state->yoff] + state->shuffle((UINT8*) im->image[state->y + state->yoff] +
state->xoff * im->pixelsize, state->buffer, state->xoff * im->pixelsize, state->buffer,
state->xsize); state->xsize);
state->y++; state->y++;
} }
if (ok != 1) if (ok != 1)
break; break;
state->state++; state->state++;
/* fall through */ /* fall through */
case 4: case 4:
/* Finish decompression */ /* Finish decompression */
if (!jpeg_finish_decompress(&context->cinfo)) { if (!jpeg_finish_decompress(&context->cinfo)) {
/* FIXME: add strictness mode test */ /* FIXME: add strictness mode test */
if (state->y < state->ysize) if (state->y < state->ysize)
break; break;
} }
/* Clean up */ /* Clean up */
jpeg_destroy_decompress(&context->cinfo); jpeg_destroy_decompress(&context->cinfo);
/* if (jerr.pub.num_warnings) return BROKEN; */ /* if (jerr.pub.num_warnings) return BROKEN; */
return -1; return -1;
} }
@ -263,5 +263,20 @@ ImagingJpegDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
} }
/* -------------------------------------------------------------------- */
/* Cleanup */
/* -------------------------------------------------------------------- */
int ImagingJpegDecodeCleanup(ImagingCodecState state){
/* called to fee the decompression engine when the decode terminates
due to a corrupt or truncated image
*/
JPEGSTATE* context = (JPEGSTATE*) state->context;
/* Clean up */
jpeg_destroy_decompress(&context->cinfo);
return -1;
}
#endif #endif

View File

@ -22,7 +22,7 @@
#include "Imaging.h" #include "Imaging.h"
#ifdef HAVE_LIBJPEG #ifdef HAVE_LIBJPEG
#undef HAVE_PROTOTYPES #undef HAVE_PROTOTYPES
#undef HAVE_STDLIB_H #undef HAVE_STDLIB_H
@ -36,7 +36,7 @@
#include "Jpeg.h" #include "Jpeg.h"
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* Suspending output handler */ /* Suspending output handler */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
METHODDEF(void) METHODDEF(void)
@ -64,16 +64,16 @@ jpeg_buffer_dest(j_compress_ptr cinfo, JPEGDESTINATION* destination)
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* Error handler */ /* Error handler */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
METHODDEF(void) METHODDEF(void)
error(j_common_ptr cinfo) error(j_common_ptr cinfo)
{ {
JPEGERROR* error; JPEGERROR* error;
error = (JPEGERROR*) cinfo->err; error = (JPEGERROR*) cinfo->err;
(*cinfo->err->output_message) (cinfo); (*cinfo->err->output_message) (cinfo);
longjmp(error->setjmp_buffer, 1); longjmp(error->setjmp_buffer, 1);
} }
@ -146,59 +146,59 @@ ImagingJpegEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
/* Use custom quantization tables */ /* Use custom quantization tables */
if (context->qtables) { if (context->qtables) {
int i; int i;
int quality = 100; int quality = 100;
if (context->quality > 0) { if (context->quality > 0) {
quality = context->quality; quality = context->quality;
} }
for (i = 0; i < sizeof(context->qtables)/sizeof(unsigned int); i++) { for (i = 0; i < sizeof(context->qtables)/sizeof(unsigned int); i++) {
// TODO: Should add support for none baseline // TODO: Should add support for none baseline
jpeg_add_quant_table(&context->cinfo, i, context->qtables[i], jpeg_add_quant_table(&context->cinfo, i, context->qtables[i],
quality, TRUE); quality, TRUE);
} }
} else if (context->quality > 0) { } else if (context->quality > 0) {
jpeg_set_quality(&context->cinfo, context->quality, 1); jpeg_set_quality(&context->cinfo, context->quality, 1);
} }
/* Set subsampling options */ /* Set subsampling options */
switch (context->subsampling) switch (context->subsampling)
{ {
case 0: /* 1x1 1x1 1x1 (4:4:4) : None */ case 0: /* 1x1 1x1 1x1 (4:4:4) : None */
{ {
context->cinfo.comp_info[0].h_samp_factor = 1; context->cinfo.comp_info[0].h_samp_factor = 1;
context->cinfo.comp_info[0].v_samp_factor = 1; context->cinfo.comp_info[0].v_samp_factor = 1;
context->cinfo.comp_info[1].h_samp_factor = 1; context->cinfo.comp_info[1].h_samp_factor = 1;
context->cinfo.comp_info[1].v_samp_factor = 1; context->cinfo.comp_info[1].v_samp_factor = 1;
context->cinfo.comp_info[2].h_samp_factor = 1; context->cinfo.comp_info[2].h_samp_factor = 1;
context->cinfo.comp_info[2].v_samp_factor = 1; context->cinfo.comp_info[2].v_samp_factor = 1;
break; break;
} }
case 1: /* 2x1, 1x1, 1x1 (4:2:2) : Medium */ case 1: /* 2x1, 1x1, 1x1 (4:2:2) : Medium */
{ {
context->cinfo.comp_info[0].h_samp_factor = 2; context->cinfo.comp_info[0].h_samp_factor = 2;
context->cinfo.comp_info[0].v_samp_factor = 1; context->cinfo.comp_info[0].v_samp_factor = 1;
context->cinfo.comp_info[1].h_samp_factor = 1; context->cinfo.comp_info[1].h_samp_factor = 1;
context->cinfo.comp_info[1].v_samp_factor = 1; context->cinfo.comp_info[1].v_samp_factor = 1;
context->cinfo.comp_info[2].h_samp_factor = 1; context->cinfo.comp_info[2].h_samp_factor = 1;
context->cinfo.comp_info[2].v_samp_factor = 1; context->cinfo.comp_info[2].v_samp_factor = 1;
break; break;
} }
case 2: /* 2x2, 1x1, 1x1 (4:1:1) : High */ case 2: /* 2x2, 1x1, 1x1 (4:1:1) : High */
{ {
context->cinfo.comp_info[0].h_samp_factor = 2; context->cinfo.comp_info[0].h_samp_factor = 2;
context->cinfo.comp_info[0].v_samp_factor = 2; context->cinfo.comp_info[0].v_samp_factor = 2;
context->cinfo.comp_info[1].h_samp_factor = 1; context->cinfo.comp_info[1].h_samp_factor = 1;
context->cinfo.comp_info[1].v_samp_factor = 1; context->cinfo.comp_info[1].v_samp_factor = 1;
context->cinfo.comp_info[2].h_samp_factor = 1; context->cinfo.comp_info[2].h_samp_factor = 1;
context->cinfo.comp_info[2].v_samp_factor = 1; context->cinfo.comp_info[2].v_samp_factor = 1;
break; break;
} }
default: default:
{ {
/* Use the lib's default */ /* Use the lib's default */
break; break;
} }
} }
if (context->progressive) if (context->progressive)
jpeg_simple_progression(&context->cinfo); jpeg_simple_progression(&context->cinfo);
context->cinfo.smoothing_factor = context->smooth; context->cinfo.smoothing_factor = context->smooth;
@ -219,24 +219,29 @@ ImagingJpegEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
jpeg_start_compress(&context->cinfo, FALSE); jpeg_start_compress(&context->cinfo, FALSE);
/* suppress extra section */ /* suppress extra section */
context->extra_offset = context->extra_size; context->extra_offset = context->extra_size;
//add exif header
if (context->rawExifLen > 0)
jpeg_write_marker(&context->cinfo, JPEG_APP0+1, (unsigned char*)context->rawExif, context->rawExifLen);
break; break;
default: default:
/* interchange stream */ /* interchange stream */
jpeg_start_compress(&context->cinfo, TRUE); jpeg_start_compress(&context->cinfo, TRUE);
//add exif header break;
if (context->rawExifLen > 0)
jpeg_write_marker(&context->cinfo, JPEG_APP0+1, (unsigned char*)context->rawExif, context->rawExifLen);
break;
} }
state->state++; state->state++;
/* fall through */ /* fall through */
case 2: case 2:
// check for exif len + 'APP1' header bytes
if (context->rawExifLen + 5 > context->destination.pub.free_in_buffer){
break;
}
//add exif header
if (context->rawExifLen > 0){
jpeg_write_marker(&context->cinfo, JPEG_APP0+1,
(unsigned char*)context->rawExif, context->rawExifLen);
}
state->state++;
/* fall through */
case 3:
if (context->extra) { if (context->extra) {
/* copy extra buffer to output buffer */ /* copy extra buffer to output buffer */
@ -253,9 +258,12 @@ ImagingJpegEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
else else
break; break;
} else } else
state->state++; state->state++;
case 3: case 4:
if (1024 > context->destination.pub.free_in_buffer){
break;
}
ok = 1; ok = 1;
while (state->y < state->ysize) { while (state->y < state->ysize) {
@ -273,7 +281,7 @@ ImagingJpegEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
state->state++; state->state++;
/* fall through */ /* fall through */
case 4: case 5:
/* Finish compression */ /* Finish compression */
if (context->destination.pub.free_in_buffer < 100) if (context->destination.pub.free_in_buffer < 100)

View File

@ -188,7 +188,7 @@ if __name__ == "__main__":
print("-"*68) print("-"*68)
#print("PIL", Image.VERSION, "TEST SUMMARY ") #print("PIL", Image.VERSION, "TEST SUMMARY ")
print("PIL (Pillow) TEST SUMMARY ") print("PIL TEST SUMMARY ")
print("-"*68) print("-"*68)
print("Python modules loaded from", os.path.dirname(Image.__file__)) print("Python modules loaded from", os.path.dirname(Image.__file__))
print("Binary modules loaded from", os.path.dirname(Image.core.__file__)) print("Binary modules loaded from", os.path.dirname(Image.core.__file__))
@ -201,6 +201,14 @@ if __name__ == "__main__":
check_module("FREETYPE2", "PIL._imagingft") check_module("FREETYPE2", "PIL._imagingft")
check_module("LITTLECMS", "PIL._imagingcms") check_module("LITTLECMS", "PIL._imagingcms")
check_module("WEBP", "PIL._webp") check_module("WEBP", "PIL._webp")
try:
from PIL import _webp
if _webp.WebPDecoderBuggyAlpha():
print("***", "Transparent WEBP", "support not installed")
else:
print("---", "Transparent WEBP", "support ok")
except Exception:
pass
print("-"*68) print("-"*68)
# use doctest to make sure the test program behaves as documented! # use doctest to make sure the test program behaves as documented!