jemmyw-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 +9 -0
- data/Gemfile +5 -0
- data/History.txt +77 -0
- data/Manifest.txt +9 -0
- data/README.txt +66 -0
- data/Rakefile +16 -0
- data/bench.rb +63 -0
- data/bin/image_science_thumb +31 -0
- data/ext/image_science/extconf.rb +10 -0
- data/ext/image_science/image_science.c +256 -0
- data/image_science.gemspec +28 -0
- data/lib/image_science/image_science.rb +83 -0
- data/lib/image_science/version.rb +3 -0
- data/lib/image_science.rb +3 -0
- data/test/pix.png +0 -0
- data/test/test_image_science.rb +191 -0
- metadata +78 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
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
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,16 @@
|
|
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
|
+
`mv extension.bundle lib/image_science/extension.bundle`
|
11
|
+
end
|
12
|
+
|
13
|
+
task :chmod do
|
14
|
+
File.chmod(0775, 'lib/image_science/extension.so')
|
15
|
+
end
|
16
|
+
|
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,256 @@
|
|
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
|
+
static VALUE bytes(VALUE self) {
|
199
|
+
FIMEMORY *hmem = NULL;
|
200
|
+
FIBITMAP *bitmap;
|
201
|
+
VALUE byte_string;
|
202
|
+
int flags;
|
203
|
+
|
204
|
+
FREE_IMAGE_FORMAT fif = FIX2INT(rb_iv_get(self, "@file_type"));
|
205
|
+
|
206
|
+
if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsWriting(fif)) {
|
207
|
+
GET_BITMAP(bitmap);
|
208
|
+
flags = fif == FIF_JPEG ? JPEG_QUALITYGOOD : 0;
|
209
|
+
BOOL result = 0, unload = 0;
|
210
|
+
|
211
|
+
if (fif == FIF_PNG) FreeImage_DestroyICCProfile(bitmap);
|
212
|
+
if (fif == FIF_JPEG && FreeImage_GetBPP(bitmap) != 24)
|
213
|
+
bitmap = FreeImage_ConvertTo24Bits(bitmap), unload = 1;
|
214
|
+
|
215
|
+
// open and allocate a memory stream
|
216
|
+
hmem = FreeImage_OpenMemory(0, 0);
|
217
|
+
// save the image to a memory stream
|
218
|
+
result = FreeImage_SaveToMemory(fif, bitmap, hmem, flags);
|
219
|
+
|
220
|
+
if (unload) FreeImage_Unload(bitmap);
|
221
|
+
|
222
|
+
// get the buffer from the memory stream
|
223
|
+
BYTE *mem_buffer = NULL;
|
224
|
+
DWORD size_in_bytes = 0;
|
225
|
+
FreeImage_AcquireMemory(hmem, &mem_buffer, &size_in_bytes);
|
226
|
+
byte_string = rb_str_new((char *)mem_buffer, size_in_bytes);
|
227
|
+
FreeImage_CloseMemory(hmem);
|
228
|
+
|
229
|
+
return byte_string;
|
230
|
+
}
|
231
|
+
|
232
|
+
return Qnil;
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
#ifdef __cplusplus
|
237
|
+
extern "C" {
|
238
|
+
#endif
|
239
|
+
void Init_extension() {
|
240
|
+
VALUE c = rb_cObject;
|
241
|
+
c = rb_const_get(c, rb_intern("ImageScience"));
|
242
|
+
|
243
|
+
rb_define_method(c, "height", (VALUE(*)(ANYARGS))height, 0);
|
244
|
+
rb_define_method(c, "resize", (VALUE(*)(ANYARGS))resize, 2);
|
245
|
+
rb_define_method(c, "save", (VALUE(*)(ANYARGS))save, 1);
|
246
|
+
rb_define_method(c, "bytes", (VALUE(*)(ANYARGS))bytes, 0);
|
247
|
+
rb_define_method(c, "width", (VALUE(*)(ANYARGS))width, 0);
|
248
|
+
rb_define_method(c, "with_crop", (VALUE(*)(ANYARGS))with_crop, 4);
|
249
|
+
rb_define_singleton_method(c, "with_image", (VALUE(*)(ANYARGS))with_image, 1);
|
250
|
+
rb_define_singleton_method(c, "with_image_from_memory", (VALUE(*)(ANYARGS))with_image_from_memory, 1);
|
251
|
+
FreeImage_SetOutputMessage(FreeImageMessageHandler);
|
252
|
+
|
253
|
+
}
|
254
|
+
#ifdef __cplusplus
|
255
|
+
}
|
256
|
+
#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 = "jemmyw-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,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
|
data/test/pix.png
ADDED
Binary file
|
@@ -0,0 +1,191 @@
|
|
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_bytes
|
86
|
+
data = File.new(@path).binmode.read
|
87
|
+
|
88
|
+
ImageScience.with_image_from_memory data do |img|
|
89
|
+
File.open(@tmppath, "wb"){|f| f.write img.bytes}
|
90
|
+
assert File.size(@tmppath) > 1100
|
91
|
+
|
92
|
+
ImageScience.with_image(@tmppath) do |img2|
|
93
|
+
assert_equal(img2.width, img.width)
|
94
|
+
assert_equal(img2.height, img.height)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_resize
|
100
|
+
ImageScience.with_image @path do |img|
|
101
|
+
img.resize(25, 25) 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_floats
|
116
|
+
ImageScience.with_image @path do |img|
|
117
|
+
img.resize(25.2, 25.7) do |thumb|
|
118
|
+
assert thumb.save(@tmppath)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
assert File.exists?(@tmppath)
|
123
|
+
|
124
|
+
ImageScience.with_image @tmppath do |img|
|
125
|
+
assert_kind_of ImageScience, img
|
126
|
+
assert_equal 25, img.height
|
127
|
+
assert_equal 25, img.width
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_resize_zero
|
132
|
+
assert_raises ArgumentError do
|
133
|
+
ImageScience.with_image @path do |img|
|
134
|
+
img.resize(0, 25) do |thumb|
|
135
|
+
assert thumb.save(@tmppath)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
refute File.exists?(@tmppath)
|
141
|
+
|
142
|
+
assert_raises ArgumentError do
|
143
|
+
ImageScience.with_image @path do |img|
|
144
|
+
img.resize(25, 0) do |thumb|
|
145
|
+
assert thumb.save(@tmppath)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
refute File.exists?(@tmppath)
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_resize_negative
|
154
|
+
assert_raises ArgumentError do
|
155
|
+
ImageScience.with_image @path do |img|
|
156
|
+
img.resize(-25, 25) do |thumb|
|
157
|
+
assert thumb.save(@tmppath)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
refute File.exists?(@tmppath)
|
163
|
+
|
164
|
+
assert_raises ArgumentError do
|
165
|
+
ImageScience.with_image @path do |img|
|
166
|
+
img.resize(25, -25) do |thumb|
|
167
|
+
assert thumb.save(@tmppath)
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
refute File.exists?(@tmppath)
|
173
|
+
end
|
174
|
+
|
175
|
+
def test_thumbnail
|
176
|
+
ImageScience.with_image @path do |img|
|
177
|
+
img.thumbnail(29) do |thumb|
|
178
|
+
assert thumb.save(@tmppath)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
assert File.exists?(@tmppath)
|
183
|
+
|
184
|
+
ImageScience.with_image @tmppath do |img|
|
185
|
+
assert_kind_of ImageScience, img
|
186
|
+
assert_equal 29, img.height
|
187
|
+
assert_equal 29, img.width
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jemmyw-image_science
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.3.2.1.Asynchrony
|
5
|
+
prerelease: 8
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ryan Davis
|
9
|
+
- Craig Buchek
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-08-13 00:00:00.000000000 Z
|
14
|
+
dependencies: []
|
15
|
+
description: ! 'ImageScience is a clean and happy Ruby library that generates
|
16
|
+
|
17
|
+
thumbnails -- and kicks the living crap out of RMagick. Oh, and it
|
18
|
+
|
19
|
+
doesn''t leak memory like a sieve. :)
|
20
|
+
|
21
|
+
|
22
|
+
This fork does not require RubyInline.
|
23
|
+
|
24
|
+
|
25
|
+
For more information (on the original variant), see http://seattlerb.rubyforge.org/ImageScience.html
|
26
|
+
|
27
|
+
'
|
28
|
+
email:
|
29
|
+
- craig.buchek@asolutions.com
|
30
|
+
executables:
|
31
|
+
- image_science_thumb
|
32
|
+
extensions:
|
33
|
+
- ext/image_science/extconf.rb
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- .gitignore
|
37
|
+
- Gemfile
|
38
|
+
- History.txt
|
39
|
+
- Manifest.txt
|
40
|
+
- README.txt
|
41
|
+
- Rakefile
|
42
|
+
- bench.rb
|
43
|
+
- bin/image_science_thumb
|
44
|
+
- ext/image_science/extconf.rb
|
45
|
+
- ext/image_science/image_science.c
|
46
|
+
- image_science.gemspec
|
47
|
+
- lib/image_science.rb
|
48
|
+
- lib/image_science/image_science.rb
|
49
|
+
- lib/image_science/version.rb
|
50
|
+
- test/pix.png
|
51
|
+
- test/test_image_science.rb
|
52
|
+
homepage: http://github.com/asynchrony/jemmyw-image_science
|
53
|
+
licenses: []
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>'
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 1.3.1
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project: image_science
|
72
|
+
rubygems_version: 1.8.24
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Replacement for RMagick; uses FreeImage instead of ImageMagick
|
76
|
+
test_files:
|
77
|
+
- test/pix.png
|
78
|
+
- test/test_image_science.rb
|