Line data Source code
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 56 : ZarrV3CodecGZip::ZarrV3CodecGZip() : ZarrV3CodecAbstractCompressor(NAME) 24 : { 25 56 : } 26 : 27 : /************************************************************************/ 28 : /* GetConfiguration() */ 29 : /************************************************************************/ 30 : 31 23 : /* static */ CPLJSONObject ZarrV3CodecGZip::GetConfiguration(int nLevel) 32 : { 33 23 : CPLJSONObject oConfig; 34 23 : oConfig.Add("level", nLevel); 35 23 : return oConfig; 36 : } 37 : 38 : /************************************************************************/ 39 : /* ZarrV3CodecGZip::InitFromConfiguration() */ 40 : /************************************************************************/ 41 : 42 56 : bool ZarrV3CodecGZip::InitFromConfiguration( 43 : const CPLJSONObject &configuration, 44 : const ZarrArrayMetadata &oInputArrayMetadata, 45 : ZarrArrayMetadata &oOutputArrayMetadata, bool /* bEmitWarnings */) 46 : { 47 56 : m_pCompressor = CPLGetCompressor("gzip"); 48 56 : m_pDecompressor = CPLGetDecompressor("gzip"); 49 56 : if (!m_pCompressor || !m_pDecompressor) 50 : { 51 0 : CPLError(CE_Failure, CPLE_AppDefined, "gzip compressor not available"); 52 0 : return false; 53 : } 54 : 55 56 : m_oConfiguration = configuration.Clone(); 56 56 : m_oInputArrayMetadata = oInputArrayMetadata; 57 : // byte->byte codec 58 56 : oOutputArrayMetadata = oInputArrayMetadata; 59 : 60 56 : int nLevel = 6; 61 : 62 56 : if (configuration.IsValid()) 63 : { 64 56 : if (configuration.GetType() != CPLJSONObject::Type::Object) 65 : { 66 0 : CPLError(CE_Failure, CPLE_AppDefined, 67 : "Codec gzip: configuration is not an object"); 68 0 : return false; 69 : } 70 : 71 112 : for (const auto &oChild : configuration.GetChildren()) 72 : { 73 56 : if (oChild.GetName() != "level") 74 : { 75 0 : CPLError( 76 : CE_Failure, CPLE_AppDefined, 77 : "Codec gzip: configuration contains a unhandled member: %s", 78 0 : oChild.GetName().c_str()); 79 0 : return false; 80 : } 81 : } 82 : 83 112 : const auto oLevel = configuration.GetObj("level"); 84 56 : if (oLevel.IsValid()) 85 : { 86 56 : if (oLevel.GetType() != CPLJSONObject::Type::Integer) 87 : { 88 0 : CPLError(CE_Failure, CPLE_AppDefined, 89 : "Codec gzip: level is not an integer"); 90 0 : return false; 91 : } 92 56 : nLevel = oLevel.ToInteger(); 93 56 : if (nLevel < 0 || nLevel > 9) 94 : { 95 0 : CPLError(CE_Failure, CPLE_AppDefined, 96 : "Codec gzip: invalid value for level: %d", nLevel); 97 0 : return false; 98 : } 99 : } 100 : } 101 : 102 56 : m_aosCompressorOptions.SetNameValue("LEVEL", CPLSPrintf("%d", nLevel)); 103 : 104 56 : return true; 105 : } 106 : 107 : /************************************************************************/ 108 : /* ZarrV3CodecGZip::Clone() */ 109 : /************************************************************************/ 110 : 111 8 : std::unique_ptr<ZarrV3Codec> ZarrV3CodecGZip::Clone() const 112 : { 113 16 : auto psClone = std::make_unique<ZarrV3CodecGZip>(); 114 16 : ZarrArrayMetadata oOutputArrayMetadata; 115 8 : psClone->InitFromConfiguration(m_oConfiguration, m_oInputArrayMetadata, 116 : oOutputArrayMetadata, 117 : /* bEmitWarnings = */ false); 118 16 : return psClone; 119 : }