Skip to content

Commit

Permalink
Merge pull request #3 from mtking2/v0.1.2
Browse files Browse the repository at this point in the history
ASCII compression
  • Loading branch information
mtking2 authored Oct 11, 2019
2 parents 14883db + d4425bb commit e6a1e99
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 10 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

* Your contribution here.

### v0.1.2 (2019/10/10)

* [#3](https://github.com/mtking2/img2zpl/pull/3): Add ASCII compression to conversion - [@mtking2](https://github.com/mtking2).

### v0.1.1 (2019/10/09)

* [#2](https://github.com/mtking2/img2zpl/pull/2): Rename primary conversion method `.zpl` --> `.to_zpl` [@mtking2](https://github.com/mtking2).
* [#2](https://github.com/mtking2/img2zpl/pull/2): Rename primary conversion method `.zpl` --> `.to_zpl` - [@mtking2](https://github.com/mtking2).

### v0.1.0 (2019/10/08)
* [#1](https://github.com/mtking2/img2zpl/pull/1): Add functioning algorithm [@mtking2](https://github.com/mtking2).
* [#1](https://github.com/mtking2/img2zpl/pull/1): Add functioning algorithm - [@mtking2](https://github.com/mtking2).
* Initial public release - [@mtking2](https://github.com/mtking2).
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source 'https://rubygems.org'
group :development, :test do
gem 'byebug'
gem 'pry'
gem 'pry-nav'
end

gemspec
39 changes: 34 additions & 5 deletions lib/img2zpl/image.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
class Img2Zpl::Image < MiniMagick::Image

def to_zpl(black_threshold: 0.5)
def to_zpl(black_threshold: 0.5, compress: true)
bytes_per_row = (width % 8).positive? ? (width / 8) + 1 : (width / 8)
byte_count = bytes_per_row * height
data, line, previous_line, byte = '', '', '', ''

get_pixels.each do |row|

row.each_with_index do |column, i|
r, g, b = column.map(&:to_i)
byte << ((r + g + b) > (black_threshold * 765) ? '0' : '1')
Expand All @@ -15,18 +14,48 @@ def to_zpl(black_threshold: 0.5)
byte = ''
end
end
data << (line == previous_line ? ':' : line.gsub(/0+$/, ',').gsub(/F+$/, '!'))

data << if compress
(line == previous_line ? ':' : line.gsub(/0+$/, ',').gsub(/F+$/, '!'))
else
line
end

previous_line = line
line = ''
end

"^FO0,0^GFA,#{byte_count},#{byte_count},#{bytes_per_row},#{data}^FS"
_compress(data) if compress
"^GFA,#{byte_count},#{byte_count},#{bytes_per_row},#{data}^FS"
end

private

def _compression_map
# TODO: finish compression map
map = {}
start = 'G'.ord
19.times { |i| map[i + 1] = (start+i).chr }
start = 'g'.ord
(20..400).step(20).each_with_index { |i, j| map[i] = (start+j).chr }
map
end

def _reduce(n)
str = ''
counts = _compression_map.keys.sort.reverse
counts.each do |c|
if c <= n
str << (_compression_map[c])
n -= c
end
end
str
end

def _compress(data)
data.gsub!(/([\da-zA-Z])(\1+)/) do |m|
"#{_reduce(m.length)}#{$1}"
end
end

end
2 changes: 1 addition & 1 deletion lib/img2zpl/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Img2Zpl
VERSION = '0.1.1'
VERSION = '0.1.2'
end
59 changes: 57 additions & 2 deletions spec/img2zpl/image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,68 @@

subject { described_class.open('spec/fixtures/default.jpg') }

it 'inherits from the MiniMagick::Image class' do
it 'should inherit from the MiniMagick::Image class' do
expect(described_class.respond_to?(:open)).to be true
expect(described_class.respond_to?(:read)).to be true
expect(subject.respond_to?(:to_zpl)).to be true
end

it 'zpl method returns a string' do
it 'should have the compression map' do
map = subject.send(:_compression_map)
expect(map[1]).to eq 'G'
expect(map[2]).to eq 'H'
expect(map[3]).to eq 'I'
expect(map[4]).to eq 'J'
expect(map[5]).to eq 'K'
expect(map[6]).to eq 'L'
expect(map[7]).to eq 'M'
expect(map[8]).to eq 'N'
expect(map[9]).to eq 'O'
expect(map[10]).to eq 'P'
expect(map[11]).to eq 'Q'
expect(map[12]).to eq 'R'
expect(map[13]).to eq 'S'
expect(map[14]).to eq 'T'
expect(map[15]).to eq 'U'
expect(map[16]).to eq 'V'
expect(map[17]).to eq 'W'
expect(map[18]).to eq 'X'
expect(map[19]).to eq 'Y'
expect(map[20]).to eq 'g'
expect(map[40]).to eq 'h'
expect(map[60]).to eq 'i'
expect(map[80]).to eq 'j'
expect(map[100]).to eq 'k'
expect(map[120]).to eq 'l'
expect(map[140]).to eq 'm'
expect(map[160]).to eq 'n'
expect(map[180]).to eq 'o'
expect(map[200]).to eq 'p'
expect(map[220]).to eq 'q'
expect(map[240]).to eq 'r'
expect(map[260]).to eq 's'
expect(map[280]).to eq 't'
expect(map[300]).to eq 'u'
expect(map[320]).to eq 'v'
expect(map[340]).to eq 'w'
expect(map[360]).to eq 'x'
expect(map[380]).to eq 'y'
expect(map[400]).to eq 'z'
end

it 'should properly compress ASCII data' do
d1 = '00000000000000000000000000000000000000000000000000000000008'
d2 = '5555555555555ADDDDDDDDDDDDDDDDDDDDDDD'
d3 = '00000000FFFFFFFFFFFFFFFFFFFFFFC0000000000000000000007FFFFFFFFF'
subject.send(:_compress, d1)
subject.send(:_compress, d2)
subject.send(:_compress, d3)
expect(d1).to eq 'hX08'
expect(d2).to eq 'S5AgID'
expect(d3).to eq 'N0gHFCgG07OF'
end

it 'should return a string when calling .to_zpl' do
expect(subject.to_zpl).to be_kind_of String
end

Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
require 'img2zpl'
require 'byebug'
require 'pry'
require 'pry-nav'


RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
Expand Down

0 comments on commit e6a1e99

Please sign in to comment.