pnm 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/benchmark/bm_converter.rb +1 -1
- data/lib/pnm.rb +1 -1
- data/lib/pnm/converter.rb +19 -15
- data/lib/pnm/image.rb +2 -2
- data/lib/pnm/parser.rb +14 -11
- data/lib/pnm/version.rb +2 -2
- data/test/test_converter.rb +18 -3
- data/test/test_image.rb +6 -0
- data/test/test_parser.rb +5 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 309f7c8934e624de40581059644cbda2d9ae575e
|
4
|
+
data.tar.gz: 36cb4969d02048d04064962091b2df86f8a24491
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d30ee950e419b1d83165f3e000ccb0769b6f9a50a223f94f6d50af048c8c8fcce9f3cb7799c0c7b74bcafddd1accbeae2fd78e6acef3b844ae1ec03b8208935
|
7
|
+
data.tar.gz: 561aaf230c4b41044bcaeb3ee62204963b032e9cbad3ea96c39e17510cd2608ef43e3334e4e6d9aba7b425f2eb1e65f80a73eb97e4030d2771428e50c672d812
|
data/README.md
CHANGED
data/benchmark/bm_converter.rb
CHANGED
data/lib/pnm.rb
CHANGED
@@ -139,7 +139,7 @@ module PNM
|
|
139
139
|
height = content[:height].to_i
|
140
140
|
maxgray = content[:maxgray].to_i
|
141
141
|
pixels = if encoding == :ascii
|
142
|
-
Converter.ascii2array(type, content[:data])
|
142
|
+
Converter.ascii2array(type, width, height, content[:data])
|
143
143
|
else
|
144
144
|
Converter.binary2array(type, width, height, content[:data])
|
145
145
|
end
|
data/lib/pnm/converter.rb
CHANGED
@@ -19,17 +19,21 @@ module PNM
|
|
19
19
|
# Converts from ASCII format to an array of pixel values.
|
20
20
|
#
|
21
21
|
# +type+:: +:pbm+, +:pgm+, or +:ppm+.
|
22
|
+
# +width+, +height+:: The image dimensions in pixels.
|
22
23
|
# +data+:: A string containing the raw pixel data in ASCII format.
|
23
24
|
#
|
24
25
|
# Returns a two-dimensional array of bilevel, gray, or RGB values.
|
25
|
-
def self.ascii2array(type, data)
|
26
|
-
|
27
|
-
row.split(/ +/).map {|value| value.to_i }
|
28
|
-
end
|
26
|
+
def self.ascii2array(type, width, height, data)
|
27
|
+
values = data.gsub(/\A[ \t\r\n]+/, '').split(/[ \t\r\n]+/).map {|value| value.to_i }
|
29
28
|
|
30
|
-
|
29
|
+
case type
|
30
|
+
when :pbm, :pgm
|
31
|
+
pixel_matrix = values.each_slice(width).to_a
|
32
|
+
when :ppm
|
33
|
+
pixel_matrix = values.each_slice(3 * width).map {|row| row.each_slice(3).to_a }
|
34
|
+
end
|
31
35
|
|
32
|
-
|
36
|
+
pixel_matrix
|
33
37
|
end
|
34
38
|
|
35
39
|
# Converts from binary format to an array of pixel values.
|
@@ -42,7 +46,7 @@ module PNM
|
|
42
46
|
def self.binary2array(type, width, height, data)
|
43
47
|
bytes_per_row = byte_width(type, width)
|
44
48
|
|
45
|
-
if data.size == bytes_per_row * height + 1 && data[-1] =~ /[ \
|
49
|
+
if data.size == bytes_per_row * height + 1 && data[-1] =~ /[ \t\r\n]/
|
46
50
|
data.slice!(-1)
|
47
51
|
end
|
48
52
|
|
@@ -52,15 +56,15 @@ module PNM
|
|
52
56
|
|
53
57
|
case type
|
54
58
|
when :pbm
|
55
|
-
|
56
|
-
|
59
|
+
rows = data.scan(/.{#{bytes_per_row}}/m)
|
60
|
+
pixel_matrix = rows.map {|row| row.unpack('B*').first[0, width].each_char.map {|char| char.to_i } }
|
57
61
|
when :pgm
|
58
|
-
|
62
|
+
pixel_matrix = data.each_byte.each_slice(bytes_per_row).to_a
|
59
63
|
when :ppm
|
60
|
-
|
64
|
+
pixel_matrix = data.each_byte.each_slice(bytes_per_row).map {|row| row.each_slice(3).to_a }
|
61
65
|
end
|
62
66
|
|
63
|
-
|
67
|
+
pixel_matrix
|
64
68
|
end
|
65
69
|
|
66
70
|
# Converts a two-dimensional array of pixel values to an ASCII format string.
|
@@ -71,12 +75,12 @@ module PNM
|
|
71
75
|
def self.array2ascii(data)
|
72
76
|
case data.first.first
|
73
77
|
when Array
|
74
|
-
|
78
|
+
data_string = data.map {|row| row.flatten.join(' ') }.join("\n")
|
75
79
|
else
|
76
|
-
|
80
|
+
data_string = data.map {|row| row.join(' ') }.join("\n")
|
77
81
|
end
|
78
82
|
|
79
|
-
|
83
|
+
data_string << "\n"
|
80
84
|
end
|
81
85
|
|
82
86
|
# Converts a two-dimensional array of pixel values to a binary format string.
|
data/lib/pnm/image.rb
CHANGED
@@ -55,7 +55,7 @@ module PNM
|
|
55
55
|
@height = pixels.size
|
56
56
|
@maxgray = options[:maxgray]
|
57
57
|
@comment = (options[:comment] || '').chomp
|
58
|
-
@pixels = pixels
|
58
|
+
@pixels = pixels.dup
|
59
59
|
|
60
60
|
@type ||= detect_type(@pixels, @maxgray)
|
61
61
|
|
@@ -66,7 +66,7 @@ module PNM
|
|
66
66
|
end
|
67
67
|
|
68
68
|
if type == :ppm && !pixels.first.first.kind_of?(Array)
|
69
|
-
@pixels
|
69
|
+
@pixels.map! {|row| row.map {|pixel| gray_to_rgb(pixel) } }
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
data/lib/pnm/parser.rb
CHANGED
@@ -23,9 +23,9 @@ module PNM
|
|
23
23
|
comments = []
|
24
24
|
|
25
25
|
until magic_number
|
26
|
-
token =
|
26
|
+
token = next_token!(content)
|
27
27
|
|
28
|
-
if token
|
28
|
+
if token.start_with?('#')
|
29
29
|
comments << token.gsub(/# */, '')
|
30
30
|
else
|
31
31
|
magic_number = token
|
@@ -36,8 +36,9 @@ module PNM
|
|
36
36
|
|
37
37
|
while tokens.size < token_number[magic_number]
|
38
38
|
content.gsub!(/\A[ \t\r\n]+/, '')
|
39
|
-
token =
|
40
|
-
|
39
|
+
token = next_token!(content)
|
40
|
+
|
41
|
+
if token.start_with?('#')
|
41
42
|
comments << token.gsub(/# */, '')
|
42
43
|
else
|
43
44
|
tokens << token
|
@@ -69,13 +70,15 @@ module PNM
|
|
69
70
|
}
|
70
71
|
end
|
71
72
|
|
72
|
-
def self.
|
73
|
-
if content
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
73
|
+
def self.next_token!(content) # :nodoc:
|
74
|
+
delimiter = if content.start_with?('#')
|
75
|
+
"\n"
|
76
|
+
else
|
77
|
+
%r{[ \t\r\n]|(?=#)}
|
78
|
+
end
|
79
|
+
|
80
|
+
token, rest = content.split(delimiter, 2)
|
81
|
+
content.replace(rest)
|
79
82
|
|
80
83
|
token
|
81
84
|
end
|
data/lib/pnm/version.rb
CHANGED
data/test/test_converter.rb
CHANGED
@@ -49,24 +49,39 @@ describe PNM::Converter do
|
|
49
49
|
end
|
50
50
|
|
51
51
|
it 'can convert from ASCII encoded PBM data' do
|
52
|
+
width = @pbm[:width]
|
53
|
+
height = @pbm[:height]
|
52
54
|
data = @pbm[:ascii]
|
53
55
|
expected = @pbm[:array]
|
54
56
|
|
55
|
-
@converter.ascii2array(:pbm, data).must_equal expected
|
57
|
+
@converter.ascii2array(:pbm, width, height, data).must_equal expected
|
56
58
|
end
|
57
59
|
|
58
60
|
it 'can convert from ASCII encoded PGM data' do
|
61
|
+
width = @pgm[:width]
|
62
|
+
height = @pgm[:height]
|
59
63
|
data = @pgm[:ascii]
|
60
64
|
expected = @pgm[:array]
|
61
65
|
|
62
|
-
@converter.ascii2array(:pgm, data).must_equal expected
|
66
|
+
@converter.ascii2array(:pgm, width, height, data).must_equal expected
|
63
67
|
end
|
64
68
|
|
65
69
|
it 'can convert from ASCII encoded PPM data' do
|
70
|
+
width = @ppm[:width]
|
71
|
+
height = @ppm[:height]
|
66
72
|
data = @ppm[:ascii]
|
67
73
|
expected = @ppm[:array]
|
68
74
|
|
69
|
-
@converter.ascii2array(:ppm, data).must_equal expected
|
75
|
+
@converter.ascii2array(:ppm, width, height, data).must_equal expected
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'accepts ASCII encoded PGM data with arbitrary whitespace' do
|
79
|
+
width = 4
|
80
|
+
height = 3
|
81
|
+
data = " \n 10 90\t170\r250\n90 \t170 250 \t\r\n\t 10\n\n\n170\n250\n10\n90\n"
|
82
|
+
expected = [[10, 90, 170, 250], [90, 170, 250, 10], [170, 250, 10, 90]]
|
83
|
+
|
84
|
+
@converter.ascii2array(:pgm, width, height, data).must_equal expected
|
70
85
|
end
|
71
86
|
|
72
87
|
it 'can convert from binary encoded PBM data (width 6)' do
|
data/test/test_image.rb
CHANGED
@@ -103,6 +103,12 @@ describe PNM::Image do
|
|
103
103
|
image.pixels.must_equal [[[0,0,0], [3,3,3], [6,6,6]], [[3,3,3], [6,6,6], [9,9,9]]]
|
104
104
|
end
|
105
105
|
|
106
|
+
it 'does not modify the input data for color images created from gray values' do
|
107
|
+
data = [[0,3,6], [3,6,9]]
|
108
|
+
PNM::Image.new(data, {:type => :ppm})
|
109
|
+
data.must_equal [[0,3,6], [3,6,9]]
|
110
|
+
end
|
111
|
+
|
106
112
|
it 'can write a bilevel image to an ASCII encoded file' do
|
107
113
|
@bilevel.write(@temp_path, :ascii)
|
108
114
|
File.binread(@temp_path).must_equal File.binread("#{@srcpath}/bilevel_ascii.pbm")
|
data/test/test_parser.rb
CHANGED
@@ -97,11 +97,11 @@ describe PNM::Parser do
|
|
97
97
|
it 'can parse comments' do
|
98
98
|
content =<<-EOF.chomp.gsub(/^ */, '')
|
99
99
|
# Comment 1
|
100
|
-
P1
|
101
|
-
6
|
102
|
-
#Comment
|
100
|
+
P1 # Comment 2
|
101
|
+
6# Comment 3
|
102
|
+
#Comment 4
|
103
103
|
#
|
104
|
-
\r \t# Comment
|
104
|
+
\r \t# Comment 6
|
105
105
|
2
|
106
106
|
0 1 0 0 1 1
|
107
107
|
0 0 0 1 1 1
|
@@ -110,7 +110,7 @@ describe PNM::Parser do
|
|
110
110
|
:magic_number => 'P1',
|
111
111
|
:width => '6',
|
112
112
|
:height => '2',
|
113
|
-
:comments => ['Comment 1', 'Comment 2', 'Comment 3', '', 'Comment
|
113
|
+
:comments => ['Comment 1', 'Comment 2', 'Comment 3', 'Comment 4', '', 'Comment 6'],
|
114
114
|
:data => "0 1 0 0 1 1\n0 0 0 1 1 1"
|
115
115
|
}
|
116
116
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pnm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcus Stollsteimer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-12-
|
11
|
+
date: 2013-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|