img2zpl 0.1.2 → 0.1.3

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: 7c8010c3b5133ed81279c44419a584d87ab6b54d1777efec3c9c7c52e556b23b
4
- data.tar.gz: 17200af08ebff21cac5f15e0d09c89dc765e3d0487379ffd69958582095be77d
3
+ metadata.gz: eaa8a3061a652dea3c0987eb08f4bd7004e78272f01c8c84880c570efceab840
4
+ data.tar.gz: 2c2b9d1e9da61cc041ab9e9f8445b71d1cacc20109969e85a0b1c31a2b50038a
5
5
  SHA512:
6
- metadata.gz: 7e0e621ec3a4dc11c2399d8808b02f750be6da3a9c2b67e9e909e186f2adc1d020e31690e51809b9e9d8740ee04bbeced89638b4b2eed361af654b98e25d8410
7
- data.tar.gz: 3cda926725cc62f06e62da712fc56db9c98a179b1c1480871212aaceee872d918f622c45f8c0ec17000bd873f16ec11cca7617480d4e8816a726de65ef50c3ac
6
+ metadata.gz: 51b29f5fcb695da020a5586cfbeffc79c577396ecd7a37502e78f9a0b26003c604124a64a25bed8cbbe484da61e985f87d03064e3a1b496f472b2888959fcefd
7
+ data.tar.gz: de921a9431a6da01cc9dedf859810293373cddc5ab534ee0b7f30027222fbc66479da959f7bfa4d4f8f4c237ce2cbd2dd68cc10dd940dd19971f957b4da28b93
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  * Your contribution here.
4
4
 
5
+ ### v0.1.3 (2019/10/16)
6
+
7
+ * Fixed module definition.
8
+
5
9
  ### v0.1.2 (2019/10/10)
6
10
 
7
11
  * [#3](https://github.com/mtking2/img2zpl/pull/3): Add ASCII compression to conversion - [@mtking2](https://github.com/mtking2).
data/CONTRIBUTING.md CHANGED
@@ -12,7 +12,7 @@ bundle install
12
12
  Then check out a working branch:
13
13
 
14
14
  ```
15
- git checkout <my-working-branch>
15
+ git checkout -b <my-working-branch>
16
16
  ```
17
17
 
18
18
  ### Write tests
@@ -30,14 +30,17 @@ Write your code to make your tests pass.
30
30
 
31
31
  Update the CHANGELOG with the description of your code changes and your name on the line after `"* Your contribution here"`.
32
32
 
33
- ### Push your changes and open a pull request
33
+ ### Commit and push your changes
34
34
 
35
- Push you changes to your working branch.
35
+ Commit and push your changes to your working branch.
36
36
 
37
37
  ```
38
+ git commit -am 'Add some feature'
38
39
  git push origin <my-working-branch>
39
40
  ```
40
41
 
42
+ ### Open a pull request
43
+
41
44
  Open a pull request against upstream master and your working branch. Give a brief description of what your PR does and explain what the code changes do.
42
45
 
43
46
  Thank you!
data/README.md CHANGED
@@ -24,7 +24,7 @@ gem install img2zpl
24
24
  require 'img2zpl'
25
25
 
26
26
  img = Img2Zpl::Image.open('foo.jpg')
27
- zpl = img.to_zpl #=> "^FO0,0^GFA, ... ^FS"
27
+ zpl = img.to_zpl #=> "^GFA, ... ^FS"
28
28
  ```
29
29
 
30
30
  ### Contributing
@@ -35,6 +35,6 @@ See [CONTRIBUTING.md](CONTRIBUTING.md)
35
35
 
36
36
  ### Copyright
37
37
 
38
- Copyright &copy; 2019, [Michael King](https://twitter.com/_mtking2) and [Contributors](CHANGELOG.md).
38
+ &copy; 2019, [Michael King](https://twitter.com/_mtking2) and [Contributors](CHANGELOG.md).
39
39
 
40
40
  MIT License, see [LICENSE](https://github.com/mtking2/img2zpl/blob/master/LICENSE)
data/lib/img2zpl/image.rb CHANGED
@@ -1,61 +1,63 @@
1
- class Img2Zpl::Image < MiniMagick::Image
2
-
3
- def to_zpl(black_threshold: 0.5, compress: true)
4
- bytes_per_row = (width % 8).positive? ? (width / 8) + 1 : (width / 8)
5
- byte_count = bytes_per_row * height
6
- data, line, previous_line, byte = '', '', '', ''
7
-
8
- get_pixels.each do |row|
9
- row.each_with_index do |column, i|
10
- r, g, b = column.map(&:to_i)
11
- byte << ((r + g + b) > (black_threshold * 765) ? '0' : '1')
12
- if (i % 8).zero?
13
- line << byte.to_i(2).to_s(16).upcase.rjust(2, '0')
14
- byte = ''
1
+ module Img2Zpl
2
+ class Image < MiniMagick::Image
3
+
4
+ def to_zpl(black_threshold: 0.5, compress: true)
5
+ bytes_per_row = (width % 8).positive? ? (width / 8) + 1 : (width / 8)
6
+ byte_count = bytes_per_row * height
7
+ data, line, previous_line, byte = '', '', '', ''
8
+
9
+ get_pixels.each do |row|
10
+ row.each_with_index do |column, i|
11
+ r, g, b = column.map(&:to_i)
12
+ byte << ((r + g + b) > (black_threshold * 765) ? '0' : '1')
13
+ if (i % 8).zero?
14
+ line << byte.to_i(2).to_s(16).upcase.rjust(2, '0')
15
+ byte = ''
16
+ end
17
+ end
18
+
19
+ data << if compress
20
+ (line == previous_line ? ':' : line.gsub(/0+$/, ',').gsub(/F+$/, '!'))
21
+ else
22
+ line
15
23
  end
16
- end
17
24
 
18
- data << if compress
19
- (line == previous_line ? ':' : line.gsub(/0+$/, ',').gsub(/F+$/, '!'))
20
- else
21
- line
25
+ previous_line = line
26
+ line = ''
22
27
  end
23
28
 
24
- previous_line = line
25
- line = ''
29
+ _compress(data) if compress
30
+ "^GFA,#{byte_count},#{byte_count},#{bytes_per_row},#{data}^FS"
26
31
  end
27
32
 
28
- _compress(data) if compress
29
- "^GFA,#{byte_count},#{byte_count},#{bytes_per_row},#{data}^FS"
30
- end
31
-
32
- private
33
+ private
33
34
 
34
- def _compression_map
35
- map = {}
36
- start = 'G'.ord
37
- 19.times { |i| map[i + 1] = (start+i).chr }
38
- start = 'g'.ord
39
- (20..400).step(20).each_with_index { |i, j| map[i] = (start+j).chr }
40
- map
41
- end
35
+ def _compression_map
36
+ map = {}
37
+ start = 'G'.ord
38
+ 19.times { |i| map[i + 1] = (start+i).chr }
39
+ start = 'g'.ord
40
+ (20..400).step(20).each_with_index { |i, j| map[i] = (start+j).chr }
41
+ map
42
+ end
42
43
 
43
- def _reduce(n)
44
- str = ''
45
- counts = _compression_map.keys.sort.reverse
46
- counts.each do |c|
47
- if c <= n
48
- str << (_compression_map[c])
49
- n -= c
44
+ def _reduce(n)
45
+ str = ''
46
+ counts = _compression_map.keys.sort.reverse
47
+ counts.each do |c|
48
+ if c <= n
49
+ str << (_compression_map[c])
50
+ n -= c
51
+ end
50
52
  end
53
+ str
51
54
  end
52
- str
53
- end
54
55
 
55
- def _compress(data)
56
- data.gsub!(/([\da-zA-Z])(\1+)/) do |m|
57
- "#{_reduce(m.length)}#{$1}"
56
+ def _compress(data)
57
+ data.gsub!(/([\da-zA-Z])(\1+)/) do |m|
58
+ "#{_reduce(m.length)}#{$1}"
59
+ end
58
60
  end
59
- end
60
61
 
62
+ end
61
63
  end
@@ -1,3 +1,3 @@
1
1
  module Img2Zpl
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: img2zpl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael King
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-11 00:00:00.000000000 Z
11
+ date: 2019-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mini_magick