Skip to content

Instantly share code, notes, and snippets.

@EncodeTheCode
Created June 23, 2025 06:59
Show Gist options
  • Save EncodeTheCode/490ebf44c5fcecd52ad4988057257444 to your computer and use it in GitHub Desktop.
Save EncodeTheCode/490ebf44c5fcecd52ad4988057257444 to your computer and use it in GitHub Desktop.
// stb_image_wrapper.c - Safe, optimized stb_image DLL interface
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#ifdef __cplusplus
extern "C" {
#endif
// -----------------------------
// Cross-platform export macro
// -----------------------------
#if defined(_WIN32) || defined(__CYGWIN__)
#ifdef BUILDING_STB_IMAGE_DLL
#define API __declspec(dllexport)
#else
#define API __declspec(dllimport)
#endif
#else
#define API __attribute__((visibility("default")))
#endif
#include <stdint.h>
#include <stddef.h>
// -----------------------------
// Utility null guard macro
// -----------------------------
#define CHECK_NULL(...) \
do { if (!( __VA_ARGS__ )) return 0; } while (0)
// -----------------------------
// 8-bit image loading
// -----------------------------
API unsigned char* load_image(
const char* filename, int* w, int* h, int* ch, int desired_ch) {
if (!filename || !w || !h || !ch) return NULL;
return stbi_load(filename, w, h, ch, desired_ch);
}
API unsigned char* load_image_from_memory(
const unsigned char* buffer, int len, int* w, int* h, int* ch, int desired_ch) {
if (!buffer || len <= 0 || !w || !h || !ch) return NULL;
return stbi_load_from_memory(buffer, len, w, h, ch, desired_ch);
}
// -----------------------------
// 16-bit image loading
// -----------------------------
API unsigned short* load_16bit_image(
const char* filename, int* w, int* h, int* ch, int desired_ch) {
if (!filename || !w || !h || !ch) return NULL;
return stbi_load_16(filename, w, h, ch, desired_ch);
}
API unsigned short* load_16bit_image_from_memory(
const unsigned char* buffer, int len, int* w, int* h, int* ch, int desired_ch) {
if (!buffer || len <= 0 || !w || !h || !ch) return NULL;
return stbi_load_16_from_memory(buffer, len, w, h, ch, desired_ch);
}
// -----------------------------
// HDR (float) image loading
// -----------------------------
API float* load_hdr_image(
const char* filename, int* w, int* h, int* ch, int desired_ch) {
if (!filename || !w || !h || !ch) return NULL;
return stbi_loadf(filename, w, h, ch, desired_ch);
}
API float* load_hdr_image_from_memory(
const unsigned char* buffer, int len, int* w, int* h, int* ch, int desired_ch) {
if (!buffer || len <= 0 || !w || !h || !ch) return NULL;
return stbi_loadf_from_memory(buffer, len, w, h, ch, desired_ch);
}
// -----------------------------
// Image info / header inspection
// -----------------------------
API int get_image_info(const char* filename, int* w, int* h, int* ch) {
CHECK_NULL(filename && w && h && ch);
return stbi_info(filename, w, h, ch);
}
API int get_image_info_from_memory(const unsigned char* buffer, int len, int* w, int* h, int* ch) {
CHECK_NULL(buffer && len > 0 && w && h && ch);
return stbi_info_from_memory(buffer, len, w, h, ch);
}
API int is_hdr(const char* filename) {
return filename ? stbi_is_hdr(filename) : 0;
}
API int is_hdr_from_memory(const unsigned char* buffer, int len) {
return (buffer && len > 0) ? stbi_is_hdr_from_memory(buffer, len) : 0;
}
// -----------------------------
// stb_image global state control
// -----------------------------
API void set_flip_vertically_on_load(int flip) {
stbi_set_flip_vertically_on_load(flip);
}
API void set_unpremultiply_on_load(int flag) {
stbi_set_unpremultiply_on_load(flag);
}
API void set_convert_iphone_png_to_rgb(int flag) {
stbi_convert_iphone_png_to_rgb(flag);
}
// -----------------------------
// Error handling
// -----------------------------
API const char* get_failure_reason() {
return stbi_failure_reason();
}
// -----------------------------
// Memory deallocation
// -----------------------------
API void free_image(void* data) {
if (data) stbi_image_free(data);
}
// -----------------------------
// UTF-16 support on Windows
// -----------------------------
#ifdef _WIN32
#include <wchar.h>
API unsigned char* load_image_w(const wchar_t* filename, int* w, int* h, int* ch, int desired_ch) {
if (!filename || !w || !h || !ch) return NULL;
return stbi_load_w(filename, w, h, ch, desired_ch);
}
#endif
#ifdef __cplusplus
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment