| 1 |
/****************************************************************************** |
|---|
| 2 |
* $Id$ |
|---|
| 3 |
* |
|---|
| 4 |
* Name: GDALColorTable.cs |
|---|
| 5 |
* Project: GDAL CSharp Interface |
|---|
| 6 |
* Purpose: A sample app for demonstrating the usage of the ColorTable object. |
|---|
| 7 |
* Author: Tamas Szekeres, szekerest@gmail.com |
|---|
| 8 |
* |
|---|
| 9 |
****************************************************************************** |
|---|
| 10 |
* Copyright (c) 2007, Tamas Szekeres |
|---|
| 11 |
* |
|---|
| 12 |
* Permission is hereby granted, free of charge, to any person obtaining a |
|---|
| 13 |
* copy of this software and associated documentation files (the "Software"), |
|---|
| 14 |
* to deal in the Software without restriction, including without limitation |
|---|
| 15 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
|---|
| 16 |
* and/or sell copies of the Software, and to permit persons to whom the |
|---|
| 17 |
* Software is furnished to do so, subject to the following conditions: |
|---|
| 18 |
* |
|---|
| 19 |
* The above copyright notice and this permission notice shall be included |
|---|
| 20 |
* in all copies or substantial portions of the Software. |
|---|
| 21 |
* |
|---|
| 22 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
|---|
| 23 |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 24 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
|---|
| 25 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 26 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|---|
| 27 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
|---|
| 28 |
* DEALINGS IN THE SOFTWARE. |
|---|
| 29 |
*****************************************************************************/ |
|---|
| 30 |
|
|---|
| 31 |
using System; |
|---|
| 32 |
|
|---|
| 33 |
using OSGeo.GDAL; |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
/** |
|---|
| 37 |
* <p>Title: GDAL C# GDALColorTable example.</p> |
|---|
| 38 |
* <p>Description: A sample app for demonstrating the usage of the ColorTable object.</p> |
|---|
| 39 |
* @author Tamas Szekeres (szekerest@gmail.com) |
|---|
| 40 |
* @version 1.0 |
|---|
| 41 |
*/ |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
/// <summary> |
|---|
| 46 |
/// A C# based sample for demonstrating the usage of the ColorTable object. |
|---|
| 47 |
/// </summary> |
|---|
| 48 |
|
|---|
| 49 |
class GDALColorTable { |
|---|
| 50 |
|
|---|
| 51 |
public static void usage() |
|---|
| 52 |
|
|---|
| 53 |
{ |
|---|
| 54 |
Console.WriteLine("usage: gdalcolortable {source dataset} {destination file}"); |
|---|
| 55 |
System.Environment.Exit(-1); |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
public static void Main(string[] args) |
|---|
| 59 |
{ |
|---|
| 60 |
|
|---|
| 61 |
if (args.Length != 2) usage(); |
|---|
| 62 |
|
|---|
| 63 |
string file = args[0]; |
|---|
| 64 |
string file_out = args[1]; |
|---|
| 65 |
try { |
|---|
| 66 |
|
|---|
| 67 |
/* -------------------------------------------------------------------- */ |
|---|
| 68 |
/* Register driver(s). */ |
|---|
| 69 |
/* -------------------------------------------------------------------- */ |
|---|
| 70 |
Gdal.AllRegister(); |
|---|
| 71 |
|
|---|
| 72 |
Driver dv = null; |
|---|
| 73 |
Dataset ds = null, ds_out = null; |
|---|
| 74 |
Band ba = null, ba_out = null; |
|---|
| 75 |
ColorTable ct = null, ct_out = null; |
|---|
| 76 |
byte [] buffer; |
|---|
| 77 |
|
|---|
| 78 |
/* -------------------------------------------------------------------- */ |
|---|
| 79 |
/* Open dataset. */ |
|---|
| 80 |
/* -------------------------------------------------------------------- */ |
|---|
| 81 |
ds = Gdal.Open(file, Access.GA_ReadOnly); |
|---|
| 82 |
ba = ds.GetRasterBand(1); |
|---|
| 83 |
ct = ba.GetRasterColorTable(); |
|---|
| 84 |
|
|---|
| 85 |
if( ct != null ) |
|---|
| 86 |
Console.WriteLine( "Band has a color table with " + ct.GetCount() + " entries."); |
|---|
| 87 |
else |
|---|
| 88 |
{ |
|---|
| 89 |
Console.WriteLine( "Data source has no color table"); |
|---|
| 90 |
return; |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
buffer = new byte [ds.RasterXSize * ds.RasterYSize]; |
|---|
| 94 |
ba.ReadRaster(0, 0, ds.RasterXSize, ds.RasterYSize, buffer, |
|---|
| 95 |
ds.RasterXSize, ds.RasterYSize, 0, 0); |
|---|
| 96 |
|
|---|
| 97 |
/* -------------------------------------------------------------------- */ |
|---|
| 98 |
/* Get driver */ |
|---|
| 99 |
/* -------------------------------------------------------------------- */ |
|---|
| 100 |
dv = Gdal.GetDriverByName("GTiff"); |
|---|
| 101 |
|
|---|
| 102 |
ds_out = dv.Create(file_out, ds.RasterXSize, ds.RasterYSize, |
|---|
| 103 |
ds.RasterCount, ba.DataType, new string [] {}); |
|---|
| 104 |
ba_out = ds_out.GetRasterBand(1); |
|---|
| 105 |
ct_out = new ColorTable(PaletteInterp.GPI_RGB); |
|---|
| 106 |
|
|---|
| 107 |
ba_out.WriteRaster(0, 0, ds.RasterXSize, ds.RasterYSize, buffer, |
|---|
| 108 |
ds.RasterXSize, ds.RasterYSize, 0, 0); |
|---|
| 109 |
|
|---|
| 110 |
/* -------------------------------------------------------------------- */ |
|---|
| 111 |
/* Copying the colortable */ |
|---|
| 112 |
/* -------------------------------------------------------------------- */ |
|---|
| 113 |
for (int i = 0; i < ct.GetCount(); i++) |
|---|
| 114 |
{ |
|---|
| 115 |
ColorEntry ce = null, ce_out = null; |
|---|
| 116 |
|
|---|
| 117 |
ce = ct.GetColorEntry(i); |
|---|
| 118 |
ce_out = new ColorEntry(); |
|---|
| 119 |
|
|---|
| 120 |
ce_out.c1 = ce.c1; |
|---|
| 121 |
ce_out.c2 = ce.c2; |
|---|
| 122 |
ce_out.c3 = ce.c3; |
|---|
| 123 |
ce_out.c4 = ce.c4; |
|---|
| 124 |
|
|---|
| 125 |
ct_out.SetColorEntry(i, ce_out); |
|---|
| 126 |
|
|---|
| 127 |
ce.Dispose(); |
|---|
| 128 |
ce_out.Dispose(); |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
ba_out.SetRasterColorTable(ct_out); |
|---|
| 132 |
} |
|---|
| 133 |
catch (Exception e) |
|---|
| 134 |
{ |
|---|
| 135 |
Console.WriteLine("Application error: " + e.Message); |
|---|
| 136 |
} |
|---|
| 137 |
} |
|---|
| 138 |
} |
|---|