Module: FreeImage::Pixels

Included in:
Bitmap
Defined in:
lib/free-image/modules/pixels.rb

Overview

Summary

The Pixel module provides methods that allow you to read, write and work pixel-by-pixel with image data. FreeImage not only can work with standard bitmap data (e.g. 1-, 4-, 8-, 16-, 24- and 32-bit) but also with scientific data such as 16-bit greyscale images, or images made up of long, double or complex values (often used in signal and image processing algorithms).

The FreeImage coordinate system is upside down relative to usual graphics conventions. Thus, the scanlines are stored upside down, with the first scan in memory being the bottommost scan in the image.

For additional information, please refer to the FreeImage::Scanline documentation.

Instance Method Summary collapse

Instance Method Details

#bitsObject

Returns the data-bits of the bitmap. It is up to you to interpret these bytes correctly, according to the results of Information#bits_per_pixel, Information#red_mask, Information#green_mask and Information#blue_mask.

If the bitmap does not contain pixel data (see Information#has_pixels), nil will be returned.

For a performance reason, the address returned by FreeImage_GetBits is aligned on a 16 bytes alignment boundary



45
46
47
48
49
# File 'lib/free-image/modules/pixels.rb', line 45

def bits
  ptr = FreeImage.FreeImage_GetBits(self)
  FreeImage.check_last_error
  ptr.read_string
end

#pixel_color(x, y) ⇒ Object

Gets the pixel color of a 16-, 24- or 32-bit image at the specified coordinate.

Parameters

x

The pixel position in horizontal direction

y

The pixel position in vertical direction.



113
114
115
116
117
118
# File 'lib/free-image/modules/pixels.rb', line 113

def pixel_color(x, y)
  color = RGBQuad.new
  result = FreeImage.FreeImage_GetPixelColor(self, x, y, color)
  FreeImage.check_last_error
  result ? color : nil
end

#pixel_index(x, y) ⇒ Object

Gets the pixel index of a palettized image at the specified coordinate.

Parameters

x

The pixel position in horizontal direction

y

The pixel position in vertical direction.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/free-image/modules/pixels.rb', line 71

def pixel_index(x, y)
  byte_type = FreeImage.find_type(:byte)
  ptr = FFI::MemoryPointer.new(byte_type)
  result = FreeImage.FreeImage_GetPixelIndex(self, x, y, ptr)
  FreeImage.check_last_error
  return nil unless result
  
  data = ptr.read_bytes(byte_type.size)
  if byte_type.size == 1
    data.ord
  else
    data
  end
end

#scanline(index) ⇒ Object

Returns the requested row of image data as a FreeImage::Scanline instance.

If the bitmap does not contain pixel data (see Information#has_pixels), nil will be returned.



55
56
57
58
59
60
61
62
63
# File 'lib/free-image/modules/pixels.rb', line 55

def scanline(index)
  unless (0...self.height).include?(index)
    raise(RangeError, "Index must be between 0 and #{self.height - 1}")
  end
  ptr = FreeImage.FreeImage_GetScanLine(self, index)
  FreeImage.check_last_error

  ptr ? Scanline.new(self, index, ptr) : nil
end

#set_pixel_color(x, y, color) ⇒ Object

Sets the pixel color of a 16-, 24- or 32-bit image at the specified coordinate.

Parameters

x

The pixel position in horizontal direction

y

The pixel position in vertical direction.

color

The new color as a RGBAQuad instance.

The function returns true on success and false otherwise.



129
130
131
132
133
# File 'lib/free-image/modules/pixels.rb', line 129

def set_pixel_color(x, y, color)
  result = FreeImage.FreeImage_SetPixelColor(self, x, y, color)
  FreeImage.check_last_error
  result
end

#set_pixel_index(x, y, index) ⇒ Object

Sets the pixel index of a palettized image at the specified coordinate.

Parameters

x

The pixel position in horizontal direction

y

The pixel position in vertical direction.

The function returns true on success and false otherwise.



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/free-image/modules/pixels.rb', line 94

def set_pixel_index(x, y, index)
  byte_type = FreeImage.find_type(:byte)
  ptr = FFI::MemoryPointer.new(byte_type.size)
  if byte_type.size == 1
    ptr.put_bytes(0, index.chr, 0, byte_type.size)
  else
    ptr.put_bytes(0, index.to_s, 0, byte_type.size)
  end
  result = FreeImage.FreeImage_SetPixelIndex(self, x, y, ptr)
  FreeImage.check_last_error
  result
end