Merge "[AT-SPI] Rework intercepting key events" into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / common / loader-png.cpp
index 397b822..fa855db 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 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.
@@ -59,13 +59,13 @@ bool LoadPngHeader(FILE* fp, unsigned int& width, unsigned int& height, png_stru
 
   // Check header to see if it is a PNG file
   size_t size = fread(header, 1, 8, fp);
-  if(size != 8)
+  if(DALI_UNLIKELY(size != 8))
   {
     DALI_LOG_ERROR("fread failed\n");
     return false;
   }
 
-  if(png_sig_cmp(header, 0, 8))
+  if(DALI_UNLIKELY(png_sig_cmp(header, 0, 8)))
   {
     DALI_LOG_ERROR("png_sig_cmp failed\n");
     return false;
@@ -73,14 +73,14 @@ bool LoadPngHeader(FILE* fp, unsigned int& width, unsigned int& height, png_stru
 
   png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
 
-  if(!png)
+  if(DALI_UNLIKELY(!png))
   {
     DALI_LOG_ERROR("Can't create PNG read structure\n");
     return false;
   }
 
   info = png_create_info_struct(png);
-  if(!info)
+  if(DALI_UNLIKELY(!info))
   {
     DALI_LOG_ERROR("png_create_info_struct failed\n");
     return false;
@@ -88,7 +88,7 @@ bool LoadPngHeader(FILE* fp, unsigned int& width, unsigned int& height, png_stru
 
   png_set_expand(png);
 
-  if(setjmp(png_jmpbuf(png)))
+  if(DALI_UNLIKELY(setjmp(png_jmpbuf(png))))
   {
     DALI_LOG_ERROR("error during png_init_io\n");
     return false;
@@ -134,7 +134,7 @@ bool LoadBitmapFromPng(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel
   bool         valid = false;
 
   // Load info from the header
-  if(!LoadPngHeader(input.file, width, height, png, info))
+  if(DALI_UNLIKELY(!LoadPngHeader(input.file, width, height, png, info)))
   {
     return false;
   }
@@ -259,7 +259,7 @@ bool LoadBitmapFromPng(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel
     }
   }
 
-  if(!valid)
+  if(DALI_UNLIKELY(!valid))
   {
     DALI_LOG_ERROR("Unsupported png format\n");
     return false;
@@ -270,7 +270,7 @@ bool LoadBitmapFromPng(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel
 
   png_read_update_info(png, info);
 
-  if(setjmp(png_jmpbuf(png)))
+  if(DALI_UNLIKELY(setjmp(png_jmpbuf(png))))
   {
     DALI_LOG_ERROR("error during png_read_image\n");
     return false;
@@ -305,10 +305,17 @@ bool LoadBitmapFromPng(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel
   auto pixels = (bitmap = Dali::Devel::PixelBuffer::New(bufferWidth, bufferHeight, pixelFormat)).GetBuffer();
 
   DALI_ASSERT_DEBUG(pixels);
+
+  if(!pixels)
+  {
+    DALI_LOG_ERROR("PixelBuffer couldn't be created\n");
+    return false;
+  }
+
   rows = reinterpret_cast<png_bytep*>(malloc(sizeof(png_bytep) * height));
-  if(!rows)
+  if(DALI_UNLIKELY(!rows))
   {
-    DALI_LOG_ERROR("malloc is failed\n");
+    DALI_LOG_ERROR("malloc is failed. request malloc size : %zu\n", sizeof(png_bytep) * height);
     return false;
   }
 
@@ -371,7 +378,7 @@ extern "C" void WriteData(png_structp png_ptr, png_bytep data, png_size_t length
     if(encoded_img)
     {
       const Vector<unsigned char>::SizeType bufferSize = encoded_img->Count();
-      encoded_img->Resize(bufferSize + length); //< Can throw OOM.
+      encoded_img->ResizeUninitialized(bufferSize + length); //< Can throw OOM.
       unsigned char* const bufferBack = encoded_img->Begin() + bufferSize;
       memcpy(bufferBack, data, length);
     }
@@ -390,7 +397,7 @@ extern "C" void WriteData(png_structp png_ptr, png_bytep data, png_size_t length
 extern "C" void FlushData(png_structp png_ptr)
 {
 #ifdef DEBUG_ENABLED
-  Debug::LogMessageWithFunctionLine(Debug::DebugInfo, "PNG Flush");
+  Debug::LogMessageWithFunctionLine(Debug::INFO, "PNG Flush");
 #endif // DEBUG_ENABLED
 }
 } // namespace
@@ -417,6 +424,18 @@ bool EncodeToPng(const unsigned char* const pixelBuffer, Vector<unsigned char>&
   // Account for RGB versus BGR and presence of alpha in input pixels:
   switch(pixelFormat)
   {
+    case Pixel::L8:
+    {
+      pngPixelFormat = PNG_COLOR_TYPE_GRAY;
+      pixelBytes     = 1;
+      break;
+    }
+    case Pixel::LA88:
+    {
+      pngPixelFormat = PNG_COLOR_TYPE_GRAY_ALPHA;
+      pixelBytes     = 2;
+      break;
+    }
     case Pixel::RGB888:
     {
       pngPixelFormat = PNG_COLOR_TYPE_RGB;
@@ -436,7 +455,7 @@ bool EncodeToPng(const unsigned char* const pixelBuffer, Vector<unsigned char>&
     }
     default:
     {
-      DALI_LOG_ERROR("Unsupported pixel format for encoding to PNG.\n");
+      DALI_LOG_ERROR("Unsupported pixel format for encoding to PNG. Format enum : [%d]\n", pixelFormat);
       return false;
     }
   }
@@ -444,14 +463,16 @@ bool EncodeToPng(const unsigned char* const pixelBuffer, Vector<unsigned char>&
   const int interlace = PNG_INTERLACE_NONE;
 
   png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
-  if(!png_ptr)
+  if(DALI_UNLIKELY(!png_ptr))
   {
+    DALI_LOG_ERROR("Fail to create png_structp for png version" PNG_LIBPNG_VER_STRING "\n");
     return false;
   }
   /* Allocate/initialize the image information data.  REQUIRED */
   png_infop info_ptr = png_create_info_struct(png_ptr);
-  if(!info_ptr)
+  if(DALI_UNLIKELY(!info_ptr))
   {
+    DALI_LOG_ERROR("Fail to create png_infop\n");
     png_destroy_write_struct(&png_ptr, NULL);
     return false;
   }
@@ -459,8 +480,9 @@ bool EncodeToPng(const unsigned char* const pixelBuffer, Vector<unsigned char>&
   /* Set error handling.  REQUIRED if you aren't supplying your own
    * error handling functions in the png_create_write_struct() call.
    */
-  if(setjmp(png_jmpbuf(png_ptr)))
+  if(DALI_UNLIKELY(setjmp(png_jmpbuf(png_ptr))))
   {
+    DALI_LOG_ERROR("Fail to png_jmpbuf\n");
     png_destroy_write_struct(&png_ptr, &info_ptr);
     return false;
   }