GridFormat 0.4.0
I/O-Library for grid-like data structures
Loading...
Searching...
No Matches
decompress.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de>
2// SPDX-License-Identifier: MIT
8#ifndef GRIDFORMAT_COMPRESSION_DECOMPRESS_HPP_
9#define GRIDFORMAT_COMPRESSION_DECOMPRESS_HPP_
10
11#include <string>
12
13#include <gridformat/common/exceptions.hpp>
14#include <gridformat/common/serialization.hpp>
15
18
19namespace GridFormat::Compression {
20
25template<std::integral HeaderType, Concepts::BlockDecompressor Decompressor>
26void decompress(Serialization& in,
27 const CompressedBlocks<HeaderType>& blocks,
28 const Decompressor& block_decompressor) {
29 using Byte = typename Decompressor::ByteType;
30
31 const auto last_block_size = blocks.residual_block_size > 0 ? blocks.residual_block_size : blocks.block_size;
32 const auto out_size = blocks.block_size*(blocks.number_of_blocks-1) + last_block_size;
33
34 std::size_t in_offset = 0;
35 std::size_t out_offset = 0;
36 Serialization out{out_size};
37
38 const auto* in_data = in.template as_span_of<const Byte>().data();
39 auto* out_data = out.template as_span_of<Byte>().data();
40 std::ranges::for_each(std::views::iota(HeaderType{0}, blocks.number_of_blocks), [&] (HeaderType i) {
41 const auto out_block_size = (i == blocks.number_of_blocks - 1) ? last_block_size : blocks.block_size;
42
43 block_decompressor(
44 std::span{in_data + in_offset, blocks.compressed_block_sizes[i]},
45 std::span{out_data + out_offset, out_block_size}
46 );
47
48 in_offset += blocks.compressed_block_sizes[i];
49 out_offset += out_block_size;
50 });
51 if (out_offset != out.size())
52 throw SizeError(
53 "Unexpected number of bytes written: " + std::to_string(out_offset) + " vs. " + std::to_string(out.size())
54 );
55 in = out;
56}
57
58} // end namespace GridFormat::Compression
59
60#endif // GRIDFORMAT_COMPRESSION_DECOMPRESS_HPP_
Common classes used in the context of data compression.
Concepts related to data compression.
void decompress(Serialization &in, const CompressedBlocks< HeaderType > &blocks, const Decompressor &block_decompressor)
Decompress compressed data.
Definition: decompress.hpp:26
Stores the uncompressed/compressed block sizes after completion of a compression.
Definition: common.hpp:41