From 7b3ca701e0266cf2ffd4a8ee4b332ae569740fd1 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Tue, 9 Sep 2025 14:51:16 +0200 Subject: [PATCH] Implement .alpha_add_if_missing for JPEG --- core/image/jpeg/jpeg.odin | 98 ++++++++++++++++++++++----- tests/core/image/test_core_image.odin | 26 +++---- 2 files changed, 95 insertions(+), 29 deletions(-) diff --git a/core/image/jpeg/jpeg.odin b/core/image/jpeg/jpeg.odin index 58c6dff50..391b4316c 100644 --- a/core/image/jpeg/jpeg.odin +++ b/core/image/jpeg/jpeg.odin @@ -927,9 +927,9 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a cbcr_pixel_column := k / luma_h_sampling_factor + 4 * h cbcr_pixel := cbcr_pixel_row * BLOCK_SIZE + cbcr_pixel_column - r := cast(i16)math.clamp(cast(f32)y_blk[.Y][i] + 1.402 * cast(f32)cbcr_blk[.Cr][cbcr_pixel] + 128, 0, 255) - g := cast(i16)math.clamp(cast(f32)y_blk[.Y][i] - 0.344 * cast(f32)cbcr_blk[.Cb][cbcr_pixel] - 0.714 * cast(f32)cbcr_blk[.Cr][cbcr_pixel] + 128, 0, 255) - b := cast(i16)math.clamp(cast(f32)y_blk[.Y][i] + 1.772 * cast(f32)cbcr_blk[.Cb][cbcr_pixel] + 128, 0, 255) + r := cast(i16)clamp(cast(f32)y_blk[.Y][i] + 1.402 * cast(f32)cbcr_blk[.Cr][cbcr_pixel] + 128, 0, 255) + g := cast(i16)clamp(cast(f32)y_blk[.Y][i] - 0.344 * cast(f32)cbcr_blk[.Cb][cbcr_pixel] - 0.714 * cast(f32)cbcr_blk[.Cr][cbcr_pixel] + 128, 0, 255) + b := cast(i16)clamp(cast(f32)y_blk[.Y][i] + 1.772 * cast(f32)cbcr_blk[.Cb][cbcr_pixel] + 128, 0, 255) y_blk[.Y][i] = r y_blk[.Cb][i] = g @@ -941,28 +941,94 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a } } + if .alpha_add_if_missing in options { + img.channels += 1 + } + if resize(&img.pixels.buf, img.width * img.height * img.channels) != nil { return img, .Unable_To_Allocate_Or_Resize } - out := mem.slice_data_cast([]image.RGB_Pixel, img.pixels.buf[:]) - for y in 0..