pnm 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b5d51775ecf00c43299084927b3ce8e0fb182127accb347f25806a840e7389a4
4
- data.tar.gz: 0ac3e779bb1af09853cf6429fdb0fe5ec1a6d5e951eaf0f227e97e7c81b9f053
3
+ metadata.gz: ef6f55635bbaa23976ceb49f1547b92cf78e2ad04d51363054ac12dbfe6943e0
4
+ data.tar.gz: 4b7c8730c80b28b6a98a4b27fc3a000a4e28eb11be34bf5c2717da188f833d9a
5
5
  SHA512:
6
- metadata.gz: aa8584a19eb7f6aad9513cc379a1700dfe9e1f5b9e58feaba2ae80e18c670050f9ba3fbd39f7528f3bad2bd59eb31ba491ae0f8673669454cbae92b4f996c416
7
- data.tar.gz: 87e0d5751baa549e9f2f14be763d6b77a40bde876c23f9ee44df5ef8b1ec642024091e6d06e32172aef09e33323bca2cf5f1945e8cecf7e7aa9945629f448811
6
+ metadata.gz: f3b07074daa3fd1601d8d0c4f05dc2764c1c2434b24368d0e69c4752ecc9af1153751f14f1af679d2319d46b20b74cff4c9e10e50341eb12fbb82abc97ca69d5
7
+ data.tar.gz: 5356021622e4656db38f8f7c7b918c5e3bbffc63670e4b39b851ca542f55f5a51b7748b2384540426d7e154ebb3b151c4d106cd1adee00c526d6e249753e54c6
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  PNM - A Ruby library for PNM image files (PBM, PGM, PPM)
2
2
  ========================================================
3
3
 
4
+ ![CI](https://github.com/stomar/pnm/actions/workflows/ci.yml/badge.svg)
5
+
4
6
  PNM is a pure [Ruby][Ruby] library for creating, reading,
5
7
  and writing of `PNM` image files (Portable Anymap):
6
8
 
@@ -96,11 +98,11 @@ Requirements
96
98
 
97
99
  - No additional Ruby gems or native libraries are needed.
98
100
 
99
- - PNM has been tested with
101
+ - PNM's current version has been tested with
100
102
 
101
- - Ruby 2.7
102
- - Ruby 2.6, 2.5, 2.4, 2.3, 2.2, 2.1, 2.0.0,
103
- - JRuby 9.2.13.0.
103
+ - Ruby 3.3, 3.2, 3.1, 3.0,
104
+ - Ruby 2.7, 2.6,
105
+ - JRuby 9.4.5.0.
104
106
 
105
107
  Documentation
106
108
  -------------
@@ -116,7 +118,7 @@ Report bugs on the PNM home page: <https://github.com/stomar/pnm/>
116
118
  License
117
119
  -------
118
120
 
119
- Copyright &copy; 2013-2020 Marcus Stollsteimer
121
+ Copyright &copy; 2013-2024 Marcus Stollsteimer
120
122
 
121
123
  `PNM` is free software: you can redistribute it and/or modify
122
124
  it under the terms of the GNU General Public License version 3 or later (GPLv3+),
data/Rakefile CHANGED
@@ -1,9 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # rakefile for the PNM library.
4
- #
5
- # Copyright (C) 2013-2020 Marcus Stollsteimer
6
-
7
3
  require "rake/testtask"
8
4
 
9
5
  require_relative "lib/pnm"
@@ -25,7 +21,7 @@ end
25
21
 
26
22
  desc "Run benchmarks"
27
23
  task :benchmark do
28
- Dir["benchmark/**/bm_*.rb"].each {|f| require_relative f }
24
+ Dir["benchmark/**/bm_*.rb"].sort.each {|f| require_relative f }
29
25
  end
30
26
 
31
27
 
@@ -1,10 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- # bm_converter.rb: Benchmarks for the PNM library.
5
- #
6
- # Copyright (C) 2013-2020 Marcus Stollsteimer
7
-
8
4
  require "benchmark"
9
5
  require_relative "../lib/pnm"
10
6
 
@@ -12,8 +8,8 @@ require_relative "../lib/pnm"
12
8
  class ConverterBenchmark
13
9
 
14
10
  def initialize
15
- @repetitions = ARGV[0].to_i if ARGV[0] =~ /[0-9]+/
16
- @repetitions ||= 10
11
+ @repetitions = ENV.fetch("BM_RUNS", "")
12
+ @repetitions = @repetitions.match?(/\A[0-9]+\z/) ? @repetitions.to_i : 10
17
13
 
18
14
  @srcpath = File.dirname(__FILE__)
19
15
 
data/lib/pnm/converter.rb CHANGED
@@ -11,7 +11,7 @@ module PNM
11
11
  def self.byte_width(type, width)
12
12
  case type
13
13
  when :pbm
14
- (width - 1) / 8 + 1
14
+ ((width - 1) / 8) + 1
15
15
  when :pgm
16
16
  width
17
17
  when :ppm
@@ -53,14 +53,14 @@ module PNM
53
53
  bytes_per_row = byte_width(type, width)
54
54
  expected_size = bytes_per_row * height
55
55
 
56
- data.slice!(-1) if data.size == expected_size + 1 && data[-1] =~ /[ \t\r\n]/
56
+ data.slice!(-1) if data.size == expected_size + 1 && data[-1].match?(/[ \t\r\n]/)
57
57
 
58
58
  assert_data_size(data.size, expected_size)
59
59
 
60
60
  pixel_matrix = case type
61
61
  when :pbm
62
62
  rows = data.scan(/.{#{bytes_per_row}}/m)
63
- rows.map {|row| row.unpack("B*").first[0, width].each_char.map(&:to_i) }
63
+ rows.map {|row| row.unpack1("B*")[0, width].each_char.map(&:to_i) }
64
64
  when :pgm
65
65
  data.each_byte.each_slice(bytes_per_row).to_a
66
66
  when :ppm
@@ -107,9 +107,9 @@ module PNM
107
107
 
108
108
  def self.convert_to_integers(data, type)
109
109
  values_as_string = if type == :pbm
110
- data.gsub(/[ \t\r\n]+/, "").split("")
110
+ data.gsub(/[ \t\r\n]+/, "").chars
111
111
  else
112
- data.gsub(/\A[ \t\r\n]+/, "").split(/[ \t\r\n]+/)
112
+ data.sub(/\A[ \t\r\n]+/, "").split(/[ \t\r\n]+/)
113
113
  end
114
114
 
115
115
  values_as_string.map {|value| Integer(value) }
data/lib/pnm/image.rb CHANGED
@@ -47,11 +47,7 @@ module PNM
47
47
  type ||= detect_type(pixels, maxgray)
48
48
 
49
49
  # except for type detection, the maxgray option must be ignored for PBM
50
- maxgray = if type == :pbm
51
- nil
52
- else
53
- maxgray
54
- end
50
+ maxgray = nil if type == :pbm
55
51
 
56
52
  image_class = case type
57
53
  when :pbm
@@ -98,9 +94,10 @@ module PNM
98
94
  #
99
95
  # Returns the number of bytes written.
100
96
  def write(file, add_extension: false, encoding: :binary)
101
- content = if encoding == :ascii
97
+ content = case encoding
98
+ when :ascii
102
99
  to_ascii
103
- elsif encoding == :binary
100
+ when :binary
104
101
  to_binary
105
102
  end
106
103
 
@@ -184,7 +181,7 @@ module PNM
184
181
 
185
182
  def self.assert_valid_maxgray(maxgray) # :nodoc:
186
183
  return unless maxgray
187
- return if maxgray.is_a?(Integer) && maxgray > 0 && maxgray <= 255
184
+ return if maxgray.is_a?(Integer) && maxgray.positive? && maxgray <= 255
188
185
 
189
186
  msg = "invalid maxgray value - #{maxgray.inspect}"
190
187
  raise PNM::ArgumentError, msg
@@ -205,7 +202,7 @@ module PNM
205
202
 
206
203
  type = type.to_sym if type.is_a?(String)
207
204
 
208
- unless [:pbm, :pgm, :ppm].include?(type)
205
+ unless %i[pbm pgm ppm].include?(type)
209
206
  msg = "invalid image type - #{type.inspect}"
210
207
  raise PNM::ArgumentError, msg
211
208
  end
@@ -261,7 +258,7 @@ module PNM
261
258
  return [""] if comment.empty?
262
259
 
263
260
  keep_trailing_null_fields = -1 # magic value for split limit
264
- comment.split(/\n/, keep_trailing_null_fields)
261
+ comment.split("\n", keep_trailing_null_fields)
265
262
  end
266
263
 
267
264
  def to_ascii # :nodoc:
@@ -289,7 +286,6 @@ module PNM
289
286
  end
290
287
  end
291
288
 
292
-
293
289
  # Class for +PBM+ images. See the Image class for documentation.
294
290
  class PBMImage < Image
295
291
 
@@ -320,7 +316,6 @@ module PNM
320
316
  end
321
317
  end
322
318
 
323
-
324
319
  # Class for +PGM+ images. See the Image class for documentation.
325
320
  class PGMImage < Image
326
321
 
@@ -351,7 +346,6 @@ module PNM
351
346
  end
352
347
  end
353
348
 
354
-
355
349
  # Class for +PPM+ images. See the Image class for documentation.
356
350
  class PPMImage < Image
357
351
 
data/lib/pnm/parser.rb CHANGED
@@ -29,7 +29,7 @@ module PNM
29
29
  token = next_token!(content)
30
30
 
31
31
  if token.start_with?("#")
32
- comments << token.gsub(/# */, "")
32
+ comments << strip_comment_prefix(token)
33
33
  else
34
34
  magic_number = token
35
35
  end
@@ -38,11 +38,11 @@ module PNM
38
38
  assert_valid_magic_number(magic_number)
39
39
 
40
40
  while tokens.size < token_number[magic_number]
41
- content.gsub!(/\A[ \t\r\n]+/, "")
41
+ content.sub!(/\A[ \t\r\n]+/, "")
42
42
  token = next_token!(content)
43
43
 
44
44
  if token.start_with?("#")
45
- comments << token.gsub(/# */, "")
45
+ comments << strip_comment_prefix(token)
46
46
  else
47
47
  tokens << token
48
48
  end
@@ -100,6 +100,10 @@ module PNM
100
100
  token
101
101
  end
102
102
 
103
+ def self.strip_comment_prefix(comment)
104
+ comment.sub(/\A# */, "")
105
+ end
106
+
103
107
  def self.assert_valid_magic_number(magic_number)
104
108
  return if %w[P1 P2 P3 P4 P5 P6].include?(magic_number)
105
109
 
@@ -108,21 +112,21 @@ module PNM
108
112
  end
109
113
 
110
114
  def self.assert_integer(value_string, value_name)
111
- return if value_string =~ /\A[0-9]+\Z/
115
+ return if value_string.match?(/\A[0-9]+\z/)
112
116
 
113
117
  msg = "#{value_name} must be an integer - `#{value_string}'"
114
118
  raise PNM::ParserError, msg
115
119
  end
116
120
 
117
121
  def self.assert_positive(value, name)
118
- return if value > 0
122
+ return if value.positive?
119
123
 
120
124
  msg = "#{name} must be greater than 0 - `#{value}'"
121
125
  raise PNM::ParserError, msg
122
126
  end
123
127
 
124
128
  def self.assert_in_maxgray_range(value)
125
- return if value > 0 && value <= 255
129
+ return if value.positive? && value <= 255
126
130
 
127
131
  msg = "invalid maxgray value - `#{value}'"
128
132
  raise PNM::ParserError, msg
data/lib/pnm/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PNM
4
- VERSION = "0.6.0"
5
- DATE = "2020-09-23"
4
+ VERSION = "0.7.0"
5
+ DATE = "2024-01-05"
6
6
  end
data/lib/pnm.rb CHANGED
@@ -87,7 +87,7 @@ require_relative "pnm/exceptions"
87
87
  #
88
88
  # == Author
89
89
  #
90
- # Copyright (C) 2013-2020 Marcus Stollsteimer
90
+ # Copyright (C) 2013-2024 Marcus Stollsteimer
91
91
  #
92
92
  # License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
93
93
  #
@@ -97,8 +97,8 @@ module PNM
97
97
  HOMEPAGE = "https://github.com/stomar/pnm"
98
98
  TAGLINE = "create/read/write PNM image files (PBM, PGM, PPM)"
99
99
 
100
- COPYRIGHT = <<-TEXT.gsub(/^ +/, "")
101
- Copyright (C) 2013-2020 Marcus Stollsteimer.
100
+ COPYRIGHT = <<~TEXT
101
+ Copyright (C) 2013-2024 Marcus Stollsteimer.
102
102
  License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
103
103
  This is free software: you are free to change and redistribute it.
104
104
  There is NO WARRANTY, to the extent permitted by law.
@@ -119,20 +119,7 @@ module PNM
119
119
 
120
120
  content = Parser.parse(raw_data)
121
121
 
122
- type, encoding = case content[:magic_number]
123
- when "P1"
124
- [:pbm, :ascii]
125
- when "P2"
126
- [:pgm, :ascii]
127
- when "P3"
128
- [:ppm, :ascii]
129
- when "P4"
130
- [:pbm, :binary]
131
- when "P5"
132
- [:pgm, :binary]
133
- when "P6"
134
- [:ppm, :binary]
135
- end
122
+ type, encoding = type_and_encoding[content[:magic_number]]
136
123
 
137
124
  width = content[:width]
138
125
  height = content[:height]
@@ -191,4 +178,16 @@ module PNM
191
178
  ppm: { ascii: "P3", binary: "P6" }
192
179
  }
193
180
  end
181
+
182
+ # @private
183
+ def self.type_and_encoding # :nodoc:
184
+ {
185
+ "P1" => %i[pbm ascii],
186
+ "P2" => %i[pgm ascii],
187
+ "P3" => %i[ppm ascii],
188
+ "P4" => %i[pbm binary],
189
+ "P5" => %i[pgm binary],
190
+ "P6" => %i[ppm binary]
191
+ }
192
+ end
194
193
  end
data/pnm.gemspec CHANGED
@@ -25,10 +25,7 @@ Gem::Specification.new do |s|
25
25
 
26
26
  s.license = "GPL-3.0"
27
27
 
28
- s.required_ruby_version = ">= 2.0.0"
29
-
30
- s.add_development_dependency "minitest", ">= 5.0"
31
- s.add_development_dependency "rake", ">= 10.0"
28
+ s.required_ruby_version = ">= 2.6.0"
32
29
 
33
30
  s.require_paths = ["lib"]
34
31
 
@@ -1,14 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # test_converter.rb: Unit tests for the PNM library.
4
- #
5
- # Copyright (C) 2013-2020 Marcus Stollsteimer
6
-
7
3
  require "minitest/autorun"
8
4
  require "pnm/converter"
9
5
 
10
- require_relative "backports"
11
-
12
6
 
13
7
  describe PNM::Converter do
14
8
 
@@ -136,9 +130,10 @@ describe PNM::Converter do
136
130
  it "accepts an additional whitespace character for binary encoded data" do
137
131
  width = @pbm14[:width]
138
132
  height = @pbm14[:height]
139
- data = @pbm14[:binary] + "\t"
133
+ data = @pbm14[:binary].dup << "\t"
140
134
  expected = @pbm14[:array]
141
135
 
136
+ refute_equal data, @pbm14[:binary]
142
137
  _(@converter.binary2array(:pbm, width, height, data)).must_equal expected
143
138
  end
144
139
 
@@ -1,15 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # test_exceptions.rb: Unit tests for the PNM library.
4
- #
5
- # Copyright (C) 2013-2020 Marcus Stollsteimer
6
-
7
3
  require "minitest/autorun"
8
4
  require "stringio"
9
5
  require "pnm"
10
6
 
11
- require_relative "backports"
12
-
13
7
 
14
8
  describe "PNM.create" do
15
9
 
data/test/test_image.rb CHANGED
@@ -1,14 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # test_image.rb: Unit tests for the PNM library.
4
- #
5
- # Copyright (C) 2013-2020 Marcus Stollsteimer
6
-
7
3
  require "minitest/autorun"
8
4
  require "pnm"
9
5
 
10
- require_relative "backports"
11
-
12
6
 
13
7
  describe PNM::Image do
14
8
 
data/test/test_parser.rb CHANGED
@@ -1,14 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # test_parser.rb: Unit tests for the PNM library.
4
- #
5
- # Copyright (C) 2013-2020 Marcus Stollsteimer
6
-
7
3
  require "minitest/autorun"
8
4
  require "pnm/parser"
9
5
 
10
- require_relative "backports"
11
-
12
6
 
13
7
  describe PNM::Parser do
14
8
 
@@ -17,7 +11,7 @@ describe PNM::Parser do
17
11
  end
18
12
 
19
13
  it "can parse ASCII encoded PBM data" do
20
- content = <<-PBM.chomp.gsub(/^ */, "")
14
+ content = <<~PBM.chomp
21
15
  P1 6 2
22
16
  0 1 0 0 1 1
23
17
  0 0 0 1 1 1
@@ -33,7 +27,7 @@ describe PNM::Parser do
33
27
  end
34
28
 
35
29
  it "can parse ASCII encoded PGM data" do
36
- content = <<-PGM.chomp.gsub(/^ */, "")
30
+ content = <<~PGM.chomp
37
31
  P2 4 2 100
38
32
  10 20 30 40
39
33
  50 60 70 80
@@ -98,7 +92,7 @@ describe PNM::Parser do
98
92
  end
99
93
 
100
94
  it "can parse comments" do
101
- content = <<-PBM.chomp.gsub(/^ */, "")
95
+ content = <<~PBM.chomp
102
96
  # Comment 1
103
97
  P1 # Comment 2
104
98
  6# Comment 3
@@ -119,4 +113,25 @@ describe PNM::Parser do
119
113
 
120
114
  _(@parser.parse(content)).must_equal expected
121
115
  end
116
+
117
+ it "can parse a comment containing a # character" do
118
+ content = <<~PBM.chomp
119
+ # Comment 1 with # character
120
+ P1
121
+ # Comment 2: '# foo' is a comment
122
+ 3
123
+ 2
124
+ 0 1 0
125
+ 1 0 1
126
+ PBM
127
+ expected = {
128
+ magic_number: "P1",
129
+ width: 3,
130
+ height: 2,
131
+ comments: ["Comment 1 with # character", "Comment 2: '# foo' is a comment"],
132
+ data: "0 1 0\n1 0 1"
133
+ }
134
+
135
+ _(@parser.parse(content)).must_equal expected
136
+ end
122
137
  end
data/test/test_pnm.rb CHANGED
@@ -1,14 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # test_pnm.rb: Unit tests for the PNM library.
4
- #
5
- # Copyright (C) 2013-2020 Marcus Stollsteimer
6
-
7
3
  require "minitest/autorun"
8
4
  require "pnm"
9
5
 
10
- require_relative "backports"
11
-
12
6
 
13
7
  describe PNM do
14
8
 
metadata CHANGED
@@ -1,43 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pnm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.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-09-23 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: minitest
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '5.0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '5.0'
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '10.0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '10.0'
11
+ date: 2024-01-05 00:00:00.000000000 Z
12
+ dependencies: []
41
13
  description: 'PNM is a pure Ruby library for creating, reading, and writing of PNM
42
14
  image files (Portable Anymap): PBM (Portable Bitmap), PGM (Portable Graymap), and
43
15
  PPM (Portable Pixmap).'
@@ -61,7 +33,6 @@ files:
61
33
  - lib/pnm/parser.rb
62
34
  - lib/pnm/version.rb
63
35
  - pnm.gemspec
64
- - test/backports.rb
65
36
  - test/bilevel_2_binary.pbm
66
37
  - test/bilevel_ascii.pbm
67
38
  - test/bilevel_binary.pbm
@@ -89,20 +60,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
60
  requirements:
90
61
  - - ">="
91
62
  - !ruby/object:Gem::Version
92
- version: 2.0.0
63
+ version: 2.6.0
93
64
  required_rubygems_version: !ruby/object:Gem::Requirement
94
65
  requirements:
95
66
  - - ">="
96
67
  - !ruby/object:Gem::Version
97
68
  version: '0'
98
69
  requirements: []
99
- rubygems_version: 3.1.2
70
+ rubygems_version: 3.5.3
100
71
  signing_key:
101
72
  specification_version: 4
102
73
  summary: PNM - create/read/write PNM image files (PBM, PGM, PPM)
103
74
  test_files:
104
- - test/test_exceptions.rb
105
75
  - test/test_converter.rb
106
- - test/test_pnm.rb
76
+ - test/test_exceptions.rb
107
77
  - test/test_image.rb
108
78
  - test/test_parser.rb
79
+ - test/test_pnm.rb
data/test/backports.rb DELETED
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module PNM
4
- module Backports # :nodoc:
5
- module Minitest # :nodoc:
6
-
7
- # Provides workaround for missing value wrappers in minitest < 5.6.0.
8
- def _(value = nil, &block)
9
- block || value
10
- end
11
- end
12
- end
13
- end
14
-
15
-
16
- if Gem.loaded_specs["minitest"].version < Gem::Version.new("5.6.0")
17
- warn "Using workaround for missing value wrappers in minitest < 5.6.0."
18
- include PNM::Backports::Minitest
19
- end