1 #ifndef __DALI_INTEGRATION_BITMAP_H__
2 #define __DALI_INTEGRATION_BITMAP_H__
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 // Licensed under the Flora License, Version 1.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
11 // http://floralicense.org/license/
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.
23 #include <dali/integration-api/debug.h>
24 #include <dali/public-api/common/intrusive-ptr.h>
25 #include <dali/public-api/images/pixel.h>
26 #include <dali/public-api/object/ref-object.h>
35 * Returns GL data type and internal format for specified pixel format
36 * @param[in] pixelformat pixel format (eg. Pixel::RGBA32)
37 * @param[out] pixelDataType pixel data type (eg. GL_UNSIGNED_BYTE)
38 * @param[out] internalFormat pixel internal format (eg. GL_RGBA)
40 DALI_IMPORT_API void ConvertToGlFormat(Pixel::Format pixelformat, unsigned& pixelDataType, unsigned& internalFormat);
43 typedef IntrusivePtr<Bitmap> BitmapPtr;
44 typedef unsigned char PixelBuffer; ///< Pixel data buffers are composed of these
48 * An abstract container for image data.
49 * \sa{BitmapPackedPixel BitmapCompressed BitmapExternal} for concrete
52 class DALI_IMPORT_API Bitmap : public Dali::RefObject
57 * Use the static function Bitmap::Create() to create instances.
58 * @param[in] discardable Flag to tell the bitmap if it can delete the buffer with the pixel data.
59 * @param[in] pixBuf External buffer of pixel data or null.
61 Bitmap( bool discardable = false, Dali::Integration::PixelBuffer* pixBuf = 0 );
64 * Initializes internal class members
65 * @param[in] pixelFormat pixel format
66 * @param[in] width Image width in pixels
67 * @param[in] height Image height in pixels
69 void Initialize(Pixel::Format pixelFormat,
75 /** Defines the characteristics of the Bitmap returned from the factory
79 /** A 2D array of pixels where each pixel is a whole number of bytes
80 * and each scanline of the backing memory buffer may have additional
81 * bytes off the right edge if requested, and there may be additional
82 * scanlines past the bottom of the image in the buffer if requested.*/
83 BITMAP_2D_PACKED_PIXELS,
84 /** The data for the bitmap is buffered in an opaque form.*/
89 * Create a new instance of a Bitmap with the profile asked for.
90 * @return Pointer to created Bitmap subclass. Clients should immediately
91 * wrap this in a reference-counting smart pointer or store it in a similarly
92 * automatic owning collection.
93 * @param[in] profile Defines required features of the bitmap (\sa Profile).
94 * @param[in] managePixelBuffer If true, the bitmap object owns it's own
95 * buffer of pixel data and can delete it, else the lifetime of the pixel
96 * buffer is managed by an external component and is guaranteed to remain
97 * dereferenceable at least as long as the Bitmap remains alive.
99 static Bitmap* New(Profile profile, bool managePixelBuffer);
101 /** \name GeneralFeatures
102 * Features that are generic between profiles. */
106 * Get the width of the image
107 * @return The width of the image
109 unsigned int GetImageWidth() const
115 * Get the height of the image
116 * @return The height of the image
118 unsigned int GetImageHeight() const
124 * Get the pixel format
125 * @return The pixel format
127 Pixel::Format GetPixelFormat() const
133 * Get the pixel buffer
134 * @return The buffer. You can modify its contents.
135 * @sa ReserveBuffer GetBufferSize
137 virtual PixelBuffer* GetBuffer()
139 DALI_ASSERT_DEBUG(mData != NULL);
144 * Get the pixel buffer size in bytes
145 * @return The buffer size in bytes.
146 * @sa ReserveBuffer GetBuffer
148 virtual size_t GetBufferSize() const = 0;
151 * Queries if the bitmap has an alpha channel
152 * @return true if there is an alpha channel
154 bool HasAlphaChannel() const
156 return mHasAlphaChannel;
160 * Queries if the bitmap has any transparent data
161 * @return true if the bitmap has alpha data
163 bool IsFullyOpaque() const
165 // check pixel format for alpha channel
166 return !(HasAlphaChannel() && mAlphaChannelUsed);
169 /**@}*/ ///< End of generic features
172 /** \name PackedPixelsProfile
173 * Features that are active only if the Bitmap was created with a
174 * BITMAP_2D_PACKED_PIXELS profile. */
177 class PackedPixelsProfile
182 * (Re-)Allocate pixel buffer for the Bitmap. Any previously allocated pixel buffer is deleted.
183 * Dali has ownership of the buffer, but its contents can be modified.
184 * Bitmap stores given size information about the image.
185 * @pre bufferWidth, bufferHeight have to be power of two
186 * @param[in] pixelFormat pixel format
187 * @param[in] width Image width in pixels
188 * @param[in] height Image height in pixels
189 * @param[in] bufferWidth Buffer width (stride) in pixels
190 * @param[in] bufferHeight Buffer height in pixels
191 * @return pixel buffer pointer
193 virtual PixelBuffer* ReserveBuffer(Pixel::Format pixelFormat,
196 unsigned int bufferWidth = 0,
197 unsigned int bufferHeight = 0) = 0;
200 * Assign a pixel buffer. Any previously allocated pixel buffer is deleted.
201 * Dali has ownership of the buffer, but it iss allowable to modify its
202 * contents after it is assigned, but before it is used.
203 * Bitmap stores the provided size information about the image.
205 * The buffer must have been allocated with the C++ array new operator, not
206 * with malloc or as a local or static object. The precise form is as follows:
208 * PixelBuffer * buffer = new PixelBuffer[bufSize];
210 * @pre bufferWidth, bufferHeight have to be power of two
211 * @param[in] pixelFormat pixel format
212 * @param[in] buffer the pixel buffer
213 * @param[in] bufferSize size of the pixel buffer
214 * @param[in] width Image width in pixels
215 * @param[in] height Image height in pixels
216 * @param[in] bufferWidth Buffer width (stride) in pixels
217 * @param[in] bufferHeight Buffer height in pixels
219 virtual void AssignBuffer(Pixel::Format pixelFormat,
221 std::size_t bufferSize,
224 unsigned int bufferWidth = 0,
225 unsigned int bufferHeight = 0) = 0;
227 * Get the width of the buffer (stride)
228 * @return The width of the buffer in pixels
230 virtual unsigned int GetBufferWidth() const = 0;
233 * Get the height of the buffer
234 * @return The height of the buffer in pixels
236 virtual unsigned int GetBufferHeight() const = 0;
239 * Get the pixel buffer stride.
240 * @return The buffer stride (in bytes) if this is bitmap of non-compressed
241 * packed pixels for which a stride is meaningful or 0 otherwise.
243 virtual unsigned int GetBufferStride() const = 0;
246 * Check the bitmap data and test whether it has any transparent pixels.
247 * This property can then be tested for with IsFullyOpaque().
249 virtual void TestForTransparency() = 0;
253 * Get interface to features that are active only if the Bitmap was created
254 * with a BITMAP_2D_PACKED_PIXELS profile. */
255 virtual const PackedPixelsProfile* GetPackedPixelsProfile() const { return 0; }
257 * Get interface to features that are active only if the Bitmap was created
258 * with a BITMAP_2D_PACKED_PIXELS profile. */
259 virtual PackedPixelsProfile* GetPackedPixelsProfile() { return 0; }
261 /**@}*/ ///< End of packed pixel features.
264 /** \name CompressedProfile
265 * Features that only apply to opaque/compressed formats. */
268 class CompressedProfile
272 * (Re-)Allocate pixel buffer for the Bitmap. Any previously allocated pixel buffer
274 * Dali has ownership of the buffer, and contents are opaque and immutable.
275 * Bitmap stores given size information about the image which the client is assumed
276 * to have retrieved from out-of-band image metadata.
277 * @param[in] pixelFormat pixel format
278 * @param[in] width Image width in pixels
279 * @param[in] height Image height in pixels
280 * @param[in] bufferSize Buffer size in bytes
281 * @return pixel buffer pointer
283 virtual PixelBuffer* ReserveBufferOfSize( Pixel::Format pixelFormat,
284 const unsigned width,
285 const unsigned height,
286 const size_t numBytes ) = 0;
289 virtual const CompressedProfile* GetCompressedProfile() const { return 0; }
290 virtual CompressedProfile* GetCompressedProfile() { return 0; }
295 * Inform the bitmap that its pixel buffer is no longer required and can be
296 * deleted to free up memory if the bitmap owns the buffer.
298 void DiscardBuffer();
301 * @return True if the buffer of pixel data is owned by this Bitmap itself,
302 * or false if the buffer is owned by an external component.
304 bool BufferIsOwned() const
310 * Transfer ownership of the pixel buffer to the calling function.
311 * @post bitmaps pixel data is set to NULL
312 * @return the bitmaps pixel buffer
314 PixelBuffer* ReleaseBuffer();
316 * Delete the pixel buffer data
318 void DeletePixelBuffer();
321 * A reference counted object may only be deleted by calling Unreference()
327 unsigned int mImageWidth; ///< Image width in pixels
328 unsigned int mImageHeight; ///< Image height in pixels
329 Pixel::Format mPixelFormat; ///< Pixel format
330 bool mHasAlphaChannel; ///< Whether the image has an alpha channel
331 bool mAlphaChannelUsed; ///< Whether the alpha channel is used in case the image owns one.
332 PixelBuffer* mData; ///< Raw pixel data
336 bool mDataIsOwned; ///< Should delete the buffer when discard buffer is called.
338 Bitmap(const Bitmap& other); ///< defined private to prevent use
339 Bitmap& operator = (const Bitmap& other); ///< defined private to prevent use
341 // Changes scope, should be at end of class
342 DALI_LOG_OBJECT_STRING_DECLARATION;
345 } // namespace Integration
349 #endif // __DALI_INTEGRATION_BITMAP_H__