image_science 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/History.txt +12 -0
  2. data/README.txt +1 -1
  3. data/Rakefile +2 -0
  4. data/bench.rb +18 -2
  5. data/lib/image_science.rb +65 -27
  6. metadata +13 -4
data/History.txt CHANGED
@@ -1,3 +1,15 @@
1
+ == 1.1.0 / 2007-01-05
2
+
3
+ * 3 major enhancements:
4
+ * Added resize(width, height)
5
+ * Added save(path)
6
+ * All thumbnail and resize methods yield instead of saving directly.
7
+ * 1 minor enhancement:
8
+ * Will now try to use FreeImage from ports if /opt/local exists.
9
+ * 2 bug fixes:
10
+ * Fixed the linker issue on PPC.
11
+ * Rakefile will now clean the image files created by bench.rb
12
+
1
13
  == 1.0.0 / 2006-12-01
2
14
 
3
15
  * 1 major enhancement
data/README.txt CHANGED
@@ -35,7 +35,7 @@ For more information including build steps, see http://seattlerb.rubyforge.org/
35
35
  == INSTALL:
36
36
 
37
37
  * Download and install FreeImage. See notes at url above.
38
- * sudo gem install -y ImageScience
38
+ * sudo gem install -y image_science
39
39
 
40
40
  == LICENSE:
41
41
 
data/Rakefile CHANGED
@@ -10,6 +10,8 @@ Hoe.new('image_science', ImageScience::VERSION) do |p|
10
10
  p.summary = p.description[/^[^.]+\./]
11
11
  p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1].first.strip
12
12
  p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
13
+ p.extra_deps << 'RubyInline'
14
+ p.clean_globs << 'blah*png'
13
15
  end
14
16
 
15
17
  # vim: syntax=Ruby
data/bench.rb CHANGED
@@ -27,7 +27,9 @@ Benchmark::bm(20) do |x|
27
27
  x.report("cropped") {
28
28
  for i in 0..max do
29
29
  ImageScience.with_image(file) do |img|
30
- img.cropped_thumbnail("blah_cropped.png", 100)
30
+ img.cropped_thumbnail(100) do |thumb|
31
+ thumb.save("blah_cropped.png")
32
+ end
31
33
  end
32
34
  end
33
35
  }
@@ -35,8 +37,22 @@ Benchmark::bm(20) do |x|
35
37
  x.report("proportional") {
36
38
  for i in 0..max do
37
39
  ImageScience.with_image(file) do |img|
38
- img.thumbnail("blah_thumb.png", 100)
40
+ img.thumbnail(100) do |thumb|
41
+ thumb.save("blah_thumb.png")
42
+ end
43
+ end
44
+ end
45
+ }
46
+
47
+ x.report("resize") {
48
+ for i in 0..max do
49
+ ImageScience.with_image(file) do |img|
50
+ img.resize(200, 200) do |resize|
51
+ resize.save("blah_resize.png")
52
+ end
39
53
  end
40
54
  end
41
55
  }
42
56
  end
57
+
58
+ # File.unlink(*Dir["blah*png"])
data/lib/image_science.rb CHANGED
@@ -11,17 +11,16 @@ require 'inline'
11
11
  # http://seattlerb.rubyforge.org/ImageScience.html
12
12
 
13
13
  class ImageScience
14
- VERSION = '1.0.0'
14
+ VERSION = '1.1.0'
15
15
 
16
16
  ##
17
- # The top-level image loader opens +path+ and then passes the image
18
- # instance to the given block.
17
+ # The top-level image loader opens +path+ and then yields the image.
19
18
 
20
19
  def self.with_image(path); end # :yields: image
21
20
 
22
21
  ##
23
22
  # Crops an image to +left+, +top+, +right+, and +bottom+ and then
24
- # passes the new image to the given block.
23
+ # yields the new image.
25
24
 
26
25
  def with_crop(left, top, right, bottom); end # :yields: image
27
26
 
@@ -35,33 +34,58 @@ class ImageScience
35
34
 
36
35
  def height; end
37
36
 
37
+ ##
38
+ # Resizes the image to +width+ and +height+ using a cubic-bspline
39
+ # filter and yields the new image.
40
+
41
+ def resize(width, height); end # :yields: image
42
+
38
43
  ##
39
44
  # Creates a proportional thumbnail of the image scaled so its widest
40
- # edge is resized to +size+ and writes the new image to +path+.
45
+ # edge is resized to +size+ and yields the new image.
41
46
 
42
- def thumbnail(path, size); end
47
+ def thumbnail(size); end # :yields: image
43
48
 
44
49
  ##
45
50
  # Creates a square thumbnail of the image cropping the longest edge
46
- # to match the shortest edge, resizes to +size+, and writes the new
47
- # image to +path+.
51
+ # to match the shortest edge, resizes to +size+, and yields the new
52
+ # image.
48
53
 
49
- def cropped_thumbnail(path, size)
54
+ def cropped_thumbnail(size) # :yields: image
50
55
  w, h = width, height
51
- l, t, r, b, half = 0, 0, w, h, (w > h ? (w - h) : (h - w)) / 2
56
+ l, t, r, b, half = 0, 0, w, h, (w - h).abs / 2
52
57
 
53
58
  l, r = half, half + h if w > h
54
59
  t, b = half, half + w if h > w
55
60
 
56
61
  with_crop(l, t, r, b) do |img|
57
- img.thumbnail(path, size)
62
+ img.thumbnail(size) do |thumb|
63
+ yield thumb
64
+ end
58
65
  end
59
66
  end
60
67
 
61
68
  inline do |builder|
69
+ if test ?d, "/opt/local" then
70
+ builder.add_compile_flags "-I/opt/local/include"
71
+ builder.add_link_flags "-L/opt/local/lib"
72
+ end
62
73
  builder.add_link_flags "-lfreeimage"
74
+ builder.add_link_flags "-lstdc++" # only needed on PPC for some reason. lame
63
75
  builder.include '"FreeImage.h"'
64
76
 
77
+ builder.prefix <<-"END"
78
+ #define GET_BITMAP(name) FIBITMAP *(name); Data_Get_Struct(self, FIBITMAP, (name)); if (!(name)) rb_raise(rb_eTypeError, "Bitmap has already been freed")
79
+
80
+ VALUE unload(VALUE self) {
81
+ GET_BITMAP(bitmap);
82
+
83
+ FreeImage_Unload(bitmap);
84
+ DATA_PTR(self) = NULL;
85
+ return Qnil;
86
+ }
87
+ END
88
+
65
89
  builder.c_singleton <<-"END"
66
90
  VALUE with_image(char * input) {
67
91
  FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
@@ -74,8 +98,7 @@ class ImageScience
74
98
  if (bitmap = FreeImage_Load(fif, input, 0)) {
75
99
  VALUE obj = Data_Wrap_Struct(self, NULL, NULL, bitmap);
76
100
  rb_iv_set(obj, "@file_type", INT2FIX(fif));
77
- result = rb_yield(obj);
78
- FreeImage_Unload(bitmap);
101
+ result = rb_ensure(rb_yield, obj, unload, obj);
79
102
  }
80
103
  return result;
81
104
  }
@@ -85,14 +108,13 @@ class ImageScience
85
108
 
86
109
  builder.c <<-"END"
87
110
  VALUE with_crop(int l, int t, int r, int b) {
88
- FIBITMAP *bitmap, *copy;
111
+ FIBITMAP *copy;
89
112
  VALUE result = Qnil;
90
- Data_Get_Struct(self, FIBITMAP, bitmap);
113
+ GET_BITMAP(bitmap);
91
114
 
92
115
  if (copy = FreeImage_Copy(bitmap, l, t, r, b)) {
93
116
  VALUE obj = Data_Wrap_Struct(CLASS_OF(self), NULL, NULL, copy);
94
- result = rb_yield(obj);
95
- FreeImage_Unload(copy);
117
+ result = rb_ensure(rb_yield, obj, unload, obj);
96
118
  }
97
119
  return result;
98
120
  }
@@ -100,30 +122,46 @@ class ImageScience
100
122
 
101
123
  builder.c <<-"END"
102
124
  int height() {
103
- FIBITMAP *bitmap;
104
- Data_Get_Struct(self, FIBITMAP, bitmap);
125
+ GET_BITMAP(bitmap);
126
+
105
127
  return FreeImage_GetHeight(bitmap);
106
128
  }
107
129
  END
108
130
 
109
131
  builder.c <<-"END"
110
132
  int width() {
111
- FIBITMAP *bitmap;
112
- Data_Get_Struct(self, FIBITMAP, bitmap);
133
+ GET_BITMAP(bitmap);
134
+
113
135
  return FreeImage_GetWidth(bitmap);
114
136
  }
115
137
  END
116
138
 
117
139
  builder.c <<-"END"
118
- VALUE thumbnail(char * output, int size) {
119
- FIBITMAP *bitmap;
140
+ VALUE thumbnail(int size) {
141
+ GET_BITMAP(bitmap);
142
+ FIBITMAP *image = FreeImage_MakeThumbnail(bitmap, size, TRUE);
143
+ VALUE obj = Data_Wrap_Struct(CLASS_OF(self), NULL, NULL, image);
144
+ return rb_ensure(rb_yield, obj, unload, obj);
145
+ }
146
+ END
147
+
148
+ builder.c <<-"END"
149
+ VALUE resize(int w, int h) {
150
+ GET_BITMAP(bitmap);
151
+ FIBITMAP *image = FreeImage_Rescale(bitmap, w, h, FILTER_BSPLINE);
152
+ VALUE obj = Data_Wrap_Struct(CLASS_OF(self), NULL, NULL, image);
153
+ return rb_ensure(rb_yield, obj, unload, obj);
154
+ }
155
+ END
156
+
157
+ builder.c <<-"END"
158
+ VALUE save(char * output) {
120
159
  FREE_IMAGE_FORMAT fif = FreeImage_GetFIFFromFilename(output);
121
160
  if (fif == FIF_UNKNOWN) fif = FIX2INT(rb_iv_get(self, "@file_type"));
122
161
  if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsWriting(fif)) {
123
- Data_Get_Struct(self, FIBITMAP, bitmap);
124
- FIBITMAP *thumbnail = FreeImage_MakeThumbnail(bitmap, size, TRUE);
125
- if (FreeImage_Save(fif, thumbnail, output, 0)) {
126
- FreeImage_Unload(thumbnail);
162
+ GET_BITMAP(bitmap);
163
+
164
+ if (FreeImage_Save(fif, bitmap, output, 0)) {
127
165
  return Qtrue;
128
166
  }
129
167
  return Qfalse;
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.0
2
+ rubygems_version: 0.9.0.8
3
3
  specification_version: 1
4
4
  name: image_science
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.0.0
7
- date: 2006-12-13 00:00:00 -05:00
6
+ version: 1.1.0
7
+ date: 2007-01-05 00:00:00 -08:00
8
8
  summary: ImageScience is a clean and happy Ruby library that generates thumbnails -- and kicks the living crap out of RMagick.
9
9
  require_paths:
10
10
  - lib
@@ -55,5 +55,14 @@ dependencies:
55
55
  requirements:
56
56
  - - ">="
57
57
  - !ruby/object:Gem::Version
58
- version: 1.1.4
58
+ version: 1.1.6
59
+ version:
60
+ - !ruby/object:Gem::Dependency
61
+ name: RubyInline
62
+ version_requirement:
63
+ version_requirements: !ruby/object:Gem::Version::Requirement
64
+ requirements:
65
+ - - ">"
66
+ - !ruby/object:Gem::Version
67
+ version: 0.0.0
59
68
  version: