Have std/png process the tRNS chunk

This commit is contained in:
Nigel Tao committed 2021-02-01 21:34:28 +11:00
1 parent c392a632de
commit 4d8e179279
2 files changed
+173 -34

No files matched your search

+54 -16
View File
@@ -72,7 +72,10 @@ pub struct decoder? implements base.image_decoder(
color_type : base.u8,
filter_distance : base.u8[..= 8],
seen_palette : base.bool,
seen_plte : base.bool,
seen_trns : base.bool,
tricky : base.bool,
dst_pixfmt : base.u32,
src_pixfmt : base.u32,
@@ -196,7 +199,7 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
} endwhile
if (this.color_type == 3) and (not this.seen_palette) {
if (this.color_type == 3) and (not this.seen_plte) {
return "#missing palette"
}
@@ -209,7 +212,7 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
width: this.width,
height: this.height,
first_frame_io_position: this.frame_config_io_position,
first_frame_is_opaque: false)
first_frame_is_opaque: (this.color_type <= 3) and (not this.seen_trns))
}
this.call_sequence = 3
@@ -282,7 +285,6 @@ pri func decoder.calculate_bytes_per_row!() {
this.dst_pixfmt = base.PIXEL_FORMAT__Y
this.src_pixfmt = base.PIXEL_FORMAT__Y
} else if this.color_type == 3 {
// TODO: s/BINARY/NONPREMUL/ and decode the tRNS chunk.
this.dst_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
} else {
@@ -316,7 +318,6 @@ pri func decoder.calculate_bytes_per_row!() {
}
} else if this.color_type == 3 {
// TODO: s/BINARY/NONPREMUL/ and decode the tRNS chunk.
this.dst_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
if this.depth == 8 {
@@ -324,6 +325,9 @@ pri func decoder.calculate_bytes_per_row!() {
this.bytes_per_row = (this.width as base.u64) * 1
}
} else if this.color_type == 4 {
// TODO: PIXEL_FORMAT__YA_NONPREMUL
} else if this.color_type == 6 {
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
this.src_pixfmt = base.PIXEL_FORMAT__RGBA_NONPREMUL
@@ -358,31 +362,35 @@ pri func decoder.choose_filter_implementations!() {
pri func decoder.decode_other_chunk?(src: base.io_reader) {
if this.chunk_type == 'PLTE'le {
if this.seen_palette {
if this.seen_plte or (this.color_type <> 3) {
return "#bad chunk"
}
this.decode_plte?(src: args.src)
this.seen_palette = true
this.seen_plte = true
} else if this.chunk_type == 'tRNS'le {
// TODO.
args.src.skip?(n: this.chunk_length)
if this.seen_trns or (this.color_type > 3) or
((this.color_type == 3) and (not this.seen_plte)) {
return "#bad chunk"
}
this.decode_trns?(src: args.src)
this.seen_trns = true
} else {
args.src.skip?(n: this.chunk_length)
}
}
pri func decoder.decode_plte?(src: base.io_reader) {
var num_palette_entries : base.u32[..= 256]
var i : base.u32
var argb : base.u32
var num_entries : base.u32[..= 256]
var i : base.u32
var argb : base.u32
if (this.chunk_length > 768) or ((this.chunk_length % 3) <> 0) {
return "#bad header"
return "#bad chunk"
}
num_palette_entries = (this.chunk_length as base.u32) / 3
num_entries = (this.chunk_length as base.u32) / 3
while i < num_palette_entries {
assert i < 256 via "a < b: a < c; c <= b"(c: num_palette_entries)
while i < num_entries {
assert i < 256 via "a < b: a < c; c <= b"(c: num_entries)
// Convert from RGB (in memory order) to ARGB (in native u32 order)
// to BGRA (in memory order).
argb = args.src.read_u24be_as_u32?()
@@ -404,6 +412,36 @@ pri func decoder.decode_plte?(src: base.io_reader) {
} endwhile
}
pri func decoder.decode_trns?(src: base.io_reader) {
var num_entries : base.u32[..= 256]
var i : base.u32
if this.chunk_length > 256 {
return "#bad chunk"
}
num_entries = this.chunk_length as base.u32
while i < num_entries {
assert i < 256 via "a < b: a < c; c <= b"(c: num_entries)
this.src_palette[(4 * i) + 3] = args.src.read_u8?()
i += 1
} endwhile
if this.color_type == 0 {
// TODO: PIXEL_FORMAT__YA_NONPREMUL
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
this.tricky = true
} else if this.color_type == 2 {
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
this.tricky = true
} else if this.color_type == 3 {
this.dst_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL
}
}
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)