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 51107 : int HEIFDriverIdentifySimplified(GDALOpenInfo *poOpenInfo)
27 : {
28 51107 : if (STARTS_WITH_CI(poOpenInfo->pszFilename, "HEIF:"))
29 : {
30 9 : return true;
31 : }
32 51098 : if (poOpenInfo->nHeaderBytes < 16 || poOpenInfo->fpL == nullptr)
33 : {
34 48290 : return false;
35 : }
36 2808 : if (memcmp(poOpenInfo->pabyHeader + 4, FTYP_4CC, sizeof(FTYP_4CC)) != 0)
37 : {
38 2775 : 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 1390 : void HEIFDriverSetCommonMetadata(GDALDriver *poDriver)
72 : {
73 1390 : poDriver->SetDescription(DRIVER_NAME);
74 1390 : poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES");
75 1390 : poDriver->SetMetadataItem(
76 : GDAL_DMD_LONGNAME,
77 1390 : "ISO/IEC 23008-12:2017 High Efficiency Image File Format");
78 1390 : poDriver->SetMetadataItem(GDAL_DMD_MIMETYPE, "image/heic");
79 1390 : poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, "drivers/raster/heif.html");
80 1390 : poDriver->SetMetadataItem(GDAL_DMD_EXTENSION, "heic");
81 : #ifdef HAS_CUSTOM_FILE_READER
82 1390 : poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES");
83 : #endif
84 :
85 1390 : poDriver->SetMetadataItem("LIBHEIF_VERSION", LIBHEIF_VERSION);
86 :
87 1390 : poDriver->pfnIdentify = HEIFDriverIdentifySimplified;
88 1390 : poDriver->SetMetadataItem(GDAL_DCAP_OPEN, "YES");
89 : #ifdef HAS_CUSTOM_FILE_WRITER
90 1390 : poDriver->SetMetadataItem(GDAL_DCAP_CREATECOPY, "YES");
91 : #endif
92 1390 : }
93 :
94 : /************************************************************************/
95 : /* DeclareDeferredHEIFPlugin() */
96 : /************************************************************************/
97 :
98 : #ifdef PLUGIN_FILENAME
99 1682 : void DeclareDeferredHEIFPlugin()
100 : {
101 1682 : if (GDALGetDriverByName(DRIVER_NAME) != nullptr)
102 : {
103 301 : return;
104 : }
105 1381 : 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 1381 : HEIFDriverSetCommonMetadata(poDriver);
111 1381 : GetGDALDriverManager()->DeclareDeferredPluginDriver(poDriver);
112 : }
113 : #endif
|