Sha256: c9c7cc597820fb6336020bb8990c4a3f7ec3214b9db742b7eca5a3f54f512435

Contents?: true

Size: 1.54 KB

Versions: 19

Compression:

Stored size: 1.54 KB

Contents

require 'delegate'

class FormatParser::DPXParser
  include FormatParser::IOUtils
  require_relative 'dpx_parser/dpx_structs'
  BE_MAGIC = 'SDPX'
  LE_MAGIC = BE_MAGIC.reverse

  class ByteOrderHintIO < SimpleDelegator
    def initialize(io, is_little_endian)
      super(io)
      @little_endian = is_little_endian
    end

    def le?
      @little_endian
    end
  end

  private_constant :ByteOrderHintIO

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

  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)

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

    image_aspect = w / h.to_f * pixel_aspect

    display_w = w
    display_h = h
    if image_aspect > 1
      display_h = (display_w / image_aspect).round
    else
      display_w = (display_h * image_aspect).round
    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,
    )
  end

  FormatParser.register_parser new, natures: :image, formats: :dpx
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
format_parser-0.25.4 lib/parsers/dpx_parser.rb
format_parser-0.25.3 lib/parsers/dpx_parser.rb
format_parser-0.25.2 lib/parsers/dpx_parser.rb
format_parser-0.25.1 lib/parsers/dpx_parser.rb
format_parser-0.25.0 lib/parsers/dpx_parser.rb
format_parser-0.24.2 lib/parsers/dpx_parser.rb
format_parser-0.24.1 lib/parsers/dpx_parser.rb
format_parser-0.24.0 lib/parsers/dpx_parser.rb
format_parser-0.23.1 lib/parsers/dpx_parser.rb
format_parser-0.23.0 lib/parsers/dpx_parser.rb
format_parser-0.22.1 lib/parsers/dpx_parser.rb
format_parser-0.22.0 lib/parsers/dpx_parser.rb
format_parser-0.21.1 lib/parsers/dpx_parser.rb
format_parser-0.21.0 lib/parsers/dpx_parser.rb
format_parser-0.20.1 lib/parsers/dpx_parser.rb
format_parser-0.20.0 lib/parsers/dpx_parser.rb
format_parser-0.19.0 lib/parsers/dpx_parser.rb
format_parser-0.18.0 lib/parsers/dpx_parser.rb
format_parser-0.17.0 lib/parsers/dpx_parser.rb