Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Commit

Permalink
Project name changed to "unflattener". Improved Pylint rating.
Browse files Browse the repository at this point in the history
Looking back, the pun based on how it presented some pixel art in
an *unflattering* light wasn't _that_ clever.
  • Loading branch information
dbohdan committed Nov 30, 2013
1 parent 98c1c1f commit 9abab28
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 40 deletions.
6 changes: 3 additions & 3 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name of unflatterer nor the names of its contributors may be
3. Neither the name of unflattener nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.

Expand All @@ -24,4 +24,4 @@ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11 changes: 10 additions & 1 deletion normalmapgen.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# unflatterer, a normal map generator for 2D art.
# unflattener, a normal map generator for 2D art.
# Copyright (C) 2013 Danyil Bohdan
# All rights Reserved.
# See the file LICENSE for licensing information.
"""
Implements the NormalMap class.
"""

from __future__ import print_function

__author__ = 'Danyil Bohdan'
__copyright__ = 'Copyright (C) 2013 Danyil Bohdan'
__license__ = 'BSD'

import Image
import numpy
Expand Down
71 changes: 45 additions & 26 deletions tests.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,59 @@
#!/usr/bin/env python
# unflatterer, a normal map generator for 2D art.
# unflattener, a normal map generator for 2D art.
# Copyright (C) 2013 Danyil Bohdan
# All rights Reserved.
# See the file LICENSE for licensing information.
"""
Implements simple sanity checks for NormalMap and functions from normalmapgen.
"""

from __future__ import print_function

import Image
import os
from normalmapgen import NormalMap
from normalmapgen import image_to_array, array_to_image, arrays_equivalent

im_files = {'left': "zombie-left.png",
'right': "zombie-right.png",
'top': "zombie-top.png",
'bottom': "zombie-bottom.png"}
TESTFILENAMES = {'left': "zombie-left.png",
'right': "zombie-right.png",
'top': "zombie-top.png",
'bottom': "zombie-bottom.png"}

test_dir = "test-images"
TESTDIR = "test-images"

# Append directory prefix to test art file names.
im_files = {k: os.path.join(test_dir, im_files[k]) for k in im_files}

for i in range(8):
depth = 1.0 / 2**i
print("Running normal map generation test with depth %0.4f (1 / 2^%i)" %
(depth, i))
sn1 = NormalMap()
sn1.create_from_files(im_files)
sn1.save_image('result1.png', depth)
sn2 = NormalMap()
sn2.load_image('result1.png', depth)
sn2.save_image('result2.png', depth)
assert(sn1.compare(sn2, depth))
assert(sn2.compare(sn1, depth))

print("Testing image to array to image conversion.")
im = Image.open(im_files['left'])
arr_there = image_to_array(im)
arr_back_again = image_to_array(array_to_image(arr_there))
assert(arrays_equivalent(arr_there, arr_back_again))
TESTIMAGEFILES = {k: os.path.join(TESTDIR, TESTFILENAMES[k]) \
for k in TESTFILENAMES}

def map_gen_test():
"""Test generating normal maps of varying depths from image files"""

for i in range(8):
depth = 1.0 / 2**i
print("Running normal map generation test with depth %0.4f (1 / 2^%i)" %
(depth, i))
nm1 = NormalMap()
nm1.create_from_files(TESTIMAGEFILES)
nm1.save_image('result1.png', depth)
# nm1.save_image("result-%u.png" % i, depth)
nm2 = NormalMap()
nm2.load_image('result1.png', depth)
nm2.save_image('result2.png', depth)
assert(nm1.compare(nm2, depth))
assert(nm2.compare(nm1, depth))

def image_comp_test():
"""Test `image_to_array` and `array_to_image` functions."""

print("Testing image to array to image conversion.")
im = Image.open(TESTIMAGEFILES['left'])
arr_there = image_to_array(im)
arr_back_again = image_to_array(array_to_image(arr_there))
assert(arrays_equivalent(arr_there, arr_back_again))

def main():
map_gen_test()
image_comp_test()

if __name__ == "__main__":
main()
26 changes: 16 additions & 10 deletions unflatten.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
#!/usr/bin/env python
# unflatterer, a normal map generator for 2D art.
# unflattener, a normal map generator for 2D art.
# Copyright (C) 2013 Danyil Bohdan
# All rights Reserved.
# See the file LICENSE for licensing information.
"""
Provides a command line interface for the NormalMap class.
"""

from __future__ import print_function

import time
import argparse
import numpy
import normalmapgen

descr = """
DESCRIPTION = """
Generate a normal map for 2D art
"""

epi = """
EPILOG = """
One input file minimum, at least two (one for each axis) highly recommended.
Input files should be 8-bit grayscale PNGs.
"""

def main():
t = time.time()
run_time = time.time()

input_file_types = ['top', 'bottom', 'left', 'right']

# Parse command line arguments.
parser = argparse.ArgumentParser(description=descr, epilog=epi)
parser = argparse.ArgumentParser(description=DESCRIPTION, epilog=EPILOG)
for argname in input_file_types:
parser.add_argument('--' + argname, '-' + argname[0],
default=None, help="%s image file" % argname)
Expand All @@ -42,14 +47,15 @@ def main():
exit(1)

# Process input files and create a normal map.
sn = normalmapgen.NormalMap()
normal_map = normalmapgen.NormalMap()

sn.create_from_files(input_file_names)
sn.save_image(argsdict['output'], depth=numpy.float64(argsdict['depth']))
normal_map.create_from_files(input_file_names)
normal_map.save_image(argsdict['output'],
depth=numpy.float64(argsdict['depth']))

# Measure the time all of the above took.
t = time.time() - t
print("Execution time: %0.3f s" % t)
run_time = time.time() - run_time
print("Execution time: %0.3f s" % run_time)

if __name__ == '__main__':
main()

0 comments on commit 9abab28

Please sign in to comment.