2 * FreeRDP: A Remote Desktop Protocol Client
3 * Color Conversion Routines
5 * Copyright 2010 Marc-Andre Moreau <marcandre.moreau@gmail.com>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
23 #include <freerdp/api.h>
24 #include <freerdp/freerdp.h>
30 /* Color Space Conversions: http://msdn.microsoft.com/en-us/library/ff566496/ */
32 /* Color Space Conversion */
34 #define RGB_555_565(_r, _g, _b) \
36 _g = (_g << 1 & ~0x1) | (_g >> 4); \
39 #define RGB_565_555(_r, _g, _b) \
44 #define RGB_555_888(_r, _g, _b) \
45 _r = (_r << 3 & ~0x7) | (_r >> 2); \
46 _g = (_g << 3 & ~0x7) | (_g >> 2); \
47 _b = (_b << 3 & ~0x7) | (_b >> 2);
49 #define RGB_565_888(_r, _g, _b) \
50 _r = (_r << 3 & ~0x7) | (_r >> 2); \
51 _g = (_g << 2 & ~0x3) | (_g >> 4); \
52 _b = (_b << 3 & ~0x7) | (_b >> 2);
54 #define RGB_888_565(_r, _g, _b) \
59 #define RGB_888_555(_r, _g, _b) \
64 /* RGB 15 (RGB_555) */
66 #define RGB555(_r, _g, _b) \
67 ((_r & 0x1F) << 10) | ((_g & 0x1F) << 5) | (_b & 0x1F)
69 #define RGB15(_r, _g, _b) \
70 (((_r >> 3) & 0x1F) << 10) | (((_g >> 3) & 0x1F) << 5) | ((_b >> 3) & 0x1F)
72 #define GetRGB_555(_r, _g, _b, _p) \
73 _r = (_p & 0x7C00) >> 10; \
74 _g = (_p & 0x3E0) >> 5; \
77 #define GetRGB15(_r, _g, _b, _p) \
78 GetRGB_555(_r, _g, _b, _p); \
79 RGB_555_888(_r, _g, _b);
81 /* BGR 15 (BGR_555) */
83 #define BGR555(_r, _g, _b) \
84 ((_b & 0x1F) << 10) | ((_g & 0x1F) << 5) | (_r & 0x1F)
86 #define BGR15(_r, _g, _b) \
87 (((_b >> 3) & 0x1F) << 10) | (((_g >> 3) & 0x1F) << 5) | ((_r >> 3) & 0x1F)
89 #define GetBGR_555(_r, _g, _b, _p) \
90 _b = (_p & 0x7C00) >> 10; \
91 _g = (_p & 0x3E0) >> 5; \
94 #define GetBGR15(_r, _g, _b, _p) \
95 GetBGR_555(_r, _g, _b, _p); \
96 RGB_555_888(_r, _g, _b);
98 /* RGB 16 (RGB_565) */
100 #define RGB565(_r, _g, _b) \
101 ((_r & 0x1F) << 11) | ((_g & 0x3F) << 5) | (_b & 0x1F)
103 #define RGB16(_r, _g, _b) \
104 (((_r >> 3) & 0x1F) << 11) | (((_g >> 2) & 0x3F) << 5) | ((_b >> 3) & 0x1F)
106 #define GetRGB_565(_r, _g, _b, _p) \
107 _r = (_p & 0xF800) >> 11; \
108 _g = (_p & 0x7E0) >> 5; \
111 #define GetRGB16(_r, _g, _b, _p) \
112 GetRGB_565(_r, _g, _b, _p); \
113 RGB_565_888(_r, _g, _b);
115 /* BGR 16 (BGR_565) */
117 #define BGR565(_r, _g, _b) \
118 ((_b & 0x1F) << 11) | ((_g & 0x3F) << 5) | (_r & 0x1F)
120 #define BGR16(_r, _g, _b) \
121 (((_b >> 3) & 0x1F) << 11) | (((_g >> 2) & 0x3F) << 5) | ((_r >> 3) & 0x1F)
123 #define GetBGR_565(_r, _g, _b, _p) \
124 _b = (_p & 0xF800) >> 11; \
125 _g = (_p & 0x7E0) >> 5; \
128 #define GetBGR16(_r, _g, _b, _p) \
129 GetBGR_565(_r, _g, _b, _p); \
130 RGB_565_888(_r, _g, _b);
132 /* RGB 24 (RGB_888) */
134 #define RGB24(_r, _g, _b) \
135 (_r << 16) | (_g << 8) | _b
137 #define GetRGB24(_r, _g, _b, _p) \
138 _r = (_p & 0xFF0000) >> 16; \
139 _g = (_p & 0xFF00) >> 8; \
142 /* BGR 24 (BGR_888) */
144 #define BGR24(_r, _g, _b) \
145 (_b << 16) | (_g << 8) | _r
147 #define GetBGR24(_r, _g, _b, _p) \
148 _b = (_p & 0xFF0000) >> 16; \
149 _g = (_p & 0xFF00) >> 8; \
152 /* RGB 32 (ARGB_8888), alpha ignored */
154 #define RGB32(_r, _g, _b) \
155 (_r << 16) | (_g << 8) | _b
157 #define GetRGB32(_r, _g, _b, _p) \
158 _r = (_p & 0xFF0000) >> 16; \
159 _g = (_p & 0xFF00) >> 8; \
162 /* ARGB 32 (ARGB_8888) */
164 #define ARGB32(_a,_r, _g, _b) \
165 (_a << 24) | (_r << 16) | (_g << 8) | _b
167 #define GetARGB32(_a, _r, _g, _b, _p) \
168 _a = (_p & 0xFF000000) >> 24; \
169 _r = (_p & 0xFF0000) >> 16; \
170 _g = (_p & 0xFF00) >> 8; \
173 /* BGR 32 (ABGR_8888), alpha ignored */
175 #define BGR32(_r, _g, _b) \
176 (_b << 16) | (_g << 8) | _r
178 #define GetBGR32(_r, _g, _b, _p) \
179 _b = (_p & 0xFF0000) >> 16; \
180 _g = (_p & 0xFF00) >> 8; \
183 /* BGR 32 (ABGR_8888) */
185 #define ABGR32(_a, _r, _g, _b) \
186 (_a << 24) | (_b << 16) | (_g << 8) | _r
188 #define GetABGR32(_a, _r, _g, _b, _p) \
189 _a = (_p & 0xFF000000) >> 24; \
190 _b = (_p & 0xFF0000) >> 16; \
191 _g = (_p & 0xFF00) >> 8; \
194 /* Color Conversion */
196 #define BGR16_RGB32(_r, _g, _b, _p) \
197 GetBGR16(_r, _g, _b, _p); \
198 RGB_565_888(_r, _g, _b); \
199 _p = RGB32(_r, _g, _b);
201 #define RGB32_RGB16(_r, _g, _b, _p) \
202 GetRGB32(_r, _g, _b, _p); \
203 RGB_888_565(_r, _g, _b); \
204 _p = RGB565(_r, _g, _b);
206 #define RGB15_RGB16(_r, _g, _b, _p) \
207 GetRGB_555(_r, _g, _b, _p); \
208 _g = (_g << 1 & ~0x1) | (_g >> 4); \
209 _p = RGB565(_r, _g, _b);
211 #define RGB16_RGB15(_r, _g, _b, _p) \
212 GetRGB_565(_r, _g, _b, _p); \
214 _p = RGB555(_r, _g, _b);
216 #define CLRCONV_ALPHA 1
217 #define CLRCONV_INVERT 2
218 /* if defined RGB555 format is used when rendering with a 16-bit frame buffer */
219 #define CLRCONV_RGB555 4
221 /* Supported Internal Buffer Formats */
222 #define CLRBUF_16BPP 8
223 #define CLRBUF_24BPP 16
224 #define CLRBUF_32BPP 32
233 typedef struct _CLRCONV CLRCONV;
234 typedef CLRCONV* HCLRCONV;
236 #define IBPP(_bpp) (((_bpp + 1)/ 8) % 5)
238 typedef uint8* (*p_freerdp_image_convert)(uint8* srcData, uint8* dstData, int width, int height, int srcBpp, int dstBpp, HCLRCONV clrconv);
240 FREERDP_API uint8* freerdp_image_convert(uint8* srcData, uint8 *dstData, int width, int height, int srcBpp, int dstBpp, HCLRCONV clrconv);
241 FREERDP_API uint8* freerdp_glyph_convert(int width, int height, uint8* data);
242 FREERDP_API void freerdp_bitmap_flip(uint8 * src, uint8 * dst, int scanLineSz, int height);
243 FREERDP_API uint8* freerdp_image_flip(uint8* srcData, uint8* dstData, int width, int height, int bpp);
244 FREERDP_API uint8* freerdp_icon_convert(uint8* srcData, uint8* dstData, uint8* mask, int width, int height, int bpp, HCLRCONV clrconv);
245 FREERDP_API uint8* freerdp_mono_image_convert(uint8* srcData, int width, int height, int srcBpp, int dstBpp, uint32 bgcolor, uint32 fgcolor, HCLRCONV clrconv);
246 FREERDP_API void freerdp_alpha_cursor_convert(uint8* alphaData, uint8* xorMask, uint8* andMask, int width, int height, int bpp, HCLRCONV clrconv);
247 FREERDP_API void freerdp_image_swap_color_order(uint8* data, int width, int height);
249 FREERDP_API uint32 freerdp_color_convert_var(uint32 srcColor, int srcBpp, int dstBpp, HCLRCONV clrconv);
250 FREERDP_API uint32 freerdp_color_convert_rgb(uint32 srcColor, int srcBpp, int dstBpp, HCLRCONV clrconv);
251 FREERDP_API uint32 freerdp_color_convert_bgr(uint32 srcColor, int srcBpp, int dstBpp, HCLRCONV clrconv);
252 FREERDP_API uint32 freerdp_color_convert_rgb_bgr(uint32 srcColor, int srcBpp, int dstBpp, HCLRCONV clrconv);
253 FREERDP_API uint32 freerdp_color_convert_bgr_rgb(uint32 srcColor, int srcBpp, int dstBpp, HCLRCONV clrconv);
254 FREERDP_API uint32 freerdp_color_convert_var_rgb(uint32 srcColor, int srcBpp, int dstBpp, HCLRCONV clrconv);
255 FREERDP_API uint32 freerdp_color_convert_var_bgr(uint32 srcColor, int srcBpp, int dstBpp, HCLRCONV clrconv);
257 FREERDP_API HCLRCONV freerdp_clrconv_new(uint32 flags);
258 FREERDP_API void freerdp_clrconv_free(HCLRCONV clrconv);
264 #endif /* __COLOR_H */