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