public Texture GenerateTexture(Size size, DataArea area, Device gDevice, GDALReader dataSrc)
        {
            Texture texture = new Texture(gDevice, size.Width, size.Height, 0, Usage.None, Format.A8R8G8B8, Pool.Managed);
            lock (this)
            {
                // find bands to use
                int r, g, b;
                dataSrc.Info.GetRGABands(out r, out g, out b);
                Size nativeSz = dataSrc.Info.Resolution;

                Band rBand = dataSrc.GetRasterBand(r + 1);
                Band gBand = dataSrc.GetRasterBand(g + 1);
                Band bBand = dataSrc.GetRasterBand(b + 1);

                // Note: This *should* work
                rData = new byte[nativeSz.Width * nativeSz.Height];
                gData = new byte[nativeSz.Width * nativeSz.Height];
                bData = new byte[nativeSz.Width * nativeSz.Height];
                rBand.ReadRaster(0, 0, nativeSz.Width, nativeSz.Height, rData, nativeSz.Width, nativeSz.Height, 0, 0);
                gBand.ReadRaster(0, 0, nativeSz.Width, nativeSz.Height, gData, nativeSz.Width, nativeSz.Height, 0, 0);
                bBand.ReadRaster(0, 0, nativeSz.Width, nativeSz.Height, bData, nativeSz.Width, nativeSz.Height, 0, 0);

                ftScaleTemp = new SizeF(1, 1);// (float)area.Area.Width / area.DataSize.Width, (float)area.Area.Height / area.DataSize.Height);
                ftScaleTemp = new SizeF(ftScaleTemp.Width * area.Area.Width, ftScaleTemp.Height * area.Area.Height);
                ftShiftTemp = new Size(area.Area.Location);
                ftNativeSz = nativeSz;
                if (area.Data is byte[])
                    TextureLoader.FillTexture(texture, FillTextureByte);
                /*else if (area.Data is float[])
                    TextureLoader.FillTexture(texture, FillTextureFloat);*/
            }
            return texture;
        }
        /// <summary>
        /// Initializes a new instance of the DigitalElevationMap class.
        /// </summary>
        /// <param name="mapSize">Size of the map</param>
        /// <param name="rDb"></param>
        /// <param name="devIf"></param>
        /// <param name="reader"></param>
        public DigitalElevationMap(Size mapSize, RasterDatabase.RasterDatabase rDb,
                                   DeviceInterface devIf, GDALReader reader)
            : base(new Vector3(-5f, -0.5f, -5f), new Vector3(5f, 1f, 5f))
        {
            this.mapSize = mapSize;
            maxDimension = mapSize.Width > mapSize.Height ? mapSize.Width : mapSize.Height;
            this.rDb = rDb;

            this.devIf = devIf;
            this.reader = reader;

            axisHelper = new Axis3DHelper(new Vector3(2.5f, 0.5f, 2.5f));
            rotAxisHelper = new RotationAxis3DHelper(new Vector3());
        }
        public static Bitmap SampleRGBDiffuseMap(GDALReader dataSrc, Size size)
        {
            // find bands to use
            int r, g, b;
            dataSrc.Info.GetRGABands(out r, out g, out b);
            Size nativeSz = dataSrc.Info.Resolution;

            Band rBand = dataSrc.GetRasterBand(r + 1);
            Band gBand = dataSrc.GetRasterBand(g + 1);
            Band bBand = dataSrc.GetRasterBand(b + 1);

            // Note: This *should* work
            byte[] rData = new byte[size.Width * size.Height];
            byte[] gData = new byte[size.Width * size.Height];
            byte[] bData = new byte[size.Width * size.Height];
            rBand.ReadRaster(0, 0, nativeSz.Width, nativeSz.Height, rData, size.Width, size.Height, 0, 0);
            gBand.ReadRaster(0, 0, nativeSz.Width, nativeSz.Height, gData, size.Width, size.Height, 0, 0);
            bBand.ReadRaster(0, 0, nativeSz.Width, nativeSz.Height, bData, size.Width, size.Height, 0, 0);

            // combine channel samples into image
            Bitmap bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
            BitmapData data = bitmap.LockBits(new Rectangle(new Point(), size), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

            unsafe
            {
                // write data to each pixel
                Bitmap24bitRGBPixel* pixels = (Bitmap24bitRGBPixel*)data.Scan0;
                for (int i = 0; i < rData.Length; i++)
                {
                    pixels->R = bData[i];
                    pixels->G = gData[i];
                    pixels->B = rData[i];

                    pixels++;
                }
            }
            bitmap.UnlockBits(data);

            return bitmap;
        }
        public void LoadVisualization(DataProfile dataProfile, DataProfile.SubProfile subProfile,
                                      IDataSourceReader dataSrcReader, DataSourceInfo dataSrcInfo)
        {
            renderFrame = true;
            renderingThread.Start();

            controlStatus = ControlStatus.Loading;
            rContext.LoadingLayer.SetProgress(0);
            rContext.LoadingLayer.SetText("Loading... DEM Geometry");
            rContext.LoadingLayer.Visible = true;
            Render();

            // load data from dlg into database
            database = new RasterDatabase.RasterDatabase();
            object filteredData = subProfile.Filter.FilterData(dataSrcReader);
            if (filteredData is byte[])
            {
                DataLayer dataLayer = new DataLayer("DEM", 8, "byte");
                dataLayer.AddArea(new ByteArea(new Rectangle(new Point(), dataSrcReader.Info.Resolution),
                                  new RectangleF(0, 0, 1, 1),
                                  (byte[])filteredData, dataSrcReader.Info.Resolution));
                database.AddLayer(dataLayer);
            }
            else if (filteredData is float[])
            {
                DataLayer dataLayer = new DataLayer("DEM", 32, "float");
                dataLayer.AddArea(new FloatArea(new Rectangle(new Point(), dataSrcReader.Info.Resolution),
                                  new RectangleF(0, 0, 1, 1),
                                  (float[])filteredData, dataSrcReader.Info.Resolution));
                database.AddLayer(dataLayer);
            }

            // load data source(s)
            DataSourceItem item = new DataSourceItem(dataSrcInfo.BppType,
                                                     (Bitmap)PreviewRasterizer.DrawRotatedBandPreview(dataSrcInfo.Bands,
                                                                                                      64, dataSrcInfo));
            heightDataSrcs.Add(item);

            // load diffuse sources
            // grey-scale for height
            GreyScaleDEMSampler srcImgSampler = new GreyScaleDEMSampler();
            Bitmap srcImg = srcImgSampler.GenerateBitmap(new Size(64, 64), database.Layers[0].Areas[0]);
            item = new DataSourceItem("Height", srcImg);
            item.DEMSampler = srcImgSampler;
                                      //(Bitmap)PreviewRasterizer.DrawRotatedBandPreview(new DataSourceInfo.DataBandInfo[] { new DataSourceInfo.DataBandInfo("Source", srcImg) },
                                                                                       //64, null));
            diffuseDataSrcs.Add(item);

            // load colour image if possible as other source (i.e the original if an RGB img)
            if (dataSrcReader.Info.SupportsRGB())
            {
                item = new DataSourceItem("SourceRGB",
                                          SourceDataDiffuseSampler.SampleRGBDiffuseMap((GDALReader)dataSrcReader,
                                                                                       new Size(64, 64)));
                diffuseDataSrcs.Add(item);
                reader = (GDALReader)dataSrcReader;
            }

            // height band range
            HeightBandRange hbr = new HeightBandRange();
            hbr.AddBand(0, Color.DarkGreen);
            hbr.AddBand(0.25f, Color.Blue);
            hbr.AddBand(0.4f, Color.DarkGreen);
            hbr.AddBand(0.5f, Color.Green);
            hbr.AddBand(0.8f, Color.Gray);
            hbr.AddBand(1, Color.White);
            HeightBandDEMSampler hBandSampler = new HeightBandDEMSampler(hbr);
            item = new DataSourceItem("HeightBands",
                                      hBandSampler.GenerateBitmap(new Size(64, 64), database.Layers[0].Areas[0]));
            item.DEMSampler = hBandSampler;
            diffuseDataSrcs.Add(item);

            // load database into visualization
            dem = new DigitalElevationMap(database.Area.Size, database, rContext.DevIf, (GDALReader)dataSrcReader/*,
                                          hBandSampler.GenerateBitmap(new Size(512, 512),
                                          database.Layers[0].Areas[0])*/
                                                                        );
            rContext.SetDEM(dem);

            // setup geometry layers
            geometryLayers.Add(new GeometryVisLayer("Diffuse", item.Thumbnail, true));
            geometryLayers.Add(new GeometryVisLayer("Overlay Grid", null, true));

            rContext.DevIf.LocalSettings["GeometryVisLayer.Diffuse"] = geometryLayers[0];
            rContext.DevIf.LocalSettings["GeometryVisLayer.Overlay"] = geometryLayers[1];

            // now load default texture
            SetDiffuseSource(0);

            rContext.LoadingLayer.SetProgress(100);
            Render();
            Thread.Sleep(500);

            rContext.LoadingLayer.Visible = false;
            controlStatus = ControlStatus.Idle;
            Render();
        }
 public void RebuildDiffuseTextures(GDALReader gdalReader)
 {
     //geom.RebuildDiffuseTextures(gdalReader);
 }
        public Texture GenerateTexture(Size size, DataArea area, Device gDevice, GDALReader dataSrc)
        {
            Texture texture = new Texture(gDevice, size.Width, size.Height, 0, Usage.None, Format.A8R8G8B8, Pool.Managed);
            lock (this)
            {
                // find bands to use
                int r, g, b;
                dataSrc.Info.GetRGABands(out r, out g, out b);
                Size nativeSz = dataSrc.Info.Resolution;

                Band rBand = dataSrc.GetRasterBand(r + 1);
                Band gBand = dataSrc.GetRasterBand(g + 1);
                Band bBand = dataSrc.GetRasterBand(b + 1);

                rData = new byte[nativeSz.Width * nativeSz.Height];
                gData = new byte[nativeSz.Width * nativeSz.Height];
                bData = new byte[nativeSz.Width * nativeSz.Height];
                rBand.ReadRaster(0, 0, nativeSz.Width, nativeSz.Height, rData, nativeSz.Width, nativeSz.Height, 0, 0);
                gBand.ReadRaster(0, 0, nativeSz.Width, nativeSz.Height, gData, nativeSz.Width, nativeSz.Height, 0, 0);
                bBand.ReadRaster(0, 0, nativeSz.Width, nativeSz.Height, bData, nativeSz.Width, nativeSz.Height, 0, 0);

                /*ftScaleTemp = new SizeF(1, 1);// (float)area.Area.Width / area.DataSize.Width, (float)area.Area.Height / area.DataSize.Height);
                ftScaleTemp = new SizeF(ftScaleTemp.Width * area.Area.Width, ftScaleTemp.Height * area.Area.Height);
                ftShiftTemp = new Size(area.Area.Location);
                ftNativeSz = nativeSz;*/
                ftScaleTemp = new SizeF((float)area.Area.Width / size.Width, (float)area.Area.Height / size.Height);
                ftShiftTemp = new Size(area.Area.Location);
                if (area.Data is byte[])
                {
                    // to heights
                    //TextureLoader.FillTexture(texture, FillTextureByteToHeights);
                    // generate normal acc
                    ftTexSz = size;
                    //texStream = texture.LockRectangle(0, LockFlags.None);
                    /*Texture normalTex = new Texture(gDevice, size.Width, size.Height, 0, Usage.None, Format.A8R8G8B8, Pool.Managed);
                    TextureLoader.FillTexture(normalTex, FillTextureByteToNormalAcc);*/
                    heights = new float[size.Width * size.Height];
                    int index = 0;
                    //SizeF nativeScale = new SizeF((float)nativeSz.Width / size.Width, (float)nativeSz.Height / size.Height);
                    for (int y = 0; y < size.Height; y++)
                    {
                        int yNative = (int)(ftShiftTemp.Height + (y * ftScaleTemp.Width));
                        for (int x = 0; x < size.Width; x++)
                        {
                            int xNative = (int)(ftShiftTemp.Width + (x * ftScaleTemp.Width));
                            int indexNative = (yNative * nativeSz.Width) + xNative;

                            float rValue = rData[indexNative] / 255f;
                            float gValue = gData[indexNative] / 255f;
                            float bValue = bData[indexNative] / 255f;

                            heights[index++] = (rValue + gValue + bValue) / 3f;
                        }
                    }
                    /*texture.UnlockRectangle(0);
                    texture.Dispose();*/

                    // calculate normals
                    Vector3[] normals;
                    GenerateMap(heights, size, out normals);

                    // write to texture
                    texStream = texture.LockRectangle(0, LockFlags.None);
                    foreach (Vector3 normal in normals)
                    {
                        texStream.WriteByte((byte)(((normal.Z / 2) + 0.5f) * 255));
                        texStream.WriteByte((byte)(((-normal.Y / 2) + 0.5f) * 255));
                        texStream.WriteByte((byte)(((normal.X / 2) + 0.5f) * 255));
                        texStream.WriteByte(255);
                    }
                    /*foreach (float height in heights)
                    {
                        texStream.WriteByte(0);
                        texStream.WriteByte((byte)(height * 255));
                        texStream.WriteByte(0);
                        texStream.WriteByte(255);
                    }*/
                    texture.UnlockRectangle(0);
                }
                return texture;
            }
        }