Exposing Exif Image metadata
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / tizen / image-loaders / loader-ktx.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include "loader-ktx.h"
20
21 // EXTERNAL INCLUDES
22 #include <cstring>
23 #include <dali/public-api/common/compile-time-assert.h>
24 #include <dali/integration-api/debug.h>
25 #include <adaptors/devel-api/adaptor-framework/pixel-buffer.h>
26
27 namespace Dali
28 {
29
30 namespace TizenPlatform
31 {
32
33 namespace
34 {
35
36 /** Max width or height of an image. */
37 const unsigned MAX_TEXTURE_DIMENSION = 4096;
38 /** Max bytes of image data allowed. Not a precise number, just a sanity check. */
39 const unsigned MAX_IMAGE_DATA_SIZE = MAX_TEXTURE_DIMENSION * MAX_TEXTURE_DIMENSION;
40 /** We don't read any of this but limit it to a resonable amount in order to be
41  * friendly to files from random tools. */
42 const unsigned MAX_BYTES_OF_KEYVALUE_DATA = 65536U;
43
44 typedef uint8_t Byte;
45
46 const Byte FileIdentifier[] = {
47    0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A
48 };
49
50
51 /** The formats we support inside a KTX file container.
52  *  Currently only compressed formats are allowed as we'd rather
53  *  use a PNG or JPEG with their own compression for the general
54  *  cases. */
55 enum KtxInternalFormat
56 {
57   KTX_NOTEXIST = 0,
58
59   // GLES 2 Extension formats:
60   KTX_ETC1_RGB8_OES                               = 0x8D64,
61   KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG             = 0x8C00,
62
63   // GLES 3 Standard compressed formats (values same as in gl3.h):
64   KTX_COMPRESSED_R11_EAC                          = 0x9270,
65   KTX_COMPRESSED_SIGNED_R11_EAC                   = 0x9271,
66   KTX_COMPRESSED_RG11_EAC                         = 0x9272,
67   KTX_COMPRESSED_SIGNED_RG11_EAC                  = 0x9273,
68   KTX_COMPRESSED_RGB8_ETC2                        = 0x9274,
69   KTX_COMPRESSED_SRGB8_ETC2                       = 0x9275,
70   KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2    = 0x9276,
71   KTX_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2   = 0x9277,
72   KTX_COMPRESSED_RGBA8_ETC2_EAC                   = 0x9278,
73   KTX_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC            = 0x9279,
74
75   // GLES 3.1 compressed formats:
76   KTX_COMPRESSED_RGBA_ASTC_4x4_KHR                = 0x93B0,
77   KTX_COMPRESSED_RGBA_ASTC_5x4_KHR                = 0x93B1,
78   KTX_COMPRESSED_RGBA_ASTC_5x5_KHR                = 0x93B2,
79   KTX_COMPRESSED_RGBA_ASTC_6x5_KHR                = 0x93B3,
80   KTX_COMPRESSED_RGBA_ASTC_6x6_KHR                = 0x93B4,
81   KTX_COMPRESSED_RGBA_ASTC_8x5_KHR                = 0x93B5,
82   KTX_COMPRESSED_RGBA_ASTC_8x6_KHR                = 0x93B6,
83   KTX_COMPRESSED_RGBA_ASTC_8x8_KHR                = 0x93B7,
84   KTX_COMPRESSED_RGBA_ASTC_10x5_KHR               = 0x93B8,
85   KTX_COMPRESSED_RGBA_ASTC_10x6_KHR               = 0x93B9,
86   KTX_COMPRESSED_RGBA_ASTC_10x8_KHR               = 0x93BA,
87   KTX_COMPRESSED_RGBA_ASTC_10x10_KHR              = 0x93BB,
88   KTX_COMPRESSED_RGBA_ASTC_12x10_KHR              = 0x93BC,
89   KTX_COMPRESSED_RGBA_ASTC_12x12_KHR              = 0x93BD,
90   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR        = 0x93D0,
91   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR        = 0x93D1,
92   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR        = 0x93D2,
93   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR        = 0x93D3,
94   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR        = 0x93D4,
95   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR        = 0x93D5,
96   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR        = 0x93D6,
97   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR        = 0x93D7,
98   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR       = 0x93D8,
99   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR       = 0x93D9,
100   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR       = 0x93DA,
101   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR      = 0x93DB,
102   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR      = 0x93DC,
103   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR      = 0x93DD,
104
105   // Uncompressed Alpha format
106   KTX_UNCOMPRESSED_ALPHA8                         = 0x1906,
107
108   KTX_SENTINEL = ~0u
109 };
110
111 const unsigned KtxInternalFormats[] =
112 {
113   // GLES 2 Extension formats:
114   KTX_ETC1_RGB8_OES,
115   KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG,
116
117   // GLES 3 Standard compressed formats:
118   KTX_COMPRESSED_R11_EAC,
119   KTX_COMPRESSED_SIGNED_R11_EAC,
120   KTX_COMPRESSED_RG11_EAC,
121   KTX_COMPRESSED_SIGNED_RG11_EAC,
122   KTX_COMPRESSED_RGB8_ETC2,
123   KTX_COMPRESSED_SRGB8_ETC2,
124   KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,
125   KTX_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2,
126   KTX_COMPRESSED_RGBA8_ETC2_EAC,
127   KTX_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC,
128
129   // GLES 3.1 Compressed formats:
130   KTX_COMPRESSED_RGBA_ASTC_4x4_KHR,
131   KTX_COMPRESSED_RGBA_ASTC_5x4_KHR,
132   KTX_COMPRESSED_RGBA_ASTC_5x5_KHR,
133   KTX_COMPRESSED_RGBA_ASTC_6x5_KHR,
134   KTX_COMPRESSED_RGBA_ASTC_6x6_KHR,
135   KTX_COMPRESSED_RGBA_ASTC_8x5_KHR,
136   KTX_COMPRESSED_RGBA_ASTC_8x6_KHR,
137   KTX_COMPRESSED_RGBA_ASTC_8x8_KHR,
138   KTX_COMPRESSED_RGBA_ASTC_10x5_KHR,
139   KTX_COMPRESSED_RGBA_ASTC_10x6_KHR,
140   KTX_COMPRESSED_RGBA_ASTC_10x8_KHR,
141   KTX_COMPRESSED_RGBA_ASTC_10x10_KHR,
142   KTX_COMPRESSED_RGBA_ASTC_12x10_KHR,
143   KTX_COMPRESSED_RGBA_ASTC_12x12_KHR,
144   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR,
145   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR,
146   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR,
147   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR,
148   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR,
149   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR,
150   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR,
151   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR,
152   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR,
153   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR,
154   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR,
155   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR,
156   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR,
157   KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR,
158
159   // Uncompressed Alpha format
160   KTX_UNCOMPRESSED_ALPHA8,
161
162   KTX_SENTINEL
163 };
164
165 struct KtxFileHeader
166 {
167   Byte   identifier[12];
168   uint32_t endianness;
169   uint32_t glType;
170   uint32_t glTypeSize;
171   uint32_t glFormat;
172   uint32_t glInternalFormat;
173   uint32_t glBaseInternalFormat;
174   uint32_t pixelWidth;
175   uint32_t pixelHeight;
176   uint32_t pixelDepth;
177   uint32_t numberOfArrayElements;
178   uint32_t numberOfFaces;
179   uint32_t numberOfMipmapLevels;
180   uint32_t bytesOfKeyValueData;
181 } __attribute__ ( (__packed__));
182 // Packed attribute stops the structure from being aligned to compiler defaults
183 // so we can be sure of reading the whole thing from file in one call to fread.
184
185 /**
186  * Function to read from the file directly into our structure.
187  * @param[in]  fp     The file to read from
188  * @param[out] header The structure we want to store our information in
189  * @return true, if read successful, false otherwise
190  */
191 inline bool ReadHeader( FILE* filePointer, KtxFileHeader& header )
192 {
193   const unsigned int readLength = sizeof( KtxFileHeader );
194
195   // Load the information directly into our structure
196   if( fread( &header, 1, readLength, filePointer ) != readLength )
197   {
198     return false;
199   }
200
201   return true;
202 }
203
204 /** Check whether the array passed in is the right size and matches the magic
205  *  values defined to be at the start of a KTX file by the specification.*/
206 template<int BYTES_IN_SIGNATURE>
207 bool CheckFileIdentifier(const Byte * const signature)
208 {
209   const unsigned signatureSize = BYTES_IN_SIGNATURE;
210   const unsigned identifierSize = sizeof(FileIdentifier);
211   DALI_COMPILE_TIME_ASSERT(signatureSize == identifierSize);
212   const bool signatureGood = 0 == memcmp( signature, FileIdentifier, std::min( signatureSize, identifierSize ) );
213   return signatureGood;
214 }
215
216 /**
217  * @returns True if the argument is a GLES compressed texture format that we support.
218  */
219 bool ValidInternalFormat(const unsigned format)
220 {
221   unsigned candidateFormat = 0;
222   for(unsigned iFormat = 0; (candidateFormat = KtxInternalFormats[iFormat]) != KTX_SENTINEL; ++iFormat)
223   {
224     if(format == candidateFormat)
225     {
226       return true;
227     }
228   }
229   DALI_LOG_ERROR("Rejecting unsupported compressed format when loading compressed texture from KTX file: 0x%x.\n", format);
230   return false;
231 }
232
233 /**
234  * @returns The Pixel::Format Dali enum corresponding to the KTX internal format
235  *          passed in, or Pixel::INVALID_PIXEL_FORMAT if the format is not valid.
236  **/
237 bool ConvertPixelFormat(const uint32_t ktxPixelFormat, Dali::Pixel::Format& format)
238 {
239   using namespace Dali::Pixel;
240   switch(ktxPixelFormat)
241   {
242     // GLES 2 extension compressed formats:
243     case KTX_ETC1_RGB8_OES:
244     {
245       format = COMPRESSED_RGB8_ETC1;
246       break;
247     }
248     case KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG:
249     {
250       format = COMPRESSED_RGB_PVRTC_4BPPV1;
251       break;
252     }
253
254     // GLES 3 extension compressed formats:
255     case KTX_COMPRESSED_R11_EAC:
256     {
257       format = COMPRESSED_R11_EAC;
258       break;
259     }
260     case KTX_COMPRESSED_SIGNED_R11_EAC:
261     {
262       format = COMPRESSED_SIGNED_R11_EAC;
263       break;
264     }
265     case KTX_COMPRESSED_RG11_EAC:
266     {
267       format = COMPRESSED_RG11_EAC;
268       break;
269     }
270     case KTX_COMPRESSED_SIGNED_RG11_EAC:
271     {
272       format = COMPRESSED_SIGNED_RG11_EAC;
273       break;
274     }
275     case KTX_COMPRESSED_RGB8_ETC2:
276     {
277       format = COMPRESSED_RGB8_ETC2;
278       break;
279     }
280     case KTX_COMPRESSED_SRGB8_ETC2:
281     {
282       format = COMPRESSED_SRGB8_ETC2;
283       break;
284     }
285     case KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
286     {
287       format = COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
288       break;
289     }
290     case KTX_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
291     {
292       format = COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
293       break;
294     }
295     case KTX_COMPRESSED_RGBA8_ETC2_EAC:
296     {
297       format = COMPRESSED_RGBA8_ETC2_EAC;
298       break;
299     }
300     case KTX_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
301     {
302       format = COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
303       break;
304     }
305
306     // GLES 3.1 extension compressed formats:
307     case KTX_COMPRESSED_RGBA_ASTC_4x4_KHR:
308     {
309       format = COMPRESSED_RGBA_ASTC_4x4_KHR;
310       break;
311     }
312     case KTX_COMPRESSED_RGBA_ASTC_5x4_KHR:
313     {
314       format = COMPRESSED_RGBA_ASTC_5x4_KHR;
315       break;
316     }
317     case KTX_COMPRESSED_RGBA_ASTC_5x5_KHR:
318     {
319       format = COMPRESSED_RGBA_ASTC_5x5_KHR;
320       break;
321     }
322     case KTX_COMPRESSED_RGBA_ASTC_6x5_KHR:
323     {
324       format = COMPRESSED_RGBA_ASTC_6x5_KHR;
325       break;
326     }
327     case KTX_COMPRESSED_RGBA_ASTC_6x6_KHR:
328     {
329       format = COMPRESSED_RGBA_ASTC_6x6_KHR;
330       break;
331     }
332     case KTX_COMPRESSED_RGBA_ASTC_8x5_KHR:
333     {
334       format = COMPRESSED_RGBA_ASTC_8x5_KHR;
335       break;
336     }
337     case KTX_COMPRESSED_RGBA_ASTC_8x6_KHR:
338     {
339       format = COMPRESSED_RGBA_ASTC_8x6_KHR;
340       break;
341     }
342     case KTX_COMPRESSED_RGBA_ASTC_8x8_KHR:
343     {
344       format = COMPRESSED_RGBA_ASTC_8x8_KHR;
345       break;
346     }
347     case KTX_COMPRESSED_RGBA_ASTC_10x5_KHR:
348     {
349       format = COMPRESSED_RGBA_ASTC_10x5_KHR;
350       break;
351     }
352     case KTX_COMPRESSED_RGBA_ASTC_10x6_KHR:
353     {
354       format = COMPRESSED_RGBA_ASTC_10x6_KHR;
355       break;
356     }
357     case KTX_COMPRESSED_RGBA_ASTC_10x8_KHR:
358     {
359       format = COMPRESSED_RGBA_ASTC_10x8_KHR;
360       break;
361     }
362     case KTX_COMPRESSED_RGBA_ASTC_10x10_KHR:
363     {
364       format = COMPRESSED_RGBA_ASTC_10x10_KHR;
365       break;
366     }
367     case KTX_COMPRESSED_RGBA_ASTC_12x10_KHR:
368     {
369       format = COMPRESSED_RGBA_ASTC_12x10_KHR;
370       break;
371     }
372     case KTX_COMPRESSED_RGBA_ASTC_12x12_KHR:
373     {
374       format = COMPRESSED_RGBA_ASTC_12x12_KHR;
375       break;
376     }
377     case KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
378     {
379       format = COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR;
380       break;
381     }
382     case KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
383     {
384       format = COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR;
385       break;
386     }
387     case KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
388     {
389       format = COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR;
390       break;
391     }
392     case KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
393     {
394       format = COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR;
395       break;
396     }
397     case KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
398     {
399       format = COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR;
400       break;
401     }
402     case KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
403     {
404       format = COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR;
405       break;
406     }
407     case KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
408     {
409       format = COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR;
410       break;
411     }
412     case KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
413     {
414       format = COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR;
415       break;
416     }
417     case KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
418     {
419       format = COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR;
420       break;
421     }
422     case KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
423     {
424       format = COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR;
425       break;
426     }
427     case KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
428     {
429       format = COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR;
430       break;
431     }
432     case KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
433     {
434       format = COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR;
435       break;
436     }
437     case KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
438     {
439       format = COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR;
440       break;
441     }
442     case KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
443     {
444       format = COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR;
445       break;
446     }
447
448     // Uncompressed Alpha format
449     case KTX_UNCOMPRESSED_ALPHA8:
450     {
451       format = A8;
452       break;
453     }
454
455     default:
456     {
457        return false;
458     }
459   }
460   return true;
461 }
462
463 bool LoadKtxHeader( FILE * const fp, unsigned int& width, unsigned int& height, KtxFileHeader& fileHeader )
464 {
465   // Pull the bytes of the file header in as a block:
466   if ( !ReadHeader( fp, fileHeader ) )
467   {
468     return false;
469   }
470   width = fileHeader.pixelWidth;
471   height = fileHeader.pixelHeight;
472
473   if ( width > MAX_TEXTURE_DIMENSION || height > MAX_TEXTURE_DIMENSION )
474   {
475     return false;
476   }
477
478   // Validate file header contents meet our minimal subset:
479   const bool signatureGood                            = CheckFileIdentifier<sizeof(fileHeader.identifier)>(fileHeader.identifier);
480   const bool fileEndiannessMatchesSystemEndianness    = fileHeader.endianness == 0x04030201; // Magic number from KTX spec.
481   const bool glTypeIsCompressed                       = fileHeader.glType == 0;
482   const bool glTypeSizeCompatibleWithCompressedTex    = fileHeader.glTypeSize == 1;
483   const bool glFormatCompatibleWithCompressedTex      = fileHeader.glFormat == 0;
484   const bool glInternalFormatIsSupportedCompressedTex = ValidInternalFormat(fileHeader.glInternalFormat);
485   // Ignore glBaseInternalFormat
486   const bool textureIsNot3D                           = fileHeader.pixelDepth == 0 || fileHeader.pixelDepth == 1;
487   const bool textureIsNotAnArray                      = fileHeader.numberOfArrayElements == 0 || fileHeader.numberOfArrayElements == 1;
488   const bool textureIsNotACubemap                     = fileHeader.numberOfFaces == 0 || fileHeader.numberOfFaces == 1;
489   const bool textureHasNoMipmapLevels                 = fileHeader.numberOfMipmapLevels == 0 || fileHeader.numberOfMipmapLevels == 1;
490   const bool keyValueDataNotTooLarge                  = fileHeader.bytesOfKeyValueData <= MAX_BYTES_OF_KEYVALUE_DATA;
491
492   bool headerIsValid = signatureGood && fileEndiannessMatchesSystemEndianness &&
493                      glTypeSizeCompatibleWithCompressedTex && textureIsNot3D && textureIsNotAnArray &&
494                      textureIsNotACubemap && textureHasNoMipmapLevels && keyValueDataNotTooLarge;
495
496   if( !glTypeIsCompressed )  // check for uncompressed Alpha
497   {
498     const bool isAlpha = ( ( fileHeader.glBaseInternalFormat == KTX_UNCOMPRESSED_ALPHA8 ) && ( fileHeader.glFormat == KTX_UNCOMPRESSED_ALPHA8 ) &&
499                          ( fileHeader.glInternalFormat == KTX_UNCOMPRESSED_ALPHA8 ) );
500     headerIsValid = headerIsValid && isAlpha;
501   }
502   else
503   {
504     headerIsValid = headerIsValid && glFormatCompatibleWithCompressedTex && glInternalFormatIsSupportedCompressedTex;
505   }
506
507   if( !headerIsValid )
508   {
509      DALI_LOG_ERROR( "KTX file invalid or using unsupported features. Header tests: sig: %d, endian: %d, gl_type: %d, gl_type_size: %d, gl_format: %d, internal_format: %d, depth: %d, array: %d, faces: %d, mipmap: %d, vey-vals: %d.\n", 0+signatureGood, 0+fileEndiannessMatchesSystemEndianness, 0+glTypeIsCompressed, 0+glTypeSizeCompatibleWithCompressedTex, 0+glFormatCompatibleWithCompressedTex, 0+glInternalFormatIsSupportedCompressedTex, 0+textureIsNot3D, 0+textureIsNotAnArray, 0+textureIsNotACubemap, 0+textureHasNoMipmapLevels, 0+keyValueDataNotTooLarge);
510   }
511
512   // Warn if there is space wasted in the file:
513   if( fileHeader.bytesOfKeyValueData > 0U )
514   {
515     DALI_LOG_WARNING("Loading of KTX file with key/value header data requested. This should be stripped in application asset/resource build.\n");
516   }
517
518   return headerIsValid;
519 }
520
521
522 } // unnamed namespace
523
524 // File loading API entry-point:
525 bool LoadKtxHeader( const ImageLoader::Input& input, unsigned int& width, unsigned int& height )
526 {
527   KtxFileHeader fileHeader;
528   FILE* const fp = input.file;
529
530   bool ret = LoadKtxHeader(fp, width, height, fileHeader);
531   return ret;
532 }
533
534 // File loading API entry-point:
535 bool LoadBitmapFromKtx( const ImageLoader::Input& input, Dali::Devel::PixelBuffer& bitmap )
536 {
537   DALI_COMPILE_TIME_ASSERT( sizeof(Byte) == 1);
538   DALI_COMPILE_TIME_ASSERT( sizeof(uint32_t) == 4);
539
540   FILE* const fp = input.file;
541   if( fp == NULL )
542   {
543     DALI_LOG_ERROR( "Null file handle passed to KTX compressed bitmap file loader.\n" );
544     return false;
545   }
546   KtxFileHeader fileHeader;
547
548   // Load the header info
549   unsigned int width, height;
550
551   if (!LoadKtxHeader(fp, width, height, fileHeader))
552   {
553       return false;
554   }
555
556   // Skip the key-values:
557   const long int imageSizeOffset = sizeof(KtxFileHeader) + fileHeader.bytesOfKeyValueData;
558   if(fseek(fp, imageSizeOffset, SEEK_SET))
559   {
560     DALI_LOG_ERROR( "Seek past key/vals in KTX compressed bitmap file failed.\n" );
561     return false;
562   }
563
564   // Load the size of the image data:
565   uint32_t imageByteCount = 0;
566   if ( fread( &imageByteCount, 1, 4, fp ) != 4 )
567   {
568     DALI_LOG_ERROR( "Read of image size failed.\n" );
569     return false;
570   }
571   // Sanity-check the image size:
572   if( imageByteCount > MAX_IMAGE_DATA_SIZE ||
573       // A compressed texture should certainly be less than 2 bytes per texel:
574       imageByteCount > width * height * 2)
575   {
576     DALI_LOG_ERROR( "KTX file with too-large image-data field.\n" );
577     return false;
578   }
579
580   Pixel::Format pixelFormat;
581   const bool pixelFormatKnown = ConvertPixelFormat(fileHeader.glInternalFormat, pixelFormat);
582   if(!pixelFormatKnown)
583   {
584     DALI_LOG_ERROR( "No internal pixel format supported for KTX file pixel format.\n" );
585     return false;
586   }
587
588   // Load up the image bytes:
589   bitmap = Dali::Devel::PixelBuffer::New(width, height, pixelFormat);
590   auto pixels = bitmap.GetBuffer();
591
592   if(!pixels)
593   {
594     DALI_LOG_ERROR( "Unable to reserve a pixel buffer to load the requested bitmap into.\n" );
595     return false;
596   }
597   const size_t bytesRead = fread(pixels, 1, imageByteCount, fp);
598   if(bytesRead != imageByteCount)
599   {
600     DALI_LOG_ERROR( "Read of image pixel data failed.\n" );
601     return false;
602   }
603
604   return true;
605 }
606
607 } // namespace TizenPlatform
608
609 } // namespace Dali