[4.0] (VectorAnimationRenderer) Add SetSize method
[platform/core/uifw/dali-adaptor.git] / adaptors / devel-api / adaptor-framework / image-loading.cpp
1 /*
2  * Copyright (c) 2017 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 <dali/public-api/object/property-map.h>
22 #include "image-loaders/image-loader.h"
23 #include <resource-loader/network/file-download.h>
24 #include <platform-abstractions/portable/file-reader.h>
25 #include "pixel-buffer-impl.h"
26
27
28
29 namespace Dali
30 {
31
32 namespace
33 {
34
35 // limit maximum image down load size to 50 MB
36 const size_t MAXIMUM_DOWNLOAD_IMAGE_SIZE  = 50 * 1024 * 1024 ;
37
38 static unsigned int gMaxTextureSize = 4096;
39
40 }
41
42 Devel::PixelBuffer LoadImageFromFile( const std::string& url, ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection )
43 {
44   Integration::BitmapResourceType resourceType( size, fittingMode, samplingMode, orientationCorrection );
45
46   Internal::Platform::FileReader fileReader( url );
47   FILE * const fp = fileReader.GetFile();
48   if( fp != NULL )
49   {
50     Dali::Devel::PixelBuffer bitmap;
51     bool success = TizenPlatform::ImageLoader::ConvertStreamToBitmap( resourceType, url, fp, bitmap );
52     if( success && bitmap )
53     {
54       return bitmap;
55     }
56   }
57   return Dali::Devel::PixelBuffer();
58 }
59
60 ImageDimensions GetClosestImageSize( const std::string& filename,
61                                      ImageDimensions size,
62                                      FittingMode::Type fittingMode,
63                                      SamplingMode::Type samplingMode,
64                                      bool orientationCorrection )
65 {
66   ImageDimensions dimension = TizenPlatform::ImageLoader::GetClosestImageSize( filename, size, fittingMode, samplingMode, orientationCorrection );
67
68   dimension.SetWidth( std::min( dimension.GetWidth(), static_cast< uint16_t >( GetMaxTextureSize() ) ) );
69   dimension.SetHeight( std::min( dimension.GetHeight(), static_cast< uint16_t >( GetMaxTextureSize() ) ) );
70
71   return dimension;
72 }
73
74
75 Devel::PixelBuffer DownloadImageSynchronously( const std::string& url, ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection )
76 {
77   Integration::BitmapResourceType resourceType( size, fittingMode, samplingMode, orientationCorrection );
78
79   bool succeeded;
80   Dali::Vector<uint8_t> dataBuffer;
81   size_t dataSize;
82
83   succeeded = TizenPlatform::Network::DownloadRemoteFileIntoMemory( url, dataBuffer, dataSize,
84                                                                     MAXIMUM_DOWNLOAD_IMAGE_SIZE );
85   if( succeeded )
86   {
87     size_t blobSize = dataBuffer.Size();
88
89     DALI_ASSERT_DEBUG( blobSize > 0U );
90
91     if( blobSize > 0U )
92     {
93       // Open a file handle on the memory buffer:
94       Dali::Internal::Platform::FileReader fileReader( dataBuffer, blobSize );
95       FILE * const fp = fileReader.GetFile();
96       if ( NULL != fp )
97       {
98         Dali::Devel::PixelBuffer bitmap;
99         bool result = TizenPlatform::ImageLoader::ConvertStreamToBitmap(
100           resourceType,
101           url,
102           fp,
103           bitmap );
104
105         if ( result && bitmap )
106         {
107           return bitmap;
108         }
109         else
110         {
111           DALI_LOG_WARNING( "Unable to decode bitmap supplied as in-memory blob.\n" );
112         }
113       }
114     }
115   }
116   return Dali::Devel::PixelBuffer();
117 }
118
119 void SetMaxTextureSize( unsigned int size )
120 {
121   gMaxTextureSize = size;
122 }
123
124 unsigned int GetMaxTextureSize()
125 {
126   return gMaxTextureSize;
127 }
128
129 } // namespace Dali