Exemple #1
0
        private Bitmap ReadGrayIndex(int xOffset, int yOffset, int xSize, int ySize)
        {
            Band firstBand;
            var  disposeBand = false;

            if (_overview >= 0 && _overviewCount > 0)
            {
                firstBand   = _band.GetOverview(_overview);
                disposeBand = true;
            }
            else
            {
                firstBand = _band;
            }
            GdalExtensions.NormalizeSizeToBand(firstBand.XSize, firstBand.YSize, xOffset, yOffset, xSize, ySize, out int width, out int height);
            byte[] rBuffer = firstBand.ReadBand(xOffset, yOffset, width, height, width, height);
            if (disposeBand)
            {
                firstBand.Dispose();
            }
            Bitmap result = GdalExtensions.GetBitmap(width, height, rBuffer, rBuffer, rBuffer, noDataValue: NoDataValue);

            rBuffer = null;
            return(result);
        }
Exemple #2
0
        private Bitmap ReadRgba(int xOffset, int yOffset, int xSize, int ySize)
        {
            if (Bands.Count < 4)
            {
                throw new GdalException("ARGB Format was indicated but there are only " + Bands.Count + " bands!");
            }
            Band aBand;
            Band rBand;
            Band gBand;
            Band bBand;
            var  disposeBand = false;

            if (_overview >= 0 && _overviewCount > 0)
            {
                rBand       = (Bands[0] as GdalRaster <T>)._band.GetOverview(_overview);
                gBand       = (Bands[1] as GdalRaster <T>)._band.GetOverview(_overview);
                bBand       = (Bands[2] as GdalRaster <T>)._band.GetOverview(_overview);
                aBand       = (Bands[3] as GdalRaster <T>)._band.GetOverview(_overview);
                disposeBand = true;
            }
            else
            {
                rBand = (Bands[0] as GdalRaster <T>)._band;
                gBand = (Bands[1] as GdalRaster <T>)._band;
                bBand = (Bands[2] as GdalRaster <T>)._band;
                aBand = (Bands[3] as GdalRaster <T>)._band;
            }

            GdalExtensions.NormalizeSizeToBand(rBand.XSize, rBand.YSize, xOffset, yOffset, xSize, ySize, out int width, out int height);
            byte[] aBuffer = aBand.ReadBand(xOffset, yOffset, width, height, width, height);
            byte[] rBuffer = rBand.ReadBand(xOffset, yOffset, width, height, width, height);
            byte[] gBuffer = gBand.ReadBand(xOffset, yOffset, width, height, width, height);
            byte[] bBuffer = bBand.ReadBand(xOffset, yOffset, width, height, width, height);
            if (disposeBand)
            {
                aBand.Dispose();
                rBand.Dispose();
                gBand.Dispose();
                bBand.Dispose();
            }
            Bitmap result = GdalExtensions.GetBitmap(width, height, rBuffer, gBuffer, bBuffer, aBuffer, NoDataValue);

            rBuffer = null;
            gBuffer = null;
            bBuffer = null;
            aBuffer = null;
            return(result);
        }
Exemple #3
0
        private Bitmap ReadGrayIndex(int xOffset, int yOffset, int xSize, int ySize, Band first)
        {
            Band firstO;
            var  disposeO = false;

            if (_overview >= 0 && first.GetOverviewCount() > 0)
            {
                firstO   = first.GetOverview(_overview);
                disposeO = true;
            }
            else
            {
                firstO = first;
            }

            int width, height;

            GdalExtensions.NormalizeSizeToBand(firstO.XSize, firstO.YSize, xOffset, yOffset, xSize, ySize, out width, out height);

            Bitmap result = new Bitmap(width, height, PixelFormat.Format32bppArgb);

            byte[] r = firstO.ReadBand(xOffset, yOffset, xSize, ySize, width, height);
            if (disposeO)
            {
                firstO.Dispose();
            }

            BitmapData bData  = result.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            int        stride = Math.Abs(bData.Stride);

            byte[] vals = new byte[height * stride];

            for (int row = 0; row < height; row++)
            {
                for (int col = 0; col < width; col++)
                {
                    byte value = r[(row * width) + col];
                    vals[(row * stride) + (col * 4)]     = value;
                    vals[(row * stride) + (col * 4) + 1] = value;
                    vals[(row * stride) + (col * 4) + 2] = value;
                    vals[(row * stride) + (col * 4) + 3] = 255;
                }
            }

            Marshal.Copy(vals, 0, bData.Scan0, vals.Length);
            result.UnlockBits(bData);
            return(result);
        }
Exemple #4
0
        private Bitmap ReadPaletteBuffered(int xOffset, int yOffset, int xSize, int ySize)
        {
            ColorTable ct = _band.GetRasterColorTable();

            if (ct == null)
            {
                throw new GdalException("Image was stored with a palette interpretation but has no color table.");
            }

            if (ct.GetPaletteInterpretation() != PaletteInterp.GPI_RGB)
            {
                throw new GdalException("Only RGB palette interpretation is currently supported by this " + " plug-in, " + ct.GetPaletteInterpretation() + " is not supported.");
            }

            int count = ct.GetCount();

            byte[][] colorTable = new byte[ct.GetCount()][];
            for (int i = 0; i < count; i++)
            {
                using (ColorEntry ce = ct.GetColorEntry(i))
                {
                    colorTable[i] = new[] { (byte)ce.c4, (byte)ce.c1, (byte)ce.c2, (byte)ce.c3 };
                }
            }
            ct.Dispose();

            Band firstBand;
            bool disposeBand = false;

            if (_overview >= 0 && _overviewCount > 0)
            {
                firstBand   = _band.GetOverview(_overview);
                disposeBand = true;
            }
            else
            {
                firstBand = _band;
            }
            GdalExtensions.NormalizeSizeToBand(firstBand.XSize, firstBand.YSize, xOffset, yOffset, xSize, ySize, out int width, out int height);
            byte[] indexBuffer = firstBand.ReadBand(xOffset, yOffset, width, height, width, height);
            if (disposeBand)
            {
                firstBand.Dispose();
            }
            byte[] rBuffer = new byte[indexBuffer.Length];
            byte[] gBuffer = new byte[indexBuffer.Length];
            byte[] bBuffer = new byte[indexBuffer.Length];
            byte[] aBuffer = new byte[indexBuffer.Length];
            for (int i = 0; i < indexBuffer.Length; i++)
            {
                int index = indexBuffer[i];
                aBuffer[i] = colorTable[index][0];
                rBuffer[i] = colorTable[index][1];
                gBuffer[i] = colorTable[index][2];
                bBuffer[i] = colorTable[index][3];
            }
            Bitmap result = GdalExtensions.GetBitmap(width, height, rBuffer, gBuffer, gBuffer, aBuffer, NoDataValue);

            rBuffer = null;
            gBuffer = null;
            bBuffer = null;
            aBuffer = null;
            return(result);
        }
Exemple #5
0
        private Bitmap ReadArgb(int xOffset, int yOffset, int xSize, int ySize, Band first, Dataset set)
        {
            if (set.RasterCount < 4)
            {
                throw new GdalException("ARGB Format was indicated but there are only " + set.RasterCount + " bands!");
            }

            Band firstO;
            Band redO;
            Band greenO;
            Band blueO;
            var  disposeO = false;

            Band red   = set.GetRasterBand(2);
            Band green = set.GetRasterBand(3);
            Band blue  = set.GetRasterBand(4);

            if (_overview >= 0 && first.GetOverviewCount() > 0)
            {
                firstO   = first.GetOverview(_overview);
                redO     = red.GetOverview(_overview);
                greenO   = green.GetOverview(_overview);
                blueO    = blue.GetOverview(_overview);
                disposeO = true;
            }
            else
            {
                firstO = first;
                redO   = red;
                greenO = green;
                blueO  = blue;
            }

            int width, height;

            GdalExtensions.NormalizeSizeToBand(red.XSize, red.YSize, xOffset, yOffset, xSize, ySize, out width, out height);
            Bitmap result = new Bitmap(width, height, PixelFormat.Format32bppArgb);

            byte[] a = firstO.ReadBand(xOffset, yOffset, xSize, ySize, width, height);
            byte[] r = redO.ReadBand(xOffset, yOffset, xSize, ySize, width, height);
            byte[] g = greenO.ReadBand(xOffset, yOffset, xSize, ySize, width, height);
            byte[] b = blueO.ReadBand(xOffset, yOffset, xSize, ySize, width, height);

            if (disposeO)
            {
                firstO.Dispose();
                redO.Dispose();
                greenO.Dispose();
                blueO.Dispose();
            }

            // Alpha disposed in caller
            red.Dispose();
            green.Dispose();
            blue.Dispose();

            BitmapData bData  = result.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            int        stride = Math.Abs(bData.Stride);

            byte[] vals = new byte[height * stride];

            for (int row = 0; row < height; row++)
            {
                for (int col = 0; col < width; col++)
                {
                    vals[(row * stride) + (col * 4)]     = b[(row * width) + col];
                    vals[(row * stride) + (col * 4) + 1] = g[(row * width) + col];
                    vals[(row * stride) + (col * 4) + 2] = r[(row * width) + col];
                    vals[(row * stride) + (col * 4) + 3] = a[(row * width) + col];
                }
            }

            Marshal.Copy(vals, 0, bData.Scan0, vals.Length);
            result.UnlockBits(bData);
            return(result);
        }
Exemple #6
0
        private Bitmap ReadPaletteBuffered(int xOffset, int yOffset, int xSize, int ySize, Band first)
        {
            ColorTable ct = first.GetRasterColorTable();

            if (ct == null)
            {
                throw new GdalException("Image was stored with a palette interpretation but has no color table.");
            }

            if (ct.GetPaletteInterpretation() != PaletteInterp.GPI_RGB)
            {
                throw new GdalException("Only RGB palette interpretation is currently supported by this " + " plug-in, " + ct.GetPaletteInterpretation() + " is not supported.");
            }

            Band firstO;
            bool disposeO = false;

            if (_overview >= 0 && first.GetOverviewCount() > 0)
            {
                firstO   = first.GetOverview(_overview);
                disposeO = true;
            }
            else
            {
                firstO = first;
            }

            int width, height;

            GdalExtensions.NormalizeSizeToBand(firstO.XSize, firstO.YSize, xOffset, yOffset, xSize, ySize, out width, out height);
            byte[] r = new byte[width * height];

            firstO.ReadRaster(xOffset, yOffset, width, height, r, width, height, 0, 0);
            if (disposeO)
            {
                firstO.Dispose();
            }

            Bitmap result = new Bitmap(width, height, PixelFormat.Format32bppArgb);

            BitmapData bData  = result.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            int        stride = Math.Abs(bData.Stride);

            const int Bpp = 4;

            byte[]   vals       = new byte[stride * height];
            byte[][] colorTable = new byte[ct.GetCount()][];
            for (int i = 0; i < ct.GetCount(); i++)
            {
                ColorEntry ce = ct.GetColorEntry(i);
                colorTable[i] = new[] { (byte)ce.c3, (byte)ce.c2, (byte)ce.c1, (byte)ce.c4 };
            }

            for (int row = 0; row < height; row++)
            {
                for (int col = 0; col < width; col++)
                {
                    Array.Copy(colorTable[r[col + (row * width)]], 0, vals, (row * stride) + (col * Bpp), 4);
                }
            }

            Marshal.Copy(vals, 0, bData.Scan0, vals.Length);
            result.UnlockBits(bData);
            return(result);
        }