Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: OpenGIS Simple Features Reference Implementation 4 : * Purpose: Implements Basis Universal / KTX2 driver. 5 : * Author: Even Rouault, <even dot rouault at spatialys.com> 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 2022, Even Rouault <even dot rouault at spatialys.com> 9 : * 10 : * Permission is hereby granted, free of charge, to any person obtaining a 11 : * copy of this software and associated documentation files (the "Software"), 12 : * to deal in the Software without restriction, including without limitation 13 : * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 : * and/or sell copies of the Software, and to permit persons to whom the 15 : * Software is furnished to do so, subject to the following conditions: 16 : * 17 : * The above copyright notice and this permission notice shall be included 18 : * in all copies or substantial portions of the Software. 19 : * 20 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 : * DEALINGS IN THE SOFTWARE. 27 : ****************************************************************************/ 28 : 29 : #include "basisudrivercore.h" 30 : #include "commoncore.h" 31 : 32 : /************************************************************************/ 33 : /* BASISUDriverIdentify() */ 34 : /************************************************************************/ 35 : 36 47775 : int BASISUDriverIdentify(GDALOpenInfo *poOpenInfo) 37 : 38 : { 39 47775 : if (STARTS_WITH(poOpenInfo->pszFilename, "BASISU:")) 40 12 : return true; 41 : // See 42 : // https://github.com/BinomialLLC/basis_universal/blob/master/spec/basis_spec.txt 43 47763 : constexpr int HEADER_SIZE = 77; 44 47763 : if (!(poOpenInfo->fpL != nullptr && 45 2588 : poOpenInfo->nHeaderBytes >= HEADER_SIZE && 46 2362 : poOpenInfo->pabyHeader[0] == 0x73 && // Signature 47 65 : poOpenInfo->pabyHeader[1] == 0x42 && // Signature 48 65 : poOpenInfo->pabyHeader[4] == HEADER_SIZE && // Header size 49 65 : poOpenInfo->pabyHeader[5] == 0)) // Header size 50 : { 51 47698 : return false; 52 : } 53 65 : const uint32_t nDataSize = CPL_LSBUINT32PTR(poOpenInfo->pabyHeader + 8); 54 65 : VSIFSeekL(poOpenInfo->fpL, 0, SEEK_END); 55 65 : const auto nFileSize = VSIFTellL(poOpenInfo->fpL); 56 65 : VSIFSeekL(poOpenInfo->fpL, 0, SEEK_SET); 57 65 : return nDataSize + HEADER_SIZE == nFileSize; 58 : } 59 : 60 : /************************************************************************/ 61 : /* BASISUDriverSetCommonMetadata() */ 62 : /************************************************************************/ 63 : 64 1229 : void BASISUDriverSetCommonMetadata(GDALDriver *poDriver) 65 : { 66 1229 : poDriver->SetDescription(BASISU_DRIVER_NAME); 67 1229 : poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES"); 68 1229 : poDriver->SetMetadataItem(GDAL_DMD_LONGNAME, 69 1229 : "Basis Universal texture format"); 70 1229 : poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, "drivers/raster/basisu.html"); 71 1229 : poDriver->SetMetadataItem(GDAL_DMD_EXTENSION, "basis"); 72 1229 : poDriver->SetMetadataItem(GDAL_DMD_CREATIONDATATYPES, "Byte"); 73 : 74 1229 : poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES"); 75 : 76 1229 : poDriver->SetMetadataItem( 77 : GDAL_DMD_CREATIONOPTIONLIST, 78 2458 : GDAL_KTX2_BASISU_GetCreationOptions(/* bIsKTX2 = */ false).c_str()); 79 : 80 1229 : poDriver->pfnIdentify = BASISUDriverIdentify; 81 1229 : poDriver->SetMetadataItem(GDAL_DCAP_OPEN, "YES"); 82 1229 : poDriver->SetMetadataItem(GDAL_DCAP_CREATECOPY, "YES"); 83 1229 : } 84 : 85 : /************************************************************************/ 86 : /* DeclareDeferredBASISUPlugin() */ 87 : /************************************************************************/ 88 : 89 : #ifdef PLUGIN_FILENAME 90 1522 : void DeclareDeferredBASISUPlugin() 91 : { 92 1522 : if (GDALGetDriverByName(BASISU_DRIVER_NAME) != nullptr) 93 : { 94 301 : return; 95 : } 96 1221 : auto poDriver = new GDALPluginDriverProxy(PLUGIN_FILENAME); 97 : #ifdef PLUGIN_INSTALLATION_MESSAGE 98 : poDriver->SetMetadataItem(GDAL_DMD_PLUGIN_INSTALLATION_MESSAGE, 99 : PLUGIN_INSTALLATION_MESSAGE); 100 : #endif 101 1221 : BASISUDriverSetCommonMetadata(poDriver); 102 1221 : GetGDALDriverManager()->DeclareDeferredPluginDriver(poDriver); 103 : } 104 : 105 1522 : void DeclareDeferredBASISU_KTX2Plugin() 106 : { 107 1522 : DeclareDeferredBASISUPlugin(); 108 1522 : DeclareDeferredKTX2Plugin(); 109 1522 : } 110 : 111 : #endif