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,6 +1,21 @@
1
- /* stb_image - v2.28 - public domain image loader - http://nothings.org/stb
1
+ /* stb_image - v2.32 - public domain image loader - http://nothings.org/stb
2
2
  no warranty implied; use at your own risk
3
3
 
4
+ == SECURITY NOTE ==
5
+
6
+ If security is a priority, consider using Wuffs instead:
7
+ https://github.com/google/wuffs-mirror-release-c
8
+ v0.3 is the current stable version, but v0.4 includes an stb_image-compatible
9
+ API using #define WUFFS_CONFIG__ENABLE_DROP_IN_REPLACEMENT__STB
10
+
11
+ Limitations:
12
+ - Wuffs is not public domain, but is available under MIT and Apache licenses.
13
+ - Wuffs supports a different set of file formats.
14
+ - Wuffs source file 1.8 MB (v0.3) or 3.5 MB (v0.4 as of 2026-11-01),
15
+ whereas stb_image is about 0.3 MB.
16
+
17
+ == stb_image ==
18
+
4
19
  Do this:
5
20
  #define STB_IMAGE_IMPLEMENTATION
6
21
  before you include this file in *one* C or C++ file to create the implementation.
@@ -15,6 +30,8 @@
15
30
  You can #define STBI_ASSERT(x) before the #include to avoid using assert.h.
16
31
  And #define STBI_MALLOC, STBI_REALLOC, and STBI_FREE to avoid using malloc,realloc,free
17
32
 
33
+ STBI_REALLOC_SIZED is an alternative to STBI_REALLOC that is also passed the previously
34
+ alocated size, which can be helpful in certain constrained environments.
18
35
 
19
36
  QUICK NOTES:
20
37
  Primarily of interest to game developers and other people who can
@@ -29,7 +46,6 @@
29
46
 
30
47
  GIF (*comp always reports as 4-channel)
31
48
  HDR (radiance rgbE format)
32
- PIC (Softimage PIC)
33
49
  PNM (PPM and PGM binary only)
34
50
 
35
51
  Animated GIF still needs a proper API, but here's one way to do it:
@@ -48,29 +64,15 @@ LICENSE
48
64
 
49
65
  RECENT REVISION HISTORY:
50
66
 
67
+ 2.32 (2026-11-12) fix req_comp regression; other fixes
68
+ 2.31 (2026-11-08) many bugfixes and etc.
69
+ 2.30 (2024-05-31) avoid erroneous gcc warning
70
+ 2.29 (2023-05-xx) optimizations
51
71
  2.28 (2023-01-29) many error fixes, security errors, just tons of stuff
52
72
  2.27 (2021-07-11) document stbi_info better, 16-bit PNM support, bug fixes
53
73
  2.26 (2020-07-13) many minor fixes
54
74
  2.25 (2020-02-02) fix warnings
55
75
  2.24 (2020-02-02) fix warnings; thread-local failure_reason and flip_vertically
56
- 2.23 (2019-08-11) fix clang static analysis warning
57
- 2.22 (2019-03-04) gif fixes, fix warnings
58
- 2.21 (2019-02-25) fix typo in comment
59
- 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs
60
- 2.19 (2018-02-11) fix warning
61
- 2.18 (2018-01-30) fix warnings
62
- 2.17 (2018-01-29) bugfix, 1-bit BMP, 16-bitness query, fix warnings
63
- 2.16 (2017-07-23) all functions have 16-bit variants; optimizations; bugfixes
64
- 2.15 (2017-03-18) fix png-1,2,4; all Imagenet JPGs; no runtime SSE detection on GCC
65
- 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs
66
- 2.13 (2016-12-04) experimental 16-bit API, only for PNG so far; fixes
67
- 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes
68
- 2.11 (2016-04-02) 16-bit PNGS; enable SSE2 in non-gcc x64
69
- RGB-format JPEG; remove white matting in PSD;
70
- allocate large structures on the stack;
71
- correct channel count for PNG & BMP
72
- 2.10 (2016-01-22) avoid warning introduced in 2.09
73
- 2.09 (2016-01-16) 16-bit TGA; comments in PNM files; STBI_REALLOC_SIZED
74
76
 
75
77
  See end of file for full revision history.
76
78
 
@@ -92,7 +94,7 @@ RECENT REVISION HISTORY:
92
94
  Optimizations & bugfixes Mikhail Morozov (1-bit BMP)
93
95
  Fabian "ryg" Giesen Anael Seghezzi (is-16-bit query)
94
96
  Arseny Kapoulkine Simon Breuss (16-bit PNM)
95
- John-Mark Allen
97
+ John-Mark Allen hikari_no_yume (stbi_info2, is_iphone_png)
96
98
  Carmelo J Fdez-Aguera
97
99
 
98
100
  Bug & warning fixes
@@ -188,6 +190,9 @@ RECENT REVISION HISTORY:
188
190
  // // returns ok=1 and sets x, y, n if image is a supported format,
189
191
  // // 0 otherwise.
190
192
  //
193
+ // The stbi_info2 family provides the same information in struct format, plus
194
+ // some additional flags.
195
+ //
191
196
  // Note that stb_image pervasively uses ints in its public API for sizes,
192
197
  // including sizes of memory buffers. This is now part of the API and thus
193
198
  // hard to change without causing breakage. As a result, the various image
@@ -272,6 +277,11 @@ RECENT REVISION HISTORY:
272
277
  // you have issues compiling it, you can disable it entirely by
273
278
  // defining STBI_NO_SIMD.
274
279
  //
280
+ // On 32-bit MinGW, SSE2 support is disabled by default. You can allow it
281
+ // by defining STBI_MINGW_ENABLE_SSE2, but this comes with caveats due to
282
+ // compiler ABI issues. Search for STBI_MINGW_ENABLE_SSE2 further down in this
283
+ // file for details.
284
+ //
275
285
  // ===========================================================================
276
286
  //
277
287
  // HDR image support (disable by defining STBI_NO_HDR)
@@ -307,24 +317,41 @@ RECENT REVISION HISTORY:
307
317
  //
308
318
  // stbi_is_hdr(char *filename);
309
319
  //
320
+ // If you also want to query other image details at the same time:
321
+ //
322
+ // stbi_info_res r = stbi_info2(filename);
323
+ // if (r.type != STBI_type_unknown && r.is_hdr) {
324
+ // // ...
325
+ // }
326
+ //
310
327
  // ===========================================================================
311
328
  //
312
329
  // iPhone PNG support:
313
330
  //
314
- // We optionally support converting iPhone-formatted PNGs (which store
315
- // premultiplied BGRA) back to RGB, even though they're internally encoded
316
- // differently. To enable this conversion, call
317
- // stbi_convert_iphone_png_to_rgb(1).
331
+ // iPhone-formatted PNGs, also known as CgBI PNGs, are supported. Unlike
332
+ // standard PNGs, these store colors in BGR/BGRA order with premultiplied alpha.
333
+ //
334
+ // By default, stb_image will load the colors in the same format they were
335
+ // stored in. If you need to know which kind of PNG was loaded, and thus the
336
+ // color format, call one of the stbi_info2 functions and check is_iphone_png.
337
+ //
338
+ // Alternatively, if you want stb_image to convert the color order to RGB/RGBA,
339
+ // call stbi_convert_iphone_png_to_rgb(1).
318
340
  //
319
- // Call stbi_set_unpremultiply_on_load(1) as well to force a divide per
320
- // pixel to remove any premultiplied alpha *only* if the image file explicitly
321
- // says there's premultiplied data (currently only happens in iPhone images,
322
- // and only if iPhone convert-to-rgb processing is on).
341
+ // Call stbi_set_unpremultiply_on_load(1) as well to force a divide per pixel
342
+ // pixel to remove any premultiplied alpha. Note that this currently only works
343
+ // when color order conversion is also in use.
323
344
  //
324
345
  // ===========================================================================
325
346
  //
326
347
  // ADDITIONAL CONFIGURATION
327
348
  //
349
+ // - You can #define STB_IMAGE_STATIC to cause all STB image functions to
350
+ // get declared with static linkage. This is useful when you want to
351
+ // write an image loader wrapper without the STB functions leaking into
352
+ // the global namespace, or when a project contains (for whatever reason)
353
+ // multiple versions of stb_image.
354
+ //
328
355
  // - You can suppress implementation of any of the decoders to reduce
329
356
  // your code footprint by #defining one or more of the following
330
357
  // symbols before creating the implementation.
@@ -336,7 +363,6 @@ RECENT REVISION HISTORY:
336
363
  // STBI_NO_TGA
337
364
  // STBI_NO_GIF
338
365
  // STBI_NO_HDR
339
- // STBI_NO_PIC
340
366
  // STBI_NO_PNM (.ppm and .pgm)
341
367
  //
342
368
  // - You can request *only* certain decoders and suppress all other ones
@@ -350,11 +376,13 @@ RECENT REVISION HISTORY:
350
376
  // STBI_ONLY_TGA
351
377
  // STBI_ONLY_GIF
352
378
  // STBI_ONLY_HDR
353
- // STBI_ONLY_PIC
354
379
  // STBI_ONLY_PNM (.ppm and .pgm)
355
380
  //
356
- // - If you use STBI_NO_PNG (or _ONLY_ without PNG), and you still
357
- // want the zlib decoder to be available, #define STBI_SUPPORT_ZLIB
381
+ // - Setting STBI_NO_LINEAR will remove the stbi_loadf family of functions
382
+ // that returns pixel data as floats.
383
+ //
384
+ // - If you use STBI_NO_PNG (or _ONLY_ without PNG), and you still
385
+ // want the zlib decoder to be available, #define STBI_SUPPORT_ZLIB
358
386
  //
359
387
  // - If you define STBI_MAX_DIMENSIONS, stb_image will reject images greater
360
388
  // than that size (in either width or height) without further processing.
@@ -381,6 +409,20 @@ enum
381
409
  STBI_rgb_alpha = 4
382
410
  };
383
411
 
412
+ enum
413
+ {
414
+ STBI_type_unsupported = 0, // image type
415
+
416
+ STBI_type_jpeg,
417
+ STBI_type_png,
418
+ STBI_type_gif,
419
+ STBI_type_bmp,
420
+ STBI_type_psd,
421
+ STBI_type_pnm,
422
+ STBI_type_hdr,
423
+ STBI_type_tga,
424
+ };
425
+
384
426
  #include <stdlib.h>
385
427
  typedef unsigned char stbi_uc;
386
428
  typedef unsigned short stbi_us;
@@ -488,7 +530,29 @@ STBIDEF const char *stbi_failure_reason (void);
488
530
  // free the loaded image -- this is just free()
489
531
  STBIDEF void stbi_image_free (void *retval_from_stbi_load);
490
532
 
491
- // get image dimensions & components without fully decoding
533
+ // get image dimensions, components, etc without fully decoding (new api)
534
+ typedef struct
535
+ {
536
+ int type; // STBI_type_*; if this is STBI_type_unsupported (0), the other fields are meaningless
537
+
538
+ int x;
539
+ int y;
540
+ int comp;
541
+
542
+ int is_16_bit;
543
+ int is_hdr;
544
+ int is_iphone_png;
545
+ } stbi_info_res;
546
+
547
+ STBIDEF stbi_info_res stbi_info2_from_memory(stbi_uc const *buffer, int len);
548
+ STBIDEF stbi_info_res stbi_info2_from_callbacks(stbi_io_callbacks const *clbk, void *user);
549
+
550
+ #ifndef STBI_NO_STDIO
551
+ STBIDEF stbi_info_res stbi_info2 (char const *filename);
552
+ STBIDEF stbi_info_res stbi_info2_from_file(FILE *f);
553
+ #endif
554
+
555
+ // get image dimensions & components without fully decoding (old api)
492
556
  STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
493
557
  STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp);
494
558
  STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len);
@@ -546,8 +610,7 @@ STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const ch
546
610
 
547
611
  #if defined(STBI_ONLY_JPEG) || defined(STBI_ONLY_PNG) || defined(STBI_ONLY_BMP) \
548
612
  || defined(STBI_ONLY_TGA) || defined(STBI_ONLY_GIF) || defined(STBI_ONLY_PSD) \
549
- || defined(STBI_ONLY_HDR) || defined(STBI_ONLY_PIC) || defined(STBI_ONLY_PNM) \
550
- || defined(STBI_ONLY_ZLIB)
613
+ || defined(STBI_ONLY_HDR) || defined(STBI_ONLY_PNM) || defined(STBI_ONLY_ZLIB)
551
614
  #ifndef STBI_ONLY_JPEG
552
615
  #define STBI_NO_JPEG
553
616
  #endif
@@ -569,9 +632,6 @@ STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const ch
569
632
  #ifndef STBI_ONLY_HDR
570
633
  #define STBI_NO_HDR
571
634
  #endif
572
- #ifndef STBI_ONLY_PIC
573
- #define STBI_NO_PIC
574
- #endif
575
635
  #ifndef STBI_ONLY_PNM
576
636
  #define STBI_NO_PNM
577
637
  #endif
@@ -659,10 +719,6 @@ typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1];
659
719
  #endif
660
720
 
661
721
  #ifdef _MSC_VER
662
- #define STBI_HAS_LROTL
663
- #endif
664
-
665
- #ifdef STBI_HAS_LROTL
666
722
  #define stbi_lrot(x,y) _lrotl(x,y)
667
723
  #else
668
724
  #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (-(y) & 31)))
@@ -687,7 +743,7 @@ typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1];
687
743
  #endif
688
744
 
689
745
  // x86/x64 detection
690
- #if defined(__x86_64__) || defined(_M_X64)
746
+ #if (defined(__x86_64__) || defined(_M_X64)) && !defined(_M_ARM64EC)
691
747
  #define STBI__X64_TARGET
692
748
  #elif defined(__i386) || defined(_M_IX86)
693
749
  #define STBI__X86_TARGET
@@ -720,7 +776,7 @@ typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1];
720
776
  #endif
721
777
 
722
778
  #if !defined(STBI_NO_SIMD) && (defined(STBI__X86_TARGET) || defined(STBI__X64_TARGET))
723
- #define STBI_SSE2
779
+ #define STBI__SSE2
724
780
  #include <emmintrin.h>
725
781
 
726
782
  #ifdef _MSC_VER
@@ -746,9 +802,9 @@ static int stbi__cpuid3(void)
746
802
  }
747
803
  #endif
748
804
 
749
- #define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name
805
+ #define STBI__SIMD_ALIGN(type, name) __declspec(align(16)) type name
750
806
 
751
- #if !defined(STBI_NO_JPEG) && defined(STBI_SSE2)
807
+ #if !defined(STBI_NO_JPEG) && defined(STBI__SSE2)
752
808
  static int stbi__sse2_available(void)
753
809
  {
754
810
  int info3 = stbi__cpuid3();
@@ -757,9 +813,9 @@ static int stbi__sse2_available(void)
757
813
  #endif
758
814
 
759
815
  #else // assume GCC-style if not VC++
760
- #define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
816
+ #define STBI__SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
761
817
 
762
- #if !defined(STBI_NO_JPEG) && defined(STBI_SSE2)
818
+ #if !defined(STBI_NO_JPEG) && defined(STBI__SSE2)
763
819
  static int stbi__sse2_available(void)
764
820
  {
765
821
  // If we're even attempting to compile this on GCC/Clang, that means
@@ -772,6 +828,11 @@ static int stbi__sse2_available(void)
772
828
  #endif
773
829
  #endif
774
830
 
831
+ // 64-bit ARM targets always support AdvSIMD
832
+ #if (defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)) && !defined(STBI_NEON)
833
+ #define STBI_NEON
834
+ #endif
835
+
775
836
  // ARM NEON
776
837
  #if defined(STBI_NO_SIMD) && defined(STBI_NEON)
777
838
  #undef STBI_NEON
@@ -780,14 +841,14 @@ static int stbi__sse2_available(void)
780
841
  #ifdef STBI_NEON
781
842
  #include <arm_neon.h>
782
843
  #ifdef _MSC_VER
783
- #define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name
844
+ #define STBI__SIMD_ALIGN(type, name) __declspec(align(16)) type name
784
845
  #else
785
- #define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
846
+ #define STBI__SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
786
847
  #endif
787
848
  #endif
788
849
 
789
- #ifndef STBI_SIMD_ALIGN
790
- #define STBI_SIMD_ALIGN(type, name) type name
850
+ #ifndef STBI__SIMD_ALIGN
851
+ #define STBI__SIMD_ALIGN(type, name) type name
791
852
  #endif
792
853
 
793
854
  #ifndef STBI_MAX_DIMENSIONS
@@ -843,6 +904,14 @@ static void stbi__start_callbacks(stbi__context *s, stbi_io_callbacks *c, void *
843
904
  s->img_buffer_original_end = s->img_buffer_end;
844
905
  }
845
906
 
907
+ static void stbi__stop(stbi__context *s)
908
+ {
909
+ if (s->img_buffer != s->img_buffer_end && s->io.read && s->io.skip) {
910
+ // there are remaining characters in the IO buffer, skip back to the actual position we finished at
911
+ (s->io.skip)(s->io_user_data, -(int) (s->img_buffer_end - s->img_buffer));
912
+ }
913
+ }
914
+
846
915
  #ifndef STBI_NO_STDIO
847
916
 
848
917
  static int stbi__stdio_read(void *user, char *data, int size)
@@ -877,8 +946,6 @@ static void stbi__start_file(stbi__context *s, FILE *f)
877
946
  stbi__start_callbacks(s, &stbi__stdio_callbacks, (void *) f);
878
947
  }
879
948
 
880
- //static void stop_file(stbi__context *s) { }
881
-
882
949
  #endif // !STBI_NO_STDIO
883
950
 
884
951
  static void stbi__rewind(stbi__context *s)
@@ -906,59 +973,53 @@ typedef struct
906
973
  #ifndef STBI_NO_JPEG
907
974
  static int stbi__jpeg_test(stbi__context *s);
908
975
  static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
909
- static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp);
976
+ static int stbi__jpeg_info(stbi__context *s, stbi_info_res *res);
910
977
  #endif
911
978
 
912
979
  #ifndef STBI_NO_PNG
913
980
  static int stbi__png_test(stbi__context *s);
914
981
  static void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
915
- static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp);
916
- static int stbi__png_is16(stbi__context *s);
982
+ static int stbi__png_info(stbi__context *s, stbi_info_res *res);
917
983
  #endif
918
984
 
919
985
  #ifndef STBI_NO_BMP
920
986
  static int stbi__bmp_test(stbi__context *s);
921
987
  static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
922
- static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp);
988
+ static int stbi__bmp_info(stbi__context *s, stbi_info_res *res);
923
989
  #endif
924
990
 
925
991
  #ifndef STBI_NO_TGA
926
992
  static int stbi__tga_test(stbi__context *s);
927
993
  static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
928
- static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp);
994
+ static int stbi__tga_info(stbi__context *s, stbi_info_res *res);
929
995
  #endif
930
996
 
931
997
  #ifndef STBI_NO_PSD
932
998
  static int stbi__psd_test(stbi__context *s);
933
999
  static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc);
934
- static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp);
935
- static int stbi__psd_is16(stbi__context *s);
1000
+ static int stbi__psd_info(stbi__context *s, stbi_info_res *res);
936
1001
  #endif
937
1002
 
938
1003
  #ifndef STBI_NO_HDR
939
1004
  static int stbi__hdr_test(stbi__context *s);
940
1005
  static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
941
- static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp);
1006
+ static int stbi__hdr_info(stbi__context *s, stbi_info_res *res);
942
1007
  #endif
943
1008
 
944
- #ifndef STBI_NO_PIC
945
- static int stbi__pic_test(stbi__context *s);
946
- static void *stbi__pic_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
947
- static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp);
948
- #endif
1009
+ // PIC support removed Oct 2026. If you really need to load a PIC file, use either an old version of stb_image
1010
+ // or convert with a tool like ImageMagick.
949
1011
 
950
1012
  #ifndef STBI_NO_GIF
951
1013
  static int stbi__gif_test(stbi__context *s);
952
1014
  static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
953
1015
  static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp);
954
- static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp);
1016
+ static int stbi__gif_info(stbi__context *s, stbi_info_res *res);
955
1017
  #endif
956
1018
 
957
1019
  #ifndef STBI_NO_PNM
958
1020
  static int stbi__pnm_test(stbi__context *s);
959
1021
  static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
960
- static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp);
961
- static int stbi__pnm_is16(stbi__context *s);
1022
+ static int stbi__pnm_info(stbi__context *s, stbi_info_res *res);
962
1023
  #endif
963
1024
 
964
1025
  static
@@ -1017,7 +1078,7 @@ static int stbi__mul2sizes_valid(int a, int b)
1017
1078
  return a <= INT_MAX/b;
1018
1079
  }
1019
1080
 
1020
- #if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR)
1081
+ #if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR) || !defined(STBI_NO_GIF)
1021
1082
  // returns 1 if "a*b + add" has no negative terms/factors and doesn't overflow
1022
1083
  static int stbi__mad2sizes_valid(int a, int b, int add)
1023
1084
  {
@@ -1033,15 +1094,13 @@ static int stbi__mad3sizes_valid(int a, int b, int c, int add)
1033
1094
  }
1034
1095
 
1035
1096
  // returns 1 if "a*b*c*d + add" has no negative terms/factors and doesn't overflow
1036
- #if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM)
1037
1097
  static int stbi__mad4sizes_valid(int a, int b, int c, int d, int add)
1038
1098
  {
1039
1099
  return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) &&
1040
1100
  stbi__mul2sizes_valid(a*b*c, d) && stbi__addsizes_valid(a*b*c*d, add);
1041
1101
  }
1042
- #endif
1043
1102
 
1044
- #if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR)
1103
+ #if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR) || !defined(STBI_NO_GIF)
1045
1104
  // mallocs with size overflow checking
1046
1105
  static void *stbi__malloc_mad2(int a, int b, int add)
1047
1106
  {
@@ -1056,7 +1115,7 @@ static void *stbi__malloc_mad3(int a, int b, int c, int add)
1056
1115
  return stbi__malloc(a*b*c + add);
1057
1116
  }
1058
1117
 
1059
- #if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM)
1118
+ #if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM) || !defined(STBI_NO_PNG) || !defined(STBI_NO_PSD)
1060
1119
  static void *stbi__malloc_mad4(int a, int b, int c, int d, int add)
1061
1120
  {
1062
1121
  if (!stbi__mad4sizes_valid(a, b, c, d, add)) return NULL;
@@ -1064,6 +1123,7 @@ static void *stbi__malloc_mad4(int a, int b, int c, int d, int add)
1064
1123
  }
1065
1124
  #endif
1066
1125
 
1126
+ #if !defined(STBI_NO_JPEG) || !defined(STBI_NO_GIF)
1067
1127
  // returns 1 if the sum of two signed ints is valid (between -2^31 and 2^31-1 inclusive), 0 on overflow.
1068
1128
  static int stbi__addints_valid(int a, int b)
1069
1129
  {
@@ -1071,15 +1131,18 @@ static int stbi__addints_valid(int a, int b)
1071
1131
  if (a < 0 && b < 0) return a >= INT_MIN - b; // same as a + b >= INT_MIN; INT_MIN - b cannot overflow since b < 0.
1072
1132
  return a <= INT_MAX - b;
1073
1133
  }
1134
+ #endif
1074
1135
 
1075
- // returns 1 if the product of two signed shorts is valid, 0 on overflow.
1076
- static int stbi__mul2shorts_valid(short a, short b)
1136
+ #if !defined(STBI_NO_JPEG)
1137
+ // returns 1 if the product of two ints fits in a signed short, 0 on overflow.
1138
+ static int stbi__mul2shorts_valid(int a, int b)
1077
1139
  {
1078
1140
  if (b == 0 || b == -1) return 1; // multiplication by 0 is always 0; check for -1 so SHRT_MIN/b doesn't overflow
1079
1141
  if ((a >= 0) == (b >= 0)) return a <= SHRT_MAX/b; // product is positive, so similar to mul2sizes_valid
1080
1142
  if (b < 0) return a <= SHRT_MIN / b; // same as a * b >= SHRT_MIN
1081
1143
  return a >= SHRT_MIN / b;
1082
1144
  }
1145
+ #endif
1083
1146
 
1084
1147
  // stbi__err - error
1085
1148
  // stbi__errpf - error returning pointer to float
@@ -1139,6 +1202,8 @@ static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int re
1139
1202
  ri->channel_order = STBI_ORDER_RGB; // all current input & output are this, but this is here so we can add BGR order
1140
1203
  ri->num_channels = 0;
1141
1204
 
1205
+ if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Invalid value for required components");
1206
+
1142
1207
  // test the formats with a very explicit header first (at least a FOURCC
1143
1208
  // or distinctive magic number first)
1144
1209
  #ifndef STBI_NO_PNG
@@ -1155,9 +1220,6 @@ static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int re
1155
1220
  #else
1156
1221
  STBI_NOTUSED(bpc);
1157
1222
  #endif
1158
- #ifndef STBI_NO_PIC
1159
- if (stbi__pic_test(s)) return stbi__pic_load(s,x,y,comp,req_comp, ri);
1160
- #endif
1161
1223
 
1162
1224
  // then the formats that can end up attempting to load with just 1 or 2
1163
1225
  // bytes matching expectations; these are prone to false positives, so
@@ -1187,12 +1249,17 @@ static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int re
1187
1249
 
1188
1250
  static stbi_uc *stbi__convert_16_to_8(stbi__uint16 *orig, int w, int h, int channels)
1189
1251
  {
1190
- int i;
1191
- int img_len = w * h * channels;
1252
+ int i, img_len;
1192
1253
  stbi_uc *reduced;
1193
1254
 
1255
+ if (!stbi__mad3sizes_valid(w, h, channels, 0)) return stbi__errpuc("too large", "Image too large to convert");
1256
+
1257
+ img_len = w * h * channels;
1194
1258
  reduced = (stbi_uc *) stbi__malloc(img_len);
1195
- if (reduced == NULL) return stbi__errpuc("outofmem", "Out of memory");
1259
+ if (reduced == NULL) {
1260
+ STBI_FREE(orig);
1261
+ return stbi__errpuc("outofmem", "Out of memory");
1262
+ }
1196
1263
 
1197
1264
  for (i = 0; i < img_len; ++i)
1198
1265
  reduced[i] = (stbi_uc)((orig[i] >> 8) & 0xFF); // top half of each byte is sufficient approx of 16->8 bit scaling
@@ -1203,12 +1270,17 @@ static stbi_uc *stbi__convert_16_to_8(stbi__uint16 *orig, int w, int h, int chan
1203
1270
 
1204
1271
  static stbi__uint16 *stbi__convert_8_to_16(stbi_uc *orig, int w, int h, int channels)
1205
1272
  {
1206
- int i;
1207
- int img_len = w * h * channels;
1273
+ int i, img_len;
1208
1274
  stbi__uint16 *enlarged;
1209
1275
 
1276
+ if (!stbi__mad4sizes_valid(w, h, channels, 2, 0)) return (stbi__uint16 *) stbi__errpuc("too large", "Image too large to convert");
1277
+
1278
+ img_len = w * h * channels;
1210
1279
  enlarged = (stbi__uint16 *) stbi__malloc(img_len*2);
1211
- if (enlarged == NULL) return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory");
1280
+ if (enlarged == NULL) {
1281
+ STBI_FREE(orig);
1282
+ return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory");
1283
+ }
1212
1284
 
1213
1285
  for (i = 0; i < img_len; ++i)
1214
1286
  enlarged[i] = (stbi__uint16)((orig[i] << 8) + orig[i]); // replicate to high and low byte, maps 0->0, 255->0xffff
@@ -1241,20 +1313,6 @@ static void stbi__vertical_flip(void *image, int w, int h, int bytes_per_pixel)
1241
1313
  }
1242
1314
  }
1243
1315
 
1244
- #ifndef STBI_NO_GIF
1245
- static void stbi__vertical_flip_slices(void *image, int w, int h, int z, int bytes_per_pixel)
1246
- {
1247
- int slice;
1248
- int slice_size = w * h * bytes_per_pixel;
1249
-
1250
- stbi_uc *bytes = (stbi_uc *)image;
1251
- for (slice = 0; slice < z; ++slice) {
1252
- stbi__vertical_flip(bytes, w, h, bytes_per_pixel);
1253
- bytes += slice_size;
1254
- }
1255
- }
1256
- #endif
1257
-
1258
1316
  static unsigned char *stbi__load_and_postprocess_8bit(stbi__context *s, int *x, int *y, int *comp, int req_comp)
1259
1317
  {
1260
1318
  stbi__result_info ri;
@@ -1377,10 +1435,7 @@ STBIDEF stbi_uc *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req
1377
1435
  stbi__context s;
1378
1436
  stbi__start_file(&s,f);
1379
1437
  result = stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);
1380
- if (result) {
1381
- // need to 'unget' all the characters in the IO buffer
1382
- fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR);
1383
- }
1438
+ stbi__stop(&s);
1384
1439
  return result;
1385
1440
  }
1386
1441
 
@@ -1390,10 +1445,7 @@ STBIDEF stbi__uint16 *stbi_load_from_file_16(FILE *f, int *x, int *y, int *comp,
1390
1445
  stbi__context s;
1391
1446
  stbi__start_file(&s,f);
1392
1447
  result = stbi__load_and_postprocess_16bit(&s,x,y,comp,req_comp);
1393
- if (result) {
1394
- // need to 'unget' all the characters in the IO buffer
1395
- fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR);
1396
- }
1448
+ stbi__stop(&s);
1397
1449
  return result;
1398
1450
  }
1399
1451
 
@@ -1419,9 +1471,12 @@ STBIDEF stbi_us *stbi_load_16_from_memory(stbi_uc const *buffer, int len, int *x
1419
1471
 
1420
1472
  STBIDEF stbi_us *stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels)
1421
1473
  {
1474
+ stbi_us *result;
1422
1475
  stbi__context s;
1423
1476
  stbi__start_callbacks(&s, (stbi_io_callbacks *)clbk, user);
1424
- return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels);
1477
+ result = stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels);
1478
+ stbi__stop(&s);
1479
+ return result;
1425
1480
  }
1426
1481
 
1427
1482
  STBIDEF stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
@@ -1433,24 +1488,24 @@ STBIDEF stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, i
1433
1488
 
1434
1489
  STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
1435
1490
  {
1491
+ stbi_uc *result;
1436
1492
  stbi__context s;
1437
1493
  stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
1438
- return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);
1494
+ result = stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);
1495
+ stbi__stop(&s);
1496
+ return result;
1439
1497
  }
1440
1498
 
1441
1499
  #ifndef STBI_NO_GIF
1442
1500
  STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp)
1443
1501
  {
1444
- unsigned char *result;
1445
1502
  stbi__context s;
1446
1503
  stbi__start_mem(&s,buffer,len);
1447
1504
 
1448
- result = (unsigned char*) stbi__load_gif_main(&s, delays, x, y, z, comp, req_comp);
1449
- if (stbi__vertically_flip_on_load) {
1450
- stbi__vertical_flip_slices( result, *x, *y, *z, *comp );
1451
- }
1505
+ if (req_comp < 0 || req_comp > 4)
1506
+ return stbi__errpuc("bad req_comp", "Invalid number of required components");
1452
1507
 
1453
- return result;
1508
+ return (unsigned char*) stbi__load_gif_main(&s, delays, x, y, z, comp, req_comp);
1454
1509
  }
1455
1510
  #endif
1456
1511
 
@@ -1482,9 +1537,12 @@ STBIDEF float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, in
1482
1537
 
1483
1538
  STBIDEF float *stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
1484
1539
  {
1540
+ float *result;
1485
1541
  stbi__context s;
1486
1542
  stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
1487
- return stbi__loadf_main(&s,x,y,comp,req_comp);
1543
+ result = stbi__loadf_main(&s,x,y,comp,req_comp);
1544
+ stbi__stop(&s);
1545
+ return result;
1488
1546
  }
1489
1547
 
1490
1548
  #ifndef STBI_NO_STDIO
@@ -1500,9 +1558,12 @@ STBIDEF float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int r
1500
1558
 
1501
1559
  STBIDEF float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
1502
1560
  {
1561
+ float *result;
1503
1562
  stbi__context s;
1504
1563
  stbi__start_file(&s,f);
1505
- return stbi__loadf_main(&s,x,y,comp,req_comp);
1564
+ result = stbi__loadf_main(&s,x,y,comp,req_comp);
1565
+ stbi__stop(&s);
1566
+ return result;
1506
1567
  }
1507
1568
  #endif // !STBI_NO_STDIO
1508
1569
 
@@ -1540,12 +1601,11 @@ STBIDEF int stbi_is_hdr (char const *filename)
1540
1601
  STBIDEF int stbi_is_hdr_from_file(FILE *f)
1541
1602
  {
1542
1603
  #ifndef STBI_NO_HDR
1543
- long pos = ftell(f);
1544
1604
  int res;
1545
1605
  stbi__context s;
1546
1606
  stbi__start_file(&s,f);
1547
1607
  res = stbi__hdr_test(&s);
1548
- fseek(f, pos, SEEK_SET);
1608
+ stbi__stop(&s);
1549
1609
  return res;
1550
1610
  #else
1551
1611
  STBI_NOTUSED(f);
@@ -1557,9 +1617,12 @@ STBIDEF int stbi_is_hdr_from_file(FILE *f)
1557
1617
  STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user)
1558
1618
  {
1559
1619
  #ifndef STBI_NO_HDR
1620
+ int result;
1560
1621
  stbi__context s;
1561
1622
  stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
1562
- return stbi__hdr_test(&s);
1623
+ result = stbi__hdr_test(&s);
1624
+ stbi__stop(&s);
1625
+ return result;
1563
1626
  #else
1564
1627
  STBI_NOTUSED(clbk);
1565
1628
  STBI_NOTUSED(user);
@@ -1620,7 +1683,7 @@ stbi_inline static stbi_uc stbi__get8(stbi__context *s)
1620
1683
  return 0;
1621
1684
  }
1622
1685
 
1623
- #if defined(STBI_NO_JPEG) && defined(STBI_NO_HDR) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM)
1686
+ #if defined(STBI_NO_JPEG) && defined(STBI_NO_HDR) && defined(STBI_NO_PNM)
1624
1687
  // nothing
1625
1688
  #else
1626
1689
  stbi_inline static int stbi__at_eof(stbi__context *s)
@@ -1636,7 +1699,7 @@ stbi_inline static int stbi__at_eof(stbi__context *s)
1636
1699
  }
1637
1700
  #endif
1638
1701
 
1639
- #if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC)
1702
+ #if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF)
1640
1703
  // nothing
1641
1704
  #else
1642
1705
  static void stbi__skip(stbi__context *s, int n)
@@ -1658,11 +1721,12 @@ static void stbi__skip(stbi__context *s, int n)
1658
1721
  }
1659
1722
  #endif
1660
1723
 
1661
- #if defined(STBI_NO_PNG) && defined(STBI_NO_TGA) && defined(STBI_NO_HDR) && defined(STBI_NO_PNM)
1724
+ #if defined(STBI_NO_PNG) && defined(STBI_NO_TGA) && defined(STBI_NO_PNM)
1662
1725
  // nothing
1663
1726
  #else
1664
1727
  static int stbi__getn(stbi__context *s, stbi_uc *buffer, int n)
1665
1728
  {
1729
+ if (n <= 0) return 1;
1666
1730
  if (s->io.read) {
1667
1731
  int blen = (int) (s->img_buffer_end - s->img_buffer);
1668
1732
  if (blen < n) {
@@ -1677,7 +1741,7 @@ static int stbi__getn(stbi__context *s, stbi_uc *buffer, int n)
1677
1741
  }
1678
1742
  }
1679
1743
 
1680
- if (s->img_buffer+n <= s->img_buffer_end) {
1744
+ if (n <= s->img_buffer_end - s->img_buffer) {
1681
1745
  memcpy(buffer, s->img_buffer, n);
1682
1746
  s->img_buffer += n;
1683
1747
  return 1;
@@ -1686,7 +1750,7 @@ static int stbi__getn(stbi__context *s, stbi_uc *buffer, int n)
1686
1750
  }
1687
1751
  #endif
1688
1752
 
1689
- #if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC)
1753
+ #if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_PSD)
1690
1754
  // nothing
1691
1755
  #else
1692
1756
  static int stbi__get16be(stbi__context *s)
@@ -1696,7 +1760,7 @@ static int stbi__get16be(stbi__context *s)
1696
1760
  }
1697
1761
  #endif
1698
1762
 
1699
- #if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC)
1763
+ #if defined(STBI_NO_PNG) && defined(STBI_NO_PSD)
1700
1764
  // nothing
1701
1765
  #else
1702
1766
  static stbi__uint32 stbi__get32be(stbi__context *s)
@@ -1727,7 +1791,7 @@ static stbi__uint32 stbi__get32le(stbi__context *s)
1727
1791
 
1728
1792
  #define STBI__BYTECAST(x) ((stbi_uc) ((x) & 255)) // truncate int to byte without warnings
1729
1793
 
1730
- #if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM)
1794
+ #if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PNM)
1731
1795
  // nothing
1732
1796
  #else
1733
1797
  //////////////////////////////////////////////////////////////////////////////
@@ -1747,7 +1811,7 @@ static stbi_uc stbi__compute_y(int r, int g, int b)
1747
1811
  }
1748
1812
  #endif
1749
1813
 
1750
- #if defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM)
1814
+ #if defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PNM)
1751
1815
  // nothing
1752
1816
  #else
1753
1817
  static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int req_comp, unsigned int x, unsigned int y)
@@ -1755,7 +1819,7 @@ static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int r
1755
1819
  int i,j;
1756
1820
  unsigned char *good;
1757
1821
 
1758
- if (req_comp == img_n) return data;
1822
+ if (req_comp == img_n || !data) return data;
1759
1823
  STBI_ASSERT(req_comp >= 1 && req_comp <= 4);
1760
1824
 
1761
1825
  good = (unsigned char *) stbi__malloc_mad3(req_comp, x, y, 0);
@@ -1795,7 +1859,7 @@ static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int r
1795
1859
  }
1796
1860
  #endif
1797
1861
 
1798
- #if defined(STBI_NO_PNG) && defined(STBI_NO_PSD)
1862
+ #if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PNM)
1799
1863
  // nothing
1800
1864
  #else
1801
1865
  static stbi__uint16 stbi__compute_y_16(int r, int g, int b)
@@ -1804,7 +1868,7 @@ static stbi__uint16 stbi__compute_y_16(int r, int g, int b)
1804
1868
  }
1805
1869
  #endif
1806
1870
 
1807
- #if defined(STBI_NO_PNG) && defined(STBI_NO_PSD)
1871
+ #if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PNM)
1808
1872
  // nothing
1809
1873
  #else
1810
1874
  static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int req_comp, unsigned int x, unsigned int y)
@@ -1812,10 +1876,10 @@ static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int r
1812
1876
  int i,j;
1813
1877
  stbi__uint16 *good;
1814
1878
 
1815
- if (req_comp == img_n) return data;
1879
+ if (req_comp == img_n || !data) return data;
1816
1880
  STBI_ASSERT(req_comp >= 1 && req_comp <= 4);
1817
1881
 
1818
- good = (stbi__uint16 *) stbi__malloc(req_comp * x * y * 2);
1882
+ good = (stbi__uint16 *) stbi__malloc_mad4(req_comp, x, y, 2, 0);
1819
1883
  if (good == NULL) {
1820
1884
  STBI_FREE(data);
1821
1885
  return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory");
@@ -2270,7 +2334,6 @@ static int stbi__jpeg_decode_block_prog_dc(stbi__jpeg *j, short data[64], stbi__
2270
2334
 
2271
2335
  if (j->succ_high == 0) {
2272
2336
  // first scan for DC coefficient, must be first
2273
- memset(data,0,64*sizeof(data[0])); // 0 all the ac values now
2274
2337
  t = stbi__jpeg_huff_decode(j, hdc);
2275
2338
  if (t < 0 || t > 15) return stbi__err("can't merge dc and ac", "Corrupt JPEG");
2276
2339
  diff = t ? stbi__extend_receive(j, t) : 0;
@@ -2421,7 +2484,7 @@ stbi_inline static stbi_uc stbi__clamp(int x)
2421
2484
  return (stbi_uc) x;
2422
2485
  }
2423
2486
 
2424
- #define stbi__f2f(x) ((int) (((x) * 4096 + 0.5)))
2487
+ #define stbi__f2f(x) ((int) (((x) * 4096 + 0.5f)))
2425
2488
  #define stbi__fsh(x) ((x) * 4096)
2426
2489
 
2427
2490
  // derived from jidctint -- DCT_ISLOW
@@ -2521,7 +2584,7 @@ static void stbi__idct_block(stbi_uc *out, int out_stride, short data[64])
2521
2584
  }
2522
2585
  }
2523
2586
 
2524
- #ifdef STBI_SSE2
2587
+ #ifdef STBI__SSE2
2525
2588
  // sse2 integer IDCT. not the fastest possible implementation but it
2526
2589
  // produces bit-identical results to the generic C version so it's
2527
2590
  // fully "transparent".
@@ -2700,7 +2763,7 @@ static void stbi__idct_simd(stbi_uc *out, int out_stride, short data[64])
2700
2763
  #undef dct_pass
2701
2764
  }
2702
2765
 
2703
- #endif // STBI_SSE2
2766
+ #endif // STBI__SSE2
2704
2767
 
2705
2768
  #ifdef STBI_NEON
2706
2769
 
@@ -2950,7 +3013,7 @@ static int stbi__parse_entropy_coded_data(stbi__jpeg *z)
2950
3013
  if (!z->progressive) {
2951
3014
  if (z->scan_n == 1) {
2952
3015
  int i,j;
2953
- STBI_SIMD_ALIGN(short, data[64]);
3016
+ STBI__SIMD_ALIGN(short, data[64]);
2954
3017
  int n = z->order[0];
2955
3018
  // non-interleaved data, we just need to process one block at a time,
2956
3019
  // in trivial scanline order
@@ -2976,7 +3039,7 @@ static int stbi__parse_entropy_coded_data(stbi__jpeg *z)
2976
3039
  return 1;
2977
3040
  } else { // interleaved
2978
3041
  int i,j,k,x,y;
2979
- STBI_SIMD_ALIGN(short, data[64]);
3042
+ STBI__SIMD_ALIGN(short, data[64]);
2980
3043
  for (j=0; j < z->img_mcu_y; ++j) {
2981
3044
  for (i=0; i < z->img_mcu_x; ++i) {
2982
3045
  // scan an interleaved mcu... process scan_n components in order
@@ -3018,13 +3081,17 @@ static int stbi__parse_entropy_coded_data(stbi__jpeg *z)
3018
3081
  for (j=0; j < h; ++j) {
3019
3082
  for (i=0; i < w; ++i) {
3020
3083
  short *data = z->img_comp[n].coeff + 64 * (i + j * z->img_comp[n].coeff_w);
3021
- if (z->spec_start == 0) {
3022
- if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->huff_dc[z->img_comp[n].hd], n))
3023
- return 0;
3024
- } else {
3025
- int ha = z->img_comp[n].ha;
3026
- if (!stbi__jpeg_decode_block_prog_ac(z, data, &z->huff_ac[ha], z->fast_ac[ha]))
3027
- return 0;
3084
+ // if we're out of data, just skip decoding this MCU
3085
+ // progressive coeffs got initialized at setup so this is fine
3086
+ if (!z->nomore) {
3087
+ if (z->spec_start == 0) {
3088
+ if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->huff_dc[z->img_comp[n].hd], n))
3089
+ return 0;
3090
+ } else {
3091
+ int ha = z->img_comp[n].ha;
3092
+ if (!stbi__jpeg_decode_block_prog_ac(z, data, &z->huff_ac[ha], z->fast_ac[ha]))
3093
+ return 0;
3094
+ }
3028
3095
  }
3029
3096
  // every data block is an MCU, so countdown the restart interval
3030
3097
  if (--z->todo <= 0) {
@@ -3345,6 +3412,8 @@ static int stbi__process_frame_header(stbi__jpeg *z, int scan)
3345
3412
  if (z->img_comp[i].raw_coeff == NULL)
3346
3413
  return stbi__free_jpeg_components(z, i+1, stbi__err("outofmem", "Out of memory"));
3347
3414
  z->img_comp[i].coeff = (short*) (((size_t) z->img_comp[i].raw_coeff + 15) & ~15);
3415
+ // 0 all the coeffs now so they're guaranteed to be initialized
3416
+ memset(z->img_comp[i].coeff,0,z->img_comp[i].w2 * z->img_comp[i].h2 * sizeof(short));
3348
3417
  }
3349
3418
  }
3350
3419
 
@@ -3384,13 +3453,13 @@ static int stbi__decode_jpeg_header(stbi__jpeg *z, int scan)
3384
3453
  return 1;
3385
3454
  }
3386
3455
 
3387
- static int stbi__skip_jpeg_junk_at_end(stbi__jpeg *j)
3456
+ static stbi_uc stbi__skip_jpeg_junk_at_end(stbi__jpeg *j)
3388
3457
  {
3389
3458
  // some JPEGs have junk at end, skip over it but if we find what looks
3390
3459
  // like a valid marker, resume there
3391
3460
  while (!stbi__at_eof(j->s)) {
3392
- int x = stbi__get8(j->s);
3393
- while (x == 255) { // might be a marker
3461
+ stbi_uc x = stbi__get8(j->s);
3462
+ while (x == 0xff) { // might be a marker
3394
3463
  if (stbi__at_eof(j->s)) return STBI__MARKER_none;
3395
3464
  x = stbi__get8(j->s);
3396
3465
  if (x != 0x00 && x != 0xff) {
@@ -3422,7 +3491,7 @@ static int stbi__decode_jpeg_image(stbi__jpeg *j)
3422
3491
  if (!stbi__process_scan_header(j)) return 0;
3423
3492
  if (!stbi__parse_entropy_coded_data(j)) return 0;
3424
3493
  if (j->marker == STBI__MARKER_none ) {
3425
- j->marker = stbi__skip_jpeg_junk_at_end(j);
3494
+ j->marker = stbi__skip_jpeg_junk_at_end(j);
3426
3495
  // if we reach eof without hitting a marker, stbi__get_marker() below will fail and we'll eventually return 0
3427
3496
  }
3428
3497
  m = stbi__get_marker(j);
@@ -3435,7 +3504,7 @@ static int stbi__decode_jpeg_image(stbi__jpeg *j)
3435
3504
  if (NL != j->s->img_y) return stbi__err("bad DNL height", "Corrupt JPEG");
3436
3505
  m = stbi__get_marker(j);
3437
3506
  } else {
3438
- if (!stbi__process_marker(j, m)) return 1;
3507
+ if (!stbi__process_marker(j, m)) return 0;
3439
3508
  m = stbi__get_marker(j);
3440
3509
  }
3441
3510
  }
@@ -3524,7 +3593,7 @@ static stbi_uc *stbi__resample_row_hv_2(stbi_uc *out, stbi_uc *in_near, stbi_uc
3524
3593
  return out;
3525
3594
  }
3526
3595
 
3527
- #if defined(STBI_SSE2) || defined(STBI_NEON)
3596
+ #if defined(STBI__SSE2) || defined(STBI_NEON)
3528
3597
  static stbi_uc *stbi__resample_row_hv_2_simd(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
3529
3598
  {
3530
3599
  // need to generate 2x2 samples for every one in input
@@ -3540,7 +3609,7 @@ static stbi_uc *stbi__resample_row_hv_2_simd(stbi_uc *out, stbi_uc *in_near, stb
3540
3609
  // note we can't handle the last pixel in a row in this loop
3541
3610
  // because we need to handle the filter boundary conditions.
3542
3611
  for (; i < ((w-1) & ~7); i += 8) {
3543
- #if defined(STBI_SSE2)
3612
+ #if defined(STBI__SSE2)
3544
3613
  // load and perform the vertical filtering pass
3545
3614
  // this uses 3*x + y = 4*x + (y - x)
3546
3615
  __m128i zero = _mm_setzero_si128();
@@ -3680,12 +3749,12 @@ static void stbi__YCbCr_to_RGB_row(stbi_uc *out, const stbi_uc *y, const stbi_uc
3680
3749
  }
3681
3750
  }
3682
3751
 
3683
- #if defined(STBI_SSE2) || defined(STBI_NEON)
3752
+ #if defined(STBI__SSE2) || defined(STBI_NEON)
3684
3753
  static void stbi__YCbCr_to_RGB_simd(stbi_uc *out, stbi_uc const *y, stbi_uc const *pcb, stbi_uc const *pcr, int count, int step)
3685
3754
  {
3686
3755
  int i = 0;
3687
3756
 
3688
- #ifdef STBI_SSE2
3757
+ #ifdef STBI__SSE2
3689
3758
  // step == 3 is pretty ugly on the final interleave, and i'm not convinced
3690
3759
  // it's useful in practice (you wouldn't use it for textures, for example).
3691
3760
  // so just accelerate step == 4 case.
@@ -3822,7 +3891,7 @@ static void stbi__setup_jpeg(stbi__jpeg *j)
3822
3891
  j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_row;
3823
3892
  j->resample_row_hv_2_kernel = stbi__resample_row_hv_2;
3824
3893
 
3825
- #ifdef STBI_SSE2
3894
+ #ifdef STBI__SSE2
3826
3895
  if (stbi__sse2_available()) {
3827
3896
  j->idct_block_kernel = stbi__idct_simd;
3828
3897
  j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd;
@@ -4063,14 +4132,17 @@ static int stbi__jpeg_info_raw(stbi__jpeg *j, int *x, int *y, int *comp)
4063
4132
  return 1;
4064
4133
  }
4065
4134
 
4066
- static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp)
4135
+ static int stbi__jpeg_info(stbi__context *s, stbi_info_res *res)
4067
4136
  {
4068
4137
  int result;
4069
4138
  stbi__jpeg* j = (stbi__jpeg*) (stbi__malloc(sizeof(stbi__jpeg)));
4070
4139
  if (!j) return stbi__err("outofmem", "Out of memory");
4071
4140
  memset(j, 0, sizeof(stbi__jpeg));
4072
4141
  j->s = s;
4073
- result = stbi__jpeg_info_raw(j, x, y, comp);
4142
+ result = stbi__jpeg_info_raw(j, &res->x, &res->y, &res->comp);
4143
+ if (result) {
4144
+ res->type = STBI_type_jpeg;
4145
+ }
4074
4146
  STBI_FREE(j);
4075
4147
  return result;
4076
4148
  }
@@ -4176,6 +4248,7 @@ typedef struct
4176
4248
  {
4177
4249
  stbi_uc *zbuffer, *zbuffer_end;
4178
4250
  int num_bits;
4251
+ int hit_zeof_once;
4179
4252
  stbi__uint32 code_buffer;
4180
4253
 
4181
4254
  char *zout;
@@ -4242,9 +4315,20 @@ stbi_inline static int stbi__zhuffman_decode(stbi__zbuf *a, stbi__zhuffman *z)
4242
4315
  int b,s;
4243
4316
  if (a->num_bits < 16) {
4244
4317
  if (stbi__zeof(a)) {
4245
- return -1; /* report error for unexpected end of data. */
4318
+ if (!a->hit_zeof_once) {
4319
+ // This is the first time we hit eof, insert 16 extra padding btis
4320
+ // to allow us to keep going; if we actually consume any of them
4321
+ // though, that is invalid data. This is caught later.
4322
+ a->hit_zeof_once = 1;
4323
+ a->num_bits += 16; // add 16 implicit zero bits
4324
+ } else {
4325
+ // We already inserted our extra 16 padding bits and are again
4326
+ // out, this stream is actually prematurely terminated.
4327
+ return -1;
4328
+ }
4329
+ } else {
4330
+ stbi__fill_bits(a);
4246
4331
  }
4247
- stbi__fill_bits(a);
4248
4332
  }
4249
4333
  b = z->fast[a->code_buffer & STBI__ZFAST_MASK];
4250
4334
  if (b) {
@@ -4264,6 +4348,7 @@ static int stbi__zexpand(stbi__zbuf *z, char *zout, int n) // need to make room
4264
4348
  if (!z->z_expandable) return stbi__err("output buffer limit","Corrupt PNG");
4265
4349
  cur = (unsigned int) (z->zout - z->zout_start);
4266
4350
  limit = old_limit = (unsigned) (z->zout_end - z->zout_start);
4351
+ if (limit == 0) limit = 1;
4267
4352
  if (UINT_MAX - cur < (unsigned) n) return stbi__err("outofmem", "Out of memory");
4268
4353
  while (cur + n > limit) {
4269
4354
  if(limit > UINT_MAX / 2) return stbi__err("outofmem", "Out of memory");
@@ -4309,6 +4394,13 @@ static int stbi__parse_huffman_block(stbi__zbuf *a)
4309
4394
  int len,dist;
4310
4395
  if (z == 256) {
4311
4396
  a->zout = zout;
4397
+ if (a->hit_zeof_once && a->num_bits < 16) {
4398
+ // The first time we hit zeof, we inserted 16 extra zero bits into our bit
4399
+ // buffer so the decoder can just do its speculative decoding. But if we
4400
+ // actually consumed any of those bits (which is the case when num_bits < 16),
4401
+ // the stream actually read past the end so it is malformed.
4402
+ return stbi__err("unexpected end","Corrupt PNG");
4403
+ }
4312
4404
  return 1;
4313
4405
  }
4314
4406
  if (z >= 286) return stbi__err("bad huffman code","Corrupt PNG"); // per DEFLATE, length codes 286 and 287 must not appear in compressed data
@@ -4320,7 +4412,7 @@ static int stbi__parse_huffman_block(stbi__zbuf *a)
4320
4412
  dist = stbi__zdist_base[z];
4321
4413
  if (stbi__zdist_extra[z]) dist += stbi__zreceive(a, stbi__zdist_extra[z]);
4322
4414
  if (zout - a->zout_start < dist) return stbi__err("bad dist","Corrupt PNG");
4323
- if (zout + len > a->zout_end) {
4415
+ if (len > a->zout_end - zout) {
4324
4416
  if (!stbi__zexpand(a, zout, len)) return 0;
4325
4417
  zout = a->zout;
4326
4418
  }
@@ -4464,6 +4556,7 @@ static int stbi__parse_zlib(stbi__zbuf *a, int parse_header)
4464
4556
  if (!stbi__parse_zlib_header(a)) return 0;
4465
4557
  a->num_bits = 0;
4466
4558
  a->code_buffer = 0;
4559
+ a->hit_zeof_once = 0;
4467
4560
  do {
4468
4561
  final = stbi__zreceive(a,1);
4469
4562
  type = stbi__zreceive(a,2);
@@ -4610,6 +4703,7 @@ typedef struct
4610
4703
  stbi__context *s;
4611
4704
  stbi_uc *idata, *expanded, *out;
4612
4705
  int depth;
4706
+ int is_iphone;
4613
4707
  } stbi__png;
4614
4708
 
4615
4709
 
@@ -4619,9 +4713,8 @@ enum {
4619
4713
  STBI__F_up=2,
4620
4714
  STBI__F_avg=3,
4621
4715
  STBI__F_paeth=4,
4622
- // synthetic filters used for first scanline to avoid needing a dummy row of 0s
4623
- STBI__F_avg_first,
4624
- STBI__F_paeth_first
4716
+ // synthetic filter used for first scanline to avoid needing a dummy row of 0s
4717
+ STBI__F_avg_first
4625
4718
  };
4626
4719
 
4627
4720
  static stbi_uc first_row_filter[5] =
@@ -4630,29 +4723,56 @@ static stbi_uc first_row_filter[5] =
4630
4723
  STBI__F_sub,
4631
4724
  STBI__F_none,
4632
4725
  STBI__F_avg_first,
4633
- STBI__F_paeth_first
4726
+ STBI__F_sub // Paeth with b=c=0 turns out to be equivalent to sub
4634
4727
  };
4635
4728
 
4636
4729
  static int stbi__paeth(int a, int b, int c)
4637
4730
  {
4638
- int p = a + b - c;
4639
- int pa = abs(p-a);
4640
- int pb = abs(p-b);
4641
- int pc = abs(p-c);
4642
- if (pa <= pb && pa <= pc) return a;
4643
- if (pb <= pc) return b;
4644
- return c;
4731
+ // This formulation looks very different from the reference in the PNG spec, but is
4732
+ // actually equivalent and has favorable data dependencies and admits straightforward
4733
+ // generation of branch-free code, which helps performance significantly.
4734
+ int thresh = c*3 - (a + b);
4735
+ int lo = a < b ? a : b;
4736
+ int hi = a < b ? b : a;
4737
+ int t0 = (hi <= thresh) ? lo : c;
4738
+ int t1 = (thresh <= lo) ? hi : t0;
4739
+ return t1;
4645
4740
  }
4646
4741
 
4647
4742
  static const stbi_uc stbi__depth_scale_table[9] = { 0, 0xff, 0x55, 0, 0x11, 0,0,0, 0x01 };
4648
4743
 
4744
+ // adds an extra all-255 alpha channel
4745
+ // dest == src is legal
4746
+ // img_n must be 1 or 3
4747
+ static void stbi__create_png_alpha_expand8(stbi_uc *dest, stbi_uc *src, stbi__uint32 x, int img_n)
4748
+ {
4749
+ int i;
4750
+ // must process data backwards since we allow dest==src
4751
+ if (img_n == 1) {
4752
+ for (i=x-1; i >= 0; --i) {
4753
+ dest[i*2+1] = 255;
4754
+ dest[i*2+0] = src[i];
4755
+ }
4756
+ } else {
4757
+ STBI_ASSERT(img_n == 3);
4758
+ for (i=x-1; i >= 0; --i) {
4759
+ dest[i*4+3] = 255;
4760
+ dest[i*4+2] = src[i*3+2];
4761
+ dest[i*4+1] = src[i*3+1];
4762
+ dest[i*4+0] = src[i*3+0];
4763
+ }
4764
+ }
4765
+ }
4766
+
4649
4767
  // create the png data from post-deflated data
4650
4768
  static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 raw_len, int out_n, stbi__uint32 x, stbi__uint32 y, int depth, int color)
4651
4769
  {
4652
- int bytes = (depth == 16? 2 : 1);
4770
+ int bytes = (depth == 16 ? 2 : 1);
4653
4771
  stbi__context *s = a->s;
4654
4772
  stbi__uint32 i,j,stride = x*out_n*bytes;
4655
4773
  stbi__uint32 img_len, img_width_bytes;
4774
+ stbi_uc *filter_buf;
4775
+ int all_ok = 1;
4656
4776
  int k;
4657
4777
  int img_n = s->img_n; // copy it into a local for later
4658
4778
 
@@ -4664,8 +4784,11 @@ static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 r
4664
4784
  a->out = (stbi_uc *) stbi__malloc_mad3(x, y, output_bytes, 0); // extra bytes to write off the end into
4665
4785
  if (!a->out) return stbi__err("outofmem", "Out of memory");
4666
4786
 
4787
+ // note: error exits here don't need to clean up a->out individually,
4788
+ // stbi__do_png always does on error.
4667
4789
  if (!stbi__mad3sizes_valid(img_n, x, depth, 7)) return stbi__err("too large", "Corrupt PNG");
4668
4790
  img_width_bytes = (((img_n * x * depth) + 7) >> 3);
4791
+ if (!stbi__mad2sizes_valid(img_width_bytes, y, img_width_bytes)) return stbi__err("too large", "Corrupt PNG");
4669
4792
  img_len = (img_width_bytes + 1) * y;
4670
4793
 
4671
4794
  // we used to check for exact match between raw_len and img_len on non-interlaced PNGs,
@@ -4673,189 +4796,137 @@ static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 r
4673
4796
  // so just check for raw_len < img_len always.
4674
4797
  if (raw_len < img_len) return stbi__err("not enough pixels","Corrupt PNG");
4675
4798
 
4799
+ // Allocate two scan lines worth of filter workspace buffer.
4800
+ filter_buf = (stbi_uc *) stbi__malloc_mad2(img_width_bytes, 2, 0);
4801
+ if (!filter_buf) return stbi__err("outofmem", "Out of memory");
4802
+
4803
+ // Filtering for low-bit-depth images
4804
+ if (depth < 8) {
4805
+ filter_bytes = 1;
4806
+ width = img_width_bytes;
4807
+ }
4808
+
4676
4809
  for (j=0; j < y; ++j) {
4677
- stbi_uc *cur = a->out + stride*j;
4678
- stbi_uc *prior;
4810
+ // cur/prior filter buffers alternate
4811
+ stbi_uc *cur = filter_buf + (j & 1)*img_width_bytes;
4812
+ stbi_uc *prior = filter_buf + (~j & 1)*img_width_bytes;
4813
+ stbi_uc *dest = a->out + stride*j;
4814
+ int nk = width * filter_bytes;
4679
4815
  int filter = *raw++;
4680
4816
 
4681
- if (filter > 4)
4682
- return stbi__err("invalid filter","Corrupt PNG");
4683
-
4684
- if (depth < 8) {
4685
- if (img_width_bytes > x) return stbi__err("invalid width","Corrupt PNG");
4686
- cur += x*out_n - img_width_bytes; // store output to the rightmost img_len bytes, so we can decode in place
4687
- filter_bytes = 1;
4688
- width = img_width_bytes;
4817
+ // check filter type
4818
+ if (filter > 4) {
4819
+ all_ok = stbi__err("invalid filter","Corrupt PNG");
4820
+ break;
4689
4821
  }
4690
- prior = cur - stride; // bugfix: need to compute this after 'cur +=' computation above
4691
4822
 
4692
4823
  // if first row, use special filter that doesn't sample previous row
4693
4824
  if (j == 0) filter = first_row_filter[filter];
4694
4825
 
4695
- // handle first byte explicitly
4696
- for (k=0; k < filter_bytes; ++k) {
4697
- switch (filter) {
4698
- case STBI__F_none : cur[k] = raw[k]; break;
4699
- case STBI__F_sub : cur[k] = raw[k]; break;
4700
- case STBI__F_up : cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break;
4701
- case STBI__F_avg : cur[k] = STBI__BYTECAST(raw[k] + (prior[k]>>1)); break;
4702
- case STBI__F_paeth : cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(0,prior[k],0)); break;
4703
- case STBI__F_avg_first : cur[k] = raw[k]; break;
4704
- case STBI__F_paeth_first: cur[k] = raw[k]; break;
4705
- }
4706
- }
4707
-
4708
- if (depth == 8) {
4709
- if (img_n != out_n)
4710
- cur[img_n] = 255; // first pixel
4711
- raw += img_n;
4712
- cur += out_n;
4713
- prior += out_n;
4714
- } else if (depth == 16) {
4715
- if (img_n != out_n) {
4716
- cur[filter_bytes] = 255; // first pixel top byte
4717
- cur[filter_bytes+1] = 255; // first pixel bottom byte
4718
- }
4719
- raw += filter_bytes;
4720
- cur += output_bytes;
4721
- prior += output_bytes;
4722
- } else {
4723
- raw += 1;
4724
- cur += 1;
4725
- prior += 1;
4826
+ // perform actual filtering
4827
+ switch (filter) {
4828
+ case STBI__F_none:
4829
+ memcpy(cur, raw, nk);
4830
+ break;
4831
+ case STBI__F_sub:
4832
+ memcpy(cur, raw, filter_bytes);
4833
+ for (k = filter_bytes; k < nk; ++k)
4834
+ cur[k] = STBI__BYTECAST(raw[k] + cur[k-filter_bytes]);
4835
+ break;
4836
+ case STBI__F_up:
4837
+ for (k = 0; k < nk; ++k)
4838
+ cur[k] = STBI__BYTECAST(raw[k] + prior[k]);
4839
+ break;
4840
+ case STBI__F_avg:
4841
+ for (k = 0; k < filter_bytes; ++k)
4842
+ cur[k] = STBI__BYTECAST(raw[k] + (prior[k]>>1));
4843
+ for (k = filter_bytes; k < nk; ++k)
4844
+ cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-filter_bytes])>>1));
4845
+ break;
4846
+ case STBI__F_paeth:
4847
+ for (k = 0; k < filter_bytes; ++k)
4848
+ cur[k] = STBI__BYTECAST(raw[k] + prior[k]); // prior[k] == stbi__paeth(0,prior[k],0)
4849
+ for (k = filter_bytes; k < nk; ++k)
4850
+ cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes], prior[k], prior[k-filter_bytes]));
4851
+ break;
4852
+ case STBI__F_avg_first:
4853
+ memcpy(cur, raw, filter_bytes);
4854
+ for (k = filter_bytes; k < nk; ++k)
4855
+ cur[k] = STBI__BYTECAST(raw[k] + (cur[k-filter_bytes] >> 1));
4856
+ break;
4726
4857
  }
4727
4858
 
4728
- // this is a little gross, so that we don't switch per-pixel or per-component
4729
- if (depth < 8 || img_n == out_n) {
4730
- int nk = (width - 1)*filter_bytes;
4731
- #define STBI__CASE(f) \
4732
- case f: \
4733
- for (k=0; k < nk; ++k)
4734
- switch (filter) {
4735
- // "none" filter turns into a memcpy here; make that explicit.
4736
- case STBI__F_none: memcpy(cur, raw, nk); break;
4737
- STBI__CASE(STBI__F_sub) { cur[k] = STBI__BYTECAST(raw[k] + cur[k-filter_bytes]); } break;
4738
- STBI__CASE(STBI__F_up) { cur[k] = STBI__BYTECAST(raw[k] + prior[k]); } break;
4739
- STBI__CASE(STBI__F_avg) { cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-filter_bytes])>>1)); } break;
4740
- STBI__CASE(STBI__F_paeth) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],prior[k],prior[k-filter_bytes])); } break;
4741
- STBI__CASE(STBI__F_avg_first) { cur[k] = STBI__BYTECAST(raw[k] + (cur[k-filter_bytes] >> 1)); } break;
4742
- STBI__CASE(STBI__F_paeth_first) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],0,0)); } break;
4743
- }
4744
- #undef STBI__CASE
4745
- raw += nk;
4746
- } else {
4747
- STBI_ASSERT(img_n+1 == out_n);
4748
- #define STBI__CASE(f) \
4749
- case f: \
4750
- for (i=x-1; i >= 1; --i, cur[filter_bytes]=255,raw+=filter_bytes,cur+=output_bytes,prior+=output_bytes) \
4751
- for (k=0; k < filter_bytes; ++k)
4752
- switch (filter) {
4753
- STBI__CASE(STBI__F_none) { cur[k] = raw[k]; } break;
4754
- STBI__CASE(STBI__F_sub) { cur[k] = STBI__BYTECAST(raw[k] + cur[k- output_bytes]); } break;
4755
- STBI__CASE(STBI__F_up) { cur[k] = STBI__BYTECAST(raw[k] + prior[k]); } break;
4756
- STBI__CASE(STBI__F_avg) { cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k- output_bytes])>>1)); } break;
4757
- STBI__CASE(STBI__F_paeth) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k- output_bytes],prior[k],prior[k- output_bytes])); } break;
4758
- STBI__CASE(STBI__F_avg_first) { cur[k] = STBI__BYTECAST(raw[k] + (cur[k- output_bytes] >> 1)); } break;
4759
- STBI__CASE(STBI__F_paeth_first) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k- output_bytes],0,0)); } break;
4760
- }
4761
- #undef STBI__CASE
4762
-
4763
- // the loop above sets the high byte of the pixels' alpha, but for
4764
- // 16 bit png files we also need the low byte set. we'll do that here.
4765
- if (depth == 16) {
4766
- cur = a->out + stride*j; // start at the beginning of the row again
4767
- for (i=0; i < x; ++i,cur+=output_bytes) {
4768
- cur[filter_bytes+1] = 255;
4769
- }
4770
- }
4771
- }
4772
- }
4859
+ raw += nk;
4773
4860
 
4774
- // we make a separate pass to expand bits to pixels; for performance,
4775
- // this could run two scanlines behind the above code, so it won't
4776
- // intefere with filtering but will still be in the cache.
4777
- if (depth < 8) {
4778
- for (j=0; j < y; ++j) {
4779
- stbi_uc *cur = a->out + stride*j;
4780
- stbi_uc *in = a->out + stride*j + x*out_n - img_width_bytes;
4781
- // unpack 1/2/4-bit into a 8-bit buffer. allows us to keep the common 8-bit path optimal at minimal cost for 1/2/4-bit
4782
- // png guarante byte alignment, if width is not multiple of 8/4/2 we'll decode dummy trailing data that will be skipped in the later loop
4861
+ // expand decoded bits in cur to dest, also adding an extra alpha channel if desired
4862
+ if (depth < 8) {
4783
4863
  stbi_uc scale = (color == 0) ? stbi__depth_scale_table[depth] : 1; // scale grayscale values to 0..255 range
4864
+ stbi_uc *in = cur;
4865
+ stbi_uc *out = dest;
4866
+ stbi_uc inb = 0;
4867
+ stbi__uint32 nsmp = x*img_n;
4784
4868
 
4785
- // note that the final byte might overshoot and write more data than desired.
4786
- // we can allocate enough data that this never writes out of memory, but it
4787
- // could also overwrite the next scanline. can it overwrite non-empty data
4788
- // on the next scanline? yes, consider 1-pixel-wide scanlines with 1-bit-per-pixel.
4789
- // so we need to explicitly clamp the final ones
4790
-
4869
+ // expand bits to bytes first
4791
4870
  if (depth == 4) {
4792
- for (k=x*img_n; k >= 2; k-=2, ++in) {
4793
- *cur++ = scale * ((*in >> 4) );
4794
- *cur++ = scale * ((*in ) & 0x0f);
4871
+ for (i=0; i < nsmp; ++i) {
4872
+ if ((i & 1) == 0) inb = *in++;
4873
+ *out++ = scale * (inb >> 4);
4874
+ inb <<= 4;
4795
4875
  }
4796
- if (k > 0) *cur++ = scale * ((*in >> 4) );
4797
4876
  } else if (depth == 2) {
4798
- for (k=x*img_n; k >= 4; k-=4, ++in) {
4799
- *cur++ = scale * ((*in >> 6) );
4800
- *cur++ = scale * ((*in >> 4) & 0x03);
4801
- *cur++ = scale * ((*in >> 2) & 0x03);
4802
- *cur++ = scale * ((*in ) & 0x03);
4877
+ for (i=0; i < nsmp; ++i) {
4878
+ if ((i & 3) == 0) inb = *in++;
4879
+ *out++ = scale * (inb >> 6);
4880
+ inb <<= 2;
4803
4881
  }
4804
- if (k > 0) *cur++ = scale * ((*in >> 6) );
4805
- if (k > 1) *cur++ = scale * ((*in >> 4) & 0x03);
4806
- if (k > 2) *cur++ = scale * ((*in >> 2) & 0x03);
4807
- } else if (depth == 1) {
4808
- for (k=x*img_n; k >= 8; k-=8, ++in) {
4809
- *cur++ = scale * ((*in >> 7) );
4810
- *cur++ = scale * ((*in >> 6) & 0x01);
4811
- *cur++ = scale * ((*in >> 5) & 0x01);
4812
- *cur++ = scale * ((*in >> 4) & 0x01);
4813
- *cur++ = scale * ((*in >> 3) & 0x01);
4814
- *cur++ = scale * ((*in >> 2) & 0x01);
4815
- *cur++ = scale * ((*in >> 1) & 0x01);
4816
- *cur++ = scale * ((*in ) & 0x01);
4882
+ } else {
4883
+ STBI_ASSERT(depth == 1);
4884
+ for (i=0; i < nsmp; ++i) {
4885
+ if ((i & 7) == 0) inb = *in++;
4886
+ *out++ = scale * (inb >> 7);
4887
+ inb <<= 1;
4817
4888
  }
4818
- if (k > 0) *cur++ = scale * ((*in >> 7) );
4819
- if (k > 1) *cur++ = scale * ((*in >> 6) & 0x01);
4820
- if (k > 2) *cur++ = scale * ((*in >> 5) & 0x01);
4821
- if (k > 3) *cur++ = scale * ((*in >> 4) & 0x01);
4822
- if (k > 4) *cur++ = scale * ((*in >> 3) & 0x01);
4823
- if (k > 5) *cur++ = scale * ((*in >> 2) & 0x01);
4824
- if (k > 6) *cur++ = scale * ((*in >> 1) & 0x01);
4825
4889
  }
4826
- if (img_n != out_n) {
4827
- int q;
4828
- // insert alpha = 255
4829
- cur = a->out + stride*j;
4890
+
4891
+ // insert alpha=255 values if desired
4892
+ if (img_n != out_n)
4893
+ stbi__create_png_alpha_expand8(dest, dest, x, img_n);
4894
+ } else if (depth == 8) {
4895
+ if (img_n == out_n)
4896
+ memcpy(dest, cur, x*img_n);
4897
+ else
4898
+ stbi__create_png_alpha_expand8(dest, cur, x, img_n);
4899
+ } else if (depth == 16) {
4900
+ // convert the image data from big-endian to platform-native
4901
+ stbi__uint16 *dest16 = (stbi__uint16*)dest;
4902
+ stbi__uint32 nsmp = x*img_n;
4903
+
4904
+ if (img_n == out_n) {
4905
+ for (i = 0; i < nsmp; ++i, ++dest16, cur += 2)
4906
+ *dest16 = (cur[0] << 8) | cur[1];
4907
+ } else {
4908
+ STBI_ASSERT(img_n+1 == out_n);
4830
4909
  if (img_n == 1) {
4831
- for (q=x-1; q >= 0; --q) {
4832
- cur[q*2+1] = 255;
4833
- cur[q*2+0] = cur[q];
4910
+ for (i = 0; i < x; ++i, dest16 += 2, cur += 2) {
4911
+ dest16[0] = (cur[0] << 8) | cur[1];
4912
+ dest16[1] = 0xffff;
4834
4913
  }
4835
4914
  } else {
4836
4915
  STBI_ASSERT(img_n == 3);
4837
- for (q=x-1; q >= 0; --q) {
4838
- cur[q*4+3] = 255;
4839
- cur[q*4+2] = cur[q*3+2];
4840
- cur[q*4+1] = cur[q*3+1];
4841
- cur[q*4+0] = cur[q*3+0];
4916
+ for (i = 0; i < x; ++i, dest16 += 4, cur += 6) {
4917
+ dest16[0] = (cur[0] << 8) | cur[1];
4918
+ dest16[1] = (cur[2] << 8) | cur[3];
4919
+ dest16[2] = (cur[4] << 8) | cur[5];
4920
+ dest16[3] = 0xffff;
4842
4921
  }
4843
4922
  }
4844
4923
  }
4845
4924
  }
4846
- } else if (depth == 16) {
4847
- // force the image data from big-endian to platform-native.
4848
- // this is done in a separate pass due to the decoding relying
4849
- // on the data being untouched, but could probably be done
4850
- // per-line during decode if care is taken.
4851
- stbi_uc *cur = a->out;
4852
- stbi__uint16 *cur16 = (stbi__uint16*)cur;
4853
-
4854
- for(i=0; i < x*y*out_n; ++i,cur16++,cur+=2) {
4855
- *cur16 = (cur[0] << 8) | cur[1];
4856
- }
4857
4925
  }
4858
4926
 
4927
+ STBI_FREE(filter_buf);
4928
+ if (!all_ok) return 0;
4929
+
4859
4930
  return 1;
4860
4931
  }
4861
4932
 
@@ -5079,15 +5150,16 @@ static void stbi__de_iphone(stbi__png *z)
5079
5150
  static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
5080
5151
  {
5081
5152
  stbi_uc palette[1024], pal_img_n=0;
5082
- stbi_uc has_trans=0, tc[3]={0};
5153
+ stbi_uc has_trans=0, tc[3]={0,0,0};
5083
5154
  stbi__uint16 tc16[3];
5084
5155
  stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0;
5085
- int first=1,k,interlace=0, color=0, is_iphone=0;
5156
+ int first=1,k,interlace=0, color=0;
5086
5157
  stbi__context *s = z->s;
5087
5158
 
5088
5159
  z->expanded = NULL;
5089
5160
  z->idata = NULL;
5090
5161
  z->out = NULL;
5162
+ z->is_iphone = 0;
5091
5163
 
5092
5164
  if (!stbi__check_png_header(s)) return 0;
5093
5165
 
@@ -5097,7 +5169,7 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
5097
5169
  stbi__pngchunk c = stbi__get_chunk_header(s);
5098
5170
  switch (c.type) {
5099
5171
  case STBI__PNG_TYPE('C','g','B','I'):
5100
- is_iphone = 1;
5172
+ z->is_iphone = 1;
5101
5173
  stbi__skip(s, c.length);
5102
5174
  break;
5103
5175
  case STBI__PNG_TYPE('I','H','D','R'): {
@@ -5113,6 +5185,7 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
5113
5185
  color = stbi__get8(s); if (color > 6) return stbi__err("bad ctype","Corrupt PNG");
5114
5186
  if (color == 3 && z->depth == 16) return stbi__err("bad ctype","Corrupt PNG");
5115
5187
  if (color == 3) pal_img_n = 3; else if (color & 1) return stbi__err("bad ctype","Corrupt PNG");
5188
+ if (z->depth < 8 && color != 0 && color != 3) return stbi__err("bad ctype","Corrupt PNG");
5116
5189
  comp = stbi__get8(s); if (comp) return stbi__err("bad comp method","Corrupt PNG");
5117
5190
  filter= stbi__get8(s); if (filter) return stbi__err("bad filter method","Corrupt PNG");
5118
5191
  interlace = stbi__get8(s); if (interlace>1) return stbi__err("bad interlace method","Corrupt PNG");
@@ -5125,6 +5198,7 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
5125
5198
  // img_n is # components to decompress/filter.
5126
5199
  s->img_n = 1;
5127
5200
  if ((1 << 30) / s->img_x / 4 < s->img_y) return stbi__err("too large","Corrupt PNG");
5201
+ memset(palette, 0, sizeof(palette));
5128
5202
  }
5129
5203
  // even with SCAN_header, have to scan to see if we have a tRNS
5130
5204
  break;
@@ -5161,9 +5235,11 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
5161
5235
  // non-paletted with tRNS = constant alpha. if header-scanning, we can stop now.
5162
5236
  if (scan == STBI__SCAN_header) { ++s->img_n; return 1; }
5163
5237
  if (z->depth == 16) {
5164
- for (k = 0; k < s->img_n; ++k) tc16[k] = (stbi__uint16)stbi__get16be(s); // copy the values as-is
5238
+ for (k = 0; k < s->img_n && k < 3; ++k) // extra loop test to suppress false GCC warning
5239
+ tc16[k] = (stbi__uint16)stbi__get16be(s); // copy the values as-is
5165
5240
  } else {
5166
- for (k = 0; k < s->img_n; ++k) tc[k] = (stbi_uc)(stbi__get16be(s) & 255) * stbi__depth_scale_table[z->depth]; // non 8-bit images will be larger
5241
+ for (k = 0; k < s->img_n && k < 3; ++k)
5242
+ tc[k] = (stbi_uc)(stbi__get16be(s) & 255) * stbi__depth_scale_table[z->depth]; // non 8-bit images will be larger
5167
5243
  }
5168
5244
  }
5169
5245
  break;
@@ -5179,13 +5255,15 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
5179
5255
  return 1;
5180
5256
  }
5181
5257
  if (c.length > (1u << 30)) return stbi__err("IDAT size limit", "IDAT section larger than 2^30 bytes");
5182
- if ((int)(ioff + c.length) < (int)ioff) return 0;
5258
+ if (!stbi__addsizes_valid(ioff, c.length)) return 0;
5183
5259
  if (ioff + c.length > idata_limit) {
5184
5260
  stbi__uint32 idata_limit_old = idata_limit;
5185
5261
  stbi_uc *p;
5186
5262
  if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096;
5187
- while (ioff + c.length > idata_limit)
5263
+ while (ioff + c.length > idata_limit) {
5264
+ if (idata_limit > INT_MAX/2) return stbi__err("IDAT size limit", "Too much image data");
5188
5265
  idata_limit *= 2;
5266
+ }
5189
5267
  STBI_NOTUSED(idata_limit_old);
5190
5268
  p = (stbi_uc *) STBI_REALLOC_SIZED(z->idata, idata_limit_old, idata_limit); if (p == NULL) return stbi__err("outofmem", "Out of memory");
5191
5269
  z->idata = p;
@@ -5203,7 +5281,7 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
5203
5281
  // initial guess for decoded data size to avoid unnecessary reallocs
5204
5282
  bpl = (s->img_x * z->depth + 7) / 8; // bytes per line, per component
5205
5283
  raw_len = bpl * s->img_y * s->img_n /* pixels */ + s->img_y /* filter mode per row */;
5206
- z->expanded = (stbi_uc *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, raw_len, (int *) &raw_len, !is_iphone);
5284
+ z->expanded = (stbi_uc *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, raw_len, (int *) &raw_len, !z->is_iphone);
5207
5285
  if (z->expanded == NULL) return 0; // zlib should set error
5208
5286
  STBI_FREE(z->idata); z->idata = NULL;
5209
5287
  if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans)
@@ -5218,7 +5296,7 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
5218
5296
  if (!stbi__compute_transparency(z, tc, s->img_out_n)) return 0;
5219
5297
  }
5220
5298
  }
5221
- if (is_iphone && stbi__de_iphone_flag && s->img_out_n > 2)
5299
+ if (z->is_iphone && stbi__de_iphone_flag && s->img_out_n > 2)
5222
5300
  stbi__de_iphone(z);
5223
5301
  if (pal_img_n) {
5224
5302
  // pal_img_n == 3 or 4
@@ -5241,15 +5319,7 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
5241
5319
  // if critical, fail
5242
5320
  if (first) return stbi__err("first not IHDR", "Corrupt PNG");
5243
5321
  if ((c.type & (1 << 29)) == 0) {
5244
- #ifndef STBI_NO_FAILURE_STRINGS
5245
- // not threadsafe
5246
- static char invalid_chunk[] = "XXXX PNG chunk not known";
5247
- invalid_chunk[0] = STBI__BYTECAST(c.type >> 24);
5248
- invalid_chunk[1] = STBI__BYTECAST(c.type >> 16);
5249
- invalid_chunk[2] = STBI__BYTECAST(c.type >> 8);
5250
- invalid_chunk[3] = STBI__BYTECAST(c.type >> 0);
5251
- #endif
5252
- return stbi__err(invalid_chunk, "PNG not supported: unknown PNG chunk type");
5322
+ return stbi__err("PNG chunk not known", "PNG not supported: unknown PNG chunk type");
5253
5323
  }
5254
5324
  stbi__skip(s, c.length);
5255
5325
  break;
@@ -5306,35 +5376,20 @@ static int stbi__png_test(stbi__context *s)
5306
5376
  return r;
5307
5377
  }
5308
5378
 
5309
- static int stbi__png_info_raw(stbi__png *p, int *x, int *y, int *comp)
5310
- {
5311
- if (!stbi__parse_png_file(p, STBI__SCAN_header, 0)) {
5312
- stbi__rewind( p->s );
5313
- return 0;
5314
- }
5315
- if (x) *x = p->s->img_x;
5316
- if (y) *y = p->s->img_y;
5317
- if (comp) *comp = p->s->img_n;
5318
- return 1;
5319
- }
5320
-
5321
- static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp)
5322
- {
5323
- stbi__png p;
5324
- p.s = s;
5325
- return stbi__png_info_raw(&p, x, y, comp);
5326
- }
5327
-
5328
- static int stbi__png_is16(stbi__context *s)
5379
+ static int stbi__png_info(stbi__context *s, stbi_info_res *res)
5329
5380
  {
5330
5381
  stbi__png p;
5331
5382
  p.s = s;
5332
- if (!stbi__png_info_raw(&p, NULL, NULL, NULL))
5333
- return 0;
5334
- if (p.depth != 16) {
5335
- stbi__rewind(p.s);
5383
+ if (!stbi__parse_png_file(&p, STBI__SCAN_header, 0)) {
5384
+ stbi__rewind( p.s );
5336
5385
  return 0;
5337
5386
  }
5387
+ res->type = STBI_type_png;
5388
+ res->x = p.s->img_x;
5389
+ res->y = p.s->img_y;
5390
+ res->comp = p.s->img_n;
5391
+ res->is_16_bit = p.depth == 16;
5392
+ res->is_iphone_png = p.is_iphone;
5338
5393
  return 1;
5339
5394
  }
5340
5395
  #endif
@@ -5429,11 +5484,12 @@ static int stbi__bmp_set_mask_defaults(stbi__bmp_data *info, int compress)
5429
5484
  info->mr = 31u << 10;
5430
5485
  info->mg = 31u << 5;
5431
5486
  info->mb = 31u << 0;
5487
+ info->ma = 0;
5432
5488
  } else if (info->bpp == 32) {
5433
5489
  info->mr = 0xffu << 16;
5434
5490
  info->mg = 0xffu << 8;
5435
5491
  info->mb = 0xffu << 0;
5436
- info->ma = 0xffu << 24;
5492
+ info->ma = 0; // BI_BITFIELDS BMPs can have alpha, but BI_RGB ones never do
5437
5493
  info->all_a = 0; // if all_a is 0 at end, then we loaded alpha channel but it was all 0
5438
5494
  } else {
5439
5495
  // otherwise, use defaults, which is all-0
@@ -5478,13 +5534,7 @@ static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info)
5478
5534
  stbi__get32le(s); // discard vres
5479
5535
  stbi__get32le(s); // discard colorsused
5480
5536
  stbi__get32le(s); // discard max important
5481
- if (hsz == 40 || hsz == 56) {
5482
- if (hsz == 56) {
5483
- stbi__get32le(s);
5484
- stbi__get32le(s);
5485
- stbi__get32le(s);
5486
- stbi__get32le(s);
5487
- }
5537
+ if (hsz == 40) { // BITMAPINFOHEADER
5488
5538
  if (info->bpp == 16 || info->bpp == 32) {
5489
5539
  if (compress == 0) {
5490
5540
  stbi__bmp_set_mask_defaults(info, compress);
@@ -5502,9 +5552,9 @@ static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info)
5502
5552
  return stbi__errpuc("bad BMP", "bad BMP");
5503
5553
  }
5504
5554
  } else {
5505
- // V4/V5 header
5555
+ // V3/V4/V5 header
5506
5556
  int i;
5507
- if (hsz != 108 && hsz != 124)
5557
+ if (hsz != 56 && hsz != 108 && hsz != 124)
5508
5558
  return stbi__errpuc("bad BMP", "bad BMP");
5509
5559
  info->mr = stbi__get32le(s);
5510
5560
  info->mg = stbi__get32le(s);
@@ -5512,9 +5562,11 @@ static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info)
5512
5562
  info->ma = stbi__get32le(s);
5513
5563
  if (compress != 3) // override mr/mg/mb unless in BI_BITFIELDS mode, as per docs
5514
5564
  stbi__bmp_set_mask_defaults(info, compress);
5515
- stbi__get32le(s); // discard color space
5516
- for (i=0; i < 12; ++i)
5517
- stbi__get32le(s); // discard color space parameters
5565
+ if (hsz >= 108) {
5566
+ stbi__get32le(s); // discard color space
5567
+ for (i=0; i < 12; ++i)
5568
+ stbi__get32le(s); // discard color space parameters
5569
+ }
5518
5570
  if (hsz == 124) {
5519
5571
  stbi__get32le(s); // discard rendering intent
5520
5572
  stbi__get32le(s); // discard offset of profile data
@@ -5534,6 +5586,7 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5534
5586
  stbi_uc pal[256][4];
5535
5587
  int psize=0,i,j,width;
5536
5588
  int flip_vertically, pad, target;
5589
+ int bytes_read_so_far;
5537
5590
  stbi__bmp_data info;
5538
5591
  STBI_NOTUSED(ri);
5539
5592
 
@@ -5553,17 +5606,20 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5553
5606
  ma = info.ma;
5554
5607
  all_a = info.all_a;
5555
5608
 
5609
+ bytes_read_so_far = info.hsz + info.extra_read;
5610
+ if (info.offset < bytes_read_so_far)
5611
+ return stbi__errpuc("bad header", "Corrupt BMP");
5612
+
5556
5613
  if (info.hsz == 12) {
5557
5614
  if (info.bpp < 24)
5558
- psize = (info.offset - info.extra_read - 24) / 3;
5615
+ psize = (info.offset - bytes_read_so_far) / 3;
5559
5616
  } else {
5560
5617
  if (info.bpp < 16)
5561
- psize = (info.offset - info.extra_read - info.hsz) >> 2;
5618
+ psize = (info.offset - bytes_read_so_far) >> 2;
5562
5619
  }
5563
5620
  if (psize == 0) {
5564
5621
  // accept some number of extra bytes after the header, but if the offset points either to before
5565
5622
  // the header ends or implies a large amount of extra data, reject the file as malformed
5566
- int bytes_read_so_far = s->callback_already_read + (int)(s->img_buffer - s->img_buffer_original);
5567
5623
  int header_limit = 1024; // max we actually read is below 256 bytes currently.
5568
5624
  int extra_data_limit = 256*4; // what ordinarily goes here is a palette; 256 entries*4 bytes is its max size.
5569
5625
  if (bytes_read_so_far <= 0 || bytes_read_so_far > header_limit) {
@@ -5598,6 +5654,7 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5598
5654
  if (info.bpp < 16) {
5599
5655
  int z=0;
5600
5656
  if (psize == 0 || psize > 256) { STBI_FREE(out); return stbi__errpuc("invalid", "Corrupt BMP"); }
5657
+ memset(pal, 0, sizeof(pal));
5601
5658
  for (i=0; i < psize; ++i) {
5602
5659
  pal[i][2] = stbi__get8(s);
5603
5660
  pal[i][1] = stbi__get8(s);
@@ -5654,7 +5711,8 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5654
5711
  int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0;
5655
5712
  int z = 0;
5656
5713
  int easy=0;
5657
- stbi__skip(s, info.offset - info.extra_read - info.hsz);
5714
+ stbi_uc alpha_override=0;
5715
+ // psize is 0, already skipped to line data above
5658
5716
  if (info.bpp == 24) width = 3 * s->img_x;
5659
5717
  else if (info.bpp == 16) width = 2*s->img_x;
5660
5718
  else /* bpp = 32 and pad = 0 */ width=0;
@@ -5662,8 +5720,12 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5662
5720
  if (info.bpp == 24) {
5663
5721
  easy = 1;
5664
5722
  } else if (info.bpp == 32) {
5665
- if (mb == 0xff && mg == 0xff00 && mr == 0x00ff0000 && ma == 0xff000000)
5723
+ if (info.hsz < 56) // before v3 header, no real alpha support, so treat alpha as not present
5724
+ ma = 0;
5725
+ if (mb == 0xff && mg == 0xff00 && mr == 0x00ff0000 && (ma == 0xff000000 || ma == 0)) {
5666
5726
  easy = 2;
5727
+ alpha_override = ma ? 0 : 255;
5728
+ }
5667
5729
  }
5668
5730
  if (!easy) {
5669
5731
  if (!mr || !mg || !mb) { STBI_FREE(out); return stbi__errpuc("bad masks", "Corrupt BMP"); }
@@ -5682,7 +5744,7 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5682
5744
  out[z+1] = stbi__get8(s);
5683
5745
  out[z+0] = stbi__get8(s);
5684
5746
  z += 3;
5685
- a = (easy == 2 ? stbi__get8(s) : 255);
5747
+ a = (easy == 2 ? (stbi__get8(s) | alpha_override) : 255);
5686
5748
  all_a |= a;
5687
5749
  if (target == 4) out[z++] = a;
5688
5750
  }
@@ -5708,16 +5770,8 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5708
5770
  for (i=4*s->img_x*s->img_y-1; i >= 0; i -= 4)
5709
5771
  out[i] = 255;
5710
5772
 
5711
- if (flip_vertically) {
5712
- stbi_uc t;
5713
- for (j=0; j < (int) s->img_y>>1; ++j) {
5714
- stbi_uc *p1 = out + j *s->img_x*target;
5715
- stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target;
5716
- for (i=0; i < (int) s->img_x*target; ++i) {
5717
- t = p1[i]; p1[i] = p2[i]; p2[i] = t;
5718
- }
5719
- }
5720
- }
5773
+ if (flip_vertically)
5774
+ stbi__vertical_flip(out, s->img_x, s->img_y, target);
5721
5775
 
5722
5776
  if (req_comp && req_comp != target) {
5723
5777
  out = stbi__convert_format(out, target, req_comp, s->img_x, s->img_y);
@@ -5734,115 +5788,80 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5734
5788
  // Targa Truevision - TGA
5735
5789
  // by Jonathan Dummer
5736
5790
  #ifndef STBI_NO_TGA
5791
+
5737
5792
  // returns STBI_rgb or whatever, 0 on error
5738
5793
  static int stbi__tga_get_comp(int bits_per_pixel, int is_grey, int* is_rgb16)
5739
5794
  {
5740
5795
  // only RGB or RGBA (incl. 16bit) or grey allowed
5741
5796
  if (is_rgb16) *is_rgb16 = 0;
5742
- switch(bits_per_pixel) {
5743
- case 8: return STBI_grey;
5744
- case 16: if(is_grey) return STBI_grey_alpha;
5745
- // fallthrough
5746
- case 15: if(is_rgb16) *is_rgb16 = 1;
5747
- return STBI_rgb;
5748
- case 24: // fallthrough
5749
- case 32: return bits_per_pixel/8;
5750
- default: return 0;
5751
- }
5752
- }
5753
-
5754
- static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp)
5755
- {
5756
- int tga_w, tga_h, tga_comp, tga_image_type, tga_bits_per_pixel, tga_colormap_bpp;
5757
- int sz, tga_colormap_type;
5758
- stbi__get8(s); // discard Offset
5759
- tga_colormap_type = stbi__get8(s); // colormap type
5760
- if( tga_colormap_type > 1 ) {
5761
- stbi__rewind(s);
5762
- return 0; // only RGB or indexed allowed
5763
- }
5764
- tga_image_type = stbi__get8(s); // image type
5765
- if ( tga_colormap_type == 1 ) { // colormapped (paletted) image
5766
- if (tga_image_type != 1 && tga_image_type != 9) {
5767
- stbi__rewind(s);
5768
- return 0;
5769
- }
5770
- stbi__skip(s,4); // skip index of first colormap entry and number of entries
5771
- sz = stbi__get8(s); // check bits per palette color entry
5772
- if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) {
5773
- stbi__rewind(s);
5774
- return 0;
5775
- }
5776
- stbi__skip(s,4); // skip image x and y origin
5777
- tga_colormap_bpp = sz;
5778
- } else { // "normal" image w/o colormap - only RGB or grey allowed, +/- RLE
5779
- if ( (tga_image_type != 2) && (tga_image_type != 3) && (tga_image_type != 10) && (tga_image_type != 11) ) {
5780
- stbi__rewind(s);
5781
- return 0; // only RGB or grey allowed, +/- RLE
5782
- }
5783
- stbi__skip(s,9); // skip colormap specification and image x/y origin
5784
- tga_colormap_bpp = 0;
5785
- }
5786
- tga_w = stbi__get16le(s);
5787
- if( tga_w < 1 ) {
5788
- stbi__rewind(s);
5789
- return 0; // test width
5790
- }
5791
- tga_h = stbi__get16le(s);
5792
- if( tga_h < 1 ) {
5793
- stbi__rewind(s);
5794
- return 0; // test height
5795
- }
5796
- tga_bits_per_pixel = stbi__get8(s); // bits per pixel
5797
- stbi__get8(s); // ignore alpha bits
5798
- if (tga_colormap_bpp != 0) {
5799
- if((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16)) {
5800
- // when using a colormap, tga_bits_per_pixel is the size of the indexes
5801
- // I don't think anything but 8 or 16bit indexes makes sense
5802
- stbi__rewind(s);
5803
- return 0;
5804
- }
5805
- tga_comp = stbi__tga_get_comp(tga_colormap_bpp, 0, NULL);
5806
- } else {
5807
- tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3) || (tga_image_type == 11), NULL);
5808
- }
5809
- if(!tga_comp) {
5810
- stbi__rewind(s);
5811
- return 0;
5812
- }
5813
- if (x) *x = tga_w;
5814
- if (y) *y = tga_h;
5815
- if (comp) *comp = tga_comp;
5816
- return 1; // seems to have passed everything
5797
+ if (is_grey && bits_per_pixel == 16) return STBI_grey_alpha;
5798
+ if (bits_per_pixel == 15 || bits_per_pixel == 16) {
5799
+ if (is_rgb16) *is_rgb16 = 1;
5800
+ return STBI_rgb;
5801
+ }
5802
+
5803
+ switch (bits_per_pixel) {
5804
+ case 8: return STBI_grey;
5805
+ case 24: return STBI_rgb;
5806
+ case 32: return STBI_rgb_alpha;
5807
+ default: return 0;
5808
+ }
5817
5809
  }
5818
5810
 
5819
- static int stbi__tga_test(stbi__context *s)
5811
+ static int stbi__tga_info_impl(stbi__context *s, int *x, int *y, int *comp)
5820
5812
  {
5821
- int res = 0;
5822
- int sz, tga_color_type;
5823
- stbi__get8(s); // discard Offset
5824
- tga_color_type = stbi__get8(s); // color type
5825
- if ( tga_color_type > 1 ) goto errorEnd; // only RGB or indexed allowed
5826
- sz = stbi__get8(s); // image type
5827
- if ( tga_color_type == 1 ) { // colormapped (paletted) image
5828
- if (sz != 1 && sz != 9) goto errorEnd; // colortype 1 demands image type 1 or 9
5813
+ int tga_w, tga_h, tga_comp, tga_image_type, tga_bits_per_pixel, tga_colormap_bpp;
5814
+ int sz, tga_colormap_type;
5815
+ stbi__get8(s); // discard Offset
5816
+ tga_colormap_type = stbi__get8(s); // colormap type
5817
+ if (tga_colormap_type > 1) return 0; // only RGB/indexed allowed
5818
+ tga_image_type = stbi__get8(s); // image type
5819
+ if (tga_colormap_type == 1) { // colormapped (paletted) image
5820
+ if (tga_image_type != 1 && tga_image_type != 9) return 0;
5829
5821
  stbi__skip(s,4); // skip index of first colormap entry and number of entries
5830
5822
  sz = stbi__get8(s); // check bits per palette color entry
5831
- if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) goto errorEnd;
5823
+ if ((sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32)) return 0;
5832
5824
  stbi__skip(s,4); // skip image x and y origin
5833
- } else { // "normal" image w/o colormap
5834
- if ( (sz != 2) && (sz != 3) && (sz != 10) && (sz != 11) ) goto errorEnd; // only RGB or grey allowed, +/- RLE
5825
+ tga_colormap_bpp = sz;
5826
+ } else { // "normal" image w/o colormap - only RGB or grey allowed, +/- RLE
5827
+ if ((tga_image_type != 2) && (tga_image_type != 3) && (tga_image_type != 10) && (tga_image_type != 11)) {
5828
+ return 0; // only RGB or grey allowed, +/- RLE
5829
+ }
5835
5830
  stbi__skip(s,9); // skip colormap specification and image x/y origin
5831
+ tga_colormap_bpp = 0;
5832
+ }
5833
+ tga_w = stbi__get16le(s);
5834
+ tga_h = stbi__get16le(s);
5835
+ if (tga_w < 1 || tga_h < 1) return 0;
5836
+ tga_bits_per_pixel = stbi__get8(s); // bits per pixel
5837
+ stbi__get8(s); // ignore alpha bits
5838
+ if (tga_colormap_bpp != 0) {
5839
+ // only support 8- and 16-bit indexes for colormaps
5840
+ if((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16)) return 0;
5841
+ tga_comp = stbi__tga_get_comp(tga_colormap_bpp, 0, NULL);
5842
+ } else {
5843
+ tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3) || (tga_image_type == 11), NULL);
5836
5844
  }
5837
- if ( stbi__get16le(s) < 1 ) goto errorEnd; // test width
5838
- if ( stbi__get16le(s) < 1 ) goto errorEnd; // test height
5839
- sz = stbi__get8(s); // bits per pixel
5840
- if ( (tga_color_type == 1) && (sz != 8) && (sz != 16) ) goto errorEnd; // for colormapped images, bpp is size of an index
5841
- if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) goto errorEnd;
5845
+ if (!tga_comp) return 0;
5846
+ if (x) *x = tga_w;
5847
+ if (y) *y = tga_h;
5848
+ if (comp) *comp = tga_comp;
5849
+ return 1; // seems to have passed everything
5850
+ }
5842
5851
 
5843
- res = 1; // if we got this far, everything's good and we can return 1 instead of 0
5852
+ static int stbi__tga_info(stbi__context *s, stbi_info_res *res)
5853
+ {
5854
+ if (!stbi__tga_info_impl(s, &res->x, &res->y, &res->comp)) {
5855
+ stbi__rewind(s);
5856
+ return 0;
5857
+ }
5858
+ res->type = STBI_type_tga;
5859
+ return 1;
5860
+ }
5844
5861
 
5845
- errorEnd:
5862
+ static int stbi__tga_test(stbi__context *s)
5863
+ {
5864
+ int res = stbi__tga_info_impl(s, NULL, NULL, NULL);
5846
5865
  stbi__rewind(s);
5847
5866
  return res;
5848
5867
  }
@@ -5869,7 +5888,7 @@ static void stbi__tga_read_rgb16(stbi__context *s, stbi_uc* out)
5869
5888
 
5870
5889
  static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
5871
5890
  {
5872
- // read in the TGA header stuff
5891
+ // read in the TGA header
5873
5892
  int tga_offset = stbi__get8(s);
5874
5893
  int tga_indexed = stbi__get8(s);
5875
5894
  int tga_image_type = stbi__get8(s);
@@ -5883,34 +5902,33 @@ static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req
5883
5902
  int tga_height = stbi__get16le(s);
5884
5903
  int tga_bits_per_pixel = stbi__get8(s);
5885
5904
  int tga_comp, tga_rgb16=0;
5886
- int tga_inverted = stbi__get8(s);
5887
- // int tga_alpha_bits = tga_inverted & 15; // the 4 lowest bits - unused (useless?)
5905
+ int tga_desc = stbi__get8(s);
5888
5906
  // image data
5889
5907
  unsigned char *tga_data;
5890
5908
  unsigned char *tga_palette = NULL;
5891
5909
  int i, j;
5892
- unsigned char raw_data[4] = {0};
5910
+ unsigned char raw_data[4] = {0,0,0,0};
5893
5911
  int RLE_count = 0;
5894
5912
  int RLE_repeating = 0;
5895
5913
  int read_next_pixel = 1;
5896
5914
  STBI_NOTUSED(ri);
5897
- STBI_NOTUSED(tga_x_origin); // @TODO
5898
- STBI_NOTUSED(tga_y_origin); // @TODO
5899
5915
 
5900
- if (tga_height > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
5901
- if (tga_width > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
5916
+ if (tga_width > STBI_MAX_DIMENSIONS || tga_height > STBI_MAX_DIMENSIONS)
5917
+ return stbi__errpuc("too large","Very large image (corrupt?)");
5918
+ if (!tga_width || !tga_height)
5919
+ return stbi__errpuc("zero size","Zero-size image");
5902
5920
 
5903
5921
  // do a tiny bit of precessing
5904
- if ( tga_image_type >= 8 )
5905
- {
5922
+ if (tga_image_type >= 8) {
5906
5923
  tga_image_type -= 8;
5907
5924
  tga_is_RLE = 1;
5908
5925
  }
5909
- tga_inverted = 1 - ((tga_inverted >> 5) & 1);
5910
5926
 
5911
5927
  // If I'm paletted, then I'll use the number of bits from the palette
5912
- if ( tga_indexed ) tga_comp = stbi__tga_get_comp(tga_palette_bits, 0, &tga_rgb16);
5913
- else tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3), &tga_rgb16);
5928
+ if (tga_indexed)
5929
+ tga_comp = stbi__tga_get_comp(tga_palette_bits, 0, &tga_rgb16);
5930
+ else
5931
+ tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3), &tga_rgb16);
5914
5932
 
5915
5933
  if(!tga_comp) // shouldn't really happen, stbi__tga_test() should have ensured basic consistency
5916
5934
  return stbi__errpuc("bad format", "Can't find out TGA pixelformat");
@@ -5927,26 +5945,25 @@ static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req
5927
5945
  if (!tga_data) return stbi__errpuc("outofmem", "Out of memory");
5928
5946
 
5929
5947
  // skip to the data's starting position (offset usually = 0)
5930
- stbi__skip(s, tga_offset );
5948
+ stbi__skip(s, tga_offset);
5931
5949
 
5932
- if ( !tga_indexed && !tga_is_RLE && !tga_rgb16 ) {
5950
+ if (!tga_indexed && !tga_is_RLE && !tga_rgb16) {
5933
5951
  for (i=0; i < tga_height; ++i) {
5934
- int row = tga_inverted ? tga_height -i - 1 : i;
5935
- stbi_uc *tga_row = tga_data + row*tga_width*tga_comp;
5936
- stbi__getn(s, tga_row, tga_width * tga_comp);
5952
+ stbi_uc *tga_row = tga_data + i*tga_width*tga_comp;
5953
+ if (!stbi__getn(s, tga_row, tga_width * tga_comp)) {
5954
+ STBI_FREE(tga_data);
5955
+ return stbi__errpuc("premature EOF", "Corrupt TGA");
5956
+ }
5937
5957
  }
5938
- } else {
5958
+ } else {
5939
5959
  // do I need to load a palette?
5940
- if ( tga_indexed)
5941
- {
5960
+ if (tga_indexed) {
5942
5961
  if (tga_palette_len == 0) { /* you have to have at least one entry! */
5943
5962
  STBI_FREE(tga_data);
5944
5963
  return stbi__errpuc("bad palette", "Corrupt TGA");
5945
5964
  }
5946
5965
 
5947
- // any data to skip? (offset usually = 0)
5948
- stbi__skip(s, tga_palette_start );
5949
- // load the palette
5966
+ stbi__skip(s, tga_palette_start);
5950
5967
  tga_palette = (unsigned char*)stbi__malloc_mad2(tga_palette_len, tga_comp, 0);
5951
5968
  if (!tga_palette) {
5952
5969
  STBI_FREE(tga_data);
@@ -5960,41 +5977,34 @@ static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req
5960
5977
  pal_entry += tga_comp;
5961
5978
  }
5962
5979
  } else if (!stbi__getn(s, tga_palette, tga_palette_len * tga_comp)) {
5963
- STBI_FREE(tga_data);
5964
- STBI_FREE(tga_palette);
5965
- return stbi__errpuc("bad palette", "Corrupt TGA");
5980
+ STBI_FREE(tga_data);
5981
+ STBI_FREE(tga_palette);
5982
+ return stbi__errpuc("bad palette", "Corrupt TGA");
5966
5983
  }
5967
5984
  }
5968
5985
  // load the data
5969
- for (i=0; i < tga_width * tga_height; ++i)
5970
- {
5986
+ for (i=0; i < tga_width * tga_height; ++i) {
5971
5987
  // if I'm in RLE mode, do I need to get a RLE stbi__pngchunk?
5972
- if ( tga_is_RLE )
5973
- {
5974
- if ( RLE_count == 0 )
5975
- {
5988
+ if (tga_is_RLE) {
5989
+ if (RLE_count == 0) {
5976
5990
  // yep, get the next byte as a RLE command
5977
5991
  int RLE_cmd = stbi__get8(s);
5978
5992
  RLE_count = 1 + (RLE_cmd & 127);
5979
5993
  RLE_repeating = RLE_cmd >> 7;
5980
5994
  read_next_pixel = 1;
5981
- } else if ( !RLE_repeating )
5982
- {
5995
+ } else if (!RLE_repeating) {
5983
5996
  read_next_pixel = 1;
5984
5997
  }
5985
- } else
5986
- {
5998
+ } else {
5987
5999
  read_next_pixel = 1;
5988
6000
  }
5989
6001
  // OK, if I need to read a pixel, do it now
5990
- if ( read_next_pixel )
5991
- {
6002
+ if (read_next_pixel) {
5992
6003
  // load however much data we did have
5993
- if ( tga_indexed )
5994
- {
6004
+ if (tga_indexed) {
5995
6005
  // read in index, then perform the lookup
5996
6006
  int pal_idx = (tga_bits_per_pixel == 8) ? stbi__get8(s) : stbi__get16le(s);
5997
- if ( pal_idx >= tga_palette_len ) {
6007
+ if (pal_idx >= tga_palette_len) {
5998
6008
  // invalid index
5999
6009
  pal_idx = 0;
6000
6010
  }
@@ -6022,36 +6032,37 @@ static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req
6022
6032
  // in case we're in RLE mode, keep counting down
6023
6033
  --RLE_count;
6024
6034
  }
6025
- // do I need to invert the image?
6026
- if ( tga_inverted )
6027
- {
6028
- for (j = 0; j*2 < tga_height; ++j)
6029
- {
6030
- int index1 = j * tga_width * tga_comp;
6031
- int index2 = (tga_height - 1 - j) * tga_width * tga_comp;
6032
- for (i = tga_width * tga_comp; i > 0; --i)
6033
- {
6034
- unsigned char temp = tga_data[index1];
6035
- tga_data[index1] = tga_data[index2];
6036
- tga_data[index2] = temp;
6037
- ++index1;
6038
- ++index2;
6035
+ // clear my palette, if I had one
6036
+ if (tga_palette != NULL) {
6037
+ STBI_FREE(tga_palette);
6038
+ }
6039
+ }
6040
+
6041
+ // horizontal flip if required
6042
+ if (tga_desc & 0x10) {
6043
+ for (i = 0; i < tga_height; ++i) {
6044
+ stbi_uc *row = tga_data + i * tga_width * tga_comp;
6045
+ for (j = 0; j < tga_width/2; ++j) {
6046
+ stbi_uc *p = row + j * tga_comp;
6047
+ stbi_uc *q = row + (tga_width - 1 - j) * tga_comp;
6048
+ int k;
6049
+ for (k = 0; k < tga_comp; ++k) {
6050
+ stbi_uc t = p[k];
6051
+ p[k] = q[k];
6052
+ q[k] = t;
6039
6053
  }
6040
6054
  }
6041
6055
  }
6042
- // clear my palette, if I had one
6043
- if ( tga_palette != NULL )
6044
- {
6045
- STBI_FREE( tga_palette );
6046
- }
6047
6056
  }
6048
6057
 
6058
+ // vertical flip if required
6059
+ if ((tga_desc & 0x20) == 0)
6060
+ stbi__vertical_flip(tga_data, tga_width, tga_height, tga_comp);
6061
+
6049
6062
  // swap RGB - if the source data was RGB16, it already is in the right order
6050
- if (tga_comp >= 3 && !tga_rgb16)
6051
- {
6063
+ if (tga_comp >= 3 && !tga_rgb16) {
6052
6064
  unsigned char* tga_pixel = tga_data;
6053
- for (i=0; i < tga_width * tga_height; ++i)
6054
- {
6065
+ for (i=0; i < tga_width * tga_height; ++i) {
6055
6066
  unsigned char temp = tga_pixel[0];
6056
6067
  tga_pixel[0] = tga_pixel[2];
6057
6068
  tga_pixel[2] = temp;
@@ -6063,11 +6074,11 @@ static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req
6063
6074
  if (req_comp && req_comp != tga_comp)
6064
6075
  tga_data = stbi__convert_format(tga_data, tga_comp, req_comp, tga_width, tga_height);
6065
6076
 
6066
- // the things I do to get rid of an error message, and yet keep
6067
- // Microsoft's C compilers happy... [8^(
6068
- tga_palette_start = tga_palette_len = tga_palette_bits =
6069
- tga_x_origin = tga_y_origin = 0;
6070
- STBI_NOTUSED(tga_palette_start);
6077
+ // these were just read for the side effect
6078
+ STBI_NOTUSED(tga_palette_bits);
6079
+ STBI_NOTUSED(tga_x_origin);
6080
+ STBI_NOTUSED(tga_y_origin);
6081
+
6071
6082
  // OK, done
6072
6083
  return tga_data;
6073
6084
  }
@@ -6325,282 +6336,52 @@ static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req
6325
6336
  #endif
6326
6337
 
6327
6338
  // *************************************************************************************************
6328
- // Softimage PIC loader
6329
- // by Tom Seddon
6330
- //
6331
- // See http://softimage.wiki.softimage.com/index.php/INFO:_PIC_file_format
6332
- // See http://ozviz.wasp.uwa.edu.au/~pbourke/dataformats/softimagepic/
6339
+ // GIF loader -- public domain by Jean-Marc Lienher -- simplified/shrunk by stb
6333
6340
 
6334
- #ifndef STBI_NO_PIC
6335
- static int stbi__pic_is4(stbi__context *s,const char *str)
6341
+ #ifndef STBI_NO_GIF
6342
+ typedef struct
6336
6343
  {
6337
- int i;
6338
- for (i=0; i<4; ++i)
6339
- if (stbi__get8(s) != (stbi_uc)str[i])
6340
- return 0;
6344
+ int w,h;
6345
+ stbi_uc *out; // output buffer (always 4 components)
6346
+ int flags, bgindex, ratio, transparent, eflags;
6347
+ stbi_uc *color_table;
6348
+ int parse, step;
6349
+ int lflags;
6350
+ int start_x, start_y;
6351
+ int max_x, max_y;
6352
+ int cur_x, cur_y;
6353
+ int line_size;
6354
+ int delay;
6355
+ stbi_uc suffix[4096 + 1]; // one extra slot as sentinel
6356
+ stbi__uint16 prefix[4096 + 1]; // one extra slot as sentinel
6357
+ stbi_uc pal[256][3];
6358
+ stbi_uc lpal[256][3];
6359
+ } stbi__gif;
6341
6360
 
6361
+ static int stbi__gif_test_raw(stbi__context *s)
6362
+ {
6363
+ int sz;
6364
+ if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8') return 0;
6365
+ sz = stbi__get8(s);
6366
+ if (sz != '9' && sz != '7') return 0;
6367
+ if (stbi__get8(s) != 'a') return 0;
6342
6368
  return 1;
6343
6369
  }
6344
6370
 
6345
- static int stbi__pic_test_core(stbi__context *s)
6371
+ static int stbi__gif_test(stbi__context *s)
6346
6372
  {
6347
- int i;
6373
+ int r = stbi__gif_test_raw(s);
6374
+ stbi__rewind(s);
6375
+ return r;
6376
+ }
6348
6377
 
6349
- if (!stbi__pic_is4(s,"\x53\x80\xF6\x34"))
6350
- return 0;
6351
-
6352
- for(i=0;i<84;++i)
6353
- stbi__get8(s);
6354
-
6355
- if (!stbi__pic_is4(s,"PICT"))
6356
- return 0;
6357
-
6358
- return 1;
6359
- }
6360
-
6361
- typedef struct
6362
- {
6363
- stbi_uc size,type,channel;
6364
- } stbi__pic_packet;
6365
-
6366
- static stbi_uc *stbi__readval(stbi__context *s, int channel, stbi_uc *dest)
6367
- {
6368
- int mask=0x80, i;
6369
-
6370
- for (i=0; i<4; ++i, mask>>=1) {
6371
- if (channel & mask) {
6372
- if (stbi__at_eof(s)) return stbi__errpuc("bad file","PIC file too short");
6373
- dest[i]=stbi__get8(s);
6374
- }
6375
- }
6376
-
6377
- return dest;
6378
- }
6379
-
6380
- static void stbi__copyval(int channel,stbi_uc *dest,const stbi_uc *src)
6381
- {
6382
- int mask=0x80,i;
6383
-
6384
- for (i=0;i<4; ++i, mask>>=1)
6385
- if (channel&mask)
6386
- dest[i]=src[i];
6387
- }
6388
-
6389
- static stbi_uc *stbi__pic_load_core(stbi__context *s,int width,int height,int *comp, stbi_uc *result)
6390
- {
6391
- int act_comp=0,num_packets=0,y,chained;
6392
- stbi__pic_packet packets[10];
6393
-
6394
- // this will (should...) cater for even some bizarre stuff like having data
6395
- // for the same channel in multiple packets.
6396
- do {
6397
- stbi__pic_packet *packet;
6398
-
6399
- if (num_packets==sizeof(packets)/sizeof(packets[0]))
6400
- return stbi__errpuc("bad format","too many packets");
6401
-
6402
- packet = &packets[num_packets++];
6403
-
6404
- chained = stbi__get8(s);
6405
- packet->size = stbi__get8(s);
6406
- packet->type = stbi__get8(s);
6407
- packet->channel = stbi__get8(s);
6408
-
6409
- act_comp |= packet->channel;
6410
-
6411
- if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (reading packets)");
6412
- if (packet->size != 8) return stbi__errpuc("bad format","packet isn't 8bpp");
6413
- } while (chained);
6414
-
6415
- *comp = (act_comp & 0x10 ? 4 : 3); // has alpha channel?
6416
-
6417
- for(y=0; y<height; ++y) {
6418
- int packet_idx;
6419
-
6420
- for(packet_idx=0; packet_idx < num_packets; ++packet_idx) {
6421
- stbi__pic_packet *packet = &packets[packet_idx];
6422
- stbi_uc *dest = result+y*width*4;
6423
-
6424
- switch (packet->type) {
6425
- default:
6426
- return stbi__errpuc("bad format","packet has bad compression type");
6427
-
6428
- case 0: {//uncompressed
6429
- int x;
6430
-
6431
- for(x=0;x<width;++x, dest+=4)
6432
- if (!stbi__readval(s,packet->channel,dest))
6433
- return 0;
6434
- break;
6435
- }
6436
-
6437
- case 1://Pure RLE
6438
- {
6439
- int left=width, i;
6440
-
6441
- while (left>0) {
6442
- stbi_uc count,value[4];
6443
-
6444
- count=stbi__get8(s);
6445
- if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pure read count)");
6446
-
6447
- if (count > left)
6448
- count = (stbi_uc) left;
6449
-
6450
- if (!stbi__readval(s,packet->channel,value)) return 0;
6451
-
6452
- for(i=0; i<count; ++i,dest+=4)
6453
- stbi__copyval(packet->channel,dest,value);
6454
- left -= count;
6455
- }
6456
- }
6457
- break;
6458
-
6459
- case 2: {//Mixed RLE
6460
- int left=width;
6461
- while (left>0) {
6462
- int count = stbi__get8(s), i;
6463
- if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (mixed read count)");
6464
-
6465
- if (count >= 128) { // Repeated
6466
- stbi_uc value[4];
6467
-
6468
- if (count==128)
6469
- count = stbi__get16be(s);
6470
- else
6471
- count -= 127;
6472
- if (count > left)
6473
- return stbi__errpuc("bad file","scanline overrun");
6474
-
6475
- if (!stbi__readval(s,packet->channel,value))
6476
- return 0;
6477
-
6478
- for(i=0;i<count;++i, dest += 4)
6479
- stbi__copyval(packet->channel,dest,value);
6480
- } else { // Raw
6481
- ++count;
6482
- if (count>left) return stbi__errpuc("bad file","scanline overrun");
6483
-
6484
- for(i=0;i<count;++i, dest+=4)
6485
- if (!stbi__readval(s,packet->channel,dest))
6486
- return 0;
6487
- }
6488
- left-=count;
6489
- }
6490
- break;
6491
- }
6492
- }
6493
- }
6494
- }
6495
-
6496
- return result;
6497
- }
6498
-
6499
- static void *stbi__pic_load(stbi__context *s,int *px,int *py,int *comp,int req_comp, stbi__result_info *ri)
6500
- {
6501
- stbi_uc *result;
6502
- int i, x,y, internal_comp;
6503
- STBI_NOTUSED(ri);
6504
-
6505
- if (!comp) comp = &internal_comp;
6506
-
6507
- for (i=0; i<92; ++i)
6508
- stbi__get8(s);
6509
-
6510
- x = stbi__get16be(s);
6511
- y = stbi__get16be(s);
6512
-
6513
- if (y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
6514
- if (x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
6515
-
6516
- if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pic header)");
6517
- if (!stbi__mad3sizes_valid(x, y, 4, 0)) return stbi__errpuc("too large", "PIC image too large to decode");
6518
-
6519
- stbi__get32be(s); //skip `ratio'
6520
- stbi__get16be(s); //skip `fields'
6521
- stbi__get16be(s); //skip `pad'
6522
-
6523
- // intermediate buffer is RGBA
6524
- result = (stbi_uc *) stbi__malloc_mad3(x, y, 4, 0);
6525
- if (!result) return stbi__errpuc("outofmem", "Out of memory");
6526
- memset(result, 0xff, x*y*4);
6527
-
6528
- if (!stbi__pic_load_core(s,x,y,comp, result)) {
6529
- STBI_FREE(result);
6530
- result=0;
6531
- }
6532
- *px = x;
6533
- *py = y;
6534
- if (req_comp == 0) req_comp = *comp;
6535
- result=stbi__convert_format(result,4,req_comp,x,y);
6536
-
6537
- return result;
6538
- }
6539
-
6540
- static int stbi__pic_test(stbi__context *s)
6541
- {
6542
- int r = stbi__pic_test_core(s);
6543
- stbi__rewind(s);
6544
- return r;
6545
- }
6546
- #endif
6547
-
6548
- // *************************************************************************************************
6549
- // GIF loader -- public domain by Jean-Marc Lienher -- simplified/shrunk by stb
6550
-
6551
- #ifndef STBI_NO_GIF
6552
- typedef struct
6553
- {
6554
- stbi__int16 prefix;
6555
- stbi_uc first;
6556
- stbi_uc suffix;
6557
- } stbi__gif_lzw;
6558
-
6559
- typedef struct
6560
- {
6561
- int w,h;
6562
- stbi_uc *out; // output buffer (always 4 components)
6563
- stbi_uc *background; // The current "background" as far as a gif is concerned
6564
- stbi_uc *history;
6565
- int flags, bgindex, ratio, transparent, eflags;
6566
- stbi_uc pal[256][4];
6567
- stbi_uc lpal[256][4];
6568
- stbi__gif_lzw codes[8192];
6569
- stbi_uc *color_table;
6570
- int parse, step;
6571
- int lflags;
6572
- int start_x, start_y;
6573
- int max_x, max_y;
6574
- int cur_x, cur_y;
6575
- int line_size;
6576
- int delay;
6577
- } stbi__gif;
6578
-
6579
- static int stbi__gif_test_raw(stbi__context *s)
6580
- {
6581
- int sz;
6582
- if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8') return 0;
6583
- sz = stbi__get8(s);
6584
- if (sz != '9' && sz != '7') return 0;
6585
- if (stbi__get8(s) != 'a') return 0;
6586
- return 1;
6587
- }
6588
-
6589
- static int stbi__gif_test(stbi__context *s)
6590
- {
6591
- int r = stbi__gif_test_raw(s);
6592
- stbi__rewind(s);
6593
- return r;
6594
- }
6595
-
6596
- static void stbi__gif_parse_colortable(stbi__context *s, stbi_uc pal[256][4], int num_entries, int transp)
6378
+ static void stbi__gif_parse_colortable(stbi__context *s, stbi_uc pal[256][3], int num_entries)
6597
6379
  {
6598
6380
  int i;
6599
6381
  for (i=0; i < num_entries; ++i) {
6600
- pal[i][2] = stbi__get8(s);
6601
- pal[i][1] = stbi__get8(s);
6602
6382
  pal[i][0] = stbi__get8(s);
6603
- pal[i][3] = transp == i ? 0 : 255;
6383
+ pal[i][1] = stbi__get8(s);
6384
+ pal[i][2] = stbi__get8(s);
6604
6385
  }
6605
6386
  }
6606
6387
 
@@ -6622,15 +6403,19 @@ static int stbi__gif_header(stbi__context *s, stbi__gif *g, int *comp, int is_in
6622
6403
  g->ratio = stbi__get8(s);
6623
6404
  g->transparent = -1;
6624
6405
 
6625
- if (g->w > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)");
6626
- if (g->h > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)");
6406
+ if (g->w > STBI_MAX_DIMENSIONS || g->h > STBI_MAX_DIMENSIONS)
6407
+ return stbi__err("too large","Very large image (corrupt?)");
6408
+ if (!g->w || !g->h)
6409
+ return stbi__err("zero pixels","Zero-pixel image");
6627
6410
 
6628
6411
  if (comp != 0) *comp = 4; // can't actually tell whether it's 3 or 4 until we parse the comments
6629
6412
 
6630
6413
  if (is_info) return 1;
6631
6414
 
6632
6415
  if (g->flags & 0x80)
6633
- stbi__gif_parse_colortable(s,g->pal, 2 << (g->flags & 7), -1);
6416
+ stbi__gif_parse_colortable(s,g->pal, 2 << (g->flags & 7));
6417
+ else
6418
+ g->bgindex = -1; // no global color table -> bg color ignored
6634
6419
 
6635
6420
  return 1;
6636
6421
  }
@@ -6650,50 +6435,14 @@ static int stbi__gif_info_raw(stbi__context *s, int *x, int *y, int *comp)
6650
6435
  return 1;
6651
6436
  }
6652
6437
 
6653
- static void stbi__out_gif_code(stbi__gif *g, stbi__uint16 code)
6654
- {
6655
- stbi_uc *p, *c;
6656
- int idx;
6657
-
6658
- // recurse to decode the prefixes, since the linked-list is backwards,
6659
- // and working backwards through an interleaved image would be nasty
6660
- if (g->codes[code].prefix >= 0)
6661
- stbi__out_gif_code(g, g->codes[code].prefix);
6662
-
6663
- if (g->cur_y >= g->max_y) return;
6664
-
6665
- idx = g->cur_x + g->cur_y;
6666
- p = &g->out[idx];
6667
- g->history[idx / 4] = 1;
6668
-
6669
- c = &g->color_table[g->codes[code].suffix * 4];
6670
- if (c[3] > 128) { // don't render transparent pixels;
6671
- p[0] = c[2];
6672
- p[1] = c[1];
6673
- p[2] = c[0];
6674
- p[3] = c[3];
6675
- }
6676
- g->cur_x += 4;
6677
-
6678
- if (g->cur_x >= g->max_x) {
6679
- g->cur_x = g->start_x;
6680
- g->cur_y += g->step;
6681
-
6682
- while (g->cur_y >= g->max_y && g->parse > 0) {
6683
- g->step = (1 << g->parse) * g->line_size;
6684
- g->cur_y = g->start_y + (g->step >> 1);
6685
- --g->parse;
6686
- }
6687
- }
6688
- }
6689
-
6690
6438
  static stbi_uc *stbi__process_gif_raster(stbi__context *s, stbi__gif *g)
6691
6439
  {
6692
6440
  stbi_uc lzw_cs;
6693
6441
  stbi__int32 len, init_code;
6694
6442
  stbi__uint32 first;
6695
- stbi__int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear;
6696
- stbi__gif_lzw *p;
6443
+ stbi__int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear, cur;
6444
+ stbi_uc revbuf[4096];
6445
+ int revind;
6697
6446
 
6698
6447
  lzw_cs = stbi__get8(s);
6699
6448
  if (lzw_cs > 12) return NULL;
@@ -6704,9 +6453,8 @@ static stbi_uc *stbi__process_gif_raster(stbi__context *s, stbi__gif *g)
6704
6453
  bits = 0;
6705
6454
  valid_bits = 0;
6706
6455
  for (init_code = 0; init_code < clear; init_code++) {
6707
- g->codes[init_code].prefix = -1;
6708
- g->codes[init_code].first = (stbi_uc) init_code;
6709
- g->codes[init_code].suffix = (stbi_uc) init_code;
6456
+ g->prefix[init_code] = 0;
6457
+ g->suffix[init_code] = (stbi_uc)init_code;
6710
6458
  }
6711
6459
 
6712
6460
  // support no starting clear code
@@ -6715,71 +6463,109 @@ static stbi_uc *stbi__process_gif_raster(stbi__context *s, stbi__gif *g)
6715
6463
 
6716
6464
  len = 0;
6717
6465
  for(;;) {
6718
- if (valid_bits < codesize) {
6466
+ stbi__int32 code;
6467
+ while (valid_bits < codesize) {
6719
6468
  if (len == 0) {
6720
6469
  len = stbi__get8(s); // start new block
6721
6470
  if (len == 0)
6722
6471
  return g->out;
6723
6472
  }
6724
6473
  --len;
6725
- bits |= (stbi__int32) stbi__get8(s) << valid_bits;
6474
+ bits |= (stbi__int32)stbi__get8(s) << valid_bits;
6726
6475
  valid_bits += 8;
6727
- } else {
6728
- stbi__int32 code = bits & codemask;
6729
- bits >>= codesize;
6730
- valid_bits -= codesize;
6731
- // @OPTIMIZE: is there some way we can accelerate the non-clear path?
6732
- if (code == clear) { // clear code
6733
- codesize = lzw_cs + 1;
6734
- codemask = (1 << codesize) - 1;
6735
- avail = clear + 2;
6736
- oldcode = -1;
6737
- first = 0;
6738
- } else if (code == clear + 1) { // end of stream code
6739
- stbi__skip(s, len);
6740
- while ((len = stbi__get8(s)) > 0)
6741
- stbi__skip(s,len);
6742
- return g->out;
6743
- } else if (code <= avail) {
6744
- if (first) {
6745
- return stbi__errpuc("no clear code", "Corrupt GIF");
6746
- }
6476
+ }
6747
6477
 
6748
- if (oldcode >= 0) {
6749
- p = &g->codes[avail++];
6750
- if (avail > 8192) {
6751
- return stbi__errpuc("too many codes", "Corrupt GIF");
6752
- }
6478
+ code = bits & codemask;
6479
+ bits >>= codesize;
6480
+ valid_bits -= codesize;
6481
+ // @OPTIMIZE: is there some way we can accelerate the non-clear path?
6482
+ if (code == clear) { // clear code
6483
+ codesize = lzw_cs + 1;
6484
+ codemask = (1 << codesize) - 1;
6485
+ avail = clear + 2;
6486
+ oldcode = -1;
6487
+ first = 0;
6488
+ } else if (code == clear + 1) { // end of stream code
6489
+ stbi__skip(s, len);
6490
+ while ((len = stbi__get8(s)) > 0)
6491
+ stbi__skip(s,len);
6492
+ return g->out;
6493
+ } else if (code <= avail) {
6494
+ if (first) {
6495
+ return stbi__errpuc("no clear code", "Corrupt GIF");
6496
+ }
6753
6497
 
6754
- p->prefix = (stbi__int16) oldcode;
6755
- p->first = g->codes[oldcode].first;
6756
- p->suffix = (code == avail) ? p->first : g->codes[code].first;
6757
- } else if (code == avail)
6498
+ revind = 4096;
6499
+ cur = code;
6500
+ if (code == avail) { // self-reference case
6501
+ if (oldcode < 0)
6758
6502
  return stbi__errpuc("illegal code in raster", "Corrupt GIF");
6503
+ // chase oldcode to find its first character
6504
+ cur = oldcode;
6505
+ while (cur >= clear)
6506
+ cur = g->prefix[cur];
6507
+ // add the first character at the end of revbuf; consumes 1 slot
6508
+ revbuf[--revind] = (stbi_uc)cur;
6509
+ cur = oldcode;
6510
+ }
6511
+ // walk linked list of prefixes to emit bytes in reverse order to revbuf
6512
+ // safe: longest chain possible can't contain more than 4096 - (clear + 2) <= 4094 bytes,
6513
+ // since every code below clear+2 terminates the chain and any code above has a
6514
+ // "prefix" link to a lower-numbered code
6515
+ for (; cur >= clear; cur = g->prefix[cur])
6516
+ revbuf[--revind] = g->suffix[cur];
6517
+
6518
+ // one final slot in revbuf: still guaranteed to be <=4096 bytes, so revind >= 0 at the end
6519
+ revbuf[--revind] = (stbi_uc)cur;
6520
+ if (oldcode >= 0) {
6521
+ g->prefix[avail] = (stbi__uint16)oldcode;
6522
+ g->suffix[avail] = (stbi_uc)cur;
6523
+ if (avail < 4096) { // increment avail unless dict is already full
6524
+ ++avail;
6525
+ if (avail > codemask && avail < 4096) {
6526
+ codesize++;
6527
+ codemask = (1 << codesize) - 1;
6528
+ }
6529
+ }
6530
+ }
6531
+ oldcode = code;
6532
+
6533
+ // turn decoded bytes into pixels
6534
+ for (; revind < 4096 && g->cur_y < g->max_y; ++revind) {
6535
+ stbi_uc col = revbuf[revind];
6536
+ if (col != g->transparent) {
6537
+ stbi_uc *c = &g->color_table[col * 3];
6538
+ stbi_uc *p = &g->out[g->cur_x + g->cur_y];
6539
+ p[0] = c[0];
6540
+ p[1] = c[1];
6541
+ p[2] = c[2];
6542
+ p[3] = 255;
6543
+ }
6544
+ g->cur_x += 4;
6759
6545
 
6760
- stbi__out_gif_code(g, (stbi__uint16) code);
6546
+ if (g->cur_x >= g->max_x) {
6547
+ g->cur_x = g->start_x;
6548
+ g->cur_y += g->step;
6761
6549
 
6762
- if ((avail & codemask) == 0 && avail <= 0x0FFF) {
6763
- codesize++;
6764
- codemask = (1 << codesize) - 1;
6550
+ while (g->cur_y >= g->max_y && g->parse > 0) {
6551
+ g->step = (1 << g->parse) * g->line_size;
6552
+ g->cur_y = g->start_y + (g->step >> 1);
6553
+ --g->parse;
6554
+ }
6765
6555
  }
6766
-
6767
- oldcode = code;
6768
- } else {
6769
- return stbi__errpuc("illegal code in raster", "Corrupt GIF");
6770
6556
  }
6557
+ } else {
6558
+ return stbi__errpuc("illegal code in raster", "Corrupt GIF");
6771
6559
  }
6772
6560
  }
6773
6561
  }
6774
6562
 
6775
6563
  // this function is designed to support animated gifs, although stb_image doesn't support it
6776
6564
  // two back is the image from two frames ago, used for a very specific disposal format
6777
- static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, int req_comp, stbi_uc *two_back)
6565
+ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, int req_comp, stbi_uc *saved)
6778
6566
  {
6779
- int dispose;
6780
- int first_frame;
6781
- int pi;
6782
- int pcount;
6567
+ int dispose, first_frame;
6568
+ int x, y, w, h;
6783
6569
  STBI_NOTUSED(req_comp);
6784
6570
 
6785
6571
  // on first frame, any non-written pixels get the background colour (non-transparent)
@@ -6788,64 +6574,44 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i
6788
6574
  if (!stbi__gif_header(s, g, comp,0)) return 0; // stbi__g_failure_reason set by stbi__gif_header
6789
6575
  if (!stbi__mad3sizes_valid(4, g->w, g->h, 0))
6790
6576
  return stbi__errpuc("too large", "GIF image is too large");
6791
- pcount = g->w * g->h;
6792
- g->out = (stbi_uc *) stbi__malloc(4 * pcount);
6793
- g->background = (stbi_uc *) stbi__malloc(4 * pcount);
6794
- g->history = (stbi_uc *) stbi__malloc(pcount);
6795
- if (!g->out || !g->background || !g->history)
6577
+ g->out = (stbi_uc *) stbi__malloc(4 * g->w * g->h);
6578
+ if (!g->out)
6796
6579
  return stbi__errpuc("outofmem", "Out of memory");
6797
6580
 
6798
- // image is treated as "transparent" at the start - ie, nothing overwrites the current background;
6799
- // background colour is only used for pixels that are not rendered first frame, after that "background"
6800
- // color refers to the color that was there the previous frame.
6801
- memset(g->out, 0x00, 4 * pcount);
6802
- memset(g->background, 0x00, 4 * pcount); // state of the background (starts transparent)
6803
- memset(g->history, 0x00, pcount); // pixels that were affected previous frame
6804
6581
  first_frame = 1;
6582
+ memset(g->out, 0, 4 * g->w * g->h); // clear to transparent
6805
6583
  } else {
6806
6584
  // second frame - how do we dispose of the previous one?
6807
6585
  dispose = (g->eflags & 0x1C) >> 2;
6808
- pcount = g->w * g->h;
6809
-
6810
- if ((dispose == 3) && (two_back == 0)) {
6586
+ if ((dispose == 3) && !saved) {
6811
6587
  dispose = 2; // if I don't have an image to revert back to, default to the old background
6812
6588
  }
6813
6589
 
6814
6590
  if (dispose == 3) { // use previous graphic
6815
- for (pi = 0; pi < pcount; ++pi) {
6816
- if (g->history[pi]) {
6817
- memcpy( &g->out[pi * 4], &two_back[pi * 4], 4 );
6818
- }
6591
+ for (y = g->start_y; y < g->max_y; y += g->line_size) {
6592
+ memcpy(&g->out[y + g->start_x], &saved[y + g->start_x], g->max_x - g->start_x);
6819
6593
  }
6820
- } else if (dispose == 2) {
6821
- // restore what was changed last frame to background before that frame;
6822
- for (pi = 0; pi < pcount; ++pi) {
6823
- if (g->history[pi]) {
6824
- memcpy( &g->out[pi * 4], &g->background[pi * 4], 4 );
6594
+ } else if (dispose == 2) { // use background color
6595
+ for (y = g->start_y; y < g->max_y; y += g->line_size) {
6596
+ stbi_uc bg[4] = { 0, 0, 0, 0 };
6597
+
6598
+ if (g->bgindex >= 0 && g->transparent < 0) {
6599
+ memcpy(bg, g->pal[g->bgindex], 3);
6600
+ bg[3] = 255;
6825
6601
  }
6602
+ for (x = g->start_x; x < g->max_x; ++x)
6603
+ g->out[y + x] = bg[x & 3];
6826
6604
  }
6827
6605
  } else {
6828
- // This is a non-disposal case eithe way, so just
6829
- // leave the pixels as is, and they will become the new background
6830
- // 1: do not dispose
6831
- // 0: not specified.
6606
+ // This is a non-disposal case either way, just leave the pixels as they are.
6832
6607
  }
6833
-
6834
- // background is what out is after the undoing of the previou frame;
6835
- memcpy( g->background, g->out, 4 * g->w * g->h );
6836
6608
  }
6837
6609
 
6838
- // clear my history;
6839
- memset( g->history, 0x00, g->w * g->h ); // pixels that were affected previous frame
6840
-
6841
6610
  for (;;) {
6842
6611
  int tag = stbi__get8(s);
6843
6612
  switch (tag) {
6844
6613
  case 0x2C: /* Image Descriptor */
6845
6614
  {
6846
- stbi__int32 x, y, w, h;
6847
- stbi_uc *o;
6848
-
6849
6615
  x = stbi__get16le(s);
6850
6616
  y = stbi__get16le(s);
6851
6617
  w = stbi__get16le(s);
@@ -6879,29 +6645,24 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i
6879
6645
  }
6880
6646
 
6881
6647
  if (g->lflags & 0x80) {
6882
- stbi__gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1);
6648
+ stbi__gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7));
6883
6649
  g->color_table = (stbi_uc *) g->lpal;
6884
6650
  } else if (g->flags & 0x80) {
6885
6651
  g->color_table = (stbi_uc *) g->pal;
6886
6652
  } else
6887
6653
  return stbi__errpuc("missing color table", "Corrupt GIF");
6888
6654
 
6889
- o = stbi__process_gif_raster(s, g);
6890
- if (!o) return NULL;
6891
-
6892
- // if this was the first frame,
6893
- pcount = g->w * g->h;
6894
- if (first_frame && (g->bgindex > 0)) {
6895
- // if first frame, any pixel not drawn to gets the background color
6896
- for (pi = 0; pi < pcount; ++pi) {
6897
- if (g->history[pi] == 0) {
6898
- g->pal[g->bgindex][3] = 255; // just in case it was made transparent, undo that; It will be reset next frame if need be;
6899
- memcpy( &g->out[pi * 4], &g->pal[g->bgindex], 4 );
6900
- }
6901
- }
6655
+ // if we have a background color set and no transparent color, first frame should clear to background
6656
+ // first frame needs to initialize the image
6657
+ if (first_frame && g->bgindex >= 0 && g->transparent < 0) {
6658
+ stbi_uc bg[4] = { 0, 0, 0, 255 };
6659
+ for (x = 0; x < 3; ++x)
6660
+ bg[x] = g->pal[g->bgindex][x];
6661
+ for (x = 0; x < g->h * g->line_size; ++x)
6662
+ g->out[x] = bg[x & 3];
6902
6663
  }
6903
6664
 
6904
- return o;
6665
+ return stbi__process_gif_raster(s, g);
6905
6666
  }
6906
6667
 
6907
6668
  case 0x21: // Comment Extension.
@@ -6914,20 +6675,9 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i
6914
6675
  g->eflags = stbi__get8(s);
6915
6676
  g->delay = 10 * stbi__get16le(s); // delay - 1/100th of a second, saving as 1/1000ths.
6916
6677
 
6917
- // unset old transparent
6918
- if (g->transparent >= 0) {
6919
- g->pal[g->transparent][3] = 255;
6920
- }
6921
- if (g->eflags & 0x01) {
6922
- g->transparent = stbi__get8(s);
6923
- if (g->transparent >= 0) {
6924
- g->pal[g->transparent][3] = 0;
6925
- }
6926
- } else {
6927
- // don't need transparent
6928
- stbi__skip(s, 1);
6678
+ g->transparent = stbi__get8(s);
6679
+ if ((g->eflags & 0x01) == 0)
6929
6680
  g->transparent = -1;
6930
- }
6931
6681
  } else {
6932
6682
  stbi__skip(s, len);
6933
6683
  break;
@@ -6948,134 +6698,164 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i
6948
6698
  }
6949
6699
  }
6950
6700
 
6951
- static void *stbi__load_gif_main_outofmem(stbi__gif *g, stbi_uc *out, int **delays)
6952
- {
6953
- STBI_FREE(g->out);
6954
- STBI_FREE(g->history);
6955
- STBI_FREE(g->background);
6956
-
6957
- if (out) STBI_FREE(out);
6958
- if (delays && *delays) STBI_FREE(*delays);
6959
- return stbi__errpuc("outofmem", "Out of memory");
6960
- }
6701
+ typedef struct stbi__gif_frame_hdr {
6702
+ struct stbi__gif_frame_hdr *prev; // pointer to previous frame
6703
+ int delay;
6704
+ // pixel data follows after hdr in same allocation
6705
+ } stbi__gif_frame_hdr;
6961
6706
 
6962
6707
  static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp)
6963
6708
  {
6964
- if (stbi__gif_test(s)) {
6965
- int layers = 0;
6966
- stbi_uc *u = 0;
6967
- stbi_uc *out = 0;
6968
- stbi_uc *two_back = 0;
6969
- stbi__gif g;
6970
- int stride;
6971
- int out_size = 0;
6972
- int delays_size = 0;
6709
+ int ok = 1;
6710
+ int layers = 0;
6711
+ int stride = 0;
6712
+ int totalbytes = 0;
6713
+ int i;
6714
+ stbi_uc *out = NULL;
6715
+ stbi_uc *saved_pixels = NULL;
6716
+ stbi__gif_frame_hdr *frames = NULL, *frame;
6717
+ stbi__gif *g;
6718
+
6719
+ // make sure we initialize everything that is passed in so we have defined values if we return
6720
+ *x = 0;
6721
+ *y = 0;
6722
+ *z = 0;
6723
+ *comp = 0;
6724
+ if (delays)
6725
+ *delays = NULL;
6726
+
6727
+ if (!stbi__gif_test(s))
6728
+ return stbi__errpuc("not GIF", "Image is not a GIF.");
6729
+
6730
+ g = (stbi__gif *) stbi__malloc(sizeof(*g));
6731
+ if (!g)
6732
+ return stbi__errpuc("outofmem", "Out of memory");
6733
+ memset(g, 0, sizeof(*g));
6973
6734
 
6974
- STBI_NOTUSED(out_size);
6975
- STBI_NOTUSED(delays_size);
6735
+ for (;;) {
6736
+ stbi_uc *pixels = stbi__gif_load_next(s, g, comp, req_comp, saved_pixels);
6737
+ if (!pixels) {
6738
+ ok = 0;
6739
+ break;
6740
+ }
6741
+ if (pixels == (stbi_uc*)s) // end of animated gif marker
6742
+ break;
6976
6743
 
6977
- memset(&g, 0, sizeof(g));
6978
- if (delays) {
6979
- *delays = 0;
6744
+ *x = g->w;
6745
+ *y = g->h;
6746
+ stride = g->w * g->h * 4;
6747
+
6748
+ // we ensure g.w > 0, g.h > 0; that means totalbytes > 0 and this check
6749
+ // also guarantees that layers doesn't overflow
6750
+ if (!stbi__addints_valid(totalbytes, stride)) {
6751
+ ok = stbi__err("toobig", "Too many bytes in decoded GIF");
6752
+ break;
6753
+ }
6754
+ totalbytes += stride;
6755
+
6756
+ // allocate this frame
6757
+ frame = (stbi__gif_frame_hdr *)stbi__malloc_mad3(g->w, g->h, 4, sizeof(*frame));
6758
+ if (!frame) {
6759
+ ok = stbi__err("outofmem", "Out of memory");
6760
+ break;
6980
6761
  }
6981
6762
 
6982
- do {
6983
- u = stbi__gif_load_next(s, &g, comp, req_comp, two_back);
6984
- if (u == (stbi_uc *) s) u = 0; // end of animated gif marker
6985
-
6986
- if (u) {
6987
- *x = g.w;
6988
- *y = g.h;
6989
- ++layers;
6990
- stride = g.w * g.h * 4;
6991
-
6992
- if (out) {
6993
- void *tmp = (stbi_uc*) STBI_REALLOC_SIZED( out, out_size, layers * stride );
6994
- if (!tmp)
6995
- return stbi__load_gif_main_outofmem(&g, out, delays);
6996
- else {
6997
- out = (stbi_uc*) tmp;
6998
- out_size = layers * stride;
6999
- }
6763
+ frame->prev = frames;
6764
+ frame->delay = g->delay;
6765
+ memcpy(frame + 1, pixels, stride); // pixels go right after frame data
6766
+ frames = frame;
6767
+ ++layers;
7000
6768
 
7001
- if (delays) {
7002
- int *new_delays = (int*) STBI_REALLOC_SIZED( *delays, delays_size, sizeof(int) * layers );
7003
- if (!new_delays)
7004
- return stbi__load_gif_main_outofmem(&g, out, delays);
7005
- *delays = new_delays;
7006
- delays_size = layers * sizeof(int);
7007
- }
7008
- } else {
7009
- out = (stbi_uc*)stbi__malloc( layers * stride );
7010
- if (!out)
7011
- return stbi__load_gif_main_outofmem(&g, out, delays);
7012
- out_size = layers * stride;
7013
- if (delays) {
7014
- *delays = (int*) stbi__malloc( layers * sizeof(int) );
7015
- if (!*delays)
7016
- return stbi__load_gif_main_outofmem(&g, out, delays);
7017
- delays_size = layers * sizeof(int);
7018
- }
7019
- }
7020
- memcpy( out + ((layers - 1) * stride), u, stride );
7021
- if (layers >= 2) {
7022
- two_back = out - 2 * stride;
7023
- }
6769
+ // if this frame is not in "restore to previous" disposal mode (which "un-paints" it),
6770
+ // it becomes the next reference frame
6771
+ if ((g->eflags & 0x1c) != 0x0c)
6772
+ saved_pixels = (stbi_uc *) (frame + 1);
6773
+ }
7024
6774
 
7025
- if (delays) {
7026
- (*delays)[layers - 1U] = g.delay;
7027
- }
6775
+ // free temp decode buffer
6776
+ STBI_FREE(g->out);
6777
+
6778
+ // allocate output space
6779
+ if (ok) {
6780
+ if (delays) {
6781
+ *delays = (int *) stbi__malloc_mad2(layers, sizeof(int), 0);
6782
+ ok &= *delays != NULL;
6783
+ }
6784
+
6785
+ if (ok) {
6786
+ out = (stbi_uc *) stbi__malloc_mad2(layers, stride, 0);
6787
+ ok &= out != NULL;
6788
+ }
6789
+ }
6790
+
6791
+ // move all frames over, then free them
6792
+ for (i = layers - 1; i >= 0; --i) {
6793
+ if (delays && *delays)
6794
+ (*delays)[i] = frames->delay;
6795
+ if (out) {
6796
+ memcpy(out + i*stride, frames + 1, stride);
6797
+ if (stbi__vertically_flip_on_load) {
6798
+ stbi__vertical_flip(out + i*stride, g->w, g->h, 4);
7028
6799
  }
7029
- } while (u != 0);
6800
+ }
7030
6801
 
7031
- // free temp buffer;
7032
- STBI_FREE(g.out);
7033
- STBI_FREE(g.history);
7034
- STBI_FREE(g.background);
6802
+ frame = frames->prev;
6803
+ STBI_FREE(frames); // free current frame
6804
+ frames = frame;
6805
+ }
7035
6806
 
7036
- // do the final conversion after loading everything;
7037
- if (req_comp && req_comp != 4)
7038
- out = stbi__convert_format(out, 4, req_comp, layers * g.w, g.h);
6807
+ // do the final conversion after loading everything
6808
+ if (out && req_comp && req_comp != 4)
6809
+ out = stbi__convert_format(out, 4, req_comp, g->w, layers * g->h);
7039
6810
 
7040
- *z = layers;
7041
- return out;
7042
- } else {
7043
- return stbi__errpuc("not GIF", "Image was not as a gif type.");
6811
+ // if conversion failed, also free delays
6812
+ if (!out && delays && *delays) {
6813
+ STBI_FREE(*delays);
6814
+ *delays = NULL;
7044
6815
  }
6816
+
6817
+ STBI_FREE(g);
6818
+ *z = layers;
6819
+ return out;
7045
6820
  }
7046
6821
 
7047
6822
  static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
7048
6823
  {
7049
6824
  stbi_uc *u = 0;
7050
- stbi__gif g;
7051
- memset(&g, 0, sizeof(g));
6825
+ stbi__gif *g = (stbi__gif *) stbi__malloc(sizeof(*g));
6826
+ if (!g)
6827
+ return stbi__errpuc("outofmem", "Out of memory");
6828
+
6829
+ memset(g, 0, sizeof(*g));
6830
+
7052
6831
  STBI_NOTUSED(ri);
7053
6832
 
7054
- u = stbi__gif_load_next(s, &g, comp, req_comp, 0);
6833
+ u = stbi__gif_load_next(s, g, comp, req_comp, 0);
7055
6834
  if (u == (stbi_uc *) s) u = 0; // end of animated gif marker
7056
6835
  if (u) {
7057
- *x = g.w;
7058
- *y = g.h;
6836
+ *x = g->w;
6837
+ *y = g->h;
7059
6838
 
7060
6839
  // moved conversion to after successful load so that the same
7061
6840
  // can be done for multiple frames.
7062
6841
  if (req_comp && req_comp != 4)
7063
- u = stbi__convert_format(u, 4, req_comp, g.w, g.h);
7064
- } else if (g.out) {
6842
+ u = stbi__convert_format(u, 4, req_comp, g->w, g->h);
6843
+ } else if (g->out) {
7065
6844
  // if there was an error and we allocated an image buffer, free it!
7066
- STBI_FREE(g.out);
6845
+ STBI_FREE(g->out);
7067
6846
  }
7068
-
7069
- // free buffers needed for multiple frame loading;
7070
- STBI_FREE(g.history);
7071
- STBI_FREE(g.background);
6847
+ STBI_FREE(g);
7072
6848
 
7073
6849
  return u;
7074
6850
  }
7075
6851
 
7076
- static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp)
6852
+ static int stbi__gif_info(stbi__context *s, stbi_info_res *res)
7077
6853
  {
7078
- return stbi__gif_info_raw(s,x,y,comp);
6854
+ if (stbi__gif_info_raw(s,&res->x,&res->y,&res->comp)) {
6855
+ res->type = STBI_type_gif;
6856
+ return 1;
6857
+ }
6858
+ return 0;
7079
6859
  }
7080
6860
  #endif
7081
6861
 
@@ -7127,31 +6907,93 @@ static char *stbi__hdr_gettoken(stbi__context *z, char *buffer)
7127
6907
  return buffer;
7128
6908
  }
7129
6909
 
7130
- static void stbi__hdr_convert(float *output, stbi_uc *input, int req_comp)
6910
+ static void stbi__hdr_makelut(float explut[256], int req_comp)
7131
6911
  {
7132
- if ( input[3] != 0 ) {
7133
- float f1;
7134
- // Exponent
7135
- f1 = (float) ldexp(1.0f, input[3] - (int)(128 + 8));
7136
- if (req_comp <= 2)
7137
- output[0] = (input[0] + input[1] + input[2]) * f1 / 3;
7138
- else {
7139
- output[0] = input[0] * f1;
7140
- output[1] = input[1] * f1;
7141
- output[2] = input[2] * f1;
7142
- }
6912
+ int k;
6913
+
6914
+ explut[0] = 0.0f; // exponent 0 always gives 0.0f
6915
+ for (k = 1; k < 256; ++k) {
6916
+ explut[k] = (float) ldexp(1.0, k - (128 + 8));
6917
+ // when req_comp <= 2, we convert to grayscale instead by averaging the three channels;
6918
+ // fold the /3 into the scale factor
6919
+ if (req_comp <= 2) explut[k] /= 3.f;
6920
+ }
6921
+ }
6922
+
6923
+ static void stbi__hdr_convert(float *output, stbi_uc *input, int req_comp, float explut[256])
6924
+ {
6925
+ float scale = explut[input[3]];
6926
+ if (req_comp <= 2) {
6927
+ output[0] = (input[0] + input[1] + input[2] + 1.5f) * scale;
7143
6928
  if (req_comp == 2) output[1] = 1;
7144
- if (req_comp == 4) output[3] = 1;
7145
6929
  } else {
7146
- switch (req_comp) {
7147
- case 4: output[3] = 1; /* fallthrough */
7148
- case 3: output[0] = output[1] = output[2] = 0;
7149
- break;
7150
- case 2: output[1] = 1; /* fallthrough */
7151
- case 1: output[0] = 0;
7152
- break;
6930
+ output[0] = (input[0] + 0.5f) * scale;
6931
+ output[1] = (input[1] + 0.5f) * scale;
6932
+ output[2] = (input[2] + 0.5f) * scale;
6933
+ if (req_comp == 4) output[3] = 1;
6934
+ }
6935
+ }
6936
+
6937
+ static int stbi__hdr_load_pixels(stbi__context *s, float *hdr_data, int width, int height, int req_comp, int isrle, stbi_uc *scanline)
6938
+ {
6939
+ int x,y,k;
6940
+ stbi_uc rgbe[4];
6941
+ stbi_uc count,value;
6942
+ float explut[256];
6943
+
6944
+ stbi__hdr_makelut(explut, req_comp);
6945
+
6946
+ // image data is stored as some number of scanlines
6947
+ for (y = 0; y < height; ++y) {
6948
+ rgbe[0] = stbi__get8(s);
6949
+ rgbe[1] = stbi__get8(s);
6950
+ rgbe[2] = stbi__get8(s);
6951
+ rgbe[3] = stbi__get8(s);
6952
+ if (!isrle || rgbe[0] != 2 || rgbe[1] != 2 || (rgbe[2] & 0x80)) {
6953
+ // data is not RLE, this was the first pixel
6954
+ isrle = 0;
6955
+ x = 0;
6956
+ for (;;) {
6957
+ stbi__hdr_convert(hdr_data+(y*width + x)*req_comp, rgbe, req_comp, explut);
6958
+ ++x;
6959
+ if (x >= width) break;
6960
+ rgbe[0] = stbi__get8(s);
6961
+ rgbe[1] = stbi__get8(s);
6962
+ rgbe[2] = stbi__get8(s);
6963
+ rgbe[3] = stbi__get8(s);
6964
+ }
6965
+ continue;
6966
+ }
6967
+ if (((rgbe[2] << 8) | rgbe[3]) != width) return stbi__err("invalid decoded scanline length", "corrupt HDR");
6968
+
6969
+ for (k = 0; k < 4; ++k) {
6970
+ x = 0;
6971
+ while (x < width) {
6972
+ count = stbi__get8(s);
6973
+ if (count > 128) {
6974
+ // Run
6975
+ value = stbi__get8(s);
6976
+ count -= 128;
6977
+ if (count == 0 || width - x < count) return stbi__err("corrupt", "bad RLE data in HDR");
6978
+ while (count--) {
6979
+ scanline[x*4 + k] = value;
6980
+ ++x;
6981
+ }
6982
+ } else {
6983
+ // Dump
6984
+ if (count == 0 || width - x < count) return stbi__err("corrupt", "bad RLE data in HDR");
6985
+ while (count--) {
6986
+ scanline[x*4 + k] = stbi__get8(s);
6987
+ ++x;
6988
+ }
6989
+ }
6990
+ }
7153
6991
  }
6992
+ for (x=0; x < width; ++x)
6993
+ stbi__hdr_convert(hdr_data+(y*width + x)*req_comp, scanline + x*4, req_comp, explut);
7154
6994
  }
6995
+
6996
+ return 1;
7155
6997
  }
7156
6998
 
7157
6999
  static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
@@ -7160,14 +7002,15 @@ static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int re
7160
7002
  char *token;
7161
7003
  int valid = 0;
7162
7004
  int width, height;
7163
- stbi_uc *scanline;
7005
+ stbi_uc *scanline = NULL;
7164
7006
  float *hdr_data;
7165
- int len;
7166
- unsigned char count, value;
7167
- int i, j, k, c1,c2, z;
7007
+ int isrle;
7168
7008
  const char *headerToken;
7169
7009
  STBI_NOTUSED(ri);
7170
7010
 
7011
+ if (req_comp < 0 || req_comp > 4)
7012
+ return stbi__errpf("bad req_comp", "Invalid number of required components");
7013
+
7171
7014
  // Check identifier
7172
7015
  headerToken = stbi__hdr_gettoken(s,buffer);
7173
7016
  if (strcmp(headerToken, "#?RADIANCE") != 0 && strcmp(headerToken, "#?RGBE") != 0)
@@ -7193,8 +7036,9 @@ static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int re
7193
7036
  token += 3;
7194
7037
  width = (int) strtol(token, NULL, 10);
7195
7038
 
7196
- if (height > STBI_MAX_DIMENSIONS) return stbi__errpf("too large","Very large image (corrupt?)");
7197
- if (width > STBI_MAX_DIMENSIONS) return stbi__errpf("too large","Very large image (corrupt?)");
7039
+ if (width <= 0 || width > STBI_MAX_DIMENSIONS || height <= 0 || height > STBI_MAX_DIMENSIONS) {
7040
+ return stbi__errpf("invalid size","Invalid image size (corrupt?)");
7041
+ }
7198
7042
 
7199
7043
  *x = width;
7200
7044
  *y = height;
@@ -7210,91 +7054,33 @@ static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int re
7210
7054
  if (!hdr_data)
7211
7055
  return stbi__errpf("outofmem", "Out of memory");
7212
7056
 
7213
- // Load image data
7214
- // image data is stored as some number of sca
7215
- if ( width < 8 || width >= 32768) {
7216
- // Read flat data
7217
- for (j=0; j < height; ++j) {
7218
- for (i=0; i < width; ++i) {
7219
- stbi_uc rgbe[4];
7220
- main_decode_loop:
7221
- stbi__getn(s, rgbe, 4);
7222
- stbi__hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp);
7223
- }
7057
+ // for sizes where it's legal, assume RLE unless proven otherwise
7058
+ isrle = (width >= 8 && width < 32768);
7059
+ if (isrle) {
7060
+ scanline = (stbi_uc *) stbi__malloc_mad2(width, 4, 0);
7061
+ if (!scanline) {
7062
+ STBI_FREE(hdr_data);
7063
+ return stbi__errpf("outofmem", "Out of memory");
7224
7064
  }
7225
- } else {
7226
- // Read RLE-encoded data
7227
- scanline = NULL;
7228
-
7229
- for (j = 0; j < height; ++j) {
7230
- c1 = stbi__get8(s);
7231
- c2 = stbi__get8(s);
7232
- len = stbi__get8(s);
7233
- if (c1 != 2 || c2 != 2 || (len & 0x80)) {
7234
- // not run-length encoded, so we have to actually use THIS data as a decoded
7235
- // pixel (note this can't be a valid pixel--one of RGB must be >= 128)
7236
- stbi_uc rgbe[4];
7237
- rgbe[0] = (stbi_uc) c1;
7238
- rgbe[1] = (stbi_uc) c2;
7239
- rgbe[2] = (stbi_uc) len;
7240
- rgbe[3] = (stbi_uc) stbi__get8(s);
7241
- stbi__hdr_convert(hdr_data, rgbe, req_comp);
7242
- i = 1;
7243
- j = 0;
7244
- STBI_FREE(scanline);
7245
- goto main_decode_loop; // yes, this makes no sense
7246
- }
7247
- len <<= 8;
7248
- len |= stbi__get8(s);
7249
- if (len != width) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("invalid decoded scanline length", "corrupt HDR"); }
7250
- if (scanline == NULL) {
7251
- scanline = (stbi_uc *) stbi__malloc_mad2(width, 4, 0);
7252
- if (!scanline) {
7253
- STBI_FREE(hdr_data);
7254
- return stbi__errpf("outofmem", "Out of memory");
7255
- }
7256
- }
7065
+ }
7257
7066
 
7258
- for (k = 0; k < 4; ++k) {
7259
- int nleft;
7260
- i = 0;
7261
- while ((nleft = width - i) > 0) {
7262
- count = stbi__get8(s);
7263
- if (count > 128) {
7264
- // Run
7265
- value = stbi__get8(s);
7266
- count -= 128;
7267
- if ((count == 0) || (count > nleft)) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); }
7268
- for (z = 0; z < count; ++z)
7269
- scanline[i++ * 4 + k] = value;
7270
- } else {
7271
- // Dump
7272
- if ((count == 0) || (count > nleft)) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); }
7273
- for (z = 0; z < count; ++z)
7274
- scanline[i++ * 4 + k] = stbi__get8(s);
7275
- }
7276
- }
7277
- }
7278
- for (i=0; i < width; ++i)
7279
- stbi__hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp);
7280
- }
7281
- if (scanline)
7282
- STBI_FREE(scanline);
7067
+ if (!stbi__hdr_load_pixels(s, hdr_data, width, height, req_comp, isrle, scanline))
7068
+ {
7069
+ STBI_FREE(hdr_data);
7070
+ hdr_data = NULL;
7283
7071
  }
7284
7072
 
7073
+ if (scanline)
7074
+ STBI_FREE(scanline);
7075
+
7285
7076
  return hdr_data;
7286
7077
  }
7287
7078
 
7288
- static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp)
7079
+ static int stbi__hdr_info(stbi__context *s, stbi_info_res *res)
7289
7080
  {
7290
7081
  char buffer[STBI__HDR_BUFLEN];
7291
7082
  char *token;
7292
- int valid = 0;
7293
- int dummy;
7294
-
7295
- if (!x) x = &dummy;
7296
- if (!y) y = &dummy;
7297
- if (!comp) comp = &dummy;
7083
+ int valid = 0, x, y;
7298
7084
 
7299
7085
  if (stbi__hdr_test(s) == 0) {
7300
7086
  stbi__rewind( s );
@@ -7317,21 +7103,26 @@ static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp)
7317
7103
  return 0;
7318
7104
  }
7319
7105
  token += 3;
7320
- *y = (int) strtol(token, &token, 10);
7106
+ y = (int) strtol(token, &token, 10);
7321
7107
  while (*token == ' ') ++token;
7322
7108
  if (strncmp(token, "+X ", 3)) {
7323
7109
  stbi__rewind( s );
7324
7110
  return 0;
7325
7111
  }
7326
7112
  token += 3;
7327
- *x = (int) strtol(token, NULL, 10);
7328
- *comp = 3;
7113
+ x = (int) strtol(token, NULL, 10);
7114
+
7115
+ res->type = STBI_type_hdr;
7116
+ res->x = x;
7117
+ res->y = y;
7118
+ res->comp = 3;
7119
+ res->is_hdr = 1;
7329
7120
  return 1;
7330
7121
  }
7331
7122
  #endif // STBI_NO_HDR
7332
7123
 
7333
7124
  #ifndef STBI_NO_BMP
7334
- static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp)
7125
+ static int stbi__bmp_info(stbi__context *s, stbi_info_res *res)
7335
7126
  {
7336
7127
  void *p;
7337
7128
  stbi__bmp_data info;
@@ -7342,25 +7133,21 @@ static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp)
7342
7133
  stbi__rewind( s );
7343
7134
  return 0;
7344
7135
  }
7345
- if (x) *x = s->img_x;
7346
- if (y) *y = s->img_y;
7347
- if (comp) {
7348
- if (info.bpp == 24 && info.ma == 0xff000000)
7349
- *comp = 3;
7350
- else
7351
- *comp = info.ma ? 4 : 3;
7352
- }
7136
+ res->type = STBI_type_bmp;
7137
+ res->x = s->img_x;
7138
+ res->y = s->img_y;
7139
+ if (info.bpp == 24 && info.ma == 0xff000000)
7140
+ res->comp = 3;
7141
+ else
7142
+ res->comp = info.ma ? 4 : 3;
7353
7143
  return 1;
7354
7144
  }
7355
7145
  #endif
7356
7146
 
7357
7147
  #ifndef STBI_NO_PSD
7358
- static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp)
7148
+ static int stbi__psd_info(stbi__context *s, stbi_info_res *res)
7359
7149
  {
7360
- int channelCount, dummy, depth;
7361
- if (!x) x = &dummy;
7362
- if (!y) y = &dummy;
7363
- if (!comp) comp = &dummy;
7150
+ int channelCount, depth, x, y;
7364
7151
  if (stbi__get32be(s) != 0x38425053) {
7365
7152
  stbi__rewind( s );
7366
7153
  return 0;
@@ -7375,8 +7162,8 @@ static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp)
7375
7162
  stbi__rewind( s );
7376
7163
  return 0;
7377
7164
  }
7378
- *y = stbi__get32be(s);
7379
- *x = stbi__get32be(s);
7165
+ y = stbi__get32be(s);
7166
+ x = stbi__get32be(s);
7380
7167
  depth = stbi__get16be(s);
7381
7168
  if (depth != 8 && depth != 16) {
7382
7169
  stbi__rewind( s );
@@ -7386,93 +7173,11 @@ static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp)
7386
7173
  stbi__rewind( s );
7387
7174
  return 0;
7388
7175
  }
7389
- *comp = 4;
7390
- return 1;
7391
- }
7392
-
7393
- static int stbi__psd_is16(stbi__context *s)
7394
- {
7395
- int channelCount, depth;
7396
- if (stbi__get32be(s) != 0x38425053) {
7397
- stbi__rewind( s );
7398
- return 0;
7399
- }
7400
- if (stbi__get16be(s) != 1) {
7401
- stbi__rewind( s );
7402
- return 0;
7403
- }
7404
- stbi__skip(s, 6);
7405
- channelCount = stbi__get16be(s);
7406
- if (channelCount < 0 || channelCount > 16) {
7407
- stbi__rewind( s );
7408
- return 0;
7409
- }
7410
- STBI_NOTUSED(stbi__get32be(s));
7411
- STBI_NOTUSED(stbi__get32be(s));
7412
- depth = stbi__get16be(s);
7413
- if (depth != 16) {
7414
- stbi__rewind( s );
7415
- return 0;
7416
- }
7417
- return 1;
7418
- }
7419
- #endif
7420
-
7421
- #ifndef STBI_NO_PIC
7422
- static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp)
7423
- {
7424
- int act_comp=0,num_packets=0,chained,dummy;
7425
- stbi__pic_packet packets[10];
7426
-
7427
- if (!x) x = &dummy;
7428
- if (!y) y = &dummy;
7429
- if (!comp) comp = &dummy;
7430
-
7431
- if (!stbi__pic_is4(s,"\x53\x80\xF6\x34")) {
7432
- stbi__rewind(s);
7433
- return 0;
7434
- }
7435
-
7436
- stbi__skip(s, 88);
7437
-
7438
- *x = stbi__get16be(s);
7439
- *y = stbi__get16be(s);
7440
- if (stbi__at_eof(s)) {
7441
- stbi__rewind( s);
7442
- return 0;
7443
- }
7444
- if ( (*x) != 0 && (1 << 28) / (*x) < (*y)) {
7445
- stbi__rewind( s );
7446
- return 0;
7447
- }
7448
-
7449
- stbi__skip(s, 8);
7450
-
7451
- do {
7452
- stbi__pic_packet *packet;
7453
-
7454
- if (num_packets==sizeof(packets)/sizeof(packets[0]))
7455
- return 0;
7456
-
7457
- packet = &packets[num_packets++];
7458
- chained = stbi__get8(s);
7459
- packet->size = stbi__get8(s);
7460
- packet->type = stbi__get8(s);
7461
- packet->channel = stbi__get8(s);
7462
- act_comp |= packet->channel;
7463
-
7464
- if (stbi__at_eof(s)) {
7465
- stbi__rewind( s );
7466
- return 0;
7467
- }
7468
- if (packet->size != 8) {
7469
- stbi__rewind( s );
7470
- return 0;
7471
- }
7472
- } while (chained);
7473
-
7474
- *comp = (act_comp & 0x10 ? 4 : 3);
7475
-
7176
+ res->type = STBI_type_psd;
7177
+ res->x = x;
7178
+ res->y = y;
7179
+ res->comp = 4;
7180
+ res->is_16_bit = depth == 16;
7476
7181
  return 1;
7477
7182
  }
7478
7183
  #endif
@@ -7483,10 +7188,6 @@ static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp)
7483
7188
  //
7484
7189
  // PGM: http://netpbm.sourceforge.net/doc/pgm.html
7485
7190
  // PPM: http://netpbm.sourceforge.net/doc/ppm.html
7486
- //
7487
- // Known limitations:
7488
- // Does not support comments in the header section
7489
- // Does not support ASCII image data (formats P2 and P3)
7490
7191
 
7491
7192
  #ifndef STBI_NO_PNM
7492
7193
 
@@ -7495,56 +7196,19 @@ static int stbi__pnm_test(stbi__context *s)
7495
7196
  char p, t;
7496
7197
  p = (char) stbi__get8(s);
7497
7198
  t = (char) stbi__get8(s);
7498
- if (p != 'P' || (t != '5' && t != '6')) {
7199
+ if (p != 'P' || (t != '2' && t != '3' && t != '5' && t != '6')) {
7499
7200
  stbi__rewind( s );
7500
7201
  return 0;
7501
7202
  }
7502
7203
  return 1;
7503
7204
  }
7504
7205
 
7505
- static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
7506
- {
7507
- stbi_uc *out;
7508
- STBI_NOTUSED(ri);
7509
-
7510
- ri->bits_per_channel = stbi__pnm_info(s, (int *)&s->img_x, (int *)&s->img_y, (int *)&s->img_n);
7511
- if (ri->bits_per_channel == 0)
7512
- return 0;
7513
-
7514
- if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
7515
- if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
7516
-
7517
- *x = s->img_x;
7518
- *y = s->img_y;
7519
- if (comp) *comp = s->img_n;
7520
-
7521
- if (!stbi__mad4sizes_valid(s->img_n, s->img_x, s->img_y, ri->bits_per_channel / 8, 0))
7522
- return stbi__errpuc("too large", "PNM too large");
7523
-
7524
- out = (stbi_uc *) stbi__malloc_mad4(s->img_n, s->img_x, s->img_y, ri->bits_per_channel / 8, 0);
7525
- if (!out) return stbi__errpuc("outofmem", "Out of memory");
7526
- if (!stbi__getn(s, out, s->img_n * s->img_x * s->img_y * (ri->bits_per_channel / 8))) {
7527
- STBI_FREE(out);
7528
- return stbi__errpuc("bad PNM", "PNM file truncated");
7529
- }
7530
-
7531
- if (req_comp && req_comp != s->img_n) {
7532
- if (ri->bits_per_channel == 16) {
7533
- out = (stbi_uc *) stbi__convert_format16((stbi__uint16 *) out, s->img_n, req_comp, s->img_x, s->img_y);
7534
- } else {
7535
- out = stbi__convert_format(out, s->img_n, req_comp, s->img_x, s->img_y);
7536
- }
7537
- if (out == NULL) return out; // stbi__convert_format frees input on failure
7538
- }
7539
- return out;
7540
- }
7541
-
7542
- static int stbi__pnm_isspace(char c)
7206
+ static int stbi__pnm_isspace(char c)
7543
7207
  {
7544
7208
  return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r';
7545
7209
  }
7546
7210
 
7547
- static void stbi__pnm_skip_whitespace(stbi__context *s, char *c)
7211
+ static void stbi__pnm_skip_whitespace(stbi__context *s, char *c)
7548
7212
  {
7549
7213
  for (;;) {
7550
7214
  while (!stbi__at_eof(s) && stbi__pnm_isspace(*c))
@@ -7558,45 +7222,43 @@ static void stbi__pnm_skip_whitespace(stbi__context *s, char *c)
7558
7222
  }
7559
7223
  }
7560
7224
 
7561
- static int stbi__pnm_isdigit(char c)
7562
- {
7563
- return c >= '0' && c <= '9';
7564
- }
7565
-
7566
- static int stbi__pnm_getinteger(stbi__context *s, char *c)
7225
+ static int stbi__pnm_getinteger(stbi__context *s, char *c)
7567
7226
  {
7568
7227
  int value = 0;
7569
7228
 
7570
- while (!stbi__at_eof(s) && stbi__pnm_isdigit(*c)) {
7229
+ while (!stbi__at_eof(s) && *c >= '0' && *c <= '9') {
7571
7230
  value = value*10 + (*c - '0');
7572
7231
  *c = (char) stbi__get8(s);
7573
7232
  if((value > 214748364) || (value == 214748364 && *c > '7'))
7574
- return stbi__err("integer parse overflow", "Parsing an integer in the PPM header overflowed a 32-bit int");
7233
+ return stbi__err("integer parse overflow", "Parsing an integer in the PPM overflowed a 32-bit int");
7575
7234
  }
7576
7235
 
7577
7236
  return value;
7578
7237
  }
7579
7238
 
7580
- static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp)
7239
+ static int stbi__pnm_info_internal(stbi__context *s, int *x, int *y, int *comp, int *maxv, int *isascii)
7581
7240
  {
7582
- int maxv, dummy;
7241
+ int dummy;
7583
7242
  char c, p, t;
7584
7243
 
7585
7244
  if (!x) x = &dummy;
7586
7245
  if (!y) y = &dummy;
7587
7246
  if (!comp) comp = &dummy;
7247
+ if (!isascii) isascii = &dummy;
7588
7248
 
7589
7249
  stbi__rewind(s);
7250
+ *isascii = 0;
7590
7251
 
7591
7252
  // Get identifier
7592
7253
  p = (char) stbi__get8(s);
7593
7254
  t = (char) stbi__get8(s);
7594
- if (p != 'P' || (t != '5' && t != '6')) {
7255
+ if (p != 'P' || (t != '2' && t != '3' && t != '5' && t != '6')) {
7595
7256
  stbi__rewind(s);
7596
7257
  return 0;
7597
7258
  }
7598
7259
 
7599
- *comp = (t == '6') ? 3 : 1; // '5' is 1-component .pgm; '6' is 3-component .ppm
7260
+ *comp = (t == '3' || t == '6') ? 3 : 1; // '2'/'5' is 1-component .pgm; '3'/'6' is 3-component .ppm
7261
+ *isascii = t == '2' || t == '3';
7600
7262
 
7601
7263
  c = (char) stbi__get8(s);
7602
7264
  stbi__pnm_skip_whitespace(s, &c);
@@ -7611,79 +7273,162 @@ static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp)
7611
7273
  return stbi__err("invalid width", "PPM image header had zero or overflowing width");
7612
7274
  stbi__pnm_skip_whitespace(s, &c);
7613
7275
 
7614
- maxv = stbi__pnm_getinteger(s, &c); // read max value
7615
- if (maxv > 65535)
7276
+ *maxv = stbi__pnm_getinteger(s, &c); // read max value
7277
+ if (*maxv == 0)
7278
+ return stbi__err("invalid maxv", "PPM image header had zero maxv");
7279
+ else if (*maxv > 65535)
7616
7280
  return stbi__err("max value > 65535", "PPM image supports only 8-bit and 16-bit images");
7617
- else if (maxv > 255)
7281
+ else if (*maxv > 255)
7618
7282
  return 16;
7619
7283
  else
7620
7284
  return 8;
7621
7285
  }
7622
7286
 
7623
- static int stbi__pnm_is16(stbi__context *s)
7287
+ static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
7624
7288
  {
7625
- if (stbi__pnm_info(s, NULL, NULL, NULL) == 16)
7626
- return 1;
7627
- return 0;
7289
+ stbi_uc *out;
7290
+ int i, maxv, expected_maxv, nvals, isascii;
7291
+ int bpv; // bytes per value
7292
+
7293
+ ri->bits_per_channel = stbi__pnm_info_internal(s, (int *)&s->img_x, (int *)&s->img_y, (int *)&s->img_n, &maxv, &isascii);
7294
+ if (ri->bits_per_channel == 0)
7295
+ return 0;
7296
+
7297
+ if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
7298
+ if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
7299
+
7300
+ *x = s->img_x;
7301
+ *y = s->img_y;
7302
+ if (comp) *comp = s->img_n;
7303
+
7304
+ bpv = ri->bits_per_channel / 8;
7305
+
7306
+ if (!stbi__mad4sizes_valid(s->img_n, s->img_x, s->img_y, bpv, 0))
7307
+ return stbi__errpuc("too large", "PNM too large");
7308
+
7309
+ out = (stbi_uc *) stbi__malloc_mad4(s->img_n, s->img_x, s->img_y, bpv, 0);
7310
+ if (!out) return stbi__errpuc("outofmem", "Out of memory");
7311
+ nvals = s->img_n * s->img_x * s->img_y;
7312
+ if (!isascii) {
7313
+ if (!stbi__getn(s, out, nvals * bpv)) {
7314
+ STBI_FREE(out);
7315
+ return stbi__errpuc("bad PNM", "PNM file truncated");
7316
+ }
7317
+ } else {
7318
+ char c = (char) stbi__get8(s);
7319
+ for (i = 0; i < nvals; ++i) {
7320
+ int val;
7321
+ stbi__pnm_skip_whitespace(s, &c);
7322
+ val = stbi__pnm_getinteger(s, &c);
7323
+ if (val < 0) {
7324
+ STBI_FREE(out);
7325
+ return NULL; // error already set
7326
+ }
7327
+ if (val > maxv) val = maxv - 1;
7328
+ if (bpv == 1)
7329
+ out[i] = (stbi_uc)val;
7330
+ else {
7331
+ out[i*2 + 0] = (stbi_uc)(val >> 8); // BE to match binary reader, fixed up below
7332
+ out[i*2 + 1] = (stbi_uc)val;
7333
+ }
7334
+ }
7335
+ }
7336
+
7337
+ // expected_maxv conversion
7338
+ expected_maxv = (bpv == 1) ? 255 : 65535;
7339
+ if (bpv == 2) {
7340
+ stbi__uint16* out16 = (stbi__uint16 *) out;
7341
+ for (i = 0; i < nvals; ++i) {
7342
+ stbi__uint32 val = (out[i*2 + 0] << 8) + out[i*2 + 1]; // BE
7343
+ if (maxv != expected_maxv) {
7344
+ if (val >= (stbi__uint32)maxv) val = maxv - 1;
7345
+ val = (val * expected_maxv) / maxv;
7346
+ }
7347
+ out16[i] = (stbi__uint16)val;
7348
+ }
7349
+ } else if (maxv != expected_maxv) {
7350
+ for (i = 0; i < nvals; ++i) {
7351
+ int val = out[i];
7352
+ if (val >= maxv) val = maxv - 1;
7353
+ out[i] = (stbi_uc)((val * expected_maxv) / maxv);
7354
+ }
7355
+ }
7356
+
7357
+ if (req_comp && req_comp != s->img_n) {
7358
+ if (bpv == 2) {
7359
+ out = (stbi_uc *) stbi__convert_format16((stbi__uint16 *) out, s->img_n, req_comp, s->img_x, s->img_y);
7360
+ } else {
7361
+ out = stbi__convert_format(out, s->img_n, req_comp, s->img_x, s->img_y);
7362
+ }
7363
+ }
7364
+ return out;
7365
+ }
7366
+
7367
+ static int stbi__pnm_info(stbi__context *s, stbi_info_res *res)
7368
+ {
7369
+ int maxv;
7370
+ int bits = stbi__pnm_info_internal(s, &res->x, &res->y, &res->comp, &maxv, NULL);
7371
+ if (bits) {
7372
+ res->type = STBI_type_pnm;
7373
+ res->is_16_bit = bits == 16;
7374
+ return 1;
7375
+ } else {
7376
+ return 0;
7377
+ }
7628
7378
  }
7629
7379
  #endif
7630
7380
 
7631
- static int stbi__info_main(stbi__context *s, int *x, int *y, int *comp)
7381
+ static stbi_info_res stbi__info2_main(stbi__context *s)
7632
7382
  {
7383
+ stbi_info_res res;
7384
+ memset(&res, 0, sizeof(res));
7385
+
7633
7386
  #ifndef STBI_NO_JPEG
7634
- if (stbi__jpeg_info(s, x, y, comp)) return 1;
7387
+ if (stbi__jpeg_info(s, &res)) return res;
7635
7388
  #endif
7636
7389
 
7637
7390
  #ifndef STBI_NO_PNG
7638
- if (stbi__png_info(s, x, y, comp)) return 1;
7391
+ if (stbi__png_info(s, &res)) return res;
7639
7392
  #endif
7640
7393
 
7641
7394
  #ifndef STBI_NO_GIF
7642
- if (stbi__gif_info(s, x, y, comp)) return 1;
7395
+ if (stbi__gif_info(s, &res)) return res;
7643
7396
  #endif
7644
7397
 
7645
7398
  #ifndef STBI_NO_BMP
7646
- if (stbi__bmp_info(s, x, y, comp)) return 1;
7399
+ if (stbi__bmp_info(s, &res)) return res;
7647
7400
  #endif
7648
7401
 
7649
7402
  #ifndef STBI_NO_PSD
7650
- if (stbi__psd_info(s, x, y, comp)) return 1;
7651
- #endif
7652
-
7653
- #ifndef STBI_NO_PIC
7654
- if (stbi__pic_info(s, x, y, comp)) return 1;
7403
+ if (stbi__psd_info(s, &res)) return res;
7655
7404
  #endif
7656
7405
 
7657
7406
  #ifndef STBI_NO_PNM
7658
- if (stbi__pnm_info(s, x, y, comp)) return 1;
7407
+ if (stbi__pnm_info(s, &res)) return res;
7659
7408
  #endif
7660
7409
 
7661
7410
  #ifndef STBI_NO_HDR
7662
- if (stbi__hdr_info(s, x, y, comp)) return 1;
7411
+ if (stbi__hdr_info(s, &res)) return res;
7663
7412
  #endif
7664
7413
 
7665
7414
  // test tga last because it's a crappy test!
7666
7415
  #ifndef STBI_NO_TGA
7667
- if (stbi__tga_info(s, x, y, comp))
7668
- return 1;
7416
+ if (stbi__tga_info(s, &res)) return res;
7669
7417
  #endif
7670
- return stbi__err("unknown image type", "Image not of any known type, or corrupt");
7418
+
7419
+ res.type = STBI_type_unsupported;
7420
+ stbi__err("unknown image type", "Image not of any known type, or corrupt");
7421
+ return res;
7671
7422
  }
7672
7423
 
7673
- static int stbi__is_16_main(stbi__context *s)
7424
+ static int stbi__info_main(stbi__context *s, int *x, int *y, int *comp)
7674
7425
  {
7675
- #ifndef STBI_NO_PNG
7676
- if (stbi__png_is16(s)) return 1;
7677
- #endif
7678
-
7679
- #ifndef STBI_NO_PSD
7680
- if (stbi__psd_is16(s)) return 1;
7681
- #endif
7682
-
7683
- #ifndef STBI_NO_PNM
7684
- if (stbi__pnm_is16(s)) return 1;
7685
- #endif
7686
- return 0;
7426
+ stbi_info_res res = stbi__info2_main(s);
7427
+ if (res.type == STBI_type_unsupported) return 0;
7428
+ if (x) *x = res.x;
7429
+ if (y) *y = res.y;
7430
+ if (comp) *comp = res.comp;
7431
+ return 1;
7687
7432
  }
7688
7433
 
7689
7434
  #ifndef STBI_NO_STDIO
@@ -7708,6 +7453,31 @@ STBIDEF int stbi_info_from_file(FILE *f, int *x, int *y, int *comp)
7708
7453
  return r;
7709
7454
  }
7710
7455
 
7456
+ STBIDEF stbi_info_res stbi_info2(char const *filename)
7457
+ {
7458
+ stbi_info_res res;
7459
+ FILE *f = stbi__fopen(filename, "rb");
7460
+ if (!f) {
7461
+ stbi__err("can't fopen", "Unable to open file");
7462
+ memset(&res,0,sizeof(res));
7463
+ return res;
7464
+ }
7465
+ res = stbi_info2_from_file(f);
7466
+ fclose(f);
7467
+ return res;
7468
+ }
7469
+
7470
+ STBIDEF stbi_info_res stbi_info2_from_file(FILE *f)
7471
+ {
7472
+ stbi_info_res res;
7473
+ stbi__context s;
7474
+ long pos = ftell(f);
7475
+ stbi__start_file(&s, f);
7476
+ res = stbi__info2_main(&s);
7477
+ fseek(f,pos,SEEK_SET);
7478
+ return res;
7479
+ }
7480
+
7711
7481
  STBIDEF int stbi_is_16_bit(char const *filename)
7712
7482
  {
7713
7483
  FILE *f = stbi__fopen(filename, "rb");
@@ -7720,13 +7490,14 @@ STBIDEF int stbi_is_16_bit(char const *filename)
7720
7490
 
7721
7491
  STBIDEF int stbi_is_16_bit_from_file(FILE *f)
7722
7492
  {
7723
- int r;
7493
+ stbi_info_res r;
7724
7494
  stbi__context s;
7725
7495
  long pos = ftell(f);
7726
7496
  stbi__start_file(&s, f);
7727
- r = stbi__is_16_main(&s);
7497
+ r = stbi__info2_main(&s);
7498
+ stbi__stop(&s);
7728
7499
  fseek(f,pos,SEEK_SET);
7729
- return r;
7500
+ return r.type != STBI_type_unsupported && r.is_16_bit;
7730
7501
  }
7731
7502
  #endif // !STBI_NO_STDIO
7732
7503
 
@@ -7739,29 +7510,69 @@ STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y
7739
7510
 
7740
7511
  STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *c, void *user, int *x, int *y, int *comp)
7741
7512
  {
7513
+ int result;
7742
7514
  stbi__context s;
7743
7515
  stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user);
7744
- return stbi__info_main(&s,x,y,comp);
7516
+ result = stbi__info_main(&s,x,y,comp);
7517
+ stbi__stop(&s);
7518
+ return result;
7519
+ }
7520
+
7521
+ STBIDEF stbi_info_res stbi_info2_from_memory(stbi_uc const *buffer, int len)
7522
+ {
7523
+ stbi__context s;
7524
+ stbi__start_mem(&s,buffer,len);
7525
+ return stbi__info2_main(&s);
7526
+ }
7527
+
7528
+ STBIDEF stbi_info_res stbi_info2_from_callbacks(stbi_io_callbacks const *c, void *user)
7529
+ {
7530
+ stbi__context s;
7531
+ stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user);
7532
+ return stbi__info2_main(&s);
7745
7533
  }
7746
7534
 
7747
7535
  STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len)
7748
7536
  {
7749
7537
  stbi__context s;
7538
+ stbi_info_res r;
7750
7539
  stbi__start_mem(&s,buffer,len);
7751
- return stbi__is_16_main(&s);
7540
+ r = stbi__info2_main(&s);
7541
+ return r.type != STBI_type_unsupported && r.is_16_bit;
7752
7542
  }
7753
7543
 
7754
7544
  STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *c, void *user)
7755
7545
  {
7756
7546
  stbi__context s;
7547
+ stbi_info_res r;
7757
7548
  stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user);
7758
- return stbi__is_16_main(&s);
7549
+ r = stbi__info2_main(&s);
7550
+ stbi__stop(&s);
7551
+ return r.type != STBI_type_unsupported && r.is_16_bit;
7759
7552
  }
7760
7553
 
7761
7554
  #endif // STB_IMAGE_IMPLEMENTATION
7762
7555
 
7763
7556
  /*
7764
7557
  revision history:
7558
+ 2.23 (2019-08-11) fix clang static analysis warning
7559
+ 2.22 (2019-03-04) gif fixes, fix warnings
7560
+ 2.21 (2019-02-25) fix typo in comment
7561
+ 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs
7562
+ 2.19 (2018-02-11) fix warning
7563
+ 2.18 (2018-01-30) fix warnings
7564
+ 2.17 (2018-01-29) bugfix, 1-bit BMP, 16-bitness query, fix warnings
7565
+ 2.16 (2017-07-23) all functions have 16-bit variants; optimizations; bugfixes
7566
+ 2.15 (2017-03-18) fix png-1,2,4; all Imagenet JPGs; no runtime SSE detection on GCC
7567
+ 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs
7568
+ 2.13 (2016-12-04) experimental 16-bit API, only for PNG so far; fixes
7569
+ 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes
7570
+ 2.11 (2016-04-02) 16-bit PNGS; enable SSE2 in non-gcc x64
7571
+ RGB-format JPEG; remove white matting in PSD;
7572
+ allocate large structures on the stack;
7573
+ correct channel count for PNG & BMP
7574
+ 2.10 (2016-01-22) avoid warning introduced in 2.09
7575
+ 2.09 (2016-01-16) 16-bit TGA; comments in PNM files; STBI_REALLOC_SIZED
7765
7576
  2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs
7766
7577
  2.19 (2018-02-11) fix warning
7767
7578
  2.18 (2018-01-30) fix warnings