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