def test_is_valid_yuv(self): """ Test is_valid() property for YUV format """ # Create fake Y and UV bitmaps bitmap = bytearray(IMAGE_WIDTH * IMAGE_HEIGHT) uv_bitmap = bytearray(int(IMAGE_WIDTH * IMAGE_HEIGHT / 2)) # Create image instance image = BitmapHandler(bitmap, BitmapHandler.YUV, IMAGE_WIDTH, IMAGE_HEIGHT, u_bitmap=uv_bitmap, v_bitmap=uv_bitmap, stride=IMAGE_WIDTH, uv_stride=int(IMAGE_WIDTH / 2)) self.assertTrue(image.is_valid) self.assertEqual(image.bitmap, bitmap) self.assertEqual(image.format, BitmapHandler.YUV) self.assertEqual(image.width, IMAGE_WIDTH) self.assertEqual(image.height, IMAGE_HEIGHT) self.assertEqual(image.u_bitmap, uv_bitmap) self.assertEqual(image.v_bitmap, uv_bitmap) self.assertEqual(image.stride, IMAGE_WIDTH) self.assertEqual(image.uv_stride, int(IMAGE_WIDTH / 2))
def test_output_RGB(self): """ Export EncodeRGB() method result to file """ image = BitmapHandler(PNG_BITMAP_DATA, BitmapHandler.RGB, IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_WIDTH * 3) result = encode.EncodeRGB(image) file(ENCODE_FILENAME.format("RGB"), "wb").write(result.data)
def test_encode_RGB(self): """ Test the EncodeRGB() method """ image = BitmapHandler(PNG_BITMAP_DATA, BitmapHandler.RGB, IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_WIDTH * 3) result = encode.EncodeRGB(image) self.assertTrue(isinstance(result, WebPHandler)) self.assertEqual(result.width, IMAGE_WIDTH) self.assertEqual(result.height, IMAGE_HEIGHT)
def test_is_valid(self): """ Test is_valid() property """ # Invalid self.assertFalse( BitmapHandler(None, None, IMAGE_WIDTH, IMAGE_HEIGHT, -1).is_valid) self.assertFalse(BitmapHandler(None, None, -1, -1, -1).is_valid) self.assertFalse( BitmapHandler(None, BitmapHandler.YUV, 0, 0, None).is_valid) # Valid image = BitmapHandler(WEBP_IMAGE_DATA, BitmapHandler.RGB, IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_WIDTH * 3) self.assertTrue(image.is_valid) self.assertEqual(image.bitmap, WEBP_IMAGE_DATA) self.assertEqual(image.format, BitmapHandler.RGB) self.assertEqual(image.width, IMAGE_WIDTH) self.assertEqual(image.height, IMAGE_HEIGHT)
def test_output_BGR(self): """ Export EncodeBGR() method result to file """ # Convert to RGBA size = IMAGE_WIDTH * IMAGE_HEIGHT bitmap = bytearray(size * 3) for i in range(size): bitmap[i * 3] = PNG_BITMAP_DATA[i * 3 + 2] bitmap[i * 3 + 1] = PNG_BITMAP_DATA[i * 3 + 1] bitmap[i * 3 + 2] = PNG_BITMAP_DATA[i * 3] image = BitmapHandler(bitmap, BitmapHandler.BGR, IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_WIDTH * 3) # Save image result = encode.EncodeBGR(image) file(ENCODE_FILENAME.format("BGR"), "wb").write(result.data)
def test_encode_BGRA(self): """ Test the EncodeBGRA() method """ # Convert to RGBA size = IMAGE_WIDTH * IMAGE_HEIGHT bitmap = bytearray(size * 4) for i in range(size): bitmap[i * 4] = PNG_BITMAP_DATA[i * 3 + 2] bitmap[i * 4 + 1] = PNG_BITMAP_DATA[i * 3 + 1] bitmap[i * 4 + 2] = PNG_BITMAP_DATA[i * 3] image = BitmapHandler(bitmap, BitmapHandler.BGRA, IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_WIDTH * 4) # Encode image result = encode.EncodeBGRA(image) self.assertTrue(isinstance(result, WebPHandler)) self.assertEqual(result.width, IMAGE_WIDTH) self.assertEqual(result.height, IMAGE_HEIGHT)