Beispiel #1
0
        private void LoadTileDataSync(PositionKey key, ICachedTile tile)
        {
            var red   = RawDataPool.Capture();
            var green = RawDataPool.Capture();
            var blue  = RawDataPool.Capture();

            try
            {
                if (key == null || tile == null)
                {
                    return;
                }

                var name = key.ToString();
                var res  = _tileStore.Exists(name);
                if (res.IsFailure)
                {
                    tile.SetState(TileState.Empty);
                    return;
                }

                var version = res.ResultData?.CurrentVersion ?? 1;
                var img     = _tileStore.Read(name, "img", version);

                if (img.IsFailure || img.ResultData == null)
                {
                    tile.SetState(TileState.Empty);
                    return;
                }

                var fileData = InterleavedFile.ReadFromStream(img.ResultData);
                if (fileData != null)
                {
                    WaveletCompress.Decompress(fileData, red, green, blue, 1);
                }

                tile.EnsureDataReady();
                var packed = tile.GetTileData() ?? throw new Exception("Byte pool allocation failed");

                var end = TileImageSize * TileImageSize;
                for (int i = 0; i < end; i++)
                {
                    packed[4 * i + 0] = blue[i];
                    packed[4 * i + 1] = green[i];
                    packed[4 * i + 2] = red[i];

                    if (blue[i] >= 254 && green[i] >= 254 && red[i] >= 254)
                    {
                        packed[4 * i + 3] = 0;
                    }
                    else
                    {
                        packed[4 * i + 3] = 255;
                    }
                }

                tile.SetState(TileState.Ready);
            }
            catch (Exception ex)
            {
                Logging.WriteLogMessage("Failed to load tile data\r\n" + ex);
                tile?.MarkCorrupted();
            }
            finally {
                RawDataPool.Release(red);
                RawDataPool.Release(green);
                RawDataPool.Release(blue);
            }
        }