[Tizen] Backport tizen_7.5 for Scene3D
[platform/core/uifw/dali-adaptor.git] / dali / devel-api / adaptor-framework / image-loading.cpp
1 /*
2  * Copyright (c) 2022 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/devel-api/adaptor-framework/pixel-buffer.h>
22 #include <dali/internal/imaging/common/file-download.h>
23 #include <dali/internal/imaging/common/image-loader.h>
24 #include <dali/internal/system/common/file-reader.h>
25 #include <dali/public-api/object/property-map.h>
26
27 namespace Dali
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 } // namespace
35
36 Devel::PixelBuffer LoadImageFromFile(const std::string& url, ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection)
37 {
38   Integration::BitmapResourceType resourceType(size, fittingMode, samplingMode, orientationCorrection);
39
40   Internal::Platform::FileReader fileReader(url);
41   FILE* const                    fp = fileReader.GetFile();
42   if(fp != NULL)
43   {
44     Dali::Devel::PixelBuffer bitmap;
45     bool                     success = TizenPlatform::ImageLoader::ConvertStreamToBitmap(resourceType, url, fp, bitmap);
46     if(success && bitmap)
47     {
48       return bitmap;
49     }
50   }
51   return Dali::Devel::PixelBuffer();
52 }
53
54 void LoadImagePlanesFromFile(const std::string& url, std::vector<Devel::PixelBuffer>& buffers, ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection)
55 {
56   Integration::BitmapResourceType resourceType(size, fittingMode, samplingMode, orientationCorrection);
57
58   Internal::Platform::FileReader fileReader(url);
59   FILE* const                    fp = fileReader.GetFile();
60   if(fp != NULL)
61   {
62     TizenPlatform::ImageLoader::ConvertStreamToPlanes(resourceType, url, fp, buffers);
63   }
64 }
65
66 Devel::PixelBuffer LoadImageFromBuffer(const Dali::Vector<uint8_t>& buffer, ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection)
67 {
68   if(buffer.Empty())
69   {
70     DALI_LOG_ERROR("buffer is empty!\n");
71     return Dali::Devel::PixelBuffer();
72   }
73   Integration::BitmapResourceType resourceType(size, fittingMode, samplingMode, orientationCorrection);
74
75   Internal::Platform::FileReader fileReader(buffer);
76   FILE* const                    fp = fileReader.GetFile();
77   if(fp != NULL)
78   {
79     Dali::Devel::PixelBuffer bitmap;
80     // Make path as empty string. Path information just for file format hint.
81     bool success = TizenPlatform::ImageLoader::ConvertStreamToBitmap(resourceType, std::string(""), fp, bitmap);
82     if(success && bitmap)
83     {
84       return bitmap;
85     }
86   }
87   return Dali::Devel::PixelBuffer();
88 }
89
90 Devel::PixelBuffer LoadImageFromBuffer(uint8_t* buffer, size_t bufferSize, ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection)
91 {
92   if(buffer == nullptr)
93   {
94     DALI_LOG_ERROR("buffer is empty!\n");
95     return Dali::Devel::PixelBuffer();
96   }
97   Integration::BitmapResourceType resourceType(size, fittingMode, samplingMode, orientationCorrection);
98
99   Internal::Platform::FileReader fileReader(buffer, bufferSize);
100   FILE* const                    fp = fileReader.GetFile();
101   if(fp != NULL)
102   {
103     Dali::Devel::PixelBuffer bitmap;
104     // Make path as empty string. Path information just for file format hint.
105     bool success = TizenPlatform::ImageLoader::ConvertStreamToBitmap(resourceType, std::string(""), fp, bitmap);
106     if(success && bitmap)
107     {
108       return bitmap;
109     }
110   }
111   return Dali::Devel::PixelBuffer();
112 }
113
114 ImageDimensions GetClosestImageSize(const std::string& filename,
115                                     ImageDimensions    size,
116                                     FittingMode::Type  fittingMode,
117                                     SamplingMode::Type samplingMode,
118                                     bool               orientationCorrection)
119 {
120   ImageDimensions dimension = TizenPlatform::ImageLoader::GetClosestImageSize(filename, size, fittingMode, samplingMode, orientationCorrection);
121
122   dimension.SetWidth(std::min(dimension.GetWidth(), static_cast<uint16_t>(GetMaxTextureSize())));
123   dimension.SetHeight(std::min(dimension.GetHeight(), static_cast<uint16_t>(GetMaxTextureSize())));
124
125   return dimension;
126 }
127
128 ImageDimensions GetOriginalImageSize(const std::string& filename, bool orientationCorrection)
129 {
130   return TizenPlatform::ImageLoader::GetClosestImageSize(filename, ImageDimensions(0, 0), FittingMode::DEFAULT, SamplingMode::BOX_THEN_LINEAR, orientationCorrection);
131 }
132
133 Devel::PixelBuffer DownloadImageSynchronously(const std::string& url, ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection)
134 {
135   Integration::BitmapResourceType resourceType(size, fittingMode, samplingMode, orientationCorrection);
136
137   bool                  succeeded;
138   Dali::Vector<uint8_t> dataBuffer;
139   size_t                dataSize;
140
141   succeeded = TizenPlatform::Network::DownloadRemoteFileIntoMemory(url, dataBuffer, dataSize, MAXIMUM_DOWNLOAD_IMAGE_SIZE);
142   if(succeeded)
143   {
144     size_t blobSize = dataBuffer.Size();
145
146     DALI_ASSERT_DEBUG(blobSize > 0U);
147
148     if(blobSize > 0U)
149     {
150       // Open a file handle on the memory buffer:
151       Dali::Internal::Platform::FileReader fileReader(dataBuffer, blobSize);
152       FILE* const                          fp = fileReader.GetFile();
153       if(NULL != fp)
154       {
155         Dali::Devel::PixelBuffer bitmap;
156         bool                     result = TizenPlatform::ImageLoader::ConvertStreamToBitmap(
157           resourceType,
158           url,
159           fp,
160           bitmap);
161
162         if(result && bitmap)
163         {
164           return bitmap;
165         }
166         else
167         {
168           DALI_LOG_WARNING("Unable to decode bitmap supplied as in-memory blob.\n");
169         }
170       }
171     }
172   }
173   return Dali::Devel::PixelBuffer();
174 }
175
176 unsigned int GetMaxTextureSize()
177 {
178   return TizenPlatform::ImageLoader::GetMaxTextureSize();
179 }
180
181 } // namespace Dali