ruby-dnn 0.8.7 → 0.8.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '081852bef9d80484e000f4c67e2be9d7b7b5e4c4c72500800ab80ba6a56322e7'
4
- data.tar.gz: 8365b770fabd3a021ea7472058569be62fa9ef7d4f459a2220a614b2e213c8ed
3
+ metadata.gz: f7fe476037a4238a20708637ccd5c7fcd8e31d824e23019ba9d779a905eb5539
4
+ data.tar.gz: 2f6a78ab7fdd95cb73992b43f9f40868af838d58ca53b489e48f1bdff4ca3dbb
5
5
  SHA512:
6
- metadata.gz: c3ee7b5203c8cb70e4cbd375d6c53f4a61e54d73aafc77582acc61fd2dd11b8a54642264ef70eaf4f02385b53d62303c0c0ca56de68ddfc2f80a39ca343953d8
7
- data.tar.gz: d26ef9bf7040e200fd9ed3673fab1538432f1174669cbbe944a8c6566736a5ea7f8a355d0a0672e3b1f51a6e0e1cdf94f8df5b836a2b2b2ce7370160c7a6066d
6
+ metadata.gz: 8a86bbe0fdc6cf4a572b33f6ec03256fee9ed61cba365c639e108efba60572752ff8fc5d0ccf76401f9885599e269423a5e0aadbf32fff08cf4f25ab002732ec
7
+ data.tar.gz: cd4615a5f1bcd4b495b9583d672dcbe248245ba26de5bc6fe768cd5e98a0ba1dce3570b266c5a15360fd45879325af6ad79a6a2620f1927f2ef1e03a9478bdb2
data/Rakefile CHANGED
@@ -3,16 +3,17 @@ require "rake/testtask"
3
3
 
4
4
  Rake::TestTask.new(:test) do |t|
5
5
  t.libs << "test"
6
+ t.libs << "ext"
6
7
  t.libs << "lib"
7
8
  t.test_files = FileList["test/*_test.rb"]
8
9
  end
9
10
 
10
11
  task :build_dataset_loader do
11
- sh "cd lib/dnn/ext/cifar10_loader; ruby extconf.rb; make"
12
+ sh "cd ext/cifar10_loader; ruby extconf.rb; make"
12
13
  end
13
14
 
14
15
  task :build_image_io do
15
- sh "cd lib/dnn/ext/rb_stb_image; ruby extconf.rb; make"
16
+ sh "cd ext/rb_stb_image; ruby extconf.rb; make"
16
17
  end
17
18
 
18
19
  task :default => [:test, :build_dataset_loader, :build_image_io]
File without changes
File without changes
@@ -4,8 +4,8 @@
4
4
  #define STB_IMAGE_IMPLEMENTATION
5
5
  #define STB_IMAGE_WRITE_IMPLEMENTATION
6
6
 
7
- #include "stb_image.h"
8
- #include "stb_image_write.h"
7
+ #include "../../third_party/stb_image.h"
8
+ #include "../../third_party/stb_image_write.h"
9
9
 
10
10
  // STBIDEF stbi_uc *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp);
11
11
  static VALUE rb_stbi_load(VALUE self, VALUE rb_filename, VALUE rb_req_comp) {
@@ -24,20 +24,20 @@ module DNN
24
24
  @stateful = stateful
25
25
  @return_sequences = return_sequences
26
26
  @layers = []
27
- @params[:h] = nil
27
+ @hidden = @params[:h] = Param.new
28
28
  @params[:weight2] = @weight2 = Param.new
29
29
  end
30
30
 
31
31
  def forward(xs)
32
32
  @xs_shape = xs.shape
33
33
  hs = Xumo::SFloat.zeros(xs.shape[0], @time_length, @num_nodes)
34
- h = (@stateful && @params[:h]) ? @params[:h] : Xumo::SFloat.zeros(xs.shape[0], @num_nodes)
34
+ h = (@stateful && @hidden.data) ? @hidden.data : Xumo::SFloat.zeros(xs.shape[0], @num_nodes)
35
35
  xs.shape[1].times do |t|
36
36
  x = xs[true, t, false]
37
37
  h = @layers[t].forward(x, h)
38
38
  hs[true, t, false] = h
39
39
  end
40
- @params[:h] = h
40
+ @hidden.data = h
41
41
  @return_sequences ? hs : h
42
42
  end
43
43
 
@@ -75,7 +75,7 @@ module DNN
75
75
  end
76
76
 
77
77
  def reset_state
78
- @params[:h] = @params[:h].fill(0) if @params[:h]
78
+ @hidden.data = @hidden.data.fill(0) if @hidden.data
79
79
  end
80
80
 
81
81
  def lasso
@@ -274,7 +274,7 @@ module DNN
274
274
  l1_lambda: 0,
275
275
  l2_lambda: 0)
276
276
  super
277
- @params[:c] = nil
277
+ @cell = @params[:c] = Param.new
278
278
  end
279
279
 
280
280
  def forward(xs)
@@ -283,8 +283,8 @@ module DNN
283
283
  h = nil
284
284
  c = nil
285
285
  if @stateful
286
- h = @params[:h] if @params[:h]
287
- c = @params[:c] if @params[:c]
286
+ h = @hidden.data if @hidden.data
287
+ c = @cell.data if @cell.data
288
288
  end
289
289
  h ||= Xumo::SFloat.zeros(xs.shape[0], @num_nodes)
290
290
  c ||= Xumo::SFloat.zeros(xs.shape[0], @num_nodes)
@@ -293,8 +293,8 @@ module DNN
293
293
  h, c = @layers[t].forward(x, h, c)
294
294
  hs[true, t, false] = h
295
295
  end
296
- @params[:h] = h
297
- @params[:c] = c
296
+ @hidden.data = h
297
+ @cell.data = c
298
298
  @return_sequences ? hs : h
299
299
  end
300
300
 
@@ -320,7 +320,7 @@ module DNN
320
320
 
321
321
  def reset_state
322
322
  super()
323
- @params[:c] = @params[:c].fill(0) if @params[:c]
323
+ @cell.data = @cell.data.fill(0) if @cell.data
324
324
  end
325
325
 
326
326
  private
@@ -1,6 +1,6 @@
1
1
  require "zlib"
2
2
  require "archive/tar/minitar"
3
- require_relative "../ext/cifar10_loader/cifar10_loader"
3
+ require_relative "../../../ext/cifar10_loader/cifar10_loader"
4
4
  require_relative "downloader"
5
5
 
6
6
  URL_CIFAR10 = "https://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz"
data/lib/dnn/lib/image.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require "numo/narray"
2
- require_relative "../ext/rb_stb_image/rb_stb_image"
2
+ require_relative "../../../ext/rb_stb_image/rb_stb_image"
3
3
 
4
4
  module DNN
5
5
  module Image
data/lib/dnn/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module DNN
2
- VERSION = "0.8.7"
2
+ VERSION = "0.8.8"
3
3
  end
data/ruby-dnn.gemspec CHANGED
@@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
13
13
  spec.description = %q{ruby-dnn is a ruby deep learning library.}
14
14
  spec.homepage = "https://github.com/unagiootoro/ruby-dnn.git"
15
15
  spec.license = "MIT"
16
- spec.extensions = ["lib/dnn/ext/cifar10_loader/extconf.rb", "lib/dnn/ext/rb_stb_image/extconf.rb"]
16
+ spec.extensions = ["ext/cifar10_loader/extconf.rb", "ext/rb_stb_image/extconf.rb"]
17
17
 
18
18
  spec.add_dependency "numo-narray"
19
19
  spec.add_dependency "archive-tar-minitar"
@@ -1,4 +1,4 @@
1
- /* stb_image - v2.19 - public domain image loader - http://nothings.org/stb
1
+ /* stb_image - v2.22 - 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,9 @@ LICENSE
48
48
 
49
49
  RECENT REVISION HISTORY:
50
50
 
51
+ 2.22 (2019-03-04) gif fixes, fix warnings
52
+ 2.21 (2019-02-25) fix typo in comment
53
+ 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs
51
54
  2.19 (2018-02-11) fix warning
52
55
  2.18 (2018-01-30) fix warnings
53
56
  2.17 (2018-01-29) bugfix, 1-bit BMP, 16-bitness query, fix warnings
@@ -84,6 +87,7 @@ RECENT REVISION HISTORY:
84
87
  Fabian "ryg" Giesen Anael Seghezzi (is-16-bit query)
85
88
  Arseny Kapoulkine
86
89
  John-Mark Allen
90
+ Carmelo J Fdez-Aguera
87
91
 
88
92
  Bug & warning fixes
89
93
  Marc LeBlanc David Woo Guillaume George Martins Mozeiko
@@ -99,7 +103,7 @@ RECENT REVISION HISTORY:
99
103
  Aldo Culquicondor Philipp Wiesemann Dale Weiler github:sammyhw
100
104
  Oriol Ferrer Mesia Josh Tobin Matthew Gregan github:phprus
101
105
  Julian Raschke Gregory Mullen Baldur Karlsson github:poppolopoppo
102
- Christian Floisand Kevin Schmidt github:darealshinji
106
+ Christian Floisand Kevin Schmidt JR Smith github:darealshinji
103
107
  Blazej Dariusz Roszkowski github:Michaelangel007
104
108
  */
105
109
 
@@ -161,6 +165,16 @@ RECENT REVISION HISTORY:
161
165
  //
162
166
  // ===========================================================================
163
167
  //
168
+ // UNICODE:
169
+ //
170
+ // If compiling for Windows and you wish to use Unicode filenames, compile
171
+ // with
172
+ // #define STBI_WINDOWS_UTF8
173
+ // and pass utf8-encoded filenames. Call stbi_convert_wchar_to_utf8 to convert
174
+ // Windows wchar_t filenames to utf8.
175
+ //
176
+ // ===========================================================================
177
+ //
164
178
  // Philosophy
165
179
  //
166
180
  // stb libraries are designed with the following priorities:
@@ -171,12 +185,12 @@ RECENT REVISION HISTORY:
171
185
  //
172
186
  // Sometimes I let "good performance" creep up in priority over "easy to maintain",
173
187
  // and for best performance I may provide less-easy-to-use APIs that give higher
174
- // performance, in addition to the easy to use ones. Nevertheless, it's important
188
+ // performance, in addition to the easy-to-use ones. Nevertheless, it's important
175
189
  // to keep in mind that from the standpoint of you, a client of this library,
176
190
  // all you care about is #1 and #3, and stb libraries DO NOT emphasize #3 above all.
177
191
  //
178
192
  // Some secondary priorities arise directly from the first two, some of which
179
- // make more explicit reasons why performance can't be emphasized.
193
+ // provide more explicit reasons why performance can't be emphasized.
180
194
  //
181
195
  // - Portable ("ease of use")
182
196
  // - Small source code footprint ("easy to maintain")
@@ -219,11 +233,10 @@ RECENT REVISION HISTORY:
219
233
  //
220
234
  // HDR image support (disable by defining STBI_NO_HDR)
221
235
  //
222
- // stb_image now supports loading HDR images in general, and currently
223
- // the Radiance .HDR file format, although the support is provided
224
- // generically. You can still load any file through the existing interface;
225
- // if you attempt to load an HDR file, it will be automatically remapped to
226
- // LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
236
+ // stb_image supports loading HDR images in general, and currently the Radiance
237
+ // .HDR file format specifically. You can still load any file through the existing
238
+ // interface; if you attempt to load an HDR file, it will be automatically remapped
239
+ // to LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
227
240
  // both of these constants can be reconfigured through this interface:
228
241
  //
229
242
  // stbi_hdr_to_ldr_gamma(2.2f);
@@ -257,7 +270,7 @@ RECENT REVISION HISTORY:
257
270
  //
258
271
  // By default we convert iphone-formatted PNGs back to RGB, even though
259
272
  // they are internally encoded differently. You can disable this conversion
260
- // by by calling stbi_convert_iphone_png_to_rgb(0), in which case
273
+ // by calling stbi_convert_iphone_png_to_rgb(0), in which case
261
274
  // you will always just get the native iphone "format" through (which
262
275
  // is BGR stored in RGB).
263
276
  //
@@ -319,6 +332,7 @@ enum
319
332
  STBI_rgb_alpha = 4
320
333
  };
321
334
 
335
+ #include <stdlib.h>
322
336
  typedef unsigned char stbi_uc;
323
337
  typedef unsigned short stbi_us;
324
338
 
@@ -326,11 +340,13 @@ typedef unsigned short stbi_us;
326
340
  extern "C" {
327
341
  #endif
328
342
 
343
+ #ifndef STBIDEF
329
344
  #ifdef STB_IMAGE_STATIC
330
345
  #define STBIDEF static
331
346
  #else
332
347
  #define STBIDEF extern
333
348
  #endif
349
+ #endif
334
350
 
335
351
  //////////////////////////////////////////////////////////////////////////////
336
352
  //
@@ -355,10 +371,6 @@ typedef struct
355
371
 
356
372
  STBIDEF stbi_uc *stbi_load_from_memory (stbi_uc const *buffer, int len , int *x, int *y, int *channels_in_file, int desired_channels);
357
373
  STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk , void *user, int *x, int *y, int *channels_in_file, int desired_channels);
358
- #ifndef STBI_NO_GIF
359
- 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);
360
- #endif
361
-
362
374
 
363
375
  #ifndef STBI_NO_STDIO
364
376
  STBIDEF stbi_uc *stbi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
@@ -366,6 +378,14 @@ STBIDEF stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *channels_in
366
378
  // for stbi_load_from_file, file pointer is left pointing immediately after image
367
379
  #endif
368
380
 
381
+ #ifndef STBI_NO_GIF
382
+ 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);
383
+ #endif
384
+
385
+ #ifdef STBI_WINDOWS_UTF8
386
+ STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input);
387
+ #endif
388
+
369
389
  ////////////////////////////////////
370
390
  //
371
391
  // 16-bits-per-channel interface
@@ -525,6 +545,12 @@ STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const ch
525
545
  #define STBI_ASSERT(x) assert(x)
526
546
  #endif
527
547
 
548
+ #ifdef __cplusplus
549
+ #define STBI_EXTERN extern "C"
550
+ #else
551
+ #define STBI_EXTERN extern
552
+ #endif
553
+
528
554
 
529
555
  #ifndef _MSC_VER
530
556
  #ifdef __cplusplus
@@ -649,14 +675,18 @@ static int stbi__cpuid3(void)
649
675
 
650
676
  #define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name
651
677
 
678
+ #if !defined(STBI_NO_JPEG) && defined(STBI_SSE2)
652
679
  static int stbi__sse2_available(void)
653
680
  {
654
681
  int info3 = stbi__cpuid3();
655
682
  return ((info3 >> 26) & 1) != 0;
656
683
  }
684
+ #endif
685
+
657
686
  #else // assume GCC-style if not VC++
658
687
  #define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
659
688
 
689
+ #if !defined(STBI_NO_JPEG) && defined(STBI_SSE2)
660
690
  static int stbi__sse2_available(void)
661
691
  {
662
692
  // If we're even attempting to compile this on GCC/Clang, that means
@@ -664,6 +694,8 @@ static int stbi__sse2_available(void)
664
694
  // instructions at will, and so are we.
665
695
  return 1;
666
696
  }
697
+ #endif
698
+
667
699
  #endif
668
700
  #endif
669
701
 
@@ -1070,6 +1102,7 @@ static void stbi__vertical_flip(void *image, int w, int h, int bytes_per_pixel)
1070
1102
  }
1071
1103
  }
1072
1104
 
1105
+ #ifndef STBI_NO_GIF
1073
1106
  static void stbi__vertical_flip_slices(void *image, int w, int h, int z, int bytes_per_pixel)
1074
1107
  {
1075
1108
  int slice;
@@ -1081,6 +1114,7 @@ static void stbi__vertical_flip_slices(void *image, int w, int h, int z, int byt
1081
1114
  bytes += slice_size;
1082
1115
  }
1083
1116
  }
1117
+ #endif
1084
1118
 
1085
1119
  static unsigned char *stbi__load_and_postprocess_8bit(stbi__context *s, int *x, int *y, int *comp, int req_comp)
1086
1120
  {
@@ -1131,7 +1165,7 @@ static stbi__uint16 *stbi__load_and_postprocess_16bit(stbi__context *s, int *x,
1131
1165
  return (stbi__uint16 *) result;
1132
1166
  }
1133
1167
 
1134
- #if !defined(STBI_NO_HDR) || !defined(STBI_NO_LINEAR)
1168
+ #if !defined(STBI_NO_HDR) && !defined(STBI_NO_LINEAR)
1135
1169
  static void stbi__float_postprocess(float *result, int *x, int *y, int *comp, int req_comp)
1136
1170
  {
1137
1171
  if (stbi__vertically_flip_on_load && result != NULL) {
@@ -1143,10 +1177,38 @@ static void stbi__float_postprocess(float *result, int *x, int *y, int *comp, in
1143
1177
 
1144
1178
  #ifndef STBI_NO_STDIO
1145
1179
 
1180
+ #if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)
1181
+ STBI_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide);
1182
+ 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);
1183
+ #endif
1184
+
1185
+ #if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)
1186
+ STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input)
1187
+ {
1188
+ return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL);
1189
+ }
1190
+ #endif
1191
+
1146
1192
  static FILE *stbi__fopen(char const *filename, char const *mode)
1147
1193
  {
1148
1194
  FILE *f;
1149
- #if defined(_MSC_VER) && _MSC_VER >= 1400
1195
+ #if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)
1196
+ wchar_t wMode[64];
1197
+ wchar_t wFilename[1024];
1198
+ if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename)))
1199
+ return 0;
1200
+
1201
+ if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode)))
1202
+ return 0;
1203
+
1204
+ #if _MSC_VER >= 1400
1205
+ if (0 != _wfopen_s(&f, wFilename, wMode))
1206
+ f = 0;
1207
+ #else
1208
+ f = _wfopen(wFilename, wMode);
1209
+ #endif
1210
+
1211
+ #elif defined(_MSC_VER) && _MSC_VER >= 1400
1150
1212
  if (0 != fopen_s(&f, filename, mode))
1151
1213
  f=0;
1152
1214
  #else
@@ -1539,18 +1601,18 @@ static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int r
1539
1601
  // convert source image with img_n components to one with req_comp components;
1540
1602
  // avoid switch per pixel, so use switch per scanline and massive macros
1541
1603
  switch (STBI__COMBO(img_n, req_comp)) {
1542
- STBI__CASE(1,2) { dest[0]=src[0], dest[1]=255; } break;
1604
+ STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=255; } break;
1543
1605
  STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
1544
- STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=255; } break;
1606
+ STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=255; } break;
1545
1607
  STBI__CASE(2,1) { dest[0]=src[0]; } break;
1546
1608
  STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
1547
- STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1]; } break;
1548
- STBI__CASE(3,4) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=255; } break;
1609
+ STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break;
1610
+ STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=255; } break;
1549
1611
  STBI__CASE(3,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break;
1550
- STBI__CASE(3,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]), dest[1] = 255; } break;
1612
+ STBI__CASE(3,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = 255; } break;
1551
1613
  STBI__CASE(4,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break;
1552
- STBI__CASE(4,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]), dest[1] = src[3]; } break;
1553
- STBI__CASE(4,3) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2]; } break;
1614
+ STBI__CASE(4,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = src[3]; } break;
1615
+ STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break;
1554
1616
  default: STBI_ASSERT(0);
1555
1617
  }
1556
1618
  #undef STBI__CASE
@@ -1588,18 +1650,18 @@ static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int r
1588
1650
  // convert source image with img_n components to one with req_comp components;
1589
1651
  // avoid switch per pixel, so use switch per scanline and massive macros
1590
1652
  switch (STBI__COMBO(img_n, req_comp)) {
1591
- STBI__CASE(1,2) { dest[0]=src[0], dest[1]=0xffff; } break;
1653
+ STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=0xffff; } break;
1592
1654
  STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
1593
- STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=0xffff; } break;
1655
+ STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=0xffff; } break;
1594
1656
  STBI__CASE(2,1) { dest[0]=src[0]; } break;
1595
1657
  STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
1596
- STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1]; } break;
1597
- STBI__CASE(3,4) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=0xffff; } break;
1658
+ STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break;
1659
+ STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=0xffff; } break;
1598
1660
  STBI__CASE(3,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break;
1599
- STBI__CASE(3,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]), dest[1] = 0xffff; } break;
1661
+ STBI__CASE(3,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = 0xffff; } break;
1600
1662
  STBI__CASE(4,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break;
1601
- STBI__CASE(4,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]), dest[1] = src[3]; } break;
1602
- STBI__CASE(4,3) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2]; } break;
1663
+ STBI__CASE(4,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = src[3]; } break;
1664
+ STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break;
1603
1665
  default: STBI_ASSERT(0);
1604
1666
  }
1605
1667
  #undef STBI__CASE
@@ -1623,7 +1685,11 @@ static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp)
1623
1685
  for (k=0; k < n; ++k) {
1624
1686
  output[i*comp + k] = (float) (pow(data[i*comp+k]/255.0f, stbi__l2h_gamma) * stbi__l2h_scale);
1625
1687
  }
1626
- if (k < comp) output[i*comp + k] = data[i*comp+k]/255.0f;
1688
+ }
1689
+ if (n < comp) {
1690
+ for (i=0; i < x*y; ++i) {
1691
+ output[i*comp + n] = data[i*comp + n]/255.0f;
1692
+ }
1627
1693
  }
1628
1694
  STBI_FREE(data);
1629
1695
  return output;
@@ -3596,7 +3662,7 @@ static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp
3596
3662
  int k;
3597
3663
  unsigned int i,j;
3598
3664
  stbi_uc *output;
3599
- stbi_uc *coutput[4];
3665
+ stbi_uc *coutput[4] = { NULL, NULL, NULL, NULL };
3600
3666
 
3601
3667
  stbi__resample res_comp[4];
3602
3668
 
@@ -3717,7 +3783,7 @@ static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp
3717
3783
  if (n == 1)
3718
3784
  for (i=0; i < z->s->img_x; ++i) out[i] = y[i];
3719
3785
  else
3720
- for (i=0; i < z->s->img_x; ++i) *out++ = y[i], *out++ = 255;
3786
+ for (i=0; i < z->s->img_x; ++i) { *out++ = y[i]; *out++ = 255; }
3721
3787
  }
3722
3788
  }
3723
3789
  }
@@ -4731,7 +4797,7 @@ static void stbi__de_iphone(stbi__png *z)
4731
4797
  static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
4732
4798
  {
4733
4799
  stbi_uc palette[1024], pal_img_n=0;
4734
- stbi_uc has_trans=0, tc[3];
4800
+ stbi_uc has_trans=0, tc[3]={0};
4735
4801
  stbi__uint16 tc16[3];
4736
4802
  stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0;
4737
4803
  int first=1,k,interlace=0, color=0, is_iphone=0;
@@ -5009,11 +5075,11 @@ static int stbi__high_bit(unsigned int z)
5009
5075
  {
5010
5076
  int n=0;
5011
5077
  if (z == 0) return -1;
5012
- if (z >= 0x10000) n += 16, z >>= 16;
5013
- if (z >= 0x00100) n += 8, z >>= 8;
5014
- if (z >= 0x00010) n += 4, z >>= 4;
5015
- if (z >= 0x00004) n += 2, z >>= 2;
5016
- if (z >= 0x00002) n += 1, z >>= 1;
5078
+ if (z >= 0x10000) { n += 16; z >>= 16; }
5079
+ if (z >= 0x00100) { n += 8; z >>= 8; }
5080
+ if (z >= 0x00010) { n += 4; z >>= 4; }
5081
+ if (z >= 0x00004) { n += 2; z >>= 2; }
5082
+ if (z >= 0x00002) { n += 1; z >>= 1; }
5017
5083
  return n;
5018
5084
  }
5019
5085
 
@@ -5030,7 +5096,7 @@ static int stbi__bitcount(unsigned int a)
5030
5096
  // extract an arbitrarily-aligned N-bit value (N=bits)
5031
5097
  // from v, and then make it 8-bits long and fractionally
5032
5098
  // extend it to full full range.
5033
- static int stbi__shiftsigned(int v, int shift, int bits)
5099
+ static int stbi__shiftsigned(unsigned int v, int shift, int bits)
5034
5100
  {
5035
5101
  static unsigned int mul_table[9] = {
5036
5102
  0,
@@ -5207,6 +5273,8 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5207
5273
  out[z++] = pal[color][0];
5208
5274
  out[z++] = pal[color][1];
5209
5275
  out[z++] = pal[color][2];
5276
+ if (target == 4) out[z++] = 255;
5277
+ if (i+1 == (int) s->img_x) break;
5210
5278
  if((--bit_offset) < 0) {
5211
5279
  bit_offset = 7;
5212
5280
  v = stbi__get8(s);
@@ -5299,7 +5367,7 @@ static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req
5299
5367
  stbi_uc *p1 = out + j *s->img_x*target;
5300
5368
  stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target;
5301
5369
  for (i=0; i < (int) s->img_x*target; ++i) {
5302
- t = p1[i], p1[i] = p2[i], p2[i] = t;
5370
+ t = p1[i]; p1[i] = p2[i]; p2[i] = t;
5303
5371
  }
5304
5372
  }
5305
5373
  }
@@ -5789,7 +5857,7 @@ static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req
5789
5857
  // Else if n is 128, noop.
5790
5858
  // Endloop
5791
5859
 
5792
- // The RLE-compressed data is preceeded by a 2-byte data count for each row in the data,
5860
+ // The RLE-compressed data is preceded by a 2-byte data count for each row in the data,
5793
5861
  // which we're going to just skip.
5794
5862
  stbi__skip(s, h * channelCount * 2 );
5795
5863
 
@@ -6342,22 +6410,27 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i
6342
6410
  int first_frame;
6343
6411
  int pi;
6344
6412
  int pcount;
6413
+ STBI_NOTUSED(req_comp);
6345
6414
 
6346
6415
  // on first frame, any non-written pixels get the background colour (non-transparent)
6347
6416
  first_frame = 0;
6348
6417
  if (g->out == 0) {
6349
- if (!stbi__gif_header(s, g, comp,0)) return 0; // stbi__g_failure_reason set by stbi__gif_header
6350
- g->out = (stbi_uc *) stbi__malloc(4 * g->w * g->h);
6351
- g->background = (stbi_uc *) stbi__malloc(4 * g->w * g->h);
6352
- g->history = (stbi_uc *) stbi__malloc(g->w * g->h);
6353
- if (g->out == 0) return stbi__errpuc("outofmem", "Out of memory");
6354
-
6355
- // image is treated as "tranparent" at the start - ie, nothing overwrites the current background;
6418
+ if (!stbi__gif_header(s, g, comp,0)) return 0; // stbi__g_failure_reason set by stbi__gif_header
6419
+ if (!stbi__mad3sizes_valid(4, g->w, g->h, 0))
6420
+ return stbi__errpuc("too large", "GIF image is too large");
6421
+ pcount = g->w * g->h;
6422
+ g->out = (stbi_uc *) stbi__malloc(4 * pcount);
6423
+ g->background = (stbi_uc *) stbi__malloc(4 * pcount);
6424
+ g->history = (stbi_uc *) stbi__malloc(pcount);
6425
+ if (!g->out || !g->background || !g->history)
6426
+ return stbi__errpuc("outofmem", "Out of memory");
6427
+
6428
+ // image is treated as "transparent" at the start - ie, nothing overwrites the current background;
6356
6429
  // background colour is only used for pixels that are not rendered first frame, after that "background"
6357
- // color refers to teh color that was there the previous frame.
6358
- memset( g->out, 0x00, 4 * g->w * g->h );
6359
- memset( g->background, 0x00, 4 * g->w * g->h ); // state of the background (starts transparent)
6360
- memset( g->history, 0x00, g->w * g->h ); // pixels that were affected previous frame
6430
+ // color refers to the color that was there the previous frame.
6431
+ memset(g->out, 0x00, 4 * pcount);
6432
+ memset(g->background, 0x00, 4 * pcount); // state of the background (starts transparent)
6433
+ memset(g->history, 0x00, pcount); // pixels that were affected previous frame
6361
6434
  first_frame = 1;
6362
6435
  } else {
6363
6436
  // second frame - how do we dispoase of the previous one?
@@ -6418,6 +6491,13 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i
6418
6491
  g->cur_x = g->start_x;
6419
6492
  g->cur_y = g->start_y;
6420
6493
 
6494
+ // if the width of the specified rectangle is 0, that means
6495
+ // we may not see *any* pixels or the image is malformed;
6496
+ // to make sure this is caught, move the current y down to
6497
+ // max_y (which is what out_gif_code checks).
6498
+ if (w == 0)
6499
+ g->cur_y = g->max_y;
6500
+
6421
6501
  g->lflags = stbi__get8(s);
6422
6502
 
6423
6503
  if (g->lflags & 0x40) {
@@ -6437,7 +6517,7 @@ static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, i
6437
6517
  return stbi__errpuc("missing color table", "Corrupt GIF");
6438
6518
 
6439
6519
  o = stbi__process_gif_raster(s, g);
6440
- if (o == NULL) return NULL;
6520
+ if (!o) return NULL;
6441
6521
 
6442
6522
  // if this was the first frame,
6443
6523
  pcount = g->w * g->h;
@@ -6565,6 +6645,7 @@ static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req
6565
6645
  stbi_uc *u = 0;
6566
6646
  stbi__gif g;
6567
6647
  memset(&g, 0, sizeof(g));
6648
+ STBI_NOTUSED(ri);
6568
6649
 
6569
6650
  u = stbi__gif_load_next(s, &g, comp, req_comp, 0);
6570
6651
  if (u == (stbi_uc *) s) u = 0; // end of animated gif marker
@@ -6576,6 +6657,9 @@ static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req
6576
6657
  // can be done for multiple frames.
6577
6658
  if (req_comp && req_comp != 4)
6578
6659
  u = stbi__convert_format(u, 4, req_comp, g.w, g.h);
6660
+ } else if (g.out) {
6661
+ // if there was an error and we allocated an image buffer, free it!
6662
+ STBI_FREE(g.out);
6579
6663
  }
6580
6664
 
6581
6665
  // free buffers needed for multiple frame loading;
@@ -7238,6 +7322,7 @@ STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *c, void *user
7238
7322
 
7239
7323
  /*
7240
7324
  revision history:
7325
+ 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs
7241
7326
  2.19 (2018-02-11) fix warning
7242
7327
  2.18 (2018-01-30) fix warnings
7243
7328
  2.17 (2018-01-29) change sbti__shiftsigned to avoid clang -O2 bug
@@ -1,4 +1,4 @@
1
- /* stb_image_write - v1.09 - public domain - http://nothings.org/stb/stb_image_write.h
1
+ /* stb_image_write - v1.13 - public domain - http://nothings.org/stb/stb_image_write.h
2
2
  writes out PNG/BMP/TGA/JPEG/HDR images to C stdio - Sean Barrett 2010-2015
3
3
  no warranty implied; use at your own risk
4
4
 
@@ -17,8 +17,7 @@
17
17
 
18
18
  ABOUT:
19
19
 
20
- This header file is a library for writing images to C stdio. It could be
21
- adapted to write to memory or a general streaming interface; let me know.
20
+ This header file is a library for writing images to C stdio or a callback.
22
21
 
23
22
  The PNG output is not optimal; it is 20-50% larger than the file
24
23
  written by a decent optimizing implementation; though providing a custom
@@ -38,6 +37,14 @@ BUILDING:
38
37
  The returned data will be freed with STBIW_FREE() (free() by default),
39
38
  so it must be heap allocated with STBIW_MALLOC() (malloc() by default),
40
39
 
40
+ UNICODE:
41
+
42
+ If compiling for Windows and you wish to use Unicode filenames, compile
43
+ with
44
+ #define STBIW_WINDOWS_UTF8
45
+ and pass utf8-encoded filenames. Call stbiw_convert_wchar_to_utf8 to convert
46
+ Windows wchar_t filenames to utf8.
47
+
41
48
  USAGE:
42
49
 
43
50
  There are five functions, one for each image file format:
@@ -148,6 +155,8 @@ LICENSE
148
155
  #ifndef INCLUDE_STB_IMAGE_WRITE_H
149
156
  #define INCLUDE_STB_IMAGE_WRITE_H
150
157
 
158
+ #include <stdlib.h>
159
+
151
160
  // if STB_IMAGE_WRITE_STATIC causes problems, try defining STBIWDEF to 'inline' or 'static inline'
152
161
  #ifndef STBIWDEF
153
162
  #ifdef STB_IMAGE_WRITE_STATIC
@@ -173,6 +182,10 @@ STBIWDEF int stbi_write_bmp(char const *filename, int w, int h, int comp, const
173
182
  STBIWDEF int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);
174
183
  STBIWDEF int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data);
175
184
  STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality);
185
+
186
+ #ifdef STBI_WINDOWS_UTF8
187
+ STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input);
188
+ #endif
176
189
  #endif
177
190
 
178
191
  typedef void stbi_write_func(void *context, void *data, int size);
@@ -275,15 +288,52 @@ static void stbi__stdio_write(void *context, void *data, int size)
275
288
  fwrite(data,1,size,(FILE*) context);
276
289
  }
277
290
 
278
- static int stbi__start_write_file(stbi__write_context *s, const char *filename)
291
+ #if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)
292
+ #ifdef __cplusplus
293
+ #define STBIW_EXTERN extern "C"
294
+ #else
295
+ #define STBIW_EXTERN extern
296
+ #endif
297
+ STBIW_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide);
298
+ STBIW_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);
299
+
300
+ STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input)
301
+ {
302
+ return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL);
303
+ }
304
+ #endif
305
+
306
+ static FILE *stbiw__fopen(char const *filename, char const *mode)
279
307
  {
280
308
  FILE *f;
281
- #ifdef STBI_MSC_SECURE_CRT
282
- if (fopen_s(&f, filename, "wb"))
283
- f = NULL;
309
+ #if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)
310
+ wchar_t wMode[64];
311
+ wchar_t wFilename[1024];
312
+ if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename)))
313
+ return 0;
314
+
315
+ if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode)))
316
+ return 0;
317
+
318
+ #if _MSC_VER >= 1400
319
+ if (0 != _wfopen_s(&f, wFilename, wMode))
320
+ f = 0;
321
+ #else
322
+ f = _wfopen(wFilename, wMode);
323
+ #endif
324
+
325
+ #elif defined(_MSC_VER) && _MSC_VER >= 1400
326
+ if (0 != fopen_s(&f, filename, mode))
327
+ f=0;
284
328
  #else
285
- f = fopen(filename, "wb");
329
+ f = fopen(filename, mode);
286
330
  #endif
331
+ return f;
332
+ }
333
+
334
+ static int stbi__start_write_file(stbi__write_context *s, const char *filename)
335
+ {
336
+ FILE *f = stbiw__fopen(filename, "wb");
287
337
  stbi__start_write_callbacks(s, stbi__stdio_write, (void *) f);
288
338
  return f != NULL;
289
339
  }
@@ -343,7 +393,7 @@ static void stbiw__putc(stbi__write_context *s, unsigned char c)
343
393
  static void stbiw__write3(stbi__write_context *s, unsigned char a, unsigned char b, unsigned char c)
344
394
  {
345
395
  unsigned char arr[3];
346
- arr[0] = a, arr[1] = b, arr[2] = c;
396
+ arr[0] = a; arr[1] = b; arr[2] = c;
347
397
  s->func(s->context, arr, 3);
348
398
  }
349
399
 
@@ -391,10 +441,11 @@ static void stbiw__write_pixels(stbi__write_context *s, int rgb_dir, int vdir, i
391
441
  if (stbi__flip_vertically_on_write)
392
442
  vdir *= -1;
393
443
 
394
- if (vdir < 0)
395
- j_end = -1, j = y-1;
396
- else
397
- j_end = y, j = 0;
444
+ if (vdir < 0) {
445
+ j_end = -1; j = y-1;
446
+ } else {
447
+ j_end = y; j = 0;
448
+ }
398
449
 
399
450
  for (; j != j_end; j += vdir) {
400
451
  for (i=0; i < x; ++i) {
@@ -552,7 +603,7 @@ STBIWDEF int stbi_write_tga(char const *filename, int x, int y, int comp, const
552
603
 
553
604
  #define stbiw__max(a, b) ((a) > (b) ? (a) : (b))
554
605
 
555
- void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear)
606
+ static void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear)
556
607
  {
557
608
  int exponent;
558
609
  float maxcomp = stbiw__max(linear[0], stbiw__max(linear[1], linear[2]));
@@ -569,7 +620,7 @@ void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear)
569
620
  }
570
621
  }
571
622
 
572
- void stbiw__write_run_data(stbi__write_context *s, int length, unsigned char databyte)
623
+ static void stbiw__write_run_data(stbi__write_context *s, int length, unsigned char databyte)
573
624
  {
574
625
  unsigned char lengthbyte = STBIW_UCHAR(length+128);
575
626
  STBIW_ASSERT(length+128 <= 255);
@@ -577,7 +628,7 @@ void stbiw__write_run_data(stbi__write_context *s, int length, unsigned char dat
577
628
  s->func(s->context, &databyte, 1);
578
629
  }
579
630
 
580
- void stbiw__write_dump_data(stbi__write_context *s, int length, unsigned char *data)
631
+ static void stbiw__write_dump_data(stbi__write_context *s, int length, unsigned char *data)
581
632
  {
582
633
  unsigned char lengthbyte = STBIW_UCHAR(length);
583
634
  STBIW_ASSERT(length <= 128); // inconsistent with spec but consistent with official code
@@ -585,7 +636,7 @@ void stbiw__write_dump_data(stbi__write_context *s, int length, unsigned char *d
585
636
  s->func(s->context, data, length);
586
637
  }
587
638
 
588
- void stbiw__write_hdr_scanline(stbi__write_context *s, int width, int ncomp, unsigned char *scratch, float *scanline)
639
+ static void stbiw__write_hdr_scanline(stbi__write_context *s, int width, int ncomp, unsigned char *scratch, float *scanline)
589
640
  {
590
641
  unsigned char scanlineheader[4] = { 2, 2, 0, 0 };
591
642
  unsigned char rgbe[4];
@@ -686,15 +737,15 @@ static int stbi_write_hdr_core(stbi__write_context *s, int x, int y, int comp, f
686
737
  char header[] = "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n";
687
738
  s->func(s->context, header, sizeof(header)-1);
688
739
 
689
- #ifdef STBI_MSC_SECURE_CRT
690
- len = sprintf_s(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x);
740
+ #ifdef __STDC_WANT_SECURE_LIB__
741
+ len = sprintf_s(buffer, sizeof(buffer), "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x);
691
742
  #else
692
743
  len = sprintf(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x);
693
744
  #endif
694
745
  s->func(s->context, buffer, len);
695
746
 
696
747
  for(i=0; i < y; i++)
697
- stbiw__write_hdr_scanline(s, x, comp, scratch, data + comp*x*(stbi__flip_vertically_on_write ? y-1-i : i)*x);
748
+ stbiw__write_hdr_scanline(s, x, comp, scratch, data + comp*x*(stbi__flip_vertically_on_write ? y-1-i : i));
698
749
  STBIW_FREE(scratch);
699
750
  return 1;
700
751
  }
@@ -809,7 +860,7 @@ static unsigned int stbiw__zhash(unsigned char *data)
809
860
 
810
861
  #endif // STBIW_ZLIB_COMPRESS
811
862
 
812
- unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_len, int quality)
863
+ STBIWDEF unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_len, int quality)
813
864
  {
814
865
  #ifdef STBIW_ZLIB_COMPRESS
815
866
  // user provided a zlib compress implementation, use that
@@ -845,7 +896,7 @@ unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_l
845
896
  for (j=0; j < n; ++j) {
846
897
  if (hlist[j]-data > i-32768) { // if entry lies within window
847
898
  int d = stbiw__zlib_countm(hlist[j], data+i, data_len-i);
848
- if (d >= best) best=d,bestloc=hlist[j];
899
+ if (d >= best) { best=d; bestloc=hlist[j]; }
849
900
  }
850
901
  }
851
902
  // when hash table entry is too long, delete half the entries
@@ -904,8 +955,8 @@ unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_l
904
955
  int blocklen = (int) (data_len % 5552);
905
956
  j=0;
906
957
  while (j < data_len) {
907
- for (i=0; i < blocklen; ++i) s1 += data[j+i], s2 += s1;
908
- s1 %= 65521, s2 %= 65521;
958
+ for (i=0; i < blocklen; ++i) { s1 += data[j+i]; s2 += s1; }
959
+ s1 %= 65521; s2 %= 65521;
909
960
  j += blocklen;
910
961
  blocklen = 5552;
911
962
  }
@@ -923,6 +974,9 @@ unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_l
923
974
 
924
975
  static unsigned int stbiw__crc32(unsigned char *buffer, int len)
925
976
  {
977
+ #ifdef STBIW_CRC32
978
+ return STBIW_CRC32(buffer, len);
979
+ #else
926
980
  static unsigned int crc_table[256] =
927
981
  {
928
982
  0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
@@ -964,6 +1018,7 @@ static unsigned int stbiw__crc32(unsigned char *buffer, int len)
964
1018
  for (i=0; i < len; ++i)
965
1019
  crc = (crc >> 8) ^ crc_table[buffer[i] ^ (crc & 0xff)];
966
1020
  return ~crc;
1021
+ #endif
967
1022
  }
968
1023
 
969
1024
  #define stbiw__wpng4(o,a,b,c,d) ((o)[0]=STBIW_UCHAR(a),(o)[1]=STBIW_UCHAR(b),(o)[2]=STBIW_UCHAR(c),(o)[3]=STBIW_UCHAR(d),(o)+=4)
@@ -994,9 +1049,15 @@ static void stbiw__encode_png_line(unsigned char *pixels, int stride_bytes, int
994
1049
  int type = mymap[filter_type];
995
1050
  unsigned char *z = pixels + stride_bytes * (stbi__flip_vertically_on_write ? height-1-y : y);
996
1051
  int signed_stride = stbi__flip_vertically_on_write ? -stride_bytes : stride_bytes;
1052
+
1053
+ if (type==0) {
1054
+ memcpy(line_buffer, z, width*n);
1055
+ return;
1056
+ }
1057
+
1058
+ // first loop isn't optimized since it's just one pixel
997
1059
  for (i = 0; i < n; ++i) {
998
1060
  switch (type) {
999
- case 0: line_buffer[i] = z[i]; break;
1000
1061
  case 1: line_buffer[i] = z[i]; break;
1001
1062
  case 2: line_buffer[i] = z[i] - z[i-signed_stride]; break;
1002
1063
  case 3: line_buffer[i] = z[i] - (z[i-signed_stride]>>1); break;
@@ -1005,20 +1066,17 @@ static void stbiw__encode_png_line(unsigned char *pixels, int stride_bytes, int
1005
1066
  case 6: line_buffer[i] = z[i]; break;
1006
1067
  }
1007
1068
  }
1008
- for (i=n; i < width*n; ++i) {
1009
- switch (type) {
1010
- case 0: line_buffer[i] = z[i]; break;
1011
- case 1: line_buffer[i] = z[i] - z[i-n]; break;
1012
- case 2: line_buffer[i] = z[i] - z[i-signed_stride]; break;
1013
- case 3: line_buffer[i] = z[i] - ((z[i-n] + z[i-signed_stride])>>1); break;
1014
- case 4: line_buffer[i] = z[i] - stbiw__paeth(z[i-n], z[i-signed_stride], z[i-signed_stride-n]); break;
1015
- case 5: line_buffer[i] = z[i] - (z[i-n]>>1); break;
1016
- case 6: line_buffer[i] = z[i] - stbiw__paeth(z[i-n], 0,0); break;
1017
- }
1069
+ switch (type) {
1070
+ case 1: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - z[i-n]; break;
1071
+ case 2: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - z[i-signed_stride]; break;
1072
+ case 3: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - ((z[i-n] + z[i-signed_stride])>>1); break;
1073
+ case 4: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - stbiw__paeth(z[i-n], z[i-signed_stride], z[i-signed_stride-n]); break;
1074
+ case 5: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - (z[i-n]>>1); break;
1075
+ case 6: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - stbiw__paeth(z[i-n], 0,0); break;
1018
1076
  }
1019
1077
  }
1020
1078
 
1021
- unsigned char *stbi_write_png_to_mem(unsigned char *pixels, int stride_bytes, int x, int y, int n, int *out_len)
1079
+ STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, int stride_bytes, int x, int y, int n, int *out_len)
1022
1080
  {
1023
1081
  int force_filter = stbi_write_force_png_filter;
1024
1082
  int ctype[5] = { -1, 0, 4, 2, 6 };
@@ -1040,11 +1098,11 @@ unsigned char *stbi_write_png_to_mem(unsigned char *pixels, int stride_bytes, in
1040
1098
  int filter_type;
1041
1099
  if (force_filter > -1) {
1042
1100
  filter_type = force_filter;
1043
- stbiw__encode_png_line(pixels, stride_bytes, x, y, j, n, force_filter, line_buffer);
1101
+ stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, force_filter, line_buffer);
1044
1102
  } else { // Estimate the best filter by running through all of them:
1045
1103
  int best_filter = 0, best_filter_val = 0x7fffffff, est, i;
1046
1104
  for (filter_type = 0; filter_type < 5; filter_type++) {
1047
- stbiw__encode_png_line(pixels, stride_bytes, x, y, j, n, filter_type, line_buffer);
1105
+ stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, filter_type, line_buffer);
1048
1106
 
1049
1107
  // Estimate the entropy of the line using this filter; the less, the better.
1050
1108
  est = 0;
@@ -1057,7 +1115,7 @@ unsigned char *stbi_write_png_to_mem(unsigned char *pixels, int stride_bytes, in
1057
1115
  }
1058
1116
  }
1059
1117
  if (filter_type != best_filter) { // If the last iteration already got us the best filter, don't redo it
1060
- stbiw__encode_png_line(pixels, stride_bytes, x, y, j, n, best_filter, line_buffer);
1118
+ stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, best_filter, line_buffer);
1061
1119
  filter_type = best_filter;
1062
1120
  }
1063
1121
  }
@@ -1109,14 +1167,10 @@ STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const
1109
1167
  {
1110
1168
  FILE *f;
1111
1169
  int len;
1112
- unsigned char *png = stbi_write_png_to_mem((unsigned char *) data, stride_bytes, x, y, comp, &len);
1170
+ unsigned char *png = stbi_write_png_to_mem((const unsigned char *) data, stride_bytes, x, y, comp, &len);
1113
1171
  if (png == NULL) return 0;
1114
- #ifdef STBI_MSC_SECURE_CRT
1115
- if (fopen_s(&f, filename, "wb"))
1116
- f = NULL;
1117
- #else
1118
- f = fopen(filename, "wb");
1119
- #endif
1172
+
1173
+ f = stbiw__fopen(filename, "wb");
1120
1174
  if (!f) { STBIW_FREE(png); return 0; }
1121
1175
  fwrite(png, 1, len, f);
1122
1176
  fclose(f);
@@ -1128,7 +1182,7 @@ STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const
1128
1182
  STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int stride_bytes)
1129
1183
  {
1130
1184
  int len;
1131
- unsigned char *png = stbi_write_png_to_mem((unsigned char *) data, stride_bytes, x, y, comp, &len);
1185
+ unsigned char *png = stbi_write_png_to_mem((const unsigned char *) data, stride_bytes, x, y, comp, &len);
1132
1186
  if (png == NULL) return 0;
1133
1187
  func(context, png, len);
1134
1188
  STBIW_FREE(png);
@@ -1423,15 +1477,13 @@ static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, in
1423
1477
  for(x = 0; x < width; x += 8) {
1424
1478
  float YDU[64], UDU[64], VDU[64];
1425
1479
  for(row = y, pos = 0; row < y+8; ++row) {
1480
+ // row >= height => use last input row
1481
+ int clamped_row = (row < height) ? row : height - 1;
1482
+ int base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*width*comp;
1426
1483
  for(col = x; col < x+8; ++col, ++pos) {
1427
- int p = (stbi__flip_vertically_on_write ? height-1-row : row)*width*comp + col*comp;
1428
1484
  float r, g, b;
1429
- if(row >= height) {
1430
- p -= width*comp*(row+1 - height);
1431
- }
1432
- if(col >= width) {
1433
- p -= comp*(col+1 - width);
1434
- }
1485
+ // if col >= width => use pixel from last input column
1486
+ int p = base_p + ((col < width) ? col : (width-1))*comp;
1435
1487
 
1436
1488
  r = imageData[p+0];
1437
1489
  g = imageData[p+ofsG];
@@ -1483,6 +1535,8 @@ STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const
1483
1535
  #endif // STB_IMAGE_WRITE_IMPLEMENTATION
1484
1536
 
1485
1537
  /* Revision history
1538
+ 1.10 (2019-02-07)
1539
+ support utf8 filenames in Windows; fix warnings and platform ifdefs
1486
1540
  1.09 (2018-02-11)
1487
1541
  fix typo in zlib quality API, improve STB_I_W_STATIC in C++
1488
1542
  1.08 (2018-01-29)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-dnn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.7
4
+ version: 0.8.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - unagiootoro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-03-16 00:00:00.000000000 Z
11
+ date: 2019-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: numo-narray
@@ -85,8 +85,8 @@ email:
85
85
  - ootoro838861@outlook.jp
86
86
  executables: []
87
87
  extensions:
88
- - lib/dnn/ext/cifar10_loader/extconf.rb
89
- - lib/dnn/ext/rb_stb_image/extconf.rb
88
+ - ext/cifar10_loader/extconf.rb
89
+ - ext/rb_stb_image/extconf.rb
90
90
  extra_rdoc_files: []
91
91
  files:
92
92
  - ".gitignore"
@@ -105,6 +105,10 @@ files:
105
105
  - examples/mnist_example.rb
106
106
  - examples/mnist_lstm_example.rb
107
107
  - examples/xor_example.rb
108
+ - ext/cifar10_loader/cifar10_loader.c
109
+ - ext/cifar10_loader/extconf.rb
110
+ - ext/rb_stb_image/extconf.rb
111
+ - ext/rb_stb_image/rb_stb_image.c
108
112
  - lib/dnn.rb
109
113
  - lib/dnn/core/activations.rb
110
114
  - lib/dnn/core/cnn_layers.rb
@@ -116,18 +120,14 @@ files:
116
120
  - lib/dnn/core/param.rb
117
121
  - lib/dnn/core/rnn_layers.rb
118
122
  - lib/dnn/core/util.rb
119
- - lib/dnn/ext/cifar10_loader/cifar10_loader.c
120
- - lib/dnn/ext/cifar10_loader/extconf.rb
121
- - lib/dnn/ext/rb_stb_image/extconf.rb
122
- - lib/dnn/ext/rb_stb_image/rb_stb_image.c
123
- - lib/dnn/ext/rb_stb_image/stb_image.h
124
- - lib/dnn/ext/rb_stb_image/stb_image_write.h
125
123
  - lib/dnn/lib/cifar10.rb
126
124
  - lib/dnn/lib/downloader.rb
127
125
  - lib/dnn/lib/image.rb
128
126
  - lib/dnn/lib/mnist.rb
129
127
  - lib/dnn/version.rb
130
128
  - ruby-dnn.gemspec
129
+ - third_party/stb_image.h
130
+ - third_party/stb_image_write.h
131
131
  homepage: https://github.com/unagiootoro/ruby-dnn.git
132
132
  licenses:
133
133
  - MIT