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 const unsigned char* const PixelBuffer::GetConstBuffer() const
127 unsigned int PixelBuffer::GetBufferSize() const
132 Dali::PixelData PixelBuffer::CreatePixelData() const
134 unsigned char* destBuffer = NULL;
136 if( mBufferSize > 0 )
138 destBuffer = static_cast<unsigned char*>( malloc( mBufferSize ) );
139 memcpy( destBuffer, mBuffer, mBufferSize );
142 Dali::PixelData pixelData = Dali::PixelData::New( destBuffer, mBufferSize,
145 Dali::PixelData::FREE );
149 void PixelBuffer::ApplyMask( const PixelBuffer& inMask, float contentScale, bool cropToMask )
153 // First scale this buffer by the contentScale, and crop to the mask size
154 // If it's too small, then scale the mask to match the image size
155 // Then apply the mask
156 ScaleAndCrop( contentScale, ImageDimensions( inMask.GetWidth(), inMask.GetHeight() ) );
158 if( inMask.mWidth > mWidth || inMask.mHeight > mHeight )
160 PixelBufferPtr mask = NewResize( inMask, ImageDimensions( mWidth, mHeight ) );
161 ApplyMaskInternal( *mask );
165 ApplyMaskInternal( inMask );
170 // First, scale the mask to match the image size,
171 // then apply the mask.
172 PixelBufferPtr mask = NewResize( inMask, ImageDimensions( mWidth, mHeight ) );
173 ApplyMaskInternal( *mask );
177 void PixelBuffer::ApplyMaskInternal( const PixelBuffer& mask )
182 Dali::Pixel::GetAlphaOffsetAndMask(mPixelFormat, byteOffset, bitMask);
183 if( Dali::Pixel::HasAlpha( mPixelFormat ) && bitMask == 255 )
185 ApplyMaskToAlphaChannel( *this, mask );
189 PixelBufferPtr newPixelBuffer = CreateNewMaskedBuffer( *this, mask );
190 TakeOwnershipOfBuffer( *newPixelBuffer );
191 // On leaving scope, newPixelBuffer will get destroyed.
195 void PixelBuffer::TakeOwnershipOfBuffer( PixelBuffer& pixelBuffer )
199 // Take ownership of new buffer
200 mBuffer = pixelBuffer.mBuffer;
201 pixelBuffer.mBuffer = NULL;
202 mBufferSize = pixelBuffer.mBufferSize;
203 mWidth = pixelBuffer.mWidth;
204 mHeight = pixelBuffer.mHeight;
205 mPixelFormat = pixelBuffer.mPixelFormat;
208 void PixelBuffer::ReleaseBuffer()
216 void PixelBuffer::AllocateFixedSize( uint32_t size )
219 mBuffer = reinterpret_cast<unsigned char*>(malloc( size ));
223 bool PixelBuffer::Rotate( Degree angle )
225 // Check first if Rotate() can perform the operation in the current pixel buffer.
227 bool validPixelFormat = false;
228 switch( mPixelFormat )
236 case Pixel::RGBA8888:
237 case Pixel::BGRA8888: // FALL THROUGH
239 validPixelFormat = true;
244 // This pixel format is not supported for this operation.
245 validPixelFormat = false;
250 if( !validPixelFormat )
252 // Can't rotate the pixel buffer with the current pixel format.
253 DALI_LOG_ERROR( "Can't rotate the pixel buffer with the current pixel format\n" );
257 float radians = Radian( angle ).radian;
259 // Transform the input angle into the range [0..2PI]
260 radians = fmod( radians, TWO_PI );
261 radians += ( radians < 0.f ) ? TWO_PI : 0.f;
263 if( radians < Dali::Math::MACHINE_EPSILON_10 )
265 // Nothing to do if the angle is zero.
269 const unsigned int pixelSize = Pixel::GetBytesPerPixel( mPixelFormat );
271 uint8_t* pixelsOut = nullptr;
272 Platform::RotateByShear( mBuffer,
281 // Check whether the rotation succedded and set the new pixel buffer data.
282 const bool success = nullptr != pixelsOut;
286 // Release the memory of the current pixel buffer.
289 // Set the new pixel buffer.
292 mBufferSize = mWidth * mHeight * pixelSize;
298 void PixelBuffer::ScaleAndCrop( float scaleFactor, ImageDimensions cropDimensions )
300 ImageDimensions outDimensions( float(mWidth) * scaleFactor,
301 float(mHeight) * scaleFactor );
303 if( outDimensions.GetWidth() != mWidth || outDimensions.GetHeight() != mHeight )
305 Resize( outDimensions );
308 ImageDimensions postCropDimensions(
309 std::min(cropDimensions.GetWidth(), outDimensions.GetWidth()),
310 std::min(cropDimensions.GetHeight(), outDimensions.GetHeight()));
312 if( postCropDimensions.GetWidth() < outDimensions.GetWidth() ||
313 postCropDimensions.GetHeight() < outDimensions.GetHeight() )
315 uint16_t x = ( outDimensions.GetWidth() - postCropDimensions.GetWidth() ) / 2;
316 uint16_t y = ( outDimensions.GetHeight() - postCropDimensions.GetHeight() ) / 2;
317 Crop( x, y, postCropDimensions );
321 void PixelBuffer::Crop( uint16_t x, uint16_t y, ImageDimensions cropDimensions )
323 PixelBufferPtr outBuffer = NewCrop( *this, x, y, cropDimensions );
324 TakeOwnershipOfBuffer( *outBuffer );
327 PixelBufferPtr PixelBuffer::NewCrop( const PixelBuffer& inBuffer, uint16_t x, uint16_t y, ImageDimensions cropDimensions )
329 PixelBufferPtr outBuffer = PixelBuffer::New( cropDimensions.GetWidth(), cropDimensions.GetHeight(), inBuffer.GetPixelFormat() );
330 int bytesPerPixel = Pixel::GetBytesPerPixel( inBuffer.mPixelFormat );
331 int srcStride = inBuffer.mWidth * bytesPerPixel;
332 int destStride = cropDimensions.GetWidth() * bytesPerPixel;
334 // Clamp crop to right edge
335 if( x + cropDimensions.GetWidth() > inBuffer.mWidth )
337 destStride = ( inBuffer.mWidth - x ) * bytesPerPixel;
340 int srcOffset = x * bytesPerPixel + y * srcStride;
342 unsigned char* destBuffer = outBuffer->mBuffer;
344 // Clamp crop to last row
345 unsigned int endRow = y + cropDimensions.GetHeight();
346 if( endRow > inBuffer.mHeight )
348 endRow = inBuffer.mHeight - 1 ;
350 for( uint16_t row = y; row < endRow; ++row )
352 memcpy(destBuffer + destOffset, inBuffer.mBuffer + srcOffset, destStride );
353 srcOffset += srcStride;
354 destOffset += destStride;
360 void PixelBuffer::SetMetadata( const Property::Map& map )
362 mMetadata.reset(new Property::Map(map));
365 bool PixelBuffer::GetMetadata(Property::Map& outMetadata) const
371 outMetadata = *mMetadata;
375 void PixelBuffer::SetMetadata(std::unique_ptr<Property::Map> metadata)
377 mMetadata = std::move(metadata);
380 void PixelBuffer::Resize( ImageDimensions outDimensions )
382 if( mWidth != outDimensions.GetWidth() || mHeight != outDimensions.GetHeight() )
384 PixelBufferPtr outBuffer = NewResize( *this, outDimensions );
385 TakeOwnershipOfBuffer( *outBuffer );
389 PixelBufferPtr PixelBuffer::NewResize( const PixelBuffer& inBuffer, ImageDimensions outDimensions )
391 PixelBufferPtr outBuffer = PixelBuffer::New( outDimensions.GetWidth(), outDimensions.GetHeight(), inBuffer.GetPixelFormat() );
392 ImageDimensions inDimensions( inBuffer.mWidth, inBuffer.mHeight );
394 bool hasAlpha = Pixel::HasAlpha( inBuffer.mPixelFormat );
395 int bytesPerPixel = Pixel::GetBytesPerPixel( inBuffer.mPixelFormat );
397 Resampler::Filter filterType = Resampler::LANCZOS4;
398 if( inDimensions.GetWidth() < outDimensions.GetWidth() && inDimensions.GetHeight() < outDimensions.GetHeight() )
400 filterType = Resampler::MITCHELL;
403 // This method only really works for 8 bit wide channels.
404 // (But could be expanded to work)
405 if( inBuffer.mPixelFormat == Pixel::A8 ||
406 inBuffer.mPixelFormat == Pixel::L8 ||
407 inBuffer.mPixelFormat == Pixel::LA88 ||
408 inBuffer.mPixelFormat == Pixel::RGB888 ||
409 inBuffer.mPixelFormat == Pixel::RGB8888 ||
410 inBuffer.mPixelFormat == Pixel::BGR8888 ||
411 inBuffer.mPixelFormat == Pixel::RGBA8888 ||
412 inBuffer.mPixelFormat == Pixel::BGRA8888 )
414 Dali::Internal::Platform::Resample( inBuffer.mBuffer, inDimensions,
415 outBuffer->GetBuffer(), outDimensions,
416 filterType, bytesPerPixel, hasAlpha );
420 DALI_LOG_ERROR( "Trying to resize an image with too narrow a channel width" );
426 void PixelBuffer::ApplyGaussianBlur( const float blurRadius )
428 // This method only works for pixel buffer in RGBA format.
429 if( mWidth > 0 && mHeight > 0 && mPixelFormat == Pixel::RGBA8888 )
431 if ( blurRadius > Math::MACHINE_EPSILON_1 )
433 PerformGaussianBlurRGBA( *this, blurRadius );
438 DALI_LOG_ERROR( "Trying to apply gaussian blur to an empty pixel buffer or a pixel buffer not in RGBA format" );
442 void PixelBuffer::MultiplyColorByAlpha()
444 auto bytesPerPixel = Pixel::GetBytesPerPixel( mPixelFormat );
446 // Compressed textures have unknown size of the pixel. Alpha premultiplication
447 // must be skipped in such case
448 if( Pixel::GetBytesPerPixel(mPixelFormat) && Pixel::HasAlpha(mPixelFormat) )
450 unsigned char* pixel = mBuffer;
451 const unsigned int bufferSize = mWidth * mHeight;
453 for( unsigned int i=0; i<bufferSize; ++i )
455 unsigned int alpha = ReadChannel( pixel, mPixelFormat, Adaptor::ALPHA );
457 auto red = ReadChannel( pixel, mPixelFormat, Adaptor::RED);
458 auto green = ReadChannel( pixel, mPixelFormat, Adaptor::GREEN);
459 auto blue = ReadChannel( pixel, mPixelFormat, Adaptor::BLUE);
460 auto luminance = ReadChannel( pixel, mPixelFormat, Adaptor::LUMINANCE);
461 WriteChannel( pixel, mPixelFormat, Adaptor::RED, red*alpha / 255 );
462 WriteChannel( pixel, mPixelFormat, Adaptor::GREEN, green*alpha/255 );
463 WriteChannel( pixel, mPixelFormat, Adaptor::BLUE, blue*alpha/255 );
464 WriteChannel( pixel, mPixelFormat, Adaptor::LUMINANCE, luminance*alpha/255 );
466 pixel += bytesPerPixel;
471 }// namespace Adaptor
472 }// namespace Internal