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>
40 PixelBuffer::PixelBuffer( unsigned char* buffer,
41 unsigned int bufferSize,
44 Dali::Pixel::Format pixelFormat )
46 mBufferSize( bufferSize ),
49 mPixelFormat( pixelFormat )
53 PixelBuffer::~PixelBuffer()
58 PixelBufferPtr PixelBuffer::New( unsigned int width,
60 Dali::Pixel::Format pixelFormat )
62 unsigned int bufferSize = width * height * Dali::Pixel::GetBytesPerPixel( pixelFormat );
63 unsigned char* buffer = NULL;
66 buffer = static_cast<unsigned char*>( malloc ( bufferSize ) );
68 return new PixelBuffer( buffer, bufferSize, width, height, pixelFormat );
71 PixelBufferPtr PixelBuffer::New( unsigned char* buffer,
72 unsigned int bufferSize,
75 Dali::Pixel::Format pixelFormat )
77 return new PixelBuffer( buffer, bufferSize, width, height, pixelFormat );
80 Dali::PixelData PixelBuffer::Convert( PixelBuffer& pixelBuffer )
82 Dali::PixelData pixelData = Dali::PixelData::New( pixelBuffer.mBuffer,
83 pixelBuffer.mBufferSize,
86 pixelBuffer.mPixelFormat,
87 Dali::PixelData::FREE );
88 pixelBuffer.mBuffer = NULL;
89 pixelBuffer.mWidth = 0;
90 pixelBuffer.mHeight = 0;
91 pixelBuffer.mBufferSize = 0;
96 unsigned int PixelBuffer::GetWidth() const
101 unsigned int PixelBuffer::GetHeight() const
106 Dali::Pixel::Format PixelBuffer::GetPixelFormat() const
111 unsigned char* PixelBuffer::GetBuffer() const
116 unsigned int PixelBuffer::GetBufferSize() const
121 Dali::PixelData PixelBuffer::CreatePixelData() const
123 unsigned char* destBuffer = NULL;
125 if( mBufferSize > 0 )
127 destBuffer = static_cast<unsigned char*>( malloc( mBufferSize ) );
128 memcpy( destBuffer, mBuffer, mBufferSize );
131 Dali::PixelData pixelData = Dali::PixelData::New( destBuffer, mBufferSize,
134 Dali::PixelData::FREE );
138 void PixelBuffer::ApplyMask( const PixelBuffer& inMask, float contentScale, bool cropToMask )
142 // First scale this buffer by the contentScale, and crop to the mask size
143 // If it's too small, then scale the mask to match the image size
144 // Then apply the mask
145 ScaleAndCrop( contentScale, ImageDimensions( inMask.GetWidth(), inMask.GetHeight() ) );
147 if( inMask.mWidth > mWidth || inMask.mHeight > mHeight )
149 PixelBufferPtr mask = NewResize( inMask, ImageDimensions( mWidth, mHeight ) );
150 ApplyMaskInternal( *mask );
154 ApplyMaskInternal( inMask );
159 // First, scale the mask to match the image size,
160 // then apply the mask.
161 PixelBufferPtr mask = NewResize( inMask, ImageDimensions( mWidth, mHeight ) );
162 ApplyMaskInternal( *mask );
166 void PixelBuffer::ApplyMaskInternal( const PixelBuffer& mask )
171 Dali::Pixel::GetAlphaOffsetAndMask(mPixelFormat, byteOffset, bitMask);
172 if( Dali::Pixel::HasAlpha( mPixelFormat ) && bitMask == 255 )
174 ApplyMaskToAlphaChannel( *this, mask );
178 PixelBufferPtr newPixelBuffer = CreateNewMaskedBuffer( *this, mask );
179 TakeOwnershipOfBuffer( *newPixelBuffer );
180 // On leaving scope, newPixelBuffer will get destroyed.
184 void PixelBuffer::TakeOwnershipOfBuffer( PixelBuffer& pixelBuffer )
188 // Take ownership of new buffer
189 mBuffer = pixelBuffer.mBuffer;
190 pixelBuffer.mBuffer = NULL;
191 mBufferSize = pixelBuffer.mBufferSize;
192 mWidth = pixelBuffer.mWidth;
193 mHeight = pixelBuffer.mHeight;
194 mPixelFormat = pixelBuffer.mPixelFormat;
197 void PixelBuffer::ReleaseBuffer()
205 void PixelBuffer::ScaleAndCrop( float scaleFactor, ImageDimensions cropDimensions )
207 ImageDimensions outDimensions( float(mWidth) * scaleFactor,
208 float(mHeight) * scaleFactor );
210 if( outDimensions.GetWidth() != mWidth || outDimensions.GetHeight() != mHeight )
212 Resize( outDimensions );
215 ImageDimensions postCropDimensions(
216 std::min(cropDimensions.GetWidth(), outDimensions.GetWidth()),
217 std::min(cropDimensions.GetHeight(), outDimensions.GetHeight()));
219 if( postCropDimensions.GetWidth() < outDimensions.GetWidth() ||
220 postCropDimensions.GetHeight() < outDimensions.GetHeight() )
222 uint16_t x = ( outDimensions.GetWidth() - postCropDimensions.GetWidth() ) / 2;
223 uint16_t y = ( outDimensions.GetHeight() - postCropDimensions.GetHeight() ) / 2;
224 Crop( x, y, postCropDimensions );
228 void PixelBuffer::Crop( uint16_t x, uint16_t y, ImageDimensions cropDimensions )
230 PixelBufferPtr outBuffer = NewCrop( *this, x, y, cropDimensions );
231 TakeOwnershipOfBuffer( *outBuffer );
234 PixelBufferPtr PixelBuffer::NewCrop( const PixelBuffer& inBuffer, uint16_t x, uint16_t y, ImageDimensions cropDimensions )
236 PixelBufferPtr outBuffer = PixelBuffer::New( cropDimensions.GetWidth(), cropDimensions.GetHeight(), inBuffer.GetPixelFormat() );
237 int bytesPerPixel = Pixel::GetBytesPerPixel( inBuffer.mPixelFormat );
238 int srcStride = inBuffer.mWidth * bytesPerPixel;
239 int destStride = cropDimensions.GetWidth() * bytesPerPixel;
241 // Clamp crop to right edge
242 if( x + cropDimensions.GetWidth() > inBuffer.mWidth )
244 destStride = ( inBuffer.mWidth - x ) * bytesPerPixel;
247 int srcOffset = x * bytesPerPixel + y * srcStride;
249 unsigned char* destBuffer = outBuffer->mBuffer;
251 // Clamp crop to last row
252 unsigned int endRow = y + cropDimensions.GetHeight();
253 if( endRow > inBuffer.mHeight )
255 endRow = inBuffer.mHeight - 1 ;
257 for( uint16_t row = y; row < endRow; ++row )
259 memcpy(destBuffer + destOffset, inBuffer.mBuffer + srcOffset, destStride );
260 srcOffset += srcStride;
261 destOffset += destStride;
267 void PixelBuffer::Resize( ImageDimensions outDimensions )
269 if( mWidth != outDimensions.GetWidth() || mHeight != outDimensions.GetHeight() )
271 PixelBufferPtr outBuffer = NewResize( *this, outDimensions );
272 TakeOwnershipOfBuffer( *outBuffer );
276 PixelBufferPtr PixelBuffer::NewResize( const PixelBuffer& inBuffer, ImageDimensions outDimensions )
278 PixelBufferPtr outBuffer = PixelBuffer::New( outDimensions.GetWidth(), outDimensions.GetHeight(), inBuffer.GetPixelFormat() );
279 ImageDimensions inDimensions( inBuffer.mWidth, inBuffer.mHeight );
281 bool hasAlpha = Pixel::HasAlpha( inBuffer.mPixelFormat );
282 int bytesPerPixel = Pixel::GetBytesPerPixel( inBuffer.mPixelFormat );
284 Resampler::Filter filterType = Resampler::LANCZOS4;
285 if( inDimensions.GetWidth() < outDimensions.GetWidth() && inDimensions.GetHeight() < outDimensions.GetHeight() )
287 filterType = Resampler::MITCHELL;
290 // This method only really works for 8 bit wide channels.
291 // (But could be expanded to work)
292 if( inBuffer.mPixelFormat == Pixel::A8 ||
293 inBuffer.mPixelFormat == Pixel::L8 ||
294 inBuffer.mPixelFormat == Pixel::LA88 ||
295 inBuffer.mPixelFormat == Pixel::RGB888 ||
296 inBuffer.mPixelFormat == Pixel::RGB8888 ||
297 inBuffer.mPixelFormat == Pixel::BGR8888 ||
298 inBuffer.mPixelFormat == Pixel::RGBA8888 ||
299 inBuffer.mPixelFormat == Pixel::BGRA8888 )
301 Dali::Internal::Platform::Resample( inBuffer.mBuffer, inDimensions,
302 outBuffer->GetBuffer(), outDimensions,
303 filterType, bytesPerPixel, hasAlpha );
307 DALI_LOG_ERROR( "Trying to resize an image with too narrow a channel width" );
313 void PixelBuffer::ApplyGaussianBlur( const float blurRadius )
315 // This method only works for pixel buffer in RGBA format.
316 if( mWidth > 0 && mHeight > 0 && mPixelFormat == Pixel::RGBA8888 )
318 if ( blurRadius > Math::MACHINE_EPSILON_1 )
320 PerformGaussianBlurRGBA( *this, blurRadius );
325 DALI_LOG_ERROR( "Trying to apply gaussian blur to an empty pixel buffer or a pixel buffer not in RGBA format" );
329 }// namespace Adaptor
330 }// namespace Internal