Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / rendering-basic-pbr / ktx-loader.cpp
index 03b6b80..5b34fda 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 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.
 #include "ktx-loader.h"
 
 // EXTERNAL INCLUDES
+#include <dali/devel-api/adaptor-framework/file-stream.h>
+#include <dali/integration-api/debug.h>
 #include <memory.h>
-#include <stdio.h>
 #include <stdint.h>
-#include <dali/integration-api/debug.h>
-#include <dali/devel-api/adaptor-framework/file-stream.h>
+#include <stdio.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;
+  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;
 };
 
 /**
@@ -51,14 +50,14 @@ struct KtxFileHeader
  */
 bool ConvertPixelFormat(const uint32_t ktxPixelFormat, Dali::Pixel::Format& format)
 {
-  switch( ktxPixelFormat )
+  switch(ktxPixelFormat)
   {
     case 0x93B0: // GL_COMPRESSED_RGBA_ASTC_4x4_KHR
     {
       format = Dali::Pixel::COMPRESSED_RGBA_ASTC_4x4_KHR;
       break;
     }
-    case 0x881B:// GL_RGB16F
+    case 0x881B: // GL_RGB16F
     {
       format = Dali::Pixel::RGB16F;
       break;
@@ -92,84 +91,55 @@ bool ConvertPixelFormat(const uint32_t ktxPixelFormat, Dali::Pixel::Format& form
   return true;
 }
 
-bool LoadCubeMapFromKtxFile( const std::string& path, CubeData& cubedata )
+bool LoadCubeMapFromKtxFile(const std::string& path, CubeData& cubedata)
 {
-  Dali::FileStream fileStream( path, Dali::FileStream::READ | Dali::FileStream::BINARY );
-  FILE* fp = fileStream.GetFile();
-  if( NULL == fp )
+  std::unique_ptr<FILE, void (*)(FILE*)> fp(fopen(path.c_str(), "rb"), [](FILE* fp) {
+    if(fp)
+    {
+      fclose(fp);
+    }
+  });
+  if(!fp)
   {
     return false;
   }
 
   KtxFileHeader header;
-
-  int result = fread(&header,1,sizeof(KtxFileHeader),fp);
-  if( 0 == result )
+  int           result = fread(&header, sizeof(KtxFileHeader), 1u, fp.get());
+  if(0 == result)
   {
     return false;
   }
 
-  long lSize = 0;
-
   // Skip the key-values:
-  const long int imageSizeOffset = sizeof(KtxFileHeader) + header.bytesOfKeyValueData;
-
-  if( fseek(fp, 0, SEEK_END) )
-  {
-    return false;
-  }
-
-  lSize = ftell(fp);
-  if( lSize == -1L )
-  {
-    return false;
-  }
-
-  lSize -= imageSizeOffset;
-
-  rewind(fp);
-
-  if( fseek(fp, imageSizeOffset, SEEK_SET) )
+  if(fseek(fp.get(), header.bytesOfKeyValueData, SEEK_CUR))
   {
     return false;
   }
 
   cubedata.img.resize(header.numberOfFaces);
 
-  for(unsigned int face=0; face < header.numberOfFaces; ++face) //array_element must be 0 or 1
+  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);
-
-  if( 0 == result )
-  {
-    free( buffer );
-    return false;
-  }
-
-  if( 0 == header.numberOfMipmapLevels )
+  if(0 == header.numberOfMipmapLevels)
   {
     header.numberOfMipmapLevels = 1u;
   }
 
-  if( 0 == header.numberOfArrayElements )
+  if(0 == header.numberOfArrayElements)
   {
     header.numberOfArrayElements = 1u;
   }
 
-  if( 0 == header.pixelDepth )
+  if(0 == header.pixelDepth)
   {
     header.pixelDepth = 1u;
   }
 
-  if( 0 == header.pixelHeight )
+  if(0 == header.pixelHeight)
   {
     header.pixelHeight = 1u;
   }
@@ -178,33 +148,36 @@ bool LoadCubeMapFromKtxFile( const std::string& path, CubeData& cubedata )
 
   ConvertPixelFormat(header.glInternalFormat, daliformat);
 
-  for( unsigned int mipmapLevel = 0; mipmapLevel < header.numberOfMipmapLevels; ++mipmapLevel )
+  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
+    uint32_t byteSize = 0;
+    if(fread(&byteSize, sizeof(byteSize), 1u, fp.get()) != 1)
+    {
+      return false;
+    }
+
+    if(0 != byteSize % 4u)
     {
-      for(unsigned int face=0; face < header.numberOfFaces; ++face)
+      byteSize += 4u - byteSize % 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)
+        std::unique_ptr<uint8_t, void (*)(void*)> img(static_cast<unsigned char*>(malloc(byteSize)), free); // resources will be freed when the PixelData is destroyed.
+        if(fread(img.get(), byteSize, 1u, fp.get()) != 1)
         {
-          byteSize += 4u - byteSize % 4u;
+          return false;
         }
-        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 );
+        cubedata.img[face][mipmapLevel] = PixelData::New(img.release(), byteSize, header.pixelWidth, header.pixelHeight, daliformat, PixelData::FREE);
       }
     }
-    header.pixelHeight/=2u;
-    header.pixelWidth/=2u;
+
+    header.pixelHeight /= 2u;
+    header.pixelWidth /= 2u;
   }
 
-  free(buffer);
   return true;
 }