[4.0] Exposing Exif Image metadata
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / tizen / image-loaders / image-loader.cpp
index d1fb566..d7a6da1 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.
@@ -17,8 +17,8 @@
 #include "image-loader.h"
 
 #include <dali/devel-api/common/ref-counted-dali-vector.h>
-#include <dali/integration-api/bitmap.h>
-#include <dali/integration-api/debug.h>
+#include <adaptors/common/pixel-buffer-impl.h>
+
 
 #include "loader-astc.h"
 #include "loader-bmp.h"
@@ -29,8 +29,7 @@
 #include "loader-png.h"
 #include "loader-wbmp.h"
 #include "image-operations.h"
-#include "image-loader-input.h"
-#include "portable/file-closer.h"
+#include "portable/file-reader.h"
 
 using namespace Dali::Integration;
 
@@ -41,7 +40,7 @@ namespace TizenPlatform
 
 namespace
 {
-typedef bool (*LoadBitmapFunction)( const ResourceLoadingClient& client, const ImageLoader::Input& input, Integration::Bitmap& bitmap );
+typedef bool (*LoadBitmapFunction)( const ImageLoader::Input& input, Dali::Devel::PixelBuffer& pixelData );
 typedef bool (*LoadBitmapHeaderFunction)( const ImageLoader::Input& input, unsigned int& width, unsigned int& height );
 
 #if defined(DEBUG_ENABLED)
@@ -252,18 +251,17 @@ bool GetBitmapLoaderFunctions( FILE *fp,
 namespace ImageLoader
 {
 
-bool ConvertStreamToBitmap( const ResourceType& resourceType, std::string path, FILE * const fp, const ResourceLoadingClient& client, BitmapPtr& ptr )
+bool ConvertStreamToBitmap( const BitmapResourceType& resource, std::string path, FILE * const fp, Dali::Devel::PixelBuffer& pixelBuffer )
 {
   DALI_LOG_TRACE_METHOD( gLogFilter );
-  DALI_ASSERT_DEBUG( ResourceBitmap == resourceType.id );
 
   bool result = false;
-  BitmapPtr bitmap = 0;
 
   if (fp != NULL)
   {
     LoadBitmapFunction function;
     LoadBitmapHeaderFunction header;
+
     Bitmap::Profile profile;
 
     if ( GetBitmapLoaderFunctions( fp,
@@ -272,29 +270,19 @@ bool ConvertStreamToBitmap( const ResourceType& resourceType, std::string path,
                                    header,
                                    profile ) )
     {
-      bitmap = Bitmap::New( profile, ResourcePolicy::OWNED_DISCARD );
-
-      DALI_LOG_SET_OBJECT_STRING( bitmap, path );
-      const BitmapResourceType& resType = static_cast<const BitmapResourceType&>( resourceType );
-      const ScalingParameters scalingParameters( resType.size, resType.scalingMode, resType.samplingMode );
-      const ImageLoader::Input input( fp, scalingParameters, resType.orientationCorrection );
-
-      // Check for cancellation now we have hit the filesystem, done some allocation, and burned some cycles:
-      // This won't do anything from synchronous API, it's only useful when called from another thread.
-      client.InterruptionPoint(); // Note: By design, this can throw an exception
+      const ScalingParameters scalingParameters( resource.size, resource.scalingMode, resource.samplingMode );
+      const ImageLoader::Input input( fp, scalingParameters, resource.orientationCorrection );
 
       // Run the image type decoder:
-      result = function( client, input, *bitmap );
+      result = function( input, pixelBuffer );
 
       if (!result)
       {
         DALI_LOG_WARNING( "Unable to convert %s\n", path.c_str() );
-        bitmap = 0;
+        pixelBuffer.Reset();
       }
 
-      // Apply the requested image attributes if not interrupted:
-      client.InterruptionPoint(); // Note: By design, this can throw an exception
-      bitmap = Internal::Platform::ApplyAttributesToBitmap( bitmap, resType.size, resType.scalingMode, resType.samplingMode );
+      pixelBuffer = Internal::Platform::ApplyAttributesToBitmap( pixelBuffer, resource.size, resource.scalingMode, resource.samplingMode );
     }
     else
     {
@@ -302,26 +290,43 @@ bool ConvertStreamToBitmap( const ResourceType& resourceType, std::string path,
     }
   }
 
-  ptr.Reset( bitmap.Get() );
   return result;
 }
 
-ResourcePointer LoadResourceSynchronously( const Integration::ResourceType& resourceType, const std::string& resourcePath )
+ResourcePointer LoadImageSynchronously( const Integration::BitmapResourceType& resource, const std::string& path )
 {
-  ResourcePointer resource;
-  BitmapPtr bitmap = 0;
+  ResourcePointer result;
+  Dali::Devel::PixelBuffer bitmap;
 
-  Internal::Platform::FileCloser fc( resourcePath.c_str(), "rb");
-  FILE * const fp = fc.GetFile();
+  Internal::Platform::FileReader fileReader( path );
+  FILE * const fp = fileReader.GetFile();
   if( fp != NULL )
   {
-    bool result = ConvertStreamToBitmap( resourceType, resourcePath, fp, StubbedResourceLoadingClient(), bitmap );
-    if( result && bitmap )
+    bool success = ConvertStreamToBitmap(resource, path, fp, bitmap);
+    if (success && bitmap)
     {
-      resource.Reset(bitmap.Get());
+      Bitmap::Profile profile{Bitmap::Profile::BITMAP_2D_PACKED_PIXELS};
+
+      // For backward compatibility the Bitmap must be created
+      auto retval = Bitmap::New(profile, Dali::ResourcePolicy::OWNED_DISCARD);
+
+      DALI_LOG_SET_OBJECT_STRING( retval, path );
+
+      retval->GetPackedPixelsProfile()->ReserveBuffer(
+              bitmap.GetPixelFormat(),
+              bitmap.GetWidth(),
+              bitmap.GetHeight(),
+              bitmap.GetWidth(),
+              bitmap.GetHeight()
+            );
+
+      auto& impl = Dali::GetImplementation(bitmap);
+
+      std::copy( impl.GetBuffer(), impl.GetBuffer()+impl.GetBufferSize(), retval->GetBuffer());
+      result.Reset(retval);
     }
   }
-  return resource;
+  return result;
 }
 
 ///@ToDo: Rename GetClosestImageSize() functions. Make them use the orientation correction and scaling information. Requires jpeg loader to tell us about reorientation. [Is there still a requirement for this functionality at all?]
@@ -334,8 +339,8 @@ ImageDimensions  GetClosestImageSize( const std::string& filename,
   unsigned int width = 0;
   unsigned int height = 0;
 
-  Internal::Platform::FileCloser fc(filename.c_str(), "rb");
-  FILE *fp = fc.GetFile();
+  Internal::Platform::FileReader fileReader( filename );
+  FILE *fp = fileReader.GetFile();
   if (fp != NULL)
   {
     LoadBitmapFunction loaderFunction;
@@ -364,8 +369,6 @@ ImageDimensions  GetClosestImageSize( const std::string& filename,
   return ImageDimensions( width, height );
 }
 
-
-
 ImageDimensions GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
                                      ImageDimensions size,
                                      FittingMode::Type fittingMode,
@@ -381,16 +384,11 @@ ImageDimensions GetClosestImageSize( Integration::ResourcePointer resourceBuffer
 
   if( encodedBlob != 0 )
   {
-    const size_t blobSize     = encodedBlob->GetVector().Size();
-    uint8_t * const blobBytes = &(encodedBlob->GetVector()[0]);
-    DALI_ASSERT_DEBUG( blobSize > 0U );
-    DALI_ASSERT_DEBUG( blobBytes != 0U );
-
-    if( blobBytes != 0 && blobSize > 0U )
+    if( encodedBlob->GetVector().Size() )
     {
       // Open a file handle on the memory buffer:
-      Internal::Platform::FileCloser fc( blobBytes, blobSize, "rb" );
-      FILE *fp = fc.GetFile();
+      Internal::Platform::FileReader fileReader( encodedBlob->GetVector() );
+      FILE *fp = fileReader.GetFile();
       if ( fp != NULL )
       {
         LoadBitmapFunction loaderFunction;