-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor
AnimatedGif
into a class rather than singleton methods.
- Loading branch information
1 parent
fa0f98e
commit 020191e
Showing
5 changed files
with
138 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# frozen_string_literal: true; encoding: ASCII-8BIT | ||
# | ||
# See COPYRIGHT for license details. | ||
|
||
class GD2::AnimatedGif | ||
class Frame < Struct.new(:ptr, :size) | ||
def read | ||
ptr.get_bytes(0, size.get_int(0)) | ||
end | ||
end | ||
|
||
def add(ptr, options = {}) | ||
size = FFI::MemoryPointer.new(:int) | ||
|
||
frame_ptr = if frames.empty? | ||
::GD2::GD2FFI.send(:gdImageGifAnimBeginPtr, ptr.image_ptr, size, -1, 0) | ||
else | ||
previous_frame_ptr = if options[:previous_frame] | ||
options[:previous_frame].image_ptr | ||
else | ||
FFI::Pointer::NULL | ||
end | ||
|
||
::GD2::GD2FFI.send(:gdImageGifAnimAddPtr, ptr.image_ptr, size, 0, 0, 0, options[:delay].to_i, 1, previous_frame_ptr) | ||
end | ||
|
||
raise LibraryError if frame_ptr.null? | ||
|
||
frames << Frame.new(frame_ptr, size) | ||
end | ||
|
||
def end | ||
size = FFI::MemoryPointer.new(:int) | ||
|
||
frame_ptr = ::GD2::GD2FFI.send(:gdImageGifAnimEndPtr, size) | ||
|
||
raise LibraryError if frame_ptr.null? | ||
|
||
frames << Frame.new(frame_ptr, size) | ||
end | ||
|
||
def export(filename_or_io) | ||
output = case filename_or_io | ||
when String | ||
File.open(filename_or_io, 'wb') | ||
else | ||
filename_or_io | ||
end | ||
|
||
frames.each do |frame| | ||
output.write(frame.read) | ||
end | ||
|
||
output.flush | ||
output.rewind | ||
|
||
output | ||
end | ||
|
||
private | ||
|
||
def frames | ||
return @frames if defined?(@frames) | ||
|
||
@frames = [] | ||
|
||
ObjectSpace.define_finalizer(@frames, frames_finalizer) | ||
|
||
@frames | ||
end | ||
|
||
def frames_finalizer | ||
proc do | ||
each do |frame| | ||
::GD2::GD2FFI.send(:gdFree, frame[:frame_ptr]) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true; encoding: ASCII-8BIT | ||
|
||
require './test/test_helper' | ||
|
||
class AnimatedGifTest < Minitest::Test | ||
include TestHelper | ||
|
||
def test_animation | ||
image1 = GD2::Image::IndexedColor.new(256, 256) | ||
|
||
black = image1.palette.allocate(GD2::Color[0, 0, 0]) | ||
white = image1.palette.allocate(GD2::Color[255, 255, 255]) | ||
lime = image1.palette.allocate(GD2::Color[0, 255, 0]) | ||
|
||
image1.draw do |pen| | ||
pen.color = white | ||
pen.line(64, 64, 192, 192) | ||
end | ||
|
||
image2 = GD2::Image::IndexedColor.new(256, 256) | ||
black = image2.palette.allocate(GD2::Color[0, 0, 0]) | ||
white = image2.palette.allocate(GD2::Color[255, 255, 255]) | ||
lime = image2.palette.allocate(GD2::Color[0, 255, 0]) | ||
|
||
image2.draw do |pen| | ||
pen.color = lime | ||
pen.rectangle(32, 64, 192, 192) | ||
end | ||
|
||
anim = GD2::AnimatedGif.new | ||
anim.add(image1) | ||
anim.add(image2, delay: 50) | ||
anim.add(image1, delay: 50) | ||
anim.end | ||
|
||
output = StringIO.new | ||
|
||
anim.export(output) | ||
|
||
assert(output.read == File.read('test/images/test_animated_gif.gif')) | ||
end | ||
end |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.