Revert "Revert "[4.0] Fixed BMP loader.""
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / tizen / image-loaders / loader-bmp.cpp
index 2219b31..fdd0837 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * 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.
@@ -84,10 +84,10 @@ struct BmpInfoHeader
 template<typename T>
 inline bool ReadHeader(FILE* fp, T& header)
 {
-  unsigned int readLength = sizeof(T);
+  const unsigned int readLength = sizeof(T);
 
   // Load the information directly into our structure
-  if (fread((void*)&header, 1, readLength, fp) != readLength)
+  if ( fread( &header, 1, readLength, fp ) != readLength )
   {
     return false;
   }
@@ -110,6 +110,11 @@ bool LoadBmpHeader(FILE *fp, unsigned int &width, unsigned int &height, BmpFileH
   width = infoHeader.width;
   height = abs(infoHeader.height);
 
+  if( infoHeader.width == 0 )
+  {
+    return false;
+  }
+
   return true;
 }
 
@@ -578,7 +583,7 @@ bool DecodeRGB1(FILE *fp,
     }
     for(unsigned int j = 0; j < fillw; j ++)
     {
-      int ctIndex = 0;
+      unsigned int ctIndex = 0;
       if((fillw * index + j ) < (fillw * height))
       {
         ctIndex = colorIndex[ fillw * index + j ];
@@ -648,7 +653,7 @@ bool DecodeRGB4(FILE *fp,
     colorIndex[i] = cmd >> 4;
     colorIndex[i + 1] = cmd & (0x0F);
   }
-  int ctIndex = 0;
+  unsigned int ctIndex = 0;
 
   for(unsigned int index = 0; index < height; index = index + 1)
   {
@@ -722,7 +727,7 @@ bool DecodeRGB8(FILE *fp,
 
     colorIndex[i] = cmd;
   }
-  int ctIndex = 0;
+  unsigned int ctIndex = 0;
   for(unsigned int index = 0; index < height; index = index + 1)
   {
     PixelBuffer *pixelsPtr = NULL;
@@ -776,10 +781,10 @@ bool DecodeRLE4(FILE *fp,
   char colorTable[64];
   std::vector<char> colorIndex(width * height >> 1);
   std::vector<char> run;
-  int x = 0;
-  int y = 0;
-  int dx = 0;
-  int dy = 0;
+  unsigned int x = 0;
+  unsigned int y = 0;
+  unsigned int dx = 0;
+  unsigned int dy = 0;
   width += (width & 1);
   width = width >> 1;
 
@@ -938,8 +943,8 @@ bool DecodeRLE8(FILE *fp,
     return false;
   }
   PixelBuffer *pixelsPtr = pixels;
-  int x = 0;
-  int y = 0;
+  unsigned int x = 0;
+  unsigned int y = 0;
   unsigned int cmdStride = 2;
 
   width = ((width & 3) != 0) ? width + 4 - (width & 3) : width;
@@ -958,8 +963,8 @@ bool DecodeRLE8(FILE *fp,
     return false;
   }
 
-  int dx = 0;
-  int dy = 0;
+  unsigned int dx = 0;
+  unsigned int dy = 0;
   bool finish = false;
   unsigned int length = 0;
   unsigned int copylength = 0;
@@ -1051,7 +1056,7 @@ bool LoadBmpHeader( const ImageLoader::Input& input, unsigned int& width, unsign
   return ret;
 }
 
-bool LoadBitmapFromBmp( const ResourceLoadingClient& client, const ImageLoader::Input& input, Integration::Bitmap& bitmap )
+bool LoadBitmapFromBmp( const ImageLoader::Input& input, Integration::Bitmap& bitmap )
 {
   DALI_ASSERT_DEBUG( bitmap.GetPackedPixelsProfile() != 0 && "Need a packed pixel bitmap to load into." );
   FILE* const fp = input.file;
@@ -1237,6 +1242,12 @@ bool LoadBitmapFromBmp( const ResourceLoadingClient& client, const ImageLoader::
     {
       pixels = bitmap.GetPackedPixelsProfile()->ReserveBuffer(pixelFormat, infoHeader.width, infoHeader.height);
     }
+    else
+    {
+      pixelBufferW = infoHeader.width;
+      pixelBufferH = infoHeader.height;
+      newPixelFormat = pixelFormat;
+    }
     break;
   }
 
@@ -1254,7 +1265,7 @@ bool LoadBitmapFromBmp( const ResourceLoadingClient& client, const ImageLoader::
     }
     case BMP_RGB4:
     {
-      decodeResult = DecodeRGB4(fp, pixels, infoHeader.width, infoHeader.height, 14 + infoHeader.infoHeaderSize, topDown);
+      decodeResult = DecodeRGB4(fp, pixels, infoHeader.width, abs(infoHeader.height), 14 + infoHeader.infoHeaderSize, topDown);
       break;
     }
     case BMP_RLE4: