From 503da57b6ee46cf655b37d49b13d1ac3e1bba6a7 Mon Sep 17 00:00:00 2001 From: homm Date: Fri, 17 Jan 2014 17:20:11 +0400 Subject: [PATCH 1/2] do not use loseless abi until it supported --- _webp.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/_webp.c b/_webp.c index 6381e1a56..c201813d7 100644 --- a/_webp.c +++ b/_webp.c @@ -34,18 +34,24 @@ PyObject* WebPEncode_wrapper(PyObject* self, PyObject* args) if (size < width * height * 4){ Py_RETURN_NONE; } + #if WEBP_ENCODER_ABI_VERSION >= 0x0100 if (lossless) { ret_size = WebPEncodeLosslessRGBA(rgb, width, height, 4* width, &output); - } else { + } else + #endif + { 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; } + #if WEBP_ENCODER_ABI_VERSION >= 0x0100 if (lossless) { ret_size = WebPEncodeLosslessRGB(rgb, width, height, 3* width, &output); - } else { + } else + #endif + { ret_size = WebPEncodeRGB(rgb, width, height, 3* width, quality_factor, &output); } } else { From 78a51237c50b1098a62b0cd3cd181ea5c08e33d0 Mon Sep 17 00:00:00 2001 From: wiredfool Date: Mon, 20 Jan 2014 13:05:30 -0800 Subject: [PATCH 2/2] tests for #491/492, webp lossless abi --- Tests/test_file_webp.py | 93 +------------------------------- Tests/test_file_webp_alpha.py | 82 ++++++++++++++++++++++++++++ Tests/test_file_webp_lossless.py | 33 ++++++++++++ 3 files changed, 117 insertions(+), 91 deletions(-) create mode 100644 Tests/test_file_webp_alpha.py create mode 100644 Tests/test_file_webp_lossless.py diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index 944067574..a89dea3c4 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -2,6 +2,7 @@ from tester import * from PIL import Image + try: from PIL import _webp except: @@ -10,10 +11,7 @@ except: def test_version(): assert_no_exception(lambda: _webp.WebPDecoderVersion()) - -def test_good_alpha(): - assert_equal(_webp.WebPDecoderBuggyAlpha(), 0) - + assert_no_exception(lambda: _webp.WebPDecoderBuggyAlpha()) def test_read_rgb(): @@ -66,92 +64,5 @@ def test_write_rgb(): assert_image_similar(image, target, 20.0) -def test_write_lossless_rgb(): - temp_file = tempfile("temp.webp") - - lena("RGB").save(temp_file, lossless=True) - - image = Image.open(temp_file) - image.load() - - 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()) - assert_image_equal(image, lena("RGB")) - - - - -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) - - - -def test_write_lossless_rgb(): - temp_file = tempfile("temp.webp") - #temp_file = "temp.webp" - - pil_image = lena('RGBA') - - mask = Image.new("RGBA", (64, 64), (128,128,128,128)) - pil_image.paste(mask, (0,0), mask) # add some partially transparent bits. - - pil_image.save(temp_file, lossless=True) - - image = Image.open(temp_file) - image.load() - - assert_equal(image.mode, "RGBA") - assert_equal(image.size, pil_image.size) - assert_equal(image.format, "WEBP") - assert_no_exception(lambda: image.load()) - assert_no_exception(lambda: image.getdata()) - - - assert_image_equal(image, pil_image) diff --git a/Tests/test_file_webp_alpha.py b/Tests/test_file_webp_alpha.py new file mode 100644 index 000000000..5ac03b9d4 --- /dev/null +++ b/Tests/test_file_webp_alpha.py @@ -0,0 +1,82 @@ +from tester import * + +from PIL import Image + +try: + from PIL import _webp +except: + skip('webp support not installed') + + +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) + + +def test_write_lossless_rgb(): + temp_file = tempfile("temp.webp") + #temp_file = "temp.webp" + + pil_image = lena('RGBA') + + mask = Image.new("RGBA", (64, 64), (128,128,128,128)) + pil_image.paste(mask, (0,0), mask) # add some partially transparent bits. + + pil_image.save(temp_file, lossless=True) + + image = Image.open(temp_file) + image.load() + + assert_equal(image.mode, "RGBA") + assert_equal(image.size, pil_image.size) + assert_equal(image.format, "WEBP") + assert_no_exception(lambda: image.load()) + assert_no_exception(lambda: image.getdata()) + + + assert_image_equal(image, pil_image) + +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) + + + + diff --git a/Tests/test_file_webp_lossless.py b/Tests/test_file_webp_lossless.py new file mode 100644 index 000000000..ca2b5af19 --- /dev/null +++ b/Tests/test_file_webp_lossless.py @@ -0,0 +1,33 @@ +from tester import * + +from PIL import Image + + +try: + from PIL import _webp +except: + skip('webp support not installed') + + +if (_webp.WebPDecoderVersion() < 0x0200): + skip('lossless not included') + +def test_write_lossless_rgb(): + temp_file = tempfile("temp.webp") + + lena("RGB").save(temp_file, lossless=True) + + image = Image.open(temp_file) + image.load() + + 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()) + + + assert_image_equal(image, lena("RGB")) + + +