Class: FormatParser::DPXParser

Inherits:
Object
  • Object
show all
Includes:
IOUtils
Defined in:
lib/parsers/dpx_parser.rb,
lib/parsers/dpx_parser/dpx_structs.rb

Defined Under Namespace

Classes: DPX

Constant Summary collapse

BE_MAGIC =
'SDPX'
LE_MAGIC =
BE_MAGIC.reverse
DPX_MIME_TYPE =

There is no official MIME type for DPX, so we have to invent something useful. We will prefix it with x- to indicate that it is a vendor subtype

'image/x-dpx'

Constants included from IOUtils

IOUtils::INTEGER_DIRECTIVES

Instance Method Summary collapse

Methods included from IOUtils

#read_bytes, #read_fixed_point, #read_int, #safe_read, #safe_skip, #skip_bytes

Instance Method Details

#call(io) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/parsers/dpx_parser.rb', line 31

def call(io)
  io = FormatParser::IOConstraint.new(io)
  magic = safe_read(io, 4)
  return unless [BE_MAGIC, LE_MAGIC].include?(magic)

  io.seek(0)

  dpx_structure = DPX.read_and_unpack(ByteOrderHintIO.new(io, magic == LE_MAGIC))

  w = dpx_structure.fetch(:image).fetch(:pixels_per_line)
  h = dpx_structure.fetch(:image).fetch(:lines_per_element)

  display_w = w
  display_h = h

  pixel_aspect_w = dpx_structure.fetch(:orientation).fetch(:horizontal_pixel_aspect)
  pixel_aspect_h = dpx_structure.fetch(:orientation).fetch(:vertical_pixel_aspect)

  # Find display height and width based on aspect only if the file structure has pixel aspects
  if pixel_aspect_h != 0 && pixel_aspect_w != 0
    pixel_aspect = pixel_aspect_w / pixel_aspect_h.to_f

    image_aspect = w / h.to_f * pixel_aspect

    if image_aspect > 1
      display_h = (display_w / image_aspect).round
    else
      display_w = (display_h * image_aspect).round
    end
  end

  FormatParser::Image.new(
    format: :dpx,
    width_px: w,
    height_px: h,
    display_width_px: display_w,
    display_height_px: display_h,
    intrinsics: dpx_structure,
    content_type: DPX_MIME_TYPE,
  )
end

#likely_match?(filename) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/parsers/dpx_parser.rb', line 27

def likely_match?(filename)
  filename =~ /\.dpx$/i
end