module DynamicImage::ImageProcessor::Frames

Public Instance Methods

frame(index) click to toggle source

Extracts a single frame from a multi-frame image.

# File lib/dynamic_image/image_processor/frames.rb, line 7
def frame(index)
  apply extract_frame(index)
end
frame_count() click to toggle source

Returns the number of frames.

# File lib/dynamic_image/image_processor/frames.rb, line 12
def frame_count
  image.get("height") / size.y
end

Private Instance Methods

each_frame(&block) click to toggle source
# File lib/dynamic_image/image_processor/frames.rb, line 18
def each_frame(&block)
  return apply(block.call(image)) unless frame_count > 1

  apply(replace_frames(frames.map { |f| block.call(f) }))
end
extract_frame(index) click to toggle source
# File lib/dynamic_image/image_processor/frames.rb, line 24
def extract_frame(index)
  image.extract_area(0, (index * size.y), size.x, size.y)
end
frames() click to toggle source
# File lib/dynamic_image/image_processor/frames.rb, line 28
def frames
  frame_count.times.map { |i| extract_frame(i) }
end
replace_frames(new_frames) click to toggle source
# File lib/dynamic_image/image_processor/frames.rb, line 32
def replace_frames(new_frames)
  new_size = Vector2d(new_frames.first.size)
  new_image = blank_image.insert(
    Vips::Image.arrayjoin(new_frames, across: 1),
    0, 0, expand: true
  ).extract_area(0, 0, new_size.x, new_size.y * frame_count).copy
  new_image.set("page-height", new_size.y)
  new_image
end