Exemplo n.º 1
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);
        }