2 * Copyright (c) 2024 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.
19 #include <dali/internal/imaging/common/pixel-buffer-impl.h>
22 #include <dali/integration-api/pixel-data-integ.h>
27 #include <dali/integration-api/debug.h>
28 #include <dali/internal/imaging/common/alpha-mask.h>
29 #include <dali/internal/imaging/common/gaussian-blur.h>
30 #include <dali/internal/imaging/common/image-operations.h>
31 #include <dali/internal/imaging/common/pixel-manipulation.h>
41 #if defined(DEBUG_ENABLED)
42 Debug::Filter* gPixelBufferFilter = Debug::Filter::New(Debug::NoLogging, false, "DALI_LOG_PIXEL_BUFFER_SIZE");
45 const float TWO_PI = 2.f * Math::PI; ///< 360 degrees in radians
46 // based on W3C Recommendations (https://www.w3.org/TR/AERT/#color-contrast)
47 constexpr uint32_t BRIGHTNESS_CONSTANT_R = 299;
48 constexpr uint32_t BRIGHTNESS_CONSTANT_G = 587;
49 constexpr uint32_t BRIGHTNESS_CONSTANT_B = 114;
52 #if defined(DEBUG_ENABLED)
53 uint32_t PixelBuffer::gPixelBufferAllocationTotal{0};
56 PixelBuffer::PixelBuffer(uint8_t* buffer,
61 Dali::Pixel::Format pixelFormat)
64 mBufferSize(bufferSize),
67 mStride(stride ? stride : width),
68 mPixelFormat(pixelFormat),
73 PixelBuffer::~PixelBuffer()
78 PixelBufferPtr PixelBuffer::New(uint32_t width,
80 Dali::Pixel::Format pixelFormat)
82 uint32_t bufferSize = width * height * Dali::Pixel::GetBytesPerPixel(pixelFormat);
83 uint8_t* buffer = NULL;
86 buffer = static_cast<uint8_t*>(malloc(bufferSize));
87 #if defined(DEBUG_ENABLED)
88 gPixelBufferAllocationTotal += bufferSize;
91 DALI_LOG_INFO(gPixelBufferFilter, Debug::Concise, "Allocated PixelBuffer of size %u\n", bufferSize);
93 return new PixelBuffer(buffer, bufferSize, width, height, width, pixelFormat);
96 PixelBufferPtr PixelBuffer::New(uint8_t* buffer,
101 Dali::Pixel::Format pixelFormat)
103 return new PixelBuffer(buffer, bufferSize, width, height, stride, pixelFormat);
106 Dali::PixelData PixelBuffer::Convert(PixelBuffer& pixelBuffer, bool releaseAfterUpload)
108 #if defined(DEBUG_ENABLED)
109 gPixelBufferAllocationTotal -= pixelBuffer.mBufferSize;
111 Dali::PixelData pixelData;
112 if(releaseAfterUpload)
114 pixelData = Dali::Integration::NewPixelDataWithReleaseAfterUpload(pixelBuffer.mBuffer,
115 pixelBuffer.mBufferSize,
119 pixelBuffer.mPixelFormat,
120 Dali::PixelData::FREE);
124 pixelData = Dali::PixelData::New(pixelBuffer.mBuffer,
125 pixelBuffer.mBufferSize,
129 pixelBuffer.mPixelFormat,
130 Dali::PixelData::FREE);
132 pixelBuffer.mBuffer = NULL;
133 pixelBuffer.mWidth = 0;
134 pixelBuffer.mHeight = 0;
135 pixelBuffer.mBufferSize = 0;
136 pixelBuffer.mStride = 0;
141 uint32_t PixelBuffer::GetWidth() const
146 uint32_t PixelBuffer::GetHeight() const
151 uint32_t PixelBuffer::GetStride() const
156 Dali::Pixel::Format PixelBuffer::GetPixelFormat() const
161 uint8_t* PixelBuffer::GetBuffer() const
166 const uint8_t* PixelBuffer::GetConstBuffer() const
171 uint32_t PixelBuffer::GetBufferSize() const
176 Dali::PixelData PixelBuffer::CreatePixelData() const
178 uint8_t* destBuffer = NULL;
182 destBuffer = static_cast<uint8_t*>(malloc(mBufferSize));
183 memcpy(destBuffer, mBuffer, mBufferSize);
186 Dali::PixelData pixelData = Dali::PixelData::New(destBuffer, mBufferSize, mWidth, mHeight, mStride, mPixelFormat, Dali::PixelData::FREE);
190 void PixelBuffer::ApplyMask(const PixelBuffer& inMask, float contentScale, bool cropToMask)
194 // First scale this buffer by the contentScale, and crop to the mask size
195 // If it's too small, then scale the mask to match the image size
196 // Then apply the mask
197 ScaleAndCrop(contentScale, ImageDimensions(inMask.GetWidth(), inMask.GetHeight()));
199 if(inMask.mWidth > mWidth || inMask.mHeight > mHeight)
201 PixelBufferPtr mask = NewResize(inMask, ImageDimensions(mWidth, mHeight));
202 ApplyMaskInternal(*mask);
206 ApplyMaskInternal(inMask);
211 // First, scale the mask to match the image size,
212 // then apply the mask.
213 PixelBufferPtr mask = NewResize(inMask, ImageDimensions(mWidth, mHeight));
214 ApplyMaskInternal(*mask);
218 void PixelBuffer::ApplyMaskInternal(const PixelBuffer& mask)
223 Dali::Pixel::GetAlphaOffsetAndMask(mPixelFormat, byteOffset, bitMask);
224 if(Dali::Pixel::HasAlpha(mPixelFormat) && bitMask == 255)
226 ApplyMaskToAlphaChannel(*this, mask);
230 PixelBufferPtr newPixelBuffer = CreateNewMaskedBuffer(*this, mask);
231 TakeOwnershipOfBuffer(*newPixelBuffer);
232 // On leaving scope, newPixelBuffer will get destroyed.
236 void PixelBuffer::TakeOwnershipOfBuffer(PixelBuffer& pixelBuffer)
240 // Take ownership of new buffer
241 mBuffer = pixelBuffer.mBuffer;
242 pixelBuffer.mBuffer = NULL;
243 mBufferSize = pixelBuffer.mBufferSize;
244 mWidth = pixelBuffer.mWidth;
245 mHeight = pixelBuffer.mHeight;
246 mStride = pixelBuffer.mStride;
247 mPixelFormat = pixelBuffer.mPixelFormat;
250 void PixelBuffer::ReleaseBuffer()
254 #if defined(DEBUG_ENABLED)
255 gPixelBufferAllocationTotal -= mBufferSize;
262 void PixelBuffer::AllocateFixedSize(uint32_t size)
265 mBuffer = reinterpret_cast<unsigned char*>(malloc(size));
267 #if defined(DEBUG_ENABLED)
268 gPixelBufferAllocationTotal += size;
272 bool PixelBuffer::Rotate(Degree angle)
274 // Check first if Rotate() can perform the operation in the current pixel buffer.
276 bool validPixelFormat = false;
285 case Pixel::RGBA8888:
286 case Pixel::BGRA8888: // FALL THROUGH
288 validPixelFormat = true;
293 // This pixel format is not supported for this operation.
294 validPixelFormat = false;
299 if(!validPixelFormat)
301 // Can't rotate the pixel buffer with the current pixel format.
302 DALI_LOG_ERROR("Can't rotate the pixel buffer with the current pixel format\n");
306 float radians = Radian(angle).radian;
308 // Transform the input angle into the range [0..2PI]
309 radians = fmod(radians, TWO_PI);
310 radians += (radians < 0.f) ? TWO_PI : 0.f;
312 if(radians < Dali::Math::MACHINE_EPSILON_10)
314 // Nothing to do if the angle is zero.
318 const unsigned int pixelSize = Pixel::GetBytesPerPixel(mPixelFormat);
320 uint8_t* pixelsOut = nullptr;
321 Platform::RotateByShear(mBuffer,
331 // Check whether the rotation succedded and set the new pixel buffer data.
332 const bool success = nullptr != pixelsOut;
336 // Release the memory of the current pixel buffer.
339 // Set the new pixel buffer.
342 mBufferSize = mWidth * mHeight * pixelSize;
343 mStride = mWidth; // The buffer is tightly packed.
345 #if defined(DEBUG_ENABLED)
346 gPixelBufferAllocationTotal += mBufferSize;
353 void PixelBuffer::ScaleAndCrop(float scaleFactor, ImageDimensions cropDimensions)
355 ImageDimensions outDimensions(float(mWidth) * scaleFactor,
356 float(mHeight) * scaleFactor);
358 if(outDimensions.GetWidth() != mWidth || outDimensions.GetHeight() != mHeight)
360 Resize(outDimensions);
363 ImageDimensions postCropDimensions(
364 std::min(cropDimensions.GetWidth(), outDimensions.GetWidth()),
365 std::min(cropDimensions.GetHeight(), outDimensions.GetHeight()));
367 if(postCropDimensions.GetWidth() < outDimensions.GetWidth() ||
368 postCropDimensions.GetHeight() < outDimensions.GetHeight())
370 uint16_t x = (outDimensions.GetWidth() - postCropDimensions.GetWidth()) / 2;
371 uint16_t y = (outDimensions.GetHeight() - postCropDimensions.GetHeight()) / 2;
372 Crop(x, y, postCropDimensions);
376 void PixelBuffer::Crop(uint16_t x, uint16_t y, ImageDimensions cropDimensions)
378 PixelBufferPtr outBuffer = NewCrop(*this, x, y, cropDimensions);
379 TakeOwnershipOfBuffer(*outBuffer);
382 PixelBufferPtr PixelBuffer::NewCrop(const PixelBuffer& inBuffer, uint16_t x, uint16_t y, ImageDimensions cropDimensions)
384 PixelBufferPtr outBuffer = PixelBuffer::New(cropDimensions.GetWidth(), cropDimensions.GetHeight(), inBuffer.GetPixelFormat());
385 int bytesPerPixel = Pixel::GetBytesPerPixel(inBuffer.mPixelFormat);
386 int srcStride = inBuffer.mStride * bytesPerPixel;
387 int destStride = cropDimensions.GetWidth() * bytesPerPixel; // The destination buffer is tightly packed
389 // Clamp crop to right edge
390 if(x + cropDimensions.GetWidth() > inBuffer.mWidth)
392 destStride = (inBuffer.mWidth - x) * bytesPerPixel;
395 int srcOffset = x * bytesPerPixel + y * srcStride;
397 uint8_t* destBuffer = outBuffer->mBuffer;
399 // Clamp crop to last row
400 uint16_t endRow = y + cropDimensions.GetHeight();
401 if(endRow > inBuffer.mHeight)
403 endRow = inBuffer.mHeight - 1;
405 for(uint16_t row = y; row < endRow; ++row)
407 memcpy(destBuffer + destOffset, inBuffer.mBuffer + srcOffset, destStride);
408 srcOffset += srcStride;
409 destOffset += destStride;
414 void PixelBuffer::SetMetadata(const Property::Map& map)
416 mMetadata.reset(new Property::Map(map));
419 bool PixelBuffer::GetMetadata(Property::Map& outMetadata) const
425 outMetadata = *mMetadata;
429 void PixelBuffer::SetMetadata(std::unique_ptr<Property::Map> metadata)
431 mMetadata = std::move(metadata);
434 void PixelBuffer::Resize(ImageDimensions outDimensions)
436 if(mWidth != outDimensions.GetWidth() || mHeight != outDimensions.GetHeight())
438 PixelBufferPtr outBuffer = NewResize(*this, outDimensions);
439 TakeOwnershipOfBuffer(*outBuffer);
443 PixelBufferPtr PixelBuffer::NewResize(const PixelBuffer& inBuffer, ImageDimensions outDimensions)
445 PixelBufferPtr outBuffer = PixelBuffer::New(outDimensions.GetWidth(), outDimensions.GetHeight(), inBuffer.GetPixelFormat());
446 ImageDimensions inDimensions(inBuffer.mWidth, inBuffer.mHeight);
448 bool hasAlpha = Pixel::HasAlpha(inBuffer.mPixelFormat);
449 int bytesPerPixel = Pixel::GetBytesPerPixel(inBuffer.mPixelFormat);
451 Resampler::Filter filterType = Resampler::LANCZOS4;
452 if(inDimensions.GetWidth() < outDimensions.GetWidth() && inDimensions.GetHeight() < outDimensions.GetHeight())
454 filterType = Resampler::MITCHELL;
457 // This method only really works for 8 bit wide channels.
458 // (But could be expanded to work)
459 if(inBuffer.mPixelFormat == Pixel::A8 ||
460 inBuffer.mPixelFormat == Pixel::L8 ||
461 inBuffer.mPixelFormat == Pixel::LA88 ||
462 inBuffer.mPixelFormat == Pixel::RGB888 ||
463 inBuffer.mPixelFormat == Pixel::RGB8888 ||
464 inBuffer.mPixelFormat == Pixel::BGR8888 ||
465 inBuffer.mPixelFormat == Pixel::RGBA8888 ||
466 inBuffer.mPixelFormat == Pixel::BGRA8888)
468 Dali::Internal::Platform::Resample(inBuffer.mBuffer, inDimensions, inBuffer.mStride, outBuffer->GetBuffer(), outDimensions, filterType, bytesPerPixel, hasAlpha);
472 DALI_LOG_ERROR("Trying to resize an image with too narrow a channel width");
478 void PixelBuffer::ApplyGaussianBlur(const float blurRadius)
480 // This method only works for pixel buffer in RGBA format.
481 if(mWidth > 0 && mHeight > 0 && mPixelFormat == Pixel::RGBA8888)
483 if(blurRadius > Math::MACHINE_EPSILON_1)
485 PerformGaussianBlurRGBA(*this, blurRadius);
490 DALI_LOG_ERROR("Trying to apply gaussian blur to an empty pixel buffer or a pixel buffer not in RGBA format");
494 void PixelBuffer::MultiplyColorByAlpha()
496 // Compressed textures have unknown size of the pixel. Alpha premultiplication
497 // must be skipped in such case
498 if(!Pixel::IsCompressed(mPixelFormat) && Pixel::HasAlpha(mPixelFormat))
500 auto bytesPerPixel = Pixel::GetBytesPerPixel(mPixelFormat);
502 uint8_t* pixel = mBuffer;
503 const uint32_t strideBytes = mStride * bytesPerPixel;
504 const uint32_t widthBytes = mWidth * bytesPerPixel;
506 // Collect all valid channel list before lookup whole buffer
507 std::vector<Channel> validChannelList;
508 for(const Channel& channel : {Adaptor::RED, Adaptor::GREEN, Adaptor::BLUE, Adaptor::LUMINANCE})
510 if(HasChannel(mPixelFormat, channel))
512 validChannelList.emplace_back(channel);
516 if(DALI_LIKELY(!validChannelList.empty()))
518 for(uint32_t y = 0; y < mHeight; y++)
520 for(uint32_t x = 0; x < widthBytes; x += bytesPerPixel)
522 uint32_t alpha = ReadChannel(&pixel[x], mPixelFormat, Adaptor::ALPHA);
525 // If alpha is 255, we don't need to change color. Skip current pixel
526 // But if alpha is not 255, we should change color.
529 for(const Channel& channel : validChannelList)
531 auto color = ReadChannel(&pixel[x], mPixelFormat, channel);
532 WriteChannel(&pixel[x], mPixelFormat, channel, Platform::MultiplyAndNormalizeColor(color, alpha));
537 // If alpha is 0, just set all pixel as zero.
538 memset(&pixel[x], 0, bytesPerPixel);
542 pixel += strideBytes;
545 mPreMultiplied = true;
549 bool PixelBuffer::IsAlphaPreMultiplied() const
551 return mPreMultiplied;
554 uint32_t PixelBuffer::GetBrightness() const
556 uint32_t brightness = 0;
557 uint32_t bytesPerPixel = Pixel::GetBytesPerPixel(mPixelFormat);
559 if(bytesPerPixel && mWidth && mHeight)
561 uint8_t* pixel = mBuffer;
562 const uint32_t strideBytes = mStride * bytesPerPixel;
563 const uint32_t widthBytes = mWidth * bytesPerPixel;
564 const uint32_t bufferSize = mWidth * mHeight;
570 for(uint32_t y = 0; y < mHeight; y++)
572 for(uint32_t x = 0; x < widthBytes; x += bytesPerPixel)
574 red += ReadChannel(&pixel[x], mPixelFormat, Adaptor::RED);
575 green += ReadChannel(&pixel[x], mPixelFormat, Adaptor::GREEN);
576 blue += ReadChannel(&pixel[x], mPixelFormat, Adaptor::BLUE);
578 pixel += strideBytes;
581 // http://www.w3.org/TR/AERT#color-contrast
582 brightness = (red * BRIGHTNESS_CONSTANT_R + green * BRIGHTNESS_CONSTANT_G + blue * BRIGHTNESS_CONSTANT_B) / (1000uLL * bufferSize);
588 } // namespace Adaptor
589 } // namespace Internal