Class: Echoes::SixelDecoder
- Inherits:
-
Object
- Object
- Echoes::SixelDecoder
- Defined in:
- lib/echoes/sixel_decoder.rb
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #decode(data) ⇒ Object
-
#initialize(params = []) ⇒ SixelDecoder
constructor
A new instance of SixelDecoder.
- #to_rgba ⇒ Object
Constructor Details
#initialize(params = []) ⇒ SixelDecoder
Returns a new instance of SixelDecoder.
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/echoes/sixel_decoder.rb', line 7 def initialize(params = []) @background_mode = params[1] || 0 @color_registers = default_color_registers @current_color = 0 @cursor_x = 0 @cursor_y = 0 @width = 0 @height = 0 @pixels = {} @declared_width = 0 @declared_height = 0 end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
5 6 7 |
# File 'lib/echoes/sixel_decoder.rb', line 5 def height @height end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
5 6 7 |
# File 'lib/echoes/sixel_decoder.rb', line 5 def width @width end |
Instance Method Details
#decode(data) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/echoes/sixel_decoder.rb', line 20 def decode(data) i = 0 len = data.bytesize while i < len byte = data.getbyte(i) case byte when 0x21 # '!' repeat i, count, ch = parse_repeat(data, i + 1) paint_sixel(ch, count) if ch when 0x22 # '"' raster attributes i, _, _, ph, pv = parse_raster_attributes(data, i + 1) @declared_width = ph if ph > 0 @declared_height = pv if pv > 0 when 0x23 # '#' color i = parse_color(data, i + 1) when 0x24 # '$' graphics CR @cursor_x = 0 i += 1 when 0x2D # '-' graphics newline @cursor_x = 0 @cursor_y += 6 i += 1 when 0x3F..0x7E # sixel character paint_sixel(byte, 1) i += 1 else i += 1 end end @width = @declared_width if @declared_width > @width @height = @declared_height if @declared_height > @height self end |
#to_rgba ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/echoes/sixel_decoder.rb', line 57 def to_rgba buf = "\x00".b * (@width * @height * 4) transparent = @background_mode == 1 @pixels.each do |(x, y), color| next if x >= @width || y >= @height offset = (y * @width + x) * 4 buf.setbyte(offset, color[0]) buf.setbyte(offset + 1, color[1]) buf.setbyte(offset + 2, color[2]) buf.setbyte(offset + 3, 255) end unless transparent # Set alpha=255 for unset pixels (background fill) (@width * @height).times do |i| offset = i * 4 buf.setbyte(offset + 3, 255) if buf.getbyte(offset + 3) == 0 end end buf end |