Class: GD2::AnimatedGif

Inherits:
Object
  • Object
show all
Defined in:
lib/gd2/animated_gif.rb

Overview

See COPYRIGHT for license details.

Defined Under Namespace

Classes: Frame

Instance Method Summary collapse

Instance Method Details

#add(ptr, options = {}) ⇒ Object

Raises:


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gd2/animated_gif.rb', line 12

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

#endObject

Raises:


32
33
34
35
36
37
38
39
40
# File 'lib/gd2/animated_gif.rb', line 32

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

#export(filename_or_io) ⇒ Object


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gd2/animated_gif.rb', line 42

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