// Copyright 2020 The Wuffs Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use "std/crc32" use "std/zlib" pub status "#bad header" pub status "#unsupported PNG file" pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0 pub struct decoder? implements base.image_decoder( // The 0x00FF_FFFF limit is arbitrary (the PNG spec says 0x7FFF_FFFF) but // it means that (width * height * bytes_per_pixel) doesn't overflow. width : base.u32[..= 0x00FF_FFFF], height : base.u32[..= 0x00FF_FFFF], // bytes_per_row doesn't include the 1 byte for the per-row filter. bytes_per_row : base.u64[..= 0x07FF_FFF8], workbuf_wi : base.u64, workbuf_length : base.u64[..= 0x0007_FFFF_F100_0007], // Call sequence states: // - 0x00: initial state. // - 0x03: image config decoded. // - 0x04: frame config decoded. // - 0xFF: end-of-data, usually after (the non-animated) frame decoded. // // State transitions: // // - 0x00 -> 0x03: via DIC // - 0x00 -> 0x04: via DFC with implicit DIC // - 0x00 -> 0xFF: via DF with implicit DIC and DFC // // - 0x03 -> 0x04: via DFC // - 0x03 -> 0xFF: via DF with implicit DFC // // - 0x04 -> 0xFF: via DFC // - 0x04 -> 0xFF: via DF // // - ???? -> 0x03: via RF for ???? > 0x00 // // Where: // - DF is decode_frame // - DFC is decode_frame_config, implicit means nullptr args.dst // - DIC is decode_image_config, implicit means nullptr args.dst // - RF is restart_frame call_sequence : base.u8, depth : base.u8[..= 16], src_pixfmt : base.u32, chunk_type : base.u32, chunk_length : base.u64, frame_config_io_position : base.u64, swizzler : base.pixel_swizzler, util : base.utility, )( crc : crc32.ieee_hasher, zlib : zlib.decoder, ) pub func decoder.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) { } pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_reader) { var magic : base.u64 var a32 : base.u32 var a8 : base.u8 var dst_pixfmt : base.u32 if this.call_sequence <> 0 { return base."#bad call sequence" } magic = args.src.read_u64le?() if magic <> '\x89PNG\x0D\x0A\x1A\x0A'le { return "#bad header" } magic = args.src.read_u64le?() if magic <> '\x00\x00\x00\x0DIHDR'le { return "#bad header" } a32 = args.src.read_u32be?() if a32 >= 0x8000_0000 { return "#bad header" } else if a32 >= 0x0100_0000 { return "#unsupported PNG file" } this.width = a32 a32 = args.src.read_u32be?() if a32 >= 0x8000_0000 { return "#bad header" } else if a32 >= 0x0100_0000 { return "#unsupported PNG file" } this.height = a32 // Depth. a8 = args.src.read_u8?() if a8 > 16 { return "#bad header" } this.depth = a8 // Color. a8 = args.src.read_u8?() if a8 == 0 { dst_pixfmt = base.PIXEL_FORMAT__Y this.src_pixfmt = base.PIXEL_FORMAT__Y if this.depth == 8 { this.bytes_per_row = (this.width as base.u64) * 1 } else { return "#unsupported PNG file" } } else if a8 == 2 { dst_pixfmt = base.PIXEL_FORMAT__BGR this.src_pixfmt = base.PIXEL_FORMAT__RGB if this.depth == 8 { this.bytes_per_row = (this.width as base.u64) * 3 } else { return "#unsupported PNG file" } } else { return "#unsupported PNG file" } this.workbuf_length = (this.height as base.u64) * (1 + this.bytes_per_row) // Compression. a8 = args.src.read_u8?() if a8 <> 0 { return "#bad header" } // Filter. a8 = args.src.read_u8?() if a8 <> 0 { return "#bad header" } // Interlace. a8 = args.src.read_u8?() if a8 == 0 { // No-op. } else if a8 == 1 { return "#unsupported PNG file" } else { return "#bad header" } // TODO: verify CRC-32 checksum. args.src.skip_u32?(n: 4) // Read up until an IDAT chunk. while true { this.chunk_length = args.src.read_u32be_as_u64?() this.chunk_type = args.src.read_u32le?() if this.chunk_type == 'IDAT'le { break } args.src.skip?(n: this.chunk_length) // TODO: verify CRC-32 checksum. args.src.skip_u32?(n: 4) } endwhile this.frame_config_io_position = args.src.position() if args.dst <> nullptr { args.dst.set!( pixfmt: dst_pixfmt, pixsub: 0, width: this.width, height: this.height, first_frame_io_position: this.frame_config_io_position, first_frame_is_opaque: false) } this.call_sequence = 3 } pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) { if this.call_sequence < 3 { this.decode_image_config?(dst: nullptr, src: args.src) } else if this.call_sequence == 3 { if this.frame_config_io_position <> args.src.position() { return base."#bad restart" } } else if this.call_sequence == 4 { this.call_sequence = 0xFF return base."@end of data" } else { return base."@end of data" } if args.dst <> nullptr { args.dst.set!(bounds: this.util.make_rect_ie_u32( min_incl_x: 0, min_incl_y: 0, max_excl_x: this.width, max_excl_y: this.height), duration: 0, index: 0, io_position: this.frame_config_io_position, disposal: 0, opaque_within_bounds: false, overwrite_instead_of_blend: false, background_color: 0x0000_0000) } this.call_sequence = 4 } pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) { var w : base.io_writer var w_mark : base.u64 var r_mark : base.u64 var zlib_status : base.status if this.call_sequence < 4 { this.decode_frame_config?(dst: nullptr, src: args.src) } else if this.call_sequence == 4 { // No-op. } else { return base."@end of data" } this.workbuf_wi = 0 while true { if (this.workbuf_wi >= this.workbuf_length) or ( this.workbuf_length >= args.workbuf.length()) { return base."#bad workbuf length" } io_bind (io: w, data: args.workbuf[this.workbuf_wi .. this.workbuf_length]) { io_limit (io: args.src, limit: this.chunk_length) { w_mark = w.mark() r_mark = args.src.mark() zlib_status =? this.zlib.transform_io?( dst: w, src: args.src, workbuf: this.util.empty_slice_u8()) this.chunk_length ~sat-= args.src.count_since(mark: r_mark) this.workbuf_wi ~sat+= w.count_since(mark: w_mark) } } if zlib_status.is_ok() { break } else if zlib_status == base."$short write" { return base."#too much data" } else if zlib_status <> base."$short read" { return zlib_status } else if this.chunk_length == 0 { // TODO: move to the next IDAT. } yield? base."$short read" } endwhile if this.workbuf_wi <> this.workbuf_length { return base."#not enough data" } this.call_sequence = 0xFF } pub func decoder.frame_dirty_rect() base.rect_ie_u32 { return this.util.make_rect_ie_u32( min_incl_x: 0, min_incl_y: 0, max_excl_x: this.width, max_excl_y: this.height) } pub func decoder.num_animation_loops() base.u32 { return 0 } pub func decoder.num_decoded_frame_configs() base.u64 { if this.call_sequence > 3 { return 1 } return 0 } pub func decoder.num_decoded_frames() base.u64 { if this.call_sequence > 4 { return 1 } return 0 } pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.status { if this.call_sequence < 3 { return base."#bad call sequence" } if args.index <> 0 { return base."#bad argument" } this.call_sequence = 3 this.frame_config_io_position = args.io_position return ok } pub func decoder.set_report_metadata!(fourcc: base.u32, report: base.bool) { // TODO. } pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_information, src: base.io_reader) { return base."#no more information" } pub func decoder.workbuf_len() base.range_ii_u64 { return this.util.make_range_ii_u64( min_incl: this.workbuf_length, max_incl: this.workbuf_length) }