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.
17
18
19
|
# File 'lib/painter.rb', line 17
def image_pointer
@image_pointer
end
|
Class Method Details
.open(path, format = nil) ⇒ Object
32
33
34
35
36
37
38
|
# File 'lib/painter.rb', line 32
def self.open(path, format = nil)
instance = new
instance.initializeFromOpen(path, format)
yield instance
ensure
instance.release!
end
|
.write(path, x, y, file_format) ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/painter.rb', line 19
def self.write(path, x, y, file_format)
instance = new
instance.initializeFromCreate(x,y)
yield instance
File.open(path, 'w') do |f|
instance.format(file_format) do |data|
f.write data
end
end
ensure
instance.release!
end
|
Instance Method Details
#degrees(n) ⇒ Object
44
45
46
|
# File 'lib/painter.rb', line 44
def degrees(n)
n * 2 * Math::PI / 360
end
|
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/painter.rb', line 115
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
|
72
73
74
|
# File 'lib/painter.rb', line 72
def format_from_filename(path)
path[/\.([^.]*)$/, 1].downcase
end
|
#info ⇒ Object
85
86
87
|
# File 'lib/painter.rb', line 85
def info
@info ||= Painter::ImageStruct.new @image_pointer
end
|
#initializeFromCreate(x, y, options = {}) ⇒ Object
57
58
59
|
# File 'lib/painter.rb', line 57
def initializeFromCreate(x, y, options = {})
@image_pointer = FFI::Pointer.new(CGD2::gdImageCreateTrueColor(x, y))
end
|
#initializeFromOpen(path, format = nil) ⇒ Object
76
77
78
79
80
81
82
83
|
# File 'lib/painter.rb', line 76
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
53
54
55
|
# File 'lib/painter.rb', line 53
def release(ptr)
CGD2::gdImageDestroy(ptr)
end
|
#release! ⇒ Object
40
41
42
|
# File 'lib/painter.rb', line 40
def release!
CGD2::gdImageDestroy(@image_pointer)
end
|
#resize_to(destination) ⇒ Object
100
101
102
103
104
|
# File 'lib/painter.rb', line 100
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
|