png 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data/History.txt +29 -1
- data/README.txt +12 -10
- data/Rakefile +7 -10
- data/example/lines.rb +4 -4
- data/example/profile.rb +1 -1
- data/example/profile_lines.rb +1 -1
- data/lib/png/font.rb +7 -9
- data/lib/png/pie.rb +12 -13
- data/lib/png/reader.rb +13 -15
- data/lib/png.rb +57 -71
- data/test/test_png.rb +107 -135
- data/test/test_png_font.rb +13 -21
- data/test/test_png_reader.rb +17 -27
- data.tar.gz.sig +0 -0
- metadata +89 -61
- metadata.gz.sig +0 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ed4cb5ea785c269b2b1528b2630d99b24a3bc99a06c4a50961dc3c681959a169
|
4
|
+
data.tar.gz: f0a06a6423b7159878cbf5ed844e3600e32fec055e9fa46ace12e7d2329a474d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f281dfb508c5c4ccc5bd82b69e69266b8931e198fa0a8deef3b47d3d13e22ca75b66b9df0d4d37ab44b9581fe3db8b9ef74a00f75a274afa6c1da9a0e8e49f0e
|
7
|
+
data.tar.gz: 6b9c5ad8ba80a4c8ed527b56c271de1529b541c9b0f4dac533dc1568a2cf9263f3800ff51336de65e6d3aa94f2c7f7747dacbfd852108495716e69858efa95e9
|
checksums.yaml.gz.sig
ADDED
Binary file
|
data/History.txt
CHANGED
@@ -1,3 +1,29 @@
|
|
1
|
+
=== 1.3.0 / 2024-08-27
|
2
|
+
|
3
|
+
Daaaamn... another 9 years! Means it is really stable, right?
|
4
|
+
|
5
|
+
* 4 minor enhancements:
|
6
|
+
|
7
|
+
* Removed a bunch of ancient cruft: getbyte, old 1.8 compile hacks, etc.
|
8
|
+
* Removed a bunch of forced deletions of ~/.ruby_inline in test. Cargo culting?
|
9
|
+
* Removed binary encoding on all source files and called #b for the few needed binary strings.
|
10
|
+
* Removed clean as a dependency for test, speeding up tests 3x.
|
11
|
+
|
12
|
+
* 1 bug fix:
|
13
|
+
|
14
|
+
* Fix errors created when string literals are frozen.
|
15
|
+
|
16
|
+
=== 1.2.1 / 2015-04-13
|
17
|
+
|
18
|
+
WOW! SIX YEARS!!
|
19
|
+
|
20
|
+
* 4 bug fixes:
|
21
|
+
|
22
|
+
* 1.9: quelled warnings
|
23
|
+
* 2.0: Added ascii-8bit encoding to test file to bypass new utf-8 default
|
24
|
+
* Rakefile cleanup. Use isolate. Nuke pathing.
|
25
|
+
* Removed dead rubyforge setting in Rakefile
|
26
|
+
|
1
27
|
=== 1.2.0 / 2009-06-23
|
2
28
|
|
3
29
|
* 26 minor enhancements:
|
@@ -56,5 +82,7 @@
|
|
56
82
|
|
57
83
|
=== 1.0.0 / 2006-09-31
|
58
84
|
|
59
|
-
*
|
85
|
+
* 1 major enhancement:
|
86
|
+
|
87
|
+
* Birthday!
|
60
88
|
|
data/README.txt
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
= PNG
|
2
2
|
|
3
|
-
|
3
|
+
home :: https://github.com/seattlerb/png
|
4
|
+
bugs :: https://github.com/seattlerb/png/issues
|
5
|
+
doco :: http://docs.seattlerb.org/png
|
4
6
|
|
5
7
|
== DESCRIPTION
|
6
8
|
|
@@ -14,20 +16,20 @@ without any C libraries.
|
|
14
16
|
* Basic PNG reader as well (someday it might do compositing and the like!).
|
15
17
|
* Almost pure ruby, does require a compiler.
|
16
18
|
|
17
|
-
==
|
19
|
+
== SYNOPSIS
|
20
|
+
|
21
|
+
require "png"
|
18
22
|
|
19
|
-
require 'png'
|
20
|
-
|
21
23
|
canvas = PNG::Canvas.new 200, 200
|
22
|
-
|
24
|
+
|
23
25
|
# Set a point to a color
|
24
26
|
canvas[100, 100] = PNG::Color::Black
|
25
|
-
|
27
|
+
|
26
28
|
# draw an anti-aliased line
|
27
29
|
canvas.line 50, 50, 100, 50, PNG::Color::Blue
|
28
|
-
|
30
|
+
|
29
31
|
png = PNG.new canvas
|
30
|
-
png.save
|
32
|
+
png.save "blah.png"
|
31
33
|
|
32
34
|
== REQUIREMENTS
|
33
35
|
|
@@ -37,13 +39,13 @@ without any C libraries.
|
|
37
39
|
|
38
40
|
== INSTALL
|
39
41
|
|
40
|
-
+ sudo gem install
|
42
|
+
+ sudo gem install png
|
41
43
|
|
42
44
|
== LICENSE
|
43
45
|
|
44
46
|
(The MIT License)
|
45
47
|
|
46
|
-
Copyright (c)
|
48
|
+
Copyright (c) Ryan Davis, Eric Hodel, Zen Spider Software
|
47
49
|
|
48
50
|
Permission is hereby granted, free of charge, to any person obtaining
|
49
51
|
a copy of this software and associated documentation files (the
|
data/Rakefile
CHANGED
@@ -1,18 +1,15 @@
|
|
1
|
-
|
2
|
-
$: << "../../hoe/dev/lib"
|
1
|
+
require "hoe"
|
3
2
|
|
4
|
-
require 'hoe'
|
5
|
-
|
6
|
-
Hoe.add_include_dirs "../../hoe/dev/lib" # HACK remove
|
7
|
-
Hoe.add_include_dirs "../../RubyInline/dev/lib", "lib"
|
8
3
|
Hoe.plugin :seattlerb
|
9
4
|
Hoe.plugin :inline
|
5
|
+
Hoe.plugin :isolate
|
10
6
|
|
11
|
-
Hoe.spec
|
12
|
-
developer
|
13
|
-
developer 'Eric Hodel', 'drbrain@segment7.net'
|
7
|
+
Hoe.spec "png" do
|
8
|
+
developer "Ryan Davis", "ryand-ruby@zenspider.com"
|
14
9
|
|
15
|
-
|
10
|
+
license "MIT"
|
16
11
|
end
|
17
12
|
|
13
|
+
task(:test).prerequisites.delete "clean" # TODO: consider removing from hoe?
|
14
|
+
|
18
15
|
# vim: syntax=Ruby
|
data/example/lines.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/local/bin/ruby -w
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "png"
|
4
|
+
require "png/font"
|
5
5
|
|
6
6
|
canvas = PNG::Canvas.new 201, 201, PNG::Color::White
|
7
7
|
|
@@ -13,8 +13,8 @@ canvas.line 100, 50, 200, 75, PNG::Color::Green
|
|
13
13
|
canvas.line 0, 200, 200, 0, PNG::Color::Black
|
14
14
|
canvas.line 0, 200, 150, 0, PNG::Color::Red
|
15
15
|
|
16
|
-
canvas.annotate
|
16
|
+
canvas.annotate "Hello World", 10, 10
|
17
17
|
|
18
18
|
png = PNG.new canvas
|
19
|
-
png.save
|
19
|
+
png.save "blah.png"
|
20
20
|
`open blah.png`
|
data/example/profile.rb
CHANGED
data/example/profile_lines.rb
CHANGED
data/lib/png/font.rb
CHANGED
@@ -1,14 +1,12 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'png/reader'
|
1
|
+
require "png/reader"
|
4
2
|
|
5
3
|
##
|
6
4
|
# Implements a simple bitmap font by extracting letters from a PNG.
|
7
5
|
|
8
6
|
class PNG::Font
|
9
|
-
LETTERS = ((
|
10
|
-
(
|
11
|
-
(
|
7
|
+
LETTERS = (("A".."Z").to_a +
|
8
|
+
("a".."z").to_a +
|
9
|
+
("0".."9").to_a + [" "] * 16 +
|
12
10
|
'({[<!@#$%^&*?_+-=;,"/~>]})'.split(//))
|
13
11
|
|
14
12
|
attr_reader :height, :width, :canvas
|
@@ -17,7 +15,7 @@ class PNG::Font
|
|
17
15
|
@@default ||= new(File.join(File.dirname(__FILE__), "default_font.png"))
|
18
16
|
end
|
19
17
|
|
20
|
-
def initialize
|
18
|
+
def initialize png_file
|
21
19
|
@canvas = PNG.load_file png_file
|
22
20
|
@height, @width = canvas.height / 4, canvas.width / 26
|
23
21
|
@cache = {}
|
@@ -50,8 +48,8 @@ class PNG::Canvas
|
|
50
48
|
#
|
51
49
|
# require 'png/font'
|
52
50
|
|
53
|
-
def annotate
|
54
|
-
font = PNG::Font.default, align = :left, style = :overwrite
|
51
|
+
def annotate string, x, y,
|
52
|
+
font = PNG::Font.default, align = :left, style = :overwrite
|
55
53
|
case align
|
56
54
|
when :left then
|
57
55
|
# do nothing
|
data/lib/png/pie.rb
CHANGED
@@ -1,12 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'png'
|
1
|
+
require "png"
|
4
2
|
|
5
3
|
class PNG
|
6
4
|
FULL = 360.0
|
7
5
|
HALF = FULL / 2
|
8
6
|
|
9
|
-
def self.angle
|
7
|
+
def self.angle x, y
|
10
8
|
return 0 if x == 0 and y == 0
|
11
9
|
rad_to_deg = 180.0 / Math::PI
|
12
10
|
(Math.atan2(-y, x) * rad_to_deg + 90) % 360
|
@@ -20,25 +18,26 @@ class PNG
|
|
20
18
|
# system 'open pie.png'
|
21
19
|
|
22
20
|
def self.pie_chart(diameter, pct_green,
|
23
|
-
|
24
|
-
|
21
|
+
good_color = PNG::Color::Green,
|
22
|
+
bad_color = PNG::Color::Red)
|
23
|
+
diameter += 1 if diameter.even?
|
25
24
|
radius = (diameter / 2.0).to_i
|
26
25
|
pct_in_deg = FULL * pct_green
|
27
|
-
rad_to_deg = HALF / Math::PI
|
28
26
|
|
29
27
|
canvas = PNG::Canvas.new(diameter, diameter)
|
30
28
|
|
31
29
|
(-radius..radius).each do |x|
|
32
30
|
(-radius..radius).each do |y|
|
33
31
|
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
32
|
|
38
|
-
|
33
|
+
next if magnitude > radius
|
34
|
+
|
35
|
+
angle = PNG.angle(x, y)
|
36
|
+
color = ((angle <= pct_in_deg) ? good_color : bad_color)
|
37
|
+
|
38
|
+
rx, ry = x+radius, y+radius
|
39
39
|
|
40
|
-
|
41
|
-
end
|
40
|
+
canvas[rx, ry] = color
|
42
41
|
end
|
43
42
|
end
|
44
43
|
|
data/lib/png/reader.rb
CHANGED
@@ -1,20 +1,18 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'png'
|
4
|
-
require 'enumerator'
|
1
|
+
require "png"
|
2
|
+
require "enumerator"
|
5
3
|
|
6
4
|
class PNG
|
7
5
|
def self.load_file path, metadata_only = false
|
8
|
-
file = File.open(path,
|
6
|
+
file = File.open(path, "rb") { |f| f.read }
|
9
7
|
self.load file, metadata_only
|
10
8
|
end
|
11
9
|
|
12
10
|
def self.load png, metadata_only = false
|
13
11
|
png = png.dup
|
14
12
|
signature = png.slice! 0, 8
|
15
|
-
raise ArgumentError,
|
13
|
+
raise ArgumentError, "Invalid PNG signature" unless signature == SIGNATURE
|
16
14
|
|
17
|
-
ihdr = read_chunk
|
15
|
+
ihdr = read_chunk "IHDR", png
|
18
16
|
|
19
17
|
bit_depth, color_type, width, height = read_IHDR ihdr, metadata_only
|
20
18
|
|
@@ -22,17 +20,17 @@ class PNG
|
|
22
20
|
|
23
21
|
canvas = PNG::Canvas.new width, height
|
24
22
|
|
25
|
-
type = png.slice(4, 4).unpack(
|
26
|
-
read_chunk type, png if type ==
|
23
|
+
type = png.slice(4, 4).unpack("a4").first
|
24
|
+
read_chunk type, png if type == "iCCP" # Ignore color profile
|
27
25
|
|
28
|
-
read_IDAT read_chunk(
|
29
|
-
read_chunk
|
26
|
+
read_IDAT read_chunk("IDAT", png), bit_depth, color_type, canvas
|
27
|
+
read_chunk "IEND", png
|
30
28
|
|
31
29
|
canvas
|
32
30
|
end
|
33
31
|
|
34
32
|
def self.read_chunk expected_type, png
|
35
|
-
size, type = png.slice!(0, 8).unpack
|
33
|
+
size, type = png.slice!(0, 8).unpack "Na4"
|
36
34
|
data, crc = png.slice!(0, size + 4).unpack "a#{size}N"
|
37
35
|
|
38
36
|
check_crc type, data, crc
|
@@ -40,7 +38,7 @@ class PNG
|
|
40
38
|
raise ArgumentError, "Expected #{expected_type} chunk, not #{type}" unless
|
41
39
|
type == expected_type
|
42
40
|
|
43
|
-
|
41
|
+
data
|
44
42
|
end
|
45
43
|
|
46
44
|
def self.check_crc type, data, crc
|
@@ -49,7 +47,7 @@ class PNG
|
|
49
47
|
end
|
50
48
|
|
51
49
|
def self.read_IHDR data, metadata_only = false
|
52
|
-
width, height, bit_depth, color_type, *rest = data.unpack
|
50
|
+
width, height, bit_depth, color_type, *rest = data.unpack "N2C5"
|
53
51
|
|
54
52
|
unless metadata_only then
|
55
53
|
raise ArgumentError, "Wrong bit depth: #{bit_depth}" unless
|
@@ -64,7 +62,7 @@ class PNG
|
|
64
62
|
end
|
65
63
|
|
66
64
|
def self.read_IDAT data, bit_depth, color_type, canvas
|
67
|
-
data = Zlib::Inflate.inflate(data).unpack
|
65
|
+
data = Zlib::Inflate.inflate(data).unpack "C*"
|
68
66
|
|
69
67
|
pixel_size = color_type == RGBA ? 4 : 3
|
70
68
|
|
data/lib/png.rb
CHANGED
@@ -1,14 +1,5 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'rubygems'
|
4
|
-
require 'zlib'
|
5
|
-
require 'inline'
|
6
|
-
|
7
|
-
unless "".respond_to? :getbyte then
|
8
|
-
class String
|
9
|
-
alias :getbyte :[]
|
10
|
-
end
|
11
|
-
end
|
1
|
+
require "zlib"
|
2
|
+
require "inline"
|
12
3
|
|
13
4
|
class String # :nodoc: # ZenTest SKIP
|
14
5
|
inline do |builder|
|
@@ -16,6 +7,10 @@ class String # :nodoc: # ZenTest SKIP
|
|
16
7
|
unsigned long png_crc() {
|
17
8
|
static unsigned long crc[256];
|
18
9
|
static char crc_table_computed = 0;
|
10
|
+
unsigned long c = 0xffffffff;
|
11
|
+
size_t len = RSTRING_LEN(self);
|
12
|
+
char * s = StringValuePtr(self);
|
13
|
+
unsigned i;
|
19
14
|
|
20
15
|
if (! crc_table_computed) {
|
21
16
|
unsigned long c;
|
@@ -31,11 +26,6 @@ class String # :nodoc: # ZenTest SKIP
|
|
31
26
|
crc_table_computed = 1;
|
32
27
|
}
|
33
28
|
|
34
|
-
unsigned long c = 0xffffffff;
|
35
|
-
unsigned len = RSTRING_LEN(self);
|
36
|
-
char * s = StringValuePtr(self);
|
37
|
-
unsigned i;
|
38
|
-
|
39
29
|
for (i = 0; i < len; i++) {
|
40
30
|
c = crc[(c ^ s[i]) & 0xff] ^ (c >> 8);
|
41
31
|
}
|
@@ -45,7 +35,7 @@ class String # :nodoc: # ZenTest SKIP
|
|
45
35
|
EOM
|
46
36
|
end
|
47
37
|
rescue CompilationError => e
|
48
|
-
warn "
|
38
|
+
warn "COMPILATION ERROR: #{e}"
|
49
39
|
|
50
40
|
unless defined? @@crc then
|
51
41
|
@@crc = Array.new(256)
|
@@ -61,12 +51,12 @@ rescue CompilationError => e
|
|
61
51
|
##
|
62
52
|
# Calculates a CRC using the algorithm in the PNG specification.
|
63
53
|
|
64
|
-
def png_crc
|
54
|
+
def png_crc
|
65
55
|
c = 0xffffffff
|
66
56
|
each_byte do |b|
|
67
57
|
c = @@crc[(c^b) & 0xff] ^ (c >> 8)
|
68
58
|
end
|
69
|
-
|
59
|
+
c ^ 0xffffffff
|
70
60
|
end
|
71
61
|
end
|
72
62
|
|
@@ -99,7 +89,7 @@ end
|
|
99
89
|
# bottom left.
|
100
90
|
|
101
91
|
class PNG
|
102
|
-
VERSION =
|
92
|
+
VERSION = "1.3.0"
|
103
93
|
SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10].pack("C*")
|
104
94
|
|
105
95
|
# Color Types:
|
@@ -118,21 +108,15 @@ class PNG
|
|
118
108
|
|
119
109
|
begin
|
120
110
|
inline do |builder|
|
121
|
-
if RUBY_VERSION < "1.8.6" then
|
122
|
-
builder.prefix <<-EOM
|
123
|
-
#define RARRAY_PTR(s) (RARRAY(s)->ptr)
|
124
|
-
#define RARRAY_LEN(s) (RARRAY(s)->len)
|
125
|
-
EOM
|
126
|
-
end
|
127
|
-
|
128
111
|
builder.c <<-EOM
|
129
112
|
VALUE png_join() {
|
130
|
-
|
131
|
-
VALUE
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
char * result
|
113
|
+
size_t i, j;
|
114
|
+
VALUE data = rb_iv_get(self, "@data");
|
115
|
+
size_t data_len = RARRAY_LEN(data);
|
116
|
+
size_t row_len = RARRAY_LEN(RARRAY_PTR(data)[0]);
|
117
|
+
size_t size = data_len * (1 + (row_len * 4));
|
118
|
+
char * result = malloc(size);
|
119
|
+
|
136
120
|
unsigned long idx = 0;
|
137
121
|
for (i = 0; i < data_len; i++) {
|
138
122
|
VALUE row = RARRAY_PTR(data)[i];
|
@@ -153,21 +137,21 @@ class PNG
|
|
153
137
|
end
|
154
138
|
rescue CompilationError
|
155
139
|
def png_join
|
156
|
-
@data.map { |row| "\0" + row.map
|
140
|
+
@data.map { |row| "\0".b + row.map(&:values).join }.join
|
157
141
|
end
|
158
142
|
end
|
159
143
|
|
160
144
|
##
|
161
145
|
# Creates a PNG chunk of type +type+ that contains +data+.
|
162
146
|
|
163
|
-
def self.chunk
|
147
|
+
def self.chunk type, data = ""
|
164
148
|
[data.size, type, data, (type + data).png_crc].pack("Na*a*N")
|
165
149
|
end
|
166
150
|
|
167
151
|
##
|
168
152
|
# Creates a new PNG object using +canvas+
|
169
153
|
|
170
|
-
def initialize
|
154
|
+
def initialize canvas
|
171
155
|
@height = canvas.height
|
172
156
|
@width = canvas.width
|
173
157
|
@bits = 8
|
@@ -177,8 +161,8 @@ class PNG
|
|
177
161
|
##
|
178
162
|
# Writes the PNG to +path+.
|
179
163
|
|
180
|
-
def save
|
181
|
-
File.open path,
|
164
|
+
def save path
|
165
|
+
File.open path, "wb" do |f|
|
182
166
|
f.write to_blob
|
183
167
|
end
|
184
168
|
end
|
@@ -192,9 +176,9 @@ class PNG
|
|
192
176
|
header = [@width, @height, @bits, RGBA, NONE, NONE, NONE]
|
193
177
|
|
194
178
|
blob << SIGNATURE
|
195
|
-
blob << PNG.chunk(
|
196
|
-
blob << PNG.chunk(
|
197
|
-
blob << PNG.chunk(
|
179
|
+
blob << PNG.chunk("IHDR", header.pack("N2C5"))
|
180
|
+
blob << PNG.chunk("IDAT", Zlib::Deflate.deflate(self.png_join))
|
181
|
+
blob << PNG.chunk("IEND", "")
|
198
182
|
blob.join
|
199
183
|
end
|
200
184
|
|
@@ -204,7 +188,7 @@ class PNG
|
|
204
188
|
|
205
189
|
class Color
|
206
190
|
|
207
|
-
MAX=
|
191
|
+
MAX = 0xFF
|
208
192
|
|
209
193
|
attr_reader :values
|
210
194
|
|
@@ -214,7 +198,7 @@ class PNG
|
|
214
198
|
|
215
199
|
def self.from str, name = nil
|
216
200
|
str = "%08x" % str if Integer === str
|
217
|
-
colors = str.scan(
|
201
|
+
colors = str.scan(/\h\h/i).map(&:hex)
|
218
202
|
colors << name
|
219
203
|
self.new(*colors)
|
220
204
|
end
|
@@ -223,7 +207,7 @@ class PNG
|
|
223
207
|
# Creates a new color with values +red+, +green+, +blue+, and +alpha+.
|
224
208
|
|
225
209
|
def initialize red, green, blue, alpha = MAX, name = nil
|
226
|
-
@values = "%c%c%c%c" % [red, green, blue, alpha]
|
210
|
+
@values = "%c%c%c%c" % [red, green, blue, alpha].map(&:chr)
|
227
211
|
@name = name
|
228
212
|
end
|
229
213
|
|
@@ -255,8 +239,8 @@ class PNG
|
|
255
239
|
# "Bitwise or" as applied to colors. Background color is
|
256
240
|
# considered false.
|
257
241
|
|
258
|
-
def |
|
259
|
-
self == Background ?
|
242
|
+
def | other
|
243
|
+
self == Background ? other : self
|
260
244
|
end
|
261
245
|
|
262
246
|
def hash # :nodoc:
|
@@ -294,15 +278,17 @@ class PNG
|
|
294
278
|
# Blends +color+ into this color returning a new blended color.
|
295
279
|
|
296
280
|
def blend color
|
297
|
-
|
298
|
-
|
281
|
+
Color.new(((r + color.r) / 2),
|
282
|
+
((g + color.g) / 2),
|
283
|
+
((b + color.b) / 2),
|
284
|
+
((a + color.a) / 2))
|
299
285
|
end
|
300
286
|
|
301
287
|
##
|
302
288
|
# Returns a new color with an alpha value adjusted by +i+.
|
303
289
|
|
304
290
|
def intensity i
|
305
|
-
|
291
|
+
Color.new(r, g, b, (a*i) >> 8)
|
306
292
|
end
|
307
293
|
|
308
294
|
def inspect # :nodoc:
|
@@ -318,11 +304,11 @@ class PNG
|
|
318
304
|
# art!
|
319
305
|
|
320
306
|
def to_ascii
|
321
|
-
return
|
307
|
+
return " " if a == 0x00
|
322
308
|
|
323
309
|
brightness = (((r + g + b) / 3) * a) / 0xFF
|
324
310
|
|
325
|
-
%w
|
311
|
+
%w[.. ,, ++ 00][brightness / 64]
|
326
312
|
end
|
327
313
|
|
328
314
|
def to_s # :nodoc:
|
@@ -341,26 +327,26 @@ class PNG
|
|
341
327
|
unless s == 0.0 then
|
342
328
|
h += 255 if h < 0
|
343
329
|
h = h / 255.0 * 6.0
|
344
|
-
s
|
345
|
-
v
|
330
|
+
s /= 255.0
|
331
|
+
v /= 255.0
|
346
332
|
i = h.floor
|
347
333
|
f = h - i
|
348
334
|
p = v * (1 - (s))
|
349
335
|
q = v * (1 - (s * (f)))
|
350
336
|
w = v * (1 - (s * (1-f)))
|
351
337
|
r, g, b = case i
|
352
|
-
when 0,6 then
|
353
|
-
[
|
338
|
+
when 0, 6 then
|
339
|
+
[v, w, p]
|
354
340
|
when 1 then
|
355
|
-
[
|
341
|
+
[q, v, p]
|
356
342
|
when 2 then
|
357
|
-
[
|
343
|
+
[p, v, w]
|
358
344
|
when 3 then
|
359
|
-
[
|
345
|
+
[p, q, v]
|
360
346
|
when 4 then
|
361
|
-
[
|
347
|
+
[w, p, v]
|
362
348
|
when 5 then
|
363
|
-
[
|
349
|
+
[v, p, q]
|
364
350
|
else
|
365
351
|
raise [h, s, v, i, f, p, q, w].inspect
|
366
352
|
end
|
@@ -420,7 +406,7 @@ class PNG
|
|
420
406
|
def initialize width, height, background = Color::Background
|
421
407
|
@width = width
|
422
408
|
@height = height
|
423
|
-
@data = Array.new(@height) {
|
409
|
+
@data = Array.new(@height) { Array.new(@width, background) }
|
424
410
|
end
|
425
411
|
|
426
412
|
##
|
@@ -489,14 +475,14 @@ class PNG
|
|
489
475
|
end
|
490
476
|
|
491
477
|
def inspect # :nodoc:
|
492
|
-
|
478
|
+
"#<%s %dx%d>" % [self.class, @width, @height]
|
493
479
|
end
|
494
480
|
|
495
481
|
##
|
496
482
|
# Blends +color+ onto the color at point (+x+, +y+).
|
497
483
|
|
498
|
-
def point
|
499
|
-
self[x,y] = self[x,y].blend(color)
|
484
|
+
def point x, y, color
|
485
|
+
self[x, y] = self[x, y].blend(color)
|
500
486
|
end
|
501
487
|
|
502
488
|
##
|
@@ -504,7 +490,7 @@ class PNG
|
|
504
490
|
#
|
505
491
|
# http://en.wikipedia.org/wiki/Xiaolin_Wu's_line_algorithm
|
506
492
|
|
507
|
-
def line
|
493
|
+
def line x0, y0, x1, y1, color
|
508
494
|
y0, y1, x0, x1 = y1, y0, x1, x0 if y0 > y1
|
509
495
|
dx = x1 - x0
|
510
496
|
sx = dx < 0 ? -1 : 1
|
@@ -513,7 +499,7 @@ class PNG
|
|
513
499
|
|
514
500
|
# 'easy' cases
|
515
501
|
if dy == 0 then
|
516
|
-
Range.new(*[x0,x1].sort).each do |x|
|
502
|
+
Range.new(*[x0, x1].sort).each do |x|
|
517
503
|
point(x, y0, color)
|
518
504
|
end
|
519
505
|
return
|
@@ -539,12 +525,12 @@ class PNG
|
|
539
525
|
e_acc = 0
|
540
526
|
if dy > dx then # vertical displacement
|
541
527
|
e = (dx << 16) / dy
|
542
|
-
(y0...y1-1).each do
|
528
|
+
(y0...y1-1).each do
|
543
529
|
e_acc_temp, e_acc = e_acc, (e_acc + e) & 0xFFFF
|
544
|
-
x0
|
530
|
+
x0 += sx if (e_acc <= e_acc_temp)
|
545
531
|
w = 0xFF-(e_acc >> 8)
|
546
532
|
point(x0, y0, color.intensity(w))
|
547
|
-
y0
|
533
|
+
y0 += 1
|
548
534
|
point(x0 + sx, y0, color.intensity(0xFF-w))
|
549
535
|
end
|
550
536
|
point(x1, y1, color)
|
@@ -553,7 +539,7 @@ class PNG
|
|
553
539
|
|
554
540
|
# horizontal displacement
|
555
541
|
e = (dy << 16) / dx
|
556
|
-
(dx - 1).downto(0) do
|
542
|
+
(dx - 1).downto(0) do
|
557
543
|
e_acc_temp, e_acc = e_acc, (e_acc + e) & 0xFFFF
|
558
544
|
y0 += 1 if (e_acc <= e_acc_temp)
|
559
545
|
w = 0xFF-(e_acc >> 8)
|
@@ -580,7 +566,7 @@ class PNG
|
|
580
566
|
image << "\n"
|
581
567
|
end
|
582
568
|
|
583
|
-
|
569
|
+
image.join
|
584
570
|
end
|
585
571
|
end # Canvas
|
586
572
|
end
|