Advertisement
Guest User

Untitled

a guest
Oct 8th, 2012
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Windows.Media;
  8. using System.Windows.Media.Imaging;
  9. using OSGeo.GDAL;
  10. using OSGeo.OGR;
  11. using OSGeo.OSR;
  12. using Driver = OSGeo.GDAL.Driver;
  13. using Encoder = System.Drawing.Imaging.Encoder;
  14. using PixelFormat = System.Drawing.Imaging.PixelFormat;
  15.  
  16. namespace RasterLoader
  17. {
  18.     public class TiffConverter
  19.     {
  20.         private string _format = "GTiff";
  21.         private string _srcFileName = null;
  22.         private int _outBands = 3;
  23.         private int _bandNumber = 1;
  24.         private string[] _args;
  25.  
  26.         public TiffConverter(string[] args)
  27.         {
  28.             _args = args;
  29.         }
  30.  
  31.         public Dataset Convert8To24Bit()
  32.         {
  33.             Gdal.AllRegister();
  34.  
  35.             string[] argv = Gdal.GeneralCmdLineProcessor(_args, 0);
  36.  
  37.             int i = 1;
  38.             while (i < argv.Count())
  39.             {
  40.                 string arg = argv.ElementAt(i);
  41.                 switch (arg)
  42.                 {
  43.                     case "-of":
  44.                         i++;
  45.                         _format = argv[i];
  46.                         break;
  47.                     case "-b":
  48.                         i++;
  49.                         _bandNumber = int.Parse(argv[i]);
  50.                         break;
  51.                     case "-rgba":
  52.                         _outBands = 4;
  53.                         break;
  54.                     default:
  55.                         if (string.IsNullOrEmpty(_srcFileName))
  56.                         {
  57.                             _srcFileName = argv[i];
  58.                         }
  59.                         else
  60.                         {
  61.                             Usage();
  62.                         }
  63.                         break;
  64.                 }
  65.                 i++;
  66.             }
  67.  
  68.             string tmpFileName = _srcFileName + ".bak";
  69.  
  70.             // open source file
  71.             Dataset srcDS = Gdal.Open(_srcFileName, Access.GA_ReadOnly);
  72.             if (srcDS == null)
  73.             {
  74.                 throw new Exception("Unable to open " + _srcFileName + ".");
  75.             }
  76.  
  77.             Band srcBand = srcDS.GetRasterBand(_bandNumber);
  78.  
  79.             // ensure we recognise the driver
  80.             Driver dstDriver = Gdal.GetDriverByName(_format);
  81.             if (dstDriver == null)
  82.             {
  83.                 throw new Exception("\"" + _format + "\" not registered.");
  84.             }
  85.  
  86.             // build color table
  87.             int[][] lookup = new int[4][];
  88.             lookup[0] = Enumerable.Range(0, 256).ToArray();
  89.             lookup[1] = Enumerable.Range(0, 256).ToArray();
  90.             lookup[2] = Enumerable.Range(0, 256).ToArray();
  91.             lookup[3] = new int[256];
  92.  
  93.             for (i = 0; i < 256; i++)
  94.             {
  95.                 lookup[3][i] = 255;
  96.             }
  97.  
  98.             ColorTable ct = srcBand.GetRasterColorTable();
  99.  
  100.             if (ct != null)
  101.             {
  102.                 for (i = 0; i < Math.Min(256, ct.GetCount()); i++)
  103.                 {
  104.                     ColorEntry entry = ct.GetColorEntry(i);
  105.                     for (int j = 0; j < 4; j++)
  106.                     {
  107.                         switch (j)
  108.                         {
  109.                             case 0:
  110.                                 lookup[j][i] = entry.c1;
  111.                                 break;
  112.                             case 1:
  113.                                 lookup[j][i] = entry.c2;
  114.                                 break;
  115.                             case 2:
  116.                                 lookup[j][i] = entry.c3;
  117.                                 break;
  118.                             case 3:
  119.                                 lookup[j][i] = entry.c4;
  120.                                 break;
  121.                         }
  122.                     }
  123.                 }
  124.             }
  125.             else
  126.             {
  127.                 return srcDS;
  128.             }
  129.  
  130.             // create the working file
  131.             string tifFileName = string.Empty;
  132.             if (_format.Equals("GTiff", StringComparison.OrdinalIgnoreCase))
  133.             {
  134.                 tifFileName = tmpFileName;
  135.             }
  136.             else
  137.             {
  138.                 tifFileName = Path.Combine(Directory.GetParent(tmpFileName).Name, "temp.gif");
  139.             }
  140.  
  141. //            Driver gTiffDriver = Gdal.GetDriverByName("GTiff");
  142.             Driver gTiffDriver = Gdal.GetDriverByName("MEM");
  143.  
  144.             Dataset tifDS = gTiffDriver.Create(tifFileName, srcDS.RasterXSize, srcDS.RasterYSize, _outBands, DataType.GDT_Byte,
  145.                                                new string[] {});
  146.  
  147.             // we should copy projection information and so forth at this point
  148.             tifDS.SetProjection(srcDS.GetProjection());
  149.             double[] geotransform = new double[6];
  150.             srcDS.GetGeoTransform(geotransform);
  151.             tifDS.SetGeoTransform(geotransform);
  152.  
  153.             if (srcDS.GetGCPCount() > 0)
  154.             {
  155.                 tifDS.SetGCPs(srcDS.GetGCPs(), srcDS.GetGCPProjection());
  156.             }
  157.  
  158.             // do the processing one scanline at a time
  159.             for (int iY = 0; iY < srcDS.RasterYSize; iY++)
  160.             {
  161.                 byte[] srcData = new byte[srcDS.RasterXSize*1];
  162.                 srcBand.ReadRaster(0, iY, srcDS.RasterXSize, 1, srcData, srcDS.RasterXSize, 1, 0, 0);
  163.  
  164.                 for (int iBand = 0; iBand < _outBands; iBand++)
  165.                 {
  166.                     int[] bandLookup = lookup[iBand];
  167.  
  168.                     int[] dstData = new int[srcData.Count()];
  169.                     for (int index = 0; index < srcData.Count(); index++)
  170.                     {
  171.                         byte b = srcData[index];
  172.                         dstData[index] = bandLookup[b];
  173.                     }
  174.  
  175.                     tifDS.GetRasterBand(iBand + 1).WriteRaster(0, iY, srcDS.RasterXSize, 1, dstData,
  176.                                                                srcDS.RasterXSize, 1, 0, 0);
  177.                 }
  178.             }
  179.  
  180. //            if (tifFileName != _srcFileName)
  181. //            {
  182. //                tifDS = Gdal.Open(tifFileName, Access.GA_ReadOnly);
  183. //                dstDriver.CreateCopy(_srcFileName, tifDS, 0, new string[] { }, null, null);;
  184. //            }
  185.  
  186.             srcDS.Dispose();
  187.  
  188.             return tifDS;
  189.         }
  190.  
  191.         private void Usage()
  192.         {
  193.             Console.WriteLine("Usage: pct2rgb.py [-of format] [-b <band>] [-rgba] source_file dest_file");
  194.             throw new Exception("Bad arguments.");
  195.         }
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement