Exemplo n.º 1
0
        private static DemTile Decode(Stream input)
        {
            // read meta info
            byte[] b = new byte[4 * sizeof(int)];
            input.Read(b, 0, b.Length);

            int width = BitConverter.ToInt32(b, 0);
            if (width < 1 || width > 1000)
                return null;

            int height = BitConverter.ToInt32(b, sizeof(int));
            if (height < 1 || height > 1000)
                return null;

            int avg = BitConverter.ToInt32(b, 2 * sizeof(int));
            if (avg < short.MinValue || avg > short.MaxValue)
                return null;

            int length = BitConverter.ToInt32(b, 3 * sizeof(int));
            if (length < 0 || length > 200000)
                return null;

            // read compressed data
            byte[] data = new byte[length];
            int offset = 0;
            while (offset < length)
            {
                int actual = input.Read(data, offset, length - offset);
                if (actual <= 0 || actual > length - offset)
                {
                    return null;
                }
                offset += actual;
            }

            short[,] pixels = new HDPhotoDecoder().Decode(data);

            if (pixels == null)
            {
                return null;
            }

            try
            {
                // read error-corrections
                BitReader r = new BitReader(input);
                for (int row = 0; row < height; row++)
                    for (int col = 0; col < width; col++)
                        if ((row & 0xF) == 0 || (col & 0xF) == 0)
                        {
                            // offset = row * width + col;
                            short err = ReadUnaryCorrection(r);
                            pixels[row, col] += err;
                        }
            }
            catch (EndOfStreamException)
            {
                return null;
            }

            // create tile
            for (int row = 0; row < height; row++)
                for (int col = 0; col < width; col++)
                    pixels[row, col] += (short)avg;
            DemTile tile = new DemTile(pixels);

            return tile;
        }
Exemplo n.º 2
0
        private bool LoadDemData()
        {
            if (!demFileExists)
            {

                return CreateDemFromParent();
            }
            bool useFloat = false;
            FileInfo fi = new FileInfo(DemFilename);

            if (dataset.Projection == ProjectionType.Mercator)
            {
                using (Stream demStream = File.Open(DemFilename, FileMode.Open))
                {
                    hdTile = DemCodec.Decompress(demStream);
                    demStream.Close();
                }

            }

            if (hdTile != null)
            {
                isHdTile = true;
                DemData = new double[demSize];
                int yh = 0;
                for (int yl = 0; yl < 33;  yl++)
                {
                    int xh = 0;
                    for (int xl = 0; xl < 33; xl++)
                    {
                        int indexI = xl + (32-yl) * 33;
                        DemData[indexI] = hdTile.AltitudeInMeters(yh, xh);
                        demAverage += DemData[indexI];

                        xh += 8;
                    }
                    yh += 8;

                }
                demAverage /= DemData.Length;

                return true;
            }

            if (fi.Length != 2178 && fi.Length != 1026 && fi.Length != 4356 && fi.Length != 2052)
            {
                return CreateDemFromParent();
            }

            if (fi.Length == 4356 || fi.Length == 2052)
            {
                useFloat = true;
            }

            FileStream fs = null;
             BinaryReader br = null;
            try
            {
                fs = new FileStream(DemFilename, FileMode.Open);
                br = new BinaryReader(fs);

                demAverage = 0;
                DemData = new double[demSize];

                Byte[] part = new Byte[4];

                for (int i = 0; i < DemData.Length; i++)
                {
                    if (useFloat)
                    {
                        DemData[i] = br.ReadSingle();
                        if (Double.IsNaN(DemData[i]))
                        {
                            DemData[i] = demAverage;
                        }
                    }
                    else
                    {
                        DemData[i] = br.ReadInt16();
                    }

                    demAverage += DemData[i];
                }

                demAverage /= DemData.Length;

                return true;
            }
            catch
            {
                if (File.Exists(DemFilename))
                {
                    File.Delete(DemFilename);
                }
                DemReady = false;
                return false;
            }
            finally
            {
                if (br != null)
                {
                    br.Close();
                }
                else if (fs != null)
                {
                    fs.Close();
                }
            }
        }
Exemplo n.º 3
0
        private static DemTile Decode(Stream input)
        {
            // read meta info
            byte[] b = new byte[4 * sizeof(int)];
            input.Read(b, 0, b.Length);

            int width = BitConverter.ToInt32(b, 0);

            if (width < 1 || width > 1000)
            {
                return(null);
            }

            int height = BitConverter.ToInt32(b, sizeof(int));

            if (height < 1 || height > 1000)
            {
                return(null);
            }

            int avg = BitConverter.ToInt32(b, 2 * sizeof(int));

            if (avg < short.MinValue || avg > short.MaxValue)
            {
                return(null);
            }

            int length = BitConverter.ToInt32(b, 3 * sizeof(int));

            if (length < 0 || length > 200000)
            {
                return(null);
            }

            // read compressed data
            byte[] data   = new byte[length];
            int    offset = 0;

            while (offset < length)
            {
                int actual = input.Read(data, offset, length - offset);
                if (actual <= 0 || actual > length - offset)
                {
                    return(null);
                }
                offset += actual;
            }

            short[,] pixels = new HDPhotoDecoder().Decode(data);

            if (pixels == null)
            {
                return(null);
            }

            try
            {
                // read error-corrections
                BitReader r = new BitReader(input);
                for (int row = 0; row < height; row++)
                {
                    for (int col = 0; col < width; col++)
                    {
                        if ((row & 0xF) == 0 || (col & 0xF) == 0)
                        {
                            // offset = row * width + col;
                            short err = ReadUnaryCorrection(r);
                            pixels[row, col] += err;
                        }
                    }
                }
            }
            catch (EndOfStreamException)
            {
                return(null);
            }

            // create tile
            for (int row = 0; row < height; row++)
            {
                for (int col = 0; col < width; col++)
                {
                    pixels[row, col] += (short)avg;
                }
            }
            DemTile tile = new DemTile(pixels);

            return(tile);
        }
Exemplo n.º 4
0
        public virtual void CleanUp(bool removeFromParent)
        {
            ReadyToRender = false;
            TextureReady = false;

            DemData = null;
            DemReady = false;
            hdTile = null;

            if (this.texture != null)
            {
                if (Earth3d.Logging) { Earth3d.WriteLogMessage("Tile:Texture Cleanup"); }
                BufferPool11.ReturnTexture(texture);
                this.texture = null;
            }
            else
            {
                if (Earth3d.Logging) { Earth3d.WriteLogMessage("Tile:Cleanup - no Texture"); }
            }

            if (this.vertexBuffer != null)
            {

                BufferPool11.ReturnPNTX2VertexBuffer(vertexBuffer);
                this.vertexBuffer = null;
            }

            if (this.indexBuffer != null)
            {
                foreach (IndexBuffer11 buffer in indexBuffer)
                {
                    if (buffer != null)
                    {
                        BufferPool11.ReturnShortIndexBuffer(buffer);
                    }
                }
                this.indexBuffer = null;
            }
        }