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 * @brief Create a new instance of a Bitmap with the profile asked for.
91 * @return Pointer to created Bitmap subclass. Clients should immediately
92 * wrap this in a reference-counting smart pointer or store it in a similarly
93 * automatic owning collection.
94 * @param[in] profile Defines required features of the bitmap (\sa Profile).
95 * @param[in] managePixelBuffer If true, the bitmap object owns it's own
96 * buffer of pixel data and can delete it, else the lifetime of the pixel
97 * buffer is managed by an external component and is guaranteed to remain
98 * dereferenceable at least as long as the Bitmap remains alive.
100 static Bitmap* New(Profile profile, bool managePixelBuffer);
103 * @brief Create a new instance of a Bitmap with a profile appropriate to the
104 * pixel format requested.
106 * The allocated buffer is owned by the instance and is not referenced
107 * externally, so its lifetime can be managed by the instance.
108 * @return Pointer to created Bitmap subclass. Clients should immediately
109 * wrap this in a reference-counting smart pointer or store it in a similarly
110 * automatic owning collection.
111 * @param[in] format Defines image data and required features of the bitmap.
113 static Bitmap* New(Pixel::Format format);
115 /** \name GeneralFeatures
116 * Features that are generic between profiles. */
120 * Get the width of the image
121 * @return The width of the image
123 unsigned int GetImageWidth() const
129 * Get the height of the image
130 * @return The height of the image
132 unsigned int GetImageHeight() const
138 * Get the pixel format
139 * @return The pixel format
141 Pixel::Format GetPixelFormat() const
147 * Get the pixel buffer
148 * @return The buffer. You can modify its contents.
149 * @sa ReserveBuffer GetBufferSize
151 virtual PixelBuffer* GetBuffer()
153 DALI_ASSERT_DEBUG(mData != NULL);
158 * Get the pixel buffer size in bytes
159 * @return The buffer size in bytes.
160 * @sa ReserveBuffer GetBuffer
162 virtual size_t GetBufferSize() const = 0;
165 * Queries if the bitmap has an alpha channel
166 * @return true if there is an alpha channel
168 bool HasAlphaChannel() const
170 return mHasAlphaChannel;
174 * @brief Sets whether the alpha channel is used.
176 * The definition of "used" is that at least one of the the pixels of the
177 * bitmap has an alpha value that is less than 1.0.
178 * @param[in] alphaChannelUsed If true, the alpha channel will be marked as
179 * used, else it will be marked as unused.
181 void SetAlphaChannelUsed( bool alphaChannelUsed )
183 mHasAlphaChannel = alphaChannelUsed;
187 * Queries if the bitmap has any transparent data
188 * @return true if the bitmap has alpha data
190 bool IsFullyOpaque() const
192 // check pixel format for alpha channel
193 return !( HasAlphaChannel() && mAlphaChannelUsed );
196 /**@}*/ ///< End of generic features
199 /** \name PackedPixelsProfile
200 * Features that are active only if the Bitmap was created with a
201 * BITMAP_2D_PACKED_PIXELS profile. */
204 class PackedPixelsProfile
209 * (Re-)Allocate pixel buffer for the Bitmap. Any previously allocated pixel buffer is deleted.
210 * Dali has ownership of the buffer, but its contents can be modified.
211 * Bitmap stores given size information about the image.
212 * @pre bufferWidth, bufferHeight have to be power of two
213 * @param[in] pixelFormat pixel format
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
218 * @return pixel buffer pointer
220 virtual PixelBuffer* ReserveBuffer(Pixel::Format pixelFormat,
223 unsigned int bufferWidth = 0,
224 unsigned int bufferHeight = 0) = 0;
227 * Assign a pixel buffer. Any previously allocated pixel buffer is deleted.
228 * Dali has ownership of the buffer, but it iss allowable to modify its
229 * contents after it is assigned, but before it is used.
230 * Bitmap stores the provided size information about the image.
232 * The buffer must have been allocated with the C++ array new operator, not
233 * with malloc or as a local or static object. The precise form is as follows:
235 * PixelBuffer * buffer = new PixelBuffer[bufSize];
237 * @pre bufferWidth, bufferHeight have to be power of two
238 * @param[in] pixelFormat pixel format
239 * @param[in] buffer the pixel buffer
240 * @param[in] bufferSize size of the pixel buffer
241 * @param[in] width Image width in pixels
242 * @param[in] height Image height in pixels
243 * @param[in] bufferWidth Buffer width (stride) in pixels
244 * @param[in] bufferHeight Buffer height in pixels
246 virtual void AssignBuffer(Pixel::Format pixelFormat,
248 std::size_t bufferSize,
251 unsigned int bufferWidth = 0,
252 unsigned int bufferHeight = 0) = 0;
254 * Get the width of the buffer (stride)
255 * @return The width of the buffer in pixels
257 virtual unsigned int GetBufferWidth() const = 0;
260 * Get the height of the buffer
261 * @return The height of the buffer in pixels
263 virtual unsigned int GetBufferHeight() const = 0;
266 * Get the pixel buffer stride.
267 * @return The buffer stride (in bytes) if this is bitmap of non-compressed
268 * packed pixels for which a stride is meaningful or 0 otherwise.
270 virtual unsigned int GetBufferStride() const = 0;
273 * Check the bitmap data and test whether it has any transparent pixels.
274 * This property can then be tested for with IsFullyOpaque().
276 virtual void TestForTransparency() = 0;
280 * Get interface to features that are active only if the Bitmap was created
281 * with a BITMAP_2D_PACKED_PIXELS profile. */
282 virtual const PackedPixelsProfile* GetPackedPixelsProfile() const { return 0; }
284 * Get interface to features that are active only if the Bitmap was created
285 * with a BITMAP_2D_PACKED_PIXELS profile. */
286 virtual PackedPixelsProfile* GetPackedPixelsProfile() { return 0; }
288 /**@}*/ ///< End of packed pixel features.
291 /** \name CompressedProfile
292 * Features that only apply to opaque/compressed formats. */
295 class CompressedProfile
299 * (Re-)Allocate pixel buffer for the Bitmap. Any previously allocated pixel buffer
301 * Dali has ownership of the buffer, and contents are opaque and immutable.
302 * Bitmap stores given size information about the image which the client is assumed
303 * to have retrieved from out-of-band image metadata.
304 * @param[in] pixelFormat pixel format
305 * @param[in] width Image width in pixels
306 * @param[in] height Image height in pixels
307 * @param[in] bufferSize Buffer size in bytes
308 * @return pixel buffer pointer
310 virtual PixelBuffer* ReserveBufferOfSize( Pixel::Format pixelFormat,
311 const unsigned width,
312 const unsigned height,
313 const size_t numBytes ) = 0;
316 virtual const CompressedProfile* GetCompressedProfile() const { return 0; }
317 virtual CompressedProfile* GetCompressedProfile() { return 0; }
322 * Inform the bitmap that its pixel buffer is no longer required and can be
323 * deleted to free up memory if the bitmap owns the buffer.
325 void DiscardBuffer();
328 * Check if the pixel buffer can be discarded
329 * @return true if the pixel buffer can be discarded
331 bool IsDiscardable() const
337 * Transfer ownership of the pixel buffer to the calling function.
338 * @post bitmaps pixel data is set to NULL
339 * @return the bitmaps pixel buffer
341 PixelBuffer* ReleaseBuffer();
343 * Delete the pixel buffer data
345 void DeletePixelBuffer();
348 * A reference counted object may only be deleted by calling Unreference()
354 unsigned int mImageWidth; ///< Image width in pixels
355 unsigned int mImageHeight; ///< Image height in pixels
356 Pixel::Format mPixelFormat; ///< Pixel format
357 bool mHasAlphaChannel; ///< Whether the image has an alpha channel
358 bool mAlphaChannelUsed; ///< Whether the alpha channel is used in case the image owns one.
359 PixelBuffer* mData; ///< Raw pixel data
363 bool mDiscardable; ///< Should delete the buffer when discard buffer is called.
365 Bitmap(const Bitmap& other); ///< defined private to prevent use
366 Bitmap& operator = (const Bitmap& other); ///< defined private to prevent use
368 // Changes scope, should be at end of class
369 DALI_LOG_OBJECT_STRING_DECLARATION;
373 typedef IntrusivePtr<ImageData> ImageDataPtr;
376 * Make a Bitmap instance from an ImageData object, destructively.
377 * @param[in] imageData A populated buffer of image data. Its buffer may be
378 * null on exit, so it should not be used again.
379 * @returns Bitmap owning the image data buffer passed in or a copy of it.
381 BitmapPtr ConvertToBitmap(ImageData& imageData);
383 } // namespace Integration
387 #endif // __DALI_INTEGRATION_BITMAP_H__