1 #ifndef DALI_INTERNAL_ADAPTOR_PIXEL_BUFFER_H
2 #define DALI_INTERNAL_ADAPTOR_PIXEL_BUFFER_H
5 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
23 #include <dali/public-api/images/image-operations.h> // For ImageDimensions
24 #include <dali/public-api/images/pixel-data.h>
25 #include <dali/public-api/object/base-object.h>
26 #include <dali/public-api/object/property-map.h>
41 typedef IntrusivePtr<PixelBuffer> PixelBufferPtr;
43 class PixelBuffer : public BaseObject
48 * @brief Create a PixelBuffer object with a pre-allocated buffer.
49 * The PixelBuffer object owns this buffer, which may be retrieved
50 * and modified using GetBuffer().
52 * @param [in] width Buffer width in pixels
53 * @param [in] height Buffer height in pixels
54 * @param [in] pixelFormat The pixel format
56 static PixelBufferPtr New( unsigned int width,
58 Pixel::Format pixelFormat );
61 * @brief Create a PixelBuffer object. For internal use only.
63 * @param [in] buffer The raw pixel data.
64 * @param [in] bufferSize The size of the buffer in bytes
65 * @param [in] width Buffer width in pixels
66 * @param [in] height Buffer height in pixels
67 * @param [in] pixelFormat The pixel format
68 * @param [in] releaseFunction The function used to release the memory.
70 static PixelBufferPtr New( unsigned char* buffer,
71 unsigned int bufferSize,
74 Pixel::Format pixelFormat );
77 * Convert a pixelBuffer object into a PixelData object.
78 * The new object takes ownership of the buffer data, and the
79 * mBuffer pointer is reset to NULL.
80 * @param[in] pixelBuffer The buffer to convert
81 * @return the pixelData
83 static Dali::PixelData Convert( PixelBuffer& pixelBuffer );
88 * @param [in] buffer The raw pixel data.
89 * @param [in] bufferSize The size of the buffer in bytes
90 * @param [in] width Buffer width in pixels
91 * @param [in] height Buffer height in pixels
92 * @param [in] pixelFormat The pixel format
94 PixelBuffer( unsigned char* buffer,
95 unsigned int bufferSize,
98 Pixel::Format pixelFormat );
105 * Release the pixel buffer if exists.
112 * Get the width of the buffer in pixels.
113 * @return The width of the buffer in pixels
115 unsigned int GetWidth() const;
118 * Get the height of the buffer in pixels
119 * @return The height of the buffer in pixels
121 unsigned int GetHeight() const;
124 * Get the pixel format
125 * @return The pixel format
127 Pixel::Format GetPixelFormat() const;
130 * Get the pixel buffer if it's present.
131 * @return The buffer if exists, or NULL if there is no pixel buffer.
133 unsigned char* GetBuffer() const;
136 * @copydoc Devel::PixelBuffer::GetBuffer()
138 const unsigned char* const GetConstBuffer() const;
141 * Get the size of the buffer in bytes
142 * @return The size of the buffer
144 unsigned int GetBufferSize() const;
147 * Copy the buffer into a new PixelData
149 Dali::PixelData CreatePixelData() const;
152 * @brief Apply the mask to the current buffer.
154 * This method may update the internal object - e.g. the new buffer
155 * may have a different pixel format - as an alpha channel may be
157 * @param[in] mask The mask to apply to this pixel buffer
158 * @param[in] contentScale The scaling factor to apply to the content
159 * @param[in] cropToMask Whether to crop the output to the mask size (true) or scale the
160 * mask to the content size (false)
162 void ApplyMask( const PixelBuffer& mask, float contentScale, bool cropToMask );
165 * @brief Apply a Gaussian blur to the current buffer with the given radius.
167 * @param[in] blurRadius The radius for Gaussian blur
169 void ApplyGaussianBlur( const float blurRadius );
172 * Crops this buffer to the given crop rectangle. Assumes the crop rectangle
173 * is within the bounds of this size.
174 * @param[in] x The top left corner's X
175 * @param[in] y The top left corner's y
176 * @param[in] cropDimensions The dimensions of the crop
178 void Crop( uint16_t x, uint16_t y, ImageDimensions cropDimensions );
181 * Resizes the buffer to the given dimensions. Uses either Lanczos4 for downscaling
182 * or Mitchell for upscaling
183 * @param[in] outDimensions The new dimensions
185 void Resize( ImageDimensions outDimensions );
188 * Multiplies the image's color values by the alpha value. This provides better
189 * blending capability.
191 void MultiplyColorByAlpha();
194 * @brief Sets image metadata
196 * @param map Property map containing Exif fields
198 void SetMetadata( const Property::Map& map );
201 * @brief Returns image metadata as a property map
202 * @param[out] outMetadata Property map to copy the data into
203 * @return True on success
205 bool GetMetadata(Property::Map& outMetadata) const;
208 * @brief Sets metadata property map for the pixel buffer
209 * @note The function takes over the ownership of the property map
210 * @param[in] metadata Property map to copy the data into
212 void SetMetadata(std::unique_ptr<Property::Map> metadata);
215 * Allocates fixed amount of memory for the pixel data. Used by compressed formats.
216 * @param[in] size Size of memory to be allocated
218 void AllocateFixedSize( uint32_t size );
221 * @copydoc Devel::PixelBuffer::Rotate()
223 bool Rotate( Degree angle );
227 * Undefined copy constructor.
229 PixelBuffer(const PixelBuffer& other);
232 * Undefined assignment operator.
234 PixelBuffer& operator= (const PixelBuffer& other);
237 * Internal method to apply the mask to this buffer. Expects that they are the same size.
239 void ApplyMaskInternal( const PixelBuffer& mask );
242 * Takes ownership of the other object's pixel buffer.
244 void TakeOwnershipOfBuffer( PixelBuffer& pixelBuffer );
249 void ReleaseBuffer();
252 * Scales this buffer buffer by the given factor, and crops at the center to the
255 void ScaleAndCrop( float scaleFactor, ImageDimensions cropDimensions );
258 * Creates a new buffer which is a crop of the passed in buffer,
259 * using the given crop rectangle. Assumes the crop rectangle is
260 * within the bounds of this size.
261 * @param[in] inBuffer The source buffer
262 * @param[in] x The top left corner's X
263 * @param[in] y The top left corner's y
264 * @param[in] cropDimensions The dimensions of the crop
265 * @return the new pixel buffer
267 static PixelBufferPtr NewCrop( const PixelBuffer& inBuffer, uint16_t x, uint16_t y, ImageDimensions cropDimensions );
270 * Creates a new buffer which is a resized version of the passed in buffer.
271 * Uses either Lanczos4 for downscaling, or Mitchell for upscaling.
272 * @param[in] inBuffer The source buffer
273 * @param[in] outDimensions The new dimensions
274 * @return a new buffer of the given size.
276 static PixelBufferPtr NewResize( const PixelBuffer& inBuffer, ImageDimensions outDimensions );
280 std::unique_ptr<Property::Map> mMetadata; ///< Metadata fields
281 unsigned char* mBuffer; ///< The raw pixel data
282 unsigned int mBufferSize; ///< Buffer sized in bytes
283 unsigned int mWidth; ///< Buffer width in pixels
284 unsigned int mHeight; ///< Buffer height in pixels
285 Pixel::Format mPixelFormat; ///< Pixel format
288 } // namespace Adaptor
290 } // namespace Internal
293 * Helper methods for public API
295 inline Internal::Adaptor::PixelBuffer& GetImplementation( Devel::PixelBuffer& handle )
297 DALI_ASSERT_ALWAYS( handle && "handle is empty" );
299 BaseObject& object = handle.GetBaseObject();
301 return static_cast<Internal::Adaptor::PixelBuffer&>( object );
304 inline const Internal::Adaptor::PixelBuffer& GetImplementation( const Devel::PixelBuffer& handle )
306 DALI_ASSERT_ALWAYS( handle && "handle is empty" );
308 const BaseObject& object = handle.GetBaseObject();
310 return static_cast<const Internal::Adaptor::PixelBuffer&>( object );
315 #endif // DALI_INTERNAL_ADAPTOR_PIXEL_BUFFER_H