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