pnm 0.5.1 → 0.5.2
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 +5 -5
- data/README.md +4 -7
- data/Rakefile +11 -10
- data/benchmark/bm_converter.rb +21 -17
- data/lib/pnm.rb +35 -39
- data/lib/pnm/converter.rb +45 -48
- data/lib/pnm/exceptions.rb +2 -0
- data/lib/pnm/image.rb +53 -44
- data/lib/pnm/parser.rb +32 -30
- data/lib/pnm/version.rb +4 -2
- data/pnm.gemspec +23 -20
- data/test/test_converter.rb +57 -54
- data/test/test_exceptions.rb +122 -120
- data/test/test_image.rb +110 -108
- data/test/test_parser.rb +45 -43
- data/test/test_pnm.rb +42 -40
- metadata +11 -12
data/test/test_parser.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# test_parser.rb: Unit tests for the PNM library.
|
2
4
|
#
|
3
|
-
# Copyright (C) 2013-
|
5
|
+
# Copyright (C) 2013-2019 Marcus Stollsteimer
|
4
6
|
|
5
|
-
require
|
6
|
-
require
|
7
|
+
require "minitest/autorun"
|
8
|
+
require "pnm/parser"
|
7
9
|
|
8
10
|
|
9
11
|
describe PNM::Parser do
|
@@ -12,89 +14,89 @@ describe PNM::Parser do
|
|
12
14
|
@parser = PNM::Parser
|
13
15
|
end
|
14
16
|
|
15
|
-
it
|
16
|
-
content
|
17
|
+
it "can parse ASCII encoded PBM data" do
|
18
|
+
content = <<-PBM.chomp.gsub(/^ */, "")
|
17
19
|
P1 6 2
|
18
20
|
0 1 0 0 1 1
|
19
21
|
0 0 0 1 1 1
|
20
|
-
|
22
|
+
PBM
|
21
23
|
expected = {
|
22
|
-
:
|
23
|
-
:
|
24
|
-
:
|
25
|
-
:
|
24
|
+
magic_number: "P1",
|
25
|
+
width: 6,
|
26
|
+
height: 2,
|
27
|
+
data: "0 1 0 0 1 1\n0 0 0 1 1 1"
|
26
28
|
}
|
27
29
|
|
28
30
|
@parser.parse(content).must_equal expected
|
29
31
|
end
|
30
32
|
|
31
|
-
it
|
32
|
-
content
|
33
|
+
it "can parse ASCII encoded PGM data" do
|
34
|
+
content = <<-PGM.chomp.gsub(/^ */, "")
|
33
35
|
P2 4 2 100
|
34
36
|
10 20 30 40
|
35
37
|
50 60 70 80
|
36
|
-
|
38
|
+
PGM
|
37
39
|
expected = {
|
38
|
-
:
|
39
|
-
:
|
40
|
-
:
|
41
|
-
:
|
42
|
-
:
|
40
|
+
magic_number: "P2",
|
41
|
+
width: 4,
|
42
|
+
height: 2,
|
43
|
+
maxgray: 100,
|
44
|
+
data: "10 20 30 40\n50 60 70 80"
|
43
45
|
}
|
44
46
|
|
45
47
|
@parser.parse(content).must_equal expected
|
46
48
|
end
|
47
49
|
|
48
|
-
it
|
49
|
-
content =
|
50
|
+
it "can parse binary encoded data" do
|
51
|
+
content = "P4 8 2 ".dup << ["05AF"].pack("H*")
|
50
52
|
expected = {
|
51
|
-
:
|
52
|
-
:
|
53
|
-
:
|
54
|
-
:
|
53
|
+
magic_number: "P4",
|
54
|
+
width: 8,
|
55
|
+
height: 2,
|
56
|
+
data: ["05AF"].pack("H*")
|
55
57
|
}
|
56
58
|
|
57
59
|
@parser.parse(content).must_equal expected
|
58
60
|
end
|
59
61
|
|
60
|
-
it
|
61
|
-
content =
|
62
|
+
it "does not change the passed data" do
|
63
|
+
content = "P1 3 2 0 1 0 0 1 1"
|
62
64
|
original_content = content.dup
|
63
65
|
@parser.parse(content)
|
64
66
|
|
65
67
|
content.must_equal original_content
|
66
68
|
end
|
67
69
|
|
68
|
-
it
|
70
|
+
it "does accept multiple whitespace as delimiter" do
|
69
71
|
content = "P1 \n\t 3 \r \n2 0 1 0 0 1 1"
|
70
72
|
expected = {
|
71
|
-
:
|
72
|
-
:
|
73
|
-
:
|
74
|
-
:
|
73
|
+
magic_number: "P1",
|
74
|
+
width: 3,
|
75
|
+
height: 2,
|
76
|
+
data: "0 1 0 0 1 1"
|
75
77
|
}
|
76
78
|
|
77
79
|
@parser.parse(content).must_equal expected
|
78
80
|
end
|
79
81
|
|
80
|
-
it
|
82
|
+
it "can parse binary encoded data including whitespace" do
|
81
83
|
@parser.parse("P4 16 4 A\nB\rC D\t")[:data].must_equal "A\nB\rC D\t"
|
82
84
|
end
|
83
85
|
|
84
|
-
it
|
86
|
+
it "can parse binary encoded data starting with whitespace" do
|
85
87
|
@parser.parse("P4 8 2 \nA")[:data].must_equal "\nA"
|
86
88
|
end
|
87
89
|
|
88
|
-
it
|
90
|
+
it "can parse binary encoded data starting with comment character" do
|
89
91
|
@parser.parse("P4 8 2 #A")[:data].must_equal "#A"
|
90
92
|
end
|
91
93
|
|
92
|
-
it
|
94
|
+
it "does not chomp newlines from parsed binary encoded data" do
|
93
95
|
@parser.parse("P4 8 2 AB\n")[:data].must_equal "AB\n"
|
94
96
|
end
|
95
97
|
|
96
|
-
it
|
97
|
-
content
|
98
|
+
it "can parse comments" do
|
99
|
+
content = <<-PBM.chomp.gsub(/^ */, "")
|
98
100
|
# Comment 1
|
99
101
|
P1 # Comment 2
|
100
102
|
6# Comment 3
|
@@ -104,13 +106,13 @@ describe PNM::Parser do
|
|
104
106
|
2
|
105
107
|
0 1 0 0 1 1
|
106
108
|
0 0 0 1 1 1
|
107
|
-
|
109
|
+
PBM
|
108
110
|
expected = {
|
109
|
-
:
|
110
|
-
:
|
111
|
-
:
|
112
|
-
:
|
113
|
-
:
|
111
|
+
magic_number: "P1",
|
112
|
+
width: 6,
|
113
|
+
height: 2,
|
114
|
+
comments: ["Comment 1", "Comment 2", "Comment 3", "Comment 4", "", "Comment 6"],
|
115
|
+
data: "0 1 0 0 1 1\n0 0 0 1 1 1"
|
114
116
|
}
|
115
117
|
|
116
118
|
@parser.parse(content).must_equal expected
|
data/test/test_pnm.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# test_pnm.rb: Unit tests for the PNM library.
|
2
4
|
#
|
3
|
-
# Copyright (C) 2013-
|
5
|
+
# Copyright (C) 2013-2019 Marcus Stollsteimer
|
4
6
|
|
5
|
-
require
|
6
|
-
require
|
7
|
+
require "minitest/autorun"
|
8
|
+
require "pnm"
|
7
9
|
|
8
10
|
|
9
11
|
describe PNM do
|
@@ -12,93 +14,93 @@ describe PNM do
|
|
12
14
|
@srcpath = File.dirname(__FILE__)
|
13
15
|
end
|
14
16
|
|
15
|
-
it
|
17
|
+
it "can read an ASCII encoded PBM file" do
|
16
18
|
file = File.expand_path("#{@srcpath}/bilevel_ascii.pbm")
|
17
19
|
image = PNM.read(file)
|
18
20
|
|
19
21
|
image.info.must_match %r{^PBM 5x6 Bilevel}
|
20
22
|
image.maxgray.must_equal 1
|
21
|
-
image.comment.must_equal
|
22
|
-
image.pixels.must_equal [[0,0,0,0,0],
|
23
|
-
[0,1,1,1,0],
|
24
|
-
[0,0,1,0,0],
|
25
|
-
[0,0,1,0,0],
|
26
|
-
[0,0,1,0,0],
|
27
|
-
[0,0,0,0,0
|
23
|
+
image.comment.must_equal "Bilevel"
|
24
|
+
image.pixels.must_equal [[0, 0, 0, 0, 0],
|
25
|
+
[0, 1, 1, 1, 0],
|
26
|
+
[0, 0, 1, 0, 0],
|
27
|
+
[0, 0, 1, 0, 0],
|
28
|
+
[0, 0, 1, 0, 0],
|
29
|
+
[0, 0, 0, 0, 0]]
|
28
30
|
end
|
29
31
|
|
30
|
-
it
|
32
|
+
it "can read an ASCII encoded PGM file" do
|
31
33
|
file = File.expand_path("#{@srcpath}/grayscale_ascii.pgm")
|
32
34
|
image = PNM.read(file)
|
33
35
|
|
34
36
|
image.info.must_match %r{^PGM 4x3 Grayscale}
|
35
37
|
image.maxgray.must_equal 250
|
36
38
|
image.comment.must_equal "Grayscale\n(with multiline comment)"
|
37
|
-
image.pixels.must_equal [[ 0,
|
38
|
-
[ 50,100,150,200],
|
39
|
-
[100,150,200,250]]
|
39
|
+
image.pixels.must_equal [[ 0, 50, 100, 150],
|
40
|
+
[ 50, 100, 150, 200],
|
41
|
+
[100, 150, 200, 250]]
|
40
42
|
end
|
41
43
|
|
42
|
-
it
|
44
|
+
it "can read an ASCII encoded PPM file" do
|
43
45
|
file = File.expand_path("#{@srcpath}/color_ascii.ppm")
|
44
46
|
image = PNM.read(file)
|
45
47
|
|
46
48
|
image.info.must_match %r{^PPM 5x3 Color}
|
47
49
|
image.maxgray.must_equal 6
|
48
|
-
image.pixels.must_equal [[[0,6,0], [1,5,1], [2,4,2], [3,3,4], [4,2,6]],
|
49
|
-
[[1,5,2], [2,4,2], [3,3,2], [4,2,2], [5,1,2]],
|
50
|
-
[[2,4,6], [3,3,4], [4,2,2], [5,1,1], [6,0,0]]]
|
50
|
+
image.pixels.must_equal [[[0, 6, 0], [1, 5, 1], [2, 4, 2], [3, 3, 4], [4, 2, 6]],
|
51
|
+
[[1, 5, 2], [2, 4, 2], [3, 3, 2], [4, 2, 2], [5, 1, 2]],
|
52
|
+
[[2, 4, 6], [3, 3, 4], [4, 2, 2], [5, 1, 1], [6, 0, 0]]]
|
51
53
|
end
|
52
54
|
|
53
|
-
it
|
55
|
+
it "can read a binary encoded PBM file" do
|
54
56
|
file = File.expand_path("#{@srcpath}/bilevel_binary.pbm")
|
55
57
|
image = PNM.read(file)
|
56
58
|
|
57
59
|
image.info.must_match %r{^PBM 5x6 Bilevel}
|
58
60
|
image.maxgray.must_equal 1
|
59
|
-
image.comment.must_equal
|
60
|
-
image.pixels.must_equal [[0,0,0,0,0],
|
61
|
-
[0,1,1,1,0],
|
62
|
-
[0,0,1,0,0],
|
63
|
-
[0,0,1,0,0],
|
64
|
-
[0,0,1,0,0],
|
65
|
-
[0,0,0,0,0
|
61
|
+
image.comment.must_equal "Bilevel"
|
62
|
+
image.pixels.must_equal [[0, 0, 0, 0, 0],
|
63
|
+
[0, 1, 1, 1, 0],
|
64
|
+
[0, 0, 1, 0, 0],
|
65
|
+
[0, 0, 1, 0, 0],
|
66
|
+
[0, 0, 1, 0, 0],
|
67
|
+
[0, 0, 0, 0, 0]]
|
66
68
|
end
|
67
69
|
|
68
|
-
it
|
70
|
+
it "can read a binary encoded PGM file" do
|
69
71
|
file = File.expand_path("#{@srcpath}/grayscale_binary.pgm")
|
70
72
|
image = PNM.read(file)
|
71
73
|
|
72
74
|
image.info.must_match %r{^PGM 4x3 Grayscale}
|
73
75
|
image.maxgray.must_equal 250
|
74
76
|
image.comment.must_equal "Grayscale\n(with multiline comment)"
|
75
|
-
image.pixels.must_equal [[ 0,
|
76
|
-
[ 50,100,150,200],
|
77
|
-
[100,150,200,250]]
|
77
|
+
image.pixels.must_equal [[ 0, 50, 100, 150],
|
78
|
+
[ 50, 100, 150, 200],
|
79
|
+
[100, 150, 200, 250]]
|
78
80
|
end
|
79
81
|
|
80
|
-
it
|
82
|
+
it "can read a binary encoded PPM file" do
|
81
83
|
file = File.expand_path("#{@srcpath}/color_binary.ppm")
|
82
84
|
image = PNM.read(file)
|
83
85
|
|
84
86
|
image.info.must_match %r{^PPM 5x3 Color}
|
85
87
|
image.maxgray.must_equal 6
|
86
|
-
image.pixels.must_equal [[[0,6,0], [1,5,1], [2,4,2], [3,3,4], [4,2,6]],
|
87
|
-
[[1,5,2], [2,4,2], [3,3,2], [4,2,2], [5,1,2]],
|
88
|
-
[[2,4,6], [3,3,4], [4,2,2], [5,1,1], [6,0,0]]]
|
88
|
+
image.pixels.must_equal [[[0, 6, 0], [1, 5, 1], [2, 4, 2], [3, 3, 4], [4, 2, 6]],
|
89
|
+
[[1, 5, 2], [2, 4, 2], [3, 3, 2], [4, 2, 2], [5, 1, 2]],
|
90
|
+
[[2, 4, 6], [3, 3, 4], [4, 2, 2], [5, 1, 1], [6, 0, 0]]]
|
89
91
|
end
|
90
92
|
|
91
|
-
it
|
93
|
+
it "can read binary data containing CRLF" do
|
92
94
|
file = File.expand_path("#{@srcpath}/grayscale_binary_crlf.pgm")
|
93
95
|
|
94
96
|
image = PNM.read(file)
|
95
|
-
image.pixels.must_equal [[65,66], [13,10], [65,66]]
|
97
|
+
image.pixels.must_equal [[65, 66], [13, 10], [65, 66]]
|
96
98
|
end
|
97
99
|
|
98
|
-
it
|
100
|
+
it "can read binary data containing CRLF from an I/O stream" do
|
99
101
|
file = File.expand_path("#{@srcpath}/grayscale_binary_crlf.pgm")
|
100
102
|
|
101
|
-
image = File.open(file,
|
102
|
-
image.pixels.must_equal [[65,66], [13,10], [65,66]]
|
103
|
+
image = File.open(file, "r") {|f| PNM.read(f) }
|
104
|
+
image.pixels.must_equal [[65, 66], [13, 10], [65, 66]]
|
103
105
|
end
|
104
106
|
end
|
metadata
CHANGED
@@ -1,43 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pnm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcus Stollsteimer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-02-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: minitest
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '5.0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '5.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '10.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '10.0'
|
41
41
|
description: 'PNM is a pure Ruby library for creating, reading, and writing of PNM
|
42
42
|
image files (Portable Anymap): PBM (Portable Bitmap), PGM (Portable Graymap), and
|
43
43
|
PPM (Portable Pixmap).'
|
@@ -95,14 +95,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
requirements: []
|
98
|
-
|
99
|
-
rubygems_version: 2.5.2
|
98
|
+
rubygems_version: 3.0.1
|
100
99
|
signing_key:
|
101
100
|
specification_version: 4
|
102
101
|
summary: PNM - create/read/write PNM image files (PBM, PGM, PPM)
|
103
102
|
test_files:
|
104
|
-
- test/test_image.rb
|
105
103
|
- test/test_exceptions.rb
|
106
|
-
- test/
|
104
|
+
- test/test_image.rb
|
107
105
|
- test/test_converter.rb
|
108
106
|
- test/test_pnm.rb
|
107
|
+
- test/test_parser.rb
|