mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-02-05 14:10:52 +03:00
merge from master
This commit is contained in:
commit
b3752ca053
BIN
Images/transparent.png
Normal file
BIN
Images/transparent.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 26 KiB |
BIN
Images/transparent.webp
Normal file
BIN
Images/transparent.webp
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.9 KiB |
|
@ -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:
|
||||||
|
|
|
@ -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.
|
||||||
|
@ -562,6 +563,10 @@ def _save(im, fp, filename):
|
||||||
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)
|
||||||
|
|
||||||
def _save_cjpeg(im, fp, filename):
|
def _save_cjpeg(im, fp, filename):
|
||||||
|
|
|
@ -3,8 +3,25 @@ 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):
|
||||||
|
|
||||||
|
@ -12,20 +29,29 @@ class WebPImageFile(ImageFile.ImageFile):
|
||||||
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)
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
@ -136,6 +136,8 @@ Some (most?) of Pillow's features require external libraries.
|
||||||
|
|
||||||
* **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)
|
||||||
|
|
||||||
Once you have installed the prerequisites, run:
|
Once you have installed the prerequisites, run:
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -7,53 +7,107 @@ try:
|
||||||
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?"""
|
|
||||||
|
|
||||||
file = "Images/lena.webp"
|
def test_version():
|
||||||
im = Image.open(file)
|
assert_no_exception(lambda: _webp.WebPDecoderVersion())
|
||||||
|
|
||||||
assert_equal(im.mode, "RGB")
|
def test_good_alpha():
|
||||||
assert_equal(im.size, (128, 128))
|
assert_equal(_webp.WebPDecoderBuggyAlpha(), 0)
|
||||||
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
|
||||||
|
expect?
|
||||||
|
|
||||||
file = tempfile("temp.webp")
|
"""
|
||||||
|
|
||||||
lena("RGB").save(file)
|
temp_file = tempfile("temp.webp")
|
||||||
|
|
||||||
im= Image.open(file)
|
lena("RGB").save(temp_file)
|
||||||
im.load()
|
|
||||||
|
|
||||||
assert_equal(im.mode, "RGB")
|
image = Image.open(temp_file)
|
||||||
assert_equal(im.size, (128, 128))
|
image.load()
|
||||||
assert_equal(im.format, "WEBP")
|
|
||||||
assert_no_exception(lambda: im.load())
|
assert_equal(image.mode, "RGB")
|
||||||
assert_no_exception(lambda: im.getdata())
|
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)
|
||||||
|
|
||||||
|
|
111
_webp.c
111
_webp.c
|
@ -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){
|
||||||
if (stride * height > size) {
|
Py_RETURN_NONE;
|
||||||
Py_INCREF(Py_None);
|
}
|
||||||
return Py_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;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
}
|
||||||
|
|
||||||
ret = PyBytes_FromStringAndSize((char*)output, width * height * 3);
|
if (vp8_status_code != VP8_STATUS_OK) {
|
||||||
free(output);
|
WebPFreeDecBuffer(&config.output);
|
||||||
return Py_BuildValue("Sii", ret, width, height);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if PY_VERSION_HEX >= 0x03000000
|
||||||
|
pymode = PyUnicode_FromString(mode);
|
||||||
|
#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) {
|
||||||
|
|
20
decode.c
20
decode.c
|
@ -48,6 +48,7 @@ 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;
|
||||||
|
@ -89,6 +90,9 @@ PyImaging_DecoderNew(int contextsize)
|
||||||
decoder->lock = NULL;
|
decoder->lock = NULL;
|
||||||
decoder->im = NULL;
|
decoder->im = NULL;
|
||||||
|
|
||||||
|
/* Initialize the cleanup function pointer */
|
||||||
|
decoder->cleanup = NULL;
|
||||||
|
|
||||||
return decoder;
|
return decoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,6 +119,20 @@ _decode(ImagingDecoderObject* decoder, PyObject* args)
|
||||||
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*
|
||||||
|
@ -178,6 +196,7 @@ _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 */
|
||||||
};
|
};
|
||||||
|
@ -755,6 +774,7 @@ PyImaging_JpegDecoderNew(PyObject* self, PyObject* args)
|
||||||
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);
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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
|
|
||||||
if (context->rawExifLen > 0)
|
|
||||||
jpeg_write_marker(&context->cinfo, JPEG_APP0+1, (unsigned char*)context->rawExif, context->rawExifLen);
|
|
||||||
|
|
||||||
break;
|
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 */
|
||||||
|
@ -255,7 +260,10 @@ ImagingJpegEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
|
||||||
} 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)
|
||||||
|
|
10
selftest.py
10
selftest.py
|
@ -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!
|
||||||
|
|
Loading…
Reference in New Issue
Block a user