gosu 1.5.0 → 2.0.0.pre6

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