1 #ifndef DALI_PIXEL_BUFFER_H
2 #define DALI_PIXEL_BUFFER_H
5 * Copyright (c) 2017 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/public-api/images/pixel.h>
23 #include <dali/public-api/images/pixel-data.h>
24 #include <dali/public-api/object/base-handle.h>
38 // Use namespace to separate from PixelBuffer typedef in buffer-image.h
43 * @brief The PixelBuffer object holds a pixel buffer.
45 * The PixelBuffer keeps ownership of it's initial buffer however, the
46 * user is free to modify the pixel data, either directly, or via
49 * In order to upload the pixel data to texture memory, there are two
50 * possibilities - either convert it back to a PixelData object, which
51 * releases the PixelBuffer object, leaving the user with an empty handle
52 * (ideal for one-time indirect image manipulation), or create a new
53 * PixelData object from this object, leaving the buffer intact (ideal
54 * for continuous manipulation)
58 class DALI_IMPORT_API PixelBuffer : public BaseHandle
63 * Create a PixelBuffer with it's own data buffer.
65 static PixelBuffer New( unsigned int width,
67 Dali::Pixel::Format pixelFormat );
70 * @brief Creates an empty handle.
71 * Use PixelBuffer::New() to create an initialized object.
85 * @brief This copy constructor is required for (smart) pointer semantics.
88 * @param[in] handle A reference to the copied handle
90 PixelBuffer(const PixelBuffer& handle);
93 * @brief This assignment operator is required for (smart) pointer semantics.
96 * @param[in] rhs A reference to the copied handle
97 * @return A reference to this object
99 PixelBuffer& operator=(const PixelBuffer& rhs);
102 * Convert to a pixel data and release the pixelBuffer's object.
103 * This handle is left empty.
105 * @warning Any other handles that keep a reference to this object
106 * will be left with no buffer, trying to access it will return NULL.
109 * @param[in,out] pixelBuffer
110 * @return a new PixelData which takes ownership of the PixelBuffer's buffer.
112 static PixelData Convert( PixelBuffer& pixelBuffer );
115 * Copy the data from this object into a new PixelData object, which could be
116 * used for uploading to a texture.
117 * @return a new PixelData object containing a copy of this pixel buffer's data.
119 Dali::PixelData CreatePixelData() const;
122 * @brief Gets the pixel buffer. This is a pointer to the internal
125 * @warning If there is no pixel buffer (e.g. this object has been
126 * converted to a PixelData), this method will return NULL.
129 * @return The pixel buffer, or NULL.
131 unsigned char* GetBuffer();
134 * @brief Gets the width of the buffer in pixels.
137 * @return The width of the buffer in pixels
139 unsigned int GetWidth() const;
142 * @brief Gets the height of the buffer in pixels.
145 * @return The height of the buffer in pixels
147 unsigned int GetHeight() const;
150 * @brief Gets the pixel format.
153 * @return The pixel format
155 Pixel::Format GetPixelFormat() const;
158 * Apply the mask to this pixel data, and return a new pixel data containing
159 * the masked image. If this PixelBuffer doesn't have an alpha channel, then
160 * the resultant PixelBuffer will be converted to a format that supports at
161 * least the width of the color channels and the alpha channel from the mask.
163 * If cropToMask is set to true, then the contentScale is applied first to
164 * this buffer, and the target buffer is cropped to the size of the mask. If
165 * it's set to false, then the mask is scaled to match this buffer's size
166 * before the mask is applied.
168 * @param[in] mask The mask to apply.
169 * @param[in] contentScale The scaling factor to apply to the content
170 * @param[in] cropToMask Whether to crop the output to the mask size (true)
171 * or scale the mask to the content size (false)
173 void ApplyMask( PixelBuffer mask, float contentScale=1.0f, bool cropToMask=false );
176 * Apply a Gaussian blur to this pixel data with the given radius.
178 * @note A bigger radius will yield a blurrier image. Only works for pixel data in RGBA format.
180 * @param[in] blurRadius The radius for Gaussian blur. A value of 0 or negative value indicates no blur.
182 void ApplyGaussianBlur( const float blurRadius );
185 * @brief Crops this buffer to the given crop rectangle.
187 * The crop rectangle will be clamped to the edges of the buffer if it is larger.
188 * @param[in] x The top left corner's X
189 * @param[in] y The top left corner's y
190 * @param[in] width The crop width
191 * @param[in] height The crop height
193 void Crop( uint16_t x, uint16_t y, uint16_t width, uint16_t height );
196 * @brief Resizes the buffer to the given dimensions.
198 * Uses either Lanczos4 for downscaling or Mitchell for upscaling
199 * @param[in] width The new width
200 * @param[in] height The new height
202 void Resize( uint16_t width, uint16_t height );
205 * @brief Multiplies the image's color values by the alpha value. This provides better
206 * blending capability.
208 void MultiplyColorByAlpha();
211 * @brief Returns Exif metadata as a property map
213 * @param[out] metadata Property map object to write into
214 * @return True on success
216 bool GetMetadata( Property::Map& metadata ) const;
219 * @brief Rotates the pixel buffer by the given angle.
221 * @note Operation valid for pixel formats: A8, L8, LA88, RGB888, RGB8888, BGR8888, RGBA8888 and BGRA8888. Does nothing otherwise.
222 * @note The operation does nothing for angles equivalent to 0 degrees: -360, 360, 720, etc.
223 * @note If the pixel buffer does rotate, all the pointers to the internal pixel buffer retrieved by the method GetPixelBuffer() become invalid.
225 * @param[in] angle The angle in degrees.
227 void Rotate( Degree angle );
232 * @brief The constructor.
233 * @note Not intended for application developers.
235 * @param[in] pointer A pointer to a newly allocated PixelBuffer
237 explicit DALI_INTERNAL PixelBuffer( Internal::Adaptor::PixelBuffer* pointer );
243 #endif // DALI_PIXEL_BUFFER_H