2 * Copyright (c) 2023 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/devel-api/adaptor-framework/image-loader-input.h>
23 #include <dali/internal/imaging/common/image-loader-plugin-proxy.h>
24 #include <dali/internal/imaging/common/image-operations.h>
25 #include <dali/internal/imaging/common/loader-astc.h>
26 #include <dali/internal/imaging/common/loader-bmp.h>
27 #include <dali/internal/imaging/common/loader-gif.h>
28 #include <dali/internal/imaging/common/loader-ico.h>
29 #include <dali/internal/imaging/common/loader-jpeg.h>
30 #include <dali/internal/imaging/common/loader-ktx.h>
31 #include <dali/internal/imaging/common/loader-png.h>
32 #include <dali/internal/imaging/common/loader-wbmp.h>
33 #include <dali/internal/imaging/common/loader-webp.h>
34 #include <dali/internal/system/common/file-reader.h>
36 using namespace Dali::Integration;
40 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 // Unsupported file format that we should not process image loader.
58 FORMAT_UNSUPPORTED = -2,
60 // Unknown file format
63 // formats that use magic bytes
72 FORMAT_MAGIC_BYTE_COUNT,
74 // formats after this one do not use magic bytes
75 FORMAT_WBMP = FORMAT_MAGIC_BYTE_COUNT,
80 * A lookup table containing all the bitmap loaders with the appropriate information.
81 * Has to be in sync with enum FileFormats
84 const Dali::ImageLoader::BitmapLoader BITMAP_LOADER_LOOKUP_TABLE[FORMAT_TOTAL_COUNT] =
86 {Png::MAGIC_BYTE_1, Png::MAGIC_BYTE_2, LoadBitmapFromPng, nullptr, LoadPngHeader, Bitmap::BITMAP_2D_PACKED_PIXELS},
87 {Jpeg::MAGIC_BYTE_1, Jpeg::MAGIC_BYTE_2, LoadBitmapFromJpeg, LoadPlanesFromJpeg, LoadJpegHeader, Bitmap::BITMAP_2D_PACKED_PIXELS},
88 {Bmp::MAGIC_BYTE_1, Bmp::MAGIC_BYTE_2, LoadBitmapFromBmp, nullptr, LoadBmpHeader, Bitmap::BITMAP_2D_PACKED_PIXELS},
89 {Gif::MAGIC_BYTE_1, Gif::MAGIC_BYTE_2, LoadBitmapFromGif, nullptr, LoadGifHeader, Bitmap::BITMAP_2D_PACKED_PIXELS},
90 {Webp::MAGIC_BYTE_1, Webp::MAGIC_BYTE_2, LoadBitmapFromWebp, nullptr, LoadWebpHeader, Bitmap::BITMAP_2D_PACKED_PIXELS},
91 {Ktx::MAGIC_BYTE_1, Ktx::MAGIC_BYTE_2, LoadBitmapFromKtx, nullptr, LoadKtxHeader, Bitmap::BITMAP_COMPRESSED },
92 {Astc::MAGIC_BYTE_1, Astc::MAGIC_BYTE_2, LoadBitmapFromAstc, nullptr, LoadAstcHeader, Bitmap::BITMAP_COMPRESSED },
93 {Ico::MAGIC_BYTE_1, Ico::MAGIC_BYTE_2, LoadBitmapFromIco, nullptr, LoadIcoHeader, Bitmap::BITMAP_2D_PACKED_PIXELS},
94 {0x0, 0x0, LoadBitmapFromWbmp, nullptr, LoadWbmpHeader, Bitmap::BITMAP_2D_PACKED_PIXELS},
98 const unsigned int MAGIC_LENGTH = 2;
101 * This code tries to predict the file format from the filename to help with format picking.
103 struct FormatExtension
105 const std::string_view extension;
110 constexpr FormatExtension FORMAT_EXTENSIONS[] =
112 {".png", FORMAT_PNG },
113 {".jpg", FORMAT_JPEG},
114 {".bmp", FORMAT_BMP },
115 {".gif", FORMAT_GIF },
116 {".webp", FORMAT_WEBP },
117 {".ktx", FORMAT_KTX },
118 {".astc", FORMAT_ASTC},
119 {".ico", FORMAT_ICO },
120 {".wbmp", FORMAT_WBMP},
121 {".svg", FORMAT_UNSUPPORTED}, // SVG
122 {".tvg", FORMAT_UNSUPPORTED}, // ThorVG
123 {".json", FORMAT_UNSUPPORTED}, // Lottie
127 const unsigned int FORMAT_EXTENSIONS_COUNT = sizeof(FORMAT_EXTENSIONS) / sizeof(FormatExtension);
129 FileFormats GetFormatHint(const std::string& filename)
131 FileFormats format = FORMAT_UNKNOWN;
133 for(unsigned int i = 0; i < FORMAT_EXTENSIONS_COUNT; ++i)
135 unsigned int length = FORMAT_EXTENSIONS[i].extension.size();
136 if((filename.size() > length) &&
137 (0 == filename.compare(filename.size() - length, length, FORMAT_EXTENSIONS[i].extension)))
139 format = FORMAT_EXTENSIONS[i].format;
148 * Checks the magic bytes of the file first to determine which Image decoder to use to decode the
150 * @param[in] fp The file to decode
151 * @param[in] format Hint about what format to try first
152 * @param[out] loader Set with the function to use to decode the image
153 * @param[out] header Set with the function to use to decode the header
154 * @param[out] profile The kind of bitmap to hold the bits loaded for the bitmap.
155 * @return true, if we can decode the image, false otherwise
157 bool GetBitmapLoaderFunctions(FILE* fp,
159 Dali::ImageLoader::LoadBitmapFunction& loader,
160 Dali::ImageLoader::LoadPlanesFunction& planeLoader,
161 Dali::ImageLoader::LoadBitmapHeaderFunction& header,
162 Bitmap::Profile& profile,
163 const std::string& filename)
165 // Fast out if image loader doesn't support the format.
166 if(DALI_UNLIKELY(format == FORMAT_UNSUPPORTED))
171 unsigned char magic[MAGIC_LENGTH];
172 size_t read = fread(magic, sizeof(unsigned char), MAGIC_LENGTH, fp);
174 // Reset to the start of the file.
175 if(fseek(fp, 0, SEEK_SET))
177 DALI_LOG_ERROR("Error seeking to start of file\n");
180 if(read != MAGIC_LENGTH)
185 bool loaderFound = false;
186 const Dali::ImageLoader::BitmapLoader* lookupPtr = BITMAP_LOADER_LOOKUP_TABLE;
187 Dali::ImageLoader::Input defaultInput(fp);
189 // try plugin image loader
190 const Dali::ImageLoader::BitmapLoader* data = Internal::Adaptor::ImageLoaderPluginProxy::BitmapLoaderLookup(filename);
194 unsigned int width = 0;
195 unsigned int height = 0;
196 loaderFound = lookupPtr->header(fp, width, height);
200 if(false == loaderFound && format != FORMAT_UNKNOWN)
202 lookupPtr = BITMAP_LOADER_LOOKUP_TABLE + format;
203 if(format >= FORMAT_MAGIC_BYTE_COUNT ||
204 (lookupPtr->magicByte1 == magic[0] && lookupPtr->magicByte2 == magic[1]))
206 unsigned int width = 0;
207 unsigned int height = 0;
208 loaderFound = lookupPtr->header(fp, width, height);
212 // then try to get a match with formats that have magic bytes
213 if(false == loaderFound)
215 for(lookupPtr = BITMAP_LOADER_LOOKUP_TABLE;
216 lookupPtr < BITMAP_LOADER_LOOKUP_TABLE + FORMAT_MAGIC_BYTE_COUNT;
219 if(lookupPtr->magicByte1 == magic[0] && lookupPtr->magicByte2 == magic[1])
221 // to seperate ico file format and wbmp file format
222 unsigned int width = 0;
223 unsigned int height = 0;
224 loaderFound = lookupPtr->header(fp, width, height);
233 // finally try formats that do not use magic bytes
234 if(false == loaderFound)
236 for(lookupPtr = BITMAP_LOADER_LOOKUP_TABLE + FORMAT_MAGIC_BYTE_COUNT;
237 lookupPtr < BITMAP_LOADER_LOOKUP_TABLE + FORMAT_TOTAL_COUNT;
240 // to seperate ico file format and wbmp file format
241 unsigned int width = 0;
242 unsigned int height = 0;
243 loaderFound = lookupPtr->header(fp, width, height);
251 // if a loader was found set the outputs
254 loader = lookupPtr->loader;
255 planeLoader = lookupPtr->planeLoader;
256 header = lookupPtr->header;
257 profile = lookupPtr->profile;
260 // Reset to the start of the file.
261 if(fseek(fp, 0, SEEK_SET))
263 DALI_LOG_ERROR("Error seeking to start of file\n");
269 } // anonymous namespace
271 namespace ImageLoader
273 bool ConvertStreamToBitmap(const BitmapResourceType& resource, const std::string& path, FILE* const fp, Dali::Devel::PixelBuffer& pixelBuffer)
275 DALI_LOG_TRACE_METHOD(gLogFilter);
281 Dali::ImageLoader::LoadBitmapFunction function;
282 Dali::ImageLoader::LoadPlanesFunction planeLoader;
283 Dali::ImageLoader::LoadBitmapHeaderFunction header;
285 Bitmap::Profile profile;
287 if(GetBitmapLoaderFunctions(fp,
295 const Dali::ImageLoader::ScalingParameters scalingParameters(resource.size, resource.scalingMode, resource.samplingMode);
296 const Dali::ImageLoader::Input input(fp, scalingParameters, resource.orientationCorrection);
298 // Run the image type decoder:
299 result = function(input, pixelBuffer);
303 DALI_LOG_ERROR("Unable to convert %s\n", path.c_str());
307 pixelBuffer = Internal::Platform::ApplyAttributesToBitmap(pixelBuffer, resource.size, resource.scalingMode, resource.samplingMode);
311 DALI_LOG_ERROR("Image Decoder for %s unavailable\n", path.c_str());
318 bool ConvertStreamToPlanes(const Integration::BitmapResourceType& resource, const std::string& path, FILE* const fp, std::vector<Dali::Devel::PixelBuffer>& pixelBuffers)
320 DALI_LOG_TRACE_METHOD(gLogFilter);
326 Dali::ImageLoader::LoadBitmapFunction loader;
327 Dali::ImageLoader::LoadPlanesFunction planeLoader;
328 Dali::ImageLoader::LoadBitmapHeaderFunction header;
330 Bitmap::Profile profile;
332 if(GetBitmapLoaderFunctions(fp,
340 const Dali::ImageLoader::ScalingParameters scalingParameters(resource.size, resource.scalingMode, resource.samplingMode);
341 const Dali::ImageLoader::Input input(fp, scalingParameters, resource.orientationCorrection);
343 pixelBuffers.clear();
345 // Run the image type decoder:
348 result = planeLoader(input, pixelBuffers);
351 DALI_LOG_ERROR("Unable to convert %s\n", path.c_str());
356 Dali::Devel::PixelBuffer pixelBuffer;
357 result = loader(input, pixelBuffer);
360 DALI_LOG_ERROR("Unable to convert %s\n", path.c_str());
364 pixelBuffer = Internal::Platform::ApplyAttributesToBitmap(pixelBuffer, resource.size, resource.scalingMode, resource.samplingMode);
367 pixelBuffers.push_back(pixelBuffer);
371 DALI_LOG_ERROR("ApplyAttributesToBitmap is failed [%s]\n", path.c_str());
378 DALI_LOG_ERROR("Image Decoder for %s unavailable\n", path.c_str());
385 ResourcePointer LoadImageSynchronously(const Integration::BitmapResourceType& resource, const std::string& path)
387 ResourcePointer result;
388 Dali::Devel::PixelBuffer bitmap;
390 Internal::Platform::FileReader fileReader(path);
391 FILE* const fp = fileReader.GetFile();
394 bool success = ConvertStreamToBitmap(resource, path, fp, bitmap);
395 if(success && bitmap)
397 Bitmap::Profile profile{Bitmap::Profile::BITMAP_2D_PACKED_PIXELS};
399 // For backward compatibility the Bitmap must be created
400 auto retval = Bitmap::New(profile, Dali::ResourcePolicy::OWNED_DISCARD);
402 DALI_LOG_SET_OBJECT_STRING(retval, path);
404 retval->GetPackedPixelsProfile()->ReserveBuffer(
405 bitmap.GetPixelFormat(),
411 auto& impl = Dali::GetImplementation(bitmap);
413 std::copy(impl.GetBuffer(), impl.GetBuffer() + impl.GetBufferSize(), retval->GetBuffer());
414 result.Reset(retval);
420 ///@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?]
421 ImageDimensions GetClosestImageSize(const std::string& filename,
422 ImageDimensions size,
423 FittingMode::Type fittingMode,
424 SamplingMode::Type samplingMode,
425 bool orientationCorrection)
427 unsigned int width = 0;
428 unsigned int height = 0;
430 const FileFormats formatHint = GetFormatHint(filename);
432 // Fast out if image loader doesn't support the format.
433 if(formatHint == FileFormats::FORMAT_UNSUPPORTED)
435 return ImageDimensions(width, height);
438 Internal::Platform::FileReader fileReader(filename);
439 FILE* fp = fileReader.GetFile();
442 Dali::ImageLoader::LoadBitmapFunction loaderFunction;
443 Dali::ImageLoader::LoadPlanesFunction planeLoader;
444 Dali::ImageLoader::LoadBitmapHeaderFunction headerFunction;
445 Bitmap::Profile profile;
447 if(GetBitmapLoaderFunctions(fp,
455 const Dali::ImageLoader::Input input(fp, Dali::ImageLoader::ScalingParameters(size, fittingMode, samplingMode), orientationCorrection);
457 const bool read_res = headerFunction(input, width, height);
460 DALI_LOG_ERROR("Image Decoder failed to read header for %s\n", filename.c_str());
465 DALI_LOG_ERROR("Image Decoder for %s unavailable\n", filename.c_str());
468 return ImageDimensions(width, height);
471 ImageDimensions GetClosestImageSize(Integration::ResourcePointer resourceBuffer,
472 ImageDimensions size,
473 FittingMode::Type fittingMode,
474 SamplingMode::Type samplingMode,
475 bool orientationCorrection)
477 unsigned int width = 0;
478 unsigned int height = 0;
480 // Get the blob of binary data that we need to decode:
481 DALI_ASSERT_DEBUG(resourceBuffer);
482 Dali::RefCountedVector<uint8_t>* const encodedBlob = reinterpret_cast<Dali::RefCountedVector<uint8_t>*>(resourceBuffer.Get());
486 if(encodedBlob->GetVector().Size())
488 // Open a file handle on the memory buffer:
489 Internal::Platform::FileReader fileReader(encodedBlob->GetVector());
490 FILE* fp = fileReader.GetFile();
493 Dali::ImageLoader::LoadBitmapFunction loaderFunction;
494 Dali::ImageLoader::LoadPlanesFunction planeLoader;
495 Dali::ImageLoader::LoadBitmapHeaderFunction headerFunction;
496 Bitmap::Profile profile;
498 if(GetBitmapLoaderFunctions(fp,
506 const Dali::ImageLoader::Input input(fp, Dali::ImageLoader::ScalingParameters(size, fittingMode, samplingMode), orientationCorrection);
507 const bool read_res = headerFunction(input, width, height);
510 DALI_LOG_ERROR("Image Decoder failed to read header for resourceBuffer\n");
516 return ImageDimensions(width, height);
519 void SetMaxTextureSize(unsigned int size)
521 gMaxTextureSize = size;
522 gMaxTextureSizeUpdated = true;
525 unsigned int GetMaxTextureSize()
527 return gMaxTextureSize;
530 bool MaxTextureSizeUpdated()
532 return gMaxTextureSizeUpdated;
535 } // namespace ImageLoader
536 } // namespace TizenPlatform