1 | /*
|
---|
2 | -----------------------------------------------------------------------------
|
---|
3 | This source file is part of OGRE
|
---|
4 | (Object-oriented Graphics Rendering Engine)
|
---|
5 | For the latest info, see http://www.ogre3d.org/
|
---|
6 |
|
---|
7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
8 | Also see acknowledgements in Readme.html
|
---|
9 |
|
---|
10 | This program is free software; you can redistribute it and/or modify it under
|
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
13 | version.
|
---|
14 |
|
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
23 | -----------------------------------------------------------------------------
|
---|
24 | */
|
---|
25 | #ifndef _PixelFormat_H__
|
---|
26 | #define _PixelFormat_H__
|
---|
27 |
|
---|
28 | #include "OgrePrerequisites.h"
|
---|
29 | #include "OgreCommon.h"
|
---|
30 |
|
---|
31 | namespace Ogre {
|
---|
32 | /** The pixel format used for images, textures, and render surfaces */
|
---|
33 | enum PixelFormat
|
---|
34 | {
|
---|
35 | /// Unknown pixel format.
|
---|
36 | PF_UNKNOWN = 0,
|
---|
37 | /// 8-bit pixel format, all bits luminace.
|
---|
38 | PF_L8 = 1,
|
---|
39 | PF_BYTE_L = PF_L8,
|
---|
40 | /// 16-bit pixel format, all bits luminace.
|
---|
41 | PF_L16 = 2,
|
---|
42 | PF_SHORT_L = PF_L16,
|
---|
43 | /// 8-bit pixel format, all bits alpha.
|
---|
44 | PF_A8 = 3,
|
---|
45 | PF_BYTE_A = PF_A8,
|
---|
46 | /// 8-bit pixel format, 4 bits alpha, 4 bits luminace.
|
---|
47 | PF_A4L4 = 4,
|
---|
48 | /// 2 byte pixel format, 1 byte luminance, 1 byte alpha
|
---|
49 | PF_BYTE_LA = 5,
|
---|
50 | /// 16-bit pixel format, 5 bits red, 6 bits green, 5 bits blue.
|
---|
51 | PF_R5G6B5 = 6,
|
---|
52 | /// 16-bit pixel format, 5 bits red, 6 bits green, 5 bits blue.
|
---|
53 | PF_B5G6R5 = 7,
|
---|
54 | /// 8-bit pixel format, 2 bits blue, 3 bits green, 3 bits red.
|
---|
55 | PF_R3G3B2 = 31,
|
---|
56 | /// 16-bit pixel format, 4 bits for alpha, red, green and blue.
|
---|
57 | PF_A4R4G4B4 = 8,
|
---|
58 | /// 16-bit pixel format, 5 bits for blue, green, red and 1 for alpha.
|
---|
59 | PF_A1R5G5B5 = 9,
|
---|
60 | /// 24-bit pixel format, 8 bits for red, green and blue.
|
---|
61 | PF_R8G8B8 = 10,
|
---|
62 | /// 24-bit pixel format, 8 bits for blue, green and red.
|
---|
63 | PF_B8G8R8 = 11,
|
---|
64 | /// 32-bit pixel format, 8 bits for alpha, red, green and blue.
|
---|
65 | PF_A8R8G8B8 = 12,
|
---|
66 | /// 32-bit pixel format, 8 bits for blue, green, red and alpha.
|
---|
67 | PF_A8B8G8R8 = 13,
|
---|
68 | /// 32-bit pixel format, 8 bits for blue, green, red and alpha.
|
---|
69 | PF_B8G8R8A8 = 14,
|
---|
70 | /// 32-bit pixel format, 8 bits for red, green, blue and alpha.
|
---|
71 | PF_R8G8B8A8 = 28,
|
---|
72 | /// 32-bit pixel format, 8 bits for red, 8 bits for green, 8 bits for blue
|
---|
73 | /// like PF_A8R8G8B8, but alpha will get discarded
|
---|
74 | PF_X8R8G8B8 = 26,
|
---|
75 | /// 32-bit pixel format, 8 bits for blue, 8 bits for green, 8 bits for red
|
---|
76 | /// like PF_A8B8G8R8, but alpha will get discarded
|
---|
77 | PF_X8B8G8R8 = 27,
|
---|
78 | #if OGRE_ENDIAN == OGRE_ENDIAN_BIG
|
---|
79 | /// 3 byte pixel format, 1 byte for red, 1 byte for green, 1 byte for blue
|
---|
80 | PF_BYTE_RGB = PF_R8G8B8,
|
---|
81 | /// 3 byte pixel format, 1 byte for blue, 1 byte for green, 1 byte for red
|
---|
82 | PF_BYTE_BGR = PF_B8G8R8,
|
---|
83 | /// 4 byte pixel format, 1 byte for blue, 1 byte for green, 1 byte for red and one byte for alpha
|
---|
84 | PF_BYTE_BGRA = PF_B8G8R8A8,
|
---|
85 | /// 4 byte pixel format, 1 byte for red, 1 byte for green, 1 byte for blue, and one byte for alpha
|
---|
86 | PF_BYTE_RGBA = PF_R8G8B8A8,
|
---|
87 | #else
|
---|
88 | /// 3 byte pixel format, 1 byte for red, 1 byte for green, 1 byte for blue
|
---|
89 | PF_BYTE_RGB = PF_B8G8R8,
|
---|
90 | /// 3 byte pixel format, 1 byte for blue, 1 byte for green, 1 byte for red
|
---|
91 | PF_BYTE_BGR = PF_R8G8B8,
|
---|
92 | /// 4 byte pixel format, 1 byte for blue, 1 byte for green, 1 byte for red and one byte for alpha
|
---|
93 | PF_BYTE_BGRA = PF_A8R8G8B8,
|
---|
94 | /// 4 byte pixel format, 1 byte for red, 1 byte for green, 1 byte for blue, and one byte for alpha
|
---|
95 | PF_BYTE_RGBA = PF_A8B8G8R8,
|
---|
96 | #endif
|
---|
97 | /// 32-bit pixel format, 2 bits for alpha, 10 bits for red, green and blue.
|
---|
98 | PF_A2R10G10B10 = 15,
|
---|
99 | /// 32-bit pixel format, 10 bits for blue, green and red, 2 bits for alpha.
|
---|
100 | PF_A2B10G10R10 = 16,
|
---|
101 | /// DDS (DirectDraw Surface) DXT1 format
|
---|
102 | PF_DXT1 = 17,
|
---|
103 | /// DDS (DirectDraw Surface) DXT2 format
|
---|
104 | PF_DXT2 = 18,
|
---|
105 | /// DDS (DirectDraw Surface) DXT3 format
|
---|
106 | PF_DXT3 = 19,
|
---|
107 | /// DDS (DirectDraw Surface) DXT4 format
|
---|
108 | PF_DXT4 = 20,
|
---|
109 | /// DDS (DirectDraw Surface) DXT5 format
|
---|
110 | PF_DXT5 = 21,
|
---|
111 | // 16-bit pixel format, 16 bits (float) for red
|
---|
112 | PF_FLOAT16_R = 32,
|
---|
113 | // 48-bit pixel format, 16 bits (float) for red, 16 bits (float) for green, 16 bits (float) for blue
|
---|
114 | PF_FLOAT16_RGB = 22,
|
---|
115 | // 64-bit pixel format, 16 bits (float) for red, 16 bits (float) for green, 16 bits (float) for blue, 16 bits (float) for alpha
|
---|
116 | PF_FLOAT16_RGBA = 23,
|
---|
117 | // 16-bit pixel format, 16 bits (float) for red
|
---|
118 | PF_FLOAT32_R = 33,
|
---|
119 | // 96-bit pixel format, 32 bits (float) for red, 32 bits (float) for green, 32 bits (float) for blue
|
---|
120 | PF_FLOAT32_RGB = 24,
|
---|
121 | // 128-bit pixel format, 32 bits (float) for red, 32 bits (float) for green, 32 bits (float) for blue, 32 bits (float) for alpha
|
---|
122 | PF_FLOAT32_RGBA = 25,
|
---|
123 | // Depth texture format
|
---|
124 | PF_DEPTH = 29,
|
---|
125 | // 64-bit pixel format, 16 bits for red, green, blue and alpha
|
---|
126 | PF_SHORT_RGBA = 30,
|
---|
127 | // Number of pixel formats currently defined
|
---|
128 | PF_COUNT = 34
|
---|
129 | };
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Flags defining some on/off properties of pixel formats
|
---|
133 | */
|
---|
134 | enum PixelFormatFlags {
|
---|
135 | // This format has an alpha channel
|
---|
136 | PFF_HASALPHA = 0x00000001,
|
---|
137 | // This format is compressed. This invalidates the values in elemBytes,
|
---|
138 | // elemBits and the bit counts as these might not be fixed in a compressed format.
|
---|
139 | PFF_COMPRESSED = 0x00000002,
|
---|
140 | // This is a floating point format
|
---|
141 | PFF_FLOAT = 0x00000004,
|
---|
142 | // This is a depth format (for depth textures)
|
---|
143 | PFF_DEPTH = 0x00000008,
|
---|
144 | // Format is in native endian. Generally true for the 16, 24 and 32 bits
|
---|
145 | // formats which can be represented as machine integers.
|
---|
146 | PFF_NATIVEENDIAN = 0x00000010,
|
---|
147 | // This is an intensity format instead of a RGB one. The luminance
|
---|
148 | // replaces R,G and B. (but not A)
|
---|
149 | PFF_LUMINANCE = 0x00000020
|
---|
150 | };
|
---|
151 | /** A primitive describing a volume (3D), image (2D) or line (1D) of pixels in memory.
|
---|
152 | In case of a rectangle, depth must be 1.
|
---|
153 | Pixels are stored as a succession of "depth" slices, each containing "height" rows of
|
---|
154 | "width" pixels.
|
---|
155 | */
|
---|
156 | class _OgreExport PixelBox: public Box {
|
---|
157 | public:
|
---|
158 | /// Parameter constructor for setting the members manually
|
---|
159 | PixelBox() {}
|
---|
160 | /** Constructor providing extents in the form of a Box object. This constructor
|
---|
161 | assumes the pixel data is laid out consecutively in memory. (this
|
---|
162 | means row after row, slice after slice, with no space in between)
|
---|
163 | @param extents Extents of the region defined by data
|
---|
164 | @param pixelFormat Format of this buffer
|
---|
165 | @param pixelData Pointer to the actual data
|
---|
166 | */
|
---|
167 | PixelBox(const Box &extents, PixelFormat pixelFormat, void *pixelData=0):
|
---|
168 | Box(extents), data(pixelData), format(pixelFormat)
|
---|
169 | {
|
---|
170 | setConsecutive();
|
---|
171 | }
|
---|
172 | /** Constructor providing width, height and depth. This constructor
|
---|
173 | assumes the pixel data is laid out consecutively in memory. (this
|
---|
174 | means row after row, slice after slice, with no space in between)
|
---|
175 | @param width Width of the region
|
---|
176 | @param height Height of the region
|
---|
177 | @param depth Depth of the region
|
---|
178 | @param pixelFormat Format of this buffer
|
---|
179 | @param pixelData Pointer to the actual data
|
---|
180 | */
|
---|
181 | PixelBox(size_t width, size_t height, size_t depth, PixelFormat pixelFormat, void *pixelData=0):
|
---|
182 | Box(0, 0, 0, width, height, depth),
|
---|
183 | data(pixelData), format(pixelFormat)
|
---|
184 | {
|
---|
185 | setConsecutive();
|
---|
186 | }
|
---|
187 |
|
---|
188 | /// The data pointer
|
---|
189 | void *data;
|
---|
190 | /// The pixel format
|
---|
191 | PixelFormat format;
|
---|
192 | /** Number of elements between the leftmost pixel of one row and the left
|
---|
193 | pixel of the next. This value must always be equal to getWidth() (consecutive)
|
---|
194 | for compressed formats.
|
---|
195 | */
|
---|
196 | size_t rowPitch;
|
---|
197 | /** Number of elements between the top left pixel of one (depth) slice and
|
---|
198 | the top left pixel of the next. This can be a negative value. Must be a multiple of
|
---|
199 | rowPitch. This value must always be equal to getWidth()*getHeight() (consecutive)
|
---|
200 | for compressed formats.
|
---|
201 | */
|
---|
202 | size_t slicePitch;
|
---|
203 |
|
---|
204 | /** Set the rowPitch and slicePitch so that the buffer is laid out consecutive
|
---|
205 | in memory.
|
---|
206 | */
|
---|
207 | void setConsecutive()
|
---|
208 | {
|
---|
209 | rowPitch = getWidth();
|
---|
210 | slicePitch = getWidth()*getHeight();
|
---|
211 | }
|
---|
212 | /** Get the number of elements between one past the rightmost pixel of
|
---|
213 | one row and the leftmost pixel of the next row. (IE this is zero if rows
|
---|
214 | are consecutive).
|
---|
215 | */
|
---|
216 | size_t getRowSkip() const { return rowPitch - getWidth(); }
|
---|
217 | /** Get the number of elements between one past the right bottom pixel of
|
---|
218 | one slice and the left top pixel of the next slice. (IE this is zero if slices
|
---|
219 | are consecutive).
|
---|
220 | */
|
---|
221 | size_t getSliceSkip() const { return slicePitch - (getHeight() * rowPitch); }
|
---|
222 |
|
---|
223 | /** Return whether this buffer is laid out consecutive in memory (ie the pitches
|
---|
224 | are equal to the dimensions)
|
---|
225 | */
|
---|
226 | bool isConsecutive() const
|
---|
227 | {
|
---|
228 | return rowPitch == getWidth() && slicePitch == getWidth()*getHeight();
|
---|
229 | }
|
---|
230 | /** Return the size (in bytes) this image would take if it was
|
---|
231 | laid out consecutive in memory
|
---|
232 | */
|
---|
233 | size_t getConsecutiveSize() const;
|
---|
234 | /** Return a subvolume of this PixelBox.
|
---|
235 | @param def Defines the bounds of the subregion to return
|
---|
236 | @returns A pixel box describing the region and the data in it
|
---|
237 | @remarks This function does not copy any data, it just returns
|
---|
238 | a PixelBox object with a data pointer pointing somewhere inside
|
---|
239 | the data of object.
|
---|
240 | @throws Exception(ERR_INVALIDPARAMS) if def is not fully contained
|
---|
241 | */
|
---|
242 | PixelBox getSubVolume(const Box &def) const;
|
---|
243 | };
|
---|
244 |
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Some utility functions for packing and unpacking pixel data
|
---|
248 | */
|
---|
249 | class _OgreExport PixelUtil {
|
---|
250 | public:
|
---|
251 | /** Returns the size in bytes of an element of the given pixel format.
|
---|
252 | @returns
|
---|
253 | The size in bytes of an element. See Remarks.
|
---|
254 | @remarks
|
---|
255 | Passing PF_UNKNOWN will result in returning a size of 0 bytes.
|
---|
256 | */
|
---|
257 | static size_t getNumElemBytes( PixelFormat format );
|
---|
258 |
|
---|
259 | /** Returns the size in bits of an element of the given pixel format.
|
---|
260 | @returns
|
---|
261 | The size in bits of an element. See Remarks.
|
---|
262 | @remarks
|
---|
263 | Passing PF_UNKNOWN will result in returning a size of 0 bits.
|
---|
264 | */
|
---|
265 | static size_t getNumElemBits( PixelFormat format );
|
---|
266 |
|
---|
267 | /** Returns the size in memory of a region with the given extents and pixel
|
---|
268 | format with consecutive memory layout.
|
---|
269 | @param width
|
---|
270 | The width of the area
|
---|
271 | @param height
|
---|
272 | The height of the area
|
---|
273 | @param depth
|
---|
274 | The depth of the area
|
---|
275 | @param format
|
---|
276 | The format of the area
|
---|
277 | @returns
|
---|
278 | The size in bytes
|
---|
279 | @remarks
|
---|
280 | In case that the format is non-compressed, this simply returns
|
---|
281 | width*height*depth*PixelUtil::getNumElemBytes(format). In the compressed
|
---|
282 | case, this does serious magic.
|
---|
283 | */
|
---|
284 | static size_t getMemorySize(size_t width, size_t height, size_t depth, PixelFormat format);
|
---|
285 |
|
---|
286 | /** Returns the property flags for this pixel format
|
---|
287 | @returns
|
---|
288 | A bitfield combination of PFF_HASALPHA, PFF_ISCOMPRESSED,
|
---|
289 | PFF_FLOAT, PFF_DEPTH, PFF_NATIVEENDIAN, PFF_LUMINANCE
|
---|
290 | @remarks
|
---|
291 | This replaces the seperate functions for formatHasAlpha, formatIsFloat, ...
|
---|
292 | */
|
---|
293 | static unsigned int getFlags( PixelFormat format );
|
---|
294 |
|
---|
295 | /** Shortcut method to determine if the format has an alpha component */
|
---|
296 | static bool hasAlpha(PixelFormat format);
|
---|
297 | /** Shortcut method to determine if the format is floating point */
|
---|
298 | static bool isFloatingPoint(PixelFormat format);
|
---|
299 | /** Shortcut method to determine if the format is compressed */
|
---|
300 | static bool isCompressed(PixelFormat format);
|
---|
301 | /** Shortcut method to determine if the format is a depth format. */
|
---|
302 | static bool isDepth(PixelFormat format);
|
---|
303 | /** Shortcut method to determine if the format is in native endian format. */
|
---|
304 | static bool isNativeEndian(PixelFormat format);
|
---|
305 | /** Shortcut method to determine if the format is a luminance format. */
|
---|
306 | static bool isLuminance(PixelFormat format);
|
---|
307 |
|
---|
308 | /** Return wether a certain image extent is valid for this image format.
|
---|
309 | @param width
|
---|
310 | The width of the area
|
---|
311 | @param height
|
---|
312 | The height of the area
|
---|
313 | @param depth
|
---|
314 | The depth of the area
|
---|
315 | @param format
|
---|
316 | The format of the area
|
---|
317 | @remarks For non-compressed formats, this is always true. For DXT formats,
|
---|
318 | only sizes with a width and height multiple of 4 and depth 1 are allowed.
|
---|
319 | */
|
---|
320 | static bool isValidExtent(size_t width, size_t height, size_t depth, PixelFormat format);
|
---|
321 |
|
---|
322 | /** Gives the number of bits (RGBA) for a format. See remarks.
|
---|
323 | @remarks For non-colour formats (dxt, depth) this returns [0,0,0,0].
|
---|
324 | */
|
---|
325 | static void getBitDepths(PixelFormat format, int rgba[4]);
|
---|
326 |
|
---|
327 | /** Gives the masks for the R, G, B and A component
|
---|
328 | @note Only valid for native endian formats
|
---|
329 | */
|
---|
330 | static void getBitMasks(PixelFormat format, uint32 rgba[4]);
|
---|
331 |
|
---|
332 | /** Gets the name of an image format
|
---|
333 | */
|
---|
334 | static String getFormatName(PixelFormat srcformat);
|
---|
335 |
|
---|
336 | /** Returns wether the format can be packed or unpacked with the packColour()
|
---|
337 | and unpackColour() functions. This is generally not true for compressed and
|
---|
338 | depth formats as they are special. It can only be true for formats with a
|
---|
339 | fixed element size.
|
---|
340 | @returns
|
---|
341 | true if yes, otherwise false
|
---|
342 | */
|
---|
343 | static bool isAccessible(PixelFormat srcformat);
|
---|
344 |
|
---|
345 | /** Pack a colour value to memory
|
---|
346 | @param colour The colour
|
---|
347 | @param pf Pixelformat in which to write the colour
|
---|
348 | @param dest Destination memory location
|
---|
349 | */
|
---|
350 | static void packColour(const ColourValue &colour, const PixelFormat pf, const void* dest);
|
---|
351 | /** Pack a colour value to memory
|
---|
352 | @param r,g,b,a The four colour components, range 0x00 to 0xFF
|
---|
353 | @param pf Pixelformat in which to write the colour
|
---|
354 | @param dest Destination memory location
|
---|
355 | */
|
---|
356 | static void packColour(const uint8 r, const uint8 g, const uint8 b, const uint8 a, const PixelFormat pf, const void* dest);
|
---|
357 | /** Pack a colour value to memory
|
---|
358 | @param r,g,b,a The four colour components, range 0.0f to 1.0f
|
---|
359 | (an exception to this case exists for floating point pixel
|
---|
360 | formats, which don't clamp to 0.0f..1.0f)
|
---|
361 | @param pf Pixelformat in which to write the colour
|
---|
362 | @param dest Destination memory location
|
---|
363 | */
|
---|
364 | static void packColour(const float r, const float g, const float b, const float a, const PixelFormat pf, const void* dest);
|
---|
365 |
|
---|
366 | /** Unpack a colour value from memory
|
---|
367 | @param colour The colour is returned here
|
---|
368 | @param pf Pixelformat in which to read the colour
|
---|
369 | @param src Source memory location
|
---|
370 | */
|
---|
371 | static void unpackColour(ColourValue *colour, PixelFormat pf, const void* src);
|
---|
372 | /** Unpack a colour value from memory
|
---|
373 | @param r,g,b,a The colour is returned here (as byte)
|
---|
374 | @param pf Pixelformat in which to read the colour
|
---|
375 | @param src Source memory location
|
---|
376 | @remarks This function returns the colour components in 8 bit precision,
|
---|
377 | this will lose precision when coming from PF_A2R10G10B10 or floating
|
---|
378 | point formats.
|
---|
379 | */
|
---|
380 | static void unpackColour(uint8 *r, uint8 *g, uint8 *b, uint8 *a, PixelFormat pf, const void* src);
|
---|
381 | /** Unpack a colour value from memory
|
---|
382 | @param r,g,b,a The colour is returned here (as float)
|
---|
383 | @param pf Pixelformat in which to read the colour
|
---|
384 | @param src Source memory location
|
---|
385 | */
|
---|
386 | static void unpackColour(float *r, float *g, float *b, float *a, PixelFormat pf, const void* src);
|
---|
387 |
|
---|
388 | /** Convert consecutive pixels from one format to another. No dithering or filtering is being done.
|
---|
389 | Converting from RGB to luminance takes the R channel. In case the source and destination format match,
|
---|
390 | just a copy is done.
|
---|
391 | @param src Pointer to source region
|
---|
392 | @param srcFormat Pixel format of source region
|
---|
393 | @param dst Pointer to destination region
|
---|
394 | @param dstFormat Pixel format of destination region
|
---|
395 | */
|
---|
396 | static void bulkPixelConversion(void *src, PixelFormat srcFormat, void *dest, PixelFormat dstFormat, unsigned int count);
|
---|
397 |
|
---|
398 | /** Convert pixels from one format to another. No dithering or filtering is being done. Converting
|
---|
399 | from RGB to luminance takes the R channel.
|
---|
400 | @param src PixelBox containing the source pixels, pitches and format
|
---|
401 | @param dst PixelBox containing the destination pixels, pitches and format
|
---|
402 | @remarks The source and destination boxes must have the same
|
---|
403 | dimensions. In case the source and destination format match, a plain copy is done.
|
---|
404 | */
|
---|
405 | static void bulkPixelConversion(const PixelBox &src, const PixelBox &dst);
|
---|
406 | };
|
---|
407 |
|
---|
408 | };
|
---|
409 |
|
---|
410 | #endif
|
---|