2 * Copyright (c) 2017 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>
41 PixelBuffer::PixelBuffer( unsigned char* buffer,
42 unsigned int bufferSize,
45 Dali::Pixel::Format pixelFormat )
47 mBufferSize( bufferSize ),
50 mPixelFormat( pixelFormat )
54 PixelBuffer::~PixelBuffer()
59 PixelBufferPtr PixelBuffer::New( unsigned int width,
61 Dali::Pixel::Format pixelFormat )
63 unsigned int bufferSize = width * height * Dali::Pixel::GetBytesPerPixel( pixelFormat );
64 unsigned char* buffer = NULL;
67 buffer = static_cast<unsigned char*>( malloc ( bufferSize ) );
69 return new PixelBuffer( buffer, bufferSize, width, height, pixelFormat );
72 PixelBufferPtr PixelBuffer::New( unsigned char* buffer,
73 unsigned int bufferSize,
76 Dali::Pixel::Format pixelFormat )
78 return new PixelBuffer( buffer, bufferSize, width, height, pixelFormat );
81 Dali::PixelData PixelBuffer::Convert( PixelBuffer& pixelBuffer )
83 Dali::PixelData pixelData = Dali::PixelData::New( pixelBuffer.mBuffer,
84 pixelBuffer.mBufferSize,
87 pixelBuffer.mPixelFormat,
88 Dali::PixelData::FREE );
89 pixelBuffer.mBuffer = NULL;
90 pixelBuffer.mWidth = 0;
91 pixelBuffer.mHeight = 0;
92 pixelBuffer.mBufferSize = 0;
97 unsigned int PixelBuffer::GetWidth() const
102 unsigned int PixelBuffer::GetHeight() const
107 Dali::Pixel::Format PixelBuffer::GetPixelFormat() const
112 unsigned char* PixelBuffer::GetBuffer() const
117 unsigned int PixelBuffer::GetBufferSize() const
122 Dali::PixelData PixelBuffer::CreatePixelData() const
124 unsigned char* destBuffer = NULL;
126 if( mBufferSize > 0 )
128 destBuffer = static_cast<unsigned char*>( malloc( mBufferSize ) );
129 memcpy( destBuffer, mBuffer, mBufferSize );
132 Dali::PixelData pixelData = Dali::PixelData::New( destBuffer, mBufferSize,
135 Dali::PixelData::FREE );
139 void PixelBuffer::ApplyMask( const PixelBuffer& inMask, float contentScale, bool cropToMask )
143 // First scale this buffer by the contentScale, and crop to the mask size
144 // If it's too small, then scale the mask to match the image size
145 // Then apply the mask
146 ScaleAndCrop( contentScale, ImageDimensions( inMask.GetWidth(), inMask.GetHeight() ) );
148 if( inMask.mWidth > mWidth || inMask.mHeight > mHeight )
150 PixelBufferPtr mask = NewResize( inMask, ImageDimensions( mWidth, mHeight ) );
151 ApplyMaskInternal( *mask );
155 ApplyMaskInternal( inMask );
160 // First, scale the mask to match the image size,
161 // then apply the mask.
162 PixelBufferPtr mask = NewResize( inMask, ImageDimensions( mWidth, mHeight ) );
163 ApplyMaskInternal( *mask );
167 void PixelBuffer::ApplyMaskInternal( const PixelBuffer& mask )
172 Dali::Pixel::GetAlphaOffsetAndMask(mPixelFormat, byteOffset, bitMask);
173 if( Dali::Pixel::HasAlpha( mPixelFormat ) && bitMask == 255 )
175 ApplyMaskToAlphaChannel( *this, mask );
179 PixelBufferPtr newPixelBuffer = CreateNewMaskedBuffer( *this, mask );
180 TakeOwnershipOfBuffer( *newPixelBuffer );
181 // On leaving scope, newPixelBuffer will get destroyed.
185 void PixelBuffer::TakeOwnershipOfBuffer( PixelBuffer& pixelBuffer )
189 // Take ownership of new buffer
190 mBuffer = pixelBuffer.mBuffer;
191 pixelBuffer.mBuffer = NULL;
192 mBufferSize = pixelBuffer.mBufferSize;
193 mWidth = pixelBuffer.mWidth;
194 mHeight = pixelBuffer.mHeight;
195 mPixelFormat = pixelBuffer.mPixelFormat;
198 void PixelBuffer::ReleaseBuffer()
206 void PixelBuffer::ScaleAndCrop( float scaleFactor, ImageDimensions cropDimensions )
208 ImageDimensions outDimensions( float(mWidth) * scaleFactor,
209 float(mHeight) * scaleFactor );
211 if( outDimensions.GetWidth() != mWidth || outDimensions.GetHeight() != mHeight )
213 Resize( outDimensions );
216 ImageDimensions postCropDimensions(
217 std::min(cropDimensions.GetWidth(), outDimensions.GetWidth()),
218 std::min(cropDimensions.GetHeight(), outDimensions.GetHeight()));
220 if( postCropDimensions.GetWidth() < outDimensions.GetWidth() ||
221 postCropDimensions.GetHeight() < outDimensions.GetHeight() )
223 uint16_t x = ( outDimensions.GetWidth() - postCropDimensions.GetWidth() ) / 2;
224 uint16_t y = ( outDimensions.GetHeight() - postCropDimensions.GetHeight() ) / 2;
225 Crop( x, y, postCropDimensions );
229 void PixelBuffer::Crop( uint16_t x, uint16_t y, ImageDimensions cropDimensions )
231 PixelBufferPtr outBuffer = NewCrop( *this, x, y, cropDimensions );
232 TakeOwnershipOfBuffer( *outBuffer );
235 PixelBufferPtr PixelBuffer::NewCrop( const PixelBuffer& inBuffer, uint16_t x, uint16_t y, ImageDimensions cropDimensions )
237 PixelBufferPtr outBuffer = PixelBuffer::New( cropDimensions.GetWidth(), cropDimensions.GetHeight(), inBuffer.GetPixelFormat() );
238 int bytesPerPixel = Pixel::GetBytesPerPixel( inBuffer.mPixelFormat );
239 int srcStride = inBuffer.mWidth * bytesPerPixel;
240 int destStride = cropDimensions.GetWidth() * bytesPerPixel;
242 // Clamp crop to right edge
243 if( x + cropDimensions.GetWidth() > inBuffer.mWidth )
245 destStride = ( inBuffer.mWidth - x ) * bytesPerPixel;
248 int srcOffset = x * bytesPerPixel + y * srcStride;
250 unsigned char* destBuffer = outBuffer->mBuffer;
252 // Clamp crop to last row
253 unsigned int endRow = y + cropDimensions.GetHeight();
254 if( endRow > inBuffer.mHeight )
256 endRow = inBuffer.mHeight - 1 ;
258 for( uint16_t row = y; row < endRow; ++row )
260 memcpy(destBuffer + destOffset, inBuffer.mBuffer + srcOffset, destStride );
261 srcOffset += srcStride;
262 destOffset += destStride;
268 void PixelBuffer::Resize( ImageDimensions outDimensions )
270 if( mWidth != outDimensions.GetWidth() || mHeight != outDimensions.GetHeight() )
272 PixelBufferPtr outBuffer = NewResize( *this, outDimensions );
273 TakeOwnershipOfBuffer( *outBuffer );
277 PixelBufferPtr PixelBuffer::NewResize( const PixelBuffer& inBuffer, ImageDimensions outDimensions )
279 PixelBufferPtr outBuffer = PixelBuffer::New( outDimensions.GetWidth(), outDimensions.GetHeight(), inBuffer.GetPixelFormat() );
280 ImageDimensions inDimensions( inBuffer.mWidth, inBuffer.mHeight );
282 bool hasAlpha = Pixel::HasAlpha( inBuffer.mPixelFormat );
283 int bytesPerPixel = Pixel::GetBytesPerPixel( inBuffer.mPixelFormat );
285 Resampler::Filter filterType = Resampler::LANCZOS4;
286 if( inDimensions.GetWidth() < outDimensions.GetWidth() && inDimensions.GetHeight() < outDimensions.GetHeight() )
288 filterType = Resampler::MITCHELL;
291 // This method only really works for 8 bit wide channels.
292 // (But could be expanded to work)
293 if( inBuffer.mPixelFormat == Pixel::A8 ||
294 inBuffer.mPixelFormat == Pixel::L8 ||
295 inBuffer.mPixelFormat == Pixel::LA88 ||
296 inBuffer.mPixelFormat == Pixel::RGB888 ||
297 inBuffer.mPixelFormat == Pixel::RGB8888 ||
298 inBuffer.mPixelFormat == Pixel::BGR8888 ||
299 inBuffer.mPixelFormat == Pixel::RGBA8888 ||
300 inBuffer.mPixelFormat == Pixel::BGRA8888 )
302 Dali::Internal::Platform::Resample( inBuffer.mBuffer, inDimensions,
303 outBuffer->GetBuffer(), outDimensions,
304 filterType, bytesPerPixel, hasAlpha );
308 DALI_LOG_ERROR( "Trying to resize an image with too narrow a channel width" );
314 void PixelBuffer::ApplyGaussianBlur( const float blurRadius )
316 // This method only works for pixel buffer in RGBA format.
317 if( mWidth > 0 && mHeight > 0 && mPixelFormat == Pixel::RGBA8888 )
319 if ( blurRadius > Math::MACHINE_EPSILON_1 )
321 PerformGaussianBlurRGBA( *this, blurRadius );
326 DALI_LOG_ERROR( "Trying to apply gaussian blur to an empty pixel buffer or a pixel buffer not in RGBA format" );
330 void PixelBuffer::MultiplyColorByAlpha()
332 auto bytesPerPixel = Pixel::GetBytesPerPixel( mPixelFormat );
334 if( Pixel::HasAlpha(mPixelFormat) )
336 unsigned char* pixel = mBuffer;
337 const unsigned int bufferSize = mWidth * mHeight;
339 for( unsigned int i=0; i<bufferSize; ++i )
341 unsigned int alpha = ReadChannel( pixel, mPixelFormat, Adaptor::ALPHA );
343 auto red = ReadChannel( pixel, mPixelFormat, Adaptor::RED);
344 auto green = ReadChannel( pixel, mPixelFormat, Adaptor::GREEN);
345 auto blue = ReadChannel( pixel, mPixelFormat, Adaptor::BLUE);
346 auto luminance = ReadChannel( pixel, mPixelFormat, Adaptor::LUMINANCE);
347 WriteChannel( pixel, mPixelFormat, Adaptor::RED, red*alpha / 255 );
348 WriteChannel( pixel, mPixelFormat, Adaptor::GREEN, green*alpha/255 );
349 WriteChannel( pixel, mPixelFormat, Adaptor::BLUE, blue*alpha/255 );
350 WriteChannel( pixel, mPixelFormat, Adaptor::LUMINANCE, luminance*alpha/255 );
352 pixel += bytesPerPixel;
360 }// namespace Adaptor
361 }// namespace Internal