Have std/tga decode RLE images

Updates #67
This commit is contained in:
Nigel Tao committed 2022-01-15 14:10:05 +11:00
1 parent fc77dc35ff
commit f4002c5b21
3 files changed
+285 -20

No files matched your search

+136 -8
View File
@@ -9641,6 +9641,7 @@ struct wuffs_png__decoder__struct {
// ---------------- Status Codes // ---------------- Status Codes
extern const char wuffs_tga__error__bad_header[]; extern const char wuffs_tga__error__bad_header[];
extern const char wuffs_tga__error__bad_run_length_encoding[];
extern const char wuffs_tga__error__unsupported_tga_file[]; extern const char wuffs_tga__error__unsupported_tga_file[];
// ---------------- Public Consts // ---------------- Public Consts
@@ -9806,6 +9807,7 @@ struct wuffs_tga__decoder__struct {
uint8_t f_header_pixel_depth; uint8_t f_header_pixel_depth;
uint8_t f_header_image_descriptor; uint8_t f_header_image_descriptor;
bool f_opaque; bool f_opaque;
uint32_t f_scratch_bytes_per_pixel;
uint32_t f_src_bytes_per_pixel; uint32_t f_src_bytes_per_pixel;
uint32_t f_src_pixfmt; uint32_t f_src_pixfmt;
uint64_t f_frame_config_io_position; uint64_t f_frame_config_io_position;
@@ -9832,6 +9834,7 @@ struct wuffs_tga__decoder__struct {
uint64_t v_mark; uint64_t v_mark;
uint32_t v_num_pixels32; uint32_t v_num_pixels32;
uint32_t v_lit_length; uint32_t v_lit_length;
uint32_t v_run_length;
uint64_t v_num_dst_bytes; uint64_t v_num_dst_bytes;
uint64_t scratch; uint64_t scratch;
} s_decode_frame[1]; } s_decode_frame[1];
@@ -41982,6 +41985,7 @@ wuffs_png__decoder__filter_and_swizzle_tricky(
// ---------------- Status Codes Implementations // ---------------- Status Codes Implementations
const char wuffs_tga__error__bad_header[] = "#tga: bad header"; const char wuffs_tga__error__bad_header[] = "#tga: bad header";
const char wuffs_tga__error__bad_run_length_encoding[] = "#tga: bad run length encoding";
const char wuffs_tga__error__unsupported_tga_file[] = "#tga: unsupported TGA file"; const char wuffs_tga__error__unsupported_tga_file[] = "#tga: unsupported TGA file";
// ---------------- Private Consts // ---------------- Private Consts
@@ -42192,10 +42196,12 @@ wuffs_tga__decoder__decode_image_config(
uint8_t t_2 = *iop_a_src++; uint8_t t_2 = *iop_a_src++;
self->private_impl.f_header_image_type = t_2; self->private_impl.f_header_image_type = t_2;
} }
if ((self->private_impl.f_header_image_type == 1) || (self->private_impl.f_header_image_type == 2) || (self->private_impl.f_header_image_type == 3)) { if ((self->private_impl.f_header_image_type == 1) ||
} else if ((self->private_impl.f_header_image_type == 9) || (self->private_impl.f_header_image_type == 10) || (self->private_impl.f_header_image_type == 11)) { (self->private_impl.f_header_image_type == 2) ||
status = wuffs_base__make_status(wuffs_tga__error__unsupported_tga_file); (self->private_impl.f_header_image_type == 3) ||
goto exit; (self->private_impl.f_header_image_type == 9) ||
(self->private_impl.f_header_image_type == 10) ||
(self->private_impl.f_header_image_type == 11)) {
} else { } else {
status = wuffs_base__make_status(wuffs_tga__error__bad_header); status = wuffs_base__make_status(wuffs_tga__error__bad_header);
goto exit; goto exit;
@@ -42423,17 +42429,22 @@ wuffs_tga__decoder__decode_image_config(
goto exit; goto exit;
} }
if ((self->private_impl.f_header_image_type | 8) == 9) { if ((self->private_impl.f_header_image_type | 8) == 9) {
self->private_impl.f_scratch_bytes_per_pixel = 1;
self->private_impl.f_src_bytes_per_pixel = 1; self->private_impl.f_src_bytes_per_pixel = 1;
self->private_impl.f_src_pixfmt = 2164523016; self->private_impl.f_src_pixfmt = 2164523016;
self->private_impl.f_opaque = ((self->private_impl.f_header_color_map_entry_size != 15) || (self->private_impl.f_header_color_map_entry_size != 24)); self->private_impl.f_opaque = ((self->private_impl.f_header_color_map_entry_size != 15) || (self->private_impl.f_header_color_map_entry_size != 24));
} else if ((self->private_impl.f_header_image_type | 8) == 10) { } else if ((self->private_impl.f_header_image_type | 8) == 10) {
if (self->private_impl.f_header_pixel_depth == 16) { if ((self->private_impl.f_header_pixel_depth == 15) || (self->private_impl.f_header_pixel_depth == 16)) {
self->private_impl.f_scratch_bytes_per_pixel = 4;
self->private_impl.f_src_bytes_per_pixel = 0;
self->private_impl.f_src_pixfmt = 2164295816; self->private_impl.f_src_pixfmt = 2164295816;
} else if (self->private_impl.f_header_pixel_depth == 24) { } else if (self->private_impl.f_header_pixel_depth == 24) {
self->private_impl.f_scratch_bytes_per_pixel = 3;
self->private_impl.f_src_bytes_per_pixel = 3; self->private_impl.f_src_bytes_per_pixel = 3;
self->private_impl.f_src_pixfmt = 2147485832; self->private_impl.f_src_pixfmt = 2147485832;
self->private_impl.f_opaque = true; self->private_impl.f_opaque = true;
} else if (self->private_impl.f_header_pixel_depth == 32) { } else if (self->private_impl.f_header_pixel_depth == 32) {
self->private_impl.f_scratch_bytes_per_pixel = 4;
self->private_impl.f_src_bytes_per_pixel = 4; self->private_impl.f_src_bytes_per_pixel = 4;
self->private_impl.f_src_pixfmt = 2164295816; self->private_impl.f_src_pixfmt = 2164295816;
} else { } else {
@@ -42442,6 +42453,7 @@ wuffs_tga__decoder__decode_image_config(
} }
} else { } else {
if (self->private_impl.f_header_pixel_depth == 8) { if (self->private_impl.f_header_pixel_depth == 8) {
self->private_impl.f_scratch_bytes_per_pixel = 1;
self->private_impl.f_src_bytes_per_pixel = 1; self->private_impl.f_src_bytes_per_pixel = 1;
self->private_impl.f_src_pixfmt = 536870920; self->private_impl.f_src_pixfmt = 536870920;
self->private_impl.f_opaque = true; self->private_impl.f_opaque = true;
@@ -42781,6 +42793,7 @@ wuffs_tga__decoder__decode_frame(
uint64_t v_num_pixels64 = 0; uint64_t v_num_pixels64 = 0;
uint32_t v_num_pixels32 = 0; uint32_t v_num_pixels32 = 0;
uint32_t v_lit_length = 0; uint32_t v_lit_length = 0;
uint32_t v_run_length = 0;
uint64_t v_num_dst_bytes = 0; uint64_t v_num_dst_bytes = 0;
uint32_t v_num_src_bytes = 0; uint32_t v_num_src_bytes = 0;
uint32_t v_c = 0; uint32_t v_c = 0;
@@ -42805,6 +42818,7 @@ wuffs_tga__decoder__decode_frame(
v_mark = self->private_data.s_decode_frame[0].v_mark; v_mark = self->private_data.s_decode_frame[0].v_mark;
v_num_pixels32 = self->private_data.s_decode_frame[0].v_num_pixels32; v_num_pixels32 = self->private_data.s_decode_frame[0].v_num_pixels32;
v_lit_length = self->private_data.s_decode_frame[0].v_lit_length; v_lit_length = self->private_data.s_decode_frame[0].v_lit_length;
v_run_length = self->private_data.s_decode_frame[0].v_run_length;
v_num_dst_bytes = self->private_data.s_decode_frame[0].v_num_dst_bytes; v_num_dst_bytes = self->private_data.s_decode_frame[0].v_num_dst_bytes;
} }
switch (coro_susp_point) { switch (coro_susp_point) {
@@ -42856,7 +42870,9 @@ wuffs_tga__decoder__decode_frame(
if ((self->private_impl.f_header_image_descriptor & 32) == 0) { if ((self->private_impl.f_header_image_descriptor & 32) == 0) {
v_dst_y = ((uint32_t)(self->private_impl.f_height - 1)); v_dst_y = ((uint32_t)(self->private_impl.f_height - 1));
} }
v_lit_length = self->private_impl.f_width; if ((self->private_impl.f_header_image_type & 8) == 0) {
v_lit_length = self->private_impl.f_width;
}
label__resume__continue:; label__resume__continue:;
while (true) { while (true) {
v_tab = wuffs_base__pixel_buffer__plane(a_dst, 0); v_tab = wuffs_base__pixel_buffer__plane(a_dst, 0);
@@ -42899,12 +42915,79 @@ wuffs_tga__decoder__decode_frame(
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(3); WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(3);
goto label__resume__continue; goto label__resume__continue;
} }
} else if (v_run_length > 0) {
v_run_length -= 1;
wuffs_base__pixel_swizzler__swizzle_interleaved_from_slice(&self->private_impl.f_swizzler, v_dst, v_dst_palette, wuffs_base__slice_u8__subslice_j(wuffs_base__make_slice_u8(self->private_data.f_scratch, 4), self->private_impl.f_scratch_bytes_per_pixel));
if (v_dst_bytes_per_pixel <= ((uint64_t)(v_dst.len))) {
v_dst = wuffs_base__slice_u8__subslice_i(v_dst, v_dst_bytes_per_pixel);
}
v_dst_x += 1;
} else {
if (((uint64_t)(io2_a_src - iop_a_src)) <= 0) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(4);
goto label__resume__continue;
}
if (((uint32_t)(wuffs_base__peek_u8be__no_bounds_check(iop_a_src))) < 128) {
v_lit_length = (((uint32_t)(wuffs_base__peek_u8be__no_bounds_check(iop_a_src))) + 1);
iop_a_src += 1;
if ((v_lit_length + v_dst_x) > self->private_impl.f_width) {
status = wuffs_base__make_status(wuffs_tga__error__bad_run_length_encoding);
goto exit;
}
} else {
if (self->private_impl.f_src_bytes_per_pixel == 1) {
if (((uint64_t)(io2_a_src - iop_a_src)) < 2) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(5);
goto label__resume__continue;
}
v_run_length = ((((uint32_t)(wuffs_base__peek_u8be__no_bounds_check(iop_a_src))) & 127) + 1);
iop_a_src += 1;
self->private_data.f_scratch[0] = wuffs_base__peek_u8be__no_bounds_check(iop_a_src);
iop_a_src += 1;
} else if (self->private_impl.f_src_bytes_per_pixel == 3) {
if (((uint64_t)(io2_a_src - iop_a_src)) < 4) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(6);
goto label__resume__continue;
}
v_run_length = ((((uint32_t)(wuffs_base__peek_u8be__no_bounds_check(iop_a_src))) & 127) + 1);
iop_a_src += 1;
self->private_data.f_scratch[0] = wuffs_base__peek_u8be__no_bounds_check(iop_a_src);
iop_a_src += 1;
self->private_data.f_scratch[1] = wuffs_base__peek_u8be__no_bounds_check(iop_a_src);
iop_a_src += 1;
self->private_data.f_scratch[2] = wuffs_base__peek_u8be__no_bounds_check(iop_a_src);
iop_a_src += 1;
} else {
if (((uint64_t)(io2_a_src - iop_a_src)) < 5) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(7);
goto label__resume__continue;
}
v_run_length = ((((uint32_t)(wuffs_base__peek_u8be__no_bounds_check(iop_a_src))) & 127) + 1);
iop_a_src += 1;
self->private_data.f_scratch[0] = wuffs_base__peek_u8be__no_bounds_check(iop_a_src);
iop_a_src += 1;
self->private_data.f_scratch[1] = wuffs_base__peek_u8be__no_bounds_check(iop_a_src);
iop_a_src += 1;
self->private_data.f_scratch[2] = wuffs_base__peek_u8be__no_bounds_check(iop_a_src);
iop_a_src += 1;
self->private_data.f_scratch[3] = wuffs_base__peek_u8be__no_bounds_check(iop_a_src);
iop_a_src += 1;
}
if ((v_run_length + v_dst_x) > self->private_impl.f_width) {
status = wuffs_base__make_status(wuffs_tga__error__bad_run_length_encoding);
goto exit;
}
}
} }
} else { } else {
if (v_lit_length > 0) { if (v_lit_length > 0) {
if (((uint64_t)(io2_a_src - iop_a_src)) < 2) { if (((uint64_t)(io2_a_src - iop_a_src)) < 2) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read); status = wuffs_base__make_status(wuffs_base__suspension__short_read);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(4); WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(8);
goto label__resume__continue; goto label__resume__continue;
} }
v_c = ((uint32_t)(wuffs_base__peek_u16le__no_bounds_check(iop_a_src))); v_c = ((uint32_t)(wuffs_base__peek_u16le__no_bounds_check(iop_a_src)));
@@ -42922,6 +43005,48 @@ wuffs_tga__decoder__decode_frame(
} }
v_dst_x += 1; v_dst_x += 1;
v_lit_length -= 1; v_lit_length -= 1;
} else if (v_run_length > 0) {
v_run_length -= 1;
wuffs_base__pixel_swizzler__swizzle_interleaved_from_slice(&self->private_impl.f_swizzler, v_dst, v_dst_palette, wuffs_base__slice_u8__subslice_j(wuffs_base__make_slice_u8(self->private_data.f_scratch, 4), self->private_impl.f_scratch_bytes_per_pixel));
if (v_dst_bytes_per_pixel <= ((uint64_t)(v_dst.len))) {
v_dst = wuffs_base__slice_u8__subslice_i(v_dst, v_dst_bytes_per_pixel);
}
v_dst_x += 1;
} else {
if (((uint64_t)(io2_a_src - iop_a_src)) <= 0) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(9);
goto label__resume__continue;
}
if (((uint32_t)(wuffs_base__peek_u8be__no_bounds_check(iop_a_src))) < 128) {
v_lit_length = (((uint32_t)(wuffs_base__peek_u8be__no_bounds_check(iop_a_src))) + 1);
iop_a_src += 1;
if ((v_lit_length + v_dst_x) > self->private_impl.f_width) {
status = wuffs_base__make_status(wuffs_tga__error__bad_run_length_encoding);
goto exit;
}
} else {
if (((uint64_t)(io2_a_src - iop_a_src)) < 3) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(10);
goto label__resume__continue;
}
v_run_length = ((((uint32_t)(wuffs_base__peek_u8be__no_bounds_check(iop_a_src))) & 127) + 1);
iop_a_src += 1;
v_c = ((uint32_t)(wuffs_base__peek_u16le__no_bounds_check(iop_a_src)));
iop_a_src += 2;
v_c5 = (31 & (v_c >> 0));
self->private_data.f_scratch[0] = ((uint8_t)(((v_c5 << 3) | (v_c5 >> 2))));
v_c5 = (31 & (v_c >> 5));
self->private_data.f_scratch[1] = ((uint8_t)(((v_c5 << 3) | (v_c5 >> 2))));
v_c5 = (31 & (v_c >> 10));
self->private_data.f_scratch[2] = ((uint8_t)(((v_c5 << 3) | (v_c5 >> 2))));
self->private_data.f_scratch[3] = 255;
if ((v_run_length + v_dst_x) > self->private_impl.f_width) {
status = wuffs_base__make_status(wuffs_tga__error__bad_run_length_encoding);
goto exit;
}
}
} }
} }
} }
@@ -42931,7 +43056,9 @@ wuffs_tga__decoder__decode_frame(
} else { } else {
v_dst_y += 1; v_dst_y += 1;
} }
v_lit_length = self->private_impl.f_width; if ((self->private_impl.f_header_image_type & 8) == 0) {
v_lit_length = self->private_impl.f_width;
}
} }
goto label__resume__break; goto label__resume__break;
} }
@@ -42953,6 +43080,7 @@ wuffs_tga__decoder__decode_frame(
self->private_data.s_decode_frame[0].v_mark = v_mark; self->private_data.s_decode_frame[0].v_mark = v_mark;
self->private_data.s_decode_frame[0].v_num_pixels32 = v_num_pixels32; self->private_data.s_decode_frame[0].v_num_pixels32 = v_num_pixels32;
self->private_data.s_decode_frame[0].v_lit_length = v_lit_length; self->private_data.s_decode_frame[0].v_lit_length = v_lit_length;
self->private_data.s_decode_frame[0].v_run_length = v_run_length;
self->private_data.s_decode_frame[0].v_num_dst_bytes = v_num_dst_bytes; self->private_data.s_decode_frame[0].v_num_dst_bytes = v_num_dst_bytes;
goto exit; goto exit;
+146 -12
View File
@@ -13,6 +13,7 @@
// limitations under the License. // limitations under the License.
pub status "#bad header" pub status "#bad header"
pub status "#bad run length encoding"
pub status "#unsupported TGA file" pub status "#unsupported TGA file"
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0 pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0
@@ -61,8 +62,9 @@ pub struct decoder? implements base.image_decoder(
opaque : base.bool, opaque : base.bool,
src_bytes_per_pixel : base.u32[..= 4], scratch_bytes_per_pixel : base.u32[..= 4],
src_pixfmt : base.u32, src_bytes_per_pixel : base.u32[..= 4],
src_pixfmt : base.u32,
frame_config_io_position : base.u64, frame_config_io_position : base.u64,
@@ -95,13 +97,11 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
this.header_image_type = args.src.read_u8?() this.header_image_type = args.src.read_u8?()
if (this.header_image_type == 0x01) or if (this.header_image_type == 0x01) or
(this.header_image_type == 0x02) or (this.header_image_type == 0x02) or
(this.header_image_type == 0x03) { (this.header_image_type == 0x03) or
// No-op. (this.header_image_type == 0x09) or
} else if (this.header_image_type == 0x09) or
(this.header_image_type == 0x0A) or (this.header_image_type == 0x0A) or
(this.header_image_type == 0x0B) { (this.header_image_type == 0x0B) {
// TODO: support run-length encoding. // No-op.
return "#unsupported TGA file"
} else { } else {
// TODO: 0x20 and 0x21 are invalid, according to the spec, but are // TODO: 0x20 and 0x21 are invalid, according to the spec, but are
// apparently unofficial extensions. // apparently unofficial extensions.
@@ -151,6 +151,7 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
} }
if (this.header_image_type | 8) == 0x09 { if (this.header_image_type | 8) == 0x09 {
this.scratch_bytes_per_pixel = 1
this.src_bytes_per_pixel = 1 this.src_bytes_per_pixel = 1
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL
this.opaque = this.opaque =
@@ -158,13 +159,20 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
(this.header_color_map_entry_size <> 0x18) (this.header_color_map_entry_size <> 0x18)
} else if (this.header_image_type | 8) == 0x0A { } else if (this.header_image_type | 8) == 0x0A {
if this.header_pixel_depth == 0x10 { if (this.header_pixel_depth == 0x0F) or
(this.header_pixel_depth == 0x10) {
// Wuffs' base.pixel_swizzler doesn't support BGRX5551, so
// scratch_bytes_per_pixel and src_bytes_per_pixel are different.
this.scratch_bytes_per_pixel = 4
this.src_bytes_per_pixel = 0
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
} else if this.header_pixel_depth == 0x18 { } else if this.header_pixel_depth == 0x18 {
this.scratch_bytes_per_pixel = 3
this.src_bytes_per_pixel = 3 this.src_bytes_per_pixel = 3
this.src_pixfmt = base.PIXEL_FORMAT__BGR this.src_pixfmt = base.PIXEL_FORMAT__BGR
this.opaque = true this.opaque = true
} else if this.header_pixel_depth == 0x20 { } else if this.header_pixel_depth == 0x20 {
this.scratch_bytes_per_pixel = 4
this.src_bytes_per_pixel = 4 this.src_bytes_per_pixel = 4
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
} else { } else {
@@ -173,6 +181,7 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
} else { } else {
if this.header_pixel_depth == 0x08 { if this.header_pixel_depth == 0x08 {
this.scratch_bytes_per_pixel = 1
this.src_bytes_per_pixel = 1 this.src_bytes_per_pixel = 1
this.src_pixfmt = base.PIXEL_FORMAT__Y this.src_pixfmt = base.PIXEL_FORMAT__Y
this.opaque = true this.opaque = true
@@ -292,6 +301,7 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
var num_pixels64 : base.u64 var num_pixels64 : base.u64
var num_pixels32 : base.u32[..= 0xFFFF] var num_pixels32 : base.u32[..= 0xFFFF]
var lit_length : base.u32[..= 0xFFFF] var lit_length : base.u32[..= 0xFFFF]
var run_length : base.u32[..= 0xFFFF]
var num_dst_bytes : base.u64[..= 0x1F_FFE0] var num_dst_bytes : base.u64[..= 0x1F_FFE0]
var num_src_bytes : base.u32[..= 0x3_FFFC] var num_src_bytes : base.u32[..= 0x3_FFFC]
var c : base.u32 var c : base.u32
@@ -330,7 +340,11 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
if (this.header_image_descriptor & 0x20) == 0 { // Bottom-to-top. if (this.header_image_descriptor & 0x20) == 0 { // Bottom-to-top.
dst_y = this.height ~mod- 1 dst_y = this.height ~mod- 1
} }
lit_length = this.width if (this.header_image_type & 8) == 0 {
// No RLE (run length encoding) means that the entire row is
// effectively literals.
lit_length = this.width
}
while.resume true { while.resume true {
tab = args.dst.plane(p: 0) tab = args.dst.plane(p: 0)
@@ -346,7 +360,7 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
} }
while dst_x < this.width { while dst_x < this.width {
assert dst_x < 0xFFFF_FFFF via "a < b: a < c; c <= b"(c: this.width) assert dst_x <= 0xFFFF via "a <= b: a <= c; c <= b"(c: this.width)
if this.src_bytes_per_pixel > 0 { if this.src_bytes_per_pixel > 0 {
if lit_length > 0 { if lit_length > 0 {
@@ -365,12 +379,81 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
} else { } else {
dst = this.util.empty_slice_u8() dst = this.util.empty_slice_u8()
} }
dst_x ~mod+= num_pixels32 dst_x += num_pixels32
lit_length = (lit_length ~mod- num_pixels32) & 0xFFFF lit_length = (lit_length ~mod- num_pixels32) & 0xFFFF
if lit_length > 0 { if lit_length > 0 {
yield? base."$short read" yield? base."$short read"
continue.resume continue.resume
} }
} else if run_length > 0 {
run_length -= 1
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst,
dst_palette: dst_palette,
src: this.scratch[.. this.scratch_bytes_per_pixel])
if dst_bytes_per_pixel <= dst.length() {
dst = dst[dst_bytes_per_pixel ..]
}
dst_x += 1
} else {
// Handle Raw vs RLE packets.
if args.src.length() <= 0 {
yield? base."$short read"
continue.resume
}
if args.src.peek_u8_as_u32() < 0x80 {
lit_length = args.src.peek_u8_as_u32() + 1
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
if (lit_length + dst_x) > this.width {
return "#bad run length encoding"
}
} else {
if this.src_bytes_per_pixel == 1 {
if args.src.length() < 2 {
yield? base."$short read"
continue.resume
}
run_length = (args.src.peek_u8_as_u32() & 0x7F) + 1
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
this.scratch[0] = args.src.peek_u8()
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
} else if this.src_bytes_per_pixel == 3 {
if args.src.length() < 4 {
yield? base."$short read"
continue.resume
}
run_length = (args.src.peek_u8_as_u32() & 0x7F) + 1
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
this.scratch[0] = args.src.peek_u8()
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
this.scratch[1] = args.src.peek_u8()
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
this.scratch[2] = args.src.peek_u8()
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
} else { // this.src_bytes_per_pixel == 4
if args.src.length() < 5 {
yield? base."$short read"
continue.resume
}
run_length = (args.src.peek_u8_as_u32() & 0x7F) + 1
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
this.scratch[0] = args.src.peek_u8()
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
this.scratch[1] = args.src.peek_u8()
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
this.scratch[2] = args.src.peek_u8()
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
this.scratch[3] = args.src.peek_u8()
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
}
if (run_length + dst_x) > this.width {
return "#bad run length encoding"
}
}
} }
} else { } else {
@@ -401,6 +484,53 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
} }
dst_x += 1 dst_x += 1
lit_length -= 1 lit_length -= 1
} else if run_length > 0 {
run_length -= 1
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst,
dst_palette: dst_palette,
src: this.scratch[.. this.scratch_bytes_per_pixel])
if dst_bytes_per_pixel <= dst.length() {
dst = dst[dst_bytes_per_pixel ..]
}
dst_x += 1
} else {
// Handle Raw vs RLE packets.
if args.src.length() <= 0 {
yield? base."$short read"
continue.resume
}
if args.src.peek_u8_as_u32() < 0x80 {
lit_length = args.src.peek_u8_as_u32() + 1
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
if (lit_length + dst_x) > this.width {
return "#bad run length encoding"
}
} else {
if args.src.length() < 3 {
yield? base."$short read"
continue.resume
}
run_length = (args.src.peek_u8_as_u32() & 0x7F) + 1
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
c = args.src.peek_u16le_as_u32()
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
c5 = 0x1F & (c >> 0)
this.scratch[0] = ((c5 << 3) | (c5 >> 2)) as base.u8
c5 = 0x1F & (c >> 5)
this.scratch[1] = ((c5 << 3) | (c5 >> 2)) as base.u8
c5 = 0x1F & (c >> 10)
this.scratch[2] = ((c5 << 3) | (c5 >> 2)) as base.u8
// TODO: can the alpha value be zero (BGRA5551 not BGRX5551)?
this.scratch[3] = 0xFF
if (run_length + dst_x) > this.width {
return "#bad run length encoding"
}
}
} }
} }
} endwhile } endwhile
@@ -411,7 +541,11 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
} else { // Top-to-bottom. } else { // Top-to-bottom.
dst_y ~mod+= 1 dst_y ~mod+= 1
} }
lit_length = this.width if (this.header_image_type & 8) == 0 {
// No RLE (run length encoding) means that the entire row is
// effectively literals.
lit_length = this.width
}
} endwhile } endwhile
break.resume break.resume
} endwhile.resume } endwhile.resume
@@ -1,4 +1,7 @@
# Generated by script/print-nia-checksums.sh # Generated by script/print-nia-checksums.sh
9fbe866a test/3pdata/tgasuite/cbw8.tga
5d75ef41 test/3pdata/tgasuite/ccm8.tga
5d75ef41 test/3pdata/tgasuite/ctc24.tga
9fbe866a test/3pdata/tgasuite/ubw8.tga 9fbe866a test/3pdata/tgasuite/ubw8.tga
5d75ef41 test/3pdata/tgasuite/ucm8.tga 5d75ef41 test/3pdata/tgasuite/ucm8.tga
5d75ef41 test/3pdata/tgasuite/utc16.tga 5d75ef41 test/3pdata/tgasuite/utc16.tga