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