2013-05-09 05:13:20 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os, mapnik
|
|
|
|
from timeit import Timer, time
|
|
|
|
from nose.tools import *
|
2013-06-03 04:28:24 +02:00
|
|
|
from utilities import execution_path, run_all
|
2013-05-09 05:13:20 +02: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 22:01:23 +02: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)
|
|
|
|
|
2013-07-24 00:38:57 +02:00
|
|
|
# Disabled for now since this breaks hard if run against
|
|
|
|
# a mapnik version that does not have the fix
|
|
|
|
#@raises(RuntimeError)
|
|
|
|
#def test_negative_image_dimensions():
|
|
|
|
#im = mapnik.Image(-40,40)
|
2013-06-26 22:46:36 +02:00
|
|
|
|
2014-12-04 16:48:19 +01:00
|
|
|
def test_tiff_round_trip_scanline():
|
2014-12-04 23:11:33 +01:00
|
|
|
filepath = '/tmp/mapnik-tiff-io-scanline.tiff'
|
2014-12-04 16:48:19 +01:00
|
|
|
im = mapnik.Image(255,267)
|
|
|
|
im.background = mapnik.Color('rgba(1,2,3,.5)')
|
2014-12-04 23:11:33 +01:00
|
|
|
im.save(filepath,'tiff:zlevel=0:scanline=1:compression=lzw')
|
2014-12-04 16:48:19 +01:00
|
|
|
im2 = mapnik.Image.open(filepath)
|
|
|
|
eq_(im.width(),im2.width())
|
|
|
|
eq_(im.height(),im2.height())
|
|
|
|
eq_(len(im.tostring()),len(im2.tostring()))
|
|
|
|
eq_(len(im.tostring('tiff')),len(im2.tostring('tiff')))
|
|
|
|
|
|
|
|
def test_tiff_round_trip_tiled():
|
2014-12-04 23:11:33 +01:00
|
|
|
filepath = '/tmp/mapnik-tiff-io-tiled.tiff'
|
2014-12-04 00:41:49 +01:00
|
|
|
im = mapnik.Image(256,256)
|
2013-05-10 23:34:29 +02:00
|
|
|
im.background = mapnik.Color('rgba(1,2,3,.5)')
|
|
|
|
im.save(filepath,'tiff')
|
|
|
|
im2 = mapnik.Image.open(filepath)
|
|
|
|
eq_(im.width(),im2.width())
|
|
|
|
eq_(im.height(),im2.height())
|
|
|
|
eq_(len(im.tostring()),len(im2.tostring()))
|
|
|
|
eq_(len(im.tostring('tiff')),len(im2.tostring('tiff')))
|
|
|
|
|
|
|
|
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)
|
|
|
|
eq_(im.width(),im2.width())
|
|
|
|
eq_(im.height(),im2.height())
|
|
|
|
eq_(len(im.tostring()),len(im2.tostring()))
|
|
|
|
eq_(len(im.tostring('jpeg')),len(im2.tostring('jpeg')))
|
|
|
|
|
|
|
|
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)
|
|
|
|
eq_(im.width(),im2.width())
|
|
|
|
eq_(im.height(),im2.height())
|
|
|
|
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')))
|
2013-05-09 05:13:20 +02: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 17:46:46 +02: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 04:55:02 +02:00
|
|
|
|
2013-05-09 17:45:11 +02:00
|
|
|
# TODO - https://github.com/mapnik/mapnik/issues/1831
|
2013-05-13 18:25:05 +02: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 05:13:20 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
setup()
|
2014-07-14 18:34:20 +02:00
|
|
|
exit(run_all(eval(x) for x in dir() if x.startswith("test_")))
|