free-image 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY +5 -0
- data/LICENSE +21 -0
- data/README.rdoc +99 -0
- data/Rakefile +47 -0
- data/cookbook.rdoc +239 -0
- data/free-image.gemspec +30 -0
- data/lib/free-image.rb +101 -0
- data/lib/free-image/bitmap.rb +154 -0
- data/lib/free-image/enums/color_types.rb +24 -0
- data/lib/free-image/enums/dithers.rb +24 -0
- data/lib/free-image/enums/filters.rb +12 -0
- data/lib/free-image/enums/formats.rb +84 -0
- data/lib/free-image/enums/image_types.rb +36 -0
- data/lib/free-image/errors.rb +44 -0
- data/lib/free-image/modules/conversions.rb +166 -0
- data/lib/free-image/modules/helper.rb +42 -0
- data/lib/free-image/modules/icc.rb +41 -0
- data/lib/free-image/modules/information.rb +305 -0
- data/lib/free-image/modules/modify.rb +261 -0
- data/lib/free-image/modules/pixels.rb +135 -0
- data/lib/free-image/modules/transforms.rb +83 -0
- data/lib/free-image/palette.rb +44 -0
- data/lib/free-image/scanline.rb +151 -0
- data/lib/free-image/sources/abstract_source.rb +172 -0
- data/lib/free-image/sources/file.rb +115 -0
- data/lib/free-image/sources/io.rb +154 -0
- data/lib/free-image/sources/memory.rb +189 -0
- data/lib/free-image/types/boolean.rb +14 -0
- data/lib/free-image/types/complex.rb +9 -0
- data/lib/free-image/types/ffi.rb +14 -0
- data/lib/free-image/types/rgb16.rb +11 -0
- data/lib/free-image/types/rgb_quad.rb +82 -0
- data/lib/free-image/types/rgb_triple.rb +42 -0
- data/lib/free-image/types/rgba16.rb +12 -0
- data/lib/free-image/types/rgbaf.rb +12 -0
- data/lib/free-image/types/rgbf.rb +11 -0
- data/test/cookbook.rb +46 -0
- data/test/images/gradient.png +0 -0
- data/test/images/lena.png +0 -0
- data/test/images/lena.tiff +0 -0
- data/test/images/lena_flipped.png +0 -0
- data/test/images/lena_rescale_bicubic.png +0 -0
- data/test/images/lena_rescale_bilinear.png +0 -0
- data/test/images/lena_rescale_box.png +0 -0
- data/test/images/lena_rescale_bspline.png +0 -0
- data/test/images/lena_rescale_catmullrom.png +0 -0
- data/test/images/lena_rescale_lanczos3.png +0 -0
- data/test/images/lena_rotate_45.png +0 -0
- data/test/images/lena_rotate_ex_45_masked.png +0 -0
- data/test/images/lena_rotate_ex_45_mirrored.png +0 -0
- data/test/images/lena_rotate_ex_45_top_left.png +0 -0
- data/test/images/lena_thumbnail.png +0 -0
- data/test/images/lena_thumbnail_border_enlarge.png +0 -0
- data/test/images/lena_thumbnail_border_paste.png +0 -0
- data/test/images/lena_thumbnail_border_scanline.png +0 -0
- data/test/images/not_an_image.txt +0 -0
- data/test/images/sample.png +0 -0
- data/test/images/sample_composite_color.png +0 -0
- data/test/test_bitmap.rb +43 -0
- data/test/test_conversions.rb +86 -0
- data/test/test_file.rb +51 -0
- data/test/test_free_image.rb +15 -0
- data/test/test_helper.rb +35 -0
- data/test/test_information.rb +118 -0
- data/test/test_io.rb +53 -0
- data/test/test_memory.rb +65 -0
- data/test/test_modify.rb +59 -0
- data/test/test_palette.rb +45 -0
- data/test/test_pixels.rb +62 -0
- data/test/test_rgb_quad.rb +26 -0
- data/test/test_scanline.rb +65 -0
- data/test/test_suite.rb +19 -0
- data/test/test_transforms.rb +30 -0
- metadata +169 -0
@@ -0,0 +1,115 @@
|
|
1
|
+
module FreeImage
|
2
|
+
# DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_GetFileType(const char *filename, int size FI_DEFAULT(0));
|
3
|
+
attach_function('FreeImage_GetFileType', [:string, :int], :format)
|
4
|
+
|
5
|
+
# DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_GetFIFFromFilename(const char *filename);
|
6
|
+
attach_function('FreeImage_GetFIFFromFilename', [:string], :format)
|
7
|
+
|
8
|
+
# DLL_API FIBITMAP *DLL_CALLCONV FreeImage_Load(FREE_IMAGE_FORMAT fif, const char *filename, int flags FI_DEFAULT(0));
|
9
|
+
attach_function('FreeImage_Load', [:format, :string, :int], :pointer)
|
10
|
+
|
11
|
+
# DLL_API BOOL DLL_CALLCONV FreeImage_FIFSupportsReading(FREE_IMAGE_FORMAT fif);
|
12
|
+
attach_function('FreeImage_FIFSupportsReading', [:format], FreeImage::Boolean)
|
13
|
+
|
14
|
+
# DLL_API BOOL DLL_CALLCONV FreeImage_Save(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, const char *filename, int flags FI_DEFAULT(0));
|
15
|
+
attach_function('FreeImage_Save', [:format, :pointer, :string, :int], FreeImage::Boolean)
|
16
|
+
|
17
|
+
# == Summary
|
18
|
+
#
|
19
|
+
# Supports loading and saving images to a file.
|
20
|
+
#
|
21
|
+
# == Usage
|
22
|
+
#
|
23
|
+
# # Open a file
|
24
|
+
# src = FreeImage::File.new('test/fixtures/lena.png')
|
25
|
+
# image = src.open
|
26
|
+
#
|
27
|
+
# # Save a file
|
28
|
+
# dest = FreeImage::File.new('test/fixtures/lena_new.jpeg')
|
29
|
+
# image.save(dest, :jpeg)
|
30
|
+
#
|
31
|
+
class File < AbstractSource
|
32
|
+
##
|
33
|
+
# :call-seq:
|
34
|
+
# file.open(format = nil, flags = 0) -> FreeImage::Bitmap
|
35
|
+
#
|
36
|
+
# Opens an image from a file.
|
37
|
+
#
|
38
|
+
# == Parameters
|
39
|
+
# format:: By default FreeImage will automatically determine an image's format. However,
|
40
|
+
# you may override this value by using this parameter to specify a
|
41
|
+
# particular {format}[rdoc-ref:FreeImage.formats].
|
42
|
+
# flags:: Format specific flags that control how a bitmap is loaded. These flags are defined
|
43
|
+
# as constants on the AbstractSource::Decoder module. Flags can be combined using
|
44
|
+
# Ruby's bitwise or operator (|)
|
45
|
+
#
|
46
|
+
# == Usage
|
47
|
+
#
|
48
|
+
# source = File.new('<path_to_file>')
|
49
|
+
# source.open(:jpeg, AbtractSource::JPEG_QUALITYSUPERB | AbtractSource::JPEG_PROGRESSIVE)
|
50
|
+
#
|
51
|
+
|
52
|
+
# Create a new FreeImage::File instance that can read and write image data
|
53
|
+
# from a file.
|
54
|
+
#
|
55
|
+
# == Parameters
|
56
|
+
# image_path:: The full path to a image file.
|
57
|
+
#
|
58
|
+
def initialize(image_path)
|
59
|
+
@image_path = image_path
|
60
|
+
end
|
61
|
+
|
62
|
+
# :call-seq:
|
63
|
+
# file.format -> :format
|
64
|
+
#
|
65
|
+
# Returns the image {format}[rdoc-ref:FreeImage.formats] for a file. If the image format cannot
|
66
|
+
# be determined then will return :unknown.
|
67
|
+
#
|
68
|
+
def format
|
69
|
+
result = FreeImage.FreeImage_GetFileType(@image_path, 0)
|
70
|
+
FreeImage.check_last_error
|
71
|
+
|
72
|
+
if result == :unknown
|
73
|
+
# Try to guess the file format from the file extension
|
74
|
+
result = FreeImage.FreeImage_GetFIFFromFilename(@image_path)
|
75
|
+
FreeImage.check_last_error
|
76
|
+
end
|
77
|
+
result
|
78
|
+
end
|
79
|
+
|
80
|
+
##
|
81
|
+
# :call-seq:
|
82
|
+
# file.save(format = nil, flags = 0) -> boolean
|
83
|
+
#
|
84
|
+
# Saves an image to a file.
|
85
|
+
#
|
86
|
+
# == Parameters
|
87
|
+
# format:: The format[rdoc-ref:FreeImage.formats] to save the image to.
|
88
|
+
# flags:: Format specific flags that control how a bitmap is saved. These flags are defined
|
89
|
+
# as constants on the AbstractSource::Encoder module. Flags can be combined using
|
90
|
+
# Ruby's bitwise or operator (|)
|
91
|
+
#
|
92
|
+
# == Usage
|
93
|
+
#
|
94
|
+
# image = Bimap.open('<path_to_file>')
|
95
|
+
# dst = File.new('<path_to_new_file>')
|
96
|
+
# dst.save(image, :jpeg, AbtractSource::JPEG_QUALITYSUPERB | AbtractSource::JPEG_PROGRESSIVE)
|
97
|
+
#
|
98
|
+
def save(bitmap, format, flags = 0)
|
99
|
+
result = FreeImage.FreeImage_Save(format, bitmap, @image_path, flags)
|
100
|
+
FreeImage.check_last_error
|
101
|
+
result
|
102
|
+
end
|
103
|
+
|
104
|
+
private
|
105
|
+
|
106
|
+
def load(format, flags)
|
107
|
+
if format == :unknown
|
108
|
+
# raise(Error, "Cannot load unknown file format")
|
109
|
+
end
|
110
|
+
ptr = FreeImage.FreeImage_Load(format, @image_path, flags)
|
111
|
+
FreeImage.check_last_error
|
112
|
+
ptr
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,154 @@
|
|
1
|
+
module FreeImage
|
2
|
+
typedef :pointer, :handle
|
3
|
+
|
4
|
+
#typedef unsigned (DLL_CALLCONV *FI_ReadProc) (void *buffer, unsigned size, unsigned count, fi_handle handle);
|
5
|
+
callback :read_proc_callback, [:pointer, :ulong, :ulong, :handle], :ulong
|
6
|
+
|
7
|
+
#typedef unsigned (DLL_CALLCONV *FI_WriteProc) (void *buffer, unsigned size, unsigned count, fi_handle handle);
|
8
|
+
callback :write_proc_callback, [:pointer, :ulong, :ulong, :handle], :ulong
|
9
|
+
|
10
|
+
#typedef int (DLL_CALLCONV *FI_SeekProc) (fi_handle handle, long offset, int origin);
|
11
|
+
callback :seek_proc_callback, [:handle, :long, :int], :ulong
|
12
|
+
|
13
|
+
#typedef long (DLL_CALLCONV *FI_TellProc) (fi_handle handle);
|
14
|
+
callback :tell_proc_callback, [:handle], :long
|
15
|
+
|
16
|
+
class IOStruct < FFI::Struct
|
17
|
+
layout :read_proc, :read_proc_callback,
|
18
|
+
:write_proc, :write_proc_callback,
|
19
|
+
:seek_proc, :seek_proc_callback,
|
20
|
+
:tell_proc, :tell_proc_callback
|
21
|
+
end
|
22
|
+
|
23
|
+
# DLL_API FREE_IMAGE_FORMAT DLL_CALLCONV FreeImage_GetFileTypeFromHandle(FreeImageIO *io, fi_handle handle, int size FI_DEFAULT(0));
|
24
|
+
attach_function('FreeImage_GetFileTypeFromHandle', [FreeImage::IOStruct, :handle, :int], :format)
|
25
|
+
|
26
|
+
# DLL_API FIBITMAP *DLL_CALLCONV FreeImage_LoadFromHandle(FREE_IMAGE_FORMAT fif, FreeImageIO *io, fi_handle handle, int flags FI_DEFAULT(0));
|
27
|
+
attach_function('FreeImage_LoadFromHandle', [:format, FreeImage::IOStruct, :handle, :int], :pointer)
|
28
|
+
|
29
|
+
# DLL_API BOOL DLL_CALLCONV FreeImage_SaveToHandle(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, FreeImageIO *io, fi_handle handle, int flags FI_DEFAULT(0));
|
30
|
+
attach_function('FreeImage_SaveToHandle', [:format, :pointer, FreeImage::IOStruct, :handle, :int], FreeImage::Boolean)
|
31
|
+
|
32
|
+
# == Summary
|
33
|
+
#
|
34
|
+
# Supports loading and saving images to a Ruby IO stream.
|
35
|
+
#
|
36
|
+
# == Usage
|
37
|
+
#
|
38
|
+
# # Read an image from an io stream string
|
39
|
+
# file = ::File.open('test/fixtures/lena.png', :encoding => Encoding::BINARY)
|
40
|
+
# image = FreeImage::IO.open(file)
|
41
|
+
#
|
42
|
+
# # Save an image to a byte string
|
43
|
+
# dest = FreeImage::IO.new(::File.open('test/fixtures/lena_new.png', :encoding => Encoding::BINARY))
|
44
|
+
# image.save(dest, :jpeg)
|
45
|
+
# dest.bytes
|
46
|
+
#
|
47
|
+
class IO < AbstractSource
|
48
|
+
##
|
49
|
+
# :call-seq:
|
50
|
+
# io.open(format = nil, flags = 0) -> FreeImage::Bitmap
|
51
|
+
#
|
52
|
+
# Opens an image from a Ruby IO stream.
|
53
|
+
#
|
54
|
+
# == Parameters
|
55
|
+
# format:: By default FreeImage will automatically determine an image's format. However,
|
56
|
+
# you may override this value by using this parameter to specify a
|
57
|
+
# particular {format}[rdoc-ref:FreeImage.formats].
|
58
|
+
# flags:: Format specific flags that control how a bitmap is loaded. These flags are defined
|
59
|
+
# as constants on the AbstractSource::Decoder module. Flags can be combined using
|
60
|
+
# Ruby's bitwise or operator (|)
|
61
|
+
#
|
62
|
+
# == Usage
|
63
|
+
#
|
64
|
+
# source = FreeImage::IO.new(::File.open('<path_to_file>'))
|
65
|
+
# source.open(:jpeg, AbtractSource::JPEG_QUALITYSUPERB | AbtractSource::JPEG_PROGRESSIVE)
|
66
|
+
#
|
67
|
+
|
68
|
+
# Create a new FreeImage::IO instance that can read and write image data
|
69
|
+
# from a Ruby IO stream.
|
70
|
+
#
|
71
|
+
# == Parameters
|
72
|
+
# io:: A standard Ruby io stream such as a file.
|
73
|
+
#
|
74
|
+
def initialize(io)
|
75
|
+
@io = io
|
76
|
+
|
77
|
+
@handle = FFI::MemoryPointer.new(:int)
|
78
|
+
@handle.put_int(0, self.object_id)
|
79
|
+
|
80
|
+
@ffi_io = FreeImage::IOStruct.new
|
81
|
+
@ffi_io[:read_proc] = method(:read)
|
82
|
+
@ffi_io[:write_proc] = method(:write)
|
83
|
+
@ffi_io[:seek_proc] = method(:seek)
|
84
|
+
@ffi_io[:tell_proc] = method(:tell)
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
# call-seq:
|
89
|
+
# handle.image_type -> :format
|
90
|
+
#
|
91
|
+
# Returns the image format for a memory stream. If the image format cannot be determined
|
92
|
+
# the :unknown will be returned.
|
93
|
+
def format
|
94
|
+
result = FreeImage.FreeImage_GetFileTypeFromHandle(@ffi_io, @handle, 0)
|
95
|
+
FreeImage.check_last_error
|
96
|
+
result
|
97
|
+
rescue Errno::EINVAL => e
|
98
|
+
:unknown
|
99
|
+
end
|
100
|
+
|
101
|
+
##
|
102
|
+
# :call-seq:
|
103
|
+
# file.save(format = nil, flags = 0) -> boolean
|
104
|
+
#
|
105
|
+
# Saves an image to a file.
|
106
|
+
#
|
107
|
+
# == Parameters
|
108
|
+
# format:: The format[rdoc-ref:FreeImage.formats] to save the image to.
|
109
|
+
# flags:: Format specific flags that control how a bitmap is saved. These flags are defined
|
110
|
+
# as constants on the AbstractSource::Encoder module. Flags can be combined using
|
111
|
+
# Ruby's bitwise or operator (|)
|
112
|
+
#
|
113
|
+
# == Usage
|
114
|
+
#
|
115
|
+
# image = Bimap.open('<path_to_file>')
|
116
|
+
# source = FreeImage::File.new('<path_to_new_file>')
|
117
|
+
# source.save(image, :jpeg, AbtractSource::JPEG_QUALITYSUPERB | AbtractSource::JPEG_PROGRESSIVE)
|
118
|
+
#
|
119
|
+
def save(bitmap, format, flags = 0)
|
120
|
+
result = FreeImage.FreeImage_SaveToHandle(format, bitmap, @ffi_io, @handle, flags)
|
121
|
+
FreeImage.check_last_error
|
122
|
+
result
|
123
|
+
end
|
124
|
+
|
125
|
+
private
|
126
|
+
|
127
|
+
def load(format, flags)
|
128
|
+
ptr = FreeImage.FreeImage_LoadFromHandle(format, @ffi_io, @handle, flags)
|
129
|
+
FreeImage.check_last_error
|
130
|
+
ptr
|
131
|
+
end
|
132
|
+
|
133
|
+
def read(buffer, size, count, handle)
|
134
|
+
bytes = @io.read(size * count)
|
135
|
+
return 0 unless bytes
|
136
|
+
buffer.put_bytes(0, bytes)
|
137
|
+
bytes.bytesize
|
138
|
+
end
|
139
|
+
|
140
|
+
def write(buffer, size, count, handle)
|
141
|
+
bytes = buffer.get_bytes(0, size * count)
|
142
|
+
@io.write(bytes)
|
143
|
+
bytes.bytesize
|
144
|
+
end
|
145
|
+
|
146
|
+
def seek(handle, offset, origin)
|
147
|
+
@io.seek(offset, origin)
|
148
|
+
end
|
149
|
+
|
150
|
+
def tell(handle)
|
151
|
+
@io.tell
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
@@ -0,0 +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
|
189
|
+
end
|