2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali/devel-api/adaptor-framework/bitmap-saver.h>
21 #include <dali/integration-api/debug.h>
22 #include <dali/public-api/common/dali-vector.h>
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>
32 // Pieces needed to save compressed images (temporary location while plumbing):
36 * Simple function to tell intended image file format from filename
38 FileFormat GetFormatFromFileName(const std::string& filename)
40 if(filename.length() < 5)
42 DALI_LOG_WARNING("Invalid (short) filename.\n");
44 FileFormat format(INVALID_FORMAT);
46 const std::size_t filenameSize = filename.length();
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"))
54 else if(!filename.compare(filenameSize - 4, 4, ".png") || !filename.compare(filenameSize - 4, 4, ".PNG"))
58 else if(!filename.compare(filenameSize - 4, 4, ".bmp") || !filename.compare(filenameSize - 4, 4, ".BMP"))
62 else if(!filename.compare(filenameSize - 4, 4, ".gif") || !filename.compare(filenameSize - 4, 4, ".GIF"))
66 else if(!filename.compare(filenameSize - 4, 4, ".ico") || !filename.compare(filenameSize - 4, 4, ".ICO"))
70 else if(filenameSize >= 5)
72 if(!filename.compare(filenameSize - 5, 5, ".jpeg") || !filename.compare(filenameSize - 5, 5, ".JPEG"))
82 bool EncodeToFormat(const unsigned char* pixelBuffer,
83 Vector<unsigned char>& encodedPixels,
84 FileFormat formatEncoding,
87 Pixel::Format pixelFormat,
88 const uint32_t quality)
90 switch(formatEncoding)
94 return TizenPlatform::EncodeToJpeg(pixelBuffer, encodedPixels, width, height, pixelFormat, quality);
99 return TizenPlatform::EncodeToPng(pixelBuffer, encodedPixels, width, height, pixelFormat);
104 DALI_LOG_ERROR("Format not supported for image encoding (supported formats are PNG and JPEG)\n");
110 } // anonymous namespace
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)
118 return EncodeToFile(pixelBuffer, filename, pixelFormat, width, height, DEFAULT_JPG_QUALITY);
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)
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);
134 DALI_LOG_ERROR("Encoding pixels failed\n");
137 return TizenPlatform::SaveFile(filename, pixbufEncoded.Begin(), pixbufEncoded.Count());