Class: SwarmSDK::Tools::ImageFormats::TiffBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_sdk/tools/image_formats/tiff_builder.rb

Overview

Builds TIFF image files from raw pixel data Supports RGB and grayscale color spaces

Class Method Summary collapse

Class Method Details

.build_gray_header(width, height, bpc) ⇒ String

Build TIFF header for grayscale images



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/swarm_sdk/tools/image_formats/tiff_builder.rb', line 42

def build_gray_header(width, height, bpc)
  long_tag  = ->(tag, count, value) { [tag, 4, count, value].pack("ssII") }
  short_tag = ->(tag, count, value) { [tag, 3, count, value].pack("ssII") }

  tag_count = 8
  header = [73, 73, 42, 8, tag_count].pack("ccsIs")

  tiff = header.dup
  tiff << short_tag.call(256, 1, width)                             # ImageWidth
  tiff << short_tag.call(257, 1, height)                            # ImageHeight
  tiff << short_tag.call(258, 1, bpc)                               # BitsPerSample
  tiff << short_tag.call(259, 1, 1)                                 # Compression (none)
  tiff << short_tag.call(262, 1, 1)                                 # PhotometricInterpretation (MinIsBlack)
  tiff << long_tag.call(273, 1, header.size + (tag_count * 12) + 4) # StripOffsets
  tiff << short_tag.call(277, 1, 1)                                 # SamplesPerPixel
  tiff << long_tag.call(279, 1, width * height)                     # StripByteCounts
  tiff << [0].pack("I")                                             # Next IFD pointer
  tiff
end

.build_rgb_header(width, height, bpc) ⇒ String

Build TIFF header for RGB images



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/swarm_sdk/tools/image_formats/tiff_builder.rb', line 15

def build_rgb_header(width, height, bpc)
  # Helper lambdas for TIFF tags
  long_tag  = ->(tag, count, value) { [tag, 4, count, value].pack("ssII") }
  short_tag = ->(tag, count, value) { [tag, 3, count, value].pack("ssII") }

  tag_count = 8
  header = [73, 73, 42, 8, tag_count].pack("ccsIs") # Little-endian TIFF

  tiff = header.dup
  tiff << short_tag.call(256, 1, width)                             # ImageWidth
  tiff << short_tag.call(257, 1, height)                            # ImageHeight
  tiff << long_tag.call(258, 3, header.size + (tag_count * 12) + 4) # BitsPerSample
  tiff << short_tag.call(259, 1, 1)                                 # Compression (none)
  tiff << short_tag.call(262, 1, 2)                                 # PhotometricInterpretation (RGB)
  tiff << long_tag.call(273, 1, header.size + (tag_count * 12) + 16) # StripOffsets
  tiff << short_tag.call(277, 1, 3)                                 # SamplesPerPixel
  tiff << long_tag.call(279, 1, width * height * 3)                 # StripByteCounts
  tiff << [0].pack("I")                                             # Next IFD pointer
  tiff << [bpc, bpc, bpc].pack("III")                               # BitsPerSample values
  tiff
end