Rename GP_Context -> GP_Pixmap
[gfxprim.git] / include / core / GP_Pixel.h
blob3270a78c76b84d4e4f4f454f372de1f69da4f178
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
21 * *
22 * Copyright (C) 2009-2015 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 * Copyright (C) 2011 Tomas Gavenciak <gavento@ucw.cz> *
25 * *
26 *****************************************************************************/
28 #ifndef CORE_GP_PIXEL_H
29 #define CORE_GP_PIXEL_H
31 #include <stdbool.h>
32 #include <stdint.h>
34 #include "GP_Common.h"
35 #include "GP_FnPerBpp.h"
37 struct GP_Pixmap;
40 * GP_PixelType is typedef enum of PixelTypes,
42 * each named GP_PIXEL_<TYPENAME>, such as GP_PIXEL_RGB888
43 * see the beginning of GP_Pixel.gen.h for a complete list
45 * The type always contains GP_PIXEL_UNKNOWN = 0 and
46 * GP_PIXEL_MAX as the last value (also the number of valid types)
47 * The types are numbered to use the entire range 0 .. GP_PIXEL_MAX-1
51 * GP_Pixel is just uint32_t
53 typedef uint32_t GP_Pixel;
54 #define GP_PIXEL_BITS (sizeof(GP_Pixel) * 8)
56 /* Generated header */
57 #include "GP_Pixel.gen.h"
58 #include "GP_GetSetBits.h"
61 * Information about ordering of pixels in byte for 1, 2 and 4 bpp
62 * used in a one bit variable in GP_Pixmap
64 typedef enum {
65 /* less significant bits contain pixels with lower indices */
66 /* also used for irrelevant bit-endian */
67 GP_BIT_ENDIAN_LE = 0,
68 /* more significant bits contain pixels with lower indices */
69 GP_BIT_ENDIAN_BE,
70 } GP_BIT_ENDIAN;
73 * Description of one channel
74 * Assumes all the channel names to be at most 7 chars long
76 * The common channel names are:
77 * R, G, B - as usual
78 * V - value, for grayscale
79 * A - opacity (0=transparent)
80 * P - palette (index)
82 typedef struct GP_PixelTypeChannel {
83 char name[8]; /* Channel name */
84 uint8_t offset; /* Offset in bits */
85 uint8_t size; /* Bit-size */
86 } GP_PixelTypeChannel;
89 * Maximum number of channels in a PixelType
91 #define GP_PIXELTYPE_MAX_CHANNELS 8
94 * Pixel type flags for various pixel properties.
96 typedef enum GP_PixelFlags {
97 GP_PIXEL_HAS_ALPHA = 0x01,
98 GP_PIXEL_IS_RGB = 0x02,
99 GP_PIXEL_IS_PALETTE = 0x04,
100 GP_PIXEL_IS_CMYK = 0x08,
101 GP_PIXEL_IS_GRAYSCALE = 0x10,
102 } GP_PixelFlags;
105 * Description of one PixelType
106 * Assumes name with at most 15 chars
107 * Assumes at most 8 channels
109 typedef struct GP_PixelTypeDescription {
110 GP_PixelType type; /* Number of the type */
111 const char name[16]; /* Name */
112 uint8_t size; /* Size in bits */
113 GP_BIT_ENDIAN bit_endian; /* Order of pixels in a byte */
114 uint8_t numchannels; /* Number of channels */
115 GP_PixelFlags flags;
116 /* String describing the bit-representaton (as in "RRRRRGGGGGGBBBBB")*/
117 const char bitmap[GP_PIXEL_BITS + 1];
118 /* Individual channels */
119 const GP_PixelTypeChannel channels[GP_PIXELTYPE_MAX_CHANNELS];
120 } GP_PixelTypeDescription;
123 * Array of size GP_PIXEL_MAX describing known pixel types
125 extern const GP_PixelTypeDescription GP_PixelTypes[GP_PIXEL_MAX];
127 #define GP_VALID_PIXELTYPE(type) (((type) > 0) && ((type) < GP_PIXEL_MAX))
129 #define GP_CHECK_VALID_PIXELTYPE(type) \
130 GP_CHECK(GP_VALID_PIXELTYPE(type), "Invalid PixelType %d", (type))
133 * Convert pixel type to name.
135 static inline const char *GP_PixelTypeName(GP_PixelType type)
137 GP_CHECK_VALID_PIXELTYPE(type);
138 return GP_PixelTypes[type].name;
142 * Returns number of bits per pixel.
144 static inline uint32_t GP_PixelSize(GP_PixelType type)
146 GP_CHECK_VALID_PIXELTYPE(type);
147 return GP_PixelTypes[type].size;
150 static inline const GP_PixelTypeDescription *GP_PixelTypeDesc(GP_PixelType type)
152 GP_CHECK_VALID_PIXELTYPE(type);
153 return &GP_PixelTypes[type];
156 static inline unsigned int GP_PixelChannelCount(GP_PixelType type)
158 GP_CHECK_VALID_PIXELTYPE(type);
159 return GP_PixelTypes[type].numchannels;
162 static inline uint8_t GP_PixelChannelBits(GP_PixelType type, uint8_t channel)
164 GP_CHECK_VALID_PIXELTYPE(type);
165 return GP_PixelTypes[type].channels[channel].size;
168 static inline const char *GP_PixelChannelName(GP_PixelType type,
169 uint8_t channel)
171 GP_CHECK_VALID_PIXELTYPE(type);
172 return GP_PixelTypes[type].channels[channel].name;
176 * Print a human-readable representation of a pixel value to a string.
177 * Arguments as for snprintf().
179 void GP_PixelSNPrint(char *buf, size_t len, GP_Pixel pixel, GP_PixelType type);
182 * Prints human-readable representation of pixel value into the stdout.
184 void GP_PixelPrint(GP_Pixel pixel, GP_PixelType type);
187 * Returns pixel type for passed human-readable name (e.g. RGB888).
189 GP_PixelType GP_PixelTypeByName(const char *name);
192 * Match pixel type to known pixel types.
194 * Returns either valid PixelType or GP_PIXEL_UNKNOWN
196 GP_PixelType GP_PixelRGBMatch(GP_Pixel rmask, GP_Pixel gmask,
197 GP_Pixel bmask, GP_Pixel amask,
198 uint8_t bits_per_pixel);
201 * Similar to GP_PixelRGBMatch but works with offsets and sizes
203 * Returns either valid PixelType or GP_PIXEL_UNKNOWN
205 GP_PixelType GP_PixelRGBLookup(uint32_t rsize, uint32_t roff,
206 uint32_t gsize, uint32_t goff,
207 uint32_t bsize, uint32_t boff,
208 uint32_t asize, uint32_t aoff,
209 uint8_t bits_per_pixel);
212 * Functions to determine pixel attributes.
214 * Call as:
216 * if (GP_PixelHasFlags(pixel_type, GP_PIXEL_IS_RGB | GP_PIXEL_HAS_ALPHA))
217 * ...
219 int GP_PixelHasFlags(GP_PixelType pixel_type, GP_PixelFlags flags);
221 #endif /* CORE_GP_PIXEL_H */