pnm 0.4.1 → 0.5.0
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 +19 -11
- data/lib/pnm.rb +13 -8
- data/lib/pnm/image.rb +21 -0
- data/lib/pnm/version.rb +2 -2
- data/pnm.gemspec +2 -1
- data/test/test_image.rb +72 -0
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 597b7231eeda234ed2c8a7446ececac07573d3dd
|
4
|
+
data.tar.gz: a2d45050c7c3449b024fe33834b6ade73192b1fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d00bcb7995660c471798433176e2328d00130fc54cef1fb3aeb9971b66250a113145dc11fde15d7bd077ab36ba4193cf75e5f0c0cbd0bc44fc6f1074caf4849a
|
7
|
+
data.tar.gz: 2d7287bb435de4a414bb05705a273a2ddf3d0f18bb3f02f14c1a8b132d2ef98e05be27755e7f9067bdab1965270ac7e81f43db3859dd8c1bf7a0a2327dfb3fcd
|
data/README.md
CHANGED
@@ -20,14 +20,14 @@ Examples
|
|
20
20
|
Create a PGM grayscale image from a two-dimensional array of gray values:
|
21
21
|
|
22
22
|
``` ruby
|
23
|
-
require
|
23
|
+
require "pnm"
|
24
24
|
|
25
25
|
# pixel data
|
26
26
|
pixels = [[ 0, 10, 20],
|
27
27
|
[10, 20, 30]]
|
28
28
|
|
29
29
|
# optional settings
|
30
|
-
options = {:maxgray => 30, :comment =>
|
30
|
+
options = {:maxgray => 30, :comment => "Test Image"}
|
31
31
|
|
32
32
|
# create the image object
|
33
33
|
image = PNM.create(pixels, options)
|
@@ -49,19 +49,20 @@ and available options.
|
|
49
49
|
Write an image to a file:
|
50
50
|
|
51
51
|
``` ruby
|
52
|
-
image.write(
|
52
|
+
image.write("test.pgm")
|
53
|
+
image.write_with_extension("test") # adds the correct file extension
|
53
54
|
|
54
55
|
# use ASCII or "plain" format (default is binary)
|
55
|
-
image.write(
|
56
|
+
image.write("test.pgm", :ascii)
|
56
57
|
|
57
58
|
# write to an I/O stream
|
58
|
-
File.open(
|
59
|
+
File.open("test.pgm", "w") {|f| image.write(f) }
|
59
60
|
```
|
60
61
|
|
61
62
|
Read an image from a file (returns a PNM::Image object):
|
62
63
|
|
63
64
|
``` ruby
|
64
|
-
image = PNM.read(
|
65
|
+
image = PNM.read("test.pgm")
|
65
66
|
image.comment # => "Test Image"
|
66
67
|
image.maxgray # => 30
|
67
68
|
image.pixels # => [[0, 10, 20], [10, 20, 30]]
|
@@ -70,8 +71,14 @@ image.pixels # => [[0, 10, 20], [10, 20, 30]]
|
|
70
71
|
Force an image type:
|
71
72
|
|
72
73
|
``` ruby
|
73
|
-
|
74
|
-
|
74
|
+
color_image = PNM.create([[0, 1],[1, 0]], :type => :ppm)
|
75
|
+
color_image.info # => "PPM 2x2 Color"
|
76
|
+
```
|
77
|
+
|
78
|
+
Check equality of two images:
|
79
|
+
|
80
|
+
``` ruby
|
81
|
+
color_image == image # => false
|
75
82
|
```
|
76
83
|
|
77
84
|
Installation
|
@@ -95,13 +102,14 @@ Requirements
|
|
95
102
|
- Ruby 2.1,
|
96
103
|
- Ruby 2.0.0,
|
97
104
|
- Ruby 1.9.3,
|
98
|
-
- JRuby 1.7.
|
99
|
-
- Rubinius 2.2.
|
105
|
+
- JRuby 1.7.19,
|
106
|
+
- Rubinius 2.5.2.
|
100
107
|
|
101
108
|
Documentation
|
102
109
|
-------------
|
103
110
|
|
104
|
-
Documentation should be available via `ri PNM
|
111
|
+
Documentation should be available via `ri PNM` or can be found at
|
112
|
+
[www.rubydoc.info/gems/pnm/](http://www.rubydoc.info/gems/pnm/).
|
105
113
|
|
106
114
|
Reporting bugs
|
107
115
|
--------------
|
data/lib/pnm.rb
CHANGED
@@ -26,14 +26,14 @@ require_relative 'pnm/exceptions'
|
|
26
26
|
#
|
27
27
|
# Create a PGM grayscale image from a two-dimensional array of gray values:
|
28
28
|
#
|
29
|
-
# require
|
29
|
+
# require "pnm"
|
30
30
|
#
|
31
31
|
# # pixel data
|
32
32
|
# pixels = [[ 0, 10, 20],
|
33
33
|
# [10, 20, 30]]
|
34
34
|
#
|
35
35
|
# # optional settings
|
36
|
-
# options = {:maxgray => 30, :comment =>
|
36
|
+
# options = {:maxgray => 30, :comment => "Test Image"}
|
37
37
|
#
|
38
38
|
# # create the image object
|
39
39
|
# image = PNM.create(pixels, options)
|
@@ -53,25 +53,30 @@ require_relative 'pnm/exceptions'
|
|
53
53
|
#
|
54
54
|
# Write an image to a file:
|
55
55
|
#
|
56
|
-
# image.write(
|
56
|
+
# image.write("test.pgm")
|
57
|
+
# image.write_with_extension("test") # adds the correct file extension
|
57
58
|
#
|
58
59
|
# # use ASCII or "plain" format (default is binary)
|
59
|
-
# image.write(
|
60
|
+
# image.write("test.pgm", :ascii)
|
60
61
|
#
|
61
62
|
# # write to an I/O stream
|
62
|
-
# File.open(
|
63
|
+
# File.open("test.pgm", "w") {|f| image.write(f) }
|
63
64
|
#
|
64
65
|
# Read an image from a file (returns a PNM::Image object):
|
65
66
|
#
|
66
|
-
# image = PNM.read(
|
67
|
+
# image = PNM.read("test.pgm")
|
67
68
|
# image.comment # => "Test Image"
|
68
69
|
# image.maxgray # => 30
|
69
70
|
# image.pixels # => [[0, 10, 20], [10, 20, 30]]
|
70
71
|
#
|
71
72
|
# Force an image type:
|
72
73
|
#
|
73
|
-
#
|
74
|
-
#
|
74
|
+
# color_image = PNM.create([[0, 1],[1, 0]], :type => :ppm)
|
75
|
+
# color_image.info # => "PPM 2x2 Color"
|
76
|
+
#
|
77
|
+
# Check equality of two images:
|
78
|
+
#
|
79
|
+
# color_image == image # => false
|
75
80
|
#
|
76
81
|
# == See also
|
77
82
|
#
|
data/lib/pnm/image.rb
CHANGED
@@ -80,6 +80,9 @@ module PNM
|
|
80
80
|
assert_pixel_value_range
|
81
81
|
|
82
82
|
post_initialize
|
83
|
+
|
84
|
+
@pixels.freeze
|
85
|
+
@comment.freeze
|
83
86
|
end
|
84
87
|
|
85
88
|
# Writes the image to +file+ (a filename or an IO object),
|
@@ -102,6 +105,15 @@ module PNM
|
|
102
105
|
end
|
103
106
|
end
|
104
107
|
|
108
|
+
# Adds the appropriate file extension to +basename+
|
109
|
+
# (+.pbm+, +.pgm+, or +.ppm+)
|
110
|
+
# and writes the image to the resulting filename.
|
111
|
+
#
|
112
|
+
# Any options are passed on to #write, which is used internally.
|
113
|
+
def write_with_extension(basename, *args)
|
114
|
+
write("#{basename}.#{type}", *args)
|
115
|
+
end
|
116
|
+
|
105
117
|
# Returns a string with a short image format description.
|
106
118
|
def info
|
107
119
|
"#{type.to_s.upcase} #{width}x#{height} #{type_string}"
|
@@ -114,6 +126,15 @@ module PNM
|
|
114
126
|
# implemented by subclasses
|
115
127
|
end
|
116
128
|
|
129
|
+
# Equality --- Two images are considered equal if they have
|
130
|
+
# the same pixel values, type, maxgray, and comments.
|
131
|
+
def ==(other)
|
132
|
+
return true if other.equal?(self)
|
133
|
+
return false unless other.instance_of?(self.class)
|
134
|
+
|
135
|
+
type == other.type && maxgray == other.maxgray && comment == other.comment && pixels == other.pixels
|
136
|
+
end
|
137
|
+
|
117
138
|
private
|
118
139
|
|
119
140
|
def self.assert_valid_array(pixels) # :nodoc:
|
data/lib/pnm/version.rb
CHANGED
data/pnm.gemspec
CHANGED
data/test/test_image.rb
CHANGED
@@ -44,6 +44,14 @@ describe PNM::Image do
|
|
44
44
|
@color = PNM.create(pixels, {:maxgray => 6})
|
45
45
|
end
|
46
46
|
|
47
|
+
it 'freezes pixel data' do
|
48
|
+
lambda { @bilevel.pixels << [1,1,0,1,1] }.must_raise RuntimeError
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'freezes comment string' do
|
52
|
+
lambda { @bilevel.comment << "string" }.must_raise RuntimeError
|
53
|
+
end
|
54
|
+
|
47
55
|
it 'sets maxgray to 1 for bilevel images' do
|
48
56
|
image = PNM.create([[0,1,0], [1,0,1]])
|
49
57
|
image.type.must_equal :pbm
|
@@ -161,6 +169,24 @@ describe PNM::Image do
|
|
161
169
|
File.delete(@temp_path)
|
162
170
|
end
|
163
171
|
|
172
|
+
it 'can write a bilevel image to a file, adding the extension' do
|
173
|
+
@bilevel.write_with_extension(@temp_path)
|
174
|
+
File.binread("#{@temp_path}.pbm").must_equal File.binread("#{@srcpath}/bilevel_binary.pbm")
|
175
|
+
File.delete("#{@temp_path}.pbm")
|
176
|
+
end
|
177
|
+
|
178
|
+
it 'can write a grayscale image to a file, adding the extension' do
|
179
|
+
@grayscale.write_with_extension(@temp_path, :ascii)
|
180
|
+
File.binread("#{@temp_path}.pgm").must_equal File.binread("#{@srcpath}/grayscale_ascii.pgm")
|
181
|
+
File.delete("#{@temp_path}.pgm")
|
182
|
+
end
|
183
|
+
|
184
|
+
it 'can write a color image to a file, adding the extension' do
|
185
|
+
@color.write_with_extension(@temp_path, :binary)
|
186
|
+
File.binread("#{@temp_path}.ppm").must_equal File.binread("#{@srcpath}/color_binary.ppm")
|
187
|
+
File.delete("#{@temp_path}.ppm")
|
188
|
+
end
|
189
|
+
|
164
190
|
it 'can return image information' do
|
165
191
|
@bilevel.info.must_equal 'PBM 5x6 Bilevel'
|
166
192
|
@grayscale.info.must_equal 'PGM 4x3 Grayscale'
|
@@ -198,4 +224,50 @@ describe PNM::Image do
|
|
198
224
|
File.binread(@temp_path).must_equal "P1\n# An empty line:\n#\n2 1\n0 0\n"
|
199
225
|
File.delete(@temp_path)
|
200
226
|
end
|
227
|
+
|
228
|
+
it 'can check equality of images (1)' do
|
229
|
+
pixels = [[0,1,0], [1,0,1]]
|
230
|
+
bilevel = PNM.create(pixels, {:comment => "image"})
|
231
|
+
bilevel2 = PNM.create(pixels, {:comment => "image"})
|
232
|
+
|
233
|
+
(bilevel2 == bilevel).must_equal true
|
234
|
+
end
|
235
|
+
|
236
|
+
it 'can check equality of images (2)' do
|
237
|
+
pixels = [[0,1,0], [1,0,1]]
|
238
|
+
bilevel = PNM.create(pixels, {:comment => "image"})
|
239
|
+
bilevel2 = PNM.create(pixels, {:comment => "other image"})
|
240
|
+
|
241
|
+
(bilevel2 == bilevel).must_equal false
|
242
|
+
end
|
243
|
+
|
244
|
+
it 'can check equality of images (3)' do
|
245
|
+
pixels = [[0,1,0], [1,0,1]]
|
246
|
+
bilevel = PNM.create(pixels)
|
247
|
+
bilevel2 = PNM.create(pixels.reverse)
|
248
|
+
|
249
|
+
(bilevel2 == bilevel).must_equal false
|
250
|
+
end
|
251
|
+
|
252
|
+
it 'can check equality of images (4)' do
|
253
|
+
pixels = [[0,1,0], [1,0,1]]
|
254
|
+
bilevel = PNM.create(pixels, {:type => :pbm})
|
255
|
+
graylevel = PNM.create(pixels, {:type => :pgm})
|
256
|
+
|
257
|
+
(graylevel == bilevel).must_equal false
|
258
|
+
end
|
259
|
+
|
260
|
+
it 'can check equality of images (5)' do
|
261
|
+
pixels = [[0,1,2], [3,4,5]]
|
262
|
+
graylevel = PNM.create(pixels, {:maxgray => 10})
|
263
|
+
graylevel2 = PNM.create(pixels, {:maxgray => 255})
|
264
|
+
|
265
|
+
(graylevel2 == graylevel).must_equal false
|
266
|
+
end
|
267
|
+
|
268
|
+
it 'can check equality of images (6)' do
|
269
|
+
image = PNM.create([[0,1,2], [3,4,5]])
|
270
|
+
|
271
|
+
(image == "a string").must_equal false
|
272
|
+
end
|
201
273
|
end
|
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.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcus Stollsteimer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-09-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -44,7 +44,8 @@ description: 'PNM is a pure Ruby library for creating, reading, and writing of P
|
|
44
44
|
email: sto.mar@web.de
|
45
45
|
executables: []
|
46
46
|
extensions: []
|
47
|
-
extra_rdoc_files:
|
47
|
+
extra_rdoc_files:
|
48
|
+
- README.md
|
48
49
|
files:
|
49
50
|
- ".yardopts"
|
50
51
|
- README.md
|
@@ -80,6 +81,7 @@ metadata: {}
|
|
80
81
|
post_install_message:
|
81
82
|
rdoc_options:
|
82
83
|
- "--charset=UTF-8"
|
84
|
+
- "--main=README.md"
|
83
85
|
require_paths:
|
84
86
|
- lib
|
85
87
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -94,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
96
|
version: '0'
|
95
97
|
requirements: []
|
96
98
|
rubyforge_project:
|
97
|
-
rubygems_version: 2.4.
|
99
|
+
rubygems_version: 2.4.8
|
98
100
|
signing_key:
|
99
101
|
specification_version: 4
|
100
102
|
summary: PNM - create/read/write PNM image files (PBM, PGM, PPM)
|
@@ -104,4 +106,3 @@ test_files:
|
|
104
106
|
- test/test_parser.rb
|
105
107
|
- test/test_converter.rb
|
106
108
|
- test/test_pnm.rb
|
107
|
-
has_rdoc:
|