gosu 1.5.0 → 2.0.0.pre6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (241) hide show
  1. checksums.yaml +4 -4
  2. data/dependencies/SDL/include/SDL.h +233 -0
  3. data/dependencies/SDL/include/SDL_assert.h +320 -0
  4. data/dependencies/SDL/include/SDL_atomic.h +414 -0
  5. data/dependencies/SDL/include/SDL_audio.h +1500 -0
  6. data/dependencies/SDL/include/{SDL3/SDL_bits.h → SDL_bits.h} +27 -47
  7. data/dependencies/SDL/include/{SDL3/SDL_blendmode.h → SDL_blendmode.h} +55 -59
  8. data/dependencies/SDL/include/SDL_clipboard.h +141 -0
  9. data/dependencies/SDL/include/SDL_config.h +331 -0
  10. data/dependencies/SDL/include/SDL_cpuinfo.h +594 -0
  11. data/dependencies/SDL/include/{SDL3/SDL_egl.h → SDL_egl.h} +8 -11
  12. data/dependencies/SDL/include/SDL_endian.h +348 -0
  13. data/dependencies/SDL/include/SDL_error.h +163 -0
  14. data/dependencies/SDL/include/SDL_events.h +1166 -0
  15. data/dependencies/SDL/include/SDL_filesystem.h +149 -0
  16. data/dependencies/SDL/include/SDL_gamecontroller.h +1074 -0
  17. data/dependencies/SDL/include/SDL_gesture.h +117 -0
  18. data/dependencies/SDL/include/SDL_guid.h +100 -0
  19. data/dependencies/SDL/include/SDL_haptic.h +1341 -0
  20. data/dependencies/SDL/include/SDL_hidapi.h +451 -0
  21. data/dependencies/SDL/include/SDL_hints.h +2613 -0
  22. data/dependencies/SDL/include/SDL_joystick.h +1069 -0
  23. data/dependencies/SDL/include/SDL_keyboard.h +353 -0
  24. data/dependencies/SDL/include/SDL_keycode.h +358 -0
  25. data/dependencies/SDL/include/SDL_loadso.h +115 -0
  26. data/dependencies/SDL/include/{SDL3/SDL_locale.h → SDL_locale.h} +24 -40
  27. data/dependencies/SDL/include/SDL_log.h +404 -0
  28. data/dependencies/SDL/include/SDL_main.h +282 -0
  29. data/dependencies/SDL/include/{SDL3/SDL_messagebox.h → SDL_messagebox.h} +48 -85
  30. data/dependencies/SDL/include/{SDL3/SDL_metal.h → SDL_metal.h} +29 -29
  31. data/dependencies/SDL/include/{SDL3/SDL_misc.h → SDL_misc.h} +12 -13
  32. data/dependencies/SDL/include/SDL_mouse.h +464 -0
  33. data/dependencies/SDL/include/SDL_mutex.h +545 -0
  34. data/dependencies/SDL/include/SDL_name.h +33 -0
  35. data/dependencies/SDL/include/{SDL3/SDL_opengl.h → SDL_opengl.h} +252 -1221
  36. data/dependencies/SDL/include/{SDL3/SDL_opengles.h → SDL_opengles.h} +7 -6
  37. data/dependencies/SDL/include/{SDL3/SDL_opengles2.h → SDL_opengles2.h} +11 -10
  38. data/dependencies/SDL/include/SDL_pixels.h +644 -0
  39. data/dependencies/SDL/include/SDL_platform.h +261 -0
  40. data/dependencies/SDL/include/{SDL3/SDL_power.h → SDL_power.h} +19 -40
  41. data/dependencies/SDL/include/SDL_quit.h +58 -0
  42. data/dependencies/SDL/include/SDL_rect.h +376 -0
  43. data/dependencies/SDL/include/SDL_render.h +1924 -0
  44. data/dependencies/SDL/include/SDL_revision.h +7 -0
  45. data/dependencies/SDL/include/SDL_rwops.h +841 -0
  46. data/dependencies/SDL/include/{SDL3/SDL_scancode.h → SDL_scancode.h} +68 -60
  47. data/dependencies/SDL/include/SDL_sensor.h +322 -0
  48. data/dependencies/SDL/include/SDL_shape.h +155 -0
  49. data/dependencies/SDL/include/SDL_stdinc.h +838 -0
  50. data/dependencies/SDL/include/SDL_surface.h +997 -0
  51. data/dependencies/SDL/include/SDL_system.h +623 -0
  52. data/dependencies/SDL/include/SDL_syswm.h +386 -0
  53. data/dependencies/SDL/include/{SDL3/SDL_test.h → SDL_test.h} +22 -16
  54. data/dependencies/SDL/include/{SDL3/SDL_test_assert.h → SDL_test_assert.h} +34 -27
  55. data/dependencies/SDL/include/{SDL3/SDL_test_common.h → SDL_test_common.h} +52 -109
  56. data/dependencies/SDL/include/{SDL3/SDL_test_compare.h → SDL_test_compare.h} +14 -22
  57. data/dependencies/SDL/include/{SDL3/SDL_test_crc32.h → SDL_test_crc32.h} +30 -27
  58. data/dependencies/SDL/include/{SDL3/SDL_test_font.h → SDL_test_font.h} +49 -50
  59. data/dependencies/SDL/include/{SDL3/SDL_test_fuzzer.h → SDL_test_fuzzer.h} +79 -64
  60. data/dependencies/SDL/include/{SDL3/SDL_test_harness.h → SDL_test_harness.h} +30 -47
  61. data/dependencies/SDL/include/SDL_test_images.h +78 -0
  62. data/dependencies/SDL/include/{SDL3/SDL_platform.h → SDL_test_log.h} +26 -25
  63. data/dependencies/SDL/include/{SDL3/SDL_test_md5.h → SDL_test_md5.h} +28 -21
  64. data/dependencies/SDL/include/{SDL3/SDL_test_memory.h → SDL_test_memory.h} +16 -19
  65. data/dependencies/SDL/include/SDL_test_random.h +115 -0
  66. data/dependencies/SDL/include/SDL_thread.h +464 -0
  67. data/dependencies/SDL/include/SDL_timer.h +222 -0
  68. data/dependencies/SDL/include/SDL_touch.h +150 -0
  69. data/dependencies/SDL/include/{SDL3/SDL_copying.h → SDL_types.h} +9 -2
  70. data/dependencies/SDL/include/SDL_version.h +204 -0
  71. data/dependencies/SDL/include/SDL_video.h +2178 -0
  72. data/dependencies/SDL/include/SDL_vulkan.h +215 -0
  73. data/dependencies/SDL/include/begin_code.h +187 -0
  74. data/dependencies/SDL/include/{SDL3/SDL_close_code.h → close_code.h} +6 -7
  75. data/dependencies/SDL/lib/x64/libSDL2.dll.a +0 -0
  76. data/dependencies/SDL/lib/x86/libSDL2.dll.a +0 -0
  77. data/dependencies/SDL_sound/SDL_sound.c +215 -136
  78. data/dependencies/SDL_sound/SDL_sound.h +725 -0
  79. data/dependencies/SDL_sound/SDL_sound_aiff.c +54 -55
  80. data/dependencies/SDL_sound/SDL_sound_au.c +32 -32
  81. data/dependencies/SDL_sound/SDL_sound_coreaudio.c +41 -24
  82. data/dependencies/SDL_sound/SDL_sound_flac.c +12 -32
  83. data/dependencies/SDL_sound/SDL_sound_internal.h +48 -33
  84. data/dependencies/SDL_sound/SDL_sound_modplug.c +29 -21
  85. data/dependencies/SDL_sound/SDL_sound_mp3.c +13 -35
  86. data/dependencies/SDL_sound/SDL_sound_raw.c +15 -14
  87. data/dependencies/SDL_sound/SDL_sound_shn.c +82 -82
  88. data/dependencies/SDL_sound/SDL_sound_voc.c +30 -30
  89. data/dependencies/SDL_sound/SDL_sound_vorbis.c +25 -34
  90. data/dependencies/SDL_sound/SDL_sound_wav.c +82 -86
  91. data/dependencies/SDL_sound/dr_flac.h +1281 -1845
  92. data/dependencies/SDL_sound/dr_mp3.h +617 -1035
  93. data/dependencies/SDL_sound/libmodplug/fastmix.c +1 -1
  94. data/dependencies/SDL_sound/libmodplug/libmodplug.h +4 -5
  95. data/dependencies/SDL_sound/libmodplug/load_ams.c +3 -6
  96. data/dependencies/SDL_sound/libmodplug/load_dsm.c +1 -2
  97. data/dependencies/SDL_sound/libmodplug/load_mt2.c +2 -2
  98. data/dependencies/SDL_sound/libmodplug/modplug.h +1 -2
  99. data/dependencies/SDL_sound/libmodplug/snd_fx.c +3 -3
  100. data/dependencies/SDL_sound/libmodplug/sndfile.c +0 -3
  101. data/dependencies/SDL_sound/libmodplug/sndmix.c +3 -7
  102. data/dependencies/SDL_sound/stb_vorbis.h +343 -397
  103. data/dependencies/mojoAL/AL/alc.h +2 -2
  104. data/dependencies/mojoAL/mojoal.c +1878 -2113
  105. data/dependencies/stb/stb_image.h +1381 -1192
  106. data/dependencies/stb/stb_image_write.h +129 -329
  107. data/dependencies/stb/stb_truetype.h +60 -304
  108. data/dependencies/utf8proc/utf8proc.c +75 -133
  109. data/dependencies/utf8proc/utf8proc.h +50 -131
  110. data/dependencies/utf8proc/utf8proc_data.h +15658 -16213
  111. data/ext/gosu-ffi/extconf.rb +28 -11
  112. data/ext/gosu-ffi/gosu-ffi.def +0 -3
  113. data/ffi/Gosu.cpp +29 -52
  114. data/ffi/Gosu.h +4 -4
  115. data/ffi/Gosu_Color.cpp +36 -80
  116. data/ffi/Gosu_FFI_internal.h +13 -12
  117. data/ffi/Gosu_Font.cpp +11 -16
  118. data/ffi/Gosu_Font.h +4 -4
  119. data/ffi/Gosu_Image.cpp +36 -69
  120. data/ffi/Gosu_Image.h +5 -16
  121. data/include/Gosu/Bitmap.hpp +1 -1
  122. data/include/Gosu/Color.hpp +4 -5
  123. data/include/Gosu/Graphics.hpp +0 -2
  124. data/include/Gosu/Image.hpp +1 -1
  125. data/include/Gosu/Math.hpp +5 -5
  126. data/include/Gosu/Utility.hpp +1 -27
  127. data/include/Gosu/Version.hpp +2 -2
  128. data/lib/SDL2.dll +0 -0
  129. data/lib/gosu/channel.rb +0 -1
  130. data/lib/gosu/color.rb +13 -36
  131. data/lib/gosu/compat.rb +14 -106
  132. data/lib/gosu/constants.rb +24 -24
  133. data/lib/gosu/ffi.rb +13 -24
  134. data/lib/gosu/font.rb +13 -19
  135. data/lib/gosu/gosu.rb +61 -101
  136. data/lib/gosu/image.rb +34 -72
  137. data/lib/gosu/sample.rb +0 -1
  138. data/lib/gosu/song.rb +0 -1
  139. data/lib/gosu/text_input.rb +1 -15
  140. data/lib/gosu/window.rb +10 -31
  141. data/lib64/SDL2.dll +0 -0
  142. data/rdoc/gosu.rb +0 -2
  143. data/src/Audio.cpp +16 -22
  144. data/src/AudioFileSDLSound.cpp +5 -5
  145. data/src/BinPacker.cpp +4 -5
  146. data/src/Bitmap.cpp +9 -16
  147. data/src/Buffer.cpp +14 -5
  148. data/src/ClipRectStack.hpp +83 -17
  149. data/src/Directories.cpp +1 -1
  150. data/src/DrawOp.hpp +17 -14
  151. data/src/DrawOpQueue.hpp +31 -25
  152. data/src/Font.cpp +1 -1
  153. data/src/GosuGLView.cpp +7 -1
  154. data/src/Graphics.cpp +25 -32
  155. data/src/GraphicsImpl.hpp +24 -0
  156. data/src/Image.cpp +2 -2
  157. data/src/Input.cpp +70 -66
  158. data/src/Macro.cpp +26 -43
  159. data/src/Macro.hpp +16 -22
  160. data/src/Math.cpp +7 -20
  161. data/src/OffScreenTarget.cpp +10 -3
  162. data/src/OpenGLContext.cpp +8 -7
  163. data/src/OpenGLContext.hpp +2 -2
  164. data/src/RenderState.hpp +54 -30
  165. data/src/Resolution.cpp +38 -48
  166. data/src/TexChunk.cpp +2 -2
  167. data/src/TextInput.cpp +9 -9
  168. data/src/Texture.cpp +1 -1
  169. data/src/TiledDrawable.cpp +7 -6
  170. data/src/TrueTypeFontUnix.cpp +9 -12
  171. data/src/Utility.cpp +26 -39
  172. data/src/UtilityWin.cpp +2 -6
  173. data/src/Window.cpp +34 -54
  174. metadata +93 -99
  175. data/dependencies/SDL/include/SDL3/SDL.h +0 -91
  176. data/dependencies/SDL/include/SDL3/SDL_assert.h +0 -710
  177. data/dependencies/SDL/include/SDL3/SDL_asyncio.h +0 -550
  178. data/dependencies/SDL/include/SDL3/SDL_atomic.h +0 -692
  179. data/dependencies/SDL/include/SDL3/SDL_audio.h +0 -2382
  180. data/dependencies/SDL/include/SDL3/SDL_begin_code.h +0 -553
  181. data/dependencies/SDL/include/SDL3/SDL_camera.h +0 -535
  182. data/dependencies/SDL/include/SDL3/SDL_clipboard.h +0 -331
  183. data/dependencies/SDL/include/SDL3/SDL_cpuinfo.h +0 -374
  184. data/dependencies/SDL/include/SDL3/SDL_dialog.h +0 -343
  185. data/dependencies/SDL/include/SDL3/SDL_dlopennote.h +0 -234
  186. data/dependencies/SDL/include/SDL3/SDL_endian.h +0 -645
  187. data/dependencies/SDL/include/SDL3/SDL_error.h +0 -226
  188. data/dependencies/SDL/include/SDL3/SDL_events.h +0 -1651
  189. data/dependencies/SDL/include/SDL3/SDL_filesystem.h +0 -537
  190. data/dependencies/SDL/include/SDL3/SDL_gamepad.h +0 -1657
  191. data/dependencies/SDL/include/SDL3/SDL_gpu.h +0 -4637
  192. data/dependencies/SDL/include/SDL3/SDL_guid.h +0 -106
  193. data/dependencies/SDL/include/SDL3/SDL_haptic.h +0 -1461
  194. data/dependencies/SDL/include/SDL3/SDL_hidapi.h +0 -571
  195. data/dependencies/SDL/include/SDL3/SDL_hints.h +0 -5041
  196. data/dependencies/SDL/include/SDL3/SDL_init.h +0 -508
  197. data/dependencies/SDL/include/SDL3/SDL_intrin.h +0 -410
  198. data/dependencies/SDL/include/SDL3/SDL_iostream.h +0 -1379
  199. data/dependencies/SDL/include/SDL3/SDL_joystick.h +0 -1385
  200. data/dependencies/SDL/include/SDL3/SDL_keyboard.h +0 -615
  201. data/dependencies/SDL/include/SDL3/SDL_keycode.h +0 -348
  202. data/dependencies/SDL/include/SDL3/SDL_loadso.h +0 -145
  203. data/dependencies/SDL/include/SDL3/SDL_log.h +0 -541
  204. data/dependencies/SDL/include/SDL3/SDL_main.h +0 -704
  205. data/dependencies/SDL/include/SDL3/SDL_main_impl.h +0 -149
  206. data/dependencies/SDL/include/SDL3/SDL_mouse.h +0 -813
  207. data/dependencies/SDL/include/SDL3/SDL_mutex.h +0 -1117
  208. data/dependencies/SDL/include/SDL3/SDL_oldnames.h +0 -1339
  209. data/dependencies/SDL/include/SDL3/SDL_pen.h +0 -198
  210. data/dependencies/SDL/include/SDL3/SDL_pixels.h +0 -1441
  211. data/dependencies/SDL/include/SDL3/SDL_platform_defines.h +0 -497
  212. data/dependencies/SDL/include/SDL3/SDL_process.h +0 -441
  213. data/dependencies/SDL/include/SDL3/SDL_properties.h +0 -572
  214. data/dependencies/SDL/include/SDL3/SDL_rect.h +0 -528
  215. data/dependencies/SDL/include/SDL3/SDL_render.h +0 -3034
  216. data/dependencies/SDL/include/SDL3/SDL_revision.h +0 -56
  217. data/dependencies/SDL/include/SDL3/SDL_sensor.h +0 -321
  218. data/dependencies/SDL/include/SDL3/SDL_stdinc.h +0 -6186
  219. data/dependencies/SDL/include/SDL3/SDL_storage.h +0 -686
  220. data/dependencies/SDL/include/SDL3/SDL_surface.h +0 -1769
  221. data/dependencies/SDL/include/SDL3/SDL_system.h +0 -840
  222. data/dependencies/SDL/include/SDL3/SDL_test_log.h +0 -83
  223. data/dependencies/SDL/include/SDL3/SDL_thread.h +0 -602
  224. data/dependencies/SDL/include/SDL3/SDL_time.h +0 -249
  225. data/dependencies/SDL/include/SDL3/SDL_timer.h +0 -454
  226. data/dependencies/SDL/include/SDL3/SDL_touch.h +0 -184
  227. data/dependencies/SDL/include/SDL3/SDL_tray.h +0 -544
  228. data/dependencies/SDL/include/SDL3/SDL_version.h +0 -198
  229. data/dependencies/SDL/include/SDL3/SDL_video.h +0 -3497
  230. data/dependencies/SDL/include/SDL3/SDL_vulkan.h +0 -287
  231. data/dependencies/SDL/lib/x64/libSDL3.dll.a +0 -0
  232. data/dependencies/SDL/lib/x86/libSDL3.dll.a +0 -0
  233. data/dependencies/SDL_sound/SDL3_sound/SDL_sound.h +0 -755
  234. data/lib/SDL3.dll +0 -0
  235. data/lib64/SDL3.dll +0 -0
  236. data/src/ClipRectStack.cpp +0 -38
  237. /data/dependencies/SDL/include/{SDL3/SDL_opengl_glext.h → SDL_opengl_glext.h} +0 -0
  238. /data/dependencies/SDL/include/{SDL3/SDL_opengles2_gl2.h → SDL_opengles2_gl2.h} +0 -0
  239. /data/dependencies/SDL/include/{SDL3/SDL_opengles2_gl2ext.h → SDL_opengles2_gl2ext.h} +0 -0
  240. /data/dependencies/SDL/include/{SDL3/SDL_opengles2_gl2platform.h → SDL_opengles2_gl2platform.h} +0 -0
  241. /data/dependencies/SDL/include/{SDL3/SDL_opengles2_khrplatform.h → SDL_opengles2_khrplatform.h} +0 -0
@@ -1,4 +1,4 @@
1
- /* stb_image_write - v1.18 - public domain - http://nothings.org/stb
1
+ /* stb_image_write - v1.16 - public domain - http://nothings.org/stb
2
2
  writes out PNG/BMP/TGA/JPEG/HDR images to C stdio - Sean Barrett 2010-2015
3
3
  no warranty implied; use at your own risk
4
4
 
@@ -44,7 +44,7 @@ USAGE:
44
44
 
45
45
  There are five functions, one for each image file format:
46
46
 
47
- int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
47
+ int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
48
48
  int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
49
49
  int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);
50
50
  int stbi_write_jpg(char const *filename, int w, int h, int comp, const void *data, int quality);
@@ -52,28 +52,18 @@ USAGE:
52
52
 
53
53
  void stbi_flip_vertically_on_write(int flag); // flag is non-zero to flip data vertically
54
54
 
55
- In addition there are four more functions which accept the "ptrdiff_t stride_in_bytes" argument, new functions for
56
- backwards compatibility:
57
-
58
- int stbi_write_bmp_strided(char const *filename, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
59
- int stbi_write_tga_strided(char const *filename, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
60
- int stbi_write_jpg_strided(char const *filename, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes, int quality);
61
- int stbi_write_hdr_strided(char const *filename, int w, int h, int comp, const float *data, ptrdiff_t stride_in_bytes);
62
-
63
55
  There are also five equivalent functions that use an arbitrary write function. You are
64
56
  expected to open/close your file-equivalent before and after calling these:
65
57
 
66
- int stbi_write_png_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
67
- int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
68
- int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
69
- int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data, ptrdiff_t stride_in_bytes);
70
- int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, int quality, const void *data, ptrdiff_t stride_in_bytes);
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);
71
63
 
72
64
  where the callback is:
73
65
  void stbi_write_func(void *context, void *data, int size);
74
66
 
75
- There are also four more "_strided" functions for these as well.
76
-
77
67
  You can configure it with these global variables:
78
68
  int stbi_write_tga_with_rle; // defaults to true; set to 0 to disable RLE
79
69
  int stbi_write_png_compression_level; // defaults to 8; set to higher for more compression
@@ -95,15 +85,6 @@ USAGE:
95
85
  For PNG, "stride_in_bytes" is the distance in bytes from the first byte of
96
86
  a row of pixels to the first byte of the next row of pixels.
97
87
 
98
- The number of components does not generally map to the number of channels
99
- written to the file. In most cases, low-channel data is expanded out to
100
- RGB/RGBA data on write. If you need to write images that actually have
101
- 1 or 2 channels, this library is not sufficient for your needs.
102
-
103
- No color space conversions or transforms beyond trivial channel replication
104
- are applied. That is, for all the lossless formats, the byte values you
105
- pass to stbi_write_* are exactly what ends up in the file.
106
-
107
88
  PNG creates output files with the same number of components as the input.
108
89
  The BMP format expands Y to RGB in the file format and does not
109
90
  output alpha.
@@ -160,8 +141,6 @@ CREDITS:
160
141
  github:ignotion
161
142
  Adam Schackart
162
143
  Andrew Kensler
163
- Sanjay Nambiar
164
- github:Nevermore1994
165
144
 
166
145
  LICENSE
167
146
 
@@ -173,7 +152,6 @@ LICENSE
173
152
  #define INCLUDE_STB_IMAGE_WRITE_H
174
153
 
175
154
  #include <stdlib.h>
176
- #include <stddef.h>
177
155
 
178
156
  // if STB_IMAGE_WRITE_STATIC causes problems, try defining STBIWDEF to 'inline' or 'static inline'
179
157
  #ifndef STBIWDEF
@@ -195,17 +173,12 @@ STBIWDEF int stbi_write_force_png_filter;
195
173
  #endif
196
174
 
197
175
  #ifndef STBI_WRITE_NO_STDIO
198
- STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
176
+ STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
199
177
  STBIWDEF int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
200
178
  STBIWDEF int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);
201
179
  STBIWDEF int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data);
202
180
  STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality);
203
181
 
204
- STBIWDEF int stbi_write_bmp_strided(char const *filename, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
205
- STBIWDEF int stbi_write_tga_strided(char const *filename, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
206
- STBIWDEF int stbi_write_hdr_strided(char const *filename, int w, int h, int comp, const float *data, ptrdiff_t stride_in_bytes);
207
- STBIWDEF int stbi_write_jpg_strided(char const *filename, int x, int y, int comp, const void *data, ptrdiff_t stride_in_bytes, int quality);
208
-
209
182
  #ifdef STBIW_WINDOWS_UTF8
210
183
  STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input);
211
184
  #endif
@@ -213,17 +186,12 @@ STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const w
213
186
 
214
187
  typedef void stbi_write_func(void *context, void *data, int size);
215
188
 
216
- STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
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);
217
190
  STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
218
191
  STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
219
192
  STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data);
220
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);
221
194
 
222
- STBIWDEF int stbi_write_bmp_strided_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
223
- STBIWDEF int stbi_write_tga_strided_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
224
- STBIWDEF int stbi_write_hdr_strided_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data, ptrdiff_t stride_in_bytes);
225
- STBIWDEF int stbi_write_jpg_strided_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, ptrdiff_t stride_in_bytes, int quality);
226
-
227
195
  STBIWDEF void stbi_flip_vertically_on_write(int flip_boolean);
228
196
 
229
197
  #endif//INCLUDE_STB_IMAGE_WRITE_H
@@ -246,8 +214,6 @@ STBIWDEF void stbi_flip_vertically_on_write(int flip_boolean);
246
214
  #include <stdarg.h>
247
215
  #include <stdlib.h>
248
216
  #include <string.h>
249
- #include <limits.h>
250
- #include <float.h>
251
217
  #include <math.h>
252
218
 
253
219
  #if defined(STBIW_MALLOC) && defined(STBIW_FREE) && (defined(STBIW_REALLOC) || defined(STBIW_REALLOC_SIZED))
@@ -302,28 +268,22 @@ typedef struct
302
268
  {
303
269
  stbi_write_func *func;
304
270
  void *context;
305
- void *file_maybe;
306
271
  unsigned char buffer[64];
307
272
  int buf_used;
308
- int ok;
309
273
  } stbi__write_context;
310
274
 
311
275
  // initialize a callback-based context
312
276
  static void stbi__start_write_callbacks(stbi__write_context *s, stbi_write_func *c, void *context)
313
277
  {
314
- s->func = c;
315
- s->context = context;
316
- s->file_maybe = NULL;
317
- s->ok = 1;
278
+ s->func = c;
279
+ s->context = context;
318
280
  }
319
281
 
320
282
  #ifndef STBI_WRITE_NO_STDIO
321
283
 
322
284
  static void stbi__stdio_write(void *context, void *data, int size)
323
285
  {
324
- stbi__write_context *s = (stbi__write_context*)context;
325
- if (fwrite(data,1,size,(FILE*) s->file_maybe) != (size_t) size)
326
- s->ok = 0;
286
+ fwrite(data,1,size,(FILE*) context);
327
287
  }
328
288
 
329
289
  #if defined(_WIN32) && defined(STBIW_WINDOWS_UTF8)
@@ -372,61 +332,17 @@ static FILE *stbiw__fopen(char const *filename, char const *mode)
372
332
  static int stbi__start_write_file(stbi__write_context *s, const char *filename)
373
333
  {
374
334
  FILE *f = stbiw__fopen(filename, "wb");
375
- stbi__start_write_callbacks(s, stbi__stdio_write, s);
376
- s->file_maybe = (void *) f;
335
+ stbi__start_write_callbacks(s, stbi__stdio_write, (void *) f);
377
336
  return f != NULL;
378
337
  }
379
338
 
380
- static int stbi__end_write_file(stbi__write_context *s, int result)
339
+ static void stbi__end_write_file(stbi__write_context *s)
381
340
  {
382
- if (fclose((FILE *)s->file_maybe) != 0)
383
- s->ok = 0;
384
- return s->ok ? result : 0;
341
+ fclose((FILE *)s->context);
385
342
  }
386
343
 
387
344
  #endif // !STBI_WRITE_NO_STDIO
388
345
 
389
- // return 1 if the sum is valid, 0 on overflow.
390
- // negative terms are considered invalid.
391
- static int stbiw__addsizes_valid(int a, int b)
392
- {
393
- if (b < 0) return 0;
394
- // now 0 <= b <= INT_MAX, hence also
395
- // 0 <= INT_MAX - b <= INTMAX.
396
- // And "a + b <= INT_MAX" (which might overflow) is the
397
- // same as a <= INT_MAX - b (no overflow)
398
- return a <= INT_MAX - b;
399
- }
400
-
401
- // returns 1 if the product is valid, 0 on overflow.
402
- // negative factors are considered invalid.
403
- static int stbiw__mul2sizes_valid(int a, int b)
404
- {
405
- if (a < 0 || b < 0) return 0;
406
- if (b == 0) return 1; // mul-by-0 is always safe
407
- // portable way to check for no overflows in a*b
408
- return a <= INT_MAX/b;
409
- }
410
-
411
- // returns 1 if "a*b + add" has no negative terms/factors and doesn't overflow
412
- static int stbiw__mad2sizes_valid(int a, int b, int add)
413
- {
414
- return stbiw__mul2sizes_valid(a, b) && stbiw__addsizes_valid(a*b, add);
415
- }
416
-
417
- // does some boilerplate validation on image dimensions that is the same for all formats.
418
- // it's intended to make sure dimensions are sane and avoid overflow issues.
419
- static int stbiw__validate_dimensions(int x, int y, int comp)
420
- {
421
- if (comp < 1 || comp > 4) return 0;
422
- // limit x to [1,INT_MAX/4] to exclude negative or 0 dimensions.
423
- // INT_MAX/4 because it guarantees that x*comp <= INT_MAX, and that's our usual offset calc.
424
- if (x <= 0 || x > INT_MAX / 4) return 0;
425
- // same bounds on y for symmetry
426
- if (y <= 0 || y > INT_MAX / 4) return 0;
427
- return 1;
428
- }
429
-
430
346
  typedef unsigned int stbiw_uint32;
431
347
  typedef int stb_image_write_test[sizeof(stbiw_uint32)==4 ? 1 : -1];
432
348
 
@@ -532,12 +448,12 @@ static void stbiw__write_pixel(stbi__write_context *s, int rgb_dir, int comp, in
532
448
  stbiw__write1(s, d[comp - 1]);
533
449
  }
534
450
 
535
- static void stbiw__write_pixels(stbi__write_context *s, int rgb_dir, int vdir, int x, int y, int comp, void *data, ptrdiff_t stride_in_bytes, int write_alpha, int scanline_pad, int expand_mono)
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)
536
452
  {
537
453
  stbiw_uint32 zero = 0;
538
454
  int i,j, j_end;
539
455
 
540
- if (!stbiw__validate_dimensions(x,y,comp))
456
+ if (y <= 0)
541
457
  return;
542
458
 
543
459
  if (stbi__flip_vertically_on_write)
@@ -549,12 +465,9 @@ static void stbiw__write_pixels(stbi__write_context *s, int rgb_dir, int vdir, i
549
465
  j_end = y; j = 0;
550
466
  }
551
467
 
552
- if (stride_in_bytes == 0)
553
- stride_in_bytes = x*comp;
554
-
555
468
  for (; j != j_end; j += vdir) {
556
469
  for (i=0; i < x; ++i) {
557
- unsigned char *d = (unsigned char *) data + j*stride_in_bytes + i*comp;
470
+ unsigned char *d = (unsigned char *) data + (j*x+i)*comp;
558
471
  stbiw__write_pixel(s, rgb_dir, comp, write_alpha, expand_mono, d);
559
472
  }
560
473
  stbiw__write_flush(s);
@@ -562,7 +475,7 @@ static void stbiw__write_pixels(stbi__write_context *s, int rgb_dir, int vdir, i
562
475
  }
563
476
  }
564
477
 
565
- static int stbiw__outfile(stbi__write_context *s, int rgb_dir, int vdir, int x, int y, int comp, int expand_mono, void *data, ptrdiff_t stride_in_bytes, int alpha, int pad, const char *fmt, ...)
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, ...)
566
479
  {
567
480
  if (y < 0 || x < 0) {
568
481
  return 0;
@@ -571,17 +484,17 @@ static int stbiw__outfile(stbi__write_context *s, int rgb_dir, int vdir, int x,
571
484
  va_start(v, fmt);
572
485
  stbiw__writefv(s, fmt, v);
573
486
  va_end(v);
574
- stbiw__write_pixels(s,rgb_dir,vdir,x,y,comp,data,stride_in_bytes,alpha,pad, expand_mono);
487
+ stbiw__write_pixels(s,rgb_dir,vdir,x,y,comp,data,alpha,pad, expand_mono);
575
488
  return 1;
576
489
  }
577
490
  }
578
491
 
579
- static int stbi_write_bmp_core(stbi__write_context *s, int x, int y, int comp, const void *data, ptrdiff_t stride_in_bytes)
492
+ static int stbi_write_bmp_core(stbi__write_context *s, int x, int y, int comp, const void *data)
580
493
  {
581
494
  if (comp != 4) {
582
495
  // write RGB bitmap
583
496
  int pad = (-x*3) & 3;
584
- return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *) data,stride_in_bytes,0,pad,
497
+ return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *) data,0,pad,
585
498
  "11 4 22 4" "4 44 22 444444",
586
499
  'B', 'M', 14+40+(x*3+pad)*y, 0,0, 14+40, // file header
587
500
  40, x,y, 1,24, 0,0,0,0,0,0); // bitmap header
@@ -589,55 +502,44 @@ static int stbi_write_bmp_core(stbi__write_context *s, int x, int y, int comp, c
589
502
  // RGBA bitmaps need a v4 header
590
503
  // use BI_BITFIELDS mode with 32bpp and alpha mask
591
504
  // (straight BI_RGB with alpha mask doesn't work in most readers)
592
- return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *)data,stride_in_bytes,1,0,
505
+ return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *)data,1,0,
593
506
  "11 4 22 4" "4 44 22 444444 4444 4 444 444 444 444",
594
507
  'B', 'M', 14+108+x*y*4, 0, 0, 14+108, // file header
595
- 108, x,y, 1,32, 3,0,0,0,0,0, 0xff0000,0xff00,0xff,0xff000000u, 0x73524742, 0,0,0, 0,0,0, 0,0,0, 0,0,0); // bitmap V4 header
508
+ 108, x,y, 1,32, 3,0,0,0,0,0, 0xff0000,0xff00,0xff,0xff000000u, 0, 0,0,0, 0,0,0, 0,0,0, 0,0,0); // bitmap V4 header
596
509
  }
597
510
  }
598
511
 
599
- STBIWDEF int stbi_write_bmp_strided_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes)
600
- {
601
- stbi__write_context s;
602
- memset(&s, 0, sizeof(s));
603
- stbi__start_write_callbacks(&s, func, context);
604
- return stbi_write_bmp_core(&s, w, h, comp, data, stride_in_bytes);
605
- }
606
-
607
512
  STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data)
608
513
  {
609
- return stbi_write_bmp_strided_to_func(func, context, x, y, comp, data, 0);
514
+ stbi__write_context s = { 0 };
515
+ stbi__start_write_callbacks(&s, func, context);
516
+ return stbi_write_bmp_core(&s, x, y, comp, data);
610
517
  }
611
518
 
612
519
  #ifndef STBI_WRITE_NO_STDIO
613
- STBIWDEF int stbi_write_bmp_strided(char const *filename, int x, int y, int comp, const void *data, ptrdiff_t stride_in_bytes)
520
+ STBIWDEF int stbi_write_bmp(char const *filename, int x, int y, int comp, const void *data)
614
521
  {
615
- stbi__write_context s;
616
- memset(&s, 0, sizeof(s));
522
+ stbi__write_context s = { 0 };
617
523
  if (stbi__start_write_file(&s,filename)) {
618
- int r = stbi_write_bmp_core(&s, x, y, comp, data, stride_in_bytes);
619
- return stbi__end_write_file(&s, r);
524
+ int r = stbi_write_bmp_core(&s, x, y, comp, data);
525
+ stbi__end_write_file(&s);
526
+ return r;
620
527
  } else
621
528
  return 0;
622
529
  }
623
-
624
- STBIWDEF int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data)
625
- {
626
- return stbi_write_bmp_strided(filename, w, h, comp, data, 0);
627
- }
628
530
  #endif //!STBI_WRITE_NO_STDIO
629
531
 
630
- static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, void *data, ptrdiff_t stride_in_bytes)
532
+ static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, void *data)
631
533
  {
632
534
  int has_alpha = (comp == 2 || comp == 4);
633
535
  int colorbytes = has_alpha ? comp-1 : comp;
634
536
  int format = colorbytes < 2 ? 3 : 2; // 3 color channels (RGB/RGBA) = 2, 1 color channel (Y/YA) = 3
635
537
 
636
- if (!stbiw__validate_dimensions(x, y, comp))
538
+ if (y < 0 || x < 0)
637
539
  return 0;
638
540
 
639
541
  if (!stbi_write_tga_with_rle) {
640
- return stbiw__outfile(s, -1, -1, x, y, comp, 0, (void *) data, stride_in_bytes, has_alpha, 0,
542
+ return stbiw__outfile(s, -1, -1, x, y, comp, 0, (void *) data, has_alpha, 0,
641
543
  "111 221 2222 11", 0, 0, format, 0, 0, 0, 0, 0, x, y, (colorbytes + has_alpha) * 8, has_alpha * 8);
642
544
  } else {
643
545
  int i,j,k;
@@ -654,11 +556,8 @@ static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, v
654
556
  jend = -1;
655
557
  jdir = -1;
656
558
  }
657
- if (stride_in_bytes == 0)
658
- stride_in_bytes = x * comp;
659
-
660
559
  for (; j != jend; j += jdir) {
661
- unsigned char *row = (unsigned char *) data + j * stride_in_bytes;
560
+ unsigned char *row = (unsigned char *) data + j * x * comp;
662
561
  int len;
663
562
 
664
563
  for (i = 0; i < x; i += len) {
@@ -709,95 +608,47 @@ static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, v
709
608
  return 1;
710
609
  }
711
610
 
712
- STBIWDEF int stbi_write_tga_strided_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes)
713
- {
714
- stbi__write_context s;
715
- memset(&s, 0, sizeof(s));
716
- stbi__start_write_callbacks(&s, func, context);
717
- return stbi_write_tga_core(&s, w, h, comp, (void *) data, stride_in_bytes);
718
- }
719
-
720
611
  STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data)
721
612
  {
722
- return stbi_write_tga_strided_to_func(func, context, x, y, comp, data, 0);
613
+ stbi__write_context s = { 0 };
614
+ stbi__start_write_callbacks(&s, func, context);
615
+ return stbi_write_tga_core(&s, x, y, comp, (void *) data);
723
616
  }
724
617
 
725
618
  #ifndef STBI_WRITE_NO_STDIO
726
- STBIWDEF int stbi_write_tga_strided(char const *filename, int x, int y, int comp, const void *data, ptrdiff_t stride_in_bytes)
619
+ STBIWDEF int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data)
727
620
  {
728
- stbi__write_context s;
729
- memset(&s, 0, sizeof(s));
621
+ stbi__write_context s = { 0 };
730
622
  if (stbi__start_write_file(&s,filename)) {
731
- int r = stbi_write_tga_core(&s, x, y, comp, (void *) data, stride_in_bytes);
732
- return stbi__end_write_file(&s, r);
623
+ int r = stbi_write_tga_core(&s, x, y, comp, (void *) data);
624
+ stbi__end_write_file(&s);
625
+ return r;
733
626
  } else
734
627
  return 0;
735
628
  }
736
-
737
- STBIWDEF int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data)
738
- {
739
- return stbi_write_tga_strided(filename, x, y, comp, data, 0);
740
- }
741
629
  #endif
742
630
 
743
631
  // *************************************************************************************************
744
632
  // Radiance RGBE HDR writer
745
633
  // by Baldur Karlsson
746
634
 
747
- static void stbiw__linear_to_rgbe(unsigned char *rgbe, float *pixel, int ncomp)
635
+ #define stbiw__max(a, b) ((a) > (b) ? (a) : (b))
636
+
637
+ #ifndef STBI_WRITE_NO_STDIO
638
+
639
+ static void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear)
748
640
  {
749
641
  int exponent;
750
- float r, g, b, maxcomp;
751
-
752
- if (ncomp >= 3) {
753
- r = pixel[0];
754
- g = pixel[1];
755
- b = pixel[2];
756
- maxcomp = r > g ? r : g;
757
- maxcomp = maxcomp > b ? maxcomp : b;
758
- } else {
759
- r = g = b = maxcomp = pixel[0];
760
- }
761
-
762
- // if one of the values is infinite, clamp it to FLT_MAX.
763
- if (maxcomp >= FLT_MAX) {
764
- r = (r < FLT_MAX) ? r : FLT_MAX;
765
- g = (g < FLT_MAX) ? g : FLT_MAX;
766
- b = (b < FLT_MAX) ? b : FLT_MAX;
767
- maxcomp = FLT_MAX;
768
- }
642
+ float maxcomp = stbiw__max(linear[0], stbiw__max(linear[1], linear[2]));
769
643
 
770
- // if maxcomp <= 1e-32f, snap to 0. this is in both the original RADIANCE and
771
- // Bruce Walter's widely-used implementation, so we consider this part of the spec.
772
- if (maxcomp <= 1e-32f) {
644
+ if (maxcomp < 1e-32f) {
773
645
  rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0;
774
646
  } else {
775
- // this is always an exact power of 2 and has no overflows or underflows, but the
776
- // reason why is a bit subtle. first, the no overflows/underflows part:
777
- //
778
- // in here, 2^(-107) < 1e-32 < maxcomp <= FLT_MAX < 2^128
779
- //
780
- // 0.5 <= frexp(maxcomp, &exponent) < 1 [frexp scale]
781
- // => 128 <= frexp(maxcomp, &exponent) * 256 < 256 [mul by 256]
782
- // => 128 / maxcomp <= normalize < 256 / maxcomp [div by maxcomp]
783
- //
784
- // normalize >= 128 / maxcomp > 128 / 2^128 = 2^(7 - 128) = 2^(-121) -> no underflow
785
- // normalize < 256 / maxcomp < 256 / 2^(-107) = 2^(8 + 107) = 2^115 -> no overflow
786
- //
787
- // exact power of 2: the return value of frexp has the same significand as maxcomp,
788
- // by definition; the only difference is the exponent.
789
- // multiplying by 256 changes the exponent, but not the significand, thus exact
790
- // (barring overflow/underflow, which we just proved doesn't occur)
791
- //
792
- // therefore, the division divides two values with the same significand, making
793
- // the quotient exact with a significand of 1 (in any rounding mode)
794
- //
795
- // -> normalize is an exact power of 2
796
- // (this was also verified exhaustively for all maxcomp in [1e-32f, FLT_MAX])
797
- float normalize = (float) (frexp(maxcomp, &exponent) * 256.0f) / maxcomp;
798
- rgbe[0] = (unsigned char)((r > 0) ? r * normalize : 0);
799
- rgbe[1] = (unsigned char)((g > 0) ? g * normalize : 0);
800
- rgbe[2] = (unsigned char)((b > 0) ? b * normalize : 0);
647
+ float normalize = (float) frexp(maxcomp, &exponent) * 256.0f/maxcomp;
648
+
649
+ rgbe[0] = (unsigned char)(linear[0] * normalize);
650
+ rgbe[1] = (unsigned char)(linear[1] * normalize);
651
+ rgbe[2] = (unsigned char)(linear[2] * normalize);
801
652
  rgbe[3] = (unsigned char)(exponent + 128);
802
653
  }
803
654
  }
@@ -822,6 +673,7 @@ static void stbiw__write_hdr_scanline(stbi__write_context *s, int width, int nco
822
673
  {
823
674
  unsigned char scanlineheader[4] = { 2, 2, 0, 0 };
824
675
  unsigned char rgbe[4];
676
+ float linear[3];
825
677
  int x;
826
678
 
827
679
  scanlineheader[2] = (width&0xff00)>>8;
@@ -830,14 +682,34 @@ static void stbiw__write_hdr_scanline(stbi__write_context *s, int width, int nco
830
682
  /* skip RLE for images too small or large */
831
683
  if (width < 8 || width >= 32768) {
832
684
  for (x=0; x < width; x++) {
833
- stbiw__linear_to_rgbe(rgbe, &scanline[x*ncomp], ncomp);
685
+ switch (ncomp) {
686
+ case 4: /* fallthrough */
687
+ case 3: linear[2] = scanline[x*ncomp + 2];
688
+ linear[1] = scanline[x*ncomp + 1];
689
+ linear[0] = scanline[x*ncomp + 0];
690
+ break;
691
+ default:
692
+ linear[0] = linear[1] = linear[2] = scanline[x*ncomp + 0];
693
+ break;
694
+ }
695
+ stbiw__linear_to_rgbe(rgbe, linear);
834
696
  s->func(s->context, rgbe, 4);
835
697
  }
836
698
  } else {
837
699
  int c,r;
838
700
  /* encode into scratch buffer */
839
701
  for (x=0; x < width; x++) {
840
- stbiw__linear_to_rgbe(rgbe, &scanline[x*ncomp], ncomp);
702
+ switch(ncomp) {
703
+ case 4: /* fallthrough */
704
+ case 3: linear[2] = scanline[x*ncomp + 2];
705
+ linear[1] = scanline[x*ncomp + 1];
706
+ linear[0] = scanline[x*ncomp + 0];
707
+ break;
708
+ default:
709
+ linear[0] = linear[1] = linear[2] = scanline[x*ncomp + 0];
710
+ break;
711
+ }
712
+ stbiw__linear_to_rgbe(rgbe, linear);
841
713
  scratch[x + width*0] = rgbe[0];
842
714
  scratch[x + width*1] = rgbe[1];
843
715
  scratch[x + width*2] = rgbe[2];
@@ -886,93 +758,49 @@ static void stbiw__write_hdr_scanline(stbi__write_context *s, int width, int nco
886
758
  }
887
759
  }
888
760
 
889
- static int stbiw__u32_to_string(unsigned int value, char* dest)
761
+ static int stbi_write_hdr_core(stbi__write_context *s, int x, int y, int comp, float *data)
890
762
  {
891
- char buff[12] = {0,0,0,0, 0,0,0,0, 0,0,0,0};
892
- char* s = &buff[11];
893
- int len = 0;
894
-
895
- do {
896
- *--s = '0' + (value % 10);
897
- value /= 10;
898
- } while (value != 0);
899
-
900
- len = (int) (11 - (s - buff));
901
- memcpy(dest, s, len);
902
- return len;
903
- }
904
-
905
- static int stbi_write_hdr_core(stbi__write_context *s, int x, int y, int comp, float *data, ptrdiff_t stride_in_bytes)
906
- {
907
- const char header_str_part1[] = "EXPOSURE= 1.0000000000000\n\n-Y ";
908
- const char header_str_part2[] = " +X ";
909
- const int header_str_part1_len = (int) (sizeof(header_str_part1) - 1); // sizeof includes terminating 0
910
- const int header_str_part2_len = (int) (sizeof(header_str_part2) - 1);
911
-
912
- if (!stbiw__validate_dimensions(x, y, comp) || data == NULL)
763
+ if (y <= 0 || x <= 0 || data == NULL)
913
764
  return 0;
914
765
  else {
915
766
  // Each component is stored separately. Allocate scratch space for full output scanline.
916
767
  unsigned char *scratch = (unsigned char *) STBIW_MALLOC(x*4);
917
- int i;
768
+ int i, len;
918
769
  char buffer[128];
919
- char* dst = buffer;
920
770
  char header[] = "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n";
921
-
922
- if (scratch == NULL)
923
- return 0;
924
-
925
771
  s->func(s->context, header, sizeof(header)-1);
926
772
 
927
- memcpy(dst, header_str_part1, header_str_part1_len);
928
- dst += header_str_part1_len;
929
- dst += stbiw__u32_to_string(y, dst);
930
- memcpy(dst, header_str_part2, header_str_part2_len);
931
- dst += header_str_part2_len;
932
- dst += stbiw__u32_to_string(x, dst);
933
- *dst++ = '\n';
934
-
935
- s->func(s->context, buffer, (int) (dst - buffer));
936
- if (stride_in_bytes == 0)
937
- stride_in_bytes = comp*x;
773
+ #ifdef __STDC_LIB_EXT1__
774
+ len = sprintf_s(buffer, sizeof(buffer), "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x);
775
+ #else
776
+ len = sprintf(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x);
777
+ #endif
778
+ s->func(s->context, buffer, len);
938
779
 
939
780
  for(i=0; i < y; i++)
940
- stbiw__write_hdr_scanline(s, x, comp, scratch, data + stride_in_bytes*(stbi__flip_vertically_on_write ? y-1-i : i));
781
+ stbiw__write_hdr_scanline(s, x, comp, scratch, data + comp*x*(stbi__flip_vertically_on_write ? y-1-i : i));
941
782
  STBIW_FREE(scratch);
942
783
  return 1;
943
784
  }
944
785
  }
945
786
 
946
- STBIWDEF int stbi_write_hdr_strided_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const float *data, ptrdiff_t stride_in_bytes)
947
- {
948
- stbi__write_context s;
949
- memset(&s, 0, sizeof(s));
950
- stbi__start_write_callbacks(&s, func, context);
951
- return stbi_write_hdr_core(&s, x, y, comp, (float *) data, stride_in_bytes);
952
- }
953
-
954
787
  STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const float *data)
955
788
  {
956
- return stbi_write_hdr_strided_to_func(func, context, x,y, comp, data, 0);
789
+ stbi__write_context s = { 0 };
790
+ stbi__start_write_callbacks(&s, func, context);
791
+ return stbi_write_hdr_core(&s, x, y, comp, (float *) data);
957
792
  }
958
793
 
959
-
960
- #ifndef STBI_WRITE_NO_STDIO
961
- STBIWDEF int stbi_write_hdr_strided(char const *filename, int x, int y, int comp, const float *data, ptrdiff_t stride_in_bytes)
794
+ STBIWDEF int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *data)
962
795
  {
963
- stbi__write_context s;
964
- memset(&s, 0, sizeof(s));
796
+ stbi__write_context s = { 0 };
965
797
  if (stbi__start_write_file(&s,filename)) {
966
- int r = stbi_write_hdr_core(&s, x, y, comp, (float *) data, stride_in_bytes);
967
- return stbi__end_write_file(&s, r);
798
+ int r = stbi_write_hdr_core(&s, x, y, comp, (float *) data);
799
+ stbi__end_write_file(&s);
800
+ return r;
968
801
  } else
969
802
  return 0;
970
803
  }
971
-
972
- STBIWDEF int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *data)
973
- {
974
- return stbi_write_hdr_strided(filename, x, y, comp, data, 0);
975
- }
976
804
  #endif // STBI_WRITE_NO_STDIO
977
805
 
978
806
 
@@ -989,7 +817,7 @@ STBIWDEF int stbi_write_hdr(char const *filename, int x, int y, int comp, const
989
817
 
990
818
  #define stbiw__sbneedgrow(a,n) ((a)==0 || stbiw__sbn(a)+n >= stbiw__sbm(a))
991
819
  #define stbiw__sbmaybegrow(a,n) (stbiw__sbneedgrow(a,(n)) ? stbiw__sbgrow(a,n) : 0)
992
- #define stbiw__sbgrow(a,n) stbiw__sbgrowf((void **) &(a), (n), sizeof(*(a)))
820
+ #define stbiw__sbgrow(a,n) stbiw__sbgrowf((void **) &(a), (n), sizeof(*(a)))
993
821
 
994
822
  #define stbiw__sbpush(a, v) (stbiw__sbmaybegrow(a,1), (a)[stbiw__sbn(a)++] = (v))
995
823
  #define stbiw__sbcount(a) ((a) ? stbiw__sbn(a) : 0)
@@ -997,21 +825,14 @@ STBIWDEF int stbi_write_hdr(char const *filename, int x, int y, int comp, const
997
825
 
998
826
  static void *stbiw__sbgrowf(void **arr, int increment, int itemsize)
999
827
  {
1000
- if (*arr == NULL || stbiw__mad2sizes_valid(2, stbiw__sbm(*arr), increment)) {
1001
- int m = *arr ? 2*stbiw__sbm(*arr)+increment : increment+1;
1002
- if (stbiw__mad2sizes_valid(itemsize, m, sizeof(int)*2)) {
1003
- void *p = STBIW_REALLOC_SIZED(*arr ? stbiw__sbraw(*arr) : 0, *arr ? (stbiw__sbm(*arr)*itemsize + sizeof(int)*2) : 0
1004
- , itemsize * m + sizeof(int)*2);
1005
- STBIW_ASSERT(p);
1006
- if (p) {
1007
- if (!*arr) ((int *) p)[1] = 0;
1008
- *arr = (void *) ((int *) p + 2);
1009
- stbiw__sbm(*arr) = m;
1010
- }
1011
- } else
1012
- *arr = 0; // force crash
1013
- } else
1014
- *arr = 0; // force crash
828
+ int m = *arr ? 2*stbiw__sbm(*arr)+increment : increment+1;
829
+ void *p = STBIW_REALLOC_SIZED(*arr ? stbiw__sbraw(*arr) : 0, *arr ? (stbiw__sbm(*arr)*itemsize + sizeof(int)*2) : 0, itemsize * m + sizeof(int)*2);
830
+ STBIW_ASSERT(p);
831
+ if (p) {
832
+ if (!*arr) ((int *) p)[1] = 0;
833
+ *arr = (void *) ((int *) p + 2);
834
+ stbiw__sbm(*arr) = m;
835
+ }
1015
836
  return *arr;
1016
837
  }
1017
838
 
@@ -1268,7 +1089,7 @@ static unsigned char stbiw__paeth(int a, int b, int c)
1268
1089
  }
1269
1090
 
1270
1091
  // @OPTIMIZE: provide an option that always forces left-predict or paeth predict
1271
- static void stbiw__encode_png_line(unsigned char *pixels, ptrdiff_t stride_bytes, int width, int height, int y, int n, int filter_type, signed char *line_buffer)
1092
+ static void stbiw__encode_png_line(unsigned char *pixels, int stride_bytes, int width, int height, int y, int n, int filter_type, signed char *line_buffer)
1272
1093
  {
1273
1094
  static int mapping[] = { 0,1,2,3,4 };
1274
1095
  static int firstmap[] = { 0,1,0,5,6 };
@@ -1276,7 +1097,7 @@ static void stbiw__encode_png_line(unsigned char *pixels, ptrdiff_t stride_bytes
1276
1097
  int i;
1277
1098
  int type = mymap[filter_type];
1278
1099
  unsigned char *z = pixels + stride_bytes * (stbi__flip_vertically_on_write ? height-1-y : y);
1279
- ptrdiff_t signed_stride = stbi__flip_vertically_on_write ? -stride_bytes : stride_bytes;
1100
+ int signed_stride = stbi__flip_vertically_on_write ? -stride_bytes : stride_bytes;
1280
1101
 
1281
1102
  if (type==0) {
1282
1103
  memcpy(line_buffer, z, width*n);
@@ -1304,7 +1125,7 @@ static void stbiw__encode_png_line(unsigned char *pixels, ptrdiff_t stride_bytes
1304
1125
  }
1305
1126
  }
1306
1127
 
1307
- STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, ptrdiff_t stride_bytes, int x, int y, int n, int *out_len)
1128
+ STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, int stride_bytes, int x, int y, int n, int *out_len)
1308
1129
  {
1309
1130
  int force_filter = stbi_write_force_png_filter;
1310
1131
  int ctype[5] = { -1, 0, 4, 2, 6 };
@@ -1313,9 +1134,6 @@ STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, ptrdi
1313
1134
  signed char *line_buffer;
1314
1135
  int j,zlen;
1315
1136
 
1316
- if (!stbiw__validate_dimensions(x, y, n))
1317
- return 0;
1318
-
1319
1137
  if (stride_bytes == 0)
1320
1138
  stride_bytes = x * n;
1321
1139
 
@@ -1323,9 +1141,6 @@ STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, ptrdi
1323
1141
  force_filter = -1;
1324
1142
  }
1325
1143
 
1326
- if (!stbiw__mad2sizes_valid(x,n,1)) return 0;
1327
- if (!stbiw__mul2sizes_valid((x*n+1),y)) return 0;
1328
-
1329
1144
  filt = (unsigned char *) STBIW_MALLOC((x*n+1) * y); if (!filt) return 0;
1330
1145
  line_buffer = (signed char *) STBIW_MALLOC(x * n); if (!line_buffer) { STBIW_FREE(filt); return 0; }
1331
1146
  for (j=0; j < y; ++j) {
@@ -1397,7 +1212,7 @@ STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, ptrdi
1397
1212
  }
1398
1213
 
1399
1214
  #ifndef STBI_WRITE_NO_STDIO
1400
- STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const void *data, ptrdiff_t stride_bytes)
1215
+ STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const void *data, int stride_bytes)
1401
1216
  {
1402
1217
  FILE *f;
1403
1218
  int len;
@@ -1413,7 +1228,7 @@ STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const
1413
1228
  }
1414
1229
  #endif
1415
1230
 
1416
- STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, ptrdiff_t stride_bytes)
1231
+ STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int stride_bytes)
1417
1232
  {
1418
1233
  int len;
1419
1234
  unsigned char *png = stbi_write_png_to_mem((const unsigned char *) data, stride_bytes, x, y, comp, &len);
@@ -1435,9 +1250,8 @@ STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int x,
1435
1250
  static const unsigned char stbiw__jpg_ZigZag[] = { 0,1,5,6,14,15,27,28,2,4,7,13,16,26,29,42,3,8,12,17,25,30,41,43,9,11,18,
1436
1251
  24,31,40,44,53,10,19,23,32,39,45,52,54,20,22,33,38,46,51,55,60,21,34,37,47,50,56,59,61,35,36,48,49,57,58,62,63 };
1437
1252
 
1438
- static void stbiw__jpg_writeBits(stbi__write_context *s, unsigned int *bitBufP, int *bitCntP, const unsigned short *bs) {
1439
- unsigned int bitBuf = *bitBufP;
1440
- int bitCnt = *bitCntP;
1253
+ static void stbiw__jpg_writeBits(stbi__write_context *s, int *bitBufP, int *bitCntP, const unsigned short *bs) {
1254
+ int bitBuf = *bitBufP, bitCnt = *bitCntP;
1441
1255
  bitCnt += bs[1];
1442
1256
  bitBuf |= bs[0] << (24 - bitCnt);
1443
1257
  while(bitCnt >= 8) {
@@ -1511,7 +1325,7 @@ static void stbiw__jpg_calcBits(int val, unsigned short bits[2]) {
1511
1325
  bits[0] = val & ((1<<bits[1])-1);
1512
1326
  }
1513
1327
 
1514
- static int stbiw__jpg_processDU(stbi__write_context *s, unsigned int *bitBuf, int *bitCnt, float *CDU, int du_stride, float *fdtbl, int DC, const unsigned short HTDC[256][2], const unsigned short HTAC[256][2]) {
1328
+ static int stbiw__jpg_processDU(stbi__write_context *s, int *bitBuf, int *bitCnt, float *CDU, int du_stride, float *fdtbl, int DC, const unsigned short HTDC[256][2], const unsigned short HTAC[256][2]) {
1515
1329
  const unsigned short EOB[2] = { HTAC[0x00][0], HTAC[0x00][1] };
1516
1330
  const unsigned short M16zeroes[2] = { HTAC[0xF0][0], HTAC[0xF0][1] };
1517
1331
  int dataOff, i, j, n, diff, end0pos, x, y;
@@ -1581,7 +1395,7 @@ static int stbiw__jpg_processDU(stbi__write_context *s, unsigned int *bitBuf, in
1581
1395
  return DU[0];
1582
1396
  }
1583
1397
 
1584
- static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, int comp, const void* data, int quality, ptrdiff_t stride_in_bytes) {
1398
+ static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, int comp, const void* data, int quality) {
1585
1399
  // Constants that don't pollute global namespace
1586
1400
  static const unsigned char std_dc_luminance_nrcodes[] = {0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0};
1587
1401
  static const unsigned char std_dc_luminance_values[] = {0,1,2,3,4,5,6,7,8,9,10,11};
@@ -1657,13 +1471,10 @@ static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, in
1657
1471
  float fdtbl_Y[64], fdtbl_UV[64];
1658
1472
  unsigned char YTable[64], UVTable[64];
1659
1473
 
1660
- if (!data || !stbiw__validate_dimensions(width, height, comp)) {
1474
+ if(!data || !width || !height || comp > 4 || comp < 1) {
1661
1475
  return 0;
1662
1476
  }
1663
1477
 
1664
- if (stride_in_bytes == 0)
1665
- stride_in_bytes = comp*width;
1666
-
1667
1478
  quality = quality ? quality : 90;
1668
1479
  subsample = quality <= 90 ? 1 : 0;
1669
1480
  quality = quality < 1 ? 1 : quality > 100 ? 100 : quality;
@@ -1712,8 +1523,7 @@ static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, in
1712
1523
  {
1713
1524
  static const unsigned short fillBits[] = {0x7F, 7};
1714
1525
  int DCY=0, DCU=0, DCV=0;
1715
- unsigned int bitBuf=0;
1716
- int bitCnt=0;
1526
+ int bitBuf=0, bitCnt=0;
1717
1527
  // comp == 2 is grey+alpha (alpha is ignored)
1718
1528
  int ofsG = comp > 2 ? 1 : 0, ofsB = comp > 2 ? 2 : 0;
1719
1529
  const unsigned char *dataR = (const unsigned char *)data;
@@ -1727,10 +1537,10 @@ static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, in
1727
1537
  for(row = y, pos = 0; row < y+16; ++row) {
1728
1538
  // row >= height => use last input row
1729
1539
  int clamped_row = (row < height) ? row : height - 1;
1730
- ptrdiff_t base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*stride_in_bytes;
1540
+ int base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*width*comp;
1731
1541
  for(col = x; col < x+16; ++col, ++pos) {
1732
1542
  // if col >= width => use pixel from last input column
1733
- ptrdiff_t p = base_p + ((col < width) ? col : (width-1))*comp;
1543
+ int p = base_p + ((col < width) ? col : (width-1))*comp;
1734
1544
  float r = dataR[p], g = dataG[p], b = dataB[p];
1735
1545
  Y[pos]= +0.29900f*r + 0.58700f*g + 0.11400f*b - 128;
1736
1546
  U[pos]= -0.16874f*r - 0.33126f*g + 0.50000f*b;
@@ -1765,10 +1575,10 @@ static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, in
1765
1575
  for(row = y, pos = 0; row < y+8; ++row) {
1766
1576
  // row >= height => use last input row
1767
1577
  int clamped_row = (row < height) ? row : height - 1;
1768
- ptrdiff_t base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*stride_in_bytes;
1578
+ int base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*width*comp;
1769
1579
  for(col = x; col < x+8; ++col, ++pos) {
1770
1580
  // if col >= width => use pixel from last input column
1771
- ptrdiff_t p = base_p + ((col < width) ? col : (width-1))*comp;
1581
+ int p = base_p + ((col < width) ? col : (width-1))*comp;
1772
1582
  float r = dataR[p], g = dataG[p], b = dataB[p];
1773
1583
  Y[pos]= +0.29900f*r + 0.58700f*g + 0.11400f*b - 128;
1774
1584
  U[pos]= -0.16874f*r - 0.33126f*g + 0.50000f*b;
@@ -1794,35 +1604,25 @@ static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, in
1794
1604
  return 1;
1795
1605
  }
1796
1606
 
1797
- STBIWDEF int stbi_write_jpg_strided_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, ptrdiff_t stride_in_bytes, int quality)
1607
+ STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality)
1798
1608
  {
1799
- stbi__write_context s;
1800
- memset(&s, 0, sizeof(s));
1609
+ stbi__write_context s = { 0 };
1801
1610
  stbi__start_write_callbacks(&s, func, context);
1802
- return stbi_write_jpg_core(&s, x, y, comp, (void *) data, quality, stride_in_bytes);
1611
+ return stbi_write_jpg_core(&s, x, y, comp, (void *) data, quality);
1803
1612
  }
1804
1613
 
1805
- STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality)
1806
- {
1807
- return stbi_write_jpg_strided_to_func(func, context, x, y, comp, data, 0, quality);
1808
- }
1809
1614
 
1810
1615
  #ifndef STBI_WRITE_NO_STDIO
1811
- STBIWDEF int stbi_write_jpg_strided(char const *filename, int x, int y, int comp, const void *data, ptrdiff_t stride_in_bytes, int quality)
1616
+ STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality)
1812
1617
  {
1813
- stbi__write_context s;
1814
- memset(&s, 0, sizeof(s));
1618
+ stbi__write_context s = { 0 };
1815
1619
  if (stbi__start_write_file(&s,filename)) {
1816
- int r = stbi_write_jpg_core(&s, x, y, comp, data, quality, stride_in_bytes);
1817
- return stbi__end_write_file(&s, r);
1620
+ int r = stbi_write_jpg_core(&s, x, y, comp, data, quality);
1621
+ stbi__end_write_file(&s);
1622
+ return r;
1818
1623
  } else
1819
1624
  return 0;
1820
1625
  }
1821
-
1822
- STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality)
1823
- {
1824
- return stbi_write_jpg_strided(filename, x, y, comp, data, 0, quality);
1825
- }
1826
1626
  #endif
1827
1627
 
1828
1628
  #endif // STB_IMAGE_WRITE_IMPLEMENTATION