mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-02-04 21:50:54 +03:00
improve simple palette detection for PNG
- reverts change #1239 which could remove transparency from valid images (see test_save_p_transparent_black test case) - improves simple palette detection to handle images from #1238
This commit is contained in:
parent
0f8d66bc96
commit
c773688f37
|
@ -74,8 +74,7 @@ _MODES = {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
_simple_palette = re.compile(b'^\xff+\x00\xff*$')
|
_simple_palette = re.compile(b'^\xff*\x00\xff*$')
|
||||||
_null_palette = re.compile(b'^\x00*$')
|
|
||||||
|
|
||||||
# Maximum decompressed size for a iTXt or zTXt chunk.
|
# Maximum decompressed size for a iTXt or zTXt chunk.
|
||||||
# Eliminates decompression bombs where compressed chunks can expand 1000x
|
# Eliminates decompression bombs where compressed chunks can expand 1000x
|
||||||
|
@ -118,7 +117,7 @@ class ChunkStream(object):
|
||||||
cid = s[4:]
|
cid = s[4:]
|
||||||
pos = self.fp.tell()
|
pos = self.fp.tell()
|
||||||
length = i32(s)
|
length = i32(s)
|
||||||
|
|
||||||
if not is_cid(cid):
|
if not is_cid(cid):
|
||||||
raise SyntaxError("broken PNG file (chunk %s)" % repr(cid))
|
raise SyntaxError("broken PNG file (chunk %s)" % repr(cid))
|
||||||
|
|
||||||
|
@ -359,12 +358,14 @@ class PngStream(ChunkStream):
|
||||||
s = ImageFile._safe_read(self.fp, length)
|
s = ImageFile._safe_read(self.fp, length)
|
||||||
if self.im_mode == "P":
|
if self.im_mode == "P":
|
||||||
if _simple_palette.match(s):
|
if _simple_palette.match(s):
|
||||||
|
# tRNS contains only one full-transparent entry,
|
||||||
|
# other entries are full opaque
|
||||||
i = s.find(b"\0")
|
i = s.find(b"\0")
|
||||||
if i >= 0:
|
if i >= 0:
|
||||||
self.im_info["transparency"] = i
|
self.im_info["transparency"] = i
|
||||||
elif _null_palette.match(s):
|
|
||||||
self.im_info["transparency"] = 0
|
|
||||||
else:
|
else:
|
||||||
|
# otherwise, we have a byte string with one alpha value
|
||||||
|
# for each palette entry
|
||||||
self.im_info["transparency"] = s
|
self.im_info["transparency"] = s
|
||||||
elif self.im_mode == "L":
|
elif self.im_mode == "L":
|
||||||
self.im_info["transparency"] = i16(s)
|
self.im_info["transparency"] = i16(s)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user