escpos-image 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3093ebac2fdbbf59507fc5c4a209f85941a90d03c0f0838a626b43401df38672
4
- data.tar.gz: 9e52328a87653d53b3d4359010feccae312c93ef10ecbbc6d41db0944c52469d
3
+ metadata.gz: 494847e3b67b9e87fec165e48507d7050a779aa36ff440a22b874233cbfd72e8
4
+ data.tar.gz: 9aadde3e6581ebc836420220e0b33949e71fafe97328786d1d7819f9e8488d59
5
5
  SHA512:
6
- metadata.gz: ef20ab54bffe996cb3ed65bcd5d6eff1c7a4cc581c63ccdcae982766e3616d8e6f13a02645f0921747923fe91971f399da5c15f9a164b34df44f7e877a284274
7
- data.tar.gz: 18cfe1a217a0910f5644b198e5c32a4eeaf04f9d94e8a1a132bdb1af82f9124e7bc2d3c66c91610008e2b4f715b232848efa265acb4113b821705a2f58602b1d
6
+ metadata.gz: 462c890880ca0ecb3a870d99c90525996d70ba8b748bfe80a00bc3c37233deea5567716e90bc957d1048308108f12359d410f3b45528e6285769639756d054f1
7
+ data.tar.gz: c6f7c07aa8886ded1887859970de46b38c71f0c6df60e4f6b82f7c803a1133e9e672547a8700759645367245081c2b09b015f2430cb39a80257a205f26cea016
data/.gitlab-ci.yml CHANGED
@@ -1,4 +1,4 @@
1
- image: ruby:2.5
1
+ image: ruby:2.6
2
2
 
3
3
  cache:
4
4
  paths:
@@ -17,6 +17,6 @@ before_script:
17
17
  test:
18
18
  stage: test
19
19
  script:
20
- - rake test
20
+ - bundle exec rake test
21
21
  environment:
22
22
  name: test
data/README.md CHANGED
@@ -33,6 +33,8 @@ image = Escpos::Image.new 'path/to/image.png', {
33
33
  convert_to_monochrome: true,
34
34
  dither: true, # the default
35
35
  extent: true, # the default
36
+ compose_alpha: false, # the default
37
+ compose_alpha_bg: 255, # default, assume white background
36
38
  }
37
39
 
38
40
  @printer.write image.to_escpos
data/escpos-image.gemspec CHANGED
@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "rake"
24
24
 
25
25
  spec.add_development_dependency "minitest", "~> 5.4"
26
+ spec.add_development_dependency "mini_magick"
26
27
 
27
28
  spec.add_dependency "escpos"
28
29
  spec.add_dependency "chunky_png"
@@ -1,8 +1,8 @@
1
1
  module Escpos
2
2
  module Helpers
3
3
 
4
- def image(path, opts = {})
5
- Image.new(path, opts).to_escpos
4
+ def image(path, options = {})
5
+ Image.new(path, options).to_escpos
6
6
  end
7
7
 
8
8
  end
data/lib/escpos/image.rb CHANGED
@@ -1,15 +1,18 @@
1
1
  module Escpos
2
2
  class Image
3
3
 
4
- VERSION = "0.0.4"
4
+ VERSION = "0.0.5"
5
5
 
6
- def initialize(image_or_path, opts = {})
6
+ attr_reader :options
7
+
8
+ def initialize(image_or_path, options = {})
9
+ @options = options
7
10
  if image_or_path.is_a?(ChunkyPNG::Image)
8
11
  @image = image_or_path
9
12
  elsif image_or_path.is_a?(String)
10
- if opts.fetch(:convert_to_monochrome, false)
13
+ if options.fetch(:convert_to_monochrome, false)
11
14
  require_mini_magick!
12
- image = convert_to_monochrome(image_or_path, opts)
15
+ image = convert_to_monochrome(image_or_path)
13
16
  @image = ChunkyPNG::Image.from_file(image.path)
14
17
  else
15
18
  @image = ChunkyPNG::Image.from_file(image_or_path)
@@ -38,6 +41,11 @@ module Escpos
38
41
  ChunkyPNG::Color.b(px)
39
42
  px = (r + b + g) / 3
40
43
  # Alpha is flattened with convert_to_monochrome option
44
+ if options.fetch(:compose_alpha, false)
45
+ bg_color = options.fetch(:compose_alpha_bg, 255)
46
+ a_quot = a / 255.0
47
+ px = (((1 - a_quot) * bg_color) + (a_quot * px)).to_i
48
+ end
41
49
  value = px >= 128 ? 255 : 0
42
50
  value = (value << 8) | value
43
51
  temp |= mask if value == 0
@@ -79,7 +87,7 @@ module Escpos
79
87
  end
80
88
  end
81
89
 
82
- def convert_to_monochrome(image_path, opts = {})
90
+ def convert_to_monochrome(image_path)
83
91
  image = MiniMagick::Image.open(image_path)
84
92
 
85
93
  # Flatten transparency
@@ -90,10 +98,10 @@ module Escpos
90
98
 
91
99
  # Optimise more actions to single call
92
100
  image.combine_options do |c|
93
- c.rotate opts.fetch(:rotate) if opts.has_key?(:rotate)
94
- c.resize opts.fetch(:resize) if opts.has_key?(:resize)
101
+ c.rotate options.fetch(:rotate) if options.has_key?(:rotate)
102
+ c.resize options.fetch(:resize) if options.has_key?(:resize)
95
103
  c.grayscale 'Rec709Luma'
96
- if opts.fetch(:dither, true)
104
+ if options.fetch(:dither, true)
97
105
  c.monochrome '+dither'
98
106
  # dither the image with FloydSteinberg algoritm for better results
99
107
  c.dither 'FloydSteinberg'
@@ -103,7 +111,7 @@ module Escpos
103
111
  end
104
112
 
105
113
  # Limit the extent of the image to nice round numbers
106
- if opts.fetch(:extent, true)
114
+ if options.fetch(:extent, true)
107
115
  image.extent "#{(image.width/8.0).round*8}x#{(image.height/8.0).round*8}"
108
116
  end
109
117
 
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: escpos-image
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Svoboda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-19 00:00:00.000000000 Z
11
+ date: 2019-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '5.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mini_magick
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: escpos
57
71
  requirement: !ruby/object:Gem::Requirement