Class: Flipbook::APNG::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/flipbook/apng/writer.rb,
sig/flipbook.rbs

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, width:, height:, loop: true, optimize: true, buffered: nil) ⇒ Writer

Returns a new instance of Writer.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/flipbook/apng/writer.rb', line 12

def initialize(io, width:, height:, loop: true, optimize: true, buffered: nil)
  @io = io
  @width = Integer(width)
  @height = Integer(height)
  raise ArgumentError, "APNG dimensions must be between 1 and 2147483647" unless @width.between?(1, 2_147_483_647) && @height.between?(1, 2_147_483_647)
  raise TypeError, "optimize must be true or false" unless [true, false].include?(optimize)

  @loop = loop_count(loop)
  @optimize = optimize
  @buffered = buffered.nil? ? !seekable?(io) : buffered
  @frames = []
  @previous_image = nil
  @frame_count = 0
  @sequence = 0
  @closed = false
  write_header unless @buffered
end

Class Method Details

.open(path, **options) {|arg0| ... } ⇒ String

Parameters:

  • path (String)
  • width: (Integer)
  • height: (Integer)
  • loop: (bool, Integer)
  • optimize: (Boolean)

Yields:

Yield Parameters:

Yield Returns:

  • (void)

Returns:

  • (String)


8
9
10
# File 'lib/flipbook/apng/writer.rb', line 8

def self.open(path, **options, &block)
  Output.open(self, path, **options, &block)
end

Instance Method Details

#add(image, delay:) ⇒ Writer

Parameters:

Returns:

Raises:

  • (IOError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/flipbook/apng/writer.rb', line 30

def add(image, delay:)
  raise IOError, "APNG writer is closed" if @closed
  raise TypeError, "frame must be a Tessel::Image" unless image.is_a?(Tessel::Image)
  raise ArgumentError, "frame dimensions must match the canvas" unless image.width == @width && image.height == @height

  seconds = Timing.duration(delay)
  if @buffered
    @frames << [image.dup, seconds]
  else
    write_frame(image, seconds)
  end
  @frame_count += 1
  @previous_image = image.dup
  self
end

#close ⇒ Writer

Returns:

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/flipbook/apng/writer.rb', line 46

def close
  return self if @closed
  raise Error, "APNG contains no frames" if @frame_count.zero?

  if @buffered
    output = StringIO.new("".b)
    writer = self.class.new(output, width: @width, height: @height, loop: @loop, optimize: @optimize, buffered: false)
    @frames.each { |image, delay| writer.add(image, delay:) }
    writer.close
    @io.write(output.string)
  else
    write_chunk("IEND", "".b)
    patch_animation_control
  end
  @closed = true
  Output.finish(@io, @target_path, @target_mode) if @owns_io
  self
end