mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-20 00:47:28 +01:00
Refactor some std/png internals
This commit is contained in:
1 parent
4942324f25
commit
4053c2abe6
2 files changed
+232
-138
No files matched your search
+74
-43
@@ -69,7 +69,7 @@ pub struct decoder? implements base.image_decoder(
|
||||
ignore_checksum : base.bool,
|
||||
|
||||
depth : base.u8[..= 16],
|
||||
color_type : base.u8,
|
||||
color_type : base.u8[..= 6],
|
||||
filter_distance : base.u8[..= 8],
|
||||
|
||||
seen_plte : base.bool,
|
||||
@@ -246,14 +246,11 @@ pri func decoder.decode_ihdr?(src: base.io_reader) {
|
||||
this.depth = a8
|
||||
|
||||
// Color.
|
||||
this.color_type = args.src.read_u8?()
|
||||
this.bytes_per_row = 0
|
||||
this.calculate_bytes_per_row!()
|
||||
if this.bytes_per_row == 0 {
|
||||
return "#unsupported PNG file"
|
||||
a8 = args.src.read_u8?()
|
||||
if (a8 == 1) or (a8 == 5) or (a8 > 6) {
|
||||
return "#bad header"
|
||||
}
|
||||
this.workbuf_length = (this.height as base.u64) * (1 + this.bytes_per_row)
|
||||
this.choose_filter_implementations!()
|
||||
this.color_type = a8
|
||||
|
||||
// Compression.
|
||||
a8 = args.src.read_u8?()
|
||||
@@ -276,9 +273,19 @@ pri func decoder.decode_ihdr?(src: base.io_reader) {
|
||||
} else {
|
||||
return "#bad header"
|
||||
}
|
||||
|
||||
// Derived fields.
|
||||
this.filter_distance = 0
|
||||
this.assign_filter_distance!()
|
||||
if this.filter_distance == 0 {
|
||||
return "#unsupported PNG file"
|
||||
}
|
||||
this.bytes_per_row = this.calculate_bytes_per_row(width: this.width)
|
||||
this.workbuf_length = (this.height as base.u64) * (1 + this.bytes_per_row)
|
||||
this.choose_filter_implementations!()
|
||||
}
|
||||
|
||||
pri func decoder.calculate_bytes_per_row!() {
|
||||
pri func decoder.assign_filter_distance!() {
|
||||
if this.depth < 8 {
|
||||
if this.color_type == 0 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__Y
|
||||
@@ -291,13 +298,6 @@ pri func decoder.calculate_bytes_per_row!() {
|
||||
}
|
||||
|
||||
this.filter_distance = 1
|
||||
if this.depth == 4 {
|
||||
this.bytes_per_row = ((this.width + 1) / 2) as base.u64
|
||||
} else if this.depth == 2 {
|
||||
this.bytes_per_row = ((this.width + 3) / 4) as base.u64
|
||||
} else if this.depth == 1 {
|
||||
this.bytes_per_row = ((this.width + 7) / 8) as base.u64
|
||||
}
|
||||
choose filter_and_swizzle = [filter_and_swizzle_low_bit_depth]
|
||||
|
||||
} else if this.color_type == 0 {
|
||||
@@ -305,12 +305,10 @@ pri func decoder.calculate_bytes_per_row!() {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__Y
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__Y
|
||||
this.filter_distance = 1
|
||||
this.bytes_per_row = (this.width as base.u64) * 1
|
||||
} else if this.depth == 16 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__Y // TODO: PIXEL_FORMAT__Y_16LE
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__Y_16BE
|
||||
this.filter_distance = 2
|
||||
this.bytes_per_row = (this.width as base.u64) * 2
|
||||
}
|
||||
|
||||
} else if this.color_type == 2 {
|
||||
@@ -318,7 +316,6 @@ pri func decoder.calculate_bytes_per_row!() {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGR
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__RGB
|
||||
this.filter_distance = 3
|
||||
this.bytes_per_row = (this.width as base.u64) * 3
|
||||
}
|
||||
|
||||
} else if this.color_type == 3 {
|
||||
@@ -326,7 +323,6 @@ pri func decoder.calculate_bytes_per_row!() {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
|
||||
this.filter_distance = 1
|
||||
this.bytes_per_row = (this.width as base.u64) * 1
|
||||
}
|
||||
|
||||
} else if this.color_type == 4 {
|
||||
@@ -337,11 +333,25 @@ pri func decoder.calculate_bytes_per_row!() {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__RGBA_NONPREMUL
|
||||
this.filter_distance = 4
|
||||
this.bytes_per_row = (this.width as base.u64) * 4
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.calculate_bytes_per_row(width: base.u32[..= 0x00FF_FFFF]) base.u64[..= 0x07FF_FFF8] {
|
||||
var bytes_per_channel : base.u64[..= 2]
|
||||
|
||||
if this.depth == 1 {
|
||||
return ((args.width + 7) / 8) as base.u64
|
||||
} else if this.depth == 2 {
|
||||
return ((args.width + 3) / 4) as base.u64
|
||||
} else if this.depth == 4 {
|
||||
return ((args.width + 1) / 2) as base.u64
|
||||
}
|
||||
bytes_per_channel = (this.depth >> 3) as base.u64
|
||||
return (args.width as base.u64) * bytes_per_channel *
|
||||
(NUM_CHANNELS[this.color_type] as base.u64)
|
||||
}
|
||||
|
||||
pri func decoder.choose_filter_implementations!() {
|
||||
// Filter 0 is a no-op. Filter 2, the up filter, should already vectorize
|
||||
// easily by a good optimizing C compiler.
|
||||
@@ -479,13 +489,7 @@ pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_
|
||||
}
|
||||
|
||||
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 swizzler_status : base.status
|
||||
var zlib_status : base.status
|
||||
var checksum_have : base.u32
|
||||
var checksum_want : base.u32
|
||||
|
||||
if this.call_sequence < 4 {
|
||||
this.decode_frame_config?(dst: nullptr, src: args.src)
|
||||
@@ -495,6 +499,38 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
|
||||
return base."@end of data"
|
||||
}
|
||||
|
||||
if (this.width <= 0) or (this.height <= 0) {
|
||||
return ok
|
||||
}
|
||||
|
||||
swizzler_status = this.swizzler.prepare!(
|
||||
dst_pixfmt: args.dst.pixel_format(),
|
||||
dst_palette: args.dst.palette_or_else(fallback: this.dst_palette[..]),
|
||||
src_pixfmt: this.util.make_pixel_format(repr: this.src_pixfmt),
|
||||
src_palette: this.src_palette[..],
|
||||
blend: args.blend)
|
||||
if not swizzler_status.is_ok() {
|
||||
return swizzler_status
|
||||
}
|
||||
|
||||
this.decode_pass?(src: args.src, workbuf: args.workbuf)
|
||||
|
||||
swizzler_status = this.filter_and_swizzle!(dst: args.dst, workbuf: args.workbuf)
|
||||
if not swizzler_status.is_ok() {
|
||||
return swizzler_status
|
||||
}
|
||||
|
||||
this.call_sequence = 0xFF
|
||||
}
|
||||
|
||||
pri func decoder.decode_pass?(src: base.io_reader, workbuf: slice base.u8) {
|
||||
var w : base.io_writer
|
||||
var w_mark : base.u64
|
||||
var r_mark : base.u64
|
||||
var zlib_status : base.status
|
||||
var checksum_have : base.u32
|
||||
var checksum_want : base.u32
|
||||
|
||||
this.workbuf_wi = 0
|
||||
while true {
|
||||
if (this.workbuf_wi > this.workbuf_length) or (
|
||||
@@ -577,22 +613,6 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
|
||||
args.workbuf[0] = 1
|
||||
}
|
||||
}
|
||||
|
||||
swizzler_status = this.swizzler.prepare!(
|
||||
dst_pixfmt: args.dst.pixel_format(),
|
||||
dst_palette: args.dst.palette_or_else(fallback: this.dst_palette[..]),
|
||||
src_pixfmt: this.util.make_pixel_format(repr: this.src_pixfmt),
|
||||
src_palette: this.src_palette[..],
|
||||
blend: args.blend)
|
||||
if not swizzler_status.is_ok() {
|
||||
return swizzler_status
|
||||
}
|
||||
swizzler_status = this.filter_and_swizzle!(dst: args.dst, workbuf: args.workbuf)
|
||||
if not swizzler_status.is_ok() {
|
||||
return swizzler_status
|
||||
}
|
||||
|
||||
this.call_sequence = 0xFF
|
||||
}
|
||||
|
||||
pri func decoder.filter_and_swizzle!(dst: ptr base.pixel_buffer, workbuf: slice base.u8) base.status,
|
||||
@@ -898,3 +918,14 @@ pri const LOW_BIT_DEPTH_MULTIPLIER : array[8] base.u8 = [
|
||||
0,
|
||||
0,
|
||||
]
|
||||
|
||||
pri const NUM_CHANNELS : array[8] base.u8[..= 4] = [
|
||||
1, // color_type == 0: Y
|
||||
0, // Unused.
|
||||
3, // color_type == 2: RGB
|
||||
1, // color_type == 3: indexed
|
||||
2, // color_type == 4: YA
|
||||
0, // Unused.
|
||||
4, // color_type == 6: RGBA
|
||||
0, // Unused.
|
||||
]
|
||||
Reference in new issue
Block a user