2 * Copyright (c) 2022 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>
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)
108 #if defined(DEBUG_ENABLED)
109 gPixelBufferAllocationTotal -= pixelBuffer.mBufferSize;
111 Dali::PixelData pixelData = Dali::PixelData::New(pixelBuffer.mBuffer,
112 pixelBuffer.mBufferSize,
116 pixelBuffer.mPixelFormat,
117 Dali::PixelData::FREE);
118 pixelBuffer.mBuffer = NULL;
119 pixelBuffer.mWidth = 0;
120 pixelBuffer.mHeight = 0;
121 pixelBuffer.mBufferSize = 0;
122 pixelBuffer.mStride = 0;
127 uint32_t PixelBuffer::GetWidth() const
132 uint32_t PixelBuffer::GetHeight() const
137 uint32_t PixelBuffer::GetStride() const
142 Dali::Pixel::Format PixelBuffer::GetPixelFormat() const
147 uint8_t* PixelBuffer::GetBuffer() const
152 const uint8_t* PixelBuffer::GetConstBuffer() const
157 uint32_t PixelBuffer::GetBufferSize() const
162 Dali::PixelData PixelBuffer::CreatePixelData() const
164 uint8_t* destBuffer = NULL;
168 destBuffer = static_cast<uint8_t*>(malloc(mBufferSize));
169 memcpy(destBuffer, mBuffer, mBufferSize);
172 Dali::PixelData pixelData = Dali::PixelData::New(destBuffer, mBufferSize, mWidth, mHeight, mStride, mPixelFormat, Dali::PixelData::FREE);
176 void PixelBuffer::ApplyMask(const PixelBuffer& inMask, float contentScale, bool cropToMask)
180 // First scale this buffer by the contentScale, and crop to the mask size
181 // If it's too small, then scale the mask to match the image size
182 // Then apply the mask
183 ScaleAndCrop(contentScale, ImageDimensions(inMask.GetWidth(), inMask.GetHeight()));
185 if(inMask.mWidth > mWidth || inMask.mHeight > mHeight)
187 PixelBufferPtr mask = NewResize(inMask, ImageDimensions(mWidth, mHeight));
188 ApplyMaskInternal(*mask);
192 ApplyMaskInternal(inMask);
197 // First, scale the mask to match the image size,
198 // then apply the mask.
199 PixelBufferPtr mask = NewResize(inMask, ImageDimensions(mWidth, mHeight));
200 ApplyMaskInternal(*mask);
204 void PixelBuffer::ApplyMaskInternal(const PixelBuffer& mask)
209 Dali::Pixel::GetAlphaOffsetAndMask(mPixelFormat, byteOffset, bitMask);
210 if(Dali::Pixel::HasAlpha(mPixelFormat) && bitMask == 255)
212 ApplyMaskToAlphaChannel(*this, mask);
216 PixelBufferPtr newPixelBuffer = CreateNewMaskedBuffer(*this, mask);
217 TakeOwnershipOfBuffer(*newPixelBuffer);
218 // On leaving scope, newPixelBuffer will get destroyed.
222 void PixelBuffer::TakeOwnershipOfBuffer(PixelBuffer& pixelBuffer)
226 // Take ownership of new buffer
227 mBuffer = pixelBuffer.mBuffer;
228 pixelBuffer.mBuffer = NULL;
229 mBufferSize = pixelBuffer.mBufferSize;
230 mWidth = pixelBuffer.mWidth;
231 mHeight = pixelBuffer.mHeight;
232 mStride = pixelBuffer.mStride;
233 mPixelFormat = pixelBuffer.mPixelFormat;
236 void PixelBuffer::ReleaseBuffer()
240 #if defined(DEBUG_ENABLED)
241 gPixelBufferAllocationTotal -= mBufferSize;
247 void PixelBuffer::AllocateFixedSize(uint32_t size)
250 mBuffer = reinterpret_cast<unsigned char*>(malloc(size));
252 #if defined(DEBUG_ENABLED)
253 gPixelBufferAllocationTotal += size;
257 bool PixelBuffer::Rotate(Degree angle)
259 // Check first if Rotate() can perform the operation in the current pixel buffer.
261 bool validPixelFormat = false;
270 case Pixel::RGBA8888:
271 case Pixel::BGRA8888: // FALL THROUGH
273 validPixelFormat = true;
278 // This pixel format is not supported for this operation.
279 validPixelFormat = false;
284 if(!validPixelFormat)
286 // Can't rotate the pixel buffer with the current pixel format.
287 DALI_LOG_ERROR("Can't rotate the pixel buffer with the current pixel format\n");
291 float radians = Radian(angle).radian;
293 // Transform the input angle into the range [0..2PI]
294 radians = fmod(radians, TWO_PI);
295 radians += (radians < 0.f) ? TWO_PI : 0.f;
297 if(radians < Dali::Math::MACHINE_EPSILON_10)
299 // Nothing to do if the angle is zero.
303 const unsigned int pixelSize = Pixel::GetBytesPerPixel(mPixelFormat);
305 uint8_t* pixelsOut = nullptr;
306 Platform::RotateByShear(mBuffer,
316 // Check whether the rotation succedded and set the new pixel buffer data.
317 const bool success = nullptr != pixelsOut;
321 // Release the memory of the current pixel buffer.
324 // Set the new pixel buffer.
327 mBufferSize = mWidth * mHeight * pixelSize;
328 mStride = mWidth; // The buffer is tightly packed.
334 void PixelBuffer::ScaleAndCrop(float scaleFactor, ImageDimensions cropDimensions)
336 ImageDimensions outDimensions(float(mWidth) * scaleFactor,
337 float(mHeight) * scaleFactor);
339 if(outDimensions.GetWidth() != mWidth || outDimensions.GetHeight() != mHeight)
341 Resize(outDimensions);
344 ImageDimensions postCropDimensions(
345 std::min(cropDimensions.GetWidth(), outDimensions.GetWidth()),
346 std::min(cropDimensions.GetHeight(), outDimensions.GetHeight()));
348 if(postCropDimensions.GetWidth() < outDimensions.GetWidth() ||
349 postCropDimensions.GetHeight() < outDimensions.GetHeight())
351 uint16_t x = (outDimensions.GetWidth() - postCropDimensions.GetWidth()) / 2;
352 uint16_t y = (outDimensions.GetHeight() - postCropDimensions.GetHeight()) / 2;
353 Crop(x, y, postCropDimensions);
357 void PixelBuffer::Crop(uint16_t x, uint16_t y, ImageDimensions cropDimensions)
359 PixelBufferPtr outBuffer = NewCrop(*this, x, y, cropDimensions);
360 TakeOwnershipOfBuffer(*outBuffer);
363 PixelBufferPtr PixelBuffer::NewCrop(const PixelBuffer& inBuffer, uint16_t x, uint16_t y, ImageDimensions cropDimensions)
365 PixelBufferPtr outBuffer = PixelBuffer::New(cropDimensions.GetWidth(), cropDimensions.GetHeight(), inBuffer.GetPixelFormat());
366 int bytesPerPixel = Pixel::GetBytesPerPixel(inBuffer.mPixelFormat);
367 int srcStride = inBuffer.mStride * bytesPerPixel;
368 int destStride = cropDimensions.GetWidth() * bytesPerPixel; // The destination buffer is tightly packed
370 // Clamp crop to right edge
371 if(x + cropDimensions.GetWidth() > inBuffer.mWidth)
373 destStride = (inBuffer.mWidth - x) * bytesPerPixel;
376 int srcOffset = x * bytesPerPixel + y * srcStride;
378 uint8_t* destBuffer = outBuffer->mBuffer;
380 // Clamp crop to last row
381 uint16_t endRow = y + cropDimensions.GetHeight();
382 if(endRow > inBuffer.mHeight)
384 endRow = inBuffer.mHeight - 1;
386 for(uint16_t row = y; row < endRow; ++row)
388 memcpy(destBuffer + destOffset, inBuffer.mBuffer + srcOffset, destStride);
389 srcOffset += srcStride;
390 destOffset += destStride;
395 void PixelBuffer::SetMetadata(const Property::Map& map)
397 mMetadata.reset(new Property::Map(map));
400 bool PixelBuffer::GetMetadata(Property::Map& outMetadata) const
406 outMetadata = *mMetadata;
410 void PixelBuffer::SetMetadata(std::unique_ptr<Property::Map> metadata)
412 mMetadata = std::move(metadata);
415 void PixelBuffer::Resize(ImageDimensions outDimensions)
417 if(mWidth != outDimensions.GetWidth() || mHeight != outDimensions.GetHeight())
419 PixelBufferPtr outBuffer = NewResize(*this, outDimensions);
420 TakeOwnershipOfBuffer(*outBuffer);
424 PixelBufferPtr PixelBuffer::NewResize(const PixelBuffer& inBuffer, ImageDimensions outDimensions)
426 PixelBufferPtr outBuffer = PixelBuffer::New(outDimensions.GetWidth(), outDimensions.GetHeight(), inBuffer.GetPixelFormat());
427 ImageDimensions inDimensions(inBuffer.mWidth, inBuffer.mHeight);
429 bool hasAlpha = Pixel::HasAlpha(inBuffer.mPixelFormat);
430 int bytesPerPixel = Pixel::GetBytesPerPixel(inBuffer.mPixelFormat);
432 Resampler::Filter filterType = Resampler::LANCZOS4;
433 if(inDimensions.GetWidth() < outDimensions.GetWidth() && inDimensions.GetHeight() < outDimensions.GetHeight())
435 filterType = Resampler::MITCHELL;
438 // This method only really works for 8 bit wide channels.
439 // (But could be expanded to work)
440 if(inBuffer.mPixelFormat == Pixel::A8 ||
441 inBuffer.mPixelFormat == Pixel::L8 ||
442 inBuffer.mPixelFormat == Pixel::LA88 ||
443 inBuffer.mPixelFormat == Pixel::RGB888 ||
444 inBuffer.mPixelFormat == Pixel::RGB8888 ||
445 inBuffer.mPixelFormat == Pixel::BGR8888 ||
446 inBuffer.mPixelFormat == Pixel::RGBA8888 ||
447 inBuffer.mPixelFormat == Pixel::BGRA8888)
449 Dali::Internal::Platform::Resample(inBuffer.mBuffer, inDimensions, inBuffer.mStride, outBuffer->GetBuffer(), outDimensions, filterType, bytesPerPixel, hasAlpha);
453 DALI_LOG_ERROR("Trying to resize an image with too narrow a channel width");
459 void PixelBuffer::ApplyGaussianBlur(const float blurRadius)
461 // This method only works for pixel buffer in RGBA format.
462 if(mWidth > 0 && mHeight > 0 && mPixelFormat == Pixel::RGBA8888)
464 if(blurRadius > Math::MACHINE_EPSILON_1)
466 PerformGaussianBlurRGBA(*this, blurRadius);
471 DALI_LOG_ERROR("Trying to apply gaussian blur to an empty pixel buffer or a pixel buffer not in RGBA format");
475 void PixelBuffer::MultiplyColorByAlpha()
477 auto bytesPerPixel = Pixel::GetBytesPerPixel(mPixelFormat);
479 // Compressed textures have unknown size of the pixel. Alpha premultiplication
480 // must be skipped in such case
481 if(Pixel::GetBytesPerPixel(mPixelFormat) && Pixel::HasAlpha(mPixelFormat))
483 uint8_t* pixel = mBuffer;
484 const uint32_t strideBytes = mStride * bytesPerPixel;
485 const uint32_t widthBytes = mWidth * bytesPerPixel;
487 // Collect all valid channel list before lookup whole buffer
488 std::vector<Channel> validChannelList;
489 for(const Channel& channel : {Adaptor::RED, Adaptor::GREEN, Adaptor::BLUE, Adaptor::LUMINANCE})
491 if(HasChannel(mPixelFormat, channel))
493 validChannelList.emplace_back(channel);
497 if(DALI_LIKELY(!validChannelList.empty()))
499 for(uint32_t y = 0; y < mHeight; y++)
501 for(uint32_t x = 0; x < widthBytes; x += bytesPerPixel)
503 uint32_t alpha = ReadChannel(&pixel[x], mPixelFormat, Adaptor::ALPHA);
506 // If alpha is 255, we don't need to change color. Skip current pixel
507 // But if alpha is not 255, we should change color.
510 for(const Channel& channel : validChannelList)
512 auto color = ReadChannel(&pixel[x], mPixelFormat, channel);
513 WriteChannel(&pixel[x], mPixelFormat, channel, Platform::MultiplyAndNormalizeColor(color, alpha));
518 // If alpha is 0, just set all pixel as zero.
519 memset(&pixel[x], 0, bytesPerPixel);
523 pixel += strideBytes;
526 mPreMultiplied = true;
530 bool PixelBuffer::IsAlphaPreMultiplied() const
532 return mPreMultiplied;
535 uint32_t PixelBuffer::GetBrightness() const
537 uint32_t brightness = 0;
538 uint32_t bytesPerPixel = Pixel::GetBytesPerPixel(mPixelFormat);
540 if(bytesPerPixel && mWidth && mHeight)
542 uint8_t* pixel = mBuffer;
543 const uint32_t strideBytes = mStride * bytesPerPixel;
544 const uint32_t widthBytes = mWidth * bytesPerPixel;
545 const uint32_t bufferSize = mWidth * mHeight;
551 for(uint32_t y = 0; y < mHeight; y++)
553 for(uint32_t x = 0; x < widthBytes; x += bytesPerPixel)
555 red += ReadChannel(&pixel[x], mPixelFormat, Adaptor::RED);
556 green += ReadChannel(&pixel[x], mPixelFormat, Adaptor::GREEN);
557 blue += ReadChannel(&pixel[x], mPixelFormat, Adaptor::BLUE);
559 pixel += strideBytes;
562 // http://www.w3.org/TR/AERT#color-contrast
563 brightness = (red * BRIGHTNESS_CONSTANT_R + green * BRIGHTNESS_CONSTANT_G + blue * BRIGHTNESS_CONSTANT_B) / (1000uLL * bufferSize);
569 } // namespace Adaptor
570 } // namespace Internal