LCOV - code coverage report
Current view: top level - frmts/zarr - zarr_v3_codec_numcodecs.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 45 75 60.0 %
Date: 2026-12-05 15:13:29 Functions: 4 6 66.7 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GDAL
       4             :  * Purpose:  Zarr driver, "numcodecs.zlib" and "numcodecs.shuffle" codecs
       5             :  *
       6             :  ******************************************************************************
       7             :  * SPDX-License-Identifier: MIT
       8             :  ****************************************************************************/
       9             : 
      10             : #include "zarr_v3_codec.h"
      11             : #include "zarr.h"
      12             : 
      13             : #include "cpl_compressor.h"
      14             : 
      15             : // Zarr v3 names of the numcodecs zlib compressor and shuffle filter, as
      16             : // written by zarr-python's numcodecs.zarr3 wrappers (and by tools that map
      17             : // TIFF DEFLATE / HDF5 shuffle+deflate chunks to Zarr v3, e.g. virtual
      18             : // references in Icechunk or kerchunk).
      19             : 
      20             : /************************************************************************/
      21             : /*                       ZarrV3CodecNumcodecsZlib                       */
      22             : /************************************************************************/
      23             : 
      24           5 : ZarrV3CodecNumcodecsZlib::ZarrV3CodecNumcodecsZlib()
      25           5 :     : ZarrV3CodecAbstractCompressor(NAME)
      26             : {
      27           5 : }
      28             : 
      29           5 : bool ZarrV3CodecNumcodecsZlib::InitFromConfiguration(
      30             :     const std::string & /* osArrayName */, const CPLJSONObject &configuration,
      31             :     const ZarrArrayMetadata &oInputArrayMetadata,
      32             :     ZarrArrayMetadata &oOutputArrayMetadata, bool /* bEmitWarnings */)
      33             : {
      34           5 :     m_pCompressor = CPLGetCompressor("zlib");
      35           5 :     m_pDecompressor = CPLGetDecompressor("zlib");
      36           5 :     if (!m_pCompressor || !m_pDecompressor)
      37             :     {
      38           0 :         CPLError(CE_Failure, CPLE_AppDefined, "zlib compressor not available");
      39           0 :         return false;
      40             :     }
      41             : 
      42           5 :     m_oConfiguration = configuration.Clone();
      43           5 :     m_oInputArrayMetadata = oInputArrayMetadata;
      44             :     // byte->byte codec
      45           5 :     oOutputArrayMetadata = oInputArrayMetadata;
      46             : 
      47           5 :     int nLevel = 1;  // numcodecs.zlib default
      48           5 :     if (configuration.IsValid())
      49             :     {
      50           5 :         if (configuration.GetType() != CPLJSONObject::Type::Object)
      51             :         {
      52           0 :             CPLError(CE_Failure, CPLE_AppDefined,
      53             :                      "Codec %s: configuration is not an object", NAME);
      54           0 :             return false;
      55             :         }
      56          10 :         for (const auto &oChild : configuration.GetChildren())
      57             :         {
      58           5 :             if (oChild.GetName() != "level")
      59             :             {
      60           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
      61             :                          "Codec %s: configuration contains a unhandled "
      62             :                          "member: %s",
      63           0 :                          NAME, oChild.GetName().c_str());
      64           0 :                 return false;
      65             :             }
      66             :         }
      67          10 :         const auto oLevel = configuration.GetObj("level");
      68           5 :         if (oLevel.IsValid())
      69             :         {
      70           5 :             if (oLevel.GetType() != CPLJSONObject::Type::Integer)
      71             :             {
      72           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
      73             :                          "Codec %s: level is not an integer", NAME);
      74           0 :                 return false;
      75             :             }
      76           5 :             nLevel = oLevel.ToInteger();
      77           5 :             if (nLevel < 0 || nLevel > 9)
      78             :             {
      79           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
      80             :                          "Codec %s: invalid value for level: %d", NAME, nLevel);
      81           0 :                 return false;
      82             :             }
      83             :         }
      84             :     }
      85             : 
      86           5 :     m_aosCompressorOptions.SetNameValue("LEVEL", CPLSPrintf("%d", nLevel));
      87           5 :     return true;
      88             : }
      89             : 
      90           0 : std::unique_ptr<ZarrV3Codec> ZarrV3CodecNumcodecsZlib::Clone() const
      91             : {
      92           0 :     auto psClone = std::make_unique<ZarrV3CodecNumcodecsZlib>();
      93           0 :     ZarrArrayMetadata oOutputArrayMetadata;
      94           0 :     psClone->InitFromConfiguration(std::string(), m_oConfiguration,
      95           0 :                                    m_oInputArrayMetadata, oOutputArrayMetadata,
      96             :                                    /* bEmitWarnings = */ false);
      97           0 :     return psClone;
      98             : }
      99             : 
     100             : /************************************************************************/
     101             : /*                     ZarrV3CodecNumcodecsShuffle                      */
     102             : /************************************************************************/
     103             : 
     104           5 : ZarrV3CodecNumcodecsShuffle::ZarrV3CodecNumcodecsShuffle()
     105           5 :     : ZarrV3CodecAbstractCompressor(NAME)
     106             : {
     107           5 : }
     108             : 
     109           5 : bool ZarrV3CodecNumcodecsShuffle::InitFromConfiguration(
     110             :     const std::string & /* osArrayName */, const CPLJSONObject &configuration,
     111             :     const ZarrArrayMetadata &oInputArrayMetadata,
     112             :     ZarrArrayMetadata &oOutputArrayMetadata, bool /* bEmitWarnings */)
     113             : {
     114           5 :     m_pCompressor = ZarrGetShuffleCompressor();
     115           5 :     m_pDecompressor = ZarrGetShuffleDecompressor();
     116             : 
     117           5 :     m_oConfiguration = configuration.Clone();
     118           5 :     m_oInputArrayMetadata = oInputArrayMetadata;
     119             :     // byte->byte codec
     120           5 :     oOutputArrayMetadata = oInputArrayMetadata;
     121             : 
     122           5 :     int nElementSize = 4;  // numcodecs.shuffle default
     123           5 :     if (configuration.IsValid())
     124             :     {
     125           5 :         if (configuration.GetType() != CPLJSONObject::Type::Object)
     126             :         {
     127           0 :             CPLError(CE_Failure, CPLE_AppDefined,
     128             :                      "Codec %s: configuration is not an object", NAME);
     129           0 :             return false;
     130             :         }
     131          10 :         for (const auto &oChild : configuration.GetChildren())
     132             :         {
     133           5 :             if (oChild.GetName() != "elementsize")
     134             :             {
     135           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
     136             :                          "Codec %s: configuration contains a unhandled "
     137             :                          "member: %s",
     138           0 :                          NAME, oChild.GetName().c_str());
     139           0 :                 return false;
     140             :             }
     141             :         }
     142          10 :         const auto oSize = configuration.GetObj("elementsize");
     143           5 :         if (oSize.IsValid())
     144             :         {
     145           5 :             if (oSize.GetType() != CPLJSONObject::Type::Integer)
     146             :             {
     147           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
     148             :                          "Codec %s: elementsize is not an integer", NAME);
     149           0 :                 return false;
     150             :             }
     151           5 :             nElementSize = oSize.ToInteger();
     152             :         }
     153             :     }
     154           5 :     if (nElementSize != 1 && nElementSize != 2 && nElementSize != 4 &&
     155             :         nElementSize != 8)
     156             :     {
     157           1 :         CPLError(CE_Failure, CPLE_NotSupported,
     158             :                  "Codec %s: only elementsize = 1, 2, 4 or 8 is supported "
     159             :                  "(got %d)",
     160             :                  NAME, nElementSize);
     161           1 :         return false;
     162             :     }
     163             : 
     164             :     m_aosCompressorOptions.SetNameValue("ELEMENTSIZE",
     165           4 :                                         CPLSPrintf("%d", nElementSize));
     166           4 :     return true;
     167             : }
     168             : 
     169           0 : std::unique_ptr<ZarrV3Codec> ZarrV3CodecNumcodecsShuffle::Clone() const
     170             : {
     171           0 :     auto psClone = std::make_unique<ZarrV3CodecNumcodecsShuffle>();
     172           0 :     ZarrArrayMetadata oOutputArrayMetadata;
     173           0 :     psClone->InitFromConfiguration(std::string(), m_oConfiguration,
     174           0 :                                    m_oInputArrayMetadata, oOutputArrayMetadata,
     175             :                                    /* bEmitWarnings = */ false);
     176           0 :     return psClone;
     177             : }

Generated by: LCOV version 1.14