Class: DerivativeRodeo::Services::ImageJp2Service

Inherits:
BaseService
  • Object
show all
Defined in:
lib/derivative_rodeo/services/image_jp2_service.rb

Overview

A utility class for extracting technical metadata from a JP2.

Constant Summary collapse

TOKEN_MARKER_START =
"\xFF".force_encoding('BINARY')
TOKEN_MARKER_SIZ =
"\x51".force_encoding('BINARY')
TOKEN_IHDR =
'ihdr'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ ImageJp2Service

Returns a new instance of ImageJp2Service.



27
28
29
30
# File 'lib/derivative_rodeo/services/image_jp2_service.rb', line 27

def initialize(path)
  super()
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



25
26
27
# File 'lib/derivative_rodeo/services/image_jp2_service.rb', line 25

def path
  @path
end

Class Method Details

.technical_metadata_for(path:) ⇒ Derivative::Rodeo::TechnicalMetadata

Parameters:

  • path (String)

    path to jp2, for reading

Returns:

  • (Derivative::Rodeo::TechnicalMetadata)


21
22
23
# File 'lib/derivative_rodeo/services/image_jp2_service.rb', line 21

def self.(path:)
  new(path).
end

Instance Method Details

#technical_metadataObject

rubocop:disable Metrics/MethodLength



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/derivative_rodeo/services/image_jp2_service.rb', line 33

def 
  io = File.open(path, 'rb')
  io.seek(0, IO::SEEK_SET)
  validate_jp2(io)
  x_siz, y_siz = extract_jp2_dim(io)
  nc, bpc = extract_jp2_components(io)
  color = nc >= 3 ? 'color' : 'gray'
  TechnicalMetadata.new(
    color: bpc == 1 ? 'monochrome' : color,
    num_components: nc,
    bits_per_component: bpc,
    width: x_siz,
    height: y_siz,
    content_type: 'image/jp2'
  )
ensure
  io.close
end