1 #ifndef __DALI_INTEGRATION_BITMAP_H__
2 #define __DALI_INTEGRATION_BITMAP_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <dali/integration-api/debug.h>
25 #include <dali/public-api/common/intrusive-ptr.h>
26 #include <dali/public-api/images/pixel.h>
27 #include <dali/public-api/object/ref-object.h>
28 #include <dali/integration-api/resource-policies.h>
37 * Returns GL data type and internal format for specified pixel format
38 * @param[in] pixelformat pixel format (eg. Pixel::RGBA32)
39 * @param[out] pixelDataType pixel data type (eg. GL_UNSIGNED_BYTE)
40 * @param[out] internalFormat pixel internal format (eg. GL_RGBA)
42 DALI_IMPORT_API void ConvertToGlFormat(Pixel::Format pixelformat, unsigned& pixelDataType, unsigned& internalFormat);
45 typedef IntrusivePtr<Bitmap> BitmapPtr;
46 typedef unsigned char PixelBuffer; ///< Pixel data buffers are composed of these
50 * An abstract container for image data.
51 * \sa{BitmapPackedPixel BitmapCompressed BitmapExternal} for concrete
54 class DALI_IMPORT_API Bitmap : public Dali::RefObject
60 * Use the static function Bitmap::New() to create instances.
61 * @param[in] discardable Flag to tell the bitmap if it can delete the buffer with the pixel data.
62 * @param[in] pixBuf External buffer of pixel data or null.
64 Bitmap( ResourcePolicy::Discardable discardable = ResourcePolicy::RETAIN, Dali::Integration::PixelBuffer* pixBuf = 0 );
67 * Initializes internal class members
68 * @param[in] pixelFormat pixel format
69 * @param[in] width Image width in pixels
70 * @param[in] height Image height in pixels
72 void Initialize(Pixel::Format pixelFormat,
78 /** Defines the characteristics of the Bitmap returned from the factory
82 /** A 2D array of pixels where each pixel is a whole number of bytes
83 * and each scanline of the backing memory buffer may have additional
84 * bytes off the right edge if requested, and there may be additional
85 * scanlines past the bottom of the image in the buffer if requested.*/
86 BITMAP_2D_PACKED_PIXELS,
87 /** The data for the bitmap is buffered in an opaque form.*/
92 * Create a new instance of a Bitmap with the required profile.
93 * @return Pointer to created Bitmap subclass. Clients should immediately
94 * wrap this in a reference-counting smart pointer or store it in a similarly
95 * automatic owning collection.
96 * @param[in] profile Defines required features of the bitmap (\sa Profile).
97 * @param[in] discardable If this is set to DISCARD, the bitmap
98 * object owns it's own buffer of pixel data and can delete it. If
99 * it's set to RETAIN, then the lifetime of the pixel buffer is
100 * managed by an external component and is guaranteed to remain
101 * dereferenceable at least as long as the Bitmap remains alive.
103 static Bitmap* New( Profile profile, ResourcePolicy::Discardable discardable );
105 /** \name GeneralFeatures
106 * Features that are generic between profiles. */
110 * Get the width of the image
111 * @return The width of the image
113 unsigned int GetImageWidth() const
119 * Get the height of the image
120 * @return The height of the image
122 unsigned int GetImageHeight() const
128 * Get the pixel format
129 * @return The pixel format
131 Pixel::Format GetPixelFormat() const
137 * Get the pixel buffer if it's present.
138 * @return The buffer if present, or NULL if there is no pixel buffer.
139 * You can modify its contents.
140 * @sa ReserveBuffer GetBufferSize
142 virtual PixelBuffer* GetBuffer()
148 * Get the pixel buffer size in bytes
149 * @return The buffer size in bytes.
150 * @sa ReserveBuffer GetBuffer
152 virtual size_t GetBufferSize() const = 0;
155 * Queries if the bitmap has an alpha channel
156 * @return true if there is an alpha channel
158 bool HasAlphaChannel() const
160 return mHasAlphaChannel;
164 * Queries if the bitmap has any transparent data
165 * @return true if the bitmap has alpha data
167 bool IsFullyOpaque() const
169 // check pixel format for alpha channel
170 return !(HasAlphaChannel() && mAlphaChannelUsed);
173 /**@}*/ ///< End of generic features
176 /** \name PackedPixelsProfile
177 * Features that are active only if the Bitmap was created with a
178 * BITMAP_2D_PACKED_PIXELS profile. */
181 class PackedPixelsProfile
186 * (Re-)Allocate pixel buffer for the Bitmap. Any previously allocated pixel buffer is deleted.
187 * Dali has ownership of the buffer, but its contents can be modified.
188 * Bitmap stores given size information about the image.
189 * @pre bufferWidth, bufferHeight have to be power of two
190 * @param[in] pixelFormat pixel format
191 * @param[in] width Image width in pixels
192 * @param[in] height Image height in pixels
193 * @param[in] bufferWidth Buffer width (stride) in pixels
194 * @param[in] bufferHeight Buffer height in pixels
195 * @return pixel buffer pointer
197 virtual PixelBuffer* ReserveBuffer(Pixel::Format pixelFormat,
200 unsigned int bufferWidth = 0,
201 unsigned int bufferHeight = 0) = 0;
204 * Assign a pixel buffer. Any previously allocated pixel buffer is deleted.
205 * Dali has ownership of the buffer, but it iss allowable to modify its
206 * contents after it is assigned, but before it is used.
207 * Bitmap stores the provided size information about the image.
209 * The buffer must have been allocated with the C++ array new operator, not
210 * with malloc or as a local or static object. The precise form is as follows:
212 * PixelBuffer * buffer = new PixelBuffer[bufSize];
214 * @pre bufferWidth, bufferHeight have to be power of two
215 * @param[in] pixelFormat pixel format
216 * @param[in] buffer the pixel buffer
217 * @param[in] bufferSize size of the pixel buffer
218 * @param[in] width Image width in pixels
219 * @param[in] height Image height in pixels
220 * @param[in] bufferWidth Buffer width (stride) in pixels
221 * @param[in] bufferHeight Buffer height in pixels
223 virtual void AssignBuffer(Pixel::Format pixelFormat,
225 std::size_t bufferSize,
228 unsigned int bufferWidth = 0,
229 unsigned int bufferHeight = 0) = 0;
231 * Get the width of the buffer (stride)
232 * @return The width of the buffer in pixels
234 virtual unsigned int GetBufferWidth() const = 0;
237 * Get the height of the buffer
238 * @return The height of the buffer in pixels
240 virtual unsigned int GetBufferHeight() const = 0;
243 * Get the pixel buffer stride.
244 * @return The buffer stride (in bytes) if this is bitmap of non-compressed
245 * packed pixels for which a stride is meaningful or 0 otherwise.
247 virtual unsigned int GetBufferStride() const = 0;
250 * Check the bitmap data and test whether it has any transparent pixels.
251 * This property can then be tested for with IsFullyOpaque().
253 virtual void TestForTransparency() = 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 const PackedPixelsProfile* GetPackedPixelsProfile() const { return 0; }
261 * Get interface to features that are active only if the Bitmap was created
262 * with a BITMAP_2D_PACKED_PIXELS profile. */
263 virtual PackedPixelsProfile* GetPackedPixelsProfile() { return 0; }
265 /**@}*/ ///< End of packed pixel features.
268 /** \name CompressedProfile
269 * Features that only apply to opaque/compressed formats. */
272 class CompressedProfile
276 * (Re-)Allocate pixel buffer for the Bitmap. Any previously allocated pixel buffer
278 * Dali has ownership of the buffer, and contents are opaque and immutable.
279 * Bitmap stores given size information about the image which the client is assumed
280 * to have retrieved from out-of-band image metadata.
281 * @param[in] pixelFormat pixel format
282 * @param[in] width Image width in pixels
283 * @param[in] height Image height in pixels
284 * @param[in] bufferSize Buffer size in bytes
285 * @return pixel buffer pointer
287 virtual PixelBuffer* ReserveBufferOfSize( Pixel::Format pixelFormat,
288 const unsigned width,
289 const unsigned height,
290 const size_t numBytes ) = 0;
293 virtual const CompressedProfile* GetCompressedProfile() const { return 0; }
294 virtual CompressedProfile* GetCompressedProfile() { return 0; }
299 * Inform the bitmap that its pixel buffer is no longer required and can be
300 * deleted to free up memory if the bitmap owns the buffer.
302 void DiscardBuffer();
305 * Check if the pixel buffer can be discarded
306 * @return true if the pixel buffer can be discarded
308 bool IsDiscardable() const
310 return mDiscardable == ResourcePolicy::DISCARD;
314 * Transfer ownership of the pixel buffer to the calling function.
315 * @post bitmaps pixel data is set to NULL
316 * @return the bitmaps pixel buffer
318 PixelBuffer* ReleaseBuffer();
320 * Delete the pixel buffer data
322 void DeletePixelBuffer();
325 * A reference counted object may only be deleted by calling Unreference()
331 unsigned int mImageWidth; ///< Image width in pixels
332 unsigned int mImageHeight; ///< Image height in pixels
333 Pixel::Format mPixelFormat; ///< Pixel format
334 bool mHasAlphaChannel; ///< Whether the image has an alpha channel
335 bool mAlphaChannelUsed; ///< Whether the alpha channel is used in case the image owns one.
336 PixelBuffer* mData; ///< Raw pixel data
340 ResourcePolicy::Discardable mDiscardable; ///< Should delete the buffer when discard buffer is called.
342 Bitmap(const Bitmap& other); ///< defined private to prevent use
343 Bitmap& operator = (const Bitmap& other); ///< defined private to prevent use
345 // Changes scope, should be at end of class
346 DALI_LOG_OBJECT_STRING_DECLARATION;
349 } // namespace Integration
353 #endif // __DALI_INTEGRATION_BITMAP_H__