6c49edee5e8bc0758bc6e80b4de942c3e1755b82
[platform/core/uifw/dali-core.git] / dali / internal / event / images / pixel-data-impl.h
1 #ifndef DALI_INTERNAL_PIXEL_DATA_H
2 #define DALI_INTERNAL_PIXEL_DATA_H
3
4 /*
5  * Copyright (c) 2016 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 <dali/public-api/images/pixel-data.h>
23 #include <dali/public-api/object/base-object.h>
24
25 namespace Dali
26 {
27
28 namespace Internal
29 {
30
31 class PixelData;
32 typedef IntrusivePtr<PixelData> PixelDataPtr;
33
34 class PixelData : public BaseObject
35 {
36 public:
37
38   /**
39    * @brief Create a PixelData object.
40    *
41    * @param [in] buffer           The raw pixel data.
42    * @param [in] bufferSize       The size of the buffer in bytes
43    * @param [in] width            Buffer width in pixels
44    * @param [in] height           Buffer height in pixels
45    * @param [in] pixelFormat      The pixel format
46    * @param [in] releaseFunction  The function used to release the memory.
47    */
48   static PixelDataPtr New( unsigned char* buffer,
49                            unsigned int bufferSize,
50                            unsigned int width,
51                            unsigned int height,
52                            Pixel::Format pixelFormat,
53                            Dali::PixelData::ReleaseFunction releaseFunction);
54
55   /**
56    * @brief Constructor.
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   PixelData( unsigned char* buffer,
66              unsigned int bufferSize,
67              unsigned int width,
68              unsigned int height,
69              Pixel::Format pixelFormat,
70              Dali::PixelData::ReleaseFunction releaseFunction );
71
72 protected:
73
74   /**
75    * @brief Destructor.
76    *
77    * Release the pixel buffer if exists.
78    */
79   ~PixelData();
80
81 public:
82
83   /**
84    * Get the width of the buffer in pixels.
85    * @return The width of the buffer in pixels
86    */
87   unsigned int GetWidth() const;
88
89   /**
90    * Get the height of the buffer in pixels
91    * @return The height of the buffer in pixels
92    */
93   unsigned int GetHeight() const;
94
95   /**
96    * Get the pixel format
97    * @return The pixel format
98    */
99   Pixel::Format GetPixelFormat() const;
100
101   /**
102    * Get the pixel buffer if it's present.
103    * @return The buffer if exists, or NULL if there is no pixel buffer.
104    */
105   unsigned char* GetBuffer() const;
106
107   /**
108    * Get the size of the buffer in bytes
109    * @return The size of the buffer
110    */
111   unsigned int GetBufferSize() const;
112
113   /**
114    * Apply the mask to this data
115    * @param[in] mask The mask to apply
116    */
117   void ApplyMask( const PixelData& mask );
118
119 private:
120   /**
121    * Release the buffer
122    */
123   void ReleaseBuffer();
124
125   /**
126    * Apply the mask to this data's alpha channel
127    * @param[in] mask The mask to apply
128    */
129   void ApplyMaskToAlphaChannel( const PixelData& mask );
130
131   /**
132    * Convert to RGBA8888 and apply the mask's alpha channel
133    * to this data's alpha channel
134    * @param[in] mask The mask to apply
135    */
136   void AddAlphaChannel( const PixelData& mask );
137
138   /**
139    * Apply the mask to this data's color channels (e.g. to apply vignette)
140    * @param[in] mask The mask to apply
141    */
142   void ApplyMaskToColorChannels( const PixelData& mask );
143
144   /**
145    * Read a weighted sample from a pixel (of unknown size)
146    * @param[in] x The x coordinate to sample from
147    * @param[in] y The y coordinate to sample from
148    */
149   float ReadWeightedSample( float x, float y ) const;
150
151    /*
152    * Undefined copy constructor.
153    */
154   PixelData(const PixelData& other);
155
156   /*
157    * Undefined assignment operator.
158    */
159   PixelData& operator= (const PixelData& other);
160
161 private:
162
163   unsigned char* mBuffer;           ///< The raw pixel data
164   unsigned int   mBufferSize;       ///< Buffer sized in bytes
165   unsigned int   mWidth;            ///< Buffer width in pixels
166   unsigned int   mHeight;           ///< Buffer height in pixels
167   Pixel::Format  mPixelFormat;      ///< Pixel format
168   Dali::PixelData::ReleaseFunction mReleaseFunction;  ///< Function for releasing memory
169 };
170
171 } // namespace Internal
172
173 /**
174  * Helper methods for public API
175  */
176 inline Internal::PixelData& GetImplementation( Dali::PixelData& handle )
177 {
178   DALI_ASSERT_ALWAYS( handle && "handle is empty" );
179
180   BaseObject& object = handle.GetBaseObject();
181
182   return static_cast<Internal::PixelData&>( object );
183 }
184
185 inline const Internal::PixelData& GetImplementation( const Dali::PixelData& handle )
186 {
187   DALI_ASSERT_ALWAYS( handle && "handle is empty" );
188
189   const BaseObject& object = handle.GetBaseObject();
190
191   return static_cast<const Internal::PixelData&>( object );
192 }
193
194 } // namespace Dali
195
196 #endif // __DALI_INTERNAL_PIXEL_DATA_H__