/
chunky_png.rb
170 lines (136 loc) · 5.25 KB
/
chunky_png.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Basic requirements from standard library
require "set"
require "zlib"
require "stringio"
# ChunkyPNG - the pure ruby library to access PNG files.
#
# The ChunkyPNG module defines some constants that are used in the
# PNG specification, specifies some exception classes, and serves as
# a namespace for all the other modules and classes in this library.
#
# {ChunkyPNG::Image}:: class to represent PNG images, including metadata.
# {ChunkyPNG::Canvas}:: class to represent the image's canvas.
# {ChunkyPNG::Color}:: module to work with color values.
# {ChunkyPNG::Palette}:: represents the palette of colors used on a {ChunkyPNG::Canvas}.
# {ChunkyPNG::Datastream}:: represents the internal structure of a PNG {ChunkyPNG::Image}.
# {ChunkyPNG::Color}:: represents one chunk of data within a {ChunkyPNG::Datastream}.
# {ChunkyPNG::Point}:: geometry helper class representing a 2-dimensional point.
# {ChunkyPNG::Dimension}:: geometry helper class representing a dimension (i.e. width x height).
# {ChunkyPNG::Vector}:: geometry helper class representing a series of points.
#
# @author Willem van Bergen
module ChunkyPNG
###################################################
# PNG international standard defined constants
###################################################
# Indicates that the PNG image uses grayscale colors, i.e. only a
# single teint channel.
# @private
COLOR_GRAYSCALE = 0
# Indicates that the PNG image uses true color, composed of a red
# green and blue channel.
# @private
COLOR_TRUECOLOR = 2
# Indicates that the PNG image uses indexed colors, where the values
# point to colors defined on a palette.
# @private
COLOR_INDEXED = 3
# Indicates that the PNG image uses grayscale colors with opacity, i.e.
# a teint channel with an alpha channel.
# @private
COLOR_GRAYSCALE_ALPHA = 4
# Indicates that the PNG image uses true color with opacity, composed of
# a red, green and blue channel, and an alpha value.
# @private
COLOR_TRUECOLOR_ALPHA = 6
# Indicates that the PNG specification's default compression
# method is used (Zlib/Deflate)
# @private
COMPRESSION_DEFAULT = 0
# Indicates that the PNG chunk content is not compressed
# flag used in iTXt chunk
# @private
UNCOMPRESSED_CONTENT = 0
# Indicates that the PNG chunk content is compressed
# flag used in iTXt chunk
# @private
COMPRESSED_CONTENT = 1
# Indicates that the image does not use interlacing.
# @private
INTERLACING_NONE = 0
# Indicates that the image uses Adam7 interlacing.
# @private
INTERLACING_ADAM7 = 1
### Filter method constants
# Indicates that the PNG specification's default filtering are
# being used in the image.
# @private
FILTERING_DEFAULT = 0
# Indicates that no filtering is used for the scanline.
# @private
FILTER_NONE = 0
# Indicates that SUB filtering is used for the scanline.
# @private
FILTER_SUB = 1
# Indicates that UP filtering is used for the scanline.
# @private
FILTER_UP = 2
# Indicates that AVERAGE filtering is used for the scanline.
# @private
FILTER_AVERAGE = 3
# Indicates that PAETH filtering is used for the scanline.
# @private
FILTER_PAETH = 4
###################################################
# ChunkyPNG exception classes
###################################################
# Default exception class for ChunkyPNG
class Exception < ::StandardError
end
# Exception that is raised for an unsupported PNG image.
class NotSupported < ChunkyPNG::Exception
end
# Exception that is raised if the PNG signature is not encountered at the
# beginning of the file.
class SignatureMismatch < ChunkyPNG::Exception
end
# Exception that is raised if the CRC check for a block fails
class CRCMismatch < ChunkyPNG::Exception
end
# Exception that is raised if an tTXt chunk does not contain valid UTF-8 data.
class InvalidUTF8 < ChunkyPNG::Exception
end
# Exception that is raised if an expectation fails.
class ExpectationFailed < ChunkyPNG::Exception
end
# Exception that when provided coordinates are out of bounds for the canvas
class OutOfBounds < ChunkyPNG::ExpectationFailed
end
# Exception that is raised when requesting the DPI of a PNG that doesn't
# specify the units of its physical pixel dimensions.
class UnitsUnknown < ChunkyPNG::Exception
end
# Empty byte array. This basically is an empty string, but with the encoding
# set correctly to ASCII-8BIT (binary) in Ruby 1.9.
# @return [String] An empty string, with encoding set to binary in Ruby 1.9
# @private
EMPTY_BYTEARRAY = "".force_encoding(Encoding::BINARY).freeze
# Null-byte, with the encoding set correctly to ASCII-8BIT (binary) in Ruby 1.9.
# @return [String] A binary string, consisting of one NULL-byte.
# @private
EXTRA_BYTE = "\0".force_encoding(Encoding::BINARY).freeze
end
require "chunky_png/version"
# PNG file structure
require "chunky_png/datastream"
require "chunky_png/chunk"
# Colors
require "chunky_png/palette"
require "chunky_png/color"
# Geometry
require "chunky_png/point"
require "chunky_png/vector"
require "chunky_png/dimension"
# Canvas / Image classes
require "chunky_png/canvas"
require "chunky_png/image"