Object-less image loading abstraction 78/98778/6
authorKimmo Hoikka <kimmo.hoikka@samsung.com>
Fri, 18 Nov 2016 16:32:34 +0000 (16:32 +0000)
committerKimmo Hoikka <kimmo.hoikka@samsung.com>
Mon, 21 Nov 2016 12:19:29 +0000 (12:19 +0000)
Change-Id: I3301e28cddfeb42c52af0c94357375ece97b852e

adaptors/common/bitmap-loader-impl.cpp
adaptors/common/bitmap-loader-impl.h
adaptors/devel-api/adaptor-framework/image-loading.cpp [new file with mode: 0644]
adaptors/devel-api/adaptor-framework/image-loading.h [new file with mode: 0644]
adaptors/devel-api/file.list
automated-tests/src/dali-adaptor/CMakeLists.txt
automated-tests/src/dali-adaptor/utc-Dali-ImageLoading.cpp [new file with mode: 0644]

index 626acea..9cef7ee 100644 (file)
@@ -18,8 +18,8 @@
 #include <string>
 
 // INTERNAL INCLUDES
+#include "image-loading.h"
 #include "bitmap-loader-impl.h"
-#include "image-loaders/image-loader.h"
 
 namespace Dali
 {
@@ -40,10 +40,13 @@ BitmapLoader::BitmapLoader(const std::string& url,
                            ImageDimensions size,
                            FittingMode::Type fittingMode,
                            SamplingMode::Type samplingMode,
-                           bool orientationCorrection)
-: mResourceType( size, fittingMode, samplingMode, orientationCorrection ),
-  mPixelData(),
-  mUrl(url)
+                           bool orientationCorrection )
+: mPixelData(),
+  mUrl(url),
+  mSize( size ),
+  mFittingMode( fittingMode ),
+  mSamplingMode( samplingMode ),
+  mOrientationCorrection( orientationCorrection )
 {
 }
 
@@ -53,21 +56,7 @@ BitmapLoader::~BitmapLoader()
 
 void BitmapLoader::Load()
 {
-  IntrusivePtr<Dali::RefObject> resource = TizenPlatform::ImageLoader::LoadResourceSynchronously( mResourceType, mUrl );
-
-  if( resource )
-  {
-    Integration::Bitmap* bitmap = static_cast<Integration::Bitmap*>(resource.Get());
-
-    // Use bitmap->GetBufferOwnership() to transfer the buffer ownership to pixelData.
-    // The destroy of bitmap will not release the buffer, instead, the pixelData is responsible for releasing when its reference count falls to zero.
-    mPixelData = Dali::PixelData::New( bitmap->GetBufferOwnership(),
-                                       bitmap->GetBufferSize(),
-                                       bitmap->GetImageWidth(),
-                                       bitmap->GetImageHeight(),
-                                       bitmap->GetPixelFormat(),
-                                       Dali::PixelData::FREE);
-  }
+  mPixelData = Dali::LoadImageFromFile( mUrl, mSize, mFittingMode, mSamplingMode, mOrientationCorrection );
 }
 
 bool BitmapLoader::IsLoaded()
index 6c9d961..f328889 100644 (file)
@@ -84,9 +84,14 @@ public:
   Dali::PixelData GetPixelData() const;
 
 private:
-  Integration::BitmapResourceType mResourceType;
+
   Dali::PixelData mPixelData;
   const std::string mUrl;
+  ImageDimensions mSize;
+  FittingMode::Type mFittingMode;
+  SamplingMode::Type mSamplingMode;
+  bool mOrientationCorrection;
+
 };
 
 } // Internal
diff --git a/adaptors/devel-api/adaptor-framework/image-loading.cpp b/adaptors/devel-api/adaptor-framework/image-loading.cpp
new file mode 100644 (file)
index 0000000..68cb698
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2016 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// CLASS HEADER
+#include "image-loading.h"
+
+// INTERNAL INCLUDES
+#include "image-loaders/image-loader.h"
+
+namespace Dali
+{
+
+PixelData LoadImageFromFile( const std::string& url, ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection )
+{
+  Integration::BitmapResourceType resourceType( size, fittingMode, samplingMode, orientationCorrection );
+  IntrusivePtr<Dali::RefObject> resource = TizenPlatform::ImageLoader::LoadResourceSynchronously( resourceType, url );
+
+  if( resource )
+  {
+    Integration::Bitmap* bitmap = static_cast<Integration::Bitmap*>( resource.Get() );
+
+    // Use bitmap->GetBufferOwnership() to transfer the buffer ownership to pixelData.
+    // The destroy of bitmap will not release the buffer, instead, the pixelData is responsible for releasing when its reference count falls to zero.
+    return Dali::PixelData::New( bitmap->GetBufferOwnership(),
+                                 bitmap->GetBufferSize(),
+                                 bitmap->GetImageWidth(),
+                                 bitmap->GetImageHeight(),
+                                 bitmap->GetPixelFormat(),
+                                 Dali::PixelData::FREE );
+  }
+  return Dali::PixelData();
+}
+
+} // namespace Dali
diff --git a/adaptors/devel-api/adaptor-framework/image-loading.h b/adaptors/devel-api/adaptor-framework/image-loading.h
new file mode 100644 (file)
index 0000000..3f20bb4
--- /dev/null
@@ -0,0 +1,49 @@
+#ifndef DALI_IMAGE_LOADING_H
+#define DALI_IMAGE_LOADING_H
+
+/*
+ * Copyright (c) 2016 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// EXTERNAL INCLUDES
+#include <string>
+#include <dali/public-api/common/dali-common.h>
+#include <dali/public-api/images/image-operations.h>
+#include <dali/public-api/images/pixel-data.h>
+
+namespace Dali
+{
+
+/**
+ * @brief Load an image synchronously from local file.
+ *
+ * @note This method is thread safe, i.e. can be called from any thread.
+ *
+ * @param [in] url The URL of the image file to load.
+ * @param [in] size The width and height to fit the loaded image to, 0.0 means whole image
+ * @param [in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter.
+ * @param [in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size.
+ * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
+ * @return handle to the loaded PixelData object or an empty handle in case loading failed.
+ */
+DALI_IMPORT_API PixelData LoadImageFromFile( const std::string& url,
+                                             ImageDimensions size = ImageDimensions( 0, 0 ),
+                                             FittingMode::Type fittingMode = FittingMode::DEFAULT,
+                                             SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR,
+                                             bool orientationCorrection = true );
+
+} // Dali
+
+#endif // DALI_IMAGE_LOADING_H
index 0e1a84a..d25abc1 100755 (executable)
@@ -12,6 +12,7 @@ devel_api_src_files = \
   $(adaptor_devel_api_dir)/adaptor-framework/event-thread-callback.cpp \
   $(adaptor_devel_api_dir)/adaptor-framework/feedback-player.cpp \
   $(adaptor_devel_api_dir)/adaptor-framework/file-loader.cpp \
+  $(adaptor_devel_api_dir)/adaptor-framework/image-loading.cpp \
   $(adaptor_devel_api_dir)/adaptor-framework/imf-manager.cpp \
   $(adaptor_devel_api_dir)/adaptor-framework/orientation.cpp \
   $(adaptor_devel_api_dir)/adaptor-framework/performance-logger.cpp \
@@ -42,6 +43,7 @@ devel_api_adaptor_framework_header_files = \
   $(adaptor_devel_api_dir)/adaptor-framework/feedback-plugin.h \
   $(adaptor_devel_api_dir)/adaptor-framework/feedback-player.h \
   $(adaptor_devel_api_dir)/adaptor-framework/file-loader.h \
+  $(adaptor_devel_api_dir)/adaptor-framework/image-loading.h \
   $(adaptor_devel_api_dir)/adaptor-framework/imf-manager.h \
   $(adaptor_devel_api_dir)/adaptor-framework/lifecycle-controller.h \
   $(adaptor_devel_api_dir)/adaptor-framework/orientation.h \
index 43ad6c9..be0800b 100644 (file)
@@ -5,15 +5,16 @@ SET(RPM_NAME "core-${PKG_NAME}-tests")
 
 SET(CAPI_LIB "dali-adaptor")
 SET(TC_SOURCES
+    utc-Dali-Application.cpp
+    utc-Dali-BitmapLoader.cpp
+    utc-Dali-FileLoader.cpp
+    utc-Dali-ImageLoading.cpp
     utc-Dali-Key.cpp
     utc-Dali-NativeImageSource.cpp
     utc-Dali-SingletonService.cpp
-    utc-Dali-Window.cpp
     utc-Dali-Timer.cpp
     utc-Dali-TtsPlayer.cpp
-    utc-Dali-Application.cpp
-    utc-Dali-FileLoader.cpp
-    utc-Dali-BitmapLoader.cpp
+    utc-Dali-Window.cpp
     #utc-Dali-Watch.cpp
     #utc-Dali-KeyGrab.cpp
 )
diff --git a/automated-tests/src/dali-adaptor/utc-Dali-ImageLoading.cpp b/automated-tests/src/dali-adaptor/utc-Dali-ImageLoading.cpp
new file mode 100644 (file)
index 0000000..75c2957
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2016 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <stdlib.h>
+#include <dali/dali.h>
+#include <dali-test-suite-utils.h>
+#include <dali/devel-api/adaptor-framework/image-loading.h>
+
+using namespace Dali;
+
+namespace
+{
+// resolution: 34*34, pixel format: RGBA8888
+static const char* gImage_34_RGBA = TEST_RESOURCE_DIR "/icon-edit.png";
+// resolution: 128*128, pixel format: RGB888
+static const char* gImage_128_RGB = TEST_RESOURCE_DIR "/gallery-small-1.jpg";
+
+// this is image is not exist, for negative test
+static const char* gImageNonExist = "non-exist.jpg";
+}
+
+void utc_dali_load_image_startup(void)
+{
+  test_return_value = TET_UNDEF;
+}
+
+void utc_dali_load_image_cleanup(void)
+{
+  test_return_value = TET_PASS;
+}
+
+int UtcDaliLoadImageP(void)
+{
+  PixelData pixelData = Dali::LoadImageFromFile( gImage_34_RGBA );
+  DALI_TEST_CHECK( pixelData );
+  DALI_TEST_EQUALS( pixelData.GetWidth(), 34u, TEST_LOCATION );
+  DALI_TEST_EQUALS( pixelData.GetHeight(), 34u, TEST_LOCATION );
+  DALI_TEST_EQUALS( pixelData.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION  );
+
+  PixelData pixelData2 = Dali::LoadImageFromFile( gImage_128_RGB );
+  DALI_TEST_CHECK( pixelData2 );
+  DALI_TEST_EQUALS( pixelData2.GetWidth(), 128u, TEST_LOCATION  );
+  DALI_TEST_EQUALS( pixelData2.GetHeight(), 128u, TEST_LOCATION  );
+  DALI_TEST_EQUALS( pixelData2.GetPixelFormat(), Pixel::RGB888, TEST_LOCATION  );
+
+  END_TEST;
+}
+
+int UtcDaliLoadImageN(void)
+{
+  PixelData pixelData = Dali::LoadImageFromFile( gImageNonExist );
+  DALI_TEST_CHECK( !pixelData );
+
+  END_TEST;
+}