mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +01:00
Have std/png support remapped transparency
This commit is contained in:
1 parent
00b329872a
commit
ca76a01ac2
4 files changed
+430
-79
No files matched your search
+82
-24
@@ -98,6 +98,17 @@ pub struct decoder? implements base.image_decoder(
|
||||
|
||||
zlib_is_dirty : base.bool,
|
||||
|
||||
// remap_transparency, if non-zero, is the 32-bit or 64-bit (depending on
|
||||
// this.depth) argb_nonpremul color that is nominally opaque but remapped
|
||||
// to transparent black.
|
||||
//
|
||||
// "Remapped" is an unofficial term. This is where the IHDR color type is 0
|
||||
// (Y) or 2 (RGB) but there's also a tRNS chunk.
|
||||
//
|
||||
// PNG transparency that isn't "remapped" is color type 3 (indexed) with
|
||||
// tRNS, color type 4 (YA) or color type 6 (RGBA).
|
||||
remap_transparency : base.u64,
|
||||
|
||||
dst_pixfmt : base.u32,
|
||||
src_pixfmt : base.u32,
|
||||
|
||||
@@ -311,7 +322,7 @@ pri func decoder.decode_ihdr?(src: base.io_reader) {
|
||||
this.filter_distance = 0
|
||||
this.assign_filter_distance!()
|
||||
if this.filter_distance == 0 {
|
||||
return "#unsupported PNG file"
|
||||
return "#bad header"
|
||||
}
|
||||
this.overall_workbuf_length = (this.height as base.u64) *
|
||||
(1 + this.calculate_bytes_per_row(width: this.width))
|
||||
@@ -320,7 +331,9 @@ pri func decoder.decode_ihdr?(src: base.io_reader) {
|
||||
|
||||
pri func decoder.assign_filter_distance!() {
|
||||
if this.depth < 8 {
|
||||
if this.color_type == 0 {
|
||||
if (this.depth <> 1) and (this.depth <> 2) and (this.depth <> 4) {
|
||||
return nothing
|
||||
} else if this.color_type == 0 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__Y
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__Y
|
||||
} else if this.color_type == 3 {
|
||||
@@ -607,33 +620,78 @@ pri func decoder.decode_srgb?(src: base.io_reader) {
|
||||
}
|
||||
|
||||
pri func decoder.decode_trns?(src: base.io_reader) {
|
||||
var num_entries : base.u32[..= 256]
|
||||
var i : base.u32
|
||||
var i : base.u32
|
||||
var n : base.u32[..= 256]
|
||||
var u : base.u64
|
||||
|
||||
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 == 3 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL
|
||||
} else {
|
||||
if this.color_type == 0 {
|
||||
choose filter_and_swizzle = [filter_and_swizzle_tricky]
|
||||
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
|
||||
} else if this.color_type == 2 {
|
||||
if this.depth <= 8 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
} else {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
}
|
||||
if this.chunk_length <> 2 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length = 0
|
||||
|
||||
u = args.src.read_u16be_as_u64?()
|
||||
if this.depth <= 1 {
|
||||
this.remap_transparency = ((u & 0x01) * 0xFF_FFFF) | 0xFF00_0000
|
||||
} else if this.depth <= 2 {
|
||||
this.remap_transparency = ((u & 0x03) * 0x55_5555) | 0xFF00_0000
|
||||
} else if this.depth <= 4 {
|
||||
this.remap_transparency = ((u & 0x0F) * 0x11_1111) | 0xFF00_0000
|
||||
} else if this.depth <= 8 {
|
||||
this.remap_transparency = ((u & 0xFF) * 0x01_0101) | 0xFF00_0000
|
||||
} else {
|
||||
this.remap_transparency = (u * 0x0001_0001_0001) | 0xFFFF_0000_0000_0000
|
||||
}
|
||||
|
||||
} else if this.color_type == 2 {
|
||||
choose filter_and_swizzle = [filter_and_swizzle_tricky]
|
||||
if this.depth <= 8 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
} else {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
}
|
||||
if this.chunk_length <> 6 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length = 0
|
||||
|
||||
u = args.src.read_u48be_as_u64?()
|
||||
if this.depth <= 8 {
|
||||
this.remap_transparency =
|
||||
(0x0000_00FF & (u >> 0)) |
|
||||
(0x0000_FF00 & (u >> 8)) |
|
||||
(0x00FF_0000 & (u >> 16)) |
|
||||
0xFF00_0000
|
||||
} else {
|
||||
this.remap_transparency = u | 0xFFFF_0000_0000_0000
|
||||
}
|
||||
|
||||
} else if this.color_type == 3 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL
|
||||
if this.chunk_length > 256 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
n = this.chunk_length as base.u32
|
||||
this.chunk_length = 0
|
||||
while i < n {
|
||||
assert i < 256 via "a < b: a < c; c <= b"(c: n)
|
||||
this.src_palette[(4 * i) + 3] = args.src.read_u8?()
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
} else {
|
||||
return "#bad chunk"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -55,15 +55,6 @@ pri func decoder.filter_and_swizzle_tricky!(dst: ptr base.pixel_buffer, workbuf:
|
||||
((this.depth >> 3) as base.u64)
|
||||
}
|
||||
|
||||
bits_unpacked[0] = 0xFF
|
||||
bits_unpacked[1] = 0xFF
|
||||
bits_unpacked[2] = 0xFF
|
||||
bits_unpacked[3] = 0xFF
|
||||
bits_unpacked[4] = 0xFF
|
||||
bits_unpacked[5] = 0xFF
|
||||
bits_unpacked[6] = 0xFF
|
||||
bits_unpacked[7] = 0xFF
|
||||
|
||||
y = INTERLACING[this.interlace_pass][5] as base.u32
|
||||
while y < this.height {
|
||||
assert y < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.height)
|
||||
@@ -118,6 +109,54 @@ pri func decoder.filter_and_swizzle_tricky!(dst: ptr base.pixel_buffer, workbuf:
|
||||
dst_palette: dst_palette,
|
||||
src: bits_unpacked[.. 4])
|
||||
}
|
||||
|
||||
} else if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) <> 0 {
|
||||
if this.color_type == 0 {
|
||||
if 1 <= s.length() {
|
||||
bits_unpacked[0] = s[0]
|
||||
bits_unpacked[1] = s[0]
|
||||
bits_unpacked[2] = s[0]
|
||||
bits_unpacked[3] = 0xFF
|
||||
s = s[1 ..]
|
||||
if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) == (
|
||||
((bits_unpacked[0] as base.u32) << 0) |
|
||||
((bits_unpacked[1] as base.u32) << 8) |
|
||||
((bits_unpacked[2] as base.u32) << 16) |
|
||||
((bits_unpacked[3] as base.u32) << 24)) {
|
||||
bits_unpacked[0] = 0
|
||||
bits_unpacked[1] = 0
|
||||
bits_unpacked[2] = 0
|
||||
bits_unpacked[3] = 0
|
||||
}
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: bits_unpacked[.. 4])
|
||||
}
|
||||
} else {
|
||||
if 3 <= s.length() {
|
||||
bits_unpacked[0] = s[2]
|
||||
bits_unpacked[1] = s[1]
|
||||
bits_unpacked[2] = s[0]
|
||||
bits_unpacked[3] = 0xFF
|
||||
s = s[3 ..]
|
||||
if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) == (
|
||||
((bits_unpacked[0] as base.u32) << 0) |
|
||||
((bits_unpacked[1] as base.u32) << 8) |
|
||||
((bits_unpacked[2] as base.u32) << 16) |
|
||||
((bits_unpacked[3] as base.u32) << 24)) {
|
||||
bits_unpacked[0] = 0
|
||||
bits_unpacked[1] = 0
|
||||
bits_unpacked[2] = 0
|
||||
bits_unpacked[3] = 0
|
||||
}
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: bits_unpacked[.. 4])
|
||||
}
|
||||
}
|
||||
|
||||
} else if src_bytes_per_pixel <= s.length() {
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
@@ -153,10 +192,31 @@ pri func decoder.filter_and_swizzle_tricky!(dst: ptr base.pixel_buffer, workbuf:
|
||||
bits_packed = bits_packed ~mod<< this.depth
|
||||
packs_remaining = packs_remaining ~mod- 1
|
||||
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: bits_unpacked[.. 1])
|
||||
if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) <> 0 {
|
||||
bits_unpacked[1] = bits_unpacked[0]
|
||||
bits_unpacked[2] = bits_unpacked[0]
|
||||
bits_unpacked[3] = 0xFF
|
||||
if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) == (
|
||||
((bits_unpacked[0] as base.u32) << 0) |
|
||||
((bits_unpacked[1] as base.u32) << 8) |
|
||||
((bits_unpacked[2] as base.u32) << 16) |
|
||||
((bits_unpacked[3] as base.u32) << 24)) {
|
||||
bits_unpacked[0] = 0
|
||||
bits_unpacked[1] = 0
|
||||
bits_unpacked[2] = 0
|
||||
bits_unpacked[3] = 0
|
||||
}
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: bits_unpacked[.. 4])
|
||||
|
||||
} else {
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: bits_unpacked[.. 1])
|
||||
}
|
||||
}
|
||||
x += (1 as base.u32) << INTERLACING[this.interlace_pass][0]
|
||||
} endwhile
|
||||
@@ -176,8 +236,29 @@ pri func decoder.filter_and_swizzle_tricky!(dst: ptr base.pixel_buffer, workbuf:
|
||||
bits_unpacked[3] = s[0]
|
||||
bits_unpacked[4] = s[1]
|
||||
bits_unpacked[5] = s[0]
|
||||
bits_unpacked[6] = 0xFF
|
||||
bits_unpacked[7] = 0xFF
|
||||
s = s[2 ..]
|
||||
if this.remap_transparency == (
|
||||
((bits_unpacked[0] as base.u64) << 0) |
|
||||
((bits_unpacked[1] as base.u64) << 8) |
|
||||
((bits_unpacked[2] as base.u64) << 16) |
|
||||
((bits_unpacked[3] as base.u64) << 24) |
|
||||
((bits_unpacked[4] as base.u64) << 32) |
|
||||
((bits_unpacked[5] as base.u64) << 40) |
|
||||
((bits_unpacked[6] as base.u64) << 48) |
|
||||
((bits_unpacked[7] as base.u64) << 56)) {
|
||||
bits_unpacked[0] = 0
|
||||
bits_unpacked[1] = 0
|
||||
bits_unpacked[2] = 0
|
||||
bits_unpacked[3] = 0
|
||||
bits_unpacked[4] = 0
|
||||
bits_unpacked[5] = 0
|
||||
bits_unpacked[6] = 0
|
||||
bits_unpacked[7] = 0
|
||||
}
|
||||
}
|
||||
|
||||
} else if this.color_type == 2 {
|
||||
if 6 <= s.length() {
|
||||
bits_unpacked[0] = s[5]
|
||||
@@ -186,8 +267,29 @@ pri func decoder.filter_and_swizzle_tricky!(dst: ptr base.pixel_buffer, workbuf:
|
||||
bits_unpacked[3] = s[2]
|
||||
bits_unpacked[4] = s[1]
|
||||
bits_unpacked[5] = s[0]
|
||||
bits_unpacked[6] = 0xFF
|
||||
bits_unpacked[7] = 0xFF
|
||||
s = s[6 ..]
|
||||
if this.remap_transparency == (
|
||||
((bits_unpacked[0] as base.u64) << 0) |
|
||||
((bits_unpacked[1] as base.u64) << 8) |
|
||||
((bits_unpacked[2] as base.u64) << 16) |
|
||||
((bits_unpacked[3] as base.u64) << 24) |
|
||||
((bits_unpacked[4] as base.u64) << 32) |
|
||||
((bits_unpacked[5] as base.u64) << 40) |
|
||||
((bits_unpacked[6] as base.u64) << 48) |
|
||||
((bits_unpacked[7] as base.u64) << 56)) {
|
||||
bits_unpacked[0] = 0
|
||||
bits_unpacked[1] = 0
|
||||
bits_unpacked[2] = 0
|
||||
bits_unpacked[3] = 0
|
||||
bits_unpacked[4] = 0
|
||||
bits_unpacked[5] = 0
|
||||
bits_unpacked[6] = 0
|
||||
bits_unpacked[7] = 0
|
||||
}
|
||||
}
|
||||
|
||||
} else if this.color_type == 4 {
|
||||
if 4 <= s.length() {
|
||||
bits_unpacked[0] = s[1]
|
||||
@@ -200,6 +302,7 @@ pri func decoder.filter_and_swizzle_tricky!(dst: ptr base.pixel_buffer, workbuf:
|
||||
bits_unpacked[7] = s[2]
|
||||
s = s[4 ..]
|
||||
}
|
||||
|
||||
} else {
|
||||
if 8 <= s.length() {
|
||||
bits_unpacked[0] = s[5]
|
||||
|
||||
Reference in new issue
Block a user