Class: GDAL::ColorTable

Inherits:
Object
  • Object
show all
Includes:
GDAL::ColorTableMixins::Extensions
Defined in:
lib/gdal/color_table.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GDAL::ColorTableMixins::Extensions

#color_entries, #color_entries_as_rgb, #color_entries_for

Constructor Details

#initialize(palette_interp_or_pointer) ⇒ ColorTable

Returns a new instance of ColorTable.

Raises:



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
# File 'lib/gdal/color_table.rb', line 40

def initialize(palette_interp_or_pointer)
  pointer =
    if FFI::GDAL::GDAL::PaletteInterp[palette_interp_or_pointer]
      ptr = FFI::GDAL::GDAL.GDALCreateColorTable(palette_interp_or_pointer)
      ptr.autorelease = false

      ptr
    elsif palette_interp_or_pointer.is_a? FFI::Pointer
      palette_interp_or_pointer
    end

  if !pointer.is_a?(FFI::Pointer) || pointer.null?
    raise GDAL::InvalidColorTable,
          "Unable to create #{self.class.name} from #{palette_interp_or_pointer}"
  end

  @c_pointer = pointer
  @color_entries = []

  case palette_interpretation
  when :GPI_Gray then extend GDAL::ColorTableTypes::Gray
  when :GPI_RGB then extend GDAL::ColorTableTypes::RGB
  when :GPI_CMYK then extend GDAL::ColorTableTypes::CMYK
  when :GPI_HLS then extend GDAL::ColorTableTypes::HLS
  else
    raise "Unknown PaletteInterpretation: #{palette_interpretation}"
  end
end

Instance Attribute Details

#c_pointerFFI::Pointer (readonly)



35
36
37
# File 'lib/gdal/color_table.rb', line 35

def c_pointer
  @c_pointer
end

Class Method Details

.new_pointer(color_table) ⇒ FFI::AutoPointer



21
22
23
24
25
# File 'lib/gdal/color_table.rb', line 21

def self.new_pointer(color_table)
  ptr = GDAL._pointer(GDAL::ColorTable, color_table, autorelease: false)

  FFI::AutoPointer.new(ptr, ColorTable.method(:release))
end

.release(pointer) ⇒ Object



28
29
30
31
32
# File 'lib/gdal/color_table.rb', line 28

def self.release(pointer)
  return unless pointer && !pointer.null?

  FFI::GDAL::GDAL.GDALDestroyColorTable(pointer)
end

Instance Method Details

#add_color_entry(index, one = nil, two = nil, three = nil, four = nil) ⇒ GDAL::ColorEntry

Add a new ColorEntry to the ColorTable. Valid values depend on the image type you’re working with (i.e. for Tiff, values can be between 0 and 65535). Values must also be relevant to the PaletteInterp type you’re working with.

rubocop:disable Metrics/ParameterLists



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/gdal/color_table.rb', line 139

def add_color_entry(index, one = nil, two = nil, three = nil, four = nil)
  entry = GDAL::ColorEntry.new
  entry.color1 = one if one
  entry.color2 = two if two
  entry.color3 = three if three
  entry.color4 = four if four

  FFI::GDAL::GDAL.GDALSetColorEntry(@c_pointer, index, entry.c_struct)
  @color_entries.insert(index, entry)

  entry
end

#cloneGDAL::ColorTable

Clones the ColorTable using the C API.



78
79
80
81
82
83
84
85
# File 'lib/gdal/color_table.rb', line 78

def clone
  ct_ptr = FFI::GDAL::GDAL.GDALCloneColorTable(@c_pointer)
  ct_ptr.autorelease = false

  return nil if ct_ptr.null?

  GDAL::ColorTable.new(ct_ptr)
end

#color_entry(index) ⇒ GDAL::ColorEntry



101
102
103
104
105
106
107
108
# File 'lib/gdal/color_table.rb', line 101

def color_entry(index)
  @color_entries.fetch(index) do
    color_entry = FFI::GDAL::GDAL.GDALGetColorEntry(@c_pointer, index)
    return nil if color_entry.null?

    GDAL::ColorEntry.new(color_entry)
  end
end

#color_entry_as_rgb(index) ⇒ GDAL::ColorEntry



112
113
114
115
116
117
118
119
120
# File 'lib/gdal/color_table.rb', line 112

def color_entry_as_rgb(index)
  entry = color_entry(index)
  return unless entry

  FFI::GDAL::GDAL.GDALGetColorEntryAsRGB(@c_pointer, index, entry.c_pointer)
  return nil if entry.c_pointer.null?

  entry
end

#color_entry_countInteger



95
96
97
# File 'lib/gdal/color_table.rb', line 95

def color_entry_count
  FFI::GDAL::GDAL.GDALGetColorEntryCount(@c_pointer)
end

#create_color_ramp!(start_index, start_color, end_index, end_color) ⇒ Integer

Automatically creates a color ramp from one color entry to another. It can be called several times to create multiple ramps in the same color table.



162
163
164
165
166
167
168
169
170
# File 'lib/gdal/color_table.rb', line 162

def create_color_ramp!(start_index, start_color, end_index, end_color)
  start_color_ptr = start_color.c_struct
  end_color_ptr = end_color.c_struct

  FFI::GDAL::GDAL.GDALCreateColorRamp(@c_pointer, start_index,
                                      start_color_ptr,
                                      end_index,
                                      end_color_ptr)
end

#destroy!Object



69
70
71
72
73
# File 'lib/gdal/color_table.rb', line 69

def destroy!
  ColorTable.release(@c_pointer)

  @c_pointer = nil
end

#palette_interpretationSymbol

Usually :GPI_RGB.



90
91
92
# File 'lib/gdal/color_table.rb', line 90

def palette_interpretation
  FFI::GDAL::GDAL.GDALGetPaletteInterpretation(@c_pointer)
end