chunky_png 1.3.11 → 1.3.12
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.
- checksums.yaml +5 -5
- data/.standard.yml +16 -0
- data/.travis.yml +5 -5
- data/.yardopts +1 -1
- data/CHANGELOG.rdoc +5 -1
- data/CONTRIBUTING.rdoc +17 -8
- data/Gemfile +3 -3
- data/LICENSE +1 -1
- data/README.md +6 -1
- data/Rakefile +3 -3
- data/benchmarks/decoding_benchmark.rb +17 -17
- data/benchmarks/encoding_benchmark.rb +22 -19
- data/benchmarks/filesize_benchmark.rb +6 -6
- data/bin/rake +29 -0
- data/bin/standardrb +29 -0
- data/chunky_png.gemspec +15 -15
- data/lib/chunky_png.rb +16 -25
- data/lib/chunky_png/canvas.rb +28 -27
- data/lib/chunky_png/canvas/adam7_interlacing.rb +14 -10
- data/lib/chunky_png/canvas/data_url_exporting.rb +1 -3
- data/lib/chunky_png/canvas/data_url_importing.rb +1 -3
- data/lib/chunky_png/canvas/drawing.rb +28 -43
- data/lib/chunky_png/canvas/masking.rb +12 -14
- data/lib/chunky_png/canvas/operations.rb +26 -24
- data/lib/chunky_png/canvas/png_decoding.rb +36 -32
- data/lib/chunky_png/canvas/png_encoding.rb +106 -100
- data/lib/chunky_png/canvas/resampling.rb +26 -33
- data/lib/chunky_png/canvas/stream_exporting.rb +6 -8
- data/lib/chunky_png/canvas/stream_importing.rb +6 -8
- data/lib/chunky_png/chunk.rb +69 -60
- data/lib/chunky_png/color.rb +211 -206
- data/lib/chunky_png/datastream.rb +20 -22
- data/lib/chunky_png/dimension.rb +16 -11
- data/lib/chunky_png/image.rb +9 -11
- data/lib/chunky_png/palette.rb +4 -9
- data/lib/chunky_png/point.rb +25 -26
- data/lib/chunky_png/rmagick.rb +8 -10
- data/lib/chunky_png/vector.rb +26 -29
- data/lib/chunky_png/version.rb +1 -1
- data/spec/chunky_png/canvas/adam7_interlacing_spec.rb +20 -21
- data/spec/chunky_png/canvas/data_url_exporting_spec.rb +8 -5
- data/spec/chunky_png/canvas/data_url_importing_spec.rb +5 -6
- data/spec/chunky_png/canvas/drawing_spec.rb +46 -38
- data/spec/chunky_png/canvas/masking_spec.rb +15 -16
- data/spec/chunky_png/canvas/operations_spec.rb +68 -67
- data/spec/chunky_png/canvas/png_decoding_spec.rb +37 -38
- data/spec/chunky_png/canvas/png_encoding_spec.rb +59 -50
- data/spec/chunky_png/canvas/resampling_spec.rb +19 -21
- data/spec/chunky_png/canvas/stream_exporting_spec.rb +47 -27
- data/spec/chunky_png/canvas/stream_importing_spec.rb +10 -11
- data/spec/chunky_png/canvas_spec.rb +57 -52
- data/spec/chunky_png/color_spec.rb +115 -114
- data/spec/chunky_png/datastream_spec.rb +49 -51
- data/spec/chunky_png/dimension_spec.rb +10 -10
- data/spec/chunky_png/image_spec.rb +11 -14
- data/spec/chunky_png/point_spec.rb +21 -23
- data/spec/chunky_png/rmagick_spec.rb +7 -8
- data/spec/chunky_png/vector_spec.rb +21 -17
- data/spec/chunky_png_spec.rb +2 -2
- data/spec/png_suite_spec.rb +35 -40
- data/spec/spec_helper.rb +6 -10
- data/tasks/benchmarks.rake +7 -8
- metadata +34 -5
- data/lib/chunky_png/compatibility.rb +0 -15
@@ -1,5 +1,4 @@
|
|
1
1
|
module ChunkyPNG
|
2
|
-
|
3
2
|
# The Datastream class represents a PNG formatted datastream. It supports
|
4
3
|
# both reading from and writing to strings, streams and files.
|
5
4
|
#
|
@@ -9,9 +8,8 @@ module ChunkyPNG
|
|
9
8
|
#
|
10
9
|
# @see ChunkyPNG::Chunk
|
11
10
|
class Datastream
|
12
|
-
|
13
11
|
# The signature that each PNG file or stream should begin with.
|
14
|
-
SIGNATURE =
|
12
|
+
SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10].pack("C8").force_encoding(Encoding::BINARY).freeze
|
15
13
|
|
16
14
|
# The header chunk of this datastream.
|
17
15
|
# @return [ChunkyPNG::Chunk::Header]
|
@@ -52,22 +50,21 @@ module ChunkyPNG
|
|
52
50
|
##############################################################################
|
53
51
|
|
54
52
|
class << self
|
55
|
-
|
56
53
|
# Reads a PNG datastream from a string.
|
57
54
|
# @param [String] str The PNG encoded string to load from.
|
58
55
|
# @return [ChunkyPNG::Datastream] The loaded datastream instance.
|
59
56
|
def from_blob(str)
|
60
|
-
from_io(StringIO.new(str))
|
57
|
+
from_io(StringIO.new(str, "rb"))
|
61
58
|
end
|
62
59
|
|
63
|
-
alias
|
60
|
+
alias from_string from_blob
|
64
61
|
|
65
62
|
# Reads a PNG datastream from a file.
|
66
63
|
# @param [String] filename The path of the file to load from.
|
67
64
|
# @return [ChunkyPNG::Datastream] The loaded datastream instance.
|
68
65
|
def from_file(filename)
|
69
66
|
ds = nil
|
70
|
-
File.open(filename,
|
67
|
+
File.open(filename, "rb") { |f| ds = from_io(f) }
|
71
68
|
ds
|
72
69
|
end
|
73
70
|
|
@@ -75,22 +72,23 @@ module ChunkyPNG
|
|
75
72
|
# @param [IO] io The stream to read from.
|
76
73
|
# @return [ChunkyPNG::Datastream] The loaded datastream instance.
|
77
74
|
def from_io(io)
|
75
|
+
io.set_encoding(Encoding::BINARY)
|
78
76
|
verify_signature!(io)
|
79
77
|
|
80
|
-
ds =
|
78
|
+
ds = new
|
81
79
|
while ds.end_chunk.nil?
|
82
80
|
chunk = ChunkyPNG::Chunk.read(io)
|
83
81
|
case chunk
|
84
|
-
when ChunkyPNG::Chunk::Header
|
85
|
-
when ChunkyPNG::Chunk::Palette
|
86
|
-
when ChunkyPNG::Chunk::Transparency
|
87
|
-
when ChunkyPNG::Chunk::ImageData
|
88
|
-
when ChunkyPNG::Chunk::Physical
|
89
|
-
when ChunkyPNG::Chunk::End
|
82
|
+
when ChunkyPNG::Chunk::Header then ds.header_chunk = chunk
|
83
|
+
when ChunkyPNG::Chunk::Palette then ds.palette_chunk = chunk
|
84
|
+
when ChunkyPNG::Chunk::Transparency then ds.transparency_chunk = chunk
|
85
|
+
when ChunkyPNG::Chunk::ImageData then ds.data_chunks << chunk
|
86
|
+
when ChunkyPNG::Chunk::Physical then ds.physical_chunk = chunk
|
87
|
+
when ChunkyPNG::Chunk::End then ds.end_chunk = chunk
|
90
88
|
else ds.other_chunks << chunk
|
91
89
|
end
|
92
90
|
end
|
93
|
-
|
91
|
+
ds
|
94
92
|
end
|
95
93
|
|
96
94
|
# Verifies that the current stream is a PNG datastream by checking its signature.
|
@@ -103,7 +101,7 @@ module ChunkyPNG
|
|
103
101
|
# the beginning of the stream.
|
104
102
|
def verify_signature!(io)
|
105
103
|
signature = io.read(ChunkyPNG::Datastream::SIGNATURE.length)
|
106
|
-
unless
|
104
|
+
unless signature == ChunkyPNG::Datastream::SIGNATURE
|
107
105
|
raise ChunkyPNG::SignatureMismatch, "PNG signature not found, found #{signature.inspect} instead of #{ChunkyPNG::Datastream::SIGNATURE.inspect}!"
|
108
106
|
end
|
109
107
|
end
|
@@ -127,7 +125,7 @@ module ChunkyPNG
|
|
127
125
|
yield(palette_chunk) if palette_chunk
|
128
126
|
yield(transparency_chunk) if transparency_chunk
|
129
127
|
yield(physical_chunk) if physical_chunk
|
130
|
-
data_chunks.each
|
128
|
+
data_chunks.each { |chunk| yield(chunk) }
|
131
129
|
yield(end_chunk)
|
132
130
|
end
|
133
131
|
|
@@ -174,19 +172,19 @@ module ChunkyPNG
|
|
174
172
|
# Saves this datastream as a PNG file.
|
175
173
|
# @param [String] filename The filename to use.
|
176
174
|
def save(filename)
|
177
|
-
File.open(filename,
|
175
|
+
File.open(filename, "wb") { |f| write(f) }
|
178
176
|
end
|
179
177
|
|
180
178
|
# Encodes this datastream into a string.
|
181
179
|
# @return [String] The encoded PNG datastream.
|
182
180
|
def to_blob
|
183
181
|
str = StringIO.new
|
184
|
-
str.set_encoding(
|
182
|
+
str.set_encoding("ASCII-8BIT")
|
185
183
|
write(str)
|
186
|
-
|
184
|
+
str.string
|
187
185
|
end
|
188
186
|
|
189
|
-
alias
|
190
|
-
alias
|
187
|
+
alias to_string to_blob
|
188
|
+
alias to_s to_blob
|
191
189
|
end
|
192
190
|
end
|
data/lib/chunky_png/dimension.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
module ChunkyPNG
|
2
|
-
|
3
2
|
# Creates a {ChunkyPNG::Dimension} instance using arguments that can be interpreted
|
4
3
|
# as width and height.
|
5
4
|
#
|
@@ -29,8 +28,8 @@ module ChunkyPNG
|
|
29
28
|
# @see ChunkyPNG::Dimension
|
30
29
|
def self.Dimension(*args)
|
31
30
|
case args.length
|
32
|
-
when 2
|
33
|
-
when 1
|
31
|
+
when 2 then ChunkyPNG::Dimension.new(*args)
|
32
|
+
when 1 then build_dimension_from_object(args.first)
|
34
33
|
else raise ArgumentError,
|
35
34
|
"Don't know how to construct a dimension from #{args.inspect}"
|
36
35
|
end
|
@@ -45,8 +44,8 @@ module ChunkyPNG
|
|
45
44
|
when Array
|
46
45
|
ChunkyPNG::Dimension.new(source[0], source[1])
|
47
46
|
when Hash
|
48
|
-
width = source[:width] || source[
|
49
|
-
height = source[:height] || source[
|
47
|
+
width = source[:width] || source["width"]
|
48
|
+
height = source[:height] || source["height"]
|
50
49
|
ChunkyPNG::Dimension.new(width, height)
|
51
50
|
when ChunkyPNG::Dimension::DIMENSION_REGEXP
|
52
51
|
ChunkyPNG::Dimension.new($1, $2)
|
@@ -58,13 +57,13 @@ module ChunkyPNG
|
|
58
57
|
end
|
59
58
|
end
|
60
59
|
end
|
60
|
+
|
61
61
|
private_class_method :build_dimension_from_object
|
62
62
|
|
63
63
|
# Class that represents the dimension of something, e.g. a {ChunkyPNG::Canvas}.
|
64
64
|
#
|
65
65
|
# This class contains some methods to simplify performing dimension related checks.
|
66
66
|
class Dimension
|
67
|
-
|
68
67
|
# @return [Regexp] The regexp to parse dimensions from a string.
|
69
68
|
# @private
|
70
69
|
DIMENSION_REGEXP = /^[\(\[\{]?(\d+)\s*[x,]?\s*(\d+)[\)\]\}]?$/
|
@@ -89,7 +88,7 @@ module ChunkyPNG
|
|
89
88
|
end
|
90
89
|
|
91
90
|
# Checks whether a point is within bounds of this dimension.
|
92
|
-
# @param [ChunkyPNG::Point, ...] A point-like to bounds-check.
|
91
|
+
# @param [ChunkyPNG::Point, ...] point_like A point-like to bounds-check.
|
93
92
|
# @return [true, false] True iff the x and y coordinate fall in this dimension.
|
94
93
|
# @see ChunkyPNG.Point
|
95
94
|
def include?(*point_like)
|
@@ -98,17 +97,23 @@ module ChunkyPNG
|
|
98
97
|
end
|
99
98
|
|
100
99
|
# Checks whether 2 dimensions are identical.
|
101
|
-
# @param [ChunkyPNG::Dimension] The dimension to compare with.
|
100
|
+
# @param [ChunkyPNG::Dimension] other The dimension to compare with.
|
102
101
|
# @return [true, false] <tt>true</tt> iff width and height match.
|
103
102
|
def eql?(other)
|
104
103
|
return false unless other.respond_to?(:width) && other.respond_to?(:height)
|
105
104
|
other.width == width && other.height == height
|
106
105
|
end
|
107
106
|
|
108
|
-
|
107
|
+
alias == eql?
|
108
|
+
|
109
|
+
# Calculates a hash for the dimension object, based on width and height
|
110
|
+
# @return [Integer] A hashed value of the dimensions
|
111
|
+
def hash
|
112
|
+
[width, height].hash
|
113
|
+
end
|
109
114
|
|
110
115
|
# Compares the size of 2 dimensions.
|
111
|
-
# @param [ChunkyPNG::Dimension] The dimension to compare with.
|
116
|
+
# @param [ChunkyPNG::Dimension] other The dimension to compare with.
|
112
117
|
# @return [-1, 0, 1] -1 if the other dimension has a larger area, 1 of this
|
113
118
|
# dimension is larger, 0 if both are identical in size.
|
114
119
|
def <=>(other)
|
@@ -121,6 +126,6 @@ module ChunkyPNG
|
|
121
126
|
[width, height]
|
122
127
|
end
|
123
128
|
|
124
|
-
|
129
|
+
alias to_ary to_a
|
125
130
|
end
|
126
131
|
end
|
data/lib/chunky_png/image.rb
CHANGED
@@ -1,18 +1,16 @@
|
|
1
1
|
module ChunkyPNG
|
2
|
-
|
3
2
|
# ChunkyPNG::Image is an extension of the {ChunkyPNG::Canvas} class, that
|
4
3
|
# also includes support for metadata.
|
5
4
|
#
|
6
5
|
# @see ChunkyPNG::Canvas
|
7
6
|
class Image < Canvas
|
8
|
-
|
9
7
|
# The minimum size of bytes the value of a metadata field should be before compression
|
10
8
|
# is enabled for the chunk.
|
11
9
|
METADATA_COMPRESSION_TRESHOLD = 300
|
12
|
-
|
10
|
+
|
13
11
|
# @return [Hash] The hash of metadata fields for this PNG image.
|
14
12
|
attr_accessor :metadata
|
15
|
-
|
13
|
+
|
16
14
|
# Initializes a new ChunkyPNG::Image instance.
|
17
15
|
# @param [Integer] width The width of the new image.
|
18
16
|
# @param [Integer] height The height of the new image.
|
@@ -23,7 +21,7 @@ module ChunkyPNG
|
|
23
21
|
super(width, height, bg_color)
|
24
22
|
@metadata = metadata
|
25
23
|
end
|
26
|
-
|
24
|
+
|
27
25
|
# Initializes a copy of another ChunkyPNG::Image instance.
|
28
26
|
#
|
29
27
|
# @param [ChunkyPNG::Image] other The other image to copy.
|
@@ -31,7 +29,7 @@ module ChunkyPNG
|
|
31
29
|
super(other)
|
32
30
|
@metadata = other.metadata
|
33
31
|
end
|
34
|
-
|
32
|
+
|
35
33
|
# Returns the metadata for this image as PNG chunks.
|
36
34
|
#
|
37
35
|
# Chunks will either be of the {ChunkyPNG::Chunk::Text} type for small
|
@@ -49,7 +47,7 @@ module ChunkyPNG
|
|
49
47
|
end
|
50
48
|
end
|
51
49
|
end
|
52
|
-
|
50
|
+
|
53
51
|
# Encodes the image to a PNG datastream for saving to disk or writing to an IO stream.
|
54
52
|
#
|
55
53
|
# Besides encoding the canvas, it will also encode the metadata fields to text chunks.
|
@@ -61,19 +59,19 @@ module ChunkyPNG
|
|
61
59
|
def to_datastream(constraints = {})
|
62
60
|
ds = super(constraints)
|
63
61
|
ds.other_chunks += metadata_chunks
|
64
|
-
|
62
|
+
ds
|
65
63
|
end
|
66
|
-
|
64
|
+
|
67
65
|
# Reads a ChunkyPNG::Image instance from a data stream.
|
68
66
|
#
|
69
67
|
# Besides decoding the canvas, this will also read the metadata fields
|
70
68
|
# from the datastream.
|
71
69
|
#
|
72
|
-
# @param [ChunkyPNG::Datastream] The datastream to read from.
|
70
|
+
# @param [ChunkyPNG::Datastream] ds The datastream to read from.
|
73
71
|
def self.from_datastream(ds)
|
74
72
|
image = super(ds)
|
75
73
|
image.metadata = ds.metadata
|
76
|
-
|
74
|
+
image
|
77
75
|
end
|
78
76
|
end
|
79
77
|
end
|
data/lib/chunky_png/palette.rb
CHANGED
@@ -40,12 +40,8 @@ module ChunkyPNG
|
|
40
40
|
decoding_map = []
|
41
41
|
index = 0
|
42
42
|
|
43
|
-
palatte_bytes = palette_chunk.content.unpack(
|
44
|
-
|
45
|
-
alpha_channel = transparency_chunk.content.unpack('C*')
|
46
|
-
else
|
47
|
-
alpha_channel = []
|
48
|
-
end
|
43
|
+
palatte_bytes = palette_chunk.content.unpack("C*")
|
44
|
+
alpha_channel = transparency_chunk ? transparency_chunk.content.unpack("C*") : []
|
49
45
|
|
50
46
|
index = 0
|
51
47
|
palatte_bytes.each_slice(3) do |bytes|
|
@@ -165,7 +161,7 @@ module ChunkyPNG
|
|
165
161
|
#
|
166
162
|
# @return [ChunkyPNG::Chunk::Transparency] The tRNS chunk.
|
167
163
|
def to_trns_chunk
|
168
|
-
ChunkyPNG::Chunk::Transparency.new(
|
164
|
+
ChunkyPNG::Chunk::Transparency.new("tRNS", map { |c| ChunkyPNG::Color.a(c) }.pack("C*"))
|
169
165
|
end
|
170
166
|
|
171
167
|
# Creates a PLTE chunk that corresponds with this palette to store the r,
|
@@ -186,7 +182,7 @@ module ChunkyPNG
|
|
186
182
|
colors += ChunkyPNG::Color.to_truecolor_bytes(color)
|
187
183
|
end
|
188
184
|
|
189
|
-
ChunkyPNG::Chunk::Palette.new(
|
185
|
+
ChunkyPNG::Chunk::Palette.new("PLTE", colors.pack("C*"))
|
190
186
|
end
|
191
187
|
|
192
188
|
# Determines the most suitable colormode for this palette.
|
@@ -219,7 +215,6 @@ module ChunkyPNG
|
|
219
215
|
when 3..4 then 2
|
220
216
|
when 5..16 then 4
|
221
217
|
when 17..256 then 8
|
222
|
-
else nil
|
223
218
|
end
|
224
219
|
end
|
225
220
|
end
|
data/lib/chunky_png/point.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
module ChunkyPNG
|
2
|
-
|
3
2
|
# Factory method to create {ChunkyPNG::Point} instances.
|
4
|
-
#
|
5
|
-
# This method tries to be as flexible as possible with regards to the given input: besides
|
3
|
+
#
|
4
|
+
# This method tries to be as flexible as possible with regards to the given input: besides
|
6
5
|
# explicit coordinates, this method also accepts arrays, hashes, strings, {ChunkyPNG::Dimension}
|
7
6
|
# instances and anything that responds to <tt>:x</tt> and <tt>:y</tt>.
|
8
|
-
#
|
7
|
+
#
|
9
8
|
# @overload Point(x, y)
|
10
9
|
# @param [Integer, :to_i] x The x-coordinate
|
11
10
|
# @param [Integer, :to_i] y The y-coordinate
|
@@ -30,11 +29,11 @@ module ChunkyPNG
|
|
30
29
|
# @see ChunkyPNG::Point
|
31
30
|
def self.Point(*args)
|
32
31
|
case args.length
|
33
|
-
when 2
|
34
|
-
when 1
|
35
|
-
else raise ArgumentError,
|
32
|
+
when 2 then ChunkyPNG::Point.new(*args)
|
33
|
+
when 1 then build_point_from_object(args.first)
|
34
|
+
else raise ArgumentError,
|
36
35
|
"Don't know how to construct a point from #{args.inspect}!"
|
37
|
-
end
|
36
|
+
end
|
38
37
|
end
|
39
38
|
|
40
39
|
def self.build_point_from_object(source)
|
@@ -46,20 +45,21 @@ module ChunkyPNG
|
|
46
45
|
when Array
|
47
46
|
ChunkyPNG::Point.new(source[0], source[1])
|
48
47
|
when Hash
|
49
|
-
x = source[:x] || source[
|
50
|
-
y = source[:y] || source[
|
48
|
+
x = source[:x] || source["x"]
|
49
|
+
y = source[:y] || source["y"]
|
51
50
|
ChunkyPNG::Point.new(x, y)
|
52
51
|
when ChunkyPNG::Point::POINT_REGEXP
|
53
52
|
ChunkyPNG::Point.new($1.to_i, $2.to_i)
|
54
|
-
else
|
53
|
+
else
|
55
54
|
if source.respond_to?(:x) && source.respond_to?(:y)
|
56
55
|
ChunkyPNG::Point.new(source.x, source.y)
|
57
|
-
else
|
58
|
-
raise ArgumentError,
|
56
|
+
else
|
57
|
+
raise ArgumentError,
|
59
58
|
"Don't know how to construct a point from #{source.inspect}!"
|
60
59
|
end
|
61
60
|
end
|
62
61
|
end
|
62
|
+
|
63
63
|
private_class_method :build_point_from_object
|
64
64
|
|
65
65
|
# Simple class that represents a point on a canvas using an x and y coordinate.
|
@@ -69,7 +69,6 @@ module ChunkyPNG
|
|
69
69
|
#
|
70
70
|
# @see ChunkyPNG.Point
|
71
71
|
class Point
|
72
|
-
|
73
72
|
# @return [Regexp] The regexp to parse points from a string.
|
74
73
|
# @private
|
75
74
|
POINT_REGEXP = /^[\(\[\{]?(\d+)\s*[,]?\s*(\d+)[\)\]\}]?$/
|
@@ -79,22 +78,22 @@ module ChunkyPNG
|
|
79
78
|
|
80
79
|
# @return [Integer] The y-coordinate of the point.
|
81
80
|
attr_accessor :y
|
82
|
-
|
81
|
+
|
83
82
|
# Initializes a new point instance.
|
84
83
|
# @param [Integer, :to_i] x The x-coordinate.
|
85
84
|
# @param [Integer, :to_i] y The y-coordinate.
|
86
85
|
def initialize(x, y)
|
87
86
|
@x, @y = x.to_i, y.to_i
|
88
87
|
end
|
89
|
-
|
88
|
+
|
90
89
|
# Checks whether 2 points are identical.
|
91
90
|
# @return [true, false] <tt>true</tt> iff the x and y coordinates match
|
92
91
|
def eql?(other)
|
93
92
|
other.x == x && other.y == y
|
94
93
|
end
|
95
|
-
|
96
|
-
|
97
|
-
|
94
|
+
|
95
|
+
alias == eql?
|
96
|
+
|
98
97
|
# Compares 2 points.
|
99
98
|
#
|
100
99
|
# It will first compare the y coordinate, and it only takes the x-coordinate into
|
@@ -106,21 +105,21 @@ module ChunkyPNG
|
|
106
105
|
# @return [-1, 0, 1] <tt>-1</tt> If this point comes before the other one, <tt>1</tt>
|
107
106
|
# if after, and <tt>0</tt> if the points are identical.
|
108
107
|
def <=>(other)
|
109
|
-
(
|
108
|
+
(y <=> other.y) == 0 ? x <=> other.x : y <=> other.y
|
110
109
|
end
|
111
|
-
|
110
|
+
|
112
111
|
# Converts the point instance to an array.
|
113
112
|
# @return [Array] A 2-element array, i.e. <tt>[x, y]</tt>.
|
114
113
|
def to_a
|
115
114
|
[x, y]
|
116
115
|
end
|
117
|
-
|
118
|
-
|
119
|
-
|
116
|
+
|
117
|
+
alias to_ary to_a
|
118
|
+
|
120
119
|
# Checks whether the point falls into a dimension
|
121
|
-
# @param [ChunkyPNG::Dimension, ...] dimension_like The dimension of which the bounds
|
120
|
+
# @param [ChunkyPNG::Dimension, ...] dimension_like The dimension of which the bounds
|
122
121
|
# should be taken for the check.
|
123
|
-
# @return [true, false] <tt>true</tt> iff the x and y coordinate fall width the width
|
122
|
+
# @return [true, false] <tt>true</tt> iff the x and y coordinate fall width the width
|
124
123
|
# and height of the dimension.
|
125
124
|
def within_bounds?(*dimension_like)
|
126
125
|
ChunkyPNG::Dimension(*dimension_like).include?(self)
|
data/lib/chunky_png/rmagick.rb
CHANGED
@@ -1,33 +1,31 @@
|
|
1
|
-
require
|
1
|
+
require "rmagick"
|
2
2
|
|
3
3
|
module ChunkyPNG
|
4
|
-
|
5
4
|
# Methods for importing and exporting RMagick image objects.
|
6
5
|
#
|
7
6
|
# By default, this module is disabled because of the dependency on RMagick.
|
8
|
-
# You need to include this module yourself if you want to use it.
|
7
|
+
# You need to include this module yourself if you want to use it.
|
9
8
|
#
|
10
9
|
# @example
|
11
10
|
#
|
12
11
|
# require 'rmagick'
|
13
12
|
# require 'chunky_png/rmagick'
|
14
|
-
#
|
13
|
+
#
|
15
14
|
# canvas = ChunkyPNG::Canvas.from_file('filename.png')
|
16
15
|
# image = ChunkyPNG::RMagick.export(canvas)
|
17
|
-
#
|
16
|
+
#
|
18
17
|
# # do something with the image using RMagick
|
19
|
-
#
|
18
|
+
#
|
20
19
|
# updated_canvas = ChunkyPNG::RMagick.import(image)
|
21
20
|
#
|
22
21
|
module RMagick
|
23
|
-
|
24
22
|
extend self
|
25
|
-
|
23
|
+
|
26
24
|
# Imports an RMagick image as Canvas object.
|
27
25
|
# @param [Magick::Image] image The image to import
|
28
26
|
# @return [ChunkyPNG::Canvas] The canvas, constructed from the RMagick image.
|
29
27
|
def import(image)
|
30
|
-
pixels = image.export_pixels_to_str(0, 0, image.columns, image.rows,
|
28
|
+
pixels = image.export_pixels_to_str(0, 0, image.columns, image.rows, "RGBA")
|
31
29
|
ChunkyPNG::Canvas.from_rgba_stream(image.columns, image.rows, pixels)
|
32
30
|
end
|
33
31
|
|
@@ -36,7 +34,7 @@ module ChunkyPNG
|
|
36
34
|
# @return [Magick::Image] The RMagick image constructed from the Canvas instance.
|
37
35
|
def export(canvas)
|
38
36
|
image = Magick::Image.new(canvas.width, canvas.height)
|
39
|
-
image.import_pixels(0,0, canvas.width, canvas.height,
|
37
|
+
image.import_pixels(0, 0, canvas.width, canvas.height, "RGBA", canvas.pixels.pack("N*"))
|
40
38
|
image
|
41
39
|
end
|
42
40
|
end
|