From eda4436b5156a69fc21096a3dd3aae2eefe60dbc Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Sun, 4 Nov 2012 06:16:18 -0500 Subject: [PATCH] ensure that the transparency level option is passed to the octree encoder - closes #1556 --- src/image_util.cpp | 4 +- .../images/support/transparency/white0.png | Bin 0 -> 242 bytes .../images/support/transparency/white1.png | Bin 0 -> 257 bytes .../images/support/transparency/white2.png | Bin 0 -> 258 bytes tests/python_tests/png_encoding_test.py | 78 ++++++++++++++++++ 5 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 tests/python_tests/images/support/transparency/white0.png create mode 100644 tests/python_tests/images/support/transparency/white1.png create mode 100644 tests/python_tests/images/support/transparency/white2.png create mode 100644 tests/python_tests/png_encoding_test.py diff --git a/src/image_util.cpp b/src/image_util.cpp index a46455d71..623cf267d 100644 --- a/src/image_util.cpp +++ b/src/image_util.cpp @@ -239,7 +239,7 @@ void save_to_stream(T const& image, else if (colors < 0) save_as_png(stream, image, compression, strategy); else if (use_octree) - save_as_png8_oct(stream, image, colors, compression, strategy); + save_as_png8_oct(stream, image, colors, compression, strategy, trans_mode); else save_as_png8_hex(stream, image, colors, compression, strategy, trans_mode, gamma); } @@ -288,7 +288,7 @@ void save_to_stream(T const& image, if (colors < 0) save_as_png(stream, image, compression, strategy); else if (use_octree) - save_as_png8_oct(stream, image, colors, compression, strategy); + save_as_png8_oct(stream, image, colors, compression, strategy, trans_mode); else save_as_png8_hex(stream, image, colors, compression, strategy, trans_mode, gamma); } diff --git a/tests/python_tests/images/support/transparency/white0.png b/tests/python_tests/images/support/transparency/white0.png new file mode 100644 index 0000000000000000000000000000000000000000..955861acc963c9be3d04e5c49ffe53677e025a0f GIT binary patch literal 242 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K58CaNs)Vi3hp+Jf=z$e6&;Xe?72uX(bEbP0l+XkKz3M9F literal 0 HcmV?d00001 diff --git a/tests/python_tests/images/support/transparency/white2.png b/tests/python_tests/images/support/transparency/white2.png new file mode 100644 index 0000000000000000000000000000000000000000..136e0980d22e7fa7959c96021da0444dab58919f GIT binary patch literal 258 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K58CaNs)Vi3hp+Jf=z$e7@|9^)6V7%zbY-OMz zQ%R6tuyI4agSG~cx6ISUF{EP7+jEAD3CA=1C3>mG^{K7=ldlboFyt=akR{09uDD4*&oF literal 0 HcmV?d00001 diff --git a/tests/python_tests/png_encoding_test.py b/tests/python_tests/png_encoding_test.py new file mode 100644 index 000000000..2234d66e4 --- /dev/null +++ b/tests/python_tests/png_encoding_test.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import sys +import os, mapnik +from timeit import Timer, time +from nose.tools import * +from utilities import execution_path + +def setup(): + # All of the paths used are relative, if we run the tests + # from another directory we need to chdir() + os.chdir(execution_path('.')) + +tmp_dir = '/tmp/mapnik-png/' +if not os.path.exists(tmp_dir): + os.makedirs(tmp_dir) + +def test_transparency_levels(): + # create partial transparency image + im = mapnik.Image(256,256) + im.background = mapnik.Color('rgba(255,255,255,.5)') + c2 = mapnik.Color('rgba(255,255,0,.2)') + c3 = mapnik.Color('rgb(0,255,255)') + for y in range(0,im.height()/2): + for x in range(0,im.width()/2): + im.set_pixel(x,y,c2) + for y in range(im.height()/2,im.height()): + for x in range(im.width()/2,im.width()): + im.set_pixel(x,y,c3) + + t0 = tmp_dir + 'white0.png' + t2 = tmp_dir + 'white2.png' + t1 = tmp_dir + 'white1.png' + + # octree + format = 'png8:m=o:t=0' + im.save(t0,format) + im_in = mapnik.Image.open(t0) + t0_len = len(im_in.tostring(format)) + eq_(t0_len,len(mapnik.Image.open('images/support/transparency/white0.png').tostring(format))) + format = 'png8:m=o:t=1' + im.save(t1,format) + im_in = mapnik.Image.open(t1) + t1_len = len(im_in.tostring(format)) + eq_(len(im.tostring(format)),len(mapnik.Image.open('images/support/transparency/white1.png').tostring(format))) + format = 'png8:m=o:t=2' + im.save(t2,format) + im_in = mapnik.Image.open(t2) + t2_len = len(im_in.tostring(format)) + eq_(len(im.tostring(format)),len(mapnik.Image.open('images/support/transparency/white2.png').tostring(format))) + + eq_(t0_len < t1_len < t2_len,True) + + # hextree + format = 'png8:m=h:t=0' + im.save(t0,format) + im_in = mapnik.Image.open(t0) + t0_len = len(im_in.tostring(format)) + eq_(t0_len,len(mapnik.Image.open('images/support/transparency/white0.png').tostring(format))) + format = 'png8:m=h:t=1' + im.save(t1,format) + im_in = mapnik.Image.open(t1) + t1_len = len(im_in.tostring(format)) + eq_(len(im.tostring(format)),len(mapnik.Image.open('images/support/transparency/white1.png').tostring(format))) + format = 'png8:m=h:t=2' + im.save(t2,format) + im_in = mapnik.Image.open(t2) + t2_len = len(im_in.tostring(format)) + eq_(len(im.tostring(format)),len(mapnik.Image.open('images/support/transparency/white2.png').tostring(format))) + + eq_(t0_len < t1_len < t2_len,True) + +if __name__ == "__main__": + setup() + for t in dir(): + if 'test_' in t: + eval(t)()