Merge pull request #359 from wiredfool/pypy

Fixes to make Pypy 2.1.0 work on Ubuntu 12.04/64
This commit is contained in:
Alex Clark ☺ 2013-10-03 05:14:23 -07:00
commit 84b0ba7207
3 changed files with 44 additions and 9 deletions

View File

@ -29,7 +29,7 @@
from PIL import Image from PIL import Image
from PIL._util import isPath from PIL._util import isPath
import traceback, os import traceback, os, sys
import io import io
MAXBLOCK = 65536 MAXBLOCK = 65536
@ -136,7 +136,8 @@ class ImageFile(Image.Image):
readonly = 0 readonly = 0
if self.filename and len(self.tile) == 1: if self.filename and len(self.tile) == 1 and not hasattr(sys, 'pypy_version_info'):
# As of pypy 2.1.0, memory mapping was failing here.
# try memory mapping # try memory mapping
d, e, o, a = self.tile[0] d, e, o, a = self.tile[0]
if d == "raw" and a[0] == self.mode and a[0] in Image._MAPMODES: if d == "raw" and a[0] == self.mode and a[0] in Image._MAPMODES:

View File

@ -45,7 +45,8 @@ def test_write_exif_metadata():
webp_exif = webp_image.info.get('exif', None) webp_exif = webp_image.info.get('exif', None)
assert_true(webp_exif) assert_true(webp_exif)
assert_equal(webp_exif, expected_exif) if webp_exif:
assert_equal(webp_exif, expected_exif, "Webp Exif didn't match")
def test_read_icc_profile(): def test_read_icc_profile():
@ -77,5 +78,7 @@ def test_write_icc_metadata():
webp_image = Image.open(buffer) webp_image = Image.open(buffer)
webp_icc_profile = webp_image.info.get('icc_profile', None) webp_icc_profile = webp_image.info.get('icc_profile', None)
assert_true(webp_icc_profile) assert_true(webp_icc_profile)
assert_equal(webp_icc_profile, expected_icc_profile) if webp_icc_profile:
assert_equal(webp_icc_profile, expected_icc_profile, "Webp ICC didn't match")

39
_webp.c
View File

@ -20,8 +20,8 @@ PyObject* WebPEncode_wrapper(PyObject* self, PyObject* args)
uint8_t *output; uint8_t *output;
char *mode; char *mode;
Py_ssize_t size; Py_ssize_t size;
Py_ssize_t icc_size; int icc_size; /* see comment below */
Py_ssize_t exif_size; int exif_size;
size_t ret_size; size_t ret_size;
if (!PyArg_ParseTuple(args, "s#iifss#s#", if (!PyArg_ParseTuple(args, "s#iifss#s#",
@ -54,6 +54,8 @@ PyObject* WebPEncode_wrapper(PyObject* self, PyObject* args)
{ {
WebPData output_data = {0}; WebPData output_data = {0};
WebPData image = { output, ret_size }; WebPData image = { output, ret_size };
WebPMuxError err;
int dbg = 0;
int copy_data = 0; // value 1 indicates given data WILL be copied to the mux int copy_data = 0; // value 1 indicates given data WILL be copied to the mux
// and value 0 indicates data will NOT be copied. // and value 0 indicates data will NOT be copied.
@ -61,14 +63,43 @@ PyObject* WebPEncode_wrapper(PyObject* self, PyObject* args)
WebPMux* mux = WebPMuxNew(); WebPMux* mux = WebPMuxNew();
WebPMuxSetImage(mux, &image, copy_data); WebPMuxSetImage(mux, &image, copy_data);
if (dbg) {
fprintf(stderr, "icc size %d, %d \n", icc_size, icc_size > 0);
}
/* icc_size and exif size used to be Py_ssize_t, now they're int.
PyPy2.1 was having trouble with these, as they were getting
cast badly. Since WebP can't take a 64 bit value, They were
ending up as null PyArg_ParseTuple can kick out ints, we're
going to use those instead so that we don't have 32/64 bit
problems here.
*/
if (icc_size > 0) { if (icc_size > 0) {
if (dbg) {
fprintf (stderr, "Adding ICC Profile\n");
}
WebPData icc_profile = { icc_bytes, icc_size }; WebPData icc_profile = { icc_bytes, icc_size };
WebPMuxSetChunk(mux, "ICCP", &icc_profile, copy_data); err = WebPMuxSetChunk(mux, "ICCP", &icc_profile, copy_data);
if (dbg && err == WEBP_MUX_INVALID_ARGUMENT) {
fprintf(stderr, "Invalid ICC Argument\n");
} else if (dbg && err == WEBP_MUX_MEMORY_ERROR) {
fprintf(stderr, "ICC Memory Error\n");
}
} }
if (dbg) {
fprintf(stderr, "exif size %d \n", exif_size);
}
if (exif_size > 0) { if (exif_size > 0) {
if (dbg){
fprintf (stderr, "Adding Exif Data\n");
}
WebPData exif = { exif_bytes, exif_size }; WebPData exif = { exif_bytes, exif_size };
WebPMuxSetChunk(mux, "EXIF", &exif, copy_data); err = WebPMuxSetChunk(mux, "EXIF", &exif, copy_data);
if (dbg && err == WEBP_MUX_INVALID_ARGUMENT) {
fprintf(stderr, "Invalid Exif Argument\n");
} else if (dbg && err == WEBP_MUX_MEMORY_ERROR) {
fprintf(stderr, "Exif Memory Error\n");
}
} }
WebPMuxAssemble(mux, &output_data); WebPMuxAssemble(mux, &output_data);