Class: Pixels::Targa32

Inherits:
TargaBase show all
Includes:
HasAlphaChannel
Defined in:
lib/pixels.rb

Instance Attribute Summary

Attributes inherited from TargaBase

#alpha_depth, #bpp, #color_depth, #height, #origin, #width

Instance Method Summary collapse

Methods included from HasAlphaChannel

#color_from_rgb, #has_alpha?, #rgb_from_color

Methods inherited from TargaBase

#close, #each_row_rgb, #each_row_rgba, #get_row_rgb, #get_row_rgba, #initialize, #put_row_rgb, #put_row_rgba, #read_row_bytes, #spec, #write_row_bytes

Constructor Details

This class inherits a constructor from Pixels::TargaBase

Instance Method Details

#color_from_rgba(r, g, b, a) ⇒ Object

Return a 32-bit integer pixel value given separate red, green, blue, and alpha values.

Each r, g, b, a value is an integer between 0 and 255.



625
626
627
628
629
630
631
# File 'lib/pixels.rb', line 625

def color_from_rgba(r, g, b, a)
  # Pack 8-bit-per-channel values
  return ((a.to_i & 0xff) << 24) |
         ((r.to_i & 0xff) << 16) |
         ((g.to_i & 0xff) << 8) |
         (b.to_i & 0xff)
end

#get_row(y) ⇒ Object

You probably want to use TargaBase#get_row_rgb or TargaBase#get_row_rgba instead.



595
596
597
598
599
600
601
602
# File 'lib/pixels.rb', line 595

def get_row(y)
  bytes = read_row_bytes(y)
  row = []
  for offset in (0..@width*@bytes_per_pixel-1).step(@bytes_per_pixel)
    row += bytes[offset,4].unpack("V")
  end
  return row
end

#put_row(y, row) ⇒ Object

You probably want to use TargaBase#put_row_rgb or TargaBase#put_row_rgba instead.



605
606
607
608
# File 'lib/pixels.rb', line 605

def put_row(y, row)
  bytes = row.pack("V" * row.length)
  write_row_bytes(y, bytes)
end

#rgba_from_color(color) ⇒ Object

Given a 16-bit integer colour value, return separate [r, g, b, a] values.

Each r, g, b, a value is an integer between 0 and 255.



613
614
615
616
617
618
619
620
# File 'lib/pixels.rb', line 613

def rgba_from_color(color)
  # Extract 8-bit-per-channel values
  b = color & 0xff
  g = (color >> 8) & 0xff
  r = (color >> 16) & 0xff
  a = (color >> 24) & 0xff
  return [r, g, b, a]
end