carlosnz-image_science 1.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +41 -0
- data/Manifest.txt +9 -0
- data/README.txt +66 -0
- data/Rakefile +17 -0
- data/bench.rb +63 -0
- data/lib/image_science.rb +339 -0
- data/quick_thumb +27 -0
- data/test/pix.png +0 -0
- data/test/test_image_science.rb +124 -0
- metadata +80 -0
data/History.txt
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
== 1.1.3 / 2007-05-30
|
2
|
+
|
3
|
+
* 2 minor enhancements:
|
4
|
+
* Added quick_thumb as an example to look at.
|
5
|
+
* Error handler doesn't raise by default. Raises if $DEBUG==true.
|
6
|
+
|
7
|
+
== 1.1.2 / 2007-04-18
|
8
|
+
|
9
|
+
* 2 bug fixes:
|
10
|
+
* reports bad height/width values for resize
|
11
|
+
* explicitly removes ICC color profiles from PNGs (bug in freeimage).
|
12
|
+
|
13
|
+
== 1.1.1 / 2007-03-08
|
14
|
+
|
15
|
+
* 5 minor enhancements:
|
16
|
+
* Added error handler that raises with information about what went wrong.
|
17
|
+
* thumbnail is now pure ruby, everything now uses resize.
|
18
|
+
* Produces cleaner JPEG files, with a small cost to file size/speed.
|
19
|
+
* resize now uses Catmull-Rom spline filter for better quality.
|
20
|
+
* resize copies existing ICC Profile to thumbnail, producing better color.
|
21
|
+
* ICC Profile NOT copied for PNG as it seems to be buggy.
|
22
|
+
* 1 bug fix:
|
23
|
+
* Fixed rdoc
|
24
|
+
|
25
|
+
== 1.1.0 / 2007-01-05
|
26
|
+
|
27
|
+
* 3 major enhancements:
|
28
|
+
* Added resize(width, height)
|
29
|
+
* Added save(path)
|
30
|
+
* All thumbnail and resize methods yield instead of saving directly.
|
31
|
+
* 1 minor enhancement:
|
32
|
+
* Will now try to use FreeImage from ports if /opt/local exists.
|
33
|
+
* 2 bug fixes:
|
34
|
+
* Fixed the linker issue on PPC.
|
35
|
+
* Rakefile will now clean the image files created by bench.rb
|
36
|
+
|
37
|
+
== 1.0.0 / 2006-12-01
|
38
|
+
|
39
|
+
* 1 major enhancement
|
40
|
+
* Birthday!
|
41
|
+
|
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
ImageScience
|
2
|
+
http://seattlerb.rubyforge.org/ImageScience.html
|
3
|
+
http://rubyforge.org/projects/seattlerb
|
4
|
+
by Ryan Davis
|
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-2007 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,17 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
require './lib/image_science.rb'
|
6
|
+
|
7
|
+
Hoe.new('image_science', ImageScience::VERSION) do |p|
|
8
|
+
p.rubyforge_name = 'seattlerb'
|
9
|
+
p.description = p.paragraphs_of('README.txt', 2..3).join(" ")
|
10
|
+
p.summary = p.description[/^[^.]+\./]
|
11
|
+
p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1].first.strip
|
12
|
+
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
|
13
|
+
p.extra_deps << 'RubyInline'
|
14
|
+
p.clean_globs << 'blah*png'
|
15
|
+
end
|
16
|
+
|
17
|
+
# vim: syntax=Ruby
|
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,339 @@
|
|
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.1.3'
|
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
|
+
# 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
|
+
# converts the image to 8 bits , note DOES NOT YIELD
|
54
|
+
#
|
55
|
+
def convert_8_bits;
|
56
|
+
end
|
57
|
+
|
58
|
+
##
|
59
|
+
# converts the image to 24 bits note DOES NOT YIELD
|
60
|
+
#
|
61
|
+
def convert_24_bits;
|
62
|
+
end
|
63
|
+
|
64
|
+
##
|
65
|
+
# converts the image to 32 bits ( including alpha chanel for compisiting) note DOES NOT YIELD
|
66
|
+
#
|
67
|
+
def convert_32_bits;
|
68
|
+
end
|
69
|
+
|
70
|
+
##
|
71
|
+
#
|
72
|
+
#
|
73
|
+
def paste;
|
74
|
+
end
|
75
|
+
|
76
|
+
#
|
77
|
+
# make sure your foreground image is 32 bit and your background image is 24 bit, and both images are the same size note DOES NOT YIELD
|
78
|
+
#
|
79
|
+
def composite;
|
80
|
+
end
|
81
|
+
|
82
|
+
##
|
83
|
+
# Creates a proportional thumbnail of the image scaled so its longest
|
84
|
+
# edge is resized to +size+ and yields the new image.
|
85
|
+
|
86
|
+
def thumbnail(size) # :yields: image
|
87
|
+
w, h = width, height
|
88
|
+
scale = size.to_f / (w > h ? w : h)
|
89
|
+
|
90
|
+
self.resize((w * scale).to_i, (h * scale).to_i) do |image|
|
91
|
+
yield image
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
##
|
96
|
+
# Creates a square thumbnail of the image cropping the longest edge
|
97
|
+
# to match the shortest edge, resizes to +size+, and yields the new
|
98
|
+
# image.
|
99
|
+
|
100
|
+
def cropped_thumbnail(size) # :yields: image
|
101
|
+
w, h = width, height
|
102
|
+
l, t, r, b, half = 0, 0, w, h, (w - h).abs / 2
|
103
|
+
|
104
|
+
l, r = half, half + h if w > h
|
105
|
+
t, b = half, half + w if h > w
|
106
|
+
|
107
|
+
with_crop(l, t, r, b) do |img|
|
108
|
+
img.thumbnail(size) do |thumb|
|
109
|
+
yield thumb
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
inline do |builder|
|
115
|
+
if test ?d, "/opt/local" then
|
116
|
+
builder.add_compile_flags "-I/opt/local/include"
|
117
|
+
builder.add_link_flags "-L/opt/local/lib"
|
118
|
+
end
|
119
|
+
|
120
|
+
builder.add_link_flags "-lfreeimage"
|
121
|
+
builder.add_link_flags "-lstdc++" # only needed on PPC for some reason. lame
|
122
|
+
builder.include '"FreeImage.h"'
|
123
|
+
|
124
|
+
builder.prefix <<-"END"
|
125
|
+
#define GET_BITMAP(name) FIBITMAP *(name); Data_Get_Struct(self, FIBITMAP, (name)); if (!(name)) rb_raise(rb_eTypeError, "Bitmap has already been freed")
|
126
|
+
END
|
127
|
+
|
128
|
+
builder.prefix <<-"END"
|
129
|
+
VALUE unload(VALUE self) {
|
130
|
+
GET_BITMAP(bitmap);
|
131
|
+
|
132
|
+
FreeImage_Unload(bitmap);
|
133
|
+
DATA_PTR(self) = NULL;
|
134
|
+
return Qnil;
|
135
|
+
}
|
136
|
+
END
|
137
|
+
|
138
|
+
builder.prefix <<-"END"
|
139
|
+
VALUE wrap(FIBITMAP *image, VALUE self, FREE_IMAGE_FORMAT fif) {
|
140
|
+
VALUE klass = fif ? self : CLASS_OF(self);
|
141
|
+
VALUE type = fif ? INT2FIX(fif) : rb_iv_get(self, "@file_type");
|
142
|
+
VALUE obj = Data_Wrap_Struct(klass, NULL, NULL, image);
|
143
|
+
rb_iv_set(obj, "@file_type", type);
|
144
|
+
return obj;
|
145
|
+
}
|
146
|
+
|
147
|
+
VALUE wrap_and_yield(FIBITMAP *image, VALUE self, FREE_IMAGE_FORMAT fif) {
|
148
|
+
VALUE obj = wrap(image, self, fif);
|
149
|
+
return rb_ensure(rb_yield, obj, unload, obj);
|
150
|
+
}
|
151
|
+
END
|
152
|
+
|
153
|
+
builder.prefix <<-"END"
|
154
|
+
void copy_icc_profile(VALUE self, FIBITMAP *from, FIBITMAP *to) {
|
155
|
+
FREE_IMAGE_FORMAT fif = FIX2INT(rb_iv_get(self, "@file_type"));
|
156
|
+
if (fif != FIF_PNG && FreeImage_FIFSupportsICCProfiles(fif)) {
|
157
|
+
FIICCPROFILE *profile = FreeImage_GetICCProfile(from);
|
158
|
+
if (profile && profile->data) {
|
159
|
+
FreeImage_CreateICCProfile(to, profile->data, profile->size);
|
160
|
+
}
|
161
|
+
}
|
162
|
+
}
|
163
|
+
END
|
164
|
+
|
165
|
+
builder.prefix <<-"END"
|
166
|
+
void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message) {
|
167
|
+
if (! RTEST(ruby_debug)) return;
|
168
|
+
rb_raise(rb_eRuntimeError,
|
169
|
+
"FreeImage exception for type %s: %s",
|
170
|
+
(fif == FIF_UNKNOWN) ? "???" : FreeImage_GetFormatFromFIF(fif),
|
171
|
+
message);
|
172
|
+
}
|
173
|
+
END
|
174
|
+
|
175
|
+
builder.add_to_init "FreeImage_SetOutputMessage(FreeImageErrorHandler);"
|
176
|
+
|
177
|
+
builder.c_singleton <<-"END"
|
178
|
+
VALUE with_image(char * input) {
|
179
|
+
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
|
180
|
+
|
181
|
+
fif = FreeImage_GetFileType(input, 0);
|
182
|
+
if (fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(input);
|
183
|
+
if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) {
|
184
|
+
FIBITMAP *bitmap;
|
185
|
+
VALUE result = Qnil;
|
186
|
+
int flags = fif == FIF_JPEG ? JPEG_ACCURATE : 0;
|
187
|
+
if (bitmap = FreeImage_Load(fif, input, flags)) {
|
188
|
+
result = wrap_and_yield(bitmap, self, fif);
|
189
|
+
}
|
190
|
+
return result;
|
191
|
+
}
|
192
|
+
rb_raise(rb_eTypeError, "Unknown file format");
|
193
|
+
}
|
194
|
+
END
|
195
|
+
|
196
|
+
builder.c <<-"END"
|
197
|
+
VALUE with_crop(int l, int t, int r, int b) {
|
198
|
+
FIBITMAP *copy;
|
199
|
+
VALUE result = Qnil;
|
200
|
+
GET_BITMAP(bitmap);
|
201
|
+
|
202
|
+
if (copy = FreeImage_Copy(bitmap, l, t, r, b)) {
|
203
|
+
copy_icc_profile(self, bitmap, copy);
|
204
|
+
result = wrap_and_yield(copy, self, 0);
|
205
|
+
}
|
206
|
+
return result;
|
207
|
+
}
|
208
|
+
END
|
209
|
+
|
210
|
+
builder.c <<-"END"
|
211
|
+
VALUE convert_to_8_bits()) {
|
212
|
+
FIBITMAP *copy;
|
213
|
+
VALUE result = Qnil;
|
214
|
+
GET_BITMAP(bitmap);
|
215
|
+
|
216
|
+
if (copy = FreeImage_ConvertTo8Bits(bitmap)) {
|
217
|
+
copy_icc_profile(self, bitmap, copy);
|
218
|
+
result = wrap(copy, self, 0);
|
219
|
+
}
|
220
|
+
return result;
|
221
|
+
}
|
222
|
+
END
|
223
|
+
|
224
|
+
builder.c <<-"END"
|
225
|
+
VALUE convert_to_24_bits()) {
|
226
|
+
FIBITMAP *copy;
|
227
|
+
VALUE result = Qnil;
|
228
|
+
GET_BITMAP(bitmap);
|
229
|
+
|
230
|
+
if (copy = FreeImage_ConvertTo24Bits(bitmap)) {
|
231
|
+
copy_icc_profile(self, bitmap, copy);
|
232
|
+
result = wrap(copy, self, 0);
|
233
|
+
}
|
234
|
+
return result;
|
235
|
+
}
|
236
|
+
END
|
237
|
+
|
238
|
+
builder.c <<-"END"
|
239
|
+
VALUE convert_to_32_bits()) {
|
240
|
+
FIBITMAP *copy;
|
241
|
+
VALUE result = Qnil;
|
242
|
+
GET_BITMAP(bitmap);
|
243
|
+
|
244
|
+
if (copy = FreeImage_ConvertTo32Bits(bitmap)) {
|
245
|
+
copy_icc_profile(self, bitmap, copy);
|
246
|
+
result = wrap(copy, self, 0);
|
247
|
+
}
|
248
|
+
return result;
|
249
|
+
}
|
250
|
+
END
|
251
|
+
|
252
|
+
builder.c <<-"END"
|
253
|
+
VALUE paste(VALUE src, int l, int t, int a) {
|
254
|
+
FIBITMAP *src_bitmap;
|
255
|
+
GET_BITMAP(bitmap);
|
256
|
+
Data_Get_Struct(src, FIBITMAP, src_bitmap);
|
257
|
+
|
258
|
+
if (FreeImage_Paste(bitmap, src_bitmap, l, t, a))
|
259
|
+
{
|
260
|
+
return Qtrue;
|
261
|
+
}
|
262
|
+
else
|
263
|
+
{
|
264
|
+
return Qfalse;
|
265
|
+
}
|
266
|
+
}
|
267
|
+
END
|
268
|
+
|
269
|
+
builder.c <<-"END"
|
270
|
+
VALUE composite(VALUE use_file_background, VALUE background_color, VALUE background_image) {
|
271
|
+
VALUE result = Qnil;
|
272
|
+
BOOL useFileBackground;
|
273
|
+
RGBQUAD *backgroundColor = NULL;
|
274
|
+
FIBITMAP *backgroundImage = NULL;
|
275
|
+
FIBITMAP *compositeImage = NULL;
|
276
|
+
GET_BITMAP(bitmap);
|
277
|
+
|
278
|
+
useFileBackground = (use_file_background == Qtrue) ? TRUE: FALSE;
|
279
|
+
|
280
|
+
if (background_color != Qnil)
|
281
|
+
Data_Get_Struct(background_color, RGBQUAD, backgroundColor);
|
282
|
+
|
283
|
+
if (background_image != Qnil)
|
284
|
+
Data_Get_Struct(background_image, FIBITMAP, backgroundImage);
|
285
|
+
|
286
|
+
if (compositeImage = FreeImage_Composite(bitmap, useFileBackground, backgroundColor, backgroundImage)) {
|
287
|
+
copy_icc_profile(self, bitmap, compositeImage);
|
288
|
+
result = wrap(compositeImage, self, 0);
|
289
|
+
}
|
290
|
+
return result;
|
291
|
+
}
|
292
|
+
END
|
293
|
+
|
294
|
+
builder.c <<-"END"
|
295
|
+
int height() {
|
296
|
+
GET_BITMAP(bitmap);
|
297
|
+
|
298
|
+
return FreeImage_GetHeight(bitmap);
|
299
|
+
}
|
300
|
+
END
|
301
|
+
|
302
|
+
builder.c <<-"END"
|
303
|
+
int width() {
|
304
|
+
GET_BITMAP(bitmap);
|
305
|
+
|
306
|
+
return FreeImage_GetWidth(bitmap);
|
307
|
+
}
|
308
|
+
END
|
309
|
+
|
310
|
+
builder.c <<-"END"
|
311
|
+
VALUE resize(long w, long h) {
|
312
|
+
FIBITMAP *image;
|
313
|
+
if (w <= 0) rb_raise(rb_eArgError, "Width <= 0");
|
314
|
+
if (h <= 0) rb_raise(rb_eArgError, "Height <= 0");
|
315
|
+
GET_BITMAP(bitmap);
|
316
|
+
image = FreeImage_Rescale(bitmap, w, h, FILTER_CATMULLROM);
|
317
|
+
if (image) {
|
318
|
+
copy_icc_profile(self, bitmap, image);
|
319
|
+
return wrap_and_yield(image, self, 0);
|
320
|
+
}
|
321
|
+
return Qnil;
|
322
|
+
}
|
323
|
+
END
|
324
|
+
|
325
|
+
builder.c <<-"END"
|
326
|
+
VALUE save(char * output) {
|
327
|
+
FREE_IMAGE_FORMAT fif = FreeImage_GetFIFFromFilename(output);
|
328
|
+
if (fif == FIF_UNKNOWN) fif = FIX2INT(rb_iv_get(self, "@file_type"));
|
329
|
+
if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsWriting(fif)) {
|
330
|
+
int flags = fif == FIF_JPEG ? JPEG_QUALITYSUPERB : 0;
|
331
|
+
GET_BITMAP(bitmap);
|
332
|
+
if (fif == FIF_PNG) FreeImage_DestroyICCProfile(bitmap);
|
333
|
+
return FreeImage_Save(fif, bitmap, output, flags) ? Qtrue : Qfalse;
|
334
|
+
}
|
335
|
+
rb_raise(rb_eTypeError, "Unknown file format");
|
336
|
+
}
|
337
|
+
END
|
338
|
+
end
|
339
|
+
end
|
data/quick_thumb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/local/bin/ruby -w
|
2
|
+
|
3
|
+
abort "#{File.basename $0} max_length files..." unless ARGV.size > 1
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
require 'image_science'
|
7
|
+
|
8
|
+
max_length = ARGV.shift.to_i
|
9
|
+
|
10
|
+
ARGV.each do |file|
|
11
|
+
begin
|
12
|
+
result = ImageScience.with_image file do |img|
|
13
|
+
begin
|
14
|
+
img.thumbnail(max_length) do |thumb|
|
15
|
+
# add _thumb and switch from gif to png. Really. gif just sucks.
|
16
|
+
out = file.sub(/(\.[^\.]+)$/, '_thumb\1').sub(/gif$/, 'png')
|
17
|
+
thumb.save(out)
|
18
|
+
end
|
19
|
+
rescue => e
|
20
|
+
warn "Exception thumbnailing #{file}: #{e}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
p file => result
|
24
|
+
rescue => e
|
25
|
+
warn "Exception opening #{file}: #{e}"
|
26
|
+
end
|
27
|
+
end
|
data/test/pix.png
ADDED
Binary file
|
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'test/unit/testcase'
|
2
|
+
require 'test/unit' if $0 == __FILE__
|
3
|
+
require 'image_science'
|
4
|
+
|
5
|
+
class TestImageScience < Test::Unit::TestCase
|
6
|
+
def deny x; assert ! x; end
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@path = 'test/pix.png'
|
10
|
+
@tmppath = 'test/pix-tmp.png'
|
11
|
+
@h = @w = 50
|
12
|
+
end
|
13
|
+
|
14
|
+
def teardown
|
15
|
+
File.unlink @tmppath if File.exist? @tmppath
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_class_with_image
|
19
|
+
ImageScience.with_image @path do |img|
|
20
|
+
assert_kind_of ImageScience, img
|
21
|
+
assert_equal @h, img.height
|
22
|
+
assert_equal @w, img.width
|
23
|
+
assert img.save(@tmppath)
|
24
|
+
end
|
25
|
+
|
26
|
+
assert File.exists?(@tmppath)
|
27
|
+
|
28
|
+
ImageScience.with_image @tmppath do |img|
|
29
|
+
assert_kind_of ImageScience, img
|
30
|
+
assert_equal @h, img.height
|
31
|
+
assert_equal @w, img.width
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_class_with_image_missing
|
36
|
+
assert_raises TypeError do
|
37
|
+
ImageScience.with_image @path + "nope" do |img|
|
38
|
+
flunk
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_class_with_image_missing_with_img_extension
|
44
|
+
assert_nil ImageScience.with_image("nope#{@path}") do |img|
|
45
|
+
flunk
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_resize
|
50
|
+
ImageScience.with_image @path do |img|
|
51
|
+
img.resize(25, 25) do |thumb|
|
52
|
+
assert thumb.save(@tmppath)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
assert File.exists?(@tmppath)
|
57
|
+
|
58
|
+
ImageScience.with_image @tmppath do |img|
|
59
|
+
assert_kind_of ImageScience, img
|
60
|
+
assert_equal 25, img.height
|
61
|
+
assert_equal 25, img.width
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_resize_floats
|
66
|
+
ImageScience.with_image @path do |img|
|
67
|
+
img.resize(25.2, 25.7) do |thumb|
|
68
|
+
assert thumb.save(@tmppath)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
assert File.exists?(@tmppath)
|
73
|
+
|
74
|
+
ImageScience.with_image @tmppath do |img|
|
75
|
+
assert_kind_of ImageScience, img
|
76
|
+
assert_equal 25, img.height
|
77
|
+
assert_equal 25, img.width
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_resize_zero
|
82
|
+
assert_raises ArgumentError do
|
83
|
+
ImageScience.with_image @path do |img|
|
84
|
+
img.resize(0, 25) do |thumb|
|
85
|
+
assert thumb.save(@tmppath)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
deny File.exists?(@tmppath)
|
91
|
+
|
92
|
+
assert_raises ArgumentError do
|
93
|
+
ImageScience.with_image @path do |img|
|
94
|
+
img.resize(25, 0) do |thumb|
|
95
|
+
assert thumb.save(@tmppath)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
deny File.exists?(@tmppath)
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_resize_negative
|
104
|
+
assert_raises ArgumentError do
|
105
|
+
ImageScience.with_image @path do |img|
|
106
|
+
img.resize(-25, 25) do |thumb|
|
107
|
+
assert thumb.save(@tmppath)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
deny File.exists?(@tmppath)
|
113
|
+
|
114
|
+
assert_raises ArgumentError do
|
115
|
+
ImageScience.with_image @path do |img|
|
116
|
+
img.resize(25, -25) do |thumb|
|
117
|
+
assert thumb.save(@tmppath)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
deny File.exists?(@tmppath)
|
123
|
+
end
|
124
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: carlosnz-image_science
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Davis
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain:
|
11
|
+
date: 2007-05-30 00:00:00 -07:00
|
12
|
+
default_executable:
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: RubyInline
|
16
|
+
version_requirement:
|
17
|
+
version_requirements: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - ">"
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.0.0
|
22
|
+
version:
|
23
|
+
- !ruby/object:Gem::Dependency
|
24
|
+
name: hoe
|
25
|
+
version_requirement:
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: 1.2.1
|
31
|
+
version:
|
32
|
+
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/
|
33
|
+
email: ryand-ruby@zenspider.com
|
34
|
+
executables: []
|
35
|
+
|
36
|
+
extensions: []
|
37
|
+
|
38
|
+
extra_rdoc_files:
|
39
|
+
- History.txt
|
40
|
+
- Manifest.txt
|
41
|
+
- README.txt
|
42
|
+
files:
|
43
|
+
- History.txt
|
44
|
+
- Manifest.txt
|
45
|
+
- README.txt
|
46
|
+
- Rakefile
|
47
|
+
- bench.rb
|
48
|
+
- lib/image_science.rb
|
49
|
+
- quick_thumb
|
50
|
+
- test/pix.png
|
51
|
+
- test/test_image_science.rb
|
52
|
+
has_rdoc: true
|
53
|
+
homepage: http://seattlerb.rubyforge.org/ImageScience.html
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options:
|
56
|
+
- --main
|
57
|
+
- README.txt
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">"
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 0.0.0
|
65
|
+
version:
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
version:
|
72
|
+
requirements: []
|
73
|
+
|
74
|
+
rubyforge_project: seattlerb
|
75
|
+
rubygems_version: 1.2.0
|
76
|
+
signing_key:
|
77
|
+
specification_version: 1
|
78
|
+
summary: ImageScience is a clean and happy Ruby library that generates thumbnails -- and kicks the living crap out of RMagick.
|
79
|
+
test_files:
|
80
|
+
- test/test_image_science.rb
|