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/image.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, Image, 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.
15
16
17
|
# File 'lib/painter.rb', line 15
def image_pointer
@image_pointer
end
|
Class Method Details
.create(x, y, file_format, &block) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/painter.rb', line 24
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
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/painter.rb', line 35
def self.open(path, format = nil)
instance = new
raise "File not found: #{path}" unless File.exists?(path)
begin
instance.initializeFromOpen(path, format)
yield instance
ensure
instance.release!
end
end
|
.write(path, x, y, file_format, &block) ⇒ Object
17
18
19
20
21
22
|
# File 'lib/painter.rb', line 17
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
50
51
52
|
# File 'lib/painter.rb', line 50
def degrees(n)
n * 2 * Math::PI / 360
end
|
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/painter.rb', line 124
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
|
78
79
80
|
# File 'lib/painter.rb', line 78
def format_from_filename(path)
path[/\.([^.]*)$/, 1].downcase
end
|
#info ⇒ Object
91
92
93
|
# File 'lib/painter.rb', line 91
def info
@info ||= Painter::ImageStruct.new @image_pointer
end
|
#initializeFromCreate(x, y, options = {}) ⇒ Object
63
64
65
|
# File 'lib/painter.rb', line 63
def initializeFromCreate(x, y, options = {})
@image_pointer = FFI::Pointer.new(CGD2::gdImageCreateTrueColor(x, y))
end
|
#initializeFromOpen(path, format = nil) ⇒ Object
82
83
84
85
86
87
88
89
|
# File 'lib/painter.rb', line 82
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
59
60
61
|
# File 'lib/painter.rb', line 59
def release(ptr)
CGD2::gdImageDestroy(ptr)
end
|
#release! ⇒ Object
46
47
48
|
# File 'lib/painter.rb', line 46
def release!
CGD2::gdImageDestroy(@image_pointer)
end
|
#resize_to(destination) ⇒ Object
106
107
108
109
110
111
112
113
|
# File 'lib/painter.rb', line 106
def resize_to(destination)
transparency_index = CGD2::gdImageColorAllocateAlpha(destination.image_pointer, 255, 255, 255, Painter::ALPHA_TRANSPARENT)
CGD2::gdImageColorTransparent(destination.image_pointer, transparency_index)
CGD2::gdImageFill(destination.image_pointer, 0, 0, transparency_index)
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
|