scambra-image_science 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,70 @@
1
+ == 1.2.0 / 2009-06-23
2
+
3
+ * 7 minor enhancements:
4
+
5
+ * Moved quick_thumb to bin/image_science_thumb and properly added.
6
+ * Added -s (square) flag to bin/image_science_thumb
7
+ * Added autorotating on image load. (choonkeat)
8
+ * Added ruby_inline to clean globs
9
+ * Added with_image_from_memory. (sumbach)
10
+ * Switched to minitest.
11
+ * Updated rakefile for now hoe capabilities.
12
+
13
+ * 3 bug fixes:
14
+
15
+ * Check and convert to 24 BPP if save type is jpg. Caused by 32bpp png to jpg.
16
+ * Fixed 1.9isms
17
+ * Fixed BMP support. Tweaked whitespace.
18
+
19
+ == 1.1.3 / 2007-05-30
20
+
21
+ * 2 minor enhancements:
22
+
23
+ * Added quick_thumb as an example to look at.
24
+ * Error handler doesn't raise by default. Raises if $DEBUG==true.
25
+
26
+ == 1.1.2 / 2007-04-18
27
+
28
+ * 2 bug fixes:
29
+
30
+ * reports bad height/width values for resize
31
+ * explicitly removes ICC color profiles from PNGs (bug in freeimage).
32
+
33
+ == 1.1.1 / 2007-03-08
34
+
35
+ * 5 minor enhancements:
36
+
37
+ * Added error handler that raises with information about what went wrong.
38
+ * thumbnail is now pure ruby, everything now uses resize.
39
+ * Produces cleaner JPEG files, with a small cost to file size/speed.
40
+ * resize now uses Catmull-Rom spline filter for better quality.
41
+ * resize copies existing ICC Profile to thumbnail, producing better color.
42
+ * ICC Profile NOT copied for PNG as it seems to be buggy.
43
+
44
+ * 1 bug fix:
45
+
46
+ * Fixed rdoc
47
+
48
+ == 1.1.0 / 2007-01-05
49
+
50
+ * 3 major enhancements:
51
+
52
+ * Added resize(width, height)
53
+ * Added save(path)
54
+ * All thumbnail and resize methods yield instead of saving directly.
55
+
56
+ * 1 minor enhancement:
57
+
58
+ * Will now try to use FreeImage from ports if /opt/local exists.
59
+
60
+ * 2 bug fixes:
61
+
62
+ * Fixed the linker issue on PPC.
63
+ * Rakefile will now clean the image files created by bench.rb
64
+
65
+ == 1.0.0 / 2006-12-01
66
+
67
+ * 1 major enhancement
68
+
69
+ * Birthday!
70
+
@@ -0,0 +1,9 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ bench.rb
6
+ bin/image_science_thumb
7
+ lib/image_science.rb
8
+ test/pix.png
9
+ test/test_image_science.rb
@@ -0,0 +1,66 @@
1
+ = ImageScience
2
+
3
+ * http://seattlerb.rubyforge.org/ImageScience.html
4
+ * http://rubyforge.org/projects/seattlerb
5
+
6
+ == DESCRIPTION:
7
+
8
+ ImageScience is a clean and happy Ruby library that generates
9
+ thumbnails -- and kicks the living crap out of RMagick. Oh, and it
10
+ doesn't leak memory like a sieve. :)
11
+
12
+ For more information including build steps, see http://seattlerb.rubyforge.org/
13
+
14
+ == FEATURES/PROBLEMS:
15
+
16
+ * Glorious graphics manipulation magi... errr, SCIENCE! in less than 200 LoC!
17
+ * Supports square and proportional thumbnails, as well as arbitrary resizes.
18
+ * Pretty much any graphics format you could want. No really.
19
+
20
+ == SYNOPSYS:
21
+
22
+ ImageScience.with_image(file) do |img|
23
+ img.cropped_thumbnail(100) do |thumb|
24
+ thumb.save "#{file}_cropped.png"
25
+ end
26
+
27
+ img.thumbnail(100) do |thumb|
28
+ thumb.save "#{file}_thumb.png"
29
+ end
30
+ end
31
+
32
+ == REQUIREMENTS:
33
+
34
+ * FreeImage
35
+ * ImageScience
36
+
37
+ == INSTALL:
38
+
39
+ * Download and install FreeImage. See notes at url above.
40
+ * sudo gem install -y image_science
41
+ * see http://seattlerb.rubyforge.org/ImageScience.html for more info.
42
+
43
+ == LICENSE:
44
+
45
+ (The MIT License)
46
+
47
+ Copyright (c) 2006-2009 Ryan Davis, Seattle.rb
48
+
49
+ Permission is hereby granted, free of charge, to any person obtaining
50
+ a copy of this software and associated documentation files (the
51
+ 'Software'), to deal in the Software without restriction, including
52
+ without limitation the rights to use, copy, modify, merge, publish,
53
+ distribute, sublicense, and/or sell copies of the Software, and to
54
+ permit persons to whom the Software is furnished to do so, subject to
55
+ the following conditions:
56
+
57
+ The above copyright notice and this permission notice shall be
58
+ included in all copies or substantial portions of the Software.
59
+
60
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
61
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
62
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
63
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
64
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
65
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
66
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ Hoe.plugin :seattlerb
7
+ Hoe.plugin :inline
8
+
9
+ Hoe.spec 'image_science' do
10
+ developer 'Ryan Davis', 'ryand-ruby@zenspider.com'
11
+
12
+ self.rubyforge_name = 'seattlerb'
13
+
14
+ clean_globs << 'blah*png' << 'images/*_thumb.*'
15
+ end
16
+
17
+ # vim: syntax=Ruby
@@ -0,0 +1,63 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ require 'benchmark'
4
+ require 'rubygems'
5
+ require 'image_science'
6
+
7
+ max = (ARGV.shift || 100).to_i
8
+ ext = ARGV.shift || "png"
9
+
10
+ file = "blah_big.#{ext}"
11
+
12
+ if RUBY_PLATFORM =~ /darwin/ then
13
+ # how fucking cool is this???
14
+ puts "taking screenshot for thumbnailing benchmarks"
15
+ system "screencapture -SC #{file}"
16
+ else
17
+ abort "You need to plonk down #{file} or buy a mac"
18
+ end unless test ?f, "#{file}"
19
+
20
+ ImageScience.with_image(file.sub(/#{ext}$/, 'png')) do |img|
21
+ img.save(file)
22
+ end if ext != "png"
23
+
24
+ puts "# of iterations = #{max}"
25
+ Benchmark::bm(20) do |x|
26
+ x.report("null_time") {
27
+ for i in 0..max do
28
+ # do nothing
29
+ end
30
+ }
31
+
32
+ x.report("cropped") {
33
+ for i in 0..max do
34
+ ImageScience.with_image(file) do |img|
35
+ img.cropped_thumbnail(100) do |thumb|
36
+ thumb.save("blah_cropped.#{ext}")
37
+ end
38
+ end
39
+ end
40
+ }
41
+
42
+ x.report("proportional") {
43
+ for i in 0..max do
44
+ ImageScience.with_image(file) do |img|
45
+ img.thumbnail(100) do |thumb|
46
+ thumb.save("blah_thumb.#{ext}")
47
+ end
48
+ end
49
+ end
50
+ }
51
+
52
+ x.report("resize") {
53
+ for i in 0..max do
54
+ ImageScience.with_image(file) do |img|
55
+ img.resize(200, 200) do |resize|
56
+ resize.save("blah_resize.#{ext}")
57
+ end
58
+ end
59
+ end
60
+ }
61
+ end
62
+
63
+ # File.unlink(*Dir["blah*#{ext}"])
@@ -0,0 +1,31 @@
1
+ #!/usr/local/bin/ruby -ws
2
+
3
+ $s ||= false
4
+
5
+ abort "#{File.basename $0} max_length files..." unless ARGV.size > 1
6
+
7
+ require 'rubygems'
8
+ require 'image_science'
9
+
10
+ max_length = ARGV.shift.to_i
11
+
12
+ msg = $s ? :cropped_thumbnail : :thumbnail
13
+
14
+ ARGV.each do |file|
15
+ begin
16
+ result = ImageScience.with_image file do |img|
17
+ begin
18
+ img.send(msg, max_length) do |thumb|
19
+ # add _thumb and switch from gif to png. Really. gif just sucks.
20
+ out = file.sub(/(\.[^\.]+)$/, '_thumb\1').sub(/gif$/, 'png')
21
+ thumb.save(out)
22
+ end
23
+ rescue => e
24
+ warn "Exception thumbnailing #{file}: #{e}"
25
+ end
26
+ end
27
+ p file => result
28
+ rescue => e
29
+ warn "Exception opening #{file}: #{e}"
30
+ end
31
+ end
@@ -0,0 +1,378 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ require 'rubygems'
4
+ require 'inline'
5
+
6
+ ##
7
+ # Provides a clean and simple API to generate thumbnails using
8
+ # FreeImage as the underlying mechanism.
9
+ #
10
+ # For more information or if you have build issues with FreeImage, see
11
+ # http://seattlerb.rubyforge.org/ImageScience.html
12
+
13
+ class ImageScience
14
+ VERSION = '1.2.0'
15
+
16
+ ##
17
+ # The top-level image loader opens +path+ and then yields the image.
18
+
19
+ def self.with_image(path) # :yields: image
20
+ end
21
+
22
+ ##
23
+ # The top-level image loader, opens an image from the string +data+ and then yields the image.
24
+
25
+ def self.with_image_from_memory(data) # :yields: image
26
+ end
27
+
28
+ ##
29
+ # Returns the type of the image.
30
+
31
+ def self.image_type(path)
32
+ case file_type(path)
33
+ when 0 then 'BMP'
34
+ when 1 then 'ICO'
35
+ when 2 then 'JPEG'
36
+ when 3 then 'JNG'
37
+ when 4 then 'KOALA'
38
+ when 5 then 'IFF'
39
+ when 6 then 'MNG'
40
+ when 7 then 'PBM'
41
+ when 8 then 'PBMRAW'
42
+ when 9 then 'PCD'
43
+ when 10 then 'PCX'
44
+ when 11 then 'PGM'
45
+ when 12 then 'PGMRAW'
46
+ when 13 then 'PNG'
47
+ when 14 then 'PPM'
48
+ when 15 then 'PPMRAW'
49
+ when 16 then 'RAS'
50
+ when 17 then 'TARGA'
51
+ when 18 then 'TIFF'
52
+ when 19 then 'WBMP'
53
+ when 20 then 'PSD'
54
+ when 21 then 'CUT'
55
+ when 22 then 'XBM'
56
+ when 23 then 'XPM'
57
+ when 24 then 'DDS'
58
+ when 25 then 'GIF'
59
+ when 26 then 'HDR'
60
+ when 27 then 'FAXG3'
61
+ when 28 then 'SGI'
62
+ when 29 then 'EXR'
63
+ when 30 then 'J2K'
64
+ when 31 then 'JP2'
65
+ end
66
+ end
67
+
68
+ ##
69
+ # Crops an image to +left+, +top+, +right+, and +bottom+ and then
70
+ # yields the new image.
71
+
72
+ def with_crop(left, top, right, bottom) # :yields: image
73
+ end
74
+
75
+ ##
76
+ # Returns the width of the image, in pixels.
77
+
78
+ def width; end
79
+
80
+ ##
81
+ # Returns the height of the image, in pixels.
82
+
83
+ def height; end
84
+
85
+ ##
86
+ # Returns the size of one pixel of the image. Possible bit depths
87
+ # are 1, 4, 8, 16, 24, 32 for standard bitmaps and 16, 32, 48, 64,
88
+ # 96 and 128 bits for non standard bitmaps.
89
+
90
+ def depth; end
91
+
92
+ ##
93
+ # Returns the size of one pixel of the image. Possible bit depths
94
+ # are 1, 4, 8, 16, 24, 32 for standard bitmaps and 16, 32, 48, 64,
95
+ # 96 and 128 bits for non standard bitmaps.
96
+
97
+ def colorspace
98
+ case colortype
99
+ when 0 then depth == 1 ? 'InvertedMonochrome' : 'InvertedGrayscale'
100
+ when 1 then depth == 1 ? 'Monochrome' : 'Grayscale'
101
+ when 2 then 'RGB'
102
+ when 3 then 'Indexed'
103
+ when 4 then 'RGBA'
104
+ when 5 then 'CMYK'
105
+ end
106
+ end
107
+
108
+ ##
109
+ # Saves the image out to +path+. Changing the file extension will
110
+ # convert the file type to the appropriate format.
111
+
112
+ def save(path); end
113
+
114
+ ##
115
+ # Resizes the image to +width+ and +height+ using a cubic-bspline
116
+ # filter and yields the new image.
117
+
118
+ def resize(width, height) # :yields: image
119
+ end
120
+
121
+ ##
122
+ # Creates a proportional thumbnail of the image scaled so its longest
123
+ # edge is resized to +size+ and yields the new image.
124
+
125
+ def thumbnail(size) # :yields: image
126
+ w, h = width, height
127
+ scale = size.to_f / (w > h ? w : h)
128
+
129
+ self.resize((w * scale).to_i, (h * scale).to_i) do |image|
130
+ yield image
131
+ end
132
+ end
133
+
134
+ ##
135
+ # Creates a square thumbnail of the image cropping the longest edge
136
+ # to match the shortest edge, resizes to +size+, and yields the new
137
+ # image.
138
+
139
+ def cropped_thumbnail(size) # :yields: image
140
+ w, h = width, height
141
+ l, t, r, b, half = 0, 0, w, h, (w - h).abs / 2
142
+
143
+ l, r = half, half + h if w > h
144
+ t, b = half, half + w if h > w
145
+
146
+ with_crop(l, t, r, b) do |img|
147
+ img.thumbnail(size) do |thumb|
148
+ yield thumb
149
+ end
150
+ end
151
+ end
152
+
153
+ inline do |builder|
154
+ if test ?d, "/opt/local" then
155
+ builder.add_compile_flags "-I/opt/local/include"
156
+ builder.add_link_flags "-L/opt/local/lib"
157
+ end
158
+
159
+ builder.add_link_flags "-lfreeimage"
160
+ builder.add_link_flags "-lstdc++" # only needed on PPC for some reason. lame
161
+ builder.include '"FreeImage.h"'
162
+
163
+ builder.prefix <<-"END"
164
+ #define GET_BITMAP(name) FIBITMAP *(name); Data_Get_Struct(self, FIBITMAP, (name)); if (!(name)) rb_raise(rb_eTypeError, "Bitmap has already been freed")
165
+ END
166
+
167
+ builder.prefix <<-"END"
168
+ VALUE unload(VALUE self) {
169
+ GET_BITMAP(bitmap);
170
+
171
+ FreeImage_Unload(bitmap);
172
+ DATA_PTR(self) = NULL;
173
+ return Qnil;
174
+ }
175
+ END
176
+
177
+ builder.prefix <<-"END"
178
+ VALUE wrap_and_yield(FIBITMAP *image, VALUE self, FREE_IMAGE_FORMAT fif) {
179
+ unsigned int self_is_class = rb_type(self) == T_CLASS;
180
+ VALUE klass = self_is_class ? self : CLASS_OF(self);
181
+ VALUE type = self_is_class ? INT2FIX(fif) : rb_iv_get(self, "@file_type");
182
+ VALUE obj = Data_Wrap_Struct(klass, NULL, NULL, image);
183
+ rb_iv_set(obj, "@file_type", type);
184
+ return rb_ensure(rb_yield, obj, unload, obj);
185
+ }
186
+ END
187
+
188
+ builder.prefix <<-"END"
189
+ void copy_icc_profile(VALUE self, FIBITMAP *from, FIBITMAP *to) {
190
+ FREE_IMAGE_FORMAT fif = FIX2INT(rb_iv_get(self, "@file_type"));
191
+ if (fif != FIF_PNG && FreeImage_FIFSupportsICCProfiles(fif)) {
192
+ FIICCPROFILE *profile = FreeImage_GetICCProfile(from);
193
+ if (profile && profile->data) {
194
+ FreeImage_CreateICCProfile(to, profile->data, profile->size);
195
+ }
196
+ }
197
+ }
198
+ END
199
+
200
+ builder.prefix <<-"END"
201
+ void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message) {
202
+ rb_raise(rb_eRuntimeError,
203
+ "FreeImage exception for type %s: %s",
204
+ (fif == FIF_UNKNOWN) ? "???" : FreeImage_GetFormatFromFIF(fif),
205
+ message);
206
+ }
207
+ END
208
+
209
+ builder.add_to_init "FreeImage_SetOutputMessage(FreeImageErrorHandler);"
210
+
211
+ builder.c_singleton <<-"END"
212
+ VALUE file_type(char * input) {
213
+ FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
214
+
215
+ fif = FreeImage_GetFileType(input, 0);
216
+ if (fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(input);
217
+ if (fif != FIF_UNKNOWN) return fif;
218
+ else return Qnil;
219
+ }
220
+ END
221
+
222
+ builder.c_singleton <<-"END"
223
+ VALUE with_image(char * input) {
224
+ FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
225
+
226
+ fif = FreeImage_GetFileType(input, 0);
227
+ if (fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(input);
228
+ if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) {
229
+ FIBITMAP *bitmap;
230
+ VALUE result = Qnil;
231
+ int flags = fif == FIF_JPEG ? JPEG_ACCURATE : 0;
232
+ if (bitmap = FreeImage_Load(fif, input, flags)) {
233
+ FITAG *tagValue = NULL;
234
+ FreeImage_GetMetadata(FIMD_EXIF_MAIN, bitmap, "Orientation", &tagValue);
235
+ switch (tagValue == NULL ? 0 : *((short *) FreeImage_GetTagValue(tagValue))) {
236
+ case 6:
237
+ bitmap = FreeImage_RotateClassic(bitmap, 270);
238
+ break;
239
+ case 3:
240
+ bitmap = FreeImage_RotateClassic(bitmap, 180);
241
+ break;
242
+ case 8:
243
+ bitmap = FreeImage_RotateClassic(bitmap, 90);
244
+ break;
245
+ default:
246
+ break;
247
+ }
248
+
249
+ result = wrap_and_yield(bitmap, self, fif);
250
+ }
251
+ return result;
252
+ }
253
+ rb_raise(rb_eTypeError, "Unknown file format");
254
+ }
255
+ END
256
+
257
+ builder.c_singleton <<-"END"
258
+ VALUE with_image_from_memory(VALUE image_data) {
259
+ FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
260
+
261
+ Check_Type(image_data, T_STRING);
262
+ BYTE *image_data_ptr = (BYTE*)RSTRING_PTR(image_data);
263
+ DWORD image_data_length = RSTRING_LEN(image_data);
264
+ FIMEMORY *stream = FreeImage_OpenMemory(image_data_ptr, image_data_length);
265
+
266
+ if (NULL == stream) {
267
+ rb_raise(rb_eTypeError, "Unable to open image_data");
268
+ }
269
+
270
+ fif = FreeImage_GetFileTypeFromMemory(stream, 0);
271
+ if ((fif == FIF_UNKNOWN) || !FreeImage_FIFSupportsReading(fif)) {
272
+ rb_raise(rb_eTypeError, "Unknown file format");
273
+ }
274
+
275
+ FIBITMAP *bitmap = NULL;
276
+ VALUE result = Qnil;
277
+ int flags = fif == FIF_JPEG ? JPEG_ACCURATE : 0;
278
+ bitmap = FreeImage_LoadFromMemory(fif, stream, flags);
279
+ FreeImage_CloseMemory(stream);
280
+ if (bitmap) {
281
+ result = wrap_and_yield(bitmap, self, fif);
282
+ }
283
+ return result;
284
+ }
285
+ END
286
+
287
+ builder.c <<-"END"
288
+ VALUE with_crop(int l, int t, int r, int b) {
289
+ FIBITMAP *copy;
290
+ VALUE result = Qnil;
291
+ GET_BITMAP(bitmap);
292
+
293
+ if (copy = FreeImage_Copy(bitmap, l, t, r, b)) {
294
+ copy_icc_profile(self, bitmap, copy);
295
+ result = wrap_and_yield(copy, self, 0);
296
+ }
297
+ return result;
298
+ }
299
+ END
300
+
301
+ builder.c <<-"END"
302
+ int height() {
303
+ GET_BITMAP(bitmap);
304
+
305
+ return FreeImage_GetHeight(bitmap);
306
+ }
307
+ END
308
+
309
+ builder.c <<-"END"
310
+ int width() {
311
+ GET_BITMAP(bitmap);
312
+
313
+ return FreeImage_GetWidth(bitmap);
314
+ }
315
+ END
316
+
317
+ builder.c <<-"END"
318
+ int image_type() {
319
+ GET_BITMAP(bitmap);
320
+
321
+ return FreeImage_GetImageType(bitmap);
322
+ }
323
+ END
324
+
325
+ builder.c <<-"END"
326
+ int depth() {
327
+ GET_BITMAP(bitmap);
328
+
329
+ return FreeImage_GetBPP(bitmap);
330
+ }
331
+ END
332
+
333
+ builder.c <<-"END"
334
+ int colortype() {
335
+ GET_BITMAP(bitmap);
336
+
337
+ return FreeImage_GetColorType(bitmap);
338
+ }
339
+ END
340
+
341
+ builder.c <<-"END"
342
+ VALUE resize(long w, long h) {
343
+ if (w <= 0) rb_raise(rb_eArgError, "Width <= 0");
344
+ if (h <= 0) rb_raise(rb_eArgError, "Height <= 0");
345
+ GET_BITMAP(bitmap);
346
+ FIBITMAP *image = FreeImage_Rescale(bitmap, w, h, FILTER_CATMULLROM);
347
+ if (image) {
348
+ copy_icc_profile(self, bitmap, image);
349
+ return wrap_and_yield(image, self, 0);
350
+ }
351
+ return Qnil;
352
+ }
353
+ END
354
+
355
+ builder.c <<-"END"
356
+ VALUE save(char * output) {
357
+ FREE_IMAGE_FORMAT fif = FreeImage_GetFIFFromFilename(output);
358
+ if (fif == FIF_UNKNOWN) fif = FIX2INT(rb_iv_get(self, "@file_type"));
359
+ if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsWriting(fif)) {
360
+ GET_BITMAP(bitmap);
361
+ int flags = fif == FIF_JPEG ? JPEG_QUALITYSUPERB : 0;
362
+ BOOL result = 0, unload = 0;
363
+
364
+ if (fif == FIF_PNG) FreeImage_DestroyICCProfile(bitmap);
365
+ if (fif == FIF_JPEG && FreeImage_GetBPP(bitmap) != 24)
366
+ bitmap = FreeImage_ConvertTo24Bits(bitmap), unload = 1; // sue me
367
+
368
+ result = FreeImage_Save(fif, bitmap, output, flags);
369
+
370
+ if (unload) FreeImage_Unload(bitmap);
371
+
372
+ return result ? Qtrue : Qfalse;
373
+ }
374
+ rb_raise(rb_eTypeError, "Unknown file format");
375
+ }
376
+ END
377
+ end
378
+ end
Binary file
@@ -0,0 +1,158 @@
1
+ dir = File.expand_path "~/.ruby_inline"
2
+ if test ?d, dir then
3
+ require 'fileutils'
4
+ puts "nuking #{dir}"
5
+ # force removal, Windoze is bitching at me, something to hunt later...
6
+ FileUtils.rm_r dir, :force => true
7
+ end
8
+
9
+ require 'rubygems'
10
+ require 'minitest/unit'
11
+ require 'minitest/autorun' if $0 == __FILE__
12
+ require 'image_science'
13
+
14
+ class TestImageScience < MiniTest::Unit::TestCase
15
+ def setup
16
+ @path = 'test/pix.png'
17
+ @tmppath = 'test/pix-tmp.png'
18
+ @h = @w = 50
19
+ end
20
+
21
+ def teardown
22
+ File.unlink @tmppath if File.exist? @tmppath
23
+ end
24
+
25
+ def test_class_with_image
26
+ ImageScience.with_image @path do |img|
27
+ assert_kind_of ImageScience, img
28
+ assert_equal @h, img.height
29
+ assert_equal @w, img.width
30
+ assert img.save(@tmppath)
31
+ end
32
+
33
+ assert File.exists?(@tmppath)
34
+
35
+ ImageScience.with_image @tmppath do |img|
36
+ assert_kind_of ImageScience, img
37
+ assert_equal @h, img.height
38
+ assert_equal @w, img.width
39
+ end
40
+ end
41
+
42
+ def test_class_with_image_missing
43
+ assert_raises TypeError do
44
+ ImageScience.with_image @path + "nope" do |img|
45
+ flunk
46
+ end
47
+ end
48
+ end
49
+
50
+ def test_class_with_image_missing_with_img_extension
51
+ assert_nil ImageScience.with_image("nope#{@path}") do |img|
52
+ flunk
53
+ end
54
+ end
55
+
56
+ def test_class_with_image_from_memory
57
+ data = File.new(@path).binmode.read
58
+
59
+ ImageScience.with_image_from_memory data do |img|
60
+ assert_kind_of ImageScience, img
61
+ assert_equal @h, img.height
62
+ assert_equal @w, img.width
63
+ assert img.save(@tmppath)
64
+ end
65
+
66
+ assert File.exists?(@tmppath)
67
+
68
+ ImageScience.with_image @tmppath do |img|
69
+ assert_kind_of ImageScience, img
70
+ assert_equal @h, img.height
71
+ assert_equal @w, img.width
72
+ end
73
+ end
74
+
75
+ def test_class_with_image_from_memory_empty_string
76
+ assert_raises TypeError do
77
+ ImageScience.with_image_from_memory "" do |img|
78
+ flunk
79
+ end
80
+ end
81
+ end
82
+
83
+ def test_resize
84
+ ImageScience.with_image @path do |img|
85
+ img.resize(25, 25) do |thumb|
86
+ assert thumb.save(@tmppath)
87
+ end
88
+ end
89
+
90
+ assert File.exists?(@tmppath)
91
+
92
+ ImageScience.with_image @tmppath do |img|
93
+ assert_kind_of ImageScience, img
94
+ assert_equal 25, img.height
95
+ assert_equal 25, img.width
96
+ end
97
+ end
98
+
99
+ def test_resize_floats
100
+ ImageScience.with_image @path do |img|
101
+ img.resize(25.2, 25.7) do |thumb|
102
+ assert thumb.save(@tmppath)
103
+ end
104
+ end
105
+
106
+ assert File.exists?(@tmppath)
107
+
108
+ ImageScience.with_image @tmppath do |img|
109
+ assert_kind_of ImageScience, img
110
+ assert_equal 25, img.height
111
+ assert_equal 25, img.width
112
+ end
113
+ end
114
+
115
+ def test_resize_zero
116
+ assert_raises ArgumentError do
117
+ ImageScience.with_image @path do |img|
118
+ img.resize(0, 25) do |thumb|
119
+ assert thumb.save(@tmppath)
120
+ end
121
+ end
122
+ end
123
+
124
+ refute File.exists?(@tmppath)
125
+
126
+ assert_raises ArgumentError do
127
+ ImageScience.with_image @path do |img|
128
+ img.resize(25, 0) do |thumb|
129
+ assert thumb.save(@tmppath)
130
+ end
131
+ end
132
+ end
133
+
134
+ refute File.exists?(@tmppath)
135
+ end
136
+
137
+ def test_resize_negative
138
+ assert_raises ArgumentError do
139
+ ImageScience.with_image @path do |img|
140
+ img.resize(-25, 25) do |thumb|
141
+ assert thumb.save(@tmppath)
142
+ end
143
+ end
144
+ end
145
+
146
+ refute File.exists?(@tmppath)
147
+
148
+ assert_raises ArgumentError do
149
+ ImageScience.with_image @path do |img|
150
+ img.resize(25, -25) do |thumb|
151
+ assert thumb.save(@tmppath)
152
+ end
153
+ end
154
+ end
155
+
156
+ refute File.exists?(@tmppath)
157
+ end
158
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scambra-image_science
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Davis
8
+ - Sergio Cambra
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-06-24 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: hoe
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 2.2.0
25
+ version:
26
+ description: ImageScience is a clean and happy Ruby library that generates thumbnails -- and kicks the living crap out of RMagick. Oh, and it doesn't leak memory like a sieve. :) For more information including build steps, see http://seattlerb.rubyforge.org/
27
+ email: sergio@entrecables.com
28
+ executables:
29
+ - image_science_thumb
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ files:
37
+ - History.txt
38
+ - Manifest.txt
39
+ - README.txt
40
+ - Rakefile
41
+ - bench.rb
42
+ - bin/image_science_thumb
43
+ - lib/image_science.rb
44
+ - test/pix.png
45
+ - test/test_image_science.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/scambra/image_science
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --main
51
+ - README.txt
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project: 1.3.4
69
+ rubygems_version: 1.2.0
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: ImageScience is a clean and happy Ruby library that generates thumbnails -- and kicks the living crap out of RMagick
73
+ test_files:
74
+ - test/test_image_science.rb