def load(self, f):
SUPPORTED_COLOR_TYPES = (COLOR_TYPE_TRUECOLOR, COLOR_TYPE_TRUECOLOR_WITH_ALPHA)<EOL>SAMPLES_PER_PIXEL = { COLOR_TYPE_TRUECOLOR: <NUM_LIT:3>,<EOL>COLOR_TYPE_TRUECOLOR_WITH_ALPHA: <NUM_LIT:4> }<EOL>assert f.read(<NUM_LIT:8>) == SIGNATURE<EOL>chunks = iter(self.chunks(f))<EOL>header = next(chunks)<EOL>assert header[<NUM_LIT:0>] == b'<STR_LIT>'<EOL>(width, height, bit_depth, color_type, compression,<EOL>filter_type, interlace) = struct.unpack(b"<STR_LIT>", header[<NUM_LIT:1>])<EOL>if bit_depth != <NUM_LIT:8>:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(bit_depth))<EOL><DEDENT>if compression != <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(compression))<EOL><DEDENT>if filter_type != <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(filter_type))<EOL><DEDENT>if interlace != <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(interlace))<EOL><DEDENT>if color_type not in SUPPORTED_COLOR_TYPES:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(SUPPORTED_COLOR_TYPES))<EOL><DEDENT>self.width = width<EOL>self.height = height<EOL>self.canvas = bytearray(self.bgcolor * width * height)<EOL>bytes_per_pixel = SAMPLES_PER_PIXEL[color_type]<EOL>bytes_per_row = bytes_per_pixel * width<EOL>bytes_per_rgba_row = SAMPLES_PER_PIXEL[COLOR_TYPE_TRUECOLOR_WITH_ALPHA] * width<EOL>bytes_per_scanline = bytes_per_row + <NUM_LIT:1><EOL>scanline_fmt = ('<STR_LIT>' % bytes_per_scanline).encode('<STR_LIT:ascii>')<EOL>reader = ByteReader(chunks)<EOL>old_row = None<EOL>cursor = <NUM_LIT:0><EOL>for row in range(height):<EOL><INDENT>scanline = reader.read(bytes_per_scanline)<EOL>unpacked = list(struct.unpack(scanline_fmt, scanline))<EOL>old_row = self.defilter(unpacked[<NUM_LIT:1>:], old_row, unpacked[<NUM_LIT:0>], bpp=bytes_per_pixel)<EOL>rgba_row = old_row if color_type == COLOR_TYPE_TRUECOLOR_WITH_ALPHA else rgb2rgba(old_row)<EOL>self.canvas[cursor:cursor + bytes_per_rgba_row] = rgba_row<EOL>cursor += bytes_per_rgba_row<EOL><DEDENT>
Load a PNG image