gosu 1.4.1 → 1.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/dependencies/SDL_sound/SDL_sound.c +21 -63
  3. data/dependencies/SDL_sound/SDL_sound.h +2 -2
  4. data/dependencies/SDL_sound/SDL_sound_aiff.c +26 -23
  5. data/dependencies/SDL_sound/SDL_sound_au.c +8 -8
  6. data/dependencies/SDL_sound/SDL_sound_coreaudio.c +4 -5
  7. data/dependencies/SDL_sound/SDL_sound_flac.c +28 -30
  8. data/dependencies/SDL_sound/SDL_sound_internal.h +4 -4
  9. data/dependencies/SDL_sound/SDL_sound_modplug.c +1 -1
  10. data/dependencies/SDL_sound/SDL_sound_mp3.c +19 -23
  11. data/dependencies/SDL_sound/SDL_sound_raw.c +5 -6
  12. data/dependencies/SDL_sound/SDL_sound_shn.c +4 -4
  13. data/dependencies/SDL_sound/SDL_sound_voc.c +15 -15
  14. data/dependencies/SDL_sound/SDL_sound_vorbis.c +14 -7
  15. data/dependencies/SDL_sound/SDL_sound_wav.c +17 -17
  16. data/dependencies/SDL_sound/dr_flac.h +10840 -4779
  17. data/dependencies/SDL_sound/dr_mp3.h +2793 -1004
  18. data/dependencies/SDL_sound/libmodplug/fastmix.c +5 -0
  19. data/dependencies/SDL_sound/libmodplug/load_669.c +1 -1
  20. data/dependencies/SDL_sound/libmodplug/load_amf.c +1 -0
  21. data/dependencies/SDL_sound/libmodplug/load_ams.c +38 -22
  22. data/dependencies/SDL_sound/libmodplug/load_it.c +18 -14
  23. data/dependencies/SDL_sound/libmodplug/load_mdl.c +18 -9
  24. data/dependencies/SDL_sound/libmodplug/load_med.c +7 -6
  25. data/dependencies/SDL_sound/libmodplug/load_mt2.c +36 -17
  26. data/dependencies/SDL_sound/libmodplug/load_okt.c +51 -24
  27. data/dependencies/SDL_sound/libmodplug/load_psm.c +4 -2
  28. data/dependencies/SDL_sound/libmodplug/load_s3m.c +4 -4
  29. data/dependencies/SDL_sound/libmodplug/load_ult.c +4 -3
  30. data/dependencies/SDL_sound/libmodplug/load_xm.c +5 -5
  31. data/dependencies/SDL_sound/libmodplug/snd_fx.c +8 -1
  32. data/dependencies/SDL_sound/libmodplug/sndfile.c +21 -4
  33. data/dependencies/SDL_sound/stb_vorbis.h +10 -18
  34. data/dependencies/mojoAL/mojoal.c +260 -6
  35. data/dependencies/stb/stb_image.h +208 -73
  36. data/dependencies/stb/stb_image_write.h +57 -23
  37. data/dependencies/stb/stb_truetype.h +345 -279
  38. data/dependencies/utf8proc/utf8proc.c +37 -18
  39. data/dependencies/utf8proc/utf8proc.h +17 -5
  40. data/dependencies/utf8proc/utf8proc_data.h +12012 -10089
  41. data/include/Gosu/Buttons.hpp +103 -103
  42. data/include/Gosu/Directories.hpp +31 -24
  43. data/include/Gosu/Font.hpp +4 -2
  44. data/include/Gosu/Gosu.hpp +5 -8
  45. data/include/Gosu/IO.hpp +0 -3
  46. data/include/Gosu/Math.hpp +0 -3
  47. data/include/Gosu/Timing.hpp +2 -8
  48. data/include/Gosu/Version.hpp +1 -1
  49. data/src/AudioImpl.cpp +0 -7
  50. data/src/AudioImpl.hpp +1 -3
  51. data/src/BitmapIO.cpp +23 -2
  52. data/src/DirectoriesApple.cpp +25 -24
  53. data/src/DirectoriesUnix.cpp +14 -12
  54. data/src/DirectoriesWin.cpp +26 -30
  55. data/src/Font.cpp +12 -2
  56. data/src/Image.cpp +10 -15
  57. data/src/RubyGosu.cxx +6 -34
  58. data/src/TimingApple.cpp +1 -7
  59. data/src/TimingUnix.cpp +0 -6
  60. data/src/TimingWin.cpp +0 -6
  61. data/src/Window.cpp +4 -3
  62. metadata +2 -2
@@ -1,4 +1,4 @@
1
- /* stb_image - v2.26 - public domain image loader - http://nothings.org/stb
1
+ /* stb_image - v2.27 - public domain image loader - http://nothings.org/stb
2
2
  no warranty implied; use at your own risk
3
3
 
4
4
  Do this:
@@ -48,6 +48,7 @@ LICENSE
48
48
 
49
49
  RECENT REVISION HISTORY:
50
50
 
51
+ 2.27 (2021-07-11) document stbi_info better, 16-bit PNM support, bug fixes
51
52
  2.26 (2020-07-13) many minor fixes
52
53
  2.25 (2020-02-02) fix warnings
53
54
  2.24 (2020-02-02) fix warnings; thread-local failure_reason and flip_vertically
@@ -89,7 +90,7 @@ RECENT REVISION HISTORY:
89
90
  Jeremy Sawicki (handle all ImageNet JPGs)
90
91
  Optimizations & bugfixes Mikhail Morozov (1-bit BMP)
91
92
  Fabian "ryg" Giesen Anael Seghezzi (is-16-bit query)
92
- Arseny Kapoulkine
93
+ Arseny Kapoulkine Simon Breuss (16-bit PNM)
93
94
  John-Mark Allen
94
95
  Carmelo J Fdez-Aguera
95
96
 
@@ -102,7 +103,7 @@ RECENT REVISION HISTORY:
102
103
  Thomas Ruf Ronny Chevalier github:rlyeh
103
104
  Janez Zemva John Bartholomew Michal Cichon github:romigrou
104
105
  Jonathan Blow Ken Hamada Tero Hanninen github:svdijk
105
- Laurent Gomila Cort Stratton github:snagar
106
+ Eugene Golushkov Laurent Gomila Cort Stratton github:snagar
106
107
  Aruelien Pocheville Sergio Gonzalez Thibault Reuille github:Zelex
107
108
  Cass Everitt Ryamond Barbiero github:grim210
108
109
  Paul Du Bois Engin Manap Aldo Culquicondor github:sammyhw
@@ -110,11 +111,13 @@ RECENT REVISION HISTORY:
110
111
  Josh Tobin Matthew Gregan github:poppolopoppo
111
112
  Julian Raschke Gregory Mullen Christian Floisand github:darealshinji
112
113
  Baldur Karlsson Kevin Schmidt JR Smith github:Michaelangel007
113
- Brad Weinberger Matvey Cherevko [reserved]
114
+ Brad Weinberger Matvey Cherevko github:mosra
114
115
  Luca Sas Alexander Veselov Zack Middleton [reserved]
115
116
  Ryan C. Gordon [reserved] [reserved]
116
117
  DO NOT ADD YOUR NAME HERE
117
118
 
119
+ Jacko Dirks
120
+
118
121
  To add your name to the credits, pick a random blank space in the middle and fill it.
119
122
  80% of merge conflicts on stb PRs are due to people adding their name at the end
120
123
  of the credits.
@@ -176,6 +179,32 @@ RECENT REVISION HISTORY:
176
179
  //
177
180
  // Paletted PNG, BMP, GIF, and PIC images are automatically depalettized.
178
181
  //
182
+ // To query the width, height and component count of an image without having to
183
+ // decode the full file, you can use the stbi_info family of functions:
184
+ //
185
+ // int x,y,n,ok;
186
+ // ok = stbi_info(filename, &x, &y, &n);
187
+ // // returns ok=1 and sets x, y, n if image is a supported format,
188
+ // // 0 otherwise.
189
+ //
190
+ // Note that stb_image pervasively uses ints in its public API for sizes,
191
+ // including sizes of memory buffers. This is now part of the API and thus
192
+ // hard to change without causing breakage. As a result, the various image
193
+ // loaders all have certain limits on image size; these differ somewhat
194
+ // by format but generally boil down to either just under 2GB or just under
195
+ // 1GB. When the decoded image would be larger than this, stb_image decoding
196
+ // will fail.
197
+ //
198
+ // Additionally, stb_image will reject image files that have any of their
199
+ // dimensions set to a larger value than the configurable STBI_MAX_DIMENSIONS,
200
+ // which defaults to 2**24 = 16777216 pixels. Due to the above memory limit,
201
+ // the only way to have an image with such dimensions load correctly
202
+ // is for it to have a rather extreme aspect ratio. Either way, the
203
+ // assumption here is that such larger images are likely to be malformed
204
+ // or malicious. If you do need to load an image with individual dimensions
205
+ // larger than that, and it still fits in the overall size limit, you can
206
+ // #define STBI_MAX_DIMENSIONS on your own to be something larger.
207
+ //
179
208
  // ===========================================================================
180
209
  //
181
210
  // UNICODE:
@@ -281,11 +310,10 @@ RECENT REVISION HISTORY:
281
310
  //
282
311
  // iPhone PNG support:
283
312
  //
284
- // By default we convert iphone-formatted PNGs back to RGB, even though
285
- // they are internally encoded differently. You can disable this conversion
286
- // by calling stbi_convert_iphone_png_to_rgb(0), in which case
287
- // you will always just get the native iphone "format" through (which
288
- // is BGR stored in RGB).
313
+ // We optionally support converting iPhone-formatted PNGs (which store
314
+ // premultiplied BGRA) back to RGB, even though they're internally encoded
315
+ // differently. To enable this conversion, call
316
+ // stbi_convert_iphone_png_to_rgb(1).
289
317
  //
290
318
  // Call stbi_set_unpremultiply_on_load(1) as well to force a divide per
291
319
  // pixel to remove any premultiplied alpha *only* if the image file explicitly
@@ -489,6 +517,8 @@ STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip);
489
517
  // as above, but only applies to images loaded on the thread that calls the function
490
518
  // this function is only available if your compiler supports thread-local variables;
491
519
  // calling it will fail to link if your compiler doesn't
520
+ STBIDEF void stbi_set_unpremultiply_on_load_thread(int flag_true_if_should_unpremultiply);
521
+ STBIDEF void stbi_convert_iphone_png_to_rgb_thread(int flag_true_if_should_convert);
492
522
  STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip);
493
523
 
494
524
  // ZLIB client - used by PNG, available for other purposes
@@ -634,7 +664,7 @@ typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1];
634
664
  #ifdef STBI_HAS_LROTL
635
665
  #define stbi_lrot(x,y) _lrotl(x,y)
636
666
  #else
637
- #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (32 - (y))))
667
+ #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (-(y) & 31)))
638
668
  #endif
639
669
 
640
670
  #if defined(STBI_MALLOC) && defined(STBI_FREE) && (defined(STBI_REALLOC) || defined(STBI_REALLOC_SIZED))
@@ -748,9 +778,12 @@ static int stbi__sse2_available(void)
748
778
 
749
779
  #ifdef STBI_NEON
750
780
  #include <arm_neon.h>
751
- // assume GCC or Clang on ARM targets
781
+ #ifdef _MSC_VER
782
+ #define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name
783
+ #else
752
784
  #define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
753
785
  #endif
786
+ #endif
754
787
 
755
788
  #ifndef STBI_SIMD_ALIGN
756
789
  #define STBI_SIMD_ALIGN(type, name) type name
@@ -924,6 +957,7 @@ static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp);
924
957
  static int stbi__pnm_test(stbi__context *s);
925
958
  static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
926
959
  static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp);
960
+ static int stbi__pnm_is16(stbi__context *s);
927
961
  #endif
928
962
 
929
963
  static
@@ -998,7 +1032,7 @@ static int stbi__mad3sizes_valid(int a, int b, int c, int add)
998
1032
  }
999
1033
 
1000
1034
  // returns 1 if "a*b*c*d + add" has no negative terms/factors and doesn't overflow
1001
- #if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR)
1035
+ #if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM)
1002
1036
  static int stbi__mad4sizes_valid(int a, int b, int c, int d, int add)
1003
1037
  {
1004
1038
  return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) &&
@@ -1021,7 +1055,7 @@ static void *stbi__malloc_mad3(int a, int b, int c, int add)
1021
1055
  return stbi__malloc(a*b*c + add);
1022
1056
  }
1023
1057
 
1024
- #if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR)
1058
+ #if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM)
1025
1059
  static void *stbi__malloc_mad4(int a, int b, int c, int d, int add)
1026
1060
  {
1027
1061
  if (!stbi__mad4sizes_valid(a, b, c, d, add)) return NULL;
@@ -1087,9 +1121,8 @@ static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int re
1087
1121
  ri->channel_order = STBI_ORDER_RGB; // all current input & output are this, but this is here so we can add BGR order
1088
1122
  ri->num_channels = 0;
1089
1123
 
1090
- #ifndef STBI_NO_JPEG
1091
- if (stbi__jpeg_test(s)) return stbi__jpeg_load(s,x,y,comp,req_comp, ri);
1092
- #endif
1124
+ // test the formats with a very explicit header first (at least a FOURCC
1125
+ // or distinctive magic number first)
1093
1126
  #ifndef STBI_NO_PNG
1094
1127
  if (stbi__png_test(s)) return stbi__png_load(s,x,y,comp,req_comp, ri);
1095
1128
  #endif
@@ -1107,6 +1140,13 @@ static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int re
1107
1140
  #ifndef STBI_NO_PIC
1108
1141
  if (stbi__pic_test(s)) return stbi__pic_load(s,x,y,comp,req_comp, ri);
1109
1142
  #endif
1143
+
1144
+ // then the formats that can end up attempting to load with just 1 or 2
1145
+ // bytes matching expectations; these are prone to false positives, so
1146
+ // try them later
1147
+ #ifndef STBI_NO_JPEG
1148
+ if (stbi__jpeg_test(s)) return stbi__jpeg_load(s,x,y,comp,req_comp, ri);
1149
+ #endif
1110
1150
  #ifndef STBI_NO_PNM
1111
1151
  if (stbi__pnm_test(s)) return stbi__pnm_load(s,x,y,comp,req_comp, ri);
1112
1152
  #endif
@@ -1262,12 +1302,12 @@ static void stbi__float_postprocess(float *result, int *x, int *y, int *comp, in
1262
1302
 
1263
1303
  #ifndef STBI_NO_STDIO
1264
1304
 
1265
- #if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)
1305
+ #if defined(_WIN32) && defined(STBI_WINDOWS_UTF8)
1266
1306
  STBI_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide);
1267
1307
  STBI_EXTERN __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigned long flags, const wchar_t *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default);
1268
1308
  #endif
1269
1309
 
1270
- #if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)
1310
+ #if defined(_WIN32) && defined(STBI_WINDOWS_UTF8)
1271
1311
  STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input)
1272
1312
  {
1273
1313
  return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL);
@@ -1277,16 +1317,16 @@ STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wch
1277
1317
  static FILE *stbi__fopen(char const *filename, char const *mode)
1278
1318
  {
1279
1319
  FILE *f;
1280
- #if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)
1320
+ #if defined(_WIN32) && defined(STBI_WINDOWS_UTF8)
1281
1321
  wchar_t wMode[64];
1282
1322
  wchar_t wFilename[1024];
1283
- if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename)))
1323
+ if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename)/sizeof(*wFilename)))
1284
1324
  return 0;
1285
1325
 
1286
- if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode)))
1326
+ if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode)/sizeof(*wMode)))
1287
1327
  return 0;
1288
1328
 
1289
- #if _MSC_VER >= 1400
1329
+ #if defined(_MSC_VER) && _MSC_VER >= 1400
1290
1330
  if (0 != _wfopen_s(&f, wFilename, wMode))
1291
1331
  f = 0;
1292
1332
  #else
@@ -1662,7 +1702,8 @@ static int stbi__get16le(stbi__context *s)
1662
1702
  static stbi__uint32 stbi__get32le(stbi__context *s)
1663
1703
  {
1664
1704
  stbi__uint32 z = stbi__get16le(s);
1665
- return z + (stbi__get16le(s) << 16);
1705
+ z += (stbi__uint32)stbi__get16le(s) << 16;
1706
+ return z;
1666
1707
  }
1667
1708
  #endif
1668
1709
 
@@ -2090,13 +2131,12 @@ stbi_inline static int stbi__extend_receive(stbi__jpeg *j, int n)
2090
2131
  int sgn;
2091
2132
  if (j->code_bits < n) stbi__grow_buffer_unsafe(j);
2092
2133
 
2093
- sgn = (stbi__int32)j->code_buffer >> 31; // sign bit is always in MSB
2134
+ sgn = j->code_buffer >> 31; // sign bit always in MSB; 0 if MSB clear (positive), 1 if MSB set (negative)
2094
2135
  k = stbi_lrot(j->code_buffer, n);
2095
- if (n < 0 || n >= (int) (sizeof(stbi__bmask)/sizeof(*stbi__bmask))) return 0;
2096
2136
  j->code_buffer = k & ~stbi__bmask[n];
2097
2137
  k &= stbi__bmask[n];
2098
2138
  j->code_bits -= n;
2099
- return k + (stbi__jbias[n] & ~sgn);
2139
+ return k + (stbi__jbias[n] & (sgn - 1));
2100
2140
  }
2101
2141
 
2102
2142
  // get some unsigned bits
@@ -2146,7 +2186,7 @@ static int stbi__jpeg_decode_block(stbi__jpeg *j, short data[64], stbi__huffman
2146
2186
 
2147
2187
  if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
2148
2188
  t = stbi__jpeg_huff_decode(j, hdc);
2149
- if (t < 0) return stbi__err("bad huffman code","Corrupt JPEG");
2189
+ if (t < 0 || t > 15) return stbi__err("bad huffman code","Corrupt JPEG");
2150
2190
 
2151
2191
  // 0 all the ac values now so we can do it 32-bits at a time
2152
2192
  memset(data,0,64*sizeof(data[0]));
@@ -2203,12 +2243,12 @@ static int stbi__jpeg_decode_block_prog_dc(stbi__jpeg *j, short data[64], stbi__
2203
2243
  // first scan for DC coefficient, must be first
2204
2244
  memset(data,0,64*sizeof(data[0])); // 0 all the ac values now
2205
2245
  t = stbi__jpeg_huff_decode(j, hdc);
2206
- if (t == -1) return stbi__err("can't merge dc and ac", "Corrupt JPEG");
2246
+ if (t < 0 || t > 15) return stbi__err("can't merge dc and ac", "Corrupt JPEG");
2207
2247
  diff = t ? stbi__extend_receive(j, t) : 0;
2208
2248
 
2209
2249
  dc = j->img_comp[b].dc_pred + diff;
2210
2250
  j->img_comp[b].dc_pred = dc;
2211
- data[0] = (short) (dc << j->succ_low);
2251
+ data[0] = (short) (dc * (1 << j->succ_low));
2212
2252
  } else {
2213
2253
  // refinement scan for DC coefficient
2214
2254
  if (stbi__jpeg_get_bit(j))
@@ -2245,7 +2285,7 @@ static int stbi__jpeg_decode_block_prog_ac(stbi__jpeg *j, short data[64], stbi__
2245
2285
  j->code_buffer <<= s;
2246
2286
  j->code_bits -= s;
2247
2287
  zig = stbi__jpeg_dezigzag[k++];
2248
- data[zig] = (short) ((r >> 8) << shift);
2288
+ data[zig] = (short) ((r >> 8) * (1 << shift));
2249
2289
  } else {
2250
2290
  int rs = stbi__jpeg_huff_decode(j, hac);
2251
2291
  if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG");
@@ -2263,7 +2303,7 @@ static int stbi__jpeg_decode_block_prog_ac(stbi__jpeg *j, short data[64], stbi__
2263
2303
  } else {
2264
2304
  k += r;
2265
2305
  zig = stbi__jpeg_dezigzag[k++];
2266
- data[zig] = (short) (stbi__extend_receive(j,s) << shift);
2306
+ data[zig] = (short) (stbi__extend_receive(j,s) * (1 << shift));
2267
2307
  }
2268
2308
  }
2269
2309
  } while (k <= j->spec_end);
@@ -3227,6 +3267,13 @@ static int stbi__process_frame_header(stbi__jpeg *z, int scan)
3227
3267
  if (z->img_comp[i].v > v_max) v_max = z->img_comp[i].v;
3228
3268
  }
3229
3269
 
3270
+ // check that plane subsampling factors are integer ratios; our resamplers can't deal with fractional ratios
3271
+ // and I've never seen a non-corrupted JPEG file actually use them
3272
+ for (i=0; i < s->img_n; ++i) {
3273
+ if (h_max % z->img_comp[i].h != 0) return stbi__err("bad H","Corrupt JPEG");
3274
+ if (v_max % z->img_comp[i].v != 0) return stbi__err("bad V","Corrupt JPEG");
3275
+ }
3276
+
3230
3277
  // compute interleaved mcu info
3231
3278
  z->img_h_max = h_max;
3232
3279
  z->img_v_max = v_max;
@@ -3782,6 +3829,10 @@ static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp
3782
3829
  else
3783
3830
  decode_n = z->s->img_n;
3784
3831
 
3832
+ // nothing to do if no components requested; check this now to avoid
3833
+ // accessing uninitialized coutput[0] later
3834
+ if (decode_n <= 0) { stbi__cleanup_jpeg(z); return NULL; }
3835
+
3785
3836
  // resample and color-convert
3786
3837
  {
3787
3838
  int k;
@@ -3924,6 +3975,7 @@ static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int re
3924
3975
  {
3925
3976
  unsigned char* result;
3926
3977
  stbi__jpeg* j = (stbi__jpeg*) stbi__malloc(sizeof(stbi__jpeg));
3978
+ if (!j) return stbi__errpuc("outofmem", "Out of memory");
3927
3979
  STBI_NOTUSED(ri);
3928
3980
  j->s = s;
3929
3981
  stbi__setup_jpeg(j);
@@ -3936,6 +3988,7 @@ static int stbi__jpeg_test(stbi__context *s)
3936
3988
  {
3937
3989
  int r;
3938
3990
  stbi__jpeg* j = (stbi__jpeg*)stbi__malloc(sizeof(stbi__jpeg));
3991
+ if (!j) return stbi__err("outofmem", "Out of memory");
3939
3992
  j->s = s;
3940
3993
  stbi__setup_jpeg(j);
3941
3994
  r = stbi__decode_jpeg_header(j, STBI__SCAN_type);
@@ -3960,6 +4013,7 @@ static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp)
3960
4013
  {
3961
4014
  int result;
3962
4015
  stbi__jpeg* j = (stbi__jpeg*) (stbi__malloc(sizeof(stbi__jpeg)));
4016
+ if (!j) return stbi__err("outofmem", "Out of memory");
3963
4017
  j->s = s;
3964
4018
  result = stbi__jpeg_info_raw(j, x, y, comp);
3965
4019
  STBI_FREE(j);
@@ -3979,6 +4033,7 @@ static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp)
3979
4033
  // fast-way is faster to check than jpeg huffman, but slow way is slower
3980
4034
  #define STBI__ZFAST_BITS 9 // accelerate all cases in default tables
3981
4035
  #define STBI__ZFAST_MASK ((1 << STBI__ZFAST_BITS) - 1)
4036
+ #define STBI__ZNSYMS 288 // number of symbols in literal/length alphabet
3982
4037
 
3983
4038
  // zlib-style huffman encoding
3984
4039
  // (jpegs packs from left, zlib from right, so can't share code)
@@ -3988,8 +4043,8 @@ typedef struct
3988
4043
  stbi__uint16 firstcode[16];
3989
4044
  int maxcode[17];
3990
4045
  stbi__uint16 firstsymbol[16];
3991
- stbi_uc size[288];
3992
- stbi__uint16 value[288];
4046
+ stbi_uc size[STBI__ZNSYMS];
4047
+ stbi__uint16 value[STBI__ZNSYMS];
3993
4048
  } stbi__zhuffman;
3994
4049
 
3995
4050
  stbi_inline static int stbi__bitreverse16(int n)
@@ -4120,7 +4175,7 @@ static int stbi__zhuffman_decode_slowpath(stbi__zbuf *a, stbi__zhuffman *z)
4120
4175
  if (s >= 16) return -1; // invalid code!
4121
4176
  // code size is s, so:
4122
4177
  b = (k >> (16-s)) - z->firstcode[s] + z->firstsymbol[s];
4123
- if (b >= sizeof (z->size)) return -1; // some data was corrupt somewhere!
4178
+ if (b >= STBI__ZNSYMS) return -1; // some data was corrupt somewhere!
4124
4179
  if (z->size[b] != s) return -1; // was originally an assert, but report failure instead.
4125
4180
  a->code_buffer >>= s;
4126
4181
  a->num_bits -= s;
@@ -4317,7 +4372,7 @@ static int stbi__parse_zlib_header(stbi__zbuf *a)
4317
4372
  return 1;
4318
4373
  }
4319
4374
 
4320
- static const stbi_uc stbi__zdefault_length[288] =
4375
+ static const stbi_uc stbi__zdefault_length[STBI__ZNSYMS] =
4321
4376
  {
4322
4377
  8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
4323
4378
  8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
@@ -4363,7 +4418,7 @@ static int stbi__parse_zlib(stbi__zbuf *a, int parse_header)
4363
4418
  } else {
4364
4419
  if (type == 1) {
4365
4420
  // use fixed code lengths
4366
- if (!stbi__zbuild_huffman(&a->z_length , stbi__zdefault_length , 288)) return 0;
4421
+ if (!stbi__zbuild_huffman(&a->z_length , stbi__zdefault_length , STBI__ZNSYMS)) return 0;
4367
4422
  if (!stbi__zbuild_huffman(&a->z_distance, stbi__zdefault_distance, 32)) return 0;
4368
4423
  } else {
4369
4424
  if (!stbi__compute_huffman_codes(a)) return 0;
@@ -4759,6 +4814,7 @@ static int stbi__create_png_image(stbi__png *a, stbi_uc *image_data, stbi__uint3
4759
4814
 
4760
4815
  // de-interlacing
4761
4816
  final = (stbi_uc *) stbi__malloc_mad3(a->s->img_x, a->s->img_y, out_bytes, 0);
4817
+ if (!final) return stbi__err("outofmem", "Out of memory");
4762
4818
  for (p=0; p < 7; ++p) {
4763
4819
  int xorig[] = { 0,4,0,2,0,1,0 };
4764
4820
  int yorig[] = { 0,0,4,0,2,0,1 };
@@ -4879,19 +4935,46 @@ static int stbi__expand_png_palette(stbi__png *a, stbi_uc *palette, int len, int
4879
4935
  return 1;
4880
4936
  }
4881
4937
 
4882
- static int stbi__unpremultiply_on_load = 0;
4883
- static int stbi__de_iphone_flag = 0;
4938
+ static int stbi__unpremultiply_on_load_global = 0;
4939
+ static int stbi__de_iphone_flag_global = 0;
4884
4940
 
4885
4941
  STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply)
4886
4942
  {
4887
- stbi__unpremultiply_on_load = flag_true_if_should_unpremultiply;
4943
+ stbi__unpremultiply_on_load_global = flag_true_if_should_unpremultiply;
4888
4944
  }
4889
4945
 
4890
4946
  STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert)
4891
4947
  {
4892
- stbi__de_iphone_flag = flag_true_if_should_convert;
4948
+ stbi__de_iphone_flag_global = flag_true_if_should_convert;
4949
+ }
4950
+
4951
+ #ifndef STBI_THREAD_LOCAL
4952
+ #define stbi__unpremultiply_on_load stbi__unpremultiply_on_load_global
4953
+ #define stbi__de_iphone_flag stbi__de_iphone_flag_global
4954
+ #else
4955
+ static STBI_THREAD_LOCAL int stbi__unpremultiply_on_load_local, stbi__unpremultiply_on_load_set;
4956
+ static STBI_THREAD_LOCAL int stbi__de_iphone_flag_local, stbi__de_iphone_flag_set;
4957
+
4958
+ STBIDEF void stbi__unpremultiply_on_load_thread(int flag_true_if_should_unpremultiply)
4959
+ {
4960
+ stbi__unpremultiply_on_load_local = flag_true_if_should_unpremultiply;
4961
+ stbi__unpremultiply_on_load_set = 1;
4962
+ }
4963
+
4964
+ STBIDEF void stbi_convert_iphone_png_to_rgb_thread(int flag_true_if_should_convert)
4965
+ {
4966
+ stbi__de_iphone_flag_local = flag_true_if_should_convert;
4967
+ stbi__de_iphone_flag_set = 1;
4893
4968
  }
4894
4969
 
4970
+ #define stbi__unpremultiply_on_load (stbi__unpremultiply_on_load_set \
4971
+ ? stbi__unpremultiply_on_load_local \
4972
+ : stbi__unpremultiply_on_load_global)
4973
+ #define stbi__de_iphone_flag (stbi__de_iphone_flag_set \
4974
+ ? stbi__de_iphone_flag_local \
4975
+ : stbi__de_iphone_flag_global)
4976
+ #endif // STBI_THREAD_LOCAL
4977
+
4895
4978
  static void stbi__de_iphone(stbi__png *z)
4896
4979
  {
4897
4980
  stbi__context *s = z->s;
@@ -5272,6 +5355,32 @@ typedef struct
5272
5355
  int extra_read;
5273
5356
  } stbi__bmp_data;
5274
5357
 
5358
+ static int stbi__bmp_set_mask_defaults(stbi__bmp_data *info, int compress)
5359
+ {
5360
+ // BI_BITFIELDS specifies masks explicitly, don't override
5361
+ if (compress == 3)
5362
+ return 1;
5363
+
5364
+ if (compress == 0) {
5365
+ if (info->bpp == 16) {
5366
+ info->mr = 31u << 10;
5367
+ info->mg = 31u << 5;
5368
+ info->mb = 31u << 0;
5369
+ } else if (info->bpp == 32) {
5370
+ info->mr = 0xffu << 16;
5371
+ info->mg = 0xffu << 8;
5372
+ info->mb = 0xffu << 0;
5373
+ info->ma = 0xffu << 24;
5374
+ info->all_a = 0; // if all_a is 0 at end, then we loaded alpha channel but it was all 0
5375
+ } else {
5376
+ // otherwise, use defaults, which is all-0
5377
+ info->mr = info->mg = info->mb = info->ma = 0;
5378
+ }
5379
+ return 1;
5380
+ }
5381
+ return 0; // error
5382
+ }
5383
+
5275
5384
  static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info)
5276
5385
  {
5277
5386
  int hsz;
@@ -5299,6 +5408,8 @@ static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info)
5299
5408
  if (hsz != 12) {
5300
5409
  int compress = stbi__get32le(s);
5301
5410
  if (compress == 1 || compress == 2) return stbi__errpuc("BMP RLE", "BMP type not supported: RLE");
5411
+ if (compress >= 4) return stbi__errpuc("BMP JPEG/PNG", "BMP type not supported: unsupported compression"); // this includes PNG/JPEG modes
5412
+ if (compress == 3 && info->bpp != 16 && info->bpp != 32) return stbi__errpuc("bad BMP", "bad BMP"); // bitfields requires 16 or 32 bits/pixel
5302
5413
  stbi__get32le(s); // discard sizeof
5303
5414
  stbi__get32le(s); // discard hres
5304
5415
  stbi__get32le(s); // discard vres
@@ -5313,17 +5424,7 @@ static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info)
5313
5424
  }
5314
5425
  if (info->bpp == 16 || info->bpp == 32) {
5315
5426
  if (compress == 0) {
5316
- if (info->bpp == 32) {
5317
- info->mr = 0xffu << 16;
5318
- info->mg = 0xffu << 8;
5319
- info->mb = 0xffu << 0;
5320
- info->ma = 0xffu << 24;
5321
- info->all_a = 0; // if all_a is 0 at end, then we loaded alpha channel but it was all 0
5322
- } else {
5323
- info->mr = 31u << 10;
5324
- info->mg = 31u << 5;
5325
- info->mb = 31u << 0;
5326
- }
5427
+ stbi__bmp_set_mask_defaults(info, compress);
5327
5428
  } else if (compress == 3) {
5328
5429
  info->mr = stbi__get32le(s);
5329
5430
  info->mg = stbi__get32le(s);
@@ -5338,6 +5439,7 @@ static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info)
5338
5439
  return stbi__errpuc("bad BMP", "bad BMP");
5339
5440
  }
5340
5441
  } else {
5442
+ // V4/V5 header
5341
5443
  int i;
5342
5444
  if (hsz != 108 && hsz != 124)
5343
5445
  return stbi__errpuc("bad BMP", "bad BMP");
@@ -5345,6 +5447,8 @@ static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info)
5345
5447
  info->mg = stbi__get32le(s);
5346
5448
  info->mb = stbi__get32le(s);
5347
5449
  info->ma = stbi__get32le(s);
5450
+ if (compress != 3) // override mr/mg/mb unless in BI_BITFIELDS mode, as per docs
5451
+ stbi__bmp_set_mask_defaults(info, compress);
5348
5452
  stbi__get32le(s); // discard color space
5349
5453
  for (i=0; i < 12; ++i)
5350
5454
  stbi__get32le(s); // discard color space parameters
@@ -5394,8 +5498,7 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5394
5498
  psize = (info.offset - info.extra_read - info.hsz) >> 2;
5395
5499
  }
5396
5500
  if (psize == 0) {
5397
- STBI_ASSERT(info.offset == s->callback_already_read + (int) (s->img_buffer - s->img_buffer_original));
5398
- if (info.offset != s->callback_already_read + (s->img_buffer - s->buffer_start)) {
5501
+ if (info.offset != s->callback_already_read + (s->img_buffer - s->img_buffer_original)) {
5399
5502
  return stbi__errpuc("bad offset", "Corrupt BMP");
5400
5503
  }
5401
5504
  }
@@ -6342,6 +6445,7 @@ static void *stbi__pic_load(stbi__context *s,int *px,int *py,int *comp,int req_c
6342
6445
 
6343
6446
  // intermediate buffer is RGBA
6344
6447
  result = (stbi_uc *) stbi__malloc_mad3(x, y, 4, 0);
6448
+ if (!result) return stbi__errpuc("outofmem", "Out of memory");
6345
6449
  memset(result, 0xff, x*y*4);
6346
6450
 
6347
6451
  if (!stbi__pic_load_core(s,x,y,comp, result)) {
@@ -6457,6 +6561,7 @@ static int stbi__gif_header(stbi__context *s, stbi__gif *g, int *comp, int is_in
6457
6561
  static int stbi__gif_info_raw(stbi__context *s, int *x, int *y, int *comp)
6458
6562
  {
6459
6563
  stbi__gif* g = (stbi__gif*) stbi__malloc(sizeof(stbi__gif));
6564
+ if (!g) return stbi__err("outofmem", "Out of memory");
6460
6565
  if (!stbi__gif_header(s, g, comp, 1)) {
6461
6566
  STBI_FREE(g);
6462
6567
  stbi__rewind( s );
@@ -6766,6 +6871,17 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i
6766
6871
  }
6767
6872
  }
6768
6873
 
6874
+ static void *stbi__load_gif_main_outofmem(stbi__gif *g, stbi_uc *out, int **delays)
6875
+ {
6876
+ STBI_FREE(g->out);
6877
+ STBI_FREE(g->history);
6878
+ STBI_FREE(g->background);
6879
+
6880
+ if (out) STBI_FREE(out);
6881
+ if (delays && *delays) STBI_FREE(*delays);
6882
+ return stbi__errpuc("outofmem", "Out of memory");
6883
+ }
6884
+
6769
6885
  static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp)
6770
6886
  {
6771
6887
  if (stbi__gif_test(s)) {
@@ -6777,6 +6893,10 @@ static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y,
6777
6893
  int stride;
6778
6894
  int out_size = 0;
6779
6895
  int delays_size = 0;
6896
+
6897
+ STBI_NOTUSED(out_size);
6898
+ STBI_NOTUSED(delays_size);
6899
+
6780
6900
  memset(&g, 0, sizeof(g));
6781
6901
  if (delays) {
6782
6902
  *delays = 0;
@@ -6794,26 +6914,29 @@ static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y,
6794
6914
 
6795
6915
  if (out) {
6796
6916
  void *tmp = (stbi_uc*) STBI_REALLOC_SIZED( out, out_size, layers * stride );
6797
- if (NULL == tmp) {
6798
- STBI_FREE(g.out);
6799
- STBI_FREE(g.history);
6800
- STBI_FREE(g.background);
6801
- return stbi__errpuc("outofmem", "Out of memory");
6802
- }
6917
+ if (!tmp)
6918
+ return stbi__load_gif_main_outofmem(&g, out, delays);
6803
6919
  else {
6804
6920
  out = (stbi_uc*) tmp;
6805
6921
  out_size = layers * stride;
6806
6922
  }
6807
6923
 
6808
6924
  if (delays) {
6809
- *delays = (int*) STBI_REALLOC_SIZED( *delays, delays_size, sizeof(int) * layers );
6925
+ int *new_delays = (int*) STBI_REALLOC_SIZED( *delays, delays_size, sizeof(int) * layers );
6926
+ if (!new_delays)
6927
+ return stbi__load_gif_main_outofmem(&g, out, delays);
6928
+ *delays = new_delays;
6810
6929
  delays_size = layers * sizeof(int);
6811
6930
  }
6812
6931
  } else {
6813
6932
  out = (stbi_uc*)stbi__malloc( layers * stride );
6933
+ if (!out)
6934
+ return stbi__load_gif_main_outofmem(&g, out, delays);
6814
6935
  out_size = layers * stride;
6815
6936
  if (delays) {
6816
6937
  *delays = (int*) stbi__malloc( layers * sizeof(int) );
6938
+ if (!*delays)
6939
+ return stbi__load_gif_main_outofmem(&g, out, delays);
6817
6940
  delays_size = layers * sizeof(int);
6818
6941
  }
6819
6942
  }
@@ -7138,9 +7261,10 @@ static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp)
7138
7261
 
7139
7262
  info.all_a = 255;
7140
7263
  p = stbi__bmp_parse_header(s, &info);
7141
- stbi__rewind( s );
7142
- if (p == NULL)
7264
+ if (p == NULL) {
7265
+ stbi__rewind( s );
7143
7266
  return 0;
7267
+ }
7144
7268
  if (x) *x = s->img_x;
7145
7269
  if (y) *y = s->img_y;
7146
7270
  if (comp) {
@@ -7206,8 +7330,8 @@ static int stbi__psd_is16(stbi__context *s)
7206
7330
  stbi__rewind( s );
7207
7331
  return 0;
7208
7332
  }
7209
- (void) stbi__get32be(s);
7210
- (void) stbi__get32be(s);
7333
+ STBI_NOTUSED(stbi__get32be(s));
7334
+ STBI_NOTUSED(stbi__get32be(s));
7211
7335
  depth = stbi__get16be(s);
7212
7336
  if (depth != 16) {
7213
7337
  stbi__rewind( s );
@@ -7286,7 +7410,6 @@ static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp)
7286
7410
  // Known limitations:
7287
7411
  // Does not support comments in the header section
7288
7412
  // Does not support ASCII image data (formats P2 and P3)
7289
- // Does not support 16-bit-per-channel
7290
7413
 
7291
7414
  #ifndef STBI_NO_PNM
7292
7415
 
@@ -7307,7 +7430,8 @@ static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req
7307
7430
  stbi_uc *out;
7308
7431
  STBI_NOTUSED(ri);
7309
7432
 
7310
- if (!stbi__pnm_info(s, (int *)&s->img_x, (int *)&s->img_y, (int *)&s->img_n))
7433
+ ri->bits_per_channel = stbi__pnm_info(s, (int *)&s->img_x, (int *)&s->img_y, (int *)&s->img_n);
7434
+ if (ri->bits_per_channel == 0)
7311
7435
  return 0;
7312
7436
 
7313
7437
  if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)");
@@ -7317,12 +7441,12 @@ static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req
7317
7441
  *y = s->img_y;
7318
7442
  if (comp) *comp = s->img_n;
7319
7443
 
7320
- if (!stbi__mad3sizes_valid(s->img_n, s->img_x, s->img_y, 0))
7444
+ if (!stbi__mad4sizes_valid(s->img_n, s->img_x, s->img_y, ri->bits_per_channel / 8, 0))
7321
7445
  return stbi__errpuc("too large", "PNM too large");
7322
7446
 
7323
- out = (stbi_uc *) stbi__malloc_mad3(s->img_n, s->img_x, s->img_y, 0);
7447
+ out = (stbi_uc *) stbi__malloc_mad4(s->img_n, s->img_x, s->img_y, ri->bits_per_channel / 8, 0);
7324
7448
  if (!out) return stbi__errpuc("outofmem", "Out of memory");
7325
- stbi__getn(s, out, s->img_n * s->img_x * s->img_y);
7449
+ stbi__getn(s, out, s->img_n * s->img_x * s->img_y * (ri->bits_per_channel / 8));
7326
7450
 
7327
7451
  if (req_comp && req_comp != s->img_n) {
7328
7452
  out = stbi__convert_format(out, s->img_n, req_comp, s->img_x, s->img_y);
@@ -7398,11 +7522,19 @@ static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp)
7398
7522
  stbi__pnm_skip_whitespace(s, &c);
7399
7523
 
7400
7524
  maxv = stbi__pnm_getinteger(s, &c); // read max value
7401
-
7402
- if (maxv > 255)
7403
- return stbi__err("max value > 255", "PPM image not 8-bit");
7525
+ if (maxv > 65535)
7526
+ return stbi__err("max value > 65535", "PPM image supports only 8-bit and 16-bit images");
7527
+ else if (maxv > 255)
7528
+ return 16;
7404
7529
  else
7405
- return 1;
7530
+ return 8;
7531
+ }
7532
+
7533
+ static int stbi__pnm_is16(stbi__context *s)
7534
+ {
7535
+ if (stbi__pnm_info(s, NULL, NULL, NULL) == 16)
7536
+ return 1;
7537
+ return 0;
7406
7538
  }
7407
7539
  #endif
7408
7540
 
@@ -7458,6 +7590,9 @@ static int stbi__is_16_main(stbi__context *s)
7458
7590
  if (stbi__psd_is16(s)) return 1;
7459
7591
  #endif
7460
7592
 
7593
+ #ifndef STBI_NO_PNM
7594
+ if (stbi__pnm_is16(s)) return 1;
7595
+ #endif
7461
7596
  return 0;
7462
7597
  }
7463
7598