Class: Pixels::Targa16

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 16-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.



539
540
541
542
543
544
545
546
# File 'lib/pixels.rb', line 539

def color_from_rgba(r, g, b, a)
  # Convert 8 bits-per-channel to 5 bits-per-channel
  r5 = (r.to_i >> 3) & 0x1f
  g5 = (g.to_i >> 3) & 0x1f
  b5 = (b.to_i >> 3) & 0x1f
  a1 = (a.to_i >> 7) & 1
  return (a1 << 15) | (b5 << 10) | (g5 << 5) | r5
end

#get_row(y) ⇒ Object

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



502
503
504
505
506
507
508
509
510
# File 'lib/pixels.rb', line 502

def get_row(y)
  bytes = read_row_bytes(y)
  row = []
  for offset in (0..@width*@bytes_per_pixel-1).step(@bytes_per_pixel)
    v, = bytes[offset,2].unpack("v")
    row << 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.



513
514
515
516
# File 'lib/pixels.rb', line 513

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.



521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/pixels.rb', line 521

def rgba_from_color(color)
  # Extract 5 bits-per-channel values
  b5 = color & 0x1f
  g5 = (color >> 5) & 0x1f
  r5 = (color >> 10) & 0x1f
  a1 = (color >> 15) & 1

  # Convert 5 bits-per-channel to 8 bits-per-channel
  r8 = r5 * 255 / 31
  g8 = g5 * 255 / 31
  b8 = b5 * 255 / 31
  a8 = (a1 > 0) ? 255 : 0
  return [r8, g8, b8, a8]
end