1 #ifndef __DALI_INTEGRATION_IMAGE_DATA_H__
2 #define __DALI_INTEGRATION_IMAGE_DATA_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.
21 #include <dali/integration-api/debug.h> // For DALI_LOG_OBJECT_STRING_DECLARATION
22 #include <dali/public-api/object/ref-object.h>
23 #include <dali/public-api/common/intrusive-ptr.h>
24 #include <dali/public-api/images/pixel.h>
36 typedef IntrusivePtr<ImageData> ImageDataPtr;
38 typedef uint8_t PixelBuffer;
41 * Used to avoid accidentally mixing the order of parameters where some are uint
42 * dimensions and one is a buffer size.
47 explicit BufferSize(size_t size) : bufferSize( size )
49 operator size_t() const { return bufferSize; }
50 const size_t bufferSize;
54 * @brief A simple container for image data.
56 * Just a pointer to a buffer and some minimal metadata. It always owns the
57 * buffer of image data that it points to until it is destroyed or the buffer
58 * is released with ReleaseImageBuffer().
60 * The width and height stored are the logical image values. Compressed formats
61 * such as ETC group pixels into blocks (ETC = 4*4) and these logical width and
62 * height may indicate, for example for ETC, that 1 to 3 pixels in the rightmost
63 * column of blocks or bottom row are ignored for rendering.
64 * For simple uncompressed images, the buffer is exactly width * height *
65 * bytes-per-pixel in size with no spare bytes at right or bottom edge.
67 class DALI_IMPORT_API ImageData : public Dali::RefObject
71 * @brief Three values for the information about the alpha values in the
74 * Alpha in pixels with the channel can be undetermined, it can be all-ones,
75 * or it can have at least one pixel that is translucent with an alpha < 1.0.
79 /** Alpha not yet been tested.*/
80 ALPHA_USAGE_UNDETERMINED,
81 /** Alpha == 1.0 in every pixel.*/
82 ALPHA_USAGE_ALL_OPAQUE,
83 /** Alpha < 1.0 in at least one pixel.*/
84 ALPHA_USAGE_SOME_TRANSLUCENT
90 * Construct an instance and allocate a buffer owned by it.
91 * @param[in] numBytes The number of bytes to allocate to hold the image data.
92 * @param[in] imageWidth The width in pixels of the image.
93 * @param[in] imageHeight The height in pixels of the image.
94 * @param[in] pixelFormat The format of the image data in the buffer.
96 ImageData( const size_t numBytes, const unsigned imageWidth, const unsigned imageHeight, const Pixel::Format pixelFormat );
99 * Construct an instance and have it take ownership of the buffer passed in.
100 * @param[in] imageBuffer A buffer of pixel data that should have been allocated using <code>new uint8_t[]</code>.
101 * @param[in] numBytes The number of bytes in the buffer pointed to by imageBuffer.
102 * @param[in] imageWidth The width in pixels of the image.
103 * @param[in] imageHeight The height in pixels of the image.
104 * @param[in] pixelFormat The format of the image data in the buffer.
106 ImageData( uint8_t * const imageBuffer, const size_t numBytes, const unsigned imageWidth, const unsigned imageHeight, const Pixel::Format pixelFormat );
112 /** Allocate and initialize a fresh ImageData object pointing at a new buffer,
113 * returning a smart pointer owning it.
114 * @note Clients can alternatively use the helper function NewBitmapImageData() to
115 * calculate the buffer size from other parameters if the image data represents
116 * a bitmap (a 2d grid of addressable pixels).
117 * @param[in] numBytes The number of bytes to allocate to hold the image data.
118 * @param[in] imageWidth The width in pixels of the image.
119 * @param[in] imageHeight The height in pixels of the image.
120 * @param[in] pixelFormat The format of the image data in the buffer.
122 static ImageDataPtr New( const BufferSize numBytes, unsigned imageWidth, unsigned imageHeight, Pixel::Format pixelFormat );
124 /** Allocate and initialise a fresh ImageData object, taking ownership of the
126 * @param[in] imageBuffer A block of memory allocated with
127 * <code>new uint8_t[]</code>. <em>This is owned by the new ImageData instance
128 * on successful return and should be forgotten by the caller</em>.
129 * @param[in] numBytes The number of bytes in the buffer pointed to by imageBuffer.
130 * @param[in] imageWidth The width in pixels of the image.
131 * @param[in] imageHeight The height in pixels of the image.
132 * @param[in] pixelFormat The format of the image data in the buffer.
134 static ImageDataPtr New( uint8_t * const imageBuffer, const BufferSize numBytes, unsigned imageWidth, unsigned imageHeight, Pixel::Format pixelFormat );
136 /** Access the buffer of image data. */
137 const uint8_t * GetBuffer() const { return mData; }
139 /** Access the buffer of image data. */
140 uint8_t * GetBuffer() { return mData; }
143 * Pass ownership of the buffer of pixel-level data that this instance
144 * currently owns to the caller, and forget about the buffer here as a
146 * @returns A pointer to the underlying buffer of pixel-level image data.
147 * The caller takes ownership of the buffer and is responsible for calling
148 * delete[] on it eventually.
150 uint8_t * ReleaseImageBuffer() { uint8_t * const data = mData; mData = 0; return data; }
153 * @brief Get whether the alpha channel in the pixels is used.
155 AlphaUsage GetAlphaUsage() const { return mAlphaChannelUsed; }
158 * @brief Set whether the alpha channel in the pixels is used.
160 void SetAlphaUsed( const bool alphaUsed ) { mAlphaChannelUsed = alphaUsed ? ALPHA_USAGE_SOME_TRANSLUCENT : ALPHA_USAGE_ALL_OPAQUE; }
164 ImageData(const ImageData& other); ///< defined private to prevent use
165 ImageData& operator = (const ImageData& other); ///< defined private to prevent use
167 /** Pointer to the buffer of image data. */
171 const size_t dataSize; ///< Number of bytes in buffer pointed at by mData
172 const uint16_t imageWidth; ///< Image logical width in pixels
173 const uint16_t imageHeight; ///< Image logical height in pixels
174 const Pixel::Format pixelFormat; ///< Pixel format
177 AlphaUsage mAlphaChannelUsed;
179 // Changes scope, should be at end of class
180 DALI_LOG_OBJECT_STRING_DECLARATION;
184 * A convenience function for creating the common case of an uncompressed image
185 * having width * height pixels in the buffer.
186 * @param[in] imageWidth The width in pixels of the image.
187 * @param[in] imageHeight The height in pixels of the image.
188 * @param[in] pixelFormat The format of the image data in the buffer.
190 DALI_IMPORT_API ImageDataPtr NewBitmapImageData( unsigned imageWidth, unsigned imageHeight, Pixel::Format pixelFormat );
192 } // namespace Integration
196 #endif // __DALI_INTEGRATION_IMAGE_DATA_H__