Sha256: c7b2c13166866fc478cde58e9369b4726d16906e99567df1457f213e040d2719

Contents?: true

Size: 1.89 KB

Versions: 6

Compression:

Stored size: 1.89 KB

Contents

# encoding: utf-8

require_relative 'base'
require 'libis/format/identifier'

require 'mini_magick'

MiniMagick.logger.level = ::Logger::UNKNOWN

MiniMagick.configure do |config|
  # config.cli = :graphicsmagick
  config.validate_on_create = false
  config.validate_on_write = false
  config.whiny = false
end

module Libis
  module Format
    module Converter

      # noinspection RubyTooManyInstanceVariablesInspection,DuplicatedCode
      class ImageAssembler < Libis::Format::Converter::Base

        def self.input_types
          [:TIFF, :JPG, :PNG, :BMP, :GIF, :PDF, :JP2]
        end

        def self.output_types(format = nil)
          return [] unless input_types.include?(format) if format
          [:PDF, :TIFF, :GIF, :PBM, :PGM, :PPM]
        end

        def self.category
          :assembler
        end

        def image_assemble(_)
          #force usage of this converter
        end

        def quiet(v)
          @quiet = !!v
        end

        def convert(source, target, format, opts = {})
          super

          FileUtils.mkpath(File.dirname(target))

          if source.is_a? Array
            assemble(source, target, format)
          elsif File.directory?(source)
            source_list = Dir[File.join(source, '**', '*')].reject {|p| File.directory? p}
            assemble(source_list, target, format)
          else
            image = MiniMagick::Image.open(source) {|b| b.quiet}
            if image.pages.size > 1
              assemble(image.pages.map {|page| page.path}, target, format)
            else
              assemble([source], target, format)
            end
          end

          target

        end

        private

        def assemble(sources, target, format)
          MiniMagick::Tool::Convert.new do |b|
            sources.each {|source| b << source}
            convert.format(format)
            b << target
          end
        end

      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
libis-format-2.0.5 lib/libis/format/converter/image_assembler.rb
libis-format-2.0.4 lib/libis/format/converter/image_assembler.rb
libis-format-2.0.3 lib/libis/format/converter/image_assembler.rb
libis-format-2.0.2 lib/libis/format/converter/image_assembler.rb
libis-format-2.0.1 lib/libis/format/converter/image_assembler.rb
libis-format-2.0.0 lib/libis/format/converter/image_assembler.rb