def test_averaging(self): a = np.array([[1, 2, 2, 1], [3, 2, 8, 1], [0, 0, 2, 2], [0, 4, 2, 2]]) config = Configuration(width=123, height=854, block_size=2, dct_size=2, transform='DCT', quantization=None) sub_sampling = SubSampling(config) res = sub_sampling.execute(a) self.assertEqual(res.shape, (2, 2)) self.assertEqual(res[0][0], 2) self.assertEqual(res[0][1], 3) self.assertEqual(res[1][0], 1) self.assertEqual(res[1][1], 2) config = Configuration(width=123, height=854, block_size=4, dct_size=2, transform='DCT', quantization=None) sub_sampling = SubSampling(config) res = sub_sampling.execute(a) self.assertEqual(res.shape, (1, 1)) self.assertEqual(res[0][0], 2)
def test_without_subsampling(self): original = np.arange(6).reshape(2, 3) config = Configuration(width=3, height=2, block_size=1) restored = decompress_band(compress_band(original, config), config) self.assertTrue(np.allclose(original, restored, rtol=0.000001))
def test_compress_and_decompress_on_array(self): original = np.arange(128).reshape(8, 16) config = Configuration(width=16, height=8, block_size=3) restored = decompress_band(compress_band(original, config), config) self.assertTrue(np.allclose(original, restored, rtol=1))
def test_decode_encoded_blocks(self): a = self.array config = Configuration(width=3, height=9, block_size=1, dct_size=3) rle = RunLengthEncoding(config=config) res = rle.invert(rle.execute(a)) self.assertEqual(res.tolist(), a.tolist())
def test_fourier_transform_option(self): original = np.arange(128).reshape(8, 16) config = Configuration(width=16, height=8, block_size=3, transform='DFT') restored = decompress_band(compress_band(original, config), config) self.assertTrue(np.allclose(original, restored, rtol=1))
def test_restore_zigzag(self): a = np.arange(32).reshape(4, 8) config = Configuration(width=8, height=4, block_size=1, dct_size=2) zigzag_step = ZigzagOrder(config) res = zigzag_step.invert(zigzag_step.execute(a)) self.assertEqual(res.shape, a.shape) self.assertEqual(res.tolist(), a.tolist())
def test_zigzag_order_step(self): a = np.arange(16).reshape(4, 4) config = Configuration(width=4, height=4, block_size=1, dct_size=2) zigzag_step = ZigzagOrder(config) res = zigzag_step.execute(a) expected = np.array([[[0, 1, 4, 5], [2, 3, 6, 7]], [[8, 9, 12, 13], [10, 11, 14, 15]]]) self.assertTupleEqual(res.shape, (2, 2, 4)) self.assertEqual(res.tolist(), expected.tolist())
def test_preserves_allowed_range(self): original = np.array([[220, 255, 123, 205], [255, 255, 112, 10], [15, 51, 83, 221], [239, 73, 62, 22]]) self.assertTrue(np.all(original < 256)) self.assertTrue(np.all(original > -1)) config = Configuration(width=4, height=4, block_size=1, dct_size=2, quantization=QuantizationMethod('divide', divisor=129)) restored = decompress_band(compress_band(original, config), config) self.assertTrue(np.all(restored < 256)) self.assertTrue(np.all(restored > -1))
def test_create_and_read_header(self): q = QuantizationMethod('qtable') config = Configuration(width=320, height=400, block_size=4, dct_size=8, transform='DFT', quantization=q) res = file_format.get_header(file_format.create_header(config)) self.assertEqual(config.width, res.width) self.assertEqual(config.height, res.height) self.assertEqual(config.block_size, res.block_size) self.assertEqual(config.dct_size, res.dct_size) self.assertEqual(config.transform, res.transform) self.assertEqual(res.quantization.name, 'qtable')
def test_create_with_different_quantization_method(self): q = QuantizationMethod('divide', divisor=93) config = Configuration(width=320, height=400, block_size=44, dct_size=16, transform='DCT', quantization=q) res = file_format.get_header(file_format.create_header(config)) self.assertEqual(res.width, 320) self.assertEqual(res.height, 400) self.assertEqual(res.block_size, 44) self.assertEqual(res.dct_size, 16) self.assertEqual(res.transform, 'DCT') self.assertEqual(res.quantization.name, 'divide') self.assertEqual(res.quantization.params, {'divisor': 93})
def test_generate_and_read_data(self): q = QuantizationMethod('divide', divisor=93) config = Configuration(width=320, height=400, block_size=44, dct_size=16, transform='DCT', quantization=q) data = CompressedData(y=bytes([4, 8, 15, 16, 23, 42]), cb=bytes([1, 2, 3, 4, 5]), cr=bytes([10])) res = file_format.generate_data(config, data) read_config, read_data = file_format.read_data(res) self.assertEqual(read_config.dct_size, 16) self.assertEqual(read_data.y, bytes([4, 8, 15, 16, 23, 42])) self.assertEqual(read_data.cb, bytes([1, 2, 3, 4, 5])) self.assertEqual(read_data.cr, bytes([10]))
def compress(input_fname, output_fname, block_size=4, dct_size=8, transform='DCT', quantization=None): im = Image.open(input_fname).convert('YCbCr') config = Configuration(width=im.width, height=im.height, block_size=block_size, dct_size=dct_size, transform=transform, quantization=quantization) jpeg = Jpeg(config) compressed_bytes = jpeg.compress(im) with open(output_fname, 'wb') as f: f.write(compressed_bytes)
def test_with_1pixel_blocks(self): original = np.arange(64).reshape(8, 8) config = Configuration(width=8, height=8, block_size=1, dct_size=1) restored = decompress_band(compress_band(original, config), config) self.assertTrue(np.allclose(original, restored, rtol=0.000001))