pnm 0.5.2 → 0.6.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 +13 -13
- data/Rakefile +1 -1
- data/benchmark/bm_converter.rb +1 -1
- data/lib/pnm/converter.rb +4 -4
- data/lib/pnm/image.rb +22 -31
- data/lib/pnm/parser.rb +18 -11
- data/lib/pnm/version.rb +2 -2
- data/lib/pnm.rb +27 -29
- data/pnm.gemspec +1 -4
- data/test/backports.rb +19 -0
- data/test/test_converter.rb +31 -28
- data/test/test_exceptions.rb +47 -45
- data/test/test_image.rb +76 -74
- data/test/test_parser.rb +13 -11
- data/test/test_pnm.rb +45 -43
- metadata +8 -35
data/test/test_exceptions.rb
CHANGED
@@ -2,123 +2,125 @@
|
|
2
2
|
|
3
3
|
# test_exceptions.rb: Unit tests for the PNM library.
|
4
4
|
#
|
5
|
-
# Copyright (C) 2013-
|
5
|
+
# Copyright (C) 2013-2022 Marcus Stollsteimer
|
6
6
|
|
7
7
|
require "minitest/autorun"
|
8
8
|
require "stringio"
|
9
9
|
require "pnm"
|
10
10
|
|
11
|
+
require_relative "backports"
|
12
|
+
|
11
13
|
|
12
14
|
describe "PNM.create" do
|
13
15
|
|
14
16
|
it "raises an exception for invalid data type (String)" do
|
15
17
|
data = "0"
|
16
|
-
|
18
|
+
_ { PNM.create(data) }.must_raise PNM::ArgumentError
|
17
19
|
end
|
18
20
|
|
19
21
|
it "raises an exception for invalid type" do
|
20
22
|
data = [[0, 0], [0, 0]]
|
21
|
-
|
23
|
+
_ { PNM.create(data, type: :abc) }.must_raise PNM::ArgumentError
|
22
24
|
end
|
23
25
|
|
24
26
|
it "raises an exception for invalid maxgray (String)" do
|
25
27
|
data = [[0, 0], [0, 0]]
|
26
|
-
|
28
|
+
_ { PNM.create(data, maxgray: "255") }.must_raise PNM::ArgumentError
|
27
29
|
end
|
28
30
|
|
29
31
|
it "raises an exception for invalid maxgray (> 255)" do
|
30
32
|
data = [[0, 0], [0, 0]]
|
31
|
-
|
33
|
+
_ { PNM.create(data, maxgray: 256) }.must_raise PNM::ArgumentError
|
32
34
|
end
|
33
35
|
|
34
36
|
it "raises an exception for invalid maxgray (0)" do
|
35
37
|
data = [[0, 0], [0, 0]]
|
36
|
-
|
38
|
+
_ { PNM.create(data, maxgray: 0) }.must_raise PNM::ArgumentError
|
37
39
|
end
|
38
40
|
|
39
41
|
it "raises an exception for invalid comment (Integer)" do
|
40
42
|
data = [[0, 0], [0, 0]]
|
41
|
-
|
43
|
+
_ { PNM.create(data, comment: 1) }.must_raise PNM::ArgumentError
|
42
44
|
end
|
43
45
|
|
44
46
|
it "raises an exception for image type and data mismatch (PBM)" do
|
45
47
|
data = [[[0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0]]]
|
46
|
-
|
48
|
+
_ { PNM.create(data, type: :pbm) }.must_raise PNM::DataError
|
47
49
|
end
|
48
50
|
|
49
51
|
it "raises an exception for image type and data mismatch (PGM)" do
|
50
52
|
data = [[[0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0]]]
|
51
|
-
|
53
|
+
_ { PNM.create(data, type: :pgm) }.must_raise PNM::DataError
|
52
54
|
end
|
53
55
|
|
54
56
|
it "raises an exception for non-integer pixel value (String)" do
|
55
57
|
data = [[0, 0], ["X", 0]]
|
56
|
-
|
58
|
+
_ { PNM.create(data) }.must_raise PNM::DataError
|
57
59
|
end
|
58
60
|
|
59
61
|
it "raises an exception for non-integer pixel value (Float)" do
|
60
62
|
data = [[0, 0], [0.5, 0]]
|
61
|
-
|
63
|
+
_ { PNM.create(data) }.must_raise PNM::DataError
|
62
64
|
end
|
63
65
|
|
64
66
|
it "raises an exception for rows of different size" do
|
65
67
|
data = [[0, 0], [0, 0, 0]]
|
66
|
-
|
68
|
+
_ { PNM.create(data) }.must_raise PNM::DataError
|
67
69
|
end
|
68
70
|
|
69
71
|
it "raises an exception for invalid array dimensions (#1)" do
|
70
72
|
data = [0, 0, 0]
|
71
|
-
|
73
|
+
_ { PNM.create(data) }.must_raise PNM::DataError
|
72
74
|
end
|
73
75
|
|
74
76
|
it "raises an exception for invalid array dimensions (#2)" do
|
75
77
|
data = [[0, 0], 0, 0]
|
76
|
-
|
78
|
+
_ { PNM.create(data) }.must_raise PNM::DataError
|
77
79
|
end
|
78
80
|
|
79
81
|
it "raises an exception for invalid array dimensions (#3)" do
|
80
82
|
data = [[0, 0], [0, [0, 0]]]
|
81
|
-
|
83
|
+
_ { PNM.create(data) }.must_raise PNM::DataError
|
82
84
|
end
|
83
85
|
|
84
86
|
it "raises an exception for invalid array dimensions (#4)" do
|
85
87
|
data = [[[0, 0], [0, 0]], [[0, 0], [0, 0]]]
|
86
|
-
|
88
|
+
_ { PNM.create(data) }.must_raise PNM::DataError
|
87
89
|
end
|
88
90
|
|
89
91
|
it "raises an exception for invalid array dimensions (#5)" do
|
90
92
|
data = [[[0, 0, 0], [0, 0, 0]], [0, 0]]
|
91
|
-
|
93
|
+
_ { PNM.create(data) }.must_raise PNM::DataError
|
92
94
|
end
|
93
95
|
|
94
96
|
it "raises an exception for invalid array dimensions (#6)" do
|
95
97
|
data = [[[0, 0, 0], 0], [0, 0]]
|
96
|
-
|
98
|
+
_ { PNM.create(data) }.must_raise PNM::DataError
|
97
99
|
end
|
98
100
|
|
99
101
|
it "raises an exception for an empty array" do
|
100
102
|
data = [[]]
|
101
|
-
|
103
|
+
_ { PNM.create(data) }.must_raise PNM::DataError
|
102
104
|
end
|
103
105
|
|
104
106
|
it "raises an exception for invalid PBM data (> 1)" do
|
105
107
|
data = [[0, 0], [2, 0]]
|
106
|
-
|
108
|
+
_ { PNM.create(data, type: :pbm) }.must_raise PNM::DataError
|
107
109
|
end
|
108
110
|
|
109
111
|
it "raises an exception for invalid PBM data (< 0)" do
|
110
112
|
data = [[0, 0], [-1, 0]]
|
111
|
-
|
113
|
+
_ { PNM.create(data, type: :pbm) }.must_raise PNM::DataError
|
112
114
|
end
|
113
115
|
|
114
116
|
it "raises an exception for invalid PGM data (> 255)" do
|
115
117
|
data = [[0, 0], [1, 500]]
|
116
|
-
|
118
|
+
_ { PNM.create(data, type: :pgm) }.must_raise PNM::DataError
|
117
119
|
end
|
118
120
|
|
119
121
|
it "raises an exception for invalid PGM data (> maxgray)" do
|
120
122
|
data = [[0, 0], [1, 200]]
|
121
|
-
|
123
|
+
_ { PNM.create(data, maxgray: 100) }.must_raise PNM::DataError
|
122
124
|
end
|
123
125
|
end
|
124
126
|
|
@@ -126,111 +128,111 @@ end
|
|
126
128
|
describe "PNM.read" do
|
127
129
|
|
128
130
|
it "raises an exception for integer argument" do
|
129
|
-
|
131
|
+
_ { PNM.read(123) }.must_raise PNM::ArgumentError
|
130
132
|
end
|
131
133
|
|
132
134
|
it "raises an exception for unknown magic number" do
|
133
135
|
file = StringIO.new("P0 1 1 0")
|
134
|
-
|
136
|
+
_ { PNM.read(file) }.must_raise PNM::ParserError
|
135
137
|
end
|
136
138
|
|
137
139
|
it "raises an exception for an empty file" do
|
138
140
|
file = StringIO.new("")
|
139
|
-
|
141
|
+
_ { PNM.read(file) }.must_raise PNM::ParserError
|
140
142
|
end
|
141
143
|
|
142
144
|
it "raises an exception for missing tokens (#1)" do
|
143
145
|
file = StringIO.new("P1")
|
144
|
-
|
146
|
+
_ { PNM.read(file) }.must_raise PNM::ParserError
|
145
147
|
end
|
146
148
|
|
147
149
|
it "raises an exception for missing tokens (#2)" do
|
148
150
|
file = StringIO.new("P1 1")
|
149
|
-
|
151
|
+
_ { PNM.read(file) }.must_raise PNM::ParserError
|
150
152
|
end
|
151
153
|
|
152
154
|
it "raises an exception for missing tokens (#3)" do
|
153
155
|
file = StringIO.new("P1 1 ")
|
154
|
-
|
156
|
+
_ { PNM.read(file) }.must_raise PNM::ParserError
|
155
157
|
end
|
156
158
|
|
157
159
|
it "raises an exception for missing tokens (#4)" do
|
158
160
|
file = StringIO.new("P1 1 1")
|
159
|
-
|
161
|
+
_ { PNM.read(file) }.must_raise PNM::ParserError
|
160
162
|
end
|
161
163
|
|
162
164
|
it "raises an exception for missing tokens (#5)" do
|
163
165
|
file = StringIO.new("P1 1 # Test\n 1")
|
164
|
-
|
166
|
+
_ { PNM.read(file) }.must_raise PNM::ParserError
|
165
167
|
end
|
166
168
|
|
167
169
|
it "raises an exception for missing tokens (#6)" do
|
168
170
|
file = StringIO.new("P2 1 1 255")
|
169
|
-
|
171
|
+
_ { PNM.read(file) }.must_raise PNM::ParserError
|
170
172
|
end
|
171
173
|
|
172
174
|
it "raises an exception for token of wrong type (#1)" do
|
173
175
|
file = StringIO.new("P1 ? 1 0")
|
174
|
-
|
176
|
+
_ { PNM.read(file) }.must_raise PNM::ParserError
|
175
177
|
end
|
176
178
|
|
177
179
|
it "raises an exception for token of wrong type (#2)" do
|
178
180
|
file = StringIO.new("P1 1 X 0")
|
179
|
-
|
181
|
+
_ { PNM.read(file) }.must_raise PNM::ParserError
|
180
182
|
end
|
181
183
|
|
182
184
|
it "raises an exception for token of wrong type (#3)" do
|
183
185
|
file = StringIO.new("P2 1 1 foo 0")
|
184
|
-
|
186
|
+
_ { PNM.read(file) }.must_raise PNM::ParserError
|
185
187
|
end
|
186
188
|
|
187
189
|
it "raises an exception for zero width" do
|
188
190
|
file = StringIO.new("P2 0 1 255 0")
|
189
|
-
|
191
|
+
_ { PNM.read(file) }.must_raise PNM::ParserError
|
190
192
|
end
|
191
193
|
|
192
194
|
it "raises an exception for zero height" do
|
193
195
|
file = StringIO.new("P2 1 0 255 0")
|
194
|
-
|
196
|
+
_ { PNM.read(file) }.must_raise PNM::ParserError
|
195
197
|
end
|
196
198
|
|
197
199
|
it "raises an exception for invalid maxgray (> 255)" do
|
198
200
|
file = StringIO.new("P2 1 1 256 0")
|
199
|
-
|
201
|
+
_ { PNM.read(file) }.must_raise PNM::ParserError
|
200
202
|
end
|
201
203
|
|
202
204
|
it "raises an exception for invalid maxgray (0)" do
|
203
205
|
file = StringIO.new("P2 1 1 0 0")
|
204
|
-
|
206
|
+
_ { PNM.read(file) }.must_raise PNM::ParserError
|
205
207
|
end
|
206
208
|
|
207
209
|
it "raises an exception for image dimension mismatch (#1)" do
|
208
210
|
file = StringIO.new("P1 2 3 0 0 0 0 0")
|
209
|
-
|
211
|
+
_ { PNM.read(file) }.must_raise PNM::DataSizeError
|
210
212
|
end
|
211
213
|
|
212
214
|
it "raises an exception for image dimension mismatch (#2)" do
|
213
215
|
file = StringIO.new("P1 2 3 0 0 0 0 0 0 0")
|
214
|
-
|
216
|
+
_ { PNM.read(file) }.must_raise PNM::DataSizeError
|
215
217
|
end
|
216
218
|
|
217
219
|
it "raises an exception for image dimension mismatch (#3)" do
|
218
220
|
file = StringIO.new("P3 2 3 255 0 0 0 0 0 0")
|
219
|
-
|
221
|
+
_ { PNM.read(file) }.must_raise PNM::DataSizeError
|
220
222
|
end
|
221
223
|
|
222
224
|
it "raises an exception for image dimension mismatch (#4)" do
|
223
225
|
file = StringIO.new("P5 2 3 255 AAAAAAA")
|
224
|
-
|
226
|
+
_ { PNM.read(file) }.must_raise PNM::DataSizeError
|
225
227
|
end
|
226
228
|
|
227
229
|
it "raises an exception for image dimension mismatch (#5)" do
|
228
230
|
file = StringIO.new("P5 2 3 255 AAAAAAA")
|
229
|
-
|
231
|
+
_ { PNM.read(file) }.must_raise PNM::DataSizeError
|
230
232
|
end
|
231
233
|
|
232
234
|
it "raises an exception for non-numeric image data" do
|
233
235
|
file = StringIO.new("P1 2 3 0 X 0 0 0 0")
|
234
|
-
|
236
|
+
_ { PNM.read(file) }.must_raise PNM::DataError
|
235
237
|
end
|
236
238
|
end
|
data/test/test_image.rb
CHANGED
@@ -2,11 +2,13 @@
|
|
2
2
|
|
3
3
|
# test_image.rb: Unit tests for the PNM library.
|
4
4
|
#
|
5
|
-
# Copyright (C) 2013-
|
5
|
+
# Copyright (C) 2013-2022 Marcus Stollsteimer
|
6
6
|
|
7
7
|
require "minitest/autorun"
|
8
8
|
require "pnm"
|
9
9
|
|
10
|
+
require_relative "backports"
|
11
|
+
|
10
12
|
|
11
13
|
describe PNM::Image do
|
12
14
|
|
@@ -47,229 +49,229 @@ describe PNM::Image do
|
|
47
49
|
end
|
48
50
|
|
49
51
|
it "freezes pixel data" do
|
50
|
-
|
52
|
+
_ { @bilevel.pixels << [1, 1, 0, 1, 1] }.must_raise RuntimeError
|
51
53
|
end
|
52
54
|
|
53
55
|
it "freezes comment string" do
|
54
|
-
|
56
|
+
_ { @bilevel.comment << "string" }.must_raise RuntimeError
|
55
57
|
end
|
56
58
|
|
57
59
|
it "sets maxgray to 1 for bilevel images" do
|
58
60
|
image = PNM.create([[0, 1, 0], [1, 0, 1]])
|
59
|
-
image.type.must_equal :pbm
|
60
|
-
image.maxgray.must_equal 1
|
61
|
+
_(image.type).must_equal :pbm
|
62
|
+
_(image.maxgray).must_equal 1
|
61
63
|
end
|
62
64
|
|
63
65
|
it "sets maxgray by default to 255 for grayscale images" do
|
64
66
|
image = PNM.create([[0, 10, 20], [10, 20, 30]])
|
65
|
-
image.type.must_equal :pgm
|
66
|
-
image.maxgray.must_equal 255
|
67
|
+
_(image.type).must_equal :pgm
|
68
|
+
_(image.maxgray).must_equal 255
|
67
69
|
end
|
68
70
|
|
69
71
|
it "sets maxgray by default to 255 for color images" do
|
70
72
|
image = PNM.create([[[0, 0, 0], [10, 10, 10]], [[10, 10, 10], [20, 20, 20]]])
|
71
|
-
image.type.must_equal :ppm
|
72
|
-
image.maxgray.must_equal 255
|
73
|
+
_(image.type).must_equal :ppm
|
74
|
+
_(image.maxgray).must_equal 255
|
73
75
|
end
|
74
76
|
|
75
77
|
it "accepts setting of maxgray to 1 for bilevel images" do
|
76
78
|
image = PNM.create([[0, 1, 0], [1, 0, 1]], maxgray: 1)
|
77
|
-
image.type.must_equal :pbm
|
78
|
-
image.maxgray.must_equal 1
|
79
|
+
_(image.type).must_equal :pbm
|
80
|
+
_(image.maxgray).must_equal 1
|
79
81
|
end
|
80
82
|
|
81
83
|
it "ignores invalid maxgray for bilevel images and sets it to 1" do
|
82
84
|
image = PNM.create([[0, 1, 0], [1, 0, 1]], type: :pbm, maxgray: 255)
|
83
|
-
image.type.must_equal :pbm
|
84
|
-
image.maxgray.must_equal 1
|
85
|
+
_(image.type).must_equal :pbm
|
86
|
+
_(image.maxgray).must_equal 1
|
85
87
|
end
|
86
88
|
|
87
89
|
it "can create a grayscale image from bilevel values (using type)" do
|
88
90
|
image = PNM.create([[0, 1, 0], [1, 0, 1]], type: :pgm)
|
89
|
-
image.type.must_equal :pgm
|
90
|
-
image.pixels.must_equal [[0, 1, 0], [1, 0, 1]]
|
91
|
-
image.maxgray.must_equal 255
|
91
|
+
_(image.type).must_equal :pgm
|
92
|
+
_(image.pixels).must_equal [[0, 1, 0], [1, 0, 1]]
|
93
|
+
_(image.maxgray).must_equal 255
|
92
94
|
end
|
93
95
|
|
94
96
|
it "also accepts types given as string instead of symbol" do
|
95
97
|
image = PNM.create([[0, 1, 0], [1, 0, 1]], type: "pgm")
|
96
|
-
image.type.must_equal :pgm
|
98
|
+
_(image.type).must_equal :pgm
|
97
99
|
end
|
98
100
|
|
99
101
|
it "can create a grayscale image from bilevel values (using maxgray)" do
|
100
102
|
image = PNM.create([[0, 1, 0], [1, 0, 1]], maxgray: 2)
|
101
|
-
image.type.must_equal :pgm
|
102
|
-
image.pixels.must_equal [[0, 1, 0], [1, 0, 1]]
|
103
|
-
image.maxgray.must_equal 2
|
103
|
+
_(image.type).must_equal :pgm
|
104
|
+
_(image.pixels).must_equal [[0, 1, 0], [1, 0, 1]]
|
105
|
+
_(image.maxgray).must_equal 2
|
104
106
|
end
|
105
107
|
|
106
108
|
it "can create a color image from bilevel values" do
|
107
109
|
image = PNM.create([[0, 1, 0], [1, 0, 1]], type: :ppm)
|
108
|
-
image.info.
|
109
|
-
image.pixels.must_equal [[[0, 0, 0], [1, 1, 1], [0, 0, 0]], [[1, 1, 1], [0, 0, 0], [1, 1, 1]]]
|
110
|
-
image.maxgray.must_equal 255
|
110
|
+
_(image.info).must_equal "PPM 3x2 Color"
|
111
|
+
_(image.pixels).must_equal [[[0, 0, 0], [1, 1, 1], [0, 0, 0]], [[1, 1, 1], [0, 0, 0], [1, 1, 1]]]
|
112
|
+
_(image.maxgray).must_equal 255
|
111
113
|
end
|
112
114
|
|
113
115
|
it "can create a color image from bilevel values with a given maxgray" do
|
114
116
|
image = PNM.create([[0, 1, 0], [1, 0, 1]], type: :ppm, maxgray: 2)
|
115
|
-
image.info.
|
116
|
-
image.pixels.must_equal [[[0, 0, 0], [1, 1, 1], [0, 0, 0]], [[1, 1, 1], [0, 0, 0], [1, 1, 1]]]
|
117
|
-
image.maxgray.must_equal 2
|
117
|
+
_(image.info).must_equal "PPM 3x2 Color"
|
118
|
+
_(image.pixels).must_equal [[[0, 0, 0], [1, 1, 1], [0, 0, 0]], [[1, 1, 1], [0, 0, 0], [1, 1, 1]]]
|
119
|
+
_(image.maxgray).must_equal 2
|
118
120
|
end
|
119
121
|
|
120
122
|
it "can create a color image from gray values" do
|
121
123
|
image = PNM.create([[0, 3, 6], [3, 6, 9]], type: :ppm)
|
122
|
-
image.info.
|
123
|
-
image.pixels.must_equal [[[0, 0, 0], [3, 3, 3], [6, 6, 6]], [[3, 3, 3], [6, 6, 6], [9, 9, 9]]]
|
124
|
+
_(image.info).must_equal "PPM 3x2 Color"
|
125
|
+
_(image.pixels).must_equal [[[0, 0, 0], [3, 3, 3], [6, 6, 6]], [[3, 3, 3], [6, 6, 6], [9, 9, 9]]]
|
124
126
|
end
|
125
127
|
|
126
128
|
it "does not modify the input data for color images created from gray values" do
|
127
129
|
data = [[0, 3, 6], [3, 6, 9]]
|
128
130
|
PNM.create(data, type: :ppm)
|
129
|
-
data.must_equal [[0, 3, 6], [3, 6, 9]]
|
131
|
+
_(data).must_equal [[0, 3, 6], [3, 6, 9]]
|
130
132
|
end
|
131
133
|
|
132
134
|
it "can write a bilevel image to an ASCII encoded file" do
|
133
|
-
@bilevel.write(@temp_path, :ascii)
|
134
|
-
File.binread(@temp_path).must_equal File.binread("#{@srcpath}/bilevel_ascii.pbm")
|
135
|
+
@bilevel.write(@temp_path, encoding: :ascii)
|
136
|
+
_(File.binread(@temp_path)).must_equal File.binread("#{@srcpath}/bilevel_ascii.pbm")
|
135
137
|
File.delete(@temp_path)
|
136
138
|
end
|
137
139
|
|
138
140
|
it "can write a bilevel image (width 5) to a binary encoded file" do
|
139
|
-
@bilevel.write(@temp_path, :binary)
|
140
|
-
File.binread(@temp_path).must_equal File.binread("#{@srcpath}/bilevel_binary.pbm")
|
141
|
+
@bilevel.write(@temp_path, encoding: :binary)
|
142
|
+
_(File.binread(@temp_path)).must_equal File.binread("#{@srcpath}/bilevel_binary.pbm")
|
141
143
|
File.delete(@temp_path)
|
142
144
|
end
|
143
145
|
|
144
146
|
it "can write a bilevel image (width 16) to a binary encoded file" do
|
145
|
-
@bilevel2.write(@temp_path, :binary)
|
146
|
-
File.binread(@temp_path).must_equal File.binread("#{@srcpath}/bilevel_2_binary.pbm")
|
147
|
+
@bilevel2.write(@temp_path, encoding: :binary)
|
148
|
+
_(File.binread(@temp_path)).must_equal File.binread("#{@srcpath}/bilevel_2_binary.pbm")
|
147
149
|
File.delete(@temp_path)
|
148
150
|
end
|
149
151
|
|
150
152
|
it "can write a grayscale image to an ASCII encoded file" do
|
151
|
-
@grayscale.write(@temp_path, :ascii)
|
152
|
-
File.binread(@temp_path).must_equal File.binread("#{@srcpath}/grayscale_ascii.pgm")
|
153
|
+
@grayscale.write(@temp_path, encoding: :ascii)
|
154
|
+
_(File.binread(@temp_path)).must_equal File.binread("#{@srcpath}/grayscale_ascii.pgm")
|
153
155
|
File.delete(@temp_path)
|
154
156
|
end
|
155
157
|
|
156
158
|
it "can write a grayscale image to a binary encoded file" do
|
157
|
-
@grayscale.write(@temp_path, :binary)
|
158
|
-
File.binread(@temp_path).must_equal File.binread("#{@srcpath}/grayscale_binary.pgm")
|
159
|
+
@grayscale.write(@temp_path, encoding: :binary)
|
160
|
+
_(File.binread(@temp_path)).must_equal File.binread("#{@srcpath}/grayscale_binary.pgm")
|
159
161
|
File.delete(@temp_path)
|
160
162
|
end
|
161
163
|
|
162
164
|
it "can write a color image to an ASCII encoded file" do
|
163
|
-
@color.write(@temp_path, :ascii)
|
164
|
-
File.binread(@temp_path).must_equal File.binread("#{@srcpath}/color_ascii.ppm")
|
165
|
+
@color.write(@temp_path, encoding: :ascii)
|
166
|
+
_(File.binread(@temp_path)).must_equal File.binread("#{@srcpath}/color_ascii.ppm")
|
165
167
|
File.delete(@temp_path)
|
166
168
|
end
|
167
169
|
|
168
170
|
it "can write a color image to a binary encoded file" do
|
169
|
-
@color.write(@temp_path, :binary)
|
170
|
-
File.binread(@temp_path).must_equal File.binread("#{@srcpath}/color_binary.ppm")
|
171
|
+
@color.write(@temp_path, encoding: :binary)
|
172
|
+
_(File.binread(@temp_path)).must_equal File.binread("#{@srcpath}/color_binary.ppm")
|
171
173
|
File.delete(@temp_path)
|
172
174
|
end
|
173
175
|
|
174
176
|
it "can write a bilevel image to a file, adding the extension" do
|
175
|
-
@bilevel.
|
176
|
-
File.binread("#{@temp_path}.pbm").must_equal File.binread("#{@srcpath}/bilevel_binary.pbm")
|
177
|
+
@bilevel.write(@temp_path, add_extension: true)
|
178
|
+
_(File.binread("#{@temp_path}.pbm")).must_equal File.binread("#{@srcpath}/bilevel_binary.pbm")
|
177
179
|
File.delete("#{@temp_path}.pbm")
|
178
180
|
end
|
179
181
|
|
180
182
|
it "can write a grayscale image to a file, adding the extension" do
|
181
|
-
@grayscale.
|
182
|
-
File.binread("#{@temp_path}.pgm").must_equal File.binread("#{@srcpath}/grayscale_ascii.pgm")
|
183
|
+
@grayscale.write(@temp_path, add_extension: true, encoding: :ascii)
|
184
|
+
_(File.binread("#{@temp_path}.pgm")).must_equal File.binread("#{@srcpath}/grayscale_ascii.pgm")
|
183
185
|
File.delete("#{@temp_path}.pgm")
|
184
186
|
end
|
185
187
|
|
186
188
|
it "can write a color image to a file, adding the extension" do
|
187
|
-
@color.
|
188
|
-
File.binread("#{@temp_path}.ppm").must_equal File.binread("#{@srcpath}/color_binary.ppm")
|
189
|
+
@color.write(@temp_path, add_extension: true, encoding: :binary)
|
190
|
+
_(File.binread("#{@temp_path}.ppm")).must_equal File.binread("#{@srcpath}/color_binary.ppm")
|
189
191
|
File.delete("#{@temp_path}.ppm")
|
190
192
|
end
|
191
193
|
|
192
194
|
it "can return image information" do
|
193
|
-
@bilevel.info.must_equal "PBM 5x6 Bilevel"
|
194
|
-
@grayscale.info.must_equal "PGM 4x3 Grayscale"
|
195
|
-
@color.info.must_equal "PPM 5x3 Color"
|
195
|
+
_(@bilevel.info).must_equal "PBM 5x6 Bilevel"
|
196
|
+
_(@grayscale.info).must_equal "PGM 4x3 Grayscale"
|
197
|
+
_(@color.info).must_equal "PPM 5x3 Color"
|
196
198
|
end
|
197
199
|
|
198
200
|
it "can return meaningful debugging information" do
|
199
|
-
@bilevel.inspect.must_match %r{
|
200
|
-
@grayscale.inspect.must_match %r{
|
201
|
-
@color.inspect.must_match %r{
|
201
|
+
_(@bilevel.inspect).must_match %r{\A#<PNM::PBMImage:0x\h+ PBM 5x6 Bilevel>\z}
|
202
|
+
_(@grayscale.inspect).must_match %r{\A#<PNM::PGMImage:0x\h+ PGM 4x3 Grayscale, maxgray=250>\z}
|
203
|
+
_(@color.inspect).must_match %r{\A#<PNM::PPMImage:0x\h+ PPM 5x3 Color, maxgray=6>\z}
|
202
204
|
end
|
203
205
|
|
204
206
|
it "can write binary data containing CRLF" do
|
205
|
-
@grayscale_crlf.write(@temp_path, :binary)
|
206
|
-
File.binread(@temp_path).must_equal File.binread("#{@srcpath}/grayscale_binary_crlf.pgm")
|
207
|
+
@grayscale_crlf.write(@temp_path, encoding: :binary)
|
208
|
+
_(File.binread(@temp_path)).must_equal File.binread("#{@srcpath}/grayscale_binary_crlf.pgm")
|
207
209
|
File.delete(@temp_path)
|
208
210
|
end
|
209
211
|
|
210
212
|
it "can write binary data containing CRLF to an I/O stream" do
|
211
|
-
File.open(@temp_path, "w") {|f| @grayscale_crlf.write(f, :binary) }
|
212
|
-
File.binread(@temp_path).must_equal File.binread("#{@srcpath}/grayscale_binary_crlf.pgm")
|
213
|
+
File.open(@temp_path, "w") {|f| @grayscale_crlf.write(f, encoding: :binary) }
|
214
|
+
_(File.binread(@temp_path)).must_equal File.binread("#{@srcpath}/grayscale_binary_crlf.pgm")
|
213
215
|
File.delete(@temp_path)
|
214
216
|
end
|
215
217
|
|
216
218
|
it "can write zero-length comments" do
|
217
219
|
comment = ""
|
218
|
-
PNM.create([[0, 0]], comment: comment).write(@temp_path, :ascii)
|
219
|
-
File.binread(@temp_path).must_equal "P1\n#\n2 1\n0 0\n"
|
220
|
+
PNM.create([[0, 0]], comment: comment).write(@temp_path, encoding: :ascii)
|
221
|
+
_(File.binread(@temp_path)).must_equal "P1\n#\n2 1\n0 0\n"
|
220
222
|
File.delete(@temp_path)
|
221
223
|
end
|
222
224
|
|
223
225
|
it "can write comments with trailing zero-length line" do
|
224
226
|
comment = "An empty line:\n"
|
225
|
-
PNM.create([[0, 0]], comment: comment).write(@temp_path, :ascii)
|
226
|
-
File.binread(@temp_path).must_equal "P1\n# An empty line:\n#\n2 1\n0 0\n"
|
227
|
+
PNM.create([[0, 0]], comment: comment).write(@temp_path, encoding: :ascii)
|
228
|
+
_(File.binread(@temp_path)).must_equal "P1\n# An empty line:\n#\n2 1\n0 0\n"
|
227
229
|
File.delete(@temp_path)
|
228
230
|
end
|
229
231
|
|
230
232
|
it "can check equality of images (1)" do
|
231
233
|
pixels = [[0, 1, 0], [1, 0, 1]]
|
232
|
-
|
234
|
+
bilevel1 = PNM.create(pixels, comment: "image")
|
233
235
|
bilevel2 = PNM.create(pixels, comment: "image")
|
234
236
|
|
235
|
-
(bilevel2 ==
|
237
|
+
_(bilevel2 == bilevel1).must_equal true
|
236
238
|
end
|
237
239
|
|
238
240
|
it "can check equality of images (2)" do
|
239
241
|
pixels = [[0, 1, 0], [1, 0, 1]]
|
240
|
-
|
242
|
+
bilevel1 = PNM.create(pixels, comment: "image")
|
241
243
|
bilevel2 = PNM.create(pixels, comment: "other image")
|
242
244
|
|
243
|
-
(bilevel2 ==
|
245
|
+
_(bilevel2 == bilevel1).must_equal false
|
244
246
|
end
|
245
247
|
|
246
248
|
it "can check equality of images (3)" do
|
247
249
|
pixels = [[0, 1, 0], [1, 0, 1]]
|
248
|
-
|
250
|
+
bilevel1 = PNM.create(pixels)
|
249
251
|
bilevel2 = PNM.create(pixels.reverse)
|
250
252
|
|
251
|
-
(bilevel2 ==
|
253
|
+
_(bilevel2 == bilevel1).must_equal false
|
252
254
|
end
|
253
255
|
|
254
256
|
it "can check equality of images (4)" do
|
255
257
|
pixels = [[0, 1, 0], [1, 0, 1]]
|
256
|
-
bilevel
|
258
|
+
bilevel = PNM.create(pixels, type: :pbm)
|
257
259
|
graylevel = PNM.create(pixels, type: :pgm)
|
258
260
|
|
259
|
-
(graylevel == bilevel).must_equal false
|
261
|
+
_(graylevel == bilevel).must_equal false
|
260
262
|
end
|
261
263
|
|
262
264
|
it "can check equality of images (5)" do
|
263
265
|
pixels = [[0, 1, 2], [3, 4, 5]]
|
264
|
-
|
266
|
+
graylevel1 = PNM.create(pixels, maxgray: 10)
|
265
267
|
graylevel2 = PNM.create(pixels, maxgray: 255)
|
266
268
|
|
267
|
-
(graylevel2 ==
|
269
|
+
_(graylevel2 == graylevel1).must_equal false
|
268
270
|
end
|
269
271
|
|
270
272
|
it "can check equality of images (6)" do
|
271
273
|
image = PNM.create([[0, 1, 2], [3, 4, 5]])
|
272
274
|
|
273
|
-
(image == "a string").must_equal false
|
275
|
+
_(image == "a string").must_equal false
|
274
276
|
end
|
275
277
|
end
|