Decoding webp using desired size 46/305546/8
authorsunghyun kim <scholb.kim@samsung.com>
Mon, 5 Feb 2024 01:37:45 +0000 (10:37 +0900)
committersunghyun kim <scholb.kim@samsung.com>
Wed, 21 Feb 2024 07:36:01 +0000 (16:36 +0900)
webp provides decoding option with provided size.
with this options, dali don't need to scaling buffer after decoding.

Change-Id: I63bb47ec29cb3950687b1814839674ca32c4cb62

dali/internal/imaging/common/animated-image-loading-impl.h
dali/internal/imaging/common/gif-loading.cpp
dali/internal/imaging/common/gif-loading.h
dali/internal/imaging/common/webp-loading.cpp
dali/internal/imaging/common/webp-loading.h

index a240b1d..a150053 100644 (file)
@@ -82,7 +82,7 @@ public:
                                      Dali::FittingMode::Type  fittingMode,
                                      Dali::SamplingMode::Type samplingMode)
   {
-    Dali::Devel::PixelBuffer pixelBuffer = LoadFrame(frameIndex);
+    Dali::Devel::PixelBuffer pixelBuffer = LoadFrame(frameIndex, size);
     return Dali::Internal::Platform::ApplyAttributesToBitmap(pixelBuffer, size, fittingMode, samplingMode);
   }
 
@@ -119,9 +119,10 @@ private:
    *
    * @note This function will load the entire animated image into memory if not already loaded.
    * @param[in] frameIndex The frame index to load.
+   * @param[in] size The width and height to fit the loaded image to.
    * @return Dali::Devel::PixelBuffer The loaded PixelBuffer. If loading is fail, return empty handle.
    */
-  virtual Dali::Devel::PixelBuffer LoadFrame(uint32_t frameIndex) = 0;
+  virtual Dali::Devel::PixelBuffer LoadFrame(uint32_t frameIndex, ImageDimensions size = ImageDimensions()) = 0;
 };
 
 } // namespace Adaptor
index 7c04fc2..37d8109 100644 (file)
@@ -1576,7 +1576,7 @@ GifLoading::~GifLoading()
   delete mImpl;
 }
 
-Dali::Devel::PixelBuffer GifLoading::LoadFrame(uint32_t frameIndex)
+Dali::Devel::PixelBuffer GifLoading::LoadFrame(uint32_t frameIndex, ImageDimensions desiredSize)
 {
   int                      error;
   Dali::Devel::PixelBuffer pixelBuffer;
index 2de8ead..dbc564a 100644 (file)
@@ -75,7 +75,7 @@ public:
    * @return Dali::Devel::PixelBuffer The loaded PixelBuffer. If loading is fail, return empty handle.
    */
 
-  Dali::Devel::PixelBuffer LoadFrame(uint32_t frameIndex) override;
+  Dali::Devel::PixelBuffer LoadFrame(uint32_t frameIndex, ImageDimensions size) override;
 
   /**
    * @brief Get the size of a gif image.
index 1903d2c..0dd3c85 100644 (file)
@@ -307,7 +307,7 @@ WebPLoading::~WebPLoading()
   delete mImpl;
 }
 
-Dali::Devel::PixelBuffer WebPLoading::LoadFrame(uint32_t frameIndex)
+Dali::Devel::PixelBuffer WebPLoading::LoadFrame(uint32_t frameIndex, ImageDimensions desiredSize)
 {
   Dali::Devel::PixelBuffer pixelBuffer;
 
@@ -334,22 +334,71 @@ Dali::Devel::PixelBuffer WebPLoading::LoadFrame(uint32_t frameIndex)
       {
         uint32_t channelNumber = (features.has_alpha) ? 4 : 3;
         uint8_t* frameBuffer   = nullptr;
-        if(channelNumber == 4)
+
+        if(desiredSize.GetWidth() > 0 && desiredSize.GetHeight() > 0)
         {
-          frameBuffer = WebPDecodeRGBA(mImpl->mBuffer, mImpl->mBufferSize, &width, &height);
+          const int desiredWidth = desiredSize.GetWidth();
+          const int desiredHeight = desiredSize.GetHeight();
+
+          WebPDecoderConfig config;
+          if(!DALI_LIKELY(WebPInitDecoderConfig(&config)))
+          {
+            DALI_LOG_ERROR("WebPInitDecoderConfig is failed");
+            return pixelBuffer;
+          }
+
+          // Apply config for scaling
+          config.options.use_scaling = 1;
+          config.options.scaled_width = desiredWidth;
+          config.options.scaled_height = desiredHeight;
+
+          if(channelNumber == 4)
+          {
+            config.output.colorspace = MODE_RGBA;
+          }
+          else
+          {
+            config.output.colorspace = MODE_RGB;
+          }
+
+          if(WebPDecode(mImpl->mBuffer, mImpl->mBufferSize, &config) == VP8_STATUS_OK)
+          {
+            frameBuffer = config.output.u.RGBA.rgba;
+          }
+          else
+          {
+            DALI_LOG_ERROR("Webp Decoding with scaled size is failed \n");
+          }
+
+          if(frameBuffer != nullptr)
+          {
+            Pixel::Format                     pixelFormat = (channelNumber == 4) ? Pixel::RGBA8888 : Pixel::RGB888;
+            int32_t                           bufferSize  = desiredWidth * desiredHeight * Dali::Pixel::GetBytesPerPixel(pixelFormat);
+            pixelBuffer  = Dali::Devel::PixelBuffer::New(desiredWidth, desiredHeight, pixelFormat);
+            memcpy(pixelBuffer.GetBuffer(), frameBuffer, bufferSize);
+          }
+
+          WebPFreeDecBuffer(&config.output);
         }
         else
         {
-          frameBuffer = WebPDecodeRGB(mImpl->mBuffer, mImpl->mBufferSize, &width, &height);
-        }
+          if(channelNumber == 4)
+          {
+            frameBuffer = WebPDecodeRGBA(mImpl->mBuffer, mImpl->mBufferSize, &width, &height);
+          }
+          else
+          {
+            frameBuffer = WebPDecodeRGB(mImpl->mBuffer, mImpl->mBufferSize, &width, &height);
+          }
 
-        if(frameBuffer != nullptr)
-        {
-          Pixel::Format                     pixelFormat = (channelNumber == 4) ? Pixel::RGBA8888 : Pixel::RGB888;
-          int32_t                           bufferSize  = width * height * Dali::Pixel::GetBytesPerPixel(pixelFormat);
-          Internal::Adaptor::PixelBufferPtr internal =
+          if(frameBuffer != nullptr)
+          {
+            Pixel::Format                     pixelFormat = (channelNumber == 4) ? Pixel::RGBA8888 : Pixel::RGB888;
+            int32_t                           bufferSize  = width * height * Dali::Pixel::GetBytesPerPixel(pixelFormat);
+            Internal::Adaptor::PixelBufferPtr internal =
             Internal::Adaptor::PixelBuffer::New(frameBuffer, bufferSize, width, height, width, pixelFormat);
-          pixelBuffer = Devel::PixelBuffer(internal.Get());
+            pixelBuffer = Devel::PixelBuffer(internal.Get());
+          }
         }
       }
     }
index 5a232fb..74eed61 100644 (file)
@@ -92,7 +92,7 @@ public:
    * @param[in] frameIndex The frame counter to load. Will usually be the next frame.
    * @return Dali::Devel::PixelBuffer The loaded PixelBuffer. If loading is fail, return empty handle.
    */
-  Dali::Devel::PixelBuffer LoadFrame(uint32_t frameIndex) override;
+  Dali::Devel::PixelBuffer LoadFrame(uint32_t frameIndex, ImageDimensions size) override;
 
   /**
    * @brief Get the size of a webp image.