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); }
private void _SetDSWriteFile(ref Dataset dsOut) { if (_opt.HaveNullData) { _SetNullDataImage(dsOut); } // Band 8bits int xSize = dsOut.RasterXSize, ySize = dsOut.RasterYSize; byte[] pixelsAlpha = new byte[0]; if (_opt.HaveAlphaBand) { pixelsAlpha = null; pixelsAlpha = new byte[xSize * ySize]; } int idOrder = 0; for (int id = 0; id < _ds.RasterCount; id++) { Band bdOut = dsOut.GetRasterBand(id + 1); idOrder = _opt.HaveOrderBands ? _opt.OrderBands[id] : id + 1; Band bdIn = (_opt.HaveOverview) ? _ds.GetRasterBand(idOrder).GetOverview(0) : _ds.GetRasterBand(idOrder); byte[] pixels = new byte[xSize * ySize]; if (_opt.HaveSubset) { bdIn.ReadRaster(_opt.xoff, _opt.yoff, xSize, ySize, pixels, xSize, ySize, 0, 0); } else { bdIn.ReadRaster(0, 0, xSize, ySize, pixels, xSize, ySize, 0, 0); } if (_opt.HaveStretchStardDesv) { ImageProcessing.SetStretchStardDevi(bdIn, ref pixels, _opt.NumStretchStardDesv); } bdOut.WriteRaster(0, 0, xSize, ySize, pixels, xSize, ySize, 0, 0); if (_opt.HaveAlphaBand) { _PopulateAlphaPixels(ref pixels, ref pixelsAlpha); } bdIn.FlushCache(); bdOut.FlushCache(); bdIn.Dispose(); bdOut.Dispose(); id++; pixels = null; } if (_opt.HaveAlphaBand) { Band bdAlpha = dsOut.GetRasterBand(_ds.RasterCount + 1); bdAlpha.WriteRaster(0, 0, dsOut.RasterXSize, dsOut.RasterYSize, pixelsAlpha, dsOut.RasterXSize, dsOut.RasterYSize, 0, 0); bdAlpha.FlushCache(); bdAlpha.Dispose(); } }
private void WriteTileEachDS(int idOverview, ref Dataset dsOut, int xoff, int yoff, int xsize, int ysize, KMLBuildOptions options) { byte[] pixelsTile = new byte[options.TileSize * options.TileSize]; byte[] pixelsAlpha = new byte[0]; if (options.HaveAlphaBand) { pixelsAlpha = null; pixelsAlpha = new byte[options.TileSize * options.TileSize]; } int idOrder = 0; for (int id = 0; id < _ds.RasterCount; id++) { Band bdOut = dsOut.GetRasterBand(id + 1); idOrder = options.HaveBandsOrder ? options.BandsOrder[id] : id + 1; Band bdIn = (idOverview != -1) ? _ds.GetRasterBand(idOrder).GetOverview(idOverview) : _ds.GetRasterBand(idOrder); if (xsize == options.TileSize) { bdIn.ReadRaster(xoff, yoff, xsize, ysize, pixelsTile, xsize, ysize, 0, 0); if (options.HaveStretchStardDesv) { ImageProcessing.SetStretchStardDevi(bdIn, ref pixelsTile, options.StretchStardDesvNum); } } else { byte[] pixels = new byte[xsize * ysize]; bdIn.ReadRaster(xoff, yoff, xsize, ysize, pixels, xsize, ysize, 0, 0); if (options.HaveStretchStardDesv) { ImageProcessing.SetStretchStardDevi(bdIn, ref pixels, options.StretchStardDesvNum); } for (int iy = 0; iy < ysize; iy++) { System.Buffer.BlockCopy(pixels, iy * xsize, pixelsTile, iy * options.TileSize, xsize); } } bdOut.WriteRaster(0, 0, options.TileSize, options.TileSize, pixelsTile, options.TileSize, options.TileSize, 0, 0); if (options.HaveAlphaBand) { ImageProcessing.PopulateAlphaPixels(ref pixelsTile, ref pixelsAlpha); } bdOut.FlushCache(); bdIn.FlushCache(); bdIn.Dispose(); bdOut.Dispose(); } if (options.HaveAlphaBand) { Band bdAlpha = dsOut.GetRasterBand(_ds.RasterCount + 1); bdAlpha.WriteRaster(0, 0, options.TileSize, options.TileSize, pixelsAlpha, options.TileSize, options.TileSize, 0, 0); bdAlpha.FlushCache(); bdAlpha.Dispose(); } }