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