2016-05-29 19:02:39 +00:00
|
|
|
# Copyright 2016 Hardcoded Software (http://www.hardcoded.net)
|
|
|
|
#
|
|
|
|
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
|
|
|
|
# which should be included with this package. The terms are also available at
|
2015-01-03 21:33:16 +00:00
|
|
|
# http://www.gnu.org/licenses/gpl-3.0.html
|
2009-06-01 09:55:11 +00:00
|
|
|
# The commented out tests are tests for function that have been converted to pure C for speed
|
2009-06-07 14:26:46 +00:00
|
|
|
|
2011-01-01 16:17:27 +00:00
|
|
|
from pytest import raises, skip
|
2011-01-11 10:59:53 +00:00
|
|
|
from hscommon.testutil import eq_
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2011-01-01 16:17:27 +00:00
|
|
|
try:
|
2016-06-01 02:32:37 +00:00
|
|
|
from ..pe.block import avgdiff, getblocks2, NoBlocksError, DifferentBlockCountError
|
2011-01-01 16:17:27 +00:00
|
|
|
except ImportError:
|
|
|
|
skip("Can't import the block module, probably hasn't been compiled.")
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
|
|
|
def my_avgdiff(
|
|
|
|
first, second, limit=768, min_iter=3
|
|
|
|
): # this is so I don't have to re-write every call
|
2009-06-01 09:55:11 +00:00
|
|
|
return avgdiff(first, second, limit, min_iter)
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2016-05-29 19:02:39 +00:00
|
|
|
BLACK = (0, 0, 0)
|
2020-01-01 02:16:27 +00:00
|
|
|
RED = (0xFF, 0, 0)
|
|
|
|
GREEN = (0, 0xFF, 0)
|
|
|
|
BLUE = (0, 0, 0xFF)
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2011-01-01 16:17:27 +00:00
|
|
|
class FakeImage:
|
2009-06-01 09:55:11 +00:00
|
|
|
def __init__(self, size, data):
|
|
|
|
self.size = size
|
|
|
|
self.data = data
|
2016-05-29 19:02:39 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def getdata(self):
|
|
|
|
return self.data
|
2016-05-29 19:02:39 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def crop(self, box):
|
|
|
|
pixels = []
|
|
|
|
for i in range(box[1], box[3]):
|
|
|
|
for j in range(box[0], box[2]):
|
|
|
|
pixel = self.data[i * self.size[0] + j]
|
|
|
|
pixels.append(pixel)
|
|
|
|
return FakeImage((box[2] - box[0], box[3] - box[1]), pixels)
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def empty():
|
2016-05-29 19:02:39 +00:00
|
|
|
return FakeImage((0, 0), [])
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
|
|
|
def single_pixel(): # one red pixel
|
|
|
|
return FakeImage((1, 1), [(0xFF, 0, 0)])
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def four_pixels():
|
2020-01-01 02:16:27 +00:00
|
|
|
pixels = [RED, (0, 0x80, 0xFF), (0x80, 0, 0), (0, 0x40, 0x80)]
|
2009-06-01 09:55:11 +00:00
|
|
|
return FakeImage((2, 2), pixels)
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2011-01-01 16:17:27 +00:00
|
|
|
class TestCasegetblock:
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_single_pixel(self):
|
|
|
|
im = single_pixel()
|
|
|
|
[b] = getblocks2(im, 1)
|
2016-05-29 19:02:39 +00:00
|
|
|
eq_(RED, b)
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_no_pixel(self):
|
|
|
|
im = empty()
|
2011-01-01 16:17:27 +00:00
|
|
|
eq_([], getblocks2(im, 1))
|
2016-05-29 19:02:39 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_four_pixels(self):
|
|
|
|
im = four_pixels()
|
|
|
|
[b] = getblocks2(im, 1)
|
2020-01-01 02:16:27 +00:00
|
|
|
meanred = (0xFF + 0x80) // 4
|
2009-06-01 09:55:11 +00:00
|
|
|
meangreen = (0x80 + 0x40) // 4
|
2020-01-01 02:16:27 +00:00
|
|
|
meanblue = (0xFF + 0x80) // 4
|
2016-05-29 19:02:39 +00:00
|
|
|
eq_((meanred, meangreen, meanblue), b)
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
# class TCdiff(unittest.TestCase):
|
|
|
|
# def test_diff(self):
|
|
|
|
# b1 = (10, 20, 30)
|
|
|
|
# b2 = (1, 2, 3)
|
2016-05-29 19:02:39 +00:00
|
|
|
# eq_(9 + 18 + 27, diff(b1, b2))
|
|
|
|
#
|
2009-06-01 09:55:11 +00:00
|
|
|
# def test_diff_negative(self):
|
|
|
|
# b1 = (10, 20, 30)
|
|
|
|
# b2 = (1, 2, 3)
|
2016-05-29 19:02:39 +00:00
|
|
|
# eq_(9 + 18 + 27, diff(b2, b1))
|
|
|
|
#
|
2009-06-01 09:55:11 +00:00
|
|
|
# def test_diff_mixed_positive_and_negative(self):
|
|
|
|
# b1 = (1, 5, 10)
|
|
|
|
# b2 = (10, 1, 15)
|
2016-05-29 19:02:39 +00:00
|
|
|
# eq_(9 + 4 + 5, diff(b1, b2))
|
|
|
|
#
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
# class TCgetblocks(unittest.TestCase):
|
|
|
|
# def test_empty_image(self):
|
|
|
|
# im = empty()
|
2016-05-29 19:02:39 +00:00
|
|
|
# blocks = getblocks(im, 1)
|
|
|
|
# eq_(0, len(blocks))
|
|
|
|
#
|
2009-06-01 09:55:11 +00:00
|
|
|
# def test_one_block_image(self):
|
|
|
|
# im = four_pixels()
|
|
|
|
# blocks = getblocks2(im, 1)
|
2016-05-29 19:02:39 +00:00
|
|
|
# eq_(1, len(blocks))
|
2009-06-01 09:55:11 +00:00
|
|
|
# block = blocks[0]
|
|
|
|
# meanred = (0xff + 0x80) // 4
|
|
|
|
# meangreen = (0x80 + 0x40) // 4
|
|
|
|
# meanblue = (0xff + 0x80) // 4
|
2016-05-29 19:02:39 +00:00
|
|
|
# eq_((meanred, meangreen, meanblue), block)
|
|
|
|
#
|
2009-06-01 09:55:11 +00:00
|
|
|
# def test_not_enough_height_to_fit_a_block(self):
|
2016-05-29 19:02:39 +00:00
|
|
|
# im = FakeImage((2, 1), [BLACK, BLACK])
|
|
|
|
# blocks = getblocks(im, 2)
|
|
|
|
# eq_(0, len(blocks))
|
|
|
|
#
|
2009-06-01 09:55:11 +00:00
|
|
|
# def xtest_dont_include_leftovers(self):
|
|
|
|
# # this test is disabled because getblocks is not used and getblock in cdeffed
|
|
|
|
# pixels = [
|
2016-05-29 19:02:39 +00:00
|
|
|
# RED,(0, 0x80, 0xff), BLACK,
|
|
|
|
# (0x80, 0, 0),(0, 0x40, 0x80), BLACK,
|
|
|
|
# BLACK, BLACK, BLACK
|
2009-06-01 09:55:11 +00:00
|
|
|
# ]
|
2016-05-29 19:02:39 +00:00
|
|
|
# im = FakeImage((3, 3), pixels)
|
|
|
|
# blocks = getblocks(im, 2)
|
2009-06-01 09:55:11 +00:00
|
|
|
# block = blocks[0]
|
|
|
|
# #Because the block is smaller than the image, only blocksize must be considered.
|
|
|
|
# meanred = (0xff + 0x80) // 4
|
|
|
|
# meangreen = (0x80 + 0x40) // 4
|
|
|
|
# meanblue = (0xff + 0x80) // 4
|
2016-05-29 19:02:39 +00:00
|
|
|
# eq_((meanred, meangreen, meanblue), block)
|
|
|
|
#
|
2009-06-01 09:55:11 +00:00
|
|
|
# def xtest_two_blocks(self):
|
|
|
|
# # this test is disabled because getblocks is not used and getblock in cdeffed
|
|
|
|
# pixels = [BLACK for i in xrange(4 * 2)]
|
|
|
|
# pixels[0] = RED
|
2016-05-29 19:02:39 +00:00
|
|
|
# pixels[1] = (0, 0x80, 0xff)
|
|
|
|
# pixels[4] = (0x80, 0, 0)
|
|
|
|
# pixels[5] = (0, 0x40, 0x80)
|
2009-06-01 09:55:11 +00:00
|
|
|
# im = FakeImage((4, 2), pixels)
|
2016-05-29 19:02:39 +00:00
|
|
|
# blocks = getblocks(im, 2)
|
|
|
|
# eq_(2, len(blocks))
|
2009-06-01 09:55:11 +00:00
|
|
|
# block = blocks[0]
|
|
|
|
# #Because the block is smaller than the image, only blocksize must be considered.
|
|
|
|
# meanred = (0xff + 0x80) // 4
|
|
|
|
# meangreen = (0x80 + 0x40) // 4
|
|
|
|
# meanblue = (0xff + 0x80) // 4
|
2016-05-29 19:02:39 +00:00
|
|
|
# eq_((meanred, meangreen, meanblue), block)
|
|
|
|
# eq_(BLACK, blocks[1])
|
|
|
|
#
|
2009-06-01 09:55:11 +00:00
|
|
|
# def test_four_blocks(self):
|
|
|
|
# pixels = [BLACK for i in xrange(4 * 4)]
|
|
|
|
# pixels[0] = RED
|
2016-05-29 19:02:39 +00:00
|
|
|
# pixels[1] = (0, 0x80, 0xff)
|
|
|
|
# pixels[4] = (0x80, 0, 0)
|
|
|
|
# pixels[5] = (0, 0x40, 0x80)
|
2009-06-01 09:55:11 +00:00
|
|
|
# im = FakeImage((4, 4), pixels)
|
|
|
|
# blocks = getblocks2(im, 2)
|
2016-05-29 19:02:39 +00:00
|
|
|
# eq_(4, len(blocks))
|
2009-06-01 09:55:11 +00:00
|
|
|
# block = blocks[0]
|
|
|
|
# #Because the block is smaller than the image, only blocksize must be considered.
|
|
|
|
# meanred = (0xff + 0x80) // 4
|
|
|
|
# meangreen = (0x80 + 0x40) // 4
|
|
|
|
# meanblue = (0xff + 0x80) // 4
|
2016-05-29 19:02:39 +00:00
|
|
|
# eq_((meanred, meangreen, meanblue), block)
|
|
|
|
# eq_(BLACK, blocks[1])
|
|
|
|
# eq_(BLACK, blocks[2])
|
|
|
|
# eq_(BLACK, blocks[3])
|
|
|
|
#
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2011-01-01 16:17:27 +00:00
|
|
|
class TestCasegetblocks2:
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_empty_image(self):
|
|
|
|
im = empty()
|
2016-05-29 19:02:39 +00:00
|
|
|
blocks = getblocks2(im, 1)
|
|
|
|
eq_(0, len(blocks))
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_one_block_image(self):
|
|
|
|
im = four_pixels()
|
2016-05-29 19:02:39 +00:00
|
|
|
blocks = getblocks2(im, 1)
|
|
|
|
eq_(1, len(blocks))
|
2009-06-01 09:55:11 +00:00
|
|
|
block = blocks[0]
|
2020-01-01 02:16:27 +00:00
|
|
|
meanred = (0xFF + 0x80) // 4
|
2009-06-01 09:55:11 +00:00
|
|
|
meangreen = (0x80 + 0x40) // 4
|
2020-01-01 02:16:27 +00:00
|
|
|
meanblue = (0xFF + 0x80) // 4
|
2016-05-29 19:02:39 +00:00
|
|
|
eq_((meanred, meangreen, meanblue), block)
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_four_blocks_all_black(self):
|
|
|
|
im = FakeImage((2, 2), [BLACK, BLACK, BLACK, BLACK])
|
2016-05-29 19:02:39 +00:00
|
|
|
blocks = getblocks2(im, 2)
|
|
|
|
eq_(4, len(blocks))
|
2009-06-01 09:55:11 +00:00
|
|
|
for block in blocks:
|
2016-05-29 19:02:39 +00:00
|
|
|
eq_(BLACK, block)
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_two_pixels_image_horizontal(self):
|
2016-05-29 19:02:39 +00:00
|
|
|
pixels = [RED, BLUE]
|
2009-06-01 09:55:11 +00:00
|
|
|
im = FakeImage((2, 1), pixels)
|
2016-05-29 19:02:39 +00:00
|
|
|
blocks = getblocks2(im, 2)
|
|
|
|
eq_(4, len(blocks))
|
|
|
|
eq_(RED, blocks[0])
|
|
|
|
eq_(BLUE, blocks[1])
|
|
|
|
eq_(RED, blocks[2])
|
|
|
|
eq_(BLUE, blocks[3])
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_two_pixels_image_vertical(self):
|
2016-05-29 19:02:39 +00:00
|
|
|
pixels = [RED, BLUE]
|
2009-06-01 09:55:11 +00:00
|
|
|
im = FakeImage((1, 2), pixels)
|
2016-05-29 19:02:39 +00:00
|
|
|
blocks = getblocks2(im, 2)
|
|
|
|
eq_(4, len(blocks))
|
|
|
|
eq_(RED, blocks[0])
|
|
|
|
eq_(RED, blocks[1])
|
|
|
|
eq_(BLUE, blocks[2])
|
|
|
|
eq_(BLUE, blocks[3])
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2011-01-01 16:17:27 +00:00
|
|
|
class TestCaseavgdiff:
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_empty(self):
|
2011-01-01 16:17:27 +00:00
|
|
|
with raises(NoBlocksError):
|
|
|
|
my_avgdiff([], [])
|
2016-05-29 19:02:39 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_two_blocks(self):
|
2016-05-29 19:02:39 +00:00
|
|
|
b1 = (5, 10, 15)
|
|
|
|
b2 = (255, 250, 245)
|
|
|
|
b3 = (0, 0, 0)
|
|
|
|
b4 = (255, 0, 255)
|
|
|
|
blocks1 = [b1, b2]
|
|
|
|
blocks2 = [b3, b4]
|
2009-06-01 09:55:11 +00:00
|
|
|
expected1 = 5 + 10 + 15
|
|
|
|
expected2 = 0 + 250 + 10
|
|
|
|
expected = (expected1 + expected2) // 2
|
2011-01-01 16:17:27 +00:00
|
|
|
eq_(expected, my_avgdiff(blocks1, blocks2))
|
2016-05-29 19:02:39 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_blocks_not_the_same_size(self):
|
2016-05-29 19:02:39 +00:00
|
|
|
b = (0, 0, 0)
|
2011-01-01 16:17:27 +00:00
|
|
|
with raises(DifferentBlockCountError):
|
2016-05-29 19:02:39 +00:00
|
|
|
my_avgdiff([b, b], [b])
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_first_arg_is_empty_but_not_second(self):
|
2020-01-01 02:16:27 +00:00
|
|
|
# Don't return 0 (as when the 2 lists are empty), raise!
|
2016-05-29 19:02:39 +00:00
|
|
|
b = (0, 0, 0)
|
2011-01-01 16:17:27 +00:00
|
|
|
with raises(DifferentBlockCountError):
|
2016-05-29 19:02:39 +00:00
|
|
|
my_avgdiff([], [b])
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_limit(self):
|
2016-05-29 19:02:39 +00:00
|
|
|
ref = (0, 0, 0)
|
2020-01-01 02:16:27 +00:00
|
|
|
b1 = (10, 10, 10) # avg 30
|
|
|
|
b2 = (20, 20, 20) # avg 45
|
|
|
|
b3 = (30, 30, 30) # avg 60
|
2016-05-29 19:02:39 +00:00
|
|
|
blocks1 = [ref, ref, ref]
|
|
|
|
blocks2 = [b1, b2, b3]
|
|
|
|
eq_(45, my_avgdiff(blocks1, blocks2, 44))
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_min_iterations(self):
|
2016-05-29 19:02:39 +00:00
|
|
|
ref = (0, 0, 0)
|
2020-01-01 02:16:27 +00:00
|
|
|
b1 = (10, 10, 10) # avg 30
|
|
|
|
b2 = (20, 20, 20) # avg 45
|
|
|
|
b3 = (10, 10, 10) # avg 40
|
2016-05-29 19:02:39 +00:00
|
|
|
blocks1 = [ref, ref, ref]
|
|
|
|
blocks2 = [b1, b2, b3]
|
|
|
|
eq_(40, my_avgdiff(blocks1, blocks2, 45 - 1, 3))
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
# Bah, I don't know why this test fails, but I don't think it matters very much
|
|
|
|
# def test_just_over_the_limit(self):
|
|
|
|
# #A score just over the limit might return exactly the limit due to truncating. We should
|
|
|
|
# #ceil() the result in this case.
|
2016-05-29 19:02:39 +00:00
|
|
|
# ref = (0, 0, 0)
|
|
|
|
# b1 = (10, 0, 0)
|
|
|
|
# b2 = (11, 0, 0)
|
|
|
|
# blocks1 = [ref, ref]
|
|
|
|
# blocks2 = [b1, b2]
|
|
|
|
# eq_(11, my_avgdiff(blocks1, blocks2, 10))
|
|
|
|
#
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_return_at_least_1_at_the_slightest_difference(self):
|
2016-05-29 19:02:39 +00:00
|
|
|
ref = (0, 0, 0)
|
|
|
|
b1 = (1, 0, 0)
|
2010-08-11 14:39:06 +00:00
|
|
|
blocks1 = [ref for i in range(250)]
|
|
|
|
blocks2 = [ref for i in range(250)]
|
2009-06-01 09:55:11 +00:00
|
|
|
blocks2[0] = b1
|
2016-05-29 19:02:39 +00:00
|
|
|
eq_(1, my_avgdiff(blocks1, blocks2))
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_return_0_if_there_is_no_difference(self):
|
2016-05-29 19:02:39 +00:00
|
|
|
ref = (0, 0, 0)
|
|
|
|
blocks1 = [ref, ref]
|
|
|
|
blocks2 = [ref, ref]
|
|
|
|
eq_(0, my_avgdiff(blocks1, blocks2))
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
# class TCmaxdiff(unittest.TestCase):
|
|
|
|
# def test_empty(self):
|
2016-05-29 19:02:39 +00:00
|
|
|
# self.assertRaises(NoBlocksError, maxdiff,[],[])
|
|
|
|
#
|
2009-06-01 09:55:11 +00:00
|
|
|
# def test_two_blocks(self):
|
2016-05-29 19:02:39 +00:00
|
|
|
# b1 = (5, 10, 15)
|
|
|
|
# b2 = (255, 250, 245)
|
|
|
|
# b3 = (0, 0, 0)
|
|
|
|
# b4 = (255, 0, 255)
|
|
|
|
# blocks1 = [b1, b2]
|
|
|
|
# blocks2 = [b3, b4]
|
2009-06-01 09:55:11 +00:00
|
|
|
# expected1 = 5 + 10 + 15
|
|
|
|
# expected2 = 0 + 250 + 10
|
2016-05-29 19:02:39 +00:00
|
|
|
# expected = max(expected1, expected2)
|
|
|
|
# eq_(expected, maxdiff(blocks1, blocks2))
|
|
|
|
#
|
2009-06-01 09:55:11 +00:00
|
|
|
# def test_blocks_not_the_same_size(self):
|
2016-05-29 19:02:39 +00:00
|
|
|
# b = (0, 0, 0)
|
|
|
|
# self.assertRaises(DifferentBlockCountError, maxdiff,[b, b],[b])
|
|
|
|
#
|
2009-06-01 09:55:11 +00:00
|
|
|
# def test_first_arg_is_empty_but_not_second(self):
|
|
|
|
# #Don't return 0 (as when the 2 lists are empty), raise!
|
2016-05-29 19:02:39 +00:00
|
|
|
# b = (0, 0, 0)
|
|
|
|
# self.assertRaises(DifferentBlockCountError, maxdiff,[],[b])
|
|
|
|
#
|
2009-06-01 09:55:11 +00:00
|
|
|
# def test_limit(self):
|
2016-05-29 19:02:39 +00:00
|
|
|
# b1 = (5, 10, 15)
|
|
|
|
# b2 = (255, 250, 245)
|
|
|
|
# b3 = (0, 0, 0)
|
|
|
|
# b4 = (255, 0, 255)
|
|
|
|
# blocks1 = [b1, b2]
|
|
|
|
# blocks2 = [b3, b4]
|
2009-06-01 09:55:11 +00:00
|
|
|
# expected1 = 5 + 10 + 15
|
|
|
|
# expected2 = 0 + 250 + 10
|
2016-05-29 19:02:39 +00:00
|
|
|
# eq_(expected1, maxdiff(blocks1, blocks2, expected1 - 1))
|
|
|
|
#
|