Merge "Change the path of 'configure' in README.md" into devel/master
[platform/core/uifw/dali-adaptor.git] / adaptors / devel-api / adaptor-framework / image-loading.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 // CLASS HEADER
18 #include "image-loading.h"
19
20 // INTERNAL INCLUDES
21 #include "image-loaders/image-loader.h"
22 #include <resource-loader/network/file-download.h>
23 #include <platform-abstractions/portable/file-closer.h>
24 #include <platform-abstractions/tizen/resource-loader/resource-loading-client.h>
25
26 namespace Dali
27 {
28
29 namespace
30 {
31 // limit maximum image down load size to 50 MB
32 const size_t MAXIMUM_DOWNLOAD_IMAGE_SIZE  = 50 * 1024 * 1024 ;
33 }
34
35 PixelData LoadImageFromFile( const std::string& url, ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection )
36 {
37   Integration::BitmapResourceType resourceType( size, fittingMode, samplingMode, orientationCorrection );
38   IntrusivePtr<Dali::RefObject> resource = TizenPlatform::ImageLoader::LoadResourceSynchronously( resourceType, url );
39
40   if( resource )
41   {
42     Integration::Bitmap* bitmap = static_cast<Integration::Bitmap*>( resource.Get() );
43
44     // Use bitmap->GetBufferOwnership() to transfer the buffer ownership to pixelData.
45     // The destroy of bitmap will not release the buffer, instead, the pixelData is responsible for releasing when its reference count falls to zero.
46     return Dali::PixelData::New( bitmap->GetBufferOwnership(),
47                                  bitmap->GetBufferSize(),
48                                  bitmap->GetImageWidth(),
49                                  bitmap->GetImageHeight(),
50                                  bitmap->GetPixelFormat(),
51                                  Dali::PixelData::FREE );
52   }
53   return Dali::PixelData();
54 }
55
56 ImageDimensions GetClosestImageSize( const std::string& filename,
57                                      ImageDimensions size,
58                                      FittingMode::Type fittingMode,
59                                      SamplingMode::Type samplingMode,
60                                      bool orientationCorrection )
61 {
62   return TizenPlatform::ImageLoader::GetClosestImageSize( filename, size, fittingMode, samplingMode, orientationCorrection );
63 }
64
65
66 PixelData DownloadImageSynchronously( const std::string& url, ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection )
67 {
68   Integration::BitmapResourceType resourceType( size, fittingMode, samplingMode, orientationCorrection );
69
70   bool succeeded;
71   Dali::Vector<uint8_t> dataBuffer;
72   size_t dataSize;
73
74   succeeded = TizenPlatform::Network::DownloadRemoteFileIntoMemory( url, dataBuffer, dataSize,
75                                                                     MAXIMUM_DOWNLOAD_IMAGE_SIZE );
76   if( succeeded )
77   {
78     void *blobBytes = static_cast<void*>(&dataBuffer[0]);
79     size_t blobSize = dataBuffer.Size();
80
81     DALI_ASSERT_DEBUG( blobSize > 0U );
82     DALI_ASSERT_DEBUG( blobBytes != 0U );
83
84     if( blobBytes != 0 && blobSize > 0U )
85     {
86       // Open a file handle on the memory buffer:
87       Dali::Internal::Platform::FileCloser fileCloser( blobBytes, blobSize, "rb" );
88       FILE * const fp = fileCloser.GetFile();
89       if ( NULL != fp )
90       {
91         Integration::BitmapPtr bitmap;
92         bool result = TizenPlatform::ImageLoader::ConvertStreamToBitmap(
93           resourceType,
94           url,
95           fp,
96           TizenPlatform::StubbedResourceLoadingClient(),
97           bitmap );
98
99         if ( result && bitmap )
100         {
101           return Dali::PixelData::New( bitmap->GetBufferOwnership(),
102                                        bitmap->GetBufferSize(),
103                                        bitmap->GetImageWidth(),
104                                        bitmap->GetImageHeight(),
105                                        bitmap->GetPixelFormat(),
106                                        Dali::PixelData::FREE );
107         }
108         else
109         {
110           DALI_LOG_WARNING( "Unable to decode bitmap supplied as in-memory blob.\n" );
111         }
112       }
113     }
114
115   }
116   return Dali::PixelData();
117 }
118
119
120 } // namespace Dali