Module Pixels
In: lib/pixels.rb
Targa32 Targa16 TargaBase Targa24 Targa15 StandardError DataFormatError HasAlphaChannel NoAlphaChannel Pixels dot/f_2.png

Ruby Pixels allows you to read and write RGB/RGBA pixel data stored in uncompressed, non-interleaved TGA (Targa) files.

Unlike some other libraries, Ruby Pixels reads and writes one row of pixels at a time, you can work with several large images at once without running out of memory.

Requirements

Ruby Pixels needs no external libraries.

Limitations

Ruby Pixels cannot read or write compressed, interleaved, or colour-mapped images. You may wish to use another tool (e.g. MiniMagick) to convert to and from other formats.

Ruby Pixels currently has no support for reading or writing individual pixels. You need to do it on a row-by-row basis.

Example Code

  # invert-image.rb - Invert the colour of an image.
  require 'pixels'

  input = Pixels.open_tga("mm/mm-01.tga")
  output = Pixels.create_tga("output.tga", input.spec)

  input.each_row_rgb do |in_row, y|
    out_row = []
    for r, g, b in in_row
      out_row << [255-r, 255-g, 255-b]
    end
    output.put_row_rgb(y, out_row)
  end

  output.close
  input.close

Methods

Classes and Modules

Module Pixels::HasAlphaChannel
Module Pixels::NoAlphaChannel
Class Pixels::DataFormatError
Class Pixels::Targa15
Class Pixels::Targa16
Class Pixels::Targa24
Class Pixels::Targa32
Class Pixels::TargaBase

Constants

VERSION = '0.0.1'

Public Class methods

Create a TGA file according to the given specification, and return an instance of one of TargaBase‘s children (one of Targa15, Targa16, Targa24, or Targa32), depending on the format specification.

file_or_path may be a pathname or a file-like object. If it is a pathname, it is opened for reading.

spec is a Hash containing the following keys:

:width
Width of the image in pixels
:height
Height of the image in pixels
:color_depth
Color depth of the image in bits per pixel. This does not include the bits used to represent the alpha channel (if any).

Currently, this is always one of [15, 24].

:has_alpha
If the image has an alpha channel, this is true. Otherwise, it is false. (default false)
:origin
Specifies which pixel appears first in the TGA file. Must be one of :UPPER_LEFT or :LOWER_LEFT.

Note: This only affects the TGA file‘s internal layout. get_row_rgb(0) always returns the uppermost row in Ruby Pixels. (default :UPPER_LEFT)

Open the specified TGA file.

file_or_path may be a pathname or a file-like object. If it is a pathname, it is opened for reading.

Returns an instance of one of TargaBase‘s children (one of Targa15, Targa16, Targa24, or Targa32), depending on the format of the specified file.

[Validate]