2 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "ktx-loader.h"
25 #include <dali/integration-api/debug.h>
26 #include <dali/devel-api/adaptor-framework/file-stream.h>
35 uint32_t glType; //(UNSIGNED_BYTE, UNSIGNED_SHORT_5_6_5, etc.)
37 uint32_t glFormat; //(RGB, RGBA, BGRA, etc.)
38 uint32_t glInternalFormat; //For uncompressed textures, specifies the internalformat parameter passed to glTexStorage*D or glTexImage*D
39 uint32_t glBaseInternalFormat;
43 uint32_t numberOfArrayElements;
44 uint32_t numberOfFaces; //Cube map faces are stored in the order: +X, -X, +Y, -Y, +Z, -Z.
45 uint32_t numberOfMipmapLevels;
46 uint32_t bytesOfKeyValueData;
50 * Convert KTX format to Dali::Pixel::Format
52 bool ConvertPixelFormat(const uint32_t ktxPixelFormat, Dali::Pixel::Format& format)
54 switch( ktxPixelFormat )
56 case 0x93B0: // GL_COMPRESSED_RGBA_ASTC_4x4_KHR
58 format = Dali::Pixel::COMPRESSED_RGBA_ASTC_4x4_KHR;
61 case 0x881B:// GL_RGB16F
63 format = Dali::Pixel::RGB16F;
66 case 0x8815: // GL_RGB32F
68 format = Dali::Pixel::RGB32F;
71 case 0x8C3A: // GL_R11F_G11F_B10F
73 format = Dali::Pixel::RGB32F;
76 case 0x8D7C: // GL_RGBA8UI
78 format = Dali::Pixel::RGBA8888;
81 case 0x8D7D: // GL_RGB8UI
83 format = Dali::Pixel::RGB888;
95 bool LoadCubeMapFromKtxFile( const std::string& path, CubeData& cubedata )
97 Dali::FileStream fileStream( path, Dali::FileStream::READ | Dali::FileStream::BINARY );
98 FILE* fp = fileStream.GetFile();
104 KtxFileHeader header;
106 int result = fread(&header,1,sizeof(KtxFileHeader),fp);
114 // Skip the key-values:
115 const long int imageSizeOffset = sizeof(KtxFileHeader) + header.bytesOfKeyValueData;
117 if( fseek(fp, imageSizeOffset, SEEK_END) )
130 if( fseek(fp, imageSizeOffset, SEEK_SET) )
135 cubedata.img.resize(header.numberOfFaces);
137 for(unsigned int face=0; face < header.numberOfFaces; ++face) //array_element must be 0 or 1
139 cubedata.img[face].resize(header.numberOfMipmapLevels);
142 unsigned char* buffer= reinterpret_cast<unsigned char*>( malloc( lSize ) );
144 unsigned char* img[6];
145 unsigned int imgSize[6];
146 unsigned char* imgPointer = buffer;
147 result = fread(buffer,1,lSize,fp);
155 if( 0 == header.numberOfMipmapLevels )
157 header.numberOfMipmapLevels = 1u;
160 if( 0 == header.numberOfArrayElements )
162 header.numberOfArrayElements = 1u;
165 if( 0 == header.pixelDepth )
167 header.pixelDepth = 1u;
170 if( 0 == header.pixelHeight )
172 header.pixelHeight = 1u;
175 Dali::Pixel::Format daliformat = Pixel::RGB888;
177 ConvertPixelFormat(header.glInternalFormat, daliformat);
179 for( unsigned int mipmapLevel = 0; mipmapLevel < header.numberOfMipmapLevels; ++mipmapLevel )
181 long int byteSize = 0;
183 memcpy(&imageSize,imgPointer,sizeof(unsigned int));
185 for(unsigned int arrayElement=0; arrayElement < header.numberOfArrayElements; ++arrayElement) //arrayElement must be 0 or 1
187 for(unsigned int face=0; face < header.numberOfFaces; ++face)
189 byteSize = imageSize;
192 byteSize += 4u - byteSize % 4u;
194 img[face] = reinterpret_cast<unsigned char*>( malloc( byteSize ) ); // resources will be freed when the PixelData is destroyed.
195 memcpy(img[face],imgPointer,byteSize);
196 imgSize[face] = byteSize;
197 imgPointer += byteSize;
198 cubedata.img[face][mipmapLevel] = PixelData::New( img[face], imgSize[face], header.pixelWidth , header.pixelHeight , daliformat, PixelData::FREE );
201 header.pixelHeight/=2u;
202 header.pixelWidth/=2u;
209 } // namespace PbrDemo