png 1.2.0 → 1.2.1
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 +4 -0
- data.tar.gz.sig +4 -2
- data/.gemtest +0 -0
- data/History.txt +14 -1
- data/README.txt +1 -1
- data/Rakefile +4 -11
- data/example/lines.rb +4 -4
- data/example/profile.rb +1 -1
- data/example/profile_lines.rb +1 -1
- data/lib/png.rb +54 -54
- data/lib/png/font.rb +7 -7
- data/lib/png/pie.rb +12 -11
- data/lib/png/reader.rb +13 -13
- data/test/test_png.rb +95 -107
- data/test/test_png_font.rb +15 -15
- data/test/test_png_reader.rb +19 -21
- metadata +92 -57
- metadata.gz.sig +0 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6a402194fe1cbe6b3c0046da8f2762b0eb663dfb
|
4
|
+
data.tar.gz: 3abefbe75ec174dae717a1f951fb1f052fca2a4e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 90dde7c2914c3b20dd54ff1b0bb1a358fffe9896ef5fa13663c44cc3d2dac64e9db53687b7144e7fb14806d009baea163259c51eac8d03e510b2136325f35d1b
|
7
|
+
data.tar.gz: 00531adf917fae6c8cb8d082189dc40584bbc8152c93ad7d0126c740dd9d1f4e4984ac692402b69b79df378b8c30b482e29831845dac0ebcea8a97186866a833
|
checksums.yaml.gz.sig
ADDED
data.tar.gz.sig
CHANGED
data/.gemtest
ADDED
File without changes
|
data/History.txt
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
=== 1.2.1 / 2015-04-13
|
2
|
+
|
3
|
+
WOW! SIX YEARS!!
|
4
|
+
|
5
|
+
* 4 bug fixes:
|
6
|
+
|
7
|
+
* 1.9: quelled warnings
|
8
|
+
* 2.0: Added ascii-8bit encoding to test file to bypass new utf-8 default
|
9
|
+
* Rakefile cleanup. Use isolate. Nuke pathing.
|
10
|
+
* Removed dead rubyforge setting in Rakefile
|
11
|
+
|
1
12
|
=== 1.2.0 / 2009-06-23
|
2
13
|
|
3
14
|
* 26 minor enhancements:
|
@@ -56,5 +67,7 @@
|
|
56
67
|
|
57
68
|
=== 1.0.0 / 2006-09-31
|
58
69
|
|
59
|
-
*
|
70
|
+
* 1 major enhancement:
|
71
|
+
|
72
|
+
* Birthday!
|
60
73
|
|
data/README.txt
CHANGED
data/Rakefile
CHANGED
@@ -1,18 +1,11 @@
|
|
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'
|
14
|
-
|
15
|
-
self.rubyforge_name = 'seattlerb'
|
7
|
+
Hoe.spec "png" do
|
8
|
+
developer "Ryan Davis", "ryand-ruby@zenspider.com"
|
16
9
|
end
|
17
10
|
|
18
11
|
# 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.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# encoding: BINARY
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
3
|
+
require "rubygems"
|
4
|
+
require "zlib"
|
5
|
+
require "inline"
|
6
6
|
|
7
7
|
unless "".respond_to? :getbyte then
|
8
8
|
class String
|
@@ -16,6 +16,10 @@ class String # :nodoc: # ZenTest SKIP
|
|
16
16
|
unsigned long png_crc() {
|
17
17
|
static unsigned long crc[256];
|
18
18
|
static char crc_table_computed = 0;
|
19
|
+
unsigned long c = 0xffffffff;
|
20
|
+
size_t len = RSTRING_LEN(self);
|
21
|
+
char * s = StringValuePtr(self);
|
22
|
+
unsigned i;
|
19
23
|
|
20
24
|
if (! crc_table_computed) {
|
21
25
|
unsigned long c;
|
@@ -31,11 +35,6 @@ class String # :nodoc: # ZenTest SKIP
|
|
31
35
|
crc_table_computed = 1;
|
32
36
|
}
|
33
37
|
|
34
|
-
unsigned long c = 0xffffffff;
|
35
|
-
unsigned len = RSTRING_LEN(self);
|
36
|
-
char * s = StringValuePtr(self);
|
37
|
-
unsigned i;
|
38
|
-
|
39
38
|
for (i = 0; i < len; i++) {
|
40
39
|
c = crc[(c ^ s[i]) & 0xff] ^ (c >> 8);
|
41
40
|
}
|
@@ -45,7 +44,7 @@ class String # :nodoc: # ZenTest SKIP
|
|
45
44
|
EOM
|
46
45
|
end
|
47
46
|
rescue CompilationError => e
|
48
|
-
warn "
|
47
|
+
warn "COMPILATION ERROR: #{e}"
|
49
48
|
|
50
49
|
unless defined? @@crc then
|
51
50
|
@@crc = Array.new(256)
|
@@ -61,12 +60,12 @@ rescue CompilationError => e
|
|
61
60
|
##
|
62
61
|
# Calculates a CRC using the algorithm in the PNG specification.
|
63
62
|
|
64
|
-
def png_crc
|
63
|
+
def png_crc
|
65
64
|
c = 0xffffffff
|
66
65
|
each_byte do |b|
|
67
66
|
c = @@crc[(c^b) & 0xff] ^ (c >> 8)
|
68
67
|
end
|
69
|
-
|
68
|
+
c ^ 0xffffffff
|
70
69
|
end
|
71
70
|
end
|
72
71
|
|
@@ -99,7 +98,7 @@ end
|
|
99
98
|
# bottom left.
|
100
99
|
|
101
100
|
class PNG
|
102
|
-
VERSION =
|
101
|
+
VERSION = "1.2.1"
|
103
102
|
SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10].pack("C*")
|
104
103
|
|
105
104
|
# Color Types:
|
@@ -127,12 +126,13 @@ class PNG
|
|
127
126
|
|
128
127
|
builder.c <<-EOM
|
129
128
|
VALUE png_join() {
|
130
|
-
|
131
|
-
VALUE
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
char * result
|
129
|
+
size_t i, j;
|
130
|
+
VALUE data = rb_iv_get(self, "@data");
|
131
|
+
size_t data_len = RARRAY_LEN(data);
|
132
|
+
size_t row_len = RARRAY_LEN(RARRAY_PTR(data)[0]);
|
133
|
+
size_t size = data_len * (1 + (row_len * 4));
|
134
|
+
char * result = malloc(size);
|
135
|
+
|
136
136
|
unsigned long idx = 0;
|
137
137
|
for (i = 0; i < data_len; i++) {
|
138
138
|
VALUE row = RARRAY_PTR(data)[i];
|
@@ -153,21 +153,21 @@ class PNG
|
|
153
153
|
end
|
154
154
|
rescue CompilationError
|
155
155
|
def png_join
|
156
|
-
@data.map { |row| "\0" + row.map
|
156
|
+
@data.map { |row| "\0" + row.map(&:values).join }.join
|
157
157
|
end
|
158
158
|
end
|
159
159
|
|
160
160
|
##
|
161
161
|
# Creates a PNG chunk of type +type+ that contains +data+.
|
162
162
|
|
163
|
-
def self.chunk
|
163
|
+
def self.chunk type, data = ""
|
164
164
|
[data.size, type, data, (type + data).png_crc].pack("Na*a*N")
|
165
165
|
end
|
166
166
|
|
167
167
|
##
|
168
168
|
# Creates a new PNG object using +canvas+
|
169
169
|
|
170
|
-
def initialize
|
170
|
+
def initialize canvas
|
171
171
|
@height = canvas.height
|
172
172
|
@width = canvas.width
|
173
173
|
@bits = 8
|
@@ -177,8 +177,8 @@ class PNG
|
|
177
177
|
##
|
178
178
|
# Writes the PNG to +path+.
|
179
179
|
|
180
|
-
def save
|
181
|
-
File.open path,
|
180
|
+
def save path
|
181
|
+
File.open path, "wb" do |f|
|
182
182
|
f.write to_blob
|
183
183
|
end
|
184
184
|
end
|
@@ -192,9 +192,9 @@ class PNG
|
|
192
192
|
header = [@width, @height, @bits, RGBA, NONE, NONE, NONE]
|
193
193
|
|
194
194
|
blob << SIGNATURE
|
195
|
-
blob << PNG.chunk(
|
196
|
-
blob << PNG.chunk(
|
197
|
-
blob << PNG.chunk(
|
195
|
+
blob << PNG.chunk("IHDR", header.pack("N2C5"))
|
196
|
+
blob << PNG.chunk("IDAT", Zlib::Deflate.deflate(self.png_join))
|
197
|
+
blob << PNG.chunk("IEND", "")
|
198
198
|
blob.join
|
199
199
|
end
|
200
200
|
|
@@ -214,7 +214,7 @@ class PNG
|
|
214
214
|
|
215
215
|
def self.from str, name = nil
|
216
216
|
str = "%08x" % str if Integer === str
|
217
|
-
colors = str.scan(/[\da-f][\da-f]/i).map
|
217
|
+
colors = str.scan(/[\da-f][\da-f]/i).map(&:hex)
|
218
218
|
colors << name
|
219
219
|
self.new(*colors)
|
220
220
|
end
|
@@ -255,8 +255,8 @@ class PNG
|
|
255
255
|
# "Bitwise or" as applied to colors. Background color is
|
256
256
|
# considered false.
|
257
257
|
|
258
|
-
def |
|
259
|
-
self == Background ?
|
258
|
+
def | other
|
259
|
+
self == Background ? other : self
|
260
260
|
end
|
261
261
|
|
262
262
|
def hash # :nodoc:
|
@@ -294,15 +294,15 @@ class PNG
|
|
294
294
|
# Blends +color+ into this color returning a new blended color.
|
295
295
|
|
296
296
|
def blend color
|
297
|
-
|
298
|
-
|
297
|
+
Color.new(((r + color.r) / 2), ((g + color.g) / 2),
|
298
|
+
((b + color.b) / 2), ((a + color.a) / 2))
|
299
299
|
end
|
300
300
|
|
301
301
|
##
|
302
302
|
# Returns a new color with an alpha value adjusted by +i+.
|
303
303
|
|
304
304
|
def intensity i
|
305
|
-
|
305
|
+
Color.new(r, g, b, (a*i) >> 8)
|
306
306
|
end
|
307
307
|
|
308
308
|
def inspect # :nodoc:
|
@@ -318,11 +318,11 @@ class PNG
|
|
318
318
|
# art!
|
319
319
|
|
320
320
|
def to_ascii
|
321
|
-
return
|
321
|
+
return " " if a == 0x00
|
322
322
|
|
323
323
|
brightness = (((r + g + b) / 3) * a) / 0xFF
|
324
324
|
|
325
|
-
%w
|
325
|
+
%w[.. ,, ++ 00][brightness / 64]
|
326
326
|
end
|
327
327
|
|
328
328
|
def to_s # :nodoc:
|
@@ -341,26 +341,26 @@ class PNG
|
|
341
341
|
unless s == 0.0 then
|
342
342
|
h += 255 if h < 0
|
343
343
|
h = h / 255.0 * 6.0
|
344
|
-
s
|
345
|
-
v
|
344
|
+
s /= 255.0
|
345
|
+
v /= 255.0
|
346
346
|
i = h.floor
|
347
347
|
f = h - i
|
348
348
|
p = v * (1 - (s))
|
349
349
|
q = v * (1 - (s * (f)))
|
350
350
|
w = v * (1 - (s * (1-f)))
|
351
351
|
r, g, b = case i
|
352
|
-
when 0,6 then
|
353
|
-
[
|
352
|
+
when 0, 6 then
|
353
|
+
[v, w, p]
|
354
354
|
when 1 then
|
355
|
-
[
|
355
|
+
[q, v, p]
|
356
356
|
when 2 then
|
357
|
-
[
|
357
|
+
[p, v, w]
|
358
358
|
when 3 then
|
359
|
-
[
|
359
|
+
[p, q, v]
|
360
360
|
when 4 then
|
361
|
-
[
|
361
|
+
[w, p, v]
|
362
362
|
when 5 then
|
363
|
-
[
|
363
|
+
[v, p, q]
|
364
364
|
else
|
365
365
|
raise [h, s, v, i, f, p, q, w].inspect
|
366
366
|
end
|
@@ -420,7 +420,7 @@ class PNG
|
|
420
420
|
def initialize width, height, background = Color::Background
|
421
421
|
@width = width
|
422
422
|
@height = height
|
423
|
-
@data = Array.new(@height) {
|
423
|
+
@data = Array.new(@height) { Array.new(@width, background) }
|
424
424
|
end
|
425
425
|
|
426
426
|
##
|
@@ -489,14 +489,14 @@ class PNG
|
|
489
489
|
end
|
490
490
|
|
491
491
|
def inspect # :nodoc:
|
492
|
-
|
492
|
+
"#<%s %dx%d>" % [self.class, @width, @height]
|
493
493
|
end
|
494
494
|
|
495
495
|
##
|
496
496
|
# Blends +color+ onto the color at point (+x+, +y+).
|
497
497
|
|
498
|
-
def point
|
499
|
-
self[x,y] = self[x,y].blend(color)
|
498
|
+
def point x, y, color
|
499
|
+
self[x, y] = self[x, y].blend(color)
|
500
500
|
end
|
501
501
|
|
502
502
|
##
|
@@ -504,7 +504,7 @@ class PNG
|
|
504
504
|
#
|
505
505
|
# http://en.wikipedia.org/wiki/Xiaolin_Wu's_line_algorithm
|
506
506
|
|
507
|
-
def line
|
507
|
+
def line x0, y0, x1, y1, color
|
508
508
|
y0, y1, x0, x1 = y1, y0, x1, x0 if y0 > y1
|
509
509
|
dx = x1 - x0
|
510
510
|
sx = dx < 0 ? -1 : 1
|
@@ -513,7 +513,7 @@ class PNG
|
|
513
513
|
|
514
514
|
# 'easy' cases
|
515
515
|
if dy == 0 then
|
516
|
-
Range.new(*[x0,x1].sort).each do |x|
|
516
|
+
Range.new(*[x0, x1].sort).each do |x|
|
517
517
|
point(x, y0, color)
|
518
518
|
end
|
519
519
|
return
|
@@ -539,12 +539,12 @@ class PNG
|
|
539
539
|
e_acc = 0
|
540
540
|
if dy > dx then # vertical displacement
|
541
541
|
e = (dx << 16) / dy
|
542
|
-
(y0...y1-1).each do
|
542
|
+
(y0...y1-1).each do
|
543
543
|
e_acc_temp, e_acc = e_acc, (e_acc + e) & 0xFFFF
|
544
|
-
x0
|
544
|
+
x0 += sx if (e_acc <= e_acc_temp)
|
545
545
|
w = 0xFF-(e_acc >> 8)
|
546
546
|
point(x0, y0, color.intensity(w))
|
547
|
-
y0
|
547
|
+
y0 += 1
|
548
548
|
point(x0 + sx, y0, color.intensity(0xFF-w))
|
549
549
|
end
|
550
550
|
point(x1, y1, color)
|
@@ -553,7 +553,7 @@ class PNG
|
|
553
553
|
|
554
554
|
# horizontal displacement
|
555
555
|
e = (dy << 16) / dx
|
556
|
-
(dx - 1).downto(0) do
|
556
|
+
(dx - 1).downto(0) do
|
557
557
|
e_acc_temp, e_acc = e_acc, (e_acc + e) & 0xFFFF
|
558
558
|
y0 += 1 if (e_acc <= e_acc_temp)
|
559
559
|
w = 0xFF-(e_acc >> 8)
|
@@ -580,7 +580,7 @@ class PNG
|
|
580
580
|
image << "\n"
|
581
581
|
end
|
582
582
|
|
583
|
-
|
583
|
+
image.join
|
584
584
|
end
|
585
585
|
end # Canvas
|
586
586
|
end
|
data/lib/png/font.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
# encoding: BINARY
|
2
2
|
|
3
|
-
require
|
3
|
+
require "png/reader"
|
4
4
|
|
5
5
|
##
|
6
6
|
# Implements a simple bitmap font by extracting letters from a PNG.
|
7
7
|
|
8
8
|
class PNG::Font
|
9
|
-
LETTERS = ((
|
10
|
-
(
|
11
|
-
(
|
9
|
+
LETTERS = (("A".."Z").to_a +
|
10
|
+
("a".."z").to_a +
|
11
|
+
("0".."9").to_a + [" "] * 16 +
|
12
12
|
'({[<!@#$%^&*?_+-=;,"/~>]})'.split(//))
|
13
13
|
|
14
14
|
attr_reader :height, :width, :canvas
|
@@ -17,7 +17,7 @@ class PNG::Font
|
|
17
17
|
@@default ||= new(File.join(File.dirname(__FILE__), "default_font.png"))
|
18
18
|
end
|
19
19
|
|
20
|
-
def initialize
|
20
|
+
def initialize png_file
|
21
21
|
@canvas = PNG.load_file png_file
|
22
22
|
@height, @width = canvas.height / 4, canvas.width / 26
|
23
23
|
@cache = {}
|
@@ -50,8 +50,8 @@ class PNG::Canvas
|
|
50
50
|
#
|
51
51
|
# require 'png/font'
|
52
52
|
|
53
|
-
def annotate
|
54
|
-
font = PNG::Font.default, align = :left, style = :overwrite
|
53
|
+
def annotate string, x, y,
|
54
|
+
font = PNG::Font.default, align = :left, style = :overwrite
|
55
55
|
case align
|
56
56
|
when :left then
|
57
57
|
# do nothing
|