Revert "Revert "Revert "[4.0] Exposing Exif Image metadata"""
[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 "image-loaders/image-loader.h"
22 #include <resource-loader/network/file-download.h>
23 #include <platform-abstractions/portable/file-reader.h>
24 #include "pixel-buffer-impl.h"
25
26 namespace Dali
27 {
28
29 namespace
30 {
31
32 // limit maximum image down load size to 50 MB
33 const size_t MAXIMUM_DOWNLOAD_IMAGE_SIZE  = 50 * 1024 * 1024 ;
34
35 static unsigned int gMaxTextureSize = 4096;
36
37 }
38
39 Devel::PixelBuffer LoadImageFromFile( const std::string& url, ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection )
40 {
41   Integration::BitmapResourceType resourceType( size, fittingMode, samplingMode, orientationCorrection );
42
43   Internal::Platform::FileReader fileReader( url );
44   FILE * const fp = fileReader.GetFile();
45   if( fp != NULL )
46   {
47     Integration::BitmapPtr bitmap;
48     bool success = TizenPlatform::ImageLoader::ConvertStreamToBitmap( resourceType, url, fp, bitmap );
49     if( success && bitmap )
50     {
51       // Use bitmap->GetBufferOwnership() to transfer the buffer ownership
52       // to pixelData.  The destroy of bitmap will not release the buffer,
53       // instead, the pixelBuffer is responsible for releasing when its
54       // reference count falls to zero.
55       Internal::Adaptor::PixelBufferPtr pixelBufferImpl =
56         Internal::Adaptor::PixelBuffer::New( bitmap->GetBufferOwnership(),
57                                              bitmap->GetBufferSize(),
58                                              bitmap->GetImageWidth(),
59                                              bitmap->GetImageHeight(),
60                                              bitmap->GetPixelFormat() );
61
62       Dali::Devel::PixelBuffer pixelBuffer( pixelBufferImpl.Get() );
63       return pixelBuffer;
64     }
65   }
66   return Dali::Devel::PixelBuffer();
67 }
68
69 ImageDimensions GetClosestImageSize( const std::string& filename,
70                                      ImageDimensions size,
71                                      FittingMode::Type fittingMode,
72                                      SamplingMode::Type samplingMode,
73                                      bool orientationCorrection )
74 {
75   ImageDimensions dimension = TizenPlatform::ImageLoader::GetClosestImageSize( filename, size, fittingMode, samplingMode, orientationCorrection );
76
77   dimension.SetWidth( std::min( dimension.GetWidth(), static_cast< uint16_t >( GetMaxTextureSize() ) ) );
78   dimension.SetHeight( std::min( dimension.GetHeight(), static_cast< uint16_t >( GetMaxTextureSize() ) ) );
79
80   return dimension;
81 }
82
83
84 Devel::PixelBuffer DownloadImageSynchronously( const std::string& url, ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection )
85 {
86   Integration::BitmapResourceType resourceType( size, fittingMode, samplingMode, orientationCorrection );
87
88   bool succeeded;
89   Dali::Vector<uint8_t> dataBuffer;
90   size_t dataSize;
91
92   succeeded = TizenPlatform::Network::DownloadRemoteFileIntoMemory( url, dataBuffer, dataSize,
93                                                                     MAXIMUM_DOWNLOAD_IMAGE_SIZE );
94   if( succeeded )
95   {
96     size_t blobSize = dataBuffer.Size();
97
98     DALI_ASSERT_DEBUG( blobSize > 0U );
99
100     if( blobSize > 0U )
101     {
102       // Open a file handle on the memory buffer:
103       Dali::Internal::Platform::FileReader fileReader( dataBuffer, blobSize );
104       FILE * const fp = fileReader.GetFile();
105       if ( NULL != fp )
106       {
107         Integration::BitmapPtr bitmap;
108         bool result = TizenPlatform::ImageLoader::ConvertStreamToBitmap(
109           resourceType,
110           url,
111           fp,
112           bitmap );
113
114         if ( result && bitmap )
115         {
116           Internal::Adaptor::PixelBufferPtr pixelBufferImpl =
117             Internal::Adaptor::PixelBuffer::New( bitmap->GetBufferOwnership(),
118                                                  bitmap->GetBufferSize(),
119                                                  bitmap->GetImageWidth(),
120                                                  bitmap->GetImageHeight(),
121                                                  bitmap->GetPixelFormat() );
122
123           Dali::Devel::PixelBuffer pixelBuffer( pixelBufferImpl.Get() );
124           return pixelBuffer;
125         }
126         else
127         {
128           DALI_LOG_WARNING( "Unable to decode bitmap supplied as in-memory blob.\n" );
129         }
130       }
131     }
132   }
133   return Dali::Devel::PixelBuffer();
134 }
135
136 void SetMaxTextureSize( unsigned int size )
137 {
138   gMaxTextureSize = size;
139 }
140
141 unsigned int GetMaxTextureSize()
142 {
143   return gMaxTextureSize;
144 }
145
146 } // namespace Dali