mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +01:00
Implement std/png filters
This commit is contained in:
1 parent
4ff1429553
commit
217b9dc9dc
2 files changed
+174
-4
No files matched your search
@@ -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!(
|
||||
|
||||
Reference in new issue
Block a user