2 * Copyright (c) 2015 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>
22 #include <dali/public-api/common/dali-vector.h>
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>
33 // Pieces needed to save compressed images (temporary location while plumbing):
38 * Simple function to tell intended image file format from filename
40 FileFormat GetFormatFromFileName( const std::string& filename )
42 if (filename.length() < 5)
44 DALI_LOG_WARNING("Invalid (short) filename.\n");
46 FileFormat format(INVALID_FORMAT);
48 const std::size_t filenameSize = filename.length();
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" ) )
56 else if( !filename.compare( filenameSize - 4, 4, ".png" )
57 || !filename.compare( filenameSize - 4, 4, ".PNG" ) )
61 else if( !filename.compare( filenameSize - 4, 4, ".bmp" )
62 || !filename.compare( filenameSize - 4, 4, ".BMP" ) )
66 else if( !filename.compare( filenameSize - 4, 4, ".gif" )
67 || !filename.compare( filenameSize - 4, 4, ".GIF" ) )
71 else if( !filename.compare( filenameSize - 4, 4, ".ico" )
72 || !filename.compare( filenameSize - 4, 4, ".ICO" ) )
76 else if(filenameSize >= 5){
77 if( !filename.compare( filenameSize - 5, 5, ".jpeg" )
78 || !filename.compare( filenameSize - 5, 5, ".JPEG" ) )
88 bool EncodeToFormat( const unsigned char* pixelBuffer,
89 Vector< unsigned char >& encodedPixels,
90 FileFormat formatEncoding,
93 Pixel::Format pixelFormat )
95 switch( formatEncoding )
99 return TizenPlatform::EncodeToJpeg( pixelBuffer, encodedPixels, width, height, pixelFormat );
104 return TizenPlatform::EncodeToPng( pixelBuffer, encodedPixels, width, height, pixelFormat );
109 DALI_LOG_ERROR("Format not supported for image encoding (supported formats are PNG and JPEG)\n");
115 } // anonymous namespace
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 )
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 );
130 DALI_LOG_ERROR("Encoding pixels failed\n");
133 return TizenPlatform::ResourceLoader::SaveFile( filename, pixbufEncoded.Begin(), pixbufEncoded.Count() );