Sha256: fd8a453a8e150d1f0114d9d2a5817d6315eeba7f1ce647dde934ba16a279760c

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module DynamicImage
  class ImageProcessor
    module Frames
      # Extracts a single frame from a multi-frame image.
      def frame(index)
        apply extract_frame(index)
      end

      # Returns the number of frames.
      def frame_count
        image.get("height") / size.y
      end

      private

      def each_frame(&block)
        return apply(block.call(image)) unless frame_count > 1

        apply(replace_frames(frames.map { |f| block.call(f) }))
      end

      def extract_frame(index)
        image.extract_area(0, (index * size.y), size.x, size.y)
      end

      def frames
        frame_count.times.map { |i| extract_frame(i) }
      end

      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
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dynamic_image-3.0.3 lib/dynamic_image/image_processor/frames.rb
dynamic_image-3.0.2 lib/dynamic_image/image_processor/frames.rb
dynamic_image-3.0.1 lib/dynamic_image/image_processor/frames.rb
dynamic_image-3.0.0 lib/dynamic_image/image_processor/frames.rb