private void WriteTileToBackingStoreSync(PositionKey key, CachedTile tile) { if (key == null) { return; } if (tile == null) { return; } if (tile.State == TileState.Locked) { return; } var red = RawDataPool.Capture(); var green = RawDataPool.Capture(); var blue = RawDataPool.Capture(); try { var name = key.ToString(); if (tile.ImageIsBlank()) { _tileStore.Delete(name, "img"); tile.SetState(TileState.Empty); } else { var packed = tile.GetTileData(); if (packed == null) { return; } var end = TileImageSize * TileImageSize; for (int i = 0; i < end; i++) { blue[i] = packed[4 * i + 0]; green[i] = packed[4 * i + 1]; red[i] = packed[4 * i + 2]; } using (var ms = new MemoryStream()) { WaveletCompress.Compress(red, green, blue, tile.Width, tile.Height).WriteToStream(ms); ms.Seek(0, SeekOrigin.Begin); var ok = _tileStore.Store(name, "img", ms); if (ok.IsFailure) { throw new Exception("Storage error: DB might be corrupt.", ok.FailureCause); } } } } finally { RawDataPool.Release(red); RawDataPool.Release(green); RawDataPool.Release(blue); } }
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); } }