Exemple #1
0
 private static void WritePaletteBuffered(Bitmap value, int xOffset, int yOffset, 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.");
     }
     int width = value.Width;
     int height = value.Height;
     BitmapData bData = value.LockBits(new Rectangle(0, 0, width, height),
                                       ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
     int stride = Math.Abs(bData.Stride);
     byte[] r = new byte[stride * height];
     Marshal.Copy(bData.Scan0, r, 0, r.Length);
     value.UnlockBits(bData);
     byte[] vals = new byte[width * 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++)
         {
             vals[row * width + col] = MatchColor(r, row * stride + col * 4, colorTable);
         }
     }
     first.WriteRaster(xOffset, yOffset, width, height, vals, width, height, 0, 0);
 }
Exemple #2
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 first_o;
            int width = xSize;
            int height = ySize;
            byte[] r = new byte[width * height];
            if (first.GetOverviewCount() > 0 && _overview >= 0)
            {
                first_o = first.GetOverview(_overview);
            }
            else
            {
                first_o = first;
            }
            if (xOffset + width > first_o.XSize)
            {
                width = first_o.XSize - xOffset;
            }
            if (yOffset + height > first_o.YSize)
            {
                height = first_o.YSize - yOffset;
            }

            first_o.ReadRaster(xOffset, yOffset, width, height, r, width, height, 0, 0);

            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 * stride]], 0, vals,
                               row * stride + col * bpp, 4);
                }
            }
            Marshal.Copy(vals, 0, bData.Scan0, vals.Length);
            result.UnlockBits(bData);
            return result;
        }