Skip to content

Commit 2f16077

Browse files
committed
Use ImageFile._Tile
1 parent 2ec01c9 commit 2f16077

1 file changed

Lines changed: 31 additions & 37 deletions

File tree

src/PIL/VtfImagePlugin.py

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -160,46 +160,36 @@ def _get_mipmap_count(width: int, height: int) -> int:
160160

161161

162162
def _write_image(fp: IO[bytes], im: Image.Image, pixel_format: VtfPF) -> None:
163-
extents = (0, 0) + im.size
164163
encoder_args: tuple[int,] | tuple[str, int, int]
165164
if pixel_format == VtfPF.DXT1:
166-
encoder = "bcn"
167165
encoder_args = (1,)
168166
im = im.convert("RGBA")
169167
elif pixel_format == VtfPF.DXT3:
170-
encoder = "bcn"
171168
encoder_args = (3,)
172169
elif pixel_format == VtfPF.DXT5:
173-
encoder = "bcn"
174170
encoder_args = (5,)
175171
elif pixel_format == VtfPF.RGB888:
176-
encoder = "raw"
177172
encoder_args = ("RGB", 0, 0)
178173
elif pixel_format == VtfPF.BGR888:
179-
encoder = "raw"
180174
encoder_args = ("BGR", 0, 0)
181175
elif pixel_format == VtfPF.RGBA8888:
182-
encoder = "raw"
183176
encoder_args = ("RGBA", 0, 0)
184177
elif pixel_format == VtfPF.A8:
185-
encoder = "raw"
186178
encoder_args = ("A", 0, 0)
187179
elif pixel_format == VtfPF.I8:
188-
encoder = "raw"
189180
encoder_args = ("L", 0, 0)
190181
im = im.convert("L")
191182
elif pixel_format == VtfPF.IA88:
192-
encoder = "raw"
193183
encoder_args = ("LA", 0, 0)
194184
im = im.convert("LA")
195185
elif pixel_format == VtfPF.UV88:
196-
encoder = "raw"
197186
encoder_args = ("RG", 0, 0)
198187
else:
199188
msg = f"Unsupported pixel format: {pixel_format!r}"
200189
raise VTFException(msg)
201190

202-
tile = [ImageFile._Tile(encoder, extents, fp.tell(), encoder_args)]
191+
codec_name = "bcn" if pixel_format in BLOCK_COMPRESSED else "raw"
192+
tile = [ImageFile._Tile(codec_name, (0, 0) + im.size, fp.tell(), encoder_args)]
203193
ImageFile._save(im, fp, tile, _get_texture_size(pixel_format, *im.size))
204194

205195

@@ -216,6 +206,7 @@ def _open(self):
216206
if not _accept(self.fp.read(12)):
217207
msg = "not a VTF file"
218208
raise SyntaxError(msg)
209+
219210
self.fp.seek(4)
220211
version = struct.unpack("<2I", self.fp.read(8))
221212
if version <= (7, 2):
@@ -245,9 +236,8 @@ def _open(self):
245236
else:
246237
msg = f"Unsupported VTF version: {version}"
247238
raise VTFException(msg)
248-
# flags = CompiledVtfFlags(header.flags)
239+
249240
pixel_format = VtfPF(header.pixel_format)
250-
low_format = VtfPF(header.low_pixel_format)
251241
if pixel_format in (
252242
VtfPF.DXT1_ONEBITALPHA,
253243
VtfPF.DXT1,
@@ -268,43 +258,47 @@ def _open(self):
268258
msg = f"Unsupported VTF pixel format: {pixel_format}"
269259
raise VTFException(msg)
270260

271-
self._size = (header.width, header.height)
272-
273-
data_start = self.fp.tell()
274-
data_start += _get_texture_size(low_format, header.low_width, header.low_height)
275-
min_res = 4 if pixel_format in BLOCK_COMPRESSED else 1
276-
for mip_id in range(header.mipmap_count - 1, 0, -1):
277-
mip_width = max(header.width >> mip_id, min_res)
278-
mip_height = max(header.height >> mip_id, min_res)
279-
280-
data_start += _get_texture_size(pixel_format, mip_width, mip_height)
281-
282261
if pixel_format in (VtfPF.DXT1, VtfPF.DXT1_ONEBITALPHA):
283-
tile = ("bcn", (0, 0) + self.size, data_start, (1, "DXT1"))
262+
args = (1, "DXT1")
284263
elif pixel_format == VtfPF.DXT3:
285-
tile = ("bcn", (0, 0) + self.size, data_start, (2, "DXT3"))
264+
args = (2, "DXT3")
286265
elif pixel_format == VtfPF.DXT5:
287-
tile = ("bcn", (0, 0) + self.size, data_start, (3, "DXT5"))
266+
args = (3, "DXT5")
288267
elif pixel_format == VtfPF.RGBA8888:
289-
tile = ("raw", (0, 0) + self.size, data_start, ("RGBA", 0, 1))
268+
args = ("RGBA", 0, 1)
290269
elif pixel_format == VtfPF.RGB888:
291-
tile = ("raw", (0, 0) + self.size, data_start, ("RGB", 0, 1))
270+
args = ("RGB", 0, 1)
292271
elif pixel_format == VtfPF.BGR888:
293-
tile = ("raw", (0, 0) + self.size, data_start, ("BGR", 0, 1))
272+
args = ("BGR", 0, 1)
294273
elif pixel_format == VtfPF.BGRA8888:
295-
tile = ("raw", (0, 0) + self.size, data_start, ("BGRA", 0, 1))
274+
args = ("BGRA", 0, 1)
296275
elif pixel_format == VtfPF.UV88:
297-
tile = ("raw", (0, 0) + self.size, data_start, ("RG", 0, 1))
276+
args = ("RG", 0, 1)
298277
elif pixel_format == VtfPF.I8:
299-
tile = ("raw", (0, 0) + self.size, data_start, ("L", 0, 1))
278+
args = ("L", 0, 1)
300279
elif pixel_format == VtfPF.A8:
301-
tile = ("raw", (0, 0) + self.size, data_start, ("A", 0, 1))
280+
args = ("A", 0, 1)
302281
elif pixel_format == VtfPF.IA88:
303-
tile = ("raw", (0, 0) + self.size, data_start, ("LA", 0, 1))
282+
args = ("LA", 0, 1)
304283
else:
305284
msg = f"Unsupported VTF pixel format: {pixel_format}"
306285
raise VTFException(msg)
307-
self.tile = [tile]
286+
287+
self._size = (header.width, header.height)
288+
289+
low_format = VtfPF(header.low_pixel_format)
290+
291+
data_start = self.fp.tell()
292+
data_start += _get_texture_size(low_format, header.low_width, header.low_height)
293+
min_res = 4 if pixel_format in BLOCK_COMPRESSED else 1
294+
for mip_id in range(header.mipmap_count - 1, 0, -1):
295+
mip_width = max(header.width >> mip_id, min_res)
296+
mip_height = max(header.height >> mip_id, min_res)
297+
298+
data_start += _get_texture_size(pixel_format, mip_width, mip_height)
299+
300+
codec_name = "bcn" if pixel_format in BLOCK_COMPRESSED else "raw"
301+
self.tile = [ImageFile._Tile(codec_name, (0, 0) + self.size, data_start, args)]
308302

309303

310304
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:

0 commit comments

Comments
 (0)