Extracted core of getheader into _get_global_header

This commit is contained in:
Andrew Murray 2017-02-18 19:02:52 +11:00
parent a43295c3dd
commit 26cfa8cc62

View File

@ -382,7 +382,8 @@ def _save(im, fp, filename, save_all=False):
im_frame = frame_data['im'] im_frame = frame_data['im']
if not frame_data['bbox']: if not frame_data['bbox']:
# global header # global header
for s in getheader(im_frame, palette, frame_data['encoderinfo'])[0]: for s in _get_global_header(im_frame, palette,
frame_data['encoderinfo']):
fp.write(s) fp.write(s)
offset = (0, 0) offset = (0, 0)
else: else:
@ -393,7 +394,7 @@ def _save(im, fp, filename, save_all=False):
offset = frame_data['bbox'][:2] offset = frame_data['bbox'][:2]
_write_frame_data(fp, im_frame, offset, frame_data['encoderinfo']) _write_frame_data(fp, im_frame, offset, frame_data['encoderinfo'])
if not save_all: if not save_all:
for s in getheader(im_out, palette, im.encoderinfo)[0]: for s in _get_global_header(im_out, palette, im.encoderinfo):
fp.write(s) fp.write(s)
# local image header # local image header
@ -665,7 +666,7 @@ def _get_palette_bytes(im, palette, info):
# returning palette, _not_ padded to 768 bytes like our internal ones. # returning palette, _not_ padded to 768 bytes like our internal ones.
return palette_bytes, used_palette_colors return palette_bytes, used_palette_colors
def getheader(im, palette=None, info=None): def _get_global_header(im, palette, info):
"""Return a list of strings representing a GIF header""" """Return a list of strings representing a GIF header"""
# Header Block # Header Block
@ -683,34 +684,34 @@ def getheader(im, palette=None, info=None):
if im.info.get("version") == b"89a": if im.info.get("version") == b"89a":
version = b"89a" version = b"89a"
header = [ palette_bytes = _get_palette_bytes(im, palette, info)[0]
color_table_size = _get_color_table_size(palette_bytes)
background = info["background"] if "background" in info else 0
return [
b"GIF"+version + # signature + version b"GIF"+version + # signature + version
o16(im.size[0]) + # canvas width o16(im.size[0]) + # canvas width
o16(im.size[1]) # canvas height o16(im.size[1]) + # canvas height
]
palette_bytes, used_palette_colors = _get_palette_bytes(im, palette, info)
# Logical Screen Descriptor # Logical Screen Descriptor
color_table_size = _get_color_table_size(palette_bytes)
# size of global color table + global color table flag # size of global color table + global color table flag
header.append(o8(color_table_size + 128)) # packed fields o8(color_table_size + 128) + # packed fields
# background + reserved/aspect # background + reserved/aspect
if info and "background" in info: o8(background) + o8(0) +
background = info["background"]
elif "background" in im.info: # Global Color Table
# This elif is redundant within GifImagePlugin _get_header_palette(palette_bytes)
# since im.info parameters are bundled into the info dictionary ]
# However, external scripts may call getheader directly
# So this maintains earlier behaviour def getheader(im, palette=None, info=[]):
background = im.info["background"] used_palette_colors = _get_optimize(im, info)
else:
background = 0 if not "background" in info and "background" in im.info:
header.append(o8(background) + o8(0)) info["background"] = im.info["background"]
# end of Logical Screen Descriptor
header = _get_global_header(im, palette, info)
# Header + Logical Screen Descriptor + Global Color Table
header.append(_get_header_palette(palette_bytes))
return header, used_palette_colors return header, used_palette_colors
def _write_frame_data(fp, im_frame, offset, params): def _write_frame_data(fp, im_frame, offset, params):