Support *.pkm format file decode
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / common / image-loader.cpp
index 9f9db11..a88270a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
 #include <dali/internal/imaging/common/loader-ico.h>
 #include <dali/internal/imaging/common/loader-jpeg.h>
 #include <dali/internal/imaging/common/loader-ktx.h>
+#include <dali/internal/imaging/common/loader-pkm.h>
 #include <dali/internal/imaging/common/loader-png.h>
 #include <dali/internal/imaging/common/loader-wbmp.h>
 #include <dali/internal/imaging/common/loader-webp.h>
@@ -54,6 +55,9 @@ static bool gMaxTextureSizeUpdated = false;
  */
 enum FileFormats
 {
+  // Unsupported file format that we should not process image loader.
+  FORMAT_UNSUPPORTED = -2,
+
   // Unknown file format
   FORMAT_UNKNOWN = -1,
 
@@ -65,6 +69,7 @@ enum FileFormats
   FORMAT_WEBP,
   FORMAT_KTX,
   FORMAT_ASTC,
+  FORMAT_PKM,
   FORMAT_ICO,
   FORMAT_MAGIC_BYTE_COUNT,
 
@@ -87,6 +92,7 @@ const Dali::ImageLoader::BitmapLoader BITMAP_LOADER_LOOKUP_TABLE[FORMAT_TOTAL_CO
     {Webp::MAGIC_BYTE_1, Webp::MAGIC_BYTE_2, LoadBitmapFromWebp, nullptr, LoadWebpHeader, Bitmap::BITMAP_2D_PACKED_PIXELS},
     {Ktx::MAGIC_BYTE_1,  Ktx::MAGIC_BYTE_2,  LoadBitmapFromKtx,  nullptr, LoadKtxHeader,  Bitmap::BITMAP_COMPRESSED      },
     {Astc::MAGIC_BYTE_1, Astc::MAGIC_BYTE_2, LoadBitmapFromAstc, nullptr, LoadAstcHeader, Bitmap::BITMAP_COMPRESSED      },
+    {Pkm::MAGIC_BYTE_1,  Pkm::MAGIC_BYTE_2,  LoadBitmapFromPkm,  nullptr, LoadPkmHeader,  Bitmap::BITMAP_COMPRESSED      },
     {Ico::MAGIC_BYTE_1,  Ico::MAGIC_BYTE_2,  LoadBitmapFromIco,  nullptr, LoadIcoHeader,  Bitmap::BITMAP_2D_PACKED_PIXELS},
     {0x0,                0x0,                LoadBitmapFromWbmp, nullptr, LoadWbmpHeader, Bitmap::BITMAP_2D_PACKED_PIXELS},
   };
@@ -99,12 +105,12 @@ const unsigned int MAGIC_LENGTH = 2;
  */
 struct FormatExtension
 {
-  const std::string extension;
-  FileFormats       format;
+  const std::string_view extension;
+  FileFormats            format;
 };
 
 // clang-format off
-const FormatExtension FORMAT_EXTENSIONS[] =
+constexpr FormatExtension FORMAT_EXTENSIONS[] =
   {
     {".png",  FORMAT_PNG },
     {".jpg",  FORMAT_JPEG},
@@ -113,8 +119,12 @@ const FormatExtension FORMAT_EXTENSIONS[] =
     {".webp", FORMAT_WEBP },
     {".ktx",  FORMAT_KTX },
     {".astc", FORMAT_ASTC},
+    {".pkm",  FORMAT_PKM},
     {".ico",  FORMAT_ICO },
-    {".wbmp", FORMAT_WBMP}
+    {".wbmp", FORMAT_WBMP},
+    {".svg",  FORMAT_UNSUPPORTED}, // SVG
+    {".tvg",  FORMAT_UNSUPPORTED}, // ThorVG
+    {".json", FORMAT_UNSUPPORTED}, // Lottie
   };
 // clang-format on
 
@@ -156,6 +166,12 @@ bool GetBitmapLoaderFunctions(FILE*                                        fp,
                               Bitmap::Profile&                             profile,
                               const std::string&                           filename)
 {
+  // Fast out if image loader doesn't support the format.
+  if(DALI_UNLIKELY(format == FORMAT_UNSUPPORTED))
+  {
+    return false;
+  }
+
   unsigned char magic[MAGIC_LENGTH];
   size_t        read = fread(magic, sizeof(unsigned char), MAGIC_LENGTH, fp);
 
@@ -415,6 +431,14 @@ ImageDimensions GetClosestImageSize(const std::string& filename,
   unsigned int width  = 0;
   unsigned int height = 0;
 
+  const FileFormats formatHint = GetFormatHint(filename);
+
+  // Fast out if image loader doesn't support the format.
+  if(formatHint == FileFormats::FORMAT_UNSUPPORTED)
+  {
+    return ImageDimensions(width, height);
+  }
+
   Internal::Platform::FileReader fileReader(filename);
   FILE*                          fp = fileReader.GetFile();
   if(fp != NULL)
@@ -425,7 +449,7 @@ ImageDimensions GetClosestImageSize(const std::string& filename,
     Bitmap::Profile                             profile;
 
     if(GetBitmapLoaderFunctions(fp,
-                                GetFormatHint(filename),
+                                formatHint,
                                 loaderFunction,
                                 planeLoader,
                                 headerFunction,