img2zpl 0.1.1 → 0.1.2

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: d438648db69b9d6a1503df5cb6169c9a21740ce15c16c032387d4e8663a65e45
4
- data.tar.gz: 0f9453b3645f0fd5f284786b22c2b75108a987c9741b8dd232c602c16760c238
3
+ metadata.gz: 7c8010c3b5133ed81279c44419a584d87ab6b54d1777efec3c9c7c52e556b23b
4
+ data.tar.gz: 17200af08ebff21cac5f15e0d09c89dc765e3d0487379ffd69958582095be77d
5
5
  SHA512:
6
- metadata.gz: 0372b42254b75b51cdad5a45ebf9e7018f26701c94028f0b43b63f3299b4cfd30f96861aa72ed80dd8e4f7c6e506477b7f24e29c8b06a31ff863d01b61f4d6d7
7
- data.tar.gz: 34201fe23f15a11548f28aa9189868a9997310c7e5d21d8125d12e49dfc7faf248ab8944463c097e3a38709074087c7d181de2a361e37f608d281bec6f579442
6
+ metadata.gz: 7e0e621ec3a4dc11c2399d8808b02f750be6da3a9c2b67e9e909e186f2adc1d020e31690e51809b9e9d8740ee04bbeced89638b4b2eed361af654b98e25d8410
7
+ data.tar.gz: 3cda926725cc62f06e62da712fc56db9c98a179b1c1480871212aaceee872d918f622c45f8c0ec17000bd873f16ec11cca7617480d4e8816a726de65ef50c3ac
@@ -0,0 +1,15 @@
1
+ ### v0.x.x (next)
2
+
3
+ * Your contribution here.
4
+
5
+ ### v0.1.2 (2019/10/10)
6
+
7
+ * [#3](https://github.com/mtking2/img2zpl/pull/3): Add ASCII compression to conversion - [@mtking2](https://github.com/mtking2).
8
+
9
+ ### v0.1.1 (2019/10/09)
10
+
11
+ * [#2](https://github.com/mtking2/img2zpl/pull/2): Rename primary conversion method `.zpl` --> `.to_zpl` - [@mtking2](https://github.com/mtking2).
12
+
13
+ ### v0.1.0 (2019/10/08)
14
+ * [#1](https://github.com/mtking2/img2zpl/pull/1): Add functioning algorithm - [@mtking2](https://github.com/mtking2).
15
+ * Initial public release - [@mtking2](https://github.com/mtking2).
@@ -0,0 +1,43 @@
1
+ ## Contributing
2
+
3
+ ### Fork & clone the repository
4
+
5
+ ```
6
+ git clone git@github.com:<your-username>/img2zpl.git
7
+ cd img2zpl
8
+ git remote add upstream git@github.com:mtking2/img2zpl.git
9
+ bundle install
10
+ ```
11
+
12
+ Then check out a working branch:
13
+
14
+ ```
15
+ git checkout <my-working-branch>
16
+ ```
17
+
18
+ ### Write tests
19
+
20
+ This project uses `rspec`. After writing your tests, you can run tests with the following command:
21
+
22
+ `bundle exec rspec`
23
+
24
+
25
+ ### Write code
26
+
27
+ Write your code to make your tests pass.
28
+
29
+ ### Update the CHANGELOG with a description and your name
30
+
31
+ Update the CHANGELOG with the description of your code changes and your name on the line after `"* Your contribution here"`.
32
+
33
+ ### Push your changes and open a pull request
34
+
35
+ Push you changes to your working branch.
36
+
37
+ ```
38
+ git push origin <my-working-branch>
39
+ ```
40
+
41
+ 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
+
43
+ Thank you!
data/Gemfile CHANGED
@@ -2,9 +2,8 @@ source 'https://rubygems.org'
2
2
 
3
3
  group :development, :test do
4
4
  gem 'byebug'
5
-
5
+ gem 'pry'
6
+ gem 'pry-nav'
6
7
  end
7
8
 
8
- gem 'pry'
9
-
10
9
  gemspec
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2019 Michael
3
+ Copyright (c) 2019 Michael King
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -5,9 +5,36 @@
5
5
 
6
6
  Ruby library to convert images to usable &amp; printable ZPL code
7
7
 
8
+ ### Installation
9
+
10
+ Add the gem to your Gemfile:
11
+ ```
12
+ gem 'img2zpl'
13
+ ```
14
+ And then run `bundle install`
15
+
16
+ Or install it yourself with:
17
+ ```
18
+ gem install img2zpl
19
+ ```
20
+
8
21
  ### Usage
9
22
 
10
23
  ```ruby
24
+ require 'img2zpl'
25
+
11
26
  img = Img2Zpl::Image.open('foo.jpg')
12
27
  zpl = img.to_zpl #=> "^FO0,0^GFA, ... ^FS"
13
28
  ```
29
+
30
+ ### Contributing
31
+
32
+ See [CONTRIBUTING.md](CONTRIBUTING.md)
33
+
34
+ #### [Changelog](CHANGELOG.md)
35
+
36
+ ### Copyright
37
+
38
+ Copyright &copy; 2019, [Michael King](https://twitter.com/_mtking2) and [Contributors](CHANGELOG.md).
39
+
40
+ MIT License, see [LICENSE](https://github.com/mtking2/img2zpl/blob/master/LICENSE)
@@ -1,12 +1,11 @@
1
1
  class Img2Zpl::Image < MiniMagick::Image
2
2
 
3
- def to_zpl(black_threshold: 0.5)
3
+ def to_zpl(black_threshold: 0.5, compress: true)
4
4
  bytes_per_row = (width % 8).positive? ? (width / 8) + 1 : (width / 8)
5
5
  byte_count = bytes_per_row * height
6
6
  data, line, previous_line, byte = '', '', '', ''
7
7
 
8
8
  get_pixels.each do |row|
9
-
10
9
  row.each_with_index do |column, i|
11
10
  r, g, b = column.map(&:to_i)
12
11
  byte << ((r + g + b) > (black_threshold * 765) ? '0' : '1')
@@ -15,18 +14,48 @@ class Img2Zpl::Image < MiniMagick::Image
15
14
  byte = ''
16
15
  end
17
16
  end
18
- data << (line == previous_line ? ':' : line.gsub(/0+$/, ',').gsub(/F+$/, '!'))
17
+
18
+ data << if compress
19
+ (line == previous_line ? ':' : line.gsub(/0+$/, ',').gsub(/F+$/, '!'))
20
+ else
21
+ line
22
+ end
23
+
19
24
  previous_line = line
20
25
  line = ''
21
26
  end
22
27
 
23
- "^FO0,0^GFA,#{byte_count},#{byte_count},#{bytes_per_row},#{data}^FS"
28
+ _compress(data) if compress
29
+ "^GFA,#{byte_count},#{byte_count},#{bytes_per_row},#{data}^FS"
24
30
  end
25
31
 
26
32
  private
27
33
 
28
34
  def _compression_map
29
- # TODO: finish 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
42
+
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
50
+ end
51
+ end
52
+ str
53
+ end
54
+
55
+ def _compress(data)
56
+ data.gsub!(/([\da-zA-Z])(\1+)/) do |m|
57
+ "#{_reduce(m.length)}#{$1}"
58
+ end
30
59
  end
31
60
 
32
61
  end
@@ -1,3 +1,3 @@
1
1
  module Img2Zpl
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
@@ -4,13 +4,68 @@ describe Img2Zpl::Image do
4
4
 
5
5
  subject { described_class.open('spec/fixtures/default.jpg') }
6
6
 
7
- it 'inherits from the MiniMagick::Image class' do
7
+ it 'should inherit from the MiniMagick::Image class' do
8
8
  expect(described_class.respond_to?(:open)).to be true
9
9
  expect(described_class.respond_to?(:read)).to be true
10
10
  expect(subject.respond_to?(:to_zpl)).to be true
11
11
  end
12
12
 
13
- it 'zpl method returns a string' do
13
+ it 'should have the compression map' do
14
+ map = subject.send(:_compression_map)
15
+ expect(map[1]).to eq 'G'
16
+ expect(map[2]).to eq 'H'
17
+ expect(map[3]).to eq 'I'
18
+ expect(map[4]).to eq 'J'
19
+ expect(map[5]).to eq 'K'
20
+ expect(map[6]).to eq 'L'
21
+ expect(map[7]).to eq 'M'
22
+ expect(map[8]).to eq 'N'
23
+ expect(map[9]).to eq 'O'
24
+ expect(map[10]).to eq 'P'
25
+ expect(map[11]).to eq 'Q'
26
+ expect(map[12]).to eq 'R'
27
+ expect(map[13]).to eq 'S'
28
+ expect(map[14]).to eq 'T'
29
+ expect(map[15]).to eq 'U'
30
+ expect(map[16]).to eq 'V'
31
+ expect(map[17]).to eq 'W'
32
+ expect(map[18]).to eq 'X'
33
+ expect(map[19]).to eq 'Y'
34
+ expect(map[20]).to eq 'g'
35
+ expect(map[40]).to eq 'h'
36
+ expect(map[60]).to eq 'i'
37
+ expect(map[80]).to eq 'j'
38
+ expect(map[100]).to eq 'k'
39
+ expect(map[120]).to eq 'l'
40
+ expect(map[140]).to eq 'm'
41
+ expect(map[160]).to eq 'n'
42
+ expect(map[180]).to eq 'o'
43
+ expect(map[200]).to eq 'p'
44
+ expect(map[220]).to eq 'q'
45
+ expect(map[240]).to eq 'r'
46
+ expect(map[260]).to eq 's'
47
+ expect(map[280]).to eq 't'
48
+ expect(map[300]).to eq 'u'
49
+ expect(map[320]).to eq 'v'
50
+ expect(map[340]).to eq 'w'
51
+ expect(map[360]).to eq 'x'
52
+ expect(map[380]).to eq 'y'
53
+ expect(map[400]).to eq 'z'
54
+ end
55
+
56
+ it 'should properly compress ASCII data' do
57
+ d1 = '00000000000000000000000000000000000000000000000000000000008'
58
+ d2 = '5555555555555ADDDDDDDDDDDDDDDDDDDDDDD'
59
+ d3 = '00000000FFFFFFFFFFFFFFFFFFFFFFC0000000000000000000007FFFFFFFFF'
60
+ subject.send(:_compress, d1)
61
+ subject.send(:_compress, d2)
62
+ subject.send(:_compress, d3)
63
+ expect(d1).to eq 'hX08'
64
+ expect(d2).to eq 'S5AgID'
65
+ expect(d3).to eq 'N0gHFCgG07OF'
66
+ end
67
+
68
+ it 'should return a string when calling .to_zpl' do
14
69
  expect(subject.to_zpl).to be_kind_of String
15
70
  end
16
71
 
@@ -14,6 +14,10 @@
14
14
  #
15
15
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
16
16
  require 'img2zpl'
17
+ require 'byebug'
18
+ require 'pry'
19
+ require 'pry-nav'
20
+
17
21
 
18
22
  RSpec.configure do |config|
19
23
  # rspec-expectations config goes here. You can use an alternate
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.1
4
+ version: 0.1.2
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-09 00:00:00.000000000 Z
11
+ date: 2019-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mini_magick
@@ -60,6 +60,8 @@ extra_rdoc_files: []
60
60
  files:
61
61
  - ".gitignore"
62
62
  - ".rspec"
63
+ - CHANGELOG.md
64
+ - CONTRIBUTING.md
63
65
  - Gemfile
64
66
  - LICENSE
65
67
  - README.md