Class: BrotherEscp::ImageConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/brother_escp/image_converter.rb

Overview

Image conversion logic

Constant Summary collapse

CONVERTER_PARAMS =

printer parameters for each supported density

{
  single_density: { density_code: 0,   line_height_in_bytes: 1 },
  high_density:   { density_code: 39,  line_height_in_bytes: 3 },
  higher_density: { density_code: 72,  line_height_in_bytes: 6 }
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(density_code: 0, line_height_in_bytes: 1) ⇒ ImageConverter

Returns a new instance of ImageConverter.

Parameters:

  • density_code (Integer) (defaults to: 0)

    density code

  • line_height_in_bytes (Integer) (defaults to: 1)

    number of bytes by line



27
28
29
30
31
# File 'lib/brother_escp/image_converter.rb', line 27

def initialize(density_code: 0, line_height_in_bytes: 1)
  @density_code = density_code
  @line_height_in_bytes = line_height_in_bytes
  @line_height_in_pixels = line_height_in_bytes * 8
end

Instance Attribute Details

#density_codeObject (readonly)

Returns the value of attribute density_code.



21
22
23
# File 'lib/brother_escp/image_converter.rb', line 21

def density_code
  @density_code
end

#line_height_in_bytesObject (readonly)

Returns the value of attribute line_height_in_bytes.



22
23
24
# File 'lib/brother_escp/image_converter.rb', line 22

def line_height_in_bytes
  @line_height_in_bytes
end

#line_height_in_pixelsObject (readonly)

Returns the value of attribute line_height_in_pixels.



23
24
25
# File 'lib/brother_escp/image_converter.rb', line 23

def line_height_in_pixels
  @line_height_in_pixels
end

Class Method Details

.converter(density: :single_density) ⇒ BrotherEscp::ImageConverter

Create a new instance of an image

Parameters:

  • density (Symbol) (defaults to: :single_density)

    set the image density, default to single_density, possible values are: :single_density, :high_density, :higher_density

Returns:



15
16
17
18
19
# File 'lib/brother_escp/image_converter.rb', line 15

def self.converter(density: :single_density)
  params = CONVERTER_PARAMS[density]
  raise "Unknown converter (#{density})" unless params
  new(density_code: params[:density_code], line_height_in_bytes: params[:line_height_in_bytes])
end

Instance Method Details

#convert(image:) ⇒ Array

Main conversion method from image data to printer bitmap format

Parameters:

  • image (ChunkyPNG::Image)

    Image to convert

Returns:

  • (Array)

    return an array of array, one array for each line, with one element for each byte



36
37
38
39
40
41
42
43
44
45
# File 'lib/brother_escp/image_converter.rb', line 36

def convert(image:)
  lines      = []
  line_count = line_count(image: image)
  BrotherEscp.logger.debug "convert, line_count = #{line_count}"
  0.upto(line_count - 1) do |line_index|
    lines << create_line(image: image, line_index: line_index)
  end
  BrotherEscp.logger.debug "convert, size by line: #{lines.map(&:size)}, total: #{lines.map(&:size).inject(:+)}"
  lines
end

#convert_line_to_escp(line:) ⇒ Object

Convert a line array to a raw data

Parameters:

  • line (Array)

    Array of bytes representing one line



49
50
51
52
53
54
55
56
# File 'lib/brother_escp/image_converter.rb', line 49

def convert_line_to_escp(line:)
  n1 = line.length / line_height_in_bytes % 256
  n2 = line.length / line_height_in_bytes / 256
  BrotherEscp.logger.debug "convert_line_to_escp, length = #{line.length}, n1 = #{n1}, n2 = #{n2}"
  data = [0x1b, 0x2a, density_code, n1, n2]
  data += line
  data.pack('C*')
end