1 #ifndef DALI_INTERNAL_ADAPTOR_PIXEL_BUFFER_H
2 #define DALI_INTERNAL_ADAPTOR_PIXEL_BUFFER_H
5 * Copyright (c) 2022 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>
38 typedef IntrusivePtr<PixelBuffer> PixelBufferPtr;
40 class PixelBuffer : public BaseObject
44 * @brief Create a PixelBuffer object with a pre-allocated buffer.
45 * The PixelBuffer object owns this buffer, which may be retrieved
46 * and modified using GetBuffer().
48 * @param [in] width Buffer width in pixels
49 * @param [in] height Buffer height in pixels
50 * @param [in] pixelFormat The pixel format
52 static PixelBufferPtr New(unsigned int width,
54 Pixel::Format pixelFormat);
57 * @brief Create a PixelBuffer object. For internal use only.
59 * @param [in] buffer The raw pixel data.
60 * @param [in] bufferSize The size of the buffer in bytes
61 * @param [in] width Buffer width in pixels
62 * @param [in] height Buffer height in pixels
63 * @param [in] stride Buffer stride in pixels, 0 means the buffer is tightly packed
64 * @param [in] pixelFormat The pixel format
66 static PixelBufferPtr New(unsigned char* buffer,
67 unsigned int bufferSize,
71 Pixel::Format pixelFormat);
74 * Convert a pixelBuffer object into a PixelData object.
75 * The new object takes ownership of the buffer data, and the
76 * mBuffer pointer is reset to NULL.
77 * @param[in] pixelBuffer The buffer to convert
78 * @return the pixelData
80 static Dali::PixelData Convert(PixelBuffer& pixelBuffer);
85 * @param [in] buffer The raw pixel data.
86 * @param [in] bufferSize The size of the buffer in bytes
87 * @param [in] width Buffer width in pixels
88 * @param [in] height Buffer height in pixels
89 * @param [in] stride Buffer stride in pixels, 0 means the buffer is tightly packed
90 * @param [in] pixelFormat The pixel format
92 PixelBuffer(unsigned char* buffer,
93 unsigned int bufferSize,
97 Pixel::Format pixelFormat);
103 * Release the pixel buffer if exists.
105 ~PixelBuffer() override;
109 * Get the width of the buffer in pixels.
110 * @return The width of the buffer in pixels
112 unsigned int GetWidth() const;
115 * Get the height of the buffer in pixels
116 * @return The height of the buffer in pixels
118 unsigned int GetHeight() const;
121 * @brief Gets the stride of the buffer in pixels.
123 * @return The stride of the buffer in pixels. 0 means the buffer is tightly packed.
125 unsigned int GetStride() const;
128 * Get the pixel format
129 * @return The pixel format
131 Pixel::Format GetPixelFormat() const;
134 * Get the pixel buffer if it's present.
135 * @return The buffer if exists, or NULL if there is no pixel buffer.
137 unsigned char* GetBuffer() const;
140 * @copydoc Devel::PixelBuffer::GetBuffer()
142 const unsigned char* const GetConstBuffer() const;
145 * Get the size of the buffer in bytes
146 * @return The size of the buffer
148 unsigned int GetBufferSize() const;
151 * Copy the buffer into a new PixelData
153 Dali::PixelData CreatePixelData() const;
156 * @brief Apply the mask to the current buffer.
158 * This method may update the internal object - e.g. the new buffer
159 * may have a different pixel format - as an alpha channel may be
161 * @param[in] mask The mask to apply to this pixel buffer
162 * @param[in] contentScale The scaling factor to apply to the content
163 * @param[in] cropToMask Whether to crop the output to the mask size (true) or scale the
164 * mask to the content size (false)
166 void ApplyMask(const PixelBuffer& mask, float contentScale, bool cropToMask);
169 * @brief Apply a Gaussian blur to the current buffer with the given radius.
171 * @param[in] blurRadius The radius for Gaussian blur
173 void ApplyGaussianBlur(const float blurRadius);
176 * Crops this buffer to the given crop rectangle. Assumes the crop rectangle
177 * is within the bounds of this size.
178 * @param[in] x The top left corner's X
179 * @param[in] y The top left corner's y
180 * @param[in] cropDimensions The dimensions of the crop
182 void Crop(uint16_t x, uint16_t y, ImageDimensions cropDimensions);
185 * Resizes the buffer to the given dimensions. Uses either Lanczos4 for downscaling
186 * or Mitchell for upscaling
187 * @param[in] outDimensions The new dimensions
189 void Resize(ImageDimensions outDimensions);
192 * Multiplies the image's color values by the alpha value. This provides better
193 * blending capability.
195 void MultiplyColorByAlpha();
198 * @brief Sets image metadata
200 * @param map Property map containing Exif fields
202 void SetMetadata(const Property::Map& map);
205 * @brief Returns image metadata as a property map
206 * @param[out] outMetadata Property map to copy the data into
207 * @return True on success
209 bool GetMetadata(Property::Map& outMetadata) const;
212 * @brief Sets metadata property map for the pixel buffer
213 * @note The function takes over the ownership of the property map
214 * @param[in] metadata Property map to copy the data into
216 void SetMetadata(std::unique_ptr<Property::Map> metadata);
219 * Allocates fixed amount of memory for the pixel data. Used by compressed formats.
220 * @param[in] size Size of memory to be allocated
222 void AllocateFixedSize(uint32_t size);
225 * @copydoc Devel::PixelBuffer::Rotate()
227 bool Rotate(Degree angle);
230 * @copydoc Devel::PixelBuffer::IsAlphaPreMultiplied()
232 bool IsAlphaPreMultiplied() const;
235 * @copydoc Devel::PixelBuffer::GetBrightness()
237 uint32_t GetBrightness() const;
241 * Undefined copy constructor.
243 PixelBuffer(const PixelBuffer& other);
246 * Undefined assignment operator.
248 PixelBuffer& operator=(const PixelBuffer& other);
251 * Internal method to apply the mask to this buffer. Expects that they are the same size.
253 void ApplyMaskInternal(const PixelBuffer& mask);
256 * Takes ownership of the other object's pixel buffer.
258 void TakeOwnershipOfBuffer(PixelBuffer& pixelBuffer);
263 void ReleaseBuffer();
266 * Scales this buffer buffer by the given factor, and crops at the center to the
269 void ScaleAndCrop(float scaleFactor, ImageDimensions cropDimensions);
272 * Creates a new buffer which is a crop of the passed in buffer,
273 * using the given crop rectangle. Assumes the crop rectangle is
274 * within the bounds of this size.
275 * @param[in] inBuffer The source buffer
276 * @param[in] x The top left corner's X
277 * @param[in] y The top left corner's y
278 * @param[in] cropDimensions The dimensions of the crop
279 * @return the new pixel buffer
281 static PixelBufferPtr NewCrop(const PixelBuffer& inBuffer, uint16_t x, uint16_t y, ImageDimensions cropDimensions);
284 * Creates a new buffer which is a resized version of the passed in buffer.
285 * Uses either Lanczos4 for downscaling, or Mitchell for upscaling.
286 * @param[in] inBuffer The source buffer
287 * @param[in] outDimensions The new dimensions
288 * @return a new buffer of the given size.
290 static PixelBufferPtr NewResize(const PixelBuffer& inBuffer, ImageDimensions outDimensions);
293 std::unique_ptr<Property::Map> mMetadata; ///< Metadata fields
294 uint8_t* mBuffer; ///< The raw pixel data
295 uint32_t mBufferSize; ///< Buffer sized in bytes
296 uint32_t mWidth; ///< Buffer width in pixels
297 uint32_t mHeight; ///< Buffer height in pixels
298 uint32_t mStride; ///< Buffer stride in bytes, 0 means the buffer is tightly packed
299 Pixel::Format mPixelFormat; ///< Pixel format
300 bool mPreMultiplied; ///< PreMultiplied
303 } // namespace Adaptor
305 } // namespace Internal
308 * Helper methods for public API
310 inline Internal::Adaptor::PixelBuffer& GetImplementation(Devel::PixelBuffer& handle)
312 DALI_ASSERT_ALWAYS(handle && "handle is empty");
314 BaseObject& object = handle.GetBaseObject();
316 return static_cast<Internal::Adaptor::PixelBuffer&>(object);
319 inline const Internal::Adaptor::PixelBuffer& GetImplementation(const Devel::PixelBuffer& handle)
321 DALI_ASSERT_ALWAYS(handle && "handle is empty");
323 const BaseObject& object = handle.GetBaseObject();
325 return static_cast<const Internal::Adaptor::PixelBuffer&>(object);
330 #endif // DALI_INTERNAL_ADAPTOR_PIXEL_BUFFER_H