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_file, #output_file_id, #source_file

Constructor Details

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

Instance Method Details

#encode_file(dest_path, 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_file(dest_path, recipe, opts={})
  output_file = self.class.tmp_file('.jp2')
  if opts[:file_path]
    self.class.encode(opts[:file_path], recipe, output_file)
  else
    Hydra::Derivatives::TempfileService.create(source_file) do |f|
      self.class.encode(f.path, recipe, output_file)
    end
  end
  out_file = File.open(output_file, "rb")
  object.add_file(out_file.read, path: dest_path, mime_type: '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_file.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_file_name = args[:datastream] || output_file_id(name)
    encode_file(output_file_name, recipe, file_path: file_path)
    File.unlink(file_path) unless file_path.nil?
  end
end