2b5782300bcdbb497e0717be574983ca5fe8a311
[platform/core/uifw/dali-adaptor.git] / adaptors / common / pixel-buffer-impl.h
1 #ifndef DALI_INTERNAL_ADAPTOR_PIXEL_BUFFER_H
2 #define DALI_INTERNAL_ADAPTOR_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 // INTERNAL INCLUDES
22 #include <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
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 namespace Adaptor
34 {
35
36 class PixelBuffer;
37 typedef IntrusivePtr<PixelBuffer> PixelBufferPtr;
38
39 class PixelBuffer : public BaseObject
40 {
41 public:
42
43   /**
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().
47    *
48    * @param [in] width            Buffer width in pixels
49    * @param [in] height           Buffer height in pixels
50    * @param [in] pixelFormat      The pixel format
51    */
52   static PixelBufferPtr New( unsigned int width,
53                              unsigned int height,
54                              Pixel::Format pixelFormat );
55
56   /**
57    * @brief Create a PixelBuffer object. For internal use only.
58    *
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] pixelFormat      The pixel format
64    * @param [in] releaseFunction  The function used to release the memory.
65    */
66   static PixelBufferPtr New( unsigned char* buffer,
67                              unsigned int bufferSize,
68                              unsigned int width,
69                              unsigned int height,
70                              Pixel::Format pixelFormat );
71
72   /**
73    * Convert a pixelBuffer object into a PixelData object.
74    * The new object takes ownership of the buffer data, and the
75    * mBuffer pointer is reset to NULL.
76    * @param[in] pixelBuffer The buffer to convert
77    * @return the pixelData
78    */
79   static Dali::PixelData Convert( PixelBuffer& pixelBuffer );
80
81   /**
82    * @brief Constructor.
83    *
84    * @param [in] buffer           The raw pixel data.
85    * @param [in] bufferSize       The size of the buffer in bytes
86    * @param [in] width            Buffer width in pixels
87    * @param [in] height           Buffer height in pixels
88    * @param [in] pixelFormat      The pixel format
89    */
90   PixelBuffer( unsigned char* buffer,
91                unsigned int bufferSize,
92                unsigned int width,
93                unsigned int height,
94                Pixel::Format pixelFormat );
95
96 protected:
97
98   /**
99    * @brief Destructor.
100    *
101    * Release the pixel buffer if exists.
102    */
103   ~PixelBuffer();
104
105 public:
106
107   /**
108    * Get the width of the buffer in pixels.
109    * @return The width of the buffer in pixels
110    */
111   unsigned int GetWidth() const;
112
113   /**
114    * Get the height of the buffer in pixels
115    * @return The height of the buffer in pixels
116    */
117   unsigned int GetHeight() const;
118
119   /**
120    * Get the pixel format
121    * @return The pixel format
122    */
123   Pixel::Format GetPixelFormat() const;
124
125   /**
126    * Get the pixel buffer if it's present.
127    * @return The buffer if exists, or NULL if there is no pixel buffer.
128    */
129   unsigned char* GetBuffer() const;
130
131   /**
132    * Get the size of the buffer in bytes
133    * @return The size of the buffer
134    */
135   unsigned int GetBufferSize() const;
136
137   /**
138    * Copy the buffer into a new PixelData
139    */
140   Dali::PixelData CreatePixelData() const;
141
142   /**
143    * @brief Apply the mask to the current buffer.
144    *
145    * This method may update the internal object - e.g. the new buffer
146    * may have a different pixel format - as an alpha channel may be
147    * added.
148    * @param[in] mask The mask to apply to this pixel buffer
149    * @param[in] contentScale The scaling factor to apply to the content
150    * @param[in] cropToMask Whether to crop the output to the mask size (true) or scale the
151    * mask to the content size (false)
152    */
153   void ApplyMask( const PixelBuffer& mask, float contentScale, bool cropToMask );
154
155   /**
156    * @brief Apply a Gaussian blur to the current buffer with the given radius.
157    *
158    * @param[in] blurRadius The radius for Gaussian blur
159    */
160   void ApplyGaussianBlur( const float blurRadius );
161
162   /**
163    * Crops this buffer to the given crop rectangle. Assumes the crop rectangle
164    * is within the bounds of this size.
165    * @param[in] x The top left corner's X
166    * @param[in] y The top left corner's y
167    * @param[in] cropDimensions The dimensions of the crop
168    */
169   void Crop( uint16_t x, uint16_t y, ImageDimensions cropDimensions );
170
171   /**
172    * Resizes the buffer to the given dimensions. Uses either Lanczos4 for downscaling
173    * or Mitchell for upscaling
174    * @param[in] outDimensions The new dimensions
175    */
176   void Resize( ImageDimensions outDimensions );
177
178   /**
179    * Multiplies the image's color values by the alpha value. This provides better
180    * blending capability.
181    */
182   void MultiplyColorByAlpha();
183
184 private:
185   /*
186    * Undefined copy constructor.
187    */
188   PixelBuffer(const PixelBuffer& other);
189
190   /*
191    * Undefined assignment operator.
192    */
193   PixelBuffer& operator= (const PixelBuffer& other);
194
195   /**
196    * Internal method to apply the mask to this buffer. Expects that they are the same size.
197    */
198   void ApplyMaskInternal( const PixelBuffer& mask );
199
200   /**
201    * Takes ownership of the other object's pixel buffer.
202    */
203   void TakeOwnershipOfBuffer( PixelBuffer& pixelBuffer );
204
205   /**
206    * Release the buffer
207    */
208   void ReleaseBuffer();
209
210   /**
211    * Scales this buffer buffer by the given factor, and crops at the center to the
212    * given dimensions.
213    */
214   void ScaleAndCrop( float scaleFactor, ImageDimensions cropDimensions );
215
216   /**
217    * Creates a new buffer which is a crop of the passed in buffer,
218    * using the given crop rectangle. Assumes the crop rectangle is
219    * within the bounds of this size.
220    * @param[in] inBuffer The source buffer
221    * @param[in] x The top left corner's X
222    * @param[in] y The top left corner's y
223    * @param[in] cropDimensions The dimensions of the crop
224    * @return the new pixel buffer
225    */
226   static PixelBufferPtr NewCrop( const PixelBuffer& inBuffer, uint16_t x, uint16_t y, ImageDimensions cropDimensions );
227
228   /**
229    * Creates a new buffer which is a resized version of the passed in buffer.
230    * Uses either Lanczos4 for downscaling, or Mitchell for upscaling.
231    * @param[in] inBuffer The source buffer
232    * @param[in] outDimensions The new dimensions
233    * @return a new buffer of the given size.
234    */
235   static PixelBufferPtr NewResize( const PixelBuffer& inBuffer, ImageDimensions outDimensions );
236
237
238 private:
239
240   unsigned char* mBuffer;           ///< The raw pixel data
241   unsigned int   mBufferSize;       ///< Buffer sized in bytes
242   unsigned int   mWidth;            ///< Buffer width in pixels
243   unsigned int   mHeight;           ///< Buffer height in pixels
244   Pixel::Format  mPixelFormat;      ///< Pixel format
245 };
246
247 } // namespace Adaptor
248
249 } // namespace Internal
250
251 /**
252  * Helper methods for public API
253  */
254 inline Internal::Adaptor::PixelBuffer& GetImplementation( Devel::PixelBuffer& handle )
255 {
256   DALI_ASSERT_ALWAYS( handle && "handle is empty" );
257
258   BaseObject& object = handle.GetBaseObject();
259
260   return static_cast<Internal::Adaptor::PixelBuffer&>( object );
261 }
262
263 inline const Internal::Adaptor::PixelBuffer& GetImplementation( const Devel::PixelBuffer& handle )
264 {
265   DALI_ASSERT_ALWAYS( handle && "handle is empty" );
266
267   const BaseObject& object = handle.GetBaseObject();
268
269   return static_cast<const Internal::Adaptor::PixelBuffer&>( object );
270 }
271
272 } // namespace Dali
273
274 #endif // __DALI_INTERNAL_ADAPTOR_PIXEL_BUFFER_H__