Class: Pura::Tiff::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/pura/tiff/encoder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image) ⇒ Encoder

Returns a new instance of Encoder.



12
13
14
15
16
17
# File 'lib/pura/tiff/encoder.rb', line 12

def initialize(image)
  @image = image
  @width = image.width
  @height = image.height
  @pixels = image.pixels
end

Class Method Details

.encode(image, output_path) ⇒ Object



6
7
8
9
10
# File 'lib/pura/tiff/encoder.rb', line 6

def self.encode(image, output_path)
  data = new(image).encode
  File.binwrite(output_path, data)
  data.bytesize
end

Instance Method Details

#encodeObject



19
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/pura/tiff/encoder.rb', line 19

def encode
  out = String.new(encoding: Encoding::BINARY)

  # We'll build: header (8) + IFD + tag data + pixel data
  # Layout:
  #   0..7:     header
  #   8..N:     IFD (2 + num_tags*12 + 4 bytes for next IFD pointer)
  #   N+1..M:   tag value data that doesn't fit in 4 bytes
  #   M+1..end: pixel data (strips)

  pixel_data = @pixels
  strip_byte_count = pixel_data.bytesize

  # Tags we'll write (sorted by tag ID as required by TIFF spec)
  tags = []
  # ImageWidth (256)
  tags << make_tag(256, TYPE_LONG, 1, [@width])
  # ImageLength (257)
  tags << make_tag(257, TYPE_LONG, 1, [@height])
  # BitsPerSample (258) - 3 values: 8, 8, 8
  tags << make_tag(258, TYPE_SHORT, 3, [8, 8, 8])
  # Compression (259) - None
  tags << make_tag(259, TYPE_SHORT, 1, [1])
  # PhotometricInterpretation (262) - RGB
  tags << make_tag(262, TYPE_SHORT, 1, [2])
  # StripOffsets (273) - will be patched
  tags << make_tag(273, TYPE_LONG, 1, [0])
  # SamplesPerPixel (277)
  tags << make_tag(277, TYPE_SHORT, 1, [3])
  # RowsPerStrip (278) - all rows in one strip
  tags << make_tag(278, TYPE_LONG, 1, [@height])
  # StripByteCounts (279)
  tags << make_tag(279, TYPE_LONG, 1, [strip_byte_count])
  # XResolution (282)
  tags << make_tag(282, TYPE_RATIONAL, 1, [72, 1])
  # YResolution (283)
  tags << make_tag(283, TYPE_RATIONAL, 1, [72, 1])
  # ResolutionUnit (296) - inches
  tags << make_tag(296, TYPE_SHORT, 1, [2])

  tags.sort_by! { |t| t[:id] }

  num_tags = tags.size
  ifd_offset = 8
  ifd_size = 2 + (num_tags * 12) + 4 # count + entries + next IFD pointer

  # Calculate overflow data offset (for values > 4 bytes)
  overflow_offset = ifd_offset + ifd_size
  overflow_data = String.new(encoding: Encoding::BINARY)

  # Assign offsets for overflow values
  tags.each do |tag|
    value_bytes = tag_value_bytes(tag)
    if value_bytes.bytesize > 4
      tag[:value_offset] = overflow_offset + overflow_data.bytesize
      overflow_data << value_bytes
    else
      tag[:value_offset] = nil # fits inline
    end
  end

  # Pixel data offset
  pixel_offset = overflow_offset + overflow_data.bytesize

  # Patch StripOffsets to point to pixel data
  strip_tag = tags.find { |t| t[:id] == 273 }
  strip_tag[:values] = [pixel_offset]

  # Write header (little-endian)
  out << "II" # Little-endian
  out << [42].pack("v")               # Magic
  out << [ifd_offset].pack("V")       # IFD offset

  # Write IFD
  out << [num_tags].pack("v")

  tags.each do |tag|
    out << [tag[:id]].pack("v")
    out << [tag[:type]].pack("v")
    out << [tag[:count]].pack("V")

    value_bytes = tag_value_bytes(tag)
    if value_bytes.bytesize > 4
      out << [tag[:value_offset]].pack("V")
    else
      # Pad to 4 bytes
      padded = value_bytes + ("\x00".b * (4 - value_bytes.bytesize))
      out << padded
    end
  end

  # Next IFD offset (0 = no more IFDs)
  out << [0].pack("V")

  # Write overflow data
  out << overflow_data

  # Write pixel data
  out << pixel_data

  out
end