Class: FreeImage::Bitmap

Inherits:
FFI::AutoPointer
  • Object
show all
Includes:
Conversions, ICC, Information, Modify, Pixels, Transforms
Defined in:
lib/free-image/bitmap.rb

Overview

Summary

Represents an image that has been loaded into memory. Once the image is loaded, you can get information about it, convert it, modify it. transform it,

Usage

# Open an image form a file
image = FreeImage::BitmapFile.open('test/fixtures/lena.png')

# Get information about it
bg = image.background_color

# Convert it
image = image.convert_to_greyscale

# Modify it
image = image.convert_to_greyscale

# Transform it
image = image.rotate(90)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Transforms

#flip_horizontal!, #flip_vertical!, #rotate, #rotate_ex

Methods included from Pixels

#bits, #pixel_color, #pixel_index, #scanline, #set_pixel_color, #set_pixel_index

Methods included from Modify

#composite, #composite_with_color, #copy, #enlarge_canvas, #fill_background!, #make_thumbnail, #paste!, #rescale

Methods included from Information

#background_color, #background_color=, #bits_per_pixel, #blue_mask, #color_type, #dib_size, #dots_per_meter_x, #dots_per_meter_x=, #dots_per_meter_y, #dots_per_meter_y=, #green_mask, #has_background_color, #has_pixels, #height, #image_type, #line, #palette, #pitch, #red_mask, #transparency_count, #transparent, #transparent=, #transparent_index, #transparent_index=, #width

Methods included from ICC

#icc_profile, #icc_profile=, #icc_supported?

Methods included from Conversions

#convert_to_16bits_555, #convert_to_16bits_565, #convert_to_24bits, #convert_to_32bits, #convert_to_4bits, #convert_to_8bits, #convert_to_greyscale, #convert_to_standard_type, #convert_to_type, #dither, #threshold

Constructor Details

#initialize(ptr, source = nil) ⇒ Bitmap

Creates a new image from a c-pointer an image source. Generally you do not want to use this method. Instead, use Bitmap.open.



110
111
112
113
# File 'lib/free-image/bitmap.rb', line 110

def initialize(ptr, source = nil)
  @source = source
  super(ptr)
end

Instance Attribute Details

#sourceObject (readonly)

The source of the image. May be a File, in Memory string, IO stream or nil.



46
47
48
# File 'lib/free-image/bitmap.rb', line 46

def source
  @source
end

Class Method Details

.create(width, height, bits_per_pixel, red_mask = 0, green_mask = 0, blue_mask = 0) ⇒ Object

Creates a new image with the specified width, height and bits per pixel.

Parameters

width

The width of the new image

height

The height of the new image

bits_per_pixel

The size in bits of a pixel

red_mask

The bit-layout of the red color component in a bitmap

green_mask:P: The bit-layout of the green color component in a bitmap

blue_mask

The bit-layout of the blue color component in a bitmap

The last three parameter are used to tell FreeImage the bit-layout of the color components in the bitmap, e.g. where in a pixel the red, green and blue components are stored.

To give you an idea about how to interpret the color masks: when red_mask is 0xFF000000 this means that the last 8 bits in one pixel are used for the color red. When green_mask is 0x000000FF, it means that the first 8 bits in a pixel are used for the color green.

The new image is initially filled completely with zeroes. Zero in a image is usually interpreted as black. This means that if your bitmap is palletized it will contain a completely black palette. You can access, and hence populate the palette via #palette.

For 8-bit images, a default greyscale palette will also be created.



74
75
76
77
78
# File 'lib/free-image/bitmap.rb', line 74

def self.create(width, height, bits_per_pixel, red_mask = 0, green_mask = 0, blue_mask = 0)
  ptr = FreeImage.FreeImage_Allocate(width, height, bits_per_pixel, red_mask, green_mask, blue_mask)
  FreeImage.check_last_error
  new(ptr)
end

.open(source) ⇒ Object

Opens a new image from the specified source.

Parameters

source

The source of the image

The source parameter can be a File, Memory or IO stream. It can also be a string which is interpreted as a fully qualified file path.



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/free-image/bitmap.rb', line 87

def self.open(source)
  bitmap = figure_source(source).open
  unless block_given?
    bitmap
  else
    begin
      yield bitmap
    ensure
      bitmap.free
    end
  end
end

.release(ptr) ⇒ Object

Closes an image and releases its associated memory. This is called by the ruby Garbage collector and should not be called directly. If you would like to free an image after using it, please use Bitmap#free.



104
105
106
# File 'lib/free-image/bitmap.rb', line 104

def self.release(ptr) #:nodoc:
  FreeImage.FreeImage_Unload(ptr)
end

Instance Method Details

#cloneObject

Creates an exact copy of the current image.



116
117
118
119
120
# File 'lib/free-image/bitmap.rb', line 116

def clone
  ptr = FreeImage.FreeImage_Clone(self)
  FreeImage.check_last_error
  self.class.new(ptr)
end

#save(source, format, flags = 0) ⇒ Object

Save the image to the specified source.

Parameters

dst

The destination where the image will be saved.

format

The format to save the image to.

flags

Format specific flags that control how a bitmap is saved. These flags are defined

as constants on the AbstractSource class.  Flags can be combined using
Ruby's bitwise or operator (|)


131
132
133
# File 'lib/free-image/bitmap.rb', line 131

def save(source, format, flags = 0)
  self.class.figure_source(source).save(self, format, flags)
end