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/integration-api/debug.h>
24 #include <dali/public-api/images/image-operations.h> // For ImageDimensions
25 #include <dali/public-api/images/pixel-data.h>
26 #include <dali/public-api/object/base-object.h>
27 #include <dali/public-api/object/property-map.h>
39 typedef IntrusivePtr<PixelBuffer> PixelBufferPtr;
41 class PixelBuffer : public BaseObject
45 * @brief Create a PixelBuffer object with a pre-allocated buffer.
46 * The PixelBuffer object owns this buffer, which may be retrieved
47 * and modified using GetBuffer().
49 * @param [in] width Buffer width in pixels
50 * @param [in] height Buffer height in pixels
51 * @param [in] pixelFormat The pixel format
53 static PixelBufferPtr New(uint32_t width,
55 Pixel::Format pixelFormat);
58 * @brief Create a PixelBuffer object. For internal use only.
60 * @param [in] buffer The raw pixel data.
61 * @param [in] bufferSize The size of the buffer in bytes
62 * @param [in] width Buffer width in pixels
63 * @param [in] height Buffer height in pixels
64 * @param [in] stride Buffer stride in pixels, 0 means the buffer is tightly packed
65 * @param [in] pixelFormat The pixel format
67 static PixelBufferPtr New(uint8_t* buffer,
72 Pixel::Format pixelFormat);
75 * Convert a pixelBuffer object into a PixelData object.
76 * The new object takes ownership of the buffer data, and the
77 * mBuffer pointer is reset to NULL.
78 * @param[in] pixelBuffer The buffer to convert
79 * @return the pixelData
81 static Dali::PixelData Convert(PixelBuffer& pixelBuffer);
86 * @param [in] buffer The raw pixel data.
87 * @param [in] bufferSize The size of the buffer in bytes
88 * @param [in] width Buffer width in pixels
89 * @param [in] height Buffer height in pixels
90 * @param [in] stride Buffer stride in pixels, 0 means the buffer is tightly packed
91 * @param [in] pixelFormat The pixel format
93 PixelBuffer(uint8_t* buffer,
98 Pixel::Format pixelFormat);
104 * Release the pixel buffer if exists.
106 ~PixelBuffer() override;
110 * Get the total allocated size of current pixel buffers
112 static uint32_t GetTotalAllocatedSize()
114 #if defined(DEBUG_ENABLED)
115 return gPixelBufferAllocationTotal;
122 * Get the width of the buffer in pixels.
123 * @return The width of the buffer in pixels
125 uint32_t GetWidth() const;
128 * Get the height of the buffer in pixels
129 * @return The height of the buffer in pixels
131 uint32_t GetHeight() const;
134 * @brief Gets the stride of the buffer in pixels.
135 * @return The stride of the buffer in pixels. 0 means the buffer is tightly packed.
137 uint32_t GetStride() const;
140 * Get the pixel format
141 * @return The pixel format
143 Pixel::Format GetPixelFormat() const;
146 * Get the pixel buffer if it's present.
147 * @return The buffer if exists, or NULL if there is no pixel buffer.
149 uint8_t* GetBuffer() const;
152 * @copydoc Devel::PixelBuffer::GetBuffer()
154 const uint8_t* GetConstBuffer() const;
157 * Get the size of the buffer in bytes
158 * @return The size of the buffer
160 uint32_t GetBufferSize() const;
163 * Copy the buffer into a new PixelData
165 Dali::PixelData CreatePixelData() const;
168 * @brief Apply the mask to the current buffer.
170 * This method may update the internal object - e.g. the new buffer
171 * may have a different pixel format - as an alpha channel may be
173 * @param[in] mask The mask to apply to this pixel buffer
174 * @param[in] contentScale The scaling factor to apply to the content
175 * @param[in] cropToMask Whether to crop the output to the mask size (true) or scale the
176 * mask to the content size (false)
178 void ApplyMask(const PixelBuffer& mask, float contentScale, bool cropToMask);
181 * @brief Apply a Gaussian blur to the current buffer with the given radius.
183 * @param[in] blurRadius The radius for Gaussian blur
185 void ApplyGaussianBlur(const float blurRadius);
188 * Crops this buffer to the given crop rectangle. Assumes the crop rectangle
189 * is within the bounds of this size.
190 * @param[in] x The top left corner's X
191 * @param[in] y The top left corner's y
192 * @param[in] cropDimensions The dimensions of the crop
194 void Crop(uint16_t x, uint16_t y, ImageDimensions cropDimensions);
197 * Resizes the buffer to the given dimensions. Uses either Lanczos4 for downscaling
198 * or Mitchell for upscaling
199 * @param[in] outDimensions The new dimensions
201 void Resize(ImageDimensions outDimensions);
204 * Multiplies the image's color values by the alpha value. This provides better
205 * blending capability.
207 void MultiplyColorByAlpha();
210 * @brief Sets image metadata
212 * @param map Property map containing Exif fields
214 void SetMetadata(const Property::Map& map);
217 * @brief Returns image metadata as a property map
218 * @param[out] outMetadata Property map to copy the data into
219 * @return True on success
221 bool GetMetadata(Property::Map& outMetadata) const;
224 * @brief Sets metadata property map for the pixel buffer
225 * @note The function takes over the ownership of the property map
226 * @param[in] metadata Property map to copy the data into
228 void SetMetadata(std::unique_ptr<Property::Map> metadata);
231 * Allocates fixed amount of memory for the pixel data. Used by compressed formats.
232 * @param[in] size Size of memory to be allocated
234 void AllocateFixedSize(uint32_t size);
237 * @copydoc Devel::PixelBuffer::Rotate()
239 bool Rotate(Degree angle);
242 * @copydoc Devel::PixelBuffer::IsAlphaPreMultiplied()
244 bool IsAlphaPreMultiplied() const;
247 * @copydoc Devel::PixelBuffer::GetBrightness()
249 uint32_t GetBrightness() const;
253 * Undefined copy constructor.
255 PixelBuffer(const PixelBuffer& other);
258 * Undefined assignment operator.
260 PixelBuffer& operator=(const PixelBuffer& other);
263 * Internal method to apply the mask to this buffer. Expects that they are the same size.
265 void ApplyMaskInternal(const PixelBuffer& mask);
268 * Takes ownership of the other object's pixel buffer.
270 void TakeOwnershipOfBuffer(PixelBuffer& pixelBuffer);
275 void ReleaseBuffer();
278 * Scales this buffer buffer by the given factor, and crops at the center to the
281 void ScaleAndCrop(float scaleFactor, ImageDimensions cropDimensions);
284 * Creates a new buffer which is a crop of the passed in buffer,
285 * using the given crop rectangle. Assumes the crop rectangle is
286 * within the bounds of this size.
287 * @param[in] inBuffer The source buffer
288 * @param[in] x The top left corner's X
289 * @param[in] y The top left corner's y
290 * @param[in] cropDimensions The dimensions of the crop
291 * @return the new pixel buffer
293 static PixelBufferPtr NewCrop(const PixelBuffer& inBuffer, uint16_t x, uint16_t y, ImageDimensions cropDimensions);
296 * Creates a new buffer which is a resized version of the passed in buffer.
297 * Uses either Lanczos4 for downscaling, or Mitchell for upscaling.
298 * @param[in] inBuffer The source buffer
299 * @param[in] outDimensions The new dimensions
300 * @return a new buffer of the given size.
302 static PixelBufferPtr NewResize(const PixelBuffer& inBuffer, ImageDimensions outDimensions);
305 std::unique_ptr<Property::Map> mMetadata; ///< Metadata fields
306 uint8_t* mBuffer; ///< The raw pixel data
307 uint32_t mBufferSize; ///< Buffer sized in bytes
308 uint32_t mWidth; ///< Buffer width in pixels
309 uint32_t mHeight; ///< Buffer height in pixels
310 uint32_t mStride; ///< Buffer stride in bytes, 0 means the buffer is tightly packed
311 Pixel::Format mPixelFormat; ///< Pixel format
312 bool mPreMultiplied; ///< PreMultiplied
314 #if defined(DEBUG_ENABLED)
315 static uint32_t gPixelBufferAllocationTotal;
319 } // namespace Adaptor
321 } // namespace Internal
324 * Helper methods for public API
326 inline Internal::Adaptor::PixelBuffer& GetImplementation(Devel::PixelBuffer& handle)
328 DALI_ASSERT_ALWAYS(handle && "handle is empty");
330 BaseObject& object = handle.GetBaseObject();
332 return static_cast<Internal::Adaptor::PixelBuffer&>(object);
335 inline const Internal::Adaptor::PixelBuffer& GetImplementation(const Devel::PixelBuffer& handle)
337 DALI_ASSERT_ALWAYS(handle && "handle is empty");
339 const BaseObject& object = handle.GetBaseObject();
341 return static_cast<const Internal::Adaptor::PixelBuffer&>(object);
346 #endif // DALI_INTERNAL_ADAPTOR_PIXEL_BUFFER_H