def test_floating_point(self): """ SCENARIO: The sample format is 32bit floating point. EXPECTED RESULT: RuntimeError """ data = fixtures.skimage.data.moon().astype(np.float32) h, w = data.shape th, tw = h // 2, w // 2 fp = libtiff.open(self.temp_tiff_filename, mode='w') libtiff.setField(fp, 'Photometric', libtiff.Photometric.MINISBLACK) libtiff.setField(fp, 'Compression', libtiff.Compression.DEFLATE) libtiff.setField(fp, 'SampleFormat', libtiff.SampleFormat.IEEEFP) libtiff.setField(fp, 'ImageLength', data.shape[0]) libtiff.setField(fp, 'ImageWidth', data.shape[1]) libtiff.setField(fp, 'TileLength', th) libtiff.setField(fp, 'TileWidth', tw) libtiff.setField(fp, 'BitsPerSample', 32) libtiff.setField(fp, 'SamplesPerPixel', 1) libtiff.writeEncodedTile(fp, 0, data[:th, :tw].copy()) libtiff.writeEncodedTile(fp, 1, data[:th, tw:w].copy()) libtiff.writeEncodedTile(fp, 2, data[th:h, :tw].copy()) libtiff.writeEncodedTile(fp, 3, data[th:h, tw:w].copy()) libtiff.close(fp) with Tiff2Jp2k(self.temp_tiff_filename, self.temp_jp2_filename) as j: with self.assertRaises(RuntimeError): j.run()
def setup_minisblack_3strip_partial_last_strip(cls, path): """ SCENARIO: create a simple monochromatic 3-strip image """ data = fixtures.skimage.data.moon() data = data[:480, :480] h, w = data.shape # instead of 160, this will cause a partially empty last strip rps = 170 fp = libtiff.open(path, mode='w') libtiff.setField(fp, 'Photometric', libtiff.Photometric.MINISBLACK) libtiff.setField(fp, 'Compression', libtiff.Compression.DEFLATE) libtiff.setField(fp, 'ImageLength', data.shape[0]) libtiff.setField(fp, 'ImageWidth', data.shape[1]) libtiff.setField(fp, 'RowsPerStrip', rps) libtiff.setField(fp, 'BitsPerSample', 8) libtiff.setField(fp, 'SamplesPerPixel', 1) libtiff.setField(fp, 'PlanarConfig', libtiff.PlanarConfig.CONTIG) libtiff.writeEncodedStrip(fp, 0, data[:rps, :].copy()) libtiff.writeEncodedStrip(fp, 1, data[rps:, :].copy()) data2 = np.vstack((data[340:480, :], np.zeros((30, 480), dtype=np.uint8))) libtiff.writeEncodedStrip(fp, 2, data2) libtiff.close(fp) cls.minisblack_3strip_partial_last_strip = path
def setup_minisblack_2x2_partial_tiles(cls, path): """ SCENARIO: create a simple monochromatic 2x2 tiled image with partial tiles. """ data = fixtures.skimage.data.moon() h, w = 480, 480 th, tw = 256, 256 fp = libtiff.open(path, mode='w') libtiff.setField(fp, 'Photometric', libtiff.Photometric.MINISBLACK) libtiff.setField(fp, 'Compression', libtiff.Compression.DEFLATE) libtiff.setField(fp, 'ImageLength', h) libtiff.setField(fp, 'ImageWidth', w) libtiff.setField(fp, 'TileLength', th) libtiff.setField(fp, 'TileWidth', tw) libtiff.setField(fp, 'BitsPerSample', 8) libtiff.setField(fp, 'SamplesPerPixel', 1) libtiff.writeEncodedTile(fp, 0, data[:th, :tw].copy()) libtiff.writeEncodedTile(fp, 1, data[:th, tw:w].copy()) libtiff.writeEncodedTile(fp, 2, data[th:h, :tw].copy()) libtiff.writeEncodedTile(fp, 3, data[th:h, tw:w].copy()) libtiff.close(fp) cls.minisblack_2x2_partial_tiles_data = data[:h, :w] cls.minisblack_2x2_partial_tiles_path = path
def setup_minisblack_3strip(cls, path): """ SCENARIO: create a simple monochromatic 3-strip image. The strips evenly divide the image. """ data = fixtures.skimage.data.moon() data = data[:480, :480] h, w = data.shape rps = h // 3 fp = libtiff.open(path, mode='w') libtiff.setField(fp, 'Photometric', libtiff.Photometric.MINISBLACK) libtiff.setField(fp, 'Compression', libtiff.Compression.DEFLATE) libtiff.setField(fp, 'ImageLength', data.shape[0]) libtiff.setField(fp, 'ImageWidth', data.shape[1]) libtiff.setField(fp, 'RowsPerStrip', rps) libtiff.setField(fp, 'BitsPerSample', 8) libtiff.setField(fp, 'SamplesPerPixel', 1) libtiff.setField(fp, 'PlanarConfig', libtiff.PlanarConfig.CONTIG) libtiff.writeEncodedStrip(fp, 0, data[:rps, :].copy()) libtiff.writeEncodedStrip(fp, 1, data[rps:rps * 2, :].copy()) libtiff.writeEncodedStrip(fp, 2, data[rps * 2:rps * 3, :].copy()) libtiff.close(fp) cls.minisblack_3_full_strips_path = path
def setup_rgb_evenly_stripped(cls, path): """ SCENARIO: create a simple RGB stripped image, stripsize of 32 """ j = Jp2k(glymur.data.goodstuff()) data = j[:] h, w, spp = data.shape rps = 32 fp = libtiff.open(path, mode='w') libtiff.setField(fp, 'Photometric', libtiff.Photometric.RGB) libtiff.setField(fp, 'Compression', libtiff.Compression.DEFLATE) libtiff.setField(fp, 'ImageLength', data.shape[0]) libtiff.setField(fp, 'ImageWidth', data.shape[1]) libtiff.setField(fp, 'RowsPerStrip', rps) libtiff.setField(fp, 'BitsPerSample', 8) libtiff.setField(fp, 'SamplesPerPixel', spp) libtiff.setField(fp, 'PlanarConfig', libtiff.PlanarConfig.CONTIG) for stripnum in range(25): row = rps * stripnum stripdata = data[row:row + rps, :, :].copy() libtiff.writeEncodedStrip(fp, stripnum, stripdata) libtiff.close(fp) cls.goodstuff_data = data cls.goodstuff_path = path
def setup_minisblack_3x3(cls, path): """ SCENARIO: create a simple monochromatic 3x3 tiled image """ data = fixtures.skimage.data.moon() data = data[:480, :480] h, w = data.shape th, tw = h // 3, w // 3 fp = libtiff.open(path, mode='w') libtiff.setField(fp, 'Photometric', libtiff.Photometric.MINISBLACK) libtiff.setField(fp, 'Compression', libtiff.Compression.DEFLATE) libtiff.setField(fp, 'ImageLength', data.shape[0]) libtiff.setField(fp, 'ImageWidth', data.shape[1]) libtiff.setField(fp, 'TileLength', th) libtiff.setField(fp, 'TileWidth', tw) libtiff.setField(fp, 'BitsPerSample', 8) libtiff.setField(fp, 'SamplesPerPixel', 1) libtiff.writeEncodedTile(fp, 0, data[:th, :tw].copy()) libtiff.writeEncodedTile(fp, 1, data[:th, tw:tw * 2].copy()) libtiff.writeEncodedTile(fp, 2, data[:th, tw * 2:w].copy()) libtiff.writeEncodedTile(fp, 3, data[th:th * 2, :tw].copy()) libtiff.writeEncodedTile(fp, 4, data[th:th * 2, tw:tw * 2].copy()) libtiff.writeEncodedTile(fp, 5, data[th:th * 2, tw * 2:w].copy()) libtiff.writeEncodedTile(fp, 6, data[2 * th:h, :tw].copy()) libtiff.writeEncodedTile(fp, 7, data[2 * th:h, tw:tw * 2].copy()) libtiff.writeEncodedTile(fp, 8, data[2 * th:h, tw * 2:w].copy()) libtiff.close(fp) cls.minisblack_3x3_data = data cls.minisblack_3x3_tif = path
def setup_ycbcr_jpeg(cls, path): """ SCENARIO: create a simple color 2x2 tiled image """ data = fixtures.skimage.data.astronaut() h, w, z = data.shape th, tw = h // 2, w // 2 fp = libtiff.open(path, mode='w') libtiff.setField(fp, 'Photometric', libtiff.Photometric.YCBCR) libtiff.setField(fp, 'Compression', libtiff.Compression.JPEG) libtiff.setField(fp, 'ImageLength', data.shape[0]) libtiff.setField(fp, 'ImageWidth', data.shape[1]) libtiff.setField(fp, 'TileLength', th) libtiff.setField(fp, 'TileWidth', tw) libtiff.setField(fp, 'BitsPerSample', 8) libtiff.setField(fp, 'SamplesPerPixel', 3) libtiff.setField(fp, 'PlanarConfig', libtiff.PlanarConfig.CONTIG) libtiff.setField(fp, 'JPEGColorMode', libtiff.PlanarConfig.CONTIG) libtiff.setField(fp, 'JPEGQuality', 100) libtiff.writeEncodedTile(fp, 0, data[:th, :tw, :].copy()) libtiff.writeEncodedTile(fp, 1, data[:th, tw:w, :].copy()) libtiff.writeEncodedTile(fp, 2, data[th:h, :tw, :].copy()) libtiff.writeEncodedTile(fp, 3, data[th:h, tw:w, :].copy()) libtiff.close(fp) # now read it back fp = libtiff.open(path) tile = np.zeros((th, tw, 4), dtype=np.uint8) actual_data = np.zeros((h, w, 3), dtype=np.uint8) libtiff.readRGBATile(fp, 0, 0, tile) actual_data[:th, :tw, :] = tile[::-1, :, :3] libtiff.readRGBATile(fp, 256, 0, tile) actual_data[:th, tw:w, :] = tile[::-1, :, :3] libtiff.readRGBATile(fp, 0, 256, tile) actual_data[th:h, :tw, :] = tile[::-1, :, :3] libtiff.readRGBATile(fp, 256, 256, tile) actual_data[th:h, tw:w, :] = tile[::-1, :, :3] libtiff.close(fp) cls.astronaut_ycbcr_jpeg_data = actual_data cls.astronaut_ycbcr_jpeg_tif = path
def setup_rgb_uint16(cls, path): """ SCENARIO: create a simple color 2x2 tiled 16bit image """ data = fixtures.skimage.data.astronaut().astype(np.uint16) h, w, z = data.shape th, tw = h // 2, w // 2 fp = libtiff.open(path, mode='w') libtiff.setField(fp, 'Photometric', libtiff.Photometric.RGB) libtiff.setField(fp, 'Compression', libtiff.Compression.DEFLATE) libtiff.setField(fp, 'ImageLength', data.shape[0]) libtiff.setField(fp, 'ImageWidth', data.shape[1]) libtiff.setField(fp, 'TileLength', th) libtiff.setField(fp, 'TileWidth', tw) libtiff.setField(fp, 'BitsPerSample', 16) libtiff.setField(fp, 'SamplesPerPixel', 3) libtiff.setField(fp, 'SampleFormat', libtiff.SampleFormat.UINT) libtiff.setField(fp, 'PlanarConfig', libtiff.PlanarConfig.CONTIG) libtiff.writeEncodedTile(fp, 0, data[:th, :tw, :].copy()) libtiff.writeEncodedTile(fp, 1, data[:th, tw:w, :].copy()) libtiff.writeEncodedTile(fp, 2, data[th:h, :tw, :].copy()) libtiff.writeEncodedTile(fp, 3, data[th:h, tw:w, :].copy()) libtiff.close(fp) # now read it back fp = libtiff.open(path) tile = np.zeros((th, tw, 3), dtype=np.uint16) actual_data = np.zeros((h, w, 3), dtype=np.uint16) libtiff.readEncodedTile(fp, 0, tile) actual_data[:th, :tw, :] = tile libtiff.readEncodedTile(fp, 1, tile) actual_data[:th, tw:w, :] = tile libtiff.readEncodedTile(fp, 2, tile) actual_data[th:h, :tw, :] = tile libtiff.readEncodedTile(fp, 3, tile) actual_data[th:h, tw:w, :] = tile libtiff.close(fp) cls.astronaut_uint16_data = actual_data cls.astronaut_uint16_filename = path
def setup_minisblack_spp1(cls, path): """ SCENARIO: create a simple monochromatic 2x2 tiled image """ data = fixtures.skimage.data.moon() h, w = data.shape th, tw = h // 2, w // 2 fp = libtiff.open(path, mode='w') libtiff.setField(fp, 'Photometric', libtiff.Photometric.MINISBLACK) libtiff.setField(fp, 'Compression', libtiff.Compression.DEFLATE) libtiff.setField(fp, 'ImageLength', data.shape[0]) libtiff.setField(fp, 'ImageWidth', data.shape[1]) libtiff.setField(fp, 'TileLength', th) libtiff.setField(fp, 'TileWidth', tw) libtiff.setField(fp, 'BitsPerSample', 8) libtiff.setField(fp, 'SamplesPerPixel', 1) libtiff.writeEncodedTile(fp, 0, data[:th, :tw].copy()) libtiff.writeEncodedTile(fp, 1, data[:th, tw:w].copy()) libtiff.writeEncodedTile(fp, 2, data[th:h, :tw].copy()) libtiff.writeEncodedTile(fp, 3, data[th:h, tw:w].copy()) libtiff.close(fp) # now read it back fp = libtiff.open(path) tile = np.zeros((th, tw), dtype=np.uint8) actual_data = np.zeros((h, w), dtype=np.uint8) libtiff.readEncodedTile(fp, 0, tile) actual_data[:th, :tw] = tile libtiff.readEncodedTile(fp, 1, tile) actual_data[:th, tw:w] = tile libtiff.readEncodedTile(fp, 2, tile) actual_data[th:h, :tw] = tile libtiff.readEncodedTile(fp, 3, tile) actual_data[th:h, tw:w] = tile libtiff.close(fp) cls.minisblack_spp1_data = actual_data cls.minisblack_spp1_path = path
def test_simple_tile(self): """ SCENARIO: create a simple monochromatic 2x2 tiled image """ data = fixtures.skimage.data.moon() h, w = data.shape th, tw = h // 2, w // 2 fp = libtiff.open(self.temp_tiff_filename, mode='w') libtiff.setField(fp, 'Photometric', libtiff.Photometric.MINISBLACK) libtiff.setField(fp, 'Compression', libtiff.Compression.DEFLATE) libtiff.setField(fp, 'ImageLength', data.shape[0]) libtiff.setField(fp, 'ImageWidth', data.shape[1]) libtiff.setField(fp, 'TileLength', th) libtiff.setField(fp, 'TileWidth', tw) libtiff.setField(fp, 'BitsPerSample', 8) libtiff.setField(fp, 'SamplesPerPixel', 1) libtiff.writeEncodedTile(fp, 0, data[:th, :tw].copy()) libtiff.writeEncodedTile(fp, 1, data[:th, tw:w].copy()) libtiff.writeEncodedTile(fp, 2, data[th:h, :tw].copy()) libtiff.writeEncodedTile(fp, 3, data[th:h, tw:w].copy()) libtiff.close(fp) fp = libtiff.open(self.temp_tiff_filename) tile = np.zeros((th, tw), dtype=np.uint8) actual_data = np.zeros((h, w), dtype=np.uint8) libtiff.readEncodedTile(fp, 0, tile) actual_data[:th, :tw] = tile libtiff.readEncodedTile(fp, 1, tile) actual_data[:th, tw:w] = tile libtiff.readEncodedTile(fp, 2, tile) actual_data[th:h, :tw] = tile libtiff.readEncodedTile(fp, 3, tile) actual_data[th:h, tw:w] = tile libtiff.close(fp) np.testing.assert_array_equal(data, actual_data)
def test_cmyk(self): """ Scenario: CMYK (or separated) is not a supported colorspace. Expected result: RuntimeError """ data = fixtures.skimage.data.moon() data = np.dstack((data, data)) h, w, spp = data.shape # instead of 160, this will cause a partially empty last strip rps = 512 fp = libtiff.open(self.temp_tiff_filename, mode='w') libtiff.setField(fp, 'Photometric', libtiff.Photometric.SEPARATED) libtiff.setField(fp, 'Compression', libtiff.Compression.DEFLATE) libtiff.setField(fp, 'ImageLength', data.shape[0]) libtiff.setField(fp, 'ImageWidth', data.shape[1]) libtiff.setField(fp, 'RowsPerStrip', rps) libtiff.setField(fp, 'BitsPerSample', 8) libtiff.setField(fp, 'SamplesPerPixel', spp) libtiff.setField(fp, 'PlanarConfig', libtiff.PlanarConfig.CONTIG) libtiff.setField(fp, 'InkSet', libtiff.InkSet.MULTIINK) libtiff.writeEncodedStrip(fp, 0, data.copy()) libtiff.close(fp) with Tiff2Jp2k(self.temp_tiff_filename, self.temp_jp2_filename) as j: with warnings.catch_warnings(): # weird warning about extra samples warnings.simplefilter('ignore') with self.assertRaises(RuntimeError): j.run()