예제 #1
0
        private void PaintWavelet(int x, int y, int wWidth, int wHeight, int iWidth, int iHeight, ICancelTracker cancelTracker)
        {
            if (CancelTracker.Canceled(cancelTracker) || _gDS == null)
            {
                return;
            }
            int pixelSpace = 3;

            _bitmap = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
            BitmapData bitmapData = _bitmap.LockBits(new Rectangle(0, 0, iWidth, iHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

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

                for (int i = 1; i <= (_gDS.RasterCount > 3 ? 3 : _gDS.RasterCount); ++i)
                {
                    using (OSGeo.GDAL.Band band = _gDS.GetRasterBand(i))
                    {
                        int ch = 0;
                        switch ((ColorInterp)band.GetRasterColorInterpretation())
                        {
                        case ColorInterp.BlueBand:
                            ch = 0;
                            break;

                        case ColorInterp.GreenBand:
                            ch = 1;
                            break;

                        case ColorInterp.RedBand:
                            ch = 2;
                            break;
                        }
                        band.ReadRaster(x, y, wWidth, wHeight,
                                        new IntPtr(buf.ToInt64() + ch),
                                        iWidth, iHeight, OSGeo.GDAL.DataType.GDT_Byte, pixelSpace, stride);

                        band.Dispose();
                    }
                }
            }
            finally
            {
                if (_bitmap != null)
                {
                    _bitmap.UnlockBits(bitmapData);
                }
            }
        }
예제 #2
0
        private ICursor QueryImage(int x, int y)
        {
            unsafe
            {
                int          bandCount = _gDS.RasterCount;
                string[]     tags      = new string[bandCount + 2];
                object[]     values    = new object[bandCount + 2];
                List <Color> colors    = new List <Color>();

                for (int i = 1; i <= bandCount; ++i)
                {
                    OSGeo.GDAL.Band band = _gDS.GetRasterBand(i);

                    string bandName = "";
                    switch ((ColorInterp)band.GetRasterColorInterpretation())
                    {
                    case ColorInterp.BlueBand:
                        bandName = "(blue)";
                        break;

                    case ColorInterp.GreenBand:
                        bandName = "(green)";
                        break;

                    case ColorInterp.RedBand:
                        bandName = "(red)";
                        break;

                    case ColorInterp.GrayIndex:
                        for (int iColor = 0; iColor < 256; iColor++)
                        {
                            colors.Add(Color.FromArgb(255, iColor, iColor, iColor));
                        }
                        break;

                    case ColorInterp.PaletteIndex:
                        tags   = new string[tags.Length + 4];
                        values = new object[values.Length + 4];

                        OSGeo.GDAL.ColorTable colTable = band.GetRasterColorTable();
                        if (colTable == null)
                        {
                            break;
                        }
                        int colCount = colTable.GetCount();
                        for (int iColor = 0; iColor < colCount; iColor++)
                        {
                            OSGeo.GDAL.ColorEntry colEntry = colTable.GetColorEntry(iColor);
                            colors.Add(Color.FromArgb(
                                           colEntry.c4, colEntry.c1, colEntry.c2, colEntry.c3));
                        }

                        break;
                    }

                    int c = 0;

                    int *buf = &c;

                    band.ReadRaster(x, y, 1, 1,
                                    (IntPtr)buf,
                                    1, 1, OSGeo.GDAL.DataType.GDT_Int32, 4, 0);

                    band.Dispose();

                    tags[i + 1]   = "Band " + i.ToString() + " " + bandName;
                    values[i + 1] = c;

                    if (colors.Count > 0 && c >= 0 && c < colors.Count)
                    {
                        Color col = colors[c];
                        tags[i + 2]   = "Alpha";
                        values[i + 2] = col.A;
                        tags[i + 3]   = "Red";
                        values[i + 3] = col.R;
                        tags[i + 4]   = "Green";
                        values[i + 4] = col.G;
                        tags[i + 5]   = "Blue";
                        values[i + 5] = col.B;
                    }
                }

                tags[0] = "ImageX"; values[0] = x;
                tags[1] = "ImageY"; values[1] = y;

                return(new QueryCursor(tags, values));
            }
        }
예제 #3
0
        private void PaintImage(int x, int y, int wWidth, int wHeight, int iWidth, int iHeight, ICancelTracker cancelTracker)
        {
            if (CancelTracker.Canceled(cancelTracker) || _gDS == null)
            {
                return;
            }
            int pixelSpace = 3;

            _bitmap = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
            BitmapData bitmapData = _bitmap.LockBits(new Rectangle(0, 0, iWidth, iHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

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

                List <Color> colors = new List <Color>();
                for (int i = 1; i <= (_gDS.RasterCount > 3 ? 3 : _gDS.RasterCount); ++i)
                {
                    using (OSGeo.GDAL.Band band = _gDS.GetRasterBand(i))
                    {
                        int ch = 0;
                        switch ((ColorInterp)band.GetRasterColorInterpretation())
                        {
                        case ColorInterp.BlueBand:
                            ch = 0;
                            break;

                        case ColorInterp.GreenBand:
                            ch = 1;
                            break;

                        case ColorInterp.RedBand:
                            ch = 2;
                            break;

                        case ColorInterp.GrayIndex:
                            for (int iColor = 0; iColor < 256; iColor++)
                            {
                                colors.Add(Color.FromArgb(255, iColor, iColor, iColor));
                            }
                            break;

                        case ColorInterp.PaletteIndex:
                            OSGeo.GDAL.ColorTable colTable = band.GetRasterColorTable();
                            if (colTable == null)
                            {
                                break;
                            }
                            int colCount = colTable.GetCount();
                            for (int iColor = 0; iColor < colCount; iColor++)
                            {
                                OSGeo.GDAL.ColorEntry colEntry = colTable.GetColorEntry(iColor);
                                colors.Add(Color.FromArgb(
                                               colEntry.c4, colEntry.c1, colEntry.c2, colEntry.c3));
                            }

                            break;
                        }
                        band.ReadRaster(x, y, wWidth, wHeight,
                                        new IntPtr(buf.ToInt64() + ch),
                                        iWidth, iHeight, OSGeo.GDAL.DataType.GDT_Byte, pixelSpace, stride);

                        band.Dispose();
                    }
                }
                if (colors.Count > 0)
                {
                    unsafe
                    {
                        byte *ptr = (byte *)(bitmapData.Scan0);
                        for (int i = 0; i < bitmapData.Height; i++)
                        {
                            if (CancelTracker.Canceled(cancelTracker))
                            {
                                return;
                            }
                            for (int j = 0; j < bitmapData.Width; j++)
                            {
                                // write the logic implementation here
                                byte  c   = ptr[0];
                                Color col = colors[(int)c];
                                ptr[0] = col.B;
                                ptr[1] = col.G;
                                ptr[2] = col.R;
                                ptr   += pixelSpace;
                            }
                            ptr += bitmapData.Stride - bitmapData.Width * pixelSpace;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
            }
            finally
            {
                if (_bitmap != null)
                {
                    _bitmap.UnlockBits(bitmapData);
                }
            }
        }
예제 #4
0
        private void GetPreview(OSGeo.GDAL.Dataset dataset, System.Drawing.Size size, Graphics g, SharpMap.Geometries.BoundingBox bbox)
        {
            double[] geoTrans = new double[6];
            dataset.GetGeoTransform(geoTrans);
            GeoTransform GT = new GeoTransform(geoTrans);

            int DsWidth  = dataset.RasterXSize;
            int DsHeight = dataset.RasterYSize;

            Bitmap bitmap     = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
            int    iPixelSize = 3; //Format24bppRgb = byte[b,g,r]

            if (dataset != null)
            {
                /*
                 * if ((float)size.Width / (float)size.Height > (float)DsWidth / (float)DsHeight)
                 *  size.Width = size.Height * DsWidth / DsHeight;
                 * else
                 *  size.Height = size.Width * DsHeight / DsWidth;
                 */


                double left   = Math.Max(bbox.Left, _Envelope.Left);
                double top    = Math.Min(bbox.Top, _Envelope.Top);
                double right  = Math.Min(bbox.Right, _Envelope.Right);
                double bottom = Math.Max(bbox.Bottom, _Envelope.Bottom);


                int x1      = (int)GT.PixelX(left);
                int y1      = (int)GT.PixelY(top);
                int x1width = (int)GT.PixelXwidth(right - left);

                int y1height = (int)GT.PixelYwidth(bottom - top);


                bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
                BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, size.Width, size.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);

                try
                {
                    unsafe
                    {
                        for (int i = 1; i <= (dataset.RasterCount > 3 ? 3 : dataset.RasterCount); ++i)
                        {
                            byte[]          buffer = new byte[size.Width * size.Height];
                            OSGeo.GDAL.Band band   = dataset.GetRasterBand(i);

                            //band.ReadRaster(x1, y1, x1width, y1height, buffer, size.Width, size.Height, (int)GT.HorizontalPixelResolution, (int)GT.VerticalPixelResolution);
                            band.ReadRaster(x1, y1, x1width, y1height, buffer, size.Width, size.Height, 0, 0);

                            int p_indx = 0;
                            int ch     = 0;

                            //#warning Check correspondance between enum and integer values
                            if (band.GetRasterColorInterpretation() == OSGeo.GDAL.ColorInterp.GCI_BlueBand)
                            {
                                ch = 0;
                            }
                            if (band.GetRasterColorInterpretation() == OSGeo.GDAL.ColorInterp.GCI_GreenBand)
                            {
                                ch = 1;
                            }
                            if (band.GetRasterColorInterpretation() == OSGeo.GDAL.ColorInterp.GCI_RedBand)
                            {
                                ch = 2;
                            }
                            if (band.GetRasterColorInterpretation() != OSGeo.GDAL.ColorInterp.GCI_PaletteIndex)
                            {
                                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 + ch] = buffer[p_indx];
                                    }
                                }
                            }
                            else //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];
                                    }
                                }
                            }
                        }
                    }
                }
                finally
                {
                    bitmap.UnlockBits(bitmapData);
                }
            }
            g.DrawImage(bitmap, new System.Drawing.Point(0, 0));
        }