[4.0] Exposing Exif Image metadata
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / tizen / tizen-platform-abstraction.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
18 // CLASS HEADER
19 #include "tizen-platform-abstraction.h"
20
21 // EXTERNAL INCLUDES
22 #include <dirent.h>
23 #include <fstream>
24 #include <dali/integration-api/debug.h>
25 #include <dali/integration-api/bitmap.h>
26 #include <dali/integration-api/resource-types.h>
27
28 // INTERNAL INCLUDES
29 #include "image-loaders/image-loader.h"
30 #include "portable/file-reader.h"
31 #include <adaptors/common/pixel-buffer-impl.h>
32
33 namespace Dali
34 {
35
36 namespace TizenPlatform
37 {
38
39 TizenPlatformAbstraction::TizenPlatformAbstraction()
40 : mDataStoragePath( "" )
41 {
42 }
43
44 TizenPlatformAbstraction::~TizenPlatformAbstraction()
45 {
46 }
47
48 ImageDimensions TizenPlatformAbstraction::GetClosestImageSize( const std::string& filename,
49                                                                ImageDimensions size,
50                                                                FittingMode::Type fittingMode,
51                                                                SamplingMode::Type samplingMode,
52                                                                bool orientationCorrection )
53 {
54   return ImageLoader::GetClosestImageSize( filename, size, fittingMode, samplingMode, orientationCorrection );
55 }
56
57 ImageDimensions TizenPlatformAbstraction::GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
58                                                                ImageDimensions size,
59                                                                FittingMode::Type fittingMode,
60                                                                SamplingMode::Type samplingMode,
61                                                                bool orientationCorrection )
62 {
63   return ImageLoader::GetClosestImageSize( resourceBuffer, size, fittingMode, samplingMode, orientationCorrection );
64 }
65
66 Integration::ResourcePointer TizenPlatformAbstraction::LoadImageSynchronously(const Integration::BitmapResourceType& resource, const std::string& resourcePath)
67 {
68   return ImageLoader::LoadImageSynchronously( resource, resourcePath );
69 }
70
71 Integration::BitmapPtr TizenPlatformAbstraction::DecodeBuffer( const Integration::BitmapResourceType& resource, uint8_t * buffer, size_t size )
72 {
73   Integration::BitmapPtr resultBitmap;
74   Dali::Devel::PixelBuffer bitmap;
75
76   Dali::Internal::Platform::FileReader fileReader( buffer, size );
77   FILE * const fp = fileReader.GetFile();
78   if( fp )
79   {
80     bool result = ImageLoader::ConvertStreamToBitmap( resource, "", fp, bitmap );
81     if ( !result || !bitmap )
82     {
83       bitmap.Reset();
84       DALI_LOG_WARNING( "Unable to decode bitmap supplied as in-memory blob.\n" );
85     }
86     else
87     {
88       Integration::Bitmap::Profile profile{Integration::Bitmap::Profile::BITMAP_2D_PACKED_PIXELS};
89
90       // For backward compatibility the Bitmap must be created
91       auto retval = Integration::Bitmap::New(profile, Dali::ResourcePolicy::OWNED_DISCARD);
92
93       retval->GetPackedPixelsProfile()->ReserveBuffer(
94               bitmap.GetPixelFormat(),
95               bitmap.GetWidth(),
96               bitmap.GetHeight(),
97               bitmap.GetWidth(),
98               bitmap.GetHeight()
99             );
100
101       auto& impl = Dali::GetImplementation(bitmap);
102
103       std::copy( impl.GetBuffer(), impl.GetBuffer()+impl.GetBufferSize(), retval->GetBuffer());
104       resultBitmap.Reset(retval);
105     }
106   }
107
108   return resultBitmap;
109 }
110
111 bool TizenPlatformAbstraction::LoadShaderBinaryFile( const std::string& filename, Dali::Vector< unsigned char >& buffer ) const
112 {
113   bool result = false;
114
115 #ifdef SHADERBIN_CACHE_ENABLED
116   std::string path;
117
118   // First check the system location where shaders are stored at install time:
119   path = DALI_SHADERBIN_DIR;
120   path += filename;
121   result = LoadFile( path, buffer );
122
123   // Fallback to the cache of shaders stored after previous runtime compilations:
124   // On desktop this looks in the current working directory that the app was launched from.
125   if( mResourceLoader && result == false )
126   {
127     path = mDataStoragePath;
128     path += filename;
129     result = LoadFile( path, buffer );
130   }
131 #endif
132
133   return result;
134 }
135
136 bool TizenPlatformAbstraction::SaveShaderBinaryFile( const std::string& filename, const unsigned char * buffer, unsigned int numBytes ) const
137 {
138   bool result = false;
139
140 #ifdef SHADERBIN_CACHE_ENABLED
141
142   // Use the cache of shaders stored after previous runtime compilations:
143   // On desktop this looks in the current working directory that the app was launched from.
144   std::string path = mDataStoragePath;
145   path += filename;
146   result = SaveFile( path, buffer, numBytes );
147
148 #endif
149
150   return result;
151 }
152
153 void TizenPlatformAbstraction::SetDataStoragePath( const std::string& path )
154 {
155   mDataStoragePath = path;
156 }
157
158 TizenPlatformAbstraction* CreatePlatformAbstraction()
159 {
160   return new TizenPlatformAbstraction();
161 }
162
163 bool SaveFile( const std::string& filename, const unsigned char * buffer, unsigned int numBytes )
164 {
165   DALI_ASSERT_DEBUG( 0 != filename.length());
166
167   bool result = false;
168
169   std::filebuf buf;
170   buf.open(filename.c_str(), std::ios::out | std::ios_base::trunc | std::ios::binary);
171   if( buf.is_open() )
172   {
173     std::ostream stream(&buf);
174
175     // determine size of buffer
176     int length = static_cast<int>(numBytes);
177
178     // write contents of buffer to the file
179     stream.write(reinterpret_cast<const char*>(buffer), length);
180
181     if( !stream.bad() )
182     {
183       result = true;
184     }
185   }
186
187   return result;
188 }
189
190 }  // namespace TizenPlatform
191
192 }  // namespace Dali