Have std/tga decode color-mapped images

Updates #67
This commit is contained in:
Nigel Tao committed 2022-01-15 12:38:21 +11:00
1 parent 2c10bc32b0
commit c997066a2a
4 files changed
+200 -12

No files matched your search

+58 -6
View File
@@ -68,12 +68,19 @@ pub struct decoder? implements base.image_decoder(
swizzler : base.pixel_swizzler,
util : base.utility,
)(
dst_palette : array[4 * 256] base.u8,
src_palette : array[4 * 256] base.u8,
)
pub func decoder.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
}
pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_reader) {
var c : base.u32
var c5 : base.u32[..= 0x1F]
var i : base.u32
if this.call_sequence <> 0 {
return base."#bad call sequence"
}
@@ -143,8 +150,11 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
}
if (this.header_image_type | 8) == 0x09 {
// TODO: base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
this.src_bytes_per_pixel = 1
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL
this.opaque =
(this.header_color_map_entry_size <> 0x0F) or
(this.header_color_map_entry_size <> 0x18)
} else if (this.header_image_type | 8) == 0x0A {
if this.header_pixel_depth == 0x10 {
@@ -180,8 +190,43 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
// Skip the Image ID.
args.src.skip_u32?(n: this.header_id_length as base.u32)
// Read the color map.
if this.header_color_map_type <> 0 {
// TODO: read the color map.
while i < (this.header_color_map_length as base.u32) {
assert i <= 0xFFFF via "a <= b: a <= c; c <= b"(c: this.header_color_map_length as base.u32)
if this.header_color_map_entry_size == 0x18 {
c = args.src.read_u24le_as_u32?()
this.src_palette[((i & 0xFF) * 4) + 0] = ((c >> 0x00) & 0xFF) as base.u8
this.src_palette[((i & 0xFF) * 4) + 1] = ((c >> 0x08) & 0xFF) as base.u8
this.src_palette[((i & 0xFF) * 4) + 2] = ((c >> 0x10) & 0xFF) as base.u8
this.src_palette[((i & 0xFF) * 4) + 3] = 0xFF
} else if this.header_color_map_entry_size == 0x20 {
c = args.src.read_u32le?()
this.src_palette[((i & 0xFF) * 4) + 0] = ((c >> 0x00) & 0xFF) as base.u8
this.src_palette[((i & 0xFF) * 4) + 1] = ((c >> 0x08) & 0xFF) as base.u8
this.src_palette[((i & 0xFF) * 4) + 2] = ((c >> 0x10) & 0xFF) as base.u8
this.src_palette[((i & 0xFF) * 4) + 3] = ((c >> 0x18) & 0xFF) as base.u8
} else {
// Expand 15-bit or 16-bit color map entries.
c = args.src.read_u16le_as_u32?()
c5 = 0x1F & (c >> 0)
this.src_palette[((i & 0xFF) * 4) + 0] = ((c5 << 3) | (c5 >> 2)) as base.u8
c5 = 0x1F & (c >> 5)
this.src_palette[((i & 0xFF) * 4) + 1] = ((c5 << 3) | (c5 >> 2)) as base.u8
c5 = 0x1F & (c >> 10)
this.src_palette[((i & 0xFF) * 4) + 2] = ((c5 << 3) | (c5 >> 2)) as base.u8
// TODO: can the alpha value be zero (BGRA5551)?
this.src_palette[((i & 0xFF) * 4) + 3] = 0xFF
}
i += 1
} endwhile
while i < 0x100 {
this.src_palette[(i * 4) + 0] = 0x00
this.src_palette[(i * 4) + 1] = 0x00
this.src_palette[(i * 4) + 2] = 0x00
this.src_palette[(i * 4) + 3] = 0xFF
i += 1
} endwhile
}
this.frame_config_io_position = args.src.position()
@@ -239,8 +284,10 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
var dst_x : base.u32
var dst_y : base.u32
var tab : table base.u8
var dst_palette : slice base.u8
var dst : slice base.u8
var dst_start : base.u64
var src_palette : slice base.u8
var src : array[4] base.u8
var mark : base.u64
var num_pixels64 : base.u64
@@ -257,11 +304,14 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
return base."@end of data"
}
if this.header_color_map_type <> 0 {
src_palette = this.src_palette[..]
}
status = this.swizzler.prepare!(
dst_pixfmt: args.dst.pixel_format(),
dst_palette: args.dst.palette(),
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.util.empty_slice_u8(),
src_palette: src_palette,
blend: args.blend)
if not status.is_ok() {
return status
@@ -283,6 +333,8 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
while.resume true {
tab = args.dst.plane(p: 0)
dst_palette = args.dst.palette_or_else(fallback: this.dst_palette[..])
while dst_y < this.height {
dst = tab.row_u32(y: dst_y)
dst_start = (dst_x as base.u64) * dst_bytes_per_pixel
@@ -305,7 +357,7 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
args.src.skip_u32?(n: num_src_bytes)
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst,
dst_palette: args.dst.palette(),
dst_palette: dst_palette,
src: args.src.since(mark: mark))
if num_dst_bytes <= dst.length() {
dst = dst[num_dst_bytes ..]