free-image 0.6.2 → 0.7.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.
- checksums.yaml +7 -0
- data/HISTORY +30 -19
- data/LICENSE +20 -20
- data/README.rdoc +120 -120
- data/Rakefile +51 -47
- data/cookbook.rdoc +248 -237
- data/free-image.gemspec +30 -29
- data/lib/free-image.rb +1 -2
- data/lib/free-image/bitmap.rb +2 -2
- data/lib/free-image/enums/filters.rb +11 -11
- data/lib/free-image/errors.rb +43 -43
- data/lib/free-image/modules/conversions.rb +253 -253
- data/lib/free-image/modules/helper.rb +41 -41
- data/lib/free-image/modules/information.rb +20 -2
- data/lib/free-image/modules/modify.rb +299 -299
- data/lib/free-image/modules/pixels.rb +134 -134
- data/lib/free-image/modules/transforms.rb +90 -90
- data/lib/free-image/sources/abstract_source.rb +178 -178
- data/lib/free-image/sources/file.rb +114 -114
- data/lib/free-image/sources/io.rb +153 -153
- data/lib/free-image/sources/memory.rb +188 -188
- data/lib/free-image/types/boolean.rb +13 -13
- data/lib/free-image/types/ffi.rb +13 -13
- data/lib/free-image/types/info_header.rb +36 -0
- data/lib/free-image/types/rgb16.rb +31 -0
- data/test/cookbook.rb +45 -46
- data/test/images/sample_composite.png +0 -0
- data/test/images/test16.bmp +0 -0
- data/test/images/test16bf555.bmp +0 -0
- data/test/images/test16bf565.bmp +0 -0
- data/test/test_bitmap.rb +61 -63
- data/test/test_conversions.rb +85 -86
- data/test/test_file.rb +68 -69
- data/test/test_free_image.rb +14 -15
- data/test/test_helper.rb +14 -0
- data/test/test_information.rb +145 -117
- data/test/test_io.rb +73 -74
- data/test/test_memory.rb +83 -84
- data/test/test_modify.rb +75 -58
- data/test/test_palette.rb +44 -45
- data/test/test_pixels.rb +61 -62
- data/test/test_rgb_quad.rb +24 -25
- data/test/test_scanline.rb +64 -65
- data/test/test_suite.rb +12 -17
- data/test/test_transforms.rb +28 -29
- metadata +58 -31
@@ -1,115 +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::
|
89
|
-
#
|
90
|
-
#
|
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
|
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[rdoc-ref:FreeImage::AbstractSource::Encoder] class.
|
90
|
+
# Flags can be combined using 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
115
|
end
|
@@ -1,154 +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(:
|
78
|
-
@handle.
|
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::
|
110
|
-
#
|
111
|
-
#
|
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
|
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(:ulong)
|
78
|
+
@handle.put_ulong(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[rdoc-ref:FreeImage::AbstractSource::Encoder] class.
|
111
|
+
# Flags can be combined using 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
154
|
end
|