From 4861da329847846fefc19c98bc071dd45cff16e2 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Fri, 25 Sep 2026 20:06:15 -0500 Subject: [PATCH] Build without stb_image when -Dstb-image=false The option already left stb_image and stb_image_write out of the build, but dvui still called them, so an application that turned it off got undefined symbols. It now reaches the source as default_options' stb_image (dvui.useStbImage): an ImageSource.imageFile fails with StbImageError and a log line instead of calling stb, and PNGEncoder and JPGEncoder are a compile error when something uses them. An application that decodes and encodes its own images and hands dvui pixels builds without stb at all. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012zMkSofB1qCR4M4bBFQ2ro --- build.zig | 5 +++++ src/Color.zig | 4 ++++ src/JPGEncoder.zig | 1 + src/PNGEncoder.zig | 1 + src/Texture.zig | 4 ++++ src/dvui.zig | 6 ++++++ 6 files changed, 21 insertions(+) diff --git a/build.zig b/build.zig index 490ae7d4..2964566d 100644 --- a/build.zig +++ b/build.zig @@ -1261,6 +1261,11 @@ const DvuiModuleOptions = struct { "freetype", self.freetype orelse @panic("makeDefaults: freetype was null"), ); + ret.addOption( + bool, + "stb_image", + self.stb_image orelse @panic("makeDefaults: stb_image was null"), + ); ret.addOption( bool, "tree_sitter", diff --git a/src/Color.zig b/src/Color.zig index d4001415..19cb9599 100644 --- a/src/Color.zig +++ b/src/Color.zig @@ -394,6 +394,10 @@ pub const PMAImage = struct { /// the returned []PMA inside PMAImage is allocated with alloc pub fn fromImageFile(dbg_name: []const u8, alloc: std.mem.Allocator, image_bytes: []const u8) !PMAImage { + if (comptime !dvui.useStbImage) { + dvui.log.warn("imageTexture cannot decode image \"{s}\": dvui was built without stb_image", .{dbg_name}); + return dvui.StbImageError.stbImageError; + } var w: c_int = undefined; var h: c_int = undefined; var channels_in_file: c_int = undefined; diff --git a/src/JPGEncoder.zig b/src/JPGEncoder.zig index 32e9e899..b02d1548 100644 --- a/src/JPGEncoder.zig +++ b/src/JPGEncoder.zig @@ -56,6 +56,7 @@ pub fn write(output: *std.Io.Writer, pixels: []u8, width: u32, height: u32) !voi /// `density` is in pixels per inch (2.54 cm). /// `density == 0` => don't write custom density pub fn writeWithQuality(self: *JPGEncoder, pixels: []u8, width: u32, height: u32, quality: u7) !void { + if (comptime !dvui.useStbImage) @compileError("JPGEncoder needs stb_image_write, and dvui was built with -Dstb-image=false"); const res = dvui.c.stbi_write_jpg_to_func( &stbi_write_jpg_callback, self, diff --git a/src/PNGEncoder.zig b/src/PNGEncoder.zig index 64947778..14cae373 100644 --- a/src/PNGEncoder.zig +++ b/src/PNGEncoder.zig @@ -35,6 +35,7 @@ pub fn write(output: *std.Io.Writer, pixels: []u8, width: u32, height: u32) !voi /// `resolution == 0` => don't write the pHYs chunk pub fn writeWithResolution(output: *std.Io.Writer, pixels: []u8, width: u32, height: u32, resolution: u32) !void { + if (comptime !dvui.useStbImage) @compileError("PNGEncoder needs stb_image_write, and dvui was built with -Dstb-image=false"); std.debug.assert(output.buffer.len >= min_buffer_size); var png_encoder = PNGEncoder{ .resolution = resolution, diff --git a/src/Texture.zig b/src/Texture.zig index 14b1ba11..6db3d40d 100644 --- a/src/Texture.zig +++ b/src/Texture.zig @@ -302,6 +302,10 @@ pub const ImageSource = union(enum) { pub fn size(source: ImageSource) !Size { switch (source) { .imageFile => |file| { + if (comptime !dvui.useStbImage) { + dvui.log.warn("imageSize cannot read image \"{s}\": dvui was built without stb_image", .{file.name}); + return StbImageError.stbImageError; + } var w: c_int = undefined; var h: c_int = undefined; var n: c_int = undefined; diff --git a/src/dvui.zig b/src/dvui.zig index 5bd012b1..ab5e53ba 100644 --- a/src/dvui.zig +++ b/src/dvui.zig @@ -326,6 +326,12 @@ pub const useLibc = @import("default_options").libc; pub const useFreeType = @import("default_options").freetype; pub const useTinyFileDialogs = @import("default_options").tiny_file_dialogs; pub const useTreeSitter = @import("default_options").tree_sitter; +/// Whether stb_image and stb_image_write are built in (`-Dstb-image`). +/// Without them an `ImageSource.imageFile` cannot be decoded and fails with +/// `StbImageError`, and `PNGEncoder` and `JPGEncoder` do not compile: an +/// application that decodes and encodes its own images hands dvui pixels +/// and needs neither. +pub const useStbImage = @import("default_options").stb_image; /// The amount of logical pixels to scroll per "tick" of the scroll wheel. /// This variable is provided in case you need a quick fix. If you need to -- knotmirror 0.0.1