escpos-image 0.0.9 → 0.0.10

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: d77f652d199f1562943324c9d167585d0dd8720d60907e9b35dd05f9e34b9c09
4
- data.tar.gz: 9948efcabb7054074d5efa222013550a6c5f6d1f386ec9f1886a341159030f34
3
+ metadata.gz: 36d745212fbf5f8f6b32fe00942ca3e76a82ddd67d9e57135b77b7ae159debbe
4
+ data.tar.gz: 8cd8ce72f04f2c80c2057178088cc7eeb4468ebf16553918a2e54332aade82af
5
5
  SHA512:
6
- metadata.gz: 5cfec52a3866a10b7edc431fc8c8e5da9e6bff791d9aabafe7843b9ea8d46753768f80e3bf4c4a3bb77f82d3b2b0969a190e111c7ff3306d85556efdc1df7b78
7
- data.tar.gz: ace68af135955b30bb38c5951650391c28f0a64b9fcaf2b7e00f854def83a5e3017dd24ffd75b24b09ea6d13c04459127dab684c9c9047a5320c4473f4a01710
6
+ metadata.gz: cb264d2de0adba6dfa565c6cd441c509a459e28d0a408dacba9c2deabd9f2094f8aebf48f6d71957d0991e49da0fff0322aa6faecbb76e58cddd68af07e3c09e
7
+ data.tar.gz: c6fb9548e36938b6880eff29aa3da56ab25f57c51f0ced7e3b6d881eeb35bae2e51910f8573fd791b4d1f52d25899d3a9e05cff78c2176baa5b01b7899250e3a
data/.gitlab-ci.yml CHANGED
@@ -13,6 +13,7 @@ before_script:
13
13
  - bundle install --jobs $(nproc) --path vendor
14
14
  - apt-get install -y imagemagick
15
15
  - gem install mini_magick
16
+ - gem install chunky_png
16
17
 
17
18
  test:
18
19
  stage: test
data/README.md CHANGED
@@ -4,10 +4,15 @@ A ruby implementation of ESC/POS (thermal) printer image command specification.
4
4
 
5
5
  ## Installation
6
6
 
7
- Add this line to your application's Gemfile:
7
+ Add this lines to your application's Gemfile:
8
8
 
9
9
  ```ruby
10
10
  gem 'escpos-image'
11
+
12
+ # Depending on chosen image processor
13
+ gem 'mini_magick'
14
+ # or
15
+ gem 'chunky_png'
11
16
  ```
12
17
 
13
18
  And then execute:
@@ -18,6 +23,13 @@ Or install it yourself as:
18
23
 
19
24
  $ gem install escpos-image
20
25
 
26
+ And then depending on chosen image processor
27
+
28
+ $ gem install mini_magick
29
+
30
+ or
31
+
32
+ $ gem install chunky_png
21
33
  ## Examples
22
34
 
23
35
  ![](https://github.com/escpos/escpos-image/blob/master/examples/IMG_20160610_232415_HDR.jpg)
@@ -29,13 +41,17 @@ Or install it yourself as:
29
41
 
30
42
  # Creating image from path
31
43
  image = Escpos::Image.new 'path/to/image.png', {
32
- processor: "ChunkyPng" # default or MiniMagick
44
+ processor: "ChunkyPng" # or MiniMagick
33
45
  # ... other options, see following sections
34
46
  }
35
47
 
48
+ # The ChunkyPng processor requires the chunky_png gem installed
36
49
  # The MiniMagick processor requires the mini_magick gem installed
37
50
 
38
- # The constructor also accepts an instance of ChunkyPNG::Image or MiniMagick::Image
51
+ # The constructor accepts an instance of:
52
+ # - String (path to image)
53
+ # - ChunkyPNG::Image
54
+ # - MiniMagick::Image
39
55
 
40
56
  @printer << image
41
57
 
@@ -53,7 +69,7 @@ image = Escpos::Image.new 'path/to/image.png', {
53
69
  | --- | --- |
54
70
  | PNG | PNG, JPG, BMP, ... (everything supported by MiniMagick) |
55
71
 
56
- When using MiniMagick processor, `mini_magick` gem has to be installed or added to the Gemfile, this makes the gem more lightweight by making this dependency optional.
72
+ When using `ChunkyPng` processor, `chunky_png` gem has to be installed or added to the Gemfile and when using `MiniMagick` processor, `mini_magick` gem has to be installed or added to the Gemfile, this makes the gem more lightweight by making dependencies optional and based on chosen image processor.
57
73
 
58
74
  ## Image manipulation
59
75
 
data/escpos-image.gemspec CHANGED
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
24
24
 
25
25
  spec.add_development_dependency "minitest", "~> 5.4"
26
26
  spec.add_development_dependency "mini_magick"
27
+ spec.add_development_dependency "chunky_png"
27
28
 
28
29
  spec.add_dependency "escpos", ">= 0.0.9"
29
- spec.add_dependency "chunky_png"
30
30
  end
data/lib/escpos/errors.rb CHANGED
@@ -12,9 +12,13 @@ module Escpos
12
12
  end
13
13
  end
14
14
 
15
- class MiniMagickNotInstalled < LoadError
15
+ class DependencyNotInstalled < LoadError
16
+ attr_reader :dependency_name
17
+ def initialize(dependency_name)
18
+ @dependency_name = dependency_name
19
+ end
16
20
  def message
17
- "Required options need the mini_magick gem installed: #{e}."
21
+ "Required options need the \"#{@dependency_name}\" gem installed."
18
22
  end
19
23
  end
20
24
 
data/lib/escpos/image.rb CHANGED
@@ -9,39 +9,29 @@ module Escpos
9
9
 
10
10
  class Image
11
11
 
12
- VERSION = "0.0.9"
12
+ VERSION = "0.0.10"
13
13
 
14
14
  attr_reader :processor, :options
15
15
 
16
16
  def initialize(image_or_path, options = {})
17
17
  @options = options
18
18
 
19
- processor_klass_name = options.fetch(:processor, "ChunkyPng")
19
+ processor_klass_name = options.fetch(:processor)
20
20
  processor_klass = ImageProcessors.const_get(processor_klass_name)
21
21
  @processor = processor_klass.new image_or_path, options
22
22
 
23
23
  @processor.process!
24
24
  end
25
25
 
26
- def chunky_png_image
27
- processor.chunky_png_image
28
- end
29
-
30
26
  def to_escpos
31
27
  bits = []
32
28
  mask = 0x80
33
29
  i = 0
34
30
  temp = 0
35
31
 
36
- 0.upto(chunky_png_image.height - 1) do |y|
37
- 0.upto(chunky_png_image.width - 1) do |x|
38
- px = chunky_png_image.get_pixel(x, y)
39
- r, g, b =
40
- ChunkyPNG::Color.r(px),
41
- ChunkyPNG::Color.g(px),
42
- ChunkyPNG::Color.b(px)
43
-
44
- px = (r + b + g) / 3
32
+ 0.upto(processor.image.height - 1) do |y|
33
+ 0.upto(processor.image.width - 1) do |x|
34
+ px = processor.get_pixel(x, y)
45
35
  value = px >= 128 ? 255 : 0
46
36
  value = (value << 8) | value
47
37
  temp |= mask if value == 0
@@ -58,7 +48,7 @@ module Escpos
58
48
 
59
49
  [
60
50
  Escpos.sequence(IMAGE),
61
- [ chunky_png_image.width / 8, chunky_png_image.height ].pack("SS"),
51
+ [ processor.image.width / 8, processor.image.height ].pack("SS"),
62
52
  bits.pack("C*")
63
53
  ].join
64
54
  end
@@ -5,6 +5,8 @@ module Escpos
5
5
  class ChunkyPng < Base
6
6
 
7
7
  def initialize(image_or_path, options = {})
8
+ require_chunky_png!
9
+
8
10
  @image = begin
9
11
  if image_or_path.is_a?(ChunkyPNG::Image)
10
12
  image_or_path
@@ -18,14 +20,30 @@ module Escpos
18
20
  super
19
21
  end
20
22
 
21
- def chunky_png_image
22
- @image
23
- end
24
-
25
23
  def assert_options!
26
24
  assert_dimensions_multiple_of_8!
27
25
  end
28
26
 
27
+ # ChunkyPng gem is not required intentionally
28
+ # This makes the gem more lightweight by making dependencies
29
+ # optional and based on chosen image processor
30
+ def require_chunky_png!
31
+ return if defined?(::ChunkyPng)
32
+ require "chunky_png"
33
+ rescue LoadError => e
34
+ raise DependencyNotInstalled.new("chunky_png")
35
+ end
36
+
37
+ def get_pixel(x, y)
38
+ px = image.get_pixel x, y
39
+ r, g, b =
40
+ ChunkyPNG::Color.r(px),
41
+ ChunkyPNG::Color.g(px),
42
+ ChunkyPNG::Color.b(px)
43
+
44
+ (r + b + g) / 3
45
+ end
46
+
29
47
  def process!
30
48
  extent = options.fetch(:extent, false)
31
49
  compose_alpha = options.fetch(:compose_alpha, false)
@@ -20,8 +20,15 @@ module Escpos
20
20
  super
21
21
  end
22
22
 
23
- def chunky_png_image
24
- @chunky_png_image ||= ChunkyPNG::Image.from_file @image.path
23
+ def get_pixel(x, y)
24
+ @pixels ||= image.get_pixels
25
+
26
+ r, g, b =
27
+ @pixels[y][x][0],
28
+ @pixels[y][x][1],
29
+ @pixels[y][x][2]
30
+
31
+ (r + b + g) / 3
25
32
  end
26
33
 
27
34
  def assert_options!
@@ -29,12 +36,13 @@ module Escpos
29
36
  end
30
37
 
31
38
  # MiniMagick gem is not required intentionally
32
- # This makes the gem more lightweight by making this dependency optional
39
+ # This makes the gem more lightweight by making dependencies
40
+ # optional and based on chosen image processor
33
41
  def require_mini_magick!
34
42
  return if defined?(::MiniMagick)
35
43
  require "mini_magick"
36
44
  rescue LoadError => e
37
- raise MiniMagickNotInstalled
45
+ raise DependencyNotInstalled.new("mini_magick")
38
46
  end
39
47
 
40
48
  def process!
@@ -70,9 +78,6 @@ module Escpos
70
78
  if options.fetch(:extent, false)
71
79
  image.extent "#{(image.width/8.0).round*8}x#{(image.height/8.0).round*8}"
72
80
  end
73
-
74
- # Force PNG format so ChunkyPNG works
75
- image.format 'png'
76
81
  end
77
82
 
78
83
  end
@@ -16,8 +16,7 @@ class ImageTest < Minitest::Test
16
16
  @printer << image
17
17
  @printer << "\n" * 10
18
18
  @printer.cut!
19
- image.chunky_png_image.metadata = {}
20
- image.chunky_png_image.save(File.join(__dir__, "../../results/#{__method__}.png"))
19
+ image.processor.image.write(File.join(__dir__, "../../results/#{__method__}.png"))
21
20
  file = File.join(__dir__, "../../results/#{__method__}.txt")
22
21
  #IO.binwrite file, @printer.to_escpos
23
22
  assert_equal IO.binread(file), @printer.to_escpos
@@ -26,13 +25,14 @@ class ImageTest < Minitest::Test
26
25
  def test_image_conversion
27
26
  image_path = File.join(__dir__, '../../fixtures/tux_alpha.png')
28
27
  image = Escpos::Image.new image_path, grayscale: true,
29
- compose_alpha: true, extent: true
28
+ compose_alpha: true, extent: true,
29
+ processor: "ChunkyPng"
30
30
 
31
31
  @printer << image.to_escpos
32
32
  @printer << "\n" * 10
33
33
  @printer.cut!
34
- image.chunky_png_image.metadata = {}
35
- image.chunky_png_image.save(File.join(__dir__, "../../results/#{__method__}.png"))
34
+ image.processor.image.metadata = {}
35
+ image.processor.image.save(File.join(__dir__, "../../results/#{__method__}.png"))
36
36
  file = File.join(__dir__, "../../results/#{__method__}.txt")
37
37
  #IO.binwrite file, @printer.to_escpos
38
38
  assert_equal IO.binread(file), @printer.to_escpos
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.9
4
+ version: 0.0.10
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-23 00:00:00.000000000 Z
11
+ date: 2019-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,33 +67,33 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: escpos
70
+ name: chunky_png
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: 0.0.9
76
- type: :runtime
75
+ version: '0'
76
+ type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: 0.0.9
82
+ version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: chunky_png
84
+ name: escpos
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
88
88
  - !ruby/object:Gem::Version
89
- version: '0'
89
+ version: 0.0.9
90
90
  type: :runtime
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
- version: '0'
96
+ version: 0.0.9
97
97
  description: A ruby implementation of ESC/POS (thermal) printer image command specification.
98
98
  email:
99
99
  - jan@mluv.cz