2 * Copyright (c) 2023 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>
26 #include <dali/integration-api/debug.h>
27 #include <dali/internal/imaging/common/alpha-mask.h>
28 #include <dali/internal/imaging/common/gaussian-blur.h>
29 #include <dali/internal/imaging/common/image-operations.h>
30 #include <dali/internal/imaging/common/pixel-manipulation.h>
40 #if defined(DEBUG_ENABLED)
41 Debug::Filter* gPixelBufferFilter = Debug::Filter::New(Debug::NoLogging, false, "DALI_LOG_PIXEL_BUFFER_SIZE");
44 const float TWO_PI = 2.f * Math::PI; ///< 360 degrees in radians
45 // based on W3C Recommendations (https://www.w3.org/TR/AERT/#color-contrast)
46 constexpr uint32_t BRIGHTNESS_CONSTANT_R = 299;
47 constexpr uint32_t BRIGHTNESS_CONSTANT_G = 587;
48 constexpr uint32_t BRIGHTNESS_CONSTANT_B = 114;
51 #if defined(DEBUG_ENABLED)
52 uint32_t PixelBuffer::gPixelBufferAllocationTotal{0};
55 PixelBuffer::PixelBuffer(uint8_t* buffer,
60 Dali::Pixel::Format pixelFormat)
63 mBufferSize(bufferSize),
66 mStride(stride ? stride : width),
67 mPixelFormat(pixelFormat),
72 PixelBuffer::~PixelBuffer()
77 PixelBufferPtr PixelBuffer::New(uint32_t width,
79 Dali::Pixel::Format pixelFormat)
81 uint32_t bufferSize = width * height * Dali::Pixel::GetBytesPerPixel(pixelFormat);
82 uint8_t* buffer = NULL;
85 buffer = static_cast<uint8_t*>(malloc(bufferSize));
86 #if defined(DEBUG_ENABLED)
87 gPixelBufferAllocationTotal += bufferSize;
90 DALI_LOG_INFO(gPixelBufferFilter, Debug::Concise, "Allocated PixelBuffer of size %u\n", bufferSize);
92 return new PixelBuffer(buffer, bufferSize, width, height, width, pixelFormat);
95 PixelBufferPtr PixelBuffer::New(uint8_t* buffer,
100 Dali::Pixel::Format pixelFormat)
102 return new PixelBuffer(buffer, bufferSize, width, height, stride, pixelFormat);
105 Dali::PixelData PixelBuffer::Convert(PixelBuffer& pixelBuffer)
107 #if defined(DEBUG_ENABLED)
108 gPixelBufferAllocationTotal -= pixelBuffer.mBufferSize;
110 Dali::PixelData pixelData = Dali::PixelData::New(pixelBuffer.mBuffer,
111 pixelBuffer.mBufferSize,
115 pixelBuffer.mPixelFormat,
116 Dali::PixelData::FREE);
117 pixelBuffer.mBuffer = NULL;
118 pixelBuffer.mWidth = 0;
119 pixelBuffer.mHeight = 0;
120 pixelBuffer.mBufferSize = 0;
121 pixelBuffer.mStride = 0;
126 uint32_t PixelBuffer::GetWidth() const
131 uint32_t PixelBuffer::GetHeight() const
136 uint32_t PixelBuffer::GetStride() const
141 Dali::Pixel::Format PixelBuffer::GetPixelFormat() const
146 uint8_t* PixelBuffer::GetBuffer() const
151 const uint8_t* PixelBuffer::GetConstBuffer() const
156 uint32_t PixelBuffer::GetBufferSize() const
161 Dali::PixelData PixelBuffer::CreatePixelData() const
163 uint8_t* destBuffer = NULL;
167 destBuffer = static_cast<uint8_t*>(malloc(mBufferSize));
168 memcpy(destBuffer, mBuffer, mBufferSize);
171 Dali::PixelData pixelData = Dali::PixelData::New(destBuffer, mBufferSize, mWidth, mHeight, mStride, mPixelFormat, Dali::PixelData::FREE);
175 void PixelBuffer::ApplyMask(const PixelBuffer& inMask, float contentScale, bool cropToMask)
179 // First scale this buffer by the contentScale, and crop to the mask size
180 // If it's too small, then scale the mask to match the image size
181 // Then apply the mask
182 ScaleAndCrop(contentScale, ImageDimensions(inMask.GetWidth(), inMask.GetHeight()));
184 if(inMask.mWidth > mWidth || inMask.mHeight > mHeight)
186 PixelBufferPtr mask = NewResize(inMask, ImageDimensions(mWidth, mHeight));
187 ApplyMaskInternal(*mask);
191 ApplyMaskInternal(inMask);
196 // First, scale the mask to match the image size,
197 // then apply the mask.
198 PixelBufferPtr mask = NewResize(inMask, ImageDimensions(mWidth, mHeight));
199 ApplyMaskInternal(*mask);
203 void PixelBuffer::ApplyMaskInternal(const PixelBuffer& mask)
208 Dali::Pixel::GetAlphaOffsetAndMask(mPixelFormat, byteOffset, bitMask);
209 if(Dali::Pixel::HasAlpha(mPixelFormat) && bitMask == 255)
211 ApplyMaskToAlphaChannel(*this, mask);
215 PixelBufferPtr newPixelBuffer = CreateNewMaskedBuffer(*this, mask);
216 TakeOwnershipOfBuffer(*newPixelBuffer);
217 // On leaving scope, newPixelBuffer will get destroyed.
221 void PixelBuffer::TakeOwnershipOfBuffer(PixelBuffer& pixelBuffer)
225 // Take ownership of new buffer
226 mBuffer = pixelBuffer.mBuffer;
227 pixelBuffer.mBuffer = NULL;
228 mBufferSize = pixelBuffer.mBufferSize;
229 mWidth = pixelBuffer.mWidth;
230 mHeight = pixelBuffer.mHeight;
231 mStride = pixelBuffer.mStride;
232 mPixelFormat = pixelBuffer.mPixelFormat;
235 void PixelBuffer::ReleaseBuffer()
239 #if defined(DEBUG_ENABLED)
240 gPixelBufferAllocationTotal -= mBufferSize;
246 void PixelBuffer::AllocateFixedSize(uint32_t size)
249 mBuffer = reinterpret_cast<unsigned char*>(malloc(size));
251 #if defined(DEBUG_ENABLED)
252 gPixelBufferAllocationTotal += size;
256 bool PixelBuffer::Rotate(Degree angle)
258 // Check first if Rotate() can perform the operation in the current pixel buffer.
260 bool validPixelFormat = false;
269 case Pixel::RGBA8888:
270 case Pixel::BGRA8888: // FALL THROUGH
272 validPixelFormat = true;
277 // This pixel format is not supported for this operation.
278 validPixelFormat = false;
283 if(!validPixelFormat)
285 // Can't rotate the pixel buffer with the current pixel format.
286 DALI_LOG_ERROR("Can't rotate the pixel buffer with the current pixel format\n");
290 float radians = Radian(angle).radian;
292 // Transform the input angle into the range [0..2PI]
293 radians = fmod(radians, TWO_PI);
294 radians += (radians < 0.f) ? TWO_PI : 0.f;
296 if(radians < Dali::Math::MACHINE_EPSILON_10)
298 // Nothing to do if the angle is zero.
302 const unsigned int pixelSize = Pixel::GetBytesPerPixel(mPixelFormat);
304 uint8_t* pixelsOut = nullptr;
305 Platform::RotateByShear(mBuffer,
315 // Check whether the rotation succedded and set the new pixel buffer data.
316 const bool success = nullptr != pixelsOut;
320 // Release the memory of the current pixel buffer.
323 // Set the new pixel buffer.
326 mBufferSize = mWidth * mHeight * pixelSize;
327 mStride = mWidth; // The buffer is tightly packed.
333 void PixelBuffer::ScaleAndCrop(float scaleFactor, ImageDimensions cropDimensions)
335 ImageDimensions outDimensions(float(mWidth) * scaleFactor,
336 float(mHeight) * scaleFactor);
338 if(outDimensions.GetWidth() != mWidth || outDimensions.GetHeight() != mHeight)
340 Resize(outDimensions);
343 ImageDimensions postCropDimensions(
344 std::min(cropDimensions.GetWidth(), outDimensions.GetWidth()),
345 std::min(cropDimensions.GetHeight(), outDimensions.GetHeight()));
347 if(postCropDimensions.GetWidth() < outDimensions.GetWidth() ||
348 postCropDimensions.GetHeight() < outDimensions.GetHeight())
350 uint16_t x = (outDimensions.GetWidth() - postCropDimensions.GetWidth()) / 2;
351 uint16_t y = (outDimensions.GetHeight() - postCropDimensions.GetHeight()) / 2;
352 Crop(x, y, postCropDimensions);
356 void PixelBuffer::Crop(uint16_t x, uint16_t y, ImageDimensions cropDimensions)
358 PixelBufferPtr outBuffer = NewCrop(*this, x, y, cropDimensions);
359 TakeOwnershipOfBuffer(*outBuffer);
362 PixelBufferPtr PixelBuffer::NewCrop(const PixelBuffer& inBuffer, uint16_t x, uint16_t y, ImageDimensions cropDimensions)
364 PixelBufferPtr outBuffer = PixelBuffer::New(cropDimensions.GetWidth(), cropDimensions.GetHeight(), inBuffer.GetPixelFormat());
365 int bytesPerPixel = Pixel::GetBytesPerPixel(inBuffer.mPixelFormat);
366 int srcStride = inBuffer.mStride * bytesPerPixel;
367 int destStride = cropDimensions.GetWidth() * bytesPerPixel; // The destination buffer is tightly packed
369 // Clamp crop to right edge
370 if(x + cropDimensions.GetWidth() > inBuffer.mWidth)
372 destStride = (inBuffer.mWidth - x) * bytesPerPixel;
375 int srcOffset = x * bytesPerPixel + y * srcStride;
377 uint8_t* destBuffer = outBuffer->mBuffer;
379 // Clamp crop to last row
380 uint16_t endRow = y + cropDimensions.GetHeight();
381 if(endRow > inBuffer.mHeight)
383 endRow = inBuffer.mHeight - 1;
385 for(uint16_t row = y; row < endRow; ++row)
387 memcpy(destBuffer + destOffset, inBuffer.mBuffer + srcOffset, destStride);
388 srcOffset += srcStride;
389 destOffset += destStride;
394 void PixelBuffer::SetMetadata(const Property::Map& map)
396 mMetadata.reset(new Property::Map(map));
399 bool PixelBuffer::GetMetadata(Property::Map& outMetadata) const
405 outMetadata = *mMetadata;
409 void PixelBuffer::SetMetadata(std::unique_ptr<Property::Map> metadata)
411 mMetadata = std::move(metadata);
414 void PixelBuffer::Resize(ImageDimensions outDimensions)
416 if(mWidth != outDimensions.GetWidth() || mHeight != outDimensions.GetHeight())
418 PixelBufferPtr outBuffer = NewResize(*this, outDimensions);
419 TakeOwnershipOfBuffer(*outBuffer);
423 PixelBufferPtr PixelBuffer::NewResize(const PixelBuffer& inBuffer, ImageDimensions outDimensions)
425 PixelBufferPtr outBuffer = PixelBuffer::New(outDimensions.GetWidth(), outDimensions.GetHeight(), inBuffer.GetPixelFormat());
426 ImageDimensions inDimensions(inBuffer.mWidth, inBuffer.mHeight);
428 bool hasAlpha = Pixel::HasAlpha(inBuffer.mPixelFormat);
429 int bytesPerPixel = Pixel::GetBytesPerPixel(inBuffer.mPixelFormat);
431 Resampler::Filter filterType = Resampler::LANCZOS4;
432 if(inDimensions.GetWidth() < outDimensions.GetWidth() && inDimensions.GetHeight() < outDimensions.GetHeight())
434 filterType = Resampler::MITCHELL;
437 // This method only really works for 8 bit wide channels.
438 // (But could be expanded to work)
439 if(inBuffer.mPixelFormat == Pixel::A8 ||
440 inBuffer.mPixelFormat == Pixel::L8 ||
441 inBuffer.mPixelFormat == Pixel::LA88 ||
442 inBuffer.mPixelFormat == Pixel::RGB888 ||
443 inBuffer.mPixelFormat == Pixel::RGB8888 ||
444 inBuffer.mPixelFormat == Pixel::BGR8888 ||
445 inBuffer.mPixelFormat == Pixel::RGBA8888 ||
446 inBuffer.mPixelFormat == Pixel::BGRA8888)
448 Dali::Internal::Platform::Resample(inBuffer.mBuffer, inDimensions, inBuffer.mStride, outBuffer->GetBuffer(), outDimensions, filterType, bytesPerPixel, hasAlpha);
452 DALI_LOG_ERROR("Trying to resize an image with too narrow a channel width");
458 void PixelBuffer::ApplyGaussianBlur(const float blurRadius)
460 // This method only works for pixel buffer in RGBA format.
461 if(mWidth > 0 && mHeight > 0 && mPixelFormat == Pixel::RGBA8888)
463 if(blurRadius > Math::MACHINE_EPSILON_1)
465 PerformGaussianBlurRGBA(*this, blurRadius);
470 DALI_LOG_ERROR("Trying to apply gaussian blur to an empty pixel buffer or a pixel buffer not in RGBA format");
474 void PixelBuffer::MultiplyColorByAlpha()
476 // Compressed textures have unknown size of the pixel. Alpha premultiplication
477 // must be skipped in such case
478 if(!Pixel::IsCompressed(mPixelFormat) && Pixel::HasAlpha(mPixelFormat))
480 auto bytesPerPixel = Pixel::GetBytesPerPixel(mPixelFormat);
482 uint8_t* pixel = mBuffer;
483 const uint32_t strideBytes = mStride * bytesPerPixel;
484 const uint32_t widthBytes = mWidth * bytesPerPixel;
486 // Collect all valid channel list before lookup whole buffer
487 std::vector<Channel> validChannelList;
488 for(const Channel& channel : {Adaptor::RED, Adaptor::GREEN, Adaptor::BLUE, Adaptor::LUMINANCE})
490 if(HasChannel(mPixelFormat, channel))
492 validChannelList.emplace_back(channel);
496 if(DALI_LIKELY(!validChannelList.empty()))
498 for(uint32_t y = 0; y < mHeight; y++)
500 for(uint32_t x = 0; x < widthBytes; x += bytesPerPixel)
502 uint32_t alpha = ReadChannel(&pixel[x], mPixelFormat, Adaptor::ALPHA);
505 // If alpha is 255, we don't need to change color. Skip current pixel
506 // But if alpha is not 255, we should change color.
509 for(const Channel& channel : validChannelList)
511 auto color = ReadChannel(&pixel[x], mPixelFormat, channel);
512 WriteChannel(&pixel[x], mPixelFormat, channel, Platform::MultiplyAndNormalizeColor(color, alpha));
517 // If alpha is 0, just set all pixel as zero.
518 memset(&pixel[x], 0, bytesPerPixel);
522 pixel += strideBytes;
525 mPreMultiplied = true;
529 bool PixelBuffer::IsAlphaPreMultiplied() const
531 return mPreMultiplied;
534 uint32_t PixelBuffer::GetBrightness() const
536 uint32_t brightness = 0;
537 uint32_t bytesPerPixel = Pixel::GetBytesPerPixel(mPixelFormat);
539 if(bytesPerPixel && mWidth && mHeight)
541 uint8_t* pixel = mBuffer;
542 const uint32_t strideBytes = mStride * bytesPerPixel;
543 const uint32_t widthBytes = mWidth * bytesPerPixel;
544 const uint32_t bufferSize = mWidth * mHeight;
550 for(uint32_t y = 0; y < mHeight; y++)
552 for(uint32_t x = 0; x < widthBytes; x += bytesPerPixel)
554 red += ReadChannel(&pixel[x], mPixelFormat, Adaptor::RED);
555 green += ReadChannel(&pixel[x], mPixelFormat, Adaptor::GREEN);
556 blue += ReadChannel(&pixel[x], mPixelFormat, Adaptor::BLUE);
558 pixel += strideBytes;
561 // http://www.w3.org/TR/AERT#color-contrast
562 brightness = (red * BRIGHTNESS_CONSTANT_R + green * BRIGHTNESS_CONSTANT_G + blue * BRIGHTNESS_CONSTANT_B) / (1000uLL * bufferSize);
568 } // namespace Adaptor
569 } // namespace Internal