|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from PIL import Image |
| 6 | + |
| 7 | +from .helper import hopper |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.parametrize( |
| 11 | + "mode, dest_modes", |
| 12 | + ( |
| 13 | + ("L", ["I", "F", "LA", "RGB", "RGBA", "RGBX", "CMYK", "YCbCr", "HSV"]), |
| 14 | + ("I", ["L", "F"]), # Technically I;32 can work for any 4x8bit storage. |
| 15 | + ("F", ["I", "L", "LA", "RGB", "RGBA", "RGBX", "CMYK", "YCbCr", "HSV"]), |
| 16 | + ("LA", ["L", "F"]), |
| 17 | + ("RGB", ["L", "F"]), |
| 18 | + ("RGBA", ["L", "F"]), |
| 19 | + ("RGBX", ["L", "F"]), |
| 20 | + ("CMYK", ["L", "F"]), |
| 21 | + ("YCbCr", ["L", "F"]), |
| 22 | + ("HSV", ["L", "F"]), |
| 23 | + ), |
| 24 | +) |
| 25 | +def test_invalid_array_type(mode: str, dest_modes: list[str]) -> None: |
| 26 | + img = hopper(mode) |
| 27 | + for dest_mode in dest_modes: |
| 28 | + with pytest.raises(ValueError): |
| 29 | + Image.fromarrow(img, dest_mode, img.size) |
| 30 | + |
| 31 | + |
| 32 | +def test_invalid_array_size() -> None: |
| 33 | + img = hopper("RGB") |
| 34 | + |
| 35 | + assert img.size != (10, 10) |
| 36 | + with pytest.raises(ValueError): |
| 37 | + Image.fromarrow(img, "RGB", (10, 10)) |
| 38 | + |
| 39 | + |
| 40 | +def test_release_schema() -> None: |
| 41 | + # these should not error out, valgrind should be clean |
| 42 | + img = hopper("L") |
| 43 | + schema = img.__arrow_c_schema__() |
| 44 | + del schema |
| 45 | + |
| 46 | + |
| 47 | +def test_release_array() -> None: |
| 48 | + # these should not error out, valgrind should be clean |
| 49 | + img = hopper("L") |
| 50 | + array, schema = img.__arrow_c_array__() |
| 51 | + del array |
| 52 | + del schema |
| 53 | + |
| 54 | + |
| 55 | +def test_readonly() -> None: |
| 56 | + img = hopper("L") |
| 57 | + reloaded = Image.fromarrow(img, img.mode, img.size) |
| 58 | + assert reloaded.readonly == 1 |
| 59 | + reloaded._readonly = 0 |
| 60 | + assert reloaded.readonly == 1 |
| 61 | + |
| 62 | + |
| 63 | +def test_multiblock_l_image() -> None: |
| 64 | + block_size = Image.core.get_block_size() |
| 65 | + |
| 66 | + # check a 2 block image in single channel mode |
| 67 | + size = (4096, 2 * block_size // 4096) |
| 68 | + img = Image.new("L", size, 128) |
| 69 | + |
| 70 | + with pytest.raises(ValueError): |
| 71 | + (schema, arr) = img.__arrow_c_array__() |
| 72 | + |
| 73 | + |
| 74 | +def test_multiblock_rgba_image() -> None: |
| 75 | + block_size = Image.core.get_block_size() |
| 76 | + |
| 77 | + # check a 2 block image in 4 channel mode |
| 78 | + size = (4096, (block_size // 4096) // 2) |
| 79 | + img = Image.new("RGBA", size, (128, 127, 126, 125)) |
| 80 | + |
| 81 | + with pytest.raises(ValueError): |
| 82 | + (schema, arr) = img.__arrow_c_array__() |
| 83 | + |
| 84 | + |
| 85 | +def test_multiblock_l_schema() -> None: |
| 86 | + block_size = Image.core.get_block_size() |
| 87 | + |
| 88 | + # check a 2 block image in single channel mode |
| 89 | + size = (4096, 2 * block_size // 4096) |
| 90 | + img = Image.new("L", size, 128) |
| 91 | + |
| 92 | + with pytest.raises(ValueError): |
| 93 | + img.__arrow_c_schema__() |
| 94 | + |
| 95 | + |
| 96 | +def test_multiblock_rgba_schema() -> None: |
| 97 | + block_size = Image.core.get_block_size() |
| 98 | + |
| 99 | + # check a 2 block image in 4 channel mode |
| 100 | + size = (4096, (block_size // 4096) // 2) |
| 101 | + img = Image.new("RGBA", size, (128, 127, 126, 125)) |
| 102 | + |
| 103 | + with pytest.raises(ValueError): |
| 104 | + img.__arrow_c_schema__() |
| 105 | + |
| 106 | + |
| 107 | +def test_singleblock_l_image() -> None: |
| 108 | + Image.core.set_use_block_allocator(1) |
| 109 | + |
| 110 | + block_size = Image.core.get_block_size() |
| 111 | + |
| 112 | + # check a 2 block image in 4 channel mode |
| 113 | + size = (4096, 2 * (block_size // 4096)) |
| 114 | + img = Image.new("L", size, 128) |
| 115 | + assert img.im.isblock() |
| 116 | + |
| 117 | + (schema, arr) = img.__arrow_c_array__() |
| 118 | + assert schema |
| 119 | + assert arr |
| 120 | + |
| 121 | + Image.core.set_use_block_allocator(0) |
| 122 | + |
| 123 | + |
| 124 | +def test_singleblock_rgba_image() -> None: |
| 125 | + Image.core.set_use_block_allocator(1) |
| 126 | + block_size = Image.core.get_block_size() |
| 127 | + |
| 128 | + # check a 2 block image in 4 channel mode |
| 129 | + size = (4096, (block_size // 4096) // 2) |
| 130 | + img = Image.new("RGBA", size, (128, 127, 126, 125)) |
| 131 | + assert img.im.isblock() |
| 132 | + |
| 133 | + (schema, arr) = img.__arrow_c_array__() |
| 134 | + assert schema |
| 135 | + assert arr |
| 136 | + Image.core.set_use_block_allocator(0) |
| 137 | + |
| 138 | + |
| 139 | +def test_singleblock_l_schema() -> None: |
| 140 | + Image.core.set_use_block_allocator(1) |
| 141 | + block_size = Image.core.get_block_size() |
| 142 | + |
| 143 | + # check a 2 block image in single channel mode |
| 144 | + size = (4096, 2 * block_size // 4096) |
| 145 | + img = Image.new("L", size, 128) |
| 146 | + assert img.im.isblock() |
| 147 | + |
| 148 | + schema = img.__arrow_c_schema__() |
| 149 | + assert schema |
| 150 | + Image.core.set_use_block_allocator(0) |
| 151 | + |
| 152 | + |
| 153 | +def test_singleblock_rgba_schema() -> None: |
| 154 | + Image.core.set_use_block_allocator(1) |
| 155 | + block_size = Image.core.get_block_size() |
| 156 | + |
| 157 | + # check a 2 block image in 4 channel mode |
| 158 | + size = (4096, (block_size // 4096) // 2) |
| 159 | + img = Image.new("RGBA", size, (128, 127, 126, 125)) |
| 160 | + assert img.im.isblock() |
| 161 | + |
| 162 | + schema = img.__arrow_c_schema__() |
| 163 | + assert schema |
| 164 | + Image.core.set_use_block_allocator(0) |
0 commit comments