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