Commit156bd449Recorded6 Sep 2026Repositorysigil-image

Add bounded memory PNG and JPEG normalization for Sigil

Changed
 .gitignore                   |    2 +
 AGENTS.md                    |   11 +
 LICENSE                      |   27 +
 README.md                    |   49 +
 native/image.c               |   52 ++
 package.sgl                  |   34 +
 src/sigil/image.sgl          |   33 +
 src/sigil/image/native.sgl   |    4 +
 test/fixtures/logo.jpg       |  Bin 0 -> 311 bytes
 test/fixtures/logo.png       |  Bin 0 -> 203 bytes
 test/test-image.sgl          |   58 ++
 vendor/stb/README.md         |    7 +
 vendor/stb/stb_image.h       | 7988 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 vendor/stb/stb_image_write.h | 1724 +++++++++++++++++++++++++++++++++++
 14 files changed, 9989 insertions(+)
Diff
.gitignoreadded
@@ -0,0 +1,2 @@
+1
build/
+2
.sigil/
AGENTS.mdadded
@@ -0,0 +1,11 @@
+1
# sigil-image
+2
+3
Use hierarchical modules: `(sigil image)` and `(sigil image native)`.
+4
Keep the codec memory-only and independent of desktop graphics and applications.
+5
Keep explicit input, dimension and pixel budgets before allocating decoded pixels.
+6
Do not add upload permissions, storage configuration or customer policy here.
+7
Preserve STB provenance and license notices when updating vendored headers.
+8
Format Sigil with readable line breaks and consistent indentation; do not compress
+9
multiple bindings, statements or condition branches onto one line.
+10
Run `sigil build` and `sigil test --sgl --strict` after codec changes. Keep tests
+11
self-contained with synthetic fixtures and README limits accurate.
LICENSEadded
@@ -0,0 +1,27 @@
+1
BSD 3-Clause License
+2
+3
Copyright (c) 2026, David Wilson
+4
All rights reserved.
+5
+6
Redistribution and use in source and binary forms, with or without
+7
modification, are permitted provided that the following conditions are met:
+8
+9
1. Redistributions of source code must retain the above copyright notice, this
+10
list of conditions and the following disclaimer.
+11
2. Redistributions in binary form must reproduce the above copyright notice,
+12
this list of conditions and the following disclaimer in the documentation
+13
and/or other materials provided with the distribution.
+14
3. Neither the name of the copyright holder nor the names of its contributors
+15
may be used to endorse or promote products derived from this software without
+16
specific prior written permission.
+17
+18
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+19
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+20
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+21
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+22
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+23
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+24
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+25
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+26
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+27
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
README.mdadded
@@ -0,0 +1,49 @@
+1
# sigil-image
+2
+3
Memory-based PNG and JPEG decoding with PNG output for Sigil servers and tools.
+4
The `(sigil image)` module uses STB without desktop graphics, windowing, GPU or
+5
filesystem dependencies. Existing `(sigil graphics image)` APIs are unchanged.
+6
+7
## Use
+8
+9
```scheme
+10
(import (sigil core)
+11
(sigil image))
+12
+13
(define result
+14
(image-normalize-png input-bytes
+15
max-dimension: 2048
+16
max-pixels: 4194304
+17
max-input-bytes: 2097152))
+18
+19
(when result
+20
(dict-ref result bytes:) ; RGBA PNG bytevector
+21
(dict-ref result width:)
+22
(dict-ref result height:))
+23
```
+24
+25
Invalid, unsupported or oversized images return `#f`. Invalid argument types or
+26
limits raise an error. Successful normalization decodes the pixels and writes a
+27
new PNG, dropping source metadata. Claimed filenames and MIME types are not used.
+28
The default limits are shown above. Maximum configurable limits are 8192 pixels
+29
per side, 16,777,216 pixels and 16,777,216 input bytes. Decoding is synchronous and
+30
buffered; callers must also bound transport bodies, concurrency and request rates.
+31
+32
Only PNG and JPEG input is supported. EXIF orientation and animation are not
+33
interpreted. The API does not retain color profiles, offer general resizing, or
+34
provide file storage, authentication or application upload policy.
+35
+36
## Build and verify
+37
+38
```sh
+39
sigil deps install
+40
sigil build
+41
sigil test --sgl --strict
+42
```
+43
+44
Requires Sigil 0.21 and its native build toolchain. The tests use tiny synthetic
+45
PNG/JPEG fixtures and check conversion, dimensions, metadata removal, malformed
+46
input and decoding budgets. No network service or credentials are needed.
+47
+48
BSD-3-Clause for the Sigil wrapper; see LICENSE. Vendored STB retains its upstream
+49
MIT/public-domain license notices. See vendor/stb/README.md for provenance.
native/image.cadded
@@ -0,0 +1,52 @@
+1
/* Memory-only STB codec. No windowing, GPU or filesystem dependencies. */
+2
#include "sigil-internal.h"
+3
#include <stdlib.h>
+4
#include <string.h>
+5
#define STBI_ONLY_PNG
+6
#define STBI_ONLY_JPEG
+7
#define STBI_NO_STDIO
+8
#define STBI_MAX_DIMENSIONS 8192
+9
#define STB_IMAGE_STATIC
+10
#define STB_IMAGE_IMPLEMENTATION
+11
#include "stb_image.h"
+12
#define STBI_WRITE_NO_STDIO
+13
#define STB_IMAGE_WRITE_STATIC
+14
#define STB_IMAGE_WRITE_IMPLEMENTATION
+15
#include "stb_image_write.h"
+16
+17
static Value raster_png(SigilVM *vm, int argc, Value *args) {
+18
(void)argc;
+19
if (!sigil_is_fixnum(args[1]) || !sigil_is_fixnum(args[2]) || !sigil_is_fixnum(args[3])) return SIGIL_FALSE;
+20
int side = (int)sigil_as_fixnum(args[1]);
+21
int pixels_limit = (int)sigil_as_fixnum(args[2]);
+22
int input_limit = (int)sigil_as_fixnum(args[3]);
+23
if (side < 1 || side > 8192 || pixels_limit < 1 || pixels_limit > 16777216 || input_limit < 1 || input_limit > 16777216) return SIGIL_FALSE;
+24
if (!sigil_is_bytevector(args[0])) return SIGIL_FALSE;
+25
SigilBytevector *input = (SigilBytevector *)sigil_as_ptr(args[0]);
+26
if (input->length == 0 || input->length > (size_t)input_limit) return SIGIL_FALSE;
+27
int width, height, channels;
+28
if (!stbi_info_from_memory(input->data, (int)input->length, &width, &height, &channels)
+29
|| width <= 0 || height <= 0 || width > side || height > side
+30
|| (size_t)width * (size_t)height > (size_t)pixels_limit) return SIGIL_FALSE;
+31
unsigned char *pixels = stbi_load_from_memory(input->data, (int)input->length,
+32
&width, &height, &channels, 4);
+33
if (!pixels) return SIGIL_FALSE;
+34
int size = 0;
+35
unsigned char *png = stbi_write_png_to_mem(pixels, width * 4, width, height, 4, &size);
+36
stbi_image_free(pixels);
+37
if (!png) return SIGIL_FALSE;
+38
if (size <= 0 || size > 80 * 1024 * 1024) { STBIW_FREE(png); return SIGIL_FALSE; }
+39
Value result = sigil_make_bytevector(vm, (size_t)size);
+40
if (sigil_is_bytevector(result)) memcpy(sigil_bytevector_data(result), png, (size_t)size);
+41
STBIW_FREE(png);
+42
return result;
+43
}
+44
+45
void sigil__init_sigil_image_module(SigilVM *vm) {
+46
SigilModule *module = sigil_begin_module(vm, "(sigil image native)");
+47
if (!module) return;
+48
sigil_module_register_native(vm, "normalize-png-bytes", raster_png, SIGIL_ARITY_EXACT(4),
+49
"Decode bounded PNG/JPEG bytes and encode a metadata-free PNG.");
+50
sigil_module_export(vm, "normalize-png-bytes");
+51
sigil_end_module(vm);
+52
}
package.sgladded
@@ -0,0 +1,34 @@
+1
(package
+2
name: "sigil-image"
+3
version: "0.1.0"
+4
sigil: "0.21"
+5
description: "Memory-based raster image decoding and PNG encoding for Sigil"
+6
license: "BSD-3-Clause"
+7
+8
configs: (list
+9
(config name: 'dev output-dir: "build/dev" debug?: #t optimize: 0)
+10
(config name: 'release output-dir: "build/release" debug?: #f optimize: 2))
+11
+12
libraries: (list
+13
(library name: "sigil-image"
+14
c-sources: '("native/image.c")
+15
c-include-dirs: '("vendor/stb")
+16
native-init: "sigil__init_sigil_image_module"))
+17
+18
dev-dependencies: (list
+19
(from-git url: "codeberg:sigil/sigil"
+20
package: "sigil-test" version: "0.21.0")
+21
(from-git url: "codeberg:sigil/sigil"
+22
package: "sigil-test-runner" version: "0.21.0"))
+23
+24
tasks: (list
+25
(task name: 'build
+26
steps: (list
+27
(compile-c-sources
+28
sources: '("native/image.c")
+29
include-dirs: '("vendor/stb")
+30
flags: (with-sigil-c-flags '("-std=c99" "-Wno-unused-function")))
+31
(create-static-library name: "sigil-image")
+32
(compile-sigil-modules
+33
sources: "src/**/*.sgl"
+34
output-dir: (config-output-subdir "lib"))))))
src/sigil/image.sgladded
@@ -0,0 +1,33 @@
+1
(define-library (sigil image)
+2
(import (sigil core)
+3
(sigil image native))
+4
+5
(export image-normalize-png)
+6
+7
(begin
+8
;;; Decode PNG/JPEG bytes and return #{bytes: width: height:} as RGBA PNG,
+9
;;; removing source metadata. Returns #f for invalid/unsupported/oversized
+10
;;; images. EXIF orientation and animation are not interpreted.
+11
(define (image-normalize-png bytes
+12
(keys: (max-dimension 2048)
+13
(max-pixels 4194304)
+14
(max-input-bytes 2097152)))
+15
(unless (and (bytevector? bytes)
+16
(integer? max-dimension)
+17
(<= 1 max-dimension 8192)
+18
(integer? max-pixels)
+19
(<= 1 max-pixels 16777216)
+20
(integer? max-input-bytes)
+21
(<= 1 max-input-bytes 16777216))
+22
(error "Invalid image input or decode limits"))
+23
(let ((png (normalize-png-bytes bytes max-dimension max-pixels max-input-bytes)))
+24
(and png
+25
(dict bytes: png
+26
width: (read-u32 png 16)
+27
height: (read-u32 png 20)))))
+28
+29
(define (read-u32 bytes at)
+30
(+ (* 16777216 (bytevector-u8-ref bytes at))
+31
(* 65536 (bytevector-u8-ref bytes (+ at 1)))
+32
(* 256 (bytevector-u8-ref bytes (+ at 2)))
+33
(bytevector-u8-ref bytes (+ at 3))))))
src/sigil/image/native.sgladded
@@ -0,0 +1,4 @@
+1
(define-library (sigil image native)
+2
(import (sigil core))
+3
(export normalize-png-bytes)
+4
(begin))
test/fixtures/logo.jpgbinary

Binary. Not rendered here.

test/fixtures/logo.pngbinary

Binary. Not rendered here.

test/test-image.sgladded
@@ -0,0 +1,58 @@
+1
(import (sigil core)
+2
(sigil io)
+3
(sigil test)
+4
(sigil image))
+5
+6
(define (fixture name)
+7
(let* ((port (open-binary-input-file (string-append "test/fixtures/" name)))
+8
(bytes (read-bytevector 65536 port)))
+9
(close-port port)
+10
bytes))
+11
+12
(define png (fixture "logo.png"))
+13
(define jpeg (fixture "logo.jpg"))
+14
+15
(test-group "bounded memory image normalization"
+16
(test "PNG decodes and re-encodes to the same dimensions"
+17
(let ((image (image-normalize-png png)))
+18
(assert-equal (dict-ref image width:) 2)
+19
(assert-equal (dict-ref image height:) 3)
+20
(assert-equal (bytevector-u8-ref (dict-ref image bytes:) 0) 137)
+21
(assert-equal (dict-ref (image-normalize-png (dict-ref image bytes:)) width:) 2)))
+22
+23
(test "JPEG is converted to PNG"
+24
(let ((image (image-normalize-png jpeg)))
+25
(assert-equal (dict-ref image width:) 2)
+26
(assert-equal (dict-ref image height:) 3)
+27
(assert-equal (bytevector-u8-ref (dict-ref image bytes:) 0) 137)))
+28
+29
(test "normalization removes ancillary source metadata"
+30
(let* ((bytes (dict-ref (image-normalize-png png) bytes:))
+31
(port (open-output-string)))
+32
;; Re-encoded PNG contains only IHDR, IDAT and IEND chunks.
+33
(let loop ((at 8))
+34
(when (< at (bytevector-length bytes))
+35
(let ((length (+ (* 16777216 (bytevector-u8-ref bytes at))
+36
(* 65536 (bytevector-u8-ref bytes (+ at 1)))
+37
(* 256 (bytevector-u8-ref bytes (+ at 2)))
+38
(bytevector-u8-ref bytes (+ at 3))))
+39
(kind (utf8->string (bytevector-copy bytes (+ at 4) (+ at 8)))))
+40
(assert-true (if (member kind '("IHDR" "IDAT" "IEND")) #t #f))
+41
(loop (+ at length 12)))))))
+42
+43
(test "invalid, truncated and unsupported bytes are rejected"
+44
(assert-false (image-normalize-png (make-bytevector 0)))
+45
(assert-false (image-normalize-png (string->utf8 "<svg></svg>")))
+46
(assert-false (image-normalize-png (bytevector-copy png 0 30)))))
+47
+48
(test-group "decode budgets"
+49
(test "dimensions, pixels and input bytes are bounded before decoding"
+50
(assert-false (image-normalize-png png max-dimension: 2))
+51
(assert-false (image-normalize-png png max-pixels: 5))
+52
(assert-false (image-normalize-png png max-input-bytes: 8)))
+53
+54
(test "invalid limits and non-bytevector input raise errors"
+55
(assert-error (image-normalize-png "not bytes"))
+56
(assert-error (image-normalize-png png max-dimension: 0))
+57
(assert-error (image-normalize-png png max-pixels: 16777217))
+58
(assert-error (image-normalize-png png max-input-bytes: 16777217))))
vendor/stb/README.mdadded
@@ -0,0 +1,7 @@
+1
# Vendored image codecs
+2
+3
Source: https://github.com/nothings/stb/tree/2c980bb59875b0d32144a71867fbdebb2f77cd20
+4
+5
Files: stb_image.h and stb_image_write.h, unmodified. Their MIT/public-domain
+6
license notices are included in each header. Rezvibe compiles only PNG/JPEG
+7
decoding and PNG encoding, without filesystem access.
vendor/stb/stb_image.hadded
@@ -0,0 +1,7988 @@
+1
/* stb_image - v2.30 - public domain image loader - http://nothings.org/stb
+2
no warranty implied; use at your own risk
+3
+4
Do this:
+5
#define STB_IMAGE_IMPLEMENTATION
+6
before you include this file in *one* C or C++ file to create the implementation.
+7
+8
// i.e. it should look like this:
+9
#include ...
+10
#include ...
+11
#include ...
+12
#define STB_IMAGE_IMPLEMENTATION
+13
#include "stb_image.h"
+14
+15
You can #define STBI_ASSERT(x) before the #include to avoid using assert.h.
+16
And #define STBI_MALLOC, STBI_REALLOC, and STBI_FREE to avoid using malloc,realloc,free
+17
+18
+19
QUICK NOTES:
+20
Primarily of interest to game developers and other people who can
+21
avoid problematic images and only need the trivial interface
+22
+23
JPEG baseline & progressive (12 bpc/arithmetic not supported, same as stock IJG lib)
+24
PNG 1/2/4/8/16-bit-per-channel
+25
+26
TGA (not sure what subset, if a subset)
+27
BMP non-1bpp, non-RLE
+28
PSD (composited view only, no extra channels, 8/16 bit-per-channel)
+29
+30
GIF (*comp always reports as 4-channel)
+31
HDR (radiance rgbE format)
+32
PIC (Softimage PIC)
+33
PNM (PPM and PGM binary only)
+34
+35
Animated GIF still needs a proper API, but here's one way to do it:
+36
http://gist.github.com/urraka/685d9a6340b26b830d49
+37
+38
- decode from memory or through FILE (define STBI_NO_STDIO to remove code)
+39
- decode from arbitrary I/O callbacks
+40
- SIMD acceleration on x86/x64 (SSE2) and ARM (NEON)
+41
+42
Full documentation under "DOCUMENTATION" below.
+43
+44
+45
LICENSE
+46
+47
See end of file for license information.
+48
+49
RECENT REVISION HISTORY:
+50
+51
2.30 (2024-05-31) avoid erroneous gcc warning
+52
2.29 (2023-05-xx) optimizations
+53
2.28 (2023-01-29) many error fixes, security errors, just tons of stuff
+54
2.27 (2021-07-11) document stbi_info better, 16-bit PNM support, bug fixes
+55
2.26 (2020-07-13) many minor fixes
+56
2.25 (2020-02-02) fix warnings
+57
2.24 (2020-02-02) fix warnings; thread-local failure_reason and flip_vertically
+58
2.23 (2019-08-11) fix clang static analysis warning
+59
2.22 (2019-03-04) gif fixes, fix warnings
+60
2.21 (2019-02-25) fix typo in comment
+61
2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs
+62
2.19 (2018-02-11) fix warning
+63
2.18 (2018-01-30) fix warnings
+64
2.17 (2018-01-29) bugfix, 1-bit BMP, 16-bitness query, fix warnings
+65
2.16 (2017-07-23) all functions have 16-bit variants; optimizations; bugfixes
+66
2.15 (2017-03-18) fix png-1,2,4; all Imagenet JPGs; no runtime SSE detection on GCC
+67
2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs
+68
2.13 (2016-12-04) experimental 16-bit API, only for PNG so far; fixes
+69
2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes
+70
2.11 (2016-04-02) 16-bit PNGS; enable SSE2 in non-gcc x64
+71
RGB-format JPEG; remove white matting in PSD;
+72
allocate large structures on the stack;
+73
correct channel count for PNG & BMP
+74
2.10 (2016-01-22) avoid warning introduced in 2.09
+75
2.09 (2016-01-16) 16-bit TGA; comments in PNM files; STBI_REALLOC_SIZED
+76
+77
See end of file for full revision history.
+78
+79
+80
============================ Contributors =========================
+81
+82
Image formats Extensions, features
+83
Sean Barrett (jpeg, png, bmp) Jetro Lauha (stbi_info)
+84
Nicolas Schulz (hdr, psd) Martin "SpartanJ" Golini (stbi_info)
+85
Jonathan Dummer (tga) James "moose2000" Brown (iPhone PNG)
+86
Jean-Marc Lienher (gif) Ben "Disch" Wenger (io callbacks)
+87
Tom Seddon (pic) Omar Cornut (1/2/4-bit PNG)
+88
Thatcher Ulrich (psd) Nicolas Guillemot (vertical flip)
+89
Ken Miller (pgm, ppm) Richard Mitton (16-bit PSD)
+90
github:urraka (animated gif) Junggon Kim (PNM comments)
+91
Christopher Forseth (animated gif) Daniel Gibson (16-bit TGA)
+92
socks-the-fox (16-bit PNG)
+93
Jeremy Sawicki (handle all ImageNet JPGs)
+94
Optimizations & bugfixes Mikhail Morozov (1-bit BMP)
+95
Fabian "ryg" Giesen Anael Seghezzi (is-16-bit query)
+96
Arseny Kapoulkine Simon Breuss (16-bit PNM)
+97
John-Mark Allen
+98
Carmelo J Fdez-Aguera
+99
+100
Bug & warning fixes
+101
Marc LeBlanc David Woo Guillaume George Martins Mozeiko
+102
Christpher Lloyd Jerry Jansson Joseph Thomson Blazej Dariusz Roszkowski
+103
Phil Jordan Dave Moore Roy Eltham
+104
Hayaki Saito Nathan Reed Won Chun
+105
Luke Graham Johan Duparc Nick Verigakis the Horde3D community
+106
Thomas Ruf Ronny Chevalier github:rlyeh
+107
Janez Zemva John Bartholomew Michal Cichon github:romigrou
+108
Jonathan Blow Ken Hamada Tero Hanninen github:svdijk
+109
Eugene Golushkov Laurent Gomila Cort Stratton github:snagar
+110
Aruelien Pocheville Sergio Gonzalez Thibault Reuille github:Zelex
+111
Cass Everitt Ryamond Barbiero github:grim210
+112
Paul Du Bois Engin Manap Aldo Culquicondor github:sammyhw
+113
Philipp Wiesemann Dale Weiler Oriol Ferrer Mesia github:phprus
+114
Josh Tobin Neil Bickford Matthew Gregan github:poppolopoppo
+115
Julian Raschke Gregory Mullen Christian Floisand github:darealshinji
+116
Baldur Karlsson Kevin Schmidt JR Smith github:Michaelangel007
+117
Brad Weinberger Matvey Cherevko github:mosra
+118
Luca Sas Alexander Veselov Zack Middleton [reserved]
+119
Ryan C. Gordon [reserved] [reserved]
+120
DO NOT ADD YOUR NAME HERE
+121
+122
Jacko Dirks
+123
+124
To add your name to the credits, pick a random blank space in the middle and fill it.
+125
80% of merge conflicts on stb PRs are due to people adding their name at the end
+126
of the credits.
+127
*/
+128
+129
#ifndef STBI_INCLUDE_STB_IMAGE_H
+130
#define STBI_INCLUDE_STB_IMAGE_H
+131
+132
// DOCUMENTATION
+133
//
+134
// Limitations:
+135
// - no 12-bit-per-channel JPEG
+136
// - no JPEGs with arithmetic coding
+137
// - GIF always returns *comp=4
+138
//
+139
// Basic usage (see HDR discussion below for HDR usage):
+140
// int x,y,n;
+141
// unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
+142
// // ... process data if not NULL ...
+143
// // ... x = width, y = height, n = # 8-bit components per pixel ...
+144
// // ... replace '0' with '1'..'4' to force that many components per pixel
+145
// // ... but 'n' will always be the number that it would have been if you said 0
+146
// stbi_image_free(data);
+147
//
+148
// Standard parameters:
+149
// int *x -- outputs image width in pixels
+150
// int *y -- outputs image height in pixels
+151
// int *channels_in_file -- outputs # of image components in image file
+152
// int desired_channels -- if non-zero, # of image components requested in result
+153
//
+154
// The return value from an image loader is an 'unsigned char *' which points
+155
// to the pixel data, or NULL on an allocation failure or if the image is
+156
// corrupt or invalid. The pixel data consists of *y scanlines of *x pixels,
+157
// with each pixel consisting of N interleaved 8-bit components; the first
+158
// pixel pointed to is top-left-most in the image. There is no padding between
+159
// image scanlines or between pixels, regardless of format. The number of
+160
// components N is 'desired_channels' if desired_channels is non-zero, or
+161
// *channels_in_file otherwise. If desired_channels is non-zero,
+162
// *channels_in_file has the number of components that _would_ have been
+163
// output otherwise. E.g. if you set desired_channels to 4, you will always
+164
// get RGBA output, but you can check *channels_in_file to see if it's trivially
+165
// opaque because e.g. there were only 3 channels in the source image.
+166
//
+167
// An output image with N components has the following components interleaved
+168
// in this order in each pixel:
+169
//
+170
// N=#comp components
+171
// 1 grey
+172
// 2 grey, alpha
+173
// 3 red, green, blue
+174
// 4 red, green, blue, alpha
+175
//
+176
// If image loading fails for any reason, the return value will be NULL,
+177
// and *x, *y, *channels_in_file will be unchanged. The function
+178
// stbi_failure_reason() can be queried for an extremely brief, end-user
+179
// unfriendly explanation of why the load failed. Define STBI_NO_FAILURE_STRINGS
+180
// to avoid compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly
+181
// more user-friendly ones.
+182
//
+183
// Paletted PNG, BMP, GIF, and PIC images are automatically depalettized.
+184
//
+185
// To query the width, height and component count of an image without having to
+186
// decode the full file, you can use the stbi_info family of functions:
+187
//
+188
// int x,y,n,ok;
+189
// ok = stbi_info(filename, &x, &y, &n);
+190
// // returns ok=1 and sets x, y, n if image is a supported format,
+191
// // 0 otherwise.
+192
//
+193
// Note that stb_image pervasively uses ints in its public API for sizes,
+194
// including sizes of memory buffers. This is now part of the API and thus
+195
// hard to change without causing breakage. As a result, the various image
+196
// loaders all have certain limits on image size; these differ somewhat
+197
// by format but generally boil down to either just under 2GB or just under
+198
// 1GB. When the decoded image would be larger than this, stb_image decoding
+199
// will fail.
+200
//
+201
// Additionally, stb_image will reject image files that have any of their
+202
// dimensions set to a larger value than the configurable STBI_MAX_DIMENSIONS,
+203
// which defaults to 2**24 = 16777216 pixels. Due to the above memory limit,
+204
// the only way to have an image with such dimensions load correctly
+205
// is for it to have a rather extreme aspect ratio. Either way, the
+206
// assumption here is that such larger images are likely to be malformed
+207
// or malicious. If you do need to load an image with individual dimensions
+208
// larger than that, and it still fits in the overall size limit, you can
+209
// #define STBI_MAX_DIMENSIONS on your own to be something larger.
+210
//
+211
// ===========================================================================
+212
//
+213
// UNICODE:
+214
//
+215
// If compiling for Windows and you wish to use Unicode filenames, compile
+216
// with
+217
// #define STBI_WINDOWS_UTF8
+218
// and pass utf8-encoded filenames. Call stbi_convert_wchar_to_utf8 to convert
+219
// Windows wchar_t filenames to utf8.
+220
//
+221
// ===========================================================================
+222
//
+223
// Philosophy
+224
//
+225
// stb libraries are designed with the following priorities:
+226
//
+227
// 1. easy to use
+228
// 2. easy to maintain
+229
// 3. good performance
+230
//
+231
// Sometimes I let "good performance" creep up in priority over "easy to maintain",
+232
// and for best performance I may provide less-easy-to-use APIs that give higher
+233
// performance, in addition to the easy-to-use ones. Nevertheless, it's important
+234
// to keep in mind that from the standpoint of you, a client of this library,
+235
// all you care about is #1 and #3, and stb libraries DO NOT emphasize #3 above all.
+236
//
+237
// Some secondary priorities arise directly from the first two, some of which
+238
// provide more explicit reasons why performance can't be emphasized.
+239
//
+240
// - Portable ("ease of use")
+241
// - Small source code footprint ("easy to maintain")
+242
// - No dependencies ("ease of use")
+243
//
+244
// ===========================================================================
+245
//
+246
// I/O callbacks
+247
//
+248
// I/O callbacks allow you to read from arbitrary sources, like packaged
+249
// files or some other source. Data read from callbacks are processed
+250
// through a small internal buffer (currently 128 bytes) to try to reduce
+251
// overhead.
+252
//
+253
// The three functions you must define are "read" (reads some bytes of data),
+254
// "skip" (skips some bytes of data), "eof" (reports if the stream is at the end).
+255
//
+256
// ===========================================================================
+257
//
+258
// SIMD support
+259
//
+260
// The JPEG decoder will try to automatically use SIMD kernels on x86 when
+261
// supported by the compiler. For ARM Neon support, you must explicitly
+262
// request it.
+263
//
+264
// (The old do-it-yourself SIMD API is no longer supported in the current
+265
// code.)
+266
//
+267
// On x86, SSE2 will automatically be used when available based on a run-time
+268
// test; if not, the generic C versions are used as a fall-back. On ARM targets,
+269
// the typical path is to have separate builds for NEON and non-NEON devices
+270
// (at least this is true for iOS and Android). Therefore, the NEON support is
+271
// toggled by a build flag: define STBI_NEON to get NEON loops.
+272
//
+273
// If for some reason you do not want to use any of SIMD code, or if
+274
// you have issues compiling it, you can disable it entirely by
+275
// defining STBI_NO_SIMD.
+276
//
+277
// ===========================================================================
+278
//
+279
// HDR image support (disable by defining STBI_NO_HDR)
+280
//
+281
// stb_image supports loading HDR images in general, and currently the Radiance
+282
// .HDR file format specifically. You can still load any file through the existing
+283
// interface; if you attempt to load an HDR file, it will be automatically remapped
+284
// to LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
+285
// both of these constants can be reconfigured through this interface:
+286
//
+287
// stbi_hdr_to_ldr_gamma(2.2f);
+288
// stbi_hdr_to_ldr_scale(1.0f);
+289
//
+290
// (note, do not use _inverse_ constants; stbi_image will invert them
+291
// appropriately).
+292
//
+293
// Additionally, there is a new, parallel interface for loading files as
+294
// (linear) floats to preserve the full dynamic range:
+295
//
+296
// float *data = stbi_loadf(filename, &x, &y, &n, 0);
+297
//
+298
// If you load LDR images through this interface, those images will
+299
// be promoted to floating point values, run through the inverse of
+300
// constants corresponding to the above:
+301
//
+302
// stbi_ldr_to_hdr_scale(1.0f);
+303
// stbi_ldr_to_hdr_gamma(2.2f);
+304
//
+305
// Finally, given a filename (or an open file or memory block--see header
+306
// file for details) containing image data, you can query for the "most
+307
// appropriate" interface to use (that is, whether the image is HDR or
+308
// not), using:
+309
//
+310
// stbi_is_hdr(char *filename);
+311
//
+312
// ===========================================================================
+313
//
+314
// iPhone PNG support:
+315
//
+316
// We optionally support converting iPhone-formatted PNGs (which store
+317
// premultiplied BGRA) back to RGB, even though they're internally encoded
+318
// differently. To enable this conversion, call
+319
// stbi_convert_iphone_png_to_rgb(1).
+320
//
+321
// Call stbi_set_unpremultiply_on_load(1) as well to force a divide per
+322
// pixel to remove any premultiplied alpha *only* if the image file explicitly
+323
// says there's premultiplied data (currently only happens in iPhone images,
+324
// and only if iPhone convert-to-rgb processing is on).
+325
//
+326
// ===========================================================================
+327
//
+328
// ADDITIONAL CONFIGURATION
+329
//
+330
// - You can suppress implementation of any of the decoders to reduce
+331
// your code footprint by #defining one or more of the following
+332
// symbols before creating the implementation.
+333
//
+334
// STBI_NO_JPEG
+335
// STBI_NO_PNG
+336
// STBI_NO_BMP
+337
// STBI_NO_PSD
+338
// STBI_NO_TGA
+339
// STBI_NO_GIF
+340
// STBI_NO_HDR
+341
// STBI_NO_PIC
+342
// STBI_NO_PNM (.ppm and .pgm)
+343
//
+344
// - You can request *only* certain decoders and suppress all other ones
+345
// (this will be more forward-compatible, as addition of new decoders
+346
// doesn't require you to disable them explicitly):
+347
//
+348
// STBI_ONLY_JPEG
+349
// STBI_ONLY_PNG
+350
// STBI_ONLY_BMP
+351
// STBI_ONLY_PSD
+352
// STBI_ONLY_TGA
+353
// STBI_ONLY_GIF
+354
// STBI_ONLY_HDR
+355
// STBI_ONLY_PIC
+356
// STBI_ONLY_PNM (.ppm and .pgm)
+357
//
+358
// - If you use STBI_NO_PNG (or _ONLY_ without PNG), and you still
+359
// want the zlib decoder to be available, #define STBI_SUPPORT_ZLIB
+360
//
+361
// - If you define STBI_MAX_DIMENSIONS, stb_image will reject images greater
+362
// than that size (in either width or height) without further processing.
+363
// This is to let programs in the wild set an upper bound to prevent
+364
// denial-of-service attacks on untrusted data, as one could generate a
+365
// valid image of gigantic dimensions and force stb_image to allocate a
+366
// huge block of memory and spend disproportionate time decoding it. By
+367
// default this is set to (1 << 24), which is 16777216, but that's still
+368
// very big.
+369
+370
#ifndef STBI_NO_STDIO
+371
#include <stdio.h>
+372
#endif // STBI_NO_STDIO
+373
+374
#define STBI_VERSION 1
+375
+376
enum
+377
{
+378
STBI_default = 0, // only used for desired_channels
+379
+380
STBI_grey = 1,
+381
STBI_grey_alpha = 2,
+382
STBI_rgb = 3,
+383
STBI_rgb_alpha = 4
+384
};
+385
+386
#include <stdlib.h>
+387
typedef unsigned char stbi_uc;
+388
typedef unsigned short stbi_us;
+389
+390
#ifdef __cplusplus
+391
extern "C" {
+392
#endif
+393
+394
#ifndef STBIDEF
+395
#ifdef STB_IMAGE_STATIC
+396
#define STBIDEF static
+397
#else
+398
#define STBIDEF extern
+399
#endif
+400
#endif
+401
+402
//////////////////////////////////////////////////////////////////////////////
+403
//
+404
// PRIMARY API - works on images of any type
+405
//
+406
+407
//
+408
// load image by filename, open file, or memory buffer
+409
//
+410
+411
typedef struct
+412
{
+413
int (*read) (void *user,char *data,int size); // fill 'data' with 'size' bytes. return number of bytes actually read
+414
void (*skip) (void *user,int n); // skip the next 'n' bytes, or 'unget' the last -n bytes if negative
+415
int (*eof) (void *user); // returns nonzero if we are at end of file/data
+416
} stbi_io_callbacks;
+417
+418
////////////////////////////////////
+419
//
+420
// 8-bits-per-channel interface
+421
//
+422
+423
STBIDEF stbi_uc *stbi_load_from_memory (stbi_uc const *buffer, int len , int *x, int *y, int *channels_in_file, int desired_channels);
+424
STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk , void *user, int *x, int *y, int *channels_in_file, int desired_channels);
+425
+426
#ifndef STBI_NO_STDIO
+427
STBIDEF stbi_uc *stbi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
+428
STBIDEF stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
+429
// for stbi_load_from_file, file pointer is left pointing immediately after image
+430
#endif
+431
+432
#ifndef STBI_NO_GIF
+433
STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp);
+434
#endif
+435
+436
#ifdef STBI_WINDOWS_UTF8
+437
STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input);
+438
#endif
+439
+440
////////////////////////////////////
+441
//
+442
// 16-bits-per-channel interface
+443
//
+444
+445
STBIDEF stbi_us *stbi_load_16_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels);
+446
STBIDEF stbi_us *stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels);
+447
+448
#ifndef STBI_NO_STDIO
+449
STBIDEF stbi_us *stbi_load_16 (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
+450
STBIDEF stbi_us *stbi_load_from_file_16(FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
+451
#endif
+452
+453
////////////////////////////////////
+454
//
+455
// float-per-channel interface
+456
//
+457
#ifndef STBI_NO_LINEAR
+458
STBIDEF float *stbi_loadf_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels);
+459
STBIDEF float *stbi_loadf_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels);
+460
+461
#ifndef STBI_NO_STDIO
+462
STBIDEF float *stbi_loadf (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
+463
STBIDEF float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
+464
#endif
+465
#endif
+466
+467
#ifndef STBI_NO_HDR
+468
STBIDEF void stbi_hdr_to_ldr_gamma(float gamma);
+469
STBIDEF void stbi_hdr_to_ldr_scale(float scale);
+470
#endif // STBI_NO_HDR
+471
+472
#ifndef STBI_NO_LINEAR
+473
STBIDEF void stbi_ldr_to_hdr_gamma(float gamma);
+474
STBIDEF void stbi_ldr_to_hdr_scale(float scale);
+475
#endif // STBI_NO_LINEAR
+476
+477
// stbi_is_hdr is always defined, but always returns false if STBI_NO_HDR
+478
STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user);
+479
STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len);
+480
#ifndef STBI_NO_STDIO
+481
STBIDEF int stbi_is_hdr (char const *filename);
+482
STBIDEF int stbi_is_hdr_from_file(FILE *f);
+483
#endif // STBI_NO_STDIO
+484
+485
+486
// get a VERY brief reason for failure
+487
// on most compilers (and ALL modern mainstream compilers) this is threadsafe
+488
STBIDEF const char *stbi_failure_reason (void);
+489
+490
// free the loaded image -- this is just free()
+491
STBIDEF void stbi_image_free (void *retval_from_stbi_load);
+492
+493
// get image dimensions & components without fully decoding
+494
STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
+495
STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp);
+496
STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len);
+497
STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *clbk, void *user);
+498
+499
#ifndef STBI_NO_STDIO

Showing the first 500 of 7989 diff lines for this file. This diff is INCOMPLETE; read the file or clone the repository for the rest.

vendor/stb/stb_image_write.hadded
@@ -0,0 +1,1724 @@
+1
/* stb_image_write - v1.16 - public domain - http://nothings.org/stb
+2
writes out PNG/BMP/TGA/JPEG/HDR images to C stdio - Sean Barrett 2010-2015
+3
no warranty implied; use at your own risk
+4
+5
Before #including,
+6
+7
#define STB_IMAGE_WRITE_IMPLEMENTATION
+8
+9
in the file that you want to have the implementation.
+10
+11
Will probably not work correctly with strict-aliasing optimizations.
+12
+13
ABOUT:
+14
+15
This header file is a library for writing images to C stdio or a callback.
+16
+17
The PNG output is not optimal; it is 20-50% larger than the file
+18
written by a decent optimizing implementation; though providing a custom
+19
zlib compress function (see STBIW_ZLIB_COMPRESS) can mitigate that.
+20
This library is designed for source code compactness and simplicity,
+21
not optimal image file size or run-time performance.
+22
+23
BUILDING:
+24
+25
You can #define STBIW_ASSERT(x) before the #include to avoid using assert.h.
+26
You can #define STBIW_MALLOC(), STBIW_REALLOC(), and STBIW_FREE() to replace
+27
malloc,realloc,free.
+28
You can #define STBIW_MEMMOVE() to replace memmove()
+29
You can #define STBIW_ZLIB_COMPRESS to use a custom zlib-style compress function
+30
for PNG compression (instead of the builtin one), it must have the following signature:
+31
unsigned char * my_compress(unsigned char *data, int data_len, int *out_len, int quality);
+32
The returned data will be freed with STBIW_FREE() (free() by default),
+33
so it must be heap allocated with STBIW_MALLOC() (malloc() by default),
+34
+35
UNICODE:
+36
+37
If compiling for Windows and you wish to use Unicode filenames, compile
+38
with
+39
#define STBIW_WINDOWS_UTF8
+40
and pass utf8-encoded filenames. Call stbiw_convert_wchar_to_utf8 to convert
+41
Windows wchar_t filenames to utf8.
+42
+43
USAGE:
+44
+45
There are five functions, one for each image file format:
+46
+47
int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
+48
int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
+49
int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);
+50
int stbi_write_jpg(char const *filename, int w, int h, int comp, const void *data, int quality);
+51
int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data);
+52
+53
void stbi_flip_vertically_on_write(int flag); // flag is non-zero to flip data vertically
+54
+55
There are also five equivalent functions that use an arbitrary write function. You are
+56
expected to open/close your file-equivalent before and after calling these:
+57
+58
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);
+59
int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
+60
int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
+61
int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data);
+62
int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality);
+63
+64
where the callback is:
+65
void stbi_write_func(void *context, void *data, int size);
+66
+67
You can configure it with these global variables:
+68
int stbi_write_tga_with_rle; // defaults to true; set to 0 to disable RLE
+69
int stbi_write_png_compression_level; // defaults to 8; set to higher for more compression
+70
int stbi_write_force_png_filter; // defaults to -1; set to 0..5 to force a filter mode
+71
+72
+73
You can define STBI_WRITE_NO_STDIO to disable the file variant of these
+74
functions, so the library will not use stdio.h at all. However, this will
+75
also disable HDR writing, because it requires stdio for formatted output.
+76
+77
Each function returns 0 on failure and non-0 on success.
+78
+79
The functions create an image file defined by the parameters. The image
+80
is a rectangle of pixels stored from left-to-right, top-to-bottom.
+81
Each pixel contains 'comp' channels of data stored interleaved with 8-bits
+82
per channel, in the following order: 1=Y, 2=YA, 3=RGB, 4=RGBA. (Y is
+83
monochrome color.) The rectangle is 'w' pixels wide and 'h' pixels tall.
+84
The *data pointer points to the first byte of the top-left-most pixel.
+85
For PNG, "stride_in_bytes" is the distance in bytes from the first byte of
+86
a row of pixels to the first byte of the next row of pixels.
+87
+88
PNG creates output files with the same number of components as the input.
+89
The BMP format expands Y to RGB in the file format and does not
+90
output alpha.
+91
+92
PNG supports writing rectangles of data even when the bytes storing rows of
+93
data are not consecutive in memory (e.g. sub-rectangles of a larger image),
+94
by supplying the stride between the beginning of adjacent rows. The other
+95
formats do not. (Thus you cannot write a native-format BMP through the BMP
+96
writer, both because it is in BGR order and because it may have padding
+97
at the end of the line.)
+98
+99
PNG allows you to set the deflate compression level by setting the global
+100
variable 'stbi_write_png_compression_level' (it defaults to 8).
+101
+102
HDR expects linear float data. Since the format is always 32-bit rgb(e)
+103
data, alpha (if provided) is discarded, and for monochrome data it is
+104
replicated across all three channels.
+105
+106
TGA supports RLE or non-RLE compressed data. To use non-RLE-compressed
+107
data, set the global variable 'stbi_write_tga_with_rle' to 0.
+108
+109
JPEG does ignore alpha channels in input data; quality is between 1 and 100.
+110
Higher quality looks better but results in a bigger image.
+111
JPEG baseline (no JPEG progressive).
+112
+113
CREDITS:
+114
+115
+116
Sean Barrett - PNG/BMP/TGA
+117
Baldur Karlsson - HDR
+118
Jean-Sebastien Guay - TGA monochrome
+119
Tim Kelsey - misc enhancements
+120
Alan Hickman - TGA RLE
+121
Emmanuel Julien - initial file IO callback implementation
+122
Jon Olick - original jo_jpeg.cpp code
+123
Daniel Gibson - integrate JPEG, allow external zlib
+124
Aarni Koskela - allow choosing PNG filter
+125
+126
bugfixes:
+127
github:Chribba
+128
Guillaume Chereau
+129
github:jry2
+130
github:romigrou
+131
Sergio Gonzalez
+132
Jonas Karlsson
+133
Filip Wasil
+134
Thatcher Ulrich
+135
github:poppolopoppo
+136
Patrick Boettcher
+137
github:xeekworx
+138
Cap Petschulat
+139
Simon Rodriguez
+140
Ivan Tikhonov
+141
github:ignotion
+142
Adam Schackart
+143
Andrew Kensler
+144
+145
LICENSE
+146
+147
See end of file for license information.
+148
+149
*/
+150
+151
#ifndef INCLUDE_STB_IMAGE_WRITE_H
+152
#define INCLUDE_STB_IMAGE_WRITE_H
+153
+154
#include <stdlib.h>
+155
+156
// if STB_IMAGE_WRITE_STATIC causes problems, try defining STBIWDEF to 'inline' or 'static inline'
+157
#ifndef STBIWDEF
+158
#ifdef STB_IMAGE_WRITE_STATIC
+159
#define STBIWDEF static
+160
#else
+161
#ifdef __cplusplus
+162
#define STBIWDEF extern "C"
+163
#else
+164
#define STBIWDEF extern
+165
#endif
+166
#endif
+167
#endif
+168
+169
#ifndef STB_IMAGE_WRITE_STATIC // C++ forbids static forward declarations
+170
STBIWDEF int stbi_write_tga_with_rle;
+171
STBIWDEF int stbi_write_png_compression_level;
+172
STBIWDEF int stbi_write_force_png_filter;
+173
#endif
+174
+175
#ifndef STBI_WRITE_NO_STDIO
+176
STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
+177
STBIWDEF int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
+178
STBIWDEF int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);
+179
STBIWDEF int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data);
+180
STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality);
+181
+182
#ifdef STBIW_WINDOWS_UTF8
+183
STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input);
+184
#endif
+185
#endif
+186
+187
typedef void stbi_write_func(void *context, void *data, int size);
+188
+189
STBIWDEF 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);
+190
STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
+191
STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
+192
STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data);
+193
STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality);
+194
+195
STBIWDEF void stbi_flip_vertically_on_write(int flip_boolean);
+196
+197
#endif//INCLUDE_STB_IMAGE_WRITE_H
+198
+199
#ifdef STB_IMAGE_WRITE_IMPLEMENTATION
+200
+201
#ifdef _WIN32
+202
#ifndef _CRT_SECURE_NO_WARNINGS
+203
#define _CRT_SECURE_NO_WARNINGS
+204
#endif
+205
#ifndef _CRT_NONSTDC_NO_DEPRECATE
+206
#define _CRT_NONSTDC_NO_DEPRECATE
+207
#endif
+208
#endif
+209
+210
#ifndef STBI_WRITE_NO_STDIO
+211
#include <stdio.h>
+212
#endif // STBI_WRITE_NO_STDIO
+213
+214
#include <stdarg.h>
+215
#include <stdlib.h>
+216
#include <string.h>
+217
#include <math.h>
+218
+219
#if defined(STBIW_MALLOC) && defined(STBIW_FREE) && (defined(STBIW_REALLOC) || defined(STBIW_REALLOC_SIZED))
+220
// ok
+221
#elif !defined(STBIW_MALLOC) && !defined(STBIW_FREE) && !defined(STBIW_REALLOC) && !defined(STBIW_REALLOC_SIZED)
+222
// ok
+223
#else
+224
#error "Must define all or none of STBIW_MALLOC, STBIW_FREE, and STBIW_REALLOC (or STBIW_REALLOC_SIZED)."
+225
#endif
+226
+227
#ifndef STBIW_MALLOC
+228
#define STBIW_MALLOC(sz) malloc(sz)
+229
#define STBIW_REALLOC(p,newsz) realloc(p,newsz)
+230
#define STBIW_FREE(p) free(p)
+231
#endif
+232
+233
#ifndef STBIW_REALLOC_SIZED
+234
#define STBIW_REALLOC_SIZED(p,oldsz,newsz) STBIW_REALLOC(p,newsz)
+235
#endif
+236
+237
+238
#ifndef STBIW_MEMMOVE
+239
#define STBIW_MEMMOVE(a,b,sz) memmove(a,b,sz)
+240
#endif
+241
+242
+243
#ifndef STBIW_ASSERT
+244
#include <assert.h>
+245
#define STBIW_ASSERT(x) assert(x)
+246
#endif
+247
+248
#define STBIW_UCHAR(x) (unsigned char) ((x) & 0xff)
+249
+250
#ifdef STB_IMAGE_WRITE_STATIC
+251
static int stbi_write_png_compression_level = 8;
+252
static int stbi_write_tga_with_rle = 1;
+253
static int stbi_write_force_png_filter = -1;
+254
#else
+255
int stbi_write_png_compression_level = 8;
+256
int stbi_write_tga_with_rle = 1;
+257
int stbi_write_force_png_filter = -1;
+258
#endif
+259
+260
static int stbi__flip_vertically_on_write = 0;
+261
+262
STBIWDEF void stbi_flip_vertically_on_write(int flag)
+263
{
+264
stbi__flip_vertically_on_write = flag;
+265
}
+266
+267
typedef struct
+268
{
+269
stbi_write_func *func;
+270
void *context;
+271
unsigned char buffer[64];
+272
int buf_used;
+273
} stbi__write_context;
+274
+275
// initialize a callback-based context
+276
static void stbi__start_write_callbacks(stbi__write_context *s, stbi_write_func *c, void *context)
+277
{
+278
s->func = c;
+279
s->context = context;
+280
}
+281
+282
#ifndef STBI_WRITE_NO_STDIO
+283
+284
static void stbi__stdio_write(void *context, void *data, int size)
+285
{
+286
fwrite(data,1,size,(FILE*) context);
+287
}
+288
+289
#if defined(_WIN32) && defined(STBIW_WINDOWS_UTF8)
+290
#ifdef __cplusplus
+291
#define STBIW_EXTERN extern "C"
+292
#else
+293
#define STBIW_EXTERN extern
+294
#endif
+295
STBIW_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide);
+296
STBIW_EXTERN __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigned long flags, const wchar_t *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default);
+297
+298
STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input)
+299
{
+300
return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL);
+301
}
+302
#endif
+303
+304
static FILE *stbiw__fopen(char const *filename, char const *mode)
+305
{
+306
FILE *f;
+307
#if defined(_WIN32) && defined(STBIW_WINDOWS_UTF8)
+308
wchar_t wMode[64];
+309
wchar_t wFilename[1024];
+310
if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename)/sizeof(*wFilename)))
+311
return 0;
+312
+313
if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode)/sizeof(*wMode)))
+314
return 0;
+315
+316
#if defined(_MSC_VER) && _MSC_VER >= 1400
+317
if (0 != _wfopen_s(&f, wFilename, wMode))
+318
f = 0;
+319
#else
+320
f = _wfopen(wFilename, wMode);
+321
#endif
+322
+323
#elif defined(_MSC_VER) && _MSC_VER >= 1400
+324
if (0 != fopen_s(&f, filename, mode))
+325
f=0;
+326
#else
+327
f = fopen(filename, mode);
+328
#endif
+329
return f;
+330
}
+331
+332
static int stbi__start_write_file(stbi__write_context *s, const char *filename)
+333
{
+334
FILE *f = stbiw__fopen(filename, "wb");
+335
stbi__start_write_callbacks(s, stbi__stdio_write, (void *) f);
+336
return f != NULL;
+337
}
+338
+339
static void stbi__end_write_file(stbi__write_context *s)
+340
{
+341
fclose((FILE *)s->context);
+342
}
+343
+344
#endif // !STBI_WRITE_NO_STDIO
+345
+346
typedef unsigned int stbiw_uint32;
+347
typedef int stb_image_write_test[sizeof(stbiw_uint32)==4 ? 1 : -1];
+348
+349
static void stbiw__writefv(stbi__write_context *s, const char *fmt, va_list v)
+350
{
+351
while (*fmt) {
+352
switch (*fmt++) {
+353
case ' ': break;
+354
case '1': { unsigned char x = STBIW_UCHAR(va_arg(v, int));
+355
s->func(s->context,&x,1);
+356
break; }
+357
case '2': { int x = va_arg(v,int);
+358
unsigned char b[2];
+359
b[0] = STBIW_UCHAR(x);
+360
b[1] = STBIW_UCHAR(x>>8);
+361
s->func(s->context,b,2);
+362
break; }
+363
case '4': { stbiw_uint32 x = va_arg(v,int);
+364
unsigned char b[4];
+365
b[0]=STBIW_UCHAR(x);
+366
b[1]=STBIW_UCHAR(x>>8);
+367
b[2]=STBIW_UCHAR(x>>16);
+368
b[3]=STBIW_UCHAR(x>>24);
+369
s->func(s->context,b,4);
+370
break; }
+371
default:
+372
STBIW_ASSERT(0);
+373
return;
+374
}
+375
}
+376
}
+377
+378
static void stbiw__writef(stbi__write_context *s, const char *fmt, ...)
+379
{
+380
va_list v;
+381
va_start(v, fmt);
+382
stbiw__writefv(s, fmt, v);
+383
va_end(v);
+384
}
+385
+386
static void stbiw__write_flush(stbi__write_context *s)
+387
{
+388
if (s->buf_used) {
+389
s->func(s->context, &s->buffer, s->buf_used);
+390
s->buf_used = 0;
+391
}
+392
}
+393
+394
static void stbiw__putc(stbi__write_context *s, unsigned char c)
+395
{
+396
s->func(s->context, &c, 1);
+397
}
+398
+399
static void stbiw__write1(stbi__write_context *s, unsigned char a)
+400
{
+401
if ((size_t)s->buf_used + 1 > sizeof(s->buffer))
+402
stbiw__write_flush(s);
+403
s->buffer[s->buf_used++] = a;
+404
}
+405
+406
static void stbiw__write3(stbi__write_context *s, unsigned char a, unsigned char b, unsigned char c)
+407
{
+408
int n;
+409
if ((size_t)s->buf_used + 3 > sizeof(s->buffer))
+410
stbiw__write_flush(s);
+411
n = s->buf_used;
+412
s->buf_used = n+3;
+413
s->buffer[n+0] = a;
+414
s->buffer[n+1] = b;
+415
s->buffer[n+2] = c;
+416
}
+417
+418
static void stbiw__write_pixel(stbi__write_context *s, int rgb_dir, int comp, int write_alpha, int expand_mono, unsigned char *d)
+419
{
+420
unsigned char bg[3] = { 255, 0, 255}, px[3];
+421
int k;
+422
+423
if (write_alpha < 0)
+424
stbiw__write1(s, d[comp - 1]);
+425
+426
switch (comp) {
+427
case 2: // 2 pixels = mono + alpha, alpha is written separately, so same as 1-channel case
+428
case 1:
+429
if (expand_mono)
+430
stbiw__write3(s, d[0], d[0], d[0]); // monochrome bmp
+431
else
+432
stbiw__write1(s, d[0]); // monochrome TGA
+433
break;
+434
case 4:
+435
if (!write_alpha) {
+436
// composite against pink background
+437
for (k = 0; k < 3; ++k)
+438
px[k] = bg[k] + ((d[k] - bg[k]) * d[3]) / 255;
+439
stbiw__write3(s, px[1 - rgb_dir], px[1], px[1 + rgb_dir]);
+440
break;
+441
}
+442
/* FALLTHROUGH */
+443
case 3:
+444
stbiw__write3(s, d[1 - rgb_dir], d[1], d[1 + rgb_dir]);
+445
break;
+446
}
+447
if (write_alpha > 0)
+448
stbiw__write1(s, d[comp - 1]);
+449
}
+450
+451
static void stbiw__write_pixels(stbi__write_context *s, int rgb_dir, int vdir, int x, int y, int comp, void *data, int write_alpha, int scanline_pad, int expand_mono)
+452
{
+453
stbiw_uint32 zero = 0;
+454
int i,j, j_end;
+455
+456
if (y <= 0)
+457
return;
+458
+459
if (stbi__flip_vertically_on_write)
+460
vdir *= -1;
+461
+462
if (vdir < 0) {
+463
j_end = -1; j = y-1;
+464
} else {
+465
j_end = y; j = 0;
+466
}
+467
+468
for (; j != j_end; j += vdir) {
+469
for (i=0; i < x; ++i) {
+470
unsigned char *d = (unsigned char *) data + (j*x+i)*comp;
+471
stbiw__write_pixel(s, rgb_dir, comp, write_alpha, expand_mono, d);
+472
}
+473
stbiw__write_flush(s);
+474
s->func(s->context, &zero, scanline_pad);
+475
}
+476
}
+477
+478
static int stbiw__outfile(stbi__write_context *s, int rgb_dir, int vdir, int x, int y, int comp, int expand_mono, void *data, int alpha, int pad, const char *fmt, ...)
+479
{
+480
if (y < 0 || x < 0) {
+481
return 0;
+482
} else {
+483
va_list v;
+484
va_start(v, fmt);
+485
stbiw__writefv(s, fmt, v);
+486
va_end(v);
+487
stbiw__write_pixels(s,rgb_dir,vdir,x,y,comp,data,alpha,pad, expand_mono);
+488
return 1;
+489
}
+490
}
+491
+492
static int stbi_write_bmp_core(stbi__write_context *s, int x, int y, int comp, const void *data)
+493
{
+494
if (comp != 4) {
+495
// write RGB bitmap
+496
int pad = (-x*3) & 3;
+497
return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *) data,0,pad,
+498
"11 4 22 4" "4 44 22 444444",
+499
'B', 'M', 14+40+(x*3+pad)*y, 0,0, 14+40, // file header

Showing the first 500 of 1725 diff lines for this file. This diff is INCOMPLETE; read the file or clone the repository for the rest.