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 <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>
34 // Pieces needed to save compressed images (temporary location while plumbing):
39 * Simple function to tell intended image file format from filename
41 FileFormat GetFormatFromFileName( const std::string& filename )
43 if (filename.length() < 5)
45 DALI_LOG_WARNING("Invalid (short) filename.\n");
47 FileFormat format(INVALID_FORMAT);
49 const std::size_t filenameSize = filename.length();
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" ) )
57 else if( !filename.compare( filenameSize - 4, 4, ".png" )
58 || !filename.compare( filenameSize - 4, 4, ".PNG" ) )
62 else if( !filename.compare( filenameSize - 4, 4, ".bmp" )
63 || !filename.compare( filenameSize - 4, 4, ".BMP" ) )
67 else if( !filename.compare( filenameSize - 4, 4, ".gif" )
68 || !filename.compare( filenameSize - 4, 4, ".GIF" ) )
72 else if( !filename.compare( filenameSize - 4, 4, ".ico" )
73 || !filename.compare( filenameSize - 4, 4, ".ICO" ) )
77 else if(filenameSize >= 5){
78 if( !filename.compare( filenameSize - 5, 5, ".jpeg" )
79 || !filename.compare( filenameSize - 5, 5, ".JPEG" ) )
89 bool EncodeToFormat( const unsigned char* pixelBuffer,
90 Vector< unsigned char >& encodedPixels,
91 FileFormat formatEncoding,
94 Pixel::Format pixelFormat )
96 switch( formatEncoding )
100 return TizenPlatform::EncodeToJpeg( pixelBuffer, encodedPixels, width, height, pixelFormat );
105 return TizenPlatform::EncodeToPng( pixelBuffer, encodedPixels, width, height, pixelFormat );
110 DALI_LOG_ERROR("Format not supported for image encoding (supported formats are PNG and JPEG)\n");
116 } // anonymous namespace
119 bool EncodeToFile(const unsigned char* const pixelBuffer,
120 const std::string& filename,
121 const Pixel::Format pixelFormat,
122 const std::size_t width,
123 const std::size_t height )
125 DALI_ASSERT_DEBUG(pixelBuffer != 0 && filename.size() > 4 && width > 0 && height > 0);
126 Vector< unsigned char > pixbufEncoded;
127 const FileFormat format = GetFormatFromFileName( filename );
128 const bool encodeResult = EncodeToFormat( pixelBuffer, pixbufEncoded, format, width, height, pixelFormat );
131 DALI_LOG_ERROR("Encoding pixels failed\n");
134 return TizenPlatform::SaveFile( filename, pixbufEncoded.Begin(), pixbufEncoded.Count() );