tlconnor-image_science 1.3.2.1.Asynchrony

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ mkmf.log
5
+ Makefile
6
+ *.so
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in number_with_error.gemspec
4
+ gemspec
data/History.txt ADDED
@@ -0,0 +1,77 @@
1
+ === 1.2.1 / 2009-08-14
2
+
3
+ * 2 minor enhancements:
4
+
5
+ * Added luis' patches to make it build properly on windows.
6
+ * with_image now raises on missing/bad files.
7
+
8
+ == 1.2.0 / 2009-06-23
9
+
10
+ * 7 minor enhancements:
11
+
12
+ * Moved quick_thumb to bin/image_science_thumb and properly added.
13
+ * Added -s (square) flag to bin/image_science_thumb
14
+ * Added autorotating on image load. (choonkeat)
15
+ * Added ruby_inline to clean globs
16
+ * Added with_image_from_memory. (sumbach)
17
+ * Switched to minitest.
18
+ * Updated rakefile for now hoe capabilities.
19
+
20
+ * 3 bug fixes:
21
+
22
+ * Check and convert to 24 BPP if save type is jpg. Caused by 32bpp png to jpg.
23
+ * Fixed 1.9isms
24
+ * Fixed BMP support. Tweaked whitespace.
25
+
26
+ == 1.1.3 / 2007-05-30
27
+
28
+ * 2 minor enhancements:
29
+
30
+ * Added quick_thumb as an example to look at.
31
+ * Error handler doesn't raise by default. Raises if $DEBUG==true.
32
+
33
+ == 1.1.2 / 2007-04-18
34
+
35
+ * 2 bug fixes:
36
+
37
+ * reports bad height/width values for resize
38
+ * explicitly removes ICC color profiles from PNGs (bug in freeimage).
39
+
40
+ == 1.1.1 / 2007-03-08
41
+
42
+ * 5 minor enhancements:
43
+
44
+ * Added error handler that raises with information about what went wrong.
45
+ * thumbnail is now pure ruby, everything now uses resize.
46
+ * Produces cleaner JPEG files, with a small cost to file size/speed.
47
+ * resize now uses Catmull-Rom spline filter for better quality.
48
+ * resize copies existing ICC Profile to thumbnail, producing better color.
49
+ * ICC Profile NOT copied for PNG as it seems to be buggy.
50
+
51
+ * 1 bug fix:
52
+
53
+ * Fixed rdoc
54
+
55
+ == 1.1.0 / 2007-01-05
56
+
57
+ * 3 major enhancements:
58
+
59
+ * Added resize(width, height)
60
+ * Added save(path)
61
+ * All thumbnail and resize methods yield instead of saving directly.
62
+
63
+ * 1 minor enhancement:
64
+
65
+ * Will now try to use FreeImage from ports if /opt/local exists.
66
+
67
+ * 2 bug fixes:
68
+
69
+ * Fixed the linker issue on PPC.
70
+ * Rakefile will now clean the image files created by bench.rb
71
+
72
+ == 1.0.0 / 2006-12-01
73
+
74
+ * 1 major enhancement
75
+
76
+ * Birthday!
77
+
data/Manifest.txt ADDED
@@ -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
data/README.txt ADDED
@@ -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.
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ task :build => [:compile, :chmod]
5
+
6
+ task :compile do
7
+ `ruby ext/image_science/extconf.rb`
8
+ `make`
9
+ `mv extension.so lib/image_science/extension.so`
10
+ end
11
+
12
+ task :chmod do
13
+ File.chmod(0775, 'lib/image_science/extension.so')
14
+ end
15
+
data/bench.rb ADDED
@@ -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,10 @@
1
+ require 'mkmf'
2
+
3
+ include_path = '/usr/local/include:/usr/include:/opt/local/include:/opt/include'
4
+ lib_path = '/usr/local/lib:/usr/lib:/opt/local/lib:/opt/lib'
5
+
6
+ dir_config("image_science", include_path, lib_path)
7
+
8
+ have_library('freeimage')
9
+
10
+ create_makefile("image_science/extension")
@@ -0,0 +1,219 @@
1
+ #include "ruby.h"
2
+ #include "FreeImage.h"
3
+
4
+ #define GET_BITMAP(name) Data_Get_Struct(self, FIBITMAP, (name)); if (!(name)) rb_raise(rb_eTypeError, "Bitmap has already been freed");
5
+
6
+ VALUE unload(VALUE self) {
7
+ FIBITMAP *bitmap;
8
+ GET_BITMAP(bitmap);
9
+
10
+ FreeImage_Unload(bitmap);
11
+ DATA_PTR(self) = NULL;
12
+ return Qnil;
13
+ }
14
+
15
+
16
+ VALUE wrap_and_yield(FIBITMAP *image, VALUE self, FREE_IMAGE_FORMAT fif) {
17
+ unsigned int self_is_class = rb_type(self) == T_CLASS;
18
+ VALUE klass = self_is_class ? self : CLASS_OF(self);
19
+ VALUE type = self_is_class ? INT2FIX(fif) : rb_iv_get(self, "@file_type");
20
+ VALUE obj = Data_Wrap_Struct(klass, NULL, NULL, image);
21
+ rb_iv_set(obj, "@file_type", type);
22
+ return rb_ensure(rb_yield, obj, unload, obj);
23
+ }
24
+
25
+
26
+ void copy_icc_profile(VALUE self, FIBITMAP *from, FIBITMAP *to) {
27
+ FREE_IMAGE_FORMAT fif = FIX2INT(rb_iv_get(self, "@file_type"));
28
+ if (fif != FIF_PNG && FreeImage_FIFSupportsICCProfiles(fif)) {
29
+ FIICCPROFILE *profile = FreeImage_GetICCProfile(from);
30
+ if (profile && profile->data) {
31
+ FreeImage_CreateICCProfile(to, profile->data, profile->size);
32
+ }
33
+ }
34
+ }
35
+
36
+ static char* lastFreeImageMessage = NULL;
37
+
38
+ void FreeImageMessageHandler(FREE_IMAGE_FORMAT fif, const char *message) {
39
+ if (lastFreeImageMessage != NULL)
40
+ free(lastFreeImageMessage);
41
+
42
+ lastFreeImageMessage = strdup(message);
43
+ }
44
+
45
+
46
+ static VALUE with_image(VALUE self, VALUE _input) {
47
+ char * input = StringValuePtr(_input);
48
+
49
+ FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
50
+ int flags;
51
+
52
+ fif = FreeImage_GetFileType(input, 0);
53
+ if (fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(input);
54
+ if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) {
55
+ FIBITMAP *bitmap;
56
+ VALUE result = Qnil;
57
+ flags = fif == FIF_JPEG ? JPEG_ACCURATE : 0;
58
+ if (bitmap = FreeImage_Load(fif, input, flags)) {
59
+ FITAG *tagValue = NULL;
60
+ FreeImage_GetMetadata(FIMD_EXIF_MAIN, bitmap, "Orientation", &tagValue);
61
+ switch (tagValue == NULL ? 0 : *((short *) FreeImage_GetTagValue(tagValue))) {
62
+ case 6:
63
+ bitmap = FreeImage_RotateClassic(bitmap, 270);
64
+ break;
65
+ case 3:
66
+ bitmap = FreeImage_RotateClassic(bitmap, 180);
67
+ break;
68
+ case 8:
69
+ bitmap = FreeImage_RotateClassic(bitmap, 90);
70
+ break;
71
+ default:
72
+ break;
73
+ }
74
+
75
+ result = wrap_and_yield(bitmap, self, fif);
76
+ } else {
77
+ rb_raise(rb_eTypeError,
78
+ "Failed to load image from file %s: %s",
79
+ input,
80
+ lastFreeImageMessage != NULL ? lastFreeImageMessage : "Unspecified error");
81
+ }
82
+
83
+ return (result);
84
+ }
85
+ rb_raise(rb_eTypeError, "Unknown file format");
86
+ }
87
+
88
+
89
+ static VALUE with_image_from_memory(VALUE self, VALUE _image_data) {
90
+ VALUE image_data = (_image_data);
91
+
92
+ FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
93
+
94
+ Check_Type(image_data, T_STRING);
95
+ BYTE *image_data_ptr = (BYTE*)RSTRING_PTR(image_data);
96
+ DWORD image_data_length = RSTRING_LEN(image_data);
97
+ FIMEMORY *stream = FreeImage_OpenMemory(image_data_ptr, image_data_length);
98
+
99
+ if (NULL == stream) {
100
+ rb_raise(rb_eTypeError, "Unable to open image_data");
101
+ }
102
+
103
+ fif = FreeImage_GetFileTypeFromMemory(stream, 0);
104
+ if ((fif == FIF_UNKNOWN) || !FreeImage_FIFSupportsReading(fif)) {
105
+ rb_raise(rb_eTypeError, "Unknown file format");
106
+ }
107
+
108
+ FIBITMAP *bitmap = NULL;
109
+ VALUE result = Qnil;
110
+ int flags = fif == FIF_JPEG ? JPEG_ACCURATE : 0;
111
+ bitmap = FreeImage_LoadFromMemory(fif, stream, flags);
112
+ FreeImage_CloseMemory(stream);
113
+ if (bitmap) {
114
+ result = wrap_and_yield(bitmap, self, fif);
115
+ }
116
+ return (result);
117
+ }
118
+
119
+
120
+ static VALUE with_crop(VALUE self, VALUE _l, VALUE _t, VALUE _r, VALUE _b) {
121
+ int l = FIX2INT(_l);
122
+ int t = FIX2INT(_t);
123
+ int r = FIX2INT(_r);
124
+ int b = FIX2INT(_b);
125
+
126
+ FIBITMAP *copy, *bitmap;
127
+ VALUE result = Qnil;
128
+ GET_BITMAP(bitmap);
129
+
130
+ if (copy = FreeImage_Copy(bitmap, l, t, r, b)) {
131
+ copy_icc_profile(self, bitmap, copy);
132
+ result = wrap_and_yield(copy, self, 0);
133
+ }
134
+ return (result);
135
+ }
136
+
137
+
138
+ static VALUE height(VALUE self) {
139
+
140
+ FIBITMAP *bitmap;
141
+ GET_BITMAP(bitmap);
142
+
143
+ return INT2FIX(FreeImage_GetHeight(bitmap));
144
+ }
145
+
146
+
147
+ static VALUE width(VALUE self) {
148
+
149
+ FIBITMAP *bitmap;
150
+ GET_BITMAP(bitmap);
151
+
152
+ return INT2FIX(FreeImage_GetWidth(bitmap));
153
+ }
154
+
155
+
156
+ static VALUE resize(VALUE self, VALUE _w, VALUE _h) {
157
+ long w = NUM2LONG(_w);
158
+ long h = NUM2LONG(_h);
159
+
160
+ FIBITMAP *bitmap, *image;
161
+ if (w <= 0) rb_raise(rb_eArgError, "Width <= 0");
162
+ if (h <= 0) rb_raise(rb_eArgError, "Height <= 0");
163
+ GET_BITMAP(bitmap);
164
+ image = FreeImage_Rescale(bitmap, w, h, FILTER_CATMULLROM);
165
+ if (image) {
166
+ copy_icc_profile(self, bitmap, image);
167
+ return (wrap_and_yield(image, self, 0));
168
+ }
169
+ return (Qnil);
170
+ }
171
+
172
+
173
+ static VALUE save(VALUE self, VALUE _output) {
174
+ char * output = StringValuePtr(_output);
175
+
176
+ int flags;
177
+ FIBITMAP *bitmap;
178
+ FREE_IMAGE_FORMAT fif = FreeImage_GetFIFFromFilename(output);
179
+ if (fif == FIF_UNKNOWN) fif = FIX2INT(rb_iv_get(self, "@file_type"));
180
+ if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsWriting(fif)) {
181
+ GET_BITMAP(bitmap);
182
+ flags = fif == FIF_JPEG ? JPEG_QUALITYGOOD : 0;
183
+ BOOL result = 0, unload = 0;
184
+
185
+ if (fif == FIF_PNG) FreeImage_DestroyICCProfile(bitmap);
186
+ if (fif == FIF_JPEG && FreeImage_GetBPP(bitmap) != 24)
187
+ bitmap = FreeImage_ConvertTo24Bits(bitmap), unload = 1;
188
+
189
+ result = FreeImage_Save(fif, bitmap, output, flags);
190
+
191
+ if (unload) FreeImage_Unload(bitmap);
192
+
193
+ return (result ? Qtrue : Qfalse);
194
+ }
195
+ rb_raise(rb_eTypeError, "Unknown file format");
196
+ }
197
+
198
+
199
+
200
+ #ifdef __cplusplus
201
+ extern "C" {
202
+ #endif
203
+ void Init_extension() {
204
+ VALUE c = rb_cObject;
205
+ c = rb_const_get(c, rb_intern("ImageScience"));
206
+
207
+ rb_define_method(c, "height", (VALUE(*)(ANYARGS))height, 0);
208
+ rb_define_method(c, "resize", (VALUE(*)(ANYARGS))resize, 2);
209
+ rb_define_method(c, "save", (VALUE(*)(ANYARGS))save, 1);
210
+ rb_define_method(c, "width", (VALUE(*)(ANYARGS))width, 0);
211
+ rb_define_method(c, "with_crop", (VALUE(*)(ANYARGS))with_crop, 4);
212
+ rb_define_singleton_method(c, "with_image", (VALUE(*)(ANYARGS))with_image, 1);
213
+ rb_define_singleton_method(c, "with_image_from_memory", (VALUE(*)(ANYARGS))with_image_from_memory, 1);
214
+ FreeImage_SetOutputMessage(FreeImageMessageHandler);
215
+
216
+ }
217
+ #ifdef __cplusplus
218
+ }
219
+ #endif
@@ -0,0 +1,28 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "image_science/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "tlconnor-image_science"
6
+ s.version = ImageScience::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ['Ryan Davis', 'Craig Buchek']
9
+ s.email = ['craig.buchek@asolutions.com']
10
+ s.homepage = "http://github.com/asynchrony/#{s.name}"
11
+ s.summary = %q{Replacement for RMagick; uses FreeImage instead of ImageMagick}
12
+ s.description = %q{ImageScience is a clean and happy Ruby library that generates
13
+ thumbnails -- and kicks the living crap out of RMagick. Oh, and it
14
+ doesn't leak memory like a sieve. :)
15
+
16
+ This fork does not require RubyInline.
17
+
18
+ For more information (on the original variant), see http://seattlerb.rubyforge.org/ImageScience.html
19
+ }
20
+
21
+ s.rubyforge_project = "image_science"
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
26
+ s.extensions = `git ls-files -- ext/*/*.rb`.split("\n")
27
+ s.require_paths = ["lib"]
28
+ end
@@ -0,0 +1,3 @@
1
+ require 'image_science/version'
2
+ require 'image_science/image_science'
3
+ require 'image_science/extension'
@@ -0,0 +1,83 @@
1
+ ##
2
+ # Provides a clean and simple API to generate thumbnails using
3
+ # FreeImage as the underlying mechanism.
4
+ #
5
+ # For more information or if you have build issues with FreeImage, see
6
+ # http://seattlerb.rubyforge.org/ImageScience.html
7
+
8
+ class ImageScience
9
+
10
+ ##
11
+ # The top-level image loader opens +path+ and then yields the image.
12
+
13
+ def self.with_image(path) # :yields: image
14
+ end
15
+
16
+ ##
17
+ # The top-level image loader, opens an image from the string +data+ and then yields the image.
18
+
19
+ def self.with_image_from_memory(data) # :yields: image
20
+ end
21
+
22
+ ##
23
+ # Crops an image to +left+, +top+, +right+, and +bottom+ and then
24
+ # yields the new image.
25
+
26
+ def with_crop(left, top, right, bottom) # :yields: image
27
+ end
28
+
29
+ ##
30
+ # Returns the width of the image, in pixels.
31
+
32
+ def width; end
33
+
34
+ ##
35
+ # Returns the height of the image, in pixels.
36
+
37
+ def height; end
38
+
39
+ ##
40
+ # Saves the image out to +path+. Changing the file extension will
41
+ # convert the file type to the appropriate format.
42
+
43
+ def save(path); end
44
+
45
+ ##
46
+ # Resizes the image to +width+ and +height+ using a cubic-bspline
47
+ # filter and yields the new image.
48
+
49
+ def resize(width, height) # :yields: image
50
+ end
51
+
52
+ ##
53
+ # Creates a proportional thumbnail of the image scaled so its longest
54
+ # edge is resized to +size+ and yields the new image.
55
+
56
+ def thumbnail(size) # :yields: image
57
+ w, h = width, height
58
+ scale = size.to_f / (w > h ? w : h)
59
+
60
+ self.resize((w * scale).round, (h * scale).round) do |image|
61
+ yield image
62
+ end
63
+ end
64
+
65
+ ##
66
+ # Creates a square thumbnail of the image cropping the longest edge
67
+ # to match the shortest edge, resizes to +size+, and yields the new
68
+ # image.
69
+
70
+ def cropped_thumbnail(size) # :yields: image
71
+ w, h = width, height
72
+ l, t, r, b, half = 0, 0, w, h, (w - h).abs / 2
73
+
74
+ l, r = half, half + h if w > h
75
+ t, b = half, half + w if h > w
76
+
77
+ with_crop(l, t, r, b) do |img|
78
+ img.thumbnail(size) do |thumb|
79
+ yield thumb
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,3 @@
1
+ class ImageScience
2
+ VERSION = '1.3.2.1.Asynchrony'
3
+ end
data/test/pix.png ADDED
Binary file
@@ -0,0 +1,177 @@
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_raises RuntimeError do
52
+ assert_nil ImageScience.with_image("nope#{@path}") do |img|
53
+ flunk
54
+ end
55
+ end
56
+ end
57
+
58
+ def test_class_with_image_from_memory
59
+ data = File.new(@path).binmode.read
60
+
61
+ ImageScience.with_image_from_memory data do |img|
62
+ assert_kind_of ImageScience, img
63
+ assert_equal @h, img.height
64
+ assert_equal @w, img.width
65
+ assert img.save(@tmppath)
66
+ end
67
+
68
+ assert File.exists?(@tmppath)
69
+
70
+ ImageScience.with_image @tmppath do |img|
71
+ assert_kind_of ImageScience, img
72
+ assert_equal @h, img.height
73
+ assert_equal @w, img.width
74
+ end
75
+ end
76
+
77
+ def test_class_with_image_from_memory_empty_string
78
+ assert_raises TypeError do
79
+ ImageScience.with_image_from_memory "" do |img|
80
+ flunk
81
+ end
82
+ end
83
+ end
84
+
85
+ def test_resize
86
+ ImageScience.with_image @path do |img|
87
+ img.resize(25, 25) do |thumb|
88
+ assert thumb.save(@tmppath)
89
+ end
90
+ end
91
+
92
+ assert File.exists?(@tmppath)
93
+
94
+ ImageScience.with_image @tmppath do |img|
95
+ assert_kind_of ImageScience, img
96
+ assert_equal 25, img.height
97
+ assert_equal 25, img.width
98
+ end
99
+ end
100
+
101
+ def test_resize_floats
102
+ ImageScience.with_image @path do |img|
103
+ img.resize(25.2, 25.7) do |thumb|
104
+ assert thumb.save(@tmppath)
105
+ end
106
+ end
107
+
108
+ assert File.exists?(@tmppath)
109
+
110
+ ImageScience.with_image @tmppath do |img|
111
+ assert_kind_of ImageScience, img
112
+ assert_equal 25, img.height
113
+ assert_equal 25, img.width
114
+ end
115
+ end
116
+
117
+ def test_resize_zero
118
+ assert_raises ArgumentError do
119
+ ImageScience.with_image @path do |img|
120
+ img.resize(0, 25) do |thumb|
121
+ assert thumb.save(@tmppath)
122
+ end
123
+ end
124
+ end
125
+
126
+ refute File.exists?(@tmppath)
127
+
128
+ assert_raises ArgumentError do
129
+ ImageScience.with_image @path do |img|
130
+ img.resize(25, 0) do |thumb|
131
+ assert thumb.save(@tmppath)
132
+ end
133
+ end
134
+ end
135
+
136
+ refute File.exists?(@tmppath)
137
+ end
138
+
139
+ def test_resize_negative
140
+ assert_raises ArgumentError do
141
+ ImageScience.with_image @path do |img|
142
+ img.resize(-25, 25) do |thumb|
143
+ assert thumb.save(@tmppath)
144
+ end
145
+ end
146
+ end
147
+
148
+ refute File.exists?(@tmppath)
149
+
150
+ assert_raises ArgumentError do
151
+ ImageScience.with_image @path do |img|
152
+ img.resize(25, -25) do |thumb|
153
+ assert thumb.save(@tmppath)
154
+ end
155
+ end
156
+ end
157
+
158
+ refute File.exists?(@tmppath)
159
+ end
160
+
161
+ def test_thumbnail
162
+ ImageScience.with_image @path do |img|
163
+ img.thumbnail(29) do |thumb|
164
+ assert thumb.save(@tmppath)
165
+ end
166
+ end
167
+
168
+ assert File.exists?(@tmppath)
169
+
170
+ ImageScience.with_image @tmppath do |img|
171
+ assert_kind_of ImageScience, img
172
+ assert_equal 29, img.height
173
+ assert_equal 29, img.width
174
+ end
175
+ end
176
+
177
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tlconnor-image_science
3
+ version: !ruby/object:Gem::Version
4
+ hash: 271900941
5
+ prerelease: 8
6
+ segments:
7
+ - 1
8
+ - 3
9
+ - 2
10
+ - 1
11
+ - Asynchrony
12
+ version: 1.3.2.1.Asynchrony
13
+ platform: ruby
14
+ authors:
15
+ - Ryan Davis
16
+ - Craig Buchek
17
+ autorequire:
18
+ bindir: bin
19
+ cert_chain: []
20
+
21
+ date: 2012-07-30 00:00:00 Z
22
+ dependencies: []
23
+
24
+ description: |
25
+ ImageScience is a clean and happy Ruby library that generates
26
+ thumbnails -- and kicks the living crap out of RMagick. Oh, and it
27
+ doesn't leak memory like a sieve. :)
28
+
29
+ This fork does not require RubyInline.
30
+
31
+ For more information (on the original variant), see http://seattlerb.rubyforge.org/ImageScience.html
32
+
33
+ email:
34
+ - craig.buchek@asolutions.com
35
+ executables:
36
+ - image_science_thumb
37
+ extensions:
38
+ - ext/image_science/extconf.rb
39
+ extra_rdoc_files: []
40
+
41
+ files:
42
+ - .gitignore
43
+ - Gemfile
44
+ - History.txt
45
+ - Manifest.txt
46
+ - README.txt
47
+ - Rakefile
48
+ - bench.rb
49
+ - bin/image_science_thumb
50
+ - ext/image_science/extconf.rb
51
+ - ext/image_science/image_science.c
52
+ - image_science.gemspec
53
+ - lib/image_science.rb
54
+ - lib/image_science/image_science.rb
55
+ - lib/image_science/version.rb
56
+ - test/pix.png
57
+ - test/test_image_science.rb
58
+ homepage: http://github.com/asynchrony/tlconnor-image_science
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options: []
63
+
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">"
79
+ - !ruby/object:Gem::Version
80
+ hash: 25
81
+ segments:
82
+ - 1
83
+ - 3
84
+ - 1
85
+ version: 1.3.1
86
+ requirements: []
87
+
88
+ rubyforge_project: image_science
89
+ rubygems_version: 1.8.24
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Replacement for RMagick; uses FreeImage instead of ImageMagick
93
+ test_files:
94
+ - test/pix.png
95
+ - test/test_image_science.rb