Implementation:Ggml org Ggml Stb image write
| File Name | examples/stb_image_write.h
|
| Repository | ggml-org/ggml |
| Lines | 1724 |
| Language | C |
| Domain Tags | Image_Processing, Third_Party, IO |
| Status | Active |
| Last Updated | 2025-05-15 12:00 GMT |
| Knowledge Sources | ggml-org/ggml repository |
Overview
examples/stb_image_write.h is a third-party single-header public domain image writing library (stb_image_write v1.16) by Sean Barrett. It is vendored as a dependency for example programs that need to save image output (e.g., SAM segmentation masks). It complements stb_image.h for complete image I/O without external library dependencies.
Description
The library provides functions to write images to files or callbacks in five formats:
- PNG -- With configurable compression level and filter mode (output is 20-50% larger than optimized implementations)
- BMP -- Simple bitmap format
- TGA -- With optional RLE compression
- JPEG -- With quality parameter
- HDR -- Radiance HDR format (float data)
Designed for simplicity over optimal file size. Uses the same single-header implementation pattern (define STB_IMAGE_WRITE_IMPLEMENTATION before including). Supports custom zlib compression (STBIW_ZLIB_COMPRESS), custom memory allocators, and Unicode filenames on Windows.
Usage
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
// Write PNG
stbi_write_png("output.png", width, height, channels, data, stride);
// Write JPEG with quality
stbi_write_jpg("output.jpg", width, height, channels, data, quality);
Code Reference
Source Location
| Repository | File | Lines |
|---|---|---|
| ggml-org/ggml | examples/stb_image_write.h |
1724 |
Key Signatures
// File-based writing
int stbi_write_png(char const * filename, int w, int h, int comp, const void * data, int stride_in_bytes);
int stbi_write_bmp(char const * filename, int w, int h, int comp, const void * data);
int stbi_write_tga(char const * filename, int w, int h, int comp, const void * data);
int stbi_write_jpg(char const * filename, int w, int h, int comp, const void * data, int quality);
int stbi_write_hdr(char const * filename, int w, int h, int comp, const float * data);
// Callback-based writing
int stbi_write_png_to_func(stbi_write_func * func, void * context, int w, int h, int comp,
const void * data, int stride_in_bytes);
int stbi_write_bmp_to_func(stbi_write_func * func, void * context, int w, int h, int comp,
const void * data);
// Configuration
void stbi_flip_vertically_on_write(int flag);
extern int stbi_write_png_compression_level; // default: 8
extern int stbi_write_tga_with_rle; // default: 1
I/O Contract
Inputs
- Pixel data -- Image data as contiguous array (left-to-right, top-to-bottom)
- Dimensions -- Width, height, and component count (1-4)
- Format-specific parameters -- Stride (PNG), quality (JPEG)
Outputs
- Image file -- Written to specified path or callback
- Return value -- 0 on failure, non-zero on success
Usage Examples
Saving inference output:
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
// Save a segmentation mask from SAM inference
unsigned char * mask = compute_segmentation_mask(model, input);
stbi_write_png("segmentation_mask.png", width, height, 1, mask, width);
// Save with callback for custom I/O
stbi_write_png_to_func(my_write_func, my_context, w, h, comp, data, stride);
Related Pages
Implements Principle
Related Implementations
- Implementation:Ggml_org_Ggml_Stb_image -- Companion image reading library
- Implementation:Ggml_org_Ggml_Sam_image_preprocess -- SAM example using image I/O