Class: BrotherEscp::ImageConverter
- Inherits:
-
Object
- Object
- BrotherEscp::ImageConverter
- 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
-
#density_code ⇒ Object
readonly
Returns the value of attribute density_code.
-
#line_height_in_bytes ⇒ Object
readonly
Returns the value of attribute line_height_in_bytes.
-
#line_height_in_pixels ⇒ Object
readonly
Returns the value of attribute line_height_in_pixels.
Class Method Summary collapse
-
.converter(density: :single_density) ⇒ BrotherEscp::ImageConverter
Create a new instance of an image.
Instance Method Summary collapse
-
#convert(image:) ⇒ Array
Main conversion method from image data to printer bitmap format.
-
#convert_line_to_escp(line:) ⇒ Object
Convert a line array to a raw data.
-
#initialize(density_code: 0, line_height_in_bytes: 1) ⇒ ImageConverter
constructor
A new instance of ImageConverter.
Constructor Details
#initialize(density_code: 0, line_height_in_bytes: 1) ⇒ ImageConverter
Returns a new instance of ImageConverter.
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_code ⇒ Object (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_bytes ⇒ Object (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_pixels ⇒ Object (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
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
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
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 |