2929 - DXT5 compression is used if alpha_encoding == 7.
3030"""
3131
32+ import os
3233import struct
3334import warnings
3435from 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
311299class _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 ))
0 commit comments