Class: Hydra::Derivatives::Jpeg2kImage

Inherits:
Processor
  • Object
show all
Includes:
ShellBasedProcessor
Defined in:
lib/hydra/derivatives/jpeg2k_image.rb

Instance Attribute Summary

Attributes inherited from Processor

#directives, #object, #source_name

Instance Method Summary collapse

Methods included from ShellBasedProcessor

#options_for

Methods inherited from Processor

#initialize, #output_datastream, #output_datastream_id, #source_datastream

Constructor Details

This class inherits a constructor from Hydra::Derivatives::Processor

Instance Method Details

#encode_datastream(dest_dsid, recipe, opts = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hydra/derivatives/jpeg2k_image.rb', line 28

def encode_datastream(dest_dsid, recipe, opts={})
  output_file = self.class.tmp_file('.jp2')
  if opts[:file_path]
    self.class.encode(opts[:file_path], recipe, output_file)
  else
    source_datastream.to_tempfile do |f|
      self.class.encode(f.path, recipe, output_file)
    end
  end
  out_file = File.open(output_file, "rb")
  object.add_file_datastream(out_file.read, dsid: dest_dsid, mimeType: 'image/jp2')
  File.unlink(output_file)
end

#processObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/hydra/derivatives/jpeg2k_image.rb', line 10

def process
  image = MiniMagick::Image.read(source_datastream.content)
  quality = image['%[channels]'] == 'gray' ? 'gray' : 'color'
  directives.each do |name, args|
    long_dim = self.class.long_dim(image)
    file_path = self.class.tmp_file('.tif')
    to_srgb = args.fetch(:to_srgb, true)
    if args[:resize] || to_srgb
      preprocess(image, resize: args[:resize], to_srgb: to_srgb, src_quality: quality)
    end
    image.write file_path
    recipe = self.class.kdu_compress_recipe(args, quality, long_dim)
    output_datastream_name = args[:datastream] || output_datastream_id(name)
    encode_datastream(output_datastream_name, recipe, file_path: file_path)
    File.unlink(file_path) unless file_path.nil?
  end
end