gosu 1.4.5 → 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 (332) hide show
  1. checksums.yaml +4 -4
  2. data/COPYING +2 -1
  3. data/README.md +0 -8
  4. data/dependencies/SDL/include/SDL3/SDL.h +91 -0
  5. data/dependencies/SDL/include/SDL3/SDL_assert.h +710 -0
  6. data/dependencies/SDL/include/SDL3/SDL_asyncio.h +550 -0
  7. data/dependencies/SDL/include/SDL3/SDL_atomic.h +692 -0
  8. data/dependencies/SDL/include/SDL3/SDL_audio.h +2382 -0
  9. data/dependencies/SDL/include/SDL3/SDL_begin_code.h +553 -0
  10. data/dependencies/SDL/include/{SDL_bits.h → SDL3/SDL_bits.h} +47 -27
  11. data/dependencies/SDL/include/{SDL_blendmode.h → SDL3/SDL_blendmode.h} +59 -55
  12. data/dependencies/SDL/include/SDL3/SDL_camera.h +535 -0
  13. data/dependencies/SDL/include/SDL3/SDL_clipboard.h +331 -0
  14. data/dependencies/SDL/include/{close_code.h → SDL3/SDL_close_code.h} +9 -8
  15. data/dependencies/SDL/include/{SDL_copying.h → SDL3/SDL_copying.h} +3 -1
  16. data/dependencies/SDL/include/SDL3/SDL_cpuinfo.h +374 -0
  17. data/dependencies/SDL/include/SDL3/SDL_dialog.h +343 -0
  18. data/dependencies/SDL/include/SDL3/SDL_dlopennote.h +234 -0
  19. data/dependencies/SDL/include/{SDL_egl.h → SDL3/SDL_egl.h} +11 -8
  20. data/dependencies/SDL/include/SDL3/SDL_endian.h +645 -0
  21. data/dependencies/SDL/include/SDL3/SDL_error.h +226 -0
  22. data/dependencies/SDL/include/SDL3/SDL_events.h +1651 -0
  23. data/dependencies/SDL/include/SDL3/SDL_filesystem.h +537 -0
  24. data/dependencies/SDL/include/SDL3/SDL_gamepad.h +1657 -0
  25. data/dependencies/SDL/include/SDL3/SDL_gpu.h +4637 -0
  26. data/dependencies/SDL/include/SDL3/SDL_guid.h +106 -0
  27. data/dependencies/SDL/include/SDL3/SDL_haptic.h +1461 -0
  28. data/dependencies/SDL/include/SDL3/SDL_hidapi.h +571 -0
  29. data/dependencies/SDL/include/SDL3/SDL_hints.h +5041 -0
  30. data/dependencies/SDL/include/SDL3/SDL_init.h +508 -0
  31. data/dependencies/SDL/include/SDL3/SDL_intrin.h +410 -0
  32. data/dependencies/SDL/include/SDL3/SDL_iostream.h +1379 -0
  33. data/dependencies/SDL/include/SDL3/SDL_joystick.h +1385 -0
  34. data/dependencies/SDL/include/SDL3/SDL_keyboard.h +615 -0
  35. data/dependencies/SDL/include/SDL3/SDL_keycode.h +348 -0
  36. data/dependencies/SDL/include/SDL3/SDL_loadso.h +145 -0
  37. data/dependencies/SDL/include/{SDL_locale.h → SDL3/SDL_locale.h} +40 -24
  38. data/dependencies/SDL/include/SDL3/SDL_log.h +541 -0
  39. data/dependencies/SDL/include/SDL3/SDL_main.h +704 -0
  40. data/dependencies/SDL/include/SDL3/SDL_main_impl.h +149 -0
  41. data/dependencies/SDL/include/{SDL_messagebox.h → SDL3/SDL_messagebox.h} +85 -48
  42. data/dependencies/SDL/include/{SDL_metal.h → SDL3/SDL_metal.h} +29 -29
  43. data/dependencies/SDL/include/{SDL_misc.h → SDL3/SDL_misc.h} +13 -12
  44. data/dependencies/SDL/include/SDL3/SDL_mouse.h +813 -0
  45. data/dependencies/SDL/include/SDL3/SDL_mutex.h +1117 -0
  46. data/dependencies/SDL/include/SDL3/SDL_oldnames.h +1339 -0
  47. data/dependencies/SDL/include/{SDL_opengl.h → SDL3/SDL_opengl.h} +1247 -278
  48. data/dependencies/SDL/include/{SDL_opengl_glext.h → SDL3/SDL_opengl_glext.h} +5 -1
  49. data/dependencies/SDL/include/{SDL_opengles.h → SDL3/SDL_opengles.h} +6 -7
  50. data/dependencies/SDL/include/{SDL_opengles2.h → SDL3/SDL_opengles2.h} +10 -11
  51. data/dependencies/SDL/include/SDL3/SDL_pen.h +198 -0
  52. data/dependencies/SDL/include/SDL3/SDL_pixels.h +1441 -0
  53. data/dependencies/SDL/include/{SDL_test_log.h → SDL3/SDL_platform.h} +25 -26
  54. data/dependencies/SDL/include/SDL3/SDL_platform_defines.h +497 -0
  55. data/dependencies/SDL/include/{SDL_power.h → SDL3/SDL_power.h} +40 -20
  56. data/dependencies/SDL/include/SDL3/SDL_process.h +441 -0
  57. data/dependencies/SDL/include/SDL3/SDL_properties.h +572 -0
  58. data/dependencies/SDL/include/SDL3/SDL_rect.h +528 -0
  59. data/dependencies/SDL/include/SDL3/SDL_render.h +3034 -0
  60. data/dependencies/SDL/include/SDL3/SDL_revision.h +56 -0
  61. data/dependencies/SDL/include/{SDL_scancode.h → SDL3/SDL_scancode.h} +60 -68
  62. data/dependencies/SDL/include/SDL3/SDL_sensor.h +321 -0
  63. data/dependencies/SDL/include/SDL3/SDL_stdinc.h +6186 -0
  64. data/dependencies/SDL/include/SDL3/SDL_storage.h +686 -0
  65. data/dependencies/SDL/include/SDL3/SDL_surface.h +1769 -0
  66. data/dependencies/SDL/include/SDL3/SDL_system.h +840 -0
  67. data/dependencies/SDL/include/{SDL_test.h → SDL3/SDL_test.h} +16 -22
  68. data/dependencies/SDL/include/{SDL_test_assert.h → SDL3/SDL_test_assert.h} +27 -34
  69. data/dependencies/SDL/include/{SDL_test_common.h → SDL3/SDL_test_common.h} +109 -52
  70. data/dependencies/SDL/include/{SDL_test_compare.h → SDL3/SDL_test_compare.h} +22 -14
  71. data/dependencies/SDL/include/{SDL_test_crc32.h → SDL3/SDL_test_crc32.h} +27 -30
  72. data/dependencies/SDL/include/{SDL_test_font.h → SDL3/SDL_test_font.h} +50 -49
  73. data/dependencies/SDL/include/{SDL_test_fuzzer.h → SDL3/SDL_test_fuzzer.h} +64 -79
  74. data/dependencies/SDL/include/{SDL_test_harness.h → SDL3/SDL_test_harness.h} +47 -30
  75. data/dependencies/SDL/include/SDL3/SDL_test_log.h +83 -0
  76. data/dependencies/SDL/include/{SDL_test_md5.h → SDL3/SDL_test_md5.h} +21 -28
  77. data/dependencies/SDL/include/{SDL_test_memory.h → SDL3/SDL_test_memory.h} +19 -16
  78. data/dependencies/SDL/include/SDL3/SDL_thread.h +602 -0
  79. data/dependencies/SDL/include/SDL3/SDL_time.h +249 -0
  80. data/dependencies/SDL/include/SDL3/SDL_timer.h +454 -0
  81. data/dependencies/SDL/include/SDL3/SDL_touch.h +184 -0
  82. data/dependencies/SDL/include/SDL3/SDL_tray.h +544 -0
  83. data/dependencies/SDL/include/SDL3/SDL_version.h +198 -0
  84. data/dependencies/SDL/include/SDL3/SDL_video.h +3497 -0
  85. data/dependencies/SDL/include/SDL3/SDL_vulkan.h +287 -0
  86. data/dependencies/SDL/lib/x64/libSDL3.dll.a +0 -0
  87. data/dependencies/SDL/lib/x86/libSDL3.dll.a +0 -0
  88. data/dependencies/SDL_sound/SDL3_sound/SDL_sound.h +755 -0
  89. data/dependencies/SDL_sound/SDL_sound.c +136 -215
  90. data/dependencies/SDL_sound/SDL_sound_aiff.c +55 -54
  91. data/dependencies/SDL_sound/SDL_sound_au.c +32 -32
  92. data/dependencies/SDL_sound/SDL_sound_coreaudio.c +24 -41
  93. data/dependencies/SDL_sound/SDL_sound_flac.c +32 -12
  94. data/dependencies/SDL_sound/SDL_sound_internal.h +34 -49
  95. data/dependencies/SDL_sound/SDL_sound_modplug.c +21 -29
  96. data/dependencies/SDL_sound/SDL_sound_mp3.c +35 -13
  97. data/dependencies/SDL_sound/SDL_sound_raw.c +14 -15
  98. data/dependencies/SDL_sound/SDL_sound_shn.c +82 -82
  99. data/dependencies/SDL_sound/SDL_sound_voc.c +30 -30
  100. data/dependencies/SDL_sound/SDL_sound_vorbis.c +34 -25
  101. data/dependencies/SDL_sound/SDL_sound_wav.c +90 -81
  102. data/dependencies/SDL_sound/dr_flac.h +1890 -1301
  103. data/dependencies/SDL_sound/dr_mp3.h +1098 -660
  104. data/dependencies/SDL_sound/libmodplug/fastmix.c +1 -1
  105. data/dependencies/SDL_sound/libmodplug/libmodplug.h +5 -4
  106. data/dependencies/SDL_sound/libmodplug/load_ams.c +6 -3
  107. data/dependencies/SDL_sound/libmodplug/load_dsm.c +2 -1
  108. data/dependencies/SDL_sound/libmodplug/load_mt2.c +2 -2
  109. data/dependencies/SDL_sound/libmodplug/modplug.h +2 -1
  110. data/dependencies/SDL_sound/libmodplug/snd_fx.c +3 -3
  111. data/dependencies/SDL_sound/libmodplug/sndfile.c +3 -0
  112. data/dependencies/SDL_sound/libmodplug/sndmix.c +7 -3
  113. data/dependencies/SDL_sound/stb_vorbis.h +397 -342
  114. data/dependencies/mojoAL/AL/alc.h +2 -2
  115. data/dependencies/mojoAL/mojoal.c +2126 -1880
  116. data/dependencies/stb/stb_image.h +1261 -1360
  117. data/dependencies/stb/stb_image_write.h +329 -129
  118. data/dependencies/stb/stb_truetype.h +304 -60
  119. data/dependencies/utf8proc/utf8proc.c +133 -75
  120. data/dependencies/utf8proc/utf8proc.h +131 -50
  121. data/dependencies/utf8proc/utf8proc_data.h +16213 -15658
  122. data/ext/gosu-ffi/extconf.rb +103 -0
  123. data/ext/gosu-ffi/gosu-ffi.def +467 -0
  124. data/ffi/Gosu.cpp +330 -0
  125. data/ffi/Gosu.h +84 -0
  126. data/ffi/Gosu_Channel.cpp +62 -0
  127. data/ffi/Gosu_Channel.h +17 -0
  128. data/ffi/Gosu_Color.cpp +176 -0
  129. data/ffi/Gosu_Color.h +31 -0
  130. data/ffi/Gosu_Constants.cpp +334 -0
  131. data/ffi/Gosu_FFI.h +34 -0
  132. data/ffi/Gosu_FFI_internal.h +160 -0
  133. data/ffi/Gosu_Font.cpp +97 -0
  134. data/ffi/Gosu_Font.h +32 -0
  135. data/ffi/Gosu_Image.cpp +239 -0
  136. data/ffi/Gosu_Image.h +71 -0
  137. data/ffi/Gosu_Sample.cpp +29 -0
  138. data/ffi/Gosu_Sample.h +14 -0
  139. data/ffi/Gosu_Song.cpp +69 -0
  140. data/ffi/Gosu_Song.h +18 -0
  141. data/ffi/Gosu_TextInput.cpp +94 -0
  142. data/ffi/Gosu_TextInput.h +25 -0
  143. data/ffi/Gosu_Window.cpp +314 -0
  144. data/ffi/Gosu_Window.h +78 -0
  145. data/include/Gosu/Audio.hpp +6 -11
  146. data/include/Gosu/Bitmap.hpp +38 -53
  147. data/include/Gosu/Buffer.hpp +54 -0
  148. data/include/Gosu/Color.hpp +29 -36
  149. data/include/Gosu/Directories.hpp +25 -28
  150. data/include/Gosu/Drawable.hpp +58 -0
  151. data/include/Gosu/Font.hpp +6 -5
  152. data/include/Gosu/Fwd.hpp +4 -6
  153. data/include/Gosu/Gosu.hpp +5 -5
  154. data/include/Gosu/Graphics.hpp +53 -61
  155. data/include/Gosu/GraphicsBase.hpp +1 -11
  156. data/include/Gosu/Image.hpp +12 -15
  157. data/include/Gosu/Math.hpp +50 -72
  158. data/include/Gosu/Transform.hpp +32 -0
  159. data/include/Gosu/Utility.hpp +77 -1
  160. data/include/Gosu/Version.hpp +2 -2
  161. data/include/Gosu/Window.hpp +15 -9
  162. data/lib/SDL3.dll +0 -0
  163. data/lib/gosu/channel.rb +50 -0
  164. data/lib/gosu/color.rb +173 -0
  165. data/lib/gosu/compat.rb +130 -17
  166. data/lib/gosu/constants.rb +386 -0
  167. data/lib/gosu/ffi.rb +269 -0
  168. data/lib/gosu/font.rb +62 -0
  169. data/lib/gosu/gl_tex_info.rb +33 -0
  170. data/lib/gosu/gosu.rb +250 -0
  171. data/lib/gosu/image.rb +179 -0
  172. data/lib/gosu/numeric.rb +17 -0
  173. data/lib/gosu/preview.rb +6 -6
  174. data/lib/gosu/sample.rb +25 -0
  175. data/lib/gosu/song.rb +57 -0
  176. data/lib/gosu/text_input.rb +83 -0
  177. data/lib/gosu/window.rb +249 -0
  178. data/lib/gosu.rb +29 -8
  179. data/lib64/SDL3.dll +0 -0
  180. data/rdoc/gosu.rb +2 -2
  181. data/src/Audio.cpp +30 -24
  182. data/src/AudioFile.hpp +6 -5
  183. data/src/AudioFileAudioToolbox.cpp +8 -8
  184. data/src/AudioFileSDLSound.cpp +13 -14
  185. data/src/AudioImpl.hpp +2 -2
  186. data/src/BinPacker.cpp +188 -0
  187. data/src/BinPacker.hpp +55 -0
  188. data/src/Bitmap.cpp +172 -143
  189. data/src/BitmapIO.cpp +60 -86
  190. data/src/Buffer.cpp +150 -0
  191. data/src/ClipRectStack.cpp +38 -0
  192. data/src/ClipRectStack.hpp +17 -83
  193. data/src/Color.cpp +75 -80
  194. data/src/Directories.cpp +47 -0
  195. data/src/DirectoriesUIKit.cpp +50 -0
  196. data/src/DrawOp.hpp +20 -18
  197. data/src/DrawOpQueue.hpp +27 -33
  198. data/src/Drawable.cpp +95 -0
  199. data/src/EmptyDrawable.hpp +38 -0
  200. data/src/FPS.cpp +31 -0
  201. data/src/Font.cpp +104 -74
  202. data/src/GosuGLView.cpp +13 -11
  203. data/src/GosuViewController.cpp +2 -10
  204. data/src/Graphics.cpp +86 -145
  205. data/src/GraphicsImpl.hpp +14 -68
  206. data/src/Image.cpp +41 -35
  207. data/src/Input.cpp +89 -90
  208. data/src/Macro.cpp +46 -29
  209. data/src/Macro.hpp +23 -17
  210. data/src/MarkupParser.cpp +5 -5
  211. data/src/Math.cpp +48 -22
  212. data/src/OffScreenTarget.cpp +47 -50
  213. data/src/OffScreenTarget.hpp +13 -11
  214. data/src/OpenGLContext.cpp +116 -0
  215. data/src/OpenGLContext.hpp +41 -0
  216. data/src/RenderState.hpp +49 -71
  217. data/src/Resolution.cpp +65 -53
  218. data/src/TexChunk.cpp +35 -80
  219. data/src/TexChunk.hpp +44 -35
  220. data/src/Text.cpp +1 -1
  221. data/src/TextBuilder.cpp +35 -21
  222. data/src/TextBuilder.hpp +6 -9
  223. data/src/TextInput.cpp +9 -9
  224. data/src/Texture.cpp +62 -80
  225. data/src/Texture.hpp +25 -23
  226. data/src/TiledDrawable.cpp +149 -0
  227. data/src/TiledDrawable.hpp +47 -0
  228. data/src/TimingApple.cpp +1 -1
  229. data/src/Transform.cpp +45 -50
  230. data/src/TransformStack.hpp +16 -16
  231. data/src/TrueTypeFont.cpp +59 -51
  232. data/src/TrueTypeFont.hpp +6 -7
  233. data/src/TrueTypeFontApple.cpp +28 -19
  234. data/src/TrueTypeFontUnix.cpp +39 -32
  235. data/src/TrueTypeFontWin.cpp +30 -30
  236. data/src/Utility.cpp +113 -37
  237. data/src/UtilityWin.cpp +49 -0
  238. data/src/Window.cpp +121 -151
  239. data/src/WindowUIKit.cpp +14 -14
  240. metadata +165 -128
  241. data/dependencies/SDL/include/SDL.h +0 -233
  242. data/dependencies/SDL/include/SDL_assert.h +0 -326
  243. data/dependencies/SDL/include/SDL_atomic.h +0 -415
  244. data/dependencies/SDL/include/SDL_audio.h +0 -1500
  245. data/dependencies/SDL/include/SDL_clipboard.h +0 -141
  246. data/dependencies/SDL/include/SDL_config.h +0 -331
  247. data/dependencies/SDL/include/SDL_config_android.h +0 -157
  248. data/dependencies/SDL/include/SDL_config_iphoneos.h +0 -166
  249. data/dependencies/SDL/include/SDL_config_macosx.h +0 -197
  250. data/dependencies/SDL/include/SDL_config_minimal.h +0 -82
  251. data/dependencies/SDL/include/SDL_config_pandora.h +0 -128
  252. data/dependencies/SDL/include/SDL_config_psp.h +0 -144
  253. data/dependencies/SDL/include/SDL_config_windows.h +0 -225
  254. data/dependencies/SDL/include/SDL_config_winrt.h +0 -215
  255. data/dependencies/SDL/include/SDL_config_wiz.h +0 -121
  256. data/dependencies/SDL/include/SDL_cpuinfo.h +0 -594
  257. data/dependencies/SDL/include/SDL_endian.h +0 -348
  258. data/dependencies/SDL/include/SDL_error.h +0 -163
  259. data/dependencies/SDL/include/SDL_events.h +0 -1166
  260. data/dependencies/SDL/include/SDL_filesystem.h +0 -149
  261. data/dependencies/SDL/include/SDL_gamecontroller.h +0 -1074
  262. data/dependencies/SDL/include/SDL_gesture.h +0 -117
  263. data/dependencies/SDL/include/SDL_guid.h +0 -100
  264. data/dependencies/SDL/include/SDL_haptic.h +0 -1341
  265. data/dependencies/SDL/include/SDL_hidapi.h +0 -451
  266. data/dependencies/SDL/include/SDL_hints.h +0 -2569
  267. data/dependencies/SDL/include/SDL_joystick.h +0 -1066
  268. data/dependencies/SDL/include/SDL_keyboard.h +0 -353
  269. data/dependencies/SDL/include/SDL_keycode.h +0 -358
  270. data/dependencies/SDL/include/SDL_loadso.h +0 -115
  271. data/dependencies/SDL/include/SDL_log.h +0 -404
  272. data/dependencies/SDL/include/SDL_main.h +0 -275
  273. data/dependencies/SDL/include/SDL_mouse.h +0 -465
  274. data/dependencies/SDL/include/SDL_mutex.h +0 -471
  275. data/dependencies/SDL/include/SDL_name.h +0 -33
  276. data/dependencies/SDL/include/SDL_pixels.h +0 -644
  277. data/dependencies/SDL/include/SDL_platform.h +0 -261
  278. data/dependencies/SDL/include/SDL_quit.h +0 -58
  279. data/dependencies/SDL/include/SDL_rect.h +0 -376
  280. data/dependencies/SDL/include/SDL_render.h +0 -1919
  281. data/dependencies/SDL/include/SDL_revision.h +0 -7
  282. data/dependencies/SDL/include/SDL_rwops.h +0 -841
  283. data/dependencies/SDL/include/SDL_sensor.h +0 -322
  284. data/dependencies/SDL/include/SDL_shape.h +0 -155
  285. data/dependencies/SDL/include/SDL_stdinc.h +0 -830
  286. data/dependencies/SDL/include/SDL_surface.h +0 -997
  287. data/dependencies/SDL/include/SDL_system.h +0 -623
  288. data/dependencies/SDL/include/SDL_syswm.h +0 -386
  289. data/dependencies/SDL/include/SDL_test_images.h +0 -78
  290. data/dependencies/SDL/include/SDL_test_random.h +0 -115
  291. data/dependencies/SDL/include/SDL_thread.h +0 -464
  292. data/dependencies/SDL/include/SDL_timer.h +0 -222
  293. data/dependencies/SDL/include/SDL_touch.h +0 -150
  294. data/dependencies/SDL/include/SDL_types.h +0 -29
  295. data/dependencies/SDL/include/SDL_version.h +0 -204
  296. data/dependencies/SDL/include/SDL_video.h +0 -2150
  297. data/dependencies/SDL/include/SDL_vulkan.h +0 -215
  298. data/dependencies/SDL/include/begin_code.h +0 -187
  299. data/dependencies/SDL/lib/x64/libSDL2.dll.a +0 -0
  300. data/dependencies/SDL/lib/x86/libSDL2.dll.a +0 -0
  301. data/dependencies/SDL_sound/SDL_sound.h +0 -725
  302. data/ext/gosu/extconf.rb +0 -119
  303. data/include/Gosu/IO.hpp +0 -254
  304. data/include/Gosu/ImageData.hpp +0 -53
  305. data/include/Gosu/Inspection.hpp +0 -7
  306. data/lib/SDL2.dll +0 -0
  307. data/lib/gosu/patches.rb +0 -66
  308. data/lib/gosu/run.rb +0 -20
  309. data/lib/gosu/swig_patches.rb +0 -110
  310. data/lib64/SDL2.dll +0 -0
  311. data/src/BlockAllocator.cpp +0 -131
  312. data/src/BlockAllocator.hpp +0 -32
  313. data/src/DirectoriesApple.cpp +0 -69
  314. data/src/DirectoriesUnix.cpp +0 -46
  315. data/src/DirectoriesWin.cpp +0 -65
  316. data/src/EmptyImageData.hpp +0 -52
  317. data/src/FileUnix.cpp +0 -99
  318. data/src/FileWin.cpp +0 -88
  319. data/src/IO.cpp +0 -60
  320. data/src/Iconv.hpp +0 -51
  321. data/src/Inspection.cpp +0 -27
  322. data/src/LargeImageData.cpp +0 -215
  323. data/src/LargeImageData.hpp +0 -39
  324. data/src/Log.hpp +0 -19
  325. data/src/RubyGosu.cxx +0 -13047
  326. data/src/RubyGosu.h +0 -51
  327. data/src/WinUtility.cpp +0 -61
  328. data/src/WinUtility.hpp +0 -27
  329. /data/dependencies/SDL/include/{SDL_opengles2_gl2.h → SDL3/SDL_opengles2_gl2.h} +0 -0
  330. /data/dependencies/SDL/include/{SDL_opengles2_gl2ext.h → SDL3/SDL_opengles2_gl2ext.h} +0 -0
  331. /data/dependencies/SDL/include/{SDL_opengles2_gl2platform.h → SDL3/SDL_opengles2_gl2platform.h} +0 -0
  332. /data/dependencies/SDL/include/{SDL_opengles2_khrplatform.h → SDL3/SDL_opengles2_khrplatform.h} +0 -0
@@ -1,6 +1,21 @@
1
- /* stb_image - v2.27 - 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,28 +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
71
+ 2.28 (2023-01-29) many error fixes, security errors, just tons of stuff
51
72
  2.27 (2021-07-11) document stbi_info better, 16-bit PNM support, bug fixes
52
73
  2.26 (2020-07-13) many minor fixes
53
74
  2.25 (2020-02-02) fix warnings
54
75
  2.24 (2020-02-02) fix warnings; thread-local failure_reason and flip_vertically
55
- 2.23 (2019-08-11) fix clang static analysis warning
56
- 2.22 (2019-03-04) gif fixes, fix warnings
57
- 2.21 (2019-02-25) fix typo in comment
58
- 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs
59
- 2.19 (2018-02-11) fix warning
60
- 2.18 (2018-01-30) fix warnings
61
- 2.17 (2018-01-29) bugfix, 1-bit BMP, 16-bitness query, fix warnings
62
- 2.16 (2017-07-23) all functions have 16-bit variants; optimizations; bugfixes
63
- 2.15 (2017-03-18) fix png-1,2,4; all Imagenet JPGs; no runtime SSE detection on GCC
64
- 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs
65
- 2.13 (2016-12-04) experimental 16-bit API, only for PNG so far; fixes
66
- 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes
67
- 2.11 (2016-04-02) 16-bit PNGS; enable SSE2 in non-gcc x64
68
- RGB-format JPEG; remove white matting in PSD;
69
- allocate large structures on the stack;
70
- correct channel count for PNG & BMP
71
- 2.10 (2016-01-22) avoid warning introduced in 2.09
72
- 2.09 (2016-01-16) 16-bit TGA; comments in PNM files; STBI_REALLOC_SIZED
73
76
 
74
77
  See end of file for full revision history.
75
78
 
@@ -91,7 +94,7 @@ RECENT REVISION HISTORY:
91
94
  Optimizations & bugfixes Mikhail Morozov (1-bit BMP)
92
95
  Fabian "ryg" Giesen Anael Seghezzi (is-16-bit query)
93
96
  Arseny Kapoulkine Simon Breuss (16-bit PNM)
94
- John-Mark Allen
97
+ John-Mark Allen hikari_no_yume (stbi_info2, is_iphone_png)
95
98
  Carmelo J Fdez-Aguera
96
99
 
97
100
  Bug & warning fixes
@@ -108,7 +111,7 @@ RECENT REVISION HISTORY:
108
111
  Cass Everitt Ryamond Barbiero github:grim210
109
112
  Paul Du Bois Engin Manap Aldo Culquicondor github:sammyhw
110
113
  Philipp Wiesemann Dale Weiler Oriol Ferrer Mesia github:phprus
111
- Josh Tobin Matthew Gregan github:poppolopoppo
114
+ Josh Tobin Neil Bickford Matthew Gregan github:poppolopoppo
112
115
  Julian Raschke Gregory Mullen Christian Floisand github:darealshinji
113
116
  Baldur Karlsson Kevin Schmidt JR Smith github:Michaelangel007
114
117
  Brad Weinberger Matvey Cherevko github:mosra
@@ -140,7 +143,7 @@ RECENT REVISION HISTORY:
140
143
  // // ... x = width, y = height, n = # 8-bit components per pixel ...
141
144
  // // ... replace '0' with '1'..'4' to force that many components per pixel
142
145
  // // ... but 'n' will always be the number that it would have been if you said 0
143
- // stbi_image_free(data)
146
+ // stbi_image_free(data);
144
147
  //
145
148
  // Standard parameters:
146
149
  // int *x -- outputs image width in pixels
@@ -187,6 +190,9 @@ RECENT REVISION HISTORY:
187
190
  // // returns ok=1 and sets x, y, n if image is a supported format,
188
191
  // // 0 otherwise.
189
192
  //
193
+ // The stbi_info2 family provides the same information in struct format, plus
194
+ // some additional flags.
195
+ //
190
196
  // Note that stb_image pervasively uses ints in its public API for sizes,
191
197
  // including sizes of memory buffers. This is now part of the API and thus
192
198
  // hard to change without causing breakage. As a result, the various image
@@ -271,6 +277,11 @@ RECENT REVISION HISTORY:
271
277
  // you have issues compiling it, you can disable it entirely by
272
278
  // defining STBI_NO_SIMD.
273
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
+ //
274
285
  // ===========================================================================
275
286
  //
276
287
  // HDR image support (disable by defining STBI_NO_HDR)
@@ -306,24 +317,41 @@ RECENT REVISION HISTORY:
306
317
  //
307
318
  // stbi_is_hdr(char *filename);
308
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
+ //
309
327
  // ===========================================================================
310
328
  //
311
329
  // iPhone PNG support:
312
330
  //
313
- // We optionally support converting iPhone-formatted PNGs (which store
314
- // premultiplied BGRA) back to RGB, even though they're internally encoded
315
- // differently. To enable this conversion, call
316
- // 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).
317
340
  //
318
- // Call stbi_set_unpremultiply_on_load(1) as well to force a divide per
319
- // pixel to remove any premultiplied alpha *only* if the image file explicitly
320
- // says there's premultiplied data (currently only happens in iPhone images,
321
- // 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.
322
344
  //
323
345
  // ===========================================================================
324
346
  //
325
347
  // ADDITIONAL CONFIGURATION
326
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
+ //
327
355
  // - You can suppress implementation of any of the decoders to reduce
328
356
  // your code footprint by #defining one or more of the following
329
357
  // symbols before creating the implementation.
@@ -335,7 +363,6 @@ RECENT REVISION HISTORY:
335
363
  // STBI_NO_TGA
336
364
  // STBI_NO_GIF
337
365
  // STBI_NO_HDR
338
- // STBI_NO_PIC
339
366
  // STBI_NO_PNM (.ppm and .pgm)
340
367
  //
341
368
  // - You can request *only* certain decoders and suppress all other ones
@@ -349,11 +376,13 @@ RECENT REVISION HISTORY:
349
376
  // STBI_ONLY_TGA
350
377
  // STBI_ONLY_GIF
351
378
  // STBI_ONLY_HDR
352
- // STBI_ONLY_PIC
353
379
  // STBI_ONLY_PNM (.ppm and .pgm)
354
380
  //
355
- // - If you use STBI_NO_PNG (or _ONLY_ without PNG), and you still
356
- // 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
357
386
  //
358
387
  // - If you define STBI_MAX_DIMENSIONS, stb_image will reject images greater
359
388
  // than that size (in either width or height) without further processing.
@@ -380,6 +409,20 @@ enum
380
409
  STBI_rgb_alpha = 4
381
410
  };
382
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
+
383
426
  #include <stdlib.h>
384
427
  typedef unsigned char stbi_uc;
385
428
  typedef unsigned short stbi_us;
@@ -487,7 +530,29 @@ STBIDEF const char *stbi_failure_reason (void);
487
530
  // free the loaded image -- this is just free()
488
531
  STBIDEF void stbi_image_free (void *retval_from_stbi_load);
489
532
 
490
- // 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)
491
556
  STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
492
557
  STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp);
493
558
  STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len);
@@ -545,8 +610,7 @@ STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const ch
545
610
 
546
611
  #if defined(STBI_ONLY_JPEG) || defined(STBI_ONLY_PNG) || defined(STBI_ONLY_BMP) \
547
612
  || defined(STBI_ONLY_TGA) || defined(STBI_ONLY_GIF) || defined(STBI_ONLY_PSD) \
548
- || defined(STBI_ONLY_HDR) || defined(STBI_ONLY_PIC) || defined(STBI_ONLY_PNM) \
549
- || defined(STBI_ONLY_ZLIB)
613
+ || defined(STBI_ONLY_HDR) || defined(STBI_ONLY_PNM) || defined(STBI_ONLY_ZLIB)
550
614
  #ifndef STBI_ONLY_JPEG
551
615
  #define STBI_NO_JPEG
552
616
  #endif
@@ -568,9 +632,6 @@ STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const ch
568
632
  #ifndef STBI_ONLY_HDR
569
633
  #define STBI_NO_HDR
570
634
  #endif
571
- #ifndef STBI_ONLY_PIC
572
- #define STBI_NO_PIC
573
- #endif
574
635
  #ifndef STBI_ONLY_PNM
575
636
  #define STBI_NO_PNM
576
637
  #endif
@@ -635,7 +696,7 @@ STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const ch
635
696
  #endif
636
697
  #endif
637
698
 
638
- #ifdef _MSC_VER
699
+ #if defined(_MSC_VER) || defined(__SYMBIAN32__)
639
700
  typedef unsigned short stbi__uint16;
640
701
  typedef signed short stbi__int16;
641
702
  typedef unsigned int stbi__uint32;
@@ -658,10 +719,6 @@ typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1];
658
719
  #endif
659
720
 
660
721
  #ifdef _MSC_VER
661
- #define STBI_HAS_LROTL
662
- #endif
663
-
664
- #ifdef STBI_HAS_LROTL
665
722
  #define stbi_lrot(x,y) _lrotl(x,y)
666
723
  #else
667
724
  #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (-(y) & 31)))
@@ -686,7 +743,7 @@ typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1];
686
743
  #endif
687
744
 
688
745
  // x86/x64 detection
689
- #if defined(__x86_64__) || defined(_M_X64)
746
+ #if (defined(__x86_64__) || defined(_M_X64)) && !defined(_M_ARM64EC)
690
747
  #define STBI__X64_TARGET
691
748
  #elif defined(__i386) || defined(_M_IX86)
692
749
  #define STBI__X86_TARGET
@@ -719,7 +776,7 @@ typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1];
719
776
  #endif
720
777
 
721
778
  #if !defined(STBI_NO_SIMD) && (defined(STBI__X86_TARGET) || defined(STBI__X64_TARGET))
722
- #define STBI_SSE2
779
+ #define STBI__SSE2
723
780
  #include <emmintrin.h>
724
781
 
725
782
  #ifdef _MSC_VER
@@ -745,9 +802,9 @@ static int stbi__cpuid3(void)
745
802
  }
746
803
  #endif
747
804
 
748
- #define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name
805
+ #define STBI__SIMD_ALIGN(type, name) __declspec(align(16)) type name
749
806
 
750
- #if !defined(STBI_NO_JPEG) && defined(STBI_SSE2)
807
+ #if !defined(STBI_NO_JPEG) && defined(STBI__SSE2)
751
808
  static int stbi__sse2_available(void)
752
809
  {
753
810
  int info3 = stbi__cpuid3();
@@ -756,9 +813,9 @@ static int stbi__sse2_available(void)
756
813
  #endif
757
814
 
758
815
  #else // assume GCC-style if not VC++
759
- #define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
816
+ #define STBI__SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
760
817
 
761
- #if !defined(STBI_NO_JPEG) && defined(STBI_SSE2)
818
+ #if !defined(STBI_NO_JPEG) && defined(STBI__SSE2)
762
819
  static int stbi__sse2_available(void)
763
820
  {
764
821
  // If we're even attempting to compile this on GCC/Clang, that means
@@ -771,6 +828,11 @@ static int stbi__sse2_available(void)
771
828
  #endif
772
829
  #endif
773
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
+
774
836
  // ARM NEON
775
837
  #if defined(STBI_NO_SIMD) && defined(STBI_NEON)
776
838
  #undef STBI_NEON
@@ -779,14 +841,14 @@ static int stbi__sse2_available(void)
779
841
  #ifdef STBI_NEON
780
842
  #include <arm_neon.h>
781
843
  #ifdef _MSC_VER
782
- #define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name
844
+ #define STBI__SIMD_ALIGN(type, name) __declspec(align(16)) type name
783
845
  #else
784
- #define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
846
+ #define STBI__SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
785
847
  #endif
786
848
  #endif
787
849
 
788
- #ifndef STBI_SIMD_ALIGN
789
- #define STBI_SIMD_ALIGN(type, name) type name
850
+ #ifndef STBI__SIMD_ALIGN
851
+ #define STBI__SIMD_ALIGN(type, name) type name
790
852
  #endif
791
853
 
792
854
  #ifndef STBI_MAX_DIMENSIONS
@@ -842,6 +904,14 @@ static void stbi__start_callbacks(stbi__context *s, stbi_io_callbacks *c, void *
842
904
  s->img_buffer_original_end = s->img_buffer_end;
843
905
  }
844
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
+
845
915
  #ifndef STBI_NO_STDIO
846
916
 
847
917
  static int stbi__stdio_read(void *user, char *data, int size)
@@ -876,8 +946,6 @@ static void stbi__start_file(stbi__context *s, FILE *f)
876
946
  stbi__start_callbacks(s, &stbi__stdio_callbacks, (void *) f);
877
947
  }
878
948
 
879
- //static void stop_file(stbi__context *s) { }
880
-
881
949
  #endif // !STBI_NO_STDIO
882
950
 
883
951
  static void stbi__rewind(stbi__context *s)
@@ -905,59 +973,53 @@ typedef struct
905
973
  #ifndef STBI_NO_JPEG
906
974
  static int stbi__jpeg_test(stbi__context *s);
907
975
  static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
908
- 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);
909
977
  #endif
910
978
 
911
979
  #ifndef STBI_NO_PNG
912
980
  static int stbi__png_test(stbi__context *s);
913
981
  static void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
914
- static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp);
915
- static int stbi__png_is16(stbi__context *s);
982
+ static int stbi__png_info(stbi__context *s, stbi_info_res *res);
916
983
  #endif
917
984
 
918
985
  #ifndef STBI_NO_BMP
919
986
  static int stbi__bmp_test(stbi__context *s);
920
987
  static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
921
- 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);
922
989
  #endif
923
990
 
924
991
  #ifndef STBI_NO_TGA
925
992
  static int stbi__tga_test(stbi__context *s);
926
993
  static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
927
- 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);
928
995
  #endif
929
996
 
930
997
  #ifndef STBI_NO_PSD
931
998
  static int stbi__psd_test(stbi__context *s);
932
999
  static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc);
933
- static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp);
934
- static int stbi__psd_is16(stbi__context *s);
1000
+ static int stbi__psd_info(stbi__context *s, stbi_info_res *res);
935
1001
  #endif
936
1002
 
937
1003
  #ifndef STBI_NO_HDR
938
1004
  static int stbi__hdr_test(stbi__context *s);
939
1005
  static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
940
- 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);
941
1007
  #endif
942
1008
 
943
- #ifndef STBI_NO_PIC
944
- static int stbi__pic_test(stbi__context *s);
945
- static void *stbi__pic_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
946
- static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp);
947
- #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.
948
1011
 
949
1012
  #ifndef STBI_NO_GIF
950
1013
  static int stbi__gif_test(stbi__context *s);
951
1014
  static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
952
1015
  static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp);
953
- 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);
954
1017
  #endif
955
1018
 
956
1019
  #ifndef STBI_NO_PNM
957
1020
  static int stbi__pnm_test(stbi__context *s);
958
1021
  static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
959
- static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp);
960
- static int stbi__pnm_is16(stbi__context *s);
1022
+ static int stbi__pnm_info(stbi__context *s, stbi_info_res *res);
961
1023
  #endif
962
1024
 
963
1025
  static
@@ -1016,7 +1078,7 @@ static int stbi__mul2sizes_valid(int a, int b)
1016
1078
  return a <= INT_MAX/b;
1017
1079
  }
1018
1080
 
1019
- #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)
1020
1082
  // returns 1 if "a*b + add" has no negative terms/factors and doesn't overflow
1021
1083
  static int stbi__mad2sizes_valid(int a, int b, int add)
1022
1084
  {
@@ -1032,15 +1094,13 @@ static int stbi__mad3sizes_valid(int a, int b, int c, int add)
1032
1094
  }
1033
1095
 
1034
1096
  // returns 1 if "a*b*c*d + add" has no negative terms/factors and doesn't overflow
1035
- #if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM)
1036
1097
  static int stbi__mad4sizes_valid(int a, int b, int c, int d, int add)
1037
1098
  {
1038
1099
  return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) &&
1039
1100
  stbi__mul2sizes_valid(a*b*c, d) && stbi__addsizes_valid(a*b*c*d, add);
1040
1101
  }
1041
- #endif
1042
1102
 
1043
- #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)
1044
1104
  // mallocs with size overflow checking
1045
1105
  static void *stbi__malloc_mad2(int a, int b, int add)
1046
1106
  {
@@ -1055,7 +1115,7 @@ static void *stbi__malloc_mad3(int a, int b, int c, int add)
1055
1115
  return stbi__malloc(a*b*c + add);
1056
1116
  }
1057
1117
 
1058
- #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)
1059
1119
  static void *stbi__malloc_mad4(int a, int b, int c, int d, int add)
1060
1120
  {
1061
1121
  if (!stbi__mad4sizes_valid(a, b, c, d, add)) return NULL;
@@ -1063,6 +1123,27 @@ static void *stbi__malloc_mad4(int a, int b, int c, int d, int add)
1063
1123
  }
1064
1124
  #endif
1065
1125
 
1126
+ #if !defined(STBI_NO_JPEG) || !defined(STBI_NO_GIF)
1127
+ // returns 1 if the sum of two signed ints is valid (between -2^31 and 2^31-1 inclusive), 0 on overflow.
1128
+ static int stbi__addints_valid(int a, int b)
1129
+ {
1130
+ if ((a >= 0) != (b >= 0)) return 1; // a and b have different signs, so no overflow
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.
1132
+ return a <= INT_MAX - b;
1133
+ }
1134
+ #endif
1135
+
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)
1139
+ {
1140
+ if (b == 0 || b == -1) return 1; // multiplication by 0 is always 0; check for -1 so SHRT_MIN/b doesn't overflow
1141
+ if ((a >= 0) == (b >= 0)) return a <= SHRT_MAX/b; // product is positive, so similar to mul2sizes_valid
1142
+ if (b < 0) return a <= SHRT_MIN / b; // same as a * b >= SHRT_MIN
1143
+ return a >= SHRT_MIN / b;
1144
+ }
1145
+ #endif
1146
+
1066
1147
  // stbi__err - error
1067
1148
  // stbi__errpf - error returning pointer to float
1068
1149
  // stbi__errpuc - error returning pointer to unsigned char
@@ -1121,6 +1202,8 @@ static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int re
1121
1202
  ri->channel_order = STBI_ORDER_RGB; // all current input & output are this, but this is here so we can add BGR order
1122
1203
  ri->num_channels = 0;
1123
1204
 
1205
+ if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Invalid value for required components");
1206
+
1124
1207
  // test the formats with a very explicit header first (at least a FOURCC
1125
1208
  // or distinctive magic number first)
1126
1209
  #ifndef STBI_NO_PNG
@@ -1137,9 +1220,6 @@ static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int re
1137
1220
  #else
1138
1221
  STBI_NOTUSED(bpc);
1139
1222
  #endif
1140
- #ifndef STBI_NO_PIC
1141
- if (stbi__pic_test(s)) return stbi__pic_load(s,x,y,comp,req_comp, ri);
1142
- #endif
1143
1223
 
1144
1224
  // then the formats that can end up attempting to load with just 1 or 2
1145
1225
  // bytes matching expectations; these are prone to false positives, so
@@ -1169,12 +1249,17 @@ static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int re
1169
1249
 
1170
1250
  static stbi_uc *stbi__convert_16_to_8(stbi__uint16 *orig, int w, int h, int channels)
1171
1251
  {
1172
- int i;
1173
- int img_len = w * h * channels;
1252
+ int i, img_len;
1174
1253
  stbi_uc *reduced;
1175
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;
1176
1258
  reduced = (stbi_uc *) stbi__malloc(img_len);
1177
- 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
+ }
1178
1263
 
1179
1264
  for (i = 0; i < img_len; ++i)
1180
1265
  reduced[i] = (stbi_uc)((orig[i] >> 8) & 0xFF); // top half of each byte is sufficient approx of 16->8 bit scaling
@@ -1185,12 +1270,17 @@ static stbi_uc *stbi__convert_16_to_8(stbi__uint16 *orig, int w, int h, int chan
1185
1270
 
1186
1271
  static stbi__uint16 *stbi__convert_8_to_16(stbi_uc *orig, int w, int h, int channels)
1187
1272
  {
1188
- int i;
1189
- int img_len = w * h * channels;
1273
+ int i, img_len;
1190
1274
  stbi__uint16 *enlarged;
1191
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;
1192
1279
  enlarged = (stbi__uint16 *) stbi__malloc(img_len*2);
1193
- 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
+ }
1194
1284
 
1195
1285
  for (i = 0; i < img_len; ++i)
1196
1286
  enlarged[i] = (stbi__uint16)((orig[i] << 8) + orig[i]); // replicate to high and low byte, maps 0->0, 255->0xffff
@@ -1223,20 +1313,6 @@ static void stbi__vertical_flip(void *image, int w, int h, int bytes_per_pixel)
1223
1313
  }
1224
1314
  }
1225
1315
 
1226
- #ifndef STBI_NO_GIF
1227
- static void stbi__vertical_flip_slices(void *image, int w, int h, int z, int bytes_per_pixel)
1228
- {
1229
- int slice;
1230
- int slice_size = w * h * bytes_per_pixel;
1231
-
1232
- stbi_uc *bytes = (stbi_uc *)image;
1233
- for (slice = 0; slice < z; ++slice) {
1234
- stbi__vertical_flip(bytes, w, h, bytes_per_pixel);
1235
- bytes += slice_size;
1236
- }
1237
- }
1238
- #endif
1239
-
1240
1316
  static unsigned char *stbi__load_and_postprocess_8bit(stbi__context *s, int *x, int *y, int *comp, int req_comp)
1241
1317
  {
1242
1318
  stbi__result_info ri;
@@ -1359,10 +1435,7 @@ STBIDEF stbi_uc *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req
1359
1435
  stbi__context s;
1360
1436
  stbi__start_file(&s,f);
1361
1437
  result = stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);
1362
- if (result) {
1363
- // need to 'unget' all the characters in the IO buffer
1364
- fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR);
1365
- }
1438
+ stbi__stop(&s);
1366
1439
  return result;
1367
1440
  }
1368
1441
 
@@ -1372,10 +1445,7 @@ STBIDEF stbi__uint16 *stbi_load_from_file_16(FILE *f, int *x, int *y, int *comp,
1372
1445
  stbi__context s;
1373
1446
  stbi__start_file(&s,f);
1374
1447
  result = stbi__load_and_postprocess_16bit(&s,x,y,comp,req_comp);
1375
- if (result) {
1376
- // need to 'unget' all the characters in the IO buffer
1377
- fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR);
1378
- }
1448
+ stbi__stop(&s);
1379
1449
  return result;
1380
1450
  }
1381
1451
 
@@ -1401,9 +1471,12 @@ STBIDEF stbi_us *stbi_load_16_from_memory(stbi_uc const *buffer, int len, int *x
1401
1471
 
1402
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)
1403
1473
  {
1474
+ stbi_us *result;
1404
1475
  stbi__context s;
1405
1476
  stbi__start_callbacks(&s, (stbi_io_callbacks *)clbk, user);
1406
- 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;
1407
1480
  }
1408
1481
 
1409
1482
  STBIDEF stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
@@ -1415,24 +1488,24 @@ STBIDEF stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, i
1415
1488
 
1416
1489
  STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
1417
1490
  {
1491
+ stbi_uc *result;
1418
1492
  stbi__context s;
1419
1493
  stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
1420
- 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;
1421
1497
  }
1422
1498
 
1423
1499
  #ifndef STBI_NO_GIF
1424
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)
1425
1501
  {
1426
- unsigned char *result;
1427
1502
  stbi__context s;
1428
1503
  stbi__start_mem(&s,buffer,len);
1429
1504
 
1430
- result = (unsigned char*) stbi__load_gif_main(&s, delays, x, y, z, comp, req_comp);
1431
- if (stbi__vertically_flip_on_load) {
1432
- stbi__vertical_flip_slices( result, *x, *y, *z, *comp );
1433
- }
1505
+ if (req_comp < 0 || req_comp > 4)
1506
+ return stbi__errpuc("bad req_comp", "Invalid number of required components");
1434
1507
 
1435
- return result;
1508
+ return (unsigned char*) stbi__load_gif_main(&s, delays, x, y, z, comp, req_comp);
1436
1509
  }
1437
1510
  #endif
1438
1511
 
@@ -1464,9 +1537,12 @@ STBIDEF float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, in
1464
1537
 
1465
1538
  STBIDEF float *stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
1466
1539
  {
1540
+ float *result;
1467
1541
  stbi__context s;
1468
1542
  stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
1469
- 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;
1470
1546
  }
1471
1547
 
1472
1548
  #ifndef STBI_NO_STDIO
@@ -1482,9 +1558,12 @@ STBIDEF float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int r
1482
1558
 
1483
1559
  STBIDEF float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
1484
1560
  {
1561
+ float *result;
1485
1562
  stbi__context s;
1486
1563
  stbi__start_file(&s,f);
1487
- 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;
1488
1567
  }
1489
1568
  #endif // !STBI_NO_STDIO
1490
1569
 
@@ -1522,12 +1601,11 @@ STBIDEF int stbi_is_hdr (char const *filename)
1522
1601
  STBIDEF int stbi_is_hdr_from_file(FILE *f)
1523
1602
  {
1524
1603
  #ifndef STBI_NO_HDR
1525
- long pos = ftell(f);
1526
1604
  int res;
1527
1605
  stbi__context s;
1528
1606
  stbi__start_file(&s,f);
1529
1607
  res = stbi__hdr_test(&s);
1530
- fseek(f, pos, SEEK_SET);
1608
+ stbi__stop(&s);
1531
1609
  return res;
1532
1610
  #else
1533
1611
  STBI_NOTUSED(f);
@@ -1539,9 +1617,12 @@ STBIDEF int stbi_is_hdr_from_file(FILE *f)
1539
1617
  STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user)
1540
1618
  {
1541
1619
  #ifndef STBI_NO_HDR
1620
+ int result;
1542
1621
  stbi__context s;
1543
1622
  stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
1544
- return stbi__hdr_test(&s);
1623
+ result = stbi__hdr_test(&s);
1624
+ stbi__stop(&s);
1625
+ return result;
1545
1626
  #else
1546
1627
  STBI_NOTUSED(clbk);
1547
1628
  STBI_NOTUSED(user);
@@ -1602,7 +1683,7 @@ stbi_inline static stbi_uc stbi__get8(stbi__context *s)
1602
1683
  return 0;
1603
1684
  }
1604
1685
 
1605
- #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)
1606
1687
  // nothing
1607
1688
  #else
1608
1689
  stbi_inline static int stbi__at_eof(stbi__context *s)
@@ -1618,7 +1699,7 @@ stbi_inline static int stbi__at_eof(stbi__context *s)
1618
1699
  }
1619
1700
  #endif
1620
1701
 
1621
- #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)
1622
1703
  // nothing
1623
1704
  #else
1624
1705
  static void stbi__skip(stbi__context *s, int n)
@@ -1640,11 +1721,12 @@ static void stbi__skip(stbi__context *s, int n)
1640
1721
  }
1641
1722
  #endif
1642
1723
 
1643
- #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)
1644
1725
  // nothing
1645
1726
  #else
1646
1727
  static int stbi__getn(stbi__context *s, stbi_uc *buffer, int n)
1647
1728
  {
1729
+ if (n <= 0) return 1;
1648
1730
  if (s->io.read) {
1649
1731
  int blen = (int) (s->img_buffer_end - s->img_buffer);
1650
1732
  if (blen < n) {
@@ -1659,7 +1741,7 @@ static int stbi__getn(stbi__context *s, stbi_uc *buffer, int n)
1659
1741
  }
1660
1742
  }
1661
1743
 
1662
- if (s->img_buffer+n <= s->img_buffer_end) {
1744
+ if (n <= s->img_buffer_end - s->img_buffer) {
1663
1745
  memcpy(buffer, s->img_buffer, n);
1664
1746
  s->img_buffer += n;
1665
1747
  return 1;
@@ -1668,7 +1750,7 @@ static int stbi__getn(stbi__context *s, stbi_uc *buffer, int n)
1668
1750
  }
1669
1751
  #endif
1670
1752
 
1671
- #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)
1672
1754
  // nothing
1673
1755
  #else
1674
1756
  static int stbi__get16be(stbi__context *s)
@@ -1678,7 +1760,7 @@ static int stbi__get16be(stbi__context *s)
1678
1760
  }
1679
1761
  #endif
1680
1762
 
1681
- #if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC)
1763
+ #if defined(STBI_NO_PNG) && defined(STBI_NO_PSD)
1682
1764
  // nothing
1683
1765
  #else
1684
1766
  static stbi__uint32 stbi__get32be(stbi__context *s)
@@ -1709,7 +1791,7 @@ static stbi__uint32 stbi__get32le(stbi__context *s)
1709
1791
 
1710
1792
  #define STBI__BYTECAST(x) ((stbi_uc) ((x) & 255)) // truncate int to byte without warnings
1711
1793
 
1712
- #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)
1713
1795
  // nothing
1714
1796
  #else
1715
1797
  //////////////////////////////////////////////////////////////////////////////
@@ -1729,7 +1811,7 @@ static stbi_uc stbi__compute_y(int r, int g, int b)
1729
1811
  }
1730
1812
  #endif
1731
1813
 
1732
- #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)
1733
1815
  // nothing
1734
1816
  #else
1735
1817
  static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int req_comp, unsigned int x, unsigned int y)
@@ -1737,7 +1819,7 @@ static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int r
1737
1819
  int i,j;
1738
1820
  unsigned char *good;
1739
1821
 
1740
- if (req_comp == img_n) return data;
1822
+ if (req_comp == img_n || !data) return data;
1741
1823
  STBI_ASSERT(req_comp >= 1 && req_comp <= 4);
1742
1824
 
1743
1825
  good = (unsigned char *) stbi__malloc_mad3(req_comp, x, y, 0);
@@ -1777,7 +1859,7 @@ static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int r
1777
1859
  }
1778
1860
  #endif
1779
1861
 
1780
- #if defined(STBI_NO_PNG) && defined(STBI_NO_PSD)
1862
+ #if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PNM)
1781
1863
  // nothing
1782
1864
  #else
1783
1865
  static stbi__uint16 stbi__compute_y_16(int r, int g, int b)
@@ -1786,7 +1868,7 @@ static stbi__uint16 stbi__compute_y_16(int r, int g, int b)
1786
1868
  }
1787
1869
  #endif
1788
1870
 
1789
- #if defined(STBI_NO_PNG) && defined(STBI_NO_PSD)
1871
+ #if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PNM)
1790
1872
  // nothing
1791
1873
  #else
1792
1874
  static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int req_comp, unsigned int x, unsigned int y)
@@ -1794,10 +1876,10 @@ static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int r
1794
1876
  int i,j;
1795
1877
  stbi__uint16 *good;
1796
1878
 
1797
- if (req_comp == img_n) return data;
1879
+ if (req_comp == img_n || !data) return data;
1798
1880
  STBI_ASSERT(req_comp >= 1 && req_comp <= 4);
1799
1881
 
1800
- good = (stbi__uint16 *) stbi__malloc(req_comp * x * y * 2);
1882
+ good = (stbi__uint16 *) stbi__malloc_mad4(req_comp, x, y, 2, 0);
1801
1883
  if (good == NULL) {
1802
1884
  STBI_FREE(data);
1803
1885
  return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory");
@@ -1985,9 +2067,12 @@ static int stbi__build_huffman(stbi__huffman *h, int *count)
1985
2067
  int i,j,k=0;
1986
2068
  unsigned int code;
1987
2069
  // build size list for each symbol (from JPEG spec)
1988
- for (i=0; i < 16; ++i)
1989
- for (j=0; j < count[i]; ++j)
2070
+ for (i=0; i < 16; ++i) {
2071
+ for (j=0; j < count[i]; ++j) {
1990
2072
  h->size[k++] = (stbi_uc) (i+1);
2073
+ if(k >= 257) return stbi__err("bad size list","Corrupt JPEG");
2074
+ }
2075
+ }
1991
2076
  h->size[k] = 0;
1992
2077
 
1993
2078
  // compute actual symbols (from jpeg spec)
@@ -2112,6 +2197,8 @@ stbi_inline static int stbi__jpeg_huff_decode(stbi__jpeg *j, stbi__huffman *h)
2112
2197
 
2113
2198
  // convert the huffman code to the symbol id
2114
2199
  c = ((j->code_buffer >> (32 - k)) & stbi__bmask[k]) + h->delta[k];
2200
+ if(c < 0 || c >= 256) // symbol id out of bounds!
2201
+ return -1;
2115
2202
  STBI_ASSERT((((j->code_buffer) >> (32 - h->size[c])) & stbi__bmask[h->size[c]]) == h->code[c]);
2116
2203
 
2117
2204
  // convert the id to a symbol
@@ -2130,6 +2217,7 @@ stbi_inline static int stbi__extend_receive(stbi__jpeg *j, int n)
2130
2217
  unsigned int k;
2131
2218
  int sgn;
2132
2219
  if (j->code_bits < n) stbi__grow_buffer_unsafe(j);
2220
+ if (j->code_bits < n) return 0; // ran out of bits from stream, return 0s intead of continuing
2133
2221
 
2134
2222
  sgn = j->code_buffer >> 31; // sign bit always in MSB; 0 if MSB clear (positive), 1 if MSB set (negative)
2135
2223
  k = stbi_lrot(j->code_buffer, n);
@@ -2144,6 +2232,7 @@ stbi_inline static int stbi__jpeg_get_bits(stbi__jpeg *j, int n)
2144
2232
  {
2145
2233
  unsigned int k;
2146
2234
  if (j->code_bits < n) stbi__grow_buffer_unsafe(j);
2235
+ if (j->code_bits < n) return 0; // ran out of bits from stream, return 0s intead of continuing
2147
2236
  k = stbi_lrot(j->code_buffer, n);
2148
2237
  j->code_buffer = k & ~stbi__bmask[n];
2149
2238
  k &= stbi__bmask[n];
@@ -2155,6 +2244,7 @@ stbi_inline static int stbi__jpeg_get_bit(stbi__jpeg *j)
2155
2244
  {
2156
2245
  unsigned int k;
2157
2246
  if (j->code_bits < 1) stbi__grow_buffer_unsafe(j);
2247
+ if (j->code_bits < 1) return 0; // ran out of bits from stream, return 0s intead of continuing
2158
2248
  k = j->code_buffer;
2159
2249
  j->code_buffer <<= 1;
2160
2250
  --j->code_bits;
@@ -2192,8 +2282,10 @@ static int stbi__jpeg_decode_block(stbi__jpeg *j, short data[64], stbi__huffman
2192
2282
  memset(data,0,64*sizeof(data[0]));
2193
2283
 
2194
2284
  diff = t ? stbi__extend_receive(j, t) : 0;
2285
+ if (!stbi__addints_valid(j->img_comp[b].dc_pred, diff)) return stbi__err("bad delta","Corrupt JPEG");
2195
2286
  dc = j->img_comp[b].dc_pred + diff;
2196
2287
  j->img_comp[b].dc_pred = dc;
2288
+ if (!stbi__mul2shorts_valid(dc, dequant[0])) return stbi__err("can't merge dc and ac", "Corrupt JPEG");
2197
2289
  data[0] = (short) (dc * dequant[0]);
2198
2290
 
2199
2291
  // decode AC components, see JPEG spec
@@ -2207,6 +2299,7 @@ static int stbi__jpeg_decode_block(stbi__jpeg *j, short data[64], stbi__huffman
2207
2299
  if (r) { // fast-AC path
2208
2300
  k += (r >> 4) & 15; // run
2209
2301
  s = r & 15; // combined length
2302
+ if (s > j->code_bits) return stbi__err("bad huffman code", "Combined length longer than code bits available");
2210
2303
  j->code_buffer <<= s;
2211
2304
  j->code_bits -= s;
2212
2305
  // decode into unzigzag'd location
@@ -2241,13 +2334,14 @@ static int stbi__jpeg_decode_block_prog_dc(stbi__jpeg *j, short data[64], stbi__
2241
2334
 
2242
2335
  if (j->succ_high == 0) {
2243
2336
  // first scan for DC coefficient, must be first
2244
- memset(data,0,64*sizeof(data[0])); // 0 all the ac values now
2245
2337
  t = stbi__jpeg_huff_decode(j, hdc);
2246
2338
  if (t < 0 || t > 15) return stbi__err("can't merge dc and ac", "Corrupt JPEG");
2247
2339
  diff = t ? stbi__extend_receive(j, t) : 0;
2248
2340
 
2341
+ if (!stbi__addints_valid(j->img_comp[b].dc_pred, diff)) return stbi__err("bad delta", "Corrupt JPEG");
2249
2342
  dc = j->img_comp[b].dc_pred + diff;
2250
2343
  j->img_comp[b].dc_pred = dc;
2344
+ if (!stbi__mul2shorts_valid(dc, 1 << j->succ_low)) return stbi__err("can't merge dc and ac", "Corrupt JPEG");
2251
2345
  data[0] = (short) (dc * (1 << j->succ_low));
2252
2346
  } else {
2253
2347
  // refinement scan for DC coefficient
@@ -2282,6 +2376,7 @@ static int stbi__jpeg_decode_block_prog_ac(stbi__jpeg *j, short data[64], stbi__
2282
2376
  if (r) { // fast-AC path
2283
2377
  k += (r >> 4) & 15; // run
2284
2378
  s = r & 15; // combined length
2379
+ if (s > j->code_bits) return stbi__err("bad huffman code", "Combined length longer than code bits available");
2285
2380
  j->code_buffer <<= s;
2286
2381
  j->code_bits -= s;
2287
2382
  zig = stbi__jpeg_dezigzag[k++];
@@ -2389,7 +2484,7 @@ stbi_inline static stbi_uc stbi__clamp(int x)
2389
2484
  return (stbi_uc) x;
2390
2485
  }
2391
2486
 
2392
- #define stbi__f2f(x) ((int) (((x) * 4096 + 0.5)))
2487
+ #define stbi__f2f(x) ((int) (((x) * 4096 + 0.5f)))
2393
2488
  #define stbi__fsh(x) ((x) * 4096)
2394
2489
 
2395
2490
  // derived from jidctint -- DCT_ISLOW
@@ -2489,7 +2584,7 @@ static void stbi__idct_block(stbi_uc *out, int out_stride, short data[64])
2489
2584
  }
2490
2585
  }
2491
2586
 
2492
- #ifdef STBI_SSE2
2587
+ #ifdef STBI__SSE2
2493
2588
  // sse2 integer IDCT. not the fastest possible implementation but it
2494
2589
  // produces bit-identical results to the generic C version so it's
2495
2590
  // fully "transparent".
@@ -2668,7 +2763,7 @@ static void stbi__idct_simd(stbi_uc *out, int out_stride, short data[64])
2668
2763
  #undef dct_pass
2669
2764
  }
2670
2765
 
2671
- #endif // STBI_SSE2
2766
+ #endif // STBI__SSE2
2672
2767
 
2673
2768
  #ifdef STBI_NEON
2674
2769
 
@@ -2918,7 +3013,7 @@ static int stbi__parse_entropy_coded_data(stbi__jpeg *z)
2918
3013
  if (!z->progressive) {
2919
3014
  if (z->scan_n == 1) {
2920
3015
  int i,j;
2921
- STBI_SIMD_ALIGN(short, data[64]);
3016
+ STBI__SIMD_ALIGN(short, data[64]);
2922
3017
  int n = z->order[0];
2923
3018
  // non-interleaved data, we just need to process one block at a time,
2924
3019
  // in trivial scanline order
@@ -2944,7 +3039,7 @@ static int stbi__parse_entropy_coded_data(stbi__jpeg *z)
2944
3039
  return 1;
2945
3040
  } else { // interleaved
2946
3041
  int i,j,k,x,y;
2947
- STBI_SIMD_ALIGN(short, data[64]);
3042
+ STBI__SIMD_ALIGN(short, data[64]);
2948
3043
  for (j=0; j < z->img_mcu_y; ++j) {
2949
3044
  for (i=0; i < z->img_mcu_x; ++i) {
2950
3045
  // scan an interleaved mcu... process scan_n components in order
@@ -2986,13 +3081,17 @@ static int stbi__parse_entropy_coded_data(stbi__jpeg *z)
2986
3081
  for (j=0; j < h; ++j) {
2987
3082
  for (i=0; i < w; ++i) {
2988
3083
  short *data = z->img_comp[n].coeff + 64 * (i + j * z->img_comp[n].coeff_w);
2989
- if (z->spec_start == 0) {
2990
- if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->huff_dc[z->img_comp[n].hd], n))
2991
- return 0;
2992
- } else {
2993
- int ha = z->img_comp[n].ha;
2994
- if (!stbi__jpeg_decode_block_prog_ac(z, data, &z->huff_ac[ha], z->fast_ac[ha]))
2995
- 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
+ }
2996
3095
  }
2997
3096
  // every data block is an MCU, so countdown the restart interval
2998
3097
  if (--z->todo <= 0) {
@@ -3102,6 +3201,7 @@ static int stbi__process_marker(stbi__jpeg *z, int m)
3102
3201
  sizes[i] = stbi__get8(z->s);
3103
3202
  n += sizes[i];
3104
3203
  }
3204
+ if(n > 256) return stbi__err("bad DHT header","Corrupt JPEG"); // Loop over i < n would write past end of values!
3105
3205
  L -= 17;
3106
3206
  if (tc == 0) {
3107
3207
  if (!stbi__build_huffman(z->huff_dc+th, sizes)) return 0;
@@ -3312,6 +3412,8 @@ static int stbi__process_frame_header(stbi__jpeg *z, int scan)
3312
3412
  if (z->img_comp[i].raw_coeff == NULL)
3313
3413
  return stbi__free_jpeg_components(z, i+1, stbi__err("outofmem", "Out of memory"));
3314
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));
3315
3417
  }
3316
3418
  }
3317
3419
 
@@ -3351,6 +3453,28 @@ static int stbi__decode_jpeg_header(stbi__jpeg *z, int scan)
3351
3453
  return 1;
3352
3454
  }
3353
3455
 
3456
+ static stbi_uc stbi__skip_jpeg_junk_at_end(stbi__jpeg *j)
3457
+ {
3458
+ // some JPEGs have junk at end, skip over it but if we find what looks
3459
+ // like a valid marker, resume there
3460
+ while (!stbi__at_eof(j->s)) {
3461
+ stbi_uc x = stbi__get8(j->s);
3462
+ while (x == 0xff) { // might be a marker
3463
+ if (stbi__at_eof(j->s)) return STBI__MARKER_none;
3464
+ x = stbi__get8(j->s);
3465
+ if (x != 0x00 && x != 0xff) {
3466
+ // not a stuffed zero or lead-in to another marker, looks
3467
+ // like an actual marker, return it
3468
+ return x;
3469
+ }
3470
+ // stuffed zero has x=0 now which ends the loop, meaning we go
3471
+ // back to regular scan loop.
3472
+ // repeated 0xff keeps trying to read the next byte of the marker.
3473
+ }
3474
+ }
3475
+ return STBI__MARKER_none;
3476
+ }
3477
+
3354
3478
  // decode image to YCbCr format
3355
3479
  static int stbi__decode_jpeg_image(stbi__jpeg *j)
3356
3480
  {
@@ -3367,25 +3491,22 @@ static int stbi__decode_jpeg_image(stbi__jpeg *j)
3367
3491
  if (!stbi__process_scan_header(j)) return 0;
3368
3492
  if (!stbi__parse_entropy_coded_data(j)) return 0;
3369
3493
  if (j->marker == STBI__MARKER_none ) {
3370
- // handle 0s at the end of image data from IP Kamera 9060
3371
- while (!stbi__at_eof(j->s)) {
3372
- int x = stbi__get8(j->s);
3373
- if (x == 255) {
3374
- j->marker = stbi__get8(j->s);
3375
- break;
3376
- }
3377
- }
3494
+ j->marker = stbi__skip_jpeg_junk_at_end(j);
3378
3495
  // if we reach eof without hitting a marker, stbi__get_marker() below will fail and we'll eventually return 0
3379
3496
  }
3497
+ m = stbi__get_marker(j);
3498
+ if (STBI__RESTART(m))
3499
+ m = stbi__get_marker(j);
3380
3500
  } else if (stbi__DNL(m)) {
3381
3501
  int Ld = stbi__get16be(j->s);
3382
3502
  stbi__uint32 NL = stbi__get16be(j->s);
3383
3503
  if (Ld != 4) return stbi__err("bad DNL len", "Corrupt JPEG");
3384
3504
  if (NL != j->s->img_y) return stbi__err("bad DNL height", "Corrupt JPEG");
3505
+ m = stbi__get_marker(j);
3385
3506
  } else {
3386
3507
  if (!stbi__process_marker(j, m)) return 0;
3508
+ m = stbi__get_marker(j);
3387
3509
  }
3388
- m = stbi__get_marker(j);
3389
3510
  }
3390
3511
  if (j->progressive)
3391
3512
  stbi__jpeg_finish(j);
@@ -3472,7 +3593,7 @@ static stbi_uc *stbi__resample_row_hv_2(stbi_uc *out, stbi_uc *in_near, stbi_uc
3472
3593
  return out;
3473
3594
  }
3474
3595
 
3475
- #if defined(STBI_SSE2) || defined(STBI_NEON)
3596
+ #if defined(STBI__SSE2) || defined(STBI_NEON)
3476
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)
3477
3598
  {
3478
3599
  // need to generate 2x2 samples for every one in input
@@ -3488,7 +3609,7 @@ static stbi_uc *stbi__resample_row_hv_2_simd(stbi_uc *out, stbi_uc *in_near, stb
3488
3609
  // note we can't handle the last pixel in a row in this loop
3489
3610
  // because we need to handle the filter boundary conditions.
3490
3611
  for (; i < ((w-1) & ~7); i += 8) {
3491
- #if defined(STBI_SSE2)
3612
+ #if defined(STBI__SSE2)
3492
3613
  // load and perform the vertical filtering pass
3493
3614
  // this uses 3*x + y = 4*x + (y - x)
3494
3615
  __m128i zero = _mm_setzero_si128();
@@ -3628,12 +3749,12 @@ static void stbi__YCbCr_to_RGB_row(stbi_uc *out, const stbi_uc *y, const stbi_uc
3628
3749
  }
3629
3750
  }
3630
3751
 
3631
- #if defined(STBI_SSE2) || defined(STBI_NEON)
3752
+ #if defined(STBI__SSE2) || defined(STBI_NEON)
3632
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)
3633
3754
  {
3634
3755
  int i = 0;
3635
3756
 
3636
- #ifdef STBI_SSE2
3757
+ #ifdef STBI__SSE2
3637
3758
  // step == 3 is pretty ugly on the final interleave, and i'm not convinced
3638
3759
  // it's useful in practice (you wouldn't use it for textures, for example).
3639
3760
  // so just accelerate step == 4 case.
@@ -3770,7 +3891,7 @@ static void stbi__setup_jpeg(stbi__jpeg *j)
3770
3891
  j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_row;
3771
3892
  j->resample_row_hv_2_kernel = stbi__resample_row_hv_2;
3772
3893
 
3773
- #ifdef STBI_SSE2
3894
+ #ifdef STBI__SSE2
3774
3895
  if (stbi__sse2_available()) {
3775
3896
  j->idct_block_kernel = stbi__idct_simd;
3776
3897
  j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd;
@@ -3976,6 +4097,7 @@ static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int re
3976
4097
  unsigned char* result;
3977
4098
  stbi__jpeg* j = (stbi__jpeg*) stbi__malloc(sizeof(stbi__jpeg));
3978
4099
  if (!j) return stbi__errpuc("outofmem", "Out of memory");
4100
+ memset(j, 0, sizeof(stbi__jpeg));
3979
4101
  STBI_NOTUSED(ri);
3980
4102
  j->s = s;
3981
4103
  stbi__setup_jpeg(j);
@@ -3989,6 +4111,7 @@ static int stbi__jpeg_test(stbi__context *s)
3989
4111
  int r;
3990
4112
  stbi__jpeg* j = (stbi__jpeg*)stbi__malloc(sizeof(stbi__jpeg));
3991
4113
  if (!j) return stbi__err("outofmem", "Out of memory");
4114
+ memset(j, 0, sizeof(stbi__jpeg));
3992
4115
  j->s = s;
3993
4116
  stbi__setup_jpeg(j);
3994
4117
  r = stbi__decode_jpeg_header(j, STBI__SCAN_type);
@@ -4009,13 +4132,17 @@ static int stbi__jpeg_info_raw(stbi__jpeg *j, int *x, int *y, int *comp)
4009
4132
  return 1;
4010
4133
  }
4011
4134
 
4012
- 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)
4013
4136
  {
4014
4137
  int result;
4015
4138
  stbi__jpeg* j = (stbi__jpeg*) (stbi__malloc(sizeof(stbi__jpeg)));
4016
4139
  if (!j) return stbi__err("outofmem", "Out of memory");
4140
+ memset(j, 0, sizeof(stbi__jpeg));
4017
4141
  j->s = s;
4018
- 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
+ }
4019
4146
  STBI_FREE(j);
4020
4147
  return result;
4021
4148
  }
@@ -4121,6 +4248,7 @@ typedef struct
4121
4248
  {
4122
4249
  stbi_uc *zbuffer, *zbuffer_end;
4123
4250
  int num_bits;
4251
+ int hit_zeof_once;
4124
4252
  stbi__uint32 code_buffer;
4125
4253
 
4126
4254
  char *zout;
@@ -4187,9 +4315,20 @@ stbi_inline static int stbi__zhuffman_decode(stbi__zbuf *a, stbi__zhuffman *z)
4187
4315
  int b,s;
4188
4316
  if (a->num_bits < 16) {
4189
4317
  if (stbi__zeof(a)) {
4190
- 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);
4191
4331
  }
4192
- stbi__fill_bits(a);
4193
4332
  }
4194
4333
  b = z->fast[a->code_buffer & STBI__ZFAST_MASK];
4195
4334
  if (b) {
@@ -4209,6 +4348,7 @@ static int stbi__zexpand(stbi__zbuf *z, char *zout, int n) // need to make room
4209
4348
  if (!z->z_expandable) return stbi__err("output buffer limit","Corrupt PNG");
4210
4349
  cur = (unsigned int) (z->zout - z->zout_start);
4211
4350
  limit = old_limit = (unsigned) (z->zout_end - z->zout_start);
4351
+ if (limit == 0) limit = 1;
4212
4352
  if (UINT_MAX - cur < (unsigned) n) return stbi__err("outofmem", "Out of memory");
4213
4353
  while (cur + n > limit) {
4214
4354
  if(limit > UINT_MAX / 2) return stbi__err("outofmem", "Out of memory");
@@ -4254,17 +4394,25 @@ static int stbi__parse_huffman_block(stbi__zbuf *a)
4254
4394
  int len,dist;
4255
4395
  if (z == 256) {
4256
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
+ }
4257
4404
  return 1;
4258
4405
  }
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
4259
4407
  z -= 257;
4260
4408
  len = stbi__zlength_base[z];
4261
4409
  if (stbi__zlength_extra[z]) len += stbi__zreceive(a, stbi__zlength_extra[z]);
4262
4410
  z = stbi__zhuffman_decode(a, &a->z_distance);
4263
- if (z < 0) return stbi__err("bad huffman code","Corrupt PNG");
4411
+ if (z < 0 || z >= 30) return stbi__err("bad huffman code","Corrupt PNG"); // per DEFLATE, distance codes 30 and 31 must not appear in compressed data
4264
4412
  dist = stbi__zdist_base[z];
4265
4413
  if (stbi__zdist_extra[z]) dist += stbi__zreceive(a, stbi__zdist_extra[z]);
4266
4414
  if (zout - a->zout_start < dist) return stbi__err("bad dist","Corrupt PNG");
4267
- if (zout + len > a->zout_end) {
4415
+ if (len > a->zout_end - zout) {
4268
4416
  if (!stbi__zexpand(a, zout, len)) return 0;
4269
4417
  zout = a->zout;
4270
4418
  }
@@ -4408,6 +4556,7 @@ static int stbi__parse_zlib(stbi__zbuf *a, int parse_header)
4408
4556
  if (!stbi__parse_zlib_header(a)) return 0;
4409
4557
  a->num_bits = 0;
4410
4558
  a->code_buffer = 0;
4559
+ a->hit_zeof_once = 0;
4411
4560
  do {
4412
4561
  final = stbi__zreceive(a,1);
4413
4562
  type = stbi__zreceive(a,2);
@@ -4554,6 +4703,7 @@ typedef struct
4554
4703
  stbi__context *s;
4555
4704
  stbi_uc *idata, *expanded, *out;
4556
4705
  int depth;
4706
+ int is_iphone;
4557
4707
  } stbi__png;
4558
4708
 
4559
4709
 
@@ -4563,9 +4713,8 @@ enum {
4563
4713
  STBI__F_up=2,
4564
4714
  STBI__F_avg=3,
4565
4715
  STBI__F_paeth=4,
4566
- // synthetic filters used for first scanline to avoid needing a dummy row of 0s
4567
- STBI__F_avg_first,
4568
- STBI__F_paeth_first
4716
+ // synthetic filter used for first scanline to avoid needing a dummy row of 0s
4717
+ STBI__F_avg_first
4569
4718
  };
4570
4719
 
4571
4720
  static stbi_uc first_row_filter[5] =
@@ -4574,29 +4723,56 @@ static stbi_uc first_row_filter[5] =
4574
4723
  STBI__F_sub,
4575
4724
  STBI__F_none,
4576
4725
  STBI__F_avg_first,
4577
- STBI__F_paeth_first
4726
+ STBI__F_sub // Paeth with b=c=0 turns out to be equivalent to sub
4578
4727
  };
4579
4728
 
4580
4729
  static int stbi__paeth(int a, int b, int c)
4581
4730
  {
4582
- int p = a + b - c;
4583
- int pa = abs(p-a);
4584
- int pb = abs(p-b);
4585
- int pc = abs(p-c);
4586
- if (pa <= pb && pa <= pc) return a;
4587
- if (pb <= pc) return b;
4588
- 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;
4589
4740
  }
4590
4741
 
4591
4742
  static const stbi_uc stbi__depth_scale_table[9] = { 0, 0xff, 0x55, 0, 0x11, 0,0,0, 0x01 };
4592
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
+
4593
4767
  // create the png data from post-deflated data
4594
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)
4595
4769
  {
4596
- int bytes = (depth == 16? 2 : 1);
4770
+ int bytes = (depth == 16 ? 2 : 1);
4597
4771
  stbi__context *s = a->s;
4598
4772
  stbi__uint32 i,j,stride = x*out_n*bytes;
4599
4773
  stbi__uint32 img_len, img_width_bytes;
4774
+ stbi_uc *filter_buf;
4775
+ int all_ok = 1;
4600
4776
  int k;
4601
4777
  int img_n = s->img_n; // copy it into a local for later
4602
4778
 
@@ -4608,8 +4784,11 @@ static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 r
4608
4784
  a->out = (stbi_uc *) stbi__malloc_mad3(x, y, output_bytes, 0); // extra bytes to write off the end into
4609
4785
  if (!a->out) return stbi__err("outofmem", "Out of memory");
4610
4786
 
4787
+ // note: error exits here don't need to clean up a->out individually,
4788
+ // stbi__do_png always does on error.
4611
4789
  if (!stbi__mad3sizes_valid(img_n, x, depth, 7)) return stbi__err("too large", "Corrupt PNG");
4612
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");
4613
4792
  img_len = (img_width_bytes + 1) * y;
4614
4793
 
4615
4794
  // we used to check for exact match between raw_len and img_len on non-interlaced PNGs,
@@ -4617,189 +4796,137 @@ static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 r
4617
4796
  // so just check for raw_len < img_len always.
4618
4797
  if (raw_len < img_len) return stbi__err("not enough pixels","Corrupt PNG");
4619
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
+
4620
4809
  for (j=0; j < y; ++j) {
4621
- stbi_uc *cur = a->out + stride*j;
4622
- 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;
4623
4815
  int filter = *raw++;
4624
4816
 
4625
- if (filter > 4)
4626
- return stbi__err("invalid filter","Corrupt PNG");
4627
-
4628
- if (depth < 8) {
4629
- if (img_width_bytes > x) return stbi__err("invalid width","Corrupt PNG");
4630
- cur += x*out_n - img_width_bytes; // store output to the rightmost img_len bytes, so we can decode in place
4631
- filter_bytes = 1;
4632
- width = img_width_bytes;
4817
+ // check filter type
4818
+ if (filter > 4) {
4819
+ all_ok = stbi__err("invalid filter","Corrupt PNG");
4820
+ break;
4633
4821
  }
4634
- prior = cur - stride; // bugfix: need to compute this after 'cur +=' computation above
4635
4822
 
4636
4823
  // if first row, use special filter that doesn't sample previous row
4637
4824
  if (j == 0) filter = first_row_filter[filter];
4638
4825
 
4639
- // handle first byte explicitly
4640
- for (k=0; k < filter_bytes; ++k) {
4641
- switch (filter) {
4642
- case STBI__F_none : cur[k] = raw[k]; break;
4643
- case STBI__F_sub : cur[k] = raw[k]; break;
4644
- case STBI__F_up : cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break;
4645
- case STBI__F_avg : cur[k] = STBI__BYTECAST(raw[k] + (prior[k]>>1)); break;
4646
- case STBI__F_paeth : cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(0,prior[k],0)); break;
4647
- case STBI__F_avg_first : cur[k] = raw[k]; break;
4648
- case STBI__F_paeth_first: cur[k] = raw[k]; break;
4649
- }
4650
- }
4651
-
4652
- if (depth == 8) {
4653
- if (img_n != out_n)
4654
- cur[img_n] = 255; // first pixel
4655
- raw += img_n;
4656
- cur += out_n;
4657
- prior += out_n;
4658
- } else if (depth == 16) {
4659
- if (img_n != out_n) {
4660
- cur[filter_bytes] = 255; // first pixel top byte
4661
- cur[filter_bytes+1] = 255; // first pixel bottom byte
4662
- }
4663
- raw += filter_bytes;
4664
- cur += output_bytes;
4665
- prior += output_bytes;
4666
- } else {
4667
- raw += 1;
4668
- cur += 1;
4669
- 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;
4670
4857
  }
4671
4858
 
4672
- // this is a little gross, so that we don't switch per-pixel or per-component
4673
- if (depth < 8 || img_n == out_n) {
4674
- int nk = (width - 1)*filter_bytes;
4675
- #define STBI__CASE(f) \
4676
- case f: \
4677
- for (k=0; k < nk; ++k)
4678
- switch (filter) {
4679
- // "none" filter turns into a memcpy here; make that explicit.
4680
- case STBI__F_none: memcpy(cur, raw, nk); break;
4681
- STBI__CASE(STBI__F_sub) { cur[k] = STBI__BYTECAST(raw[k] + cur[k-filter_bytes]); } break;
4682
- STBI__CASE(STBI__F_up) { cur[k] = STBI__BYTECAST(raw[k] + prior[k]); } break;
4683
- STBI__CASE(STBI__F_avg) { cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-filter_bytes])>>1)); } break;
4684
- STBI__CASE(STBI__F_paeth) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],prior[k],prior[k-filter_bytes])); } break;
4685
- STBI__CASE(STBI__F_avg_first) { cur[k] = STBI__BYTECAST(raw[k] + (cur[k-filter_bytes] >> 1)); } break;
4686
- STBI__CASE(STBI__F_paeth_first) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],0,0)); } break;
4687
- }
4688
- #undef STBI__CASE
4689
- raw += nk;
4690
- } else {
4691
- STBI_ASSERT(img_n+1 == out_n);
4692
- #define STBI__CASE(f) \
4693
- case f: \
4694
- for (i=x-1; i >= 1; --i, cur[filter_bytes]=255,raw+=filter_bytes,cur+=output_bytes,prior+=output_bytes) \
4695
- for (k=0; k < filter_bytes; ++k)
4696
- switch (filter) {
4697
- STBI__CASE(STBI__F_none) { cur[k] = raw[k]; } break;
4698
- STBI__CASE(STBI__F_sub) { cur[k] = STBI__BYTECAST(raw[k] + cur[k- output_bytes]); } break;
4699
- STBI__CASE(STBI__F_up) { cur[k] = STBI__BYTECAST(raw[k] + prior[k]); } break;
4700
- STBI__CASE(STBI__F_avg) { cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k- output_bytes])>>1)); } break;
4701
- STBI__CASE(STBI__F_paeth) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k- output_bytes],prior[k],prior[k- output_bytes])); } break;
4702
- STBI__CASE(STBI__F_avg_first) { cur[k] = STBI__BYTECAST(raw[k] + (cur[k- output_bytes] >> 1)); } break;
4703
- STBI__CASE(STBI__F_paeth_first) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k- output_bytes],0,0)); } break;
4704
- }
4705
- #undef STBI__CASE
4706
-
4707
- // the loop above sets the high byte of the pixels' alpha, but for
4708
- // 16 bit png files we also need the low byte set. we'll do that here.
4709
- if (depth == 16) {
4710
- cur = a->out + stride*j; // start at the beginning of the row again
4711
- for (i=0; i < x; ++i,cur+=output_bytes) {
4712
- cur[filter_bytes+1] = 255;
4713
- }
4714
- }
4715
- }
4716
- }
4859
+ raw += nk;
4717
4860
 
4718
- // we make a separate pass to expand bits to pixels; for performance,
4719
- // this could run two scanlines behind the above code, so it won't
4720
- // intefere with filtering but will still be in the cache.
4721
- if (depth < 8) {
4722
- for (j=0; j < y; ++j) {
4723
- stbi_uc *cur = a->out + stride*j;
4724
- stbi_uc *in = a->out + stride*j + x*out_n - img_width_bytes;
4725
- // 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
4726
- // 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) {
4727
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;
4728
4868
 
4729
- // note that the final byte might overshoot and write more data than desired.
4730
- // we can allocate enough data that this never writes out of memory, but it
4731
- // could also overwrite the next scanline. can it overwrite non-empty data
4732
- // on the next scanline? yes, consider 1-pixel-wide scanlines with 1-bit-per-pixel.
4733
- // so we need to explicitly clamp the final ones
4734
-
4869
+ // expand bits to bytes first
4735
4870
  if (depth == 4) {
4736
- for (k=x*img_n; k >= 2; k-=2, ++in) {
4737
- *cur++ = scale * ((*in >> 4) );
4738
- *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;
4739
4875
  }
4740
- if (k > 0) *cur++ = scale * ((*in >> 4) );
4741
4876
  } else if (depth == 2) {
4742
- for (k=x*img_n; k >= 4; k-=4, ++in) {
4743
- *cur++ = scale * ((*in >> 6) );
4744
- *cur++ = scale * ((*in >> 4) & 0x03);
4745
- *cur++ = scale * ((*in >> 2) & 0x03);
4746
- *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;
4747
4881
  }
4748
- if (k > 0) *cur++ = scale * ((*in >> 6) );
4749
- if (k > 1) *cur++ = scale * ((*in >> 4) & 0x03);
4750
- if (k > 2) *cur++ = scale * ((*in >> 2) & 0x03);
4751
- } else if (depth == 1) {
4752
- for (k=x*img_n; k >= 8; k-=8, ++in) {
4753
- *cur++ = scale * ((*in >> 7) );
4754
- *cur++ = scale * ((*in >> 6) & 0x01);
4755
- *cur++ = scale * ((*in >> 5) & 0x01);
4756
- *cur++ = scale * ((*in >> 4) & 0x01);
4757
- *cur++ = scale * ((*in >> 3) & 0x01);
4758
- *cur++ = scale * ((*in >> 2) & 0x01);
4759
- *cur++ = scale * ((*in >> 1) & 0x01);
4760
- *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;
4761
4888
  }
4762
- if (k > 0) *cur++ = scale * ((*in >> 7) );
4763
- if (k > 1) *cur++ = scale * ((*in >> 6) & 0x01);
4764
- if (k > 2) *cur++ = scale * ((*in >> 5) & 0x01);
4765
- if (k > 3) *cur++ = scale * ((*in >> 4) & 0x01);
4766
- if (k > 4) *cur++ = scale * ((*in >> 3) & 0x01);
4767
- if (k > 5) *cur++ = scale * ((*in >> 2) & 0x01);
4768
- if (k > 6) *cur++ = scale * ((*in >> 1) & 0x01);
4769
4889
  }
4770
- if (img_n != out_n) {
4771
- int q;
4772
- // insert alpha = 255
4773
- 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);
4774
4909
  if (img_n == 1) {
4775
- for (q=x-1; q >= 0; --q) {
4776
- cur[q*2+1] = 255;
4777
- 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;
4778
4913
  }
4779
4914
  } else {
4780
4915
  STBI_ASSERT(img_n == 3);
4781
- for (q=x-1; q >= 0; --q) {
4782
- cur[q*4+3] = 255;
4783
- cur[q*4+2] = cur[q*3+2];
4784
- cur[q*4+1] = cur[q*3+1];
4785
- 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;
4786
4921
  }
4787
4922
  }
4788
4923
  }
4789
4924
  }
4790
- } else if (depth == 16) {
4791
- // force the image data from big-endian to platform-native.
4792
- // this is done in a separate pass due to the decoding relying
4793
- // on the data being untouched, but could probably be done
4794
- // per-line during decode if care is taken.
4795
- stbi_uc *cur = a->out;
4796
- stbi__uint16 *cur16 = (stbi__uint16*)cur;
4797
-
4798
- for(i=0; i < x*y*out_n; ++i,cur16++,cur+=2) {
4799
- *cur16 = (cur[0] << 8) | cur[1];
4800
- }
4801
4925
  }
4802
4926
 
4927
+ STBI_FREE(filter_buf);
4928
+ if (!all_ok) return 0;
4929
+
4803
4930
  return 1;
4804
4931
  }
4805
4932
 
@@ -4955,7 +5082,7 @@ STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert)
4955
5082
  static STBI_THREAD_LOCAL int stbi__unpremultiply_on_load_local, stbi__unpremultiply_on_load_set;
4956
5083
  static STBI_THREAD_LOCAL int stbi__de_iphone_flag_local, stbi__de_iphone_flag_set;
4957
5084
 
4958
- STBIDEF void stbi__unpremultiply_on_load_thread(int flag_true_if_should_unpremultiply)
5085
+ STBIDEF void stbi_set_unpremultiply_on_load_thread(int flag_true_if_should_unpremultiply)
4959
5086
  {
4960
5087
  stbi__unpremultiply_on_load_local = flag_true_if_should_unpremultiply;
4961
5088
  stbi__unpremultiply_on_load_set = 1;
@@ -5023,15 +5150,16 @@ static void stbi__de_iphone(stbi__png *z)
5023
5150
  static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
5024
5151
  {
5025
5152
  stbi_uc palette[1024], pal_img_n=0;
5026
- stbi_uc has_trans=0, tc[3]={0};
5153
+ stbi_uc has_trans=0, tc[3]={0,0,0};
5027
5154
  stbi__uint16 tc16[3];
5028
5155
  stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0;
5029
- int first=1,k,interlace=0, color=0, is_iphone=0;
5156
+ int first=1,k,interlace=0, color=0;
5030
5157
  stbi__context *s = z->s;
5031
5158
 
5032
5159
  z->expanded = NULL;
5033
5160
  z->idata = NULL;
5034
5161
  z->out = NULL;
5162
+ z->is_iphone = 0;
5035
5163
 
5036
5164
  if (!stbi__check_png_header(s)) return 0;
5037
5165
 
@@ -5041,7 +5169,7 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
5041
5169
  stbi__pngchunk c = stbi__get_chunk_header(s);
5042
5170
  switch (c.type) {
5043
5171
  case STBI__PNG_TYPE('C','g','B','I'):
5044
- is_iphone = 1;
5172
+ z->is_iphone = 1;
5045
5173
  stbi__skip(s, c.length);
5046
5174
  break;
5047
5175
  case STBI__PNG_TYPE('I','H','D','R'): {
@@ -5057,6 +5185,7 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
5057
5185
  color = stbi__get8(s); if (color > 6) return stbi__err("bad ctype","Corrupt PNG");
5058
5186
  if (color == 3 && z->depth == 16) return stbi__err("bad ctype","Corrupt PNG");
5059
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");
5060
5189
  comp = stbi__get8(s); if (comp) return stbi__err("bad comp method","Corrupt PNG");
5061
5190
  filter= stbi__get8(s); if (filter) return stbi__err("bad filter method","Corrupt PNG");
5062
5191
  interlace = stbi__get8(s); if (interlace>1) return stbi__err("bad interlace method","Corrupt PNG");
@@ -5064,14 +5193,14 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
5064
5193
  if (!pal_img_n) {
5065
5194
  s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0);
5066
5195
  if ((1 << 30) / s->img_x / s->img_n < s->img_y) return stbi__err("too large", "Image too large to decode");
5067
- if (scan == STBI__SCAN_header) return 1;
5068
5196
  } else {
5069
5197
  // if paletted, then pal_n is our final components, and
5070
5198
  // img_n is # components to decompress/filter.
5071
5199
  s->img_n = 1;
5072
5200
  if ((1 << 30) / s->img_x / 4 < s->img_y) return stbi__err("too large","Corrupt PNG");
5073
- // if SCAN_header, have to scan to see if we have a tRNS
5201
+ memset(palette, 0, sizeof(palette));
5074
5202
  }
5203
+ // even with SCAN_header, have to scan to see if we have a tRNS
5075
5204
  break;
5076
5205
  }
5077
5206
 
@@ -5103,10 +5232,14 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
5103
5232
  if (!(s->img_n & 1)) return stbi__err("tRNS with alpha","Corrupt PNG");
5104
5233
  if (c.length != (stbi__uint32) s->img_n*2) return stbi__err("bad tRNS len","Corrupt PNG");
5105
5234
  has_trans = 1;
5235
+ // non-paletted with tRNS = constant alpha. if header-scanning, we can stop now.
5236
+ if (scan == STBI__SCAN_header) { ++s->img_n; return 1; }
5106
5237
  if (z->depth == 16) {
5107
- 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
5108
5240
  } else {
5109
- 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
5110
5243
  }
5111
5244
  }
5112
5245
  break;
@@ -5115,14 +5248,22 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
5115
5248
  case STBI__PNG_TYPE('I','D','A','T'): {
5116
5249
  if (first) return stbi__err("first not IHDR", "Corrupt PNG");
5117
5250
  if (pal_img_n && !pal_len) return stbi__err("no PLTE","Corrupt PNG");
5118
- if (scan == STBI__SCAN_header) { s->img_n = pal_img_n; return 1; }
5119
- if ((int)(ioff + c.length) < (int)ioff) return 0;
5251
+ if (scan == STBI__SCAN_header) {
5252
+ // header scan definitely stops at first IDAT
5253
+ if (pal_img_n)
5254
+ s->img_n = pal_img_n;
5255
+ return 1;
5256
+ }
5257
+ if (c.length > (1u << 30)) return stbi__err("IDAT size limit", "IDAT section larger than 2^30 bytes");
5258
+ if (!stbi__addsizes_valid(ioff, c.length)) return 0;
5120
5259
  if (ioff + c.length > idata_limit) {
5121
5260
  stbi__uint32 idata_limit_old = idata_limit;
5122
5261
  stbi_uc *p;
5123
5262
  if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096;
5124
- 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");
5125
5265
  idata_limit *= 2;
5266
+ }
5126
5267
  STBI_NOTUSED(idata_limit_old);
5127
5268
  p = (stbi_uc *) STBI_REALLOC_SIZED(z->idata, idata_limit_old, idata_limit); if (p == NULL) return stbi__err("outofmem", "Out of memory");
5128
5269
  z->idata = p;
@@ -5140,7 +5281,7 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
5140
5281
  // initial guess for decoded data size to avoid unnecessary reallocs
5141
5282
  bpl = (s->img_x * z->depth + 7) / 8; // bytes per line, per component
5142
5283
  raw_len = bpl * s->img_y * s->img_n /* pixels */ + s->img_y /* filter mode per row */;
5143
- 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);
5144
5285
  if (z->expanded == NULL) return 0; // zlib should set error
5145
5286
  STBI_FREE(z->idata); z->idata = NULL;
5146
5287
  if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans)
@@ -5155,7 +5296,7 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
5155
5296
  if (!stbi__compute_transparency(z, tc, s->img_out_n)) return 0;
5156
5297
  }
5157
5298
  }
5158
- 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)
5159
5300
  stbi__de_iphone(z);
5160
5301
  if (pal_img_n) {
5161
5302
  // pal_img_n == 3 or 4
@@ -5178,15 +5319,7 @@ static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
5178
5319
  // if critical, fail
5179
5320
  if (first) return stbi__err("first not IHDR", "Corrupt PNG");
5180
5321
  if ((c.type & (1 << 29)) == 0) {
5181
- #ifndef STBI_NO_FAILURE_STRINGS
5182
- // not threadsafe
5183
- static char invalid_chunk[] = "XXXX PNG chunk not known";
5184
- invalid_chunk[0] = STBI__BYTECAST(c.type >> 24);
5185
- invalid_chunk[1] = STBI__BYTECAST(c.type >> 16);
5186
- invalid_chunk[2] = STBI__BYTECAST(c.type >> 8);
5187
- invalid_chunk[3] = STBI__BYTECAST(c.type >> 0);
5188
- #endif
5189
- 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");
5190
5323
  }
5191
5324
  stbi__skip(s, c.length);
5192
5325
  break;
@@ -5243,35 +5376,20 @@ static int stbi__png_test(stbi__context *s)
5243
5376
  return r;
5244
5377
  }
5245
5378
 
5246
- static int stbi__png_info_raw(stbi__png *p, int *x, int *y, int *comp)
5247
- {
5248
- if (!stbi__parse_png_file(p, STBI__SCAN_header, 0)) {
5249
- stbi__rewind( p->s );
5250
- return 0;
5251
- }
5252
- if (x) *x = p->s->img_x;
5253
- if (y) *y = p->s->img_y;
5254
- if (comp) *comp = p->s->img_n;
5255
- return 1;
5256
- }
5257
-
5258
- static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp)
5259
- {
5260
- stbi__png p;
5261
- p.s = s;
5262
- return stbi__png_info_raw(&p, x, y, comp);
5263
- }
5264
-
5265
- static int stbi__png_is16(stbi__context *s)
5379
+ static int stbi__png_info(stbi__context *s, stbi_info_res *res)
5266
5380
  {
5267
5381
  stbi__png p;
5268
5382
  p.s = s;
5269
- if (!stbi__png_info_raw(&p, NULL, NULL, NULL))
5270
- return 0;
5271
- if (p.depth != 16) {
5272
- stbi__rewind(p.s);
5383
+ if (!stbi__parse_png_file(&p, STBI__SCAN_header, 0)) {
5384
+ stbi__rewind( p.s );
5273
5385
  return 0;
5274
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;
5275
5393
  return 1;
5276
5394
  }
5277
5395
  #endif
@@ -5366,11 +5484,12 @@ static int stbi__bmp_set_mask_defaults(stbi__bmp_data *info, int compress)
5366
5484
  info->mr = 31u << 10;
5367
5485
  info->mg = 31u << 5;
5368
5486
  info->mb = 31u << 0;
5487
+ info->ma = 0;
5369
5488
  } else if (info->bpp == 32) {
5370
5489
  info->mr = 0xffu << 16;
5371
5490
  info->mg = 0xffu << 8;
5372
5491
  info->mb = 0xffu << 0;
5373
- info->ma = 0xffu << 24;
5492
+ info->ma = 0; // BI_BITFIELDS BMPs can have alpha, but BI_RGB ones never do
5374
5493
  info->all_a = 0; // if all_a is 0 at end, then we loaded alpha channel but it was all 0
5375
5494
  } else {
5376
5495
  // otherwise, use defaults, which is all-0
@@ -5415,13 +5534,7 @@ static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info)
5415
5534
  stbi__get32le(s); // discard vres
5416
5535
  stbi__get32le(s); // discard colorsused
5417
5536
  stbi__get32le(s); // discard max important
5418
- if (hsz == 40 || hsz == 56) {
5419
- if (hsz == 56) {
5420
- stbi__get32le(s);
5421
- stbi__get32le(s);
5422
- stbi__get32le(s);
5423
- stbi__get32le(s);
5424
- }
5537
+ if (hsz == 40) { // BITMAPINFOHEADER
5425
5538
  if (info->bpp == 16 || info->bpp == 32) {
5426
5539
  if (compress == 0) {
5427
5540
  stbi__bmp_set_mask_defaults(info, compress);
@@ -5439,9 +5552,9 @@ static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info)
5439
5552
  return stbi__errpuc("bad BMP", "bad BMP");
5440
5553
  }
5441
5554
  } else {
5442
- // V4/V5 header
5555
+ // V3/V4/V5 header
5443
5556
  int i;
5444
- if (hsz != 108 && hsz != 124)
5557
+ if (hsz != 56 && hsz != 108 && hsz != 124)
5445
5558
  return stbi__errpuc("bad BMP", "bad BMP");
5446
5559
  info->mr = stbi__get32le(s);
5447
5560
  info->mg = stbi__get32le(s);
@@ -5449,9 +5562,11 @@ static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info)
5449
5562
  info->ma = stbi__get32le(s);
5450
5563
  if (compress != 3) // override mr/mg/mb unless in BI_BITFIELDS mode, as per docs
5451
5564
  stbi__bmp_set_mask_defaults(info, compress);
5452
- stbi__get32le(s); // discard color space
5453
- for (i=0; i < 12; ++i)
5454
- 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
+ }
5455
5570
  if (hsz == 124) {
5456
5571
  stbi__get32le(s); // discard rendering intent
5457
5572
  stbi__get32le(s); // discard offset of profile data
@@ -5471,6 +5586,7 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5471
5586
  stbi_uc pal[256][4];
5472
5587
  int psize=0,i,j,width;
5473
5588
  int flip_vertically, pad, target;
5589
+ int bytes_read_so_far;
5474
5590
  stbi__bmp_data info;
5475
5591
  STBI_NOTUSED(ri);
5476
5592
 
@@ -5490,16 +5606,33 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5490
5606
  ma = info.ma;
5491
5607
  all_a = info.all_a;
5492
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
+
5493
5613
  if (info.hsz == 12) {
5494
5614
  if (info.bpp < 24)
5495
- psize = (info.offset - info.extra_read - 24) / 3;
5615
+ psize = (info.offset - bytes_read_so_far) / 3;
5496
5616
  } else {
5497
5617
  if (info.bpp < 16)
5498
- psize = (info.offset - info.extra_read - info.hsz) >> 2;
5618
+ psize = (info.offset - bytes_read_so_far) >> 2;
5499
5619
  }
5500
5620
  if (psize == 0) {
5501
- if (info.offset != s->callback_already_read + (s->img_buffer - s->img_buffer_original)) {
5502
- return stbi__errpuc("bad offset", "Corrupt BMP");
5621
+ // accept some number of extra bytes after the header, but if the offset points either to before
5622
+ // the header ends or implies a large amount of extra data, reject the file as malformed
5623
+ int header_limit = 1024; // max we actually read is below 256 bytes currently.
5624
+ int extra_data_limit = 256*4; // what ordinarily goes here is a palette; 256 entries*4 bytes is its max size.
5625
+ if (bytes_read_so_far <= 0 || bytes_read_so_far > header_limit) {
5626
+ return stbi__errpuc("bad header", "Corrupt BMP");
5627
+ }
5628
+ // we established that bytes_read_so_far is positive and sensible.
5629
+ // the first half of this test rejects offsets that are either too small positives, or
5630
+ // negative, and guarantees that info.offset >= bytes_read_so_far > 0. this in turn
5631
+ // ensures the number computed in the second half of the test can't overflow.
5632
+ if (info.offset < bytes_read_so_far || info.offset - bytes_read_so_far > extra_data_limit) {
5633
+ return stbi__errpuc("bad offset", "Corrupt BMP");
5634
+ } else {
5635
+ stbi__skip(s, info.offset - bytes_read_so_far);
5503
5636
  }
5504
5637
  }
5505
5638
 
@@ -5521,6 +5654,7 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5521
5654
  if (info.bpp < 16) {
5522
5655
  int z=0;
5523
5656
  if (psize == 0 || psize > 256) { STBI_FREE(out); return stbi__errpuc("invalid", "Corrupt BMP"); }
5657
+ memset(pal, 0, sizeof(pal));
5524
5658
  for (i=0; i < psize; ++i) {
5525
5659
  pal[i][2] = stbi__get8(s);
5526
5660
  pal[i][1] = stbi__get8(s);
@@ -5577,7 +5711,8 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5577
5711
  int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0;
5578
5712
  int z = 0;
5579
5713
  int easy=0;
5580
- 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
5581
5716
  if (info.bpp == 24) width = 3 * s->img_x;
5582
5717
  else if (info.bpp == 16) width = 2*s->img_x;
5583
5718
  else /* bpp = 32 and pad = 0 */ width=0;
@@ -5585,8 +5720,12 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5585
5720
  if (info.bpp == 24) {
5586
5721
  easy = 1;
5587
5722
  } else if (info.bpp == 32) {
5588
- 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)) {
5589
5726
  easy = 2;
5727
+ alpha_override = ma ? 0 : 255;
5728
+ }
5590
5729
  }
5591
5730
  if (!easy) {
5592
5731
  if (!mr || !mg || !mb) { STBI_FREE(out); return stbi__errpuc("bad masks", "Corrupt BMP"); }
@@ -5605,7 +5744,7 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5605
5744
  out[z+1] = stbi__get8(s);
5606
5745
  out[z+0] = stbi__get8(s);
5607
5746
  z += 3;
5608
- a = (easy == 2 ? stbi__get8(s) : 255);
5747
+ a = (easy == 2 ? (stbi__get8(s) | alpha_override) : 255);
5609
5748
  all_a |= a;
5610
5749
  if (target == 4) out[z++] = a;
5611
5750
  }
@@ -5631,16 +5770,8 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5631
5770
  for (i=4*s->img_x*s->img_y-1; i >= 0; i -= 4)
5632
5771
  out[i] = 255;
5633
5772
 
5634
- if (flip_vertically) {
5635
- stbi_uc t;
5636
- for (j=0; j < (int) s->img_y>>1; ++j) {
5637
- stbi_uc *p1 = out + j *s->img_x*target;
5638
- stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target;
5639
- for (i=0; i < (int) s->img_x*target; ++i) {
5640
- t = p1[i]; p1[i] = p2[i]; p2[i] = t;
5641
- }
5642
- }
5643
- }
5773
+ if (flip_vertically)
5774
+ stbi__vertical_flip(out, s->img_x, s->img_y, target);
5644
5775
 
5645
5776
  if (req_comp && req_comp != target) {
5646
5777
  out = stbi__convert_format(out, target, req_comp, s->img_x, s->img_y);
@@ -5657,115 +5788,80 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5657
5788
  // Targa Truevision - TGA
5658
5789
  // by Jonathan Dummer
5659
5790
  #ifndef STBI_NO_TGA
5791
+
5660
5792
  // returns STBI_rgb or whatever, 0 on error
5661
5793
  static int stbi__tga_get_comp(int bits_per_pixel, int is_grey, int* is_rgb16)
5662
5794
  {
5663
5795
  // only RGB or RGBA (incl. 16bit) or grey allowed
5664
5796
  if (is_rgb16) *is_rgb16 = 0;
5665
- switch(bits_per_pixel) {
5666
- case 8: return STBI_grey;
5667
- case 16: if(is_grey) return STBI_grey_alpha;
5668
- // fallthrough
5669
- case 15: if(is_rgb16) *is_rgb16 = 1;
5670
- return STBI_rgb;
5671
- case 24: // fallthrough
5672
- case 32: return bits_per_pixel/8;
5673
- default: return 0;
5674
- }
5675
- }
5676
-
5677
- static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp)
5678
- {
5679
- int tga_w, tga_h, tga_comp, tga_image_type, tga_bits_per_pixel, tga_colormap_bpp;
5680
- int sz, tga_colormap_type;
5681
- stbi__get8(s); // discard Offset
5682
- tga_colormap_type = stbi__get8(s); // colormap type
5683
- if( tga_colormap_type > 1 ) {
5684
- stbi__rewind(s);
5685
- return 0; // only RGB or indexed allowed
5686
- }
5687
- tga_image_type = stbi__get8(s); // image type
5688
- if ( tga_colormap_type == 1 ) { // colormapped (paletted) image
5689
- if (tga_image_type != 1 && tga_image_type != 9) {
5690
- stbi__rewind(s);
5691
- return 0;
5692
- }
5693
- stbi__skip(s,4); // skip index of first colormap entry and number of entries
5694
- sz = stbi__get8(s); // check bits per palette color entry
5695
- if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) {
5696
- stbi__rewind(s);
5697
- return 0;
5698
- }
5699
- stbi__skip(s,4); // skip image x and y origin
5700
- tga_colormap_bpp = sz;
5701
- } else { // "normal" image w/o colormap - only RGB or grey allowed, +/- RLE
5702
- if ( (tga_image_type != 2) && (tga_image_type != 3) && (tga_image_type != 10) && (tga_image_type != 11) ) {
5703
- stbi__rewind(s);
5704
- return 0; // only RGB or grey allowed, +/- RLE
5705
- }
5706
- stbi__skip(s,9); // skip colormap specification and image x/y origin
5707
- tga_colormap_bpp = 0;
5708
- }
5709
- tga_w = stbi__get16le(s);
5710
- if( tga_w < 1 ) {
5711
- stbi__rewind(s);
5712
- return 0; // test width
5713
- }
5714
- tga_h = stbi__get16le(s);
5715
- if( tga_h < 1 ) {
5716
- stbi__rewind(s);
5717
- return 0; // test height
5718
- }
5719
- tga_bits_per_pixel = stbi__get8(s); // bits per pixel
5720
- stbi__get8(s); // ignore alpha bits
5721
- if (tga_colormap_bpp != 0) {
5722
- if((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16)) {
5723
- // when using a colormap, tga_bits_per_pixel is the size of the indexes
5724
- // I don't think anything but 8 or 16bit indexes makes sense
5725
- stbi__rewind(s);
5726
- return 0;
5727
- }
5728
- tga_comp = stbi__tga_get_comp(tga_colormap_bpp, 0, NULL);
5729
- } else {
5730
- tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3) || (tga_image_type == 11), NULL);
5731
- }
5732
- if(!tga_comp) {
5733
- stbi__rewind(s);
5734
- return 0;
5735
- }
5736
- if (x) *x = tga_w;
5737
- if (y) *y = tga_h;
5738
- if (comp) *comp = tga_comp;
5739
- 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
+ }
5740
5809
  }
5741
5810
 
5742
- static int stbi__tga_test(stbi__context *s)
5811
+ static int stbi__tga_info_impl(stbi__context *s, int *x, int *y, int *comp)
5743
5812
  {
5744
- int res = 0;
5745
- int sz, tga_color_type;
5746
- stbi__get8(s); // discard Offset
5747
- tga_color_type = stbi__get8(s); // color type
5748
- if ( tga_color_type > 1 ) goto errorEnd; // only RGB or indexed allowed
5749
- sz = stbi__get8(s); // image type
5750
- if ( tga_color_type == 1 ) { // colormapped (paletted) image
5751
- 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;
5752
5821
  stbi__skip(s,4); // skip index of first colormap entry and number of entries
5753
5822
  sz = stbi__get8(s); // check bits per palette color entry
5754
- 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;
5755
5824
  stbi__skip(s,4); // skip image x and y origin
5756
- } else { // "normal" image w/o colormap
5757
- 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
+ }
5758
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);
5759
5844
  }
5760
- if ( stbi__get16le(s) < 1 ) goto errorEnd; // test width
5761
- if ( stbi__get16le(s) < 1 ) goto errorEnd; // test height
5762
- sz = stbi__get8(s); // bits per pixel
5763
- if ( (tga_color_type == 1) && (sz != 8) && (sz != 16) ) goto errorEnd; // for colormapped images, bpp is size of an index
5764
- 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
+ }
5765
5851
 
5766
- 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
+ }
5767
5861
 
5768
- errorEnd:
5862
+ static int stbi__tga_test(stbi__context *s)
5863
+ {
5864
+ int res = stbi__tga_info_impl(s, NULL, NULL, NULL);
5769
5865
  stbi__rewind(s);
5770
5866
  return res;
5771
5867
  }
@@ -5792,7 +5888,7 @@ static void stbi__tga_read_rgb16(stbi__context *s, stbi_uc* out)
5792
5888
 
5793
5889
  static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
5794
5890
  {
5795
- // read in the TGA header stuff
5891
+ // read in the TGA header
5796
5892
  int tga_offset = stbi__get8(s);
5797
5893
  int tga_indexed = stbi__get8(s);
5798
5894
  int tga_image_type = stbi__get8(s);
@@ -5806,34 +5902,33 @@ static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req
5806
5902
  int tga_height = stbi__get16le(s);
5807
5903
  int tga_bits_per_pixel = stbi__get8(s);
5808
5904
  int tga_comp, tga_rgb16=0;
5809
- int tga_inverted = stbi__get8(s);
5810
- // int tga_alpha_bits = tga_inverted & 15; // the 4 lowest bits - unused (useless?)
5905
+ int tga_desc = stbi__get8(s);
5811
5906
  // image data
5812
5907
  unsigned char *tga_data;
5813
5908
  unsigned char *tga_palette = NULL;
5814
5909
  int i, j;
5815
- unsigned char raw_data[4] = {0};
5910
+ unsigned char raw_data[4] = {0,0,0,0};
5816
5911
  int RLE_count = 0;
5817
5912
  int RLE_repeating = 0;
5818
5913
  int read_next_pixel = 1;
5819
5914
  STBI_NOTUSED(ri);
5820
- STBI_NOTUSED(tga_x_origin); // @TODO
5821
- STBI_NOTUSED(tga_y_origin); // @TODO
5822
5915
 
5823
- if (tga_height > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
5824
- 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");
5825
5920
 
5826
5921
  // do a tiny bit of precessing
5827
- if ( tga_image_type >= 8 )
5828
- {
5922
+ if (tga_image_type >= 8) {
5829
5923
  tga_image_type -= 8;
5830
5924
  tga_is_RLE = 1;
5831
5925
  }
5832
- tga_inverted = 1 - ((tga_inverted >> 5) & 1);
5833
5926
 
5834
5927
  // If I'm paletted, then I'll use the number of bits from the palette
5835
- if ( tga_indexed ) tga_comp = stbi__tga_get_comp(tga_palette_bits, 0, &tga_rgb16);
5836
- 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);
5837
5932
 
5838
5933
  if(!tga_comp) // shouldn't really happen, stbi__tga_test() should have ensured basic consistency
5839
5934
  return stbi__errpuc("bad format", "Can't find out TGA pixelformat");
@@ -5850,26 +5945,25 @@ static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req
5850
5945
  if (!tga_data) return stbi__errpuc("outofmem", "Out of memory");
5851
5946
 
5852
5947
  // skip to the data's starting position (offset usually = 0)
5853
- stbi__skip(s, tga_offset );
5948
+ stbi__skip(s, tga_offset);
5854
5949
 
5855
- if ( !tga_indexed && !tga_is_RLE && !tga_rgb16 ) {
5950
+ if (!tga_indexed && !tga_is_RLE && !tga_rgb16) {
5856
5951
  for (i=0; i < tga_height; ++i) {
5857
- int row = tga_inverted ? tga_height -i - 1 : i;
5858
- stbi_uc *tga_row = tga_data + row*tga_width*tga_comp;
5859
- 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
+ }
5860
5957
  }
5861
- } else {
5958
+ } else {
5862
5959
  // do I need to load a palette?
5863
- if ( tga_indexed)
5864
- {
5960
+ if (tga_indexed) {
5865
5961
  if (tga_palette_len == 0) { /* you have to have at least one entry! */
5866
5962
  STBI_FREE(tga_data);
5867
5963
  return stbi__errpuc("bad palette", "Corrupt TGA");
5868
5964
  }
5869
5965
 
5870
- // any data to skip? (offset usually = 0)
5871
- stbi__skip(s, tga_palette_start );
5872
- // load the palette
5966
+ stbi__skip(s, tga_palette_start);
5873
5967
  tga_palette = (unsigned char*)stbi__malloc_mad2(tga_palette_len, tga_comp, 0);
5874
5968
  if (!tga_palette) {
5875
5969
  STBI_FREE(tga_data);
@@ -5883,41 +5977,34 @@ static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req
5883
5977
  pal_entry += tga_comp;
5884
5978
  }
5885
5979
  } else if (!stbi__getn(s, tga_palette, tga_palette_len * tga_comp)) {
5886
- STBI_FREE(tga_data);
5887
- STBI_FREE(tga_palette);
5888
- 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");
5889
5983
  }
5890
5984
  }
5891
5985
  // load the data
5892
- for (i=0; i < tga_width * tga_height; ++i)
5893
- {
5986
+ for (i=0; i < tga_width * tga_height; ++i) {
5894
5987
  // if I'm in RLE mode, do I need to get a RLE stbi__pngchunk?
5895
- if ( tga_is_RLE )
5896
- {
5897
- if ( RLE_count == 0 )
5898
- {
5988
+ if (tga_is_RLE) {
5989
+ if (RLE_count == 0) {
5899
5990
  // yep, get the next byte as a RLE command
5900
5991
  int RLE_cmd = stbi__get8(s);
5901
5992
  RLE_count = 1 + (RLE_cmd & 127);
5902
5993
  RLE_repeating = RLE_cmd >> 7;
5903
5994
  read_next_pixel = 1;
5904
- } else if ( !RLE_repeating )
5905
- {
5995
+ } else if (!RLE_repeating) {
5906
5996
  read_next_pixel = 1;
5907
5997
  }
5908
- } else
5909
- {
5998
+ } else {
5910
5999
  read_next_pixel = 1;
5911
6000
  }
5912
6001
  // OK, if I need to read a pixel, do it now
5913
- if ( read_next_pixel )
5914
- {
6002
+ if (read_next_pixel) {
5915
6003
  // load however much data we did have
5916
- if ( tga_indexed )
5917
- {
6004
+ if (tga_indexed) {
5918
6005
  // read in index, then perform the lookup
5919
6006
  int pal_idx = (tga_bits_per_pixel == 8) ? stbi__get8(s) : stbi__get16le(s);
5920
- if ( pal_idx >= tga_palette_len ) {
6007
+ if (pal_idx >= tga_palette_len) {
5921
6008
  // invalid index
5922
6009
  pal_idx = 0;
5923
6010
  }
@@ -5945,36 +6032,37 @@ static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req
5945
6032
  // in case we're in RLE mode, keep counting down
5946
6033
  --RLE_count;
5947
6034
  }
5948
- // do I need to invert the image?
5949
- if ( tga_inverted )
5950
- {
5951
- for (j = 0; j*2 < tga_height; ++j)
5952
- {
5953
- int index1 = j * tga_width * tga_comp;
5954
- int index2 = (tga_height - 1 - j) * tga_width * tga_comp;
5955
- for (i = tga_width * tga_comp; i > 0; --i)
5956
- {
5957
- unsigned char temp = tga_data[index1];
5958
- tga_data[index1] = tga_data[index2];
5959
- tga_data[index2] = temp;
5960
- ++index1;
5961
- ++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;
5962
6053
  }
5963
6054
  }
5964
6055
  }
5965
- // clear my palette, if I had one
5966
- if ( tga_palette != NULL )
5967
- {
5968
- STBI_FREE( tga_palette );
5969
- }
5970
6056
  }
5971
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
+
5972
6062
  // swap RGB - if the source data was RGB16, it already is in the right order
5973
- if (tga_comp >= 3 && !tga_rgb16)
5974
- {
6063
+ if (tga_comp >= 3 && !tga_rgb16) {
5975
6064
  unsigned char* tga_pixel = tga_data;
5976
- for (i=0; i < tga_width * tga_height; ++i)
5977
- {
6065
+ for (i=0; i < tga_width * tga_height; ++i) {
5978
6066
  unsigned char temp = tga_pixel[0];
5979
6067
  tga_pixel[0] = tga_pixel[2];
5980
6068
  tga_pixel[2] = temp;
@@ -5986,11 +6074,11 @@ static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req
5986
6074
  if (req_comp && req_comp != tga_comp)
5987
6075
  tga_data = stbi__convert_format(tga_data, tga_comp, req_comp, tga_width, tga_height);
5988
6076
 
5989
- // the things I do to get rid of an error message, and yet keep
5990
- // Microsoft's C compilers happy... [8^(
5991
- tga_palette_start = tga_palette_len = tga_palette_bits =
5992
- tga_x_origin = tga_y_origin = 0;
5993
- 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
+
5994
6082
  // OK, done
5995
6083
  return tga_data;
5996
6084
  }
@@ -6248,247 +6336,14 @@ static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req
6248
6336
  #endif
6249
6337
 
6250
6338
  // *************************************************************************************************
6251
- // Softimage PIC loader
6252
- // by Tom Seddon
6253
- //
6254
- // See http://softimage.wiki.softimage.com/index.php/INFO:_PIC_file_format
6255
- // See http://ozviz.wasp.uwa.edu.au/~pbourke/dataformats/softimagepic/
6339
+ // GIF loader -- public domain by Jean-Marc Lienher -- simplified/shrunk by stb
6256
6340
 
6257
- #ifndef STBI_NO_PIC
6258
- static int stbi__pic_is4(stbi__context *s,const char *str)
6259
- {
6260
- int i;
6261
- for (i=0; i<4; ++i)
6262
- if (stbi__get8(s) != (stbi_uc)str[i])
6263
- return 0;
6264
-
6265
- return 1;
6266
- }
6267
-
6268
- static int stbi__pic_test_core(stbi__context *s)
6269
- {
6270
- int i;
6271
-
6272
- if (!stbi__pic_is4(s,"\x53\x80\xF6\x34"))
6273
- return 0;
6274
-
6275
- for(i=0;i<84;++i)
6276
- stbi__get8(s);
6277
-
6278
- if (!stbi__pic_is4(s,"PICT"))
6279
- return 0;
6280
-
6281
- return 1;
6282
- }
6283
-
6284
- typedef struct
6285
- {
6286
- stbi_uc size,type,channel;
6287
- } stbi__pic_packet;
6288
-
6289
- static stbi_uc *stbi__readval(stbi__context *s, int channel, stbi_uc *dest)
6290
- {
6291
- int mask=0x80, i;
6292
-
6293
- for (i=0; i<4; ++i, mask>>=1) {
6294
- if (channel & mask) {
6295
- if (stbi__at_eof(s)) return stbi__errpuc("bad file","PIC file too short");
6296
- dest[i]=stbi__get8(s);
6297
- }
6298
- }
6299
-
6300
- return dest;
6301
- }
6302
-
6303
- static void stbi__copyval(int channel,stbi_uc *dest,const stbi_uc *src)
6304
- {
6305
- int mask=0x80,i;
6306
-
6307
- for (i=0;i<4; ++i, mask>>=1)
6308
- if (channel&mask)
6309
- dest[i]=src[i];
6310
- }
6311
-
6312
- static stbi_uc *stbi__pic_load_core(stbi__context *s,int width,int height,int *comp, stbi_uc *result)
6313
- {
6314
- int act_comp=0,num_packets=0,y,chained;
6315
- stbi__pic_packet packets[10];
6316
-
6317
- // this will (should...) cater for even some bizarre stuff like having data
6318
- // for the same channel in multiple packets.
6319
- do {
6320
- stbi__pic_packet *packet;
6321
-
6322
- if (num_packets==sizeof(packets)/sizeof(packets[0]))
6323
- return stbi__errpuc("bad format","too many packets");
6324
-
6325
- packet = &packets[num_packets++];
6326
-
6327
- chained = stbi__get8(s);
6328
- packet->size = stbi__get8(s);
6329
- packet->type = stbi__get8(s);
6330
- packet->channel = stbi__get8(s);
6331
-
6332
- act_comp |= packet->channel;
6333
-
6334
- if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (reading packets)");
6335
- if (packet->size != 8) return stbi__errpuc("bad format","packet isn't 8bpp");
6336
- } while (chained);
6337
-
6338
- *comp = (act_comp & 0x10 ? 4 : 3); // has alpha channel?
6339
-
6340
- for(y=0; y<height; ++y) {
6341
- int packet_idx;
6342
-
6343
- for(packet_idx=0; packet_idx < num_packets; ++packet_idx) {
6344
- stbi__pic_packet *packet = &packets[packet_idx];
6345
- stbi_uc *dest = result+y*width*4;
6346
-
6347
- switch (packet->type) {
6348
- default:
6349
- return stbi__errpuc("bad format","packet has bad compression type");
6350
-
6351
- case 0: {//uncompressed
6352
- int x;
6353
-
6354
- for(x=0;x<width;++x, dest+=4)
6355
- if (!stbi__readval(s,packet->channel,dest))
6356
- return 0;
6357
- break;
6358
- }
6359
-
6360
- case 1://Pure RLE
6361
- {
6362
- int left=width, i;
6363
-
6364
- while (left>0) {
6365
- stbi_uc count,value[4];
6366
-
6367
- count=stbi__get8(s);
6368
- if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pure read count)");
6369
-
6370
- if (count > left)
6371
- count = (stbi_uc) left;
6372
-
6373
- if (!stbi__readval(s,packet->channel,value)) return 0;
6374
-
6375
- for(i=0; i<count; ++i,dest+=4)
6376
- stbi__copyval(packet->channel,dest,value);
6377
- left -= count;
6378
- }
6379
- }
6380
- break;
6381
-
6382
- case 2: {//Mixed RLE
6383
- int left=width;
6384
- while (left>0) {
6385
- int count = stbi__get8(s), i;
6386
- if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (mixed read count)");
6387
-
6388
- if (count >= 128) { // Repeated
6389
- stbi_uc value[4];
6390
-
6391
- if (count==128)
6392
- count = stbi__get16be(s);
6393
- else
6394
- count -= 127;
6395
- if (count > left)
6396
- return stbi__errpuc("bad file","scanline overrun");
6397
-
6398
- if (!stbi__readval(s,packet->channel,value))
6399
- return 0;
6400
-
6401
- for(i=0;i<count;++i, dest += 4)
6402
- stbi__copyval(packet->channel,dest,value);
6403
- } else { // Raw
6404
- ++count;
6405
- if (count>left) return stbi__errpuc("bad file","scanline overrun");
6406
-
6407
- for(i=0;i<count;++i, dest+=4)
6408
- if (!stbi__readval(s,packet->channel,dest))
6409
- return 0;
6410
- }
6411
- left-=count;
6412
- }
6413
- break;
6414
- }
6415
- }
6416
- }
6417
- }
6418
-
6419
- return result;
6420
- }
6421
-
6422
- static void *stbi__pic_load(stbi__context *s,int *px,int *py,int *comp,int req_comp, stbi__result_info *ri)
6423
- {
6424
- stbi_uc *result;
6425
- int i, x,y, internal_comp;
6426
- STBI_NOTUSED(ri);
6427
-
6428
- if (!comp) comp = &internal_comp;
6429
-
6430
- for (i=0; i<92; ++i)
6431
- stbi__get8(s);
6432
-
6433
- x = stbi__get16be(s);
6434
- y = stbi__get16be(s);
6435
-
6436
- if (y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
6437
- if (x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
6438
-
6439
- if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pic header)");
6440
- if (!stbi__mad3sizes_valid(x, y, 4, 0)) return stbi__errpuc("too large", "PIC image too large to decode");
6441
-
6442
- stbi__get32be(s); //skip `ratio'
6443
- stbi__get16be(s); //skip `fields'
6444
- stbi__get16be(s); //skip `pad'
6445
-
6446
- // intermediate buffer is RGBA
6447
- result = (stbi_uc *) stbi__malloc_mad3(x, y, 4, 0);
6448
- if (!result) return stbi__errpuc("outofmem", "Out of memory");
6449
- memset(result, 0xff, x*y*4);
6450
-
6451
- if (!stbi__pic_load_core(s,x,y,comp, result)) {
6452
- STBI_FREE(result);
6453
- result=0;
6454
- }
6455
- *px = x;
6456
- *py = y;
6457
- if (req_comp == 0) req_comp = *comp;
6458
- result=stbi__convert_format(result,4,req_comp,x,y);
6459
-
6460
- return result;
6461
- }
6462
-
6463
- static int stbi__pic_test(stbi__context *s)
6464
- {
6465
- int r = stbi__pic_test_core(s);
6466
- stbi__rewind(s);
6467
- return r;
6468
- }
6469
- #endif
6470
-
6471
- // *************************************************************************************************
6472
- // GIF loader -- public domain by Jean-Marc Lienher -- simplified/shrunk by stb
6473
-
6474
- #ifndef STBI_NO_GIF
6475
- typedef struct
6476
- {
6477
- stbi__int16 prefix;
6478
- stbi_uc first;
6479
- stbi_uc suffix;
6480
- } stbi__gif_lzw;
6481
-
6482
- typedef struct
6341
+ #ifndef STBI_NO_GIF
6342
+ typedef struct
6483
6343
  {
6484
6344
  int w,h;
6485
6345
  stbi_uc *out; // output buffer (always 4 components)
6486
- stbi_uc *background; // The current "background" as far as a gif is concerned
6487
- stbi_uc *history;
6488
6346
  int flags, bgindex, ratio, transparent, eflags;
6489
- stbi_uc pal[256][4];
6490
- stbi_uc lpal[256][4];
6491
- stbi__gif_lzw codes[8192];
6492
6347
  stbi_uc *color_table;
6493
6348
  int parse, step;
6494
6349
  int lflags;
@@ -6497,6 +6352,10 @@ typedef struct
6497
6352
  int cur_x, cur_y;
6498
6353
  int line_size;
6499
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];
6500
6359
  } stbi__gif;
6501
6360
 
6502
6361
  static int stbi__gif_test_raw(stbi__context *s)
@@ -6516,14 +6375,13 @@ static int stbi__gif_test(stbi__context *s)
6516
6375
  return r;
6517
6376
  }
6518
6377
 
6519
- 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)
6520
6379
  {
6521
6380
  int i;
6522
6381
  for (i=0; i < num_entries; ++i) {
6523
- pal[i][2] = stbi__get8(s);
6524
- pal[i][1] = stbi__get8(s);
6525
6382
  pal[i][0] = stbi__get8(s);
6526
- pal[i][3] = transp == i ? 0 : 255;
6383
+ pal[i][1] = stbi__get8(s);
6384
+ pal[i][2] = stbi__get8(s);
6527
6385
  }
6528
6386
  }
6529
6387
 
@@ -6545,15 +6403,19 @@ static int stbi__gif_header(stbi__context *s, stbi__gif *g, int *comp, int is_in
6545
6403
  g->ratio = stbi__get8(s);
6546
6404
  g->transparent = -1;
6547
6405
 
6548
- if (g->w > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)");
6549
- 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");
6550
6410
 
6551
6411
  if (comp != 0) *comp = 4; // can't actually tell whether it's 3 or 4 until we parse the comments
6552
6412
 
6553
6413
  if (is_info) return 1;
6554
6414
 
6555
6415
  if (g->flags & 0x80)
6556
- 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
6557
6419
 
6558
6420
  return 1;
6559
6421
  }
@@ -6573,50 +6435,14 @@ static int stbi__gif_info_raw(stbi__context *s, int *x, int *y, int *comp)
6573
6435
  return 1;
6574
6436
  }
6575
6437
 
6576
- static void stbi__out_gif_code(stbi__gif *g, stbi__uint16 code)
6577
- {
6578
- stbi_uc *p, *c;
6579
- int idx;
6580
-
6581
- // recurse to decode the prefixes, since the linked-list is backwards,
6582
- // and working backwards through an interleaved image would be nasty
6583
- if (g->codes[code].prefix >= 0)
6584
- stbi__out_gif_code(g, g->codes[code].prefix);
6585
-
6586
- if (g->cur_y >= g->max_y) return;
6587
-
6588
- idx = g->cur_x + g->cur_y;
6589
- p = &g->out[idx];
6590
- g->history[idx / 4] = 1;
6591
-
6592
- c = &g->color_table[g->codes[code].suffix * 4];
6593
- if (c[3] > 128) { // don't render transparent pixels;
6594
- p[0] = c[2];
6595
- p[1] = c[1];
6596
- p[2] = c[0];
6597
- p[3] = c[3];
6598
- }
6599
- g->cur_x += 4;
6600
-
6601
- if (g->cur_x >= g->max_x) {
6602
- g->cur_x = g->start_x;
6603
- g->cur_y += g->step;
6604
-
6605
- while (g->cur_y >= g->max_y && g->parse > 0) {
6606
- g->step = (1 << g->parse) * g->line_size;
6607
- g->cur_y = g->start_y + (g->step >> 1);
6608
- --g->parse;
6609
- }
6610
- }
6611
- }
6612
-
6613
6438
  static stbi_uc *stbi__process_gif_raster(stbi__context *s, stbi__gif *g)
6614
6439
  {
6615
6440
  stbi_uc lzw_cs;
6616
6441
  stbi__int32 len, init_code;
6617
6442
  stbi__uint32 first;
6618
- stbi__int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear;
6619
- stbi__gif_lzw *p;
6443
+ stbi__int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear, cur;
6444
+ stbi_uc revbuf[4096];
6445
+ int revind;
6620
6446
 
6621
6447
  lzw_cs = stbi__get8(s);
6622
6448
  if (lzw_cs > 12) return NULL;
@@ -6627,9 +6453,8 @@ static stbi_uc *stbi__process_gif_raster(stbi__context *s, stbi__gif *g)
6627
6453
  bits = 0;
6628
6454
  valid_bits = 0;
6629
6455
  for (init_code = 0; init_code < clear; init_code++) {
6630
- g->codes[init_code].prefix = -1;
6631
- g->codes[init_code].first = (stbi_uc) init_code;
6632
- g->codes[init_code].suffix = (stbi_uc) init_code;
6456
+ g->prefix[init_code] = 0;
6457
+ g->suffix[init_code] = (stbi_uc)init_code;
6633
6458
  }
6634
6459
 
6635
6460
  // support no starting clear code
@@ -6638,71 +6463,109 @@ static stbi_uc *stbi__process_gif_raster(stbi__context *s, stbi__gif *g)
6638
6463
 
6639
6464
  len = 0;
6640
6465
  for(;;) {
6641
- if (valid_bits < codesize) {
6466
+ stbi__int32 code;
6467
+ while (valid_bits < codesize) {
6642
6468
  if (len == 0) {
6643
6469
  len = stbi__get8(s); // start new block
6644
6470
  if (len == 0)
6645
6471
  return g->out;
6646
6472
  }
6647
6473
  --len;
6648
- bits |= (stbi__int32) stbi__get8(s) << valid_bits;
6474
+ bits |= (stbi__int32)stbi__get8(s) << valid_bits;
6649
6475
  valid_bits += 8;
6650
- } else {
6651
- stbi__int32 code = bits & codemask;
6652
- bits >>= codesize;
6653
- valid_bits -= codesize;
6654
- // @OPTIMIZE: is there some way we can accelerate the non-clear path?
6655
- if (code == clear) { // clear code
6656
- codesize = lzw_cs + 1;
6657
- codemask = (1 << codesize) - 1;
6658
- avail = clear + 2;
6659
- oldcode = -1;
6660
- first = 0;
6661
- } else if (code == clear + 1) { // end of stream code
6662
- stbi__skip(s, len);
6663
- while ((len = stbi__get8(s)) > 0)
6664
- stbi__skip(s,len);
6665
- return g->out;
6666
- } else if (code <= avail) {
6667
- if (first) {
6668
- return stbi__errpuc("no clear code", "Corrupt GIF");
6669
- }
6476
+ }
6670
6477
 
6671
- if (oldcode >= 0) {
6672
- p = &g->codes[avail++];
6673
- if (avail > 8192) {
6674
- return stbi__errpuc("too many codes", "Corrupt GIF");
6675
- }
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
+ }
6676
6497
 
6677
- p->prefix = (stbi__int16) oldcode;
6678
- p->first = g->codes[oldcode].first;
6679
- p->suffix = (code == avail) ? p->first : g->codes[code].first;
6680
- } else if (code == avail)
6498
+ revind = 4096;
6499
+ cur = code;
6500
+ if (code == avail) { // self-reference case
6501
+ if (oldcode < 0)
6681
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;
6682
6545
 
6683
- 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;
6684
6549
 
6685
- if ((avail & codemask) == 0 && avail <= 0x0FFF) {
6686
- codesize++;
6687
- 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
+ }
6688
6555
  }
6689
-
6690
- oldcode = code;
6691
- } else {
6692
- return stbi__errpuc("illegal code in raster", "Corrupt GIF");
6693
6556
  }
6557
+ } else {
6558
+ return stbi__errpuc("illegal code in raster", "Corrupt GIF");
6694
6559
  }
6695
6560
  }
6696
6561
  }
6697
6562
 
6698
6563
  // this function is designed to support animated gifs, although stb_image doesn't support it
6699
6564
  // two back is the image from two frames ago, used for a very specific disposal format
6700
- 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)
6701
6566
  {
6702
- int dispose;
6703
- int first_frame;
6704
- int pi;
6705
- int pcount;
6567
+ int dispose, first_frame;
6568
+ int x, y, w, h;
6706
6569
  STBI_NOTUSED(req_comp);
6707
6570
 
6708
6571
  // on first frame, any non-written pixels get the background colour (non-transparent)
@@ -6711,64 +6574,44 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i
6711
6574
  if (!stbi__gif_header(s, g, comp,0)) return 0; // stbi__g_failure_reason set by stbi__gif_header
6712
6575
  if (!stbi__mad3sizes_valid(4, g->w, g->h, 0))
6713
6576
  return stbi__errpuc("too large", "GIF image is too large");
6714
- pcount = g->w * g->h;
6715
- g->out = (stbi_uc *) stbi__malloc(4 * pcount);
6716
- g->background = (stbi_uc *) stbi__malloc(4 * pcount);
6717
- g->history = (stbi_uc *) stbi__malloc(pcount);
6718
- if (!g->out || !g->background || !g->history)
6577
+ g->out = (stbi_uc *) stbi__malloc(4 * g->w * g->h);
6578
+ if (!g->out)
6719
6579
  return stbi__errpuc("outofmem", "Out of memory");
6720
6580
 
6721
- // image is treated as "transparent" at the start - ie, nothing overwrites the current background;
6722
- // background colour is only used for pixels that are not rendered first frame, after that "background"
6723
- // color refers to the color that was there the previous frame.
6724
- memset(g->out, 0x00, 4 * pcount);
6725
- memset(g->background, 0x00, 4 * pcount); // state of the background (starts transparent)
6726
- memset(g->history, 0x00, pcount); // pixels that were affected previous frame
6727
6581
  first_frame = 1;
6582
+ memset(g->out, 0, 4 * g->w * g->h); // clear to transparent
6728
6583
  } else {
6729
6584
  // second frame - how do we dispose of the previous one?
6730
6585
  dispose = (g->eflags & 0x1C) >> 2;
6731
- pcount = g->w * g->h;
6732
-
6733
- if ((dispose == 3) && (two_back == 0)) {
6586
+ if ((dispose == 3) && !saved) {
6734
6587
  dispose = 2; // if I don't have an image to revert back to, default to the old background
6735
6588
  }
6736
6589
 
6737
6590
  if (dispose == 3) { // use previous graphic
6738
- for (pi = 0; pi < pcount; ++pi) {
6739
- if (g->history[pi]) {
6740
- memcpy( &g->out[pi * 4], &two_back[pi * 4], 4 );
6741
- }
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);
6742
6593
  }
6743
- } else if (dispose == 2) {
6744
- // restore what was changed last frame to background before that frame;
6745
- for (pi = 0; pi < pcount; ++pi) {
6746
- if (g->history[pi]) {
6747
- 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;
6748
6601
  }
6602
+ for (x = g->start_x; x < g->max_x; ++x)
6603
+ g->out[y + x] = bg[x & 3];
6749
6604
  }
6750
6605
  } else {
6751
- // This is a non-disposal case eithe way, so just
6752
- // leave the pixels as is, and they will become the new background
6753
- // 1: do not dispose
6754
- // 0: not specified.
6606
+ // This is a non-disposal case either way, just leave the pixels as they are.
6755
6607
  }
6756
-
6757
- // background is what out is after the undoing of the previou frame;
6758
- memcpy( g->background, g->out, 4 * g->w * g->h );
6759
6608
  }
6760
6609
 
6761
- // clear my history;
6762
- memset( g->history, 0x00, g->w * g->h ); // pixels that were affected previous frame
6763
-
6764
6610
  for (;;) {
6765
6611
  int tag = stbi__get8(s);
6766
6612
  switch (tag) {
6767
6613
  case 0x2C: /* Image Descriptor */
6768
6614
  {
6769
- stbi__int32 x, y, w, h;
6770
- stbi_uc *o;
6771
-
6772
6615
  x = stbi__get16le(s);
6773
6616
  y = stbi__get16le(s);
6774
6617
  w = stbi__get16le(s);
@@ -6802,29 +6645,24 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i
6802
6645
  }
6803
6646
 
6804
6647
  if (g->lflags & 0x80) {
6805
- 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));
6806
6649
  g->color_table = (stbi_uc *) g->lpal;
6807
6650
  } else if (g->flags & 0x80) {
6808
6651
  g->color_table = (stbi_uc *) g->pal;
6809
6652
  } else
6810
6653
  return stbi__errpuc("missing color table", "Corrupt GIF");
6811
6654
 
6812
- o = stbi__process_gif_raster(s, g);
6813
- if (!o) return NULL;
6814
-
6815
- // if this was the first frame,
6816
- pcount = g->w * g->h;
6817
- if (first_frame && (g->bgindex > 0)) {
6818
- // if first frame, any pixel not drawn to gets the background color
6819
- for (pi = 0; pi < pcount; ++pi) {
6820
- if (g->history[pi] == 0) {
6821
- g->pal[g->bgindex][3] = 255; // just in case it was made transparent, undo that; It will be reset next frame if need be;
6822
- memcpy( &g->out[pi * 4], &g->pal[g->bgindex], 4 );
6823
- }
6824
- }
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];
6825
6663
  }
6826
6664
 
6827
- return o;
6665
+ return stbi__process_gif_raster(s, g);
6828
6666
  }
6829
6667
 
6830
6668
  case 0x21: // Comment Extension.
@@ -6837,20 +6675,9 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i
6837
6675
  g->eflags = stbi__get8(s);
6838
6676
  g->delay = 10 * stbi__get16le(s); // delay - 1/100th of a second, saving as 1/1000ths.
6839
6677
 
6840
- // unset old transparent
6841
- if (g->transparent >= 0) {
6842
- g->pal[g->transparent][3] = 255;
6843
- }
6844
- if (g->eflags & 0x01) {
6845
- g->transparent = stbi__get8(s);
6846
- if (g->transparent >= 0) {
6847
- g->pal[g->transparent][3] = 0;
6848
- }
6849
- } else {
6850
- // don't need transparent
6851
- stbi__skip(s, 1);
6678
+ g->transparent = stbi__get8(s);
6679
+ if ((g->eflags & 0x01) == 0)
6852
6680
  g->transparent = -1;
6853
- }
6854
6681
  } else {
6855
6682
  stbi__skip(s, len);
6856
6683
  break;
@@ -6871,134 +6698,164 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i
6871
6698
  }
6872
6699
  }
6873
6700
 
6874
- static void *stbi__load_gif_main_outofmem(stbi__gif *g, stbi_uc *out, int **delays)
6875
- {
6876
- STBI_FREE(g->out);
6877
- STBI_FREE(g->history);
6878
- STBI_FREE(g->background);
6879
-
6880
- if (out) STBI_FREE(out);
6881
- if (delays && *delays) STBI_FREE(*delays);
6882
- return stbi__errpuc("outofmem", "Out of memory");
6883
- }
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;
6884
6706
 
6885
6707
  static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp)
6886
6708
  {
6887
- if (stbi__gif_test(s)) {
6888
- int layers = 0;
6889
- stbi_uc *u = 0;
6890
- stbi_uc *out = 0;
6891
- stbi_uc *two_back = 0;
6892
- stbi__gif g;
6893
- int stride;
6894
- int out_size = 0;
6895
- 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));
6896
6734
 
6897
- STBI_NOTUSED(out_size);
6898
- 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;
6899
6743
 
6900
- memset(&g, 0, sizeof(g));
6901
- if (delays) {
6902
- *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;
6903
6761
  }
6904
6762
 
6905
- do {
6906
- u = stbi__gif_load_next(s, &g, comp, req_comp, two_back);
6907
- if (u == (stbi_uc *) s) u = 0; // end of animated gif marker
6908
-
6909
- if (u) {
6910
- *x = g.w;
6911
- *y = g.h;
6912
- ++layers;
6913
- stride = g.w * g.h * 4;
6914
-
6915
- if (out) {
6916
- void *tmp = (stbi_uc*) STBI_REALLOC_SIZED( out, out_size, layers * stride );
6917
- if (!tmp)
6918
- return stbi__load_gif_main_outofmem(&g, out, delays);
6919
- else {
6920
- out = (stbi_uc*) tmp;
6921
- out_size = layers * stride;
6922
- }
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;
6923
6768
 
6924
- if (delays) {
6925
- int *new_delays = (int*) STBI_REALLOC_SIZED( *delays, delays_size, sizeof(int) * layers );
6926
- if (!new_delays)
6927
- return stbi__load_gif_main_outofmem(&g, out, delays);
6928
- *delays = new_delays;
6929
- delays_size = layers * sizeof(int);
6930
- }
6931
- } else {
6932
- out = (stbi_uc*)stbi__malloc( layers * stride );
6933
- if (!out)
6934
- return stbi__load_gif_main_outofmem(&g, out, delays);
6935
- out_size = layers * stride;
6936
- if (delays) {
6937
- *delays = (int*) stbi__malloc( layers * sizeof(int) );
6938
- if (!*delays)
6939
- return stbi__load_gif_main_outofmem(&g, out, delays);
6940
- delays_size = layers * sizeof(int);
6941
- }
6942
- }
6943
- memcpy( out + ((layers - 1) * stride), u, stride );
6944
- if (layers >= 2) {
6945
- two_back = out - 2 * stride;
6946
- }
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
+ }
6947
6774
 
6948
- if (delays) {
6949
- (*delays)[layers - 1U] = g.delay;
6950
- }
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);
6951
6799
  }
6952
- } while (u != 0);
6800
+ }
6953
6801
 
6954
- // free temp buffer;
6955
- STBI_FREE(g.out);
6956
- STBI_FREE(g.history);
6957
- STBI_FREE(g.background);
6802
+ frame = frames->prev;
6803
+ STBI_FREE(frames); // free current frame
6804
+ frames = frame;
6805
+ }
6958
6806
 
6959
- // do the final conversion after loading everything;
6960
- if (req_comp && req_comp != 4)
6961
- 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);
6962
6810
 
6963
- *z = layers;
6964
- return out;
6965
- } else {
6966
- 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;
6967
6815
  }
6816
+
6817
+ STBI_FREE(g);
6818
+ *z = layers;
6819
+ return out;
6968
6820
  }
6969
6821
 
6970
6822
  static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
6971
6823
  {
6972
6824
  stbi_uc *u = 0;
6973
- stbi__gif g;
6974
- 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
+
6975
6831
  STBI_NOTUSED(ri);
6976
6832
 
6977
- u = stbi__gif_load_next(s, &g, comp, req_comp, 0);
6833
+ u = stbi__gif_load_next(s, g, comp, req_comp, 0);
6978
6834
  if (u == (stbi_uc *) s) u = 0; // end of animated gif marker
6979
6835
  if (u) {
6980
- *x = g.w;
6981
- *y = g.h;
6836
+ *x = g->w;
6837
+ *y = g->h;
6982
6838
 
6983
6839
  // moved conversion to after successful load so that the same
6984
6840
  // can be done for multiple frames.
6985
6841
  if (req_comp && req_comp != 4)
6986
- u = stbi__convert_format(u, 4, req_comp, g.w, g.h);
6987
- } else if (g.out) {
6842
+ u = stbi__convert_format(u, 4, req_comp, g->w, g->h);
6843
+ } else if (g->out) {
6988
6844
  // if there was an error and we allocated an image buffer, free it!
6989
- STBI_FREE(g.out);
6845
+ STBI_FREE(g->out);
6990
6846
  }
6991
-
6992
- // free buffers needed for multiple frame loading;
6993
- STBI_FREE(g.history);
6994
- STBI_FREE(g.background);
6847
+ STBI_FREE(g);
6995
6848
 
6996
6849
  return u;
6997
6850
  }
6998
6851
 
6999
- 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)
7000
6853
  {
7001
- 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;
7002
6859
  }
7003
6860
  #endif
7004
6861
 
@@ -7050,31 +6907,93 @@ static char *stbi__hdr_gettoken(stbi__context *z, char *buffer)
7050
6907
  return buffer;
7051
6908
  }
7052
6909
 
7053
- 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)
7054
6911
  {
7055
- if ( input[3] != 0 ) {
7056
- float f1;
7057
- // Exponent
7058
- f1 = (float) ldexp(1.0f, input[3] - (int)(128 + 8));
7059
- if (req_comp <= 2)
7060
- output[0] = (input[0] + input[1] + input[2]) * f1 / 3;
7061
- else {
7062
- output[0] = input[0] * f1;
7063
- output[1] = input[1] * f1;
7064
- output[2] = input[2] * f1;
7065
- }
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;
7066
6928
  if (req_comp == 2) output[1] = 1;
7067
- if (req_comp == 4) output[3] = 1;
7068
6929
  } else {
7069
- switch (req_comp) {
7070
- case 4: output[3] = 1; /* fallthrough */
7071
- case 3: output[0] = output[1] = output[2] = 0;
7072
- break;
7073
- case 2: output[1] = 1; /* fallthrough */
7074
- case 1: output[0] = 0;
7075
- 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;
7076
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
+ }
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);
7077
6994
  }
6995
+
6996
+ return 1;
7078
6997
  }
7079
6998
 
7080
6999
  static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
@@ -7083,14 +7002,15 @@ static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int re
7083
7002
  char *token;
7084
7003
  int valid = 0;
7085
7004
  int width, height;
7086
- stbi_uc *scanline;
7005
+ stbi_uc *scanline = NULL;
7087
7006
  float *hdr_data;
7088
- int len;
7089
- unsigned char count, value;
7090
- int i, j, k, c1,c2, z;
7007
+ int isrle;
7091
7008
  const char *headerToken;
7092
7009
  STBI_NOTUSED(ri);
7093
7010
 
7011
+ if (req_comp < 0 || req_comp > 4)
7012
+ return stbi__errpf("bad req_comp", "Invalid number of required components");
7013
+
7094
7014
  // Check identifier
7095
7015
  headerToken = stbi__hdr_gettoken(s,buffer);
7096
7016
  if (strcmp(headerToken, "#?RADIANCE") != 0 && strcmp(headerToken, "#?RGBE") != 0)
@@ -7116,8 +7036,9 @@ static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int re
7116
7036
  token += 3;
7117
7037
  width = (int) strtol(token, NULL, 10);
7118
7038
 
7119
- if (height > STBI_MAX_DIMENSIONS) return stbi__errpf("too large","Very large image (corrupt?)");
7120
- 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
+ }
7121
7042
 
7122
7043
  *x = width;
7123
7044
  *y = height;
@@ -7133,91 +7054,33 @@ static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int re
7133
7054
  if (!hdr_data)
7134
7055
  return stbi__errpf("outofmem", "Out of memory");
7135
7056
 
7136
- // Load image data
7137
- // image data is stored as some number of sca
7138
- if ( width < 8 || width >= 32768) {
7139
- // Read flat data
7140
- for (j=0; j < height; ++j) {
7141
- for (i=0; i < width; ++i) {
7142
- stbi_uc rgbe[4];
7143
- main_decode_loop:
7144
- stbi__getn(s, rgbe, 4);
7145
- stbi__hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp);
7146
- }
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");
7147
7064
  }
7148
- } else {
7149
- // Read RLE-encoded data
7150
- scanline = NULL;
7151
-
7152
- for (j = 0; j < height; ++j) {
7153
- c1 = stbi__get8(s);
7154
- c2 = stbi__get8(s);
7155
- len = stbi__get8(s);
7156
- if (c1 != 2 || c2 != 2 || (len & 0x80)) {
7157
- // not run-length encoded, so we have to actually use THIS data as a decoded
7158
- // pixel (note this can't be a valid pixel--one of RGB must be >= 128)
7159
- stbi_uc rgbe[4];
7160
- rgbe[0] = (stbi_uc) c1;
7161
- rgbe[1] = (stbi_uc) c2;
7162
- rgbe[2] = (stbi_uc) len;
7163
- rgbe[3] = (stbi_uc) stbi__get8(s);
7164
- stbi__hdr_convert(hdr_data, rgbe, req_comp);
7165
- i = 1;
7166
- j = 0;
7167
- STBI_FREE(scanline);
7168
- goto main_decode_loop; // yes, this makes no sense
7169
- }
7170
- len <<= 8;
7171
- len |= stbi__get8(s);
7172
- if (len != width) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("invalid decoded scanline length", "corrupt HDR"); }
7173
- if (scanline == NULL) {
7174
- scanline = (stbi_uc *) stbi__malloc_mad2(width, 4, 0);
7175
- if (!scanline) {
7176
- STBI_FREE(hdr_data);
7177
- return stbi__errpf("outofmem", "Out of memory");
7178
- }
7179
- }
7065
+ }
7180
7066
 
7181
- for (k = 0; k < 4; ++k) {
7182
- int nleft;
7183
- i = 0;
7184
- while ((nleft = width - i) > 0) {
7185
- count = stbi__get8(s);
7186
- if (count > 128) {
7187
- // Run
7188
- value = stbi__get8(s);
7189
- count -= 128;
7190
- if (count > nleft) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); }
7191
- for (z = 0; z < count; ++z)
7192
- scanline[i++ * 4 + k] = value;
7193
- } else {
7194
- // Dump
7195
- if (count > nleft) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); }
7196
- for (z = 0; z < count; ++z)
7197
- scanline[i++ * 4 + k] = stbi__get8(s);
7198
- }
7199
- }
7200
- }
7201
- for (i=0; i < width; ++i)
7202
- stbi__hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp);
7203
- }
7204
- if (scanline)
7205
- 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;
7206
7071
  }
7207
7072
 
7073
+ if (scanline)
7074
+ STBI_FREE(scanline);
7075
+
7208
7076
  return hdr_data;
7209
7077
  }
7210
7078
 
7211
- 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)
7212
7080
  {
7213
7081
  char buffer[STBI__HDR_BUFLEN];
7214
7082
  char *token;
7215
- int valid = 0;
7216
- int dummy;
7217
-
7218
- if (!x) x = &dummy;
7219
- if (!y) y = &dummy;
7220
- if (!comp) comp = &dummy;
7083
+ int valid = 0, x, y;
7221
7084
 
7222
7085
  if (stbi__hdr_test(s) == 0) {
7223
7086
  stbi__rewind( s );
@@ -7240,21 +7103,26 @@ static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp)
7240
7103
  return 0;
7241
7104
  }
7242
7105
  token += 3;
7243
- *y = (int) strtol(token, &token, 10);
7106
+ y = (int) strtol(token, &token, 10);
7244
7107
  while (*token == ' ') ++token;
7245
7108
  if (strncmp(token, "+X ", 3)) {
7246
7109
  stbi__rewind( s );
7247
7110
  return 0;
7248
7111
  }
7249
7112
  token += 3;
7250
- *x = (int) strtol(token, NULL, 10);
7251
- *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;
7252
7120
  return 1;
7253
7121
  }
7254
7122
  #endif // STBI_NO_HDR
7255
7123
 
7256
7124
  #ifndef STBI_NO_BMP
7257
- 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)
7258
7126
  {
7259
7127
  void *p;
7260
7128
  stbi__bmp_data info;
@@ -7265,25 +7133,21 @@ static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp)
7265
7133
  stbi__rewind( s );
7266
7134
  return 0;
7267
7135
  }
7268
- if (x) *x = s->img_x;
7269
- if (y) *y = s->img_y;
7270
- if (comp) {
7271
- if (info.bpp == 24 && info.ma == 0xff000000)
7272
- *comp = 3;
7273
- else
7274
- *comp = info.ma ? 4 : 3;
7275
- }
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;
7276
7143
  return 1;
7277
7144
  }
7278
7145
  #endif
7279
7146
 
7280
7147
  #ifndef STBI_NO_PSD
7281
- 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)
7282
7149
  {
7283
- int channelCount, dummy, depth;
7284
- if (!x) x = &dummy;
7285
- if (!y) y = &dummy;
7286
- if (!comp) comp = &dummy;
7150
+ int channelCount, depth, x, y;
7287
7151
  if (stbi__get32be(s) != 0x38425053) {
7288
7152
  stbi__rewind( s );
7289
7153
  return 0;
@@ -7298,8 +7162,8 @@ static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp)
7298
7162
  stbi__rewind( s );
7299
7163
  return 0;
7300
7164
  }
7301
- *y = stbi__get32be(s);
7302
- *x = stbi__get32be(s);
7165
+ y = stbi__get32be(s);
7166
+ x = stbi__get32be(s);
7303
7167
  depth = stbi__get16be(s);
7304
7168
  if (depth != 8 && depth != 16) {
7305
7169
  stbi__rewind( s );
@@ -7309,93 +7173,11 @@ static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp)
7309
7173
  stbi__rewind( s );
7310
7174
  return 0;
7311
7175
  }
7312
- *comp = 4;
7313
- return 1;
7314
- }
7315
-
7316
- static int stbi__psd_is16(stbi__context *s)
7317
- {
7318
- int channelCount, depth;
7319
- if (stbi__get32be(s) != 0x38425053) {
7320
- stbi__rewind( s );
7321
- return 0;
7322
- }
7323
- if (stbi__get16be(s) != 1) {
7324
- stbi__rewind( s );
7325
- return 0;
7326
- }
7327
- stbi__skip(s, 6);
7328
- channelCount = stbi__get16be(s);
7329
- if (channelCount < 0 || channelCount > 16) {
7330
- stbi__rewind( s );
7331
- return 0;
7332
- }
7333
- STBI_NOTUSED(stbi__get32be(s));
7334
- STBI_NOTUSED(stbi__get32be(s));
7335
- depth = stbi__get16be(s);
7336
- if (depth != 16) {
7337
- stbi__rewind( s );
7338
- return 0;
7339
- }
7340
- return 1;
7341
- }
7342
- #endif
7343
-
7344
- #ifndef STBI_NO_PIC
7345
- static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp)
7346
- {
7347
- int act_comp=0,num_packets=0,chained,dummy;
7348
- stbi__pic_packet packets[10];
7349
-
7350
- if (!x) x = &dummy;
7351
- if (!y) y = &dummy;
7352
- if (!comp) comp = &dummy;
7353
-
7354
- if (!stbi__pic_is4(s,"\x53\x80\xF6\x34")) {
7355
- stbi__rewind(s);
7356
- return 0;
7357
- }
7358
-
7359
- stbi__skip(s, 88);
7360
-
7361
- *x = stbi__get16be(s);
7362
- *y = stbi__get16be(s);
7363
- if (stbi__at_eof(s)) {
7364
- stbi__rewind( s);
7365
- return 0;
7366
- }
7367
- if ( (*x) != 0 && (1 << 28) / (*x) < (*y)) {
7368
- stbi__rewind( s );
7369
- return 0;
7370
- }
7371
-
7372
- stbi__skip(s, 8);
7373
-
7374
- do {
7375
- stbi__pic_packet *packet;
7376
-
7377
- if (num_packets==sizeof(packets)/sizeof(packets[0]))
7378
- return 0;
7379
-
7380
- packet = &packets[num_packets++];
7381
- chained = stbi__get8(s);
7382
- packet->size = stbi__get8(s);
7383
- packet->type = stbi__get8(s);
7384
- packet->channel = stbi__get8(s);
7385
- act_comp |= packet->channel;
7386
-
7387
- if (stbi__at_eof(s)) {
7388
- stbi__rewind( s );
7389
- return 0;
7390
- }
7391
- if (packet->size != 8) {
7392
- stbi__rewind( s );
7393
- return 0;
7394
- }
7395
- } while (chained);
7396
-
7397
- *comp = (act_comp & 0x10 ? 4 : 3);
7398
-
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;
7399
7181
  return 1;
7400
7182
  }
7401
7183
  #endif
@@ -7406,10 +7188,6 @@ static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp)
7406
7188
  //
7407
7189
  // PGM: http://netpbm.sourceforge.net/doc/pgm.html
7408
7190
  // PPM: http://netpbm.sourceforge.net/doc/ppm.html
7409
- //
7410
- // Known limitations:
7411
- // Does not support comments in the header section
7412
- // Does not support ASCII image data (formats P2 and P3)
7413
7191
 
7414
7192
  #ifndef STBI_NO_PNM
7415
7193
 
@@ -7418,49 +7196,19 @@ static int stbi__pnm_test(stbi__context *s)
7418
7196
  char p, t;
7419
7197
  p = (char) stbi__get8(s);
7420
7198
  t = (char) stbi__get8(s);
7421
- if (p != 'P' || (t != '5' && t != '6')) {
7199
+ if (p != 'P' || (t != '2' && t != '3' && t != '5' && t != '6')) {
7422
7200
  stbi__rewind( s );
7423
7201
  return 0;
7424
7202
  }
7425
7203
  return 1;
7426
7204
  }
7427
7205
 
7428
- static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
7429
- {
7430
- stbi_uc *out;
7431
- STBI_NOTUSED(ri);
7432
-
7433
- ri->bits_per_channel = stbi__pnm_info(s, (int *)&s->img_x, (int *)&s->img_y, (int *)&s->img_n);
7434
- if (ri->bits_per_channel == 0)
7435
- return 0;
7436
-
7437
- if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
7438
- if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
7439
-
7440
- *x = s->img_x;
7441
- *y = s->img_y;
7442
- if (comp) *comp = s->img_n;
7443
-
7444
- if (!stbi__mad4sizes_valid(s->img_n, s->img_x, s->img_y, ri->bits_per_channel / 8, 0))
7445
- return stbi__errpuc("too large", "PNM too large");
7446
-
7447
- out = (stbi_uc *) stbi__malloc_mad4(s->img_n, s->img_x, s->img_y, ri->bits_per_channel / 8, 0);
7448
- if (!out) return stbi__errpuc("outofmem", "Out of memory");
7449
- stbi__getn(s, out, s->img_n * s->img_x * s->img_y * (ri->bits_per_channel / 8));
7450
-
7451
- if (req_comp && req_comp != s->img_n) {
7452
- out = stbi__convert_format(out, s->img_n, req_comp, s->img_x, s->img_y);
7453
- if (out == NULL) return out; // stbi__convert_format frees input on failure
7454
- }
7455
- return out;
7456
- }
7457
-
7458
- static int stbi__pnm_isspace(char c)
7206
+ static int stbi__pnm_isspace(char c)
7459
7207
  {
7460
7208
  return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r';
7461
7209
  }
7462
7210
 
7463
- static void stbi__pnm_skip_whitespace(stbi__context *s, char *c)
7211
+ static void stbi__pnm_skip_whitespace(stbi__context *s, char *c)
7464
7212
  {
7465
7213
  for (;;) {
7466
7214
  while (!stbi__at_eof(s) && stbi__pnm_isspace(*c))
@@ -7474,126 +7222,213 @@ static void stbi__pnm_skip_whitespace(stbi__context *s, char *c)
7474
7222
  }
7475
7223
  }
7476
7224
 
7477
- static int stbi__pnm_isdigit(char c)
7478
- {
7479
- return c >= '0' && c <= '9';
7480
- }
7481
-
7482
- static int stbi__pnm_getinteger(stbi__context *s, char *c)
7225
+ static int stbi__pnm_getinteger(stbi__context *s, char *c)
7483
7226
  {
7484
7227
  int value = 0;
7485
7228
 
7486
- while (!stbi__at_eof(s) && stbi__pnm_isdigit(*c)) {
7229
+ while (!stbi__at_eof(s) && *c >= '0' && *c <= '9') {
7487
7230
  value = value*10 + (*c - '0');
7488
7231
  *c = (char) stbi__get8(s);
7232
+ if((value > 214748364) || (value == 214748364 && *c > '7'))
7233
+ return stbi__err("integer parse overflow", "Parsing an integer in the PPM overflowed a 32-bit int");
7489
7234
  }
7490
7235
 
7491
7236
  return value;
7492
7237
  }
7493
7238
 
7494
- 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)
7495
7240
  {
7496
- int maxv, dummy;
7241
+ int dummy;
7497
7242
  char c, p, t;
7498
7243
 
7499
7244
  if (!x) x = &dummy;
7500
7245
  if (!y) y = &dummy;
7501
7246
  if (!comp) comp = &dummy;
7247
+ if (!isascii) isascii = &dummy;
7502
7248
 
7503
7249
  stbi__rewind(s);
7250
+ *isascii = 0;
7504
7251
 
7505
7252
  // Get identifier
7506
7253
  p = (char) stbi__get8(s);
7507
7254
  t = (char) stbi__get8(s);
7508
- if (p != 'P' || (t != '5' && t != '6')) {
7255
+ if (p != 'P' || (t != '2' && t != '3' && t != '5' && t != '6')) {
7509
7256
  stbi__rewind(s);
7510
7257
  return 0;
7511
7258
  }
7512
7259
 
7513
- *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';
7514
7262
 
7515
7263
  c = (char) stbi__get8(s);
7516
7264
  stbi__pnm_skip_whitespace(s, &c);
7517
7265
 
7518
7266
  *x = stbi__pnm_getinteger(s, &c); // read width
7267
+ if(*x == 0)
7268
+ return stbi__err("invalid width", "PPM image header had zero or overflowing width");
7519
7269
  stbi__pnm_skip_whitespace(s, &c);
7520
7270
 
7521
7271
  *y = stbi__pnm_getinteger(s, &c); // read height
7272
+ if (*y == 0)
7273
+ return stbi__err("invalid width", "PPM image header had zero or overflowing width");
7522
7274
  stbi__pnm_skip_whitespace(s, &c);
7523
7275
 
7524
- maxv = stbi__pnm_getinteger(s, &c); // read max value
7525
- 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)
7526
7280
  return stbi__err("max value > 65535", "PPM image supports only 8-bit and 16-bit images");
7527
- else if (maxv > 255)
7281
+ else if (*maxv > 255)
7528
7282
  return 16;
7529
7283
  else
7530
7284
  return 8;
7531
7285
  }
7532
7286
 
7533
- 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)
7534
7288
  {
7535
- if (stbi__pnm_info(s, NULL, NULL, NULL) == 16)
7536
- return 1;
7537
- 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
+ }
7538
7378
  }
7539
7379
  #endif
7540
7380
 
7541
- 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)
7542
7382
  {
7383
+ stbi_info_res res;
7384
+ memset(&res, 0, sizeof(res));
7385
+
7543
7386
  #ifndef STBI_NO_JPEG
7544
- if (stbi__jpeg_info(s, x, y, comp)) return 1;
7387
+ if (stbi__jpeg_info(s, &res)) return res;
7545
7388
  #endif
7546
7389
 
7547
7390
  #ifndef STBI_NO_PNG
7548
- if (stbi__png_info(s, x, y, comp)) return 1;
7391
+ if (stbi__png_info(s, &res)) return res;
7549
7392
  #endif
7550
7393
 
7551
7394
  #ifndef STBI_NO_GIF
7552
- if (stbi__gif_info(s, x, y, comp)) return 1;
7395
+ if (stbi__gif_info(s, &res)) return res;
7553
7396
  #endif
7554
7397
 
7555
7398
  #ifndef STBI_NO_BMP
7556
- if (stbi__bmp_info(s, x, y, comp)) return 1;
7399
+ if (stbi__bmp_info(s, &res)) return res;
7557
7400
  #endif
7558
7401
 
7559
7402
  #ifndef STBI_NO_PSD
7560
- if (stbi__psd_info(s, x, y, comp)) return 1;
7561
- #endif
7562
-
7563
- #ifndef STBI_NO_PIC
7564
- if (stbi__pic_info(s, x, y, comp)) return 1;
7403
+ if (stbi__psd_info(s, &res)) return res;
7565
7404
  #endif
7566
7405
 
7567
7406
  #ifndef STBI_NO_PNM
7568
- if (stbi__pnm_info(s, x, y, comp)) return 1;
7407
+ if (stbi__pnm_info(s, &res)) return res;
7569
7408
  #endif
7570
7409
 
7571
7410
  #ifndef STBI_NO_HDR
7572
- if (stbi__hdr_info(s, x, y, comp)) return 1;
7411
+ if (stbi__hdr_info(s, &res)) return res;
7573
7412
  #endif
7574
7413
 
7575
7414
  // test tga last because it's a crappy test!
7576
7415
  #ifndef STBI_NO_TGA
7577
- if (stbi__tga_info(s, x, y, comp))
7578
- return 1;
7416
+ if (stbi__tga_info(s, &res)) return res;
7579
7417
  #endif
7580
- 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;
7581
7422
  }
7582
7423
 
7583
- static int stbi__is_16_main(stbi__context *s)
7424
+ static int stbi__info_main(stbi__context *s, int *x, int *y, int *comp)
7584
7425
  {
7585
- #ifndef STBI_NO_PNG
7586
- if (stbi__png_is16(s)) return 1;
7587
- #endif
7588
-
7589
- #ifndef STBI_NO_PSD
7590
- if (stbi__psd_is16(s)) return 1;
7591
- #endif
7592
-
7593
- #ifndef STBI_NO_PNM
7594
- if (stbi__pnm_is16(s)) return 1;
7595
- #endif
7596
- 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;
7597
7432
  }
7598
7433
 
7599
7434
  #ifndef STBI_NO_STDIO
@@ -7618,6 +7453,31 @@ STBIDEF int stbi_info_from_file(FILE *f, int *x, int *y, int *comp)
7618
7453
  return r;
7619
7454
  }
7620
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
+
7621
7481
  STBIDEF int stbi_is_16_bit(char const *filename)
7622
7482
  {
7623
7483
  FILE *f = stbi__fopen(filename, "rb");
@@ -7630,13 +7490,14 @@ STBIDEF int stbi_is_16_bit(char const *filename)
7630
7490
 
7631
7491
  STBIDEF int stbi_is_16_bit_from_file(FILE *f)
7632
7492
  {
7633
- int r;
7493
+ stbi_info_res r;
7634
7494
  stbi__context s;
7635
7495
  long pos = ftell(f);
7636
7496
  stbi__start_file(&s, f);
7637
- r = stbi__is_16_main(&s);
7497
+ r = stbi__info2_main(&s);
7498
+ stbi__stop(&s);
7638
7499
  fseek(f,pos,SEEK_SET);
7639
- return r;
7500
+ return r.type != STBI_type_unsupported && r.is_16_bit;
7640
7501
  }
7641
7502
  #endif // !STBI_NO_STDIO
7642
7503
 
@@ -7649,29 +7510,69 @@ STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y
7649
7510
 
7650
7511
  STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *c, void *user, int *x, int *y, int *comp)
7651
7512
  {
7513
+ int result;
7652
7514
  stbi__context s;
7653
7515
  stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user);
7654
- 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);
7655
7533
  }
7656
7534
 
7657
7535
  STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len)
7658
7536
  {
7659
7537
  stbi__context s;
7538
+ stbi_info_res r;
7660
7539
  stbi__start_mem(&s,buffer,len);
7661
- return stbi__is_16_main(&s);
7540
+ r = stbi__info2_main(&s);
7541
+ return r.type != STBI_type_unsupported && r.is_16_bit;
7662
7542
  }
7663
7543
 
7664
7544
  STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *c, void *user)
7665
7545
  {
7666
7546
  stbi__context s;
7547
+ stbi_info_res r;
7667
7548
  stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user);
7668
- 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;
7669
7552
  }
7670
7553
 
7671
7554
  #endif // STB_IMAGE_IMPLEMENTATION
7672
7555
 
7673
7556
  /*
7674
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
7675
7576
  2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs
7676
7577
  2.19 (2018-02-11) fix warning
7677
7578
  2.18 (2018-01-30) fix warnings