free-image 0.6.2 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/HISTORY +30 -19
  3. data/LICENSE +20 -20
  4. data/README.rdoc +120 -120
  5. data/Rakefile +51 -47
  6. data/cookbook.rdoc +248 -237
  7. data/free-image.gemspec +30 -29
  8. data/lib/free-image.rb +1 -2
  9. data/lib/free-image/bitmap.rb +2 -2
  10. data/lib/free-image/enums/filters.rb +11 -11
  11. data/lib/free-image/errors.rb +43 -43
  12. data/lib/free-image/modules/conversions.rb +253 -253
  13. data/lib/free-image/modules/helper.rb +41 -41
  14. data/lib/free-image/modules/information.rb +20 -2
  15. data/lib/free-image/modules/modify.rb +299 -299
  16. data/lib/free-image/modules/pixels.rb +134 -134
  17. data/lib/free-image/modules/transforms.rb +90 -90
  18. data/lib/free-image/sources/abstract_source.rb +178 -178
  19. data/lib/free-image/sources/file.rb +114 -114
  20. data/lib/free-image/sources/io.rb +153 -153
  21. data/lib/free-image/sources/memory.rb +188 -188
  22. data/lib/free-image/types/boolean.rb +13 -13
  23. data/lib/free-image/types/ffi.rb +13 -13
  24. data/lib/free-image/types/info_header.rb +36 -0
  25. data/lib/free-image/types/rgb16.rb +31 -0
  26. data/test/cookbook.rb +45 -46
  27. data/test/images/sample_composite.png +0 -0
  28. data/test/images/test16.bmp +0 -0
  29. data/test/images/test16bf555.bmp +0 -0
  30. data/test/images/test16bf565.bmp +0 -0
  31. data/test/test_bitmap.rb +61 -63
  32. data/test/test_conversions.rb +85 -86
  33. data/test/test_file.rb +68 -69
  34. data/test/test_free_image.rb +14 -15
  35. data/test/test_helper.rb +14 -0
  36. data/test/test_information.rb +145 -117
  37. data/test/test_io.rb +73 -74
  38. data/test/test_memory.rb +83 -84
  39. data/test/test_modify.rb +75 -58
  40. data/test/test_palette.rb +44 -45
  41. data/test/test_pixels.rb +61 -62
  42. data/test/test_rgb_quad.rb +24 -25
  43. data/test/test_scanline.rb +64 -65
  44. data/test/test_suite.rb +12 -17
  45. data/test/test_transforms.rb +28 -29
  46. metadata +58 -31
@@ -1,189 +1,189 @@
1
- module FreeImage
2
- typedef :pointer, :memory
3
-
4
- #DLL_API void DLL_CALLCONV FreeImage_CloseMemory(FIMEMORY *stream);
5
- attach_function('FreeImage_CloseMemory', [:memory], :void)
6
-
7
- #DLL_API FIMEMORY *DLL_CALLCONV FreeImage_OpenMemory(BYTE *data FI_DEFAULT(0), DWORD size_in_bytes FI_DEFAULT(0));
8
- attach_function('FreeImage_OpenMemory', [:pointer, :dword], :memory)
9
-
10
- # DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_GetFileTypeFromMemory(FIMEMORY *stream, int size FI_DEFAULT(0));
11
- attach_function('FreeImage_GetFileTypeFromMemory', [:memory, :int], :format)
12
-
13
- # DLL_API FIBITMAP *DLL_CALLCONV FreeImage_LoadFromMemory(FREE_IMAGE_FORMAT fif, FIMEMORY *stream, int flags FI_DEFAULT(0))
14
- attach_function('FreeImage_LoadFromMemory', [:format, :memory, :int], :pointer)
15
-
16
- # DLL_API BOOL DLL_CALLCONV FreeImage_SaveToMemory(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, FIMEMORY *stream, int flags FI_DEFAULT(0));
17
- attach_function('FreeImage_SaveToMemory', [:format, :pointer, :memory, :int], FreeImage::Boolean)
18
-
19
- #DLL_API unsigned DLL_CALLCONV FreeImage_ReadMemory(void *buffer, unsigned size, unsigned count, FIMEMORY *stream);
20
- attach_function('FreeImage_ReadMemory', [:pointer, :ulong, :ulong, :memory], :ulong)
21
-
22
- # DLL_API long DLL_CALLCONV FreeImage_TellMemory(FIMEMORY *stream);
23
- attach_function('FreeImage_TellMemory', [:memory], :long)
24
-
25
- #DLL_API BOOL DLL_CALLCONV FreeImage_SeekMemory(FIMEMORY *stream, long offset, int origin);
26
- attach_function('FreeImage_SeekMemory', [:memory, :long, :int], FreeImage::Boolean)
27
-
28
- #DLL_API BOOL DLL_CALLCONV FreeImage_AcquireMemory(FIMEMORY *stream, BYTE **data, DWORD *size_in_bytes);
29
- #DLL_API unsigned DLL_CALLCONV FreeImage_WriteMemory(const void *buffer, unsigned size, unsigned count, FIMEMORY *stream);
30
- #DLL_API FIMULTIBITMAP *DLL_CALLCONV FreeImage_LoadMultiBitmapFromMemory(FREE_IMAGE_FORMAT fif, FIMEMORY *stream, int flags FI_DEFAULT(0));
31
- #DLL_API BOOL DLL_CALLCONV FreeImage_SaveMultiBitmapToMemory(FREE_IMAGE_FORMAT fif, FIMULTIBITMAP *bitmap, FIMEMORY *stream, int flags);
32
-
33
- # Wrapper for a FreeImage memory stream which allows images to be read and written
34
- # to memory. Memory streams are usefule for storing images as blobs in a database
35
- # or writing them to an to a Internet stream.
36
- class MemoryStream < FFI::AutoPointer
37
- def self.release(ptr)
38
- FreeImage.FreeImage_CloseMemory(ptr)
39
- FreeImage.check_last_error
40
- end
41
-
42
- # Create a new memory stream.
43
- #
44
- # == Parameters
45
- # bytes:: If specified, a binary encoded Ruby string that stores image data. FreeImage
46
- # will treat the string as read-only. If not specified, a writable MemoryStream
47
- # is created.
48
- def initialize(bytes = nil)
49
- ptr = if bytes
50
- buf = FFI::MemoryPointer.from_string(bytes)
51
- FreeImage.FreeImage_OpenMemory(buf, bytes.bytesize)
52
- else
53
- FreeImage.FreeImage_OpenMemory(nil, 0)
54
- end
55
- FreeImage.check_last_error
56
-
57
- super(ptr)
58
- end
59
-
60
- # Returns the size of the memory stream.
61
- def count
62
- # Store current position
63
- pos = FreeImage.FreeImage_TellMemory(self)
64
- FreeImage.check_last_error
65
-
66
- # Go to end of stream to get length
67
- FreeImage.FreeImage_SeekMemory(self, 0, ::IO::SEEK_END)
68
- FreeImage.check_last_error
69
- count = FreeImage.FreeImage_TellMemory(self)
70
-
71
- # Restore position
72
- FreeImage.FreeImage_SeekMemory(self, pos, ::IO::SEEK_SET)
73
- FreeImage.check_last_error
74
-
75
- count
76
- end
77
-
78
- # Returns the bytes of the memory stream.
79
- def bytes
80
- size = FFI::Type::CHAR.size
81
-
82
- # Reset memory to start
83
- FreeImage.FreeImage_SeekMemory(self, 0, ::IO::SEEK_SET)
84
- FreeImage.check_last_error
85
-
86
- buffer = FFI::MemoryPointer.new(FFI::Type::CHAR, size * count)
87
- FreeImage.check_last_error
88
- size = FreeImage.FreeImage_ReadMemory(buffer, size, count, self)
89
- buffer.null? ? nil : buffer.read_string
90
- end
91
- end
92
-
93
- # == Summary
94
- #
95
- # Supports loading and saving images to a Ruby string.
96
- #
97
- # == Usage
98
- #
99
- # # Read an image from a byte string
100
- # bytes = ::File.read('test/fixtures/lena.png', :encoding => Encoding::BINARY)
101
- # image = FreeImage::Memory.open(bytes)
102
- #
103
- # # Save an image to a byte string
104
- # dest = FreeImage::Memory.new
105
- # image.save(dest, :jpeg)
106
- # dest.bytes
107
- #
108
- class Memory < AbstractSource
109
- ##
110
- # MemoryStream used to read and write data
111
- attr_reader :memory
112
-
113
- ##
114
- # :call-seq:
115
- # file.open(format = nil, flags = 0) -> FreeImage::Bitmap
116
- #
117
- # Opens an image from a file.
118
- #
119
- # == Parameters
120
- # format:: By default FreeImage will automatically determine an image's format. However,
121
- # you may override this value by using this parameter to specify a
122
- # particular {format}[rdoc-ref:FreeImage.formats].
123
- # flags:: Format specific flags that control how a bitmap is loaded. These flags are defined
124
- # as constants on the AbstractSource::Decoder module. Flags can be combined using
125
- # Ruby's bitwise or operator (|)
126
- #
127
- # == Usage
128
- #
129
- # source = File.new('<path_to_file>')
130
- # source.open(:jpeg, AbtractSource::JPEG_QUALITYSUPERB | AbtractSource::JPEG_PROGRESSIVE)
131
- #
132
-
133
- # Create a new FreeImage::File instance that can read and write image data
134
- # from memory.
135
- #
136
- # == Parameters
137
- # bytes:: If specified, FreeImage will read image from the bytes string and treat
138
- # it as readonly. If not specified, then FreeImage will create a writable
139
- # memory stream.
140
- #
141
- def initialize(bytes = nil)
142
- @memory = MemoryStream.new(bytes)
143
- end
144
-
145
- # call-seq:
146
- # memory.format -> :format
147
- #
148
- # Returns the image format for a memory stream. If the image format cannot be determined
149
- # the :unknown will be returned.
150
- def format
151
- result = FreeImage.FreeImage_GetFileTypeFromMemory(@memory, 0)
152
- FreeImage.check_last_error
153
- result
154
- end
155
-
156
- ##
157
- # :call-seq:
158
- # memory.save(format = nil, flags = 0) -> boolean
159
- #
160
- # Saves an image to memory.
161
- #
162
- # == Parameters
163
- # format:: The format[rdoc-ref:FreeImage.formats] to save the image to.
164
- # flags:: Format specific flags that control how a bitmap is saved. These flags are defined
165
- # as constants on the AbstractSource::Encoder module. Flags can be combined using
166
- # Ruby's bitwise or operator (|)
167
- #
168
- # == Usage
169
- #
170
- # image = Bimap.open('<path_to_file>')
171
- # dst = FreeImage::Memory.new
172
- # dst.save(image, :jpeg, AbtractSource::JPEG_QUALITYSUPERB | AbtractSource::JPEG_PROGRESSIVE)
173
- # dst.bytes
174
- #
175
- def save(bitmap, format, flags = 0)
176
- result = FreeImage.FreeImage_SaveToMemory(format, bitmap, @memory, flags)
177
- FreeImage.check_last_error
178
- result
179
- end
180
-
181
- private
182
-
183
- def load(format, flags)
184
- ptr = FreeImage.FreeImage_LoadFromMemory(format, @memory, flags)
185
- FreeImage.check_last_error
186
- ptr
187
- end
188
- end
1
+ module FreeImage
2
+ typedef :pointer, :memory
3
+
4
+ #DLL_API void DLL_CALLCONV FreeImage_CloseMemory(FIMEMORY *stream);
5
+ attach_function('FreeImage_CloseMemory', [:memory], :void)
6
+
7
+ #DLL_API FIMEMORY *DLL_CALLCONV FreeImage_OpenMemory(BYTE *data FI_DEFAULT(0), DWORD size_in_bytes FI_DEFAULT(0));
8
+ attach_function('FreeImage_OpenMemory', [:pointer, :dword], :memory)
9
+
10
+ # DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_GetFileTypeFromMemory(FIMEMORY *stream, int size FI_DEFAULT(0));
11
+ attach_function('FreeImage_GetFileTypeFromMemory', [:memory, :int], :format)
12
+
13
+ # DLL_API FIBITMAP *DLL_CALLCONV FreeImage_LoadFromMemory(FREE_IMAGE_FORMAT fif, FIMEMORY *stream, int flags FI_DEFAULT(0))
14
+ attach_function('FreeImage_LoadFromMemory', [:format, :memory, :int], :pointer)
15
+
16
+ # DLL_API BOOL DLL_CALLCONV FreeImage_SaveToMemory(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, FIMEMORY *stream, int flags FI_DEFAULT(0));
17
+ attach_function('FreeImage_SaveToMemory', [:format, :pointer, :memory, :int], FreeImage::Boolean)
18
+
19
+ #DLL_API unsigned DLL_CALLCONV FreeImage_ReadMemory(void *buffer, unsigned size, unsigned count, FIMEMORY *stream);
20
+ attach_function('FreeImage_ReadMemory', [:pointer, :ulong, :ulong, :memory], :ulong)
21
+
22
+ # DLL_API long DLL_CALLCONV FreeImage_TellMemory(FIMEMORY *stream);
23
+ attach_function('FreeImage_TellMemory', [:memory], :long)
24
+
25
+ #DLL_API BOOL DLL_CALLCONV FreeImage_SeekMemory(FIMEMORY *stream, long offset, int origin);
26
+ attach_function('FreeImage_SeekMemory', [:memory, :long, :int], FreeImage::Boolean)
27
+
28
+ #DLL_API BOOL DLL_CALLCONV FreeImage_AcquireMemory(FIMEMORY *stream, BYTE **data, DWORD *size_in_bytes);
29
+ #DLL_API unsigned DLL_CALLCONV FreeImage_WriteMemory(const void *buffer, unsigned size, unsigned count, FIMEMORY *stream);
30
+ #DLL_API FIMULTIBITMAP *DLL_CALLCONV FreeImage_LoadMultiBitmapFromMemory(FREE_IMAGE_FORMAT fif, FIMEMORY *stream, int flags FI_DEFAULT(0));
31
+ #DLL_API BOOL DLL_CALLCONV FreeImage_SaveMultiBitmapToMemory(FREE_IMAGE_FORMAT fif, FIMULTIBITMAP *bitmap, FIMEMORY *stream, int flags);
32
+
33
+ # Wrapper for a FreeImage memory stream which allows images to be read and written
34
+ # to memory. Memory streams are usefule for storing images as blobs in a database
35
+ # or writing them to an to a Internet stream.
36
+ class MemoryStream < FFI::AutoPointer
37
+ def self.release(ptr)
38
+ FreeImage.FreeImage_CloseMemory(ptr)
39
+ FreeImage.check_last_error
40
+ end
41
+
42
+ # Create a new memory stream.
43
+ #
44
+ # == Parameters
45
+ # bytes:: If specified, a binary encoded Ruby string that stores image data. FreeImage
46
+ # will treat the string as read-only. If not specified, a writable MemoryStream
47
+ # is created.
48
+ def initialize(bytes = nil)
49
+ ptr = if bytes
50
+ buf = FFI::MemoryPointer.from_string(bytes)
51
+ FreeImage.FreeImage_OpenMemory(buf, bytes.bytesize)
52
+ else
53
+ FreeImage.FreeImage_OpenMemory(nil, 0)
54
+ end
55
+ FreeImage.check_last_error
56
+
57
+ super(ptr)
58
+ end
59
+
60
+ # Returns the size of the memory stream.
61
+ def count
62
+ # Store current position
63
+ pos = FreeImage.FreeImage_TellMemory(self)
64
+ FreeImage.check_last_error
65
+
66
+ # Go to end of stream to get length
67
+ FreeImage.FreeImage_SeekMemory(self, 0, ::IO::SEEK_END)
68
+ FreeImage.check_last_error
69
+ count = FreeImage.FreeImage_TellMemory(self)
70
+
71
+ # Restore position
72
+ FreeImage.FreeImage_SeekMemory(self, pos, ::IO::SEEK_SET)
73
+ FreeImage.check_last_error
74
+
75
+ count
76
+ end
77
+
78
+ # Returns the bytes of the memory stream.
79
+ def bytes
80
+ size = FFI::Type::CHAR.size
81
+
82
+ # Reset memory to start
83
+ FreeImage.FreeImage_SeekMemory(self, 0, ::IO::SEEK_SET)
84
+ FreeImage.check_last_error
85
+
86
+ buffer = FFI::MemoryPointer.new(FFI::Type::CHAR, size * count)
87
+ FreeImage.check_last_error
88
+ size = FreeImage.FreeImage_ReadMemory(buffer, size, count, self)
89
+ buffer.null? ? nil : buffer.read_string
90
+ end
91
+ end
92
+
93
+ # == Summary
94
+ #
95
+ # Supports loading and saving images to a Ruby string.
96
+ #
97
+ # == Usage
98
+ #
99
+ # # Read an image from a byte string
100
+ # bytes = ::File.read('test/fixtures/lena.png', :encoding => Encoding::BINARY)
101
+ # image = FreeImage::Memory.open(bytes)
102
+ #
103
+ # # Save an image to a byte string
104
+ # dest = FreeImage::Memory.new
105
+ # image.save(dest, :jpeg)
106
+ # dest.bytes
107
+ #
108
+ class Memory < AbstractSource
109
+ ##
110
+ # MemoryStream used to read and write data
111
+ attr_reader :memory
112
+
113
+ ##
114
+ # :call-seq:
115
+ # file.open(format = nil, flags = 0) -> FreeImage::Bitmap
116
+ #
117
+ # Opens an image from a file.
118
+ #
119
+ # == Parameters
120
+ # format:: By default FreeImage will automatically determine an image's format. However,
121
+ # you may override this value by using this parameter to specify a
122
+ # particular {format}[rdoc-ref:FreeImage.formats].
123
+ # flags:: Format specific flags that control how a bitmap is loaded. These flags are defined
124
+ # as constants on the AbstractSource::Decoder module. Flags can be combined using
125
+ # Ruby's bitwise or operator (|)
126
+ #
127
+ # == Usage
128
+ #
129
+ # source = File.new('<path_to_file>')
130
+ # source.open(:jpeg, AbtractSource::JPEG_QUALITYSUPERB | AbtractSource::JPEG_PROGRESSIVE)
131
+ #
132
+
133
+ # Create a new FreeImage::File instance that can read and write image data
134
+ # from memory.
135
+ #
136
+ # == Parameters
137
+ # bytes:: If specified, FreeImage will read image from the bytes string and treat
138
+ # it as readonly. If not specified, then FreeImage will create a writable
139
+ # memory stream.
140
+ #
141
+ def initialize(bytes = nil)
142
+ @memory = MemoryStream.new(bytes)
143
+ end
144
+
145
+ # call-seq:
146
+ # memory.format -> :format
147
+ #
148
+ # Returns the image format for a memory stream. If the image format cannot be determined
149
+ # the :unknown will be returned.
150
+ def format
151
+ result = FreeImage.FreeImage_GetFileTypeFromMemory(@memory, 0)
152
+ FreeImage.check_last_error
153
+ result
154
+ end
155
+
156
+ ##
157
+ # :call-seq:
158
+ # memory.save(format = nil, flags = 0) -> boolean
159
+ #
160
+ # Saves an image to memory.
161
+ #
162
+ # == Parameters
163
+ # format:: The format[rdoc-ref:FreeImage.formats] to save the image to.
164
+ # flags:: Format specific flags that control how a bitmap is saved. These flags are defined
165
+ # as constants on the AbstractSource[rdoc-ref:FreeImage::AbstractSource::Encoder] class.
166
+ # Flags can be combined using Ruby's bitwise or operator (|)
167
+ #
168
+ # == Usage
169
+ #
170
+ # image = Bimap.open('<path_to_file>')
171
+ # dst = FreeImage::Memory.new
172
+ # dst.save(image, :jpeg, AbtractSource::JPEG_QUALITYSUPERB | AbtractSource::JPEG_PROGRESSIVE)
173
+ # dst.bytes
174
+ #
175
+ def save(bitmap, format, flags = 0)
176
+ result = FreeImage.FreeImage_SaveToMemory(format, bitmap, @memory, flags)
177
+ FreeImage.check_last_error
178
+ result
179
+ end
180
+
181
+ private
182
+
183
+ def load(format, flags)
184
+ ptr = FreeImage.FreeImage_LoadFromMemory(format, @memory, flags)
185
+ FreeImage.check_last_error
186
+ ptr
187
+ end
188
+ end
189
189
  end
@@ -1,14 +1,14 @@
1
- module FreeImage
2
- class Boolean
3
- extend FFI::DataConverter
4
- native_type :uint32
5
-
6
- def self.to_native(val, ctx)
7
- val == false ? 0 : 1
8
- end
9
-
10
- def self.from_native(val, ctx)
11
- val == 0 ? false : true
12
- end
13
- end
1
+ module FreeImage
2
+ class Boolean
3
+ extend FFI::DataConverter
4
+ native_type :uint32
5
+
6
+ def self.to_native(val, ctx)
7
+ val == false ? 0 : 1
8
+ end
9
+
10
+ def self.from_native(val, ctx)
11
+ val == 0 ? false : true
12
+ end
13
+ end
14
14
  end
@@ -1,14 +1,14 @@
1
- module FreeImage
2
- if !self.msvc?
3
- typedef :int32, :bool
4
- typedef :uint32, :byte
5
- typedef :uint16, :word
6
- typedef :uint32, :dword
7
- typedef :int32, :long
8
- else
9
- typedef :long, :bool
10
- typedef :uchar, :byte
11
- typedef :ushort, :word
12
- typedef :ulong, :dword
13
- end
1
+ module FreeImage
2
+ if !self.msvc?
3
+ typedef :int32, :bool
4
+ typedef :uint8, :byte
5
+ typedef :uint16, :word
6
+ typedef :uint32, :dword
7
+ typedef :int32, :long
8
+ else
9
+ typedef :long, :bool
10
+ typedef :uchar, :byte
11
+ typedef :ushort, :word
12
+ typedef :ulong, :dword
13
+ end
14
14
  end
@@ -0,0 +1,36 @@
1
+ # encoding: UTF-8
2
+
3
+ module FreeImage
4
+ # BI_RGB
5
+ RGB = 0
6
+ # BI_BITFIELDS
7
+ BITFIELDS = 3
8
+ # Structure for BITMAPINFOHEADER
9
+ =begin
10
+ DWORD biSize;
11
+ LONG biWidth;
12
+ LONG biHeight;
13
+ WORD biPlanes;
14
+ WORD biBitCount;
15
+ DWORD biCompression;
16
+ DWORD biSizeImage;
17
+ LONG biXPelsPerMeter;
18
+ LONG biYPelsPerMeter;
19
+ DWORD biClrUsed;
20
+ DWORD biClrImportant;
21
+ =end
22
+ class InfoHeader < FFI::Struct
23
+ layout :biSize, :dword, 0,
24
+ :biWidth, :long, 4,
25
+ :biHeight, :long, 8,
26
+ :biPlanes, :word, 12,
27
+ :biBitCount, :word, 14,
28
+ :biCompression, :dword, 16,
29
+ :biSizeImage, :dword, 20,
30
+ :biXPelsPerMeter, :long, 24,
31
+ :biYPelsPerMeter, :long, 28,
32
+ :biClrUsed, :dword, 32,
33
+ :biClrImportant, :dword, 36
34
+
35
+ end
36
+ end