png 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,12 @@
1
1
  # encoding: BINARY
2
2
 
3
- require 'png'
3
+ require "png"
4
4
 
5
5
  class PNG
6
6
  FULL = 360.0
7
7
  HALF = FULL / 2
8
8
 
9
- def self.angle(x, y)
9
+ def self.angle x, y
10
10
  return 0 if x == 0 and y == 0
11
11
  rad_to_deg = 180.0 / Math::PI
12
12
  (Math.atan2(-y, x) * rad_to_deg + 90) % 360
@@ -20,25 +20,26 @@ class PNG
20
20
  # system 'open pie.png'
21
21
 
22
22
  def self.pie_chart(diameter, pct_green,
23
- good_color=PNG::Color::Green, bad_color=PNG::Color::Red)
24
- diameter += 1 if diameter % 2 == 0
23
+ good_color = PNG::Color::Green,
24
+ bad_color = PNG::Color::Red)
25
+ diameter += 1 if diameter.even?
25
26
  radius = (diameter / 2.0).to_i
26
27
  pct_in_deg = FULL * pct_green
27
- rad_to_deg = HALF / Math::PI
28
28
 
29
29
  canvas = PNG::Canvas.new(diameter, diameter)
30
30
 
31
31
  (-radius..radius).each do |x|
32
32
  (-radius..radius).each do |y|
33
33
  magnitude = Math.sqrt(x*x + y*y)
34
- if magnitude <= radius then
35
- angle = PNG.angle(x, y)
36
- color = ((angle <= pct_in_deg) ? good_color : bad_color)
37
34
 
38
- rx, ry = x+radius, y+radius
35
+ next if magnitude > radius
39
36
 
40
- canvas[ rx, ry ] = color
41
- end
37
+ angle = PNG.angle(x, y)
38
+ color = ((angle <= pct_in_deg) ? good_color : bad_color)
39
+
40
+ rx, ry = x+radius, y+radius
41
+
42
+ canvas[rx, ry] = color
42
43
  end
43
44
  end
44
45
 
@@ -1,20 +1,20 @@
1
1
  # encoding: BINARY
2
2
 
3
- require 'png'
4
- require 'enumerator'
3
+ require "png"
4
+ require "enumerator"
5
5
 
6
6
  class PNG
7
7
  def self.load_file path, metadata_only = false
8
- file = File.open(path, 'rb') { |f| f.read }
8
+ file = File.open(path, "rb") { |f| f.read }
9
9
  self.load file, metadata_only
10
10
  end
11
11
 
12
12
  def self.load png, metadata_only = false
13
13
  png = png.dup
14
14
  signature = png.slice! 0, 8
15
- raise ArgumentError, 'Invalid PNG signature' unless signature == SIGNATURE
15
+ raise ArgumentError, "Invalid PNG signature" unless signature == SIGNATURE
16
16
 
17
- ihdr = read_chunk 'IHDR', png
17
+ ihdr = read_chunk "IHDR", png
18
18
 
19
19
  bit_depth, color_type, width, height = read_IHDR ihdr, metadata_only
20
20
 
@@ -22,17 +22,17 @@ class PNG
22
22
 
23
23
  canvas = PNG::Canvas.new width, height
24
24
 
25
- type = png.slice(4, 4).unpack('a4').first
26
- read_chunk type, png if type == 'iCCP' # Ignore color profile
25
+ type = png.slice(4, 4).unpack("a4").first
26
+ read_chunk type, png if type == "iCCP" # Ignore color profile
27
27
 
28
- read_IDAT read_chunk('IDAT', png), bit_depth, color_type, canvas
29
- read_chunk 'IEND', png
28
+ read_IDAT read_chunk("IDAT", png), bit_depth, color_type, canvas
29
+ read_chunk "IEND", png
30
30
 
31
31
  canvas
32
32
  end
33
33
 
34
34
  def self.read_chunk expected_type, png
35
- size, type = png.slice!(0, 8).unpack 'Na4'
35
+ size, type = png.slice!(0, 8).unpack "Na4"
36
36
  data, crc = png.slice!(0, size + 4).unpack "a#{size}N"
37
37
 
38
38
  check_crc type, data, crc
@@ -40,7 +40,7 @@ class PNG
40
40
  raise ArgumentError, "Expected #{expected_type} chunk, not #{type}" unless
41
41
  type == expected_type
42
42
 
43
- return data
43
+ data
44
44
  end
45
45
 
46
46
  def self.check_crc type, data, crc
@@ -49,7 +49,7 @@ class PNG
49
49
  end
50
50
 
51
51
  def self.read_IHDR data, metadata_only = false
52
- width, height, bit_depth, color_type, *rest = data.unpack 'N2C5'
52
+ width, height, bit_depth, color_type, *rest = data.unpack "N2C5"
53
53
 
54
54
  unless metadata_only then
55
55
  raise ArgumentError, "Wrong bit depth: #{bit_depth}" unless
@@ -64,7 +64,7 @@ class PNG
64
64
  end
65
65
 
66
66
  def self.read_IDAT data, bit_depth, color_type, canvas
67
- data = Zlib::Inflate.inflate(data).unpack 'C*'
67
+ data = Zlib::Inflate.inflate(data).unpack "C*"
68
68
 
69
69
  pixel_size = color_type == RGBA ? 4 : 3
70
70
 
@@ -1,30 +1,32 @@
1
+ # encoding: ascii-8bit
2
+
1
3
  dir = File.expand_path "~/.ruby_inline"
2
- if test ?d, dir then
3
- require 'fileutils'
4
+ if File.directory? dir then
5
+ require "fileutils"
4
6
  puts "nuking #{dir}"
5
7
  # force removal, Windoze is bitching at me, something to hunt later...
6
8
  FileUtils.rm_r dir, :force => true
7
9
  end
8
10
 
9
- require 'minitest/autorun'
10
- require 'rubygems'
11
- require 'png'
12
- require 'png/reader'
13
- require 'png/pie'
11
+ require "minitest/autorun"
12
+ require "rubygems"
13
+ require "png"
14
+ require "png/reader"
15
+ require "png/pie"
14
16
 
15
- class TestPng < MiniTest::Unit::TestCase
17
+ class TestPng < Minitest::Test
16
18
  def setup
17
19
  @canvas = PNG::Canvas.new 5, 10, PNG::Color::White
18
20
  @png = PNG.new @canvas
19
21
 
20
- @blob = <<-EOF.unpack('m*').first
22
+ @blob = <<-EOF.unpack("m*").first
21
23
  iVBORw0KGgoAAAANSUhEUgAAAAUAAAAKCAYAAAB8OZQwAAAAD0lEQVR4nGP4
22
24
  jwUwDGVBALuJxzlQugpEAAAAAElFTkSuQmCC
23
25
  EOF
24
26
  end
25
27
 
26
28
  def test_class_chunk
27
- chunk = PNG.chunk 'IHDR', [10, 10, 8, 6, 0, 0, 0 ].pack('N2C5')
29
+ chunk = PNG.chunk "IHDR", [10, 10, 8, 6, 0, 0, 0].pack("N2C5")
28
30
 
29
31
  header_crc = "\2152\317\275"
30
32
  header_data = "\000\000\000\n\000\000\000\n\b\006\000\000\000"
@@ -35,8 +37,8 @@ jwUwDGVBALuJxzlQugpEAAAAAElFTkSuQmCC
35
37
  end
36
38
 
37
39
  def test_class_chunk_empty
38
- chunk = PNG.chunk 'IHDR'
39
- expected = "#{0.chr * 4}IHDR#{["IHDR".png_crc].pack 'N'}"
40
+ chunk = PNG.chunk "IHDR"
41
+ expected = "#{0.chr * 4}IHDR#{["IHDR".png_crc].pack "N"}"
40
42
  assert_equal expected, chunk
41
43
  end
42
44
 
@@ -47,7 +49,7 @@ jwUwDGVBALuJxzlQugpEAAAAAElFTkSuQmCC
47
49
  def test_save
48
50
  path = "blah.png"
49
51
  @png.save(path)
50
- file = File.open(path, 'rb') { |f| f.read }
52
+ file = File.open(path, "rb") { |f| f.read }
51
53
  assert_equal @blob, file
52
54
  ensure
53
55
  assert_equal 1, File.unlink(path)
@@ -55,7 +57,7 @@ jwUwDGVBALuJxzlQugpEAAAAAElFTkSuQmCC
55
57
 
56
58
  end
57
59
 
58
- class TestCanvas < MiniTest::Unit::TestCase
60
+ class TestCanvas < Minitest::Test
59
61
 
60
62
  def setup
61
63
  @canvas = PNG::Canvas.new 5, 10, PNG::Color::White
@@ -70,9 +72,9 @@ class TestCanvas < MiniTest::Unit::TestCase
70
72
  xxxxxxxx
71
73
  xx..xxxx
72
74
  ..xxxxxx
73
- ".gsub(/ /, '')
75
+ ".gsub(/ /, "")
74
76
 
75
- assert_equal expected, canvas1.to_s.gsub(/ /, 'x')
77
+ assert_equal expected, canvas1.to_s.gsub(/ /, "x")
76
78
  end
77
79
 
78
80
  def test_composite_underlay
@@ -84,9 +86,9 @@ class TestCanvas < MiniTest::Unit::TestCase
84
86
  xxxx..xx
85
87
  xx00xxxx
86
88
  ..xxxxxx
87
- ".gsub(/ /, '')
89
+ ".gsub(/ /, "")
88
90
 
89
- assert_equal expected, canvas1.to_s.gsub(/ /, 'x')
91
+ assert_equal expected, canvas1.to_s.gsub(/ /, "x")
90
92
  end
91
93
 
92
94
  def test_composite_overlay
@@ -98,9 +100,9 @@ class TestCanvas < MiniTest::Unit::TestCase
98
100
  xxxx..xx
99
101
  xx..xxxx
100
102
  ..xxxxxx
101
- ".gsub(/ /, '')
103
+ ".gsub(/ /, "")
102
104
 
103
- assert_equal expected, canvas1.to_s.gsub(/ /, 'x')
105
+ assert_equal expected, canvas1.to_s.gsub(/ /, "x")
104
106
  end
105
107
 
106
108
  def test_composite_blend
@@ -112,9 +114,9 @@ class TestCanvas < MiniTest::Unit::TestCase
112
114
  xxxx..xx
113
115
  xx,,xxxx
114
116
  ..xxxxxx
115
- ".gsub(/ /, '')
117
+ ".gsub(/ /, "")
116
118
 
117
- assert_equal expected, canvas1.to_s.gsub(/ /, 'x')
119
+ assert_equal expected, canvas1.to_s.gsub(/ /, "x")
118
120
  end
119
121
 
120
122
  def test_composite_bad_style
@@ -132,17 +134,17 @@ class TestCanvas < MiniTest::Unit::TestCase
132
134
  xxxx..xx
133
135
  xx00xxxx
134
136
  ..xxxxxx
135
- ".gsub(/ /, '')
137
+ ".gsub(/ /, "")
136
138
 
137
- assert_equal expected, canvas1.to_s.gsub(/ /, 'x')
139
+ assert_equal expected, canvas1.to_s.gsub(/ /, "x")
138
140
 
139
141
  canvas2 = canvas1.extract(1, 1, 2, 2)
140
142
 
141
143
  expected = " xx..
142
144
  00xx
143
- ".gsub(/ /, '')
145
+ ".gsub(/ /, "")
144
146
 
145
- assert_equal expected, canvas2.to_s.gsub(/ /, 'x')
147
+ assert_equal expected, canvas2.to_s.gsub(/ /, "x")
146
148
  end
147
149
 
148
150
  def test_index
@@ -152,10 +154,10 @@ class TestCanvas < MiniTest::Unit::TestCase
152
154
 
153
155
  def test_index_tall
154
156
  @canvas = PNG::Canvas.new 2, 4, PNG::Color::White
155
- @canvas[ 0, 0] = PNG::Color::Black
156
- @canvas[ 0, 3] = PNG::Color::Background
157
- @canvas[ 1, 0] = PNG::Color::Yellow
158
- @canvas[ 1, 3] = PNG::Color::Blue
157
+ @canvas[0, 0] = PNG::Color::Black
158
+ @canvas[0, 3] = PNG::Color::Background
159
+ @canvas[1, 0] = PNG::Color::Yellow
160
+ @canvas[1, 3] = PNG::Color::Blue
159
161
 
160
162
  expected = " ,,\n0000\n0000\n..++\n"
161
163
 
@@ -164,10 +166,10 @@ class TestCanvas < MiniTest::Unit::TestCase
164
166
 
165
167
  def test_index_wide
166
168
  @canvas = PNG::Canvas.new 4, 2, PNG::Color::White
167
- @canvas[ 0, 0] = PNG::Color::Black
168
- @canvas[ 3, 0] = PNG::Color::Background
169
- @canvas[ 0, 1] = PNG::Color::Yellow
170
- @canvas[ 3, 1] = PNG::Color::Blue
169
+ @canvas[0, 0] = PNG::Color::Black
170
+ @canvas[3, 0] = PNG::Color::Background
171
+ @canvas[0, 1] = PNG::Color::Yellow
172
+ @canvas[3, 1] = PNG::Color::Blue
171
173
 
172
174
  expected = "++0000,,\n..0000 \n"
173
175
 
@@ -175,23 +177,19 @@ class TestCanvas < MiniTest::Unit::TestCase
175
177
  end
176
178
 
177
179
  def test_index_bad_x
178
- begin
179
- @canvas[6, 1]
180
- rescue => e
181
- assert_equal "bad x value 6 >= 5", e.message
182
- else
183
- flunk "didn't raise"
184
- end
180
+ @canvas[6, 1] # TODO: convert these to assert_raises
181
+ rescue => e
182
+ assert_equal "bad x value 6 >= 5", e.message
183
+ else
184
+ flunk "didn't raise"
185
185
  end
186
186
 
187
187
  def test_index_bad_y
188
- begin
189
- @canvas[1, 11]
190
- rescue => e
191
- assert_equal "bad y value 11 >= 10", e.message
192
- else
193
- flunk "didn't raise"
194
- end
188
+ @canvas[1, 11]
189
+ rescue => e
190
+ assert_equal "bad y value 11 >= 10", e.message
191
+ else
192
+ flunk "didn't raise"
195
193
  end
196
194
 
197
195
  def test_index_equals
@@ -215,28 +213,24 @@ class TestCanvas < MiniTest::Unit::TestCase
215
213
  end
216
214
 
217
215
  def test_index_equals_bad_x
218
- begin
219
- @canvas[6, 1] = PNG::Color::Red
220
- rescue => e
221
- assert_equal "bad x value 6 >= 5", e.message
222
- else
223
- flunk "didn't raise"
224
- end
216
+ @canvas[6, 1] = PNG::Color::Red
217
+ rescue => e
218
+ assert_equal "bad x value 6 >= 5", e.message
219
+ else
220
+ flunk "didn't raise"
225
221
  end
226
222
 
227
223
  def test_index_equals_bad_y
228
- begin
229
- @canvas[1, 11] = PNG::Color::Red
230
- rescue => e
231
- assert_equal "bad y value 11 >= 10", e.message
232
- else
233
- flunk "didn't raise"
234
- end
224
+ @canvas[1, 11] = PNG::Color::Red
225
+ rescue => e
226
+ assert_equal "bad y value 11 >= 10", e.message
227
+ else
228
+ flunk "didn't raise"
235
229
  end
236
230
 
237
- # def test_point
238
- # raise NotImplementedError, 'Need to write test_point'
239
- # end
231
+ # def test_point
232
+ # raise NotImplementedError, 'Need to write test_point'
233
+ # end
240
234
 
241
235
  def test_inspect
242
236
  assert_equal "#<PNG::Canvas 5x10>", @canvas.inspect
@@ -320,29 +314,28 @@ class TestCanvas < MiniTest::Unit::TestCase
320
314
  xxxx..xx
321
315
  xx00xxxx
322
316
  ..xxxxxx
323
- ".gsub(/ /, '')
324
-
325
- assert_equal expected, canvas1.to_s.gsub(/ /, 'x')
317
+ ".gsub(/ /, "")
326
318
 
319
+ assert_equal expected, canvas1.to_s.gsub(/ /, "x")
327
320
 
328
321
  canvas2 = PNG::Canvas.new 2, 2
329
322
  canvas2[0, 0] = PNG::Color::Black
330
323
 
331
- expected = " xxxx
324
+ expected = " xxxx
332
325
  ..xx
333
- ".gsub(/ /, '')
326
+ ".gsub(/ /, "")
334
327
 
335
- assert_equal expected, canvas2.to_s.gsub(/ /, 'x')
328
+ assert_equal expected, canvas2.to_s.gsub(/ /, "x")
336
329
 
337
330
  return canvas1, canvas2
338
331
  end
339
332
 
340
- def util_ascii_art(width, height)
333
+ def util_ascii_art width, height
341
334
  (("0" * width * 2) + "\n") * height
342
335
  end
343
336
  end
344
337
 
345
- class TestPng::TestColor < MiniTest::Unit::TestCase
338
+ class TestPng::TestColor < Minitest::Test
346
339
  def setup
347
340
  @color = PNG::Color.new 0x01, 0x02, 0x03, 0x04
348
341
  end
@@ -380,11 +373,6 @@ class TestPng::TestColor < MiniTest::Unit::TestCase
380
373
  end
381
374
 
382
375
  def test_blend
383
- # c1 = @color
384
- # c2 = PNG::Color.new 0xFF, 0xFE, 0xFD, 0xFC
385
-
386
- # assert_equal PNG::Color.new(0xFB, 0xFA, 0xF9, 0xF8), c1.blend(c2)
387
-
388
376
  c1 = PNG::Color::White
389
377
  c2 = PNG::Color::Black
390
378
 
@@ -420,50 +408,50 @@ class TestPng::TestColor < MiniTest::Unit::TestCase
420
408
  end
421
409
 
422
410
  def test_to_ascii
423
- assert_equal '00', PNG::Color::White.to_ascii, "white"
424
- assert_equal '++', PNG::Color::Yellow.to_ascii, "yellow"
425
- assert_equal ',,', PNG::Color::Red.to_ascii, "red"
426
- assert_equal '..', PNG::Color::Black.to_ascii, "black"
427
- assert_equal ' ', PNG::Color::Background.to_ascii, "background"
411
+ assert_equal "00", PNG::Color::White.to_ascii, "white"
412
+ assert_equal "++", PNG::Color::Yellow.to_ascii, "yellow"
413
+ assert_equal ",,", PNG::Color::Red.to_ascii, "red"
414
+ assert_equal "..", PNG::Color::Black.to_ascii, "black"
415
+ assert_equal " ", PNG::Color::Background.to_ascii, "background"
428
416
  end
429
417
 
430
418
  def test_to_ascii_alpha
431
- assert_equal '00', PNG::Color.new(255,255,255,255).to_ascii
432
- assert_equal '00', PNG::Color.new(255,255,255,192).to_ascii
433
- assert_equal '++', PNG::Color.new(255,255,255,191).to_ascii
434
- assert_equal ',,', PNG::Color.new(255,255,255,127).to_ascii
435
- assert_equal ',,', PNG::Color.new(255,255,255,126).to_ascii
436
- assert_equal ',,', PNG::Color.new(255,255,255, 64).to_ascii
437
- assert_equal '..', PNG::Color.new(255,255,255, 63).to_ascii
438
- assert_equal '..', PNG::Color.new(255,255,255, 1).to_ascii
439
- assert_equal ' ', PNG::Color.new(255,255,255, 0).to_ascii
419
+ assert_equal "00", PNG::Color.new(255, 255, 255, 255).to_ascii
420
+ assert_equal "00", PNG::Color.new(255, 255, 255, 192).to_ascii
421
+ assert_equal "++", PNG::Color.new(255, 255, 255, 191).to_ascii
422
+ assert_equal ",,", PNG::Color.new(255, 255, 255, 127).to_ascii
423
+ assert_equal ",,", PNG::Color.new(255, 255, 255, 126).to_ascii
424
+ assert_equal ",,", PNG::Color.new(255, 255, 255, 64).to_ascii
425
+ assert_equal "..", PNG::Color.new(255, 255, 255, 63).to_ascii
426
+ assert_equal "..", PNG::Color.new(255, 255, 255, 1).to_ascii
427
+ assert_equal " ", PNG::Color.new(255, 255, 255, 0).to_ascii
440
428
  end
441
429
 
442
430
  def test_to_s_name
443
- assert_equal 'Red', PNG::Color::Red.to_s
431
+ assert_equal "Red", PNG::Color::Red.to_s
444
432
  end
445
433
 
446
434
  def test_to_s
447
- obj = PNG::Color.new(255,255,255, 0)
448
- assert_equal '#<PNG::Color:0xXXXXXX>', obj.to_s.sub(/0x[0-9a-f]+/, '0xXXXXXX')
435
+ obj = PNG::Color.new(255, 255, 255, 0)
436
+ assert_equal "#<PNG::Color:0xXXXXXX>", obj.to_s.sub(/0x[0-9a-f]+/, "0xXXXXXX")
449
437
  end
450
438
 
451
439
  def test_equals2
452
- assert_equal PNG::Color.new(255,255,255, 0), PNG::Color.new(255,255,255, 0)
440
+ assert_equal PNG::Color.new(255, 255, 255, 0), PNG::Color.new(255, 255, 255, 0)
453
441
  end
454
442
 
455
443
  def test_hash
456
- a = PNG::Color.new(255,255,255, 0)
457
- b = PNG::Color.new(255,255,255, 0)
444
+ a = PNG::Color.new(255, 255, 255, 0)
445
+ b = PNG::Color.new(255, 255, 255, 0)
458
446
  assert_equal a.hash, b.hash
459
447
  end
460
448
 
461
- # def test_values
462
- # raise NotImplementedError, 'Need to write test_values'
463
- # end
449
+ # def test_values
450
+ # raise NotImplementedError, 'Need to write test_values'
451
+ # end
464
452
  end
465
453
 
466
- class TestPng::TestPie < MiniTest::Unit::TestCase
454
+ class TestPng::TestPie < Minitest::Test
467
455
  def test_pie_chart_odd
468
456
  expected =
469
457
  [" .. ",
@@ -477,9 +465,9 @@ class TestPng::TestPie < MiniTest::Unit::TestCase
477
465
  " ,,,,,,,,,,,,,,,,,, ",
478
466
  " ,,,,,,,,,,,,,, ",
479
467
  " ,, ",
480
- nil].join("\n")
468
+ nil].join("\n")
481
469
 
482
- actual = PNG::pie_chart(11, 0.25, PNG::Color::Black, PNG::Color::Green)
470
+ actual = PNG.pie_chart(11, 0.25, PNG::Color::Black, PNG::Color::Green)
483
471
  assert_equal expected, actual.to_s
484
472
  end
485
473
 
@@ -496,13 +484,13 @@ class TestPng::TestPie < MiniTest::Unit::TestCase
496
484
  " ,,,,,,,,,,,,,,,,,, ",
497
485
  " ,,,,,,,,,,,,,, ",
498
486
  " ,, ",
499
- nil].join("\n")
487
+ nil].join("\n")
500
488
 
501
- actual = PNG::pie_chart(10, 0.25, PNG::Color::Black, PNG::Color::Green)
489
+ actual = PNG.pie_chart(10, 0.25, PNG::Color::Black, PNG::Color::Green)
502
490
  assert_equal expected, actual.to_s
503
491
  end
504
492
 
505
- def util_angle(expect, x, y)
493
+ def util_angle expect, x, y
506
494
  actual = PNG.angle(x, y)
507
495
  case expect
508
496
  when Integer then