FreeRDP
Loading...
Searching...
No Matches
include/freerdp/codec/color.h
1
22#ifndef FREERDP_CODEC_COLOR_H
23#define FREERDP_CODEC_COLOR_H
24
25#include <winpr/crt.h>
26#include <freerdp/api.h>
27
28#ifdef __cplusplus
29extern "C"
30{
31#endif
32
33#define FREERDP_PIXEL_FORMAT_TYPE_A 0
34#define FREERDP_PIXEL_FORMAT_TYPE_ARGB 1
35#define FREERDP_PIXEL_FORMAT_TYPE_ABGR 2
36#define FREERDP_PIXEL_FORMAT_TYPE_RGBA 3
37#define FREERDP_PIXEL_FORMAT_TYPE_BGRA 4
38
39#define FREERDP_PIXEL_FORMAT_IS_ABGR(_format) \
40 (FREERDP_PIXEL_FORMAT_TYPE(_format) == FREERDP_PIXEL_FORMAT_TYPE_ABGR)
41
43 enum FREERDP_IMAGE_FLAGS
44 {
45 FREERDP_FLIP_NONE = 0,
46 FREERDP_FLIP_VERTICAL = 1,
47 FREERDP_FLIP_HORIZONTAL = 2,
48 FREERDP_KEEP_DST_ALPHA = 4
49 };
50
51#define FREERDP_PIXEL_FORMAT(_bpp, _type, _a, _r, _g, _b) \
52 ((_bpp << 24) | (_type << 16) | (_a << 12) | (_r << 8) | (_g << 4) | (_b))
53
54#define FREERDP_PIXEL_FORMAT_TYPE(_format) (((_format) >> 16) & 0x07)
55
56/*** Design considerations
57 *
58 * The format naming scheme is based on byte position in memory.
59 * RGBA for example names a byte array with red on position 0, green on 1 etc.
60 *
61 * To read and write the appropriate format from / to memory use FreeRDPReadColor and
62 * FreeRDPWriteColor.
63 *
64 * The single pixel manipulation functions use an intermediate integer representation
65 * that must not be interpreted outside the functions as it is platform dependent.
66 *
67 * X for alpha channel denotes unused (but existing) alpha channel data.
68 */
69
74/* 32bpp formats */
75#define PIXEL_FORMAT_ARGB32 FREERDP_PIXEL_FORMAT(32, FREERDP_PIXEL_FORMAT_TYPE_ARGB, 8, 8, 8, 8)
76#define PIXEL_FORMAT_XRGB32 FREERDP_PIXEL_FORMAT(32, FREERDP_PIXEL_FORMAT_TYPE_ARGB, 0, 8, 8, 8)
77#define PIXEL_FORMAT_ABGR32 FREERDP_PIXEL_FORMAT(32, FREERDP_PIXEL_FORMAT_TYPE_ABGR, 8, 8, 8, 8)
78#define PIXEL_FORMAT_XBGR32 FREERDP_PIXEL_FORMAT(32, FREERDP_PIXEL_FORMAT_TYPE_ABGR, 0, 8, 8, 8)
79#define PIXEL_FORMAT_BGRA32 FREERDP_PIXEL_FORMAT(32, FREERDP_PIXEL_FORMAT_TYPE_BGRA, 8, 8, 8, 8)
80#define PIXEL_FORMAT_BGRX32 FREERDP_PIXEL_FORMAT(32, FREERDP_PIXEL_FORMAT_TYPE_BGRA, 0, 8, 8, 8)
81#define PIXEL_FORMAT_RGBA32 FREERDP_PIXEL_FORMAT(32, FREERDP_PIXEL_FORMAT_TYPE_RGBA, 8, 8, 8, 8)
82#define PIXEL_FORMAT_RGBX32 FREERDP_PIXEL_FORMAT(32, FREERDP_PIXEL_FORMAT_TYPE_RGBA, 0, 8, 8, 8)
83#define PIXEL_FORMAT_BGRX32_DEPTH30 \
84 FREERDP_PIXEL_FORMAT(32, FREERDP_PIXEL_FORMAT_TYPE_BGRA, 0, 10, 10, 10)
85#define PIXEL_FORMAT_RGBX32_DEPTH30 \
86 FREERDP_PIXEL_FORMAT(32, FREERDP_PIXEL_FORMAT_TYPE_RGBA, 0, 10, 10, 10)
87
88/* 24bpp formats */
89#define PIXEL_FORMAT_RGB24 FREERDP_PIXEL_FORMAT(24, FREERDP_PIXEL_FORMAT_TYPE_ARGB, 0, 8, 8, 8)
90#define PIXEL_FORMAT_BGR24 FREERDP_PIXEL_FORMAT(24, FREERDP_PIXEL_FORMAT_TYPE_ABGR, 0, 8, 8, 8)
91
92/* 16bpp formats */
93#define PIXEL_FORMAT_RGB16 FREERDP_PIXEL_FORMAT(16, FREERDP_PIXEL_FORMAT_TYPE_ARGB, 0, 5, 6, 5)
94#define PIXEL_FORMAT_BGR16 FREERDP_PIXEL_FORMAT(16, FREERDP_PIXEL_FORMAT_TYPE_ABGR, 0, 5, 6, 5)
95#define PIXEL_FORMAT_ARGB15 FREERDP_PIXEL_FORMAT(16, FREERDP_PIXEL_FORMAT_TYPE_ARGB, 1, 5, 5, 5)
96#define PIXEL_FORMAT_RGB15 FREERDP_PIXEL_FORMAT(15, FREERDP_PIXEL_FORMAT_TYPE_ARGB, 0, 5, 5, 5)
97#define PIXEL_FORMAT_ABGR15 FREERDP_PIXEL_FORMAT(16, FREERDP_PIXEL_FORMAT_TYPE_ABGR, 1, 5, 5, 5)
98#define PIXEL_FORMAT_BGR15 FREERDP_PIXEL_FORMAT(15, FREERDP_PIXEL_FORMAT_TYPE_ABGR, 0, 5, 5, 5)
99
100/* 8bpp formats */
101#define PIXEL_FORMAT_RGB8 FREERDP_PIXEL_FORMAT(8, FREERDP_PIXEL_FORMAT_TYPE_A, 8, 0, 0, 0)
102
103/* 4 bpp formats */
104#define PIXEL_FORMAT_A4 FREERDP_PIXEL_FORMAT(4, FREERDP_PIXEL_FORMAT_TYPE_A, 4, 0, 0, 0)
105
106/* 1bpp formats */
107#define PIXEL_FORMAT_MONO FREERDP_PIXEL_FORMAT(1, FREERDP_PIXEL_FORMAT_TYPE_A, 1, 0, 0, 0)
108
112 {
113 UINT32 format;
114 UINT32 palette[256];
115 };
116typedef struct gdi_palette gdiPalette;
117
118 /* Compare two color formats but ignore differences in alpha channel.
119 */
120 FREERDP_API DWORD FreeRDPAreColorFormatsEqualNoAlpha(DWORD first, DWORD second);
121
122 /* Color Space Conversions: http://msdn.microsoft.com/en-us/library/ff566496/ */
123
124 /***
125 *
126 * Get a string representation of a color
127 *
128 * @param format The pixel color format
129 *
130 * @return A string representation of format
131 */
132#if defined(WITH_FREERDP_DEPRECATED)
133#define GetColorFormatName(...) FreeRDPGetColorFormatName(__VA_ARGS__)
134#endif
135 FREERDP_API const char* FreeRDPGetColorFormatName(UINT32 format);
136
144 FREERDP_API uint32_t FreeRDPGetColorFromatFromName(const char* name);
145
146 /***
147 *
148 * Converts a pixel color in internal representation to its red, green, blue
149 * and alpha components.
150 *
151 * @param color The color in format internal representation
152 * @param format one of PIXEL_FORMAT_* color format defines
153 * @param _r red color value
154 * @param _g green color value
155 * @param _b blue color value
156 * @param _a alpha color value
157 * @param palette palette to use (only used for 8 bit color!)
158 */
159#if defined(WITH_FREERDP_DEPRECATED)
160#define SplitColor(...) FreeRDPSplitColor(__VA_ARGS__)
161#endif
162 FREERDP_API void FreeRDPSplitColor(UINT32 color, UINT32 format, BYTE* _r, BYTE* _g, BYTE* _b,
163 BYTE* _a, const gdiPalette* palette);
164
165 /***
166 *
167 * Converts red, green, blue and alpha values to internal representation.
168 *
169 * @param format one of PIXEL_FORMAT_* color format defines
170 * @param r red color value
171 * @param g green color value
172 * @param b blue color value
173 * @param a alpha color value
174 *
175 * @return The pixel color in the desired format. Value is in internal
176 * representation.
177 */
178#if defined(WITH_FREERDP_DEPRECATED)
179#define GetColor(...) FreeRDPGetColor(__VA_ARGS__)
180#endif
181 FREERDP_API UINT32 FreeRDPGetColor(UINT32 format, BYTE r, BYTE g, BYTE b, BYTE a);
182
183 /***
184 *
185 * Returns the number of bits the format format uses.
186 *
187 * @param format One of PIXEL_FORMAT_* defines
188 *
189 * @return The number of bits the format requires per pixel.
190 */
191#if defined(WITH_FREERDP_DEPRECATED)
192#define GetBitsPerPixel(...) FreeRDPGetBitsPerPixel(__VA_ARGS__)
193#endif
194 static inline UINT32 FreeRDPGetBitsPerPixel(UINT32 format)
195 {
196 return (((format) >> 24) & 0x3F);
197 }
198
199 /***
200 * @param format one of PIXEL_FORMAT_* color format defines
201 *
202 * @return TRUE if the format has an alpha channel, FALSE otherwise.
203 */
204#if defined(WITH_FREERDP_DEPRECATED)
205#define ColorHasAlpha(...) FreeRDPColorHasAlpha(__VA_ARGS__)
206#endif
207 static inline BOOL FreeRDPColorHasAlpha(UINT32 format)
208 {
209 UINT32 alpha = (((format) >> 12) & 0x0F);
210
211 if (alpha == 0)
212 return FALSE;
213
214 return TRUE;
215 }
216
217 /***
218 *
219 * Read a pixel from memory to internal representation
220 *
221 * @param src The source buffer
222 * @param format The PIXEL_FORMAT_* define the source buffer uses for encoding
223 *
224 * @return The pixel color in internal representation
225 */
226#if defined(WITH_FREERDP_DEPRECATED)
227#define ReadColor(...) FreeRDPReadColor(__VA_ARGS__)
228#endif
229 FREERDP_API UINT32 FreeRDPReadColor(const BYTE* WINPR_RESTRICT src, UINT32 format);
230
231 /***
232 *
233 * Write a pixel from internal representation to memory
234 *
235 * @param dst The destination buffer
236 * @param format The PIXEL_FORMAT_* define for encoding
237 * @param color The pixel color in internal representation
238 *
239 * @return TRUE if successful, FALSE otherwise
240 */
241#if defined(WITH_FREERDP_DEPRECATED)
242#define WriteColor(...) FreeRDPWriteColor(__VA_ARGS__)
243#define WriteColorIgnoreAlpha(...) FreeRDPWriteColorIgnoreAlpha(__VA_ARGS__)
244#endif
245 FREERDP_API BOOL FreeRDPWriteColor(BYTE* WINPR_RESTRICT dst, UINT32 format, UINT32 color);
246 FREERDP_API BOOL FreeRDPWriteColorIgnoreAlpha(BYTE* WINPR_RESTRICT dst, UINT32 format,
247 UINT32 color);
248
249 /***
250 *
251 * Converts a pixel in internal representation format srcFormat to internal
252 * representation format dstFormat
253 *
254 * @param color The pixel color in srcFormat representation
255 * @param srcFormat The PIXEL_FORMAT_* of color
256 * @param dstFormat The PIXEL_FORMAT_* of the return.
257 * @param palette palette to use (only used for 8 bit color!)
258 *
259 * @return The converted pixel color in dstFormat representation
260 */
261#if defined(WITH_FREERDP_DEPRECATED)
262#define ConvertColor(...) FreeRDPConvertColor(__VA_ARGS__)
263#endif
264 static inline UINT32 FreeRDPConvertColor(UINT32 color, UINT32 srcFormat, UINT32 dstFormat,
265 const gdiPalette* palette)
266 {
267 BYTE r = 0;
268 BYTE g = 0;
269 BYTE b = 0;
270 BYTE a = 0;
271 FreeRDPSplitColor(color, srcFormat, &r, &g, &b, &a, palette);
272 return FreeRDPGetColor(dstFormat, r, g, b, a);
273 }
274
275 /***
276 *
277 * Returns the number of bytes the format format uses.
278 *
279 * @param format One of PIXEL_FORMAT_* defines
280 *
281 * @return The number of bytes the format requires per pixel.
282 */
283#if defined(WITH_FREERDP_DEPRECATED)
284#define GetBytesPerPixel(...) FreeRDPGetBytesPerPixel(__VA_ARGS__)
285#endif
286 static inline UINT32 FreeRDPGetBytesPerPixel(UINT32 format)
287 {
288 return (FreeRDPGetBitsPerPixel(format) + 7) / 8;
289 }
290
291 /***
292 *
293 * @param width width to copy in pixels
294 * @param height height to copy in pixels
295 * @param data source buffer, must be (nWidth + 7) / 8 bytes long
296 *
297 * @return A buffer allocated with winpr_aligned_malloc(width * height, 16)
298 * if successful, NULL otherwise.
299 */
300 WINPR_ATTR_MALLOC(winpr_aligned_free, 1)
301 FREERDP_API BYTE* freerdp_glyph_convert(UINT32 width, UINT32 height,
302 const BYTE* WINPR_RESTRICT data);
303
304 /***
305 *
306 * @param pDstData destination buffer
307 * @param DstFormat destination buffer format
308 * @param nDstStep destination buffer stride (line in bytes) 0 for default
309 * @param nXDst destination buffer offset x
310 * @param nYDst destination buffer offset y
311 * @param nWidth width to copy in pixels
312 * @param nHeight height to copy in pixels
313 * @param pSrcData source buffer, must be (nWidth + 7) / 8 bytes long
314 * @param backColor The background color in internal representation format
315 * @param foreColor The foreground color in internal representation format
316 * @param palette palette to use (only used for 8 bit color!)
317 *
318 * @return TRUE if success, FALSE otherwise
319 */
320 FREERDP_API BOOL freerdp_image_copy_from_monochrome(
321 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst,
322 UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT pSrcData,
323 UINT32 backColor, UINT32 foreColor, const gdiPalette* WINPR_RESTRICT palette);
324
325 /***
326 *
327 * @param pDstData destination buffer
328 * @param DstFormat destination buffer format
329 * @param nDstStep destination buffer stride (line in bytes) 0 for default
330 * @param nXDst destination buffer offset x
331 * @param nYDst destination buffer offset y
332 * @param nWidth width to copy in pixels
333 * @param nHeight height to copy in pixels
334 * @param bitsColor icon's image data buffer
335 * @param cbBitsColor length of the image data buffer in bytes
336 * @param bitsMask icon's 1bpp image mask buffer
337 * @param cbBitsMask length of the image mask buffer in bytes
338 * @param colorTable icon's image color table
339 * @param cbColorTable length of the image color table buffer in bytes
340 * @param bpp color image data bits per pixel
341 *
342 * @return TRUE if success, FALSE otherwise
343 */
344 FREERDP_API BOOL freerdp_image_copy_from_icon_data(
345 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst,
346 UINT32 nYDst, UINT16 nWidth, UINT16 nHeight, const BYTE* WINPR_RESTRICT bitsColor,
347 UINT16 cbBitsColor, const BYTE* WINPR_RESTRICT bitsMask, UINT16 cbBitsMask,
348 const BYTE* WINPR_RESTRICT colorTable, UINT16 cbColorTable, UINT32 bpp);
349
350 /***
351 *
352 * @param pDstData destination buffer
353 * @param DstFormat destination buffer format
354 * @param nDstStep destination buffer stride (line in bytes) 0 for default
355 * @param nXDst destination buffer offset x
356 * @param nYDst destination buffer offset y
357 * @param nWidth width to copy in pixels
358 * @param nHeight height to copy in pixels
359 * @param xorMask XOR mask buffer
360 * @param xorMaskLength XOR mask length in bytes
361 * @param andMask AND mask buffer
362 * @param andMaskLength AND mask length in bytes
363 * @param xorBpp XOR bits per pixel
364 * @param palette palette to use (only used for 8 bit color!)
365 *
366 * @return TRUE if success, FALSE otherwise
367 */
368 FREERDP_API BOOL freerdp_image_copy_from_pointer_data(
369 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst,
370 UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT xorMask,
371 UINT32 xorMaskLength, const BYTE* WINPR_RESTRICT andMask, UINT32 andMaskLength,
372 UINT32 xorBpp, const gdiPalette* WINPR_RESTRICT palette);
373
374 /*** Copies an image from source to destination, converting if necessary.
375 * Source and destination may overlap.
376 *
377 * @param pDstData destination buffer
378 * @param DstFormat destination buffer format
379 * @param nDstStep destination buffer stride (line in bytes) 0 for default
380 * @param nXDst destination buffer offset x
381 * @param nYDst destination buffer offset y
382 * @param nWidth width to copy in pixels
383 * @param nHeight height to copy in pixels
384 * @param pSrcData source buffer
385 * @param SrcFormat source buffer format
386 * @param nSrcStep source buffer stride (line in bytes) 0 for default
387 * @param nXSrc source buffer x offset in pixels
388 * @param nYSrc source buffer y offset in pixels
389 * @param palette palette to use (only used for 8 bit color!)
390 * @param flags Image flipping flags FREERDP_FLIP_NONE et al
391 *
392 * @return TRUE if success, FALSE otherwise
393 */
394 FREERDP_API BOOL freerdp_image_copy(BYTE* pDstData, DWORD DstFormat, UINT32 nDstStep,
395 UINT32 nXDst, UINT32 nYDst, UINT32 nWidth, UINT32 nHeight,
396 const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep,
397 UINT32 nXSrc, UINT32 nYSrc,
398 const gdiPalette* WINPR_RESTRICT palette, UINT32 flags);
399
404 FREERDP_API BOOL freerdp_image_copy_overlap(
405 BYTE* pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
406 UINT32 nHeight, const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep, UINT32 nXSrc,
407 UINT32 nYSrc, const gdiPalette* WINPR_RESTRICT palette, UINT32 flags);
408
409 /*** Same as @ref freerdp_image_copy but only for non overlapping source and destination
410 * @since version 3.6.0
411 */
412 FREERDP_API BOOL freerdp_image_copy_no_overlap(
413 BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
414 UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
415 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc, const gdiPalette* WINPR_RESTRICT palette,
416 UINT32 flags);
417
418 /*** Scale an image to destination
419 *
420 * @param pDstData destination buffer
421 * @param DstFormat destination buffer format
422 * @param nDstStep destination buffer stride (line in bytes) 0 for default
423 * @param nXDst destination buffer offset x
424 * @param nYDst destination buffer offset y
425 * @param nDstWidth width of destination in pixels
426 * @param nDstHeight height of destination in pixels
427 * @param pSrcData source buffer
428 * @param SrcFormat source buffer format
429 * @param nSrcStep source buffer stride (line in bytes) 0 for default
430 * @param nXSrc source buffer x offset in pixels
431 * @param nYSrc source buffer y offset in pixels
432 * @param nSrcWidth width of source in pixels
433 * @param nSrcHeight height of source in pixels
434 *
435 * @return TRUE if success, FALSE otherwise
436 */
437 FREERDP_API BOOL freerdp_image_scale(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat,
438 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
439 UINT32 nDstWidth, UINT32 nDstHeight,
440 const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
441 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
442 UINT32 nSrcWidth, UINT32 nSrcHeight);
443
458 FREERDP_API BOOL freerdp_image_fill(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat,
459 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
460 UINT32 nHeight, UINT32 color);
461
462#define FREERDP_IMAGE_FILL_IGNORE_ALPHA 1
482 FREERDP_API BOOL freerdp_image_fill_ex(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat,
483 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
484 UINT32 nWidth, UINT32 nHeight, UINT32 color,
485 UINT32 flags);
486
487#ifdef __cplusplus
488}
489#endif
490
491#endif /* FREERDP_CODEC_COLOR_H */