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"
38 PixelBuffer::PixelBuffer( unsigned char* buffer,
39 unsigned int bufferSize,
42 Dali::Pixel::Format pixelFormat )
44 mBufferSize( bufferSize ),
47 mPixelFormat( pixelFormat )
51 PixelBuffer::~PixelBuffer()
56 PixelBufferPtr PixelBuffer::New( unsigned int width,
58 Dali::Pixel::Format pixelFormat )
60 unsigned int bufferSize = width * height * Dali::Pixel::GetBytesPerPixel( pixelFormat );
61 unsigned char* buffer = NULL;
64 buffer = static_cast<unsigned char*>( malloc ( bufferSize ) );
66 return new PixelBuffer( buffer, bufferSize, width, height, pixelFormat );
69 PixelBufferPtr PixelBuffer::New( unsigned char* buffer,
70 unsigned int bufferSize,
73 Dali::Pixel::Format pixelFormat )
75 return new PixelBuffer( buffer, bufferSize, width, height, pixelFormat );
78 Dali::PixelData PixelBuffer::Convert( PixelBuffer& pixelBuffer )
80 Dali::PixelData pixelData = Dali::PixelData::New( pixelBuffer.mBuffer,
81 pixelBuffer.mBufferSize,
84 pixelBuffer.mPixelFormat,
85 Dali::PixelData::FREE );
86 pixelBuffer.mBuffer = NULL;
87 pixelBuffer.mWidth = 0;
88 pixelBuffer.mHeight = 0;
89 pixelBuffer.mBufferSize = 0;
94 unsigned int PixelBuffer::GetWidth() const
99 unsigned int PixelBuffer::GetHeight() const
104 Dali::Pixel::Format PixelBuffer::GetPixelFormat() const
109 unsigned char* PixelBuffer::GetBuffer() const
114 unsigned int PixelBuffer::GetBufferSize() const
119 Dali::PixelData PixelBuffer::CreatePixelData() const
121 unsigned char* destBuffer = NULL;
123 if( mBufferSize > 0 )
125 destBuffer = static_cast<unsigned char*>( malloc( mBufferSize ) );
126 memcpy( destBuffer, mBuffer, mBufferSize );
129 Dali::PixelData pixelData = Dali::PixelData::New( destBuffer, mBufferSize,
132 Dali::PixelData::FREE );
136 void PixelBuffer::ApplyMask( const PixelBuffer& mask )
141 Dali::Pixel::GetAlphaOffsetAndMask(mPixelFormat, byteOffset, bitMask);
143 if( Dali::Pixel::HasAlpha( mPixelFormat ) && bitMask == 255 )
145 ApplyMaskToAlphaChannel( *this, mask );
149 PixelBufferPtr newPixelBuffer = CreateNewMaskedBuffer( *this, mask );
152 // Take ownership of new buffer
153 mBuffer = newPixelBuffer->mBuffer;
154 newPixelBuffer->mBuffer = NULL;
155 mPixelFormat = newPixelBuffer->mPixelFormat;
156 mBufferSize = newPixelBuffer->mBufferSize;
158 // On leaving scope, newPixelBuffer will get destroyed.
162 void PixelBuffer::ReleaseBuffer()
171 }// namespace Adaptor
172 }// namespace Internal