stb_image_write.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* stb_image_write - v1.02 - public domain - http://nothings.org/stb/stb_image_write.h
  2. writes out PNG/BMP/TGA images to C stdio - Sean Barrett 2010-2015
  3. no warranty implied; use at your own risk
  4. Before #including,
  5. #define STB_IMAGE_WRITE_IMPLEMENTATION
  6. in the file that you want to have the implementation.
  7. Will probably not work correctly with strict-aliasing optimizations.
  8. ABOUT:
  9. This header file is a library for writing images to C stdio. It could be
  10. adapted to write to memory or a general streaming interface; let me know.
  11. The PNG output is not optimal; it is 20-50% larger than the file
  12. written by a decent optimizing implementation. This library is designed
  13. for source code compactness and simplicity, not optimal image file size
  14. or run-time performance.
  15. BUILDING:
  16. You can #define STBIW_ASSERT(x) before the #include to avoid using assert.h.
  17. You can #define STBIW_MALLOC(), STBIW_REALLOC(), and STBIW_FREE() to replace
  18. malloc,realloc,free.
  19. You can define STBIW_MEMMOVE() to replace memmove()
  20. USAGE:
  21. There are four functions, one for each image file format:
  22. int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
  23. int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
  24. int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);
  25. int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data);
  26. There are also four equivalent functions that use an arbitrary write function. You are
  27. expected to open/close your file-equivalent before and after calling these:
  28. 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);
  29. int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
  30. int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
  31. int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data);
  32. where the callback is:
  33. void stbi_write_func(void *context, void *data, int size);
  34. You can define STBI_WRITE_NO_STDIO to disable the file variant of these
  35. functions, so the library will not use stdio.h at all. However, this will
  36. also disable HDR writing, because it requires stdio for formatted output.
  37. Each function returns 0 on failure and non-0 on success.
  38. The functions create an image file defined by the parameters. The image
  39. is a rectangle of pixels stored from left-to-right, top-to-bottom.
  40. Each pixel contains 'comp' channels of data stored interleaved with 8-bits
  41. per channel, in the following order: 1=Y, 2=YA, 3=RGB, 4=RGBA. (Y is
  42. monochrome color.) The rectangle is 'w' pixels wide and 'h' pixels tall.
  43. The *data pointer points to the first byte of the top-left-most pixel.
  44. For PNG, "stride_in_bytes" is the distance in bytes from the first byte of
  45. a row of pixels to the first byte of the next row of pixels.
  46. PNG creates output files with the same number of components as the input.
  47. The BMP format expands Y to RGB in the file format and does not
  48. output alpha.
  49. PNG supports writing rectangles of data even when the bytes storing rows of
  50. data are not consecutive in memory (e.g. sub-rectangles of a larger image),
  51. by supplying the stride between the beginning of adjacent rows. The other
  52. formats do not. (Thus you cannot write a native-format BMP through the BMP
  53. writer, both because it is in BGR order and because it may have padding
  54. at the end of the line.)
  55. HDR expects linear float data. Since the format is always 32-bit rgb(e)
  56. data, alpha (if provided) is discarded, and for monochrome data it is
  57. replicated across all three channels.
  58. TGA supports RLE or non-RLE compressed data. To use non-RLE-compressed
  59. data, set the global variable 'stbi_write_tga_with_rle' to 0.
  60. CREDITS:
  61. PNG/BMP/TGA
  62. Sean Barrett
  63. HDR
  64. Baldur Karlsson
  65. TGA monochrome:
  66. Jean-Sebastien Guay
  67. misc enhancements:
  68. Tim Kelsey
  69. TGA RLE
  70. Alan Hickman
  71. initial file IO callback implementation
  72. Emmanuel Julien
  73. bugfixes:
  74. github:Chribba
  75. Guillaume Chereau
  76. github:jry2
  77. github:romigrou
  78. Sergio Gonzalez
  79. Jonas Karlsson
  80. Filip Wasil
  81. Thatcher Ulrich
  82. LICENSE
  83. This software is dual-licensed to the public domain and under the following
  84. license: you are granted a perpetual, irrevocable license to copy, modify,
  85. publish, and distribute this file as you see fit.
  86. */
  87. // Modified by Lasse Oorni for Urho3D
  88. #ifndef INCLUDE_STB_IMAGE_WRITE_H
  89. #define INCLUDE_STB_IMAGE_WRITE_H
  90. #ifdef __cplusplus
  91. extern "C" {
  92. #endif
  93. #ifdef STB_IMAGE_WRITE_STATIC
  94. #define STBIWDEF static
  95. #else
  96. #define STBIWDEF extern
  97. extern int stbi_write_tga_with_rle;
  98. #endif
  99. #ifndef STBI_WRITE_NO_STDIO
  100. STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
  101. STBIWDEF int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
  102. STBIWDEF int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);
  103. STBIWDEF int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data);
  104. #endif
  105. typedef void stbi_write_func(void *context, void *data, int size);
  106. 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);
  107. STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
  108. STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
  109. STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data);
  110. // ATOMIC BEGIN
  111. unsigned char *stbi_write_png_to_mem(unsigned char *pixels, int stride_bytes, int x, int y, int n, int *out_len);
  112. // ATOMIC END
  113. #ifdef __cplusplus
  114. }
  115. #endif
  116. #endif//INCLUDE_STB_IMAGE_WRITE_H
  117. /* Revision history
  118. 1.02 (2016-04-02)
  119. avoid allocating large structures on the stack
  120. 1.01 (2016-01-16)
  121. STBIW_REALLOC_SIZED: support allocators with no realloc support
  122. avoid race-condition in crc initialization
  123. minor compile issues
  124. 1.00 (2015-09-14)
  125. installable file IO function
  126. 0.99 (2015-09-13)
  127. warning fixes; TGA rle support
  128. 0.98 (2015-04-08)
  129. added STBIW_MALLOC, STBIW_ASSERT etc
  130. 0.97 (2015-01-18)
  131. fixed HDR asserts, rewrote HDR rle logic
  132. 0.96 (2015-01-17)
  133. add HDR output
  134. fix monochrome BMP
  135. 0.95 (2014-08-17)
  136. add monochrome TGA output
  137. 0.94 (2014-05-31)
  138. rename private functions to avoid conflicts with stb_image.h
  139. 0.93 (2014-05-27)
  140. warning fixes
  141. 0.92 (2010-08-01)
  142. casts to unsigned char to fix warnings
  143. 0.91 (2010-07-17)
  144. first public release
  145. 0.90 first internal release
  146. */