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 +4 -4
- data/CHANGELOG.md +4 -0
- data/CONTRIBUTING.md +6 -3
- data/README.md +2 -2
- data/lib/img2zpl/image.rb +49 -47
- data/lib/img2zpl/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eaa8a3061a652dea3c0987eb08f4bd7004e78272f01c8c84880c570efceab840
|
4
|
+
data.tar.gz: 2c2b9d1e9da61cc041ab9e9f8445b71d1cacc20109969e85a0b1c31a2b50038a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 51b29f5fcb695da020a5586cfbeffc79c577396ecd7a37502e78f9a0b26003c604124a64a25bed8cbbe484da61e985f87d03064e3a1b496f472b2888959fcefd
|
7
|
+
data.tar.gz: de921a9431a6da01cc9dedf859810293373cddc5ab534ee0b7f30027222fbc66479da959f7bfa4d4f8f4c237ce2cbd2dd68cc10dd940dd19971f957b4da28b93
|
data/CHANGELOG.md
CHANGED
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
|
-
###
|
33
|
+
### Commit and push your changes
|
34
34
|
|
35
|
-
|
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 #=> "^
|
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
|
-
|
38
|
+
© 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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
-
|
19
|
-
|
20
|
-
else
|
21
|
-
line
|
25
|
+
previous_line = line
|
26
|
+
line = ''
|
22
27
|
end
|
23
28
|
|
24
|
-
|
25
|
-
|
29
|
+
_compress(data) if compress
|
30
|
+
"^GFA,#{byte_count},#{byte_count},#{bytes_per_row},#{data}^FS"
|
26
31
|
end
|
27
32
|
|
28
|
-
|
29
|
-
"^GFA,#{byte_count},#{byte_count},#{bytes_per_row},#{data}^FS"
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
+
private
|
33
34
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
-
|
56
|
-
|
57
|
-
|
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
|
data/lib/img2zpl/version.rb
CHANGED
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.
|
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
|
+
date: 2019-10-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mini_magick
|