Class: FormatParser::CR2Parser

Inherits:
Object
  • Object
show all
Includes:
IOUtils
Defined in:
lib/parsers/cr2_parser.rb

Constant Summary collapse

TIFF_HEADER =
[0x49, 0x49, 0x2a, 0x00]
CR2_HEADER =
[0x43, 0x52, 0x02, 0x00]
PREVIEW_ORIENTATION_TAG =
0x0112
PREVIEW_RESOLUTION_TAG =
0x011a
PREVIEW_IMAGE_OFFSET_TAG =
0x0111
PREVIEW_IMAGE_BYTE_COUNT_TAG =
0x0117
EXIF_OFFSET_TAG =
0x8769
MAKERNOTE_OFFSET_TAG =
0x927c
AFINFO_TAG =
0x0012
AFINFO2_TAG =
0x0026
CAMERA_MODEL_TAG =
0x0110
SHOOT_DATE_TAG =
0x0132
EXPOSURE_TAG =
0x829a
APERTURE_TAG =
0x829d

Instance Method Summary collapse

Methods included from IOUtils

#safe_read, #safe_skip

Instance Method Details

#call(io) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
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
# File 'lib/parsers/cr2_parser.rb', line 20

def call(io)
  io = FormatParser::IOConstraint.new(io)

  tiff_header = safe_read(io, 8)

  # Check whether it's a CR2 file
  tiff_bytes = tiff_header[0..3].bytes
  magic_bytes = safe_read(io, 4).unpack('C4')

  return if !magic_bytes.eql?(CR2_HEADER) || !tiff_bytes.eql?(TIFF_HEADER)

  # Offset to IFD #0 where the preview image data is located
  # For more information about CR2 format,
  # see http://lclevy.free.fr/cr2/
  # and https://github.com/lclevy/libcraw2/blob/master/docs/cr2_poster.pdf
  if0_offset = parse_sequence_to_int tiff_header[4..7]

  parse_ifd_0(io, if0_offset)
  set_orientation(io, if0_offset)

  exif_offset = parse_ifd(io, if0_offset, EXIF_OFFSET_TAG)

  set_photo_info(io, exif_offset[0])

  makernote_offset = parse_ifd(io, exif_offset[0], MAKERNOTE_OFFSET_TAG)

  # Old Canon models have CanonAFInfo tags
  # Newer models have CanonAFInfo2 tags instead
  # See https://sno.phy.queensu.ca/~phil/exiftool/TagNames/Canon.html
  af_info = parse_ifd(io, makernote_offset[0], AFINFO2_TAG)
  unless af_info.nil?
    parse_dimensions(io, af_info[0], af_info[1], 8, 10)
  else
    af_info = parse_ifd(io, makernote_offset[0], AFINFO_TAG)
    parse_dimensions(io, af_info[0], af_info[1], 4, 6)
  end

  FormatParser::Image.new(
    format: :cr2,
    width_px: @width,
    height_px: @height,
    orientation: @orientation,
    image_orientation: @image_orientation,
    intrinsics: intrinsics
  )
end