mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-02-04 05:34:36 +03:00
Simplified code
This commit is contained in:
parent
a0e1fde1ed
commit
747029bea9
|
@ -29,6 +29,7 @@ BLP files come in many different flavours:
|
||||||
- DXT5 compression is used if alpha_encoding == 7.
|
- DXT5 compression is used if alpha_encoding == 7.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
import struct
|
import struct
|
||||||
import warnings
|
import warnings
|
||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
|
@ -276,7 +277,12 @@ class BlpImageFile(ImageFile.ImageFile):
|
||||||
|
|
||||||
def _open(self):
|
def _open(self):
|
||||||
self.magic = self.fp.read(4)
|
self.magic = self.fp.read(4)
|
||||||
self._read_blp_header()
|
|
||||||
|
self.fp.seek(5, os.SEEK_CUR)
|
||||||
|
(self._blp_alpha_depth,) = struct.unpack("<b", self.fp.read(1))
|
||||||
|
|
||||||
|
self.fp.seek(2, os.SEEK_CUR)
|
||||||
|
self._size = struct.unpack("<II", self.fp.read(8))
|
||||||
|
|
||||||
if self.magic == b"BLP1":
|
if self.magic == b"BLP1":
|
||||||
decoder = "BLP1"
|
decoder = "BLP1"
|
||||||
|
@ -289,32 +295,12 @@ class BlpImageFile(ImageFile.ImageFile):
|
||||||
|
|
||||||
self.tile = [(decoder, (0, 0) + self.size, 0, (self.mode, 0, 1))]
|
self.tile = [(decoder, (0, 0) + self.size, 0, (self.mode, 0, 1))]
|
||||||
|
|
||||||
def _read_blp_header(self):
|
|
||||||
(self._blp_compression,) = struct.unpack("<i", self.fp.read(4))
|
|
||||||
|
|
||||||
(self._blp_encoding,) = struct.unpack("<b", self.fp.read(1))
|
|
||||||
(self._blp_alpha_depth,) = struct.unpack("<b", self.fp.read(1))
|
|
||||||
(self._blp_alpha_encoding,) = struct.unpack("<b", self.fp.read(1))
|
|
||||||
(self._blp_mips,) = struct.unpack("<b", self.fp.read(1))
|
|
||||||
|
|
||||||
self._size = struct.unpack("<II", self.fp.read(8))
|
|
||||||
|
|
||||||
if self.magic == b"BLP1":
|
|
||||||
# Only present for BLP1
|
|
||||||
(self._blp_encoding,) = struct.unpack("<i", self.fp.read(4))
|
|
||||||
(self._blp_subtype,) = struct.unpack("<i", self.fp.read(4))
|
|
||||||
|
|
||||||
self._blp_offsets = struct.unpack("<16I", self.fp.read(16 * 4))
|
|
||||||
self._blp_lengths = struct.unpack("<16I", self.fp.read(16 * 4))
|
|
||||||
|
|
||||||
|
|
||||||
class _BLPBaseDecoder(ImageFile.PyDecoder):
|
class _BLPBaseDecoder(ImageFile.PyDecoder):
|
||||||
_pulls_fd = True
|
_pulls_fd = True
|
||||||
|
|
||||||
def decode(self, buffer):
|
def decode(self, buffer):
|
||||||
try:
|
try:
|
||||||
self.fd.seek(0)
|
|
||||||
self.magic = self.fd.read(4)
|
|
||||||
self._read_blp_header()
|
self._read_blp_header()
|
||||||
self._load()
|
self._load()
|
||||||
except struct.error as e:
|
except struct.error as e:
|
||||||
|
@ -335,19 +321,20 @@ class _BLPBaseDecoder(ImageFile.PyDecoder):
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def _read_blp_header(self):
|
def _read_blp_header(self):
|
||||||
|
self.fd.seek(4)
|
||||||
(self._blp_compression,) = struct.unpack("<i", self._safe_read(4))
|
(self._blp_compression,) = struct.unpack("<i", self._safe_read(4))
|
||||||
|
|
||||||
(self._blp_encoding,) = struct.unpack("<b", self._safe_read(1))
|
(self._blp_encoding,) = struct.unpack("<b", self._safe_read(1))
|
||||||
(self._blp_alpha_depth,) = struct.unpack("<b", self._safe_read(1))
|
(self._blp_alpha_depth,) = struct.unpack("<b", self._safe_read(1))
|
||||||
(self._blp_alpha_encoding,) = struct.unpack("<b", self._safe_read(1))
|
(self._blp_alpha_encoding,) = struct.unpack("<b", self._safe_read(1))
|
||||||
(self._blp_mips,) = struct.unpack("<b", self._safe_read(1))
|
self.fd.seek(1, os.SEEK_CUR) # mips
|
||||||
|
|
||||||
self.size = struct.unpack("<II", self._safe_read(8))
|
self.size = struct.unpack("<II", self._safe_read(8))
|
||||||
|
|
||||||
if self.magic == b"BLP1":
|
if isinstance(self, BLP1Decoder):
|
||||||
# Only present for BLP1
|
# Only present for BLP1
|
||||||
(self._blp_encoding,) = struct.unpack("<i", self._safe_read(4))
|
(self._blp_encoding,) = struct.unpack("<i", self._safe_read(4))
|
||||||
(self._blp_subtype,) = struct.unpack("<i", self._safe_read(4))
|
self.fd.seek(4, os.SEEK_CUR) # subtype
|
||||||
|
|
||||||
self._blp_offsets = struct.unpack("<16I", self._safe_read(16 * 4))
|
self._blp_offsets = struct.unpack("<16I", self._safe_read(16 * 4))
|
||||||
self._blp_lengths = struct.unpack("<16I", self._safe_read(16 * 4))
|
self._blp_lengths = struct.unpack("<16I", self._safe_read(16 * 4))
|
||||||
|
|
|
@ -149,14 +149,13 @@ _encode(ImagingEncoderObject *encoder, PyObject *args) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
_encode_to_pyfd(ImagingEncoderObject *encoder, PyObject *args) {
|
_encode_to_pyfd(ImagingEncoderObject *encoder) {
|
||||||
PyObject *result;
|
PyObject *result;
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
if (!encoder->pushes_fd) {
|
if (!encoder->pushes_fd) {
|
||||||
// UNDONE, appropriate errcode???
|
// UNDONE, appropriate errcode???
|
||||||
result = Py_BuildValue("ii", 0, IMAGING_CODEC_CONFIG);
|
result = Py_BuildValue("ii", 0, IMAGING_CODEC_CONFIG);
|
||||||
;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -307,7 +306,7 @@ static struct PyMethodDef methods[] = {
|
||||||
{"encode", (PyCFunction)_encode, METH_VARARGS},
|
{"encode", (PyCFunction)_encode, METH_VARARGS},
|
||||||
{"cleanup", (PyCFunction)_encode_cleanup, METH_VARARGS},
|
{"cleanup", (PyCFunction)_encode_cleanup, METH_VARARGS},
|
||||||
{"encode_to_file", (PyCFunction)_encode_to_file, METH_VARARGS},
|
{"encode_to_file", (PyCFunction)_encode_to_file, METH_VARARGS},
|
||||||
{"encode_to_pyfd", (PyCFunction)_encode_to_pyfd, METH_VARARGS},
|
{"encode_to_pyfd", (PyCFunction)_encode_to_pyfd, METH_NOARGS},
|
||||||
{"setimage", (PyCFunction)_setimage, METH_VARARGS},
|
{"setimage", (PyCFunction)_setimage, METH_VARARGS},
|
||||||
{"setfd", (PyCFunction)_setfd, METH_VARARGS},
|
{"setfd", (PyCFunction)_setfd, METH_VARARGS},
|
||||||
{NULL, NULL} /* sentinel */
|
{NULL, NULL} /* sentinel */
|
||||||
|
|
Loading…
Reference in New Issue
Block a user