pnm 0.5.3 → 0.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5a3dd7ccd09f9d5a2f16212910e8aba16f25c78648e965c42847a86bc057632e
4
- data.tar.gz: 7f22c53142d2317692fa2e840a6b12758e39990123c1d297d9624abc26a71c4b
3
+ metadata.gz: b5d51775ecf00c43299084927b3ce8e0fb182127accb347f25806a840e7389a4
4
+ data.tar.gz: 0ac3e779bb1af09853cf6429fdb0fe5ec1a6d5e951eaf0f227e97e7c81b9f053
5
5
  SHA512:
6
- metadata.gz: c7ca3afc52aac295f564ecb5222c1339ce9ae623db0213ee7c878d05591396d735a76e0a19d62acce0cb095802352ebd1f1442ea4a803c32aa2bc4d8bf169289
7
- data.tar.gz: '08e3653dd113aabd4333c789a4077b95733a18f3422db4e6006e7f534692f365f35474791dfb6d8c66716bb5c6e16ced1f07e2fbf7b36e6ba2842a09911c00b1'
6
+ metadata.gz: aa8584a19eb7f6aad9513cc379a1700dfe9e1f5b9e58feaba2ae80e18c670050f9ba3fbd39f7528f3bad2bd59eb31ba491ae0f8673669454cbae92b4f996c416
7
+ data.tar.gz: 87e0d5751baa549e9f2f14be763d6b77a40bde876c23f9ee44df5ef8b1ec642024091e6d06e32172aef09e33323bca2cf5f1945e8cecf7e7aa9945629f448811
data/README.md CHANGED
@@ -26,11 +26,11 @@ require "pnm"
26
26
  pixels = [[ 0, 10, 20],
27
27
  [10, 20, 30]]
28
28
 
29
- # optional settings
30
- options = { maxgray: 30, comment: "Test Image" }
31
-
32
29
  # create the image object
33
- image = PNM.create(pixels, options)
30
+ image = PNM.create(pixels)
31
+
32
+ # create the image with additional optional settings
33
+ image = PNM.create(pixels, maxgray: 30, comment: "Test Image")
34
34
 
35
35
  # retrieve some image properties
36
36
  image.info # => "PGM 3x2 Grayscale"
@@ -50,10 +50,10 @@ Write an image to a file:
50
50
 
51
51
  ``` ruby
52
52
  image.write("test.pgm")
53
- image.write_with_extension("test") # adds the correct file extension
53
+ image.write("test", add_extension: true) # adds the appropriate file extension
54
54
 
55
- # use ASCII or "plain" format (default is binary)
56
- image.write("test.pgm", :ascii)
55
+ # use ASCII or "plain" format (default is :binary)
56
+ image.write("test.pgm", encoding: :ascii)
57
57
 
58
58
  # write to an I/O stream
59
59
  File.open("test.pgm", "w") {|f| image.write(f) }
@@ -99,9 +99,8 @@ Requirements
99
99
  - PNM has been tested with
100
100
 
101
101
  - Ruby 2.7
102
- - Ruby 2.6, 2.5, 2.4, 2.3, 2.2, 2.1, 2.0.0, 1.9.3,
103
- - JRuby 9.2.9.0,
104
- - Rubinius 2.5.2.
102
+ - Ruby 2.6, 2.5, 2.4, 2.3, 2.2, 2.1, 2.0.0,
103
+ - JRuby 9.2.13.0.
105
104
 
106
105
  Documentation
107
106
  -------------
data/lib/pnm.rb CHANGED
@@ -34,11 +34,11 @@ require_relative "pnm/exceptions"
34
34
  # pixels = [[ 0, 10, 20],
35
35
  # [10, 20, 30]]
36
36
  #
37
- # # optional settings
38
- # options = { maxgray: 30, comment: "Test Image" }
39
- #
40
37
  # # create the image object
41
- # image = PNM.create(pixels, options)
38
+ # image = PNM.create(pixels)
39
+ #
40
+ # # create the image with additional optional settings
41
+ # image = PNM.create(pixels, maxgray: 30, comment: "Test Image")
42
42
  #
43
43
  # # retrieve some image properties
44
44
  # image.info # => "PGM 3x2 Grayscale"
@@ -56,10 +56,10 @@ require_relative "pnm/exceptions"
56
56
  # Write an image to a file:
57
57
  #
58
58
  # image.write("test.pgm")
59
- # image.write_with_extension("test") # adds the correct file extension
59
+ # image.write("test", add_extension: true) # adds the appropriate file extension
60
60
  #
61
- # # use ASCII or "plain" format (default is binary)
62
- # image.write("test.pgm", :ascii)
61
+ # # use ASCII or "plain" format (default is :binary)
62
+ # image.write("test.pgm", encoding: :ascii)
63
63
  #
64
64
  # # write to an I/O stream
65
65
  # File.open("test.pgm", "w") {|f| image.write(f) }
@@ -143,10 +143,9 @@ module PNM
143
143
  Converter.binary2array(type, width, height, content[:data])
144
144
  end
145
145
 
146
- options = { type: type, maxgray: maxgray }
147
- options[:comment] = content[:comments].join("\n") if content[:comments]
146
+ comment = content[:comments].join("\n") if content[:comments]
148
147
 
149
- create(pixels, options)
148
+ create(pixels, type: type, maxgray: maxgray, comment: comment)
150
149
  end
151
150
 
152
151
  # Creates an image from a two-dimensional array of bilevel,
@@ -162,7 +161,7 @@ module PNM
162
161
  # corresponding to red, green, and blue (RGB);
163
162
  # a value of 0 means that the color is turned off.
164
163
  #
165
- # Optional settings that can be specified in the +options+ hash:
164
+ # Optional settings:
166
165
  #
167
166
  # +type+:: The type of the image (+:pbm+, +:pgm+, or +:ppm+).
168
167
  # By explicitly setting +type+, PGM images can be
@@ -180,8 +179,8 @@ module PNM
180
179
  # +comment+:: A multiline comment string.
181
180
  #
182
181
  # Returns a PNM::Image object.
183
- def self.create(pixels, options = {})
184
- Image.create(pixels, options)
182
+ def self.create(pixels, type: nil, maxgray: nil, comment: nil)
183
+ Image.create(pixels, type: type, maxgray: maxgray, comment: comment)
185
184
  end
186
185
 
187
186
  # @private
@@ -38,19 +38,19 @@ module PNM
38
38
  # This method should be called as PNM.create.
39
39
  # See there for a description of pixel data formats
40
40
  # and available options.
41
- def self.create(pixels, options = {})
41
+ def self.create(pixels, type: nil, maxgray: nil, comment: nil)
42
42
  assert_valid_array(pixels)
43
- assert_valid_maxgray(options[:maxgray])
44
- assert_valid_comment(options[:comment])
43
+ assert_valid_maxgray(maxgray)
44
+ assert_valid_comment(comment)
45
45
 
46
- type = sanitize_and_assert_valid_type(options[:type])
47
- type ||= detect_type(pixels, options[:maxgray])
46
+ type = sanitize_and_assert_valid_type(type)
47
+ type ||= detect_type(pixels, maxgray)
48
48
 
49
49
  # except for type detection, the maxgray option must be ignored for PBM
50
50
  maxgray = if type == :pbm
51
51
  nil
52
52
  else
53
- options[:maxgray]
53
+ maxgray
54
54
  end
55
55
 
56
56
  image_class = case type
@@ -62,7 +62,7 @@ module PNM
62
62
  PPMImage
63
63
  end
64
64
 
65
- image_class.new(pixels, maxgray, options[:comment])
65
+ image_class.new(pixels, maxgray, comment)
66
66
  end
67
67
 
68
68
  class << self
@@ -87,12 +87,17 @@ module PNM
87
87
  @comment.freeze
88
88
  end
89
89
 
90
- # Writes the image to +file+ (a filename or an IO object),
91
- # using the specified encoding.
92
- # Valid encodings are +:binary+ (default) and +:ascii+.
90
+ # Writes the image to +file+ (a filename or an IO object).
91
+ #
92
+ # When +add_extension+ is set to +true+ (default: +false+)
93
+ # the appropriate file extension is added to the provided filename
94
+ # (+.pbm+, +.pgm+, or +.ppm+).
95
+ #
96
+ # The encoding can be set using the +encoding+ keyword argument,
97
+ # valid options are +:binary+ (default) and +:ascii+.
93
98
  #
94
99
  # Returns the number of bytes written.
95
- def write(file, encoding = :binary)
100
+ def write(file, add_extension: false, encoding: :binary)
96
101
  content = if encoding == :ascii
97
102
  to_ascii
98
103
  elsif encoding == :binary
@@ -100,22 +105,14 @@ module PNM
100
105
  end
101
106
 
102
107
  if file.is_a?(String)
103
- File.binwrite(file, content)
108
+ filename = add_extension ? "#{file}.#{type}" : file
109
+ File.binwrite(filename, content)
104
110
  else
105
111
  file.binmode
106
112
  file.write content
107
113
  end
108
114
  end
109
115
 
110
- # Adds the appropriate file extension to +basename+
111
- # (+.pbm+, +.pgm+, or +.ppm+)
112
- # and writes the image to the resulting filename.
113
- #
114
- # Any options are passed on to #write, which is used internally.
115
- def write_with_extension(basename, *args)
116
- write("#{basename}.#{type}", *args)
117
- end
118
-
119
116
  # Returns a string with a short image format description.
120
117
  def info
121
118
  "#{type.to_s.upcase} #{width}x#{height} #{type_string}"
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PNM
4
- VERSION = "0.5.3"
5
- DATE = "2020-01-19"
4
+ VERSION = "0.6.0"
5
+ DATE = "2020-09-23"
6
6
  end
@@ -25,7 +25,7 @@ Gem::Specification.new do |s|
25
25
 
26
26
  s.license = "GPL-3.0"
27
27
 
28
- s.required_ruby_version = ">= 1.9.3"
28
+ s.required_ruby_version = ">= 2.0.0"
29
29
 
30
30
  s.add_development_dependency "minitest", ">= 5.0"
31
31
  s.add_development_dependency "rake", ">= 10.0"
@@ -132,61 +132,61 @@ describe PNM::Image do
132
132
  end
133
133
 
134
134
  it "can write a bilevel image to an ASCII encoded file" do
135
- @bilevel.write(@temp_path, :ascii)
135
+ @bilevel.write(@temp_path, encoding: :ascii)
136
136
  _(File.binread(@temp_path)).must_equal File.binread("#{@srcpath}/bilevel_ascii.pbm")
137
137
  File.delete(@temp_path)
138
138
  end
139
139
 
140
140
  it "can write a bilevel image (width 5) to a binary encoded file" do
141
- @bilevel.write(@temp_path, :binary)
141
+ @bilevel.write(@temp_path, encoding: :binary)
142
142
  _(File.binread(@temp_path)).must_equal File.binread("#{@srcpath}/bilevel_binary.pbm")
143
143
  File.delete(@temp_path)
144
144
  end
145
145
 
146
146
  it "can write a bilevel image (width 16) to a binary encoded file" do
147
- @bilevel2.write(@temp_path, :binary)
147
+ @bilevel2.write(@temp_path, encoding: :binary)
148
148
  _(File.binread(@temp_path)).must_equal File.binread("#{@srcpath}/bilevel_2_binary.pbm")
149
149
  File.delete(@temp_path)
150
150
  end
151
151
 
152
152
  it "can write a grayscale image to an ASCII encoded file" do
153
- @grayscale.write(@temp_path, :ascii)
153
+ @grayscale.write(@temp_path, encoding: :ascii)
154
154
  _(File.binread(@temp_path)).must_equal File.binread("#{@srcpath}/grayscale_ascii.pgm")
155
155
  File.delete(@temp_path)
156
156
  end
157
157
 
158
158
  it "can write a grayscale image to a binary encoded file" do
159
- @grayscale.write(@temp_path, :binary)
159
+ @grayscale.write(@temp_path, encoding: :binary)
160
160
  _(File.binread(@temp_path)).must_equal File.binread("#{@srcpath}/grayscale_binary.pgm")
161
161
  File.delete(@temp_path)
162
162
  end
163
163
 
164
164
  it "can write a color image to an ASCII encoded file" do
165
- @color.write(@temp_path, :ascii)
165
+ @color.write(@temp_path, encoding: :ascii)
166
166
  _(File.binread(@temp_path)).must_equal File.binread("#{@srcpath}/color_ascii.ppm")
167
167
  File.delete(@temp_path)
168
168
  end
169
169
 
170
170
  it "can write a color image to a binary encoded file" do
171
- @color.write(@temp_path, :binary)
171
+ @color.write(@temp_path, encoding: :binary)
172
172
  _(File.binread(@temp_path)).must_equal File.binread("#{@srcpath}/color_binary.ppm")
173
173
  File.delete(@temp_path)
174
174
  end
175
175
 
176
176
  it "can write a bilevel image to a file, adding the extension" do
177
- @bilevel.write_with_extension(@temp_path)
177
+ @bilevel.write(@temp_path, add_extension: true)
178
178
  _(File.binread("#{@temp_path}.pbm")).must_equal File.binread("#{@srcpath}/bilevel_binary.pbm")
179
179
  File.delete("#{@temp_path}.pbm")
180
180
  end
181
181
 
182
182
  it "can write a grayscale image to a file, adding the extension" do
183
- @grayscale.write_with_extension(@temp_path, :ascii)
183
+ @grayscale.write(@temp_path, add_extension: true, encoding: :ascii)
184
184
  _(File.binread("#{@temp_path}.pgm")).must_equal File.binread("#{@srcpath}/grayscale_ascii.pgm")
185
185
  File.delete("#{@temp_path}.pgm")
186
186
  end
187
187
 
188
188
  it "can write a color image to a file, adding the extension" do
189
- @color.write_with_extension(@temp_path, :binary)
189
+ @color.write(@temp_path, add_extension: true, encoding: :binary)
190
190
  _(File.binread("#{@temp_path}.ppm")).must_equal File.binread("#{@srcpath}/color_binary.ppm")
191
191
  File.delete("#{@temp_path}.ppm")
192
192
  end
@@ -204,27 +204,27 @@ describe PNM::Image do
204
204
  end
205
205
 
206
206
  it "can write binary data containing CRLF" do
207
- @grayscale_crlf.write(@temp_path, :binary)
207
+ @grayscale_crlf.write(@temp_path, encoding: :binary)
208
208
  _(File.binread(@temp_path)).must_equal File.binread("#{@srcpath}/grayscale_binary_crlf.pgm")
209
209
  File.delete(@temp_path)
210
210
  end
211
211
 
212
212
  it "can write binary data containing CRLF to an I/O stream" do
213
- File.open(@temp_path, "w") {|f| @grayscale_crlf.write(f, :binary) }
213
+ File.open(@temp_path, "w") {|f| @grayscale_crlf.write(f, encoding: :binary) }
214
214
  _(File.binread(@temp_path)).must_equal File.binread("#{@srcpath}/grayscale_binary_crlf.pgm")
215
215
  File.delete(@temp_path)
216
216
  end
217
217
 
218
218
  it "can write zero-length comments" do
219
219
  comment = ""
220
- PNM.create([[0, 0]], comment: comment).write(@temp_path, :ascii)
220
+ PNM.create([[0, 0]], comment: comment).write(@temp_path, encoding: :ascii)
221
221
  _(File.binread(@temp_path)).must_equal "P1\n#\n2 1\n0 0\n"
222
222
  File.delete(@temp_path)
223
223
  end
224
224
 
225
225
  it "can write comments with trailing zero-length line" do
226
226
  comment = "An empty line:\n"
227
- PNM.create([[0, 0]], comment: comment).write(@temp_path, :ascii)
227
+ PNM.create([[0, 0]], comment: comment).write(@temp_path, encoding: :ascii)
228
228
  _(File.binread(@temp_path)).must_equal "P1\n# An empty line:\n#\n2 1\n0 0\n"
229
229
  File.delete(@temp_path)
230
230
  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.5.3
4
+ version: 0.6.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: 2020-01-19 00:00:00.000000000 Z
11
+ date: 2020-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -89,7 +89,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
89
  requirements:
90
90
  - - ">="
91
91
  - !ruby/object:Gem::Version
92
- version: 1.9.3
92
+ version: 2.0.0
93
93
  required_rubygems_version: !ruby/object:Gem::Requirement
94
94
  requirements:
95
95
  - - ">="