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