mirror of
https://git.suyu.dev/suyu/suyu.git
synced 2024-12-29 11:50:58 +01:00
298 lines
13 KiB
C++
298 lines
13 KiB
C++
|
// SPDX-FileCopyrightText: stb http://nothings.org/stb
|
||
|
// SPDX-License-Identifier: MIT
|
||
|
|
||
|
/* stb_image_write - v1.16 - public domain - http://nothings.org/stb
|
||
|
writes out PNG/BMP/TGA/JPEG/HDR images to C stdio - Sean Barrett 2010-2015
|
||
|
no warranty implied; use at your own risk
|
||
|
|
||
|
Before #including,
|
||
|
|
||
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||
|
|
||
|
in the file that you want to have the implementation.
|
||
|
|
||
|
Will probably not work correctly with strict-aliasing optimizations.
|
||
|
|
||
|
ABOUT:
|
||
|
|
||
|
This header file is a library for writing images to C stdio or a callback.
|
||
|
|
||
|
The PNG output is not optimal; it is 20-50% larger than the file
|
||
|
written by a decent optimizing implementation; though providing a custom
|
||
|
zlib compress function (see STBIW_ZLIB_COMPRESS) can mitigate that.
|
||
|
This library is designed for source code compactness and simplicity,
|
||
|
not optimal image file size or run-time performance.
|
||
|
|
||
|
BUILDING:
|
||
|
|
||
|
You can #define STBIW_ASSERT(x) before the #include to avoid using assert.h.
|
||
|
You can #define STBIW_MALLOC(), STBIW_REALLOC(), and STBIW_FREE() to replace
|
||
|
malloc,realloc,free.
|
||
|
You can #define STBIW_MEMMOVE() to replace memmove()
|
||
|
You can #define STBIW_ZLIB_COMPRESS to use a custom zlib-style compress function
|
||
|
for PNG compression (instead of the builtin one), it must have the following signature:
|
||
|
unsigned char * my_compress(unsigned char *data, int data_len, int *out_len, int quality);
|
||
|
The returned data will be freed with STBIW_FREE() (free() by default),
|
||
|
so it must be heap allocated with STBIW_MALLOC() (malloc() by default),
|
||
|
|
||
|
UNICODE:
|
||
|
|
||
|
If compiling for Windows and you wish to use Unicode filenames, compile
|
||
|
with
|
||
|
#define STBIW_WINDOWS_UTF8
|
||
|
and pass utf8-encoded filenames. Call stbiw_convert_wchar_to_utf8 to convert
|
||
|
Windows wchar_t filenames to utf8.
|
||
|
|
||
|
USAGE:
|
||
|
|
||
|
There are five functions, one for each image file format:
|
||
|
|
||
|
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);
|
||
|
|
||
|
void stbi_flip_vertically_on_write(int flag); // flag is non-zero to flip data vertically
|
||
|
|
||
|
There are also five equivalent functions that use an arbitrary write function. You are
|
||
|
expected to open/close your file-equivalent before and after calling these:
|
||
|
|
||
|
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);
|
||
|
int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
|
||
|
int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data);
|
||
|
int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality);
|
||
|
|
||
|
where the callback is:
|
||
|
void stbi_write_func(void *context, void *data, int size);
|
||
|
|
||
|
You can configure it with these global variables:
|
||
|
int stbi_write_tga_with_rle; // defaults to true; set to 0 to disable RLE
|
||
|
int stbi_write_png_compression_level; // defaults to 8; set to higher for more compression
|
||
|
int stbi_write_force_png_filter; // defaults to -1; set to 0..5 to force a filter mode
|
||
|
|
||
|
|
||
|
You can define STBI_WRITE_NO_STDIO to disable the file variant of these
|
||
|
functions, so the library will not use stdio.h at all. However, this will
|
||
|
also disable HDR writing, because it requires stdio for formatted output.
|
||
|
|
||
|
Each function returns 0 on failure and non-0 on success.
|
||
|
|
||
|
The functions create an image file defined by the parameters. The image
|
||
|
is a rectangle of pixels stored from left-to-right, top-to-bottom.
|
||
|
Each pixel contains 'comp' channels of data stored interleaved with 8-bits
|
||
|
per channel, in the following order: 1=Y, 2=YA, 3=RGB, 4=RGBA. (Y is
|
||
|
monochrome color.) The rectangle is 'w' pixels wide and 'h' pixels tall.
|
||
|
The *data pointer points to the first byte of the top-left-most pixel.
|
||
|
For PNG, "stride_in_bytes" is the distance in bytes from the first byte of
|
||
|
a row of pixels to the first byte of the next row of pixels.
|
||
|
|
||
|
PNG creates output files with the same number of components as the input.
|
||
|
The BMP format expands Y to RGB in the file format and does not
|
||
|
output alpha.
|
||
|
|
||
|
PNG supports writing rectangles of data even when the bytes storing rows of
|
||
|
data are not consecutive in memory (e.g. sub-rectangles of a larger image),
|
||
|
by supplying the stride between the beginning of adjacent rows. The other
|
||
|
formats do not. (Thus you cannot write a native-format BMP through the BMP
|
||
|
writer, both because it is in BGR order and because it may have padding
|
||
|
at the end of the line.)
|
||
|
|
||
|
PNG allows you to set the deflate compression level by setting the global
|
||
|
variable 'stbi_write_png_compression_level' (it defaults to 8).
|
||
|
|
||
|
HDR expects linear float data. Since the format is always 32-bit rgb(e)
|
||
|
data, alpha (if provided) is discarded, and for monochrome data it is
|
||
|
replicated across all three channels.
|
||
|
|
||
|
TGA supports RLE or non-RLE compressed data. To use non-RLE-compressed
|
||
|
data, set the global variable 'stbi_write_tga_with_rle' to 0.
|
||
|
|
||
|
JPEG does ignore alpha channels in input data; quality is between 1 and 100.
|
||
|
Higher quality looks better but results in a bigger image.
|
||
|
JPEG baseline (no JPEG progressive).
|
||
|
|
||
|
CREDITS:
|
||
|
|
||
|
|
||
|
Sean Barrett - PNG/BMP/TGA
|
||
|
Baldur Karlsson - HDR
|
||
|
Jean-Sebastien Guay - TGA monochrome
|
||
|
Tim Kelsey - misc enhancements
|
||
|
Alan Hickman - TGA RLE
|
||
|
Emmanuel Julien - initial file IO callback implementation
|
||
|
Jon Olick - original jo_jpeg.cpp code
|
||
|
Daniel Gibson - integrate JPEG, allow external zlib
|
||
|
Aarni Koskela - allow choosing PNG filter
|
||
|
|
||
|
bugfixes:
|
||
|
github:Chribba
|
||
|
Guillaume Chereau
|
||
|
github:jry2
|
||
|
github:romigrou
|
||
|
Sergio Gonzalez
|
||
|
Jonas Karlsson
|
||
|
Filip Wasil
|
||
|
Thatcher Ulrich
|
||
|
github:poppolopoppo
|
||
|
Patrick Boettcher
|
||
|
github:xeekworx
|
||
|
Cap Petschulat
|
||
|
Simon Rodriguez
|
||
|
Ivan Tikhonov
|
||
|
github:ignotion
|
||
|
Adam Schackart
|
||
|
Andrew Kensler
|
||
|
|
||
|
LICENSE
|
||
|
|
||
|
See end of file for license information.
|
||
|
|
||
|
*/
|
||
|
|
||
|
#ifndef INCLUDE_STB_IMAGE_WRITE_H
|
||
|
#define INCLUDE_STB_IMAGE_WRITE_H
|
||
|
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
// if STB_IMAGE_WRITE_STATIC causes problems, try defining STBIWDEF to 'inline' or 'static inline'
|
||
|
#ifndef STBIWDEF
|
||
|
#ifdef STB_IMAGE_WRITE_STATIC
|
||
|
#define STBIWDEF static
|
||
|
#else
|
||
|
#ifdef __cplusplus
|
||
|
#define STBIWDEF extern "C"
|
||
|
#else
|
||
|
#define STBIWDEF extern
|
||
|
#endif
|
||
|
#endif
|
||
|
#endif
|
||
|
|
||
|
#ifndef STB_IMAGE_WRITE_STATIC // C++ forbids static forward declarations
|
||
|
STBIWDEF int stbi_write_tga_with_rle;
|
||
|
STBIWDEF int stbi_write_png_compression_level;
|
||
|
STBIWDEF int stbi_write_force_png_filter;
|
||
|
#endif
|
||
|
|
||
|
#ifndef STBI_WRITE_NO_STDIO
|
||
|
STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
|
||
|
STBIWDEF int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
|
||
|
STBIWDEF int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);
|
||
|
STBIWDEF int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data);
|
||
|
STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality);
|
||
|
|
||
|
#ifdef STBIW_WINDOWS_UTF8
|
||
|
STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input);
|
||
|
#endif
|
||
|
#endif
|
||
|
|
||
|
typedef void stbi_write_func(void *context, void *data, int size);
|
||
|
|
||
|
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);
|
||
|
STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
|
||
|
STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
|
||
|
STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data);
|
||
|
STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality);
|
||
|
STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, int stride_bytes, int x, int y, int n, int *out_len);
|
||
|
|
||
|
STBIWDEF void stbi_flip_vertically_on_write(int flip_boolean);
|
||
|
|
||
|
#endif//INCLUDE_STB_IMAGE_WRITE_H
|
||
|
|
||
|
/* Revision history
|
||
|
1.16 (2021-07-11)
|
||
|
make Deflate code emit uncompressed blocks when it would otherwise expand
|
||
|
support writing BMPs with alpha channel
|
||
|
1.15 (2020-07-13) unknown
|
||
|
1.14 (2020-02-02) updated JPEG writer to downsample chroma channels
|
||
|
1.13
|
||
|
1.12
|
||
|
1.11 (2019-08-11)
|
||
|
|
||
|
1.10 (2019-02-07)
|
||
|
support utf8 filenames in Windows; fix warnings and platform ifdefs
|
||
|
1.09 (2018-02-11)
|
||
|
fix typo in zlib quality API, improve STB_I_W_STATIC in C++
|
||
|
1.08 (2018-01-29)
|
||
|
add stbi__flip_vertically_on_write, external zlib, zlib quality, choose PNG filter
|
||
|
1.07 (2017-07-24)
|
||
|
doc fix
|
||
|
1.06 (2017-07-23)
|
||
|
writing JPEG (using Jon Olick's code)
|
||
|
1.05 ???
|
||
|
1.04 (2017-03-03)
|
||
|
monochrome BMP expansion
|
||
|
1.03 ???
|
||
|
1.02 (2016-04-02)
|
||
|
avoid allocating large structures on the stack
|
||
|
1.01 (2016-01-16)
|
||
|
STBIW_REALLOC_SIZED: support allocators with no realloc support
|
||
|
avoid race-condition in crc initialization
|
||
|
minor compile issues
|
||
|
1.00 (2015-09-14)
|
||
|
installable file IO function
|
||
|
0.99 (2015-09-13)
|
||
|
warning fixes; TGA rle support
|
||
|
0.98 (2015-04-08)
|
||
|
added STBIW_MALLOC, STBIW_ASSERT etc
|
||
|
0.97 (2015-01-18)
|
||
|
fixed HDR asserts, rewrote HDR rle logic
|
||
|
0.96 (2015-01-17)
|
||
|
add HDR output
|
||
|
fix monochrome BMP
|
||
|
0.95 (2014-08-17)
|
||
|
add monochrome TGA output
|
||
|
0.94 (2014-05-31)
|
||
|
rename private functions to avoid conflicts with stb_image.h
|
||
|
0.93 (2014-05-27)
|
||
|
warning fixes
|
||
|
0.92 (2010-08-01)
|
||
|
casts to unsigned char to fix warnings
|
||
|
0.91 (2010-07-17)
|
||
|
first public release
|
||
|
0.90 first internal release
|
||
|
*/
|
||
|
|
||
|
/*
|
||
|
------------------------------------------------------------------------------
|
||
|
This software is available under 2 licenses -- choose whichever you prefer.
|
||
|
------------------------------------------------------------------------------
|
||
|
ALTERNATIVE A - MIT License
|
||
|
Copyright (c) 2017 Sean Barrett
|
||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||
|
this software and associated documentation files (the "Software"), to deal in
|
||
|
the Software without restriction, including without limitation the rights to
|
||
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||
|
of the Software, and to permit persons to whom the Software is furnished to do
|
||
|
so, subject to the following conditions:
|
||
|
The above copyright notice and this permission notice shall be included in all
|
||
|
copies or substantial portions of the Software.
|
||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||
|
SOFTWARE.
|
||
|
------------------------------------------------------------------------------
|
||
|
ALTERNATIVE B - Public Domain (www.unlicense.org)
|
||
|
This is free and unencumbered software released into the public domain.
|
||
|
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
|
||
|
software, either in source code form or as a compiled binary, for any purpose,
|
||
|
commercial or non-commercial, and by any means.
|
||
|
In jurisdictions that recognize copyright laws, the author or authors of this
|
||
|
software dedicate any and all copyright interest in the software to the public
|
||
|
domain. We make this dedication for the benefit of the public at large and to
|
||
|
the detriment of our heirs and successors. We intend this dedication to be an
|
||
|
overt act of relinquishment in perpetuity of all present and future rights to
|
||
|
this software under copyright law.
|
||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
|
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||
|
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||
|
------------------------------------------------------------------------------
|
||
|
*/
|