gd2-ffij 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/COPYING +340 -0
- data/COPYRIGHT +17 -0
- data/README +34 -0
- data/Rakefile +32 -0
- data/gd2-ffij.gemspec +96 -0
- data/lib/gd2-ffij.rb +209 -0
- data/lib/gd2/canvas.rb +422 -0
- data/lib/gd2/color.rb +240 -0
- data/lib/gd2/ffi_struct.rb +76 -0
- data/lib/gd2/font.rb +347 -0
- data/lib/gd2/image.rb +785 -0
- data/lib/gd2/palette.rb +253 -0
- data/test/canvas_test.rb +186 -0
- data/test/image_test.rb +149 -0
- data/test/images/test.bmp +0 -0
- data/test/images/test.gd +0 -0
- data/test/images/test.gd2 +0 -0
- data/test/images/test.gif +0 -0
- data/test/images/test.jpg +0 -0
- data/test/images/test.png +0 -0
- data/test/images/test.wbmp +0 -0
- data/test/images/test.xbm +686 -0
- data/test/images/test.xcf +0 -0
- data/test/images/test.xpm +261 -0
- data/test/images/test_arc.gd2 +0 -0
- data/test/images/test_canvas_filled_polygon.gd2 +0 -0
- data/test/images/test_canvas_filled_rectangle.gd2 +0 -0
- data/test/images/test_canvas_line.gd2 +0 -0
- data/test/images/test_canvas_move_to_and_line_to.gd2 +0 -0
- data/test/images/test_canvas_polygon.gd2 +0 -0
- data/test/images/test_canvas_rectangle.gd2 +0 -0
- data/test/images/test_circle.gd2 +0 -0
- data/test/images/test_color.gd2 +0 -0
- data/test/images/test_color.png +0 -0
- data/test/images/test_color.xcf +0 -0
- data/test/images/test_color_indexed.gd2 +0 -0
- data/test/images/test_color_sharpened.gd2 +0 -0
- data/test/images/test_cropped.gd2 +0 -0
- data/test/images/test_ellipse.gd2 +0 -0
- data/test/images/test_fill.gd2 +0 -0
- data/test/images/test_fill_to.gd2 +0 -0
- data/test/images/test_filled_circle.gd2 +0 -0
- data/test/images/test_filled_ellipse.gd2 +0 -0
- data/test/images/test_filled_wedge.gd2 +0 -0
- data/test/images/test_polar_transform.gd2 +0 -0
- data/test/images/test_resampled.gd2 +0 -0
- data/test/images/test_resized.gd2 +0 -0
- data/test/images/test_rotated_180.gd2 +0 -0
- data/test/images/test_text.gd2 +0 -0
- data/test/images/test_text_circle.gd2 +0 -0
- data/test/images/test_wedge.gd2 +0 -0
- data/test/test_helper.rb +13 -0
- data/vendor/fonts/ttf/DejaVuSans.ttf +0 -0
- data/vendor/fonts/ttf/LICENSE +99 -0
- metadata +118 -0
data/lib/gd2/palette.rb
ADDED
@@ -0,0 +1,253 @@
|
|
1
|
+
#
|
2
|
+
# Ruby/GD2 -- Ruby binding for gd 2 graphics library
|
3
|
+
#
|
4
|
+
# Copyright © 2005 Robert Leslie, 2010 J Smith
|
5
|
+
#
|
6
|
+
# This file is part of Ruby/GD2.
|
7
|
+
#
|
8
|
+
# Ruby/GD2 is free software; you can redistribute it and/or modify it under
|
9
|
+
# the terms of the GNU General Public License as published by the Free
|
10
|
+
# Software Foundation; either version 2 of the License, or (at your option)
|
11
|
+
# any later version.
|
12
|
+
#
|
13
|
+
# This program is distributed in the hope that it will be useful, but
|
14
|
+
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
15
|
+
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
16
|
+
# for more details.
|
17
|
+
#
|
18
|
+
# You should have received a copy of the GNU General Public License along
|
19
|
+
# with this program; if not, write to the Free Software Foundation, Inc.,
|
20
|
+
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
21
|
+
#
|
22
|
+
|
23
|
+
module GD2
|
24
|
+
# = Description
|
25
|
+
#
|
26
|
+
# Palette objects are associated with an Image and hold the selection of
|
27
|
+
# pixel colors available to the Image. This is primarily a concern for
|
28
|
+
# Image::IndexedColor images, but their use with Image::TrueColor images is
|
29
|
+
# supported for consistency.
|
30
|
+
#
|
31
|
+
# == Obtaining
|
32
|
+
#
|
33
|
+
# Obtain a Palette object from the associated Image:
|
34
|
+
#
|
35
|
+
# palette = image.palette
|
36
|
+
#
|
37
|
+
class Palette
|
38
|
+
class PaletteFullError < StandardError; end
|
39
|
+
class ColorNotFoundError < StandardError; end
|
40
|
+
|
41
|
+
# The Image associated with this Palette
|
42
|
+
attr_reader :image
|
43
|
+
|
44
|
+
def initialize(image) #:nodoc:
|
45
|
+
@image = image
|
46
|
+
end
|
47
|
+
|
48
|
+
# Return the maximum number of colors this palette can hold.
|
49
|
+
def size
|
50
|
+
MAX_COLORS
|
51
|
+
end
|
52
|
+
alias length size
|
53
|
+
|
54
|
+
def allocated?(index) #:nodoc:
|
55
|
+
@image.image_ptr[:open][index].zero?
|
56
|
+
end
|
57
|
+
protected :allocated?
|
58
|
+
|
59
|
+
# Return the number of colors presently allocated in this palette.
|
60
|
+
def used
|
61
|
+
(0...size).inject(0) do |sum, i|
|
62
|
+
sum + (allocated?(i) ? 1 : 0)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# Return the number of palette entries available to be allocated for new
|
67
|
+
# colors.
|
68
|
+
def available
|
69
|
+
size - used
|
70
|
+
end
|
71
|
+
|
72
|
+
# Return the color allocated to the specified +index+, or *nil* if the
|
73
|
+
# entry is unallocated.
|
74
|
+
def [](index)
|
75
|
+
index = size + index if index < 0
|
76
|
+
raise RangeError unless (0...size).include? index
|
77
|
+
return nil unless allocated?(index)
|
78
|
+
get_color(index)
|
79
|
+
end
|
80
|
+
|
81
|
+
# Assign (allocate) the given +color+ to the palette entry identified by
|
82
|
+
# +index+. If the entry was previously allocated to a different color,
|
83
|
+
# every pixel in the image having the given color +index+ will effectively
|
84
|
+
# be changed to the new color. This method cannot be used with
|
85
|
+
# Image::TrueColor palettes.
|
86
|
+
def []=(index, color)
|
87
|
+
raise RangeError unless (0...MAX_COLORS).include? index
|
88
|
+
if color.nil?
|
89
|
+
deallocate(self[index] ||
|
90
|
+
Color.new_from_palette(0, 0, 0, 0, index, self))
|
91
|
+
else
|
92
|
+
ptr = @image.image_ptr
|
93
|
+
ptr[:red][index] = color.red
|
94
|
+
ptr[:green][index] = color.green
|
95
|
+
ptr[:blue][index] = color.blue
|
96
|
+
ptr[:alpha][index] = color.alpha
|
97
|
+
ptr[:open][index] = 0
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# Locate the given +color+ in this palette and return it if found;
|
102
|
+
# otherwise try to allocate the +color+, or if the palette is full, return
|
103
|
+
# a color from the palette that is closest to it.
|
104
|
+
def resolve(color)
|
105
|
+
raise TypeError unless color.kind_of? Color
|
106
|
+
c = GD2FFI.send(:gdImageColorResolveAlpha, @image.image_ptr,
|
107
|
+
color.red, color. green, color.blue, color.alpha)
|
108
|
+
c == -1 ? nil : get_color(c)
|
109
|
+
end
|
110
|
+
|
111
|
+
# Locate the given +color+ in this palette and return it. Returns *nil*
|
112
|
+
# if the color is not presently in the palette.
|
113
|
+
def exact(color)
|
114
|
+
raise TypeError unless color.kind_of? Color
|
115
|
+
c = GD2FFI.send(:gdImageColorExactAlpha, @image.image_ptr,
|
116
|
+
color.red, color.green, color.blue, color.alpha)
|
117
|
+
c == -1 ? nil : get_color(c)
|
118
|
+
end
|
119
|
+
|
120
|
+
# Like Palette#exact except an error is raised if the color is not
|
121
|
+
# presently in the palette.
|
122
|
+
def exact!(color)
|
123
|
+
exact(color) or raise Palette::ColorNotFoundError,
|
124
|
+
"Color #{color} is not in the palette"
|
125
|
+
end
|
126
|
+
|
127
|
+
# Return the color in this palette that is closest to the given +color+
|
128
|
+
# according to Euclidian distance.
|
129
|
+
def closest(color)
|
130
|
+
raise TypeError unless color.kind_of? Color
|
131
|
+
c = GD2FFI.send(:gdImageColorClosestAlpha, @image.image_ptr,
|
132
|
+
color.red, color.green, color.blue, color.alpha)
|
133
|
+
c == -1 ? nil : get_color(c)
|
134
|
+
end
|
135
|
+
|
136
|
+
# Return the color in this palette that is closest to the given +color+
|
137
|
+
# according to hue, whiteness, and blackness.
|
138
|
+
def closest_hwb(color)
|
139
|
+
raise TypeError unless color.kind_of? Color
|
140
|
+
c = GD2FFI.send(:gdImageColorClosestHWB, @image.image_ptr,
|
141
|
+
color.red, color.green, color.blue)
|
142
|
+
c == -1 ? nil : get_color(c)
|
143
|
+
end
|
144
|
+
|
145
|
+
# Assign the given +color+ to an unoccupied entry in this palette and
|
146
|
+
# return it. Does not check whether the color is already allocated, and
|
147
|
+
# raises an error for Image::IndexedColor palettes if the palette is full.
|
148
|
+
def allocate(color)
|
149
|
+
raise TypeError unless color.kind_of? Color
|
150
|
+
c = GD2FFI.send(:gdImageColorAllocateAlpha, @image.image_ptr,
|
151
|
+
color.red, color.green, color.blue, color.alpha)
|
152
|
+
c == -1 ? raise(Palette::PaletteFullError, 'Palette is full') :
|
153
|
+
get_color(c)
|
154
|
+
end
|
155
|
+
|
156
|
+
# Ensure the given +color+ is present in this palette, allocating it if
|
157
|
+
# necessary. Returns the palette so calls may be stacked.
|
158
|
+
def <<(color)
|
159
|
+
exact(color) or allocate(color)
|
160
|
+
self
|
161
|
+
end
|
162
|
+
|
163
|
+
# Remove the given +color+ from this palette.
|
164
|
+
def deallocate(color)
|
165
|
+
color = exact(color) unless color.index
|
166
|
+
return nil if color.nil? || color.index.nil?
|
167
|
+
GD2FFI.send(:gdImageColorDeallocate, @image.image_ptr, color.index)
|
168
|
+
nil
|
169
|
+
end
|
170
|
+
|
171
|
+
# Remove all colors from this palette that are not currently in use by the
|
172
|
+
# associated Image. This is an expensive operation. Returns the number of
|
173
|
+
# palette entries deallocated.
|
174
|
+
def deallocate_unused
|
175
|
+
# implemented by subclass
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
class Palette::IndexedColor < Palette
|
180
|
+
include Enumerable
|
181
|
+
|
182
|
+
def inspect #:nodoc:
|
183
|
+
"#<#{self.class} [#{used}]>"
|
184
|
+
end
|
185
|
+
|
186
|
+
def get_color(index) #:nodoc:
|
187
|
+
ptr = @image.image_ptr
|
188
|
+
Color.new_from_palette(
|
189
|
+
ptr[:red][index],
|
190
|
+
ptr[:green][index],
|
191
|
+
ptr[:blue][index],
|
192
|
+
ptr[:alpha][index],
|
193
|
+
index, self)
|
194
|
+
end
|
195
|
+
protected :get_color
|
196
|
+
|
197
|
+
# Iterate through every color allocated in this palette.
|
198
|
+
def each #:yields: color
|
199
|
+
(0...MAX_COLORS).each do |i|
|
200
|
+
color = self[i]
|
201
|
+
yield color unless color.nil?
|
202
|
+
end
|
203
|
+
self
|
204
|
+
end
|
205
|
+
|
206
|
+
def deallocate_unused #:nodoc:
|
207
|
+
used = @image.collect.flatten.uniq.inject(Array.new(MAX_COLORS)) do
|
208
|
+
|ary, c|
|
209
|
+
ary[c] = true
|
210
|
+
ary
|
211
|
+
end
|
212
|
+
count = 0
|
213
|
+
each do |color|
|
214
|
+
unless used.at(color.index)
|
215
|
+
self[color.index] = nil
|
216
|
+
count += 1
|
217
|
+
end
|
218
|
+
end
|
219
|
+
count.zero? ? nil : count
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
class Palette::TrueColor < Palette
|
224
|
+
def inspect #:nodoc:
|
225
|
+
"#<#{self.class}>"
|
226
|
+
end
|
227
|
+
|
228
|
+
def size #:nodoc:
|
229
|
+
((1 + RGB_MAX) ** 3) * (1 + ALPHA_MAX)
|
230
|
+
end
|
231
|
+
alias length size
|
232
|
+
alias used size
|
233
|
+
|
234
|
+
def allocated?(index) #:nodoc:
|
235
|
+
true
|
236
|
+
end
|
237
|
+
protected :allocated?
|
238
|
+
|
239
|
+
# Return *true*.
|
240
|
+
def include?(color)
|
241
|
+
true
|
242
|
+
end
|
243
|
+
|
244
|
+
def get_color(index) #:nodoc:
|
245
|
+
Color.new_from_rgba(index)
|
246
|
+
end
|
247
|
+
protected :get_color
|
248
|
+
|
249
|
+
def []=(index, color) #:nodoc:
|
250
|
+
raise "Palette assignment not supported for #{self.class}"
|
251
|
+
end
|
252
|
+
end
|
253
|
+
end
|
data/test/canvas_test.rb
ADDED
@@ -0,0 +1,186 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'tmpdir'
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'gd2-ffij'
|
6
|
+
|
7
|
+
require 'test/test_helper'
|
8
|
+
|
9
|
+
class CanvasTest < Test::Unit::TestCase
|
10
|
+
include TestHelper
|
11
|
+
|
12
|
+
def test_line
|
13
|
+
image = new_image
|
14
|
+
image.draw do |pen|
|
15
|
+
pen.color = image.palette.resolve(GD2::Color[255, 255, 255])
|
16
|
+
pen.line(64, 64, 192, 192)
|
17
|
+
end
|
18
|
+
assert(image == load_image('test_canvas_line.gd2'))
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_rectangle
|
22
|
+
image = new_image
|
23
|
+
image.draw do |pen|
|
24
|
+
pen.color = image.palette.resolve(GD2::Color[255, 255, 255])
|
25
|
+
pen.rectangle(64, 64, 192, 192)
|
26
|
+
end
|
27
|
+
assert(image == load_image('test_canvas_rectangle.gd2'))
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_filled_rectangle
|
31
|
+
image = new_image
|
32
|
+
image.draw do |pen|
|
33
|
+
pen.color = image.palette.resolve(GD2::Color[255, 255, 255])
|
34
|
+
pen.rectangle(64, 64, 192, 192, true)
|
35
|
+
end
|
36
|
+
assert(image == load_image('test_canvas_filled_rectangle.gd2'))
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_polygon
|
40
|
+
image = new_image
|
41
|
+
image.draw do |pen|
|
42
|
+
pen.color = image.palette.resolve(GD2::Color[255, 255, 255])
|
43
|
+
pen.polygon([
|
44
|
+
[ 64, 64 ],
|
45
|
+
[ 192, 192 ],
|
46
|
+
[ 64, 128 ]
|
47
|
+
])
|
48
|
+
end
|
49
|
+
assert(image == load_image('test_canvas_polygon.gd2'))
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_filled_polygon
|
53
|
+
image = new_image
|
54
|
+
image.draw do |pen|
|
55
|
+
pen.color = image.palette.resolve(GD2::Color[255, 255, 255])
|
56
|
+
pen.polygon([
|
57
|
+
[ 64, 64 ],
|
58
|
+
[ 192, 192 ],
|
59
|
+
[ 64, 128 ]
|
60
|
+
], true)
|
61
|
+
end
|
62
|
+
assert(image == load_image('test_canvas_filled_polygon.gd2'))
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_move_to_and_line_to
|
66
|
+
image = new_image
|
67
|
+
image.draw do |pen|
|
68
|
+
pen.color = image.palette.resolve(GD2::Color[255, 255, 255])
|
69
|
+
pen.move_to(64, 64)
|
70
|
+
pen.line_to(128, 128)
|
71
|
+
end
|
72
|
+
assert(image == load_image('test_canvas_move_to_and_line_to.gd2'))
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_fill
|
76
|
+
image = new_image
|
77
|
+
image.draw do |pen|
|
78
|
+
pen.color = image.palette.resolve(GD2::Color[32, 64, 128])
|
79
|
+
pen.rectangle(64, 64, 192, 192)
|
80
|
+
pen.move_to(65, 65)
|
81
|
+
pen.fill
|
82
|
+
end
|
83
|
+
assert(image == load_image('test_fill.gd2'))
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_arc
|
87
|
+
image = new_image
|
88
|
+
image.draw do |pen|
|
89
|
+
pen.color = image.palette.resolve(GD2::Color[32, 64, 128])
|
90
|
+
pen.arc(128, 128, 128, 128, 0..256)
|
91
|
+
end
|
92
|
+
assert(image == load_image('test_arc.gd2'))
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_text
|
96
|
+
image = new_image
|
97
|
+
image.draw do |pen|
|
98
|
+
pen.color = image.palette.resolve(GD2::Color[32, 64, 128])
|
99
|
+
pen.font = GD2::Font::TrueType[PATH_TO_FONT, 32]
|
100
|
+
pen.move_to(0, 128)
|
101
|
+
pen.text("HELLO")
|
102
|
+
pen.move_to(256, 128)
|
103
|
+
pen.text("WORLD", Math::PI)
|
104
|
+
end
|
105
|
+
assert(image == load_image('test_text.gd2'))
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_text_circle
|
109
|
+
image = new_image
|
110
|
+
image.draw do |pen|
|
111
|
+
pen.color = image.palette.resolve(GD2::Color[32, 64, 128])
|
112
|
+
pen.font = GD2::Font::TrueType[PATH_TO_FONT, 32]
|
113
|
+
pen.move_to(128, 128)
|
114
|
+
pen.text_circle('HELLO', 'WORLD', 100, 20, 1)
|
115
|
+
end
|
116
|
+
assert(image == load_image('test_text_circle.gd2'))
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_wedge
|
120
|
+
image = new_image
|
121
|
+
image.draw do |pen|
|
122
|
+
pen.color = image.palette.resolve(GD2::Color[32, 64, 128])
|
123
|
+
pen.wedge(128, 128, 256, 256, 0..180)
|
124
|
+
end
|
125
|
+
assert(image == load_image('test_wedge.gd2'))
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_filled_wedge
|
129
|
+
image = new_image
|
130
|
+
image.draw do |pen|
|
131
|
+
pen.color = image.palette.resolve(GD2::Color[32, 64, 128])
|
132
|
+
pen.wedge(128, 128, 256, 256, 0..180, true)
|
133
|
+
end
|
134
|
+
assert(image == load_image('test_filled_wedge.gd2'))
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_ellipse
|
138
|
+
image = new_image
|
139
|
+
image.draw do |pen|
|
140
|
+
pen.color = image.palette.resolve(GD2::Color[32, 64, 128])
|
141
|
+
pen.ellipse(128, 128, 64, 32)
|
142
|
+
end
|
143
|
+
assert(image == load_image('test_ellipse.gd2'))
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_filled_ellipse
|
147
|
+
image = new_image
|
148
|
+
image.draw do |pen|
|
149
|
+
pen.color = image.palette.resolve(GD2::Color[32, 64, 128])
|
150
|
+
pen.ellipse(128, 128, 64, 32, true)
|
151
|
+
end
|
152
|
+
assert(image == load_image('test_filled_ellipse.gd2'))
|
153
|
+
end
|
154
|
+
|
155
|
+
def test_circle
|
156
|
+
image = new_image
|
157
|
+
image.draw do |pen|
|
158
|
+
pen.color = image.palette.resolve(GD2::Color[32, 64, 128])
|
159
|
+
pen.circle(128, 128, 128)
|
160
|
+
end
|
161
|
+
assert(image == load_image('test_circle.gd2'))
|
162
|
+
end
|
163
|
+
|
164
|
+
def test_filled_circle
|
165
|
+
image = new_image
|
166
|
+
image.draw do |pen|
|
167
|
+
pen.color = image.palette.resolve(GD2::Color[32, 64, 128])
|
168
|
+
pen.circle(128, 128, 128, true)
|
169
|
+
end
|
170
|
+
assert(image == load_image('test_filled_circle.gd2'))
|
171
|
+
end
|
172
|
+
|
173
|
+
def test_fill_to
|
174
|
+
image = new_image
|
175
|
+
image.draw do |pen|
|
176
|
+
blue = image.palette.resolve(GD2::Color[32, 64, 128])
|
177
|
+
red = image.palette.resolve(GD2::Color[255, 0, 0])
|
178
|
+
pen.color = blue
|
179
|
+
pen.arc(128, 128, 256, 64, 0..(Math::PI))
|
180
|
+
pen.color = red
|
181
|
+
pen.move(128, 128)
|
182
|
+
pen.fill_to(blue)
|
183
|
+
end
|
184
|
+
assert(image == load_image('test_fill_to.gd2'))
|
185
|
+
end
|
186
|
+
end
|
data/test/image_test.rb
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'tmpdir'
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'gd2-ffij'
|
6
|
+
|
7
|
+
require 'test/test_helper'
|
8
|
+
|
9
|
+
class ImageTest < Test::Unit::TestCase
|
10
|
+
include TestHelper
|
11
|
+
|
12
|
+
def test_image_new_and_release
|
13
|
+
img = GD2::Image.new(50, 50)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_image_true_color_new_and_release
|
17
|
+
img = GD2::Image::TrueColor.new(50, 50)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_image_indexed_color_new_and_release
|
21
|
+
img = GD2::Image::IndexedColor.new(50, 50)
|
22
|
+
end
|
23
|
+
|
24
|
+
[ :png, :gif, :jpg, :wbmp, :gd2 ].each do |ext|
|
25
|
+
self.class_eval(<<-EOF)
|
26
|
+
def test_load_#{ext}_from_file
|
27
|
+
img = GD2::Image.load(File.open(File.join(PATH_TO_IMAGES, 'test.#{ext}')))
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_load_#{ext}_from_string
|
31
|
+
img = GD2::Image.load(File.read(File.join(PATH_TO_IMAGES, 'test.#{ext}')))
|
32
|
+
end
|
33
|
+
EOF
|
34
|
+
end
|
35
|
+
|
36
|
+
# TODO: add xbm, xpm and wbmp tests
|
37
|
+
[ :png, :gif, :jpg, :gd, :gd2 ].each do |ext|
|
38
|
+
self.class_eval(<<-EOF)
|
39
|
+
def test_import_#{ext}_from_file
|
40
|
+
img = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test.#{ext}'))
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_export_#{ext}
|
44
|
+
img = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test.gd2'))
|
45
|
+
out = File.join(Dir.tmpdir, 'test.#{ext}')
|
46
|
+
img.export(out)
|
47
|
+
|
48
|
+
assert(File.exists?(out))
|
49
|
+
|
50
|
+
imgA = GD2::Image.import(out)
|
51
|
+
imgB = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test.#{ext}'))
|
52
|
+
|
53
|
+
assert(imgA == imgB)
|
54
|
+
|
55
|
+
File.unlink(out)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_compare_#{ext}
|
59
|
+
imgA = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test.#{ext}'))
|
60
|
+
imgB = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test.#{ext}'))
|
61
|
+
assert_equal(imgA.compare(imgB), 0)
|
62
|
+
assert_equal(imgA.compare(imgA.dup), 0)
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_eqeq_#{ext}
|
66
|
+
imgA = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test.#{ext}'))
|
67
|
+
imgB = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test.#{ext}'))
|
68
|
+
assert(imgA == imgB)
|
69
|
+
assert(imgA == imgA.dup)
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_height_#{ext}
|
73
|
+
img = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test.#{ext}'))
|
74
|
+
assert_equal(img.height, 256)
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_width_#{ext}
|
78
|
+
img = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test.#{ext}'))
|
79
|
+
assert_equal(img.width, 256)
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_size_#{ext}
|
83
|
+
img = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test.#{ext}'))
|
84
|
+
assert_equal(img.size, [ 256, 256 ])
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_aspect_#{ext}
|
88
|
+
img = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test.#{ext}'))
|
89
|
+
assert_in_delta(img.aspect, 1.0, 0.00000001)
|
90
|
+
end
|
91
|
+
EOF
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_rotate
|
95
|
+
imgA = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test.gd2')).rotate!(Math::PI)
|
96
|
+
imgB = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test_rotated_180.gd2'))
|
97
|
+
|
98
|
+
assert(imgA == imgB)
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_crop
|
102
|
+
imgA = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test.gd2')).crop!(64, 64, 128, 128)
|
103
|
+
imgB = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test_cropped.gd2'))
|
104
|
+
|
105
|
+
assert(imgA == imgB)
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_uncrop
|
109
|
+
img = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test.gd2')).crop!(64, 64, 128, 128)
|
110
|
+
img.uncrop!(64)
|
111
|
+
|
112
|
+
assert_equal(img.size, [ 256, 256 ])
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_resize
|
116
|
+
imgA = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test.gd2')).resize!(512, 512)
|
117
|
+
imgB = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test_resized.gd2'))
|
118
|
+
|
119
|
+
assert(imgA == imgB)
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_resampled
|
123
|
+
imgA = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test.gd2')).resize!(512, 512, true)
|
124
|
+
imgB = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test_resampled.gd2'))
|
125
|
+
|
126
|
+
assert(imgA == imgB)
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_polar_transform
|
130
|
+
imgA = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test.gd2')).polar_transform!(100)
|
131
|
+
imgB = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test_polar_transform.gd2'))
|
132
|
+
|
133
|
+
assert(imgA == imgB)
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_color_sharpened
|
137
|
+
imgA = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test_color.gd2')).sharpen(100)
|
138
|
+
imgB = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test_color_sharpened.gd2'))
|
139
|
+
|
140
|
+
assert(imgA == imgB)
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_resampled
|
144
|
+
imgA = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test_color.gd2')).to_indexed_color
|
145
|
+
imgB = GD2::Image.import(File.join(PATH_TO_IMAGES, 'test_color_indexed.gd2'))
|
146
|
+
|
147
|
+
assert(imgA == imgB)
|
148
|
+
end
|
149
|
+
end
|