From 77e978cae58fd2c701bd0cd73168dc28baa2a21f Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 21 Apr 2026 16:48:07 +0300 Subject: [PATCH] stbi: fix c.stbi_load failing on clang windows (fixes #17248) --- vlib/gg/image_private_test.v | 10 ++++++++++ vlib/stbi/stbi.c.v | 16 ++++++++++++++++ vlib/stbi/stbi_test.v | 11 +++++++++++ 3 files changed, 37 insertions(+) diff --git a/vlib/gg/image_private_test.v b/vlib/gg/image_private_test.v index f41294269..a79fdc9b2 100644 --- a/vlib/gg/image_private_test.v +++ b/vlib/gg/image_private_test.v @@ -3,6 +3,16 @@ module gg import os +const background_path = os.join_path(@VEXEROOT, 'examples/flappylearning/assets/img/background.png') + +fn test_load_image_loads_supported_file_contents() { + img := load_image(background_path)! + assert img.width > 0 + assert img.height > 0 + assert img.nr_channels == 4 + assert img.path == background_path +} + fn test_load_image_returns_error_for_unsupported_file_contents() { file := os.join_path(os.vtmp_dir(), 'gg_unsupported_image_${os.getpid()}.jpg') os.write_file(file, 'not an image') or { panic(err) } diff --git a/vlib/stbi/stbi.c.v b/vlib/stbi/stbi.c.v index 75f8c7196..20c23bad1 100644 --- a/vlib/stbi/stbi.c.v +++ b/vlib/stbi/stbi.c.v @@ -4,6 +4,8 @@ module stbi +import os + @[if trace_stbi_allocations ?] fn trace_allocation(message string) { eprintln(message) @@ -129,6 +131,20 @@ pub: // Converting/resizing it, should work fine though. pub fn load(path string, params LoadParams) !Image { ext := path.all_after_last('.') + $if windows { + $if clang { + // stb's file-based loader crashes on Windows when compiled with clang. + // Read the file through V and decode the in-memory bytes instead. + file_bytes := os.read_bytes(path) or { + return error('stbi_image failed to load from "${path}"') + } + mut img := load_from_memory(file_bytes.data, file_bytes.len, params) or { + return error('stbi_image failed to load from "${path}"') + } + img.ext = ext + return img + } + } mut res := Image{ ok: true ext: ext diff --git a/vlib/stbi/stbi_test.v b/vlib/stbi/stbi_test.v index 4105bb3e0..8b8211326 100644 --- a/vlib/stbi/stbi_test.v +++ b/vlib/stbi/stbi_test.v @@ -101,3 +101,14 @@ fn test_load_image_from_memory_with_channels_different_than_4() { assert img3.nr_channels == 3, 'load_from_memory should preserve the source channel count when desired_channels is 0' assert img3.original_nr_channels == 3 } + +fn test_load_from_file_matches_memory_decode() { + background_bytes := os.read_bytes(background_path)! + img_from_file := stbi.load(background_path)! + img_from_memory := stbi.load_from_memory(background_bytes.data, background_bytes.len)! + assert img_from_file.width == img_from_memory.width + assert img_from_file.height == img_from_memory.height + assert img_from_file.nr_channels == img_from_memory.nr_channels + assert img_from_file.original_nr_channels == img_from_memory.original_nr_channels + assert img_from_file.ext == 'png' +} -- 2.39.5