Revert "[3.0] Fix Gif Loader using uninitialized variable"
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / tizen / image-loaders / loader-gif.cpp
index 6d9ed0e..9bb3657 100644 (file)
 
 #include <dali/integration-api/debug.h>
 #include <dali/integration-api/bitmap.h>
-#include <dali/public-api/images/image-attributes.h>
+
+// We need to check if giflib has the new open and close API (including error parameter).
+#ifdef GIFLIB_MAJOR
+#define LIBGIF_VERSION_5_1_OR_ABOVE
+#endif
 
 namespace Dali
 {
@@ -48,7 +52,17 @@ struct AutoCleanupGif
     if(NULL != gifInfo)
     {
       // clean up GIF resources
-      DGifCloseFile(gifInfo);
+#ifdef LIBGIF_VERSION_5_1_OR_ABOVE
+      int errorCode = 0; //D_GIF_SUCCEEDED is 0
+      DGifCloseFile( gifInfo, &errorCode );
+
+      if( errorCode )
+      {
+        DALI_LOG_ERROR( "GIF Loader: DGifCloseFile Error. Code: %d\n", errorCode );
+      }
+#else
+      DGifCloseFile( gifInfo );
+#endif
     }
   }
 
@@ -99,10 +113,17 @@ int ReadDataFromGif(GifFileType *gifInfo, GifByteType *data, int length)
 /// Loads the GIF Header.
 bool LoadGifHeader(FILE *fp, unsigned int &width, unsigned int &height, GifFileType** gifInfo)
 {
-  *gifInfo = DGifOpen(reinterpret_cast<void*>(fp), ReadDataFromGif);
+  int errorCode = 0; //D_GIF_SUCCEEDED is 0
 
-  if ( !(*gifInfo) )
+#ifdef LIBGIF_VERSION_5_1_OR_ABOVE
+  *gifInfo = DGifOpen( reinterpret_cast<void*>(fp), ReadDataFromGif, &errorCode );
+#else
+  *gifInfo = DGifOpen( reinterpret_cast<void*>(fp), ReadDataFromGif );
+#endif
+
+  if ( !(*gifInfo) || errorCode )
   {
+    DALI_LOG_ERROR( "GIF Loader: DGifOpen Error. Code: %d\n", errorCode );
     return false;
   }
 
@@ -174,7 +195,7 @@ GifColorType* GetImageColors( SavedImage* image, GifFileType* gifInfo )
 }
 
 /// Called when we want to handle IMAGE_DESC_RECORD_TYPE
-bool HandleImageDescriptionRecordType( Bitmap& bitmap, ImageAttributes& attributes, GifFileType* gifInfo, unsigned int width, unsigned int height, bool& finished )
+bool HandleImageDescriptionRecordType( Bitmap& bitmap, GifFileType* gifInfo, unsigned int width, unsigned int height, bool& finished )
 {
   if ( DGifGetImageDesc( gifInfo ) == GIF_ERROR )
   {
@@ -206,8 +227,6 @@ bool HandleImageDescriptionRecordType( Bitmap& bitmap, ImageAttributes& attribut
     return false;
   }
 
-  // TODO: Add scaling support
-
   // Get the colormap for the GIF
   GifColorType* color( GetImageColors( image, gifInfo ) );
 
@@ -231,8 +250,6 @@ bool HandleImageDescriptionRecordType( Bitmap& bitmap, ImageAttributes& attribut
     }
   }
 
-  attributes.SetSize( actualWidth, actualHeight );
-
   finished = true;
 
   return true;
@@ -246,8 +263,14 @@ bool HandleExtensionRecordType( GifFileType* gifInfo )
   image.ExtensionBlockCount = 0;
   GifByteType *extensionByte( NULL );
 
+#ifdef LIBGIF_VERSION_5_1_OR_ABOVE
+  int *extensionBlockTypePointer = &image.ExtensionBlocks->Function;
+#else
+  int *extensionBlockTypePointer = &image.Function;
+#endif
+
   // Not really interested in the extensions so just skip them unless there is an error.
-  for ( int extRetCode = DGifGetExtension( gifInfo, &image.Function, &extensionByte );
+  for ( int extRetCode = DGifGetExtension( gifInfo, extensionBlockTypePointer, &extensionByte );
         extensionByte != NULL;
         extRetCode = DGifGetExtensionNext( gifInfo, &extensionByte ) )
   {
@@ -263,16 +286,18 @@ bool HandleExtensionRecordType( GifFileType* gifInfo )
 
 } // unnamed namespace
 
-bool LoadGifHeader(FILE *fp, const ImageAttributes& attributes, unsigned int &width, unsigned int &height )
+bool LoadGifHeader( const ImageLoader::Input& input, unsigned int& width, unsigned int& height )
 {
   GifFileType* gifInfo = NULL;
   AutoCleanupGif autoCleanupGif(gifInfo);
+  FILE* const fp = input.file;
 
   return LoadGifHeader(fp, width, height, &gifInfo);
 }
 
-bool LoadBitmapFromGif(FILE *fp, Bitmap& bitmap, ImageAttributes& attributes, const ResourceLoadingClient& client )
+bool LoadBitmapFromGif( const ResourceLoadingClient& client, const ImageLoader::Input& input, Integration::Bitmap& bitmap )
 {
+  FILE* const fp = input.file;
   // Load the GIF Header file.
 
   GifFileType* gifInfo( NULL );
@@ -300,7 +325,7 @@ bool LoadBitmapFromGif(FILE *fp, Bitmap& bitmap, ImageAttributes& attributes, co
 
     if( IMAGE_DESC_RECORD_TYPE == recordType )
     {
-      if ( !HandleImageDescriptionRecordType( bitmap, attributes, gifInfo, width, height, finished ) )
+      if ( !HandleImageDescriptionRecordType( bitmap, gifInfo, width, height, finished ) )
       {
         return false;
       }