ktx-loader.cpp
4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* Copyright (c) 2017 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// FILE HEADER
#include "ktx-loader.h"
// EXTERNAL INCLUDES
#include <memory.h>
#include <stdio.h>
#include <stdint.h>
namespace PbrDemo
{
struct KtxFileHeader
{
char identifier[12];
uint32_t endianness;
uint32_t glType; //(UNSIGNED_BYTE, UNSIGNED_SHORT_5_6_5, etc.)
uint32_t glTypeSize;
uint32_t glFormat; //(RGB, RGBA, BGRA, etc.)
uint32_t glInternalFormat; //For uncompressed textures, specifies the internalformat parameter passed to glTexStorage*D or glTexImage*D
uint32_t glBaseInternalFormat;
uint32_t pixelWidth;
uint32_t pixelHeight;
uint32_t pixelDepth;
uint32_t numberOfArrayElements;
uint32_t numberOfFaces; //Cube map faces are stored in the order: +X, -X, +Y, -Y, +Z, -Z.
uint32_t numberOfMipmapLevels;
uint32_t bytesOfKeyValueData;
};
/**
* Convert KTX format to Dali::Pixel::Format
*/
bool ConvertPixelFormat(const uint32_t ktxPixelFormat, Dali::Pixel::Format& format)
{
switch( ktxPixelFormat )
{
case 0x93B0: // GL_COMPRESSED_RGBA_ASTC_4x4_KHR
{
format = Dali::Pixel::COMPRESSED_RGBA_ASTC_4x4_KHR;
break;
}
case 0x881B:// GL_RGB16F
{
format = Dali::Pixel::RGB16F;
break;
}
case 0x8815: // GL_RGB32F
{
format = Dali::Pixel::RGB32F;
break;
}
case 0x8C3A: // GL_R11F_G11F_B10F
{
format = Dali::Pixel::RGB32F;
break;
}
case 0x8D7C: // GL_RGBA8UI
{
format = Dali::Pixel::RGBA8888;
break;
}
case 0x8D7D: // GL_RGB8UI
{
format = Dali::Pixel::RGB888;
break;
}
default:
{
return false;
}
}
return true;
}
bool LoadCubeMapFromKtxFile( const std::string& path, CubeData& cubedata )
{
FILE* fp = fopen(path.c_str(),"rb");
if( NULL == fp )
{
return false;
}
KtxFileHeader header;
int result = fread(&header,1,sizeof(KtxFileHeader),fp);
if( 0 == result )
{
fclose( fp );
return false;
}
long lSize = 0;
// Skip the key-values:
const long int imageSizeOffset = sizeof(KtxFileHeader) + header.bytesOfKeyValueData;
if( fseek(fp, imageSizeOffset, SEEK_END) )
{
fclose( fp );
return false;
}
lSize = ftell(fp);
if( lSize == -1L )
{
fclose( fp );
return false;
}
rewind(fp);
if( fseek(fp, imageSizeOffset, SEEK_SET) )
{
fclose( fp );
return false;
}
cubedata.img.resize(header.numberOfFaces);
for(unsigned int face=0; face < header.numberOfFaces; ++face) //array_element must be 0 or 1
{
cubedata.img[face].resize(header.numberOfMipmapLevels);
}
unsigned char* buffer= reinterpret_cast<unsigned char*>( malloc( lSize ) );
unsigned char* img[6];
unsigned int imgSize[6];
unsigned char* imgPointer = buffer;
result = fread(buffer,1,lSize,fp);
fclose(fp);
if( 0 == result )
{
free( buffer );
return false;
}
if( 0 == header.numberOfMipmapLevels )
{
header.numberOfMipmapLevels = 1u;
}
if( 0 == header.numberOfArrayElements )
{
header.numberOfArrayElements = 1u;
}
if( 0 == header.pixelDepth )
{
header.pixelDepth = 1u;
}
if( 0 == header.pixelHeight )
{
header.pixelHeight = 1u;
}
Dali::Pixel::Format daliformat = Pixel::RGB888;
ConvertPixelFormat(header.glInternalFormat, daliformat);
for( unsigned int mipmapLevel = 0; mipmapLevel < header.numberOfMipmapLevels; ++mipmapLevel )
{
long int byteSize = 0;
int imageSize;
memcpy(&imageSize,imgPointer,sizeof(unsigned int));
imgPointer += 4u;
for(unsigned int arrayElement=0; arrayElement < header.numberOfArrayElements; ++arrayElement) //arrayElement must be 0 or 1
{
for(unsigned int face=0; face < header.numberOfFaces; ++face)
{
byteSize = imageSize;
if(byteSize % 4u)
{
byteSize += 4u - byteSize % 4u;
}
img[face] = reinterpret_cast<unsigned char*>( malloc( byteSize ) ); // resources will be freed when the PixelData is destroyed.
memcpy(img[face],imgPointer,byteSize);
imgSize[face] = byteSize;
imgPointer += byteSize;
cubedata.img[face][mipmapLevel] = PixelData::New( img[face], imgSize[face], header.pixelWidth , header.pixelHeight , daliformat, PixelData::FREE );
}
}
header.pixelHeight/=2u;
header.pixelWidth/=2u;
}
free(buffer);
return true;
}
} // namespace PbrDemo