Merge "Added PixelBuffer for image loading and operations." into devel/master
[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/pixel-data.h>
24 #include <dali/public-api/object/base-object.h>
25
26 namespace Dali
27 {
28
29 namespace Internal
30 {
31
32 namespace Adaptor
33 {
34
35 class PixelBuffer;
36 typedef IntrusivePtr<PixelBuffer> PixelBufferPtr;
37
38 class PixelBuffer : public BaseObject
39 {
40 public:
41
42   /**
43    * @brief Create a PixelBuffer object with a pre-allocated buffer.
44    * The PixelBuffer object owns this buffer, which may be retrieved
45    * and modified using GetBuffer().
46    *
47    * @param [in] width            Buffer width in pixels
48    * @param [in] height           Buffer height in pixels
49    * @param [in] pixelFormat      The pixel format
50    */
51   static PixelBufferPtr New( unsigned int width,
52                              unsigned int height,
53                              Pixel::Format pixelFormat );
54
55   /**
56    * @brief Create a PixelBuffer object. For internal use only.
57    *
58    * @param [in] buffer           The raw pixel data.
59    * @param [in] bufferSize       The size of the buffer in bytes
60    * @param [in] width            Buffer width in pixels
61    * @param [in] height           Buffer height in pixels
62    * @param [in] pixelFormat      The pixel format
63    * @param [in] releaseFunction  The function used to release the memory.
64    */
65   static PixelBufferPtr New( unsigned char* buffer,
66                              unsigned int bufferSize,
67                              unsigned int width,
68                              unsigned int height,
69                              Pixel::Format pixelFormat );
70
71   /**
72    * Convert a pixelBuffer object into a PixelData object.
73    * The new object takes ownership of the buffer data, and the
74    * mBuffer pointer is reset to NULL.
75    * @param[in] pixelBuffer The buffer to convert
76    * @return the pixelData
77    */
78   static Dali::PixelData Convert( PixelBuffer& pixelBuffer );
79
80   /**
81    * @brief Constructor.
82    *
83    * @param [in] buffer           The raw pixel data.
84    * @param [in] bufferSize       The size of the buffer in bytes
85    * @param [in] width            Buffer width in pixels
86    * @param [in] height           Buffer height in pixels
87    * @param [in] pixelFormat      The pixel format
88    */
89   PixelBuffer( unsigned char* buffer,
90                unsigned int bufferSize,
91                unsigned int width,
92                unsigned int height,
93                Pixel::Format pixelFormat );
94
95 protected:
96
97   /**
98    * @brief Destructor.
99    *
100    * Release the pixel buffer if exists.
101    */
102   ~PixelBuffer();
103
104 public:
105
106   /**
107    * Get the width of the buffer in pixels.
108    * @return The width of the buffer in pixels
109    */
110   unsigned int GetWidth() const;
111
112   /**
113    * Get the height of the buffer in pixels
114    * @return The height of the buffer in pixels
115    */
116   unsigned int GetHeight() const;
117
118   /**
119    * Get the pixel format
120    * @return The pixel format
121    */
122   Pixel::Format GetPixelFormat() const;
123
124   /**
125    * Get the pixel buffer if it's present.
126    * @return The buffer if exists, or NULL if there is no pixel buffer.
127    */
128   unsigned char* GetBuffer() const;
129
130   /**
131    * Get the size of the buffer in bytes
132    * @return The size of the buffer
133    */
134   unsigned int GetBufferSize() const;
135
136   /**
137    * Copy the buffer into a new PixelData
138    */
139   Dali::PixelData CreatePixelData() const;
140
141   /**
142    * Apply the mask to the current buffer. This method may update the
143    * internal object - e.g. the new buffer may have a different pixel
144    * format - as an alpha channel may be added.
145    * @param[in] mask The mask to apply to this pixel buffer
146    */
147   void ApplyMask( const PixelBuffer& mask );
148
149 private:
150   /*
151    * Undefined copy constructor.
152    */
153   PixelBuffer(const PixelBuffer& other);
154
155   /*
156    * Undefined assignment operator.
157    */
158   PixelBuffer& operator= (const PixelBuffer& other);
159
160   /**
161    * Release the buffer
162    */
163   void ReleaseBuffer();
164
165 private:
166
167   unsigned char* mBuffer;           ///< The raw pixel data
168   unsigned int   mBufferSize;       ///< Buffer sized in bytes
169   unsigned int   mWidth;            ///< Buffer width in pixels
170   unsigned int   mHeight;           ///< Buffer height in pixels
171   Pixel::Format  mPixelFormat;      ///< Pixel format
172 };
173
174 } // namespace Adaptor
175
176 } // namespace Internal
177
178 /**
179  * Helper methods for public API
180  */
181 inline Internal::Adaptor::PixelBuffer& GetImplementation( Devel::PixelBuffer& handle )
182 {
183   DALI_ASSERT_ALWAYS( handle && "handle is empty" );
184
185   BaseObject& object = handle.GetBaseObject();
186
187   return static_cast<Internal::Adaptor::PixelBuffer&>( object );
188 }
189
190 inline const Internal::Adaptor::PixelBuffer& GetImplementation( const Devel::PixelBuffer& handle )
191 {
192   DALI_ASSERT_ALWAYS( handle && "handle is empty" );
193
194   const BaseObject& object = handle.GetBaseObject();
195
196   return static_cast<const Internal::Adaptor::PixelBuffer&>( object );
197 }
198
199 } // namespace Dali
200
201 #endif // __DALI_INTERNAL_ADAPTOR_PIXEL_BUFFER_H__