2 * Copyright (c) 2022 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 // Unknown file format
60 // formats that use magic bytes
69 FORMAT_MAGIC_BYTE_COUNT,
71 // formats after this one do not use magic bytes
72 FORMAT_WBMP = FORMAT_MAGIC_BYTE_COUNT,
77 * A lookup table containing all the bitmap loaders with the appropriate information.
78 * Has to be in sync with enum FileFormats
81 const Dali::ImageLoader::BitmapLoader BITMAP_LOADER_LOOKUP_TABLE[FORMAT_TOTAL_COUNT] =
83 {Png::MAGIC_BYTE_1, Png::MAGIC_BYTE_2, LoadBitmapFromPng, nullptr, LoadPngHeader, Bitmap::BITMAP_2D_PACKED_PIXELS},
84 {Jpeg::MAGIC_BYTE_1, Jpeg::MAGIC_BYTE_2, LoadBitmapFromJpeg, LoadPlanesFromJpeg, LoadJpegHeader, Bitmap::BITMAP_2D_PACKED_PIXELS},
85 {Bmp::MAGIC_BYTE_1, Bmp::MAGIC_BYTE_2, LoadBitmapFromBmp, nullptr, LoadBmpHeader, Bitmap::BITMAP_2D_PACKED_PIXELS},
86 {Gif::MAGIC_BYTE_1, Gif::MAGIC_BYTE_2, LoadBitmapFromGif, nullptr, LoadGifHeader, Bitmap::BITMAP_2D_PACKED_PIXELS},
87 {Webp::MAGIC_BYTE_1, Webp::MAGIC_BYTE_2, LoadBitmapFromWebp, nullptr, LoadWebpHeader, Bitmap::BITMAP_2D_PACKED_PIXELS},
88 {Ktx::MAGIC_BYTE_1, Ktx::MAGIC_BYTE_2, LoadBitmapFromKtx, nullptr, LoadKtxHeader, Bitmap::BITMAP_COMPRESSED },
89 {Astc::MAGIC_BYTE_1, Astc::MAGIC_BYTE_2, LoadBitmapFromAstc, nullptr, LoadAstcHeader, Bitmap::BITMAP_COMPRESSED },
90 {Ico::MAGIC_BYTE_1, Ico::MAGIC_BYTE_2, LoadBitmapFromIco, nullptr, LoadIcoHeader, Bitmap::BITMAP_2D_PACKED_PIXELS},
91 {0x0, 0x0, LoadBitmapFromWbmp, nullptr, LoadWbmpHeader, Bitmap::BITMAP_2D_PACKED_PIXELS},
95 const unsigned int MAGIC_LENGTH = 2;
98 * This code tries to predict the file format from the filename to help with format picking.
100 struct FormatExtension
102 const std::string extension;
107 const FormatExtension FORMAT_EXTENSIONS[] =
109 {".png", FORMAT_PNG },
110 {".jpg", FORMAT_JPEG},
111 {".bmp", FORMAT_BMP },
112 {".gif", FORMAT_GIF },
113 {".webp", FORMAT_WEBP },
114 {".ktx", FORMAT_KTX },
115 {".astc", FORMAT_ASTC},
116 {".ico", FORMAT_ICO },
117 {".wbmp", FORMAT_WBMP}
121 const unsigned int FORMAT_EXTENSIONS_COUNT = sizeof(FORMAT_EXTENSIONS) / sizeof(FormatExtension);
123 FileFormats GetFormatHint(const std::string& filename)
125 FileFormats format = FORMAT_UNKNOWN;
127 for(unsigned int i = 0; i < FORMAT_EXTENSIONS_COUNT; ++i)
129 unsigned int length = FORMAT_EXTENSIONS[i].extension.size();
130 if((filename.size() > length) &&
131 (0 == filename.compare(filename.size() - length, length, FORMAT_EXTENSIONS[i].extension)))
133 format = FORMAT_EXTENSIONS[i].format;
142 * Checks the magic bytes of the file first to determine which Image decoder to use to decode the
144 * @param[in] fp The file to decode
145 * @param[in] format Hint about what format to try first
146 * @param[out] loader Set with the function to use to decode the image
147 * @param[out] header Set with the function to use to decode the header
148 * @param[out] profile The kind of bitmap to hold the bits loaded for the bitmap.
149 * @return true, if we can decode the image, false otherwise
151 bool GetBitmapLoaderFunctions(FILE* fp,
153 Dali::ImageLoader::LoadBitmapFunction& loader,
154 Dali::ImageLoader::LoadPlanesFunction& planeLoader,
155 Dali::ImageLoader::LoadBitmapHeaderFunction& header,
156 Bitmap::Profile& profile,
157 const std::string& filename)
159 unsigned char magic[MAGIC_LENGTH];
160 size_t read = fread(magic, sizeof(unsigned char), MAGIC_LENGTH, fp);
162 // Reset to the start of the file.
163 if(fseek(fp, 0, SEEK_SET))
165 DALI_LOG_ERROR("Error seeking to start of file\n");
168 if(read != MAGIC_LENGTH)
173 bool loaderFound = false;
174 const Dali::ImageLoader::BitmapLoader* lookupPtr = BITMAP_LOADER_LOOKUP_TABLE;
175 Dali::ImageLoader::Input defaultInput(fp);
177 // try plugin image loader
178 const Dali::ImageLoader::BitmapLoader* data = Internal::Adaptor::ImageLoaderPluginProxy::BitmapLoaderLookup(filename);
182 unsigned int width = 0;
183 unsigned int height = 0;
184 loaderFound = lookupPtr->header(fp, width, height);
188 if(false == loaderFound && format != FORMAT_UNKNOWN)
190 lookupPtr = BITMAP_LOADER_LOOKUP_TABLE + format;
191 if(format >= FORMAT_MAGIC_BYTE_COUNT ||
192 (lookupPtr->magicByte1 == magic[0] && lookupPtr->magicByte2 == magic[1]))
194 unsigned int width = 0;
195 unsigned int height = 0;
196 loaderFound = lookupPtr->header(fp, width, height);
200 // then try to get a match with formats that have magic bytes
201 if(false == loaderFound)
203 for(lookupPtr = BITMAP_LOADER_LOOKUP_TABLE;
204 lookupPtr < BITMAP_LOADER_LOOKUP_TABLE + FORMAT_MAGIC_BYTE_COUNT;
207 if(lookupPtr->magicByte1 == magic[0] && lookupPtr->magicByte2 == magic[1])
209 // to seperate ico file format and wbmp file format
210 unsigned int width = 0;
211 unsigned int height = 0;
212 loaderFound = lookupPtr->header(fp, width, height);
221 // finally try formats that do not use magic bytes
222 if(false == loaderFound)
224 for(lookupPtr = BITMAP_LOADER_LOOKUP_TABLE + FORMAT_MAGIC_BYTE_COUNT;
225 lookupPtr < BITMAP_LOADER_LOOKUP_TABLE + FORMAT_TOTAL_COUNT;
228 // to seperate ico file format and wbmp file format
229 unsigned int width = 0;
230 unsigned int height = 0;
231 loaderFound = lookupPtr->header(fp, width, height);
239 // if a loader was found set the outputs
242 loader = lookupPtr->loader;
243 planeLoader = lookupPtr->planeLoader;
244 header = lookupPtr->header;
245 profile = lookupPtr->profile;
248 // Reset to the start of the file.
249 if(fseek(fp, 0, SEEK_SET))
251 DALI_LOG_ERROR("Error seeking to start of file\n");
257 } // anonymous namespace
259 namespace ImageLoader
261 bool ConvertStreamToBitmap(const BitmapResourceType& resource, std::string path, FILE* const fp, Dali::Devel::PixelBuffer& pixelBuffer)
263 DALI_LOG_TRACE_METHOD(gLogFilter);
269 Dali::ImageLoader::LoadBitmapFunction function;
270 Dali::ImageLoader::LoadPlanesFunction planeLoader;
271 Dali::ImageLoader::LoadBitmapHeaderFunction header;
273 Bitmap::Profile profile;
275 if(GetBitmapLoaderFunctions(fp,
283 const Dali::ImageLoader::ScalingParameters scalingParameters(resource.size, resource.scalingMode, resource.samplingMode);
284 const Dali::ImageLoader::Input input(fp, scalingParameters, resource.orientationCorrection);
286 // Run the image type decoder:
287 result = function(input, pixelBuffer);
291 DALI_LOG_ERROR("Unable to convert %s\n", path.c_str());
295 pixelBuffer = Internal::Platform::ApplyAttributesToBitmap(pixelBuffer, resource.size, resource.scalingMode, resource.samplingMode);
299 DALI_LOG_ERROR("Image Decoder for %s unavailable\n", path.c_str());
306 bool ConvertStreamToPlanes(const Integration::BitmapResourceType& resource, std::string path, FILE* const fp, std::vector<Dali::Devel::PixelBuffer>& pixelBuffers)
308 DALI_LOG_TRACE_METHOD(gLogFilter);
314 Dali::ImageLoader::LoadBitmapFunction loader;
315 Dali::ImageLoader::LoadPlanesFunction planeLoader;
316 Dali::ImageLoader::LoadBitmapHeaderFunction header;
318 Bitmap::Profile profile;
320 if(GetBitmapLoaderFunctions(fp,
328 const Dali::ImageLoader::ScalingParameters scalingParameters(resource.size, resource.scalingMode, resource.samplingMode);
329 const Dali::ImageLoader::Input input(fp, scalingParameters, resource.orientationCorrection);
331 // Run the image type decoder:
334 result = planeLoader(input, pixelBuffers);
338 Dali::Devel::PixelBuffer pixelBuffer;
339 result = loader(input, pixelBuffer);
342 DALI_LOG_ERROR("Unable to convert %s\n", path.c_str());
345 pixelBuffer = Internal::Platform::ApplyAttributesToBitmap(pixelBuffer, resource.size, resource.scalingMode, resource.samplingMode);
347 pixelBuffers.push_back(pixelBuffer);
352 DALI_LOG_ERROR("Image Decoder for %s unavailable\n", path.c_str());
359 ResourcePointer LoadImageSynchronously(const Integration::BitmapResourceType& resource, const std::string& path)
361 ResourcePointer result;
362 Dali::Devel::PixelBuffer bitmap;
364 Internal::Platform::FileReader fileReader(path);
365 FILE* const fp = fileReader.GetFile();
368 bool success = ConvertStreamToBitmap(resource, path, fp, bitmap);
369 if(success && bitmap)
371 Bitmap::Profile profile{Bitmap::Profile::BITMAP_2D_PACKED_PIXELS};
373 // For backward compatibility the Bitmap must be created
374 auto retval = Bitmap::New(profile, Dali::ResourcePolicy::OWNED_DISCARD);
376 DALI_LOG_SET_OBJECT_STRING(retval, path);
378 retval->GetPackedPixelsProfile()->ReserveBuffer(
379 bitmap.GetPixelFormat(),
385 auto& impl = Dali::GetImplementation(bitmap);
387 std::copy(impl.GetBuffer(), impl.GetBuffer() + impl.GetBufferSize(), retval->GetBuffer());
388 result.Reset(retval);
394 ///@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?]
395 ImageDimensions GetClosestImageSize(const std::string& filename,
396 ImageDimensions size,
397 FittingMode::Type fittingMode,
398 SamplingMode::Type samplingMode,
399 bool orientationCorrection)
401 unsigned int width = 0;
402 unsigned int height = 0;
404 Internal::Platform::FileReader fileReader(filename);
405 FILE* fp = fileReader.GetFile();
408 Dali::ImageLoader::LoadBitmapFunction loaderFunction;
409 Dali::ImageLoader::LoadPlanesFunction planeLoader;
410 Dali::ImageLoader::LoadBitmapHeaderFunction headerFunction;
411 Bitmap::Profile profile;
413 if(GetBitmapLoaderFunctions(fp,
414 GetFormatHint(filename),
421 const Dali::ImageLoader::Input input(fp, Dali::ImageLoader::ScalingParameters(size, fittingMode, samplingMode), orientationCorrection);
423 const bool read_res = headerFunction(input, width, height);
426 DALI_LOG_ERROR("Image Decoder failed to read header for %s\n", filename.c_str());
431 DALI_LOG_ERROR("Image Decoder for %s unavailable\n", filename.c_str());
434 return ImageDimensions(width, height);
437 ImageDimensions GetClosestImageSize(Integration::ResourcePointer resourceBuffer,
438 ImageDimensions size,
439 FittingMode::Type fittingMode,
440 SamplingMode::Type samplingMode,
441 bool orientationCorrection)
443 unsigned int width = 0;
444 unsigned int height = 0;
446 // Get the blob of binary data that we need to decode:
447 DALI_ASSERT_DEBUG(resourceBuffer);
448 Dali::RefCountedVector<uint8_t>* const encodedBlob = reinterpret_cast<Dali::RefCountedVector<uint8_t>*>(resourceBuffer.Get());
452 if(encodedBlob->GetVector().Size())
454 // Open a file handle on the memory buffer:
455 Internal::Platform::FileReader fileReader(encodedBlob->GetVector());
456 FILE* fp = fileReader.GetFile();
459 Dali::ImageLoader::LoadBitmapFunction loaderFunction;
460 Dali::ImageLoader::LoadPlanesFunction planeLoader;
461 Dali::ImageLoader::LoadBitmapHeaderFunction headerFunction;
462 Bitmap::Profile profile;
464 if(GetBitmapLoaderFunctions(fp,
472 const Dali::ImageLoader::Input input(fp, Dali::ImageLoader::ScalingParameters(size, fittingMode, samplingMode), orientationCorrection);
473 const bool read_res = headerFunction(input, width, height);
476 DALI_LOG_ERROR("Image Decoder failed to read header for resourceBuffer\n");
482 return ImageDimensions(width, height);
485 void SetMaxTextureSize(unsigned int size)
487 gMaxTextureSize = size;
488 gMaxTextureSizeUpdated = true;
491 unsigned int GetMaxTextureSize()
493 return gMaxTextureSize;
496 bool MaxTextureSizeUpdated()
498 return gMaxTextureSizeUpdated;
501 } // namespace ImageLoader
502 } // namespace TizenPlatform