pnm 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +93 -0
- data/Rakefile +34 -0
- data/benchmark/bm_converter.rb +85 -0
- data/benchmark/random_image.pbm +0 -0
- data/benchmark/random_image.pgm +0 -0
- data/benchmark/random_image.ppm +0 -0
- data/lib/pnm.rb +155 -0
- data/lib/pnm/converter.rb +101 -0
- data/lib/pnm/image.rb +129 -0
- data/lib/pnm/parser.rb +83 -0
- data/lib/pnm/version.rb +4 -0
- data/pnm.gemspec +42 -0
- data/test/bilevel_2_binary.pbm +0 -0
- data/test/bilevel_ascii.pbm +9 -0
- data/test/bilevel_binary.pbm +0 -0
- data/test/color_ascii.ppm +6 -0
- data/test/color_binary.ppm +0 -0
- data/test/grayscale_ascii.pgm +8 -0
- data/test/grayscale_binary.pgm +0 -0
- data/test/grayscale_binary_crlf.pgm +5 -0
- data/test/test_converter.rb +183 -0
- data/test/test_image.rb +113 -0
- data/test/test_parser.rb +119 -0
- data/test/test_pnm.rb +105 -0
- metadata +88 -0
data/test/test_image.rb
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
# test_image.rb: Unit tests for the PNM library.
|
2
|
+
#
|
3
|
+
# Copyright (C) 2013 Marcus Stollsteimer
|
4
|
+
|
5
|
+
require 'minitest/spec'
|
6
|
+
require 'minitest/autorun'
|
7
|
+
require 'pnm/image'
|
8
|
+
|
9
|
+
|
10
|
+
describe PNM::Image do
|
11
|
+
|
12
|
+
before do
|
13
|
+
@srcpath = File.dirname(__FILE__)
|
14
|
+
@temp_path = File.expand_path("#{@srcpath}/temp.pnm")
|
15
|
+
|
16
|
+
pixels = [[0,0,0,0,0],
|
17
|
+
[0,1,1,1,0],
|
18
|
+
[0,0,1,0,0],
|
19
|
+
[0,0,1,0,0],
|
20
|
+
[0,0,1,0,0],
|
21
|
+
[0,0,0,0,0]]
|
22
|
+
comment = 'Bilevel'
|
23
|
+
@bilevel = PNM::Image.new(:pbm, pixels, {:comment => comment})
|
24
|
+
|
25
|
+
pixels = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
|
26
|
+
[0,1,1,1,0,1,1,1,1,0,1,1,1,1,1,0],
|
27
|
+
[0,0,1,0,0,1,0,0,1,0,1,0,1,0,1,0],
|
28
|
+
[0,0,1,0,0,1,0,0,1,0,1,0,1,0,1,0],
|
29
|
+
[0,0,1,0,0,1,1,1,1,0,1,0,1,0,1,0],
|
30
|
+
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
|
31
|
+
@bilevel_2 = PNM::Image.new(:pbm, pixels)
|
32
|
+
|
33
|
+
pixels = [[ 0, 50,100,150],
|
34
|
+
[ 50,100,150,200],
|
35
|
+
[100,150,200,250]]
|
36
|
+
comment = "Grayscale\n(with multiline comment)"
|
37
|
+
@grayscale = PNM::Image.new(:pgm, pixels, {:maxgray => 250, :comment => comment})
|
38
|
+
|
39
|
+
pixels = [[65,66], [13,10], [65,66]]
|
40
|
+
@grayscale_crlf = PNM::Image.new(:pgm, pixels)
|
41
|
+
|
42
|
+
pixels = [[[0,6,0], [1,5,1], [2,4,2], [3,3,4], [4,2,6]],
|
43
|
+
[[1,5,2], [2,4,2], [3,3,2], [4,2,2], [5,1,2]],
|
44
|
+
[[2,4,6], [3,3,4], [4,2,2], [5,1,1], [6,0,0]]]
|
45
|
+
@color = PNM::Image.new(:ppm, pixels, {:maxgray => 6})
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'can create a color image from gray values' do
|
49
|
+
image = PNM::Image.new(:ppm, [[0,3,6], [3,6,9]])
|
50
|
+
image.info.must_match %r{^PPM 3x2 Color}
|
51
|
+
image.pixels.must_equal [[[0,0,0], [3,3,3], [6,6,6]], [[3,3,3], [6,6,6], [9,9,9]]]
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'can write a bilevel image to an ASCII encoded file' do
|
55
|
+
@bilevel.write(@temp_path, :ascii)
|
56
|
+
File.binread(@temp_path).must_equal File.binread("#{@srcpath}/bilevel_ascii.pbm")
|
57
|
+
File.delete(@temp_path)
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'can write a bilevel image (width 5) to a binary encoded file' do
|
61
|
+
@bilevel.write(@temp_path, :binary)
|
62
|
+
File.binread(@temp_path).must_equal File.binread("#{@srcpath}/bilevel_binary.pbm")
|
63
|
+
File.delete(@temp_path)
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'can write a bilevel image (width 16) to a binary encoded file' do
|
67
|
+
@bilevel_2.write(@temp_path, :binary)
|
68
|
+
File.binread(@temp_path).must_equal File.binread("#{@srcpath}/bilevel_2_binary.pbm")
|
69
|
+
File.delete(@temp_path)
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'can write a grayscale image to an ASCII encoded file' do
|
73
|
+
@grayscale.write(@temp_path, :ascii)
|
74
|
+
File.binread(@temp_path).must_equal File.binread("#{@srcpath}/grayscale_ascii.pgm")
|
75
|
+
File.delete(@temp_path)
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'can write a grayscale image to a binary encoded file' do
|
79
|
+
@grayscale.write(@temp_path, :binary)
|
80
|
+
File.binread(@temp_path).must_equal File.binread("#{@srcpath}/grayscale_binary.pgm")
|
81
|
+
File.delete(@temp_path)
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'can write a color image to an ASCII encoded file' do
|
85
|
+
@color.write(@temp_path, :ascii)
|
86
|
+
File.binread(@temp_path).must_equal File.binread("#{@srcpath}/color_ascii.ppm")
|
87
|
+
File.delete(@temp_path)
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'can write a color image to a binary encoded file' do
|
91
|
+
@color.write(@temp_path, :binary)
|
92
|
+
File.binread(@temp_path).must_equal File.binread("#{@srcpath}/color_binary.ppm")
|
93
|
+
File.delete(@temp_path)
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'can return image information' do
|
97
|
+
@bilevel.info.must_equal 'PBM 5x6 Bilevel'
|
98
|
+
@grayscale.info.must_equal 'PGM 4x3 Grayscale'
|
99
|
+
@color.info.must_equal 'PPM 5x3 Color'
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'can write binary data containing CRLF' do
|
103
|
+
@grayscale_crlf.write(@temp_path, :binary)
|
104
|
+
File.binread(@temp_path).must_equal File.binread("#{@srcpath}/grayscale_binary_crlf.pgm")
|
105
|
+
File.delete(@temp_path)
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'can write binary data containing CRLF from an I/O stream' do
|
109
|
+
File.open(@temp_path, 'w') {|f| @grayscale_crlf.write(f, :binary) }
|
110
|
+
File.binread(@temp_path).must_equal File.binread("#{@srcpath}/grayscale_binary_crlf.pgm")
|
111
|
+
File.delete(@temp_path)
|
112
|
+
end
|
113
|
+
end
|
data/test/test_parser.rb
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
# test_parser.rb: Unit tests for the PNM library.
|
2
|
+
#
|
3
|
+
# Copyright (C) 2013 Marcus Stollsteimer
|
4
|
+
|
5
|
+
require 'minitest/spec'
|
6
|
+
require 'minitest/autorun'
|
7
|
+
require 'pnm/parser'
|
8
|
+
|
9
|
+
|
10
|
+
describe PNM::Parser do
|
11
|
+
|
12
|
+
before do
|
13
|
+
@parser = PNM::Parser
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'can parse ASCII encoded PBM data' do
|
17
|
+
content =<<-EOF.chomp.gsub(/^ */, '')
|
18
|
+
P1 6 2
|
19
|
+
0 1 0 0 1 1
|
20
|
+
0 0 0 1 1 1
|
21
|
+
EOF
|
22
|
+
expected = {
|
23
|
+
:magic_number => 'P1',
|
24
|
+
:width => '6',
|
25
|
+
:height => '2',
|
26
|
+
:data => "0 1 0 0 1 1\n0 0 0 1 1 1"
|
27
|
+
}
|
28
|
+
|
29
|
+
@parser.parse(content).must_equal expected
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'can parse ASCII encoded PGM data' do
|
33
|
+
content =<<-EOF.chomp.gsub(/^ */, '')
|
34
|
+
P2 4 2 100
|
35
|
+
10 20 30 40
|
36
|
+
50 60 70 80
|
37
|
+
EOF
|
38
|
+
expected = {
|
39
|
+
:magic_number => 'P2',
|
40
|
+
:width => '4',
|
41
|
+
:height => '2',
|
42
|
+
:maxgray => '100',
|
43
|
+
:data => "10 20 30 40\n50 60 70 80"
|
44
|
+
}
|
45
|
+
|
46
|
+
@parser.parse(content).must_equal expected
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'can parse binary encoded data' do
|
50
|
+
content = 'P4 8 2 ' << ['05AF'].pack('H*')
|
51
|
+
expected = {
|
52
|
+
:magic_number => 'P4',
|
53
|
+
:width => '8',
|
54
|
+
:height => '2',
|
55
|
+
:data => ['05AF'].pack('H*')
|
56
|
+
}
|
57
|
+
|
58
|
+
@parser.parse(content).must_equal expected
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'does not change the passed data' do
|
62
|
+
content = 'P1 3 2 0 1 0 0 1 1'
|
63
|
+
original_content = content.dup
|
64
|
+
@parser.parse(content)
|
65
|
+
|
66
|
+
content.must_equal original_content
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'does accept multiple whitespace as delimiter' do
|
70
|
+
content = "P1 \n\t 3 \r \n2 0 1 0 0 1 1"
|
71
|
+
expected = {
|
72
|
+
:magic_number => 'P1',
|
73
|
+
:width => '3',
|
74
|
+
:height => '2',
|
75
|
+
:data => '0 1 0 0 1 1'
|
76
|
+
}
|
77
|
+
|
78
|
+
@parser.parse(content).must_equal expected
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'can parse binary encoded data including whitespace' do
|
82
|
+
@parser.parse("P4 16 4 A\nB\rC D\t")[:data].must_equal "A\nB\rC D\t"
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'can parse binary encoded data starting with whitespace' do
|
86
|
+
@parser.parse("P4 8 2 \nA")[:data].must_equal "\nA"
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'can parse binary encoded data starting with comment character' do
|
90
|
+
@parser.parse("P4 8 2 #A")[:data].must_equal "#A"
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'does not chomp newlines from parsed binary encoded data' do
|
94
|
+
@parser.parse("P4 8 2 AB\n")[:data].must_equal "AB\n"
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'can parse comments' do
|
98
|
+
content =<<-EOF.chomp.gsub(/^ */, '')
|
99
|
+
# Comment 1
|
100
|
+
P1 # Comment 2
|
101
|
+
6
|
102
|
+
#Comment 3
|
103
|
+
#
|
104
|
+
\r \t# Comment 5
|
105
|
+
2
|
106
|
+
0 1 0 0 1 1
|
107
|
+
0 0 0 1 1 1
|
108
|
+
EOF
|
109
|
+
expected = {
|
110
|
+
:magic_number => 'P1',
|
111
|
+
:width => '6',
|
112
|
+
:height => '2',
|
113
|
+
:comments => ['Comment 1', 'Comment 2', 'Comment 3', '', 'Comment 5'],
|
114
|
+
:data => "0 1 0 0 1 1\n0 0 0 1 1 1"
|
115
|
+
}
|
116
|
+
|
117
|
+
@parser.parse(content).must_equal expected
|
118
|
+
end
|
119
|
+
end
|
data/test/test_pnm.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
# test_pnm.rb: Unit tests for the PNM library.
|
2
|
+
#
|
3
|
+
# Copyright (C) 2013 Marcus Stollsteimer
|
4
|
+
|
5
|
+
require 'minitest/spec'
|
6
|
+
require 'minitest/autorun'
|
7
|
+
require 'pnm'
|
8
|
+
|
9
|
+
|
10
|
+
describe PNM do
|
11
|
+
|
12
|
+
before do
|
13
|
+
@srcpath = File.dirname(__FILE__)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'can read an ASCII encoded PBM file' do
|
17
|
+
file = File.expand_path("#{@srcpath}/bilevel_ascii.pbm")
|
18
|
+
image = PNM.read(file)
|
19
|
+
|
20
|
+
image.info.must_match %r{^PBM 5x6 Bilevel}
|
21
|
+
image.maxgray.must_equal 1
|
22
|
+
image.comment.must_equal 'Bilevel'
|
23
|
+
image.pixels.must_equal [[0,0,0,0,0],
|
24
|
+
[0,1,1,1,0],
|
25
|
+
[0,0,1,0,0],
|
26
|
+
[0,0,1,0,0],
|
27
|
+
[0,0,1,0,0],
|
28
|
+
[0,0,0,0,0,]]
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'can read an ASCII encoded PGM file' do
|
32
|
+
file = File.expand_path("#{@srcpath}/grayscale_ascii.pgm")
|
33
|
+
image = PNM.read(file)
|
34
|
+
|
35
|
+
image.info.must_match %r{^PGM 4x3 Grayscale}
|
36
|
+
image.maxgray.must_equal 250
|
37
|
+
image.comment.must_equal "Grayscale\n(with multiline comment)"
|
38
|
+
image.pixels.must_equal [[ 0, 50,100,150],
|
39
|
+
[ 50,100,150,200],
|
40
|
+
[100,150,200,250]]
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'can read an ASCII encoded PPM file' do
|
44
|
+
file = File.expand_path("#{@srcpath}/color_ascii.ppm")
|
45
|
+
image = PNM.read(file)
|
46
|
+
|
47
|
+
image.info.must_match %r{^PPM 5x3 Color}
|
48
|
+
image.maxgray.must_equal 6
|
49
|
+
image.pixels.must_equal [[[0,6,0], [1,5,1], [2,4,2], [3,3,4], [4,2,6]],
|
50
|
+
[[1,5,2], [2,4,2], [3,3,2], [4,2,2], [5,1,2]],
|
51
|
+
[[2,4,6], [3,3,4], [4,2,2], [5,1,1], [6,0,0]]]
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'can read a binary encoded PBM file' do
|
55
|
+
file = File.expand_path("#{@srcpath}/bilevel_binary.pbm")
|
56
|
+
image = PNM.read(file)
|
57
|
+
|
58
|
+
image.info.must_match %r{^PBM 5x6 Bilevel}
|
59
|
+
image.maxgray.must_equal 1
|
60
|
+
image.comment.must_equal 'Bilevel'
|
61
|
+
image.pixels.must_equal [[0,0,0,0,0],
|
62
|
+
[0,1,1,1,0],
|
63
|
+
[0,0,1,0,0],
|
64
|
+
[0,0,1,0,0],
|
65
|
+
[0,0,1,0,0],
|
66
|
+
[0,0,0,0,0,]]
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'can read a binary encoded PGM file' do
|
70
|
+
file = File.expand_path("#{@srcpath}/grayscale_binary.pgm")
|
71
|
+
image = PNM.read(file)
|
72
|
+
|
73
|
+
image.info.must_match %r{^PGM 4x3 Grayscale}
|
74
|
+
image.maxgray.must_equal 250
|
75
|
+
image.comment.must_equal "Grayscale\n(with multiline comment)"
|
76
|
+
image.pixels.must_equal [[ 0, 50,100,150],
|
77
|
+
[ 50,100,150,200],
|
78
|
+
[100,150,200,250]]
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'can read a binary encoded PPM file' do
|
82
|
+
file = File.expand_path("#{@srcpath}/color_binary.ppm")
|
83
|
+
image = PNM.read(file)
|
84
|
+
|
85
|
+
image.info.must_match %r{^PPM 5x3 Color}
|
86
|
+
image.maxgray.must_equal 6
|
87
|
+
image.pixels.must_equal [[[0,6,0], [1,5,1], [2,4,2], [3,3,4], [4,2,6]],
|
88
|
+
[[1,5,2], [2,4,2], [3,3,2], [4,2,2], [5,1,2]],
|
89
|
+
[[2,4,6], [3,3,4], [4,2,2], [5,1,1], [6,0,0]]]
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'can read binary data containing CRLF' do
|
93
|
+
file = File.expand_path("#{@srcpath}/grayscale_binary_crlf.pgm")
|
94
|
+
|
95
|
+
image = PNM.read(file)
|
96
|
+
image.pixels.must_equal [[65,66], [13,10], [65,66]]
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'can read binary data containing CRLF from an I/O stream' do
|
100
|
+
file = File.expand_path("#{@srcpath}/grayscale_binary_crlf.pgm")
|
101
|
+
|
102
|
+
image = File.open(file, 'r') {|f| PNM.read(f) }
|
103
|
+
image.pixels.must_equal [[65,66], [13,10], [65,66]]
|
104
|
+
end
|
105
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pnm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marcus Stollsteimer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: 'PNM is a pure Ruby library for creating, reading, and writing of PNM
|
28
|
+
image files (Portable Anymap): PBM (Portable Bitmap), PGM (Portable Graymap), and
|
29
|
+
PPM (Portable Pixmap).'
|
30
|
+
email: sto.mar@web.de
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- pnm.gemspec
|
38
|
+
- benchmark/random_image.ppm
|
39
|
+
- benchmark/bm_converter.rb
|
40
|
+
- benchmark/random_image.pgm
|
41
|
+
- benchmark/random_image.pbm
|
42
|
+
- lib/pnm.rb
|
43
|
+
- lib/pnm/image.rb
|
44
|
+
- lib/pnm/parser.rb
|
45
|
+
- lib/pnm/version.rb
|
46
|
+
- lib/pnm/converter.rb
|
47
|
+
- test/bilevel_ascii.pbm
|
48
|
+
- test/grayscale_binary.pgm
|
49
|
+
- test/test_image.rb
|
50
|
+
- test/grayscale_binary_crlf.pgm
|
51
|
+
- test/color_binary.ppm
|
52
|
+
- test/bilevel_binary.pbm
|
53
|
+
- test/test_parser.rb
|
54
|
+
- test/test_converter.rb
|
55
|
+
- test/bilevel_2_binary.pbm
|
56
|
+
- test/color_ascii.ppm
|
57
|
+
- test/test_pnm.rb
|
58
|
+
- test/grayscale_ascii.pgm
|
59
|
+
homepage: https://github.com/stomar/pnm
|
60
|
+
licenses:
|
61
|
+
- GPL-3
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options:
|
65
|
+
- --charset=UTF-8
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 1.9.3
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.1.9
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: PNM - create/read/write PNM image files (PBM, PGM, PPM)
|
84
|
+
test_files:
|
85
|
+
- test/test_image.rb
|
86
|
+
- test/test_parser.rb
|
87
|
+
- test/test_converter.rb
|
88
|
+
- test/test_pnm.rb
|