chunky_png 1.3.7 → 1.3.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.standard.yml +16 -0
- data/.travis.yml +8 -6
- data/.yardopts +1 -1
- data/CHANGELOG.rdoc +17 -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 +19 -15
- data/lib/chunky_png.rb +49 -39
- 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 +141 -52
- data/lib/chunky_png/color.rb +211 -206
- data/lib/chunky_png/datastream.rb +27 -23
- 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 +110 -13
- 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/resources/itxt_chunk.png +0 -0
- data/spec/spec_helper.rb +15 -9
- data/tasks/benchmarks.rake +7 -8
- metadata +53 -20
- 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]
|
@@ -29,6 +27,10 @@ module ChunkyPNG
|
|
29
27
|
# @return [ChunkyPNG::Chunk::Transparency]
|
30
28
|
attr_accessor :transparency_chunk
|
31
29
|
|
30
|
+
# The chunk containing the physical dimensions of the PNG's pixels.
|
31
|
+
# @return [ChunkyPNG::Chunk::Physical]
|
32
|
+
attr_accessor :physical_chunk
|
33
|
+
|
32
34
|
# The chunks that together compose the images pixel data.
|
33
35
|
# @return [Array<ChunkyPNG::Chunk::ImageData>]
|
34
36
|
attr_accessor :data_chunks
|
@@ -48,22 +50,21 @@ module ChunkyPNG
|
|
48
50
|
##############################################################################
|
49
51
|
|
50
52
|
class << self
|
51
|
-
|
52
53
|
# Reads a PNG datastream from a string.
|
53
54
|
# @param [String] str The PNG encoded string to load from.
|
54
55
|
# @return [ChunkyPNG::Datastream] The loaded datastream instance.
|
55
56
|
def from_blob(str)
|
56
|
-
from_io(StringIO.new(str))
|
57
|
+
from_io(StringIO.new(str, "rb"))
|
57
58
|
end
|
58
59
|
|
59
|
-
alias
|
60
|
+
alias from_string from_blob
|
60
61
|
|
61
62
|
# Reads a PNG datastream from a file.
|
62
63
|
# @param [String] filename The path of the file to load from.
|
63
64
|
# @return [ChunkyPNG::Datastream] The loaded datastream instance.
|
64
65
|
def from_file(filename)
|
65
66
|
ds = nil
|
66
|
-
File.open(filename,
|
67
|
+
File.open(filename, "rb") { |f| ds = from_io(f) }
|
67
68
|
ds
|
68
69
|
end
|
69
70
|
|
@@ -71,21 +72,23 @@ module ChunkyPNG
|
|
71
72
|
# @param [IO] io The stream to read from.
|
72
73
|
# @return [ChunkyPNG::Datastream] The loaded datastream instance.
|
73
74
|
def from_io(io)
|
75
|
+
io.set_encoding(Encoding::BINARY)
|
74
76
|
verify_signature!(io)
|
75
77
|
|
76
|
-
ds =
|
78
|
+
ds = new
|
77
79
|
while ds.end_chunk.nil?
|
78
80
|
chunk = ChunkyPNG::Chunk.read(io)
|
79
81
|
case chunk
|
80
|
-
when ChunkyPNG::Chunk::Header
|
81
|
-
when ChunkyPNG::Chunk::Palette
|
82
|
-
when ChunkyPNG::Chunk::Transparency
|
83
|
-
when ChunkyPNG::Chunk::ImageData
|
84
|
-
when ChunkyPNG::Chunk::
|
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
|
85
88
|
else ds.other_chunks << chunk
|
86
89
|
end
|
87
90
|
end
|
88
|
-
|
91
|
+
ds
|
89
92
|
end
|
90
93
|
|
91
94
|
# Verifies that the current stream is a PNG datastream by checking its signature.
|
@@ -98,7 +101,7 @@ module ChunkyPNG
|
|
98
101
|
# the beginning of the stream.
|
99
102
|
def verify_signature!(io)
|
100
103
|
signature = io.read(ChunkyPNG::Datastream::SIGNATURE.length)
|
101
|
-
unless
|
104
|
+
unless signature == ChunkyPNG::Datastream::SIGNATURE
|
102
105
|
raise ChunkyPNG::SignatureMismatch, "PNG signature not found, found #{signature.inspect} instead of #{ChunkyPNG::Datastream::SIGNATURE.inspect}!"
|
103
106
|
end
|
104
107
|
end
|
@@ -121,7 +124,8 @@ module ChunkyPNG
|
|
121
124
|
other_chunks.each { |chunk| yield(chunk) }
|
122
125
|
yield(palette_chunk) if palette_chunk
|
123
126
|
yield(transparency_chunk) if transparency_chunk
|
124
|
-
|
127
|
+
yield(physical_chunk) if physical_chunk
|
128
|
+
data_chunks.each { |chunk| yield(chunk) }
|
125
129
|
yield(end_chunk)
|
126
130
|
end
|
127
131
|
|
@@ -136,8 +140,8 @@ module ChunkyPNG
|
|
136
140
|
# @return [Hash] A hash containing metadata fields and their values.
|
137
141
|
def metadata
|
138
142
|
metadata = {}
|
139
|
-
other_chunks.
|
140
|
-
metadata[chunk.keyword] = chunk.value if chunk.respond_to?(:keyword)
|
143
|
+
other_chunks.each do |chunk|
|
144
|
+
metadata[chunk.keyword] = chunk.value if chunk.respond_to?(:keyword) && chunk.respond_to?(:value)
|
141
145
|
end
|
142
146
|
metadata
|
143
147
|
end
|
@@ -168,19 +172,19 @@ module ChunkyPNG
|
|
168
172
|
# Saves this datastream as a PNG file.
|
169
173
|
# @param [String] filename The filename to use.
|
170
174
|
def save(filename)
|
171
|
-
File.open(filename,
|
175
|
+
File.open(filename, "wb") { |f| write(f) }
|
172
176
|
end
|
173
177
|
|
174
178
|
# Encodes this datastream into a string.
|
175
179
|
# @return [String] The encoded PNG datastream.
|
176
180
|
def to_blob
|
177
181
|
str = StringIO.new
|
178
|
-
str.set_encoding(
|
182
|
+
str.set_encoding("ASCII-8BIT")
|
179
183
|
write(str)
|
180
|
-
|
184
|
+
str.string
|
181
185
|
end
|
182
186
|
|
183
|
-
alias
|
184
|
-
alias
|
187
|
+
alias to_string to_blob
|
188
|
+
alias to_s to_blob
|
185
189
|
end
|
186
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)
|