Added Crop and Resize APIs to PixelBuffer
[platform/core/uifw/dali-adaptor.git] / adaptors / devel-api / adaptor-framework / pixel-buffer.h
1 #ifndef DALI_PIXEL_BUFFER_H
2 #define DALI_PIXEL_BUFFER_H
3
4 /*
5  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 #include <dali/public-api/images/pixel.h>
22 #include <dali/public-api/images/pixel-data.h>
23 #include <dali/public-api/object/base-handle.h>
24
25 namespace Dali
26 {
27
28 namespace Internal
29 {
30 namespace Adaptor
31 {
32 class PixelBuffer;
33 }
34 }
35
36
37 // Use namespace to separate from PixelBuffer typedef in buffer-image.h
38 namespace Devel
39 {
40
41 /**
42  * @brief The PixelBuffer object holds a pixel buffer.
43  *
44  * The PixelBuffer keeps ownership of it's initial buffer however, the
45  * user is free to modify the pixel data, either directly, or via
46  * image operations.
47  *
48  * In order to upload the pixel data to texture memory, there are two
49  * possibilities - either convert it back to a PixelData object, which
50  * releases the PixelBuffer object, leaving the user with an empty handle
51  * (ideal for one-time indirect image manipulation), or create a new
52  * PixelData object from this object, leaving the buffer intact (ideal
53  * for continuous manipulation)
54  *
55  * @SINCE_1_2.46
56  */
57 class DALI_IMPORT_API PixelBuffer : public BaseHandle
58 {
59 public:
60
61   /**
62    * Create a PixelBuffer with it's own data buffer.
63    */
64   static PixelBuffer New( unsigned int width,
65                           unsigned int height,
66                           Dali::Pixel::Format pixelFormat );
67
68   /**
69    * @brief Creates an empty handle.
70    * Use PixelBuffer::New() to create an initialized object.
71    *
72    * @SINCE_1_2.46
73    */
74   PixelBuffer();
75
76   /**
77    * @brief Destructor.
78    *
79    * @SINCE_1_2.46
80    */
81   ~PixelBuffer();
82
83   /**
84    * @brief This copy constructor is required for (smart) pointer semantics.
85    *
86    * @SINCE_1_2.46
87    * @param[in] handle A reference to the copied handle
88    */
89   PixelBuffer(const PixelBuffer& handle);
90
91   /**
92    * @brief This assignment operator is required for (smart) pointer semantics.
93    *
94    * @SINCE_1_2.46
95    * @param[in] rhs A reference to the copied handle
96    * @return A reference to this object
97    */
98   PixelBuffer& operator=(const PixelBuffer& rhs);
99
100   /**
101    * Convert to a pixel data and release the pixelBuffer's object.
102    * This handle is left empty.
103    *
104    * @warning Any other handles that keep a reference to this object
105    * will be left with no buffer, trying to access it will return NULL.
106    *
107    * @SINCE_1_2.46
108    * @param[in,out] pixelBuffer
109    * @return a new PixelData which takes ownership of the PixelBuffer's buffer.
110    */
111   static PixelData Convert( PixelBuffer& pixelBuffer );
112
113   /**
114    * Copy the data from this object into a new PixelData object, which could be
115    * used for uploading to a texture.
116    * @return a new PixelData object containing a copy of this pixel buffer's data.
117    */
118   Dali::PixelData CreatePixelData() const;
119
120   /**
121    * @brief Gets the pixel buffer. This is a pointer to the internal
122    * pixel buffer.
123    *
124    * @warning If there is no pixel buffer (e.g. this object has been
125    * converted to a PixelData), this method will return NULL.
126    *
127    * @SINCE_1_2.46
128    * @return The pixel buffer, or NULL.
129    */
130   unsigned char* GetBuffer();
131
132   /**
133    * @brief Gets the width of the buffer in pixels.
134    *
135    * @SINCE_1_2.46
136    * @return The width of the buffer in pixels
137    */
138   unsigned int GetWidth() const;
139
140   /**
141    * @brief Gets the height of the buffer in pixels.
142    *
143    * @SINCE_1_2.46
144    * @return The height of the buffer in pixels
145    */
146   unsigned int GetHeight() const;
147
148   /**
149    * @brief Gets the pixel format.
150    *
151    * @SINCE_1_2.46
152    * @return The pixel format
153    */
154   Pixel::Format GetPixelFormat() const;
155
156   /**
157    * Apply the mask to this pixel data, and return a new pixel data containing
158    * the masked image. If this PixelBuffer doesn't have an alpha channel, then
159    * the resultant PixelBuffer will be converted to a format that supports at
160    * least the width of the color channels and the alpha channel from the mask.
161    *
162    * If cropToMask is set to true, then the contentScale is applied first to
163    * this buffer, and the target buffer is cropped to the size of the mask. If
164    * it's set to false, then the mask is scaled to match this buffer's size
165    * before the mask is applied.
166    *
167    * @param[in] mask The mask to apply.
168    * @param[in] contentScale The scaling factor to apply to the content
169    * @param[in] cropToMask Whether to crop the output to the mask size (true)
170    * or scale the mask to the content size (false)
171    */
172   void ApplyMask( PixelBuffer mask, float contentScale=1.0f, bool cropToMask=false );
173
174   /**
175    * Apply a Gaussian blur to this pixel data with the given radius.
176    *
177    * @note A bigger radius will yield a blurrier image. Only works for pixel data in RGBA format.
178    *
179    * @param[in] blurRadius The radius for Gaussian blur. A value of 0 or negative value indicates no blur.
180    */
181   void ApplyGaussianBlur( const float blurRadius );
182
183   /**
184    * @brief Crops this buffer to the given crop rectangle.
185    *
186    * The crop rectangle will be clamped to the edges of the buffer if it is larger.
187    * @param[in] x The top left corner's X
188    * @param[in] y The top left corner's y
189    * @param[in] width The crop width
190    * @param[in] height The crop height
191    */
192   void Crop( uint16_t x, uint16_t y, uint16_t width, uint16_t height );
193
194   /**
195    * @brief Resizes the buffer to the given dimensions.
196    *
197    * Uses either Lanczos4 for downscaling or Mitchell for upscaling
198    * @param[in] width The new width
199    * @param[in] height The new height
200    */
201   void Resize( uint16_t width, uint16_t height );
202
203 public:
204
205   /**
206    * @brief The constructor.
207    * @note  Not intended for application developers.
208    * @SINCE_1_2.46
209    * @param[in] pointer A pointer to a newly allocated PixelBuffer
210    */
211   explicit DALI_INTERNAL PixelBuffer( Internal::Adaptor::PixelBuffer* pointer );
212 };
213
214 } // namespace Devel
215 } // namespace Dali
216
217 #endif // DALI_PIXEL_BUFFER_H