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 "pixel-buffer-impl.h"
26 #include "pixel-manipulation.h"
27 #include "alpha-mask.h"
28 #include "gaussian-blur.h"
29 #include <platform-abstractions/portable/image-operations.h>
30 #include <platform-abstractions/portable/pixel-manipulation.h>
43 const float TWO_PI = 2.f * Math::PI; ///< 360 degrees in radians
46 PixelBuffer::PixelBuffer( unsigned char* buffer,
47 unsigned int bufferSize,
50 Dali::Pixel::Format pixelFormat )
53 mBufferSize( bufferSize ),
56 mPixelFormat( pixelFormat )
60 PixelBuffer::~PixelBuffer()
65 PixelBufferPtr PixelBuffer::New( unsigned int width,
67 Dali::Pixel::Format pixelFormat )
69 unsigned int bufferSize = width * height * Dali::Pixel::GetBytesPerPixel( pixelFormat );
70 unsigned char* buffer = NULL;
73 buffer = static_cast<unsigned char*>( malloc ( bufferSize ) );
75 return new PixelBuffer( buffer, bufferSize, width, height, pixelFormat );
78 PixelBufferPtr PixelBuffer::New( unsigned char* buffer,
79 unsigned int bufferSize,
82 Dali::Pixel::Format pixelFormat )
84 return new PixelBuffer( buffer, bufferSize, width, height, pixelFormat );
87 Dali::PixelData PixelBuffer::Convert( PixelBuffer& pixelBuffer )
89 Dali::PixelData pixelData = Dali::PixelData::New( pixelBuffer.mBuffer,
90 pixelBuffer.mBufferSize,
93 pixelBuffer.mPixelFormat,
94 Dali::PixelData::FREE );
95 pixelBuffer.mBuffer = NULL;
96 pixelBuffer.mWidth = 0;
97 pixelBuffer.mHeight = 0;
98 pixelBuffer.mBufferSize = 0;
103 unsigned int PixelBuffer::GetWidth() const
108 unsigned int PixelBuffer::GetHeight() const
113 Dali::Pixel::Format PixelBuffer::GetPixelFormat() const
118 unsigned char* PixelBuffer::GetBuffer() const
123 unsigned int PixelBuffer::GetBufferSize() const
128 Dali::PixelData PixelBuffer::CreatePixelData() const
130 unsigned char* destBuffer = NULL;
132 if( mBufferSize > 0 )
134 destBuffer = static_cast<unsigned char*>( malloc( mBufferSize ) );
135 memcpy( destBuffer, mBuffer, mBufferSize );
138 Dali::PixelData pixelData = Dali::PixelData::New( destBuffer, mBufferSize,
141 Dali::PixelData::FREE );
145 void PixelBuffer::ApplyMask( const PixelBuffer& inMask, float contentScale, bool cropToMask )
149 // First scale this buffer by the contentScale, and crop to the mask size
150 // If it's too small, then scale the mask to match the image size
151 // Then apply the mask
152 ScaleAndCrop( contentScale, ImageDimensions( inMask.GetWidth(), inMask.GetHeight() ) );
154 if( inMask.mWidth > mWidth || inMask.mHeight > mHeight )
156 PixelBufferPtr mask = NewResize( inMask, ImageDimensions( mWidth, mHeight ) );
157 ApplyMaskInternal( *mask );
161 ApplyMaskInternal( inMask );
166 // First, scale the mask to match the image size,
167 // then apply the mask.
168 PixelBufferPtr mask = NewResize( inMask, ImageDimensions( mWidth, mHeight ) );
169 ApplyMaskInternal( *mask );
173 void PixelBuffer::ApplyMaskInternal( const PixelBuffer& mask )
178 Dali::Pixel::GetAlphaOffsetAndMask(mPixelFormat, byteOffset, bitMask);
179 if( Dali::Pixel::HasAlpha( mPixelFormat ) && bitMask == 255 )
181 ApplyMaskToAlphaChannel( *this, mask );
185 PixelBufferPtr newPixelBuffer = CreateNewMaskedBuffer( *this, mask );
186 TakeOwnershipOfBuffer( *newPixelBuffer );
187 // On leaving scope, newPixelBuffer will get destroyed.
191 void PixelBuffer::TakeOwnershipOfBuffer( PixelBuffer& pixelBuffer )
195 // Take ownership of new buffer
196 mBuffer = pixelBuffer.mBuffer;
197 pixelBuffer.mBuffer = NULL;
198 mBufferSize = pixelBuffer.mBufferSize;
199 mWidth = pixelBuffer.mWidth;
200 mHeight = pixelBuffer.mHeight;
201 mPixelFormat = pixelBuffer.mPixelFormat;
204 void PixelBuffer::ReleaseBuffer()
212 void PixelBuffer::AllocateFixedSize( uint32_t size )
215 mBuffer = reinterpret_cast<unsigned char*>(malloc( size ));
219 void PixelBuffer::Rotate( Degree angle )
221 // Check first if Rotate() can perform the operation in the current pixel buffer.
223 bool validPixelFormat = false;
224 switch( mPixelFormat )
232 case Pixel::RGBA8888:
233 case Pixel::BGRA8888: // FALL THROUGH
235 validPixelFormat = true;
240 // This pixel format is not supported for this operation.
241 validPixelFormat = false;
246 if( !validPixelFormat )
248 // Can't rotate the pixel buffer with the current pixel format.
249 DALI_LOG_ERROR( "Can't rotate the pixel buffer with the current pixel format\n" );
253 float radians = Radian( angle ).radian;
255 // Transform the input angle into the range [0..2PI]
256 radians = fmod( radians, TWO_PI );
257 radians += ( radians < 0.f ) ? TWO_PI : 0.f;
259 if( radians < Dali::Math::MACHINE_EPSILON_10 )
261 // Nothing to do if the angle is zero.
265 const unsigned int pixelSize = Pixel::GetBytesPerPixel( mPixelFormat );
267 uint8_t* pixelsOut = nullptr;
268 Platform::RotateByShear( mBuffer,
277 // Release the memory of the current pixel buffer.
280 // Set the new pixel buffer.
283 mBufferSize = mWidth * mHeight * pixelSize;
286 void PixelBuffer::ScaleAndCrop( float scaleFactor, ImageDimensions cropDimensions )
288 ImageDimensions outDimensions( float(mWidth) * scaleFactor,
289 float(mHeight) * scaleFactor );
291 if( outDimensions.GetWidth() != mWidth || outDimensions.GetHeight() != mHeight )
293 Resize( outDimensions );
296 ImageDimensions postCropDimensions(
297 std::min(cropDimensions.GetWidth(), outDimensions.GetWidth()),
298 std::min(cropDimensions.GetHeight(), outDimensions.GetHeight()));
300 if( postCropDimensions.GetWidth() < outDimensions.GetWidth() ||
301 postCropDimensions.GetHeight() < outDimensions.GetHeight() )
303 uint16_t x = ( outDimensions.GetWidth() - postCropDimensions.GetWidth() ) / 2;
304 uint16_t y = ( outDimensions.GetHeight() - postCropDimensions.GetHeight() ) / 2;
305 Crop( x, y, postCropDimensions );
309 void PixelBuffer::Crop( uint16_t x, uint16_t y, ImageDimensions cropDimensions )
311 PixelBufferPtr outBuffer = NewCrop( *this, x, y, cropDimensions );
312 TakeOwnershipOfBuffer( *outBuffer );
315 PixelBufferPtr PixelBuffer::NewCrop( const PixelBuffer& inBuffer, uint16_t x, uint16_t y, ImageDimensions cropDimensions )
317 PixelBufferPtr outBuffer = PixelBuffer::New( cropDimensions.GetWidth(), cropDimensions.GetHeight(), inBuffer.GetPixelFormat() );
318 int bytesPerPixel = Pixel::GetBytesPerPixel( inBuffer.mPixelFormat );
319 int srcStride = inBuffer.mWidth * bytesPerPixel;
320 int destStride = cropDimensions.GetWidth() * bytesPerPixel;
322 // Clamp crop to right edge
323 if( x + cropDimensions.GetWidth() > inBuffer.mWidth )
325 destStride = ( inBuffer.mWidth - x ) * bytesPerPixel;
328 int srcOffset = x * bytesPerPixel + y * srcStride;
330 unsigned char* destBuffer = outBuffer->mBuffer;
332 // Clamp crop to last row
333 unsigned int endRow = y + cropDimensions.GetHeight();
334 if( endRow > inBuffer.mHeight )
336 endRow = inBuffer.mHeight - 1 ;
338 for( uint16_t row = y; row < endRow; ++row )
340 memcpy(destBuffer + destOffset, inBuffer.mBuffer + srcOffset, destStride );
341 srcOffset += srcStride;
342 destOffset += destStride;
348 void PixelBuffer::SetMetadata( const Property::Map& map )
350 mMetadata.reset(new Property::Map(map));
353 bool PixelBuffer::GetMetadata(Property::Map& outMetadata) const
359 outMetadata = *mMetadata;
363 void PixelBuffer::SetMetadata(std::unique_ptr<Property::Map> metadata)
365 mMetadata = std::move(metadata);
368 void PixelBuffer::Resize( ImageDimensions outDimensions )
370 if( mWidth != outDimensions.GetWidth() || mHeight != outDimensions.GetHeight() )
372 PixelBufferPtr outBuffer = NewResize( *this, outDimensions );
373 TakeOwnershipOfBuffer( *outBuffer );
377 PixelBufferPtr PixelBuffer::NewResize( const PixelBuffer& inBuffer, ImageDimensions outDimensions )
379 PixelBufferPtr outBuffer = PixelBuffer::New( outDimensions.GetWidth(), outDimensions.GetHeight(), inBuffer.GetPixelFormat() );
380 ImageDimensions inDimensions( inBuffer.mWidth, inBuffer.mHeight );
382 bool hasAlpha = Pixel::HasAlpha( inBuffer.mPixelFormat );
383 int bytesPerPixel = Pixel::GetBytesPerPixel( inBuffer.mPixelFormat );
385 Resampler::Filter filterType = Resampler::LANCZOS4;
386 if( inDimensions.GetWidth() < outDimensions.GetWidth() && inDimensions.GetHeight() < outDimensions.GetHeight() )
388 filterType = Resampler::MITCHELL;
391 // This method only really works for 8 bit wide channels.
392 // (But could be expanded to work)
393 if( inBuffer.mPixelFormat == Pixel::A8 ||
394 inBuffer.mPixelFormat == Pixel::L8 ||
395 inBuffer.mPixelFormat == Pixel::LA88 ||
396 inBuffer.mPixelFormat == Pixel::RGB888 ||
397 inBuffer.mPixelFormat == Pixel::RGB8888 ||
398 inBuffer.mPixelFormat == Pixel::BGR8888 ||
399 inBuffer.mPixelFormat == Pixel::RGBA8888 ||
400 inBuffer.mPixelFormat == Pixel::BGRA8888 )
402 Dali::Internal::Platform::Resample( inBuffer.mBuffer, inDimensions,
403 outBuffer->GetBuffer(), outDimensions,
404 filterType, bytesPerPixel, hasAlpha );
408 DALI_LOG_ERROR( "Trying to resize an image with too narrow a channel width" );
414 void PixelBuffer::ApplyGaussianBlur( const float blurRadius )
416 // This method only works for pixel buffer in RGBA format.
417 if( mWidth > 0 && mHeight > 0 && mPixelFormat == Pixel::RGBA8888 )
419 if ( blurRadius > Math::MACHINE_EPSILON_1 )
421 PerformGaussianBlurRGBA( *this, blurRadius );
426 DALI_LOG_ERROR( "Trying to apply gaussian blur to an empty pixel buffer or a pixel buffer not in RGBA format" );
430 void PixelBuffer::MultiplyColorByAlpha()
432 auto bytesPerPixel = Pixel::GetBytesPerPixel( mPixelFormat );
434 // Compressed textures have unknown size of the pixel. Alpha premultiplication
435 // must be skipped in such case
436 if( Pixel::GetBytesPerPixel(mPixelFormat) && Pixel::HasAlpha(mPixelFormat) )
438 unsigned char* pixel = mBuffer;
439 const unsigned int bufferSize = mWidth * mHeight;
441 for( unsigned int i=0; i<bufferSize; ++i )
443 unsigned int alpha = ReadChannel( pixel, mPixelFormat, Adaptor::ALPHA );
445 auto red = ReadChannel( pixel, mPixelFormat, Adaptor::RED);
446 auto green = ReadChannel( pixel, mPixelFormat, Adaptor::GREEN);
447 auto blue = ReadChannel( pixel, mPixelFormat, Adaptor::BLUE);
448 auto luminance = ReadChannel( pixel, mPixelFormat, Adaptor::LUMINANCE);
449 WriteChannel( pixel, mPixelFormat, Adaptor::RED, red*alpha / 255 );
450 WriteChannel( pixel, mPixelFormat, Adaptor::GREEN, green*alpha/255 );
451 WriteChannel( pixel, mPixelFormat, Adaptor::BLUE, blue*alpha/255 );
452 WriteChannel( pixel, mPixelFormat, Adaptor::LUMINANCE, luminance*alpha/255 );
454 pixel += bytesPerPixel;
462 }// namespace Adaptor
463 }// namespace Internal