oil 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -4,7 +4,8 @@ http://github.com/ender672/oil
4
4
 
5
5
  == DESCRIPTION:
6
6
 
7
- Oil resizes JPEG and PNG images.
7
+ Oil resizes JPEG and PNG images. It aims for fast performance and low memory
8
+ use.
8
9
 
9
10
  == INSTALLATION:
10
11
 
@@ -23,6 +24,8 @@ Oil resizes JPEG and PNG images.
23
24
 
24
25
  == REQUIREMENTS:
25
26
 
27
+ These requirements do not apply to the JRuby gem.
28
+
26
29
  * IJG JPEG Library or libjpeg-turbo.
27
30
  * libpng
28
31
 
@@ -43,8 +46,8 @@ Compile & run unit tests. Should show no warnings and no failing tests:
43
46
 
44
47
  Valgrind should not complain (ruby-1.9.3p125, compiled with -O3):
45
48
 
46
- $ valgrind /path/to/ruby -Iext test/test_jpeg.rb
47
- $ valgrind --suppressions=test/valgrind_suppressions.txt /path/to/ruby -Iext test/test_png.rb
49
+ $ valgrind /path/to/ruby -Iext:test test/test_jpeg.rb
50
+ $ valgrind --suppressions=test/valgrind_suppressions.txt /path/to/ruby -Iext:test test/test_png.rb
48
51
 
49
52
  Tests should not leak memory:
50
53
 
@@ -56,7 +59,7 @@ Changes to the interpolator should be analyzed using ResampleScope:
56
59
 
57
60
  == TODO:
58
61
 
59
- * JRuby
62
+ * Windows
60
63
 
61
64
  == LICENSE:
62
65
 
data/Rakefile CHANGED
@@ -1,6 +1,14 @@
1
1
  require 'rake/clean'
2
2
  require 'rake/testtask'
3
3
 
4
+ file 'ext/oil.jar' => FileList['ext/*.java'] do
5
+ cd 'ext' do
6
+ sh "javac -g -cp #{Config::CONFIG['prefix']}/lib/jruby.jar #{FileList['*.java']}"
7
+ quoted_files = (FileList.new('*.class').to_a.map { |f| "'#{f}'" }).join(' ')
8
+ sh "jar cf oil.jar #{quoted_files}"
9
+ end
10
+ end
11
+
4
12
  file 'ext/Makefile' do
5
13
  cd 'ext' do
6
14
  ruby "extconf.rb #{ENV['EXTOPTS']}"
@@ -14,15 +22,20 @@ file 'ext/oil.so' => FileList.new('ext/Makefile', 'ext/oil.c') do
14
22
  end
15
23
 
16
24
  Rake::TestTask.new do |t|
17
- t.libs = ['ext']
18
- t.test_files = FileList['test/test*.rb']
25
+ t.libs = ['ext', 'test']
26
+ t.test_files = FileList['test/test_*.rb']
19
27
  end
20
28
 
21
- CLEAN.add('ext/*{.o,.so,.log}', 'ext/Makefile')
29
+ CLEAN.add('ext/*{.o,.so,.log,.class,.jar}', 'ext/Makefile')
22
30
  CLOBBER.add('*.gem')
23
31
 
32
+ desc 'Build the gem and include the java library'
33
+ task :gem => "ext/oil.jar" do
34
+ system "gem build oil.gemspec"
35
+ end
36
+
24
37
  desc 'Compile the extension'
25
- task :compile => "ext/oil.so"
38
+ task :compile => "ext/oil.#{RUBY_PLATFORM =~ /java/ ? 'jar' : 'so'}"
26
39
 
27
40
  task :test => :compile
28
41
  task :default => :test
data/ext/oil.c CHANGED
@@ -8,8 +8,8 @@ static ID id_read, id_seek;
8
8
 
9
9
  struct thumbdata {
10
10
  VALUE io;
11
- long width;
12
- long height;
11
+ unsigned int width;
12
+ unsigned int height;
13
13
  };
14
14
 
15
15
  static struct jpeg_error_mgr jerr;
@@ -31,16 +31,32 @@ struct png_src {
31
31
  };
32
32
 
33
33
  struct interpolation {
34
- long sw;
35
- long sh;
36
- long dw;
37
- long dh;
38
- int cmp;
39
- void (*read)(char *, void *);
40
- void (*write)(char *, void *);
41
- void *data;
34
+ long sw;
35
+ long sh;
36
+ long dw;
37
+ long dh;
38
+ int cmp;
39
+ void (*read)(char *, void *);
40
+ void (*write)(char *, void *);
41
+ void *read_data;
42
+ void *write_data;
42
43
  };
43
44
 
45
+ struct bitmap {
46
+ long rowlen;
47
+ char *cur;
48
+ };
49
+
50
+ /* bitmap source callback */
51
+
52
+ static void
53
+ bitmap_read(char *sl, void *data)
54
+ {
55
+ struct bitmap *b = (struct bitmap *)data;
56
+ memcpy(sl, b->cur, b->rowlen);
57
+ b->cur += b->rowlen;
58
+ }
59
+
44
60
  /* jpeglib error callbacks */
45
61
 
46
62
  static void output_message(j_common_ptr cinfo) {}
@@ -147,17 +163,19 @@ png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
147
163
  void
148
164
  png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
149
165
  {
150
- VALUE string;
166
+ VALUE ret, buf;
151
167
  png_size_t rlen;
152
168
  struct png_src *io_ptr;
153
169
 
154
170
  if (!png_ptr) return;
155
171
  io_ptr = (struct png_src *)png_get_io_ptr(png_ptr);
156
- string = io_ptr->buffer;
157
- rb_funcall(io_ptr->io, id_read, 2, INT2FIX(length), string);
158
- rlen = RSTRING_LEN(string);
159
- if (rlen > length) rb_raise(rb_eRuntimeError, "IO return buffer is too big.");
160
- memcpy(data, RSTRING_PTR(string), RSTRING_LEN(string));
172
+ buf = io_ptr->buffer;
173
+ ret = rb_funcall(io_ptr->io, id_read, 2, INT2FIX(length), buf);
174
+
175
+ rlen = RSTRING_LEN(buf);
176
+ if (rlen > length)
177
+ rb_raise(rb_eRuntimeError, "IO return buffer is too big.");
178
+ if (!NIL_P(ret)) memcpy(data, RSTRING_PTR(buf), rlen);
161
179
  }
162
180
 
163
181
  /* png read/write callbacks */
@@ -165,15 +183,13 @@ png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
165
183
  static void
166
184
  png_read(char *sl, void *data)
167
185
  {
168
- void **data_ary = (void **)data;
169
- png_read_row((png_structp)data_ary[2], (png_bytep)sl, (png_bytep)NULL);
186
+ png_read_row((png_structp)data, (png_bytep)sl, (png_bytep)NULL);
170
187
  }
171
188
 
172
189
  static void
173
190
  png_write(char *sl, void *data)
174
191
  {
175
- void **data_ary = (void **)data;
176
- png_write_row((png_structp)*data_ary, (png_bytep)sl);
192
+ png_write_row((png_structp)data, (png_bytep)sl);
177
193
  }
178
194
 
179
195
  /* Bilinear Interpolation */
@@ -189,12 +205,12 @@ bilinear_get_scanlines(struct interpolation *bi, char **sl1, char **sl2,
189
205
  n = line + 1 - *next;
190
206
  if (line < bi->sh - 1) n++;
191
207
  *next += n;
192
-
208
+
193
209
  for (i=0; i<n; i++) {
194
210
  tmp = *sl1;
195
211
  *sl1 = *sl2;
196
212
  *sl2 = tmp;
197
- bi->read(*sl2, bi->data);
213
+ bi->read(*sl2, bi->read_data);
198
214
 
199
215
  for (j=0; j<bi->cmp; j++)
200
216
  sl2[0][len + bi->cmp - j] = sl2[0][len - j];
@@ -212,12 +228,15 @@ bilinear3(char *sl1, char *sl2, char *sl_out, size_t sw, size_t dw, int cmp,
212
228
  size_t xsmp_i, i, j;
213
229
  double xscale_inv, xscale_ctr, ty;
214
230
 
231
+ if (ysmp < 0) ysmp = 0;
232
+
215
233
  xscale_inv = sw / (float)dw;
216
234
  xscale_ctr = (xscale_inv - 1) / 2;
217
235
  ty = ysmp - (int)ysmp;
218
236
 
219
237
  for (i = 0; i < dw; i++) {
220
238
  xsmp = i * xscale_inv + xscale_ctr;
239
+ if (xsmp < 0) xsmp = 0;
221
240
  xsmp_i = (int)xsmp;
222
241
 
223
242
  tx = xsmp - xsmp_i;
@@ -255,7 +274,7 @@ bilinear2(VALUE _args)
255
274
 
256
275
  bilinear_get_scanlines(bi, &sl1, &sl2, &line, ysmp);
257
276
  bilinear3(sl1, sl2, sl_out, bi->sw, bi->dw, bi->cmp, ysmp);
258
- bi->write(sl_out, bi->data);
277
+ bi->write(sl_out, bi->write_data);
259
278
  }
260
279
 
261
280
  return Qnil;
@@ -286,20 +305,22 @@ bilinear(struct interpolation *bi)
286
305
  if (state) rb_jump_tag(state);
287
306
  }
288
307
 
289
- /* JPEG helper functions */
308
+ /* helper functions */
290
309
 
291
310
  static void
292
- jpeg_fix_aspect_ratio(j_compress_ptr cinfo, j_decompress_ptr dinfo)
311
+ fix_ratio(unsigned int sw, unsigned int sh, unsigned int *dw, unsigned int *dh)
293
312
  {
294
313
  double x, y;
295
-
296
- x = cinfo->image_width / (float)dinfo->output_width;
297
- y = cinfo->image_height / (float)dinfo->output_height;
298
-
299
- if (x < y) cinfo->image_height = dinfo->output_height * x;
300
- else cinfo->image_width = dinfo->output_width * y;
314
+ x = *dw / (float)sw;
315
+ y = *dh / (float)sh;
316
+ if (x<y) *dh = sh * x;
317
+ else *dw = sw * y;
318
+ if (!*dh) *dh = 1;
319
+ if (!*dw) *dw = 1;
301
320
  }
302
321
 
322
+ /* jpeg helper functions */
323
+
303
324
  static void
304
325
  jpeg_set_output_header(j_compress_ptr cinfo, j_decompress_ptr dinfo)
305
326
  {
@@ -322,15 +343,13 @@ jpeg_pre_scale(j_compress_ptr cinfo, j_decompress_ptr dinfo)
322
343
  static void
323
344
  jpeg_read(char *sl, void *data)
324
345
  {
325
- void **data_ary = (void **)data;
326
- jpeg_read_scanlines((j_decompress_ptr)*data_ary, (JSAMPROW *)&sl, 1);
346
+ jpeg_read_scanlines((j_decompress_ptr)data, (JSAMPROW *)&sl, 1);
327
347
  }
328
348
 
329
349
  static void
330
350
  jpeg_write(char *sl, void *data)
331
351
  {
332
- void **data_ary = (void **)data;
333
- jpeg_write_scanlines((j_compress_ptr)data_ary[1], (JSAMPROW *)&sl, 1);
352
+ jpeg_write_scanlines((j_compress_ptr)data, (JSAMPROW *)&sl, 1);
334
353
  }
335
354
 
336
355
  static VALUE
@@ -340,12 +359,12 @@ jpeg_each3(VALUE _args)
340
359
  j_decompress_ptr dinfo = (j_decompress_ptr)args[0];
341
360
  j_compress_ptr cinfo = (j_compress_ptr)args[1];
342
361
  struct interpolation intrp;
343
- void *data[2];
344
362
 
345
363
  jpeg_read_header(dinfo, TRUE);
346
364
  jpeg_calc_output_dimensions(dinfo);
347
365
  jpeg_set_output_header(cinfo, dinfo);
348
- jpeg_fix_aspect_ratio(cinfo, dinfo);
366
+ fix_ratio(dinfo->output_width, dinfo->output_height, &cinfo->image_width,
367
+ &cinfo->image_height);
349
368
  jpeg_pre_scale(cinfo, dinfo);
350
369
 
351
370
  jpeg_start_compress(cinfo, TRUE);
@@ -358,12 +377,10 @@ jpeg_each3(VALUE _args)
358
377
  intrp.cmp = dinfo->output_components;
359
378
  intrp.read = jpeg_read;
360
379
  intrp.write = jpeg_write;
361
- data[0] = dinfo;
362
- data[1] = cinfo;
363
- intrp.data = (void *)data;
380
+ intrp.read_data = dinfo;
381
+ intrp.write_data = cinfo;
364
382
 
365
383
  bilinear(&intrp);
366
- //nearest(&intrp);
367
384
 
368
385
  jpeg_abort_decompress(dinfo);
369
386
  jpeg_finish_compress(cinfo);
@@ -485,6 +502,71 @@ jpeg_each(VALUE self)
485
502
  return self;
486
503
  }
487
504
 
505
+ static void
506
+ png_normalize_input(png_structp read_ptr, png_infop read_i_ptr)
507
+ {
508
+ png_byte ctype;
509
+ int bit_depth;
510
+
511
+ bit_depth = png_get_bit_depth(read_ptr, read_i_ptr);
512
+ ctype = png_get_color_type(read_ptr, read_i_ptr);
513
+
514
+ if (ctype == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
515
+ png_set_gray_1_2_4_to_8(read_ptr);
516
+
517
+ if (bit_depth < 8)
518
+ png_set_packing(read_ptr);
519
+ else if (bit_depth == 16)
520
+ #if PNG_LIBPNG_VER >= 10504
521
+ png_set_scale_16(read_ptr);
522
+ #else
523
+ png_set_strip_16(read_ptr);
524
+ #endif
525
+
526
+ if (png_get_valid(read_ptr, read_i_ptr, PNG_INFO_tRNS))
527
+ png_set_tRNS_to_alpha(read_ptr);
528
+
529
+ if (ctype == PNG_COLOR_TYPE_PALETTE)
530
+ png_set_palette_to_rgb(read_ptr);
531
+
532
+ png_read_update_info(read_ptr, read_i_ptr);
533
+ }
534
+
535
+ static VALUE
536
+ png_interlaced2(VALUE arg)
537
+ {
538
+ bilinear((struct interpolation *)arg);
539
+ }
540
+
541
+ static void
542
+ png_interlaced(png_structp rpng, struct interpolation *intrp)
543
+ {
544
+ struct bitmap b;
545
+ png_bytep *rows;
546
+ char *data;
547
+ int i, state;
548
+
549
+ b.rowlen = intrp->sw * intrp->cmp;
550
+ data = malloc(b.rowlen * intrp->sh);
551
+ b.cur = data;
552
+
553
+ rows = malloc(intrp->sh * sizeof(png_bytep));
554
+ for (i=0; i<intrp->sh; i++)
555
+ rows[i] = data + (i * b.rowlen);
556
+
557
+ png_read_image(rpng, rows);
558
+
559
+ intrp->read = bitmap_read;
560
+ intrp->read_data = (void *)&b;
561
+
562
+ rb_protect(png_interlaced2, (VALUE)intrp, &state);
563
+
564
+ free(rows);
565
+ free(data);
566
+
567
+ if (state) rb_jump_tag(state);
568
+ }
569
+
488
570
  static VALUE
489
571
  png_each2(VALUE _args)
490
572
  {
@@ -494,33 +576,48 @@ png_each2(VALUE _args)
494
576
  png_structp read_ptr=(png_structp)args[2];
495
577
  png_infop read_i_ptr=(png_infop)args[3];
496
578
  struct thumbdata *thumb=(struct thumbdata *)args[4];
497
- void *data[4];
498
579
  struct interpolation intrp;
580
+ png_byte ctype;
499
581
 
500
582
  png_read_info(read_ptr, read_i_ptr);
583
+ png_normalize_input(read_ptr, read_i_ptr);
584
+ ctype = png_get_color_type(read_ptr, read_i_ptr);
585
+
586
+ intrp.sw = png_get_image_width(read_ptr, read_i_ptr);
587
+ intrp.sh = png_get_image_height(read_ptr, read_i_ptr);
588
+ fix_ratio(intrp.sw, intrp.sh, &thumb->width, &thumb->height);
501
589
  png_set_IHDR(write_ptr, write_i_ptr, thumb->width, thumb->height, 8,
502
- PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
503
- PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
590
+ ctype, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
591
+ PNG_FILTER_TYPE_DEFAULT);
504
592
  png_write_info(write_ptr, write_i_ptr);
505
593
 
506
- intrp.sw = png_get_image_width(read_ptr, read_i_ptr);
507
- intrp.sh = png_get_image_height(read_ptr, read_i_ptr);
594
+ switch (ctype) {
595
+ case PNG_COLOR_TYPE_GRAY: intrp.cmp = 1; break;
596
+ case PNG_COLOR_TYPE_GRAY_ALPHA: intrp.cmp = 2; break;
597
+ case PNG_COLOR_TYPE_RGB: intrp.cmp = 3; break;
598
+ case PNG_COLOR_TYPE_RGB_ALPHA: intrp.cmp = 4; break;
599
+ default: rb_raise(rb_eRuntimeError, "png color type not supported.");
600
+ }
601
+
508
602
  intrp.dw = thumb->width;
509
603
  intrp.dh = thumb->height;
510
- intrp.cmp = 3;
511
- intrp.read = png_read;
512
604
  intrp.write = png_write;
513
- data[0] = write_ptr;
514
- data[1] = write_i_ptr;
515
- data[2] = read_ptr;
516
- data[3] = read_i_ptr;
517
- intrp.data = (void *)data;
518
-
519
- bilinear(&intrp);
605
+ intrp.write_data = write_ptr;
520
606
 
521
- png_read_end(read_ptr, (png_infop)NULL);
607
+ switch (png_get_interlace_type(read_ptr, read_i_ptr)) {
608
+ case PNG_INTERLACE_NONE:
609
+ intrp.read = png_read;
610
+ intrp.read_data = read_ptr;
611
+ bilinear(&intrp);
612
+ break;
613
+ case PNG_INTERLACE_ADAM7:
614
+ png_interlaced(read_ptr, &intrp);
615
+ break;
616
+ default: rb_raise(rb_eRuntimeError, "png interlace type not supported.");
617
+ }
618
+
522
619
  png_write_end(write_ptr, write_i_ptr);
523
-
620
+
524
621
  return Qnil;
525
622
  }
526
623
 
@@ -543,17 +640,17 @@ png_each(VALUE self)
543
640
  Data_Get_Struct(self, struct thumbdata, thumb);
544
641
 
545
642
  write_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
546
- (png_error_ptr)png_error_fn,
547
- (png_error_ptr)png_warning_fn);
643
+ (png_error_ptr)png_error_fn,
644
+ (png_error_ptr)png_warning_fn);
548
645
  write_i_ptr = png_create_info_struct(write_ptr);
549
646
  png_set_write_fn(write_ptr, NULL, png_write_data, png_flush_data);
550
647
 
551
648
  src.io = thumb->io;
552
- src.buffer = rb_str_new(0, 0);
649
+ src.buffer = rb_str_new(0, 0);
553
650
 
554
651
  read_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
555
- (png_error_ptr)png_error_fn,
556
- (png_error_ptr)png_warning_fn);
652
+ (png_error_ptr)png_error_fn,
653
+ (png_error_ptr)png_warning_fn);
557
654
  read_i_ptr = png_create_info_struct(read_ptr);
558
655
  png_set_read_fn(read_ptr, (void *)&src, png_read_data);
559
656
 
@@ -572,7 +669,7 @@ png_each(VALUE self)
572
669
  }
573
670
 
574
671
  void
575
- Init_oil()
672
+ Init_oil(void)
576
673
  {
577
674
  VALUE mOil = rb_define_module("Oil");
578
675
 
data/ext/oil.jar ADDED
Binary file
data/test/helper.rb ADDED
@@ -0,0 +1,12 @@
1
+ class CustomError < RuntimeError; end
2
+
3
+ class CustomIO
4
+ def initialize(proc, *args)
5
+ @parent = StringIO.new(*args)
6
+ @proc = proc
7
+ end
8
+
9
+ def read(size, buf=nil)
10
+ @proc.call(@parent, size, buf) if @proc
11
+ end
12
+ end
data/test/test_jpeg.rb CHANGED
@@ -2,6 +2,7 @@ require 'rubygems'
2
2
  require 'minitest/autorun'
3
3
  require 'oil'
4
4
  require 'stringio'
5
+ require 'helper'
5
6
 
6
7
  module Oil
7
8
  class TestJPEG < MiniTest::Unit::TestCase
@@ -63,8 +64,14 @@ module Oil
63
64
 
64
65
  def test_io_returns_too_much_data
65
66
  proc = Proc.new do |io, size, buf|
66
- buf.slice!(0,0)
67
- buf << (io.read(size)[0..-2] * 2)
67
+ str = io.read(size)
68
+ str = str[0..-2] * 2 if str
69
+ if buf
70
+ buf.slice!(0,0) # this empties the buffer
71
+ buf << str
72
+ else
73
+ str
74
+ end
68
75
  end
69
76
  assert_raises(RuntimeError) { custom_io proc, JPEG_DATA }
70
77
  end
@@ -88,7 +95,11 @@ module Oil
88
95
  proc = Proc.new do |parent, size, buf|
89
96
  raise CustomError if flag
90
97
  flag = true
91
- parent.read(size, buf)
98
+ if buf
99
+ parent.read(size, buf)
100
+ else
101
+ parent.read(size)
102
+ end
92
103
  end
93
104
  assert_raises(CustomError) { custom_io proc, big_jpeg }
94
105
  end
@@ -98,25 +109,44 @@ module Oil
98
109
  proc = Proc.new do |parent, size, buf|
99
110
  throw :foo if flag
100
111
  flag = true
101
- parent.read(size, buf)
112
+ if buf
113
+ parent.read(size, buf)
114
+ else
115
+ parent.read(size)
116
+ end
102
117
  end
103
118
  catch(:foo){ custom_io proc, big_jpeg }
104
119
  end
105
120
 
106
121
  def test_io_shrinks_buffer
107
122
  proc = Proc.new do |parent, size, buf|
108
- parent.read(size, buf)
109
- buf.slice!(0, 10)
123
+ if buf
124
+ parent.read(size, buf)
125
+ buf.slice!(0, 10)
126
+ else
127
+ res = parent.read(size)
128
+ res[10, -1]
129
+ end
110
130
  end
111
131
  assert_raises(RuntimeError) { custom_io(proc, big_jpeg) }
112
132
  end
113
133
 
114
- def test_io_enlarges_buffer
134
+ def test_io_enlarges_buffer_with_eof
115
135
  proc = Proc.new do |parent, size, buf|
116
- res = parent.read(size, buf)
117
- buf << res
136
+ if buf
137
+ parent.read(size, buf)
138
+ buf << buf
139
+ else
140
+ str = parent.read(size)
141
+ str * 2
142
+ end
143
+ end
144
+
145
+ if(RUBY_PLATFORM =~ /java/)
146
+ assert_raises(RuntimeError) { custom_io(proc, big_jpeg) }
147
+ else
148
+ validate_jpeg custom_io(proc, big_jpeg) # libjpeg stops when reaches eof
118
149
  end
119
- validate_jpeg custom_io(proc, big_jpeg) # how is this okay?
120
150
  end
121
151
 
122
152
  def test_io_seek
@@ -187,18 +217,4 @@ module Oil
187
217
  io
188
218
  end
189
219
  end
190
-
191
- class CustomError < RuntimeError; end
192
-
193
- class CustomIO
194
- def initialize(proc, *args)
195
- @parent = StringIO.new(*args)
196
- @proc = proc
197
- end
198
-
199
- def read(size, buf)
200
- @proc.call(@parent, size, buf) if @proc
201
- end
202
- end
203
220
  end
204
-
data/test/test_png.rb CHANGED
@@ -2,6 +2,7 @@ require 'rubygems'
2
2
  require 'minitest/autorun'
3
3
  require 'oil'
4
4
  require 'stringio'
5
+ require 'helper'
5
6
 
6
7
  module Oil
7
8
  class TestPNG < MiniTest::Unit::TestCase
@@ -31,7 +32,7 @@ module Oil
31
32
  def test_bogus_end_chunk
32
33
  str = PNG_DATA.dup
33
34
  str[-6] = "\x10"
34
- assert_raises(RuntimeError) { resize_string(str) }
35
+ validate_png resize_string(str)
35
36
  end
36
37
 
37
38
  def test_calls_each_during_yield
@@ -61,8 +62,14 @@ module Oil
61
62
 
62
63
  def test_io_returns_too_much_data
63
64
  proc = Proc.new do |io, size, buf|
64
- buf.slice!(0,0)
65
- buf << (io.read(size)[0..-2] * 2)
65
+ str = io.read(size)
66
+ str = str[0..-2] * 2 if str
67
+ if buf
68
+ buf.slice!(0,0) # this empties the buffer
69
+ buf << str
70
+ else
71
+ str
72
+ end
66
73
  end
67
74
  assert_raises(RuntimeError) { custom_io proc, PNG_DATA }
68
75
  end
@@ -86,7 +93,11 @@ module Oil
86
93
  proc = Proc.new do |parent, size, buf|
87
94
  raise CustomError if flag
88
95
  flag = true
89
- parent.read(size, buf)
96
+ if buf
97
+ parent.read(size, buf)
98
+ else
99
+ parent.read(size)
100
+ end
90
101
  end
91
102
  assert_raises(CustomError) { custom_io proc, big_png }
92
103
  end
@@ -96,25 +107,40 @@ module Oil
96
107
  proc = Proc.new do |parent, size, buf|
97
108
  throw :foo if flag
98
109
  flag = true
99
- parent.read(size, buf)
110
+ if buf
111
+ parent.read(size, buf)
112
+ else
113
+ parent.read(size)
114
+ end
100
115
  end
101
116
  catch(:foo){ custom_io proc, big_png }
102
117
  end
103
118
 
104
119
  def test_io_shrinks_buffer
105
120
  proc = Proc.new do |parent, size, buf|
106
- parent.read(size, buf)
107
- buf.slice!(0, 10)
121
+ if buf
122
+ parent.read(size, buf)
123
+ buf.slice!(0, 10)
124
+ else
125
+ res = parent.read(size)
126
+ res[10, -1]
127
+ end
108
128
  end
109
129
  assert_raises(RuntimeError) { custom_io(proc, big_png) }
110
130
  end
111
131
 
112
- def test_io_enlarges_buffer
132
+ def test_io_enlarges_buffer_with_eof
113
133
  proc = Proc.new do |parent, size, buf|
114
- res = parent.read(size, buf)
115
- buf << res
134
+ if buf
135
+ parent.read(size, buf)
136
+ buf << buf
137
+ else
138
+ str = parent.read(size)
139
+ str * 2
140
+ end
116
141
  end
117
- assert_raises(RuntimeError) { validate_jpeg custom_io(proc, big_png) }
142
+
143
+ assert_raises(RuntimeError) { custom_io(proc, big_png) }
118
144
  end
119
145
 
120
146
  # Test yielding
@@ -180,18 +206,4 @@ module Oil
180
206
  io
181
207
  end
182
208
  end
183
-
184
- class CustomError < RuntimeError; end
185
-
186
- class CustomIO
187
- def initialize(proc, *args)
188
- @parent = StringIO.new(*args)
189
- @proc = proc
190
- end
191
-
192
- def read(size, buf)
193
- @proc.call(@parent, size, buf) if @proc
194
- end
195
- end
196
209
  end
197
-
metadata CHANGED
@@ -1,54 +1,63 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: oil
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.2
3
+ version: !ruby/object:Gem::Version
5
4
  prerelease:
5
+ version: 0.0.3
6
6
  platform: ruby
7
- authors:
8
- - Timothy Elliott
7
+ authors:
8
+ - Timothy Elliott
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-08 00:00:00.000000000 Z
12
+
13
+ date: 2012-03-28 00:00:00 Z
13
14
  dependencies: []
14
- description:
15
+
16
+ description: Oil resizes JPEG and PNG images. It aims for fast performance and low memory use.
15
17
  email: tle@holymonkey.com
16
18
  executables: []
17
- extensions:
18
- - ext/extconf.rb
19
- extra_rdoc_files:
20
- - README.rdoc
21
- files:
22
- - Rakefile
23
- - README.rdoc
24
- - ext/oil.c
25
- - ext/extconf.rb
26
- - test/test_jpeg.rb
27
- - test/test_png.rb
19
+
20
+ extensions:
21
+ - ext/extconf.rb
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - Rakefile
26
+ - README.rdoc
27
+ - ext/oil.c
28
+ - ext/extconf.rb
29
+ - ext/oil.jar
30
+ - test/helper.rb
31
+ - test/test_jpeg.rb
32
+ - test/test_png.rb
28
33
  homepage: http://github.com/ender672/oil
29
34
  licenses: []
35
+
30
36
  post_install_message:
31
37
  rdoc_options: []
32
- require_paths:
33
- - ext
34
- required_ruby_version: !ruby/object:Gem::Requirement
38
+
39
+ require_paths:
40
+ - ext
41
+ required_ruby_version: !ruby/object:Gem::Requirement
35
42
  none: false
36
- requirements:
37
- - - ! '>='
38
- - !ruby/object:Gem::Version
39
- version: '0'
40
- required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
48
  none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
46
53
  requirements: []
54
+
47
55
  rubyforge_project:
48
- rubygems_version: 1.8.17
56
+ rubygems_version: 1.8.21
49
57
  signing_key:
50
58
  specification_version: 3
51
59
  summary: Oil resizes JPEG and PNG images.
52
- test_files:
53
- - test/test_jpeg.rb
54
- - test/test_png.rb
60
+ test_files:
61
+ - test/helper.rb
62
+ - test/test_jpeg.rb
63
+ - test/test_png.rb