2 * Copyright (c) 2017 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.
17 #include <dali/internal/imaging/common/image-loader.h>
19 #include <dali/devel-api/common/ref-counted-dali-vector.h>
20 #include <dali/internal/imaging/common/pixel-buffer-impl.h>
22 #include <dali/internal/imaging/common/loader-astc.h>
23 #include <dali/internal/imaging/common/loader-bmp.h>
24 #include <dali/internal/imaging/common/loader-gif.h>
25 #include <dali/internal/imaging/common/loader-ico.h>
26 #include <dali/internal/imaging/common/loader-jpeg.h>
27 #include <dali/internal/imaging/common/loader-ktx.h>
28 #include <dali/internal/imaging/common/loader-png.h>
29 #include <dali/internal/imaging/common/loader-wbmp.h>
30 #include <dali/internal/imaging/common/image-operations.h>
31 #include <dali/devel-api/adaptor-framework/image-loader-input.h>
32 #include <dali/internal/imaging/common/image-loader-plugin-proxy.h>
33 #include <dali/internal/system/common/file-reader.h>
35 using namespace Dali::Integration;
39 namespace TizenPlatform
44 #if defined(DEBUG_ENABLED)
45 Integration::Log::Filter* gLogFilter = Debug::Filter::New( Debug::Concise, false, "LOG_IMAGE_LOADING" );
48 static unsigned int gMaxTextureSize = 4096;
50 static bool gMaxTextureSizeUpdated = false;
53 * Enum for file formats, has to be in sync with BITMAP_LOADER_LOOKUP_TABLE
57 // Unknown file format
60 // formats that use magic bytes
68 FORMAT_MAGIC_BYTE_COUNT,
70 // formats after this one do not use magic bytes
71 FORMAT_WBMP = FORMAT_MAGIC_BYTE_COUNT,
76 * A lookup table containing all the bitmap loaders with the appropriate information.
77 * Has to be in sync with enum FileFormats
79 const Dali::ImageLoader::BitmapLoader BITMAP_LOADER_LOOKUP_TABLE[FORMAT_TOTAL_COUNT] =
81 { Png::MAGIC_BYTE_1, Png::MAGIC_BYTE_2, LoadBitmapFromPng, LoadPngHeader, Bitmap::BITMAP_2D_PACKED_PIXELS },
82 { Jpeg::MAGIC_BYTE_1, Jpeg::MAGIC_BYTE_2, LoadBitmapFromJpeg, LoadJpegHeader, Bitmap::BITMAP_2D_PACKED_PIXELS },
83 { Bmp::MAGIC_BYTE_1, Bmp::MAGIC_BYTE_2, LoadBitmapFromBmp, LoadBmpHeader, Bitmap::BITMAP_2D_PACKED_PIXELS },
84 { Gif::MAGIC_BYTE_1, Gif::MAGIC_BYTE_2, LoadBitmapFromGif, LoadGifHeader, Bitmap::BITMAP_2D_PACKED_PIXELS },
85 { Ktx::MAGIC_BYTE_1, Ktx::MAGIC_BYTE_2, LoadBitmapFromKtx, LoadKtxHeader, Bitmap::BITMAP_COMPRESSED },
86 { Astc::MAGIC_BYTE_1, Astc::MAGIC_BYTE_2, LoadBitmapFromAstc, LoadAstcHeader, Bitmap::BITMAP_COMPRESSED },
87 { Ico::MAGIC_BYTE_1, Ico::MAGIC_BYTE_2, LoadBitmapFromIco, LoadIcoHeader, Bitmap::BITMAP_2D_PACKED_PIXELS },
88 { 0x0, 0x0, LoadBitmapFromWbmp, LoadWbmpHeader, Bitmap::BITMAP_2D_PACKED_PIXELS },
91 const unsigned int MAGIC_LENGTH = 2;
94 * This code tries to predict the file format from the filename to help with format picking.
96 struct FormatExtension
98 const std::string extension;
102 const FormatExtension FORMAT_EXTENSIONS[] =
104 { ".png", FORMAT_PNG },
105 { ".jpg", FORMAT_JPEG },
106 { ".bmp", FORMAT_BMP },
107 { ".gif", FORMAT_GIF },
108 { ".ktx", FORMAT_KTX },
109 { ".astc", FORMAT_ASTC },
110 { ".ico", FORMAT_ICO },
111 { ".wbmp", FORMAT_WBMP }
114 const unsigned int FORMAT_EXTENSIONS_COUNT = sizeof(FORMAT_EXTENSIONS) / sizeof(FormatExtension);
116 FileFormats GetFormatHint( const std::string& filename )
118 FileFormats format = FORMAT_UNKNOWN;
120 for ( unsigned int i = 0; i < FORMAT_EXTENSIONS_COUNT; ++i )
122 unsigned int length = FORMAT_EXTENSIONS[i].extension.size();
123 if ( ( filename.size() > length ) &&
124 ( 0 == filename.compare( filename.size() - length, length, FORMAT_EXTENSIONS[i].extension ) ) )
126 format = FORMAT_EXTENSIONS[i].format;
135 * Checks the magic bytes of the file first to determine which Image decoder to use to decode the
137 * @param[in] fp The file to decode
138 * @param[in] format Hint about what format to try first
139 * @param[out] loader Set with the function to use to decode the image
140 * @param[out] header Set with the function to use to decode the header
141 * @param[out] profile The kind of bitmap to hold the bits loaded for the bitmap.
142 * @return true, if we can decode the image, false otherwise
144 bool GetBitmapLoaderFunctions( FILE *fp,
146 Dali::ImageLoader::LoadBitmapFunction& loader,
147 Dali::ImageLoader::LoadBitmapHeaderFunction& header,
148 Bitmap::Profile& profile,
149 const std::string& filename )
151 unsigned char magic[MAGIC_LENGTH];
152 size_t read = fread(magic, sizeof(unsigned char), MAGIC_LENGTH, fp);
154 // Reset to the start of the file.
155 if( fseek(fp, 0, SEEK_SET) )
157 DALI_LOG_ERROR("Error seeking to start of file\n");
160 if (read != MAGIC_LENGTH)
165 bool loaderFound = false;
166 const Dali::ImageLoader::BitmapLoader *lookupPtr = BITMAP_LOADER_LOOKUP_TABLE;
167 Dali::ImageLoader::Input defaultInput( fp );
169 // try plugin image loader
170 const Dali::ImageLoader::BitmapLoader* data = Internal::Adaptor::ImageLoaderPluginProxy::BitmapLoaderLookup( filename );
174 unsigned int width = 0;
175 unsigned int height = 0;
176 loaderFound = lookupPtr->header( fp, width, height );
180 if ( false == loaderFound && format != FORMAT_UNKNOWN )
182 lookupPtr = BITMAP_LOADER_LOOKUP_TABLE + format;
183 if ( format >= FORMAT_MAGIC_BYTE_COUNT ||
184 ( lookupPtr->magicByte1 == magic[0] && lookupPtr->magicByte2 == magic[1] ) )
186 unsigned int width = 0;
187 unsigned int height = 0;
188 loaderFound = lookupPtr->header( fp, width, height );
192 // then try to get a match with formats that have magic bytes
193 if ( false == loaderFound )
195 for ( lookupPtr = BITMAP_LOADER_LOOKUP_TABLE;
196 lookupPtr < BITMAP_LOADER_LOOKUP_TABLE + FORMAT_MAGIC_BYTE_COUNT;
199 if ( lookupPtr->magicByte1 == magic[0] && lookupPtr->magicByte2 == magic[1] )
201 // to seperate ico file format and wbmp file format
202 unsigned int width = 0;
203 unsigned int height = 0;
204 loaderFound = lookupPtr->header(fp, width, height);
213 // finally try formats that do not use magic bytes
214 if ( false == loaderFound )
216 for ( lookupPtr = BITMAP_LOADER_LOOKUP_TABLE + FORMAT_MAGIC_BYTE_COUNT;
217 lookupPtr < BITMAP_LOADER_LOOKUP_TABLE + FORMAT_TOTAL_COUNT;
220 // to seperate ico file format and wbmp file format
221 unsigned int width = 0;
222 unsigned int height = 0;
223 loaderFound = lookupPtr->header(fp, width, height);
231 // if a loader was found set the outputs
234 loader = lookupPtr->loader;
235 header = lookupPtr->header;
236 profile = lookupPtr->profile;
239 // Reset to the start of the file.
240 if( fseek(fp, 0, SEEK_SET) )
242 DALI_LOG_ERROR("Error seeking to start of file\n");
248 } // anonymous namespace
251 namespace ImageLoader
254 bool ConvertStreamToBitmap( const BitmapResourceType& resource, std::string path, FILE * const fp, Dali::Devel::PixelBuffer& pixelBuffer )
256 DALI_LOG_TRACE_METHOD( gLogFilter );
262 Dali::ImageLoader::LoadBitmapFunction function;
263 Dali::ImageLoader::LoadBitmapHeaderFunction header;
265 Bitmap::Profile profile;
267 if ( GetBitmapLoaderFunctions( fp,
268 GetFormatHint( path ),
274 const Dali::ImageLoader::ScalingParameters scalingParameters( resource.size, resource.scalingMode, resource.samplingMode );
275 const Dali::ImageLoader::Input input( fp, scalingParameters, resource.orientationCorrection );
277 // Run the image type decoder:
278 result = function( input, pixelBuffer );
282 DALI_LOG_WARNING( "Unable to convert %s\n", path.c_str() );
286 pixelBuffer = Internal::Platform::ApplyAttributesToBitmap( pixelBuffer, resource.size, resource.scalingMode, resource.samplingMode );
290 DALI_LOG_WARNING( "Image Decoder for %s unavailable\n", path.c_str() );
297 ResourcePointer LoadImageSynchronously( const Integration::BitmapResourceType& resource, const std::string& path )
299 ResourcePointer result;
300 Dali::Devel::PixelBuffer bitmap;
302 Internal::Platform::FileReader fileReader( path );
303 FILE * const fp = fileReader.GetFile();
306 bool success = ConvertStreamToBitmap(resource, path, fp, bitmap);
307 if (success && bitmap)
309 Bitmap::Profile profile{Bitmap::Profile::BITMAP_2D_PACKED_PIXELS};
311 // For backward compatibility the Bitmap must be created
312 auto retval = Bitmap::New(profile, Dali::ResourcePolicy::OWNED_DISCARD);
314 DALI_LOG_SET_OBJECT_STRING( retval, path );
316 retval->GetPackedPixelsProfile()->ReserveBuffer(
317 bitmap.GetPixelFormat(),
324 auto& impl = Dali::GetImplementation(bitmap);
326 std::copy( impl.GetBuffer(), impl.GetBuffer()+impl.GetBufferSize(), retval->GetBuffer());
327 result.Reset(retval);
333 ///@ToDo: Rename GetClosestImageSize() functions. Make them use the orientation correction and scaling information. Requires jpeg loader to tell us about reorientation. [Is there still a requirement for this functionality at all?]
334 ImageDimensions GetClosestImageSize( const std::string& filename,
335 ImageDimensions size,
336 FittingMode::Type fittingMode,
337 SamplingMode::Type samplingMode,
338 bool orientationCorrection )
340 unsigned int width = 0;
341 unsigned int height = 0;
343 Internal::Platform::FileReader fileReader( filename );
344 FILE *fp = fileReader.GetFile();
347 Dali::ImageLoader::LoadBitmapFunction loaderFunction;
348 Dali::ImageLoader::LoadBitmapHeaderFunction headerFunction;
349 Bitmap::Profile profile;
351 if ( GetBitmapLoaderFunctions( fp,
352 GetFormatHint(filename),
358 const Dali::ImageLoader::Input input( fp, Dali::ImageLoader::ScalingParameters( size, fittingMode, samplingMode ), orientationCorrection );
360 const bool read_res = headerFunction( input, width, height );
363 DALI_LOG_WARNING("Image Decoder failed to read header for %s\n", filename.c_str());
368 DALI_LOG_WARNING("Image Decoder for %s unavailable\n", filename.c_str());
371 return ImageDimensions( width, height );
374 ImageDimensions GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
375 ImageDimensions size,
376 FittingMode::Type fittingMode,
377 SamplingMode::Type samplingMode,
378 bool orientationCorrection )
380 unsigned int width = 0;
381 unsigned int height = 0;
383 // Get the blob of binary data that we need to decode:
384 DALI_ASSERT_DEBUG( resourceBuffer );
385 Dali::RefCountedVector<uint8_t>* const encodedBlob = reinterpret_cast<Dali::RefCountedVector<uint8_t>*>( resourceBuffer.Get() );
387 if( encodedBlob != 0 )
389 if( encodedBlob->GetVector().Size() )
391 // Open a file handle on the memory buffer:
392 Internal::Platform::FileReader fileReader( encodedBlob->GetVector() );
393 FILE *fp = fileReader.GetFile();
396 Dali::ImageLoader::LoadBitmapFunction loaderFunction;
397 Dali::ImageLoader::LoadBitmapHeaderFunction headerFunction;
398 Bitmap::Profile profile;
400 if ( GetBitmapLoaderFunctions( fp,
407 const Dali::ImageLoader::Input input( fp, Dali::ImageLoader::ScalingParameters( size, fittingMode, samplingMode ), orientationCorrection );
408 const bool read_res = headerFunction( input, width, height );
411 DALI_LOG_WARNING( "Image Decoder failed to read header for resourceBuffer\n" );
417 return ImageDimensions( width, height );
420 void SetMaxTextureSize( unsigned int size )
422 gMaxTextureSize = size;
423 gMaxTextureSizeUpdated = true;
426 unsigned int GetMaxTextureSize()
428 return gMaxTextureSize;
431 bool MaxTextureSizeUpdated()
433 return gMaxTextureSizeUpdated;