pnm 0.1.0 → 0.2.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 +40 -24
- data/benchmark/bm_converter.rb +0 -1
- data/lib/pnm.rb +8 -3
- data/lib/pnm/image.rb +30 -9
- data/lib/pnm/version.rb +2 -2
- data/test/test_image.rb +59 -7
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d185b233565cd87cea47d7a47fa8cd53cbd3120d
|
4
|
+
data.tar.gz: 65754714716d6b399cc8950e0ff90584cb42485a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0737d6700d5ce343d0e326006213640eebe097181a47e3d99a70610a4f4aed95765a2fc1ccbee7b767177a9279224ef5814ee4fd53891a9de9b1eb0b9ca8a131
|
7
|
+
data.tar.gz: 2633bcdac0cf6fae08e3c36a9a81a6af985712273a9dc5361824794713c778300f12fa5ebee86eff5a34a7bf8bc4f38b4287f4e97887f3ca5685ea447c4d607f
|
data/README.md
CHANGED
@@ -13,50 +13,62 @@ Examples
|
|
13
13
|
|
14
14
|
Create a PGM grayscale image from a two-dimensional array of gray values:
|
15
15
|
|
16
|
-
|
16
|
+
``` ruby
|
17
|
+
require 'pnm'
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
19
|
+
# pixel data
|
20
|
+
pixels = [[ 0, 10, 20],
|
21
|
+
[10, 20, 30]]
|
21
22
|
|
22
|
-
|
23
|
-
|
23
|
+
# optional settings
|
24
|
+
options = {:maxgray => 30, :comment => 'Test Image'}
|
24
25
|
|
25
|
-
|
26
|
-
|
26
|
+
# create the image object
|
27
|
+
image = PNM::Image.new(pixels, options)
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
# retrieve some image properties
|
30
|
+
image.info # => "PGM 3x2 Grayscale"
|
31
|
+
image.width # => 3
|
32
|
+
image.height # => 2
|
33
|
+
```
|
32
34
|
|
33
35
|
See PNM::Image.new for a more detailed description of pixel data formats
|
34
36
|
and available options.
|
35
37
|
|
36
38
|
Write an image to a file:
|
37
39
|
|
38
|
-
|
40
|
+
``` ruby
|
41
|
+
image.write('test.pgm')
|
39
42
|
|
40
|
-
|
41
|
-
|
43
|
+
# use ASCII or "plain" format (default is binary)
|
44
|
+
image.write('test.pgm', :ascii)
|
42
45
|
|
43
|
-
|
44
|
-
|
46
|
+
# write to an I/O stream
|
47
|
+
File.open('test.pgm', 'w') {|f| image.write(f) }
|
48
|
+
```
|
45
49
|
|
46
50
|
Read an image from a file (returns a PNM::Image object):
|
47
51
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
+
``` ruby
|
53
|
+
image = PNM.read('test.pgm')
|
54
|
+
image.comment # => "Test Image"
|
55
|
+
image.maxgray # => 30
|
56
|
+
image.pixels # => [[0, 10, 20], [10, 20, 30]]
|
57
|
+
```
|
52
58
|
|
59
|
+
Force an image type:
|
60
|
+
|
61
|
+
``` ruby
|
62
|
+
image = PNM::Image.new([[0, 1],[1, 0]], :type => :ppm)
|
63
|
+
image.info # => "PPM 2x2 Color"
|
64
|
+
```
|
53
65
|
|
54
66
|
Installation
|
55
67
|
------------
|
56
68
|
|
57
69
|
To install PNM, you can either
|
58
70
|
|
59
|
-
- use `gem install pnm
|
71
|
+
- use `gem install pnm` to install from RubyGems.org,
|
60
72
|
|
61
73
|
- clone or download the repository and use
|
62
74
|
`rake build` and `[sudo] gem install pnm`.
|
@@ -66,8 +78,12 @@ Requirements
|
|
66
78
|
|
67
79
|
- No additional Ruby gems or native libraries are needed.
|
68
80
|
|
69
|
-
- PNM has been tested with
|
70
|
-
|
81
|
+
- PNM has been tested with
|
82
|
+
|
83
|
+
- Ruby 2.0.0 on Linux and on Windows,
|
84
|
+
- Ruby 1.9.3,
|
85
|
+
- JRuby 1.7.8,
|
86
|
+
- Rubinius 2.2.1.
|
71
87
|
|
72
88
|
Documentation
|
73
89
|
-------------
|
data/benchmark/bm_converter.rb
CHANGED
data/lib/pnm.rb
CHANGED
@@ -29,7 +29,7 @@ require_relative 'pnm/converter'
|
|
29
29
|
# options = {:maxgray => 30, :comment => 'Test Image'}
|
30
30
|
#
|
31
31
|
# # create the image object
|
32
|
-
# image = PNM::Image.new(
|
32
|
+
# image = PNM::Image.new(pixels, options)
|
33
33
|
#
|
34
34
|
# # retrieve some image properties
|
35
35
|
# image.info # => "PGM 3x2 Grayscale"
|
@@ -56,6 +56,11 @@ require_relative 'pnm/converter'
|
|
56
56
|
# image.maxgray # => 30
|
57
57
|
# image.pixels # => [[0, 10, 20], [10, 20, 30]]
|
58
58
|
#
|
59
|
+
# Force an image type:
|
60
|
+
#
|
61
|
+
# image = PNM::Image.new([[0, 1],[1, 0]], :type => :ppm)
|
62
|
+
# image.info # => "PPM 2x2 Color"
|
63
|
+
#
|
59
64
|
# == See also
|
60
65
|
#
|
61
66
|
# Further information on the PNM library is available on the
|
@@ -139,10 +144,10 @@ module PNM
|
|
139
144
|
Converter.binary2array(type, width, height, content[:data])
|
140
145
|
end
|
141
146
|
|
142
|
-
options = {:maxgray => maxgray}
|
147
|
+
options = {:type => type, :maxgray => maxgray}
|
143
148
|
options[:comment] = content[:comments].join("\n") if content[:comments]
|
144
149
|
|
145
|
-
Image.new(
|
150
|
+
Image.new(pixels, options)
|
146
151
|
end
|
147
152
|
|
148
153
|
def self.magic_number # :nodoc:
|
data/lib/pnm/image.rb
CHANGED
@@ -12,7 +12,8 @@ module PNM
|
|
12
12
|
# The height of the image in pixels.
|
13
13
|
attr_reader :height
|
14
14
|
|
15
|
-
# The maximum gray or color value
|
15
|
+
# The maximum gray or color value (for PBM always set to 1).
|
16
|
+
# See ::new for details.
|
16
17
|
attr_reader :maxgray
|
17
18
|
|
18
19
|
# The pixel data, given as a two-dimensional array.
|
@@ -25,7 +26,6 @@ module PNM
|
|
25
26
|
# Creates an image from a two-dimensional array of bilevel,
|
26
27
|
# gray, or RGB values.
|
27
28
|
#
|
28
|
-
# +type+:: The type of the image (+:pbm+, +:pgm+, or +:ppm+).
|
29
29
|
# +pixels+:: The pixel data, given as a two-dimensional array of
|
30
30
|
#
|
31
31
|
# * for PBM: bilevel values (0 or 1),
|
@@ -33,26 +33,37 @@ module PNM
|
|
33
33
|
# * for PPM: an array of 3 values between 0 and +maxgray+,
|
34
34
|
# corresponding to red, green, and blue (RGB).
|
35
35
|
#
|
36
|
-
# PPM also accepts an array of gray values.
|
36
|
+
# PPM also accepts an array of bilevel or gray values.
|
37
37
|
#
|
38
38
|
# A value of 0 means that the color is turned off.
|
39
39
|
#
|
40
40
|
# Optional settings that can be specified in the +options+ hash:
|
41
41
|
#
|
42
|
+
# +type+:: The type of the image (+:pbm+, +:pgm+, or +:ppm+).
|
43
|
+
# By default, the type is guessed from the provided
|
44
|
+
# pixel data, unless this option is used.
|
42
45
|
# +maxgray+:: The maximum gray or color value.
|
43
46
|
# For PGM and PPM, +maxgray+ must be less or equal 255
|
44
|
-
# (the default value).
|
45
|
-
#
|
47
|
+
# (the default value).
|
48
|
+
# For PBM pixel data, setting +maxgray+ implies a conversion
|
49
|
+
# to +:pgm+. If +type+ is explicitly set to +:pbm+,
|
50
|
+
# the +maxgray+ setting is disregarded.
|
46
51
|
# +comment+:: A multiline comment string (default: empty string).
|
47
|
-
def initialize(
|
48
|
-
@type = type
|
52
|
+
def initialize(pixels, options = {})
|
53
|
+
@type = options[:type]
|
49
54
|
@width = pixels.first.size
|
50
55
|
@height = pixels.size
|
51
|
-
@maxgray = options[:maxgray]
|
56
|
+
@maxgray = options[:maxgray]
|
52
57
|
@comment = (options[:comment] || '').chomp
|
53
58
|
@pixels = pixels
|
54
59
|
|
55
|
-
@
|
60
|
+
@type ||= detect_type(@pixels, @maxgray)
|
61
|
+
|
62
|
+
if @type == :pbm
|
63
|
+
@maxgray = 1
|
64
|
+
else
|
65
|
+
@maxgray ||= 255
|
66
|
+
end
|
56
67
|
|
57
68
|
if type == :ppm && !pixels.first.first.kind_of?(Array)
|
58
69
|
@pixels = pixels.map {|row| row.map {|pixel| gray_to_rgb(pixel) } }
|
@@ -99,6 +110,16 @@ module PNM
|
|
99
110
|
end
|
100
111
|
end
|
101
112
|
|
113
|
+
def detect_type(pixels, maxgray) # :nodoc:
|
114
|
+
if pixels.first.first.kind_of?(Array)
|
115
|
+
:ppm
|
116
|
+
elsif pixels.flatten.max <= 1
|
117
|
+
maxgray ? :pgm : :pbm
|
118
|
+
else
|
119
|
+
:pgm
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
102
123
|
def header(encoding) # :nodoc:
|
103
124
|
header = "#{PNM.magic_number[type][encoding]}\n"
|
104
125
|
if comment
|
data/lib/pnm/version.rb
CHANGED
data/test/test_image.rb
CHANGED
@@ -20,7 +20,7 @@ describe PNM::Image do
|
|
20
20
|
[0,0,1,0,0],
|
21
21
|
[0,0,0,0,0]]
|
22
22
|
comment = 'Bilevel'
|
23
|
-
@bilevel = PNM::Image.new(
|
23
|
+
@bilevel = PNM::Image.new(pixels, {:comment => comment})
|
24
24
|
|
25
25
|
pixels = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
|
26
26
|
[0,1,1,1,0,1,1,1,1,0,1,1,1,1,1,0],
|
@@ -28,25 +28,77 @@ describe PNM::Image do
|
|
28
28
|
[0,0,1,0,0,1,0,0,1,0,1,0,1,0,1,0],
|
29
29
|
[0,0,1,0,0,1,1,1,1,0,1,0,1,0,1,0],
|
30
30
|
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
|
31
|
-
@bilevel_2 = PNM::Image.new(
|
31
|
+
@bilevel_2 = PNM::Image.new(pixels)
|
32
32
|
|
33
33
|
pixels = [[ 0, 50,100,150],
|
34
34
|
[ 50,100,150,200],
|
35
35
|
[100,150,200,250]]
|
36
36
|
comment = "Grayscale\n(with multiline comment)"
|
37
|
-
@grayscale = PNM::Image.new(
|
37
|
+
@grayscale = PNM::Image.new(pixels, {:maxgray => 250, :comment => comment})
|
38
38
|
|
39
39
|
pixels = [[65,66], [13,10], [65,66]]
|
40
|
-
@grayscale_crlf = PNM::Image.new(
|
40
|
+
@grayscale_crlf = PNM::Image.new(pixels)
|
41
41
|
|
42
42
|
pixels = [[[0,6,0], [1,5,1], [2,4,2], [3,3,4], [4,2,6]],
|
43
43
|
[[1,5,2], [2,4,2], [3,3,2], [4,2,2], [5,1,2]],
|
44
44
|
[[2,4,6], [3,3,4], [4,2,2], [5,1,1], [6,0,0]]]
|
45
|
-
@color = PNM::Image.new(
|
45
|
+
@color = PNM::Image.new(pixels, {:maxgray => 6})
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'sets maxgray to 1 for bilevel images' do
|
49
|
+
image = PNM::Image.new([[0,1,0], [1,0,1]])
|
50
|
+
image.type.must_equal :pbm
|
51
|
+
image.maxgray.must_equal 1
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'sets maxgray by default to 255 for grayscale images' do
|
55
|
+
image = PNM::Image.new([[0,10,20], [10,20,30]])
|
56
|
+
image.type.must_equal :pgm
|
57
|
+
image.maxgray.must_equal 255
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'sets maxgray by default to 255 for color images' do
|
61
|
+
image = PNM::Image.new([[[0,0,0], [10,10,10]], [[10,10,10], [20,20,20]]])
|
62
|
+
image.type.must_equal :ppm
|
63
|
+
image.maxgray.must_equal 255
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'ignores maxgray for bilevel images and sets it to 1' do
|
67
|
+
image = PNM::Image.new([[0,1,0], [1,0,1]], {:type => :pbm, :maxgray => 255})
|
68
|
+
image.type.must_equal :pbm
|
69
|
+
image.maxgray.must_equal 1
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'can create a grayscale image from bilevel values (using type)' do
|
73
|
+
image = PNM::Image.new([[0,1,0], [1,0,1]], {:type => :pgm})
|
74
|
+
image.type.must_equal :pgm
|
75
|
+
image.pixels.must_equal [[0,1,0], [1,0,1]]
|
76
|
+
image.maxgray.must_equal 255
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'can create a grayscale image from bilevel values (using maxgray)' do
|
80
|
+
image = PNM::Image.new([[0,1,0], [1,0,1]], {:maxgray => 1})
|
81
|
+
image.type.must_equal :pgm
|
82
|
+
image.pixels.must_equal [[0,1,0], [1,0,1]]
|
83
|
+
image.maxgray.must_equal 1
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'can create a color image from bilevel values' do
|
87
|
+
image = PNM::Image.new([[0,1,0], [1,0,1]], {:type => :ppm})
|
88
|
+
image.info.must_match %r{^PPM 3x2 Color}
|
89
|
+
image.pixels.must_equal [[[0,0,0], [1,1,1], [0,0,0]], [[1,1,1], [0,0,0], [1,1,1]]]
|
90
|
+
image.maxgray.must_equal 255
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'can create a color image from bilevel values with a given maxgray' do
|
94
|
+
image = PNM::Image.new([[0,1,0], [1,0,1]], {:type => :ppm, :maxgray => 2})
|
95
|
+
image.info.must_match %r{^PPM 3x2 Color}
|
96
|
+
image.pixels.must_equal [[[0,0,0], [1,1,1], [0,0,0]], [[1,1,1], [0,0,0], [1,1,1]]]
|
97
|
+
image.maxgray.must_equal 2
|
46
98
|
end
|
47
99
|
|
48
100
|
it 'can create a color image from gray values' do
|
49
|
-
image = PNM::Image.new(
|
101
|
+
image = PNM::Image.new([[0,3,6], [3,6,9]], {:type => :ppm})
|
50
102
|
image.info.must_match %r{^PPM 3x2 Color}
|
51
103
|
image.pixels.must_equal [[[0,0,0], [3,3,3], [6,6,6]], [[3,3,3], [6,6,6], [9,9,9]]]
|
52
104
|
end
|
@@ -105,7 +157,7 @@ describe PNM::Image do
|
|
105
157
|
File.delete(@temp_path)
|
106
158
|
end
|
107
159
|
|
108
|
-
it 'can write binary data containing CRLF
|
160
|
+
it 'can write binary data containing CRLF to an I/O stream' do
|
109
161
|
File.open(@temp_path, 'w') {|f| @grayscale_crlf.write(f, :binary) }
|
110
162
|
File.binread(@temp_path).must_equal File.binread("#{@srcpath}/grayscale_binary_crlf.pgm")
|
111
163
|
File.delete(@temp_path)
|
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.2.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: 2013-
|
11
|
+
date: 2013-12-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -77,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
77
|
version: '0'
|
78
78
|
requirements: []
|
79
79
|
rubyforge_project:
|
80
|
-
rubygems_version: 2.1.
|
80
|
+
rubygems_version: 2.1.11
|
81
81
|
signing_key:
|
82
82
|
specification_version: 4
|
83
83
|
summary: PNM - create/read/write PNM image files (PBM, PGM, PPM)
|