Exemple #1
0
        public ColorInterp GetRasterColorInterpretation()
        {
            ColorInterp ret = (ColorInterp)GdalPINVOKE.Band_GetRasterColorInterpretation(swigCPtr);

            if (GdalPINVOKE.SWIGPendingException.Pending)
            {
                throw GdalPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Exemple #2
0
        public static string GetColorInterpretationName(ColorInterp eColorInterp)
        {
            string ret = GdalPINVOKE.GetColorInterpretationName((int)eColorInterp);

            if (GdalPINVOKE.SWIGPendingException.Pending)
            {
                throw GdalPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Exemple #3
0
        public CPLErr SetRasterColorInterpretation(ColorInterp val)
        {
            CPLErr ret = (CPLErr)GdalPINVOKE.Band_SetRasterColorInterpretation(swigCPtr, (int)val);

            if (GdalPINVOKE.SWIGPendingException.Pending)
            {
                throw GdalPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Exemple #4
0
        private void ReadHeader()
        {
            DataType         = typeof(T);
            NumColumnsInFile = _dataset.RasterXSize;
            NumColumns       = NumColumnsInFile;
            NumRowsInFile    = _dataset.RasterYSize;
            NumRows          = NumRowsInFile;

            // Todo: look for prj file if GetProjection returns null.
            // Do we need to read this as an Esri string if we don't get a proj4 string?
            string projString = _dataset.GetProjection();

            Projection = ProjectionInfo.FromEsriString(projString);
            if (_band != null)
            {
                double val;
                _band.GetNoDataValue(out val, out _);
                base.NoDataValue = val;
                _overviewCount   = _band.GetOverviewCount();
                _colorInterp     = _band.GetColorInterpretation();
                int maxPixels = 2048 * 2048;
                if (_overviewCount <= 0 && NumColumnsInFile * NumRowsInFile > maxPixels)
                {
                    int ret = Helpers.CreateOverview(_dataset);
                    _overviewCount = _band.GetOverviewCount();
                }
            }

            double[] affine = new double[6];
            _dataset.GetGeoTransform(affine);
            if (_dataset.GetGCPCount() > 0)
            {
                var gcps = _dataset.GetGCPs();
                var ret  = Gdal.GCPsToGeoTransform(gcps, affine, 1);
                projString = _dataset.GetGCPProjection();
                Projection = ProjectionInfo.FromEsriString(projString);
            }
            else
            {
                // 解决无投影的栅格影像的反转显示问题
                if (affine[5] > 0)
                {
                    affine[5] = -affine[5];
                }
            }

            // in gdal (row,col) coordinates are defined relative to the top-left corner of the top-left cell
            // shift them by half a cell to give coordinates relative to the center of the top-left cell
            affine           = new AffineTransform(affine).TransfromToCorner(0.5, 0.5);
            ProjectionString = projString;
            Bounds           = new RasterBounds(NumRows, NumColumns, affine);
            PixelSpace       = Marshal.SizeOf(typeof(T));
        }
Exemple #5
0
        private Band getBandInfo(Band tBand, int iOverview, ColorInterp type)
        {
            Band tRetBand = null;

            switch (type)
            {
            case ColorInterp.GCI_RedBand:
                if (tBand.GetRasterColorInterpretation() != ColorInterp.GCI_RedBand)
                {
                    m_ctrlLogger.WarnFormat("Non RGB images are not supported by this sample! ColorInterp = {0}",
                                            tBand.GetRasterColorInterpretation().ToString());
                    return(tRetBand);
                }

                if (m_tGdalDs.RasterCount < 3)
                {
                    m_ctrlLogger.Warn("The number of the raster bands is not enough to run this sample");
                    return(tRetBand);
                }
                break;

            case ColorInterp.GCI_BlueBand:
                if (tBand.GetRasterColorInterpretation() != ColorInterp.GCI_BlueBand)
                {
                    m_ctrlLogger.WarnFormat("Non RGB images are not supported by this sample! ColorInterp = ",
                                            tBand.GetRasterColorInterpretation().ToString());
                    return(tRetBand);
                }
                break;

            case ColorInterp.GCI_GreenBand:
                if (tBand.GetRasterColorInterpretation() != ColorInterp.GCI_GreenBand)
                {
                    m_ctrlLogger.WarnFormat("Non RGB images are not supported by this sample! ColorInterp = ",
                                            tBand.GetRasterColorInterpretation().ToString());
                    return(tRetBand);
                }
                break;
            }
            if (iOverview >= 0 && tBand.GetOverviewCount() > iOverview)
            {
                tRetBand = tBand.GetOverview(iOverview);
            }
            else
            {
                tRetBand = tBand;
            }
            return(tRetBand);
        }
Exemple #6
0
        private static Image Load(Dataset dataset, ImageCodecCriteria criteria)
        {
            if (dataset == null)
            {
                throw new ArgumentNullException("dataset");
            }

            ColorInterp[] bandSemantic = new ColorInterp[dataset.RasterCount];

            for (int i = 1; i <= dataset.RasterCount; i++)
            {
                using (Band band = dataset.GetRasterBand(i)) {
                    bandSemantic[i - 1] = band.GetColorInterpretation();
                }
            }

            switch (bandSemantic[0])
            {
            case ColorInterp.GCI_GrayIndex:
                return(Load_GrayIndex(dataset, 1, criteria));

            default:
                throw new NotSupportedException();

            // Driver dependent definitions
            case ColorInterp.GCI_Undefined:
                switch (dataset.RasterCount)
                {
                case 1:
                    switch (dataset.GetDriver().ShortName)
                    {
                    case "SRTMHGT":
                    case "VRT":
                        return(Load_GrayIndex(dataset, 1, criteria));

                    default:
                        throw new NotSupportedException();
                    }

                default:
                    throw new NotSupportedException();
                }
            }
        }
Exemple #7
0
 /**
  * Retorna a banda para determinada cor (red, green, blue ou alfa)
  * O dataset deve ter 4 bandas
  * */
 public static Band GetBand(Dataset ImageDataSet, ColorInterp colorInterp)
 {
     if (colorInterp.Equals(ImageDataSet.GetRasterBand(1).GetRasterColorInterpretation()))
     {
         return(ImageDataSet.GetRasterBand(1));
     }
     else if (colorInterp.Equals(ImageDataSet.GetRasterBand(2).GetRasterColorInterpretation()))
     {
         return(ImageDataSet.GetRasterBand(2));
     }
     else if (colorInterp.Equals(ImageDataSet.GetRasterBand(3).GetRasterColorInterpretation()))
     {
         return(ImageDataSet.GetRasterBand(3));
     }
     else
     {
         return(ImageDataSet.GetRasterBand(4));
     }
 }
 public CPLErr SetRasterColorInterpretation(ColorInterp val)
 {
     CPLErr ret = (CPLErr)GdalPINVOKE.Band_SetRasterColorInterpretation(swigCPtr, (int)val);
     if (GdalPINVOKE.SWIGPendingException.Pending) throw GdalPINVOKE.SWIGPendingException.Retrieve();
     return ret;
 }
Exemple #9
0
        private IImageData OpenFile(string fileName)
        {
            var  dataset = Helpers.Open(fileName);
            bool hasOverviews;

            using (var red = dataset.GetRasterBand(1))
            {
                ColorInterp bandType = red.GetRasterColorInterpretation();
                if (bandType != ColorInterp.GCI_PaletteIndex &&
                    bandType != ColorInterp.GCI_GrayIndex &&
                    bandType != ColorInterp.GCI_RedBand &&
                    bandType != ColorInterp.GCI_AlphaBand)
                {
                    // This is an image, not a raster, so return null.
                    dataset.Dispose();
                    return(null);
                }
                hasOverviews = red.GetOverviewCount() > 0;
            }

            GdalImage result = new GdalImage(fileName);

            // Firstly, if there are pyramids inside of the GDAL file itself, we can just work with this directly,
            // without creating our own pyramid image.
            if ((result.Width > 8000 || result.Height > 8000) && !hasOverviews)
            {
                // For now, we can't get fast, low-res versions without some kind of pyramiding happening.
                // since that can take a while for huge images, I'd rather do this once, and create a kind of
                // standardized file-based pyramid system.  Maybe in future pyramid tiffs could be used instead?
                string pyrFile = Path.ChangeExtension(fileName, ".mwi");
                if (File.Exists(pyrFile))
                {
                    if (File.Exists(Path.ChangeExtension(pyrFile, ".mwh")))
                    {
                        return(new PyramidImage(fileName));
                    }
                    File.Delete(pyrFile);
                }

                GdalImageSource gs          = new GdalImageSource(fileName);
                PyramidImage    py          = new PyramidImage(pyrFile, gs.Bounds);
                int             width       = gs.Bounds.NumColumns;
                int             blockHeight = 64000000 / width;
                if (blockHeight > gs.Bounds.NumRows)
                {
                    blockHeight = gs.Bounds.NumRows;
                }
                int           numBlocks = (int)Math.Ceiling(gs.Bounds.NumRows / (double)blockHeight);
                ProgressMeter pm        = new ProgressMeter(ProgressHandler, "Copying Data To Pyramids", numBlocks * 2);
                //ProgressHandler.Progress("pyramid", 0, "Copying Data To Pyramids: 0% Complete");
                Application.DoEvents();
                for (int j = 0; j < numBlocks; j++)
                {
                    int h = blockHeight;
                    if (j == numBlocks - 1)
                    {
                        h = gs.Bounds.NumRows - j * blockHeight;
                    }
                    byte[] vals = gs.ReadWindow(j * blockHeight, 0, h, width, 0);

                    pm.CurrentValue = j * 2 + 1;
                    py.WriteWindow(vals, j * blockHeight, 0, h, width, 0);
                    pm.CurrentValue = (j + 1) * 2;
                }
                gs.Dispose();
                pm.Reset();
                py.ProgressHandler = ProgressHandler;
                py.CreatePyramids();
                py.WriteHeader(pyrFile);
                result.Dispose();

                return(py);
            }
            result.Open();
            return(result);
        }
Exemple #10
0
 public static string GetColorInterpretationName(ColorInterp eColorInterp)
 {
     string ret = GdalPINVOKE.GetColorInterpretationName((int)eColorInterp);
     if (GdalPINVOKE.SWIGPendingException.Pending) throw GdalPINVOKE.SWIGPendingException.Retrieve();
     return ret;
 }
Exemple #11
0
        public Bitmap GetBitmap(Size size, int[] Order, int SD)
        {
            /*
             * Original source from:
             * http://www.codeplex.com/SharpMap/WorkItem/AttachmentDownload.ashx?WorkItemId=8873&FileAttachmentId=3384
             * Namefile: GdalRasterLayer.cs
             * date download 10/04/2008
             *
             * Modify original source
             * Test only case in :
             * " else // Assume Palette Interpretation Name is RGB" - last conditional
             * in this case had hard changes.
             */
            int DsWidth  = _ds.RasterXSize;
            int DsHeight = _ds.RasterYSize;

            Bitmap     bitmap     = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
            int        iPixelSize = 3; //Format24bppRgb = byte[b,g,r]
            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, size.Width, size.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);

            try
            {
                unsafe
                {
                    for (int idBand = 1; idBand <= (_ds.RasterCount > iPixelSize ? iPixelSize : _ds.RasterCount); ++idBand)
                    {
                        byte[] buffer = new byte[size.Width * size.Height];

                        Band band = _ds.GetRasterBand(Order == null ? idBand : Order[idBand - 1]);

                        //band.ReadRaster(x1, y1, x1width, y1height, buffer, size.Width, size.Height, (int)GT.HorizontalPixelResolution, (int)GT.VerticalPixelResolution);
                        band.ReadRaster(0, 0, _ds.RasterXSize, _ds.RasterYSize, buffer, size.Width, size.Height, 0, 0);
                        if (SD != 0)
                        {
                            ImageProcessing.SetStretchStardDevi(band, ref buffer, SD);
                        }

                        int         p_indx = 0;
                        ColorInterp ci     = band.GetRasterColorInterpretation();

                        //int ch = 0;
                        //if (ci == ColorInterp.GCI_BlueBand) ch = 0;
                        //if (ci == ColorInterp.GCI_GreenBand) ch = 1;
                        //if (ci == ColorInterp.GCI_RedBand) ch = 2;

                        if (ci == ColorInterp.GCI_GrayIndex) //8bit Grayscale
                        {
                            for (int y = 0; y < size.Height; y++)
                            {
                                byte *row = (byte *)bitmapData.Scan0 + (y * bitmapData.Stride);
                                for (int x = 0; x < size.Width; x++, p_indx++)
                                {
                                    row[x * iPixelSize]     = buffer[p_indx];
                                    row[x * iPixelSize + 1] = buffer[p_indx];
                                    row[x * iPixelSize + 2] = buffer[p_indx];
                                }
                            }
                        }
                        else if (ci == ColorInterp.GCI_PaletteIndex)
                        {
                            // If raster type is Palette then the raster is an offset into the image's colour table
                            // Palettes can be of type: Gray, RGB, CMYK (Cyan/Magenta/Yellow/Black) and HLS (Hue/Light/Saturation)
                            // This code deals with only the Gray and RGB types
                            // For Grayscale the colour table contains: c1-Gray value
                            // For RGB the colour table contains: c1-Red c2-Green c3-Blue c4-Alpha
                            ColorTable table = band.GetRasterColorTable();
                            if (Gdal.GetPaletteInterpretationName(table.GetPaletteInterpretation()) == "Gray")
                            {
                                // Palette Grayscale
                                for (int y = 0; y < size.Height; y++)
                                {
                                    byte *row = (byte *)bitmapData.Scan0 + (y * bitmapData.Stride);
                                    for (int x = 0; x < size.Width; x++, p_indx++)
                                    {
                                        row[x * iPixelSize]     = (byte)table.GetColorEntry(buffer[p_indx]).c1;
                                        row[x * iPixelSize + 1] = (byte)table.GetColorEntry(buffer[p_indx]).c1;
                                        row[x * iPixelSize + 2] = (byte)table.GetColorEntry(buffer[p_indx]).c1;
                                    }
                                }
                            }
                            else // Assume Palette Interpretation Name is RGB
                            {
                                for (int y = 0; y < size.Height; y++)
                                {
                                    byte *row = (byte *)bitmapData.Scan0 + (y * bitmapData.Stride);
                                    for (int x = 0; x < size.Width; x++, p_indx++)
                                    {
                                        row[x * iPixelSize]     = (byte)table.GetColorEntry(buffer[p_indx]).c3;
                                        row[x * iPixelSize + 1] = (byte)table.GetColorEntry(buffer[p_indx]).c2;
                                        row[x * iPixelSize + 2] = (byte)table.GetColorEntry(buffer[p_indx]).c1;
                                    }
                                }
                            }
                        }
                        else  //Normal RGB
                        {
                            // Make changes for ShapMap(original)
                            for (int y = 0; y < size.Height; y++)
                            {
                                byte *row = (byte *)bitmapData.Scan0 + (y * bitmapData.Stride);

                                for (int x = 0; x < size.Width; x++, p_indx++)
                                {
                                    row[x * iPixelSize + (iPixelSize - idBand)] = buffer[p_indx];
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                bitmap.UnlockBits(bitmapData);
            }

            return(bitmap);
        }
Exemple #12
0
        public GDAL_Info(Dataset ds)
        {
            Projection = ds.GetProjectionRef();
            Resolution = new Size(ds.RasterXSize, ds.RasterYSize);
            Bands      = new DataBandInfo[ds.RasterCount];
            for (int i = 0; i < ds.RasterCount; i++)
            {
                Band band = ds.GetRasterBand(i + 1);
                Bands[i] = new DataBandInfo();
                int temp;
                band.GetMaximum(out Bands[i].MaxValue, out temp);
                band.GetMaximum(out Bands[i].MinValue, out temp);
                band.GetNoDataValue(out Bands[i].NODATAValue, out temp);
                ColorInterp clr = band.GetRasterColorInterpretation();
                switch (clr)
                {
                case ColorInterp.GCI_RedBand:
                    Bands[i].Name  = "RedBand";
                    Bands[i].Image = Resource1.red_layer;
                    BppType       += "R";
                    break;

                case ColorInterp.GCI_GreenBand:
                    Bands[i].Name  = "GreenBand";
                    Bands[i].Image = Resource1.green_layer;
                    BppType       += "G";
                    break;

                case ColorInterp.GCI_BlueBand:
                    Bands[i].Name  = "BlueBand";
                    Bands[i].Image = Resource1.blue_layer;
                    BppType       += "B";
                    break;

                default:
                    Bands[i].Name  = clr.ToString();
                    Bands[i].Image = null;
                    BppType       += "?";
                    break;
                }
                BppType += "[" + Gdal.GetDataTypeName(band.DataType) + "]";
                Bpp     += (ushort)Gdal.GetDataTypeSize(band.DataType);

                if (i + 1 < ds.RasterCount)
                {
                    BppType += ",";
                }
            }
            BppType += " (" + Bpp + ")";

            Driver = ds.GetDriver().LongName;

            Metadata       = ds.GetMetadata("");
            ImgMetadata    = ds.GetMetadata("IMAGE_STRUCTURE");
            SubDSMetadata  = ds.GetMetadata("SUBDATASETS");
            GeoLocMetadata = ds.GetMetadata("GEOLOCATION");

            GDALInfoGetPosition(ds, 0.0, 0.0, out UpperLeftX, out UpperLeftY);
            GDALInfoGetPosition(ds, 0.0, ds.RasterYSize, out UpperRightX, out UpperRightY);
            GDALInfoGetPosition(ds, ds.RasterXSize, 0.0, out LowerLeftX, out LowerLeftY);
            GDALInfoGetPosition(ds, ds.RasterXSize, ds.RasterYSize, out LowerRightX, out LowerRightY);
            GDALInfoGetPosition(ds, ds.RasterXSize / 2, ds.RasterYSize / 2, out CenterX, out CenterY);
        }
Exemple #13
0
        private static Bitmap ReadBitmapDirect(Dataset ds, int xOff, int yOff, int width, int height, int imageWidth, int imageHeight)
        {
            if (ds.RasterCount == 0)
            {
                return(null);
            }

            int[] bandMap = new int[4] {
                1, 1, 1, 1
            };
            int        channelCount = 1;
            bool       hasAlpha     = false;
            bool       isIndexed    = false;
            int        channelSize  = 8;
            ColorTable colortable   = null;

            // Evaluate the bands and find out a proper image transfer format
            for (int i = 0; i < ds.RasterCount; i++)
            {
                Band band = ds.GetRasterBand(i + 1);
                if (Gdal.GetDataTypeSize(band.DataType) > 8)
                {
                    channelSize = 16;
                }

                ColorInterp colorMode = band.GetRasterColorInterpretation();
                switch (colorMode)
                {
                case ColorInterp.GCI_AlphaBand:
                    channelCount = 4;
                    hasAlpha     = true;
                    bandMap[3]   = i + 1;
                    break;

                case ColorInterp.GCI_BlueBand:
                    if (channelCount < 3)
                    {
                        channelCount = 3;
                    }
                    bandMap[0] = i + 1;
                    break;

                case ColorInterp.GCI_RedBand:
                    if (channelCount < 3)
                    {
                        channelCount = 3;
                    }
                    bandMap[2] = i + 1;
                    break;

                case ColorInterp.GCI_GreenBand:
                    if (channelCount < 3)
                    {
                        channelCount = 3;
                    }
                    bandMap[1] = i + 1;
                    break;

                case ColorInterp.GCI_PaletteIndex:
                    colortable = band.GetRasterColorTable();
                    isIndexed  = true;
                    bandMap[0] = i + 1;
                    break;

                case ColorInterp.GCI_GrayIndex:
                    isIndexed  = true;
                    bandMap[0] = i + 1;
                    break;

                default:
                    // we create the bandmap using the dataset ordering by default
                    if (i < 4 && bandMap[i] == 0)
                    {
                        if (channelCount < i)
                        {
                            channelCount = i;
                        }
                        bandMap[i] = i + 1;
                    }
                    break;
                }
            }

            // find out the pixel format based on the gathered information
            PixelFormat pixelFormat;
            DataType    dataType;
            int         pixelSpace;

            if (isIndexed)
            {
                pixelFormat = PixelFormat.Format8bppIndexed;
                dataType    = DataType.GDT_Byte;
                pixelSpace  = 1;
            }
            else
            {
                if (channelCount == 1)
                {
                    if (channelSize > 8)
                    {
                        pixelFormat = PixelFormat.Format16bppGrayScale;
                        dataType    = DataType.GDT_Int16;
                        pixelSpace  = 2;
                    }
                    else
                    {
                        pixelFormat  = PixelFormat.Format24bppRgb;
                        channelCount = 3;
                        dataType     = DataType.GDT_Byte;
                        pixelSpace   = 3;
                    }
                }
                else
                {
                    if (hasAlpha)
                    {
                        if (channelSize > 8)
                        {
                            pixelFormat = PixelFormat.Format64bppArgb;
                            dataType    = DataType.GDT_UInt16;
                            pixelSpace  = 8;
                        }
                        else
                        {
                            pixelFormat = PixelFormat.Format32bppArgb;
                            dataType    = DataType.GDT_Byte;
                            pixelSpace  = 4;
                        }
                        channelCount = 4;
                    }
                    else
                    {
                        if (channelSize > 8)
                        {
                            pixelFormat = PixelFormat.Format48bppRgb;
                            dataType    = DataType.GDT_UInt16;
                            pixelSpace  = 6;
                        }
                        else
                        {
                            pixelFormat = PixelFormat.Format24bppRgb;
                            dataType    = DataType.GDT_Byte;
                            pixelSpace  = 3;
                        }
                        channelCount = 3;
                    }
                }
            }

            // Create a Bitmap to store the GDAL image in
            Bitmap bitmap = new Bitmap(imageWidth, imageHeight, pixelFormat);

            if (isIndexed)
            {
                // setting up the color table
                if (colortable != null)
                {
                    int          iCol = colortable.GetCount();
                    ColorPalette pal  = bitmap.Palette;
                    for (int i = 0; i < iCol; i++)
                    {
                        ColorEntry ce = colortable.GetColorEntry(i);
                        pal.Entries[i] = Color.FromArgb(ce.c4, ce.c1, ce.c2, ce.c3);
                    }
                    bitmap.Palette = pal;
                }
                else
                {
                    // grayscale
                    ColorPalette pal = bitmap.Palette;
                    for (int i = 0; i < 256; i++)
                    {
                        pal.Entries[i] = Color.FromArgb(255, i, i, i);
                    }
                    bitmap.Palette = pal;
                }
            }

            // Use GDAL raster reading methods to read the image data directly into the Bitmap
            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, imageWidth, imageHeight), ImageLockMode.ReadWrite, pixelFormat);

            try
            {
                int    stride = bitmapData.Stride;
                IntPtr buf    = bitmapData.Scan0;

                ds.ReadRaster(xOff, yOff, width, height, buf, imageWidth, imageHeight, dataType,
                              channelCount, bandMap, pixelSpace, stride, 1);
            }
            finally
            {
                bitmap.UnlockBits(bitmapData);
            }

            return(bitmap);
        }