2013-05-09 03:13:20 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import os, mapnik
|
2015-02-02 18:31:16 +00:00
|
|
|
from nose.tools import eq_,raises
|
2013-06-03 02:28:24 +00:00
|
|
|
from utilities import execution_path, run_all
|
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('.'))
|
|
|
|
|
2013-07-02 20:01:23 +00:00
|
|
|
def test_image_premultiply():
|
|
|
|
im = mapnik.Image(256,256)
|
|
|
|
eq_(im.premultiplied(),False)
|
|
|
|
im.premultiply()
|
|
|
|
eq_(im.premultiplied(),True)
|
|
|
|
im.demultiply()
|
|
|
|
eq_(im.premultiplied(),False)
|
|
|
|
|
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)
|
|
|
|
im.background = mapnik.Color('rgba(1,2,3,.5)')
|
|
|
|
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)
|
|
|
|
im.background = mapnik.Color('rgba(1,2,3,.5)')
|
|
|
|
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_")))
|