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