1
1
import subprocess
2
2
import tempfile
3
3
from copy import copy
4
+ from dataclasses import dataclass
4
5
from weakref import WeakKeyDictionary
5
6
6
7
import piexif
16
17
Transformations = None
17
18
18
19
20
+ @dataclass
21
+ class LibheifError :
22
+ code : int
23
+ subcode : int
24
+
25
+ def __eq__ (self , e ):
26
+ if not isinstance (e , HeifError ): # pragma: no cover
27
+ return False
28
+ return e .code == self .code and e .subcode == self .subcode
29
+
30
+
31
+ class Errors :
32
+ end_of_file = LibheifError (7 , 100 )
33
+ unsupported_color_conversion = LibheifError (4 , 3003 )
34
+
35
+
19
36
ffi = FFI ()
20
37
_keep_refs = WeakKeyDictionary ()
21
38
HEIF_ENC_BIN = 'heif-enc'
@@ -27,7 +44,7 @@ def _crop_heif_file(heif):
27
44
if crop == (0 , 0 ) + heif .size :
28
45
return heif
29
46
30
- if heif .mode not in ("L" , "RGB" , "RGBA" ):
47
+ if heif .mode not in ("L" , "RGB" , "RGBA" ): # pragma: no cover
31
48
raise ValueError ("Unknown mode" )
32
49
pixel_size = len (heif .mode )
33
50
@@ -151,15 +168,15 @@ def load(self):
151
168
try :
152
169
heif_file = heif_file .load ()
153
170
except HeifError as e :
154
- if not ( e . code == 4 and e . subcode == 3003 ) :
171
+ if e != Errors . unsupported_color_conversion :
155
172
raise
156
173
# Unsupported feature: Unsupported color conversion
157
174
# https://github.com/strukturag/libheif/issues/1273
158
175
self .fp .seek (0 )
159
176
heif_file = self ._open_heif_file (True ).load ()
160
177
except HeifError as e :
161
178
# Ignore EOF error and return blank image otherwise
162
- cropped_file = e . code == 7 and e . subcode == 100
179
+ cropped_file = e == Errors . end_of_file
163
180
if not cropped_file or not ImageFile .LOAD_TRUNCATED_IMAGES :
164
181
raise
165
182
0 commit comments