701ccc722bc3f993ccab5031dbddafdd47eb2e91
[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
25 namespace Dali
26 {
27
28 namespace
29 {
30 // limit maximum image down load size to 50 MB
31 const size_t MAXIMUM_DOWNLOAD_IMAGE_SIZE  = 50 * 1024 * 1024 ;
32 }
33
34 PixelData LoadImageFromFile( const std::string& url, ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection )
35 {
36   Integration::BitmapResourceType resourceType( size, fittingMode, samplingMode, orientationCorrection );
37   IntrusivePtr<Dali::RefObject> resource = TizenPlatform::ImageLoader::LoadImageSynchronously( resourceType, url );
38
39   if( resource )
40   {
41     Integration::Bitmap* bitmap = static_cast<Integration::Bitmap*>( resource.Get() );
42
43     // Use bitmap->GetBufferOwnership() to transfer the buffer ownership to pixelData.
44     // The destroy of bitmap will not release the buffer, instead, the pixelData is responsible for releasing when its reference count falls to zero.
45     return Dali::PixelData::New( bitmap->GetBufferOwnership(),
46                                  bitmap->GetBufferSize(),
47                                  bitmap->GetImageWidth(),
48                                  bitmap->GetImageHeight(),
49                                  bitmap->GetPixelFormat(),
50                                  Dali::PixelData::FREE );
51   }
52   return Dali::PixelData();
53 }
54
55 ImageDimensions GetClosestImageSize( const std::string& filename,
56                                      ImageDimensions size,
57                                      FittingMode::Type fittingMode,
58                                      SamplingMode::Type samplingMode,
59                                      bool orientationCorrection )
60 {
61   return TizenPlatform::ImageLoader::GetClosestImageSize( filename, size, fittingMode, samplingMode, orientationCorrection );
62 }
63
64
65 PixelData DownloadImageSynchronously( const std::string& url, ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection )
66 {
67   Integration::BitmapResourceType resourceType( size, fittingMode, samplingMode, orientationCorrection );
68
69   bool succeeded;
70   Dali::Vector<uint8_t> dataBuffer;
71   size_t dataSize;
72
73   succeeded = TizenPlatform::Network::DownloadRemoteFileIntoMemory( url, dataBuffer, dataSize,
74                                                                     MAXIMUM_DOWNLOAD_IMAGE_SIZE );
75   if( succeeded )
76   {
77     void *blobBytes = static_cast<void*>(&dataBuffer[0]);
78     size_t blobSize = dataBuffer.Size();
79
80     DALI_ASSERT_DEBUG( blobSize > 0U );
81     DALI_ASSERT_DEBUG( blobBytes != 0U );
82
83     if( blobBytes != 0 && blobSize > 0U )
84     {
85       // Open a file handle on the memory buffer:
86       Dali::Internal::Platform::FileCloser fileCloser( blobBytes, blobSize, "rb" );
87       FILE * const fp = fileCloser.GetFile();
88       if ( NULL != fp )
89       {
90         Integration::BitmapPtr bitmap;
91         bool result = TizenPlatform::ImageLoader::ConvertStreamToBitmap(
92           resourceType,
93           url,
94           fp,
95           bitmap );
96
97         if ( result && bitmap )
98         {
99           return Dali::PixelData::New( bitmap->GetBufferOwnership(),
100                                        bitmap->GetBufferSize(),
101                                        bitmap->GetImageWidth(),
102                                        bitmap->GetImageHeight(),
103                                        bitmap->GetPixelFormat(),
104                                        Dali::PixelData::FREE );
105         }
106         else
107         {
108           DALI_LOG_WARNING( "Unable to decode bitmap supplied as in-memory blob.\n" );
109         }
110       }
111     }
112
113   }
114   return Dali::PixelData();
115 }
116
117
118 } // namespace Dali