From 217b9dc9dcbd44c7313764e38ebccbc48fac1091 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 26 Dec 2020 19:26:47 +1100 Subject: [PATCH] Implement std/png filters --- release/c/wuffs-unsupported-snapshot.c | 80 ++++++++++++++++++++- std/png/decode_png.wuffs | 98 +++++++++++++++++++++++++- 2 files changed, 174 insertions(+), 4 deletions(-) diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 8481c4a3..250f19c0 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -8194,6 +8194,7 @@ extern "C" { // ---------------- Status Codes extern const char wuffs_png__error__bad_chunk[]; +extern const char wuffs_png__error__bad_filter[]; extern const char wuffs_png__error__bad_header[]; extern const char wuffs_png__error__unsupported_png_file[]; @@ -8345,6 +8346,7 @@ struct wuffs_png__decoder__struct { uint64_t f_workbuf_length; uint8_t f_call_sequence; uint8_t f_depth; + uint8_t f_filter_distance; uint32_t f_src_pixfmt; uint32_t f_chunk_type; uint64_t f_chunk_length; @@ -29798,6 +29800,7 @@ wuffs_zlib__decoder__transform_io( // ---------------- Status Codes Implementations const char wuffs_png__error__bad_chunk[] = "#png: bad chunk"; +const char wuffs_png__error__bad_filter[] = "#png: bad filter"; const char wuffs_png__error__bad_header[] = "#png: bad header"; const char wuffs_png__error__unsupported_png_file[] = "#png: unsupported PNG file"; const char wuffs_png__error__internal_error_inconsistent_workbuf_length[] = "#png: internal error: inconsistent workbuf length"; @@ -30168,6 +30171,7 @@ wuffs_png__decoder__decode_image_config( v_dst_pixfmt = 536870920; self->private_impl.f_src_pixfmt = 536870920; if (self->private_impl.f_depth == 8) { + self->private_impl.f_filter_distance = 1; self->private_impl.f_bytes_per_row = (((uint64_t)(self->private_impl.f_width)) * 1); } else { status = wuffs_base__make_status(wuffs_png__error__unsupported_png_file); @@ -30177,6 +30181,7 @@ wuffs_png__decoder__decode_image_config( v_dst_pixfmt = 2147485832; self->private_impl.f_src_pixfmt = 2684356744; if (self->private_impl.f_depth == 8) { + self->private_impl.f_filter_distance = 3; self->private_impl.f_bytes_per_row = (((uint64_t)(self->private_impl.f_width)) * 3); } else { status = wuffs_base__make_status(wuffs_png__error__unsupported_png_file); @@ -30761,11 +30766,20 @@ wuffs_png__decoder__filter_and_swizzle( uint64_t v_dst_bytes_per_row = 0; wuffs_base__slice_u8 v_dst_palette = {0}; wuffs_base__table_u8 v_tab = {0}; + uint64_t v_filter_distance = 0; uint32_t v_y = 0; wuffs_base__slice_u8 v_dst = {0}; uint8_t v_filter = 0; wuffs_base__slice_u8 v_curr_row = {0}; wuffs_base__slice_u8 v_prev_row = {0}; + uint64_t v_i = 0; + uint32_t v_fa = 0; + uint32_t v_fb = 0; + uint32_t v_fc = 0; + uint32_t v_pp = 0; + uint32_t v_pa = 0; + uint32_t v_pb = 0; + uint32_t v_pc = 0; v_dst_pixfmt = wuffs_base__pixel_buffer__pixel_format(a_dst); v_dst_bits_per_pixel = wuffs_base__pixel_format__bits_per_pixel(&v_dst_pixfmt); @@ -30776,6 +30790,7 @@ wuffs_png__decoder__filter_and_swizzle( v_dst_bytes_per_row = (((uint64_t)(self->private_impl.f_width)) * v_dst_bytes_per_pixel); v_dst_palette = wuffs_base__pixel_buffer__palette_or_else(a_dst, wuffs_base__make_slice_u8(self->private_data.f_dst_palette, 1024)); v_tab = wuffs_base__pixel_buffer__plane(a_dst, 0); + v_filter_distance = ((uint64_t)(self->private_impl.f_filter_distance)); while (v_y < self->private_impl.f_height) { v_dst = wuffs_base__table_u8__row(v_tab, v_y); if (v_dst_bytes_per_row < ((uint64_t)(v_dst.len))) { @@ -30792,7 +30807,70 @@ wuffs_png__decoder__filter_and_swizzle( v_curr_row = wuffs_base__slice_u8__subslice_j(a_workbuf, self->private_impl.f_bytes_per_row); a_workbuf = wuffs_base__slice_u8__subslice_i(a_workbuf, self->private_impl.f_bytes_per_row); if (v_filter == 0) { - } else if (((uint64_t)(v_prev_row.len)) == 0) { + } else if (v_filter == 1) { + v_i = v_filter_distance; + while (v_i < ((uint64_t)(v_curr_row.len))) { + if (v_i >= v_filter_distance) { + if ((v_i - v_filter_distance) < ((uint64_t)(v_curr_row.len))) { + v_curr_row.ptr[v_i] += v_curr_row.ptr[(v_i - v_filter_distance)]; + } + } + v_i += 1; + } + } else if (v_filter == 2) { + v_i = 0; + while ((v_i < ((uint64_t)(v_curr_row.len))) && (v_i < ((uint64_t)(v_prev_row.len)))) { + v_curr_row.ptr[v_i] += v_prev_row.ptr[v_i]; + v_i += 1; + } + } else if (v_filter == 3) { + v_i = 0; + while ((v_i < ((uint64_t)(v_curr_row.len))) && (v_i < ((uint64_t)(v_prev_row.len)))) { + if (v_i >= v_filter_distance) { + if ((v_i - v_filter_distance) < ((uint64_t)(v_curr_row.len))) { + v_curr_row.ptr[v_i] += ((uint8_t)(((((uint32_t)(v_curr_row.ptr[(v_i - v_filter_distance)])) + ((uint32_t)(v_prev_row.ptr[v_i]))) / 2))); + } + } else { + v_curr_row.ptr[v_i] += (v_prev_row.ptr[v_i] / 2); + } + v_i += 1; + } + } else if (v_filter == 4) { + v_i = 0; + while ((v_i < ((uint64_t)(v_curr_row.len))) && (v_i < ((uint64_t)(v_prev_row.len)))) { + if (v_i < v_filter_distance) { + v_curr_row.ptr[v_i] += v_prev_row.ptr[v_i]; + } else { + if (((v_i - v_filter_distance) < ((uint64_t)(v_curr_row.len))) && ((v_i - v_filter_distance) < ((uint64_t)(v_prev_row.len)))) { + v_fa = ((uint32_t)(v_curr_row.ptr[(v_i - v_filter_distance)])); + v_fb = ((uint32_t)(v_prev_row.ptr[v_i])); + v_fc = ((uint32_t)(v_prev_row.ptr[(v_i - v_filter_distance)])); + v_pp = ((v_fa + v_fb) - v_fc); + v_pa = (v_pp - v_fa); + if (v_pa >= 2147483648) { + v_pa = (0 - v_pa); + } + v_pb = (v_pp - v_fb); + if (v_pb >= 2147483648) { + v_pb = (0 - v_pb); + } + v_pc = (v_pp - v_fc); + if (v_pc >= 2147483648) { + v_pc = (0 - v_pc); + } + if ((v_pa <= v_pb) && (v_pa <= v_pc)) { + v_curr_row.ptr[v_i] += ((uint8_t)((v_fa & 255))); + } else if (v_pb <= v_pc) { + v_curr_row.ptr[v_i] += ((uint8_t)((v_fb & 255))); + } else { + v_curr_row.ptr[v_i] += ((uint8_t)((v_fc & 255))); + } + } + } + v_i += 1; + } + } else { + return wuffs_base__make_status(wuffs_png__error__bad_filter); } wuffs_base__pixel_swizzler__swizzle_interleaved_from_slice(&self->private_impl.f_swizzler, v_dst, v_dst_palette, v_curr_row); v_prev_row = v_curr_row; diff --git a/std/png/decode_png.wuffs b/std/png/decode_png.wuffs index 720a1c35..6c665f5f 100644 --- a/std/png/decode_png.wuffs +++ b/std/png/decode_png.wuffs @@ -16,6 +16,7 @@ use "std/crc32" use "std/zlib" pub status "#bad chunk" +pub status "#bad filter" pub status "#bad header" pub status "#unsupported PNG file" @@ -63,7 +64,8 @@ pub struct decoder? implements base.image_decoder( // - RF is restart_frame call_sequence : base.u8, - depth : base.u8[..= 16], + depth : base.u8[..= 16], + filter_distance : base.u8[..= 8], src_pixfmt : base.u32, @@ -134,6 +136,7 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_ dst_pixfmt = base.PIXEL_FORMAT__Y this.src_pixfmt = base.PIXEL_FORMAT__Y if this.depth == 8 { + this.filter_distance = 1 this.bytes_per_row = (this.width as base.u64) * 1 } else { return "#unsupported PNG file" @@ -142,6 +145,7 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_ dst_pixfmt = base.PIXEL_FORMAT__BGR this.src_pixfmt = base.PIXEL_FORMAT__RGB if this.depth == 8 { + this.filter_distance = 3 this.bytes_per_row = (this.width as base.u64) * 3 } else { return "#unsupported PNG file" @@ -316,6 +320,7 @@ pri func decoder.filter_and_swizzle!(dst: ptr base.pixel_buffer, workbuf: slice var dst_bytes_per_row : base.u64 var dst_palette : slice base.u8 var tab : table base.u8 + var filter_distance : base.u64[..= 8] var y : base.u32 var dst : slice base.u8 @@ -323,6 +328,15 @@ pri func decoder.filter_and_swizzle!(dst: ptr base.pixel_buffer, workbuf: slice var curr_row : slice base.u8 var prev_row : slice base.u8 + var i : base.u64 + var fa : base.u32 + var fb : base.u32 + var fc : base.u32 + var pp : base.u32 + var pa : base.u32 + var pb : base.u32 + var pc : base.u32 + // TODO: the dst_pixfmt variable shouldn't be necessary. We should be able // to chain the two calls: "args.dst.pixel_format().bits_per_pixel()". dst_pixfmt = args.dst.pixel_format() @@ -334,6 +348,7 @@ pri func decoder.filter_and_swizzle!(dst: ptr base.pixel_buffer, workbuf: slice dst_bytes_per_row = (this.width as base.u64) * dst_bytes_per_pixel dst_palette = args.dst.palette_or_else(fallback: this.dst_palette[..]) tab = args.dst.plane(p: 0) + filter_distance = this.filter_distance as base.u64 while y < this.height { assert y < 0xFFFF_FFFF via "a < b: a < c; c <= b"(c: this.height) @@ -355,8 +370,85 @@ pri func decoder.filter_and_swizzle!(dst: ptr base.pixel_buffer, workbuf: slice if filter == 0 { // No-op. - } else if prev_row.length() == 0 { - // TODO. + } else if filter == 1 { + i = filter_distance + while i < curr_row.length(), + inv y < 0xFFFF_FFFF, + { + assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: curr_row.length()) + if i >= filter_distance { + if (i - filter_distance) < curr_row.length() { + curr_row[i] ~mod+= curr_row[i - filter_distance] + } + } + i += 1 + } endwhile + } else if filter == 2 { + i = 0 + while (i < curr_row.length()) and (i < prev_row.length()), + inv y < 0xFFFF_FFFF, + { + assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: curr_row.length()) + curr_row[i] ~mod+= prev_row[i] + i += 1 + } endwhile + } else if filter == 3 { + i = 0 + while (i < curr_row.length()) and (i < prev_row.length()), + inv y < 0xFFFF_FFFF, + { + assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: curr_row.length()) + if i >= filter_distance { + if (i - filter_distance) < curr_row.length() { + curr_row[i] ~mod+= (( + (curr_row[i - filter_distance] as base.u32) + + (prev_row[i] as base.u32)) / 2) as base.u8 + } + } else { + curr_row[i] ~mod+= prev_row[i] / 2 + } + i += 1 + } endwhile + } else if filter == 4 { + i = 0 + while (i < curr_row.length()) and (i < prev_row.length()), + inv y < 0xFFFF_FFFF, + { + assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: curr_row.length()) + if i < filter_distance { + curr_row[i] ~mod+= prev_row[i] + } else { + if ((i - filter_distance) < curr_row.length()) and + ((i - filter_distance) < prev_row.length()) { + fa = curr_row[i - filter_distance] as base.u32 + fb = prev_row[i] as base.u32 + fc = prev_row[i - filter_distance] as base.u32 + pp = (fa ~mod+ fb) ~mod- fc + pa = pp ~mod- fa + if pa >= 0x8000_0000 { + pa = 0 ~mod- pa + } + pb = pp ~mod- fb + if pb >= 0x8000_0000 { + pb = 0 ~mod- pb + } + pc = pp ~mod- fc + if pc >= 0x8000_0000 { + pc = 0 ~mod- pc + } + if (pa <= pb) and (pa <= pc) { + curr_row[i] ~mod+= (fa & 0xFF) as base.u8 + } else if pb <= pc { + curr_row[i] ~mod+= (fb & 0xFF) as base.u8 + } else { + curr_row[i] ~mod+= (fc & 0xFF) as base.u8 + } + } + } + i += 1 + } endwhile + } else { + return "#bad filter" } this.swizzler.swizzle_interleaved_from_slice!(