Class: Libis::Format::Converter::Jp2Converter

Inherits:
Base
  • Object
show all
Defined in:
lib/libis/format/converter/jp2_converter.rb

Instance Attribute Summary

Attributes inherited from Base

#flags, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

inherited, using_temp, #using_temp

Constructor Details

#initializeJp2Converter

Returns a new instance of Jp2Converter.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/libis/format/converter/jp2_converter.rb', line 22

def initialize
  super
  @options = {
    color_xform: false,
    error_resilience: :ALL,
    lossless: true,
    progression_order: 'RLCP',
    tile_size: [1024, 1024],
    codeblock_size: [6, 6]
  }
end

Class Method Details

.input_typesObject



12
13
14
# File 'lib/libis/format/converter/jp2_converter.rb', line 12

def self.input_types
  i[TIFF JPG PNG BMP GIF PDF]
end

.output_types(format = nil) ⇒ Object



16
17
18
19
20
# File 'lib/libis/format/converter/jp2_converter.rb', line 16

def self.output_types(format = nil)
  return [] unless input_types.include?(format)

  [:JP2]
end

Instance Method Details

#codeblock_size(height = 6, width = 6) ⇒ Object



58
59
60
# File 'lib/libis/format/converter/jp2_converter.rb', line 58

def codeblock_size(height = 6, width = 6)
  @options[:codeblock_size] = [height, width]
end

#color_xform(flag = true) ⇒ Object



38
39
40
# File 'lib/libis/format/converter/jp2_converter.rb', line 38

def color_xform(flag = true)
  @options[:color_xform] = flag
end

#convert(source, target, format, opts = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/libis/format/converter/jp2_converter.rb', line 62

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

  FileUtils.mkpath(File.dirname(target))

  options = []

  @options.each do |key, value|
    case key
    when :color_xform
      options << '--set-output-j2k-color-xform' << (value ? 'YES' : 'NO')
    when :error_resilience
      options << '--set-output-j2k-error-resilience' << value.to_s
    when :lossless
      if value
        options << '--set-output-j2k-xform' << 'R53' << '5' << '--set-output-j2k-ratio' << '0'
      else
        options << '--set-output-j2k-xform' << 'I97' << '--set-output-j2k-psnr' << '46'
      end
    when :progression_order
      options << '--set-output-j2k-progression-order' << value.to_s
    when :tile_size
      options << '--set-output-j2k-tile-size' << value[0].to_s << value[1].to_s
    when :codeblock_size
      options << '--set-output-j2k-codeblock-size' << value[0].to_s << value[1].to_s
    else # rubocop:disable Style/EmptyElse
      # do nothing
    end
  end

  result = Libis::Tools::Command.run(
    Libis::Format::Config[:j2k_cmd],
    '--input-file-name', source,
    '--set-output-type', 'JP2',
    *options,
    '--output-file-name', target
  )

  result.merge(files: [target], converter: self.class.name)
end

#error_resilience(value = :ALL) ⇒ Object



42
43
44
# File 'lib/libis/format/converter/jp2_converter.rb', line 42

def error_resilience(value = :ALL)
  @options[:error_resilience] = value
end

#j2kdriver(_) ⇒ Object



34
35
36
# File 'lib/libis/format/converter/jp2_converter.rb', line 34

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

#lossless(value = true) ⇒ Object



46
47
48
# File 'lib/libis/format/converter/jp2_converter.rb', line 46

def lossless(value = true)
  @options[:lossless] = value
end

#progression_order(value = 'RLCP') ⇒ Object



50
51
52
# File 'lib/libis/format/converter/jp2_converter.rb', line 50

def progression_order(value = 'RLCP')
  @options[:progression_order] = value
end

#tile_size(width = 1024, height = 1024) ⇒ Object



54
55
56
# File 'lib/libis/format/converter/jp2_converter.rb', line 54

def tile_size(width = 1024, height = 1024)
  @options[:tile_size] = [width, height]
end