Class: Painter
- Inherits:
-
Object
show all
- Defined in:
- lib/painter.rb,
lib/painter/cgd2.rb,
lib/painter/line.rb,
lib/painter/rect.rb,
lib/painter/text.rb,
lib/painter/color.rb,
lib/painter/pixel.rb,
lib/painter/point.rb,
lib/painter/stdio.rb,
lib/painter/operation.rb,
lib/painter/image_struct.rb
Defined Under Namespace
Modules: CGD2, Stdio
Classes: Color, ImageStruct, Line, Operation, Pixel, Point, Rect, Text
Constant Summary
collapse
- ALPHA_MAX =
127
- ALPHA_OPAQUE =
0
- ALPHA_TRANSPARENT =
127
- RGB_MAX =
255
- FORMAT_TO_FUNCTION =
{
'jpg' => :gdImageCreateFromJpeg,
'png' => :gdImageCreateFromPng,
'gif' => :gdImageCreateFromGif,
'gd' => :gdImageCreateFromGd,
'gd2' => :gdImageCreateFromGd2,
'wbmp' => :gdImageCreateFromWBMP,
'xbm' => :gdImageCreateFromXbm
}
- OUTPUT_FORMAT_TO_FUNCTION =
{
'gif' => :gdImageGifPtr,
'jpeg' => :gdImageJpegPtr,
'jpg' => :gdImageJpegPtr,
'png' => :gdImagePngPtr,
'wbmp' => :gdImageWBMPPtr,
'gd' => :gdImageGdPtr,
'gd2' => :gdImageGd2
}
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Attribute Details
#image_pointer ⇒ Object
Returns the value of attribute image_pointer.
14
15
16
|
# File 'lib/painter.rb', line 14
def image_pointer
@image_pointer
end
|
Class Method Details
.create(x, y, file_format, &block) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/painter.rb', line 23
def self.create(x, y, file_format, &block)
instance = new
instance.initializeFromCreate(x,y)
block.call(instance)
data = nil
instance.format(file_format) do |output|
data = output
end
instance.release!
return data
end
|
.open(path, format = nil) ⇒ Object
34
35
36
37
38
39
40
|
# File 'lib/painter.rb', line 34
def self.open(path, format = nil)
instance = new
instance.initializeFromOpen(path, format)
yield instance
ensure
instance.release!
end
|
.write(path, x, y, file_format, &block) ⇒ Object
16
17
18
19
20
21
|
# File 'lib/painter.rb', line 16
def self.write(path, x, y, file_format, &block)
data = create(x, y, file_format, &block)
File.open(path, 'w') do |f|
f.write data
end
end
|
Instance Method Details
#degrees(n) ⇒ Object
46
47
48
|
# File 'lib/painter.rb', line 46
def degrees(n)
n * 2 * Math::PI / 360
end
|
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/painter.rb', line 117
def format(file_format, quality = 100)
size = FFI::MemoryPointer.new(:pointer)
function_name = OUTPUT_FORMAT_TO_FUNCTION[file_format.to_s]
raise "Unknown format: #{file_format}" unless function_name
args = [@image_pointer, size]
if function_name == :gdImageJpegPtr
args << quality
end
begin
ip = CGD2.send(function_name, *args)
size = size.get_int(0)
data = ip.get_bytes(0, size)
yield data
ensure
CGD2::gdFree(ip)
end
end
|
74
75
76
|
# File 'lib/painter.rb', line 74
def format_from_filename(path)
path[/\.([^.]*)$/, 1].downcase
end
|
#info ⇒ Object
87
88
89
|
# File 'lib/painter.rb', line 87
def info
@info ||= Painter::ImageStruct.new @image_pointer
end
|
#initializeFromCreate(x, y, options = {}) ⇒ Object
59
60
61
|
# File 'lib/painter.rb', line 59
def initializeFromCreate(x, y, options = {})
@image_pointer = FFI::Pointer.new(CGD2::gdImageCreateTrueColor(x, y))
end
|
#initializeFromOpen(path, format = nil) ⇒ Object
78
79
80
81
82
83
84
85
|
# File 'lib/painter.rb', line 78
def initializeFromOpen(path, format = nil)
f = Painter::Stdio.fopen(path, "rb")
format ||= format_from_filename(path)
function_name = FORMAT_TO_FUNCTION[format.to_s]
raise "Unknown format: #{format}" unless function_name
@image_pointer = CGD2.send(function_name, f)
Painter::Stdio.fclose(f)
end
|
#release(ptr) ⇒ Object
55
56
57
|
# File 'lib/painter.rb', line 55
def release(ptr)
CGD2::gdImageDestroy(ptr)
end
|
#release! ⇒ Object
42
43
44
|
# File 'lib/painter.rb', line 42
def release!
CGD2::gdImageDestroy(@image_pointer)
end
|
#resize_to(destination) ⇒ Object
102
103
104
105
106
|
# File 'lib/painter.rb', line 102
def resize_to(destination)
CGD2::gdImageCopyResampled(destination.image_pointer, self.image_pointer,
0, 0, 0, 0,
destination.info.x, destination.info.y, self.info.x, self.info.y)
end
|