2 * Copyright (c) 2018 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/pixel-manipulation.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>
42 const float TWO_PI = 2.f * Math::PI; ///< 360 degrees in radians
45 PixelBuffer::PixelBuffer( unsigned char* buffer,
46 unsigned int bufferSize,
49 Dali::Pixel::Format pixelFormat )
52 mBufferSize( bufferSize ),
55 mPixelFormat( pixelFormat )
59 PixelBuffer::~PixelBuffer()
64 PixelBufferPtr PixelBuffer::New( unsigned int width,
66 Dali::Pixel::Format pixelFormat )
68 unsigned int bufferSize = width * height * Dali::Pixel::GetBytesPerPixel( pixelFormat );
69 unsigned char* buffer = NULL;
72 buffer = static_cast<unsigned char*>( malloc ( bufferSize ) );
74 return new PixelBuffer( buffer, bufferSize, width, height, pixelFormat );
77 PixelBufferPtr PixelBuffer::New( unsigned char* buffer,
78 unsigned int bufferSize,
81 Dali::Pixel::Format pixelFormat )
83 return new PixelBuffer( buffer, bufferSize, width, height, pixelFormat );
86 Dali::PixelData PixelBuffer::Convert( PixelBuffer& pixelBuffer )
88 Dali::PixelData pixelData = Dali::PixelData::New( pixelBuffer.mBuffer,
89 pixelBuffer.mBufferSize,
92 pixelBuffer.mPixelFormat,
93 Dali::PixelData::FREE );
94 pixelBuffer.mBuffer = NULL;
95 pixelBuffer.mWidth = 0;
96 pixelBuffer.mHeight = 0;
97 pixelBuffer.mBufferSize = 0;
102 unsigned int PixelBuffer::GetWidth() const
107 unsigned int PixelBuffer::GetHeight() const
112 Dali::Pixel::Format PixelBuffer::GetPixelFormat() const
117 unsigned char* PixelBuffer::GetBuffer() const
122 unsigned int PixelBuffer::GetBufferSize() const
127 Dali::PixelData PixelBuffer::CreatePixelData() const
129 unsigned char* destBuffer = NULL;
131 if( mBufferSize > 0 )
133 destBuffer = static_cast<unsigned char*>( malloc( mBufferSize ) );
134 memcpy( destBuffer, mBuffer, mBufferSize );
137 Dali::PixelData pixelData = Dali::PixelData::New( destBuffer, mBufferSize,
140 Dali::PixelData::FREE );
144 void PixelBuffer::ApplyMask( const PixelBuffer& inMask, float contentScale, bool cropToMask )
148 // First scale this buffer by the contentScale, and crop to the mask size
149 // If it's too small, then scale the mask to match the image size
150 // Then apply the mask
151 ScaleAndCrop( contentScale, ImageDimensions( inMask.GetWidth(), inMask.GetHeight() ) );
153 if( inMask.mWidth > mWidth || inMask.mHeight > mHeight )
155 PixelBufferPtr mask = NewResize( inMask, ImageDimensions( mWidth, mHeight ) );
156 ApplyMaskInternal( *mask );
160 ApplyMaskInternal( inMask );
165 // First, scale the mask to match the image size,
166 // then apply the mask.
167 PixelBufferPtr mask = NewResize( inMask, ImageDimensions( mWidth, mHeight ) );
168 ApplyMaskInternal( *mask );
172 void PixelBuffer::ApplyMaskInternal( const PixelBuffer& mask )
177 Dali::Pixel::GetAlphaOffsetAndMask(mPixelFormat, byteOffset, bitMask);
178 if( Dali::Pixel::HasAlpha( mPixelFormat ) && bitMask == 255 )
180 ApplyMaskToAlphaChannel( *this, mask );
184 PixelBufferPtr newPixelBuffer = CreateNewMaskedBuffer( *this, mask );
185 TakeOwnershipOfBuffer( *newPixelBuffer );
186 // On leaving scope, newPixelBuffer will get destroyed.
190 void PixelBuffer::TakeOwnershipOfBuffer( PixelBuffer& pixelBuffer )
194 // Take ownership of new buffer
195 mBuffer = pixelBuffer.mBuffer;
196 pixelBuffer.mBuffer = NULL;
197 mBufferSize = pixelBuffer.mBufferSize;
198 mWidth = pixelBuffer.mWidth;
199 mHeight = pixelBuffer.mHeight;
200 mPixelFormat = pixelBuffer.mPixelFormat;
203 void PixelBuffer::ReleaseBuffer()
211 void PixelBuffer::AllocateFixedSize( uint32_t size )
214 mBuffer = reinterpret_cast<unsigned char*>(malloc( size ));
218 bool PixelBuffer::Rotate( Degree angle )
220 // Check first if Rotate() can perform the operation in the current pixel buffer.
222 bool validPixelFormat = false;
223 switch( mPixelFormat )
231 case Pixel::RGBA8888:
232 case Pixel::BGRA8888: // FALL THROUGH
234 validPixelFormat = true;
239 // This pixel format is not supported for this operation.
240 validPixelFormat = false;
245 if( !validPixelFormat )
247 // Can't rotate the pixel buffer with the current pixel format.
248 DALI_LOG_ERROR( "Can't rotate the pixel buffer with the current pixel format\n" );
252 float radians = Radian( angle ).radian;
254 // Transform the input angle into the range [0..2PI]
255 radians = fmod( radians, TWO_PI );
256 radians += ( radians < 0.f ) ? TWO_PI : 0.f;
258 if( radians < Dali::Math::MACHINE_EPSILON_10 )
260 // Nothing to do if the angle is zero.
264 const unsigned int pixelSize = Pixel::GetBytesPerPixel( mPixelFormat );
266 uint8_t* pixelsOut = nullptr;
267 Platform::RotateByShear( mBuffer,
276 // Check whether the rotation succedded and set the new pixel buffer data.
277 const bool success = nullptr != pixelsOut;
281 // Release the memory of the current pixel buffer.
284 // Set the new pixel buffer.
287 mBufferSize = mWidth * mHeight * pixelSize;
293 void PixelBuffer::ScaleAndCrop( float scaleFactor, ImageDimensions cropDimensions )
295 ImageDimensions outDimensions( float(mWidth) * scaleFactor,
296 float(mHeight) * scaleFactor );
298 if( outDimensions.GetWidth() != mWidth || outDimensions.GetHeight() != mHeight )
300 Resize( outDimensions );
303 ImageDimensions postCropDimensions(
304 std::min(cropDimensions.GetWidth(), outDimensions.GetWidth()),
305 std::min(cropDimensions.GetHeight(), outDimensions.GetHeight()));
307 if( postCropDimensions.GetWidth() < outDimensions.GetWidth() ||
308 postCropDimensions.GetHeight() < outDimensions.GetHeight() )
310 uint16_t x = ( outDimensions.GetWidth() - postCropDimensions.GetWidth() ) / 2;
311 uint16_t y = ( outDimensions.GetHeight() - postCropDimensions.GetHeight() ) / 2;
312 Crop( x, y, postCropDimensions );
316 void PixelBuffer::Crop( uint16_t x, uint16_t y, ImageDimensions cropDimensions )
318 PixelBufferPtr outBuffer = NewCrop( *this, x, y, cropDimensions );
319 TakeOwnershipOfBuffer( *outBuffer );
322 PixelBufferPtr PixelBuffer::NewCrop( const PixelBuffer& inBuffer, uint16_t x, uint16_t y, ImageDimensions cropDimensions )
324 PixelBufferPtr outBuffer = PixelBuffer::New( cropDimensions.GetWidth(), cropDimensions.GetHeight(), inBuffer.GetPixelFormat() );
325 int bytesPerPixel = Pixel::GetBytesPerPixel( inBuffer.mPixelFormat );
326 int srcStride = inBuffer.mWidth * bytesPerPixel;
327 int destStride = cropDimensions.GetWidth() * bytesPerPixel;
329 // Clamp crop to right edge
330 if( x + cropDimensions.GetWidth() > inBuffer.mWidth )
332 destStride = ( inBuffer.mWidth - x ) * bytesPerPixel;
335 int srcOffset = x * bytesPerPixel + y * srcStride;
337 unsigned char* destBuffer = outBuffer->mBuffer;
339 // Clamp crop to last row
340 unsigned int endRow = y + cropDimensions.GetHeight();
341 if( endRow > inBuffer.mHeight )
343 endRow = inBuffer.mHeight - 1 ;
345 for( uint16_t row = y; row < endRow; ++row )
347 memcpy(destBuffer + destOffset, inBuffer.mBuffer + srcOffset, destStride );
348 srcOffset += srcStride;
349 destOffset += destStride;
355 void PixelBuffer::SetMetadata( const Property::Map& map )
357 mMetadata.reset(new Property::Map(map));
360 bool PixelBuffer::GetMetadata(Property::Map& outMetadata) const
366 outMetadata = *mMetadata;
370 void PixelBuffer::SetMetadata(std::unique_ptr<Property::Map> metadata)
372 mMetadata = std::move(metadata);
375 void PixelBuffer::Resize( ImageDimensions outDimensions )
377 if( mWidth != outDimensions.GetWidth() || mHeight != outDimensions.GetHeight() )
379 PixelBufferPtr outBuffer = NewResize( *this, outDimensions );
380 TakeOwnershipOfBuffer( *outBuffer );
384 PixelBufferPtr PixelBuffer::NewResize( const PixelBuffer& inBuffer, ImageDimensions outDimensions )
386 PixelBufferPtr outBuffer = PixelBuffer::New( outDimensions.GetWidth(), outDimensions.GetHeight(), inBuffer.GetPixelFormat() );
387 ImageDimensions inDimensions( inBuffer.mWidth, inBuffer.mHeight );
389 bool hasAlpha = Pixel::HasAlpha( inBuffer.mPixelFormat );
390 int bytesPerPixel = Pixel::GetBytesPerPixel( inBuffer.mPixelFormat );
392 Resampler::Filter filterType = Resampler::LANCZOS4;
393 if( inDimensions.GetWidth() < outDimensions.GetWidth() && inDimensions.GetHeight() < outDimensions.GetHeight() )
395 filterType = Resampler::MITCHELL;
398 // This method only really works for 8 bit wide channels.
399 // (But could be expanded to work)
400 if( inBuffer.mPixelFormat == Pixel::A8 ||
401 inBuffer.mPixelFormat == Pixel::L8 ||
402 inBuffer.mPixelFormat == Pixel::LA88 ||
403 inBuffer.mPixelFormat == Pixel::RGB888 ||
404 inBuffer.mPixelFormat == Pixel::RGB8888 ||
405 inBuffer.mPixelFormat == Pixel::BGR8888 ||
406 inBuffer.mPixelFormat == Pixel::RGBA8888 ||
407 inBuffer.mPixelFormat == Pixel::BGRA8888 )
409 Dali::Internal::Platform::Resample( inBuffer.mBuffer, inDimensions,
410 outBuffer->GetBuffer(), outDimensions,
411 filterType, bytesPerPixel, hasAlpha );
415 DALI_LOG_ERROR( "Trying to resize an image with too narrow a channel width" );
421 void PixelBuffer::ApplyGaussianBlur( const float blurRadius )
423 // This method only works for pixel buffer in RGBA format.
424 if( mWidth > 0 && mHeight > 0 && mPixelFormat == Pixel::RGBA8888 )
426 if ( blurRadius > Math::MACHINE_EPSILON_1 )
428 PerformGaussianBlurRGBA( *this, blurRadius );
433 DALI_LOG_ERROR( "Trying to apply gaussian blur to an empty pixel buffer or a pixel buffer not in RGBA format" );
437 void PixelBuffer::MultiplyColorByAlpha()
439 auto bytesPerPixel = Pixel::GetBytesPerPixel( mPixelFormat );
441 // Compressed textures have unknown size of the pixel. Alpha premultiplication
442 // must be skipped in such case
443 if( Pixel::GetBytesPerPixel(mPixelFormat) && Pixel::HasAlpha(mPixelFormat) )
445 unsigned char* pixel = mBuffer;
446 const unsigned int bufferSize = mWidth * mHeight;
448 for( unsigned int i=0; i<bufferSize; ++i )
450 unsigned int alpha = ReadChannel( pixel, mPixelFormat, Adaptor::ALPHA );
452 auto red = ReadChannel( pixel, mPixelFormat, Adaptor::RED);
453 auto green = ReadChannel( pixel, mPixelFormat, Adaptor::GREEN);
454 auto blue = ReadChannel( pixel, mPixelFormat, Adaptor::BLUE);
455 auto luminance = ReadChannel( pixel, mPixelFormat, Adaptor::LUMINANCE);
456 WriteChannel( pixel, mPixelFormat, Adaptor::RED, red*alpha / 255 );
457 WriteChannel( pixel, mPixelFormat, Adaptor::GREEN, green*alpha/255 );
458 WriteChannel( pixel, mPixelFormat, Adaptor::BLUE, blue*alpha/255 );
459 WriteChannel( pixel, mPixelFormat, Adaptor::LUMINANCE, luminance*alpha/255 );
461 pixel += bytesPerPixel;
466 }// namespace Adaptor
467 }// namespace Internal