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#if !defined(WITHOUT_FREERDP_3x_DEPRECATED)
292 /***
293 *
294 * @param width width to copy in pixels
295 * @param height height to copy in pixels
296 * @param data source buffer, must be (nWidth + 7) / 8 bytes long
297 *
298 * @return A buffer allocated with winpr_aligned_malloc(width * height, 16)
299 * if successful, NULL otherwise.
300 */
301
302 WINPR_DEPRECATED_VAR("[since 3.21.0] use freerdp_glyph_convert_ex instead",
303 WINPR_ATTR_MALLOC(winpr_aligned_free, 1)
304 FREERDP_API BYTE* freerdp_glyph_convert(
305 UINT32 width, UINT32 height, const BYTE* WINPR_RESTRICT data));
306#endif
307
308 /***
309 *
310 * @param width width to copy in pixels
311 * @param height height to copy in pixels
312 * @param data source buffer, must be (nWidth + 7) / 8 bytes long
313 * @param len the length of \ref data in bytes
314 *
315 * @return A buffer allocated with winpr_aligned_malloc(width * height, 16)
316 * if successful, NULL otherwise.
317 * @since version 3.21.0
318 */
319 WINPR_ATTR_MALLOC(winpr_aligned_free, 1)
320 FREERDP_API BYTE* freerdp_glyph_convert_ex(UINT32 width, UINT32 height,
321 const BYTE* WINPR_RESTRICT data, size_t len);
322
323 /***
324 *
325 * @param pDstData destination buffer
326 * @param DstFormat destination buffer format
327 * @param nDstStep destination buffer stride (line in bytes) 0 for default
328 * @param nXDst destination buffer offset x
329 * @param nYDst destination buffer offset y
330 * @param nWidth width to copy in pixels
331 * @param nHeight height to copy in pixels
332 * @param pSrcData source buffer, must be (nWidth + 7) / 8 bytes long
333 * @param backColor The background color in internal representation format
334 * @param foreColor The foreground color in internal representation format
335 * @param palette palette to use (only used for 8 bit color!)
336 *
337 * @return TRUE if success, FALSE otherwise
338 */
339 FREERDP_API BOOL freerdp_image_copy_from_monochrome(
340 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst,
341 UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT pSrcData,
342 UINT32 backColor, UINT32 foreColor, const gdiPalette* WINPR_RESTRICT palette);
343
344 /***
345 *
346 * @param pDstData destination buffer
347 * @param DstFormat destination buffer format
348 * @param nDstStep destination buffer stride (line in bytes) 0 for default
349 * @param nXDst destination buffer offset x
350 * @param nYDst destination buffer offset y
351 * @param nWidth width to copy in pixels
352 * @param nHeight height to copy in pixels
353 * @param bitsColor icon's image data buffer
354 * @param cbBitsColor length of the image data buffer in bytes
355 * @param bitsMask icon's 1bpp image mask buffer
356 * @param cbBitsMask length of the image mask buffer in bytes
357 * @param colorTable icon's image color table
358 * @param cbColorTable length of the image color table buffer in bytes
359 * @param bpp color image data bits per pixel
360 *
361 * @return TRUE if success, FALSE otherwise
362 */
363 FREERDP_API BOOL freerdp_image_copy_from_icon_data(
364 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst,
365 UINT32 nYDst, UINT16 nWidth, UINT16 nHeight, const BYTE* WINPR_RESTRICT bitsColor,
366 UINT16 cbBitsColor, const BYTE* WINPR_RESTRICT bitsMask, UINT16 cbBitsMask,
367 const BYTE* WINPR_RESTRICT colorTable, UINT16 cbColorTable, UINT32 bpp);
368
369 /***
370 *
371 * @param pDstData destination buffer
372 * @param DstFormat destination buffer format
373 * @param nDstStep destination buffer stride (line in bytes) 0 for default
374 * @param nXDst destination buffer offset x
375 * @param nYDst destination buffer offset y
376 * @param nWidth width to copy in pixels
377 * @param nHeight height to copy in pixels
378 * @param xorMask XOR mask buffer
379 * @param xorMaskLength XOR mask length in bytes
380 * @param andMask AND mask buffer
381 * @param andMaskLength AND mask length in bytes
382 * @param xorBpp XOR bits per pixel
383 * @param palette palette to use (only used for 8 bit color!)
384 *
385 * @return TRUE if success, FALSE otherwise
386 */
387 FREERDP_API BOOL freerdp_image_copy_from_pointer_data(
388 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst,
389 UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT xorMask,
390 UINT32 xorMaskLength, const BYTE* WINPR_RESTRICT andMask, UINT32 andMaskLength,
391 UINT32 xorBpp, const gdiPalette* WINPR_RESTRICT palette);
392
393 /*** Copies an image from source to destination, converting if necessary.
394 * Source and destination may overlap.
395 *
396 * @param pDstData destination buffer
397 * @param DstFormat destination buffer format
398 * @param nDstStep destination buffer stride (line in bytes) 0 for default
399 * @param nXDst destination buffer offset x
400 * @param nYDst destination buffer offset y
401 * @param nWidth width to copy in pixels
402 * @param nHeight height to copy in pixels
403 * @param pSrcData source buffer
404 * @param SrcFormat source buffer format
405 * @param nSrcStep source buffer stride (line in bytes) 0 for default
406 * @param nXSrc source buffer x offset in pixels
407 * @param nYSrc source buffer y offset in pixels
408 * @param palette palette to use (only used for 8 bit color!)
409 * @param flags Image flipping flags FREERDP_FLIP_NONE et al
410 *
411 * @return TRUE if success, FALSE otherwise
412 */
413 FREERDP_API BOOL freerdp_image_copy(BYTE* pDstData, DWORD DstFormat, UINT32 nDstStep,
414 UINT32 nXDst, UINT32 nYDst, UINT32 nWidth, UINT32 nHeight,
415 const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep,
416 UINT32 nXSrc, UINT32 nYSrc,
417 const gdiPalette* WINPR_RESTRICT palette, UINT32 flags);
418
423 FREERDP_API BOOL freerdp_image_copy_overlap(
424 BYTE* pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
425 UINT32 nHeight, const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep, UINT32 nXSrc,
426 UINT32 nYSrc, const gdiPalette* WINPR_RESTRICT palette, UINT32 flags);
427
428 /*** Same as @ref freerdp_image_copy but only for non overlapping source and destination
429 * @since version 3.6.0
430 */
431 FREERDP_API BOOL freerdp_image_copy_no_overlap(
432 BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
433 UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
434 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc, const gdiPalette* WINPR_RESTRICT palette,
435 UINT32 flags);
436
437 /*** Scale an image to destination
438 *
439 * @param pDstData destination buffer
440 * @param DstFormat destination buffer format
441 * @param nDstStep destination buffer stride (line in bytes) 0 for default
442 * @param nXDst destination buffer offset x
443 * @param nYDst destination buffer offset y
444 * @param nDstWidth width of destination in pixels
445 * @param nDstHeight height of destination in pixels
446 * @param pSrcData source buffer
447 * @param SrcFormat source buffer format
448 * @param nSrcStep source buffer stride (line in bytes) 0 for default
449 * @param nXSrc source buffer x offset in pixels
450 * @param nYSrc source buffer y offset in pixels
451 * @param nSrcWidth width of source in pixels
452 * @param nSrcHeight height of source in pixels
453 *
454 * @return TRUE if success, FALSE otherwise
455 */
456 FREERDP_API BOOL freerdp_image_scale(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat,
457 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
458 UINT32 nDstWidth, UINT32 nDstHeight,
459 const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
460 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
461 UINT32 nSrcWidth, UINT32 nSrcHeight);
462
477 FREERDP_API BOOL freerdp_image_fill(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat,
478 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
479 UINT32 nHeight, UINT32 color);
480
481#define FREERDP_IMAGE_FILL_IGNORE_ALPHA 1
501 FREERDP_API BOOL freerdp_image_fill_ex(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat,
502 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
503 UINT32 nWidth, UINT32 nHeight, UINT32 color,
504 UINT32 flags);
505
506#ifdef __cplusplus
507}
508#endif
509
510#endif /* FREERDP_CODEC_COLOR_H */