Skip to content

Commit 747029b

Browse files
committed
Simplified code
1 parent a0e1fde commit 747029b

File tree

2 files changed

+13
-27
lines changed

2 files changed

+13
-27
lines changed

src/PIL/BlpImagePlugin.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- DXT5 compression is used if alpha_encoding == 7.
3030
"""
3131

32+
import os
3233
import struct
3334
import warnings
3435
from enum import IntEnum
@@ -276,7 +277,12 @@ class BlpImageFile(ImageFile.ImageFile):
276277

277278
def _open(self):
278279
self.magic = self.fp.read(4)
279-
self._read_blp_header()
280+
281+
self.fp.seek(5, os.SEEK_CUR)
282+
(self._blp_alpha_depth,) = struct.unpack("<b", self.fp.read(1))
283+
284+
self.fp.seek(2, os.SEEK_CUR)
285+
self._size = struct.unpack("<II", self.fp.read(8))
280286

281287
if self.magic == b"BLP1":
282288
decoder = "BLP1"
@@ -289,32 +295,12 @@ def _open(self):
289295

290296
self.tile = [(decoder, (0, 0) + self.size, 0, (self.mode, 0, 1))]
291297

292-
def _read_blp_header(self):
293-
(self._blp_compression,) = struct.unpack("<i", self.fp.read(4))
294-
295-
(self._blp_encoding,) = struct.unpack("<b", self.fp.read(1))
296-
(self._blp_alpha_depth,) = struct.unpack("<b", self.fp.read(1))
297-
(self._blp_alpha_encoding,) = struct.unpack("<b", self.fp.read(1))
298-
(self._blp_mips,) = struct.unpack("<b", self.fp.read(1))
299-
300-
self._size = struct.unpack("<II", self.fp.read(8))
301-
302-
if self.magic == b"BLP1":
303-
# Only present for BLP1
304-
(self._blp_encoding,) = struct.unpack("<i", self.fp.read(4))
305-
(self._blp_subtype,) = struct.unpack("<i", self.fp.read(4))
306-
307-
self._blp_offsets = struct.unpack("<16I", self.fp.read(16 * 4))
308-
self._blp_lengths = struct.unpack("<16I", self.fp.read(16 * 4))
309-
310298

311299
class _BLPBaseDecoder(ImageFile.PyDecoder):
312300
_pulls_fd = True
313301

314302
def decode(self, buffer):
315303
try:
316-
self.fd.seek(0)
317-
self.magic = self.fd.read(4)
318304
self._read_blp_header()
319305
self._load()
320306
except struct.error as e:
@@ -335,19 +321,20 @@ def _read_palette(self):
335321
return ret
336322

337323
def _read_blp_header(self):
324+
self.fd.seek(4)
338325
(self._blp_compression,) = struct.unpack("<i", self._safe_read(4))
339326

340327
(self._blp_encoding,) = struct.unpack("<b", self._safe_read(1))
341328
(self._blp_alpha_depth,) = struct.unpack("<b", self._safe_read(1))
342329
(self._blp_alpha_encoding,) = struct.unpack("<b", self._safe_read(1))
343-
(self._blp_mips,) = struct.unpack("<b", self._safe_read(1))
330+
self.fd.seek(1, os.SEEK_CUR) # mips
344331

345332
self.size = struct.unpack("<II", self._safe_read(8))
346333

347-
if self.magic == b"BLP1":
334+
if isinstance(self, BLP1Decoder):
348335
# Only present for BLP1
349336
(self._blp_encoding,) = struct.unpack("<i", self._safe_read(4))
350-
(self._blp_subtype,) = struct.unpack("<i", self._safe_read(4))
337+
self.fd.seek(4, os.SEEK_CUR) # subtype
351338

352339
self._blp_offsets = struct.unpack("<16I", self._safe_read(16 * 4))
353340
self._blp_lengths = struct.unpack("<16I", self._safe_read(16 * 4))

src/encode.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,13 @@ _encode(ImagingEncoderObject *encoder, PyObject *args) {
149149
}
150150

151151
static PyObject *
152-
_encode_to_pyfd(ImagingEncoderObject *encoder, PyObject *args) {
152+
_encode_to_pyfd(ImagingEncoderObject *encoder) {
153153
PyObject *result;
154154
int status;
155155

156156
if (!encoder->pushes_fd) {
157157
// UNDONE, appropriate errcode???
158158
result = Py_BuildValue("ii", 0, IMAGING_CODEC_CONFIG);
159-
;
160159
return result;
161160
}
162161

@@ -307,7 +306,7 @@ static struct PyMethodDef methods[] = {
307306
{"encode", (PyCFunction)_encode, METH_VARARGS},
308307
{"cleanup", (PyCFunction)_encode_cleanup, METH_VARARGS},
309308
{"encode_to_file", (PyCFunction)_encode_to_file, METH_VARARGS},
310-
{"encode_to_pyfd", (PyCFunction)_encode_to_pyfd, METH_VARARGS},
309+
{"encode_to_pyfd", (PyCFunction)_encode_to_pyfd, METH_NOARGS},
311310
{"setimage", (PyCFunction)_setimage, METH_VARARGS},
312311
{"setfd", (PyCFunction)_setfd, METH_VARARGS},
313312
{NULL, NULL} /* sentinel */

0 commit comments

Comments
 (0)