gosu 1.4.6 → 1.5.0

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 (320) hide show
  1. checksums.yaml +4 -4
  2. data/COPYING +2 -1
  3. data/dependencies/SDL/include/SDL3/SDL.h +91 -0
  4. data/dependencies/SDL/include/SDL3/SDL_assert.h +710 -0
  5. data/dependencies/SDL/include/SDL3/SDL_asyncio.h +550 -0
  6. data/dependencies/SDL/include/SDL3/SDL_atomic.h +692 -0
  7. data/dependencies/SDL/include/SDL3/SDL_audio.h +2382 -0
  8. data/dependencies/SDL/include/SDL3/SDL_begin_code.h +553 -0
  9. data/dependencies/SDL/include/{SDL_bits.h → SDL3/SDL_bits.h} +47 -27
  10. data/dependencies/SDL/include/{SDL_blendmode.h → SDL3/SDL_blendmode.h} +59 -55
  11. data/dependencies/SDL/include/SDL3/SDL_camera.h +535 -0
  12. data/dependencies/SDL/include/SDL3/SDL_clipboard.h +331 -0
  13. data/dependencies/SDL/include/{close_code.h → SDL3/SDL_close_code.h} +9 -8
  14. data/dependencies/SDL/include/{SDL_types.h → SDL3/SDL_copying.h} +2 -9
  15. data/dependencies/SDL/include/SDL3/SDL_cpuinfo.h +374 -0
  16. data/dependencies/SDL/include/SDL3/SDL_dialog.h +343 -0
  17. data/dependencies/SDL/include/SDL3/SDL_dlopennote.h +234 -0
  18. data/dependencies/SDL/include/{SDL_egl.h → SDL3/SDL_egl.h} +11 -8
  19. data/dependencies/SDL/include/SDL3/SDL_endian.h +645 -0
  20. data/dependencies/SDL/include/SDL3/SDL_error.h +226 -0
  21. data/dependencies/SDL/include/SDL3/SDL_events.h +1651 -0
  22. data/dependencies/SDL/include/SDL3/SDL_filesystem.h +537 -0
  23. data/dependencies/SDL/include/SDL3/SDL_gamepad.h +1657 -0
  24. data/dependencies/SDL/include/SDL3/SDL_gpu.h +4637 -0
  25. data/dependencies/SDL/include/SDL3/SDL_guid.h +106 -0
  26. data/dependencies/SDL/include/SDL3/SDL_haptic.h +1461 -0
  27. data/dependencies/SDL/include/SDL3/SDL_hidapi.h +571 -0
  28. data/dependencies/SDL/include/SDL3/SDL_hints.h +5041 -0
  29. data/dependencies/SDL/include/SDL3/SDL_init.h +508 -0
  30. data/dependencies/SDL/include/SDL3/SDL_intrin.h +410 -0
  31. data/dependencies/SDL/include/SDL3/SDL_iostream.h +1379 -0
  32. data/dependencies/SDL/include/SDL3/SDL_joystick.h +1385 -0
  33. data/dependencies/SDL/include/SDL3/SDL_keyboard.h +615 -0
  34. data/dependencies/SDL/include/SDL3/SDL_keycode.h +348 -0
  35. data/dependencies/SDL/include/SDL3/SDL_loadso.h +145 -0
  36. data/dependencies/SDL/include/{SDL_locale.h → SDL3/SDL_locale.h} +40 -24
  37. data/dependencies/SDL/include/SDL3/SDL_log.h +541 -0
  38. data/dependencies/SDL/include/SDL3/SDL_main.h +704 -0
  39. data/dependencies/SDL/include/SDL3/SDL_main_impl.h +149 -0
  40. data/dependencies/SDL/include/{SDL_messagebox.h → SDL3/SDL_messagebox.h} +85 -48
  41. data/dependencies/SDL/include/{SDL_metal.h → SDL3/SDL_metal.h} +29 -29
  42. data/dependencies/SDL/include/{SDL_misc.h → SDL3/SDL_misc.h} +13 -12
  43. data/dependencies/SDL/include/SDL3/SDL_mouse.h +813 -0
  44. data/dependencies/SDL/include/SDL3/SDL_mutex.h +1117 -0
  45. data/dependencies/SDL/include/SDL3/SDL_oldnames.h +1339 -0
  46. data/dependencies/SDL/include/{SDL_opengl.h → SDL3/SDL_opengl.h} +1247 -278
  47. data/dependencies/SDL/include/{SDL_opengl_glext.h → SDL3/SDL_opengl_glext.h} +5 -1
  48. data/dependencies/SDL/include/{SDL_opengles.h → SDL3/SDL_opengles.h} +6 -7
  49. data/dependencies/SDL/include/{SDL_opengles2.h → SDL3/SDL_opengles2.h} +10 -11
  50. data/dependencies/SDL/include/SDL3/SDL_pen.h +198 -0
  51. data/dependencies/SDL/include/SDL3/SDL_pixels.h +1441 -0
  52. data/dependencies/SDL/include/{SDL_test_log.h → SDL3/SDL_platform.h} +25 -26
  53. data/dependencies/SDL/include/SDL3/SDL_platform_defines.h +497 -0
  54. data/dependencies/SDL/include/{SDL_power.h → SDL3/SDL_power.h} +40 -20
  55. data/dependencies/SDL/include/SDL3/SDL_process.h +441 -0
  56. data/dependencies/SDL/include/SDL3/SDL_properties.h +572 -0
  57. data/dependencies/SDL/include/SDL3/SDL_rect.h +528 -0
  58. data/dependencies/SDL/include/SDL3/SDL_render.h +3034 -0
  59. data/dependencies/SDL/include/SDL3/SDL_revision.h +56 -0
  60. data/dependencies/SDL/include/{SDL_scancode.h → SDL3/SDL_scancode.h} +60 -68
  61. data/dependencies/SDL/include/SDL3/SDL_sensor.h +321 -0
  62. data/dependencies/SDL/include/SDL3/SDL_stdinc.h +6186 -0
  63. data/dependencies/SDL/include/SDL3/SDL_storage.h +686 -0
  64. data/dependencies/SDL/include/SDL3/SDL_surface.h +1769 -0
  65. data/dependencies/SDL/include/SDL3/SDL_system.h +840 -0
  66. data/dependencies/SDL/include/{SDL_test.h → SDL3/SDL_test.h} +16 -22
  67. data/dependencies/SDL/include/{SDL_test_assert.h → SDL3/SDL_test_assert.h} +27 -34
  68. data/dependencies/SDL/include/{SDL_test_common.h → SDL3/SDL_test_common.h} +109 -52
  69. data/dependencies/SDL/include/{SDL_test_compare.h → SDL3/SDL_test_compare.h} +22 -14
  70. data/dependencies/SDL/include/{SDL_test_crc32.h → SDL3/SDL_test_crc32.h} +27 -30
  71. data/dependencies/SDL/include/{SDL_test_font.h → SDL3/SDL_test_font.h} +50 -49
  72. data/dependencies/SDL/include/{SDL_test_fuzzer.h → SDL3/SDL_test_fuzzer.h} +64 -79
  73. data/dependencies/SDL/include/{SDL_test_harness.h → SDL3/SDL_test_harness.h} +47 -30
  74. data/dependencies/SDL/include/SDL3/SDL_test_log.h +83 -0
  75. data/dependencies/SDL/include/{SDL_test_md5.h → SDL3/SDL_test_md5.h} +21 -28
  76. data/dependencies/SDL/include/{SDL_test_memory.h → SDL3/SDL_test_memory.h} +19 -16
  77. data/dependencies/SDL/include/SDL3/SDL_thread.h +602 -0
  78. data/dependencies/SDL/include/SDL3/SDL_time.h +249 -0
  79. data/dependencies/SDL/include/SDL3/SDL_timer.h +454 -0
  80. data/dependencies/SDL/include/SDL3/SDL_touch.h +184 -0
  81. data/dependencies/SDL/include/SDL3/SDL_tray.h +544 -0
  82. data/dependencies/SDL/include/SDL3/SDL_version.h +198 -0
  83. data/dependencies/SDL/include/SDL3/SDL_video.h +3497 -0
  84. data/dependencies/SDL/include/SDL3/SDL_vulkan.h +287 -0
  85. data/dependencies/SDL/lib/x64/libSDL3.dll.a +0 -0
  86. data/dependencies/SDL/lib/x86/libSDL3.dll.a +0 -0
  87. data/dependencies/SDL_sound/SDL3_sound/SDL_sound.h +755 -0
  88. data/dependencies/SDL_sound/SDL_sound.c +136 -215
  89. data/dependencies/SDL_sound/SDL_sound_aiff.c +55 -54
  90. data/dependencies/SDL_sound/SDL_sound_au.c +32 -32
  91. data/dependencies/SDL_sound/SDL_sound_coreaudio.c +24 -41
  92. data/dependencies/SDL_sound/SDL_sound_flac.c +32 -12
  93. data/dependencies/SDL_sound/SDL_sound_internal.h +33 -48
  94. data/dependencies/SDL_sound/SDL_sound_modplug.c +21 -29
  95. data/dependencies/SDL_sound/SDL_sound_mp3.c +35 -13
  96. data/dependencies/SDL_sound/SDL_sound_raw.c +14 -15
  97. data/dependencies/SDL_sound/SDL_sound_shn.c +82 -82
  98. data/dependencies/SDL_sound/SDL_sound_voc.c +30 -30
  99. data/dependencies/SDL_sound/SDL_sound_vorbis.c +34 -25
  100. data/dependencies/SDL_sound/SDL_sound_wav.c +86 -82
  101. data/dependencies/SDL_sound/dr_flac.h +1890 -1301
  102. data/dependencies/SDL_sound/dr_mp3.h +1098 -660
  103. data/dependencies/SDL_sound/libmodplug/fastmix.c +1 -1
  104. data/dependencies/SDL_sound/libmodplug/libmodplug.h +5 -4
  105. data/dependencies/SDL_sound/libmodplug/load_ams.c +6 -3
  106. data/dependencies/SDL_sound/libmodplug/load_dsm.c +2 -1
  107. data/dependencies/SDL_sound/libmodplug/load_mt2.c +2 -2
  108. data/dependencies/SDL_sound/libmodplug/modplug.h +2 -1
  109. data/dependencies/SDL_sound/libmodplug/snd_fx.c +3 -3
  110. data/dependencies/SDL_sound/libmodplug/sndfile.c +3 -0
  111. data/dependencies/SDL_sound/libmodplug/sndmix.c +7 -3
  112. data/dependencies/SDL_sound/stb_vorbis.h +397 -342
  113. data/dependencies/mojoAL/AL/alc.h +2 -2
  114. data/dependencies/mojoAL/mojoal.c +2120 -1885
  115. data/dependencies/stb/stb_image.h +1190 -1379
  116. data/dependencies/stb/stb_image_write.h +329 -129
  117. data/dependencies/stb/stb_truetype.h +304 -60
  118. data/dependencies/utf8proc/utf8proc.c +133 -75
  119. data/dependencies/utf8proc/utf8proc.h +131 -50
  120. data/dependencies/utf8proc/utf8proc_data.h +16213 -15658
  121. data/ext/gosu-ffi/extconf.rb +103 -0
  122. data/ext/gosu-ffi/gosu-ffi.def +467 -0
  123. data/ffi/Gosu.cpp +330 -0
  124. data/ffi/Gosu.h +84 -0
  125. data/ffi/Gosu_Channel.cpp +62 -0
  126. data/ffi/Gosu_Channel.h +17 -0
  127. data/ffi/Gosu_Color.cpp +176 -0
  128. data/ffi/Gosu_Color.h +31 -0
  129. data/ffi/Gosu_Constants.cpp +334 -0
  130. data/ffi/Gosu_FFI.h +34 -0
  131. data/ffi/Gosu_FFI_internal.h +160 -0
  132. data/ffi/Gosu_Font.cpp +97 -0
  133. data/ffi/Gosu_Font.h +32 -0
  134. data/ffi/Gosu_Image.cpp +239 -0
  135. data/ffi/Gosu_Image.h +71 -0
  136. data/ffi/Gosu_Sample.cpp +29 -0
  137. data/ffi/Gosu_Sample.h +14 -0
  138. data/ffi/Gosu_Song.cpp +69 -0
  139. data/ffi/Gosu_Song.h +18 -0
  140. data/ffi/Gosu_TextInput.cpp +94 -0
  141. data/ffi/Gosu_TextInput.h +25 -0
  142. data/ffi/Gosu_Window.cpp +314 -0
  143. data/ffi/Gosu_Window.h +78 -0
  144. data/include/Gosu/Audio.hpp +6 -11
  145. data/include/Gosu/Bitmap.hpp +38 -53
  146. data/include/Gosu/Buffer.hpp +54 -0
  147. data/include/Gosu/Color.hpp +29 -36
  148. data/include/Gosu/Directories.hpp +25 -28
  149. data/include/Gosu/Drawable.hpp +58 -0
  150. data/include/Gosu/Font.hpp +6 -5
  151. data/include/Gosu/Fwd.hpp +4 -6
  152. data/include/Gosu/Gosu.hpp +5 -5
  153. data/include/Gosu/Graphics.hpp +53 -61
  154. data/include/Gosu/GraphicsBase.hpp +1 -11
  155. data/include/Gosu/Image.hpp +12 -15
  156. data/include/Gosu/Math.hpp +50 -72
  157. data/include/Gosu/Transform.hpp +32 -0
  158. data/include/Gosu/Utility.hpp +77 -1
  159. data/include/Gosu/Version.hpp +2 -2
  160. data/include/Gosu/Window.hpp +15 -9
  161. data/lib/SDL3.dll +0 -0
  162. data/lib/gosu/channel.rb +50 -0
  163. data/lib/gosu/color.rb +173 -0
  164. data/lib/gosu/compat.rb +130 -17
  165. data/lib/gosu/constants.rb +386 -0
  166. data/lib/gosu/ffi.rb +269 -0
  167. data/lib/gosu/font.rb +62 -0
  168. data/lib/gosu/gl_tex_info.rb +33 -0
  169. data/lib/gosu/gosu.rb +250 -0
  170. data/lib/gosu/image.rb +179 -0
  171. data/lib/gosu/numeric.rb +17 -0
  172. data/lib/gosu/preview.rb +6 -6
  173. data/lib/gosu/sample.rb +25 -0
  174. data/lib/gosu/song.rb +57 -0
  175. data/lib/gosu/text_input.rb +83 -0
  176. data/lib/gosu/window.rb +249 -0
  177. data/lib/gosu.rb +29 -8
  178. data/lib64/SDL3.dll +0 -0
  179. data/rdoc/gosu.rb +2 -2
  180. data/src/Audio.cpp +30 -24
  181. data/src/AudioFile.hpp +5 -4
  182. data/src/AudioFileAudioToolbox.cpp +8 -8
  183. data/src/AudioFileSDLSound.cpp +12 -15
  184. data/src/BinPacker.cpp +188 -0
  185. data/src/BinPacker.hpp +55 -0
  186. data/src/Bitmap.cpp +172 -143
  187. data/src/BitmapIO.cpp +60 -86
  188. data/src/Buffer.cpp +150 -0
  189. data/src/ClipRectStack.cpp +38 -0
  190. data/src/ClipRectStack.hpp +17 -83
  191. data/src/Color.cpp +75 -80
  192. data/src/Directories.cpp +47 -0
  193. data/src/DirectoriesUIKit.cpp +50 -0
  194. data/src/DrawOp.hpp +20 -18
  195. data/src/DrawOpQueue.hpp +27 -33
  196. data/src/Drawable.cpp +95 -0
  197. data/src/EmptyDrawable.hpp +38 -0
  198. data/src/FPS.cpp +31 -0
  199. data/src/Font.cpp +104 -74
  200. data/src/GosuGLView.cpp +13 -11
  201. data/src/GosuViewController.cpp +2 -10
  202. data/src/Graphics.cpp +86 -145
  203. data/src/GraphicsImpl.hpp +14 -68
  204. data/src/Image.cpp +41 -35
  205. data/src/Input.cpp +73 -78
  206. data/src/Macro.cpp +46 -29
  207. data/src/Macro.hpp +23 -17
  208. data/src/MarkupParser.cpp +5 -5
  209. data/src/Math.cpp +48 -22
  210. data/src/OffScreenTarget.cpp +47 -50
  211. data/src/OffScreenTarget.hpp +13 -11
  212. data/src/OpenGLContext.cpp +116 -0
  213. data/src/OpenGLContext.hpp +41 -0
  214. data/src/RenderState.hpp +49 -71
  215. data/src/Resolution.cpp +65 -53
  216. data/src/TexChunk.cpp +35 -80
  217. data/src/TexChunk.hpp +44 -35
  218. data/src/Text.cpp +1 -1
  219. data/src/TextBuilder.cpp +35 -21
  220. data/src/TextBuilder.hpp +6 -9
  221. data/src/TextInput.cpp +9 -9
  222. data/src/Texture.cpp +62 -80
  223. data/src/Texture.hpp +25 -23
  224. data/src/TiledDrawable.cpp +149 -0
  225. data/src/TiledDrawable.hpp +47 -0
  226. data/src/TimingApple.cpp +1 -1
  227. data/src/Transform.cpp +45 -50
  228. data/src/TransformStack.hpp +16 -16
  229. data/src/TrueTypeFont.cpp +59 -51
  230. data/src/TrueTypeFont.hpp +6 -7
  231. data/src/TrueTypeFontApple.cpp +28 -19
  232. data/src/TrueTypeFontUnix.cpp +39 -32
  233. data/src/TrueTypeFontWin.cpp +30 -30
  234. data/src/Utility.cpp +113 -37
  235. data/src/UtilityWin.cpp +49 -0
  236. data/src/Window.cpp +121 -151
  237. data/src/WindowUIKit.cpp +14 -14
  238. metadata +165 -118
  239. data/dependencies/SDL/include/SDL.h +0 -233
  240. data/dependencies/SDL/include/SDL_assert.h +0 -320
  241. data/dependencies/SDL/include/SDL_atomic.h +0 -415
  242. data/dependencies/SDL/include/SDL_audio.h +0 -1500
  243. data/dependencies/SDL/include/SDL_clipboard.h +0 -141
  244. data/dependencies/SDL/include/SDL_config.h +0 -331
  245. data/dependencies/SDL/include/SDL_cpuinfo.h +0 -594
  246. data/dependencies/SDL/include/SDL_endian.h +0 -348
  247. data/dependencies/SDL/include/SDL_error.h +0 -163
  248. data/dependencies/SDL/include/SDL_events.h +0 -1166
  249. data/dependencies/SDL/include/SDL_filesystem.h +0 -149
  250. data/dependencies/SDL/include/SDL_gamecontroller.h +0 -1074
  251. data/dependencies/SDL/include/SDL_gesture.h +0 -117
  252. data/dependencies/SDL/include/SDL_guid.h +0 -100
  253. data/dependencies/SDL/include/SDL_haptic.h +0 -1341
  254. data/dependencies/SDL/include/SDL_hidapi.h +0 -451
  255. data/dependencies/SDL/include/SDL_hints.h +0 -2569
  256. data/dependencies/SDL/include/SDL_joystick.h +0 -1066
  257. data/dependencies/SDL/include/SDL_keyboard.h +0 -353
  258. data/dependencies/SDL/include/SDL_keycode.h +0 -358
  259. data/dependencies/SDL/include/SDL_loadso.h +0 -115
  260. data/dependencies/SDL/include/SDL_log.h +0 -404
  261. data/dependencies/SDL/include/SDL_main.h +0 -275
  262. data/dependencies/SDL/include/SDL_mouse.h +0 -465
  263. data/dependencies/SDL/include/SDL_mutex.h +0 -471
  264. data/dependencies/SDL/include/SDL_name.h +0 -33
  265. data/dependencies/SDL/include/SDL_pixels.h +0 -644
  266. data/dependencies/SDL/include/SDL_platform.h +0 -261
  267. data/dependencies/SDL/include/SDL_quit.h +0 -58
  268. data/dependencies/SDL/include/SDL_rect.h +0 -376
  269. data/dependencies/SDL/include/SDL_render.h +0 -1919
  270. data/dependencies/SDL/include/SDL_revision.h +0 -7
  271. data/dependencies/SDL/include/SDL_rwops.h +0 -841
  272. data/dependencies/SDL/include/SDL_sensor.h +0 -322
  273. data/dependencies/SDL/include/SDL_shape.h +0 -155
  274. data/dependencies/SDL/include/SDL_stdinc.h +0 -830
  275. data/dependencies/SDL/include/SDL_surface.h +0 -997
  276. data/dependencies/SDL/include/SDL_system.h +0 -623
  277. data/dependencies/SDL/include/SDL_syswm.h +0 -386
  278. data/dependencies/SDL/include/SDL_test_images.h +0 -78
  279. data/dependencies/SDL/include/SDL_test_random.h +0 -115
  280. data/dependencies/SDL/include/SDL_thread.h +0 -464
  281. data/dependencies/SDL/include/SDL_timer.h +0 -222
  282. data/dependencies/SDL/include/SDL_touch.h +0 -150
  283. data/dependencies/SDL/include/SDL_version.h +0 -204
  284. data/dependencies/SDL/include/SDL_video.h +0 -2150
  285. data/dependencies/SDL/include/SDL_vulkan.h +0 -215
  286. data/dependencies/SDL/include/begin_code.h +0 -187
  287. data/dependencies/SDL/lib/x64/libSDL2.dll.a +0 -0
  288. data/dependencies/SDL/lib/x86/libSDL2.dll.a +0 -0
  289. data/dependencies/SDL_sound/SDL_sound.h +0 -725
  290. data/ext/gosu/extconf.rb +0 -119
  291. data/include/Gosu/IO.hpp +0 -254
  292. data/include/Gosu/ImageData.hpp +0 -53
  293. data/include/Gosu/Inspection.hpp +0 -7
  294. data/lib/SDL2.dll +0 -0
  295. data/lib/gosu/patches.rb +0 -66
  296. data/lib/gosu/run.rb +0 -20
  297. data/lib/gosu/swig_patches.rb +0 -110
  298. data/lib64/SDL2.dll +0 -0
  299. data/src/BlockAllocator.cpp +0 -131
  300. data/src/BlockAllocator.hpp +0 -32
  301. data/src/DirectoriesApple.cpp +0 -69
  302. data/src/DirectoriesUnix.cpp +0 -46
  303. data/src/DirectoriesWin.cpp +0 -65
  304. data/src/EmptyImageData.hpp +0 -52
  305. data/src/FileUnix.cpp +0 -99
  306. data/src/FileWin.cpp +0 -88
  307. data/src/IO.cpp +0 -60
  308. data/src/Iconv.hpp +0 -51
  309. data/src/Inspection.cpp +0 -27
  310. data/src/LargeImageData.cpp +0 -215
  311. data/src/LargeImageData.hpp +0 -39
  312. data/src/Log.hpp +0 -19
  313. data/src/RubyGosu.cxx +0 -13100
  314. data/src/RubyGosu.h +0 -49
  315. data/src/WinUtility.cpp +0 -61
  316. data/src/WinUtility.hpp +0 -27
  317. /data/dependencies/SDL/include/{SDL_opengles2_gl2.h → SDL3/SDL_opengles2_gl2.h} +0 -0
  318. /data/dependencies/SDL/include/{SDL_opengles2_gl2ext.h → SDL3/SDL_opengles2_gl2ext.h} +0 -0
  319. /data/dependencies/SDL/include/{SDL_opengles2_gl2platform.h → SDL3/SDL_opengles2_gl2platform.h} +0 -0
  320. /data/dependencies/SDL/include/{SDL_opengles2_khrplatform.h → SDL3/SDL_opengles2_khrplatform.h} +0 -0
@@ -1,4 +1,4 @@
1
- /* stb_image_write - v1.16 - public domain - http://nothings.org/stb
1
+ /* stb_image_write - v1.18 - 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, int stride_in_bytes);
47
+ int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, ptrdiff_t 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,18 +52,28 @@ 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
+
55
63
  There are also five equivalent functions that use an arbitrary write function. You are
56
64
  expected to open/close your file-equivalent before and after calling these:
57
65
 
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);
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);
63
71
 
64
72
  where the callback is:
65
73
  void stbi_write_func(void *context, void *data, int size);
66
74
 
75
+ There are also four more "_strided" functions for these as well.
76
+
67
77
  You can configure it with these global variables:
68
78
  int stbi_write_tga_with_rle; // defaults to true; set to 0 to disable RLE
69
79
  int stbi_write_png_compression_level; // defaults to 8; set to higher for more compression
@@ -85,6 +95,15 @@ USAGE:
85
95
  For PNG, "stride_in_bytes" is the distance in bytes from the first byte of
86
96
  a row of pixels to the first byte of the next row of pixels.
87
97
 
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
+
88
107
  PNG creates output files with the same number of components as the input.
89
108
  The BMP format expands Y to RGB in the file format and does not
90
109
  output alpha.
@@ -141,6 +160,8 @@ CREDITS:
141
160
  github:ignotion
142
161
  Adam Schackart
143
162
  Andrew Kensler
163
+ Sanjay Nambiar
164
+ github:Nevermore1994
144
165
 
145
166
  LICENSE
146
167
 
@@ -152,6 +173,7 @@ LICENSE
152
173
  #define INCLUDE_STB_IMAGE_WRITE_H
153
174
 
154
175
  #include <stdlib.h>
176
+ #include <stddef.h>
155
177
 
156
178
  // if STB_IMAGE_WRITE_STATIC causes problems, try defining STBIWDEF to 'inline' or 'static inline'
157
179
  #ifndef STBIWDEF
@@ -173,12 +195,17 @@ STBIWDEF int stbi_write_force_png_filter;
173
195
  #endif
174
196
 
175
197
  #ifndef STBI_WRITE_NO_STDIO
176
- STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
198
+ STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, ptrdiff_t stride_in_bytes);
177
199
  STBIWDEF int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
178
200
  STBIWDEF int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);
179
201
  STBIWDEF int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data);
180
202
  STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality);
181
203
 
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
+
182
209
  #ifdef STBIW_WINDOWS_UTF8
183
210
  STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input);
184
211
  #endif
@@ -186,12 +213,17 @@ STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const w
186
213
 
187
214
  typedef void stbi_write_func(void *context, void *data, int size);
188
215
 
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);
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);
190
217
  STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
191
218
  STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
192
219
  STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data);
193
220
  STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality);
194
221
 
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
+
195
227
  STBIWDEF void stbi_flip_vertically_on_write(int flip_boolean);
196
228
 
197
229
  #endif//INCLUDE_STB_IMAGE_WRITE_H
@@ -214,6 +246,8 @@ STBIWDEF void stbi_flip_vertically_on_write(int flip_boolean);
214
246
  #include <stdarg.h>
215
247
  #include <stdlib.h>
216
248
  #include <string.h>
249
+ #include <limits.h>
250
+ #include <float.h>
217
251
  #include <math.h>
218
252
 
219
253
  #if defined(STBIW_MALLOC) && defined(STBIW_FREE) && (defined(STBIW_REALLOC) || defined(STBIW_REALLOC_SIZED))
@@ -268,22 +302,28 @@ typedef struct
268
302
  {
269
303
  stbi_write_func *func;
270
304
  void *context;
305
+ void *file_maybe;
271
306
  unsigned char buffer[64];
272
307
  int buf_used;
308
+ int ok;
273
309
  } stbi__write_context;
274
310
 
275
311
  // initialize a callback-based context
276
312
  static void stbi__start_write_callbacks(stbi__write_context *s, stbi_write_func *c, void *context)
277
313
  {
278
- s->func = c;
279
- s->context = context;
314
+ s->func = c;
315
+ s->context = context;
316
+ s->file_maybe = NULL;
317
+ s->ok = 1;
280
318
  }
281
319
 
282
320
  #ifndef STBI_WRITE_NO_STDIO
283
321
 
284
322
  static void stbi__stdio_write(void *context, void *data, int size)
285
323
  {
286
- fwrite(data,1,size,(FILE*) context);
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;
287
327
  }
288
328
 
289
329
  #if defined(_WIN32) && defined(STBIW_WINDOWS_UTF8)
@@ -332,17 +372,61 @@ static FILE *stbiw__fopen(char const *filename, char const *mode)
332
372
  static int stbi__start_write_file(stbi__write_context *s, const char *filename)
333
373
  {
334
374
  FILE *f = stbiw__fopen(filename, "wb");
335
- stbi__start_write_callbacks(s, stbi__stdio_write, (void *) f);
375
+ stbi__start_write_callbacks(s, stbi__stdio_write, s);
376
+ s->file_maybe = (void *) f;
336
377
  return f != NULL;
337
378
  }
338
379
 
339
- static void stbi__end_write_file(stbi__write_context *s)
380
+ static int stbi__end_write_file(stbi__write_context *s, int result)
340
381
  {
341
- fclose((FILE *)s->context);
382
+ if (fclose((FILE *)s->file_maybe) != 0)
383
+ s->ok = 0;
384
+ return s->ok ? result : 0;
342
385
  }
343
386
 
344
387
  #endif // !STBI_WRITE_NO_STDIO
345
388
 
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
+
346
430
  typedef unsigned int stbiw_uint32;
347
431
  typedef int stb_image_write_test[sizeof(stbiw_uint32)==4 ? 1 : -1];
348
432
 
@@ -448,12 +532,12 @@ static void stbiw__write_pixel(stbi__write_context *s, int rgb_dir, int comp, in
448
532
  stbiw__write1(s, d[comp - 1]);
449
533
  }
450
534
 
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)
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)
452
536
  {
453
537
  stbiw_uint32 zero = 0;
454
538
  int i,j, j_end;
455
539
 
456
- if (y <= 0)
540
+ if (!stbiw__validate_dimensions(x,y,comp))
457
541
  return;
458
542
 
459
543
  if (stbi__flip_vertically_on_write)
@@ -465,9 +549,12 @@ static void stbiw__write_pixels(stbi__write_context *s, int rgb_dir, int vdir, i
465
549
  j_end = y; j = 0;
466
550
  }
467
551
 
552
+ if (stride_in_bytes == 0)
553
+ stride_in_bytes = x*comp;
554
+
468
555
  for (; j != j_end; j += vdir) {
469
556
  for (i=0; i < x; ++i) {
470
- unsigned char *d = (unsigned char *) data + (j*x+i)*comp;
557
+ unsigned char *d = (unsigned char *) data + j*stride_in_bytes + i*comp;
471
558
  stbiw__write_pixel(s, rgb_dir, comp, write_alpha, expand_mono, d);
472
559
  }
473
560
  stbiw__write_flush(s);
@@ -475,7 +562,7 @@ static void stbiw__write_pixels(stbi__write_context *s, int rgb_dir, int vdir, i
475
562
  }
476
563
  }
477
564
 
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, ...)
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, ...)
479
566
  {
480
567
  if (y < 0 || x < 0) {
481
568
  return 0;
@@ -484,17 +571,17 @@ static int stbiw__outfile(stbi__write_context *s, int rgb_dir, int vdir, int x,
484
571
  va_start(v, fmt);
485
572
  stbiw__writefv(s, fmt, v);
486
573
  va_end(v);
487
- stbiw__write_pixels(s,rgb_dir,vdir,x,y,comp,data,alpha,pad, expand_mono);
574
+ stbiw__write_pixels(s,rgb_dir,vdir,x,y,comp,data,stride_in_bytes,alpha,pad, expand_mono);
488
575
  return 1;
489
576
  }
490
577
  }
491
578
 
492
- static int stbi_write_bmp_core(stbi__write_context *s, int x, int y, int comp, const void *data)
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)
493
580
  {
494
581
  if (comp != 4) {
495
582
  // write RGB bitmap
496
583
  int pad = (-x*3) & 3;
497
- return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *) data,0,pad,
584
+ return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *) data,stride_in_bytes,0,pad,
498
585
  "11 4 22 4" "4 44 22 444444",
499
586
  'B', 'M', 14+40+(x*3+pad)*y, 0,0, 14+40, // file header
500
587
  40, x,y, 1,24, 0,0,0,0,0,0); // bitmap header
@@ -502,44 +589,55 @@ static int stbi_write_bmp_core(stbi__write_context *s, int x, int y, int comp, c
502
589
  // RGBA bitmaps need a v4 header
503
590
  // use BI_BITFIELDS mode with 32bpp and alpha mask
504
591
  // (straight BI_RGB with alpha mask doesn't work in most readers)
505
- return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *)data,1,0,
592
+ return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *)data,stride_in_bytes,1,0,
506
593
  "11 4 22 4" "4 44 22 444444 4444 4 444 444 444 444",
507
594
  'B', 'M', 14+108+x*y*4, 0, 0, 14+108, // file 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
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
509
596
  }
510
597
  }
511
598
 
512
- STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data)
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)
513
600
  {
514
- stbi__write_context s = { 0 };
601
+ stbi__write_context s;
602
+ memset(&s, 0, sizeof(s));
515
603
  stbi__start_write_callbacks(&s, func, context);
516
- return stbi_write_bmp_core(&s, x, y, comp, data);
604
+ return stbi_write_bmp_core(&s, w, h, comp, data, stride_in_bytes);
605
+ }
606
+
607
+ STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data)
608
+ {
609
+ return stbi_write_bmp_strided_to_func(func, context, x, y, comp, data, 0);
517
610
  }
518
611
 
519
612
  #ifndef STBI_WRITE_NO_STDIO
520
- STBIWDEF int stbi_write_bmp(char const *filename, int x, int y, int comp, const void *data)
613
+ STBIWDEF int stbi_write_bmp_strided(char const *filename, int x, int y, int comp, const void *data, ptrdiff_t stride_in_bytes)
521
614
  {
522
- stbi__write_context s = { 0 };
615
+ stbi__write_context s;
616
+ memset(&s, 0, sizeof(s));
523
617
  if (stbi__start_write_file(&s,filename)) {
524
- int r = stbi_write_bmp_core(&s, x, y, comp, data);
525
- stbi__end_write_file(&s);
526
- return r;
618
+ int r = stbi_write_bmp_core(&s, x, y, comp, data, stride_in_bytes);
619
+ return stbi__end_write_file(&s, r);
527
620
  } else
528
621
  return 0;
529
622
  }
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
+ }
530
628
  #endif //!STBI_WRITE_NO_STDIO
531
629
 
532
- static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, void *data)
630
+ static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, void *data, ptrdiff_t stride_in_bytes)
533
631
  {
534
632
  int has_alpha = (comp == 2 || comp == 4);
535
633
  int colorbytes = has_alpha ? comp-1 : comp;
536
634
  int format = colorbytes < 2 ? 3 : 2; // 3 color channels (RGB/RGBA) = 2, 1 color channel (Y/YA) = 3
537
635
 
538
- if (y < 0 || x < 0)
636
+ if (!stbiw__validate_dimensions(x, y, comp))
539
637
  return 0;
540
638
 
541
639
  if (!stbi_write_tga_with_rle) {
542
- return stbiw__outfile(s, -1, -1, x, y, comp, 0, (void *) data, has_alpha, 0,
640
+ return stbiw__outfile(s, -1, -1, x, y, comp, 0, (void *) data, stride_in_bytes, has_alpha, 0,
543
641
  "111 221 2222 11", 0, 0, format, 0, 0, 0, 0, 0, x, y, (colorbytes + has_alpha) * 8, has_alpha * 8);
544
642
  } else {
545
643
  int i,j,k;
@@ -556,8 +654,11 @@ static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, v
556
654
  jend = -1;
557
655
  jdir = -1;
558
656
  }
657
+ if (stride_in_bytes == 0)
658
+ stride_in_bytes = x * comp;
659
+
559
660
  for (; j != jend; j += jdir) {
560
- unsigned char *row = (unsigned char *) data + j * x * comp;
661
+ unsigned char *row = (unsigned char *) data + j * stride_in_bytes;
561
662
  int len;
562
663
 
563
664
  for (i = 0; i < x; i += len) {
@@ -608,47 +709,95 @@ static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, v
608
709
  return 1;
609
710
  }
610
711
 
611
- STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data)
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)
612
713
  {
613
- stbi__write_context s = { 0 };
714
+ stbi__write_context s;
715
+ memset(&s, 0, sizeof(s));
614
716
  stbi__start_write_callbacks(&s, func, context);
615
- return stbi_write_tga_core(&s, x, y, comp, (void *) data);
717
+ return stbi_write_tga_core(&s, w, h, comp, (void *) data, stride_in_bytes);
718
+ }
719
+
720
+ STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data)
721
+ {
722
+ return stbi_write_tga_strided_to_func(func, context, x, y, comp, data, 0);
616
723
  }
617
724
 
618
725
  #ifndef STBI_WRITE_NO_STDIO
619
- STBIWDEF int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data)
726
+ STBIWDEF int stbi_write_tga_strided(char const *filename, int x, int y, int comp, const void *data, ptrdiff_t stride_in_bytes)
620
727
  {
621
- stbi__write_context s = { 0 };
728
+ stbi__write_context s;
729
+ memset(&s, 0, sizeof(s));
622
730
  if (stbi__start_write_file(&s,filename)) {
623
- int r = stbi_write_tga_core(&s, x, y, comp, (void *) data);
624
- stbi__end_write_file(&s);
625
- return r;
731
+ int r = stbi_write_tga_core(&s, x, y, comp, (void *) data, stride_in_bytes);
732
+ return stbi__end_write_file(&s, r);
626
733
  } else
627
734
  return 0;
628
735
  }
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
+ }
629
741
  #endif
630
742
 
631
743
  // *************************************************************************************************
632
744
  // Radiance RGBE HDR writer
633
745
  // by Baldur Karlsson
634
746
 
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)
747
+ static void stbiw__linear_to_rgbe(unsigned char *rgbe, float *pixel, int ncomp)
640
748
  {
641
749
  int exponent;
642
- float maxcomp = stbiw__max(linear[0], stbiw__max(linear[1], linear[2]));
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
+ }
643
769
 
644
- if (maxcomp < 1e-32f) {
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) {
645
773
  rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0;
646
774
  } else {
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);
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);
652
801
  rgbe[3] = (unsigned char)(exponent + 128);
653
802
  }
654
803
  }
@@ -673,7 +822,6 @@ static void stbiw__write_hdr_scanline(stbi__write_context *s, int width, int nco
673
822
  {
674
823
  unsigned char scanlineheader[4] = { 2, 2, 0, 0 };
675
824
  unsigned char rgbe[4];
676
- float linear[3];
677
825
  int x;
678
826
 
679
827
  scanlineheader[2] = (width&0xff00)>>8;
@@ -682,34 +830,14 @@ static void stbiw__write_hdr_scanline(stbi__write_context *s, int width, int nco
682
830
  /* skip RLE for images too small or large */
683
831
  if (width < 8 || width >= 32768) {
684
832
  for (x=0; x < width; x++) {
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);
833
+ stbiw__linear_to_rgbe(rgbe, &scanline[x*ncomp], ncomp);
696
834
  s->func(s->context, rgbe, 4);
697
835
  }
698
836
  } else {
699
837
  int c,r;
700
838
  /* encode into scratch buffer */
701
839
  for (x=0; x < width; x++) {
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);
840
+ stbiw__linear_to_rgbe(rgbe, &scanline[x*ncomp], ncomp);
713
841
  scratch[x + width*0] = rgbe[0];
714
842
  scratch[x + width*1] = rgbe[1];
715
843
  scratch[x + width*2] = rgbe[2];
@@ -758,49 +886,93 @@ static void stbiw__write_hdr_scanline(stbi__write_context *s, int width, int nco
758
886
  }
759
887
  }
760
888
 
761
- static int stbi_write_hdr_core(stbi__write_context *s, int x, int y, int comp, float *data)
889
+ static int stbiw__u32_to_string(unsigned int value, char* dest)
762
890
  {
763
- if (y <= 0 || x <= 0 || data == NULL)
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)
764
913
  return 0;
765
914
  else {
766
915
  // Each component is stored separately. Allocate scratch space for full output scanline.
767
916
  unsigned char *scratch = (unsigned char *) STBIW_MALLOC(x*4);
768
- int i, len;
917
+ int i;
769
918
  char buffer[128];
919
+ char* dst = buffer;
770
920
  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
+
771
925
  s->func(s->context, header, sizeof(header)-1);
772
926
 
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);
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;
779
938
 
780
939
  for(i=0; i < y; i++)
781
- stbiw__write_hdr_scanline(s, x, comp, scratch, data + comp*x*(stbi__flip_vertically_on_write ? y-1-i : i));
940
+ stbiw__write_hdr_scanline(s, x, comp, scratch, data + stride_in_bytes*(stbi__flip_vertically_on_write ? y-1-i : i));
782
941
  STBIW_FREE(scratch);
783
942
  return 1;
784
943
  }
785
944
  }
786
945
 
787
- STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const float *data)
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)
788
947
  {
789
- stbi__write_context s = { 0 };
948
+ stbi__write_context s;
949
+ memset(&s, 0, sizeof(s));
790
950
  stbi__start_write_callbacks(&s, func, context);
791
- return stbi_write_hdr_core(&s, x, y, comp, (float *) data);
951
+ return stbi_write_hdr_core(&s, x, y, comp, (float *) data, stride_in_bytes);
792
952
  }
793
953
 
794
- STBIWDEF int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *data)
954
+ STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const float *data)
795
955
  {
796
- stbi__write_context s = { 0 };
956
+ return stbi_write_hdr_strided_to_func(func, context, x,y, comp, data, 0);
957
+ }
958
+
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)
962
+ {
963
+ stbi__write_context s;
964
+ memset(&s, 0, sizeof(s));
797
965
  if (stbi__start_write_file(&s,filename)) {
798
- int r = stbi_write_hdr_core(&s, x, y, comp, (float *) data);
799
- stbi__end_write_file(&s);
800
- return r;
966
+ int r = stbi_write_hdr_core(&s, x, y, comp, (float *) data, stride_in_bytes);
967
+ return stbi__end_write_file(&s, r);
801
968
  } else
802
969
  return 0;
803
970
  }
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
+ }
804
976
  #endif // STBI_WRITE_NO_STDIO
805
977
 
806
978
 
@@ -817,7 +989,7 @@ STBIWDEF int stbi_write_hdr(char const *filename, int x, int y, int comp, const
817
989
 
818
990
  #define stbiw__sbneedgrow(a,n) ((a)==0 || stbiw__sbn(a)+n >= stbiw__sbm(a))
819
991
  #define stbiw__sbmaybegrow(a,n) (stbiw__sbneedgrow(a,(n)) ? stbiw__sbgrow(a,n) : 0)
820
- #define stbiw__sbgrow(a,n) stbiw__sbgrowf((void **) &(a), (n), sizeof(*(a)))
992
+ #define stbiw__sbgrow(a,n) stbiw__sbgrowf((void **) &(a), (n), sizeof(*(a)))
821
993
 
822
994
  #define stbiw__sbpush(a, v) (stbiw__sbmaybegrow(a,1), (a)[stbiw__sbn(a)++] = (v))
823
995
  #define stbiw__sbcount(a) ((a) ? stbiw__sbn(a) : 0)
@@ -825,14 +997,21 @@ STBIWDEF int stbi_write_hdr(char const *filename, int x, int y, int comp, const
825
997
 
826
998
  static void *stbiw__sbgrowf(void **arr, int increment, int itemsize)
827
999
  {
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
- }
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
836
1015
  return *arr;
837
1016
  }
838
1017
 
@@ -1089,7 +1268,7 @@ static unsigned char stbiw__paeth(int a, int b, int c)
1089
1268
  }
1090
1269
 
1091
1270
  // @OPTIMIZE: provide an option that always forces left-predict or paeth predict
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)
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)
1093
1272
  {
1094
1273
  static int mapping[] = { 0,1,2,3,4 };
1095
1274
  static int firstmap[] = { 0,1,0,5,6 };
@@ -1097,7 +1276,7 @@ static void stbiw__encode_png_line(unsigned char *pixels, int stride_bytes, int
1097
1276
  int i;
1098
1277
  int type = mymap[filter_type];
1099
1278
  unsigned char *z = pixels + stride_bytes * (stbi__flip_vertically_on_write ? height-1-y : y);
1100
- int signed_stride = stbi__flip_vertically_on_write ? -stride_bytes : stride_bytes;
1279
+ ptrdiff_t signed_stride = stbi__flip_vertically_on_write ? -stride_bytes : stride_bytes;
1101
1280
 
1102
1281
  if (type==0) {
1103
1282
  memcpy(line_buffer, z, width*n);
@@ -1125,7 +1304,7 @@ static void stbiw__encode_png_line(unsigned char *pixels, int stride_bytes, int
1125
1304
  }
1126
1305
  }
1127
1306
 
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)
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)
1129
1308
  {
1130
1309
  int force_filter = stbi_write_force_png_filter;
1131
1310
  int ctype[5] = { -1, 0, 4, 2, 6 };
@@ -1134,6 +1313,9 @@ STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, int s
1134
1313
  signed char *line_buffer;
1135
1314
  int j,zlen;
1136
1315
 
1316
+ if (!stbiw__validate_dimensions(x, y, n))
1317
+ return 0;
1318
+
1137
1319
  if (stride_bytes == 0)
1138
1320
  stride_bytes = x * n;
1139
1321
 
@@ -1141,6 +1323,9 @@ STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, int s
1141
1323
  force_filter = -1;
1142
1324
  }
1143
1325
 
1326
+ if (!stbiw__mad2sizes_valid(x,n,1)) return 0;
1327
+ if (!stbiw__mul2sizes_valid((x*n+1),y)) return 0;
1328
+
1144
1329
  filt = (unsigned char *) STBIW_MALLOC((x*n+1) * y); if (!filt) return 0;
1145
1330
  line_buffer = (signed char *) STBIW_MALLOC(x * n); if (!line_buffer) { STBIW_FREE(filt); return 0; }
1146
1331
  for (j=0; j < y; ++j) {
@@ -1212,7 +1397,7 @@ STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, int s
1212
1397
  }
1213
1398
 
1214
1399
  #ifndef STBI_WRITE_NO_STDIO
1215
- STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const void *data, int stride_bytes)
1400
+ STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const void *data, ptrdiff_t stride_bytes)
1216
1401
  {
1217
1402
  FILE *f;
1218
1403
  int len;
@@ -1228,7 +1413,7 @@ STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const
1228
1413
  }
1229
1414
  #endif
1230
1415
 
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)
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)
1232
1417
  {
1233
1418
  int len;
1234
1419
  unsigned char *png = stbi_write_png_to_mem((const unsigned char *) data, stride_bytes, x, y, comp, &len);
@@ -1250,8 +1435,9 @@ STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int x,
1250
1435
  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,
1251
1436
  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 };
1252
1437
 
1253
- static void stbiw__jpg_writeBits(stbi__write_context *s, int *bitBufP, int *bitCntP, const unsigned short *bs) {
1254
- int bitBuf = *bitBufP, bitCnt = *bitCntP;
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;
1255
1441
  bitCnt += bs[1];
1256
1442
  bitBuf |= bs[0] << (24 - bitCnt);
1257
1443
  while(bitCnt >= 8) {
@@ -1325,7 +1511,7 @@ static void stbiw__jpg_calcBits(int val, unsigned short bits[2]) {
1325
1511
  bits[0] = val & ((1<<bits[1])-1);
1326
1512
  }
1327
1513
 
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]) {
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]) {
1329
1515
  const unsigned short EOB[2] = { HTAC[0x00][0], HTAC[0x00][1] };
1330
1516
  const unsigned short M16zeroes[2] = { HTAC[0xF0][0], HTAC[0xF0][1] };
1331
1517
  int dataOff, i, j, n, diff, end0pos, x, y;
@@ -1395,7 +1581,7 @@ static int stbiw__jpg_processDU(stbi__write_context *s, int *bitBuf, int *bitCnt
1395
1581
  return DU[0];
1396
1582
  }
1397
1583
 
1398
- static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, int comp, const void* data, int quality) {
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) {
1399
1585
  // Constants that don't pollute global namespace
1400
1586
  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};
1401
1587
  static const unsigned char std_dc_luminance_values[] = {0,1,2,3,4,5,6,7,8,9,10,11};
@@ -1471,10 +1657,13 @@ static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, in
1471
1657
  float fdtbl_Y[64], fdtbl_UV[64];
1472
1658
  unsigned char YTable[64], UVTable[64];
1473
1659
 
1474
- if(!data || !width || !height || comp > 4 || comp < 1) {
1660
+ if (!data || !stbiw__validate_dimensions(width, height, comp)) {
1475
1661
  return 0;
1476
1662
  }
1477
1663
 
1664
+ if (stride_in_bytes == 0)
1665
+ stride_in_bytes = comp*width;
1666
+
1478
1667
  quality = quality ? quality : 90;
1479
1668
  subsample = quality <= 90 ? 1 : 0;
1480
1669
  quality = quality < 1 ? 1 : quality > 100 ? 100 : quality;
@@ -1523,7 +1712,8 @@ static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, in
1523
1712
  {
1524
1713
  static const unsigned short fillBits[] = {0x7F, 7};
1525
1714
  int DCY=0, DCU=0, DCV=0;
1526
- int bitBuf=0, bitCnt=0;
1715
+ unsigned int bitBuf=0;
1716
+ int bitCnt=0;
1527
1717
  // comp == 2 is grey+alpha (alpha is ignored)
1528
1718
  int ofsG = comp > 2 ? 1 : 0, ofsB = comp > 2 ? 2 : 0;
1529
1719
  const unsigned char *dataR = (const unsigned char *)data;
@@ -1537,10 +1727,10 @@ static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, in
1537
1727
  for(row = y, pos = 0; row < y+16; ++row) {
1538
1728
  // row >= height => use last input row
1539
1729
  int clamped_row = (row < height) ? row : height - 1;
1540
- int base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*width*comp;
1730
+ ptrdiff_t base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*stride_in_bytes;
1541
1731
  for(col = x; col < x+16; ++col, ++pos) {
1542
1732
  // if col >= width => use pixel from last input column
1543
- int p = base_p + ((col < width) ? col : (width-1))*comp;
1733
+ ptrdiff_t p = base_p + ((col < width) ? col : (width-1))*comp;
1544
1734
  float r = dataR[p], g = dataG[p], b = dataB[p];
1545
1735
  Y[pos]= +0.29900f*r + 0.58700f*g + 0.11400f*b - 128;
1546
1736
  U[pos]= -0.16874f*r - 0.33126f*g + 0.50000f*b;
@@ -1575,10 +1765,10 @@ static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, in
1575
1765
  for(row = y, pos = 0; row < y+8; ++row) {
1576
1766
  // row >= height => use last input row
1577
1767
  int clamped_row = (row < height) ? row : height - 1;
1578
- int base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*width*comp;
1768
+ ptrdiff_t base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*stride_in_bytes;
1579
1769
  for(col = x; col < x+8; ++col, ++pos) {
1580
1770
  // if col >= width => use pixel from last input column
1581
- int p = base_p + ((col < width) ? col : (width-1))*comp;
1771
+ ptrdiff_t p = base_p + ((col < width) ? col : (width-1))*comp;
1582
1772
  float r = dataR[p], g = dataG[p], b = dataB[p];
1583
1773
  Y[pos]= +0.29900f*r + 0.58700f*g + 0.11400f*b - 128;
1584
1774
  U[pos]= -0.16874f*r - 0.33126f*g + 0.50000f*b;
@@ -1604,25 +1794,35 @@ static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, in
1604
1794
  return 1;
1605
1795
  }
1606
1796
 
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)
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)
1608
1798
  {
1609
- stbi__write_context s = { 0 };
1799
+ stbi__write_context s;
1800
+ memset(&s, 0, sizeof(s));
1610
1801
  stbi__start_write_callbacks(&s, func, context);
1611
- return stbi_write_jpg_core(&s, x, y, comp, (void *) data, quality);
1802
+ return stbi_write_jpg_core(&s, x, y, comp, (void *) data, quality, stride_in_bytes);
1612
1803
  }
1613
1804
 
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
+ }
1614
1809
 
1615
1810
  #ifndef STBI_WRITE_NO_STDIO
1616
- STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality)
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)
1617
1812
  {
1618
- stbi__write_context s = { 0 };
1813
+ stbi__write_context s;
1814
+ memset(&s, 0, sizeof(s));
1619
1815
  if (stbi__start_write_file(&s,filename)) {
1620
- int r = stbi_write_jpg_core(&s, x, y, comp, data, quality);
1621
- stbi__end_write_file(&s);
1622
- return r;
1816
+ int r = stbi_write_jpg_core(&s, x, y, comp, data, quality, stride_in_bytes);
1817
+ return stbi__end_write_file(&s, r);
1623
1818
  } else
1624
1819
  return 0;
1625
1820
  }
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
+ }
1626
1826
  #endif
1627
1827
 
1628
1828
  #endif // STB_IMAGE_WRITE_IMPLEMENTATION