2 * Copyright (c) 2014 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 "bitmap-saver.h"
21 #include <dali/integration-api/debug.h>
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>
32 // Pieces needed to save compressed images (temporary location while plumbing):
37 * Simple function to tell intended image file format from filename
39 FileFormat GetFormatFromFileName( const std::string& filename )
41 if (filename.length() < 5)
43 DALI_LOG_WARNING("Invalid (short) filename.");
45 FileFormat format(INVALID_FORMAT);
47 const std::size_t filenameSize = filename.length();
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" ) )
55 else if( !filename.compare( filenameSize - 4, 4, ".png" )
56 || !filename.compare( filenameSize - 4, 4, ".PNG" ) )
60 else if( !filename.compare( filenameSize - 4, 4, ".bmp" )
61 || !filename.compare( filenameSize - 4, 4, ".BMP" ) )
65 else if( !filename.compare( filenameSize - 4, 4, ".gif" )
66 || !filename.compare( filenameSize - 4, 4, ".GIF" ) )
70 else if( !filename.compare( filenameSize - 4, 4, ".ico" )
71 || !filename.compare( filenameSize - 4, 4, ".ICO" ) )
75 else if(filenameSize >= 5){
76 if( !filename.compare( filenameSize - 5, 5, ".jpeg" )
77 || !filename.compare( filenameSize - 5, 5, ".JPEG" ) )
87 bool EncodeToFormat( const unsigned char* pixelBuffer,
88 std::vector< unsigned char >& encodedPixels,
89 FileFormat formatEncoding,
92 Pixel::Format pixelFormat )
94 switch( formatEncoding )
98 return TizenPlatform::EncodeToJpeg( pixelBuffer, encodedPixels, width, height, pixelFormat );
103 return TizenPlatform::EncodeToPng( pixelBuffer, encodedPixels, width, height, pixelFormat );
108 DALI_LOG_ERROR("Format not supported for image encoding (supported formats are PNG and JPEG)");
114 } // anonymous namespace
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 )
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 );
129 DALI_LOG_ERROR("Encoding pixels failed");
132 return TizenPlatform::ResourceLoader::SaveFile( filename, pixbufEncoded );