stateless-systems-image_science 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +76 -0
- data/Manifest.txt +9 -0
- data/README.txt +66 -0
- data/Rakefile +17 -0
- data/bench.rb +63 -0
- data/bin/image_science_thumb +31 -0
- data/lib/image_science.rb +281 -0
- data/test/pix.png +0 -0
- data/test/test_image_science.rb +158 -0
- metadata +75 -0
data/History.txt
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
== 1.2.1 / 2009-07-01
|
2
|
+
|
3
|
+
* 1 bug fix:
|
4
|
+
|
5
|
+
* Corrupt images won't raise exceptions, inline with 1.1.3 functionality.
|
6
|
+
|
7
|
+
== 1.2.0 / 2009-06-23
|
8
|
+
|
9
|
+
* 7 minor enhancements:
|
10
|
+
|
11
|
+
* Moved quick_thumb to bin/image_science_thumb and properly added.
|
12
|
+
* Added -s (square) flag to bin/image_science_thumb
|
13
|
+
* Added autorotating on image load. (choonkeat)
|
14
|
+
* Added ruby_inline to clean globs
|
15
|
+
* Added with_image_from_memory. (sumbach)
|
16
|
+
* Switched to minitest.
|
17
|
+
* Updated rakefile for now hoe capabilities.
|
18
|
+
|
19
|
+
* 3 bug fixes:
|
20
|
+
|
21
|
+
* Check and convert to 24 BPP if save type is jpg. Caused by 32bpp png to jpg.
|
22
|
+
* Fixed 1.9isms
|
23
|
+
* Fixed BMP support. Tweaked whitespace.
|
24
|
+
|
25
|
+
== 1.1.3 / 2007-05-30
|
26
|
+
|
27
|
+
* 2 minor enhancements:
|
28
|
+
|
29
|
+
* Added quick_thumb as an example to look at.
|
30
|
+
* Error handler doesn't raise by default. Raises if $DEBUG==true.
|
31
|
+
|
32
|
+
== 1.1.2 / 2007-04-18
|
33
|
+
|
34
|
+
* 2 bug fixes:
|
35
|
+
|
36
|
+
* reports bad height/width values for resize
|
37
|
+
* explicitly removes ICC color profiles from PNGs (bug in freeimage).
|
38
|
+
|
39
|
+
== 1.1.1 / 2007-03-08
|
40
|
+
|
41
|
+
* 5 minor enhancements:
|
42
|
+
|
43
|
+
* Added error handler that raises with information about what went wrong.
|
44
|
+
* thumbnail is now pure ruby, everything now uses resize.
|
45
|
+
* Produces cleaner JPEG files, with a small cost to file size/speed.
|
46
|
+
* resize now uses Catmull-Rom spline filter for better quality.
|
47
|
+
* resize copies existing ICC Profile to thumbnail, producing better color.
|
48
|
+
* ICC Profile NOT copied for PNG as it seems to be buggy.
|
49
|
+
|
50
|
+
* 1 bug fix:
|
51
|
+
|
52
|
+
* Fixed rdoc
|
53
|
+
|
54
|
+
== 1.1.0 / 2007-01-05
|
55
|
+
|
56
|
+
* 3 major enhancements:
|
57
|
+
|
58
|
+
* Added resize(width, height)
|
59
|
+
* Added save(path)
|
60
|
+
* All thumbnail and resize methods yield instead of saving directly.
|
61
|
+
|
62
|
+
* 1 minor enhancement:
|
63
|
+
|
64
|
+
* Will now try to use FreeImage from ports if /opt/local exists.
|
65
|
+
|
66
|
+
* 2 bug fixes:
|
67
|
+
|
68
|
+
* Fixed the linker issue on PPC.
|
69
|
+
* Rakefile will now clean the image files created by bench.rb
|
70
|
+
|
71
|
+
== 1.0.0 / 2006-12-01
|
72
|
+
|
73
|
+
* 1 major enhancement
|
74
|
+
|
75
|
+
* Birthday!
|
76
|
+
|
data/Manifest.txt
ADDED
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,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
|
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,281 @@
|
|
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.1'
|
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
|
+
# Crops an image to +left+, +top+, +right+, and +bottom+ and then
|
30
|
+
# yields the new image.
|
31
|
+
|
32
|
+
def with_crop(left, top, right, bottom) # :yields: image
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Returns the width of the image, in pixels.
|
37
|
+
|
38
|
+
def width; end
|
39
|
+
|
40
|
+
##
|
41
|
+
# Returns the height of the image, in pixels.
|
42
|
+
|
43
|
+
def height; end
|
44
|
+
|
45
|
+
##
|
46
|
+
# Saves the image out to +path+. Changing the file extension will
|
47
|
+
# convert the file type to the appropriate format.
|
48
|
+
|
49
|
+
def save(path); end
|
50
|
+
|
51
|
+
##
|
52
|
+
# Resizes the image to +width+ and +height+ using a cubic-bspline
|
53
|
+
# filter and yields the new image.
|
54
|
+
|
55
|
+
def resize(width, height) # :yields: image
|
56
|
+
end
|
57
|
+
|
58
|
+
##
|
59
|
+
# Creates a proportional thumbnail of the image scaled so its longest
|
60
|
+
# edge is resized to +size+ and yields the new image.
|
61
|
+
|
62
|
+
def thumbnail(size) # :yields: image
|
63
|
+
w, h = width, height
|
64
|
+
scale = size.to_f / (w > h ? w : h)
|
65
|
+
|
66
|
+
self.resize((w * scale).to_i, (h * scale).to_i) do |image|
|
67
|
+
yield image
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
##
|
72
|
+
# Creates a square thumbnail of the image cropping the longest edge
|
73
|
+
# to match the shortest edge, resizes to +size+, and yields the new
|
74
|
+
# image.
|
75
|
+
|
76
|
+
def cropped_thumbnail(size) # :yields: image
|
77
|
+
w, h = width, height
|
78
|
+
l, t, r, b, half = 0, 0, w, h, (w - h).abs / 2
|
79
|
+
|
80
|
+
l, r = half, half + h if w > h
|
81
|
+
t, b = half, half + w if h > w
|
82
|
+
|
83
|
+
with_crop(l, t, r, b) do |img|
|
84
|
+
img.thumbnail(size) do |thumb|
|
85
|
+
yield thumb
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
inline do |builder|
|
91
|
+
if test ?d, "/opt/local" then
|
92
|
+
builder.add_compile_flags "-I/opt/local/include"
|
93
|
+
builder.add_link_flags "-L/opt/local/lib"
|
94
|
+
end
|
95
|
+
|
96
|
+
builder.add_link_flags "-lfreeimage"
|
97
|
+
builder.add_link_flags "-lstdc++" # only needed on PPC for some reason. lame
|
98
|
+
builder.include '"FreeImage.h"'
|
99
|
+
|
100
|
+
builder.prefix <<-"END"
|
101
|
+
#define GET_BITMAP(name) FIBITMAP *(name); Data_Get_Struct(self, FIBITMAP, (name)); if (!(name)) rb_raise(rb_eTypeError, "Bitmap has already been freed")
|
102
|
+
END
|
103
|
+
|
104
|
+
builder.prefix <<-"END"
|
105
|
+
VALUE unload(VALUE self) {
|
106
|
+
GET_BITMAP(bitmap);
|
107
|
+
|
108
|
+
FreeImage_Unload(bitmap);
|
109
|
+
DATA_PTR(self) = NULL;
|
110
|
+
return Qnil;
|
111
|
+
}
|
112
|
+
END
|
113
|
+
|
114
|
+
builder.prefix <<-"END"
|
115
|
+
VALUE wrap_and_yield(FIBITMAP *image, VALUE self, FREE_IMAGE_FORMAT fif) {
|
116
|
+
unsigned int self_is_class = rb_type(self) == T_CLASS;
|
117
|
+
VALUE klass = self_is_class ? self : CLASS_OF(self);
|
118
|
+
VALUE type = self_is_class ? INT2FIX(fif) : rb_iv_get(self, "@file_type");
|
119
|
+
VALUE obj = Data_Wrap_Struct(klass, NULL, NULL, image);
|
120
|
+
rb_iv_set(obj, "@file_type", type);
|
121
|
+
return rb_ensure(rb_yield, obj, unload, obj);
|
122
|
+
}
|
123
|
+
END
|
124
|
+
|
125
|
+
builder.prefix <<-"END"
|
126
|
+
void copy_icc_profile(VALUE self, FIBITMAP *from, FIBITMAP *to) {
|
127
|
+
FREE_IMAGE_FORMAT fif = FIX2INT(rb_iv_get(self, "@file_type"));
|
128
|
+
if (fif != FIF_PNG && FreeImage_FIFSupportsICCProfiles(fif)) {
|
129
|
+
FIICCPROFILE *profile = FreeImage_GetICCProfile(from);
|
130
|
+
if (profile && profile->data) {
|
131
|
+
FreeImage_CreateICCProfile(to, profile->data, profile->size);
|
132
|
+
}
|
133
|
+
}
|
134
|
+
}
|
135
|
+
END
|
136
|
+
|
137
|
+
builder.prefix <<-"END"
|
138
|
+
void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message) {
|
139
|
+
if (! RTEST(ruby_debug)) return;
|
140
|
+
rb_raise(rb_eRuntimeError,
|
141
|
+
"FreeImage exception for type %s: %s",
|
142
|
+
(fif == FIF_UNKNOWN) ? "???" : FreeImage_GetFormatFromFIF(fif),
|
143
|
+
message);
|
144
|
+
}
|
145
|
+
END
|
146
|
+
|
147
|
+
builder.add_to_init "FreeImage_SetOutputMessage(FreeImageErrorHandler);"
|
148
|
+
|
149
|
+
builder.c_singleton <<-"END"
|
150
|
+
VALUE with_image(char * input) {
|
151
|
+
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
|
152
|
+
|
153
|
+
fif = FreeImage_GetFileType(input, 0);
|
154
|
+
if (fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(input);
|
155
|
+
if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) {
|
156
|
+
FIBITMAP *bitmap;
|
157
|
+
VALUE result = Qnil;
|
158
|
+
int flags = fif == FIF_JPEG ? JPEG_ACCURATE : 0;
|
159
|
+
if (bitmap = FreeImage_Load(fif, input, flags)) {
|
160
|
+
FITAG *tagValue = NULL;
|
161
|
+
FreeImage_GetMetadata(FIMD_EXIF_MAIN, bitmap, "Orientation", &tagValue);
|
162
|
+
switch (tagValue == NULL ? 0 : *((short *) FreeImage_GetTagValue(tagValue))) {
|
163
|
+
case 6:
|
164
|
+
bitmap = FreeImage_RotateClassic(bitmap, 270);
|
165
|
+
break;
|
166
|
+
case 3:
|
167
|
+
bitmap = FreeImage_RotateClassic(bitmap, 180);
|
168
|
+
break;
|
169
|
+
case 8:
|
170
|
+
bitmap = FreeImage_RotateClassic(bitmap, 90);
|
171
|
+
break;
|
172
|
+
default:
|
173
|
+
break;
|
174
|
+
}
|
175
|
+
|
176
|
+
result = wrap_and_yield(bitmap, self, fif);
|
177
|
+
}
|
178
|
+
return result;
|
179
|
+
}
|
180
|
+
rb_raise(rb_eTypeError, "Unknown file format");
|
181
|
+
}
|
182
|
+
END
|
183
|
+
|
184
|
+
builder.c_singleton <<-"END"
|
185
|
+
VALUE with_image_from_memory(VALUE image_data) {
|
186
|
+
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
|
187
|
+
|
188
|
+
Check_Type(image_data, T_STRING);
|
189
|
+
BYTE *image_data_ptr = (BYTE*)RSTRING_PTR(image_data);
|
190
|
+
DWORD image_data_length = RSTRING_LEN(image_data);
|
191
|
+
FIMEMORY *stream = FreeImage_OpenMemory(image_data_ptr, image_data_length);
|
192
|
+
|
193
|
+
if (NULL == stream) {
|
194
|
+
rb_raise(rb_eTypeError, "Unable to open image_data");
|
195
|
+
}
|
196
|
+
|
197
|
+
fif = FreeImage_GetFileTypeFromMemory(stream, 0);
|
198
|
+
if ((fif == FIF_UNKNOWN) || !FreeImage_FIFSupportsReading(fif)) {
|
199
|
+
rb_raise(rb_eTypeError, "Unknown file format");
|
200
|
+
}
|
201
|
+
|
202
|
+
FIBITMAP *bitmap = NULL;
|
203
|
+
VALUE result = Qnil;
|
204
|
+
int flags = fif == FIF_JPEG ? JPEG_ACCURATE : 0;
|
205
|
+
bitmap = FreeImage_LoadFromMemory(fif, stream, flags);
|
206
|
+
FreeImage_CloseMemory(stream);
|
207
|
+
if (bitmap) {
|
208
|
+
result = wrap_and_yield(bitmap, self, fif);
|
209
|
+
}
|
210
|
+
return result;
|
211
|
+
}
|
212
|
+
END
|
213
|
+
|
214
|
+
builder.c <<-"END"
|
215
|
+
VALUE with_crop(int l, int t, int r, int b) {
|
216
|
+
FIBITMAP *copy;
|
217
|
+
VALUE result = Qnil;
|
218
|
+
GET_BITMAP(bitmap);
|
219
|
+
|
220
|
+
if (copy = FreeImage_Copy(bitmap, l, t, r, b)) {
|
221
|
+
copy_icc_profile(self, bitmap, copy);
|
222
|
+
result = wrap_and_yield(copy, self, 0);
|
223
|
+
}
|
224
|
+
return result;
|
225
|
+
}
|
226
|
+
END
|
227
|
+
|
228
|
+
builder.c <<-"END"
|
229
|
+
int height() {
|
230
|
+
GET_BITMAP(bitmap);
|
231
|
+
|
232
|
+
return FreeImage_GetHeight(bitmap);
|
233
|
+
}
|
234
|
+
END
|
235
|
+
|
236
|
+
builder.c <<-"END"
|
237
|
+
int width() {
|
238
|
+
GET_BITMAP(bitmap);
|
239
|
+
|
240
|
+
return FreeImage_GetWidth(bitmap);
|
241
|
+
}
|
242
|
+
END
|
243
|
+
|
244
|
+
builder.c <<-"END"
|
245
|
+
VALUE resize(long w, long h) {
|
246
|
+
if (w <= 0) rb_raise(rb_eArgError, "Width <= 0");
|
247
|
+
if (h <= 0) rb_raise(rb_eArgError, "Height <= 0");
|
248
|
+
GET_BITMAP(bitmap);
|
249
|
+
FIBITMAP *image = FreeImage_Rescale(bitmap, w, h, FILTER_CATMULLROM);
|
250
|
+
if (image) {
|
251
|
+
copy_icc_profile(self, bitmap, image);
|
252
|
+
return wrap_and_yield(image, self, 0);
|
253
|
+
}
|
254
|
+
return Qnil;
|
255
|
+
}
|
256
|
+
END
|
257
|
+
|
258
|
+
builder.c <<-"END"
|
259
|
+
VALUE save(char * output) {
|
260
|
+
FREE_IMAGE_FORMAT fif = FreeImage_GetFIFFromFilename(output);
|
261
|
+
if (fif == FIF_UNKNOWN) fif = FIX2INT(rb_iv_get(self, "@file_type"));
|
262
|
+
if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsWriting(fif)) {
|
263
|
+
GET_BITMAP(bitmap);
|
264
|
+
int flags = fif == FIF_JPEG ? JPEG_QUALITYSUPERB : 0;
|
265
|
+
BOOL result = 0, unload = 0;
|
266
|
+
|
267
|
+
if (fif == FIF_PNG) FreeImage_DestroyICCProfile(bitmap);
|
268
|
+
if (fif == FIF_JPEG && FreeImage_GetBPP(bitmap) != 24)
|
269
|
+
bitmap = FreeImage_ConvertTo24Bits(bitmap), unload = 1; // sue me
|
270
|
+
|
271
|
+
result = FreeImage_Save(fif, bitmap, output, flags);
|
272
|
+
|
273
|
+
if (unload) FreeImage_Unload(bitmap);
|
274
|
+
|
275
|
+
return result ? Qtrue : Qfalse;
|
276
|
+
}
|
277
|
+
rb_raise(rb_eTypeError, "Unknown file format");
|
278
|
+
}
|
279
|
+
END
|
280
|
+
end
|
281
|
+
end
|
data/test/pix.png
ADDED
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,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stateless-systems-image_science
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Davis
|
8
|
+
- Sergio Cambra
|
9
|
+
- Stateless Systems
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
|
14
|
+
date: 2009-07-01 00:00:00 -07:00
|
15
|
+
default_executable:
|
16
|
+
dependencies:
|
17
|
+
- !ruby/object:Gem::Dependency
|
18
|
+
name: hoe
|
19
|
+
type: :runtime
|
20
|
+
version_requirement:
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: 2.2.0
|
26
|
+
version:
|
27
|
+
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/
|
28
|
+
email: sergio@entrecables.com
|
29
|
+
executables:
|
30
|
+
- image_science_thumb
|
31
|
+
extensions: []
|
32
|
+
|
33
|
+
extra_rdoc_files:
|
34
|
+
- History.txt
|
35
|
+
- Manifest.txt
|
36
|
+
- README.txt
|
37
|
+
files:
|
38
|
+
- History.txt
|
39
|
+
- Manifest.txt
|
40
|
+
- README.txt
|
41
|
+
- Rakefile
|
42
|
+
- bench.rb
|
43
|
+
- bin/image_science_thumb
|
44
|
+
- lib/image_science.rb
|
45
|
+
- test/pix.png
|
46
|
+
- test/test_image_science.rb
|
47
|
+
has_rdoc: true
|
48
|
+
homepage: http://github.com/stateless-systems/image_science
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options:
|
51
|
+
- --main
|
52
|
+
- README.txt
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
version:
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project: 1.3.4
|
70
|
+
rubygems_version: 1.2.0
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: ImageScience is a clean and happy Ruby library that generates thumbnails -- and kicks the living crap out of RMagick
|
74
|
+
test_files:
|
75
|
+
- test/test_image_science.rb
|