Module: GDAL::RasterBandMixins::AlgorithmMethods::ClassMethods

Defined in:
lib/gdal/raster_band_mixins/algorithm_methods.rb

Instance Method Summary collapse

Instance Method Details

#compute_median_cut_pct(red_band, green_band, blue_band, colors, color_interpretation, progress_function: nil, progress_arg: nil) ⇒ GDAL::ColorTable

Compute the optimal PCT for RGB image. Implements a median cut algorithm to compute an “optimal” pseudo-color table for representing an input RGB image. This PCT could then be used with dither_rgb_to_pct to convert a 24-bit RGB image into an 8-bit pseudo-colored image.

Parameters:

  • red_band (GDAL::RasterBand, FFI::Pointer)
  • green_band (GDAL::RasterBand, FFI::Pointer)
  • blue_band (GDAL::RasterBand, FFI::Pointer)
  • colors (Integer)

    Number of colors to return; 2-256.

  • color_interpretation (FFI::GDAL::GDAL::PaletteInterp)

    The type of ColorTable to return.

  • progress_function (Proc, FFI:GDAL::GDAL.ProgressFunc) (defaults to: nil)
  • progress_arg (FFI::Pointer) (defaults to: nil)

    Usually used when when using a FFI::CPL::Progress.GDALCreateScaledProgress.

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gdal/raster_band_mixins/algorithm_methods.rb', line 27

def compute_median_cut_pct(red_band, green_band, blue_band,
  colors, color_interpretation, progress_function: nil, progress_arg: nil)
  color_table = GDAL::ColorTable.new(color_interpretation)

  FFI::GDAL::Alg.GDALComputeMedianCutPCT(
    red_band,
    green_band,
    blue_band,
    nil, # This isn't yet supported in GDAL.
    colors,
    color_table.c_pointer,
    progress_function,
    progress_arg
  )

  color_table
end

#dither_rgb_to_pct(red_band, green_band, blue_band, output_band, color_table, progress_function: nil, progress_arg: nil) ⇒ GDAL::RasterBand

24-bit to 8-bit conversion with dithering. Utilizes Floyd-Steinberg dithering, using the provided color table.

The red, green, and blue input bands do not necessarily need to come from the same file, but they must be the same width and height. They will be clipped to 8-bit during reading, so non-eight bit bands are generally inappropriate. Likewise, output_band will be written with 8-bit values and must match the width and height of the source bands.

The ColorTable cannot have more than 256 entries.

Parameters:

  • red_band (GDAL::RasterBand, FFI::Pointer)
  • green_band (GDAL::RasterBand, FFI::Pointer)
  • blue_band (GDAL::RasterBand, FFI::Pointer)
  • output_band (GDAL::RasterBand, FFI::Pointer)
  • color_table (GDAL::ColorTable, FFI::Pointer)
  • progress_function (Proc, FFI:GDAL::GDAL.ProgressFunc) (defaults to: nil)
  • progress_arg (FFI::Pointer) (defaults to: nil)

    Usually used when when using a FFI::CPL::Progress.GDALCreateScaledProgress.

Returns:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/gdal/raster_band_mixins/algorithm_methods.rb', line 66

def dither_rgb_to_pct(red_band, green_band, blue_band, output_band,
  color_table, progress_function: nil, progress_arg: nil)
  red_ptr = GDAL._pointer(GDAL::RasterBand, red_band)
  green_ptr = GDAL._pointer(GDAL::RasterBand, green_band)
  blue_ptr = GDAL._pointer(GDAL::RasterBand, blue_band)
  output_ptr = GDAL._pointer(GDAL::RasterBand, output_band)
  color_table_ptr = GDAL._pointer(GDAL::ColorTable, color_table)

  FFI::GDAL::Alg.GDALDitherRGB2PCT(
    red_ptr,
    green_ptr,
    blue_ptr,
    output_ptr,
    color_table_ptr,
    progress_function,
    progress_arg
  )

  output_band
end