Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
argparse==1.2.1
noise==1.2.2
numpy==1.9.2
Pillow==2.8.2
pypng==0.0.18
PyPlatec==1.4.0
protobuf==3.0.0a3
six==1.10.0
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
'entry_points': {
'console_scripts': ['worldengine=worldengine.cli.main:main'],
},
'install_requires': ['Pillow==2.8.2', 'PyPlatec==1.4.0',
'argparse==1.2.1', 'noise==1.2.2', 'protobuf>=2.6.0',
'numpy>=1.9.2'],
'install_requires': ['PyPlatec==1.4.0', 'pypng>=0.0.18', 'numpy>=1.9.2',
'argparse==1.2.1', 'noise==1.2.2', 'protobuf>=2.6.0'],
'license': 'MIT License'
}

Expand Down
4 changes: 2 additions & 2 deletions tests/blessed_images/generated_blessed_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import platform
from worldengine.world import *
from worldengine.draw import *
from worldengine.image_io import PNGWriter


def main(blessed_images_dir, tests_data_dir):
Expand All @@ -31,8 +32,7 @@ def main(blessed_images_dir, tests_data_dir):
draw_ancientmap_on_file(w, "%s/ancientmap_28070_factor3.png" % blessed_images_dir, resize_factor=3)
draw_ancientmap_on_file(w_large, "%s/ancientmap_48956.png" % blessed_images_dir, resize_factor=1)

img = ImagePixelSetter(w.width * 2, w.height * 2, "%s/rivers_28070_factor2.png" %
blessed_images_dir)
img = PNGWriter.rgba_from_dimensions(w.width * 2, w.height * 2, "%s/rivers_28070_factor2.png" % blessed_images_dir)
draw_rivers_on_image(w, img, factor=2)
img.complete()

Expand Down
80 changes: 33 additions & 47 deletions tests/draw_test.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,12 @@
import unittest
import os
from worldengine.draw import _biome_colors, Image, draw_simple_elevation, elevation_color, \
import numpy
from worldengine.draw import _biome_colors, draw_simple_elevation, elevation_color, \
draw_elevation, draw_riversmap, draw_grayscale_heightmap, draw_ocean, draw_precipitation, \
draw_world, draw_temperature_levels, draw_biome, draw_scatter_plot
from worldengine.biome import Biome
from worldengine.world import World


class PixelCollector:

def __init__(self, width, height):
self.pixels = {}
self.width = width
self.height = height
for y in range(height):
for x in range(width):
self.pixels[x, y] = (0, 0, 0, 0)

def set_pixel(self, x, y, color):
if len(color) == 3:
color = color + (255,)
self.pixels[(x, y)] = color

def __getitem__(self, item):
return self.pixels[item]

def __setitem__(self, key, value):
self.pixels[key] = value
from worldengine.image_io import PNGWriter, PNGReader


class TestBase(unittest.TestCase):
Expand Down Expand Up @@ -55,19 +35,25 @@ def _assert_are_colors_equal(self, expected, actual):
self.assertEqual(expected, actual)

def _assert_img_equal(self, blessed_image_name, drawn_image):
blessed_img = Image.open("%s/%s.png" % (self.tests_blessed_images_dir, blessed_image_name))
blessed_img_pixels = blessed_img.load()
blessed_img = PNGReader("%s/%s.png" % (self.tests_blessed_images_dir, blessed_image_name))

# check shapes (i.e. (height, width, channels)-tuple)
self.assertTrue(blessed_img.array.shape == drawn_image.array.shape,
"Blessed and drawn images differ in height, width " +
"and/or amount of channels. Blessed %s, drawn %s"
% (str(blessed_img.array.shape), str(drawn_image.array.shape)))

# compare images; cmp_array will be an array of booleans in case of equal shapes (and a pure boolean otherwise)
cmp_array = blessed_img.array != drawn_image.array

blessed_img_width, blessed_img_height = blessed_img.size
self.assertEqual(blessed_img_width, drawn_image.width)
self.assertEqual(blessed_img_height, drawn_image.height)
for y in range(blessed_img_height):
for x in range(blessed_img_width):
blessed_pixel = blessed_img_pixels[x, y]
drawn_pixel = drawn_image[x, y]
self.assertEqual(blessed_pixel, drawn_pixel,
"Pixels at %i, %i are different. Blessed %s, drawn %s"
% (x, y, blessed_pixel, drawn_pixel))
# avoid calling assertTrue if shapes differed; results would be weird (and meaningless)
if numpy.any(cmp_array):
diff = numpy.transpose(numpy.nonzero(cmp_array)) # list of tuples of differing indices
self.assertTrue(False,
"Pixels at %i, %i are different. Blessed %s, drawn %s"
% (diff[0][0], diff[0][1],
blessed_img.array[diff[0][0], diff[0][1]],
drawn_image.array[diff[0][0], diff[0][1]]))


class TestDraw(TestBase):
Expand Down Expand Up @@ -102,69 +88,69 @@ def test_elevation_color(self):

def test_draw_simple_elevation(self):
w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
target = PixelCollector(w.width, w.height)
target = PNGWriter.rgba_from_dimensions(w.width, w.height)
draw_simple_elevation(w, w.sea_level(), target)
self._assert_img_equal("simple_elevation_28070", target)

def test_draw_elevation_shadow(self):
w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
data = w.elevation['data']
target = PixelCollector(w.width, w.height)
target = PNGWriter.rgba_from_dimensions(w.width, w.height)
draw_elevation(w, True, target)
self._assert_img_equal("elevation_28070_shadow", target)

def test_draw_elevation_no_shadow(self):
w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
data = w.elevation['data']
target = PixelCollector(w.width, w.height)
target = PNGWriter.rgba_from_dimensions(w.width, w.height)
draw_elevation(w, False, target)
self._assert_img_equal("elevation_28070_no_shadow", target)

def test_draw_river_map(self):
w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
target = PixelCollector(w.width, w.height)
target = PNGWriter.rgba_from_dimensions(w.width, w.height)
draw_riversmap(w, target)
self._assert_img_equal("riversmap_28070", target)

def test_draw_grayscale_heightmap(self):
w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
target = PixelCollector(w.width, w.height)
draw_grayscale_heightmap(w, target)
target = PNGWriter.grayscale_from_array(w.elevation['data'], scale_to_range=True)
#draw_grayscale_heightmap(w, target)
self._assert_img_equal("grayscale_heightmap_28070", target)

def test_draw_ocean(self):
w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
target = PixelCollector(w.width, w.height)
target = PNGWriter.rgba_from_dimensions(w.width, w.height)
draw_ocean(w.ocean, target)
self._assert_img_equal("ocean_28070", target)

def test_draw_precipitation(self):
w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
target = PixelCollector(w.width, w.height)
target = PNGWriter.rgba_from_dimensions(w.width, w.height)
draw_precipitation(w, target)
self._assert_img_equal("precipitation_28070", target)

def test_draw_world(self):
w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
target = PixelCollector(w.width, w.height)
target = PNGWriter.rgba_from_dimensions(w.width, w.height)
draw_world(w, target)
self._assert_img_equal("world_28070", target)

def test_draw_temperature_levels(self):
w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
target = PixelCollector(w.width, w.height)
target = PNGWriter.rgba_from_dimensions(w.width, w.height)
draw_temperature_levels(w, target)
self._assert_img_equal("temperature_28070", target)

def test_draw_biome(self):
w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
target = PixelCollector(w.width, w.height)
target = PNGWriter.rgba_from_dimensions(w.width, w.height)
draw_biome(w, target)
self._assert_img_equal("biome_28070", target)

def test_draw_scatter_plot(self):
w = World.open_protobuf("%s/seed_28070.world" % self.tests_data_dir)
target = PixelCollector(16, 16)
target = PNGWriter.rgba_from_dimensions(16, 16)
draw_scatter_plot(w, 16, target)

if __name__ == '__main__':
Expand Down
9 changes: 5 additions & 4 deletions tests/drawing_functions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

from worldengine.drawing_functions import draw_ancientmap, gradient, draw_rivers_on_image
from worldengine.world import World
from tests.draw_test import TestBase, PixelCollector
from worldengine.image_io import PNGWriter
from tests.draw_test import TestBase


class TestDrawingFunctions(TestBase):
Expand All @@ -14,12 +15,12 @@ def setUp(self):

def test_draw_ancient_map_factor1(self):
w_large = World.from_pickle_file("%s/py%s_seed_48956.world" % (self.tests_data_dir, platform.python_version_tuple()[0]))
target = PixelCollector(w_large.width, w_large.height)
target = PNGWriter.rgba_from_dimensions(w_large.width, w_large.height)
draw_ancientmap(w_large, target, resize_factor=1)
self._assert_img_equal("ancientmap_48956", target)

def test_draw_ancient_map_factor3(self):
target = PixelCollector(self.w.width * 3, self.w.height * 3)
target = PNGWriter.rgba_from_dimensions(self.w.width * 3, self.w.height * 3)
draw_ancientmap(self.w, target, resize_factor=3)
self._assert_img_equal("ancientmap_28070_factor3", target)

Expand All @@ -32,7 +33,7 @@ def test_gradient(self):
gradient(0.5, 0.0, 1.0, (10, 20, 40), (0, 128, 240)))

def test_draw_rivers_on_image(self):
target = PixelCollector(self.w.width * 2, self.w.height * 2)
target = PNGWriter.rgba_from_dimensions(self.w.width * 2, self.w.height * 2)
draw_rivers_on_image(self.w, target, factor=2)
self._assert_img_equal("rivers_28070_factor2", target)

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ skipsdist = True

[base]
deps =
Pillow
PyPlatec
noise
nose
protobuf
six
pypng

[testenv]
deps =
Expand Down
Loading