Remove orientation from application class
[platform/core/uifw/dali-adaptor.git] / adaptors / public-api / adaptor-framework / bitmap-saver.cpp
1 /*
2  * Copyright (c) 2014 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 "bitmap-saver.h"
19
20 // EXTERNAL INCLUDES
21 #include <dali/integration-api/debug.h>
22
23 // INTERNAL INCLUDES
24 #include <platform-abstractions/tizen/resource-loader/resource-loader.h>
25 #include <platform-abstractions/tizen/image-loaders/loader-jpeg.h>
26 #include <platform-abstractions/tizen/image-loaders/loader-png.h>
27 #include <image-encoder.h>
28
29 namespace Dali
30 {
31
32 // Pieces needed to save compressed images (temporary location while plumbing):
33 namespace
34 {
35
36 /**
37  * Simple function to tell intended image file format from filename
38  */
39 FileFormat GetFormatFromFileName( const std::string& filename )
40 {
41   if (filename.length() < 5)
42   {
43     DALI_LOG_WARNING("Invalid (short) filename.");
44   }
45   FileFormat format(INVALID_FORMAT);
46
47   const std::size_t filenameSize = filename.length();
48
49   if(filenameSize >= 4){ // Avoid throwing out_of_range or failing silently if exceptions are turned-off on the compare(). (http://www.cplusplus.com/reference/string/string/compare/)
50     if( !filename.compare( filenameSize - 4, 4, ".jpg" )
51         || !filename.compare( filenameSize - 4, 4, ".JPG" ) )
52     {
53       format = JPG_FORMAT;
54     }
55     else if( !filename.compare( filenameSize - 4, 4, ".png" )
56              || !filename.compare( filenameSize - 4, 4, ".PNG" ) )
57     {
58       format = PNG_FORMAT;
59     }
60     else if( !filename.compare( filenameSize - 4, 4, ".bmp" )
61              || !filename.compare( filenameSize - 4, 4, ".BMP" ) )
62     {
63       format = BMP_FORMAT;
64     }
65     else if( !filename.compare( filenameSize - 4, 4, ".gif" )
66              || !filename.compare( filenameSize - 4, 4, ".GIF" ) )
67     {
68       format = GIF_FORMAT;
69     }
70     else if( !filename.compare( filenameSize - 4, 4, ".ico" )
71              || !filename.compare( filenameSize - 4, 4, ".ICO" ) )
72     {
73       format = ICO_FORMAT;
74     }
75     else if(filenameSize >= 5){
76       if( !filename.compare( filenameSize - 5, 5, ".jpeg" )
77           || !filename.compare( filenameSize - 5, 5, ".JPEG" ) )
78       {
79         format = JPG_FORMAT;
80       }
81     }
82   }
83
84   return format;
85 }
86
87 bool EncodeToFormat( const unsigned char* pixelBuffer,
88                      std::vector< unsigned char >& encodedPixels,
89                      FileFormat formatEncoding,
90                      std::size_t width,
91                      std::size_t height,
92                      Pixel::Format pixelFormat )
93 {
94   switch( formatEncoding )
95   {
96     case JPG_FORMAT:
97     {
98       return TizenPlatform::EncodeToJpeg( pixelBuffer, encodedPixels, width, height, pixelFormat );
99       break;
100     }
101     case PNG_FORMAT:
102     {
103       return TizenPlatform::EncodeToPng( pixelBuffer, encodedPixels, width, height, pixelFormat );
104       break;
105     }
106     default:
107     {
108       DALI_LOG_ERROR("Format not supported for image encoding (supported formats are PNG and JPEG)");
109       break;
110     }
111   }
112   return false;
113 }
114 } // anonymous namespace
115
116
117 bool EncodeToFile(const unsigned char* const pixelBuffer,
118                   const std::string& filename,
119                   const Pixel::Format pixelFormat,
120                   const std::size_t width,
121                   const std::size_t height )
122 {
123   DALI_ASSERT_DEBUG(pixelBuffer != 0 && filename.size() > 4 && width > 0 && height > 0);
124   std::vector< unsigned char > pixbufEncoded;
125   const FileFormat format = GetFormatFromFileName( filename );
126   const bool encodeResult = EncodeToFormat( pixelBuffer, pixbufEncoded, format, width, height, pixelFormat );
127   if(!encodeResult)
128   {
129     DALI_LOG_ERROR("Encoding pixels failed");
130     return false;
131   }
132   return TizenPlatform::ResourceLoader::SaveFile( filename, pixbufEncoded );
133 }
134
135 } // namespace Dali