root/trunk/gdal/swig/csharp/apps/GDALRead.cs

Revision 13437, 11.1 kB (checked in by tamas, 1 year ago)

Added standardized copyright headers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /******************************************************************************
2  * $Id$
3  *
4  * Name:     GDALRead.cs
5  * Project:  GDAL CSharp Interface
6  * Purpose:  A sample app to read GDAL raster data.
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 using System.Drawing;
33 using System.Drawing.Imaging;
34
35 using OSGeo.GDAL;
36
37
38 /**
39
40  * <p>Title: GDAL C# GDALRead example.</p>
41  * <p>Description: A sample app to read GDAL raster data.</p>
42  * @author Tamas Szekeres (szekerest@gmail.com)
43  * @version 1.0
44  */
45
46
47
48 /// <summary>
49 /// A C# based sample to read GDAL raster data.
50 /// </summary>
51
52 class GDALRead {
53        
54         public static void usage()
55
56         {
57                 Console.WriteLine("usage: gdalread {GDAL dataset name} {output file name} {overview}");
58                 System.Environment.Exit(-1);
59         }
60  
61     public static void Main(string[] args)
62     {
63         int iOverview = -1;
64         if (args.Length < 2) usage();
65         if (args.Length == 3) iOverview = int.Parse(args[2]);
66
67         // Using early initialization of System.Console
68         Console.WriteLine("");
69
70         try
71         {
72             /* -------------------------------------------------------------------- */
73             /*      Register driver(s).                                             */
74             /* -------------------------------------------------------------------- */
75             Gdal.AllRegister();
76
77             /* -------------------------------------------------------------------- */
78             /*      Open dataset.                                                   */
79             /* -------------------------------------------------------------------- */
80             Dataset ds = Gdal.Open( args[0], Access.GA_ReadOnly );
81                
82             if (ds == null)
83             {
84                 Console.WriteLine("Can't open " + args[0]);
85                 System.Environment.Exit(-1);
86             }
87
88             Console.WriteLine("Raster dataset parameters:");
89             Console.WriteLine("  Projection: " + ds.GetProjectionRef());
90             Console.WriteLine("  RasterCount: " + ds.RasterCount);
91             Console.WriteLine("  RasterSize (" + ds.RasterXSize + "," + ds.RasterYSize + ")");
92            
93             /* -------------------------------------------------------------------- */
94             /*      Get driver                                                      */
95             /* -------------------------------------------------------------------- */ 
96             Driver drv = ds.GetDriver();
97
98             if (drv == null)
99             {
100                 Console.WriteLine("Can't get driver.");
101                 System.Environment.Exit(-1);
102             }
103            
104             Console.WriteLine("Using driver " + drv.LongName);
105
106             /* -------------------------------------------------------------------- */
107             /*      Get raster band                                                 */
108             /* -------------------------------------------------------------------- */
109             for (int iBand = 1; iBand <= ds.RasterCount; iBand++)
110             {
111                 Band band = ds.GetRasterBand(iBand);
112                 Console.WriteLine("Band " + iBand + " :");
113                 Console.WriteLine("   DataType: " + band.DataType);
114                 Console.WriteLine("   Size (" + band.XSize + "," + band.YSize + ")");
115                 Console.WriteLine("   PaletteInterp: " + band.GetRasterColorInterpretation().ToString());
116
117                 for (int iOver = 0; iOver < band.GetOverviewCount(); iOver++)
118                 {
119                     Band over = band.GetOverview(iOver);
120                     Console.WriteLine("      OverView " + iOver + " :");
121                     Console.WriteLine("         DataType: " + over.DataType);
122                     Console.WriteLine("         Size (" + over.XSize + "," + over.YSize + ")");
123                     Console.WriteLine("         PaletteInterp: " + over.GetRasterColorInterpretation().ToString());
124                 }
125             }
126
127             /* -------------------------------------------------------------------- */
128             /*      Processing the raster                                           */
129             /* -------------------------------------------------------------------- */
130             SaveBitmapBuffered(ds, args[1], iOverview);
131            
132         }
133         catch (Exception e)
134         {
135             Console.WriteLine("Application error: " + e.Message);
136         }
137     }
138
139     private static void SaveBitmapBuffered(Dataset ds, string filename, int iOverview)
140     {
141         // Get the GDAL Band objects from the Dataset
142         Band redBand = ds.GetRasterBand(1);
143        
144                 if (redBand.GetRasterColorInterpretation() == ColorInterp.GCI_PaletteIndex)
145                 {
146                         SaveBitmapPaletteBuffered(ds, filename, iOverview);
147                         return;
148                 }
149
150                 if (redBand.GetRasterColorInterpretation() == ColorInterp.GCI_GrayIndex)
151                 {
152                         SaveBitmapGrayBuffered(ds, filename, iOverview);
153                         return;
154                 }
155
156                 if (redBand.GetRasterColorInterpretation() != ColorInterp.GCI_RedBand)
157                 {
158             Console.WriteLine("Non RGB images are not supported by this sample! ColorInterp = " +
159                 redBand.GetRasterColorInterpretation().ToString());
160                         return;
161                 }
162
163                 if (ds.RasterCount < 3)
164                 {
165                         Console.WriteLine("The number of the raster bands is not enough to run this sample");
166                         System.Environment.Exit(-1);
167                 }
168
169         if (iOverview >= 0 && redBand.GetOverviewCount() > iOverview)
170             redBand = redBand.GetOverview(iOverview);
171
172         Band greenBand = ds.GetRasterBand(2);
173        
174                 if (greenBand.GetRasterColorInterpretation() != ColorInterp.GCI_GreenBand)
175                 {
176             Console.WriteLine("Non RGB images are not supported by this sample! ColorInterp = " +
177                 greenBand.GetRasterColorInterpretation().ToString());
178                         return;
179                 }
180
181         if (iOverview >= 0 && greenBand.GetOverviewCount() > iOverview)
182             greenBand = greenBand.GetOverview(iOverview);
183
184         Band blueBand = ds.GetRasterBand(3);
185        
186                 if (blueBand.GetRasterColorInterpretation() != ColorInterp.GCI_BlueBand)
187                 {
188             Console.WriteLine("Non RGB images are not supported by this sample! ColorInterp = " +
189                 blueBand.GetRasterColorInterpretation().ToString());
190                         return;
191                 }
192
193         if (iOverview >= 0 && blueBand.GetOverviewCount() > iOverview)
194             blueBand = blueBand.GetOverview(iOverview);
195
196         // Get the width and height of the raster
197         int width = redBand.XSize;
198         int height = redBand.YSize;
199
200         // Create a Bitmap to store the GDAL image in
201         Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb);
202
203         DateTime start = DateTime.Now;
204        
205         byte[] r = new byte[width * height];
206         byte[] g = new byte[width * height];
207         byte[] b = new byte[width * height];
208
209         redBand.ReadRaster(0, 0, width, height, r, width, height, 0, 0);
210         greenBand.ReadRaster(0, 0, width, height, g, width, height, 0, 0);
211         blueBand.ReadRaster(0, 0, width, height, b, width, height, 0, 0);
212         TimeSpan renderTime = DateTime.Now - start;
213         Console.WriteLine("SaveBitmapBuffered fetch time: " + renderTime.TotalMilliseconds + " ms");
214
215         int i, j;
216         for (i = 0; i< width; i++)
217         {
218             for (j=0; j<height; j++)
219             {
220                 Color newColor = Color.FromArgb(Convert.ToInt32(r[i+j*width]),Convert.ToInt32(g[i+j*width]), Convert.ToInt32(b[i+j*width]));
221                 bitmap.SetPixel(i, j, newColor);
222             }
223         }
224
225         bitmap.Save(filename);
226     }
227
228         private static void SaveBitmapPaletteBuffered(Dataset ds, string filename, int iOverview)
229         {
230                 // Get the GDAL Band objects from the Dataset
231                 Band band = ds.GetRasterBand(1);
232         if (iOverview >= 0 && band.GetOverviewCount() > iOverview)
233             band = band.GetOverview(iOverview);
234
235                 ColorTable ct = band.GetRasterColorTable();
236                 if (ct == null)
237                 {
238                         Console.WriteLine("   Band has no color table!");
239                         return;
240                 }
241
242                 if (ct.GetPaletteInterpretation() != PaletteInterp.GPI_RGB)
243                 {
244                         Console.WriteLine("   Only RGB palette interp is supported by this sample!");
245                         return;
246                 }
247
248                 // Get the width and height of the Dataset
249         int width = band.XSize;
250         int height = band.YSize;
251
252                 // Create a Bitmap to store the GDAL image in
253                 Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb);
254
255                 DateTime start = DateTime.Now;
256        
257                 byte[] r = new byte[width * height];
258                
259                 band.ReadRaster(0, 0, width, height, r, width, height, 0, 0);
260                 TimeSpan renderTime = DateTime.Now - start;
261                 Console.WriteLine("SaveBitmapBuffered fetch time: " + renderTime.TotalMilliseconds + " ms");
262
263                 int i, j;
264                 for (i = 0; i< width; i++)
265                 {
266                         for (j=0; j<height; j++)
267                         {
268                                 ColorEntry entry = ct.GetColorEntry(r[i+j*width]);
269                                 Color newColor = Color.FromArgb(Convert.ToInt32(entry.c1),Convert.ToInt32(entry.c2), Convert.ToInt32(entry.c3));
270                                 bitmap.SetPixel(i, j, newColor);
271                         }
272                 }
273
274                 bitmap.Save(filename);
275         }
276
277         private static void SaveBitmapGrayBuffered(Dataset ds, string filename, int iOverview)
278         {
279                 // Get the GDAL Band objects from the Dataset
280                 Band band = ds.GetRasterBand(1);
281         if (iOverview >= 0 && band.GetOverviewCount() > iOverview)
282             band = band.GetOverview(iOverview);
283
284                 // Get the width and height of the Dataset
285                 int width = band.XSize;
286         int height = band.YSize;
287
288                 // Create a Bitmap to store the GDAL image in
289                 Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb);
290
291                 DateTime start = DateTime.Now;
292        
293                 byte[] r = new byte[width * height];
294                
295                 band.ReadRaster(0, 0, width, height, r, width, height, 0, 0);
296                 TimeSpan renderTime = DateTime.Now - start;
297                 Console.WriteLine("SaveBitmapBuffered fetch time: " + renderTime.TotalMilliseconds + " ms");
298
299                 int i, j;
300                 for (i = 0; i< width; i++)
301                 {
302                         for (j=0; j<height; j++)
303                         {
304                                 Color newColor = Color.FromArgb(Convert.ToInt32(r[i+j*width]),Convert.ToInt32(r[i+j*width]), Convert.ToInt32(r[i+j*width]));
305                                 bitmap.SetPixel(i, j, newColor);
306                         }
307                 }
308
309                 bitmap.Save(filename);
310         }
311 }
Note: See TracBrowser for help on using the browser.