free-image 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. data/HISTORY +5 -0
  2. data/LICENSE +21 -0
  3. data/README.rdoc +99 -0
  4. data/Rakefile +47 -0
  5. data/cookbook.rdoc +239 -0
  6. data/free-image.gemspec +30 -0
  7. data/lib/free-image.rb +101 -0
  8. data/lib/free-image/bitmap.rb +154 -0
  9. data/lib/free-image/enums/color_types.rb +24 -0
  10. data/lib/free-image/enums/dithers.rb +24 -0
  11. data/lib/free-image/enums/filters.rb +12 -0
  12. data/lib/free-image/enums/formats.rb +84 -0
  13. data/lib/free-image/enums/image_types.rb +36 -0
  14. data/lib/free-image/errors.rb +44 -0
  15. data/lib/free-image/modules/conversions.rb +166 -0
  16. data/lib/free-image/modules/helper.rb +42 -0
  17. data/lib/free-image/modules/icc.rb +41 -0
  18. data/lib/free-image/modules/information.rb +305 -0
  19. data/lib/free-image/modules/modify.rb +261 -0
  20. data/lib/free-image/modules/pixels.rb +135 -0
  21. data/lib/free-image/modules/transforms.rb +83 -0
  22. data/lib/free-image/palette.rb +44 -0
  23. data/lib/free-image/scanline.rb +151 -0
  24. data/lib/free-image/sources/abstract_source.rb +172 -0
  25. data/lib/free-image/sources/file.rb +115 -0
  26. data/lib/free-image/sources/io.rb +154 -0
  27. data/lib/free-image/sources/memory.rb +189 -0
  28. data/lib/free-image/types/boolean.rb +14 -0
  29. data/lib/free-image/types/complex.rb +9 -0
  30. data/lib/free-image/types/ffi.rb +14 -0
  31. data/lib/free-image/types/rgb16.rb +11 -0
  32. data/lib/free-image/types/rgb_quad.rb +82 -0
  33. data/lib/free-image/types/rgb_triple.rb +42 -0
  34. data/lib/free-image/types/rgba16.rb +12 -0
  35. data/lib/free-image/types/rgbaf.rb +12 -0
  36. data/lib/free-image/types/rgbf.rb +11 -0
  37. data/test/cookbook.rb +46 -0
  38. data/test/images/gradient.png +0 -0
  39. data/test/images/lena.png +0 -0
  40. data/test/images/lena.tiff +0 -0
  41. data/test/images/lena_flipped.png +0 -0
  42. data/test/images/lena_rescale_bicubic.png +0 -0
  43. data/test/images/lena_rescale_bilinear.png +0 -0
  44. data/test/images/lena_rescale_box.png +0 -0
  45. data/test/images/lena_rescale_bspline.png +0 -0
  46. data/test/images/lena_rescale_catmullrom.png +0 -0
  47. data/test/images/lena_rescale_lanczos3.png +0 -0
  48. data/test/images/lena_rotate_45.png +0 -0
  49. data/test/images/lena_rotate_ex_45_masked.png +0 -0
  50. data/test/images/lena_rotate_ex_45_mirrored.png +0 -0
  51. data/test/images/lena_rotate_ex_45_top_left.png +0 -0
  52. data/test/images/lena_thumbnail.png +0 -0
  53. data/test/images/lena_thumbnail_border_enlarge.png +0 -0
  54. data/test/images/lena_thumbnail_border_paste.png +0 -0
  55. data/test/images/lena_thumbnail_border_scanline.png +0 -0
  56. data/test/images/not_an_image.txt +0 -0
  57. data/test/images/sample.png +0 -0
  58. data/test/images/sample_composite_color.png +0 -0
  59. data/test/test_bitmap.rb +43 -0
  60. data/test/test_conversions.rb +86 -0
  61. data/test/test_file.rb +51 -0
  62. data/test/test_free_image.rb +15 -0
  63. data/test/test_helper.rb +35 -0
  64. data/test/test_information.rb +118 -0
  65. data/test/test_io.rb +53 -0
  66. data/test/test_memory.rb +65 -0
  67. data/test/test_modify.rb +59 -0
  68. data/test/test_palette.rb +45 -0
  69. data/test/test_pixels.rb +62 -0
  70. data/test/test_rgb_quad.rb +26 -0
  71. data/test/test_scanline.rb +65 -0
  72. data/test/test_suite.rb +19 -0
  73. data/test/test_transforms.rb +30 -0
  74. metadata +169 -0
@@ -0,0 +1,154 @@
1
+ module FreeImage
2
+ #DLL_API FIBITMAP *DLL_CALLCONV FreeImage_Allocate(int width, int height, int bpp, unsigned red_mask FI_DEFAULT(0), unsigned green_mask FI_DEFAULT(0), unsigned blue_mask FI_DEFAULT(0));
3
+ attach_function('FreeImage_Allocate', [:int, :int, :int, :uint, :uint, :uint], :pointer)
4
+
5
+ # DLL_API FIBITMAP * DLL_CALLCONV FreeImage_Clone(FIBITMAP *dib);
6
+ attach_function('FreeImage_Clone', [:pointer], :pointer)
7
+
8
+ # DLL_API void DLL_CALLCONV FreeImage_Unload(FIBITMAP *dib);
9
+ attach_function('FreeImage_Unload', [:pointer], :void)
10
+
11
+ # == Summary
12
+ #
13
+ # Represents an image that has been loaded into memory. Once the image is loaded,
14
+ # you can get
15
+ # information[rdoc-ref:FreeImage::Information] about it,
16
+ # convert[rdoc-ref:FreeImage::Conversions] it,
17
+ # modify[rdoc-ref:FreeImage::Modify] it.
18
+ # transform[rdoc-ref:FreeImage::Information] it,
19
+ #
20
+ # == Usage
21
+ #
22
+ # # Open an image form a file
23
+ # image = FreeImage::BitmapFile.open('test/fixtures/lena.png')
24
+ #
25
+ # # Get information about it
26
+ # bg = image.background_color
27
+ #
28
+ # # Convert it
29
+ # image = image.convert_to_greyscale
30
+ #
31
+ # # Modify it
32
+ # image = image.convert_to_greyscale
33
+ #
34
+ # # Transform it
35
+ # image = image.rotate(90)
36
+ #
37
+ class Bitmap < FFI::AutoPointer
38
+ include Conversions
39
+ include ICC
40
+ include Information
41
+ include Modify
42
+ include Pixels
43
+ include Transforms
44
+
45
+ ## The source of the image. May be a File, in Memory string, IO stream or nil.
46
+ attr_reader :source
47
+
48
+ # Creates a new image with the specified width, height and bits per pixel.
49
+ #
50
+ # == Parameters
51
+ # width:: The width of the new image
52
+ # height:: The height of the new image
53
+ # bits_per_pixel:: The size in bits of a pixel
54
+ # red_mask:: The bit-layout of the red color component in a bitmap
55
+ # green_mask:P: The bit-layout of the green color component in a bitmap
56
+ # blue_mask:: The bit-layout of the blue color component in a bitmap
57
+ #
58
+ # The last three parameter are used to tell FreeImage the bit-layout of
59
+ # the color components in the bitmap, e.g. where in a pixel the red,
60
+ # green and blue components are stored.
61
+ #
62
+ # To give you an idea about how to interpret the color masks:
63
+ # when red_mask is 0xFF000000 this means that the last 8 bits in
64
+ # one pixel are used for the color red. When green_mask is 0x000000FF, it
65
+ # means that the first 8 bits in a pixel are used for the color green.
66
+ #
67
+ # The new image is initially filled completely with zeroes. Zero in a image
68
+ # is usually interpreted as black. This means that if your bitmap is palletized
69
+ # it will contain a completely black palette. You can access, and hence populate
70
+ # the palette via #palette.
71
+ #
72
+ # For 8-bit images, a default greyscale palette will also be created.
73
+ #
74
+ def self.create(width, height, bits_per_pixel, red_mask = 0, green_mask = 0, blue_mask = 0)
75
+ ptr = FreeImage.FreeImage_Allocate(width, height, bits_per_pixel, red_mask, green_mask, blue_mask)
76
+ FreeImage.check_last_error
77
+ new(ptr)
78
+ end
79
+
80
+ # Opens a new image from the specified source.
81
+ #
82
+ # == Parameters
83
+ # source:: The source of the image
84
+ #
85
+ # The source parameter can be a File, Memory or IO stream. It can
86
+ # also be a string which is interpreted as a fully qualified file path.
87
+ def self.open(source)
88
+ bitmap = figure_source(source).open
89
+ unless block_given?
90
+ bitmap
91
+ else
92
+ begin
93
+ yield bitmap
94
+ ensure
95
+ bitmap.free
96
+ end
97
+ end
98
+ end
99
+
100
+ # Closes an image and releases its associated memory. This
101
+ # is called by the ruby Garbage collector and should not
102
+ # be called directly. If you would like to free an image
103
+ # after using it, please use Bitmap#free.
104
+ def self.release(ptr) #:nodoc:
105
+ FreeImage.FreeImage_Unload(ptr)
106
+ end
107
+
108
+ # Creates a new image from a c-pointer an image source. Generally you
109
+ # do not want to use this method. Instead, use Bitmap.open.
110
+ def initialize(ptr, source = nil)
111
+ @source = source
112
+ super(ptr)
113
+ end
114
+
115
+ # Creates an exact copy of the current image.
116
+ def clone
117
+ ptr = FreeImage.FreeImage_Clone(self)
118
+ FreeImage.check_last_error
119
+ self.class.new(ptr)
120
+ end
121
+
122
+ # Save the image to the specified source.
123
+ #
124
+ # == Parameters
125
+ # dst:: The destination where the image will be saved.
126
+ # format:: The format[rdoc-ref:FreeImage.formats] to save the image to.
127
+ # flags:: Format specific flags that control how a bitmap is saved. These flags are defined
128
+ # as constants on the AbstractSource class. Flags can be combined using
129
+ # Ruby's bitwise or operator (|)
130
+ #
131
+ def save(source, format, flags = 0)
132
+ self.class.figure_source(source).save(self, format, flags)
133
+ end
134
+
135
+ private
136
+
137
+ def self.figure_source(source)
138
+ case source
139
+ when AbstractSource
140
+ source
141
+ when ::IO
142
+ IO.new(source)
143
+ when String
144
+ File.new(source)
145
+ else
146
+ raise(ArgumentError, "Unknown source type: #{source.class.name}")
147
+ end
148
+ end
149
+
150
+ def figure_color_klass
151
+
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: UTF-8
2
+
3
+ module FreeImage
4
+ ##
5
+ # FreeImage supports the following color types:
6
+ #
7
+ # :minis_black:: Monochrome bitmap (1-bit) -> first palette entry is black.
8
+ # Palletised bitmap (4 or 8-bit) and single channel non standard bitmap -> the bitmap has a greyscale palette
9
+ # :minis_white:: Monochrome bitmap (1-bit) -> first palette entry is white.
10
+ # Palletised bitmap (4 or 8-bit) and single channel non standard bitmap -> the bitmap has an inverted greyscale palette
11
+ # :palette:: Palettized bitmap (1, 4 or 8 bit)
12
+ # :rgb:: High-color bitmap (16, 24 or 32 bit), RGB16 or RGBF
13
+ # :rgb_alpha:: High-color bitmap with an alpha channel (32 bit bitmap, RGBA16 or RGBAF)
14
+ # :cmyk:: CMYK bitmap (32 bit only)
15
+ #
16
+ # :method: color_types
17
+
18
+ FreeImage.enum :color_type, [:minis_white, 0,
19
+ :minis_black, 1,
20
+ :rgb, 2,
21
+ :palette, 3,
22
+ :rgb_alpha, 4,
23
+ :cmyk, 5]
24
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: UTF-8
2
+
3
+ module FreeImage
4
+ ##
5
+ # FreeImage supports the following dithering algorithms:
6
+ #
7
+ # :fs:: Floyd & Steinberg error diffusion algorithm
8
+ # :bayer4x4:: Bayer ordered dispersed dot dithering (order 2 – 4x4 -dithering matrix)
9
+ # :bayer8x8:: Bayer ordered dispersed dot dithering (order 3 – 8x8 -dithering matrix)
10
+ # :bayer16x16:: Bayer ordered dispersed dot dithering (order 4 – 16x16 dithering matrix)
11
+ # :cluster6x6:: Ordered clustered dot dithering (order 3 - 6x6 matrix)
12
+ # :cluster8x8:: Ordered clustered dot dithering (order 4 - 8x8 matrix)
13
+ # :cluster16x16:: Ordered clustered dot dithering (order 8 - 16x16 matrix)
14
+ #
15
+ # :method: dithers
16
+
17
+ FreeImage.enum :dither, [:fs, 0,
18
+ :bayer4x4, 1,
19
+ :bayer8x8, 2,
20
+ :cluster6x6, 3,
21
+ :cluster8x8, 4,
22
+ :cluster16x16, 5,
23
+ :bayer16x16, 6]
24
+ end
@@ -0,0 +1,12 @@
1
+ module FreeImage
2
+ ##
3
+ #FreeImage supports the following rescaling filters:
4
+ #
5
+ # :method: filters
6
+ enum :filter, [:box, 0,
7
+ :bicubic, 1,
8
+ :bilinear, 2,
9
+ :bspline, 3,
10
+ :catmullrom, 4,
11
+ :lanczos3, 5]
12
+ end
@@ -0,0 +1,84 @@
1
+ # encoding: UTF-8
2
+
3
+ module FreeImage
4
+ ##
5
+ # FreeImage supports over 30 bitmap types, which are indentified via symbols.
6
+ # Supported formats include:
7
+ #
8
+ # :bmp:: Windows or OS/2 Bitmap File (*.BMP)
9
+ # :cut:: Dr. Halo (*.CUT)
10
+ # :dds:: DirectDraw Surface (*.DDS)
11
+ # :exr:: ILM OpenEXR (*.EXR)
12
+ # :faxg3:: Raw Fax format CCITT G3 (*.G3)
13
+ # :gif:: Graphics Interchange Format (*.GIF)
14
+ # :hdr:: High Dynamic Range (*.HDR)
15
+ # :ico:: Windows Icon (*.ICO)
16
+ # :iff:: Amiga IFF (*.IFF, *.LBM)
17
+ # :jpeg:: Independent JPEG Group (*.JPG, *.JIF, *.JPEG, *.JPE)
18
+ # :jng:: JPEG Network Graphics (*.JNG)
19
+ # :j2k:: JPEG-2000 codestream (*.J2K, *.J2C)
20
+ # :jp2:: JPEG-2000 File Format (*.JP2)
21
+ # :koala:: Commodore 64 Koala format (*.KOA)
22
+ # :lbm:: Amiga IFF (*.IFF, *.LBM)
23
+ # :mng:: Multiple Network Graphics (*.MNG)
24
+ # :pbm:: Portable Bitmap (ASCII) (*.PBM)
25
+ # :pbmraw:: Portable Bitmap (BINARY) (*.PBM)
26
+ # :pcd:: Kodak PhotoCD (*.PCD)
27
+ # :pcsx:: Zsoft Paintbrush PCX bitmap format (*.PCX)
28
+ # :pfm:: Portable Floatmap (*.PFM)
29
+ # :pgm:: Portable Graymap (ASCII) (*.PGM)
30
+ # :pgmraw:: Portable Graymap (BINARY) (*.PGM)
31
+ # :pict:: Macintosh PICT (*.PCT, *.PICT, *.PIC)
32
+ # :png:: Portable Network Graphics (*.PNG)
33
+ # :ppm:: Portable Pixelmap (ASCII) (*.PPM)
34
+ # :ppmraw:: Portable Pixelmap (BINARY) (*.PPM)
35
+ # :psd:: Adobe Photoshop (*.PSD)
36
+ # :ras:: Sun Rasterfile (*.RAS)
37
+ # :raw:: RAW camera image (many extensions)
38
+ # :sgi:: Silicon Graphics SGI image format (*.SGI)
39
+ # :targa:: Truevision Targa files (*.TGA, *.TARGA)
40
+ # :tiff:: Tagged Image File Format (*.TIF, *.TIFF)
41
+ # :wbmp:: Wireless Bitmap (*.WBMP)
42
+ # :xbm:: X11 Bitmap Format (*.XBM)
43
+ # :xpm:: X11 Pitmap Format (*.XPM)
44
+ #
45
+ # :method: formats
46
+
47
+ FreeImage.enum :format, [:unknown, -1,
48
+ :bmp, 0,
49
+ :ico , 1,
50
+ :jpeg, 2,
51
+ :jng , 3,
52
+ :koala, 4,
53
+ :lbm, 5,
54
+ :iff, 5, # Yes this is intentional!
55
+ :mng, 6,
56
+ :pbm, 7,
57
+ :pbmraw, 8,
58
+ :pcd, 9,
59
+ :pcsx, 10,
60
+ :pgm, 11,
61
+ :pgmraw, 12,
62
+ :png, 13,
63
+ :ppm, 14,
64
+ :ppmraw, 15,
65
+ :ras, 16,
66
+ :targa, 17,
67
+ :tiff, 18,
68
+ :wbmp, 19,
69
+ :psd, 20,
70
+ :cut, 21,
71
+ :xbm, 22,
72
+ :xpm, 23,
73
+ :dds, 24,
74
+ :gif, 25,
75
+ :hdr, 26,
76
+ :faxg3, 27,
77
+ :sgi, 28,
78
+ :exr, 29,
79
+ :j2k, 30,
80
+ :jp2, 31,
81
+ :pfm, 32,
82
+ :pict, 33,
83
+ :raw, 34]
84
+ end
@@ -0,0 +1,36 @@
1
+ # encoding: UTF-8
2
+
3
+ module FreeImage
4
+ ##
5
+ # FreeImage supports the following image types:
6
+ #
7
+ # :unknown:: Unknown format (returned value only, never use it as input value)
8
+ # :bitmap:: Standard image: 1-, 4-, 8-, 16-, 24-, 32-bit
9
+ # :uint16:: Array of unsigned short: unsigned 16-bit
10
+ # :int16:: Array of short: signed 16-bit
11
+ # :uint32:: Array of unsigned long: unsigned 32-bit
12
+ # :int32:: Array of long: signed 32-bit
13
+ # :float:: Array of float: 32-bit IEEE floating point
14
+ # :double:: Array of double: 64-bit IEEE floating point
15
+ # :complex:: Array of FICOMPLEX: 2 x 64-bit IEEE floating point
16
+ # :rgb16:: 48-bit rgb image: 3 x 16-bit
17
+ # :rgba16:: 64-bit rgbA image: 4 x 16-bit
18
+ # :rgbf:: 96-bit rgb float image: 3 x 32-bit IEEE floating point
19
+ # :rgbaf:: 128-bit rgbA float image: 4 x 32-bit IEEE floating point
20
+ #
21
+ # :method: image_types
22
+
23
+ FreeImage.enum :image_type, [:unknown,
24
+ :bitmap,
25
+ :uint16,
26
+ :int16,
27
+ :uint32,
28
+ :int32,
29
+ :float,
30
+ :double,
31
+ :complex,
32
+ :rgb16,
33
+ :rgba16,
34
+ :rgbf,
35
+ :rgbaf]
36
+ end
@@ -0,0 +1,44 @@
1
+ module FreeImage
2
+ LAST_ERROR = 'free_image_error'
3
+
4
+ #typedef void (*FreeImage_OutputMessageFunction)(FREE_IMAGE_FORMAT fif, const char *msg);
5
+ #typedef void (DLL_CALLCONV *FreeImage_OutputMessageFunctionStdCall)(FREE_IMAGE_FORMAT fif, const char *msg);
6
+ callback(:output_message_callback, [:format, :pointer], :void)
7
+
8
+ if FFI::Platform.windows?
9
+ #DLL_API void DLL_CALLCONV FreeImage_SetOutputMessageStdCall(FreeImage_OutputMessageFunctionStdCall omf);
10
+ attach_function('FreeImage_SetOutputMessage', 'FreeImage_SetOutputMessageStdCall', [:output_message_callback], :void)
11
+ else
12
+ #DLL_API void DLL_CALLCONV FreeImage_SetOutputMessage(FreeImage_OutputMessageFunction omf);
13
+ attach_function('FreeImage_SetOutputMessage', [:output_message_callback], :void)
14
+ end
15
+
16
+ class Error < StandardError
17
+ attr_reader :format
18
+
19
+ def initialize(format, message)
20
+ @format = format
21
+ super(message)
22
+ end
23
+
24
+ # def to_s
25
+ # "#{self.message} Format: #{self.format}"
26
+ # end
27
+ end
28
+
29
+ CALLBACK = Proc.new do |format, ptr|
30
+ # Create an exception object and stash it away. We can't raise it here
31
+ # because FreeImage won't be able to clean up any resources it needs to.
32
+ # Instead, the calling code must call check_last_error.
33
+ message = ptr.get_string(0)
34
+ Thread.current[LAST_ERROR] = Error.new(format, message)
35
+ end
36
+ FreeImage_SetOutputMessage(CALLBACK)
37
+
38
+ def check_last_error
39
+ error = Thread.current[LAST_ERROR]
40
+ Thread.current[LAST_ERROR] = nil
41
+ raise(error) if error
42
+ end
43
+ module_function :check_last_error
44
+ end
@@ -0,0 +1,166 @@
1
+ module FreeImage
2
+ #DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertTo4Bits(FIBITMAP *dib);
3
+ attach_function('FreeImage_ConvertTo4Bits', [:pointer], :pointer)
4
+
5
+ #DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertTo8Bits(FIBITMAP *dib);
6
+ attach_function('FreeImage_ConvertTo8Bits', [:pointer], :pointer)
7
+
8
+ #DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertToGreyscale(FIBITMAP *dib);
9
+ attach_function('FreeImage_ConvertToGreyscale', [:pointer], :pointer)
10
+
11
+ #DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertTo16Bits555(FIBITMAP *dib);
12
+ attach_function('FreeImage_ConvertTo16Bits555', [:pointer], :pointer)
13
+
14
+ #DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertTo16Bits565(FIBITMAP *dib);
15
+ attach_function('FreeImage_ConvertTo16Bits565', [:pointer], :pointer)
16
+
17
+ #DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertTo24Bits(FIBITMAP *dib);
18
+ attach_function('FreeImage_ConvertTo24Bits', [:pointer], :pointer)
19
+
20
+ #DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertTo32Bits(FIBITMAP *dib);
21
+ attach_function('FreeImage_ConvertTo32Bits', [:pointer], :pointer)
22
+
23
+ #DLL_API FIBITMAP *DLL_CALLCONV FreeImage_Dither(FIBITMAP *dib, FREE_IMAGE_DITHER algorithm);
24
+ attach_function('FreeImage_Dither', [:pointer, :dither], :pointer)
25
+
26
+ #DLL_API FIBITMAP *DLL_CALLCONV FreeImage_Threshold(FIBITMAP *dib, BYTE T);
27
+ attach_function('FreeImage_Threshold', [:pointer, :byte], :pointer)
28
+
29
+ #DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertToStandardType(FIBITMAP *src, BOOL scale_linear FI_DEFAULT(TRUE));
30
+ attach_function('FreeImage_ConvertToStandardType', [:pointer, FreeImage::Boolean], :pointer)
31
+
32
+ #DLL_API FIBITMAP *DLL_CALLCONV FreeImage_ConvertToType(FIBITMAP *src, FREE_IMAGE_TYPE dst_type, BOOL scale_linear FI_DEFAULT(TRUE));
33
+ attach_function('FreeImage_ConvertToType', [:pointer, :image_type, FreeImage::Boolean], :pointer)
34
+
35
+ module Conversions
36
+ # Converts a bitmap to 4 bits. If the bitmap is a high-color (16, 24 or 32-bit),
37
+ # monochrome or greyscale bitmap (1 or 8-bit) the end result will be a greyscale bitmap.
38
+ # A 1-bit bitmap will become a palletized bitmap.
39
+ #
40
+ # Note that "greyscale" means that the resulting bitmap will have grey colors,
41
+ # but the palette won't be a linear greyscale palette. Thus, FreeImage::Bitmap.color_type
42
+ # will return a :palette.
43
+ def convert_to_4bits
44
+ ptr = FreeImage.FreeImage_ConvertTo4Bits(self)
45
+ FreeImage.check_last_error
46
+ self.class.new(ptr)
47
+ end
48
+
49
+ # Converts a bitmap to 8 bits. If the bitmap is a high-color (16, 24 or 32-bit),
50
+ # monochrome or greyscale bitmap (1 or 4-bit) the end result will be a greyscale bitmap.
51
+ # A 1-bit or 4-bit bitmap will become a palletized bitmap.
52
+ #
53
+ # For 16-bit greyscale images (images whose type is :uint16), conversion is done by
54
+ # dividing the 16-bit channel by 256 (see also FreeImage::Bitmap.ConvertToStandardType).
55
+ # A nil value is returned for other non-standard bitmap types.
56
+ def convert_to_8bits
57
+ ptr = FreeImage.FreeImage_ConvertTo8Bits(self)
58
+ FreeImage.check_last_error
59
+ self.class.new(ptr)
60
+ end
61
+
62
+ # Converts a bitmap to a 8-bit greyscale image with a linear ramp. Contrary to
63
+ # the FreeImage::Conversions#convert_to_8bits function, 1-, 4- and 8-bit palletized
64
+ # bitmaps are correctly converted, as well as images with a :minis_white color type.
65
+ def convert_to_greyscale
66
+ ptr = FreeImage.FreeImage_ConvertToGreyscale(self)
67
+ FreeImage.check_last_error
68
+ self.class.new(ptr)
69
+ end
70
+
71
+ # Converts a bitmap to 16 bits, where each pixel has a color pattern of
72
+ # 5 bits red, 5 bits green and 5 bits blue. One bit in each pixel is
73
+ # unused.
74
+ def convert_to_16bits_555
75
+ ptr = FreeImage.FreeImage_ConvertTo16Bits555(self)
76
+ FreeImage.check_last_error
77
+ self.class.new(ptr)
78
+ end
79
+
80
+ # Converts a bitmap to 16 bits, where each pixel has a color pattern of
81
+ # 5 bits red, 6 bits green and 5 bits blue. One bit in each pixel is
82
+ # unused.
83
+ def convert_to_16bits_565
84
+ ptr = FreeImage.FreeImage_ConvertTo16Bits565(self)
85
+ FreeImage.check_last_error
86
+ self.class.new(ptr)
87
+ end
88
+
89
+ # Converts a bitmap to 24 bits. For 48-bit RGB images, conversion is done
90
+ # by dividing each 16-bit channel by 256. A nil value is returned for
91
+ # other non-standard bitmap types.
92
+ def convert_to_24bits
93
+ ptr = FreeImage.FreeImage_ConvertTo24Bits(self)
94
+ FreeImage.check_last_error
95
+ self.class.new(ptr)
96
+ end
97
+
98
+ # Converts a bitmap to 32 bits. For 48-bit RGB images, conversion is done
99
+ # by dividing each 16-bit channel by 256 and by setting the alpha channel
100
+ # to an opaque value (0xFF). For 64-bit RGBA images, conversion is done
101
+ # by dividing each 16-bit channel by 256. A nil value is returned for
102
+ # other non-standard bitmap types.
103
+ def convert_to_32bits
104
+ ptr = FreeImage.FreeImage_ConvertTo32Bits(self)
105
+ FreeImage.check_last_error
106
+ self.class.new(ptr)
107
+ end
108
+
109
+ # Converts a non standard image whose color type is :minis_black to a
110
+ # standard 8-bit greyscale image. When the scale_linear parameter is
111
+ # true, conversion is done by scaling linearly each pixel value from [min, max]
112
+ # to an integer value between [0..255], where min and max are the minimum
113
+ # and maximum pixel values in the image. When scale_linear is false, conversion
114
+ # is done by rounding each pixel value to an integer between [0..255]. Rounding
115
+ # is done using the following formula:
116
+ #
117
+ # dst_pixel = (BYTE) MIN(255, MAX(0, q)) where int q = int(src_pixel + 0.5)
118
+ #
119
+ # For standard bitmaps, a clone of the original bitmap is returned.
120
+ # For complex images, the magnitude is extracted as a double image and then converted
121
+ # according to the scale parameter.
122
+ def convert_to_standard_type(scale_linear = true)
123
+ ptr = FreeImage.FreeImage_ConvertToStandardType(self, scale_linear)
124
+ FreeImage.check_last_error
125
+ self.class.new(ptr)
126
+ end
127
+
128
+ # Converts a bitmap to the specified destination image type. When the image_type
129
+ # is equal to :bitmap, the function calls FreeImage::Converstions#convert_to_standard_type.
130
+ # Otherwise, conversion is done using standard C language casting conventions. When
131
+ # a conversion is not allowed, a nil value is returned and an error is thrown.
132
+ # Please refer to the FreeImage documentation for allowed conversions.
133
+ def convert_to_type(dst_image_type, scale_linear = true)
134
+ ptr = FreeImage.FreeImage_ConvertToType(self, dst_image_type, scale_linear)
135
+ FreeImage.check_last_error
136
+ self.class.new(ptr)
137
+ end
138
+
139
+ # Converts a bitmap to 1-bit monochrome bitmap using the specified
140
+ # {dithering}[rdoc-ref:FreeImage.dithers] algorithm. For 1-bit input
141
+ # bitmaps, the function clones the input bitmap and builds a
142
+ # monochrome palette. Otherwise the function first converts the
143
+ # bitmap to a 8-bit greyscale bitmap.
144
+ def dither(algorithm)
145
+ ptr = FreeImage.FreeImage_Dither(self, algorithm)
146
+ FreeImage.check_last_error
147
+ self.class.new(ptr)
148
+ end
149
+
150
+ # Converts a bitmap to 1-bit monochrome bitmap using a threshold value
151
+ # between 0 and 255. The function first converts the bitmap to a 8-bit
152
+ # greyscale bitmap. Then any brightness level that is less than the
153
+ # threshold is set to zero and any value above is set to 1.
154
+ # For 1-bit input bitmaps, the function clones the input bitmap and
155
+ # builds a monochrome palette.
156
+ def threshold(value)
157
+ value = Integer(value)
158
+ unless (0..255).include?(value)
159
+ raise(RangeError, "Value is out of range 0..255. Value: #{value}")
160
+ end
161
+ ptr = FreeImage.FreeImage_Threshold(self, value)
162
+ FreeImage.check_last_error
163
+ self.class.new(ptr)
164
+ end
165
+ end
166
+ end