Sha256: 05ed30ad8a53303ab6929aeb64304f7d9041413027bf3ac20799a5c0525acc00

Contents?: true

Size: 1.81 KB

Versions: 4

Compression:

Stored size: 1.81 KB

Contents

require 'open3'

module IiifPrint
  class TIFFDerivativeService < BaseDerivativeService
    self.target_extension = 'tiff'.freeze

    # For imagemagick commands, the output type is determined by the
    #   output file's extension.
    # TIFF (LZW, 8 bit grayscale)
    GRAY_CMD = 'convert %<source_file>s ' \
               '-depth 8 -colorspace Gray ' \
               '-compress lzw %<out_file>s'.freeze

    # Monochrome one-bit black/white TIFF, Group 4 compressed:
    MONO_CMD = 'convert %<source_file>s ' \
               '-depth 1 -monochrome -compress Group4 -type bilevel ' \
               '%<out_file>s'.freeze

    # sRBG color TIFF (8 bits per channel, lzw)
    COLOR_CMD = 'convert %<source_file>s ' \
                '-depth 24 ' \
                '-compress lzw %<out_file>s'.freeze

    def initialize(file_set)
      super(file_set)
    end

    # Get conversion command; command varies on whether or not we have
    #   JP2 source, and whether we have color or grayscale material.
    def convert_cmd
      source_path = @source_path
      source_path += '[0]' if @source_path.ends_with?('pdf')
      template = use_color? ? COLOR_CMD : GRAY_CMD
      template = MONO_CMD if one_bit?
      data = format(template, source_file: source_path, out_file: @dest_path)
      IiifPrint.copy_derivatives_from_data_store(stream: data, directives: { url: file_set.id.to_s, container: 'service_file', mime_type: mime_type_for(target_extension) })
      data
    end

    def create_derivatives(filename)
      # Base class takes care of loading @source_path, @dest_path
      super(filename)

      # no creation of TIFF deriviative if primary is TIFF
      return if mime_type == 'image/tiff'

      return jp2_convert if mime_type == 'image/jp2'
      # Otherwise, get, run imagemagick command to convert
      im_convert
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
iiif_print-3.0.1 lib/iiif_print/tiff_derivative_service.rb
iiif_print-3.0.0 lib/iiif_print/tiff_derivative_service.rb
iiif_print-2.0.1 lib/iiif_print/tiff_derivative_service.rb
iiif_print-2.0.0 lib/iiif_print/tiff_derivative_service.rb