/src/gdal/frmts/zarr/zarr_v3_codec_gzip.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: GDAL |
4 | | * Purpose: Zarr driver, "gzip" codec |
5 | | * Author: Even Rouault <even dot rouault at spatialys.com> |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 2023, Even Rouault <even dot rouault at spatialys.com> |
9 | | * |
10 | | * SPDX-License-Identifier: MIT |
11 | | ****************************************************************************/ |
12 | | |
13 | | #include "zarr_v3_codec.h" |
14 | | |
15 | | #include "cpl_compressor.h" |
16 | | |
17 | | // Implements https://zarr-specs.readthedocs.io/en/latest/v3/codecs/gzip/index.html |
18 | | |
19 | | /************************************************************************/ |
20 | | /* ZarrV3CodecGZip() */ |
21 | | /************************************************************************/ |
22 | | |
23 | 0 | ZarrV3CodecGZip::ZarrV3CodecGZip() : ZarrV3CodecAbstractCompressor(NAME) |
24 | 0 | { |
25 | 0 | } |
26 | | |
27 | | /************************************************************************/ |
28 | | /* GetConfiguration() */ |
29 | | /************************************************************************/ |
30 | | |
31 | | /* static */ CPLJSONObject ZarrV3CodecGZip::GetConfiguration(int nLevel) |
32 | 0 | { |
33 | 0 | CPLJSONObject oConfig; |
34 | 0 | oConfig.Add("level", nLevel); |
35 | 0 | return oConfig; |
36 | 0 | } |
37 | | |
38 | | /************************************************************************/ |
39 | | /* ZarrV3CodecGZip::InitFromConfiguration() */ |
40 | | /************************************************************************/ |
41 | | |
42 | | bool ZarrV3CodecGZip::InitFromConfiguration( |
43 | | const std::string & /* osArrayName */, const CPLJSONObject &configuration, |
44 | | const ZarrArrayMetadata &oInputArrayMetadata, |
45 | | ZarrArrayMetadata &oOutputArrayMetadata, bool /* bEmitWarnings */) |
46 | 0 | { |
47 | 0 | m_pCompressor = CPLGetCompressor("gzip"); |
48 | 0 | m_pDecompressor = CPLGetDecompressor("gzip"); |
49 | 0 | if (!m_pCompressor || !m_pDecompressor) |
50 | 0 | { |
51 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "gzip compressor not available"); |
52 | 0 | return false; |
53 | 0 | } |
54 | | |
55 | 0 | m_oConfiguration = configuration.Clone(); |
56 | 0 | m_oInputArrayMetadata = oInputArrayMetadata; |
57 | | // byte->byte codec |
58 | 0 | oOutputArrayMetadata = oInputArrayMetadata; |
59 | |
|
60 | 0 | int nLevel = 6; |
61 | |
|
62 | 0 | if (configuration.IsValid()) |
63 | 0 | { |
64 | 0 | if (configuration.GetType() != CPLJSONObject::Type::Object) |
65 | 0 | { |
66 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
67 | 0 | "Codec gzip: configuration is not an object"); |
68 | 0 | return false; |
69 | 0 | } |
70 | | |
71 | 0 | for (const auto &oChild : configuration.GetChildren()) |
72 | 0 | { |
73 | 0 | if (oChild.GetName() != "level") |
74 | 0 | { |
75 | 0 | CPLError( |
76 | 0 | CE_Failure, CPLE_AppDefined, |
77 | 0 | "Codec gzip: configuration contains a unhandled member: %s", |
78 | 0 | oChild.GetName().c_str()); |
79 | 0 | return false; |
80 | 0 | } |
81 | 0 | } |
82 | | |
83 | 0 | const auto oLevel = configuration.GetObj("level"); |
84 | 0 | if (oLevel.IsValid()) |
85 | 0 | { |
86 | 0 | if (oLevel.GetType() != CPLJSONObject::Type::Integer) |
87 | 0 | { |
88 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
89 | 0 | "Codec gzip: level is not an integer"); |
90 | 0 | return false; |
91 | 0 | } |
92 | 0 | nLevel = oLevel.ToInteger(); |
93 | 0 | if (nLevel < 0 || nLevel > 9) |
94 | 0 | { |
95 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
96 | 0 | "Codec gzip: invalid value for level: %d", nLevel); |
97 | 0 | return false; |
98 | 0 | } |
99 | 0 | } |
100 | 0 | } |
101 | | |
102 | 0 | m_aosCompressorOptions.SetNameValue("LEVEL", CPLSPrintf("%d", nLevel)); |
103 | |
|
104 | 0 | return true; |
105 | 0 | } |
106 | | |
107 | | /************************************************************************/ |
108 | | /* ZarrV3CodecGZip::Clone() */ |
109 | | /************************************************************************/ |
110 | | |
111 | | std::unique_ptr<ZarrV3Codec> ZarrV3CodecGZip::Clone() const |
112 | 0 | { |
113 | 0 | auto psClone = std::make_unique<ZarrV3CodecGZip>(); |
114 | 0 | ZarrArrayMetadata oOutputArrayMetadata; |
115 | 0 | psClone->InitFromConfiguration(std::string(), m_oConfiguration, |
116 | 0 | m_oInputArrayMetadata, oOutputArrayMetadata, |
117 | 0 | /* bEmitWarnings = */ false); |
118 | 0 | return psClone; |
119 | 0 | } |