LCOV - code coverage report
Current view: top level - third_party/LercLib - Lerc_c_api_impl.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 53 114 46.5 %
Date: 2024-11-21 22:18:42 Functions: 3 7 42.9 %

          Line data    Source code
       1             : /*
       2             : Copyright 2016 Esri
       3             : 
       4             : Licensed under the Apache License, Version 2.0 (the "License");
       5             : you may not use this file except in compliance with the License.
       6             : You may obtain a copy of the License at
       7             : 
       8             : http://www.apache.org/licenses/LICENSE-2.0
       9             : 
      10             : Unless required by applicable law or agreed to in writing, software
      11             : distributed under the License is distributed on an "AS IS" BASIS,
      12             : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13             : See the License for the specific language governing permissions and
      14             : limitations under the License.
      15             : 
      16             : A local copy of the license and additional notices are located with the
      17             : source distribution at:
      18             : 
      19             : http://github.com/Esri/lerc/
      20             : 
      21             : Contributors:  Thomas Maurer
      22             : */
      23             : 
      24             : #include "Defines.h"
      25             : #include "Lerc_c_api.h"
      26             : #include "Lerc_types.h"
      27             : #include "Lerc.h"
      28             : 
      29             : USING_NAMESPACE_LERC
      30             : 
      31             : // -------------------------------------------------------------------------- ;
      32             : 
      33           0 : lerc_status lerc_computeCompressedSize(const void* pData, unsigned int dataType, int nDim, int nCols, int nRows, int nBands, 
      34             :   const unsigned char* pValidBytes, double maxZErr, unsigned int* numBytes)
      35             : {
      36           0 :   return lerc_computeCompressedSizeForVersion(pData, -1, dataType, nDim, nCols, nRows, nBands, pValidBytes, maxZErr, numBytes);
      37             : }
      38             : 
      39             : // -------------------------------------------------------------------------- ;
      40             : 
      41           0 : lerc_status lerc_computeCompressedSizeForVersion(const void* pData, int version, unsigned int dataType, int nDim, int nCols, int nRows, int nBands,
      42             :   const unsigned char* pValidBytes, double maxZErr, unsigned int* numBytes)
      43             : {
      44           0 :   if (!pData || dataType >= Lerc::DT_Undefined || nDim <= 0 || nCols <= 0 || nRows <= 0 || nBands <= 0 || maxZErr < 0 || !numBytes)
      45           0 :     return (lerc_status)ErrCode::WrongParam;
      46             : 
      47           0 :   BitMask bitMask;
      48           0 :   if (pValidBytes)
      49             :   {
      50           0 :     bitMask.SetSize(nCols, nRows);
      51           0 :     bitMask.SetAllValid();
      52             : 
      53           0 :     for (int k = 0, i = 0; i < nRows; i++)
      54           0 :       for (int j = 0; j < nCols; j++, k++)
      55           0 :         if (!pValidBytes[k])
      56           0 :           bitMask.SetInvalid(k);
      57             :   }
      58           0 :   const BitMask* pBitMask = pValidBytes ? &bitMask : nullptr;
      59             : 
      60           0 :   Lerc::DataType dt = (Lerc::DataType)dataType;
      61           0 :   return (lerc_status)Lerc::ComputeCompressedSize(pData, version, dt, nDim, nCols, nRows, nBands, pBitMask, maxZErr, *numBytes);
      62             : }
      63             : 
      64             : // -------------------------------------------------------------------------- ;
      65             : 
      66           0 : lerc_status lerc_encode(const void* pData, unsigned int dataType, int nDim, int nCols, int nRows, int nBands,
      67             :   const unsigned char* pValidBytes, double maxZErr, unsigned char* pOutBuffer, unsigned int outBufferSize,
      68             :   unsigned int* nBytesWritten)
      69             : {
      70           0 :   return lerc_encodeForVersion(pData, -1, dataType, nDim, nCols, nRows, nBands, pValidBytes, maxZErr, pOutBuffer, outBufferSize, nBytesWritten);
      71             : }
      72             : 
      73             : // -------------------------------------------------------------------------- ;
      74             : 
      75        5181 : lerc_status lerc_encodeForVersion(const void* pData, int version, unsigned int dataType, int nDim, int nCols, int nRows, int nBands, 
      76             :   const unsigned char* pValidBytes, double maxZErr, unsigned char* pOutBuffer, unsigned int outBufferSize,
      77             :   unsigned int* nBytesWritten)
      78             : {
      79        5181 :   if (!pData || dataType >= Lerc::DT_Undefined || nDim <= 0 || nCols <= 0 || nRows <= 0 || nBands <= 0 || maxZErr < 0 || !pOutBuffer || !outBufferSize || !nBytesWritten)
      80           0 :     return (lerc_status)ErrCode::WrongParam;
      81             : 
      82           0 :   BitMask bitMask;
      83        5181 :   if (pValidBytes)
      84             :   {
      85          18 :     bitMask.SetSize(nCols, nRows);
      86          18 :     bitMask.SetAllValid();
      87             : 
      88          42 :     for (int k = 0, i = 0; i < nRows; i++)
      89        2212 :       for (int j = 0; j < nCols; j++, k++)
      90        2188 :         if (!pValidBytes[k])
      91        1781 :           bitMask.SetInvalid(k);
      92             :   }
      93        5181 :   const BitMask* pBitMask = pValidBytes ? &bitMask : nullptr;
      94             : 
      95        5181 :   Lerc::DataType dt = (Lerc::DataType)dataType;
      96        5181 :   return (lerc_status)Lerc::Encode(pData, version, dt, nDim, nCols, nRows, nBands, pBitMask, maxZErr, pOutBuffer, outBufferSize, *nBytesWritten);
      97             : }
      98             : 
      99             : // -------------------------------------------------------------------------- ;
     100             : 
     101         531 : lerc_status lerc_getBlobInfo(const unsigned char* pLercBlob, unsigned int blobSize, 
     102             :   unsigned int* infoArray, double* dataRangeArray, int infoArraySize, int dataRangeArraySize)
     103             : {
     104         531 :   if (!pLercBlob || !blobSize || (!infoArray && !dataRangeArray) || ((infoArraySize <= 0) && (dataRangeArraySize <= 0)))
     105           0 :     return (lerc_status)ErrCode::WrongParam;
     106             : 
     107             :   Lerc::LercInfo lercInfo;
     108         531 :   ErrCode errCode = Lerc::GetLercInfo(pLercBlob, blobSize, lercInfo);
     109         531 :   if (errCode != ErrCode::Ok)
     110           0 :     return (lerc_status)errCode;
     111             : 
     112         531 :   if (infoArray)
     113             :   {
     114         531 :     int i = 0, ias = infoArraySize;
     115             : 
     116         531 :     if (ias > 0)
     117         531 :       memset(infoArray, 0, ias * sizeof(infoArray[0]));
     118             : 
     119         531 :     if (i < ias)
     120         531 :       infoArray[i++] = (unsigned int)lercInfo.version;
     121         531 :     if (i < ias)
     122         531 :       infoArray[i++] = (unsigned int)lercInfo.dt;
     123         531 :     if (i < ias)
     124         531 :       infoArray[i++] = (unsigned int)lercInfo.nDim;
     125         531 :     if (i < ias)
     126         531 :       infoArray[i++] = (unsigned int)lercInfo.nCols;
     127         531 :     if (i < ias)
     128         531 :       infoArray[i++] = (unsigned int)lercInfo.nRows;
     129         531 :     if (i < ias)
     130         531 :       infoArray[i++] = (unsigned int)lercInfo.nBands;
     131         531 :     if (i < ias)
     132         531 :       infoArray[i++] = (unsigned int)lercInfo.numValidPixel;
     133         531 :     if (i < ias)
     134         530 :       infoArray[i++] = (unsigned int)lercInfo.blobSize;
     135             :   }
     136             : 
     137         531 :   if (dataRangeArray)
     138             :   {
     139           0 :     int i = 0, dras = dataRangeArraySize;
     140             : 
     141           0 :     if (dras > 0)
     142           0 :       memset(dataRangeArray, 0, dras * sizeof(dataRangeArray[0]));
     143             : 
     144           0 :     if (i < dras)
     145           0 :       dataRangeArray[i++] = lercInfo.zMin;
     146           0 :     if (i < dras)
     147           0 :       dataRangeArray[i++] = lercInfo.zMax;
     148           0 :     if (i < dras)
     149           0 :       dataRangeArray[i++] = lercInfo.maxZError;
     150             :   }
     151             : 
     152         531 :   return (lerc_status)ErrCode::Ok;
     153             : }
     154             : 
     155             : // -------------------------------------------------------------------------- ;
     156             : 
     157        2142 : lerc_status lerc_decode(const unsigned char* pLercBlob, unsigned int blobSize,
     158             :   unsigned char* pValidBytes, int nDim, int nCols, int nRows, int nBands, unsigned int dataType, void* pData)
     159             : {
     160        2142 :   if (!pLercBlob || !blobSize || !pData || dataType >= Lerc::DT_Undefined || nDim <= 0 || nCols <= 0 || nRows <= 0 || nBands <= 0)
     161           0 :     return (lerc_status)ErrCode::WrongParam;
     162             : 
     163        2142 :   BitMask bitMask;
     164        2142 :   if (pValidBytes)
     165             :   {
     166          51 :     bitMask.SetSize(nCols, nRows);
     167          51 :     bitMask.SetAllInvalid();
     168             :   }
     169        2142 :   BitMask* pBitMask = pValidBytes ? &bitMask : nullptr;
     170             : 
     171        2142 :   Lerc::DataType dt = (Lerc::DataType)dataType;
     172             : 
     173        2142 :   ErrCode errCode = Lerc::Decode(pLercBlob, blobSize, pBitMask, nDim, nCols, nRows, nBands, dt, pData);
     174        2142 :   if (errCode != ErrCode::Ok)
     175           0 :     return (lerc_status)errCode;
     176             : 
     177        2142 :   if (pValidBytes)
     178             :   {
     179         657 :     for (int k = 0, i = 0; i < nRows; i++)
     180      268772 :       for (int j = 0; j < nCols; j++, k++)
     181      268166 :         pValidBytes[k] = bitMask.IsValid(k);
     182             :   }
     183             : 
     184        2142 :   return (lerc_status)ErrCode::Ok;
     185             : }
     186             : 
     187             : // -------------------------------------------------------------------------- ;
     188             : 
     189           0 : lerc_status lerc_decodeToDouble(const unsigned char* pLercBlob, unsigned int blobSize,
     190             :   unsigned char* pValidBytes, int nDim, int nCols, int nRows, int nBands, double* pData)
     191             : {
     192           0 :   if (!pLercBlob || !blobSize || !pData || nDim <= 0 || nCols <= 0 || nRows <= 0 || nBands <= 0)
     193           0 :     return (lerc_status)ErrCode::WrongParam;
     194             : 
     195             :   Lerc::LercInfo lercInfo;
     196             :   ErrCode errCode;
     197           0 :   if ((errCode = Lerc::GetLercInfo(pLercBlob, blobSize, lercInfo)) != ErrCode::Ok)
     198           0 :     return (lerc_status)errCode;
     199             : 
     200           0 :   Lerc::DataType dt = lercInfo.dt;
     201           0 :   if (dt > Lerc::DT_Double)
     202           0 :     return (lerc_status)ErrCode::Failed;
     203             : 
     204           0 :   BitMask bitMask;
     205           0 :   if (pValidBytes)
     206             :   {
     207           0 :     bitMask.SetSize(nCols, nRows);
     208           0 :     bitMask.SetAllInvalid();
     209             :   }
     210           0 :   BitMask* pBitMask = pValidBytes ? &bitMask : nullptr;
     211             : 
     212           0 :   if (dt == Lerc::DT_Double)
     213             :   {
     214           0 :     if ((errCode = Lerc::Decode(pLercBlob, blobSize, pBitMask, nDim, nCols, nRows, nBands, dt, pData)) != ErrCode::Ok)
     215           0 :       return (lerc_status)errCode;
     216             :   }
     217             :   else
     218             :   {
     219             :     // use the buffer passed for in place decode and convert
     220           0 :     int sizeofDt[] = { 1, 1, 2, 2, 4, 4, 4, 8 };
     221           0 :     size_t nDataValues = nDim * nCols * nRows * nBands;
     222           0 :     void* ptrDec = (Byte*)pData + nDataValues * (sizeof(double) - sizeofDt[dt]);
     223             : 
     224           0 :     if ((errCode = Lerc::Decode(pLercBlob, blobSize, pBitMask, nDim, nCols, nRows, nBands, dt, ptrDec)) != ErrCode::Ok)
     225           0 :       return (lerc_status)errCode;
     226             : 
     227           0 :     if ((errCode = Lerc::ConvertToDouble(ptrDec, dt, nDataValues, pData)) != ErrCode::Ok)
     228           0 :       return (lerc_status)errCode;
     229             :   }
     230             : 
     231           0 :   if (pValidBytes)
     232             :   {
     233           0 :     for (int k = 0, i = 0; i < nRows; i++)
     234           0 :       for (int j = 0; j < nCols; j++, k++)
     235           0 :         pValidBytes[k] = bitMask.IsValid(k);
     236             :   }
     237             : 
     238           0 :   return (lerc_status)ErrCode::Ok;
     239             : }
     240             : 
     241             : // -------------------------------------------------------------------------- ;
     242             : 

Generated by: LCOV version 1.14