From 572b26a846b743933962719fde4a737da24db305 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Tue, 9 Sep 2025 17:13:21 +0200 Subject: [PATCH] Expand grayscale JPEGs to RGB(A) And handle grayscale jpeg example file in test suite. --- core/image/jpeg/jpeg.odin | 34 +++++++++++++++++++-------- tests/core/.gitignore | 2 ++ tests/core/download_assets.py | 1 + tests/core/image/test_core_image.odin | 8 +++++++ 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/core/image/jpeg/jpeg.odin b/core/image/jpeg/jpeg.odin index 391b4316c..a2ad7e61e 100644 --- a/core/image/jpeg/jpeg.odin +++ b/core/image/jpeg/jpeg.odin @@ -190,6 +190,10 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a options -= {.return_header} } + if .do_not_expand_channels in options || .do_not_expand_grayscale in options { + return img, .Unsupported_Option + } + first := compress.read_u8(ctx) or_return soi := cast(image.JPEG_Marker)compress.read_u8(ctx) or_return if first != 0xFF && soi != .SOI { @@ -941,16 +945,25 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a } } + orig_channels := img.channels + + // We automatically expand grayscale images to RGB + if img.channels == 1 { + img.channels += 2 + } + if .alpha_add_if_missing in options { - img.channels += 1 + img.channels += 1 + orig_channels += 1 } if resize(&img.pixels.buf, img.width * img.height * img.channels) != nil { return img, .Unable_To_Allocate_Or_Resize } - switch img.channels { - case 1: + switch orig_channels { + case 1: // Grayscale JPEG expanded to RGB + out := mem.slice_data_cast([]image.RGB_Pixel, img.pixels.buf[:]) out_idx := 0 for y in 0..