std/{bmp,nie}: allow decode_frame to small pixbuf

This commit is contained in:
Nigel Tao committed 2023-03-27 21:22:06 +11:00
1 parent baac49dca7
commit 17f7613262
3 files changed
+199 -48

No files matched your search

+95 -20
View File
@@ -24195,10 +24195,12 @@ wuffs_bmp__decoder__swizzle_none(
uint32_t v_dst_bits_per_pixel = 0;
uint64_t v_dst_bytes_per_pixel = 0;
uint64_t v_dst_bytes_per_row = 0;
uint32_t v_src_bytes_per_pixel = 0;
wuffs_base__slice_u8 v_dst_palette = {0};
wuffs_base__table_u8 v_tab = {0};
wuffs_base__slice_u8 v_dst = {0};
uint64_t v_i = 0;
uint64_t v_j = 0;
uint64_t v_n = 0;
const uint8_t* iop_a_src = NULL;
@@ -24232,7 +24234,6 @@ wuffs_bmp__decoder__swizzle_none(
self->private_impl.f_pending_pad -= 1;
iop_a_src += 1;
}
label__inner__continue:;
while (true) {
if (self->private_impl.f_dst_x == self->private_impl.f_width) {
self->private_impl.f_dst_x = 0;
@@ -24253,14 +24254,38 @@ wuffs_bmp__decoder__swizzle_none(
}
v_i = (((uint64_t)(self->private_impl.f_dst_x)) * v_dst_bytes_per_pixel);
if (v_i >= ((uint64_t)(v_dst.len))) {
goto label__inner__continue;
if (self->private_impl.f_bits_per_pixel > 32) {
status = wuffs_base__make_status(wuffs_bmp__error__unsupported_bmp_file);
goto exit;
}
v_src_bytes_per_pixel = (self->private_impl.f_bits_per_pixel / 8);
if (v_src_bytes_per_pixel == 0) {
status = wuffs_base__make_status(wuffs_bmp__error__unsupported_bmp_file);
goto exit;
}
v_n = (((uint64_t)(io2_a_src - iop_a_src)) / ((uint64_t)(v_src_bytes_per_pixel)));
v_n = wuffs_base__u64__min(v_n, ((uint64_t)(((uint32_t)(self->private_impl.f_width - self->private_impl.f_dst_x)))));
v_j = v_n;
while (v_j >= 8) {
if (((uint64_t)(io2_a_src - iop_a_src)) >= ((uint64_t)((v_src_bytes_per_pixel * 8)))) {
iop_a_src += (v_src_bytes_per_pixel * 8);
}
v_j -= 8;
}
while (v_j > 0) {
if (((uint64_t)(io2_a_src - iop_a_src)) >= ((uint64_t)((v_src_bytes_per_pixel * 1)))) {
iop_a_src += (v_src_bytes_per_pixel * 1);
}
v_j -= 1;
}
} else {
v_n = wuffs_base__pixel_swizzler__swizzle_interleaved_from_reader(
&self->private_impl.f_swizzler,
wuffs_base__slice_u8__subslice_i(v_dst, v_i),
v_dst_palette,
&iop_a_src,
io2_a_src);
}
v_n = wuffs_base__pixel_swizzler__swizzle_interleaved_from_reader(
&self->private_impl.f_swizzler,
wuffs_base__slice_u8__subslice_i(v_dst, v_i),
v_dst_palette,
&iop_a_src,
io2_a_src);
if (v_n == 0) {
status = wuffs_base__make_status(wuffs_bmp__note__internal_note_short_read);
goto ok;
@@ -24601,7 +24626,6 @@ wuffs_bmp__decoder__swizzle_bitfields(
self->private_impl.f_pending_pad -= 1;
iop_a_src += 1;
}
label__inner__continue:;
while (true) {
if (self->private_impl.f_dst_x == self->private_impl.f_width) {
self->private_impl.f_dst_x = 0;
@@ -24660,9 +24684,10 @@ wuffs_bmp__decoder__swizzle_bitfields(
}
v_i = (((uint64_t)(self->private_impl.f_dst_x)) * v_dst_bytes_per_pixel);
if (v_i >= ((uint64_t)(v_dst.len))) {
goto label__inner__continue;
v_n = ((uint64_t)(v_p0));
} else {
v_n = wuffs_base__pixel_swizzler__swizzle_interleaved_from_slice(&self->private_impl.f_swizzler, wuffs_base__slice_u8__subslice_i(v_dst, v_i), v_dst_palette, wuffs_base__make_slice_u8(self->private_data.f_scratch, (8 * v_p0)));
}
v_n = wuffs_base__pixel_swizzler__swizzle_interleaved_from_slice(&self->private_impl.f_swizzler, wuffs_base__slice_u8__subslice_i(v_dst, v_i), v_dst_palette, wuffs_base__make_slice_u8(self->private_data.f_scratch, (8 * v_p0)));
if (v_n == 0) {
status = wuffs_base__make_status(wuffs_bmp__note__internal_note_short_read);
goto ok;
@@ -24705,6 +24730,7 @@ wuffs_bmp__decoder__swizzle_low_bit_depth(
uint32_t v_p0 = 0;
uint32_t v_chunk_bits = 0;
uint32_t v_chunk_count = 0;
uint32_t v_pixels_per_chunk = 0;
const uint8_t* iop_a_src = NULL;
const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
@@ -24742,6 +24768,35 @@ wuffs_bmp__decoder__swizzle_low_bit_depth(
}
v_i = (((uint64_t)(self->private_impl.f_dst_x)) * v_dst_bytes_per_pixel);
if (v_i >= ((uint64_t)(v_dst.len))) {
if (self->private_impl.f_bits_per_pixel == 1) {
v_chunk_count = ((wuffs_base__u32__sat_sub(self->private_impl.f_width, self->private_impl.f_dst_x) + 31) / 32);
v_pixels_per_chunk = 32;
} else if (self->private_impl.f_bits_per_pixel == 2) {
v_chunk_count = ((wuffs_base__u32__sat_sub(self->private_impl.f_width, self->private_impl.f_dst_x) + 15) / 16);
v_pixels_per_chunk = 16;
} else {
v_chunk_count = ((wuffs_base__u32__sat_sub(self->private_impl.f_width, self->private_impl.f_dst_x) + 7) / 8);
v_pixels_per_chunk = 8;
}
while ((v_chunk_count >= 64) && (((uint64_t)(io2_a_src - iop_a_src)) >= 256)) {
iop_a_src += 256;
self->private_impl.f_dst_x = wuffs_base__u32__min(self->private_impl.f_width, ((uint32_t)(self->private_impl.f_dst_x + (v_pixels_per_chunk * 64))));
v_chunk_count -= 64;
}
while ((v_chunk_count >= 8) && (((uint64_t)(io2_a_src - iop_a_src)) >= 32)) {
iop_a_src += 32;
self->private_impl.f_dst_x = wuffs_base__u32__min(self->private_impl.f_width, ((uint32_t)(self->private_impl.f_dst_x + (v_pixels_per_chunk * 8))));
v_chunk_count -= 8;
}
while (v_chunk_count > 0) {
if (((uint64_t)(io2_a_src - iop_a_src)) < 4) {
status = wuffs_base__make_status(wuffs_bmp__note__internal_note_short_read);
goto ok;
}
iop_a_src += 4;
self->private_impl.f_dst_x = wuffs_base__u32__min(self->private_impl.f_width, ((uint32_t)(self->private_impl.f_dst_x + (v_pixels_per_chunk * 1))));
v_chunk_count -= 1;
}
goto label__loop__continue;
}
v_dst = wuffs_base__slice_u8__subslice_i(v_dst, v_i);
@@ -24812,7 +24867,7 @@ wuffs_bmp__decoder__swizzle_low_bit_depth(
v_p0 = ((v_p0 & 511) + 16);
v_chunk_count -= 1;
}
} else if (self->private_impl.f_bits_per_pixel == 4) {
} else {
v_chunk_count = ((wuffs_base__u32__sat_sub(self->private_impl.f_width, self->private_impl.f_dst_x) + 7) / 8);
v_chunk_count = wuffs_base__u32__min(v_chunk_count, 64);
while ((v_chunk_count > 0) && (((uint64_t)(io2_a_src - iop_a_src)) >= 4)) {
@@ -37894,9 +37949,11 @@ wuffs_nie__decoder__swizzle(
uint32_t v_dst_bits_per_pixel = 0;
uint64_t v_dst_bytes_per_pixel = 0;
uint64_t v_dst_bytes_per_row = 0;
uint32_t v_src_bytes_per_pixel = 0;
wuffs_base__table_u8 v_tab = {0};
wuffs_base__slice_u8 v_dst = {0};
uint64_t v_i = 0;
uint64_t v_j = 0;
uint64_t v_n = 0;
const uint8_t* iop_a_src = NULL;
@@ -37919,7 +37976,6 @@ wuffs_nie__decoder__swizzle(
v_dst_bytes_per_pixel = ((uint64_t)((v_dst_bits_per_pixel / 8)));
v_dst_bytes_per_row = (((uint64_t)(self->private_impl.f_width)) * v_dst_bytes_per_pixel);
v_tab = wuffs_base__pixel_buffer__plane(a_dst, 0);
label__0__continue:;
while (true) {
if (self->private_impl.f_dst_x == self->private_impl.f_width) {
self->private_impl.f_dst_x = 0;
@@ -37934,14 +37990,33 @@ wuffs_nie__decoder__swizzle(
}
v_i = (((uint64_t)(self->private_impl.f_dst_x)) * v_dst_bytes_per_pixel);
if (v_i >= ((uint64_t)(v_dst.len))) {
goto label__0__continue;
v_src_bytes_per_pixel = 4;
if (self->private_impl.f_pixfmt == 2164308923) {
v_src_bytes_per_pixel = 8;
}
v_n = (((uint64_t)(io2_a_src - iop_a_src)) / ((uint64_t)(v_src_bytes_per_pixel)));
v_n = wuffs_base__u64__min(v_n, ((uint64_t)(((uint32_t)(self->private_impl.f_width - self->private_impl.f_dst_x)))));
v_j = v_n;
while (v_j >= 8) {
if (((uint64_t)(io2_a_src - iop_a_src)) >= ((uint64_t)((v_src_bytes_per_pixel * 8)))) {
iop_a_src += (v_src_bytes_per_pixel * 8);
}
v_j -= 8;
}
while (v_j > 0) {
if (((uint64_t)(io2_a_src - iop_a_src)) >= ((uint64_t)((v_src_bytes_per_pixel * 1)))) {
iop_a_src += (v_src_bytes_per_pixel * 1);
}
v_j -= 1;
}
} else {
v_n = wuffs_base__pixel_swizzler__swizzle_interleaved_from_reader(
&self->private_impl.f_swizzler,
wuffs_base__slice_u8__subslice_i(v_dst, v_i),
wuffs_base__pixel_buffer__palette(a_dst),
&iop_a_src,
io2_a_src);
}
v_n = wuffs_base__pixel_swizzler__swizzle_interleaved_from_reader(
&self->private_impl.f_swizzler,
wuffs_base__slice_u8__subslice_i(v_dst, v_i),
wuffs_base__pixel_buffer__palette(a_dst),
&iop_a_src,
io2_a_src);
if (v_n == 0) {
status = wuffs_base__make_status(wuffs_nie__note__internal_note_short_read);
goto ok;
+72 -21
View File
@@ -50,7 +50,7 @@ pub struct decoder? implements base.image_decoder(
bitmap_info_len : base.u32,
padding : base.u32,
bits_per_pixel : base.u32,
bits_per_pixel : base.u32[..= 0xFFFF],
compression : base.u32,
// channel_etc's indexes are: B, G, R, A. This is in Wuffs' default order,
@@ -504,10 +504,12 @@ pri func decoder.swizzle_none!(dst: ptr base.pixel_buffer, src: base.io_reader)
var dst_bits_per_pixel : base.u32[..= 256]
var dst_bytes_per_pixel : base.u64[..= 32]
var dst_bytes_per_row : base.u64
var src_bytes_per_pixel : base.u32[..= 4]
var dst_palette : slice base.u8
var tab : table base.u8
var dst : slice base.u8
var i : base.u64
var j : base.u64
var n : base.u64
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
@@ -552,14 +554,38 @@ pri func decoder.swizzle_none!(dst: ptr base.pixel_buffer, src: base.io_reader)
}
i = (this.dst_x as base.u64) * dst_bytes_per_pixel
if i >= dst.length() {
// TODO: advance args.src if the dst pixel_buffer bounds is
// smaller than this BMP's image bounds?
continue.inner
if this.bits_per_pixel > 32 {
return "#unsupported BMP file"
}
src_bytes_per_pixel = this.bits_per_pixel / 8
if src_bytes_per_pixel == 0 {
return "#unsupported BMP file"
}
n = args.src.length() / (src_bytes_per_pixel as base.u64)
n = n.min(a: (this.width ~mod- this.dst_x) as base.u64)
j = n
while j >= 8 {
if args.src.length() >= ((src_bytes_per_pixel * 8) as base.u64) {
args.src.skip_u32_fast!(
actual: src_bytes_per_pixel * 8,
worst_case: src_bytes_per_pixel * 8)
}
j -= 8
} endwhile
while j > 0 {
if args.src.length() >= ((src_bytes_per_pixel * 1) as base.u64) {
args.src.skip_u32_fast!(
actual: src_bytes_per_pixel * 1,
worst_case: src_bytes_per_pixel * 1)
}
j -= 1
} endwhile
} else {
n = this.swizzler.swizzle_interleaved_from_reader!(
dst: dst[i ..],
dst_palette: dst_palette,
src: args.src)
}
n = this.swizzler.swizzle_interleaved_from_reader!(
dst: dst[i ..],
dst_palette: dst_palette,
src: args.src)
if n == 0 {
return "@internal note: short read"
}
@@ -943,15 +969,13 @@ pri func decoder.swizzle_bitfields!(dst: ptr base.pixel_buffer, src: base.io_rea
}
i = (this.dst_x as base.u64) * dst_bytes_per_pixel
if i >= dst.length() {
// TODO: advance args.src if the dst pixel_buffer bounds is
// smaller than this BMP's image bounds?
continue.inner
n = p0 as base.u64
} else {
n = this.swizzler.swizzle_interleaved_from_slice!(
dst: dst[i ..],
dst_palette: dst_palette,
src: this.scratch[.. 8 * p0])
}
n = this.swizzler.swizzle_interleaved_from_slice!(
dst: dst[i ..],
dst_palette: dst_palette,
src: this.scratch[.. 8 * p0])
if n == 0 {
return "@internal note: short read"
}
@@ -975,8 +999,9 @@ pri func decoder.swizzle_low_bit_depth!(dst: ptr base.pixel_buffer, src: base.io
var p0 : base.u32[..= 543]
var chunk_bits : base.u32
var chunk_count : base.u32
var chunk_bits : base.u32
var chunk_count : base.u32
var pixels_per_chunk : base.u32[..= 32]
// 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()".
@@ -1005,8 +1030,34 @@ pri func decoder.swizzle_low_bit_depth!(dst: ptr base.pixel_buffer, src: base.io
}
i = (this.dst_x as base.u64) * dst_bytes_per_pixel
if i >= dst.length() {
// TODO: advance args.src if the dst pixel_buffer bounds is
// smaller than this BMP's image bounds?
if this.bits_per_pixel == 1 {
chunk_count = ((this.width ~sat- this.dst_x) + 31) / 32
pixels_per_chunk = 32
} else if this.bits_per_pixel == 2 {
chunk_count = ((this.width ~sat- this.dst_x) + 15) / 16
pixels_per_chunk = 16
} else {
chunk_count = ((this.width ~sat- this.dst_x) + 7) / 8
pixels_per_chunk = 8
}
while (chunk_count >= 64) and (args.src.length() >= 256) {
args.src.skip_u32_fast!(actual: 256, worst_case: 256)
this.dst_x = this.width.min(a: this.dst_x ~mod+ (pixels_per_chunk * 64))
chunk_count -= 64
} endwhile
while (chunk_count >= 8) and (args.src.length() >= 32) {
args.src.skip_u32_fast!(actual: 32, worst_case: 32)
this.dst_x = this.width.min(a: this.dst_x ~mod+ (pixels_per_chunk * 8))
chunk_count -= 8
} endwhile
while chunk_count > 0 {
if args.src.length() < 4 {
return "@internal note: short read"
}
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
this.dst_x = this.width.min(a: this.dst_x ~mod+ (pixels_per_chunk * 1))
chunk_count -= 1
} endwhile
continue.loop
}
dst = dst[i ..]
@@ -1084,7 +1135,7 @@ pri func decoder.swizzle_low_bit_depth!(dst: ptr base.pixel_buffer, src: base.io
chunk_count -= 1
} endwhile
} else if this.bits_per_pixel == 4 {
} else {
// Calculate the remaining number of 32-bit chunks. At 4 bits per
// pixel there are 8 pixels per chunk. Division rounds up.
chunk_count = ((this.width ~sat- this.dst_x) + 7) / 8
+32 -7
View File
@@ -201,9 +201,11 @@ pri func decoder.swizzle!(dst: ptr base.pixel_buffer, src: base.io_reader) base.
var dst_bits_per_pixel : base.u32[..= 256]
var dst_bytes_per_pixel : base.u64[..= 32]
var dst_bytes_per_row : base.u64
var src_bytes_per_pixel : base.u32[..= 8]
var tab : table base.u8
var dst : slice base.u8
var i : base.u64
var j : base.u64
var n : base.u64
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
@@ -232,14 +234,37 @@ pri func decoder.swizzle!(dst: ptr base.pixel_buffer, src: base.io_reader) base.
}
i = (this.dst_x as base.u64) * dst_bytes_per_pixel
if i >= dst.length() {
// TODO: advance args.src if the dst pixel_buffer bounds is
// smaller than this NIE's image bounds?
continue
src_bytes_per_pixel = 4
assert src_bytes_per_pixel > 0
if this.pixfmt == base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE {
src_bytes_per_pixel = 8
assert src_bytes_per_pixel > 0
}
n = args.src.length() / (src_bytes_per_pixel as base.u64)
n = n.min(a: (this.width ~mod- this.dst_x) as base.u64)
j = n
while j >= 8 {
if args.src.length() >= ((src_bytes_per_pixel * 8) as base.u64) {
args.src.skip_u32_fast!(
actual: src_bytes_per_pixel * 8,
worst_case: src_bytes_per_pixel * 8)
}
j -= 8
} endwhile
while j > 0 {
if args.src.length() >= ((src_bytes_per_pixel * 1) as base.u64) {
args.src.skip_u32_fast!(
actual: src_bytes_per_pixel * 1,
worst_case: src_bytes_per_pixel * 1)
}
j -= 1
} endwhile
} else {
n = this.swizzler.swizzle_interleaved_from_reader!(
dst: dst[i ..],
dst_palette: args.dst.palette(),
src: args.src)
}
n = this.swizzler.swizzle_interleaved_from_reader!(
dst: dst[i ..],
dst_palette: args.dst.palette(),
src: args.src)
if n == 0 {
return "@internal note: short read"
}