2013-05-09 03:13:20 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import os, mapnik
|
2015-02-03 08:38:55 +00:00
|
|
|
from nose.tools import eq_,raises, assert_almost_equal
|
2015-01-16 03:03:42 +00:00
|
|
|
from utilities import execution_path, run_all, get_unique_colors
|
2013-05-09 03:13:20 +00:00
|
|
|
|
|
|
|
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('.'))
|
|
|
|
|
2015-03-06 23:24:23 +00:00
|
|
|
def test_type():
|
|
|
|
im = mapnik.Image(256, 256)
|
|
|
|
eq_(im.get_type(), mapnik.ImageType.rgba8)
|
|
|
|
im = mapnik.Image(256, 256, mapnik.ImageType.gray8)
|
|
|
|
eq_(im.get_type(), mapnik.ImageType.gray8)
|
|
|
|
|
2013-07-02 20:01:23 +00:00
|
|
|
def test_image_premultiply():
|
|
|
|
im = mapnik.Image(256,256)
|
|
|
|
eq_(im.premultiplied(),False)
|
2015-01-15 23:57:21 +00:00
|
|
|
# Premultiply should return true that it worked
|
|
|
|
eq_(im.premultiply(), True)
|
2013-07-02 20:01:23 +00:00
|
|
|
eq_(im.premultiplied(),True)
|
2015-01-15 23:57:21 +00:00
|
|
|
# Premultipling again should return false as nothing should happen
|
|
|
|
eq_(im.premultiply(), False)
|
|
|
|
eq_(im.premultiplied(),True)
|
|
|
|
# Demultiply should return true that it worked
|
|
|
|
eq_(im.demultiply(), True)
|
|
|
|
eq_(im.premultiplied(),False)
|
|
|
|
# Demultiply again should not work and return false as it did nothing
|
|
|
|
eq_(im.demultiply(), False)
|
2013-07-02 20:01:23 +00:00
|
|
|
eq_(im.premultiplied(),False)
|
|
|
|
|
2015-01-24 00:08:59 +00:00
|
|
|
def test_image_premultiply_values():
|
|
|
|
im = mapnik.Image(256,256)
|
2015-01-29 20:27:42 +00:00
|
|
|
im.fill(mapnik.Color(16, 33, 255, 128))
|
2015-01-24 00:08:59 +00:00
|
|
|
im.premultiply()
|
2015-02-23 16:27:00 +00:00
|
|
|
c = im.get_pixel(0,0, True)
|
2015-01-24 00:08:59 +00:00
|
|
|
eq_(c.r, 8)
|
|
|
|
eq_(c.g, 17)
|
|
|
|
eq_(c.b, 128)
|
|
|
|
eq_(c.a, 128)
|
|
|
|
im.demultiply()
|
|
|
|
# Do to the nature of this operation the result will not be exactly the same
|
2015-02-23 16:27:00 +00:00
|
|
|
c = im.get_pixel(0,0,True)
|
2015-01-24 00:08:59 +00:00
|
|
|
eq_(c.r,15)
|
|
|
|
eq_(c.g,33)
|
|
|
|
eq_(c.b,255)
|
|
|
|
eq_(c.a,128)
|
|
|
|
|
|
|
|
def test_background():
|
|
|
|
im = mapnik.Image(256,256)
|
|
|
|
eq_(im.premultiplied(), False)
|
2015-01-29 20:27:42 +00:00
|
|
|
im.fill(mapnik.Color(32,64,125,128))
|
2015-01-24 00:08:59 +00:00
|
|
|
eq_(im.premultiplied(), False)
|
2015-02-23 16:27:00 +00:00
|
|
|
c = im.get_pixel(0,0,True)
|
2015-01-24 00:08:59 +00:00
|
|
|
eq_(c.get_premultiplied(), False)
|
|
|
|
eq_(c.r,32)
|
|
|
|
eq_(c.g,64)
|
|
|
|
eq_(c.b,125)
|
|
|
|
eq_(c.a,128)
|
|
|
|
# Now again with a premultiplied alpha
|
2015-01-29 20:27:42 +00:00
|
|
|
im.fill(mapnik.Color(32,64,125,128,True))
|
2015-01-24 00:08:59 +00:00
|
|
|
eq_(im.premultiplied(), True)
|
2015-02-23 16:27:00 +00:00
|
|
|
c = im.get_pixel(0,0,True)
|
2015-01-24 00:08:59 +00:00
|
|
|
eq_(c.get_premultiplied(), True)
|
|
|
|
eq_(c.r,32)
|
|
|
|
eq_(c.g,64)
|
|
|
|
eq_(c.b,125)
|
|
|
|
eq_(c.a,128)
|
|
|
|
|
|
|
|
def test_set_and_get_pixel():
|
|
|
|
# Create an image that is not premultiplied
|
|
|
|
im = mapnik.Image(256,256)
|
|
|
|
c0 = mapnik.Color(16,33,255,128)
|
|
|
|
c0_pre = mapnik.Color(16,33,255,128, True)
|
|
|
|
im.set_pixel(0,0,c0)
|
|
|
|
im.set_pixel(1,1,c0_pre)
|
|
|
|
# No differences for non premultiplied pixels
|
|
|
|
c1_int = mapnik.Color(im.get_pixel(0,0))
|
|
|
|
eq_(c0.r, c1_int.r)
|
|
|
|
eq_(c0.g, c1_int.g)
|
|
|
|
eq_(c0.b, c1_int.b)
|
|
|
|
eq_(c0.a, c1_int.a)
|
2015-02-23 16:27:00 +00:00
|
|
|
c1 = im.get_pixel(0,0,True)
|
2015-01-24 00:08:59 +00:00
|
|
|
eq_(c0.r, c1.r)
|
|
|
|
eq_(c0.g, c1.g)
|
|
|
|
eq_(c0.b, c1.b)
|
|
|
|
eq_(c0.a, c1.a)
|
|
|
|
# The premultiplied Color should be demultiplied before being applied.
|
|
|
|
c0_pre.demultiply()
|
|
|
|
c1_int = mapnik.Color(im.get_pixel(1,1))
|
|
|
|
eq_(c0_pre.r, c1_int.r)
|
|
|
|
eq_(c0_pre.g, c1_int.g)
|
|
|
|
eq_(c0_pre.b, c1_int.b)
|
|
|
|
eq_(c0_pre.a, c1_int.a)
|
2015-02-23 16:27:00 +00:00
|
|
|
c1 = im.get_pixel(1,1,True)
|
2015-01-24 00:08:59 +00:00
|
|
|
eq_(c0_pre.r, c1.r)
|
|
|
|
eq_(c0_pre.g, c1.g)
|
|
|
|
eq_(c0_pre.b, c1.b)
|
|
|
|
eq_(c0_pre.a, c1.a)
|
|
|
|
|
|
|
|
# Now create a new image that is premultiplied
|
|
|
|
im = mapnik.Image(256,256, mapnik.ImageType.rgba8, True, True)
|
|
|
|
c0 = mapnik.Color(16,33,255,128)
|
|
|
|
c0_pre = mapnik.Color(16,33,255,128, True)
|
|
|
|
im.set_pixel(0,0,c0)
|
|
|
|
im.set_pixel(1,1,c0_pre)
|
|
|
|
# It should have put pixels that are the same as premultiplied so premultiply c0
|
|
|
|
c0.premultiply()
|
|
|
|
c1_int = mapnik.Color(im.get_pixel(0,0))
|
|
|
|
eq_(c0.r, c1_int.r)
|
|
|
|
eq_(c0.g, c1_int.g)
|
|
|
|
eq_(c0.b, c1_int.b)
|
|
|
|
eq_(c0.a, c1_int.a)
|
2015-02-23 16:27:00 +00:00
|
|
|
c1 = im.get_pixel(0,0,True)
|
2015-01-24 00:08:59 +00:00
|
|
|
eq_(c0.r, c1.r)
|
|
|
|
eq_(c0.g, c1.g)
|
|
|
|
eq_(c0.b, c1.b)
|
|
|
|
eq_(c0.a, c1.a)
|
|
|
|
# The premultiplied Color should be the same though
|
|
|
|
c1_int = mapnik.Color(im.get_pixel(1,1))
|
|
|
|
eq_(c0_pre.r, c1_int.r)
|
|
|
|
eq_(c0_pre.g, c1_int.g)
|
|
|
|
eq_(c0_pre.b, c1_int.b)
|
|
|
|
eq_(c0_pre.a, c1_int.a)
|
2015-02-23 16:27:00 +00:00
|
|
|
c1 = im.get_pixel(1,1,True)
|
2015-01-24 00:08:59 +00:00
|
|
|
eq_(c0_pre.r, c1.r)
|
|
|
|
eq_(c0_pre.g, c1.g)
|
|
|
|
eq_(c0_pre.b, c1.b)
|
|
|
|
eq_(c0_pre.a, c1.a)
|
|
|
|
|
2015-02-16 20:23:55 +00:00
|
|
|
def test_pixel_gray8():
|
|
|
|
im = mapnik.Image(4,4,mapnik.ImageType.gray8)
|
|
|
|
val_list = range(20)
|
|
|
|
for v in val_list:
|
|
|
|
im.set_pixel(0,0, v)
|
|
|
|
eq_(im.get_pixel(0,0), v)
|
|
|
|
im.set_pixel(0,0, -v)
|
|
|
|
eq_(im.get_pixel(0,0), 0)
|
|
|
|
|
|
|
|
def test_pixel_gray8s():
|
|
|
|
im = mapnik.Image(4,4,mapnik.ImageType.gray8s)
|
|
|
|
val_list = range(20)
|
|
|
|
for v in val_list:
|
|
|
|
im.set_pixel(0,0, v)
|
|
|
|
eq_(im.get_pixel(0,0), v)
|
|
|
|
im.set_pixel(0,0, -v)
|
|
|
|
eq_(im.get_pixel(0,0), -v)
|
|
|
|
|
|
|
|
def test_pixel_gray16():
|
|
|
|
im = mapnik.Image(4,4,mapnik.ImageType.gray16)
|
|
|
|
val_list = range(20)
|
|
|
|
for v in val_list:
|
|
|
|
im.set_pixel(0,0, v)
|
|
|
|
eq_(im.get_pixel(0,0), v)
|
|
|
|
im.set_pixel(0,0, -v)
|
|
|
|
eq_(im.get_pixel(0,0), 0)
|
|
|
|
|
|
|
|
def test_pixel_gray16s():
|
|
|
|
im = mapnik.Image(4,4,mapnik.ImageType.gray16s)
|
|
|
|
val_list = range(20)
|
|
|
|
for v in val_list:
|
|
|
|
im.set_pixel(0,0, v)
|
|
|
|
eq_(im.get_pixel(0,0), v)
|
|
|
|
im.set_pixel(0,0, -v)
|
|
|
|
eq_(im.get_pixel(0,0), -v)
|
|
|
|
|
|
|
|
def test_pixel_gray32():
|
|
|
|
im = mapnik.Image(4,4,mapnik.ImageType.gray32)
|
|
|
|
val_list = range(20)
|
|
|
|
for v in val_list:
|
|
|
|
im.set_pixel(0,0, v)
|
|
|
|
eq_(im.get_pixel(0,0), v)
|
|
|
|
im.set_pixel(0,0, -v)
|
|
|
|
eq_(im.get_pixel(0,0), 0)
|
|
|
|
|
|
|
|
def test_pixel_gray32s():
|
|
|
|
im = mapnik.Image(4,4,mapnik.ImageType.gray32s)
|
|
|
|
val_list = range(20)
|
|
|
|
for v in val_list:
|
|
|
|
im.set_pixel(0,0, v)
|
|
|
|
eq_(im.get_pixel(0,0), v)
|
|
|
|
im.set_pixel(0,0, -v)
|
|
|
|
eq_(im.get_pixel(0,0), -v)
|
|
|
|
|
|
|
|
def test_pixel_gray64():
|
|
|
|
im = mapnik.Image(4,4,mapnik.ImageType.gray64)
|
|
|
|
val_list = range(20)
|
|
|
|
for v in val_list:
|
|
|
|
im.set_pixel(0,0, v)
|
|
|
|
eq_(im.get_pixel(0,0), v)
|
|
|
|
im.set_pixel(0,0, -v)
|
|
|
|
eq_(im.get_pixel(0,0), 0)
|
|
|
|
|
|
|
|
def test_pixel_gray64s():
|
|
|
|
im = mapnik.Image(4,4,mapnik.ImageType.gray64s)
|
|
|
|
val_list = range(20)
|
|
|
|
for v in val_list:
|
|
|
|
im.set_pixel(0,0, v)
|
|
|
|
eq_(im.get_pixel(0,0), v)
|
|
|
|
im.set_pixel(0,0, -v)
|
|
|
|
eq_(im.get_pixel(0,0), -v)
|
|
|
|
|
2015-01-29 20:27:42 +00:00
|
|
|
def test_pixel_floats():
|
|
|
|
im = mapnik.Image(4,4,mapnik.ImageType.gray32f)
|
|
|
|
val_list = [0.9, 0.99, 0.999, 0.9999, 0.99999, 1, 1.0001, 1.001, 1.01, 1.1]
|
|
|
|
for v in val_list:
|
|
|
|
im.set_pixel(0,0, v)
|
|
|
|
assert_almost_equal(im.get_pixel(0,0), v)
|
2015-02-16 20:23:55 +00:00
|
|
|
im.set_pixel(0,0, -v)
|
|
|
|
assert_almost_equal(im.get_pixel(0,0), -v)
|
|
|
|
|
|
|
|
def test_pixel_doubles():
|
|
|
|
im = mapnik.Image(4,4,mapnik.ImageType.gray64f)
|
|
|
|
val_list = [0.9, 0.99, 0.999, 0.9999, 0.99999, 1, 1.0001, 1.001, 1.01, 1.1]
|
|
|
|
for v in val_list:
|
|
|
|
im.set_pixel(0,0, v)
|
|
|
|
assert_almost_equal(im.get_pixel(0,0), v)
|
2015-01-29 20:27:42 +00:00
|
|
|
im.set_pixel(0,0, -v)
|
|
|
|
assert_almost_equal(im.get_pixel(0,0), -v)
|
|
|
|
|
2015-01-29 02:20:14 +00:00
|
|
|
def test_pixel_overflow():
|
|
|
|
im = mapnik.Image(4,4,mapnik.ImageType.gray8)
|
|
|
|
im.set_pixel(0,0,256)
|
|
|
|
eq_(im.get_pixel(0,0),255)
|
|
|
|
|
|
|
|
def test_pixel_underflow():
|
|
|
|
im = mapnik.Image(4,4,mapnik.ImageType.gray8)
|
|
|
|
im.set_pixel(0,0,-1)
|
|
|
|
eq_(im.get_pixel(0,0),0)
|
2015-02-04 21:41:58 +00:00
|
|
|
im = mapnik.Image(4,4,mapnik.ImageType.gray16)
|
|
|
|
im.set_pixel(0,0,-1)
|
|
|
|
eq_(im.get_pixel(0,0),0)
|
2015-01-29 02:20:14 +00:00
|
|
|
|
2015-01-24 00:08:59 +00:00
|
|
|
@raises(IndexError)
|
|
|
|
def test_set_pixel_out_of_range_1():
|
|
|
|
im = mapnik.Image(4,4)
|
|
|
|
c = mapnik.Color('blue')
|
|
|
|
im.set_pixel(5,5,c)
|
|
|
|
|
|
|
|
@raises(OverflowError)
|
|
|
|
def test_set_pixel_out_of_range_2():
|
|
|
|
im = mapnik.Image(4,4)
|
|
|
|
c = mapnik.Color('blue')
|
|
|
|
im.set_pixel(-1,1,c)
|
|
|
|
|
|
|
|
@raises(IndexError)
|
|
|
|
def test_get_pixel_out_of_range_1():
|
|
|
|
im = mapnik.Image(4,4)
|
|
|
|
c = im.get_pixel(5,5)
|
|
|
|
|
|
|
|
@raises(OverflowError)
|
|
|
|
def test_get_pixel_out_of_range_2():
|
|
|
|
im = mapnik.Image(4,4)
|
|
|
|
c = im.get_pixel(-1,1)
|
|
|
|
|
|
|
|
@raises(IndexError)
|
|
|
|
def test_get_pixel_color_out_of_range_1():
|
|
|
|
im = mapnik.Image(4,4)
|
2015-02-23 16:27:00 +00:00
|
|
|
c = im.get_pixel(5,5,True)
|
2015-01-24 00:08:59 +00:00
|
|
|
|
|
|
|
@raises(OverflowError)
|
|
|
|
def test_get_pixel_color_out_of_range_2():
|
|
|
|
im = mapnik.Image(4,4)
|
2015-02-23 16:27:00 +00:00
|
|
|
c = im.get_pixel(-1,1,True)
|
2015-01-24 00:08:59 +00:00
|
|
|
|
2015-01-16 03:03:42 +00:00
|
|
|
def test_set_color_to_alpha():
|
|
|
|
im = mapnik.Image(256,256)
|
2015-01-29 20:27:42 +00:00
|
|
|
im.fill(mapnik.Color('rgba(12,12,12,255)'))
|
2015-01-16 03:03:42 +00:00
|
|
|
eq_(get_unique_colors(im), ['rgba(12,12,12,255)'])
|
|
|
|
im.set_color_to_alpha(mapnik.Color('rgba(12,12,12,0)'))
|
|
|
|
eq_(get_unique_colors(im), ['rgba(0,0,0,0)'])
|
|
|
|
|
2014-12-05 16:36:44 +00:00
|
|
|
@raises(RuntimeError)
|
|
|
|
def test_negative_image_dimensions():
|
|
|
|
# TODO - this may have regressed in https://github.com/mapnik/mapnik/commit/4f3521ac24b61fc8ae8fd344a16dc3a5fdf15af7
|
|
|
|
im = mapnik.Image(-40,40)
|
2015-02-02 18:31:16 +00:00
|
|
|
# should not get here
|
|
|
|
eq_(im.width(),0)
|
|
|
|
eq_(im.height(),0)
|
2013-06-26 20:46:36 +00:00
|
|
|
|
2013-05-10 21:34:29 +00:00
|
|
|
def test_jpeg_round_trip():
|
|
|
|
filepath = '/tmp/mapnik-jpeg-io.jpeg'
|
|
|
|
im = mapnik.Image(255,267)
|
2015-01-29 20:27:42 +00:00
|
|
|
im.fill(mapnik.Color('rgba(1,2,3,.5)'))
|
2013-05-10 21:34:29 +00:00
|
|
|
im.save(filepath,'jpeg')
|
|
|
|
im2 = mapnik.Image.open(filepath)
|
2014-12-05 16:36:44 +00:00
|
|
|
im3 = mapnik.Image.fromstring(open(filepath,'r').read())
|
2013-05-10 21:34:29 +00:00
|
|
|
eq_(im.width(),im2.width())
|
|
|
|
eq_(im.height(),im2.height())
|
2014-12-05 16:36:44 +00:00
|
|
|
eq_(im.width(),im3.width())
|
|
|
|
eq_(im.height(),im3.height())
|
2013-05-10 21:34:29 +00:00
|
|
|
eq_(len(im.tostring()),len(im2.tostring()))
|
|
|
|
eq_(len(im.tostring('jpeg')),len(im2.tostring('jpeg')))
|
2014-12-05 16:36:44 +00:00
|
|
|
eq_(len(im.tostring()),len(im3.tostring()))
|
|
|
|
eq_(len(im.tostring('jpeg')),len(im3.tostring('jpeg')))
|
2013-05-10 21:34:29 +00:00
|
|
|
|
|
|
|
def test_png_round_trip():
|
|
|
|
filepath = '/tmp/mapnik-png-io.png'
|
|
|
|
im = mapnik.Image(255,267)
|
2015-01-29 20:27:42 +00:00
|
|
|
im.fill(mapnik.Color('rgba(1,2,3,.5)'))
|
2013-05-10 21:34:29 +00:00
|
|
|
im.save(filepath,'png')
|
|
|
|
im2 = mapnik.Image.open(filepath)
|
2014-12-05 16:36:44 +00:00
|
|
|
im3 = mapnik.Image.fromstring(open(filepath,'r').read())
|
2013-05-10 21:34:29 +00:00
|
|
|
eq_(im.width(),im2.width())
|
|
|
|
eq_(im.height(),im2.height())
|
2014-12-05 16:36:44 +00:00
|
|
|
eq_(im.width(),im3.width())
|
|
|
|
eq_(im.height(),im3.height())
|
2013-05-10 21:34:29 +00:00
|
|
|
eq_(len(im.tostring()),len(im2.tostring()))
|
|
|
|
eq_(len(im.tostring('png')),len(im2.tostring('png')))
|
|
|
|
eq_(len(im.tostring('png8')),len(im2.tostring('png8')))
|
2014-12-05 16:36:44 +00:00
|
|
|
eq_(len(im.tostring()),len(im3.tostring()))
|
|
|
|
eq_(len(im.tostring('png')),len(im3.tostring('png')))
|
|
|
|
eq_(len(im.tostring('png8')),len(im3.tostring('png8')))
|
2013-05-09 03:13:20 +00:00
|
|
|
|
|
|
|
def test_image_open_from_string():
|
|
|
|
filepath = '../data/images/dummy.png'
|
|
|
|
im1 = mapnik.Image.open(filepath)
|
|
|
|
im2 = mapnik.Image.fromstring(open(filepath,'rb').read())
|
|
|
|
eq_(im1.width(),im2.width())
|
|
|
|
length = len(im1.tostring())
|
|
|
|
eq_(length,len(im2.tostring()))
|
|
|
|
eq_(len(mapnik.Image.fromstring(im1.tostring('png')).tostring()),length)
|
|
|
|
eq_(len(mapnik.Image.fromstring(im1.tostring('jpeg')).tostring()),length)
|
2013-05-09 15:46:46 +00:00
|
|
|
eq_(len(mapnik.Image.frombuffer(buffer(im1.tostring('png'))).tostring()),length)
|
|
|
|
eq_(len(mapnik.Image.frombuffer(buffer(im1.tostring('jpeg'))).tostring()),length)
|
2013-09-19 02:55:02 +00:00
|
|
|
|
2013-05-09 15:45:11 +00:00
|
|
|
# TODO - https://github.com/mapnik/mapnik/issues/1831
|
2013-05-13 16:25:05 +00:00
|
|
|
eq_(len(mapnik.Image.fromstring(im1.tostring('tiff')).tostring()),length)
|
|
|
|
eq_(len(mapnik.Image.frombuffer(buffer(im1.tostring('tiff'))).tostring()),length)
|
2013-05-09 03:13:20 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
setup()
|
2014-07-14 16:34:20 +00:00
|
|
|
exit(run_all(eval(x) for x in dir() if x.startswith("test_")))
|