escpos-image 0.0.9 → 0.0.12

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: dc80022cd61c7bc3eff82db4ee5d577dd9dc0a31749536ea6e50a9c9305901b6
4
+ data.tar.gz: 2e53ed6568bdc148b20594818a44c4daaf5c7da4850a37e29b0d7ca69d1a605b
5
5
  SHA512:
6
- metadata.gz: 5cfec52a3866a10b7edc431fc8c8e5da9e6bff791d9aabafe7843b9ea8d46753768f80e3bf4c4a3bb77f82d3b2b0969a190e111c7ff3306d85556efdc1df7b78
7
- data.tar.gz: ace68af135955b30bb38c5951650391c28f0a64b9fcaf2b7e00f854def83a5e3017dd24ffd75b24b09ea6d13c04459127dab684c9c9047a5320c4473f4a01710
6
+ metadata.gz: 1dec2c046d2b46682ab5ddde4bab2ec49b518042a29f0a0db4ae10073aa7440289aa6b4afc83531a29670a1e7132bf02e09b757fe6f5aa81d1616d7deb1132ff
7
+ data.tar.gz: 949bf4644f2674cd62c3d0098f707da79cf8f0f59791d87e395bb65ac1ed52b0a083e57098a4c7227e3716ca0fcbf028c39bdbdb23c937918d72334de3227fce
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
@@ -1,13 +1,20 @@
1
+ [Build Status](https://gitlab.com/escpos/escpos-image/pipelines)
2
+
1
3
  # Escpos-image
2
4
 
3
5
  A ruby implementation of ESC/POS (thermal) printer image command specification.
4
6
 
5
7
  ## Installation
6
8
 
7
- Add this line to your application's Gemfile:
9
+ Add this lines to your application's Gemfile:
8
10
 
9
11
  ```ruby
10
12
  gem 'escpos-image'
13
+
14
+ # Depending on chosen image processor
15
+ gem 'mini_magick'
16
+ # or
17
+ gem 'chunky_png'
11
18
  ```
12
19
 
13
20
  And then execute:
@@ -18,8 +25,16 @@ Or install it yourself as:
18
25
 
19
26
  $ gem install escpos-image
20
27
 
28
+ And then depending on chosen image processor
29
+
30
+ $ gem install mini_magick
31
+
32
+ or
33
+
34
+ $ gem install chunky_png
21
35
  ## Examples
22
36
 
37
+ ![](https://github.com/escpos/escpos-image/blob/master/examples/IMG_20190225_162935.jpg)
23
38
  ![](https://github.com/escpos/escpos-image/blob/master/examples/IMG_20160610_232415_HDR.jpg)
24
39
 
25
40
  ## Usage
@@ -29,13 +44,18 @@ Or install it yourself as:
29
44
 
30
45
  # Creating image from path
31
46
  image = Escpos::Image.new 'path/to/image.png', {
32
- processor: "ChunkyPng" # default or MiniMagick
47
+ processor: "ChunkyPng" # or MiniMagick
33
48
  # ... other options, see following sections
34
49
  }
35
50
 
51
+ # The ChunkyPng processor requires the chunky_png gem installed
36
52
  # The MiniMagick processor requires the mini_magick gem installed
37
53
 
38
- # The constructor also accepts an instance of ChunkyPNG::Image or MiniMagick::Image
54
+ # The constructor accepts an instance of:
55
+ # - String (path to image)
56
+ # - File
57
+ # - ChunkyPNG::Image
58
+ # - MiniMagick::Image
39
59
 
40
60
  @printer << image
41
61
 
@@ -51,9 +71,9 @@ image = Escpos::Image.new 'path/to/image.png', {
51
71
 
52
72
  | ChunkyPng | MiniMagick |
53
73
  | --- | --- |
54
- | PNG | PNG, JPG, BMP, ... (everything supported by MiniMagick) |
74
+ | PNG | PNG, JPG, GIF, BMP, TIF, PCX, ... (everything supported by MiniMagick) |
55
75
 
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.
76
+ 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
77
 
58
78
  ## Image manipulation
59
79
 
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
- spec.add_dependency "escpos", ">= 0.0.9"
29
- spec.add_dependency "chunky_png"
29
+ spec.add_dependency "escpos", ">= 0.0.10"
30
30
  end
Binary file
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
@@ -1,4 +1,5 @@
1
1
  require_relative "errors"
2
+ require_relative "helpers"
2
3
  require_relative "image_processors/chunky_png"
3
4
  require_relative "image_processors/mini_magick"
4
5
 
@@ -9,39 +10,29 @@ module Escpos
9
10
 
10
11
  class Image
11
12
 
12
- VERSION = "0.0.9"
13
+ VERSION = "0.0.12"
13
14
 
14
15
  attr_reader :processor, :options
15
16
 
16
17
  def initialize(image_or_path, options = {})
17
18
  @options = options
18
19
 
19
- processor_klass_name = options.fetch(:processor, "ChunkyPng")
20
+ processor_klass_name = options.fetch(:processor)
20
21
  processor_klass = ImageProcessors.const_get(processor_klass_name)
21
22
  @processor = processor_klass.new image_or_path, options
22
23
 
23
24
  @processor.process!
24
25
  end
25
26
 
26
- def chunky_png_image
27
- processor.chunky_png_image
28
- end
29
-
30
27
  def to_escpos
31
28
  bits = []
32
29
  mask = 0x80
33
30
  i = 0
34
31
  temp = 0
35
32
 
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
33
+ 0.upto(processor.image.height - 1) do |y|
34
+ 0.upto(processor.image.width - 1) do |x|
35
+ px = processor.get_pixel(x, y)
45
36
  value = px >= 128 ? 255 : 0
46
37
  value = (value << 8) | value
47
38
  temp |= mask if value == 0
@@ -58,7 +49,7 @@ module Escpos
58
49
 
59
50
  [
60
51
  Escpos.sequence(IMAGE),
61
- [ chunky_png_image.width / 8, chunky_png_image.height ].pack("SS"),
52
+ [ processor.image.width / 8, processor.image.height ].pack("SS"),
62
53
  bits.pack("C*")
63
54
  ].join
64
55
  end
@@ -5,9 +5,13 @@ 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
13
+ elsif image_or_path.is_a?(File)
14
+ ChunkyPNG::Image.from_file(image_or_path.path)
11
15
  elsif image_or_path.is_a?(String)
12
16
  ChunkyPNG::Image.from_file(image_or_path)
13
17
  else
@@ -18,14 +22,30 @@ module Escpos
18
22
  super
19
23
  end
20
24
 
21
- def chunky_png_image
22
- @image
23
- end
24
-
25
25
  def assert_options!
26
26
  assert_dimensions_multiple_of_8!
27
27
  end
28
28
 
29
+ # ChunkyPng gem is not required intentionally
30
+ # This makes the gem more lightweight by making dependencies
31
+ # optional and based on chosen image processor
32
+ def require_chunky_png!
33
+ return if defined?(::ChunkyPng)
34
+ require "chunky_png"
35
+ rescue LoadError => e
36
+ raise DependencyNotInstalled.new("chunky_png")
37
+ end
38
+
39
+ def get_pixel(x, y)
40
+ px = image.get_pixel x, y
41
+ r, g, b =
42
+ ChunkyPNG::Color.r(px),
43
+ ChunkyPNG::Color.g(px),
44
+ ChunkyPNG::Color.b(px)
45
+
46
+ (r + b + g) / 3
47
+ end
48
+
29
49
  def process!
30
50
  extent = options.fetch(:extent, false)
31
51
  compose_alpha = options.fetch(:compose_alpha, false)
@@ -10,6 +10,8 @@ module Escpos
10
10
  @image = begin
11
11
  if image_or_path.is_a?(::MiniMagick::Image)
12
12
  image_or_path
13
+ elsif image_or_path.is_a?(File)
14
+ ::MiniMagick::Image.open(image_or_path.path)
13
15
  elsif image_or_path.is_a?(String)
14
16
  ::MiniMagick::Image.open(image_or_path)
15
17
  else
@@ -20,8 +22,15 @@ module Escpos
20
22
  super
21
23
  end
22
24
 
23
- def chunky_png_image
24
- @chunky_png_image ||= ChunkyPNG::Image.from_file @image.path
25
+ def get_pixel(x, y)
26
+ @pixels ||= image.get_pixels
27
+
28
+ r, g, b =
29
+ @pixels[y][x][0],
30
+ @pixels[y][x][1],
31
+ @pixels[y][x][2]
32
+
33
+ (r + b + g) / 3
25
34
  end
26
35
 
27
36
  def assert_options!
@@ -29,12 +38,13 @@ module Escpos
29
38
  end
30
39
 
31
40
  # MiniMagick gem is not required intentionally
32
- # This makes the gem more lightweight by making this dependency optional
41
+ # This makes the gem more lightweight by making dependencies
42
+ # optional and based on chosen image processor
33
43
  def require_mini_magick!
34
44
  return if defined?(::MiniMagick)
35
45
  require "mini_magick"
36
- rescue LoadError => e
37
- raise MiniMagickNotInstalled
46
+ rescue LoadError => e
47
+ raise DependencyNotInstalled.new("mini_magick")
38
48
  end
39
49
 
40
50
  def process!
@@ -70,9 +80,6 @@ module Escpos
70
80
  if options.fetch(:extent, false)
71
81
  image.extent "#{(image.width/8.0).round*8}x#{(image.height/8.0).round*8}"
72
82
  end
73
-
74
- # Force PNG format so ChunkyPNG works
75
- image.format 'png'
76
83
  end
77
84
 
78
85
  end
@@ -1,40 +1,42 @@
1
1
  require_relative '../../test_helper'
2
2
 
3
3
  class ImageTest < Minitest::Test
4
+
4
5
  def setup
5
6
  @printer = Escpos::Printer.new
6
7
  end
7
8
 
8
- # TODO photos of results
9
- # TODO USB with option print tests (dev dependency)
10
-
11
9
  def test_image
12
10
  image_path = File.join(__dir__, '../../fixtures/tux_mono.png')
13
- image = Escpos::Image.new image_path,
11
+ image_file = File.new image_path
12
+ image = Escpos::Image.new image_file,
14
13
  processor: "MiniMagick"
14
+ image_file.close
15
15
 
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
- #IO.binwrite file, @printer.to_escpos
21
+ #@printer.save file
22
+
23
23
  assert_equal IO.binread(file), @printer.to_escpos
24
24
  end
25
25
 
26
26
  def test_image_conversion
27
27
  image_path = File.join(__dir__, '../../fixtures/tux_alpha.png')
28
28
  image = Escpos::Image.new image_path, grayscale: true,
29
- compose_alpha: true, extent: true
29
+ compose_alpha: true, extent: true,
30
+ processor: "ChunkyPng"
30
31
 
31
32
  @printer << image.to_escpos
32
33
  @printer << "\n" * 10
33
34
  @printer.cut!
34
- image.chunky_png_image.metadata = {}
35
- image.chunky_png_image.save(File.join(__dir__, "../../results/#{__method__}.png"))
35
+ image.processor.image.metadata = {}
36
+ image.processor.image.save(File.join(__dir__, "../../results/#{__method__}.png"))
36
37
  file = File.join(__dir__, "../../results/#{__method__}.txt")
37
- #IO.binwrite file, @printer.to_escpos
38
+ #@printer.save file
39
+
38
40
  assert_equal IO.binread(file), @printer.to_escpos
39
41
  end
40
42
 
Binary file
data/test/test_helper.rb CHANGED
@@ -5,5 +5,4 @@ require 'minitest/autorun'
5
5
  require 'pp'
6
6
 
7
7
  require 'escpos'
8
- require 'chunky_png'
9
8
  require File.expand_path('../../lib/escpos/image.rb', __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.12
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: 2022-09-06 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.10
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.10
97
97
  description: A ruby implementation of ESC/POS (thermal) printer image command specification.
98
98
  email:
99
99
  - jan@mluv.cz
@@ -112,6 +112,7 @@ files:
112
112
  - bin/setup
113
113
  - escpos-image.gemspec
114
114
  - examples/IMG_20160610_232415_HDR.jpg
115
+ - examples/IMG_20190225_162935.jpg
115
116
  - lib/escpos/errors.rb
116
117
  - lib/escpos/helpers.rb
117
118
  - lib/escpos/image.rb
@@ -145,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
146
  - !ruby/object:Gem::Version
146
147
  version: '0'
147
148
  requirements: []
148
- rubygems_version: 3.0.2
149
+ rubygems_version: 3.1.2
149
150
  signing_key:
150
151
  specification_version: 4
151
152
  summary: A ruby implementation of ESC/POS (thermal) printer image command specification.