LCOV - code coverage report
Current view: top level - frmts/webp - webpdataset.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 340 469 72.5 %
Date: 2025-05-31 00:00:17 Functions: 17 18 94.4 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GDAL WEBP Driver
       4             :  * Purpose:  Implement GDAL WEBP Support based on libwebp
       5             :  * Author:   Even Rouault, <even dot rouault at spatialys.com>
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2011-2013, Even Rouault <even dot rouault at spatialys.com>
       9             :  *
      10             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #include "cpl_string.h"
      14             : #include "gdal_frmts.h"
      15             : #include "gdal_pam.h"
      16             : 
      17             : #include "webp_headers.h"
      18             : #include "webpdrivercore.h"
      19             : 
      20             : #include <limits>
      21             : 
      22             : /************************************************************************/
      23             : /* ==================================================================== */
      24             : /*                               WEBPDataset                            */
      25             : /* ==================================================================== */
      26             : /************************************************************************/
      27             : 
      28             : class WEBPRasterBand;
      29             : 
      30             : class WEBPDataset final : public GDALPamDataset
      31             : {
      32             :     friend class WEBPRasterBand;
      33             : 
      34             :     VSILFILE *fpImage;
      35             :     GByte *pabyUncompressed;
      36             :     int bHasBeenUncompressed;
      37             :     CPLErr eUncompressErrRet;
      38             :     CPLErr Uncompress();
      39             : 
      40             :     int bHasReadXMPMetadata;
      41             : 
      42             :   public:
      43             :     WEBPDataset();
      44             :     virtual ~WEBPDataset();
      45             : 
      46             :     virtual CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
      47             :                              GDALDataType, int, BANDMAP_TYPE,
      48             :                              GSpacing nPixelSpace, GSpacing nLineSpace,
      49             :                              GSpacing nBandSpace,
      50             :                              GDALRasterIOExtraArg *psExtraArg) override;
      51             : 
      52             :     virtual char **GetMetadataDomainList() override;
      53             :     virtual char **GetMetadata(const char *pszDomain = "") override;
      54             : 
      55             :     CPLStringList GetCompressionFormats(int nXOff, int nYOff, int nXSize,
      56             :                                         int nYSize, int nBandCount,
      57             :                                         const int *panBandList) override;
      58             :     CPLErr ReadCompressedData(const char *pszFormat, int nXOff, int nYOff,
      59             :                               int nXSize, int nYSize, int nBandCount,
      60             :                               const int *panBandList, void **ppBuffer,
      61             :                               size_t *pnBufferSize,
      62             :                               char **ppszDetailedFormat) override;
      63             : 
      64             :     static GDALPamDataset *OpenPAM(GDALOpenInfo *poOpenInfo);
      65             :     static GDALDataset *Open(GDALOpenInfo *);
      66             :     static GDALDataset *CreateCopy(const char *pszFilename,
      67             :                                    GDALDataset *poSrcDS, int bStrict,
      68             :                                    char **papszOptions,
      69             :                                    GDALProgressFunc pfnProgress,
      70             :                                    void *pProgressData);
      71             : };
      72             : 
      73             : /************************************************************************/
      74             : /* ==================================================================== */
      75             : /*                            WEBPRasterBand                            */
      76             : /* ==================================================================== */
      77             : /************************************************************************/
      78             : 
      79             : class WEBPRasterBand final : public GDALPamRasterBand
      80             : {
      81             :     friend class WEBPDataset;
      82             : 
      83             :   public:
      84             :     WEBPRasterBand(WEBPDataset *, int);
      85             : 
      86             :     virtual CPLErr IReadBlock(int, int, void *) override;
      87             :     virtual GDALColorInterp GetColorInterpretation() override;
      88             : };
      89             : 
      90             : /************************************************************************/
      91             : /*                          WEBPRasterBand()                            */
      92             : /************************************************************************/
      93             : 
      94        1691 : WEBPRasterBand::WEBPRasterBand(WEBPDataset *poDSIn, int)
      95             : {
      96        1691 :     poDS = poDSIn;
      97             : 
      98        1691 :     eDataType = GDT_Byte;
      99             : 
     100        1691 :     nBlockXSize = poDSIn->nRasterXSize;
     101        1691 :     nBlockYSize = 1;
     102        1691 : }
     103             : 
     104             : /************************************************************************/
     105             : /*                             IReadBlock()                             */
     106             : /************************************************************************/
     107             : 
     108       11626 : CPLErr WEBPRasterBand::IReadBlock(CPL_UNUSED int nBlockXOff, int nBlockYOff,
     109             :                                   void *pImage)
     110             : {
     111       11626 :     WEBPDataset *poGDS = reinterpret_cast<WEBPDataset *>(poDS);
     112             : 
     113       11626 :     if (poGDS->Uncompress() != CE_None)
     114           0 :         return CE_Failure;
     115             : 
     116       11626 :     GByte *pabyUncompressed =
     117       11626 :         &poGDS->pabyUncompressed[nBlockYOff * nRasterXSize * poGDS->nBands +
     118       11626 :                                  nBand - 1];
     119     2882680 :     for (int i = 0; i < nRasterXSize; i++)
     120     2871060 :         reinterpret_cast<GByte *>(pImage)[i] =
     121     2871060 :             pabyUncompressed[poGDS->nBands * i];
     122             : 
     123       11626 :     return CE_None;
     124             : }
     125             : 
     126             : /************************************************************************/
     127             : /*                       GetColorInterpretation()                       */
     128             : /************************************************************************/
     129             : 
     130          78 : GDALColorInterp WEBPRasterBand::GetColorInterpretation()
     131             : 
     132             : {
     133          78 :     if (nBand == 1)
     134          25 :         return GCI_RedBand;
     135             : 
     136          53 :     else if (nBand == 2)
     137          25 :         return GCI_GreenBand;
     138             : 
     139          28 :     else if (nBand == 3)
     140          25 :         return GCI_BlueBand;
     141             : 
     142           3 :     return GCI_AlphaBand;
     143             : }
     144             : 
     145             : /************************************************************************/
     146             : /* ==================================================================== */
     147             : /*                             WEBPDataset                               */
     148             : /* ==================================================================== */
     149             : /************************************************************************/
     150             : 
     151             : /************************************************************************/
     152             : /*                            WEBPDataset()                              */
     153             : /************************************************************************/
     154             : 
     155         465 : WEBPDataset::WEBPDataset()
     156             :     : fpImage(nullptr), pabyUncompressed(nullptr), bHasBeenUncompressed(FALSE),
     157         465 :       eUncompressErrRet(CE_None), bHasReadXMPMetadata(FALSE)
     158             : {
     159         465 : }
     160             : 
     161             : /************************************************************************/
     162             : /*                           ~WEBPDataset()                             */
     163             : /************************************************************************/
     164             : 
     165         930 : WEBPDataset::~WEBPDataset()
     166             : 
     167             : {
     168         465 :     FlushCache(true);
     169         465 :     if (fpImage)
     170         465 :         VSIFCloseL(fpImage);
     171         465 :     VSIFree(pabyUncompressed);
     172         930 : }
     173             : 
     174             : /************************************************************************/
     175             : /*                      GetMetadataDomainList()                         */
     176             : /************************************************************************/
     177             : 
     178           1 : char **WEBPDataset::GetMetadataDomainList()
     179             : {
     180           1 :     return BuildMetadataDomainList(GDALPamDataset::GetMetadataDomainList(),
     181           1 :                                    TRUE, "xml:XMP", nullptr);
     182             : }
     183             : 
     184             : /************************************************************************/
     185             : /*                           GetMetadata()                              */
     186             : /************************************************************************/
     187             : 
     188          52 : char **WEBPDataset::GetMetadata(const char *pszDomain)
     189             : {
     190          52 :     if ((pszDomain != nullptr && EQUAL(pszDomain, "xml:XMP")) &&
     191           4 :         !bHasReadXMPMetadata)
     192             :     {
     193           4 :         bHasReadXMPMetadata = TRUE;
     194             : 
     195           4 :         VSIFSeekL(fpImage, 12, SEEK_SET);
     196             : 
     197           4 :         bool bFirst = true;
     198             :         while (true)
     199             :         {
     200             :             char szHeader[5];
     201             :             GUInt32 nChunkSize;
     202             : 
     203          16 :             if (VSIFReadL(szHeader, 1, 4, fpImage) != 4 ||
     204           8 :                 VSIFReadL(&nChunkSize, 1, 4, fpImage) != 4)
     205           4 :                 break;
     206             : 
     207           8 :             szHeader[4] = '\0';
     208           8 :             CPL_LSBPTR32(&nChunkSize);
     209             : 
     210           8 :             if (bFirst)
     211             :             {
     212           4 :                 if (strcmp(szHeader, "VP8X") != 0 || nChunkSize < 10)
     213             :                     break;
     214             : 
     215             :                 int l_nFlags;
     216           2 :                 if (VSIFReadL(&l_nFlags, 1, 4, fpImage) != 4)
     217           0 :                     break;
     218           2 :                 CPL_LSBPTR32(&l_nFlags);
     219           2 :                 if ((l_nFlags & 8) == 0)
     220           0 :                     break;
     221             : 
     222           2 :                 VSIFSeekL(fpImage, nChunkSize - 4, SEEK_CUR);
     223             : 
     224           2 :                 bFirst = false;
     225             :             }
     226           4 :             else if (strcmp(szHeader, "META") == 0)
     227             :             {
     228           2 :                 if (nChunkSize > 1024 * 1024)
     229           0 :                     break;
     230             : 
     231             :                 char *pszXMP =
     232           2 :                     reinterpret_cast<char *>(VSIMalloc(nChunkSize + 1));
     233           2 :                 if (pszXMP == nullptr)
     234           0 :                     break;
     235             : 
     236           2 :                 if (static_cast<GUInt32>(VSIFReadL(pszXMP, 1, nChunkSize,
     237           2 :                                                    fpImage)) != nChunkSize)
     238             :                 {
     239           0 :                     VSIFree(pszXMP);
     240           0 :                     break;
     241             :                 }
     242           2 :                 pszXMP[nChunkSize] = '\0';
     243             : 
     244             :                 /* Avoid setting the PAM dirty bit just for that */
     245           2 :                 const int nOldPamFlags = nPamFlags;
     246             : 
     247           2 :                 char *apszMDList[2] = {pszXMP, nullptr};
     248           2 :                 SetMetadata(apszMDList, "xml:XMP");
     249             : 
     250             :                 // cppcheck-suppress redundantAssignment
     251           2 :                 nPamFlags = nOldPamFlags;
     252             : 
     253           2 :                 VSIFree(pszXMP);
     254           2 :                 break;
     255             :             }
     256             :             else
     257           2 :                 VSIFSeekL(fpImage, nChunkSize, SEEK_CUR);
     258           4 :         }
     259             :     }
     260             : 
     261          52 :     return GDALPamDataset::GetMetadata(pszDomain);
     262             : }
     263             : 
     264             : /************************************************************************/
     265             : /*                            Uncompress()                              */
     266             : /************************************************************************/
     267             : 
     268       11941 : CPLErr WEBPDataset::Uncompress()
     269             : {
     270       11941 :     if (bHasBeenUncompressed)
     271       11605 :         return eUncompressErrRet;
     272             : 
     273         336 :     bHasBeenUncompressed = TRUE;
     274         336 :     eUncompressErrRet = CE_Failure;
     275             : 
     276             :     // To avoid excessive memory allocation attempts
     277             :     // Normally WebP images are no larger than 16383x16383*4 ~= 1 GB
     278         336 :     if (nRasterXSize > INT_MAX / (nRasterYSize * nBands))
     279             :     {
     280           0 :         CPLError(CE_Failure, CPLE_NotSupported, "Too large image");
     281           0 :         return CE_Failure;
     282             :     }
     283             : 
     284         336 :     pabyUncompressed = reinterpret_cast<GByte *>(
     285         336 :         VSIMalloc3(nRasterXSize, nRasterYSize, nBands));
     286         336 :     if (pabyUncompressed == nullptr)
     287           0 :         return CE_Failure;
     288             : 
     289         336 :     VSIFSeekL(fpImage, 0, SEEK_END);
     290         336 :     vsi_l_offset nSizeLarge = VSIFTellL(fpImage);
     291         336 :     if (nSizeLarge !=
     292         336 :         static_cast<vsi_l_offset>(static_cast<uint32_t>(nSizeLarge)))
     293           0 :         return CE_Failure;
     294         336 :     VSIFSeekL(fpImage, 0, SEEK_SET);
     295         336 :     uint32_t nSize = static_cast<uint32_t>(nSizeLarge);
     296         336 :     uint8_t *pabyCompressed = reinterpret_cast<uint8_t *>(VSIMalloc(nSize));
     297         336 :     if (pabyCompressed == nullptr)
     298           0 :         return CE_Failure;
     299         336 :     VSIFReadL(pabyCompressed, 1, nSize, fpImage);
     300             :     uint8_t *pRet;
     301             : 
     302         336 :     if (nBands == 4)
     303         216 :         pRet = WebPDecodeRGBAInto(pabyCompressed, static_cast<uint32_t>(nSize),
     304         216 :                                   static_cast<uint8_t *>(pabyUncompressed),
     305         216 :                                   static_cast<size_t>(nRasterXSize) *
     306         216 :                                       nRasterYSize * nBands,
     307         216 :                                   nRasterXSize * nBands);
     308             :     else
     309         120 :         pRet = WebPDecodeRGBInto(pabyCompressed, static_cast<uint32_t>(nSize),
     310         120 :                                  static_cast<uint8_t *>(pabyUncompressed),
     311         120 :                                  static_cast<size_t>(nRasterXSize) *
     312         120 :                                      nRasterYSize * nBands,
     313         120 :                                  nRasterXSize * nBands);
     314             : 
     315         336 :     VSIFree(pabyCompressed);
     316         336 :     if (pRet == nullptr)
     317             :     {
     318           0 :         CPLError(CE_Failure, CPLE_AppDefined, "WebPDecodeRGBInto() failed");
     319           0 :         return CE_Failure;
     320             :     }
     321         336 :     eUncompressErrRet = CE_None;
     322             : 
     323         336 :     return CE_None;
     324             : }
     325             : 
     326             : /************************************************************************/
     327             : /*                             IRasterIO()                              */
     328             : /************************************************************************/
     329             : 
     330         319 : CPLErr WEBPDataset::IRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,
     331             :                               int nXSize, int nYSize, void *pData,
     332             :                               int nBufXSize, int nBufYSize,
     333             :                               GDALDataType eBufType, int nBandCount,
     334             :                               BANDMAP_TYPE panBandMap, GSpacing nPixelSpace,
     335             :                               GSpacing nLineSpace, GSpacing nBandSpace,
     336             :                               GDALRasterIOExtraArg *psExtraArg)
     337             : 
     338             : {
     339         319 :     if ((eRWFlag == GF_Read) && (nBandCount == nBands) && (nXOff == 0) &&
     340         319 :         (nYOff == 0) && (nXSize == nBufXSize) && (nXSize == nRasterXSize) &&
     341         315 :         (nYSize == nBufYSize) && (nYSize == nRasterYSize) &&
     342         953 :         (eBufType == GDT_Byte) && (pData != nullptr) &&
     343         315 :         IsAllBands(nBandCount, panBandMap))
     344             :     {
     345         315 :         if (Uncompress() != CE_None)
     346           0 :             return CE_Failure;
     347         315 :         if (nPixelSpace == nBands && nLineSpace == (nPixelSpace * nXSize) &&
     348             :             nBandSpace == 1)
     349             :         {
     350          75 :             memcpy(pData, pabyUncompressed,
     351          75 :                    static_cast<size_t>(nBands) * nXSize * nYSize);
     352             :         }
     353             :         else
     354             :         {
     355        8024 :             for (int y = 0; y < nYSize; ++y)
     356             :             {
     357        7784 :                 GByte *pabyScanline = pabyUncompressed + y * nBands * nXSize;
     358     1101290 :                 for (int x = 0; x < nXSize; ++x)
     359             :                 {
     360     5276670 :                     for (int iBand = 0; iBand < nBands; iBand++)
     361             :                         reinterpret_cast<GByte *>(
     362     4183170 :                             pData)[(y * nLineSpace) + (x * nPixelSpace) +
     363     4183170 :                                    iBand * nBandSpace] =
     364     4183170 :                             pabyScanline[x * nBands + iBand];
     365             :                 }
     366             :             }
     367             :         }
     368             : 
     369         315 :         return CE_None;
     370             :     }
     371             : 
     372           4 :     return GDALPamDataset::IRasterIO(eRWFlag, nXOff, nYOff, nXSize, nYSize,
     373             :                                      pData, nBufXSize, nBufYSize, eBufType,
     374             :                                      nBandCount, panBandMap, nPixelSpace,
     375           4 :                                      nLineSpace, nBandSpace, psExtraArg);
     376             : }
     377             : 
     378             : /************************************************************************/
     379             : /*                       GetCompressionFormats()                        */
     380             : /************************************************************************/
     381             : 
     382           0 : CPLStringList WEBPDataset::GetCompressionFormats(int nXOff, int nYOff,
     383             :                                                  int nXSize, int nYSize,
     384             :                                                  int nBandCount,
     385             :                                                  const int *panBandList)
     386             : {
     387           0 :     CPLStringList aosRet;
     388           0 :     if (nXOff == 0 && nYOff == 0 && nXSize == nRasterXSize &&
     389           0 :         nYSize == nRasterYSize && IsAllBands(nBandCount, panBandList))
     390             :     {
     391           0 :         aosRet.AddString("WEBP");
     392             :     }
     393           0 :     return aosRet;
     394             : }
     395             : 
     396             : /************************************************************************/
     397             : /*                       ReadCompressedData()                           */
     398             : /************************************************************************/
     399             : 
     400           2 : CPLErr WEBPDataset::ReadCompressedData(const char *pszFormat, int nXOff,
     401             :                                        int nYOff, int nXSize, int nYSize,
     402             :                                        int nBandCount, const int *panBandList,
     403             :                                        void **ppBuffer, size_t *pnBufferSize,
     404             :                                        char **ppszDetailedFormat)
     405             : {
     406           2 :     if (nXOff == 0 && nYOff == 0 && nXSize == nRasterXSize &&
     407           4 :         nYSize == nRasterYSize && IsAllBands(nBandCount, panBandList))
     408             :     {
     409           2 :         const CPLStringList aosTokens(CSLTokenizeString2(pszFormat, ";", 0));
     410           2 :         if (aosTokens.size() != 1)
     411           0 :             return CE_Failure;
     412             : 
     413           2 :         if (EQUAL(aosTokens[0], "WEBP"))
     414             :         {
     415           2 :             if (ppszDetailedFormat)
     416           0 :                 *ppszDetailedFormat = VSIStrdup("WEBP");
     417           2 :             VSIFSeekL(fpImage, 0, SEEK_END);
     418           2 :             const auto nFileSize = VSIFTellL(fpImage);
     419           2 :             if (nFileSize > std::numeric_limits<uint32_t>::max())
     420           0 :                 return CE_Failure;
     421           2 :             auto nSize = static_cast<uint32_t>(nFileSize);
     422           2 :             if (ppBuffer)
     423             :             {
     424           2 :                 if (!pnBufferSize)
     425           0 :                     return CE_Failure;
     426           2 :                 bool bFreeOnError = false;
     427           2 :                 if (*ppBuffer)
     428             :                 {
     429           0 :                     if (*pnBufferSize < nSize)
     430           0 :                         return CE_Failure;
     431             :                 }
     432             :                 else
     433             :                 {
     434           2 :                     *ppBuffer = VSI_MALLOC_VERBOSE(nSize);
     435           2 :                     if (*ppBuffer == nullptr)
     436           0 :                         return CE_Failure;
     437           2 :                     bFreeOnError = true;
     438             :                 }
     439           2 :                 VSIFSeekL(fpImage, 0, SEEK_SET);
     440           2 :                 if (VSIFReadL(*ppBuffer, nSize, 1, fpImage) != 1)
     441             :                 {
     442           0 :                     if (bFreeOnError)
     443             :                     {
     444           0 :                         VSIFree(*ppBuffer);
     445           0 :                         *ppBuffer = nullptr;
     446             :                     }
     447           0 :                     return CE_Failure;
     448             :                 }
     449             : 
     450             :                 // Remove META box
     451           2 :                 if (nSize > 12 && memcmp(*ppBuffer, "RIFF", 4) == 0)
     452             :                 {
     453           2 :                     size_t nPos = 12;
     454           2 :                     GByte *pabyData = static_cast<GByte *>(*ppBuffer);
     455           6 :                     while (nPos <= nSize - 8)
     456             :                     {
     457           4 :                         char szBoxName[5] = {0, 0, 0, 0, 0};
     458           4 :                         memcpy(szBoxName, pabyData + nPos, 4);
     459             :                         uint32_t nChunkSize;
     460           4 :                         memcpy(&nChunkSize, pabyData + nPos + 4, 4);
     461           4 :                         CPL_LSBPTR32(&nChunkSize);
     462           4 :                         if (nChunkSize % 2)  // Payload padding if needed
     463           1 :                             nChunkSize++;
     464           4 :                         if (nChunkSize > nSize - (nPos + 8))
     465           0 :                             break;
     466           4 :                         if (memcmp(szBoxName, "META", 4) == 0)
     467             :                         {
     468           1 :                             CPLDebug("WEBP",
     469             :                                      "Remove existing %s box from "
     470             :                                      "source compressed data",
     471             :                                      szBoxName);
     472           1 :                             if (nPos + 8 + nChunkSize < nSize)
     473             :                             {
     474           0 :                                 memmove(pabyData + nPos,
     475           0 :                                         pabyData + nPos + 8 + nChunkSize,
     476           0 :                                         nSize - (nPos + 8 + nChunkSize));
     477             :                             }
     478           1 :                             nSize -= 8 + nChunkSize;
     479             :                         }
     480             :                         else
     481             :                         {
     482           3 :                             nPos += 8 + nChunkSize;
     483             :                         }
     484             :                     }
     485             : 
     486             :                     // Patch size of RIFF
     487           2 :                     uint32_t nSize32 = nSize - 8;
     488           2 :                     CPL_LSBPTR32(&nSize32);
     489           2 :                     memcpy(pabyData + 4, &nSize32, 4);
     490             :                 }
     491             :             }
     492           2 :             if (pnBufferSize)
     493           2 :                 *pnBufferSize = nSize;
     494           2 :             return CE_None;
     495             :         }
     496             :     }
     497           0 :     return CE_Failure;
     498             : }
     499             : 
     500             : /************************************************************************/
     501             : /*                          OpenPAM()                                   */
     502             : /************************************************************************/
     503             : 
     504         465 : GDALPamDataset *WEBPDataset::OpenPAM(GDALOpenInfo *poOpenInfo)
     505             : 
     506             : {
     507         465 :     if (!WEBPDriverIdentify(poOpenInfo) || poOpenInfo->fpL == nullptr)
     508           0 :         return nullptr;
     509             : 
     510             :     int nWidth, nHeight;
     511         465 :     if (!WebPGetInfo(reinterpret_cast<const uint8_t *>(poOpenInfo->pabyHeader),
     512         465 :                      static_cast<uint32_t>(poOpenInfo->nHeaderBytes), &nWidth,
     513             :                      &nHeight))
     514           0 :         return nullptr;
     515             : 
     516         465 :     int nBands = 3;
     517             : 
     518         930 :     auto poDS = std::make_unique<WEBPDataset>();
     519             : 
     520             : #if WEBP_DECODER_ABI_VERSION >= 0x0002
     521             :     WebPDecoderConfig config;
     522         465 :     if (!WebPInitDecoderConfig(&config))
     523           0 :         return nullptr;
     524             : 
     525             :     const bool bOK =
     526         465 :         WebPGetFeatures(poOpenInfo->pabyHeader, poOpenInfo->nHeaderBytes,
     527         465 :                         &config.input) == VP8_STATUS_OK;
     528             : 
     529             :     // Cf commit https://github.com/webmproject/libwebp/commit/86c0031eb2c24f78d4dcfc5dab752ebc9f511607#diff-859d219dccb3163cc11cd538effed461ff0145135070abfe70bd263f16408023
     530             :     // Added in webp 0.4.0
     531             : #if WEBP_DECODER_ABI_VERSION >= 0x0202
     532         930 :     poDS->GDALDataset::SetMetadataItem(
     533             :         "COMPRESSION_REVERSIBILITY",
     534         465 :         config.input.format == 2 ? "LOSSLESS" : "LOSSY", "IMAGE_STRUCTURE");
     535             : #endif
     536             : 
     537         465 :     if (config.input.has_alpha)
     538         296 :         nBands = 4;
     539             : 
     540         465 :     WebPFreeDecBuffer(&config.output);
     541             : 
     542         465 :     if (!bOK)
     543           0 :         return nullptr;
     544             : 
     545             : #endif
     546             : 
     547         465 :     if (poOpenInfo->eAccess == GA_Update)
     548             :     {
     549           0 :         ReportUpdateNotSupportedByDriver("WEBP");
     550           0 :         return nullptr;
     551             :     }
     552             : 
     553             :     /* -------------------------------------------------------------------- */
     554             :     /*      Create a corresponding GDALDataset.                             */
     555             :     /* -------------------------------------------------------------------- */
     556         465 :     poDS->nRasterXSize = nWidth;
     557         465 :     poDS->nRasterYSize = nHeight;
     558         465 :     poDS->fpImage = poOpenInfo->fpL;
     559         465 :     poOpenInfo->fpL = nullptr;
     560             : 
     561             :     /* -------------------------------------------------------------------- */
     562             :     /*      Create band information objects.                                */
     563             :     /* -------------------------------------------------------------------- */
     564        2156 :     for (int iBand = 0; iBand < nBands; iBand++)
     565        1691 :         poDS->SetBand(iBand + 1, new WEBPRasterBand(poDS.get(), iBand + 1));
     566             : 
     567             :     /* -------------------------------------------------------------------- */
     568             :     /*      Initialize any PAM information.                                 */
     569             :     /* -------------------------------------------------------------------- */
     570         465 :     poDS->SetDescription(poOpenInfo->pszFilename);
     571             : 
     572         465 :     poDS->TryLoadXML(poOpenInfo->GetSiblingFiles());
     573             : 
     574             :     /* -------------------------------------------------------------------- */
     575             :     /*      Open overviews.                                                 */
     576             :     /* -------------------------------------------------------------------- */
     577         930 :     poDS->oOvManager.Initialize(poDS.get(), poOpenInfo->pszFilename,
     578         465 :                                 poOpenInfo->GetSiblingFiles());
     579             : 
     580         465 :     return poDS.release();
     581             : }
     582             : 
     583             : /************************************************************************/
     584             : /*                             Open()                                   */
     585             : /************************************************************************/
     586             : 
     587         360 : GDALDataset *WEBPDataset::Open(GDALOpenInfo *poOpenInfo)
     588             : 
     589             : {
     590         360 :     return OpenPAM(poOpenInfo);
     591             : }
     592             : 
     593             : /************************************************************************/
     594             : /*                              WebPUserData                            */
     595             : /************************************************************************/
     596             : 
     597             : typedef struct
     598             : {
     599             :     VSILFILE *fp;
     600             :     GDALProgressFunc pfnProgress;
     601             :     void *pProgressData;
     602             : } WebPUserData;
     603             : 
     604             : /************************************************************************/
     605             : /*                         WEBPDatasetWriter()                          */
     606             : /************************************************************************/
     607             : 
     608         843 : static int WEBPDatasetWriter(const uint8_t *data, size_t data_size,
     609             :                              const WebPPicture *const picture)
     610             : {
     611         843 :     WebPUserData *pUserData =
     612             :         reinterpret_cast<WebPUserData *>(picture->custom_ptr);
     613         843 :     return VSIFWriteL(data, 1, data_size, pUserData->fp) == data_size;
     614             : }
     615             : 
     616             : /************************************************************************/
     617             : /*                        WEBPDatasetProgressHook()                     */
     618             : /************************************************************************/
     619             : 
     620             : #if WEBP_ENCODER_ABI_VERSION >= 0x0100
     621         972 : static int WEBPDatasetProgressHook(int percent,
     622             :                                    const WebPPicture *const picture)
     623             : {
     624         972 :     WebPUserData *pUserData =
     625             :         reinterpret_cast<WebPUserData *>(picture->custom_ptr);
     626         972 :     return pUserData->pfnProgress(percent / 100.0, nullptr,
     627         972 :                                   pUserData->pProgressData);
     628             : }
     629             : #endif
     630             : 
     631             : /************************************************************************/
     632             : /*                              CreateCopy()                            */
     633             : /************************************************************************/
     634             : 
     635         132 : GDALDataset *WEBPDataset::CreateCopy(const char *pszFilename,
     636             :                                      GDALDataset *poSrcDS, int bStrict,
     637             :                                      char **papszOptions,
     638             :                                      GDALProgressFunc pfnProgress,
     639             :                                      void *pProgressData)
     640             : 
     641             : {
     642             :     const char *pszLossLessCopy =
     643         132 :         CSLFetchNameValueDef(papszOptions, "LOSSLESS_COPY", "AUTO");
     644         132 :     if (EQUAL(pszLossLessCopy, "AUTO") || CPLTestBool(pszLossLessCopy))
     645             :     {
     646         132 :         void *pWEBPContent = nullptr;
     647         132 :         size_t nWEBPContent = 0;
     648         132 :         if (poSrcDS->ReadCompressedData(
     649             :                 "WEBP", 0, 0, poSrcDS->GetRasterXSize(),
     650             :                 poSrcDS->GetRasterYSize(), poSrcDS->GetRasterCount(), nullptr,
     651         264 :                 &pWEBPContent, &nWEBPContent, nullptr) == CE_None)
     652             :         {
     653           2 :             CPLDebug("WEBP", "Lossless copy from source dataset");
     654           2 :             std::vector<GByte> abyData;
     655             :             try
     656             :             {
     657           2 :                 abyData.assign(static_cast<const GByte *>(pWEBPContent),
     658           2 :                                static_cast<const GByte *>(pWEBPContent) +
     659           2 :                                    nWEBPContent);
     660             : 
     661           2 :                 char **papszXMP = poSrcDS->GetMetadata("xml:XMP");
     662           2 :                 if (papszXMP && papszXMP[0])
     663             :                 {
     664             :                     GByte abyChunkHeader[8];
     665           1 :                     memcpy(abyChunkHeader, "META", 4);
     666           1 :                     const size_t nXMPSize = strlen(papszXMP[0]);
     667           1 :                     uint32_t nChunkSize = static_cast<uint32_t>(nXMPSize);
     668           1 :                     CPL_LSBPTR32(&nChunkSize);
     669           1 :                     memcpy(abyChunkHeader + 4, &nChunkSize, 4);
     670           0 :                     abyData.insert(abyData.end(), abyChunkHeader,
     671           1 :                                    abyChunkHeader + sizeof(abyChunkHeader));
     672             :                     abyData.insert(
     673           0 :                         abyData.end(), reinterpret_cast<GByte *>(papszXMP[0]),
     674           1 :                         reinterpret_cast<GByte *>(papszXMP[0]) + nXMPSize);
     675           1 :                     if ((abyData.size() % 2) != 0)  // Payload padding if needed
     676           1 :                         abyData.push_back(0);
     677             : 
     678             :                     // Patch size of RIFF
     679             :                     uint32_t nSize32 =
     680           1 :                         static_cast<uint32_t>(abyData.size()) - 8;
     681           1 :                     CPL_LSBPTR32(&nSize32);
     682           1 :                     memcpy(abyData.data() + 4, &nSize32, 4);
     683             :                 }
     684             :             }
     685           0 :             catch (const std::exception &e)
     686             :             {
     687           0 :                 CPLError(CE_Failure, CPLE_AppDefined, "Exception occurred: %s",
     688           0 :                          e.what());
     689           0 :                 abyData.clear();
     690             :             }
     691           2 :             VSIFree(pWEBPContent);
     692             : 
     693           2 :             if (!abyData.empty())
     694             :             {
     695           2 :                 VSILFILE *fpImage = VSIFOpenL(pszFilename, "wb");
     696           2 :                 if (fpImage == nullptr)
     697             :                 {
     698           0 :                     CPLError(CE_Failure, CPLE_OpenFailed,
     699             :                              "Unable to create jpeg file %s.", pszFilename);
     700             : 
     701           0 :                     return nullptr;
     702             :                 }
     703           4 :                 if (VSIFWriteL(abyData.data(), 1, abyData.size(), fpImage) !=
     704           2 :                     abyData.size())
     705             :                 {
     706           0 :                     CPLError(CE_Failure, CPLE_FileIO,
     707           0 :                              "Failure writing data: %s", VSIStrerror(errno));
     708           0 :                     VSIFCloseL(fpImage);
     709           0 :                     return nullptr;
     710             :                 }
     711           2 :                 if (VSIFCloseL(fpImage) != 0)
     712             :                 {
     713           0 :                     CPLError(CE_Failure, CPLE_FileIO,
     714           0 :                              "Failure writing data: %s", VSIStrerror(errno));
     715           0 :                     return nullptr;
     716             :                 }
     717             : 
     718           2 :                 pfnProgress(1.0, nullptr, pProgressData);
     719             : 
     720             :                 // Re-open file and clone missing info to PAM
     721           2 :                 GDALOpenInfo oOpenInfo(pszFilename, GA_ReadOnly);
     722           2 :                 auto poDS = OpenPAM(&oOpenInfo);
     723           2 :                 if (poDS)
     724             :                 {
     725           2 :                     poDS->CloneInfo(poSrcDS, GCIF_PAM_DEFAULT);
     726             :                 }
     727             : 
     728           2 :                 return poDS;
     729             :             }
     730             :         }
     731             :     }
     732             : 
     733         130 :     const bool bLossless = CPLFetchBool(papszOptions, "LOSSLESS", false);
     734         251 :     if (!bLossless &&
     735         121 :         (!EQUAL(pszLossLessCopy, "AUTO") && CPLTestBool(pszLossLessCopy)))
     736             :     {
     737           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     738             :                  "LOSSLESS_COPY=YES requested but not possible");
     739           0 :         return nullptr;
     740             :     }
     741             : 
     742             :     /* -------------------------------------------------------------------- */
     743             :     /*      WEBP library initialization                                     */
     744             :     /* -------------------------------------------------------------------- */
     745             : 
     746             :     WebPPicture sPicture;
     747         130 :     if (!WebPPictureInit(&sPicture))
     748             :     {
     749           0 :         CPLError(CE_Failure, CPLE_AppDefined, "WebPPictureInit() failed");
     750           0 :         return nullptr;
     751             :     }
     752             : 
     753             :     /* -------------------------------------------------------------------- */
     754             :     /*      Some some rudimentary checks                                    */
     755             :     /* -------------------------------------------------------------------- */
     756             : 
     757         130 :     const int nXSize = poSrcDS->GetRasterXSize();
     758         130 :     const int nYSize = poSrcDS->GetRasterYSize();
     759         130 :     if (nXSize > 16383 || nYSize > 16383)
     760             :     {
     761           0 :         CPLError(CE_Failure, CPLE_NotSupported,
     762             :                  "WEBP maximum image dimensions are 16383 x 16383.");
     763             : 
     764           0 :         return nullptr;
     765             :     }
     766             : 
     767         130 :     const int nBands = poSrcDS->GetRasterCount();
     768         130 :     if (nBands != 3
     769             : #if WEBP_ENCODER_ABI_VERSION >= 0x0100
     770          96 :         && nBands != 4
     771             : #endif
     772             :     )
     773             :     {
     774          14 :         CPLError(CE_Failure, CPLE_NotSupported,
     775             :                  "WEBP driver doesn't support %d bands. Must be 3 (RGB) "
     776             : #if WEBP_ENCODER_ABI_VERSION >= 0x0100
     777             :                  "or 4 (RGBA) "
     778             : #endif
     779             :                  "bands.",
     780             :                  nBands);
     781             : 
     782          14 :         return nullptr;
     783             :     }
     784             : 
     785         116 :     const GDALDataType eDT = poSrcDS->GetRasterBand(1)->GetRasterDataType();
     786             : 
     787         116 :     if (eDT != GDT_Byte)
     788             :     {
     789           0 :         CPLError((bStrict) ? CE_Failure : CE_Warning, CPLE_NotSupported,
     790             :                  "WEBP driver doesn't support data type %s. "
     791             :                  "Only eight bit byte bands supported.",
     792             :                  GDALGetDataTypeName(
     793             :                      poSrcDS->GetRasterBand(1)->GetRasterDataType()));
     794             : 
     795           0 :         if (bStrict)
     796           0 :             return nullptr;
     797             :     }
     798             : 
     799             :     /* -------------------------------------------------------------------- */
     800             :     /*      What options has the user selected?                             */
     801             :     /* -------------------------------------------------------------------- */
     802         116 :     float fQuality = 75.0f;
     803         116 :     const char *pszQUALITY = CSLFetchNameValue(papszOptions, "QUALITY");
     804         116 :     if (pszQUALITY != nullptr)
     805             :     {
     806          91 :         fQuality = static_cast<float>(CPLAtof(pszQUALITY));
     807          91 :         if (fQuality < 0.0f || fQuality > 100.0f)
     808             :         {
     809           0 :             CPLError(CE_Failure, CPLE_IllegalArg, "%s=%s is not a legal value.",
     810             :                      "QUALITY", pszQUALITY);
     811           0 :             return nullptr;
     812             :         }
     813             :     }
     814             : 
     815         116 :     WebPPreset nPreset = WEBP_PRESET_DEFAULT;
     816             :     const char *pszPRESET =
     817         116 :         CSLFetchNameValueDef(papszOptions, "PRESET", "DEFAULT");
     818         116 :     if (EQUAL(pszPRESET, "DEFAULT"))
     819         116 :         nPreset = WEBP_PRESET_DEFAULT;
     820           0 :     else if (EQUAL(pszPRESET, "PICTURE"))
     821           0 :         nPreset = WEBP_PRESET_PICTURE;
     822           0 :     else if (EQUAL(pszPRESET, "PHOTO"))
     823           0 :         nPreset = WEBP_PRESET_PHOTO;
     824           0 :     else if (EQUAL(pszPRESET, "PICTURE"))
     825           0 :         nPreset = WEBP_PRESET_PICTURE;
     826           0 :     else if (EQUAL(pszPRESET, "DRAWING"))
     827           0 :         nPreset = WEBP_PRESET_DRAWING;
     828           0 :     else if (EQUAL(pszPRESET, "ICON"))
     829           0 :         nPreset = WEBP_PRESET_ICON;
     830           0 :     else if (EQUAL(pszPRESET, "TEXT"))
     831           0 :         nPreset = WEBP_PRESET_TEXT;
     832             :     else
     833             :     {
     834           0 :         CPLError(CE_Failure, CPLE_IllegalArg, "%s=%s is not a legal value.",
     835             :                  "PRESET", pszPRESET);
     836           0 :         return nullptr;
     837             :     }
     838             : 
     839             :     WebPConfig sConfig;
     840         116 :     if (!WebPConfigInitInternal(&sConfig, nPreset, fQuality,
     841             :                                 WEBP_ENCODER_ABI_VERSION))
     842             :     {
     843           0 :         CPLError(CE_Failure, CPLE_AppDefined, "WebPConfigInit() failed");
     844           0 :         return nullptr;
     845             :     }
     846             : 
     847             :     // TODO: Get rid of this macro in a reasonable way.
     848             : #define FETCH_AND_SET_OPTION_INT(name, fieldname, minval, maxval)              \
     849             :     {                                                                          \
     850             :         const char *pszVal = CSLFetchNameValue(papszOptions, name);            \
     851             :         if (pszVal != nullptr)                                                 \
     852             :         {                                                                      \
     853             :             sConfig.fieldname = atoi(pszVal);                                  \
     854             :             if (sConfig.fieldname < minval || sConfig.fieldname > maxval)      \
     855             :             {                                                                  \
     856             :                 CPLError(CE_Failure, CPLE_IllegalArg,                          \
     857             :                          "%s=%s is not a legal value.", name, pszVal);         \
     858             :                 return nullptr;                                                \
     859             :             }                                                                  \
     860             :         }                                                                      \
     861             :     }
     862             : 
     863         116 :     FETCH_AND_SET_OPTION_INT("TARGETSIZE", target_size, 0, INT_MAX - 1);
     864             : 
     865         116 :     const char *pszPSNR = CSLFetchNameValue(papszOptions, "PSNR");
     866         116 :     if (pszPSNR)
     867             :     {
     868           0 :         sConfig.target_PSNR = static_cast<float>(CPLAtof(pszPSNR));
     869           0 :         if (sConfig.target_PSNR < 0)
     870             :         {
     871           0 :             CPLError(CE_Failure, CPLE_IllegalArg,
     872             :                      "PSNR=%s is not a legal value.", pszPSNR);
     873           0 :             return nullptr;
     874             :         }
     875             :     }
     876             : 
     877         116 :     FETCH_AND_SET_OPTION_INT("METHOD", method, 0, 6);
     878         116 :     FETCH_AND_SET_OPTION_INT("SEGMENTS", segments, 1, 4);
     879         116 :     FETCH_AND_SET_OPTION_INT("SNS_STRENGTH", sns_strength, 0, 100);
     880         116 :     FETCH_AND_SET_OPTION_INT("FILTER_STRENGTH", filter_strength, 0, 100);
     881         116 :     FETCH_AND_SET_OPTION_INT("FILTER_SHARPNESS", filter_sharpness, 0, 7);
     882         116 :     FETCH_AND_SET_OPTION_INT("FILTER_TYPE", filter_type, 0, 1);
     883         116 :     FETCH_AND_SET_OPTION_INT("AUTOFILTER", autofilter, 0, 1);
     884         116 :     FETCH_AND_SET_OPTION_INT("PASS", pass, 1, 10);
     885         116 :     FETCH_AND_SET_OPTION_INT("PREPROCESSING", preprocessing, 0, 1);
     886         116 :     FETCH_AND_SET_OPTION_INT("PARTITIONS", partitions, 0, 3);
     887             : #if WEBP_ENCODER_ABI_VERSION >= 0x0002
     888         116 :     FETCH_AND_SET_OPTION_INT("PARTITION_LIMIT", partition_limit, 0, 100);
     889             : #endif
     890             : #if WEBP_ENCODER_ABI_VERSION >= 0x0100
     891         116 :     sConfig.lossless = bLossless;
     892         116 :     if (sConfig.lossless)
     893           9 :         sPicture.use_argb = 1;
     894             : #endif
     895             : #if WEBP_ENCODER_ABI_VERSION >= 0x0209
     896         116 :     FETCH_AND_SET_OPTION_INT("EXACT", exact, 0, 1);
     897             : #endif
     898             : 
     899         116 :     if (!WebPValidateConfig(&sConfig))
     900             :     {
     901           0 :         CPLError(CE_Failure, CPLE_AppDefined, "WebPValidateConfig() failed");
     902           0 :         return nullptr;
     903             :     }
     904             : 
     905             :     /* -------------------------------------------------------------------- */
     906             :     /*      Allocate memory                                                 */
     907             :     /* -------------------------------------------------------------------- */
     908             :     GByte *pabyBuffer =
     909         116 :         reinterpret_cast<GByte *>(VSI_MALLOC3_VERBOSE(nBands, nXSize, nYSize));
     910         116 :     if (pabyBuffer == nullptr)
     911             :     {
     912           0 :         return nullptr;
     913             :     }
     914             : 
     915             :     /* -------------------------------------------------------------------- */
     916             :     /*      Create the dataset.                                             */
     917             :     /* -------------------------------------------------------------------- */
     918         116 :     VSILFILE *fpImage = VSIFOpenL(pszFilename, "wb");
     919         116 :     if (fpImage == nullptr)
     920             :     {
     921           3 :         CPLError(CE_Failure, CPLE_OpenFailed,
     922             :                  "Unable to create WEBP file %s.\n", pszFilename);
     923           3 :         VSIFree(pabyBuffer);
     924           3 :         return nullptr;
     925             :     }
     926             : 
     927             :     WebPUserData sUserData;
     928         113 :     sUserData.fp = fpImage;
     929         113 :     sUserData.pfnProgress = pfnProgress ? pfnProgress : GDALDummyProgress;
     930         113 :     sUserData.pProgressData = pProgressData;
     931             : 
     932             :     /* -------------------------------------------------------------------- */
     933             :     /*      WEBP library settings                                           */
     934             :     /* -------------------------------------------------------------------- */
     935             : 
     936         113 :     sPicture.width = nXSize;
     937         113 :     sPicture.height = nYSize;
     938         113 :     sPicture.writer = WEBPDatasetWriter;
     939         113 :     sPicture.custom_ptr = &sUserData;
     940             : #if WEBP_ENCODER_ABI_VERSION >= 0x0100
     941         113 :     sPicture.progress_hook = WEBPDatasetProgressHook;
     942             : #endif
     943         113 :     if (!WebPPictureAlloc(&sPicture))
     944             :     {
     945           0 :         CPLError(CE_Failure, CPLE_AppDefined, "WebPPictureAlloc() failed");
     946           0 :         VSIFree(pabyBuffer);
     947           0 :         VSIFCloseL(fpImage);
     948           0 :         return nullptr;
     949             :     }
     950             : 
     951             :     /* -------------------------------------------------------------------- */
     952             :     /*      Acquire source imagery.                                         */
     953             :     /* -------------------------------------------------------------------- */
     954             :     CPLErr eErr =
     955         226 :         poSrcDS->RasterIO(GF_Read, 0, 0, nXSize, nYSize, pabyBuffer, nXSize,
     956             :                           nYSize, GDT_Byte, nBands, nullptr, nBands,
     957         113 :                           static_cast<GSpacing>(nBands) * nXSize, 1, nullptr);
     958             : 
     959             : /* -------------------------------------------------------------------- */
     960             : /*      Import and write to file                                        */
     961             : /* -------------------------------------------------------------------- */
     962             : #if WEBP_ENCODER_ABI_VERSION >= 0x0100
     963         113 :     if (eErr == CE_None && nBands == 4)
     964             :     {
     965          82 :         if (!WebPPictureImportRGBA(&sPicture, pabyBuffer, nBands * nXSize))
     966             :         {
     967           0 :             CPLError(CE_Failure, CPLE_AppDefined,
     968             :                      "WebPPictureImportRGBA() failed");
     969           0 :             eErr = CE_Failure;
     970             :         }
     971             :     }
     972             :     else
     973             : #endif
     974          62 :         if (eErr == CE_None &&
     975          31 :             !WebPPictureImportRGB(&sPicture, pabyBuffer, nBands * nXSize))
     976             :     {
     977           0 :         CPLError(CE_Failure, CPLE_AppDefined, "WebPPictureImportRGB() failed");
     978           0 :         eErr = CE_Failure;
     979             :     }
     980             : 
     981         113 :     if (eErr == CE_None && !WebPEncode(&sConfig, &sPicture))
     982             :     {
     983             : #if WEBP_ENCODER_ABI_VERSION >= 0x0100
     984          10 :         const char *pszErrorMsg = nullptr;
     985          10 :         switch (sPicture.error_code)
     986             :         {
     987           0 :             case VP8_ENC_ERROR_OUT_OF_MEMORY:
     988           0 :                 pszErrorMsg = "Out of memory";
     989           0 :                 break;
     990           0 :             case VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY:
     991           0 :                 pszErrorMsg = "Out of memory while flushing bits";
     992           0 :                 break;
     993           0 :             case VP8_ENC_ERROR_NULL_PARAMETER:
     994           0 :                 pszErrorMsg = "A pointer parameter is NULL";
     995           0 :                 break;
     996           0 :             case VP8_ENC_ERROR_INVALID_CONFIGURATION:
     997           0 :                 pszErrorMsg = "Configuration is invalid";
     998           0 :                 break;
     999           0 :             case VP8_ENC_ERROR_BAD_DIMENSION:
    1000           0 :                 pszErrorMsg = "Picture has invalid width/height";
    1001           0 :                 break;
    1002           0 :             case VP8_ENC_ERROR_PARTITION0_OVERFLOW:
    1003           0 :                 pszErrorMsg = "Partition is bigger than 512k. Try using less "
    1004             :                               "SEGMENTS, or increase PARTITION_LIMIT value";
    1005           0 :                 break;
    1006           0 :             case VP8_ENC_ERROR_PARTITION_OVERFLOW:
    1007           0 :                 pszErrorMsg = "Partition is bigger than 16M";
    1008           0 :                 break;
    1009           5 :             case VP8_ENC_ERROR_BAD_WRITE:
    1010           5 :                 pszErrorMsg = "Error while flushing bytes";
    1011           5 :                 break;
    1012           0 :             case VP8_ENC_ERROR_FILE_TOO_BIG:
    1013           0 :                 pszErrorMsg = "File is bigger than 4G";
    1014           0 :                 break;
    1015           0 :             case VP8_ENC_ERROR_USER_ABORT:
    1016           0 :                 pszErrorMsg = "User interrupted";
    1017           0 :                 break;
    1018           5 :             default:
    1019           5 :                 CPLError(CE_Failure, CPLE_AppDefined,
    1020             :                          "WebPEncode returned an unknown error code: %d",
    1021           5 :                          sPicture.error_code);
    1022           5 :                 pszErrorMsg = "Unknown WebP error type.";
    1023           5 :                 break;
    1024             :         }
    1025          10 :         CPLError(CE_Failure, CPLE_AppDefined, "WebPEncode() failed : %s",
    1026             :                  pszErrorMsg);
    1027             : #else
    1028             :         CPLError(CE_Failure, CPLE_AppDefined, "WebPEncode() failed");
    1029             : #endif
    1030          10 :         eErr = CE_Failure;
    1031             :     }
    1032             : 
    1033             :     /* -------------------------------------------------------------------- */
    1034             :     /*      Cleanup and close.                                              */
    1035             :     /* -------------------------------------------------------------------- */
    1036         113 :     CPLFree(pabyBuffer);
    1037             : 
    1038         113 :     WebPPictureFree(&sPicture);
    1039             : 
    1040         113 :     VSIFCloseL(fpImage);
    1041             : 
    1042         113 :     if (pfnProgress)
    1043         113 :         pfnProgress(1.0, "", pProgressData);
    1044             : 
    1045         113 :     if (eErr != CE_None)
    1046             :     {
    1047          10 :         VSIUnlink(pszFilename);
    1048          10 :         return nullptr;
    1049             :     }
    1050             : 
    1051             :     /* -------------------------------------------------------------------- */
    1052             :     /*      Re-open dataset, and copy any auxiliary pam information.        */
    1053             :     /* -------------------------------------------------------------------- */
    1054         206 :     GDALOpenInfo oOpenInfo(pszFilename, GA_ReadOnly);
    1055             : 
    1056             :     /* If writing to stdout, we can't reopen it, so return */
    1057             :     /* a fake dataset to make the caller happy */
    1058         103 :     CPLPushErrorHandler(CPLQuietErrorHandler);
    1059         103 :     auto poDS = WEBPDataset::OpenPAM(&oOpenInfo);
    1060         103 :     CPLPopErrorHandler();
    1061         103 :     if (poDS)
    1062             :     {
    1063         103 :         poDS->CloneInfo(poSrcDS, GCIF_PAM_DEFAULT);
    1064         103 :         return poDS;
    1065             :     }
    1066             : 
    1067           0 :     return nullptr;
    1068             : }
    1069             : 
    1070             : /************************************************************************/
    1071             : /*                         GDALRegister_WEBP()                          */
    1072             : /************************************************************************/
    1073             : 
    1074          12 : void GDALRegister_WEBP()
    1075             : 
    1076             : {
    1077          12 :     if (GDALGetDriverByName(DRIVER_NAME) != nullptr)
    1078           0 :         return;
    1079             : 
    1080          12 :     GDALDriver *poDriver = new GDALDriver();
    1081          12 :     WEBPDriverSetCommonMetadata(poDriver);
    1082             : 
    1083          12 :     poDriver->pfnOpen = WEBPDataset::Open;
    1084          12 :     poDriver->pfnCreateCopy = WEBPDataset::CreateCopy;
    1085             : 
    1086          12 :     GetGDALDriverManager()->RegisterDriver(poDriver);
    1087             : }

Generated by: LCOV version 1.14