-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_AEI.py
301 lines (217 loc) · 10.1 KB
/
test_AEI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
from io import BytesIO
from PIL.Image import Image
from AEPi import AEI, Texture, CompressionFormat
from AEPi.codec import ImageCodecAdaptor, supportsFormats
import pytest
from PIL import Image
from AEPi.constants import CompressionFormat
SMILEY_AEI_2TEXTURES_PATH = "src/tests/assets/smiley_ATC_twotextures_nomipmap_nosymbols_high.aei"
SMILEY_PNG_PATH = "src/tests/assets/smiley.png"
PIXEL_AEI_PATH = "src/tests/assets/pixel_ATC_nomipmap_nosymbols_high.aei"
DECOMPRESSED = Image.new("RGBA", (1, 1), (100, 200, 200, 255))
# This is the RGBA pixel from DECOMPRESSED, compressed to ATC 4bpp using AEIEditor.
COMPRESSED = b"\xFF\xFF\xFF\xFF\xFF\xFF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\xFF\xFF\xFF\xFF\xFF\xFF\x87\x25\xFF\xFF\xFF\xFF\x3F\x00"
def smileyImage():
png = Image.open(SMILEY_PNG_PATH)
if png.mode != "RGBA":
return png.convert("RGBA")
return png
g_useSmiley = False
@supportsFormats(
both=[CompressionFormat.ATC]
)
class MockCodec(ImageCodecAdaptor):
@classmethod
def compress(cls, im, format, quality): # type: ignore[reportMissingParameterType]
global g_useSmiley
if g_useSmiley:
return COMPRESSED_SMILEY_ATC
return COMPRESSED
@classmethod
def decompress(cls, fp, format, width, height, quality): # type: ignore[reportMissingParameterType]
global g_useSmiley
if g_useSmiley:
return smileyImage()
return DECOMPRESSED
#region dimensions
def test_resize_shrink_succeeds():
with AEI((10, 10)) as aei:
aei.addTexture(Texture(0, 0, 9, 10))
aei.shape = (9, 10)
assert aei.shape == (9, 10)
def test_resize_grow_succeeds():
with AEI((10, 10)) as aei:
aei.addTexture(Texture(0, 0, 10, 10))
aei.shape = (11, 10)
assert aei.shape == (11, 10)
def test_setWidth_shrink_succeeds():
with AEI((10, 10)) as aei:
aei.addTexture(Texture(0, 0, 9, 10))
aei.width = 9
assert aei.width == 9
def test_setWidth_grow_succeeds():
with AEI((10, 10)) as aei:
aei.addTexture(Texture(0, 0, 10, 10))
aei.width = 11
assert aei.width == 11
def test_setHeight_grow_succeeds():
with AEI((10, 10)) as aei:
aei.addTexture(Texture(0, 0, 10, 10))
aei.height = 10
assert aei.height == 10
def test_setHeight_shrink_succeeds():
with AEI((10, 10)) as aei:
aei.addTexture(Texture(0, 0, 10, 9))
aei.height = 9
assert aei.height == 9
def test_resize_shrink_failsFor_outOfBounds():
with AEI((10, 10)) as aei:
aei.addTexture(Texture(0, 0, 10, 10))
with pytest.raises(ValueError):
aei.shape = (9, 10)
def test_setWidth_shrink_failsFor_outOfBounds():
with AEI((10, 10)) as aei:
aei.addTexture(Texture(0, 0, 10, 10))
with pytest.raises(ValueError):
aei.width = 9
def test_setHeight_shrink_failsFor_outOfBounds():
with AEI((10, 10)) as aei:
aei.addTexture(Texture(0, 0, 10, 10))
with pytest.raises(ValueError):
aei.height = 9
def test_getWidth_getsWidth():
with AEI((10, 5)) as aei:
assert aei.width == 10
def test_getHeight_getsHeight():
with AEI((10, 5)) as aei:
assert aei.height == 5
#endregion dimensions
#region aei files
#region read
def test_read_readsImage():
with AEI.read(PIXEL_AEI_PATH) as aei:
assert aei.width == DECOMPRESSED.width
assert aei.height == DECOMPRESSED.height
for x in range(DECOMPRESSED.width):
for y in range(DECOMPRESSED.height):
assert aei._image.getpixel((x, y)) == DECOMPRESSED.getpixel((x, y)) # type: ignore[reportUnknownMemberType]
def test_read_readsTextures():
with AEI.read(PIXEL_AEI_PATH) as aei:
assert aei.textures[0].x == 0
assert aei.textures[0].y == 0
assert aei.textures[0].width == DECOMPRESSED.width
assert aei.textures[0].height == DECOMPRESSED.height
def test_read_twoTextures_isCorrect():
global g_useSmiley
g_useSmiley = True
with AEI.read(SMILEY_AEI_2TEXTURES_PATH) as aei:
assert len(aei.textures) == 2
assert aei.textures[0].shape == (8, 8)
assert aei.textures[0].position == (0, 0)
assert aei.textures[1].shape == (8, 8)
assert aei.textures[1].position == (8, 8)
g_useSmiley = False
#endregion read
#region write
def test_write_isCorrect():
with AEI(DECOMPRESSED) as aei, BytesIO() as outBytes, open(PIXEL_AEI_PATH, "rb") as expected:
aei.write(outBytes, format=CompressionFormat.ATC, quality=3)
expectedText = expected.read()
outBytes.seek(0)
actualText = outBytes.read()
assert expectedText == actualText
def test_write_twoTextures_isCorrect():
global g_useSmiley
g_useSmiley = True
with smileyImage() as png, open(SMILEY_AEI_2TEXTURES_PATH, "rb") as expected:
with AEI(png) as aei, BytesIO() as outBytes:
aei.addTexture(0, 0, 8, 8)
aei.addTexture(8, 8, 8, 8)
aei.write(outBytes, format=CompressionFormat.ATC, quality=3)
expectedText = expected.read()
outBytes.seek(0)
actualText = outBytes.read()
assert expectedText == actualText
g_useSmiley = False
#endregion write
#endregion aei files
#region textures
def test_addTexture_addsTexture():
with AEI((10, 10)) as aei:
aei.addTexture(Texture(0, 0, 10, 10))
assert aei.textures[0].x == 0
assert aei.textures[0].y == 0
assert aei.textures[0].width == 10
assert aei.textures[0].height == 10
def test_addTexture_withImage_addsImage():
with smileyImage() as png, AEI((16, 16)) as aei:
aei.addTexture(png, 0, 0)
for x in range(png.width):
for y in range(png.height):
assert aei._image.getpixel((x, y)) == png.getpixel((x, y)) # type: ignore[reportUnknownMemberType]
def test_addTexture_conflict_raises():
with AEI((10, 10)) as aei:
aei.addTexture(Texture(0, 0, 10, 10))
with pytest.raises(ValueError):
aei.addTexture(Texture(0, 0, 10, 10))
def test_addTexture_outOfBounds_raises():
with AEI((10, 10)) as aei:
with pytest.raises(ValueError):
aei.addTexture(Texture(11, 0, 10, 10))
def test_addTexture_withImage_incorrectMode_raises():
with smileyImage() as png, AEI((16, 16)) as aei:
with png.convert("L") as converted:
with pytest.raises(ValueError):
aei.addTexture(converted, 0, 0)
def test_removeTexture_removesTexture():
with AEI((10, 10)) as aei:
aei.addTexture(Texture(0, 0, 10, 10))
aei.removeTexture(0, 0, 10, 10)
assert len(aei.textures) == 0
def test_removeTexture_clearImage_clearsImage():
with Image.new("RGBA", (1, 1), (255, 255, 255, 255)) as png, AEI((1, 1)) as aei:
aei.addTexture(png, 0, 0)
aei.removeTexture(0, 0, 1, 1, clearImage=True)
assert aei._image.getpixel((0, 0)) == (0, 0, 0, 0) # type: ignore[reportUnknownMemberType]
def test_removeTexture_unknown_raises():
with AEI((10, 10)) as aei:
with pytest.raises(KeyError):
aei.removeTexture(0, 0, 10, 10)
def test_removeTexture_outOfBounds_raises():
with AEI((10, 10)) as aei:
with pytest.raises(ValueError):
aei.removeTexture(11, 0, 10, 10)
def test_replaceTexture_replacesTexture():
with Image.new("RGBA", (1, 1), (100, 100, 100, 100)) as png, Image.new("RGBA", (1, 1), (255, 255, 255, 255)) as newPng, AEI((1, 1)) as aei:
aei.addTexture(png, 0, 0)
aei.replaceTexture(newPng, Texture(0, 0, 1, 1))
assert aei._image.getpixel((0, 0)) == (255, 255, 255, 255) # type: ignore[reportUnknownMemberType]
def test_replaceTexture_unknown_raises():
with Image.new("RGBA", (1, 1)) as png, AEI((10, 10)) as aei:
with pytest.raises(KeyError):
aei.replaceTexture(png, Texture(0, 0, 1, 1))
def test_replaceTexture_outOfBounds_raises():
with Image.new("RGBA", (1, 1)) as png, AEI((10, 10)) as aei:
with pytest.raises(ValueError):
aei.replaceTexture(png, Texture(11, 0, 1, 1))
def test_replaceTexture_withImage_incorrectTexture_raises():
with Image.new("RGBA", (1, 1)) as png, AEI((10, 10)) as aei:
with pytest.raises(ValueError):
aei.replaceTexture(png, Texture(10, 0, 2, 2))
def test_replaceTexture_withImage_incorrectMode_raises():
with Image.new("L", (1, 1)) as png, AEI((10, 10)) as aei:
aei.addTexture(0, 0, 1, 1)
with pytest.raises(ValueError):
aei.replaceTexture(png, Texture(0, 0, 1, 1))
def test_getTexture_getsTexture():
with Image.new("RGBA", (1, 1), (255, 255, 255, 255)) as png, AEI((16, 16)) as aei:
aei.addTexture(png, 0, 0)
actual = aei.getTexture(0, 0, 1, 1)
assert actual.getpixel((0, 0)) == (255, 255, 255, 255) # type: ignore[reportUnknownMemberType]
def test_getTexture_outOfBounds_raises():
with AEI((10, 10)) as aei:
with pytest.raises(ValueError):
aei.getTexture(Texture(11, 0, 10, 10))
#endregion textures