image_science 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -0
- data/Manifest.txt +6 -0
- data/README.txt +63 -0
- data/Rakefile +15 -0
- data/bench.rb +42 -0
- data/lib/image_science.rb +135 -0
- metadata +59 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
ImageScience
|
2
|
+
http://rubyforge.org/projects/seattlerb
|
3
|
+
http://seattlerb.rubyforge.org/
|
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
|
+
* 93 glorious lines of graphics manipulation magi... errr, SCIENCE!
|
17
|
+
* Supports both square and proportional thumbnails.
|
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("#{file}_cropped.png", 100)
|
24
|
+
end
|
25
|
+
|
26
|
+
ImageScience.with_image(file) do |img|
|
27
|
+
img.thumbnail("#{file}_thumb.png", 100)
|
28
|
+
end
|
29
|
+
|
30
|
+
== REQUIREMENTS:
|
31
|
+
|
32
|
+
* FreeImage
|
33
|
+
* ImageScience
|
34
|
+
|
35
|
+
== INSTALL:
|
36
|
+
|
37
|
+
* Download and install FreeImage. See notes at url above.
|
38
|
+
* sudo gem install -y ImageScience
|
39
|
+
|
40
|
+
== LICENSE:
|
41
|
+
|
42
|
+
(The MIT License)
|
43
|
+
|
44
|
+
Copyright (c) 2006 Ryan Davis, Seattle.rb
|
45
|
+
|
46
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
47
|
+
a copy of this software and associated documentation files (the
|
48
|
+
'Software'), to deal in the Software without restriction, including
|
49
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
50
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
51
|
+
permit persons to whom the Software is furnished to do so, subject to
|
52
|
+
the following conditions:
|
53
|
+
|
54
|
+
The above copyright notice and this permission notice shall be
|
55
|
+
included in all copies or substantial portions of the Software.
|
56
|
+
|
57
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
58
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
59
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
60
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
61
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
62
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
63
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
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
|
+
end
|
14
|
+
|
15
|
+
# vim: syntax=Ruby
|
data/bench.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
#!/usr/local/bin/ruby -w
|
2
|
+
|
3
|
+
require 'benchmark'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'image_science'
|
6
|
+
|
7
|
+
file = "blah_big.png"
|
8
|
+
|
9
|
+
if RUBY_PLATFORM =~ /darwin/ then
|
10
|
+
# how fucking cool is this???
|
11
|
+
puts "taking screenshot for thumbnailing benchmarks"
|
12
|
+
system "screencapture -SC #{file}"
|
13
|
+
else
|
14
|
+
abort "You need to plonk down #{file} or buy a mac"
|
15
|
+
end unless test ?f, "#{file}"
|
16
|
+
|
17
|
+
max = (ARGV.shift || 100).to_i
|
18
|
+
|
19
|
+
puts "# of iterations = #{max}"
|
20
|
+
Benchmark::bm(20) do |x|
|
21
|
+
x.report("null_time") {
|
22
|
+
for i in 0..max do
|
23
|
+
# do nothing
|
24
|
+
end
|
25
|
+
}
|
26
|
+
|
27
|
+
x.report("cropped") {
|
28
|
+
for i in 0..max do
|
29
|
+
ImageScience.with_image(file) do |img|
|
30
|
+
img.cropped_thumbnail("blah_cropped.png", 100)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
}
|
34
|
+
|
35
|
+
x.report("proportional") {
|
36
|
+
for i in 0..max do
|
37
|
+
ImageScience.with_image(file) do |img|
|
38
|
+
img.thumbnail("blah_thumb.png", 100)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
}
|
42
|
+
end
|
@@ -0,0 +1,135 @@
|
|
1
|
+
#!/usr/local/bin/ruby -w
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'inline'
|
5
|
+
|
6
|
+
##
|
7
|
+
# Provides a simple and clean 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.0.0'
|
15
|
+
|
16
|
+
##
|
17
|
+
# The top-level image loader opens +path+ and then passes the image
|
18
|
+
# instance to the given block.
|
19
|
+
|
20
|
+
def self.with_image(path); end # :yields: image
|
21
|
+
|
22
|
+
##
|
23
|
+
# Crops an image to +left+, +top+, +right+, and +bottom+ and then
|
24
|
+
# passes the new image to the given block.
|
25
|
+
|
26
|
+
def with_crop(left, top, right, bottom); end # :yields: image
|
27
|
+
|
28
|
+
##
|
29
|
+
# Returns the width of the image, in pixels.
|
30
|
+
|
31
|
+
def width; end
|
32
|
+
|
33
|
+
##
|
34
|
+
# Returns the height of the image, in pixels.
|
35
|
+
|
36
|
+
def height; end
|
37
|
+
|
38
|
+
##
|
39
|
+
# Creates a proportional thumbnail of the image scaled so its widest
|
40
|
+
# edge is resized to +size+ and writes the new image to +path+.
|
41
|
+
|
42
|
+
def thumbnail(path, size); end
|
43
|
+
|
44
|
+
##
|
45
|
+
# Creates a square thumbnail of the image cropping the longest edge
|
46
|
+
# to match the shortest edge, resizes to +size+, and writes the new
|
47
|
+
# image to +path+.
|
48
|
+
|
49
|
+
def cropped_thumbnail(path, size)
|
50
|
+
w, h = width, height
|
51
|
+
l, t, r, b, half = 0, 0, w, h, (w > h ? (w - h) : (h - w)) / 2
|
52
|
+
|
53
|
+
l, r = half, half + h if w > h
|
54
|
+
t, b = half, half + w if h > w
|
55
|
+
|
56
|
+
with_crop(l, t, r, b) do |img|
|
57
|
+
img.thumbnail(path, size)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
inline do |builder|
|
62
|
+
builder.add_link_flags "-lfreeimage"
|
63
|
+
builder.include '"FreeImage.h"'
|
64
|
+
|
65
|
+
builder.c_singleton <<-"END"
|
66
|
+
VALUE with_image(char * input) {
|
67
|
+
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
|
68
|
+
|
69
|
+
fif = FreeImage_GetFileType(input, 0);
|
70
|
+
if (fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(input);
|
71
|
+
if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) {
|
72
|
+
FIBITMAP *bitmap;
|
73
|
+
VALUE result = Qnil;
|
74
|
+
if (bitmap = FreeImage_Load(fif, input, 0)) {
|
75
|
+
VALUE obj = Data_Wrap_Struct(self, NULL, NULL, bitmap);
|
76
|
+
rb_iv_set(obj, "@file_type", INT2FIX(fif));
|
77
|
+
result = rb_yield(obj);
|
78
|
+
FreeImage_Unload(bitmap);
|
79
|
+
}
|
80
|
+
return result;
|
81
|
+
}
|
82
|
+
rb_raise(rb_eTypeError, "Unknown file format");
|
83
|
+
}
|
84
|
+
END
|
85
|
+
|
86
|
+
builder.c <<-"END"
|
87
|
+
VALUE with_crop(int l, int t, int r, int b) {
|
88
|
+
FIBITMAP *bitmap, *copy;
|
89
|
+
VALUE result = Qnil;
|
90
|
+
Data_Get_Struct(self, FIBITMAP, bitmap);
|
91
|
+
|
92
|
+
if (copy = FreeImage_Copy(bitmap, l, t, r, b)) {
|
93
|
+
VALUE obj = Data_Wrap_Struct(CLASS_OF(self), NULL, NULL, copy);
|
94
|
+
result = rb_yield(obj);
|
95
|
+
FreeImage_Unload(copy);
|
96
|
+
}
|
97
|
+
return result;
|
98
|
+
}
|
99
|
+
END
|
100
|
+
|
101
|
+
builder.c <<-"END"
|
102
|
+
int height() {
|
103
|
+
FIBITMAP *bitmap;
|
104
|
+
Data_Get_Struct(self, FIBITMAP, bitmap);
|
105
|
+
return FreeImage_GetHeight(bitmap);
|
106
|
+
}
|
107
|
+
END
|
108
|
+
|
109
|
+
builder.c <<-"END"
|
110
|
+
int width() {
|
111
|
+
FIBITMAP *bitmap;
|
112
|
+
Data_Get_Struct(self, FIBITMAP, bitmap);
|
113
|
+
return FreeImage_GetWidth(bitmap);
|
114
|
+
}
|
115
|
+
END
|
116
|
+
|
117
|
+
builder.c <<-"END"
|
118
|
+
VALUE thumbnail(char * output, int size) {
|
119
|
+
FIBITMAP *bitmap;
|
120
|
+
FREE_IMAGE_FORMAT fif = FreeImage_GetFIFFromFilename(output);
|
121
|
+
if (fif == FIF_UNKNOWN) fif = FIX2INT(rb_iv_get(self, "@file_type"));
|
122
|
+
if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsWriting(fif)) {
|
123
|
+
Data_Get_Struct(self, FIBITMAP, bitmap);
|
124
|
+
FIBITMAP *thumbnail = FreeImage_MakeThumbnail(bitmap, size, TRUE);
|
125
|
+
if (FreeImage_Save(fif, thumbnail, output, 0)) {
|
126
|
+
FreeImage_Unload(thumbnail);
|
127
|
+
return Qtrue;
|
128
|
+
}
|
129
|
+
return Qfalse;
|
130
|
+
}
|
131
|
+
rb_raise(rb_eTypeError, "Unknown file format");
|
132
|
+
}
|
133
|
+
END
|
134
|
+
end
|
135
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.0
|
3
|
+
specification_version: 1
|
4
|
+
name: image_science
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 1.0.0
|
7
|
+
date: 2006-12-13 00:00:00 -05:00
|
8
|
+
summary: ImageScience is a clean and happy Ruby library that generates thumbnails -- and kicks the living crap out of RMagick.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: ryand-ruby@zenspider.com
|
12
|
+
homepage: http://rubyforge.org/projects/seattlerb
|
13
|
+
rubyforge_project: seattlerb
|
14
|
+
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/
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Ryan Davis
|
31
|
+
files:
|
32
|
+
- History.txt
|
33
|
+
- Manifest.txt
|
34
|
+
- README.txt
|
35
|
+
- Rakefile
|
36
|
+
- bench.rb
|
37
|
+
- lib/image_science.rb
|
38
|
+
test_files: []
|
39
|
+
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
extra_rdoc_files: []
|
43
|
+
|
44
|
+
executables: []
|
45
|
+
|
46
|
+
extensions: []
|
47
|
+
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
dependencies:
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: hoe
|
53
|
+
version_requirement:
|
54
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: 1.1.4
|
59
|
+
version:
|