chunky_png 0.7.1 → 0.7.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,8 +3,8 @@ Gem::Specification.new do |s|
3
3
 
4
4
  # Do not change the version and date fields by hand. This will be done
5
5
  # automatically by the gem release script.
6
- s.version = "0.7.1"
7
- s.date = "2010-03-23"
6
+ s.version = "0.7.3"
7
+ s.date = "2010-04-29"
8
8
 
9
9
  s.summary = "Pure ruby library for read/write, chunk-level access to PNG files"
10
10
  s.description = <<-EOT
@@ -27,7 +27,7 @@ module ChunkyPNG
27
27
 
28
28
  # The current version of ChunkyPNG. This value will be updated automatically
29
29
  # by them gem:release rake task.
30
- VERSION = "0.7.1"
30
+ VERSION = "0.7.3"
31
31
 
32
32
  ###################################################
33
33
  # PNG international standard defined constants
@@ -51,4 +51,30 @@ module ChunkyPNG
51
51
  FILTER_UP = 2
52
52
  FILTER_AVERAGE = 3
53
53
  FILTER_PAETH = 4
54
+
55
+ ###################################################
56
+ # ChunkyPNG exception classes
57
+ ###################################################
58
+
59
+ # Default exception class for ChunkyPNG
60
+ class Exception < ::StandardError
61
+ end
62
+
63
+ # Exception that is raised for an unsopported PNG image.
64
+ class NotSupported < ChunkyPNG::Exception
65
+ end
66
+
67
+ # Exception that is raised if the PNG signature is not encountered at the
68
+ # beginning of the file.
69
+ class SignatureMismatch < ChunkyPNG::Exception
70
+ end
71
+
72
+ # Exception that is raised if the CRC check for a block failes
73
+ class CRCMismatch < ChunkyPNG::Exception
74
+ end
75
+
76
+
77
+ # Exception that is raised if an expectation fails.
78
+ class ExpectationFailed < ChunkyPNG::Exception
79
+ end
54
80
  end
@@ -64,7 +64,7 @@ module ChunkyPNG
64
64
  elsif initial.kind_of?(Array) && initial.size == width * height
65
65
  @pixels = initial
66
66
  else
67
- raise "Cannot use this value as initial canvas: #{initial.inspect}!"
67
+ raise ChunkyPNG::ExpectationFailed, "Cannot use this value as initial canvas: #{initial.inspect}!"
68
68
  end
69
69
  end
70
70
 
@@ -53,15 +53,15 @@ module ChunkyPNG
53
53
  end
54
54
 
55
55
  def change_mask_color!(new_color)
56
- raise "This is not a mask image!" if palette.opaque_palette.size != 1
56
+ raise ChunkyPNG::ExpectationFailed, "This is not a mask image!" if palette.opaque_palette.size != 1
57
57
  pixels.map! { |pixel| (new_color & 0xffffff00) | ChunkyPNG::Color.a(pixel) }
58
58
  end
59
59
 
60
60
  protected
61
61
 
62
62
  def check_size_constraints!(other, offset_x, offset_y)
63
- raise "Background image width is too small!" if width < other.width + offset_x
64
- raise "Background image height is too small!" if height < other.height + offset_y
63
+ raise ChunkyPNG::ExpectationFailed, "Background image width is too small!" if width < other.width + offset_x
64
+ raise ChunkyPNG::ExpectationFailed, "Background image height is too small!" if height < other.height + offset_y
65
65
  end
66
66
  end
67
67
  end
@@ -61,7 +61,7 @@ module ChunkyPNG
61
61
  # @param [ChunkyPNG::Datastream] ds The datastream to decode.
62
62
  # @return [ChunkyPNG::Canvas] The canvas decoded from the PNG datastream.
63
63
  def from_datastream(ds)
64
- raise "Only 8-bit color depth is currently supported by ChunkyPNG!" unless ds.header_chunk.depth == 8
64
+ raise ChunkyPNG::NotSupported, "Only 8-bit color depth is currently supported by ChunkyPNG!" unless ds.header_chunk.depth == 8
65
65
 
66
66
  width = ds.header_chunk.width
67
67
  height = ds.header_chunk.height
@@ -83,12 +83,12 @@ module ChunkyPNG
83
83
  # @param [Integer] interlace The interlace method of the encoded pixelstream.
84
84
  # @return [ChunkyPNG::Canvas] The decoded Canvas instance.
85
85
  def decode_png_pixelstream(stream, width, height, color_mode = ChunkyPNG::COLOR_TRUECOLOR, interlace = ChunkyPNG::INTERLACING_NONE)
86
- raise "This palette is not suitable for decoding!" if decoding_palette && !decoding_palette.can_decode?
86
+ raise ChunkyPNG::ExpectationFailed, "This palette is not suitable for decoding!" if decoding_palette && !decoding_palette.can_decode?
87
87
 
88
88
  return case interlace
89
89
  when ChunkyPNG::INTERLACING_NONE then decode_png_without_interlacing(stream, width, height, color_mode)
90
90
  when ChunkyPNG::INTERLACING_ADAM7 then decode_png_with_adam7_interlacing(stream, width, height, color_mode)
91
- else raise "Don't know how the handle interlacing method #{interlace}!"
91
+ else raise ChunkyPNG::NotSupported, "Don't know how the handle interlacing method #{interlace}!"
92
92
  end
93
93
  end
94
94
 
@@ -146,13 +146,13 @@ module ChunkyPNG
146
146
  when ChunkyPNG::COLOR_INDEXED then lambda { |bytes| decoding_palette[bytes.first] }
147
147
  when ChunkyPNG::COLOR_GRAYSCALE then lambda { |bytes| ChunkyPNG::Color.grayscale(*bytes) }
148
148
  when ChunkyPNG::COLOR_GRAYSCALE_ALPHA then lambda { |bytes| ChunkyPNG::Color.grayscale_alpha(*bytes) }
149
- else raise "No suitable pixel decoder found for color mode #{color_mode}!"
149
+ else raise ChunkyPNG::NotSupported, "No suitable pixel decoder found for color mode #{color_mode}!"
150
150
  end
151
151
 
152
152
  pixels = []
153
153
  if width > 0
154
154
 
155
- raise "Invalid stream length!" unless stream.length - start_pos >= width * height * pixel_size + height
155
+ raise ChunkyPNG::ExpectationFailed, "Invalid stream length!" unless stream.length - start_pos >= width * height * pixel_size + height
156
156
 
157
157
  decoded_bytes = Array.new(width * pixel_size, 0)
158
158
  for line_no in 0...height do
@@ -193,7 +193,7 @@ module ChunkyPNG
193
193
  when ChunkyPNG::FILTER_UP then decode_png_scanline_up( bytes, previous_bytes, pixelsize)
194
194
  when ChunkyPNG::FILTER_AVERAGE then decode_png_scanline_average( bytes, previous_bytes, pixelsize)
195
195
  when ChunkyPNG::FILTER_PAETH then decode_png_scanline_paeth( bytes, previous_bytes, pixelsize)
196
- else raise "Unknown filter type"
196
+ else raise ChunkyPNG::NotSupported, "Unknown filter type: #{filter}!"
197
197
  end
198
198
  end
199
199
 
@@ -125,13 +125,13 @@ module ChunkyPNG
125
125
  def encode_png_pixelstream(color_mode = ChunkyPNG::COLOR_TRUECOLOR, interlace = ChunkyPNG::INTERLACING_NONE, compression = ZLib::DEFAULT_COMPRESSION)
126
126
 
127
127
  if color_mode == ChunkyPNG::COLOR_INDEXED && (encoding_palette.nil? || !encoding_palette.can_encode?)
128
- raise "This palette is not suitable for encoding!"
128
+ raise ChunkyPNG::ExpectationFailed, "This palette is not suitable for encoding!"
129
129
  end
130
130
 
131
131
  case interlace
132
132
  when ChunkyPNG::INTERLACING_NONE then encode_png_image_without_interlacing(color_mode, compression)
133
133
  when ChunkyPNG::INTERLACING_ADAM7 then encode_png_image_with_interlacing(color_mode, compression)
134
- else raise "Unknown interlacing method!"
134
+ else raise ChunkyPNG::NotSupported, "Unknown interlacing method: #{interlace}!"
135
135
  end
136
136
  end
137
137
 
@@ -188,7 +188,7 @@ module ChunkyPNG
188
188
  when ChunkyPNG::COLOR_INDEXED then lambda { |color| [encoding_palette.index(color)] }
189
189
  when ChunkyPNG::COLOR_GRAYSCALE then lambda { |color| Color.to_grayscale_bytes(color) }
190
190
  when ChunkyPNG::COLOR_GRAYSCALE_ALPHA then lambda { |color| Color.to_grayscale_alpha_bytes(color) }
191
- else raise "Cannot encode pixels for this mode: #{color_mode}!"
191
+ else raise ChunkyPNG::NotSupported, "Cannot encode pixels for this mode: #{color_mode}!"
192
192
  end
193
193
 
194
194
  previous_bytes = Array.new(pixel_size * width, 0)
@@ -223,7 +223,7 @@ module ChunkyPNG
223
223
  when ChunkyPNG::FILTER_UP then encode_png_scanline_up( bytes, previous_bytes, pixelsize)
224
224
  when ChunkyPNG::FILTER_AVERAGE then encode_png_scanline_average( bytes, previous_bytes, pixelsize)
225
225
  when ChunkyPNG::FILTER_PAETH then encode_png_scanline_paeth( bytes, previous_bytes, pixelsize)
226
- else raise "Unknown filter type"
226
+ else raise ChunkyPNG::NotSupported, "Unknown filter type: #{filter}!"
227
227
  end
228
228
  end
229
229
 
@@ -36,7 +36,7 @@ module ChunkyPNG
36
36
  # is not equal to the expected CRC value.
37
37
  def self.verify_crc!(type, content, found_crc)
38
38
  expected_crc = Zlib.crc32(content, Zlib.crc32(type))
39
- raise "Chuck CRC mismatch!" if found_crc != expected_crc
39
+ raise ChunkyPNG::CRCMismatch, "Chuck CRC mismatch!" if found_crc != expected_crc
40
40
  end
41
41
 
42
42
  # The base chunk class is the superclass for every chunk type. It contains
@@ -246,7 +246,7 @@ module ChunkyPNG
246
246
 
247
247
  def self.read(type, content)
248
248
  keyword, compression, value = content.unpack('Z*Ca*')
249
- raise "Compression method #{compression.inspect} not supported!" unless compression == ChunkyPNG::COMPRESSION_DEFAULT
249
+ raise ChunkyPNG::NotSupported, "Compression method #{compression.inspect} not supported!" unless compression == ChunkyPNG::COMPRESSION_DEFAULT
250
250
  new(keyword, Zlib::Inflate.inflate(value))
251
251
  end
252
252
 
@@ -82,7 +82,7 @@ module ChunkyPNG
82
82
  case str
83
83
  when /^(?:#|0x)?([0-9a-f]{6})$/i then ($1.hex << 8) | 0xff
84
84
  when /^(?:#|0x)?([0-9a-f]{8})$/i then $1.hex
85
- else raise "Not a valid hex color notation: #{str.inspect}!"
85
+ else raise ChunkyPNG::ExpectationFailed, "Not a valid hex color notation: #{str.inspect}!"
86
86
  end
87
87
  end
88
88
 
@@ -396,7 +396,7 @@ module ChunkyPNG
396
396
  when ChunkyPNG::COLOR_TRUECOLOR_ALPHA then 4
397
397
  when ChunkyPNG::COLOR_GRAYSCALE then 1
398
398
  when ChunkyPNG::COLOR_GRAYSCALE_ALPHA then 2
399
- else raise "Don't know the bytesize of pixels in this colormode: #{color_mode}!"
399
+ else raise ChunkyPNG::NotSupported, "Don't know the bytesize of pixels in this colormode: #{color_mode}!"
400
400
  end
401
401
  end
402
402
  end
@@ -98,7 +98,9 @@ module ChunkyPNG
98
98
  # the beginning of the stream.
99
99
  def verify_signature!(io)
100
100
  signature = io.read(ChunkyPNG::Datastream::SIGNATURE.length)
101
- raise "PNG signature not found!" unless signature == ChunkyPNG::Datastream::SIGNATURE
101
+ unless signature == ChunkyPNG::Datastream::SIGNATURE
102
+ raise ChunkyPNG::SignatureMismatch, "PNG signature not found!"
103
+ end
102
104
  end
103
105
  end
104
106
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 7
8
- - 1
9
- version: 0.7.1
8
+ - 3
9
+ version: 0.7.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Willem van Bergen
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-23 00:00:00 -04:00
17
+ date: 2010-04-29 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency