Module: FreeImage::Conversions

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

Instance Method Summary collapse

Instance Method Details

#convert_to_16bits_555Object

Converts a bitmap to 16 bits, where each pixel has a color pattern of 5 bits red, 5 bits green and 5 bits blue. One bit in each pixel is unused.



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

def convert_to_16bits_555
  ptr = FreeImage.FreeImage_ConvertTo16Bits555(self)
  FreeImage.check_last_error
  self.class.new(ptr)
end

#convert_to_16bits_565Object

Converts a bitmap to 16 bits, where each pixel has a color pattern of 5 bits red, 6 bits green and 5 bits blue. One bit in each pixel is unused.



83
84
85
86
87
# File 'lib/free-image/modules/conversions.rb', line 83

def convert_to_16bits_565
  ptr = FreeImage.FreeImage_ConvertTo16Bits565(self)
  FreeImage.check_last_error
  self.class.new(ptr)
end

#convert_to_24bitsObject

Converts a bitmap to 24 bits. For 48-bit RGB images, conversion is done by dividing each 16-bit channel by 256. A nil value is returned for other non-standard bitmap types.



92
93
94
95
96
# File 'lib/free-image/modules/conversions.rb', line 92

def convert_to_24bits
  ptr = FreeImage.FreeImage_ConvertTo24Bits(self)
  FreeImage.check_last_error
  self.class.new(ptr)
end

#convert_to_32bitsObject

Converts a bitmap to 32 bits. For 48-bit RGB images, conversion is done by dividing each 16-bit channel by 256 and by setting the alpha channel to an opaque value (0xFF). For 64-bit RGBA images, conversion is done by dividing each 16-bit channel by 256. A nil value is returned for other non-standard bitmap types.



103
104
105
106
107
# File 'lib/free-image/modules/conversions.rb', line 103

def convert_to_32bits
  ptr = FreeImage.FreeImage_ConvertTo32Bits(self)
  FreeImage.check_last_error
  self.class.new(ptr)
end

#convert_to_4bitsObject

Converts a bitmap to 4 bits. If the bitmap is a high-color (16, 24 or 32-bit), monochrome or greyscale bitmap (1 or 8-bit) the end result will be a greyscale bitmap. A 1-bit bitmap will become a palletized bitmap.

Note that “greyscale” means that the resulting bitmap will have grey colors, but the palette won’t be a linear greyscale palette. Thus, FreeImage::Bitmap.color_type will return a :palette.



43
44
45
46
47
# File 'lib/free-image/modules/conversions.rb', line 43

def convert_to_4bits
  ptr = FreeImage.FreeImage_ConvertTo4Bits(self)
  FreeImage.check_last_error
  self.class.new(ptr)
end

#convert_to_8bitsObject

Converts a bitmap to 8 bits. If the bitmap is a high-color (16, 24 or 32-bit), monochrome or greyscale bitmap (1 or 4-bit) the end result will be a greyscale bitmap. A 1-bit or 4-bit bitmap will become a palletized bitmap.

For 16-bit greyscale images (images whose type is :uint16), conversion is done by dividing the 16-bit channel by 256 (see also FreeImage::Bitmap.ConvertToStandardType). A nil value is returned for other non-standard bitmap types.



56
57
58
59
60
# File 'lib/free-image/modules/conversions.rb', line 56

def convert_to_8bits
  ptr = FreeImage.FreeImage_ConvertTo8Bits(self)
  FreeImage.check_last_error
  self.class.new(ptr)
end

#convert_to_greyscaleObject

Converts a bitmap to a 8-bit greyscale image with a linear ramp. Contrary to the FreeImage::Conversions#convert_to_8bits function, 1-, 4- and 8-bit palletized bitmaps are correctly converted, as well as images with a :minis_white color type.



65
66
67
68
69
# File 'lib/free-image/modules/conversions.rb', line 65

def convert_to_greyscale
  ptr = FreeImage.FreeImage_ConvertToGreyscale(self)
  FreeImage.check_last_error
  self.class.new(ptr)
end

#convert_to_standard_type(scale_linear = true) ⇒ Object

Converts a non standard image whose color type is :minis_black to a standard 8-bit greyscale image. When the scale_linear parameter is true, conversion is done by scaling linearly each pixel value from [min, max] to an integer value between [0..255], where min and max are the minimum and maximum pixel values in the image. When scale_linear is false, conversion is done by rounding each pixel value to an integer between [0..255]. Rounding is done using the following formula:

dst_pixel = (BYTE) MIN(255, MAX(0, q)) where int q = int(src_pixel + 0.5)

For standard bitmaps, a clone of the original bitmap is returned. For complex images, the magnitude is extracted as a double image and then converted according to the scale parameter.



122
123
124
125
126
# File 'lib/free-image/modules/conversions.rb', line 122

def convert_to_standard_type(scale_linear = true)
  ptr = FreeImage.FreeImage_ConvertToStandardType(self, scale_linear)
  FreeImage.check_last_error
  self.class.new(ptr)
end

#convert_to_type(dst_image_type, scale_linear = true) ⇒ Object

Converts a bitmap to the specified destination image type. When the image_type is equal to :bitmap, the function calls FreeImage::Converstions#convert_to_standard_type. Otherwise, conversion is done using standard C language casting conventions. When a conversion is not allowed, a nil value is returned and an error is thrown. Please refer to the FreeImage documentation for allowed conversions.



133
134
135
136
137
# File 'lib/free-image/modules/conversions.rb', line 133

def convert_to_type(dst_image_type, scale_linear = true)
  ptr = FreeImage.FreeImage_ConvertToType(self, dst_image_type, scale_linear)
  FreeImage.check_last_error
  self.class.new(ptr)
end

#dither(algorithm) ⇒ Object

Converts a bitmap to 1-bit monochrome bitmap using the specified dithering algorithm. For 1-bit input bitmaps, the function clones the input bitmap and builds a monochrome palette. Otherwise the function first converts the bitmap to a 8-bit greyscale bitmap.



144
145
146
147
148
# File 'lib/free-image/modules/conversions.rb', line 144

def dither(algorithm)
  ptr = FreeImage.FreeImage_Dither(self, algorithm)
  FreeImage.check_last_error
  self.class.new(ptr)
end

#threshold(value) ⇒ Object

Converts a bitmap to 1-bit monochrome bitmap using a threshold value between 0 and 255. The function first converts the bitmap to a 8-bit greyscale bitmap. Then any brightness level that is less than the threshold is set to zero and any value above is set to 1. For 1-bit input bitmaps, the function clones the input bitmap and builds a monochrome palette.



156
157
158
159
160
161
162
163
164
# File 'lib/free-image/modules/conversions.rb', line 156

def threshold(value)
  value = Integer(value)
  unless (0..255).include?(value)
    raise(RangeError, "Value is out of range 0..255. Value: #{value}")
  end
  ptr = FreeImage.FreeImage_Threshold(self, value)
  FreeImage.check_last_error
  self.class.new(ptr)
end