Module: JXL::Color::ICCProfile

Defined in:
lib/jxl/color/icc_profile.rb

Overview

rubocop:disable Metrics/ModuleLength

Constant Summary collapse

WHITE_POINTS =
{ 1 => [0.3127, 0.3290], 10 => [1.0 / 3, 1.0 / 3], 11 => [0.314, 0.351] }.freeze
PRIMARIES =
{
  1 => [[0.639998686, 0.330010138], [0.300003784, 0.600003357], [0.150002046, 0.059997204]],
  9 => [[0.708, 0.292], [0.170, 0.797], [0.131, 0.046]],
  11 => [[0.680, 0.320], [0.265, 0.690], [0.150, 0.060]]
}.freeze
BRADFORD =
[[0.8951, 0.2664, -0.1614], [-0.7502, 1.7135, 0.0367],
[0.0389, -0.0685, 1.0296]].freeze
BRADFORD_INVERSE =
[[0.9869929, -0.1470543, 0.1599627], [0.4323053, 0.5183603, 0.0492912],
[-0.0085287, 0.0400428, 0.9684867]].freeze
TRANSFER_NAMES =
{ 1 => "709", 8 => "Lin", 13 => "SRG", 16 => "PQ", 17 => "DCI", 18 => "HLG" }.freeze
INTENT_NAMES =
%w[Per Rel Sat Abs].freeze

Class Method Summary collapse

Class Method Details

.create(encoding) ⇒ Object



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
66
67
68
69
70
71
# File 'lib/jxl/color/icc_profile.rb', line 23

def create(encoding)
  raise UnsupportedFeatureError, "ICC profile for embedded encoding" if encoding.want_icc
  unless [0, 1].include?(encoding.colour_space)
    raise UnsupportedFeatureError, "ICC profile colour space #{encoding.colour_space}"
  end

  white = encoding.white_point == 2 ? encoding.white_point_xy : WHITE_POINTS.fetch(encoding.white_point)
  primaries = if encoding.colour_space == 1
                PRIMARIES[1]
              elsif encoding.primaries == 2
                encoding.primaries_xy
              else
                PRIMARIES.fetch(encoding.primaries)
              end
  raise CorruptError, "missing custom ICC white point" unless white
  raise CorruptError, "missing custom ICC primaries" unless primaries

  header = create_header(encoding)
  table = +"\0\0\0\0".b
  tags = +"".b
  entries = []
  add_tag!(entries, tags, "desc", mluc(description(encoding)))
  add_tag!(entries, tags, "cprt", mluc("CC0"))
  white_xyz = encoding.colour_space == 1 ? xy_to_xyz(white) : [0.964203, 1.0, 0.824905]
  add_tag!(entries, tags, "wtpt", xyz_tag(white_xyz))
  if encoding.colour_space.zero?
    adaptation = adapt_to_d50(white)
    add_tag!(entries, tags, "chad", fixed_tag("sf32", adaptation.flatten))
    add_cicp!(entries, tags, encoding)
    rgb_matrix(primaries, white).transpose.each_with_index do |column, index|
      add_tag!(entries, tags, %w[rXYZ gXYZ bXYZ][index], xyz_tag(column))
    end
  end
  trc_offset = tags.bytesize
  trc = transfer_tag(encoding)
  tags << trc << ("\0" * ((-trc.bytesize) & 3))
  trc_names = encoding.colour_space == 1 ? ["kTRC"] : %w[rTRC gTRC bTRC]
  trc_names.each { entries << [_1, trc_offset, tags.bytesize - trc_offset] }

  table[0, 4] = [entries.length].pack("N")
  base = header.bytesize + 4 + (entries.length * 12)
  entries.each { |name, offset, size| table << name << [base + offset, size].pack("N2") }
  profile = header + table + tags
  profile[0, 4] = [profile.bytesize].pack("N")
  checksum = profile.dup
  checksum[44, 4] = checksum[64, 4] = "\0" * 4
  profile[84, 16] = Digest::MD5.digest(checksum)
  profile
end