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 */
120WINPR_ATTR_NODISCARD
121FREERDP_API DWORD FreeRDPAreColorFormatsEqualNoAlpha(DWORD first, DWORD second);
122
123/* Color Space Conversions: http://msdn.microsoft.com/en-us/library/ff566496/ */
124
125/***
126 *
127 * Get a string representation of a color
128 *
129 * @param format The pixel color format
130 *
131 * @return A string representation of format
132 */
133#if defined(WITH_FREERDP_DEPRECATED)
134#define GetColorFormatName(...) FreeRDPGetColorFormatName(__VA_ARGS__)
135#endif
136 WINPR_ATTR_NODISCARD
137 FREERDP_API const char* FreeRDPGetColorFormatName(UINT32 format);
138
146 WINPR_ATTR_NODISCARD
147 FREERDP_API uint32_t FreeRDPGetColorFromatFromName(const char* name);
148
149/***
150 *
151 * Converts a pixel color in internal representation to its red, green, blue
152 * and alpha components.
153 *
154 * @param color The color in format internal representation
155 * @param format one of PIXEL_FORMAT_* color format defines
156 * @param _r red color value
157 * @param _g green color value
158 * @param _b blue color value
159 * @param _a alpha color value
160 * @param palette palette to use (only used for 8 bit color!)
161 */
162#if defined(WITH_FREERDP_DEPRECATED)
163#define SplitColor(...) FreeRDPSplitColor(__VA_ARGS__)
164#endif
165 FREERDP_API void FreeRDPSplitColor(UINT32 color, UINT32 format, BYTE* _r, BYTE* _g, BYTE* _b,
166 BYTE* _a, const gdiPalette* palette);
167
168 /***
169 *
170 * Converts red, green, blue and alpha values to internal representation.
171 *
172 * @param format one of PIXEL_FORMAT_* color format defines
173 * @param r red color value
174 * @param g green color value
175 * @param b blue color value
176 * @param a alpha color value
177 *
178 * @return The pixel color in the desired format. Value is in internal
179 * representation.
180 */
181#if defined(WITH_FREERDP_DEPRECATED)
182#define GetColor(...) FreeRDPGetColor(__VA_ARGS__)
183#endif
184 WINPR_ATTR_NODISCARD
185 FREERDP_API UINT32 FreeRDPGetColor(UINT32 format, BYTE r, BYTE g, BYTE b, BYTE a);
186
187 /***
188 *
189 * Returns the number of bits the format format uses.
190 *
191 * @param format One of PIXEL_FORMAT_* defines
192 *
193 * @return The number of bits the format requires per pixel.
194 */
195#if defined(WITH_FREERDP_DEPRECATED)
196#define GetBitsPerPixel(...) FreeRDPGetBitsPerPixel(__VA_ARGS__)
197#endif
198 WINPR_ATTR_NODISCARD
199 static inline UINT32 FreeRDPGetBitsPerPixel(UINT32 format)
200 {
201 return (((format) >> 24) & 0x3F);
202 }
203
204 /***
205 * @param format one of PIXEL_FORMAT_* color format defines
206 *
207 * @return TRUE if the format has an alpha channel, FALSE otherwise.
208 */
209#if defined(WITH_FREERDP_DEPRECATED)
210#define ColorHasAlpha(...) FreeRDPColorHasAlpha(__VA_ARGS__)
211#endif
212 WINPR_ATTR_NODISCARD
213 static inline BOOL FreeRDPColorHasAlpha(UINT32 format)
214 {
215 UINT32 alpha = (((format) >> 12) & 0x0F);
216
217 return (alpha != 0);
218 }
219
220 /***
221 *
222 * Read a pixel from memory to internal representation
223 *
224 * @param src The source buffer
225 * @param format The PIXEL_FORMAT_* define the source buffer uses for encoding
226 *
227 * @return The pixel color in internal representation
228 */
229#if defined(WITH_FREERDP_DEPRECATED)
230#define ReadColor(...) FreeRDPReadColor(__VA_ARGS__)
231#endif
232 WINPR_ATTR_NODISCARD
233 FREERDP_API UINT32 FreeRDPReadColor(const BYTE* WINPR_RESTRICT src, UINT32 format);
234
235 /***
236 *
237 * Write a pixel from internal representation to memory
238 *
239 * @param dst The destination buffer
240 * @param format The PIXEL_FORMAT_* define for encoding
241 * @param color The pixel color in internal representation
242 *
243 * @return TRUE if successful, FALSE otherwise
244 */
245#if defined(WITH_FREERDP_DEPRECATED)
246#define WriteColor(...) FreeRDPWriteColor(__VA_ARGS__)
247#define WriteColorIgnoreAlpha(...) FreeRDPWriteColorIgnoreAlpha(__VA_ARGS__)
248#endif
249 FREERDP_API BOOL FreeRDPWriteColor(BYTE* WINPR_RESTRICT dst, UINT32 format, UINT32 color);
250
251 FREERDP_API BOOL FreeRDPWriteColorIgnoreAlpha(BYTE* WINPR_RESTRICT dst, UINT32 format,
252 UINT32 color);
253
254 /***
255 *
256 * Converts a pixel in internal representation format srcFormat to internal
257 * representation format dstFormat
258 *
259 * @param color The pixel color in srcFormat representation
260 * @param srcFormat The PIXEL_FORMAT_* of color
261 * @param dstFormat The PIXEL_FORMAT_* of the return.
262 * @param palette palette to use (only used for 8 bit color!)
263 *
264 * @return The converted pixel color in dstFormat representation
265 */
266#if defined(WITH_FREERDP_DEPRECATED)
267#define ConvertColor(...) FreeRDPConvertColor(__VA_ARGS__)
268#endif
269 WINPR_ATTR_NODISCARD
270 static inline UINT32 FreeRDPConvertColor(UINT32 color, UINT32 srcFormat, UINT32 dstFormat,
271 const gdiPalette* palette)
272 {
273 BYTE r = 0;
274 BYTE g = 0;
275 BYTE b = 0;
276 BYTE a = 0;
277 FreeRDPSplitColor(color, srcFormat, &r, &g, &b, &a, palette);
278 return FreeRDPGetColor(dstFormat, r, g, b, a);
279 }
280
281 /***
282 *
283 * Returns the number of bytes the format format uses.
284 *
285 * @param format One of PIXEL_FORMAT_* defines
286 *
287 * @return The number of bytes the format requires per pixel.
288 */
289#if defined(WITH_FREERDP_DEPRECATED)
290#define GetBytesPerPixel(...) FreeRDPGetBytesPerPixel(__VA_ARGS__)
291#endif
292 WINPR_ATTR_NODISCARD
293 static inline UINT32 FreeRDPGetBytesPerPixel(UINT32 format)
294 {
295 return (FreeRDPGetBitsPerPixel(format) + 7) / 8;
296 }
297
298#if !defined(WITHOUT_FREERDP_3x_DEPRECATED)
299 /***
300 *
301 * @param width width to copy in pixels
302 * @param height height to copy in pixels
303 * @param data source buffer, must be (nWidth + 7) / 8 bytes long
304 *
305 * @return A buffer allocated with winpr_aligned_malloc(width * height, 16)
306 * if successful, nullptr otherwise.
307 */
308
309 WINPR_DEPRECATED_VAR("[since 3.21.0] use freerdp_glyph_convert_ex instead",
310 WINPR_ATTR_MALLOC(winpr_aligned_free, 1)
311 WINPR_ATTR_NODISCARD FREERDP_API BYTE* freerdp_glyph_convert(
312 UINT32 width, UINT32 height, const BYTE* WINPR_RESTRICT data));
313#endif
314
315 /***
316 *
317 * @param width width to copy in pixels
318 * @param height height to copy in pixels
319 * @param data source buffer, must be (nWidth + 7) / 8 bytes long
320 * @param len the length of \ref data in bytes
321 *
322 * @return A buffer allocated with winpr_aligned_malloc(width * height, 16)
323 * if successful, nullptr otherwise.
324 * @since version 3.21.0
325 */
326 WINPR_ATTR_MALLOC(winpr_aligned_free, 1)
327 WINPR_ATTR_NODISCARD
328 FREERDP_API BYTE* freerdp_glyph_convert_ex(UINT32 width, UINT32 height,
329 const BYTE* WINPR_RESTRICT data, size_t len);
330
331 /***
332 *
333 * @param pDstData destination buffer
334 * @param DstFormat destination buffer format
335 * @param nDstStep destination buffer stride (line in bytes) 0 for default
336 * @param nXDst destination buffer offset x
337 * @param nYDst destination buffer offset y
338 * @param nWidth width to copy in pixels
339 * @param nHeight height to copy in pixels
340 * @param pSrcData source buffer, must be (nWidth + 7) / 8 bytes long
341 * @param backColor The background color in internal representation format
342 * @param foreColor The foreground color in internal representation format
343 * @param palette palette to use (only used for 8 bit color!)
344 *
345 * @return TRUE if success, FALSE otherwise
346 */
347 WINPR_ATTR_NODISCARD
348 FREERDP_API BOOL freerdp_image_copy_from_monochrome(
349 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst,
350 UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT pSrcData,
351 UINT32 backColor, UINT32 foreColor, const gdiPalette* WINPR_RESTRICT palette);
352
353 /***
354 *
355 * @param pDstData destination buffer
356 * @param DstFormat destination buffer format
357 * @param nDstStep destination buffer stride (line in bytes) 0 for default
358 * @param nXDst destination buffer offset x
359 * @param nYDst destination buffer offset y
360 * @param nWidth width to copy in pixels
361 * @param nHeight height to copy in pixels
362 * @param bitsColor icon's image data buffer
363 * @param cbBitsColor length of the image data buffer in bytes
364 * @param bitsMask icon's 1bpp image mask buffer
365 * @param cbBitsMask length of the image mask buffer in bytes
366 * @param colorTable icon's image color table
367 * @param cbColorTable length of the image color table buffer in bytes
368 * @param bpp color image data bits per pixel
369 *
370 * @return TRUE if success, FALSE otherwise
371 */
372 WINPR_ATTR_NODISCARD
373 FREERDP_API BOOL freerdp_image_copy_from_icon_data(
374 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst,
375 UINT32 nYDst, UINT16 nWidth, UINT16 nHeight, const BYTE* WINPR_RESTRICT bitsColor,
376 UINT16 cbBitsColor, const BYTE* WINPR_RESTRICT bitsMask, UINT16 cbBitsMask,
377 const BYTE* WINPR_RESTRICT colorTable, UINT16 cbColorTable, UINT32 bpp);
378
379 /***
380 *
381 * @param pDstData destination buffer
382 * @param DstFormat destination buffer format
383 * @param nDstStep destination buffer stride (line in bytes) 0 for default
384 * @param nXDst destination buffer offset x
385 * @param nYDst destination buffer offset y
386 * @param nWidth width to copy in pixels
387 * @param nHeight height to copy in pixels
388 * @param xorMask XOR mask buffer
389 * @param xorMaskLength XOR mask length in bytes
390 * @param andMask AND mask buffer
391 * @param andMaskLength AND mask length in bytes
392 * @param xorBpp XOR bits per pixel
393 * @param palette palette to use (only used for 8 bit color!)
394 *
395 * @return TRUE if success, FALSE otherwise
396 */
397 WINPR_ATTR_NODISCARD
398 FREERDP_API BOOL freerdp_image_copy_from_pointer_data(
399 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst,
400 UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT xorMask,
401 UINT32 xorMaskLength, const BYTE* WINPR_RESTRICT andMask, UINT32 andMaskLength,
402 UINT32 xorBpp, const gdiPalette* WINPR_RESTRICT palette);
403
404 /*** Copies an image from source to destination, converting if necessary.
405 * Source and destination may overlap.
406 *
407 * @param pDstData destination buffer
408 * @param DstFormat destination buffer format
409 * @param nDstStep destination buffer stride (line in bytes) 0 for default
410 * @param nXDst destination buffer offset x
411 * @param nYDst destination buffer offset y
412 * @param nWidth width to copy in pixels
413 * @param nHeight height to copy in pixels
414 * @param pSrcData source buffer
415 * @param SrcFormat source buffer format
416 * @param nSrcStep source buffer stride (line in bytes) 0 for default
417 * @param nXSrc source buffer x offset in pixels
418 * @param nYSrc source buffer y offset in pixels
419 * @param palette palette to use (only used for 8 bit color!)
420 * @param flags Image flipping flags FREERDP_FLIP_NONE et al
421 *
422 * @return TRUE if success, FALSE otherwise
423 */
424 WINPR_ATTR_NODISCARD
425 FREERDP_API BOOL freerdp_image_copy(BYTE* pDstData, DWORD DstFormat, UINT32 nDstStep,
426 UINT32 nXDst, UINT32 nYDst, UINT32 nWidth, UINT32 nHeight,
427 const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep,
428 UINT32 nXSrc, UINT32 nYSrc,
429 const gdiPalette* WINPR_RESTRICT palette, UINT32 flags);
430
435 WINPR_ATTR_NODISCARD
436 FREERDP_API BOOL freerdp_image_copy_overlap(
437 BYTE* pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
438 UINT32 nHeight, const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep, UINT32 nXSrc,
439 UINT32 nYSrc, const gdiPalette* WINPR_RESTRICT palette, UINT32 flags);
440
441 /*** Same as @ref freerdp_image_copy but only for non overlapping source and destination
442 * @since version 3.6.0
443 */
444 WINPR_ATTR_NODISCARD
445 FREERDP_API BOOL freerdp_image_copy_no_overlap(
446 BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
447 UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
448 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc, const gdiPalette* WINPR_RESTRICT palette,
449 UINT32 flags);
450
451 /*** Scale an image to destination
452 *
453 * @param pDstData destination buffer
454 * @param DstFormat destination buffer format
455 * @param nDstStep destination buffer stride (line in bytes) 0 for default
456 * @param nXDst destination buffer offset x
457 * @param nYDst destination buffer offset y
458 * @param nDstWidth width of destination in pixels
459 * @param nDstHeight height of destination in pixels
460 * @param pSrcData source buffer
461 * @param SrcFormat source buffer format
462 * @param nSrcStep source buffer stride (line in bytes) 0 for default
463 * @param nXSrc source buffer x offset in pixels
464 * @param nYSrc source buffer y offset in pixels
465 * @param nSrcWidth width of source in pixels
466 * @param nSrcHeight height of source in pixels
467 *
468 * @return TRUE if success, FALSE otherwise
469 */
470 WINPR_ATTR_NODISCARD
471 FREERDP_API BOOL freerdp_image_scale(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat,
472 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
473 UINT32 nDstWidth, UINT32 nDstHeight,
474 const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
475 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
476 UINT32 nSrcWidth, UINT32 nSrcHeight);
477
492 WINPR_ATTR_NODISCARD
493 FREERDP_API BOOL freerdp_image_fill(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat,
494 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
495 UINT32 nHeight, UINT32 color);
496
497#define FREERDP_IMAGE_FILL_IGNORE_ALPHA 1
517 WINPR_ATTR_NODISCARD
518 FREERDP_API BOOL freerdp_image_fill_ex(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat,
519 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
520 UINT32 nWidth, UINT32 nHeight, UINT32 color,
521 UINT32 flags);
522
523#ifdef __cplusplus
524}
525#endif
526
527#endif /* FREERDP_CODEC_COLOR_H */