tdd-image_science 1.2.1
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.
- data/History.txt +79 -0
- data/Manifest.txt +9 -0
- data/README.txt +71 -0
- data/Rakefile +18 -0
- data/bench.rb +63 -0
- data/bin/image_science_thumb +31 -0
- data/lib/image_science.rb +295 -0
- data/test/pix.png +0 -0
- data/test/test_image_science.rb +183 -0
- metadata +83 -0
data/History.txt
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
== 1.2.1-tdd / 2009-08-04
|
2
|
+
|
3
|
+
* 1 enhancement:
|
4
|
+
|
5
|
+
* Added an optional jpeg_quality argument to save, taken into account when
|
6
|
+
saving to JPEG, which lets you define which quality level you want for
|
7
|
+
the JPEG output. Any FreeImage JPEG_xxx constant is acceptable (they're
|
8
|
+
also present, commented, in the Ruby source).
|
9
|
+
|
10
|
+
== 1.2.0 / 2009-06-23
|
11
|
+
|
12
|
+
* 7 minor enhancements:
|
13
|
+
|
14
|
+
* Moved quick_thumb to bin/image_science_thumb and properly added.
|
15
|
+
* Added -s (square) flag to bin/image_science_thumb
|
16
|
+
* Added autorotating on image load. (choonkeat)
|
17
|
+
* Added ruby_inline to clean globs
|
18
|
+
* Added with_image_from_memory. (sumbach)
|
19
|
+
* Switched to minitest.
|
20
|
+
* Updated rakefile for now hoe capabilities.
|
21
|
+
|
22
|
+
* 3 bug fixes:
|
23
|
+
|
24
|
+
* Check and convert to 24 BPP if save type is jpg. Caused by 32bpp png to jpg.
|
25
|
+
* Fixed 1.9isms
|
26
|
+
* Fixed BMP support. Tweaked whitespace.
|
27
|
+
|
28
|
+
== 1.1.3 / 2007-05-30
|
29
|
+
|
30
|
+
* 2 minor enhancements:
|
31
|
+
|
32
|
+
* Added quick_thumb as an example to look at.
|
33
|
+
* Error handler doesn't raise by default. Raises if $DEBUG==true.
|
34
|
+
|
35
|
+
== 1.1.2 / 2007-04-18
|
36
|
+
|
37
|
+
* 2 bug fixes:
|
38
|
+
|
39
|
+
* reports bad height/width values for resize
|
40
|
+
* explicitly removes ICC color profiles from PNGs (bug in freeimage).
|
41
|
+
|
42
|
+
== 1.1.1 / 2007-03-08
|
43
|
+
|
44
|
+
* 5 minor enhancements:
|
45
|
+
|
46
|
+
* Added error handler that raises with information about what went wrong.
|
47
|
+
* thumbnail is now pure ruby, everything now uses resize.
|
48
|
+
* Produces cleaner JPEG files, with a small cost to file size/speed.
|
49
|
+
* resize now uses Catmull-Rom spline filter for better quality.
|
50
|
+
* resize copies existing ICC Profile to thumbnail, producing better color.
|
51
|
+
* ICC Profile NOT copied for PNG as it seems to be buggy.
|
52
|
+
|
53
|
+
* 1 bug fix:
|
54
|
+
|
55
|
+
* Fixed rdoc
|
56
|
+
|
57
|
+
== 1.1.0 / 2007-01-05
|
58
|
+
|
59
|
+
* 3 major enhancements:
|
60
|
+
|
61
|
+
* Added resize(width, height)
|
62
|
+
* Added save(path)
|
63
|
+
* All thumbnail and resize methods yield instead of saving directly.
|
64
|
+
|
65
|
+
* 1 minor enhancement:
|
66
|
+
|
67
|
+
* Will now try to use FreeImage from ports if /opt/local exists.
|
68
|
+
|
69
|
+
* 2 bug fixes:
|
70
|
+
|
71
|
+
* Fixed the linker issue on PPC.
|
72
|
+
* Rakefile will now clean the image files created by bench.rb
|
73
|
+
|
74
|
+
== 1.0.0 / 2006-12-01
|
75
|
+
|
76
|
+
* 1 major enhancement
|
77
|
+
|
78
|
+
* Birthday!
|
79
|
+
|
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
= ImageScience
|
2
|
+
|
3
|
+
* http://seattlerb.rubyforge.org/ImageScience.html
|
4
|
+
* http://rubyforge.org/projects/seattlerb
|
5
|
+
* http://github.com/tdd/image_science/tree/master (fork with JPEG quality patch)
|
6
|
+
|
7
|
+
== DESCRIPTION:
|
8
|
+
|
9
|
+
ImageScience is a clean and happy Ruby library that generates
|
10
|
+
thumbnails -- and kicks the living crap out of RMagick. Oh, and it
|
11
|
+
doesn't leak memory like a sieve. :)
|
12
|
+
|
13
|
+
For more information including build steps, see http://seattlerb.rubyforge.org/
|
14
|
+
|
15
|
+
== FEATURES/PROBLEMS:
|
16
|
+
|
17
|
+
* Glorious graphics manipulation magi... errr, SCIENCE! in less than 200 LoC!
|
18
|
+
* Supports square and proportional thumbnails, as well as arbitrary resizes.
|
19
|
+
* Pretty much any graphics format you could want. No really.
|
20
|
+
|
21
|
+
== SYNOPSYS:
|
22
|
+
|
23
|
+
ImageScience.with_image(file) do |img|
|
24
|
+
img.cropped_thumbnail(100) do |thumb|
|
25
|
+
thumb.save "#{file}_cropped.png"
|
26
|
+
end
|
27
|
+
|
28
|
+
img.thumbnail(100) do |thumb|
|
29
|
+
thumb.save "#{file}_thumb.png"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
The save method accepts an optional jpeg_quality argument, defaulting to JPEG_QUALITYSUPERB
|
34
|
+
(100% quality / lossless), which you can change to any of the JPEG_xxx constants in FreeImage
|
35
|
+
when saving to JPEG in order to considerably reduce file size.
|
36
|
+
|
37
|
+
== REQUIREMENTS:
|
38
|
+
|
39
|
+
* FreeImage
|
40
|
+
* ImageScience
|
41
|
+
|
42
|
+
== INSTALL:
|
43
|
+
|
44
|
+
* Download and install FreeImage. See notes at url above.
|
45
|
+
* sudo gem install -y image_science
|
46
|
+
* see http://seattlerb.rubyforge.org/ImageScience.html for more info.
|
47
|
+
|
48
|
+
== LICENSE:
|
49
|
+
|
50
|
+
(The MIT License)
|
51
|
+
|
52
|
+
Copyright (c) 2006-2009 Ryan Davis, Seattle.rb
|
53
|
+
|
54
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
55
|
+
a copy of this software and associated documentation files (the
|
56
|
+
'Software'), to deal in the Software without restriction, including
|
57
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
58
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
59
|
+
permit persons to whom the Software is furnished to do so, subject to
|
60
|
+
the following conditions:
|
61
|
+
|
62
|
+
The above copyright notice and this permission notice shall be
|
63
|
+
included in all copies or substantial portions of the Software.
|
64
|
+
|
65
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
66
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
67
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
68
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
69
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
70
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
71
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
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
|
+
developer 'Christophe Porteneuve', 'tdd@tddsworld.com'
|
12
|
+
|
13
|
+
self.rubyforge_name = 'seattlerb'
|
14
|
+
|
15
|
+
clean_globs << 'blah*png' << 'images/*_thumb.*'
|
16
|
+
end
|
17
|
+
|
18
|
+
# 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,295 @@
|
|
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.tdd'
|
15
|
+
|
16
|
+
# These actually come from FreeImage.h.
|
17
|
+
JPEG_DEFAULT = 0 # 75%
|
18
|
+
JPEG_FAST = 1
|
19
|
+
JPEG_ACCURATE = 2
|
20
|
+
JPEG_QUALITYSUPERB = 0x80 # 100%
|
21
|
+
JPEG_QUALITYGOOD = 0x100 # 75%
|
22
|
+
JPEG_QUALITYNORMAL = 0x200 # 50%
|
23
|
+
JPEG_QUALITYAVERAGE = 0x400 # 25%
|
24
|
+
JPEG_QUALITYBAD = 0x800 # 10%
|
25
|
+
JPEG_PROGRESSIVE = 0x2000 # save as a progressive-JPEG (use | to combine with other flags)
|
26
|
+
|
27
|
+
##
|
28
|
+
# The top-level image loader opens +path+ and then yields the image.
|
29
|
+
|
30
|
+
def self.with_image(path) # :yields: image
|
31
|
+
end
|
32
|
+
|
33
|
+
##
|
34
|
+
# The top-level image loader, opens an image from the string +data+ and then yields the image.
|
35
|
+
|
36
|
+
def self.with_image_from_memory(data) # :yields: image
|
37
|
+
end
|
38
|
+
|
39
|
+
##
|
40
|
+
# Crops an image to +left+, +top+, +right+, and +bottom+ and then
|
41
|
+
# yields the new image.
|
42
|
+
|
43
|
+
def with_crop(left, top, right, bottom) # :yields: image
|
44
|
+
end
|
45
|
+
|
46
|
+
##
|
47
|
+
# Returns the width of the image, in pixels.
|
48
|
+
|
49
|
+
def width; end
|
50
|
+
|
51
|
+
##
|
52
|
+
# Returns the height of the image, in pixels.
|
53
|
+
|
54
|
+
def height; end
|
55
|
+
|
56
|
+
##
|
57
|
+
# Saves the image out to +path+. Changing the file extension will
|
58
|
+
# convert the file type to the appropriate format.
|
59
|
+
|
60
|
+
def save(path, jpeg_quality = JPEG_QUALITYSUPERB)
|
61
|
+
save_with_quality(path, jpeg_quality)
|
62
|
+
end
|
63
|
+
|
64
|
+
def save_with_quality(path, jpeg_quality); end
|
65
|
+
|
66
|
+
##
|
67
|
+
# Resizes the image to +width+ and +height+ using a cubic-bspline
|
68
|
+
# filter and yields the new image.
|
69
|
+
|
70
|
+
def resize(width, height) # :yields: image
|
71
|
+
end
|
72
|
+
|
73
|
+
##
|
74
|
+
# Creates a proportional thumbnail of the image scaled so its longest
|
75
|
+
# edge is resized to +size+ and yields the new image.
|
76
|
+
|
77
|
+
def thumbnail(size) # :yields: image
|
78
|
+
w, h = width, height
|
79
|
+
scale = size.to_f / (w > h ? w : h)
|
80
|
+
|
81
|
+
self.resize((w * scale).to_i, (h * scale).to_i) do |image|
|
82
|
+
yield image
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
##
|
87
|
+
# Creates a square thumbnail of the image cropping the longest edge
|
88
|
+
# to match the shortest edge, resizes to +size+, and yields the new
|
89
|
+
# image.
|
90
|
+
|
91
|
+
def cropped_thumbnail(size) # :yields: image
|
92
|
+
w, h = width, height
|
93
|
+
l, t, r, b, half = 0, 0, w, h, (w - h).abs / 2
|
94
|
+
|
95
|
+
l, r = half, half + h if w > h
|
96
|
+
t, b = half, half + w if h > w
|
97
|
+
|
98
|
+
with_crop(l, t, r, b) do |img|
|
99
|
+
img.thumbnail(size) do |thumb|
|
100
|
+
yield thumb
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
inline do |builder|
|
106
|
+
if test ?d, "/opt/local" then
|
107
|
+
builder.add_compile_flags "-I/opt/local/include"
|
108
|
+
builder.add_link_flags "-L/opt/local/lib"
|
109
|
+
end
|
110
|
+
|
111
|
+
builder.add_link_flags "-lfreeimage"
|
112
|
+
builder.add_link_flags "-lstdc++" # only needed on PPC for some reason. lame
|
113
|
+
builder.include '"FreeImage.h"'
|
114
|
+
|
115
|
+
builder.prefix <<-"END"
|
116
|
+
#define GET_BITMAP(name) FIBITMAP *(name); Data_Get_Struct(self, FIBITMAP, (name)); if (!(name)) rb_raise(rb_eTypeError, "Bitmap has already been freed")
|
117
|
+
END
|
118
|
+
|
119
|
+
builder.prefix <<-"END"
|
120
|
+
VALUE unload(VALUE self) {
|
121
|
+
GET_BITMAP(bitmap);
|
122
|
+
|
123
|
+
FreeImage_Unload(bitmap);
|
124
|
+
DATA_PTR(self) = NULL;
|
125
|
+
return Qnil;
|
126
|
+
}
|
127
|
+
END
|
128
|
+
|
129
|
+
builder.prefix <<-"END"
|
130
|
+
VALUE wrap_and_yield(FIBITMAP *image, VALUE self, FREE_IMAGE_FORMAT fif) {
|
131
|
+
unsigned int self_is_class = rb_type(self) == T_CLASS;
|
132
|
+
VALUE klass = self_is_class ? self : CLASS_OF(self);
|
133
|
+
VALUE type = self_is_class ? INT2FIX(fif) : rb_iv_get(self, "@file_type");
|
134
|
+
VALUE obj = Data_Wrap_Struct(klass, NULL, NULL, image);
|
135
|
+
rb_iv_set(obj, "@file_type", type);
|
136
|
+
return rb_ensure(rb_yield, obj, unload, obj);
|
137
|
+
}
|
138
|
+
END
|
139
|
+
|
140
|
+
builder.prefix <<-"END"
|
141
|
+
void copy_icc_profile(VALUE self, FIBITMAP *from, FIBITMAP *to) {
|
142
|
+
FREE_IMAGE_FORMAT fif = FIX2INT(rb_iv_get(self, "@file_type"));
|
143
|
+
if (fif != FIF_PNG && FreeImage_FIFSupportsICCProfiles(fif)) {
|
144
|
+
FIICCPROFILE *profile = FreeImage_GetICCProfile(from);
|
145
|
+
if (profile && profile->data) {
|
146
|
+
FreeImage_CreateICCProfile(to, profile->data, profile->size);
|
147
|
+
}
|
148
|
+
}
|
149
|
+
}
|
150
|
+
END
|
151
|
+
|
152
|
+
builder.prefix <<-"END"
|
153
|
+
void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message) {
|
154
|
+
rb_raise(rb_eRuntimeError,
|
155
|
+
"FreeImage exception for type %s: %s",
|
156
|
+
(fif == FIF_UNKNOWN) ? "???" : FreeImage_GetFormatFromFIF(fif),
|
157
|
+
message);
|
158
|
+
}
|
159
|
+
END
|
160
|
+
|
161
|
+
builder.add_to_init "FreeImage_SetOutputMessage(FreeImageErrorHandler);"
|
162
|
+
|
163
|
+
builder.c_singleton <<-"END"
|
164
|
+
VALUE with_image(char * input) {
|
165
|
+
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
|
166
|
+
|
167
|
+
fif = FreeImage_GetFileType(input, 0);
|
168
|
+
if (fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(input);
|
169
|
+
if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) {
|
170
|
+
FIBITMAP *bitmap;
|
171
|
+
VALUE result = Qnil;
|
172
|
+
int flags = fif == FIF_JPEG ? JPEG_ACCURATE : 0;
|
173
|
+
if (bitmap = FreeImage_Load(fif, input, flags)) {
|
174
|
+
FITAG *tagValue = NULL;
|
175
|
+
FreeImage_GetMetadata(FIMD_EXIF_MAIN, bitmap, "Orientation", &tagValue);
|
176
|
+
switch (tagValue == NULL ? 0 : *((short *) FreeImage_GetTagValue(tagValue))) {
|
177
|
+
case 6:
|
178
|
+
bitmap = FreeImage_RotateClassic(bitmap, 270);
|
179
|
+
break;
|
180
|
+
case 3:
|
181
|
+
bitmap = FreeImage_RotateClassic(bitmap, 180);
|
182
|
+
break;
|
183
|
+
case 8:
|
184
|
+
bitmap = FreeImage_RotateClassic(bitmap, 90);
|
185
|
+
break;
|
186
|
+
default:
|
187
|
+
break;
|
188
|
+
}
|
189
|
+
|
190
|
+
result = wrap_and_yield(bitmap, self, fif);
|
191
|
+
}
|
192
|
+
return result;
|
193
|
+
}
|
194
|
+
rb_raise(rb_eTypeError, "Unknown file format");
|
195
|
+
}
|
196
|
+
END
|
197
|
+
|
198
|
+
builder.c_singleton <<-"END"
|
199
|
+
VALUE with_image_from_memory(VALUE image_data) {
|
200
|
+
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
|
201
|
+
|
202
|
+
Check_Type(image_data, T_STRING);
|
203
|
+
BYTE *image_data_ptr = (BYTE*)RSTRING_PTR(image_data);
|
204
|
+
DWORD image_data_length = RSTRING_LEN(image_data);
|
205
|
+
FIMEMORY *stream = FreeImage_OpenMemory(image_data_ptr, image_data_length);
|
206
|
+
|
207
|
+
if (NULL == stream) {
|
208
|
+
rb_raise(rb_eTypeError, "Unable to open image_data");
|
209
|
+
}
|
210
|
+
|
211
|
+
fif = FreeImage_GetFileTypeFromMemory(stream, 0);
|
212
|
+
if ((fif == FIF_UNKNOWN) || !FreeImage_FIFSupportsReading(fif)) {
|
213
|
+
rb_raise(rb_eTypeError, "Unknown file format");
|
214
|
+
}
|
215
|
+
|
216
|
+
FIBITMAP *bitmap = NULL;
|
217
|
+
VALUE result = Qnil;
|
218
|
+
int flags = fif == FIF_JPEG ? JPEG_ACCURATE : 0;
|
219
|
+
bitmap = FreeImage_LoadFromMemory(fif, stream, flags);
|
220
|
+
FreeImage_CloseMemory(stream);
|
221
|
+
if (bitmap) {
|
222
|
+
result = wrap_and_yield(bitmap, self, fif);
|
223
|
+
}
|
224
|
+
return result;
|
225
|
+
}
|
226
|
+
END
|
227
|
+
|
228
|
+
builder.c <<-"END"
|
229
|
+
VALUE with_crop(int l, int t, int r, int b) {
|
230
|
+
FIBITMAP *copy;
|
231
|
+
VALUE result = Qnil;
|
232
|
+
GET_BITMAP(bitmap);
|
233
|
+
|
234
|
+
if (copy = FreeImage_Copy(bitmap, l, t, r, b)) {
|
235
|
+
copy_icc_profile(self, bitmap, copy);
|
236
|
+
result = wrap_and_yield(copy, self, 0);
|
237
|
+
}
|
238
|
+
return result;
|
239
|
+
}
|
240
|
+
END
|
241
|
+
|
242
|
+
builder.c <<-"END"
|
243
|
+
int height() {
|
244
|
+
GET_BITMAP(bitmap);
|
245
|
+
|
246
|
+
return FreeImage_GetHeight(bitmap);
|
247
|
+
}
|
248
|
+
END
|
249
|
+
|
250
|
+
builder.c <<-"END"
|
251
|
+
int width() {
|
252
|
+
GET_BITMAP(bitmap);
|
253
|
+
|
254
|
+
return FreeImage_GetWidth(bitmap);
|
255
|
+
}
|
256
|
+
END
|
257
|
+
|
258
|
+
builder.c <<-"END"
|
259
|
+
VALUE resize(long w, long h) {
|
260
|
+
if (w <= 0) rb_raise(rb_eArgError, "Width <= 0");
|
261
|
+
if (h <= 0) rb_raise(rb_eArgError, "Height <= 0");
|
262
|
+
GET_BITMAP(bitmap);
|
263
|
+
FIBITMAP *image = FreeImage_Rescale(bitmap, w, h, FILTER_CATMULLROM);
|
264
|
+
if (image) {
|
265
|
+
copy_icc_profile(self, bitmap, image);
|
266
|
+
return wrap_and_yield(image, self, 0);
|
267
|
+
}
|
268
|
+
return Qnil;
|
269
|
+
}
|
270
|
+
END
|
271
|
+
|
272
|
+
builder.c <<-"END"
|
273
|
+
VALUE save_with_quality(char * output, int jpeg_quality) {
|
274
|
+
FREE_IMAGE_FORMAT fif = FreeImage_GetFIFFromFilename(output);
|
275
|
+
if (fif == FIF_UNKNOWN) fif = FIX2INT(rb_iv_get(self, "@file_type"));
|
276
|
+
if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsWriting(fif)) {
|
277
|
+
GET_BITMAP(bitmap);
|
278
|
+
int flags = fif == FIF_JPEG ? jpeg_quality : 0;
|
279
|
+
BOOL result = 0, unload = 0;
|
280
|
+
|
281
|
+
if (fif == FIF_PNG) FreeImage_DestroyICCProfile(bitmap);
|
282
|
+
if (fif == FIF_JPEG && FreeImage_GetBPP(bitmap) != 24)
|
283
|
+
bitmap = FreeImage_ConvertTo24Bits(bitmap), unload = 1; // sue me
|
284
|
+
|
285
|
+
result = FreeImage_Save(fif, bitmap, output, flags);
|
286
|
+
|
287
|
+
if (unload) FreeImage_Unload(bitmap);
|
288
|
+
|
289
|
+
return result ? Qtrue : Qfalse;
|
290
|
+
}
|
291
|
+
rb_raise(rb_eTypeError, "Unknown file format");
|
292
|
+
}
|
293
|
+
END
|
294
|
+
end
|
295
|
+
end
|
data/test/pix.png
ADDED
Binary file
|
@@ -0,0 +1,183 @@
|
|
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_save_with_quality
|
162
|
+
tmppath = 'pix-tmp.jpg'
|
163
|
+
ImageScience.with_image @path do |img|
|
164
|
+
assert img.save(tmppath)
|
165
|
+
end
|
166
|
+
|
167
|
+
assert File.exists?(tmppath)
|
168
|
+
superb_size = File.size(tmppath)
|
169
|
+
|
170
|
+
ImageScience.with_image @path do |img|
|
171
|
+
assert img.save(tmppath, ImageScience::JPEG_QUALITYNORMAL)
|
172
|
+
end
|
173
|
+
|
174
|
+
ImageScience.with_image tmppath do |img|
|
175
|
+
assert_kind_of ImageScience, img
|
176
|
+
assert File.size(tmppath) <= superb_size / 2
|
177
|
+
end
|
178
|
+
|
179
|
+
ensure
|
180
|
+
File.unlink tmppath if File.exist? tmppath
|
181
|
+
end
|
182
|
+
|
183
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tdd-image_science
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Davis
|
8
|
+
- Christophe Porteneuve
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2009-08-04 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: mime-types
|
18
|
+
type: :runtime
|
19
|
+
version_requirement:
|
20
|
+
version_requirements: !ruby/object:Gem::Requirement
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "1.15"
|
25
|
+
version:
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: diff-lcs
|
28
|
+
type: :runtime
|
29
|
+
version_requirement:
|
30
|
+
version_requirements: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 1.1.2
|
35
|
+
version:
|
36
|
+
description: "This is a fork of Ryan\xE2\x80\x99s image_science lib that adds JPEG quality control on save operations."
|
37
|
+
email: tdd@tddsworld.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files:
|
43
|
+
- README.txt
|
44
|
+
files:
|
45
|
+
- History.txt
|
46
|
+
- Manifest.txt
|
47
|
+
- README.txt
|
48
|
+
- Rakefile
|
49
|
+
- bench.rb
|
50
|
+
- bin/image_science_thumb
|
51
|
+
- lib/image_science.rb
|
52
|
+
- test/pix.png
|
53
|
+
- test/test_image_science.rb
|
54
|
+
has_rdoc: true
|
55
|
+
homepage: http://github.com/tdd/image_science
|
56
|
+
licenses:
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options:
|
59
|
+
- --inline-source
|
60
|
+
- --charset=UTF-8
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
version:
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: "0"
|
74
|
+
version:
|
75
|
+
requirements: []
|
76
|
+
|
77
|
+
rubyforge_project: image_science
|
78
|
+
rubygems_version: 1.3.5
|
79
|
+
signing_key:
|
80
|
+
specification_version: 2
|
81
|
+
summary: "This is a fork of Ryan\xE2\x80\x99s image_science lib that adds JPEG quality control on save operations."
|
82
|
+
test_files: []
|
83
|
+
|