stb_image_write.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // SPDX-FileCopyrightText: stb http://nothings.org/stb
  2. // SPDX-License-Identifier: MIT
  3. /* stb_image_write - v1.16 - public domain - http://nothings.org/stb
  4. writes out PNG/BMP/TGA/JPEG/HDR images to C stdio - Sean Barrett 2010-2015
  5. no warranty implied; use at your own risk
  6. Before #including,
  7. #define STB_IMAGE_WRITE_IMPLEMENTATION
  8. in the file that you want to have the implementation.
  9. Will probably not work correctly with strict-aliasing optimizations.
  10. ABOUT:
  11. This header file is a library for writing images to C stdio or a callback.
  12. The PNG output is not optimal; it is 20-50% larger than the file
  13. written by a decent optimizing implementation; though providing a custom
  14. zlib compress function (see STBIW_ZLIB_COMPRESS) can mitigate that.
  15. This library is designed for source code compactness and simplicity,
  16. not optimal image file size or run-time performance.
  17. BUILDING:
  18. You can #define STBIW_ASSERT(x) before the #include to avoid using assert.h.
  19. You can #define STBIW_MALLOC(), STBIW_REALLOC(), and STBIW_FREE() to replace
  20. malloc,realloc,free.
  21. You can #define STBIW_MEMMOVE() to replace memmove()
  22. You can #define STBIW_ZLIB_COMPRESS to use a custom zlib-style compress function
  23. for PNG compression (instead of the builtin one), it must have the following signature:
  24. unsigned char * my_compress(unsigned char *data, int data_len, int *out_len, int quality);
  25. The returned data will be freed with STBIW_FREE() (free() by default),
  26. so it must be heap allocated with STBIW_MALLOC() (malloc() by default),
  27. UNICODE:
  28. If compiling for Windows and you wish to use Unicode filenames, compile
  29. with
  30. #define STBIW_WINDOWS_UTF8
  31. and pass utf8-encoded filenames. Call stbiw_convert_wchar_to_utf8 to convert
  32. Windows wchar_t filenames to utf8.
  33. USAGE:
  34. There are five functions, one for each image file format:
  35. int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
  36. int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
  37. int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);
  38. int stbi_write_jpg(char const *filename, int w, int h, int comp, const void *data, int quality);
  39. int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data);
  40. void stbi_flip_vertically_on_write(int flag); // flag is non-zero to flip data vertically
  41. There are also five equivalent functions that use an arbitrary write function. You are
  42. expected to open/close your file-equivalent before and after calling these:
  43. 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);
  44. int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
  45. int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
  46. int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data);
  47. int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality);
  48. where the callback is:
  49. void stbi_write_func(void *context, void *data, int size);
  50. You can configure it with these global variables:
  51. int stbi_write_tga_with_rle; // defaults to true; set to 0 to disable RLE
  52. int stbi_write_png_compression_level; // defaults to 8; set to higher for more compression
  53. int stbi_write_force_png_filter; // defaults to -1; set to 0..5 to force a filter mode
  54. You can define STBI_WRITE_NO_STDIO to disable the file variant of these
  55. functions, so the library will not use stdio.h at all. However, this will
  56. also disable HDR writing, because it requires stdio for formatted output.
  57. Each function returns 0 on failure and non-0 on success.
  58. The functions create an image file defined by the parameters. The image
  59. is a rectangle of pixels stored from left-to-right, top-to-bottom.
  60. Each pixel contains 'comp' channels of data stored interleaved with 8-bits
  61. per channel, in the following order: 1=Y, 2=YA, 3=RGB, 4=RGBA. (Y is
  62. monochrome color.) The rectangle is 'w' pixels wide and 'h' pixels tall.
  63. The *data pointer points to the first byte of the top-left-most pixel.
  64. For PNG, "stride_in_bytes" is the distance in bytes from the first byte of
  65. a row of pixels to the first byte of the next row of pixels.
  66. PNG creates output files with the same number of components as the input.
  67. The BMP format expands Y to RGB in the file format and does not
  68. output alpha.
  69. PNG supports writing rectangles of data even when the bytes storing rows of
  70. data are not consecutive in memory (e.g. sub-rectangles of a larger image),
  71. by supplying the stride between the beginning of adjacent rows. The other
  72. formats do not. (Thus you cannot write a native-format BMP through the BMP
  73. writer, both because it is in BGR order and because it may have padding
  74. at the end of the line.)
  75. PNG allows you to set the deflate compression level by setting the global
  76. variable 'stbi_write_png_compression_level' (it defaults to 8).
  77. HDR expects linear float data. Since the format is always 32-bit rgb(e)
  78. data, alpha (if provided) is discarded, and for monochrome data it is
  79. replicated across all three channels.
  80. TGA supports RLE or non-RLE compressed data. To use non-RLE-compressed
  81. data, set the global variable 'stbi_write_tga_with_rle' to 0.
  82. JPEG does ignore alpha channels in input data; quality is between 1 and 100.
  83. Higher quality looks better but results in a bigger image.
  84. JPEG baseline (no JPEG progressive).
  85. CREDITS:
  86. Sean Barrett - PNG/BMP/TGA
  87. Baldur Karlsson - HDR
  88. Jean-Sebastien Guay - TGA monochrome
  89. Tim Kelsey - misc enhancements
  90. Alan Hickman - TGA RLE
  91. Emmanuel Julien - initial file IO callback implementation
  92. Jon Olick - original jo_jpeg.cpp code
  93. Daniel Gibson - integrate JPEG, allow external zlib
  94. Aarni Koskela - allow choosing PNG filter
  95. bugfixes:
  96. github:Chribba
  97. Guillaume Chereau
  98. github:jry2
  99. github:romigrou
  100. Sergio Gonzalez
  101. Jonas Karlsson
  102. Filip Wasil
  103. Thatcher Ulrich
  104. github:poppolopoppo
  105. Patrick Boettcher
  106. github:xeekworx
  107. Cap Petschulat
  108. Simon Rodriguez
  109. Ivan Tikhonov
  110. github:ignotion
  111. Adam Schackart
  112. Andrew Kensler
  113. LICENSE
  114. See end of file for license information.
  115. */
  116. #ifndef INCLUDE_STB_IMAGE_WRITE_H
  117. #define INCLUDE_STB_IMAGE_WRITE_H
  118. #include <stdlib.h>
  119. // if STB_IMAGE_WRITE_STATIC causes problems, try defining STBIWDEF to 'inline' or 'static inline'
  120. #ifndef STBIWDEF
  121. #ifdef STB_IMAGE_WRITE_STATIC
  122. #define STBIWDEF static
  123. #else
  124. #ifdef __cplusplus
  125. #define STBIWDEF extern "C"
  126. #else
  127. #define STBIWDEF extern
  128. #endif
  129. #endif
  130. #endif
  131. #ifndef STB_IMAGE_WRITE_STATIC // C++ forbids static forward declarations
  132. STBIWDEF int stbi_write_tga_with_rle;
  133. STBIWDEF int stbi_write_png_compression_level;
  134. STBIWDEF int stbi_write_force_png_filter;
  135. #endif
  136. #ifndef STBI_WRITE_NO_STDIO
  137. STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
  138. STBIWDEF int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
  139. STBIWDEF int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);
  140. STBIWDEF int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data);
  141. STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality);
  142. #ifdef STBIW_WINDOWS_UTF8
  143. STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input);
  144. #endif
  145. #endif
  146. typedef void stbi_write_func(void *context, void *data, int size);
  147. 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);
  148. STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
  149. STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
  150. STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data);
  151. STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality);
  152. STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, int stride_bytes, int x, int y, int n, int *out_len);
  153. STBIWDEF void stbi_flip_vertically_on_write(int flip_boolean);
  154. #endif//INCLUDE_STB_IMAGE_WRITE_H
  155. /* Revision history
  156. 1.16 (2021-07-11)
  157. make Deflate code emit uncompressed blocks when it would otherwise expand
  158. support writing BMPs with alpha channel
  159. 1.15 (2020-07-13) unknown
  160. 1.14 (2020-02-02) updated JPEG writer to downsample chroma channels
  161. 1.13
  162. 1.12
  163. 1.11 (2019-08-11)
  164. 1.10 (2019-02-07)
  165. support utf8 filenames in Windows; fix warnings and platform ifdefs
  166. 1.09 (2018-02-11)
  167. fix typo in zlib quality API, improve STB_I_W_STATIC in C++
  168. 1.08 (2018-01-29)
  169. add stbi__flip_vertically_on_write, external zlib, zlib quality, choose PNG filter
  170. 1.07 (2017-07-24)
  171. doc fix
  172. 1.06 (2017-07-23)
  173. writing JPEG (using Jon Olick's code)
  174. 1.05 ???
  175. 1.04 (2017-03-03)
  176. monochrome BMP expansion
  177. 1.03 ???
  178. 1.02 (2016-04-02)
  179. avoid allocating large structures on the stack
  180. 1.01 (2016-01-16)
  181. STBIW_REALLOC_SIZED: support allocators with no realloc support
  182. avoid race-condition in crc initialization
  183. minor compile issues
  184. 1.00 (2015-09-14)
  185. installable file IO function
  186. 0.99 (2015-09-13)
  187. warning fixes; TGA rle support
  188. 0.98 (2015-04-08)
  189. added STBIW_MALLOC, STBIW_ASSERT etc
  190. 0.97 (2015-01-18)
  191. fixed HDR asserts, rewrote HDR rle logic
  192. 0.96 (2015-01-17)
  193. add HDR output
  194. fix monochrome BMP
  195. 0.95 (2014-08-17)
  196. add monochrome TGA output
  197. 0.94 (2014-05-31)
  198. rename private functions to avoid conflicts with stb_image.h
  199. 0.93 (2014-05-27)
  200. warning fixes
  201. 0.92 (2010-08-01)
  202. casts to unsigned char to fix warnings
  203. 0.91 (2010-07-17)
  204. first public release
  205. 0.90 first internal release
  206. */
  207. /*
  208. ------------------------------------------------------------------------------
  209. This software is available under 2 licenses -- choose whichever you prefer.
  210. ------------------------------------------------------------------------------
  211. ALTERNATIVE A - MIT License
  212. Copyright (c) 2017 Sean Barrett
  213. Permission is hereby granted, free of charge, to any person obtaining a copy of
  214. this software and associated documentation files (the "Software"), to deal in
  215. the Software without restriction, including without limitation the rights to
  216. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  217. of the Software, and to permit persons to whom the Software is furnished to do
  218. so, subject to the following conditions:
  219. The above copyright notice and this permission notice shall be included in all
  220. copies or substantial portions of the Software.
  221. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  222. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  223. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  224. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  225. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  226. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  227. SOFTWARE.
  228. ------------------------------------------------------------------------------
  229. ALTERNATIVE B - Public Domain (www.unlicense.org)
  230. This is free and unencumbered software released into the public domain.
  231. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  232. software, either in source code form or as a compiled binary, for any purpose,
  233. commercial or non-commercial, and by any means.
  234. In jurisdictions that recognize copyright laws, the author or authors of this
  235. software dedicate any and all copyright interest in the software to the public
  236. domain. We make this dedication for the benefit of the public at large and to
  237. the detriment of our heirs and successors. We intend this dedication to be an
  238. overt act of relinquishment in perpetuity of all present and future rights to
  239. this software under copyright law.
  240. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  241. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  242. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  243. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  244. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  245. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  246. ------------------------------------------------------------------------------
  247. */