LCOV - code coverage report
Current view: top level - frmts/heif - heifdrivercore.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 38 39 97.4 %
Date: 2024-11-21 22:18:42 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  HEIF Driver
       4             :  * Author:   Even Rouault <even.rouault at spatialys.com>
       5             :  *
       6             :  ******************************************************************************
       7             :  * Copyright (c) 2020, Even Rouault <even.rouault at spatialys.com>
       8             :  *
       9             :  * SPDX-License-Identifier: MIT
      10             :  ****************************************************************************/
      11             : 
      12             : #include "include_libheif.h"
      13             : 
      14             : #include "heifdrivercore.h"
      15             : 
      16             : /************************************************************************/
      17             : /*                    HEIFDriverIdentifySimplified()                    */
      18             : /************************************************************************/
      19             : 
      20             : static const GByte FTYP_4CC[] = {'f', 't', 'y', 'p'};
      21             : static const GByte supportedBrands[][4]{
      22             :     {'a', 'v', 'i', 'f'}, {'h', 'e', 'i', 'c'}, {'h', 'e', 'i', 'x'},
      23             :     {'j', '2', 'k', 'i'}, {'j', 'p', 'e', 'g'}, {'m', 'i', 'a', 'f'},
      24             :     {'m', 'i', 'f', '1'}, {'m', 'i', 'f', '2'}, {'v', 'v', 'i', 'c'}};
      25             : 
      26       50542 : int HEIFDriverIdentifySimplified(GDALOpenInfo *poOpenInfo)
      27             : {
      28       50542 :     if (STARTS_WITH_CI(poOpenInfo->pszFilename, "HEIF:"))
      29             :     {
      30           9 :         return true;
      31             :     }
      32       50533 :     if (poOpenInfo->nHeaderBytes < 16 || poOpenInfo->fpL == nullptr)
      33             :     {
      34       47820 :         return false;
      35             :     }
      36        2713 :     if (memcmp(poOpenInfo->pabyHeader + 4, FTYP_4CC, sizeof(FTYP_4CC)) != 0)
      37             :     {
      38        2680 :         return false;
      39             :     }
      40             :     uint32_t lengthBigEndian;
      41          33 :     memcpy(&lengthBigEndian, poOpenInfo->pabyHeader, sizeof(uint32_t));
      42          33 :     uint32_t lengthHostEndian = CPL_MSBWORD32(lengthBigEndian);
      43          33 :     if (lengthHostEndian > static_cast<uint32_t>(poOpenInfo->nHeaderBytes))
      44             :     {
      45           0 :         lengthHostEndian = static_cast<uint32_t>(poOpenInfo->nHeaderBytes);
      46             :     }
      47         225 :     for (const GByte *supportedBrand : supportedBrands)
      48             :     {
      49         208 :         if (memcmp(poOpenInfo->pabyHeader + 8, supportedBrand, 4) == 0)
      50             :         {
      51          16 :             return true;
      52             :         }
      53             :     }
      54          31 :     for (uint32_t offset = 16; offset + 4 <= lengthHostEndian; offset += 4)
      55             :     {
      56         155 :         for (const GByte *supportedBrand : supportedBrands)
      57             :         {
      58         141 :             if (memcmp(poOpenInfo->pabyHeader + offset, supportedBrand, 4) == 0)
      59             :             {
      60           3 :                 return true;
      61             :             }
      62             :         }
      63             :     }
      64          14 :     return false;
      65             : }
      66             : 
      67             : /************************************************************************/
      68             : /*                     HEIFDriverSetCommonMetadata()                    */
      69             : /************************************************************************/
      70             : 
      71        1302 : void HEIFDriverSetCommonMetadata(GDALDriver *poDriver)
      72             : {
      73        1302 :     poDriver->SetDescription(DRIVER_NAME);
      74        1302 :     poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES");
      75        1302 :     poDriver->SetMetadataItem(
      76             :         GDAL_DMD_LONGNAME,
      77        1302 :         "ISO/IEC 23008-12:2017 High Efficiency Image File Format");
      78        1302 :     poDriver->SetMetadataItem(GDAL_DMD_MIMETYPE, "image/heic");
      79        1302 :     poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, "drivers/raster/heif.html");
      80        1302 :     poDriver->SetMetadataItem(GDAL_DMD_EXTENSION, "heic");
      81             : #ifdef HAS_CUSTOM_FILE_READER
      82        1302 :     poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES");
      83             : #endif
      84             : 
      85        1302 :     poDriver->SetMetadataItem("LIBHEIF_VERSION", LIBHEIF_VERSION);
      86             : 
      87        1302 :     poDriver->pfnIdentify = HEIFDriverIdentifySimplified;
      88        1302 :     poDriver->SetMetadataItem(GDAL_DCAP_OPEN, "YES");
      89             : #ifdef HAS_CUSTOM_FILE_WRITER
      90        1302 :     poDriver->SetMetadataItem(GDAL_DCAP_CREATECOPY, "YES");
      91             : #endif
      92        1302 : }
      93             : 
      94             : /************************************************************************/
      95             : /*                     DeclareDeferredHEIFPlugin()                      */
      96             : /************************************************************************/
      97             : 
      98             : #ifdef PLUGIN_FILENAME
      99        1595 : void DeclareDeferredHEIFPlugin()
     100             : {
     101        1595 :     if (GDALGetDriverByName(DRIVER_NAME) != nullptr)
     102             :     {
     103         302 :         return;
     104             :     }
     105        1293 :     auto poDriver = new GDALPluginDriverProxy(PLUGIN_FILENAME);
     106             : #ifdef PLUGIN_INSTALLATION_MESSAGE
     107             :     poDriver->SetMetadataItem(GDAL_DMD_PLUGIN_INSTALLATION_MESSAGE,
     108             :                               PLUGIN_INSTALLATION_MESSAGE);
     109             : #endif
     110        1293 :     HEIFDriverSetCommonMetadata(poDriver);
     111        1293 :     GetGDALDriverManager()->DeclareDeferredPluginDriver(poDriver);
     112             : }
     113             : #endif

Generated by: LCOV version 1.14