2 * Copyright (c) 2021 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/internal/imaging/common/alpha-mask.h>
27 #include <dali/internal/imaging/common/gaussian-blur.h>
28 #include <dali/internal/imaging/common/image-operations.h>
29 #include <dali/internal/imaging/common/pixel-manipulation.h>
39 const float TWO_PI = 2.f * Math::PI; ///< 360 degrees in radians
40 // based on W3C Recommendations (https://www.w3.org/TR/AERT/#color-contrast)
41 constexpr uint32_t BRIGHTNESS_CONSTANT_R = 299;
42 constexpr uint32_t BRIGHTNESS_CONSTANT_G = 587;
43 constexpr uint32_t BRIGHTNESS_CONSTANT_B = 114;
46 PixelBuffer::PixelBuffer(unsigned char* buffer,
47 unsigned int bufferSize,
50 Dali::Pixel::Format pixelFormat)
53 mBufferSize(bufferSize),
56 mPixelFormat(pixelFormat),
61 PixelBuffer::~PixelBuffer()
66 PixelBufferPtr PixelBuffer::New(unsigned int width,
68 Dali::Pixel::Format pixelFormat)
70 unsigned int bufferSize = width * height * Dali::Pixel::GetBytesPerPixel(pixelFormat);
71 unsigned char* buffer = NULL;
74 buffer = static_cast<unsigned char*>(malloc(bufferSize));
76 return new PixelBuffer(buffer, bufferSize, width, height, pixelFormat);
79 PixelBufferPtr PixelBuffer::New(unsigned char* buffer,
80 unsigned int bufferSize,
83 Dali::Pixel::Format pixelFormat)
85 return new PixelBuffer(buffer, bufferSize, width, height, pixelFormat);
88 Dali::PixelData PixelBuffer::Convert(PixelBuffer& pixelBuffer)
90 Dali::PixelData pixelData = Dali::PixelData::New(pixelBuffer.mBuffer,
91 pixelBuffer.mBufferSize,
94 pixelBuffer.mPixelFormat,
95 Dali::PixelData::FREE);
96 pixelBuffer.mBuffer = NULL;
97 pixelBuffer.mWidth = 0;
98 pixelBuffer.mHeight = 0;
99 pixelBuffer.mBufferSize = 0;
104 unsigned int PixelBuffer::GetWidth() const
109 unsigned int PixelBuffer::GetHeight() const
114 Dali::Pixel::Format PixelBuffer::GetPixelFormat() const
119 unsigned char* PixelBuffer::GetBuffer() const
124 const unsigned char* const PixelBuffer::GetConstBuffer() const
129 unsigned int PixelBuffer::GetBufferSize() const
134 Dali::PixelData PixelBuffer::CreatePixelData() const
136 unsigned char* destBuffer = NULL;
140 destBuffer = static_cast<unsigned char*>(malloc(mBufferSize));
141 memcpy(destBuffer, mBuffer, mBufferSize);
144 Dali::PixelData pixelData = Dali::PixelData::New(destBuffer, mBufferSize, mWidth, mHeight, mPixelFormat, Dali::PixelData::FREE);
148 void PixelBuffer::ApplyMask(const PixelBuffer& inMask, float contentScale, bool cropToMask)
152 // First scale this buffer by the contentScale, and crop to the mask size
153 // If it's too small, then scale the mask to match the image size
154 // Then apply the mask
155 ScaleAndCrop(contentScale, ImageDimensions(inMask.GetWidth(), inMask.GetHeight()));
157 if(inMask.mWidth > mWidth || inMask.mHeight > mHeight)
159 PixelBufferPtr mask = NewResize(inMask, ImageDimensions(mWidth, mHeight));
160 ApplyMaskInternal(*mask);
164 ApplyMaskInternal(inMask);
169 // First, scale the mask to match the image size,
170 // then apply the mask.
171 PixelBufferPtr mask = NewResize(inMask, ImageDimensions(mWidth, mHeight));
172 ApplyMaskInternal(*mask);
176 void PixelBuffer::ApplyMaskInternal(const PixelBuffer& mask)
181 Dali::Pixel::GetAlphaOffsetAndMask(mPixelFormat, byteOffset, bitMask);
182 if(Dali::Pixel::HasAlpha(mPixelFormat) && bitMask == 255)
184 ApplyMaskToAlphaChannel(*this, mask);
188 PixelBufferPtr newPixelBuffer = CreateNewMaskedBuffer(*this, mask);
189 TakeOwnershipOfBuffer(*newPixelBuffer);
190 // On leaving scope, newPixelBuffer will get destroyed.
194 void PixelBuffer::TakeOwnershipOfBuffer(PixelBuffer& pixelBuffer)
198 // Take ownership of new buffer
199 mBuffer = pixelBuffer.mBuffer;
200 pixelBuffer.mBuffer = NULL;
201 mBufferSize = pixelBuffer.mBufferSize;
202 mWidth = pixelBuffer.mWidth;
203 mHeight = pixelBuffer.mHeight;
204 mPixelFormat = pixelBuffer.mPixelFormat;
207 void PixelBuffer::ReleaseBuffer()
215 void PixelBuffer::AllocateFixedSize(uint32_t size)
218 mBuffer = reinterpret_cast<unsigned char*>(malloc(size));
222 bool PixelBuffer::Rotate(Degree angle)
224 // Check first if Rotate() can perform the operation in the current pixel buffer.
226 bool validPixelFormat = false;
235 case Pixel::RGBA8888:
236 case Pixel::BGRA8888: // FALL THROUGH
238 validPixelFormat = true;
243 // This pixel format is not supported for this operation.
244 validPixelFormat = false;
249 if(!validPixelFormat)
251 // Can't rotate the pixel buffer with the current pixel format.
252 DALI_LOG_ERROR("Can't rotate the pixel buffer with the current pixel format\n");
256 float radians = Radian(angle).radian;
258 // Transform the input angle into the range [0..2PI]
259 radians = fmod(radians, TWO_PI);
260 radians += (radians < 0.f) ? TWO_PI : 0.f;
262 if(radians < Dali::Math::MACHINE_EPSILON_10)
264 // Nothing to do if the angle is zero.
268 const unsigned int pixelSize = Pixel::GetBytesPerPixel(mPixelFormat);
270 uint8_t* pixelsOut = nullptr;
271 Platform::RotateByShear(mBuffer,
280 // Check whether the rotation succedded and set the new pixel buffer data.
281 const bool success = nullptr != pixelsOut;
285 // Release the memory of the current pixel buffer.
288 // Set the new pixel buffer.
291 mBufferSize = mWidth * mHeight * pixelSize;
297 void PixelBuffer::ScaleAndCrop(float scaleFactor, ImageDimensions cropDimensions)
299 ImageDimensions outDimensions(float(mWidth) * scaleFactor,
300 float(mHeight) * scaleFactor);
302 if(outDimensions.GetWidth() != mWidth || outDimensions.GetHeight() != mHeight)
304 Resize(outDimensions);
307 ImageDimensions postCropDimensions(
308 std::min(cropDimensions.GetWidth(), outDimensions.GetWidth()),
309 std::min(cropDimensions.GetHeight(), outDimensions.GetHeight()));
311 if(postCropDimensions.GetWidth() < outDimensions.GetWidth() ||
312 postCropDimensions.GetHeight() < outDimensions.GetHeight())
314 uint16_t x = (outDimensions.GetWidth() - postCropDimensions.GetWidth()) / 2;
315 uint16_t y = (outDimensions.GetHeight() - postCropDimensions.GetHeight()) / 2;
316 Crop(x, y, postCropDimensions);
320 void PixelBuffer::Crop(uint16_t x, uint16_t y, ImageDimensions cropDimensions)
322 PixelBufferPtr outBuffer = NewCrop(*this, x, y, cropDimensions);
323 TakeOwnershipOfBuffer(*outBuffer);
326 PixelBufferPtr PixelBuffer::NewCrop(const PixelBuffer& inBuffer, uint16_t x, uint16_t y, ImageDimensions cropDimensions)
328 PixelBufferPtr outBuffer = PixelBuffer::New(cropDimensions.GetWidth(), cropDimensions.GetHeight(), inBuffer.GetPixelFormat());
329 int bytesPerPixel = Pixel::GetBytesPerPixel(inBuffer.mPixelFormat);
330 int srcStride = inBuffer.mWidth * bytesPerPixel;
331 int destStride = cropDimensions.GetWidth() * bytesPerPixel;
333 // Clamp crop to right edge
334 if(x + cropDimensions.GetWidth() > inBuffer.mWidth)
336 destStride = (inBuffer.mWidth - x) * bytesPerPixel;
339 int srcOffset = x * bytesPerPixel + y * srcStride;
341 unsigned char* destBuffer = outBuffer->mBuffer;
343 // Clamp crop to last row
344 unsigned int endRow = y + cropDimensions.GetHeight();
345 if(endRow > inBuffer.mHeight)
347 endRow = inBuffer.mHeight - 1;
349 for(uint16_t row = y; row < endRow; ++row)
351 memcpy(destBuffer + destOffset, inBuffer.mBuffer + srcOffset, destStride);
352 srcOffset += srcStride;
353 destOffset += destStride;
358 void PixelBuffer::SetMetadata(const Property::Map& map)
360 mMetadata.reset(new Property::Map(map));
363 bool PixelBuffer::GetMetadata(Property::Map& outMetadata) const
369 outMetadata = *mMetadata;
373 void PixelBuffer::SetMetadata(std::unique_ptr<Property::Map> metadata)
375 mMetadata = std::move(metadata);
378 void PixelBuffer::Resize(ImageDimensions outDimensions)
380 if(mWidth != outDimensions.GetWidth() || mHeight != outDimensions.GetHeight())
382 PixelBufferPtr outBuffer = NewResize(*this, outDimensions);
383 TakeOwnershipOfBuffer(*outBuffer);
387 PixelBufferPtr PixelBuffer::NewResize(const PixelBuffer& inBuffer, ImageDimensions outDimensions)
389 PixelBufferPtr outBuffer = PixelBuffer::New(outDimensions.GetWidth(), outDimensions.GetHeight(), inBuffer.GetPixelFormat());
390 ImageDimensions inDimensions(inBuffer.mWidth, inBuffer.mHeight);
392 bool hasAlpha = Pixel::HasAlpha(inBuffer.mPixelFormat);
393 int bytesPerPixel = Pixel::GetBytesPerPixel(inBuffer.mPixelFormat);
395 Resampler::Filter filterType = Resampler::LANCZOS4;
396 if(inDimensions.GetWidth() < outDimensions.GetWidth() && inDimensions.GetHeight() < outDimensions.GetHeight())
398 filterType = Resampler::MITCHELL;
401 // This method only really works for 8 bit wide channels.
402 // (But could be expanded to work)
403 if(inBuffer.mPixelFormat == Pixel::A8 ||
404 inBuffer.mPixelFormat == Pixel::L8 ||
405 inBuffer.mPixelFormat == Pixel::LA88 ||
406 inBuffer.mPixelFormat == Pixel::RGB888 ||
407 inBuffer.mPixelFormat == Pixel::RGB8888 ||
408 inBuffer.mPixelFormat == Pixel::BGR8888 ||
409 inBuffer.mPixelFormat == Pixel::RGBA8888 ||
410 inBuffer.mPixelFormat == Pixel::BGRA8888)
412 Dali::Internal::Platform::Resample(inBuffer.mBuffer, inDimensions, outBuffer->GetBuffer(), outDimensions, filterType, bytesPerPixel, hasAlpha);
416 DALI_LOG_ERROR("Trying to resize an image with too narrow a channel width");
422 void PixelBuffer::ApplyGaussianBlur(const float blurRadius)
424 // This method only works for pixel buffer in RGBA format.
425 if(mWidth > 0 && mHeight > 0 && mPixelFormat == Pixel::RGBA8888)
427 if(blurRadius > Math::MACHINE_EPSILON_1)
429 PerformGaussianBlurRGBA(*this, blurRadius);
434 DALI_LOG_ERROR("Trying to apply gaussian blur to an empty pixel buffer or a pixel buffer not in RGBA format");
438 void PixelBuffer::MultiplyColorByAlpha()
440 auto bytesPerPixel = Pixel::GetBytesPerPixel(mPixelFormat);
442 // Compressed textures have unknown size of the pixel. Alpha premultiplication
443 // must be skipped in such case
444 if(Pixel::GetBytesPerPixel(mPixelFormat) && Pixel::HasAlpha(mPixelFormat))
446 unsigned char* pixel = mBuffer;
447 const unsigned int bufferSize = mWidth * mHeight;
449 for(unsigned int i = 0; i < bufferSize; ++i)
451 unsigned int alpha = ReadChannel(pixel, mPixelFormat, Adaptor::ALPHA);
453 auto red = ReadChannel(pixel, mPixelFormat, Adaptor::RED);
454 auto green = ReadChannel(pixel, mPixelFormat, Adaptor::GREEN);
455 auto blue = ReadChannel(pixel, mPixelFormat, Adaptor::BLUE);
456 auto luminance = ReadChannel(pixel, mPixelFormat, Adaptor::LUMINANCE);
457 WriteChannel(pixel, mPixelFormat, Adaptor::RED, red * alpha / 255);
458 WriteChannel(pixel, mPixelFormat, Adaptor::GREEN, green * alpha / 255);
459 WriteChannel(pixel, mPixelFormat, Adaptor::BLUE, blue * alpha / 255);
460 WriteChannel(pixel, mPixelFormat, Adaptor::LUMINANCE, luminance * alpha / 255);
462 pixel += bytesPerPixel;
465 mPreMultiplied = true;
468 bool PixelBuffer::IsAlphaPreMultiplied() const
470 return mPreMultiplied;
473 uint32_t PixelBuffer::GetBrightness() const
475 uint32_t brightness = 0;
477 uint32_t bytesPerPixel = Pixel::GetBytesPerPixel(mPixelFormat);
480 unsigned char* pixel = mBuffer;
481 const uint32_t bufferSize = mWidth * mHeight;
486 for(uint32_t i = 0; i < bufferSize; ++i)
488 red += ReadChannel(pixel, mPixelFormat, Adaptor::RED);
489 green += ReadChannel(pixel, mPixelFormat, Adaptor::GREEN);
490 blue += ReadChannel(pixel, mPixelFormat, Adaptor::BLUE);
491 pixel += bytesPerPixel;
493 // http://www.w3.org/TR/AERT#color-contrast
494 brightness = (red * BRIGHTNESS_CONSTANT_R + green * BRIGHTNESS_CONSTANT_G + blue * BRIGHTNESS_CONSTANT_B) / (1000uLL * bufferSize);
500 } // namespace Adaptor
501 } // namespace Internal