std/jpeg: apply Inverse DCT

Updates #42
This commit is contained in:
Nigel Tao committed 2023-05-11 21:29:07 +10:00
1 parent 8cfe304f3c
commit 3bd83e3ebb
4 files changed
+486 -124

No files matched your search

+215 -51
View File
@@ -8581,6 +8581,7 @@ extern const char wuffs_jpeg__error__bad_sos_marker[];
extern const char wuffs_jpeg__error__bad_header[]; extern const char wuffs_jpeg__error__bad_header[];
extern const char wuffs_jpeg__error__bad_marker[]; extern const char wuffs_jpeg__error__bad_marker[];
extern const char wuffs_jpeg__error__missing_huffman_table[]; extern const char wuffs_jpeg__error__missing_huffman_table[];
extern const char wuffs_jpeg__error__missing_quantization_table[];
extern const char wuffs_jpeg__error__truncated_input[]; extern const char wuffs_jpeg__error__truncated_input[];
extern const char wuffs_jpeg__error__unsupported_dqt_after_sof_markers[]; extern const char wuffs_jpeg__error__unsupported_dqt_after_sof_markers[];
extern const char wuffs_jpeg__error__unsupported_arithmetic_coding[]; extern const char wuffs_jpeg__error__unsupported_arithmetic_coding[];
@@ -8595,7 +8596,7 @@ extern const char wuffs_jpeg__error__unsupported_precision[];
// ---------------- Public Consts // ---------------- Public Consts
#define WUFFS_JPEG__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE 0 #define WUFFS_JPEG__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE 274877906944
// ---------------- Struct Declarations // ---------------- Struct Declarations
@@ -8744,6 +8745,8 @@ struct wuffs_jpeg__decoder__struct {
uint32_t f_width; uint32_t f_width;
uint32_t f_height; uint32_t f_height;
uint32_t f_width_in_mcus;
uint32_t f_height_in_mcus;
uint8_t f_call_sequence; uint8_t f_call_sequence;
uint8_t f_sof_marker; uint8_t f_sof_marker;
uint8_t f_next_restart_marker; uint8_t f_next_restart_marker;
@@ -8756,6 +8759,8 @@ struct wuffs_jpeg__decoder__struct {
uint8_t f_components_tq[4]; uint8_t f_components_tq[4];
uint32_t f_components_width_in_blocks[4]; uint32_t f_components_width_in_blocks[4];
uint32_t f_components_height_in_blocks[4]; uint32_t f_components_height_in_blocks[4];
uint32_t f_components_workbuf_strides[4];
uint64_t f_components_workbuf_offsets[5];
uint32_t f_scan_num_components; uint32_t f_scan_num_components;
uint8_t f_scan_comps_cselector[4]; uint8_t f_scan_comps_cselector[4];
uint8_t f_scan_comps_td[4]; uint8_t f_scan_comps_td[4];
@@ -8766,6 +8771,8 @@ struct wuffs_jpeg__decoder__struct {
uint8_t f_scan_al; uint8_t f_scan_al;
uint32_t f_scan_width_in_mcus; uint32_t f_scan_width_in_mcus;
uint32_t f_scan_height_in_mcus; uint32_t f_scan_height_in_mcus;
uint8_t f_scan_comps_bx_offset[16];
uint8_t f_scan_comps_by_offset[16];
uint32_t f_mcu_num_blocks; uint32_t f_mcu_num_blocks;
uint32_t f_mcu_current_block; uint32_t f_mcu_current_block;
uint8_t f_mcu_blocks_sselector[16]; uint8_t f_mcu_blocks_sselector[16];
@@ -35378,6 +35385,7 @@ const char wuffs_jpeg__error__bad_sos_marker[] = "#jpeg: bad SOS marker";
const char wuffs_jpeg__error__bad_header[] = "#jpeg: bad header"; const char wuffs_jpeg__error__bad_header[] = "#jpeg: bad header";
const char wuffs_jpeg__error__bad_marker[] = "#jpeg: bad marker"; const char wuffs_jpeg__error__bad_marker[] = "#jpeg: bad marker";
const char wuffs_jpeg__error__missing_huffman_table[] = "#jpeg: missing Huffman table"; const char wuffs_jpeg__error__missing_huffman_table[] = "#jpeg: missing Huffman table";
const char wuffs_jpeg__error__missing_quantization_table[] = "#jpeg: missing Quantization table";
const char wuffs_jpeg__error__truncated_input[] = "#jpeg: truncated input"; const char wuffs_jpeg__error__truncated_input[] = "#jpeg: truncated input";
const char wuffs_jpeg__error__unsupported_dqt_after_sof_markers[] = "#jpeg: unsupported DQT after SOF markers"; const char wuffs_jpeg__error__unsupported_dqt_after_sof_markers[] = "#jpeg: unsupported DQT after SOF markers";
const char wuffs_jpeg__error__unsupported_arithmetic_coding[] = "#jpeg: unsupported arithmetic coding"; const char wuffs_jpeg__error__unsupported_arithmetic_coding[] = "#jpeg: unsupported arithmetic coding";
@@ -35621,7 +35629,13 @@ wuffs_jpeg__decoder__skip_past_the_next_restart_marker(
wuffs_base__io_buffer* a_src); wuffs_base__io_buffer* a_src);
static wuffs_base__status static wuffs_base__status
wuffs_jpeg__decoder__swizzle( wuffs_jpeg__decoder__swizzle_gray(
wuffs_jpeg__decoder* self,
wuffs_base__pixel_buffer* a_dst,
wuffs_base__slice_u8 a_workbuf);
static wuffs_base__status
wuffs_jpeg__decoder__swizzle_colorful(
wuffs_jpeg__decoder* self, wuffs_jpeg__decoder* self,
wuffs_base__pixel_buffer* a_dst); wuffs_base__pixel_buffer* a_dst);
@@ -36640,6 +36654,7 @@ wuffs_jpeg__decoder__do_decode_image_config(
uint8_t v_c = 0; uint8_t v_c = 0;
uint8_t v_marker = 0; uint8_t v_marker = 0;
uint32_t v_pixfmt = 0;
const uint8_t* iop_a_src = NULL; const uint8_t* iop_a_src = NULL;
const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
@@ -36858,9 +36873,13 @@ wuffs_jpeg__decoder__do_decode_image_config(
label__0__break:; label__0__break:;
self->private_impl.f_frame_config_io_position = wuffs_base__u64__sat_add((a_src ? a_src->meta.pos : 0), ((uint64_t)(iop_a_src - io0_a_src))); self->private_impl.f_frame_config_io_position = wuffs_base__u64__sat_add((a_src ? a_src->meta.pos : 0), ((uint64_t)(iop_a_src - io0_a_src)));
if (a_dst != NULL) { if (a_dst != NULL) {
v_pixfmt = 536870920;
if (self->private_impl.f_num_components > 1) {
v_pixfmt = 2415954056;
}
wuffs_base__image_config__set( wuffs_base__image_config__set(
a_dst, a_dst,
2415954056, v_pixfmt,
0, 0,
self->private_impl.f_width, self->private_impl.f_width,
self->private_impl.f_height, self->private_impl.f_height,
@@ -37078,6 +37097,10 @@ wuffs_jpeg__decoder__decode_sof(
bool v_has_h3 = false; bool v_has_h3 = false;
bool v_has_v24 = false; bool v_has_v24 = false;
bool v_has_v3 = false; bool v_has_v3 = false;
uint64_t v_workbuf_fragment_length0 = 0;
uint64_t v_workbuf_fragment_length1 = 0;
uint64_t v_workbuf_fragment_length2 = 0;
uint64_t v_workbuf_fragment_length3 = 0;
const uint8_t* iop_a_src = NULL; const uint8_t* iop_a_src = NULL;
const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
@@ -37253,7 +37276,7 @@ wuffs_jpeg__decoder__decode_sof(
uint8_t t_6 = *iop_a_src++; uint8_t t_6 = *iop_a_src++;
v_c = t_6; v_c = t_6;
} }
if (v_c > 4) { if (v_c >= 4) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_sof_marker); status = wuffs_base__make_status(wuffs_jpeg__error__bad_sof_marker);
goto exit; goto exit;
} }
@@ -37268,21 +37291,28 @@ wuffs_jpeg__decoder__decode_sof(
} }
v_i += 1; v_i += 1;
} }
v_has_h24 = false; if (self->private_impl.f_num_components == 1) {
v_has_h3 = false; self->private_impl.f_max_incl_components_h = 1;
v_has_v24 = false; self->private_impl.f_max_incl_components_v = 1;
v_has_v3 = false; self->private_impl.f_components_h[0] = 1;
v_i = 0; self->private_impl.f_components_v[0] = 1;
while (v_i < self->private_impl.f_num_components) { } else {
v_has_h24 = (v_has_h24 || (self->private_impl.f_components_h[v_i] == 2) || (self->private_impl.f_components_h[v_i] == 4)); v_has_h24 = false;
v_has_h3 = (v_has_h3 || (self->private_impl.f_components_h[v_i] == 3)); v_has_h3 = false;
v_has_v24 = (v_has_v24 || (self->private_impl.f_components_v[v_i] == 2) || (self->private_impl.f_components_v[v_i] == 4)); v_has_v24 = false;
v_has_v3 = (v_has_v3 || (self->private_impl.f_components_v[v_i] == 3)); v_has_v3 = false;
v_i += 1; v_i = 0;
} while (v_i < self->private_impl.f_num_components) {
if ((v_has_h24 && v_has_h3) || (v_has_v24 && v_has_v3)) { v_has_h24 = (v_has_h24 || (self->private_impl.f_components_h[v_i] == 2) || (self->private_impl.f_components_h[v_i] == 4));
status = wuffs_base__make_status(wuffs_jpeg__error__unsupported_fractional_sampling); v_has_h3 = (v_has_h3 || (self->private_impl.f_components_h[v_i] == 3));
goto exit; v_has_v24 = (v_has_v24 || (self->private_impl.f_components_v[v_i] == 2) || (self->private_impl.f_components_v[v_i] == 4));
v_has_v3 = (v_has_v3 || (self->private_impl.f_components_v[v_i] == 3));
v_i += 1;
}
if ((v_has_h24 && v_has_h3) || (v_has_v24 && v_has_v3)) {
status = wuffs_base__make_status(wuffs_jpeg__error__unsupported_fractional_sampling);
goto exit;
}
} }
v_i = 0; v_i = 0;
while (v_i < self->private_impl.f_num_components) { while (v_i < self->private_impl.f_num_components) {
@@ -37306,6 +37336,49 @@ wuffs_jpeg__decoder__decode_sof(
} }
v_i += 1; v_i += 1;
} }
if (self->private_impl.f_max_incl_components_h == 1) {
self->private_impl.f_width_in_mcus = ((self->private_impl.f_width + 7) / 8);
} else if (self->private_impl.f_max_incl_components_h == 2) {
self->private_impl.f_width_in_mcus = ((self->private_impl.f_width + 15) / 16);
} else if (self->private_impl.f_max_incl_components_h == 3) {
self->private_impl.f_width_in_mcus = ((self->private_impl.f_width + 23) / 24);
} else {
self->private_impl.f_width_in_mcus = ((self->private_impl.f_width + 31) / 32);
}
if (self->private_impl.f_max_incl_components_v == 1) {
self->private_impl.f_height_in_mcus = ((self->private_impl.f_height + 7) / 8);
} else if (self->private_impl.f_max_incl_components_v == 2) {
self->private_impl.f_height_in_mcus = ((self->private_impl.f_height + 15) / 16);
} else if (self->private_impl.f_max_incl_components_v == 3) {
self->private_impl.f_height_in_mcus = ((self->private_impl.f_height + 23) / 24);
} else {
self->private_impl.f_height_in_mcus = ((self->private_impl.f_height + 31) / 32);
}
self->private_impl.f_components_workbuf_strides[0] = (8 * ((uint32_t)(self->private_impl.f_width_in_mcus)) * ((uint32_t)(self->private_impl.f_components_h[0])));
self->private_impl.f_components_workbuf_strides[1] = (8 * ((uint32_t)(self->private_impl.f_width_in_mcus)) * ((uint32_t)(self->private_impl.f_components_h[1])));
self->private_impl.f_components_workbuf_strides[2] = (8 * ((uint32_t)(self->private_impl.f_width_in_mcus)) * ((uint32_t)(self->private_impl.f_components_h[2])));
self->private_impl.f_components_workbuf_strides[3] = (8 * ((uint32_t)(self->private_impl.f_width_in_mcus)) * ((uint32_t)(self->private_impl.f_components_h[3])));
v_workbuf_fragment_length0 = (8 *
((uint64_t)(self->private_impl.f_components_workbuf_strides[0])) *
((uint64_t)(self->private_impl.f_height_in_mcus)) *
((uint64_t)(self->private_impl.f_components_v[0])));
v_workbuf_fragment_length1 = (8 *
((uint64_t)(self->private_impl.f_components_workbuf_strides[1])) *
((uint64_t)(self->private_impl.f_height_in_mcus)) *
((uint64_t)(self->private_impl.f_components_v[1])));
v_workbuf_fragment_length2 = (8 *
((uint64_t)(self->private_impl.f_components_workbuf_strides[2])) *
((uint64_t)(self->private_impl.f_height_in_mcus)) *
((uint64_t)(self->private_impl.f_components_v[2])));
v_workbuf_fragment_length3 = (8 *
((uint64_t)(self->private_impl.f_components_workbuf_strides[3])) *
((uint64_t)(self->private_impl.f_height_in_mcus)) *
((uint64_t)(self->private_impl.f_components_v[3])));
self->private_impl.f_components_workbuf_offsets[0] = 0;
self->private_impl.f_components_workbuf_offsets[1] = (self->private_impl.f_components_workbuf_offsets[0] + v_workbuf_fragment_length0);
self->private_impl.f_components_workbuf_offsets[2] = (self->private_impl.f_components_workbuf_offsets[1] + v_workbuf_fragment_length1);
self->private_impl.f_components_workbuf_offsets[3] = (self->private_impl.f_components_workbuf_offsets[2] + v_workbuf_fragment_length2);
self->private_impl.f_components_workbuf_offsets[4] = (self->private_impl.f_components_workbuf_offsets[3] + v_workbuf_fragment_length3);
goto ok; goto ok;
ok: ok:
@@ -37563,6 +37636,7 @@ wuffs_jpeg__decoder__do_decode_frame(
wuffs_base__decode_frame_options* a_opts) { wuffs_base__decode_frame_options* a_opts) {
wuffs_base__status status = wuffs_base__make_status(NULL); wuffs_base__status status = wuffs_base__make_status(NULL);
uint32_t v_pixfmt = 0;
wuffs_base__status v_status = wuffs_base__make_status(NULL); wuffs_base__status v_status = wuffs_base__make_status(NULL);
uint8_t v_c = 0; uint8_t v_c = 0;
uint8_t v_marker = 0; uint8_t v_marker = 0;
@@ -37602,10 +37676,14 @@ wuffs_jpeg__decoder__do_decode_frame(
status = wuffs_base__make_status(wuffs_base__note__end_of_data); status = wuffs_base__make_status(wuffs_base__note__end_of_data);
goto ok; goto ok;
} }
v_pixfmt = 536870920;
if (self->private_impl.f_num_components > 1) {
v_pixfmt = 2415954056;
}
v_status = wuffs_base__pixel_swizzler__prepare(&self->private_impl.f_swizzler, v_status = wuffs_base__pixel_swizzler__prepare(&self->private_impl.f_swizzler,
wuffs_base__pixel_buffer__pixel_format(a_dst), wuffs_base__pixel_buffer__pixel_format(a_dst),
wuffs_base__pixel_buffer__palette_or_else(a_dst, wuffs_base__make_slice_u8(self->private_data.f_dst_palette, 1024)), wuffs_base__pixel_buffer__palette_or_else(a_dst, wuffs_base__make_slice_u8(self->private_data.f_dst_palette, 1024)),
wuffs_base__utility__make_pixel_format(2415954056), wuffs_base__utility__make_pixel_format(v_pixfmt),
wuffs_base__utility__empty_slice_u8(), wuffs_base__utility__empty_slice_u8(),
a_blend); a_blend);
if ( ! wuffs_base__status__is_ok(&v_status)) { if ( ! wuffs_base__status__is_ok(&v_status)) {
@@ -38069,8 +38147,13 @@ wuffs_jpeg__decoder__decode_sos(
uint32_t v_mx = 0; uint32_t v_mx = 0;
uint32_t v_decode_mcu_result = 0; uint32_t v_decode_mcu_result = 0;
uint32_t v_bitstream_length = 0; uint32_t v_bitstream_length = 0;
uint32_t v_b = 0;
uint32_t v_i = 0; uint32_t v_i = 0;
uint32_t v_j = 0; uint8_t v_csel = 0;
uint64_t v_h = 0;
uint64_t v_v = 0;
uint64_t v_stride = 0;
uint64_t v_offset = 0;
wuffs_base__status v_status = wuffs_base__make_status(NULL); wuffs_base__status v_status = wuffs_base__make_status(NULL);
uint32_t coro_susp_point = self->private_impl.p_decode_sos[0]; uint32_t coro_susp_point = self->private_impl.p_decode_sos[0];
@@ -38081,6 +38164,10 @@ wuffs_jpeg__decoder__decode_sos(
switch (coro_susp_point) { switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
if (((uint64_t)(a_workbuf.len)) < self->private_impl.f_components_workbuf_offsets[4]) {
status = wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
goto exit;
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
status = wuffs_jpeg__decoder__prepare_scan(self, a_src); status = wuffs_jpeg__decoder__prepare_scan(self, a_src);
if (status.repr) { if (status.repr) {
@@ -38123,14 +38210,30 @@ wuffs_jpeg__decoder__decode_sos(
label__fill_bitstream__break:; label__fill_bitstream__break:;
} }
label__decode_mcu__break:; label__decode_mcu__break:;
v_i = 0; v_b = 0;
v_j = 0; while (v_b < self->private_impl.f_mcu_num_blocks) {
while (v_i < self->private_impl.f_mcu_num_blocks) { v_csel = self->private_impl.f_scan_comps_cselector[self->private_impl.f_mcu_blocks_sselector[v_b]];
while (v_j < 64) { v_h = ((uint64_t)(self->private_impl.f_components_h[v_csel]));
self->private_data.f_mcu_blocks[v_i][v_j] = 0; v_v = ((uint64_t)(self->private_impl.f_components_v[v_csel]));
v_j += 1; v_stride = ((uint64_t)(self->private_impl.f_components_workbuf_strides[v_csel]));
v_offset = (self->private_impl.f_components_workbuf_offsets[v_csel] + (8 * (((v_h * ((uint64_t)(v_mx))) + ((uint64_t)(self->private_impl.f_scan_comps_bx_offset[v_b]))) + (((v_v * ((uint64_t)(v_my))) + ((uint64_t)(self->private_impl.f_scan_comps_by_offset[v_b]))) * v_stride))));
if (v_offset <= ((uint64_t)(a_workbuf.len))) {
wuffs_jpeg__decoder__decode_idct(self,
wuffs_base__slice_u8__subslice_i(a_workbuf, v_offset),
v_stride,
v_b,
((uint32_t)(self->private_impl.f_components_tq[v_csel])));
} }
v_i += 1; v_b += 1;
}
v_b = 0;
while (v_b < self->private_impl.f_mcu_num_blocks) {
v_i = 0;
while (v_i < 64) {
self->private_data.f_mcu_blocks[v_b][v_i] = 0;
v_i += 1;
}
v_b += 1;
} }
if (self->private_impl.f_restarts_remaining > 0) { if (self->private_impl.f_restarts_remaining > 0) {
#if defined(__GNUC__) #if defined(__GNUC__)
@@ -38162,7 +38265,18 @@ wuffs_jpeg__decoder__decode_sos(
} }
v_my += 1; v_my += 1;
} }
v_status = wuffs_jpeg__decoder__swizzle(self, a_dst); if (self->private_impl.f_num_components == 1) {
v_status = wuffs_jpeg__decoder__swizzle_gray(self, a_dst, a_workbuf);
status = v_status;
if (wuffs_base__status__is_error(&status)) {
goto exit;
} else if (wuffs_base__status__is_suspension(&status)) {
status = wuffs_base__make_status(wuffs_base__error__cannot_return_a_suspension);
goto exit;
}
goto ok;
}
v_status = wuffs_jpeg__decoder__swizzle_colorful(self, a_dst);
status = v_status; status = v_status;
if (wuffs_base__status__is_error(&status)) { if (wuffs_base__status__is_error(&status)) {
goto exit; goto exit;
@@ -38199,9 +38313,13 @@ wuffs_jpeg__decoder__prepare_scan(
uint8_t v_c = 0; uint8_t v_c = 0;
uint32_t v_i = 0; uint32_t v_i = 0;
uint32_t v_j = 0; uint32_t v_j = 0;
uint32_t v_h = 0;
uint32_t v_v = 0;
uint32_t v_hv = 0; uint32_t v_hv = 0;
uint32_t v_total_hv = 0; uint32_t v_total_hv = 0;
uint32_t v_b = 0; uint32_t v_b = 0;
uint32_t v_bx_offset = 0;
uint32_t v_by_offset = 0;
const uint8_t* iop_a_src = NULL; const uint8_t* iop_a_src = NULL;
const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
@@ -38239,7 +38357,7 @@ wuffs_jpeg__decoder__prepare_scan(
goto exit; goto exit;
} }
self->private_impl.f_scan_num_components = ((uint32_t)(v_c)); self->private_impl.f_scan_num_components = ((uint32_t)(v_c));
if ((self->private_impl.f_scan_num_components > self->private_impl.f_num_components) || (self->private_impl.f_payload_length != (4 + (2 * self->private_impl.f_scan_num_components)))) { if ((self->private_impl.f_scan_num_components > self->private_impl.f_num_components) || ((self->private_impl.f_scan_num_components < self->private_impl.f_num_components) && (self->private_impl.f_sof_marker < 194)) || (self->private_impl.f_payload_length != (4 + (2 * self->private_impl.f_scan_num_components)))) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_sos_marker); status = wuffs_base__make_status(wuffs_jpeg__error__bad_sos_marker);
goto exit; goto exit;
} }
@@ -38272,6 +38390,10 @@ wuffs_jpeg__decoder__prepare_scan(
goto exit; goto exit;
} }
} }
if ( ! self->private_impl.f_seen_dqt[self->private_impl.f_components_tq[v_j]]) {
status = wuffs_base__make_status(wuffs_jpeg__error__missing_quantization_table);
goto exit;
}
self->private_impl.f_scan_comps_cselector[v_i] = ((uint8_t)(v_j)); self->private_impl.f_scan_comps_cselector[v_i] = ((uint8_t)(v_j));
goto label__0__break; goto label__0__break;
} }
@@ -38349,36 +38471,36 @@ wuffs_jpeg__decoder__prepare_scan(
if (self->private_impl.f_scan_num_components == 1) { if (self->private_impl.f_scan_num_components == 1) {
self->private_impl.f_scan_width_in_mcus = self->private_impl.f_components_width_in_blocks[self->private_impl.f_scan_comps_cselector[0]]; self->private_impl.f_scan_width_in_mcus = self->private_impl.f_components_width_in_blocks[self->private_impl.f_scan_comps_cselector[0]];
self->private_impl.f_scan_height_in_mcus = self->private_impl.f_components_height_in_blocks[self->private_impl.f_scan_comps_cselector[0]]; self->private_impl.f_scan_height_in_mcus = self->private_impl.f_components_height_in_blocks[self->private_impl.f_scan_comps_cselector[0]];
self->private_impl.f_scan_comps_bx_offset[0] = 0;
self->private_impl.f_scan_comps_by_offset[0] = 0;
self->private_impl.f_mcu_num_blocks = 1; self->private_impl.f_mcu_num_blocks = 1;
self->private_impl.f_mcu_blocks_sselector[0] = 0; self->private_impl.f_mcu_blocks_sselector[0] = 0;
} else { } else {
if (self->private_impl.f_max_incl_components_h == 1) { self->private_impl.f_scan_width_in_mcus = self->private_impl.f_width_in_mcus;
self->private_impl.f_scan_width_in_mcus = ((self->private_impl.f_width + 7) / 8); self->private_impl.f_scan_height_in_mcus = self->private_impl.f_height_in_mcus;
} else if (self->private_impl.f_max_incl_components_h == 2) {
self->private_impl.f_scan_width_in_mcus = ((self->private_impl.f_width + 15) / 16);
} else if (self->private_impl.f_max_incl_components_h == 3) {
self->private_impl.f_scan_width_in_mcus = ((self->private_impl.f_width + 23) / 24);
} else {
self->private_impl.f_scan_width_in_mcus = ((self->private_impl.f_width + 31) / 32);
}
if (self->private_impl.f_max_incl_components_v == 1) {
self->private_impl.f_scan_height_in_mcus = ((self->private_impl.f_height + 7) / 8);
} else if (self->private_impl.f_max_incl_components_v == 2) {
self->private_impl.f_scan_height_in_mcus = ((self->private_impl.f_height + 15) / 16);
} else if (self->private_impl.f_max_incl_components_v == 3) {
self->private_impl.f_scan_height_in_mcus = ((self->private_impl.f_height + 23) / 24);
} else {
self->private_impl.f_scan_height_in_mcus = ((self->private_impl.f_height + 31) / 32);
}
v_total_hv = 0; v_total_hv = 0;
v_i = 0; v_i = 0;
v_b = 0; v_b = 0;
v_bx_offset = 0;
v_by_offset = 0;
while (v_i < self->private_impl.f_scan_num_components) { while (v_i < self->private_impl.f_scan_num_components) {
v_h = ((uint32_t)(self->private_impl.f_components_h[self->private_impl.f_scan_comps_cselector[v_i]]));
v_v = ((uint32_t)(self->private_impl.f_components_v[self->private_impl.f_scan_comps_cselector[v_i]]));
v_hv = (((uint32_t)(self->private_impl.f_components_h[self->private_impl.f_scan_comps_cselector[v_i]])) * ((uint32_t)(self->private_impl.f_components_v[self->private_impl.f_scan_comps_cselector[v_i]]))); v_hv = (((uint32_t)(self->private_impl.f_components_h[self->private_impl.f_scan_comps_cselector[v_i]])) * ((uint32_t)(self->private_impl.f_components_v[self->private_impl.f_scan_comps_cselector[v_i]])));
v_total_hv += v_hv; v_total_hv += v_hv;
while (v_hv > 0) { while (v_hv > 0) {
self->private_impl.f_scan_comps_bx_offset[(v_b & 15)] = ((uint8_t)((v_bx_offset & 3)));
self->private_impl.f_scan_comps_by_offset[(v_b & 15)] = ((uint8_t)((v_by_offset & 3)));
self->private_impl.f_mcu_blocks_sselector[(v_b & 15)] = ((uint8_t)(v_i)); self->private_impl.f_mcu_blocks_sselector[(v_b & 15)] = ((uint8_t)(v_i));
v_b += 1; v_b += 1;
v_bx_offset += 1;
if (v_bx_offset == v_h) {
v_bx_offset = 0;
v_by_offset += 1;
if (v_by_offset == v_v) {
v_by_offset = 0;
}
}
v_hv -= 1; v_hv -= 1;
} }
v_i += 1; v_i += 1;
@@ -38559,10 +38681,52 @@ wuffs_jpeg__decoder__skip_past_the_next_restart_marker(
return status; return status;
} }
// -------- func jpeg.decoder.swizzle // -------- func jpeg.decoder.swizzle_gray
static wuffs_base__status static wuffs_base__status
wuffs_jpeg__decoder__swizzle( wuffs_jpeg__decoder__swizzle_gray(
wuffs_jpeg__decoder* self,
wuffs_base__pixel_buffer* a_dst,
wuffs_base__slice_u8 a_workbuf) {
wuffs_base__pixel_format v_dst_pixfmt = {0};
uint32_t v_dst_bits_per_pixel = 0;
uint32_t v_dst_bytes_per_pixel = 0;
uint64_t v_dst_length = 0;
wuffs_base__table_u8 v_tab = {0};
wuffs_base__slice_u8 v_dst = {0};
uint32_t v_y = 0;
uint64_t v_stride = 0;
v_dst_pixfmt = wuffs_base__pixel_buffer__pixel_format(a_dst);
v_dst_bits_per_pixel = wuffs_base__pixel_format__bits_per_pixel(&v_dst_pixfmt);
if ((v_dst_bits_per_pixel & 7) != 0) {
return wuffs_base__make_status(wuffs_base__error__unsupported_option);
}
v_dst_bytes_per_pixel = (v_dst_bits_per_pixel / 8);
v_dst_length = ((uint64_t)((v_dst_bytes_per_pixel * self->private_impl.f_width)));
v_tab = wuffs_base__pixel_buffer__plane(a_dst, 0);
v_y = 0;
while (v_y < self->private_impl.f_height) {
v_dst = wuffs_base__table_u8__row_u32(v_tab, v_y);
if (v_dst_length < ((uint64_t)(v_dst.len))) {
v_dst = wuffs_base__slice_u8__subslice_j(v_dst, v_dst_length);
}
wuffs_base__pixel_swizzler__swizzle_interleaved_from_slice(&self->private_impl.f_swizzler, v_dst, wuffs_base__pixel_buffer__palette_or_else(a_dst, wuffs_base__make_slice_u8(self->private_data.f_dst_palette, 1024)), a_workbuf);
v_stride = ((uint64_t)(self->private_impl.f_components_workbuf_strides[0]));
if (v_stride <= ((uint64_t)(a_workbuf.len))) {
a_workbuf = wuffs_base__slice_u8__subslice_i(a_workbuf, v_stride);
} else {
a_workbuf = wuffs_base__utility__empty_slice_u8();
}
v_y += 1;
}
return wuffs_base__make_status(NULL);
}
// -------- func jpeg.decoder.swizzle_colorful
static wuffs_base__status
wuffs_jpeg__decoder__swizzle_colorful(
wuffs_jpeg__decoder* self, wuffs_jpeg__decoder* self,
wuffs_base__pixel_buffer* a_dst) { wuffs_base__pixel_buffer* a_dst) {
wuffs_base__pixel_format v_dst_pixfmt = {0}; wuffs_base__pixel_format v_dst_pixfmt = {0};
@@ -38776,7 +38940,7 @@ wuffs_jpeg__decoder__workbuf_len(
return wuffs_base__utility__empty_range_ii_u64(); return wuffs_base__utility__empty_range_ii_u64();
} }
return wuffs_base__utility__make_range_ii_u64(0, 0); return wuffs_base__utility__make_range_ii_u64(self->private_impl.f_components_workbuf_offsets[4], self->private_impl.f_components_workbuf_offsets[4]);
} }
// -------- func jpeg.decoder.decode_mcu // -------- func jpeg.decoder.decode_mcu
+266 -72
View File
@@ -20,6 +20,7 @@ pub status "#bad SOS marker"
pub status "#bad header" pub status "#bad header"
pub status "#bad marker" pub status "#bad marker"
pub status "#missing Huffman table" pub status "#missing Huffman table"
pub status "#missing Quantization table"
pub status "#truncated input" pub status "#truncated input"
pub status "#unsupported DQT after SOF markers" pub status "#unsupported DQT after SOF markers"
pub status "#unsupported arithmetic coding" pub status "#unsupported arithmetic coding"
@@ -34,12 +35,15 @@ pub status "#unsupported precision"
pri status "#internal error: inconsistent decoder state" pri status "#internal error: inconsistent decoder state"
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0 pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0x40_0000_0000
pub struct decoder? implements base.image_decoder( pub struct decoder? implements base.image_decoder(
width : base.u32[..= 0xFFFF], width : base.u32[..= 0xFFFF],
height : base.u32[..= 0xFFFF], height : base.u32[..= 0xFFFF],
width_in_mcus : base.u32[..= 0x2000],
height_in_mcus : base.u32[..= 0x2000],
// The call sequence state machine is discussed in // The call sequence state machine is discussed in
// (/doc/std/image-decoders-call-sequence.md). // (/doc/std/image-decoders-call-sequence.md).
call_sequence : base.u8, call_sequence : base.u8,
@@ -54,11 +58,35 @@ pub struct decoder? implements base.image_decoder(
components_c : array[4] base.u8, components_c : array[4] base.u8,
components_h : array[4] base.u8[..= 4], components_h : array[4] base.u8[..= 4],
components_v : array[4] base.u8[..= 4], components_v : array[4] base.u8[..= 4],
components_tq : array[4] base.u8[..= 4], components_tq : array[4] base.u8[..= 3],
components_width_in_blocks : array[4] base.u32[..= 0x2000], components_width_in_blocks : array[4] base.u32[..= 0x2000],
components_height_in_blocks : array[4] base.u32[..= 0x2000], components_height_in_blocks : array[4] base.u32[..= 0x2000],
// components_workbuf_offsets, indexed by (b) and (b + 1), is the i and
// j bounds in the workbuf[i .. j] slice holding the post-IDCT values.
// components_workbuf_strides[b] is the stride between its rows.
//
// For example, a 4:2:0 chroma-subsampled, 36 pixels wide × 28 pixels
// high image has 6 blocks (4 Y, 1 Cb, 1 Cr; 8×8 samples each) per MCU.
// Each MCU is 16×16 pixels. The image is 3 MCUs wide × 2 MCUs high.
// The post-IDCT image dimensions round up to 48 × 32 pixels.
//
// The components_workbuf_strides array is:
// 0: 0x30 = 48 // Y
// 1: 0x18 = 24 // Cb
// 2: 0x18 = 24 // Cr
// 3: 0x00 = 0 // Unused
//
// The components_workbuf_offsets array is:
// 0: 0x000 = 0
// 1: 0x600 = 1536 = previous + (48 * 32)
// 2: 0x790 = 1920 = previous + (24 * 16)
// 3: 0x900 = 2304 = previous + (24 * 16)
// 4: 0x900 = 2304 = previous + ( 0 * 0)
components_workbuf_strides : array[4] base.u32[..= 0x4_0000],
components_workbuf_offsets : array[5] base.u64[..= 0x40_0000_0000],
scan_num_components : base.u32[..= 4], scan_num_components : base.u32[..= 4],
scan_comps_cselector : array[4] base.u8[..= 3], scan_comps_cselector : array[4] base.u8[..= 3],
scan_comps_td : array[4] base.u8[..= 3], scan_comps_td : array[4] base.u8[..= 3],
@@ -72,6 +100,9 @@ pub struct decoder? implements base.image_decoder(
scan_width_in_mcus : base.u32[..= 0x2000], scan_width_in_mcus : base.u32[..= 0x2000],
scan_height_in_mcus : base.u32[..= 0x2000], scan_height_in_mcus : base.u32[..= 0x2000],
scan_comps_bx_offset : array[16] base.u8[..= 3],
scan_comps_by_offset : array[16] base.u8[..= 3],
mcu_num_blocks : base.u32[..= 10], mcu_num_blocks : base.u32[..= 10],
mcu_current_block : base.u32[..= 10], mcu_current_block : base.u32[..= 10],
mcu_blocks_sselector : array[16] base.u8[..= 3], mcu_blocks_sselector : array[16] base.u8[..= 3],
@@ -151,6 +182,7 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.io_reader) { pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.io_reader) {
var c : base.u8 var c : base.u8
var marker : base.u8 var marker : base.u8
var pixfmt : base.u32
if this.call_sequence <> 0x00 { if this.call_sequence <> 0x00 {
return base."#bad call sequence" return base."#bad call sequence"
@@ -276,10 +308,14 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.
this.frame_config_io_position = args.src.position() this.frame_config_io_position = args.src.position()
if args.dst <> nullptr { if args.dst <> nullptr {
pixfmt = base.PIXEL_FORMAT__Y
if this.num_components > 1 {
// TODO: base.PIXEL_FORMAT__YCBCR is probably more correct,
// although possibly less convenient for the caller.
pixfmt = base.PIXEL_FORMAT__BGRX
}
args.dst.set!( args.dst.set!(
// TODO: base.PIXEL_FORMAT__YCBCR is probably more correct, pixfmt: pixfmt,
// although possibly less convenient for the caller.
pixfmt: base.PIXEL_FORMAT__BGRX,
pixsub: 0, pixsub: 0,
width: this.width, width: this.width,
height: this.height, height: this.height,
@@ -341,6 +377,11 @@ pri func decoder.decode_sof?(src: base.io_reader) {
var has_v24 : base.bool var has_v24 : base.bool
var has_v3 : base.bool var has_v3 : base.bool
var workbuf_fragment_length0 : base.u64[..= 0x10_0000_0000]
var workbuf_fragment_length1 : base.u64[..= 0x10_0000_0000]
var workbuf_fragment_length2 : base.u64[..= 0x10_0000_0000]
var workbuf_fragment_length3 : base.u64[..= 0x10_0000_0000]
if this.payload_length < 6 { if this.payload_length < 6 {
return "#bad SOF marker" return "#bad SOF marker"
} }
@@ -392,7 +433,7 @@ pri func decoder.decode_sof?(src: base.io_reader) {
this.max_incl_components_v = this.components_v[i] this.max_incl_components_v = this.components_v[i]
} }
c = args.src.read_u8?() c = args.src.read_u8?()
if c > 4 { if c >= 4 {
return "#bad SOF marker" return "#bad SOF marker"
} }
this.components_tq[i] = c this.components_tq[i] = c
@@ -413,21 +454,34 @@ pri func decoder.decode_sof?(src: base.io_reader) {
i += 1 i += 1
} endwhile } endwhile
has_h24 = false if this.num_components == 1 {
has_h3 = false // The first (and only) component's H and V factors are effectively
has_v24 = false // always 1. Section A.2.2: "When Ns = 1 (where Ns is the number of
has_v3 = false // components in a scan), the order of data units [8x8 blocks] within a
i = 0 // scan shall be left-to-right and top-to-bottom... regardless of the
while i < this.num_components { // values of H1 and V1.".
assert i < 4 via "a < b: a < c; c <= b"(c: this.num_components) this.max_incl_components_h = 1
has_h24 = has_h24 or (this.components_h[i] == 2) or (this.components_h[i] == 4) this.max_incl_components_v = 1
has_h3 = has_h3 or (this.components_h[i] == 3) this.components_h[0] = 1
has_v24 = has_v24 or (this.components_v[i] == 2) or (this.components_v[i] == 4) this.components_v[0] = 1
has_v3 = has_v3 or (this.components_v[i] == 3)
i += 1 } else {
} endwhile has_h24 = false
if (has_h24 and has_h3) or (has_v24 and has_v3) { has_h3 = false
return "#unsupported fractional sampling" has_v24 = false
has_v3 = false
i = 0
while i < this.num_components {
assert i < 4 via "a < b: a < c; c <= b"(c: this.num_components)
has_h24 = has_h24 or (this.components_h[i] == 2) or (this.components_h[i] == 4)
has_h3 = has_h3 or (this.components_h[i] == 3)
has_v24 = has_v24 or (this.components_v[i] == 2) or (this.components_v[i] == 4)
has_v3 = has_v3 or (this.components_v[i] == 3)
i += 1
} endwhile
if (has_h24 and has_h3) or (has_v24 and has_v3) {
return "#unsupported fractional sampling"
}
} }
i = 0 i = 0
@@ -456,6 +510,50 @@ pri func decoder.decode_sof?(src: base.io_reader) {
i += 1 i += 1
} endwhile } endwhile
if this.max_incl_components_h == 1 {
this.width_in_mcus = (this.width + 0x07) / 0x08
} else if this.max_incl_components_h == 2 {
this.width_in_mcus = (this.width + 0x0F) / 0x10
} else if this.max_incl_components_h == 3 {
this.width_in_mcus = (this.width + 0x17) / 0x18
} else {
this.width_in_mcus = (this.width + 0x1F) / 0x20
}
if this.max_incl_components_v == 1 {
this.height_in_mcus = (this.height + 0x07) / 0x08
} else if this.max_incl_components_v == 2 {
this.height_in_mcus = (this.height + 0x0F) / 0x10
} else if this.max_incl_components_v == 3 {
this.height_in_mcus = (this.height + 0x17) / 0x18
} else {
this.height_in_mcus = (this.height + 0x1F) / 0x20
}
this.components_workbuf_strides[0] = 8 *
(this.width_in_mcus as base.u32) * (this.components_h[0] as base.u32)
this.components_workbuf_strides[1] = 8 *
(this.width_in_mcus as base.u32) * (this.components_h[1] as base.u32)
this.components_workbuf_strides[2] = 8 *
(this.width_in_mcus as base.u32) * (this.components_h[2] as base.u32)
this.components_workbuf_strides[3] = 8 *
(this.width_in_mcus as base.u32) * (this.components_h[3] as base.u32)
workbuf_fragment_length0 = 8 * (this.components_workbuf_strides[0] as base.u64) *
(this.height_in_mcus as base.u64) * (this.components_v[0] as base.u64)
workbuf_fragment_length1 = 8 * (this.components_workbuf_strides[1] as base.u64) *
(this.height_in_mcus as base.u64) * (this.components_v[1] as base.u64)
workbuf_fragment_length2 = 8 * (this.components_workbuf_strides[2] as base.u64) *
(this.height_in_mcus as base.u64) * (this.components_v[2] as base.u64)
workbuf_fragment_length3 = 8 * (this.components_workbuf_strides[3] as base.u64) *
(this.height_in_mcus as base.u64) * (this.components_v[3] as base.u64)
this.components_workbuf_offsets[0] = 0
this.components_workbuf_offsets[1] = this.components_workbuf_offsets[0] + workbuf_fragment_length0
this.components_workbuf_offsets[2] = this.components_workbuf_offsets[1] + workbuf_fragment_length1
this.components_workbuf_offsets[3] = this.components_workbuf_offsets[2] + workbuf_fragment_length2
this.components_workbuf_offsets[4] = this.components_workbuf_offsets[3] + workbuf_fragment_length3
} }
pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) { pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
@@ -517,6 +615,7 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
} }
pri func decoder.do_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) { pri func decoder.do_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 pixfmt : base.u32
var status : base.status var status : base.status
var c : base.u8 var c : base.u8
var marker : base.u8 var marker : base.u8
@@ -529,10 +628,14 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade
return base."@end of data" return base."@end of data"
} }
pixfmt = base.PIXEL_FORMAT__Y
if this.num_components > 1 {
pixfmt = base.PIXEL_FORMAT__BGRX
}
status = this.swizzler.prepare!( status = this.swizzler.prepare!(
dst_pixfmt: args.dst.pixel_format(), dst_pixfmt: args.dst.pixel_format(),
dst_palette: args.dst.palette_or_else(fallback: this.dst_palette[..]), dst_palette: args.dst.palette_or_else(fallback: this.dst_palette[..]),
src_pixfmt: this.util.make_pixel_format(repr: base.PIXEL_FORMAT__BGRX), src_pixfmt: this.util.make_pixel_format(repr: pixfmt),
src_palette: this.util.empty_slice_u8(), src_palette: this.util.empty_slice_u8(),
blend: args.blend) blend: args.blend)
if not status.is_ok() { if not status.is_ok() {
@@ -838,13 +941,25 @@ pri func decoder.calculate_huff_tables!(tc4_th: base.u8[..= 7], total_count: bas
} }
pri func decoder.decode_sos?(dst: ptr base.pixel_buffer, src: base.io_reader, workbuf: slice base.u8) { pri func decoder.decode_sos?(dst: ptr base.pixel_buffer, src: base.io_reader, workbuf: slice base.u8) {
var my : base.u32 var my : base.u32
var mx : base.u32 var mx : base.u32
var decode_mcu_result : base.u32 var decode_mcu_result : base.u32
var bitstream_length : base.u32 var bitstream_length : base.u32
var i : base.u32
var j : base.u32 var b : base.u32
var status : base.status var i : base.u32
var csel : base.u8[..= 3]
var h : base.u64[..= 4]
var v : base.u64[..= 4]
var stride : base.u64[..= 0x4_0000]
var offset : base.u64
var status : base.status
if args.workbuf.length() < this.components_workbuf_offsets[4] {
return base."#bad workbuf length"
}
this.prepare_scan?(src: args.src) this.prepare_scan?(src: args.src)
@@ -896,27 +1011,51 @@ pri func decoder.decode_sos?(dst: ptr base.pixel_buffer, src: base.io_reader, wo
} endwhile.fill_bitstream } endwhile.fill_bitstream
} endwhile.decode_mcu } endwhile.decode_mcu
// TODO: IDCT on this.mcu_blocks. // Apply IDCT.
b = 0
// Clear this.mcu_blocks. while b < this.mcu_num_blocks,
i = 0
j = 0
while i < this.mcu_num_blocks,
inv my < 0x2000, inv my < 0x2000,
inv mx < 0x2000, inv mx < 0x2000,
{ {
assert i < 10 via "a < b: a < c; c <= b"(c: this.mcu_num_blocks) assert b < 10 via "a < b: a < c; c <= b"(c: this.mcu_num_blocks)
while j < 64, csel = this.scan_comps_cselector[this.mcu_blocks_sselector[b]]
inv my < 0x2000, h = this.components_h[csel] as base.u64
inv mx < 0x2000, v = this.components_v[csel] as base.u64
inv i < 10, stride = this.components_workbuf_strides[csel] as base.u64
{ offset = this.components_workbuf_offsets[csel] + (8 *
this.mcu_blocks[i][j] = 0 ((((h * (mx as base.u64)) + (this.scan_comps_bx_offset[b] as base.u64))) +
j += 1 ((((v * (my as base.u64)) + (this.scan_comps_by_offset[b] as base.u64))) * stride)))
} endwhile if offset <= args.workbuf.length() {
i += 1 this.decode_idct!(
dst_buffer: args.workbuf[offset ..],
dst_stride: stride,
b: b,
q: this.components_tq[csel] as base.u32)
}
b += 1
} endwhile } endwhile
// Clear this.mcu_blocks.
b = 0
while b < this.mcu_num_blocks,
inv my < 0x2000,
inv mx < 0x2000,
{
assert b < 10 via "a < b: a < c; c <= b"(c: this.mcu_num_blocks)
i = 0
while i < 64,
inv my < 0x2000,
inv mx < 0x2000,
inv b < 10,
{
this.mcu_blocks[b][i] = 0
i += 1
} endwhile
b += 1
} endwhile
// Check for a restart.
if this.restarts_remaining > 0 { if this.restarts_remaining > 0 {
this.restarts_remaining -= 1 this.restarts_remaining -= 1
if this.restarts_remaining == 0 { if this.restarts_remaining == 0 {
@@ -940,17 +1079,25 @@ pri func decoder.decode_sos?(dst: ptr base.pixel_buffer, src: base.io_reader, wo
my += 1 my += 1
} endwhile } endwhile
status = this.swizzle!(dst: args.dst) if this.num_components == 1 {
status = this.swizzle_gray!(dst: args.dst, workbuf: args.workbuf)
return status
}
status = this.swizzle_colorful!(dst: args.dst)
return status return status
} }
pri func decoder.prepare_scan?(src: base.io_reader) { pri func decoder.prepare_scan?(src: base.io_reader) {
var c : base.u8 var c : base.u8
var i : base.u32 var i : base.u32
var j : base.u32 var j : base.u32
var hv : base.u32[..= 16] var h : base.u32[..= 4]
var total_hv : base.u32 var v : base.u32[..= 4]
var b : base.u32 var hv : base.u32[..= 16]
var total_hv : base.u32
var b : base.u32
var bx_offset : base.u32
var by_offset : base.u32
if (this.payload_length < 6) or (this.payload_length > 12) { if (this.payload_length < 6) or (this.payload_length > 12) {
return "#bad SOS marker" return "#bad SOS marker"
@@ -961,6 +1108,7 @@ pri func decoder.prepare_scan?(src: base.io_reader) {
} }
this.scan_num_components = c as base.u32 this.scan_num_components = c as base.u32
if (this.scan_num_components > this.num_components) or if (this.scan_num_components > this.num_components) or
((this.scan_num_components < this.num_components) and (this.sof_marker < 0xC2)) or
(this.payload_length <> (4 + (2 * this.scan_num_components))) { (this.payload_length <> (4 + (2 * this.scan_num_components))) {
return "#bad SOS marker" return "#bad SOS marker"
} }
@@ -988,6 +1136,9 @@ pri func decoder.prepare_scan?(src: base.io_reader) {
return "#bad SOS marker" return "#bad SOS marker"
} }
} }
if not this.seen_dqt[this.components_tq[j]] {
return "#missing Quantization table"
}
this.scan_comps_cselector[i] = j as base.u8 this.scan_comps_cselector[i] = j as base.u8
break break
} endwhile } endwhile
@@ -1044,35 +1195,23 @@ pri func decoder.prepare_scan?(src: base.io_reader) {
if this.scan_num_components == 1 { if this.scan_num_components == 1 {
this.scan_width_in_mcus = this.components_width_in_blocks[this.scan_comps_cselector[0]] this.scan_width_in_mcus = this.components_width_in_blocks[this.scan_comps_cselector[0]]
this.scan_height_in_mcus = this.components_height_in_blocks[this.scan_comps_cselector[0]] this.scan_height_in_mcus = this.components_height_in_blocks[this.scan_comps_cselector[0]]
this.scan_comps_bx_offset[0] = 0
this.scan_comps_by_offset[0] = 0
this.mcu_num_blocks = 1 this.mcu_num_blocks = 1
this.mcu_blocks_sselector[0] = 0 this.mcu_blocks_sselector[0] = 0
} else { } else {
if this.max_incl_components_h == 1 { this.scan_width_in_mcus = this.width_in_mcus
this.scan_width_in_mcus = (this.width + 0x07) / 0x08 this.scan_height_in_mcus = this.height_in_mcus
} else if this.max_incl_components_h == 2 {
this.scan_width_in_mcus = (this.width + 0x0F) / 0x10
} else if this.max_incl_components_h == 3 {
this.scan_width_in_mcus = (this.width + 0x17) / 0x18
} else {
this.scan_width_in_mcus = (this.width + 0x1F) / 0x20
}
if this.max_incl_components_v == 1 {
this.scan_height_in_mcus = (this.height + 0x07) / 0x08
} else if this.max_incl_components_v == 2 {
this.scan_height_in_mcus = (this.height + 0x0F) / 0x10
} else if this.max_incl_components_v == 3 {
this.scan_height_in_mcus = (this.height + 0x17) / 0x18
} else {
this.scan_height_in_mcus = (this.height + 0x1F) / 0x20
}
total_hv = 0 total_hv = 0
i = 0 i = 0
b = 0 b = 0
bx_offset = 0
by_offset = 0
while i < this.scan_num_components { while i < this.scan_num_components {
assert i < 4 via "a < b: a < c; c <= b"(c: this.scan_num_components) assert i < 4 via "a < b: a < c; c <= b"(c: this.scan_num_components)
h = this.components_h[this.scan_comps_cselector[i]] as base.u32
v = this.components_v[this.scan_comps_cselector[i]] as base.u32
hv = ((this.components_h[this.scan_comps_cselector[i]] as base.u32) * hv = ((this.components_h[this.scan_comps_cselector[i]] as base.u32) *
(this.components_v[this.scan_comps_cselector[i]] as base.u32)) (this.components_v[this.scan_comps_cselector[i]] as base.u32))
total_hv ~mod+= hv total_hv ~mod+= hv
@@ -1080,8 +1219,18 @@ pri func decoder.prepare_scan?(src: base.io_reader) {
while hv > 0, while hv > 0,
inv i < 4, inv i < 4,
{ {
this.scan_comps_bx_offset[b & 15] = (bx_offset & 3) as base.u8
this.scan_comps_by_offset[b & 15] = (by_offset & 3) as base.u8
this.mcu_blocks_sselector[b & 15] = i as base.u8 this.mcu_blocks_sselector[b & 15] = i as base.u8
b ~mod+= 1 b ~mod+= 1
bx_offset ~mod+= 1
if bx_offset == h {
bx_offset = 0
by_offset ~mod+= 1
if by_offset == v {
by_offset = 0
}
}
hv -= 1 hv -= 1
} endwhile } endwhile
@@ -1210,7 +1359,50 @@ pri func decoder.skip_past_the_next_restart_marker?(src: base.io_reader) {
this.next_restart_marker = (this.next_restart_marker ~mod+ 1) & 7 this.next_restart_marker = (this.next_restart_marker ~mod+ 1) & 7
} }
pri func decoder.swizzle!(dst: ptr base.pixel_buffer) base.status { pri func decoder.swizzle_gray!(dst: ptr base.pixel_buffer, workbuf: slice base.u8) base.status {
var dst_pixfmt : base.pixel_format
var dst_bits_per_pixel : base.u32[..= 256]
var dst_bytes_per_pixel : base.u32[..= 32]
var dst_length : base.u64
var tab : table base.u8
var dst : slice base.u8
var y : base.u32
var stride : base.u64[..= 0x4_0000]
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
dst_pixfmt = args.dst.pixel_format()
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
if (dst_bits_per_pixel & 7) <> 0 {
return base."#unsupported option"
}
dst_bytes_per_pixel = dst_bits_per_pixel / 8
dst_length = (dst_bytes_per_pixel * this.width) as base.u64
tab = args.dst.plane(p: 0)
y = 0
while y < this.height {
assert y < 0xFFFF via "a < b: a < c; c <= b"(c: this.height)
dst = tab.row_u32(y: y)
if dst_length < dst.length() {
dst = dst[.. dst_length]
}
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst,
dst_palette: args.dst.palette_or_else(fallback: this.dst_palette[..]),
src: args.workbuf)
stride = this.components_workbuf_strides[0] as base.u64
if stride <= args.workbuf.length() {
args.workbuf = args.workbuf[stride ..]
} else {
args.workbuf = this.util.empty_slice_u8()
}
y += 1
} endwhile
return ok
}
pri func decoder.swizzle_colorful!(dst: ptr base.pixel_buffer) base.status {
var dst_pixfmt : base.pixel_format var dst_pixfmt : base.pixel_format
var dst_bits_per_pixel : base.u32[..= 256] var dst_bits_per_pixel : base.u32[..= 256]
var dst_bytes_per_pixel : base.u32[..= 32] var dst_bytes_per_pixel : base.u32[..= 32]
@@ -1313,5 +1505,7 @@ pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_inform
} }
pub func decoder.workbuf_len() base.range_ii_u64 { pub func decoder.workbuf_len() base.range_ii_u64 {
return this.util.make_range_ii_u64(min_incl: 0, max_incl: 0) return this.util.make_range_ii_u64(
min_incl: this.components_workbuf_offsets[4],
max_incl: this.components_workbuf_offsets[4])
} }
+4
View File
@@ -611,6 +611,10 @@ test_wuffs_jpeg_decode_mcu() {
&dec, sizeof dec, WUFFS_VERSION, &dec, sizeof dec, WUFFS_VERSION,
WUFFS_INITIALIZE__DEFAULT_OPTIONS)); WUFFS_INITIALIZE__DEFAULT_OPTIONS));
// Bypass the "#missing Quantization table" check.
dec.private_impl.f_seen_dqt[0] = true;
dec.private_impl.f_seen_dqt[1] = true;
// Decode the 0xC0 SOF marker, four 0xC4 DHT markers and 0xDA SOS marker. The // Decode the 0xC0 SOF marker, four 0xC4 DHT markers and 0xDA SOS marker. The
// SOS marker is only partially processed, since complete SOS processing // SOS marker is only partially processed, since complete SOS processing
// would call wuffs_jpeg__decoder__decode_mcu multiple times. // would call wuffs_jpeg__decoder__decode_mcu multiple times.
+1 -1
View File
@@ -34,7 +34,7 @@ f36c2e80 test/data/bricks-dither.no-ancillary.png
f36c2e80 test/data/bricks-dither.png f36c2e80 test/data/bricks-dither.png
c2bce675 test/data/bricks-gray.bmp c2bce675 test/data/bricks-gray.bmp
c2bce675 test/data/bricks-gray.gif c2bce675 test/data/bricks-gray.gif
84e5ba7c test/data/bricks-gray.jpeg 3a2478ad test/data/bricks-gray.jpeg
c2bce675 test/data/bricks-gray.no-ancillary.png c2bce675 test/data/bricks-gray.no-ancillary.png
c2bce675 test/data/bricks-gray.png c2bce675 test/data/bricks-gray.png
c2bce675 test/data/bricks-gray.tga c2bce675 test/data/bricks-gray.tga