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 bool 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 // Check whether the rotation succedded and set the new pixel buffer data.
278 const bool success = nullptr != pixelsOut;
282 // Release the memory of the current pixel buffer.
285 // Set the new pixel buffer.
288 mBufferSize = mWidth * mHeight * pixelSize;
294 void PixelBuffer::ScaleAndCrop( float scaleFactor, ImageDimensions cropDimensions )
296 ImageDimensions outDimensions( float(mWidth) * scaleFactor,
297 float(mHeight) * scaleFactor );
299 if( outDimensions.GetWidth() != mWidth || outDimensions.GetHeight() != mHeight )
301 Resize( outDimensions );
304 ImageDimensions postCropDimensions(
305 std::min(cropDimensions.GetWidth(), outDimensions.GetWidth()),
306 std::min(cropDimensions.GetHeight(), outDimensions.GetHeight()));
308 if( postCropDimensions.GetWidth() < outDimensions.GetWidth() ||
309 postCropDimensions.GetHeight() < outDimensions.GetHeight() )
311 uint16_t x = ( outDimensions.GetWidth() - postCropDimensions.GetWidth() ) / 2;
312 uint16_t y = ( outDimensions.GetHeight() - postCropDimensions.GetHeight() ) / 2;
313 Crop( x, y, postCropDimensions );
317 void PixelBuffer::Crop( uint16_t x, uint16_t y, ImageDimensions cropDimensions )
319 PixelBufferPtr outBuffer = NewCrop( *this, x, y, cropDimensions );
320 TakeOwnershipOfBuffer( *outBuffer );
323 PixelBufferPtr PixelBuffer::NewCrop( const PixelBuffer& inBuffer, uint16_t x, uint16_t y, ImageDimensions cropDimensions )
325 PixelBufferPtr outBuffer = PixelBuffer::New( cropDimensions.GetWidth(), cropDimensions.GetHeight(), inBuffer.GetPixelFormat() );
326 int bytesPerPixel = Pixel::GetBytesPerPixel( inBuffer.mPixelFormat );
327 int srcStride = inBuffer.mWidth * bytesPerPixel;
328 int destStride = cropDimensions.GetWidth() * bytesPerPixel;
330 // Clamp crop to right edge
331 if( x + cropDimensions.GetWidth() > inBuffer.mWidth )
333 destStride = ( inBuffer.mWidth - x ) * bytesPerPixel;
336 int srcOffset = x * bytesPerPixel + y * srcStride;
338 unsigned char* destBuffer = outBuffer->mBuffer;
340 // Clamp crop to last row
341 unsigned int endRow = y + cropDimensions.GetHeight();
342 if( endRow > inBuffer.mHeight )
344 endRow = inBuffer.mHeight - 1 ;
346 for( uint16_t row = y; row < endRow; ++row )
348 memcpy(destBuffer + destOffset, inBuffer.mBuffer + srcOffset, destStride );
349 srcOffset += srcStride;
350 destOffset += destStride;
356 void PixelBuffer::SetMetadata( const Property::Map& map )
358 mMetadata.reset(new Property::Map(map));
361 bool PixelBuffer::GetMetadata(Property::Map& outMetadata) const
367 outMetadata = *mMetadata;
371 void PixelBuffer::SetMetadata(std::unique_ptr<Property::Map> metadata)
373 mMetadata = std::move(metadata);
376 void PixelBuffer::Resize( ImageDimensions outDimensions )
378 if( mWidth != outDimensions.GetWidth() || mHeight != outDimensions.GetHeight() )
380 PixelBufferPtr outBuffer = NewResize( *this, outDimensions );
381 TakeOwnershipOfBuffer( *outBuffer );
385 PixelBufferPtr PixelBuffer::NewResize( const PixelBuffer& inBuffer, ImageDimensions outDimensions )
387 PixelBufferPtr outBuffer = PixelBuffer::New( outDimensions.GetWidth(), outDimensions.GetHeight(), inBuffer.GetPixelFormat() );
388 ImageDimensions inDimensions( inBuffer.mWidth, inBuffer.mHeight );
390 bool hasAlpha = Pixel::HasAlpha( inBuffer.mPixelFormat );
391 int bytesPerPixel = Pixel::GetBytesPerPixel( inBuffer.mPixelFormat );
393 Resampler::Filter filterType = Resampler::LANCZOS4;
394 if( inDimensions.GetWidth() < outDimensions.GetWidth() && inDimensions.GetHeight() < outDimensions.GetHeight() )
396 filterType = Resampler::MITCHELL;
399 // This method only really works for 8 bit wide channels.
400 // (But could be expanded to work)
401 if( inBuffer.mPixelFormat == Pixel::A8 ||
402 inBuffer.mPixelFormat == Pixel::L8 ||
403 inBuffer.mPixelFormat == Pixel::LA88 ||
404 inBuffer.mPixelFormat == Pixel::RGB888 ||
405 inBuffer.mPixelFormat == Pixel::RGB8888 ||
406 inBuffer.mPixelFormat == Pixel::BGR8888 ||
407 inBuffer.mPixelFormat == Pixel::RGBA8888 ||
408 inBuffer.mPixelFormat == Pixel::BGRA8888 )
410 Dali::Internal::Platform::Resample( inBuffer.mBuffer, inDimensions,
411 outBuffer->GetBuffer(), outDimensions,
412 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;
467 }// namespace Adaptor
468 }// namespace Internal