Module: FreeImage::Conversions

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

Instance Method Summary collapse

Instance Method Details

#convert_to_16bits_555(&block) ⇒ Object

:call-seq:

image.convert_to_16bits_555 -> bitmap
image.convert_to_16bits_555 {|img| block} -> bitmap

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.

If an optional block is provided, it will be passed the new image as an argument. The image will be automatically closed when the block completes.



106
107
108
109
110
# File 'lib/free-image/modules/conversions.rb', line 106

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

#convert_to_16bits_565(&block) ⇒ Object

:call-seq:

image.convert_to_16bits_565 -> bitmap
image.convert_to_16bits_565 {|img| block} -> bitmap

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.

If an optional block is provided, it will be passed the new image as an argument. The image will be automatically closed when the block completes.



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

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

#convert_to_24bits(&block) ⇒ Object

:call-seq:

image.convert_to_24bits -> bitmap
image.convert_to_24bits {|img| block} -> bitmap

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.

If an optional block is provided, it will be passed the new image as an argument. The image will be automatically closed when the block completes.



140
141
142
143
144
# File 'lib/free-image/modules/conversions.rb', line 140

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

#convert_to_32bits(&block) ⇒ Object

:call-seq:

image.convert_to_32bits -> bitmap
image.convert_to_32bits {|img| block} -> bitmap

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.

If an optional block is provided, it will be passed the new image as an argument. The image will be automatically closed when the block completes.



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

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

#convert_to_4bits(&block) ⇒ Object

:call-seq:

image.convert_to_4bits -> bitmap
image.convert_to_4bits {|img| block} -> bitmap

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.

If an optional block is provided, it will be passed the new image as an argument. The image will be automatically closed when the block completes.



51
52
53
54
55
# File 'lib/free-image/modules/conversions.rb', line 51

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

#convert_to_8bits(&block) ⇒ Object

:call-seq:

image.convert_to_8bits -> bitmap
image.convert_to_8bits {|img| block} -> bitmap

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.

If an optional block is provided, it will be passed the new image as an argument. The image will be automatically closed when the block completes.



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

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

#convert_to_greyscale(&block) ⇒ Object

:call-seq:

image.convert_to_greyscale -> bitmap
image.convert_to_greyscale {|img| block} -> bitmap

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.

If an optional block is provided, it will be passed the new image as an argument. The image will be automatically closed when the block completes.



89
90
91
92
93
# File 'lib/free-image/modules/conversions.rb', line 89

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

#convert_to_standard_type(scale_linear = true, &block) ⇒ Object

:call-seq:

image.convert_to_standard_type(scale_linear = true) -> bitmap
image.convert_to_standard_type(scale_linear = true) {|img| block} -> bitmap

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.

If an optional block is provided, it will be passed the new image as an argument. The image will be automatically closed when the block completes.



186
187
188
189
190
# File 'lib/free-image/modules/conversions.rb', line 186

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

#convert_to_type(dst_image_type, scale_linear = true, &block) ⇒ Object

:call-seq:

image.convert_to_type(dst_image_type, scale_linear = true) -> bitmap
image.convert_to_type(dst_image_type, scale_linear = true) {|img| block} -> bitmap

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.

If an optional block is provided, it will be passed the new image as an argument. The image will be automatically closed when the block completes.



205
206
207
208
209
# File 'lib/free-image/modules/conversions.rb', line 205

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

#dither(algorithm, &block) ⇒ Object

:call-seq:

image.dither(algorithm) -> bitmap
image.dither(algorithm) {|img| block} -> bitmap

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.

If an optional block is provided, it will be passed the new image as an argument. The image will be automatically closed when the block completes.



224
225
226
227
228
# File 'lib/free-image/modules/conversions.rb', line 224

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

#threshold(value, &block) ⇒ Object

:call-seq:

image.threshold(value) -> bitmap
image.threshold(value) {|img| block} -> bitmap

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.

If an optional block is provided, it will be passed the new image as an argument. The image will be automatically closed when the block completes.



244
245
246
247
248
249
250
251
252
# File 'lib/free-image/modules/conversions.rb', line 244

def threshold(value, &block)
  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, &block)
end