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 +4 -4
 - data/.gitlab-ci.yml +2 -2
 - data/README.md +2 -0
 - data/escpos-image.gemspec +1 -0
 - data/lib/escpos/helpers.rb +2 -2
 - data/lib/escpos/image.rb +17 -9
 - data/test/results/test_image_conversion.png +0 -0
 - metadata +16 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 494847e3b67b9e87fec165e48507d7050a779aa36ff440a22b874233cbfd72e8
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 9aadde3e6581ebc836420220e0b33949e71fafe97328786d1d7819f9e8488d59
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 462c890880ca0ecb3a870d99c90525996d70ba8b748bfe80a00bc3c37233deea5567716e90bc957d1048308108f12359d410f3b45528e6285769639756d054f1
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: c6f7c07aa8886ded1887859970de46b38c71f0c6df60e4f6b82f7c803a1133e9e672547a8700759645367245081c2b09b015f2430cb39a80257a205f26cea016
         
     | 
    
        data/.gitlab-ci.yml
    CHANGED
    
    
    
        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
    
    
    
        data/lib/escpos/helpers.rb
    CHANGED
    
    
    
        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 
     | 
    
         
            +
                VERSION = "0.0.5"
         
     | 
| 
       5 
5 
     | 
    
         | 
| 
       6 
     | 
    
         
            -
                 
     | 
| 
      
 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  
     | 
| 
      
 13 
     | 
    
         
            +
                    if options.fetch(:convert_to_monochrome, false)
         
     | 
| 
       11 
14 
     | 
    
         
             
                      require_mini_magick!
         
     | 
| 
       12 
     | 
    
         
            -
                      image = convert_to_monochrome(image_or_path 
     | 
| 
      
 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 
     | 
| 
      
 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  
     | 
| 
       94 
     | 
    
         
            -
                    c.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  
     | 
| 
      
 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  
     | 
| 
      
 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 
     | 
    
         
            +
              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- 
     | 
| 
      
 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
         
     |