1 /**
2  * FreeRDP: A Remote Desktop Protocol Server
3  * freerdp wrapper
4  *
5  * Copyright 2011-2012 Jay Sorg
6  *
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19 
20 #if defined(HAVE_CONFIG_H)
21 #include <config_ac.h>
22 #endif
23 
24 #include "xrdp-neutrinordp.h"
25 
26 char *
convert_bitmap(int in_bpp,int out_bpp,char * bmpdata,int width,int height,int * palette)27 convert_bitmap(int in_bpp, int out_bpp, char *bmpdata,
28                int width, int height, int *palette)
29 {
30     char *out;
31     char *src;
32     char *dst;
33     int i;
34     int j;
35     int red;
36     int green;
37     int blue;
38     int pixel;
39 
40     if ((in_bpp == 8) && (out_bpp == 8))
41     {
42         out = (char *)g_malloc(width * height, 0);
43         src = bmpdata;
44         dst = out;
45 
46         for (i = 0; i < height; i++)
47         {
48             for (j = 0; j < width; j++)
49             {
50                 pixel = *((tui8 *)src);
51                 pixel = palette[pixel];
52                 SPLITCOLOR32(red, green, blue, pixel);
53                 pixel = COLOR8(red, green, blue);
54                 *dst = pixel;
55                 src++;
56                 dst++;
57             }
58         }
59 
60         return out;
61     }
62 
63     if ((in_bpp == 8) && (out_bpp == 16))
64     {
65         out = (char *)g_malloc(width * height * 2, 0);
66         src = bmpdata;
67         dst = out;
68 
69         for (i = 0; i < height; i++)
70         {
71             for (j = 0; j < width; j++)
72             {
73                 pixel = *((tui8 *)src);
74                 pixel = palette[pixel];
75                 SPLITCOLOR32(red, green, blue, pixel);
76                 pixel = COLOR16(red, green, blue);
77                 *((tui16 *)dst) = pixel;
78                 src++;
79                 dst += 2;
80             }
81         }
82 
83         return out;
84     }
85 
86     if ((in_bpp == 8) && (out_bpp == 24))
87     {
88         out = (char *)g_malloc(width * height * 4, 0);
89         src = bmpdata;
90         dst = out;
91 
92         for (i = 0; i < height; i++)
93         {
94             for (j = 0; j < width; j++)
95             {
96                 pixel = *((tui8 *)src);
97                 pixel = palette[pixel];
98                 SPLITCOLOR32(red, green, blue, pixel);
99                 pixel = COLOR24RGB(red, green, blue);
100                 *((tui32 *)dst) = pixel;
101                 src++;
102                 dst += 4;
103             }
104         }
105 
106         return out;
107     }
108 
109     if ((in_bpp == 15) && (out_bpp == 16))
110     {
111         out = (char *)g_malloc(width * height * 2, 0);
112         src = bmpdata;
113         dst = out;
114 
115         for (i = 0; i < height; i++)
116         {
117             for (j = 0; j < width; j++)
118             {
119                 pixel = *((tui16 *)src);
120                 SPLITCOLOR15(red, green, blue, pixel);
121                 pixel = COLOR16(red, green, blue);
122                 *((tui16 *)dst) = pixel;
123                 src += 2;
124                 dst += 2;
125             }
126         }
127 
128         return out;
129     }
130 
131     if ((in_bpp == 15) && (out_bpp == 24))
132     {
133         out = (char *)g_malloc(width * height * 4, 0);
134         src = bmpdata;
135         dst = out;
136 
137         for (i = 0; i < height; i++)
138         {
139             for (j = 0; j < width; j++)
140             {
141                 pixel = *((tui16 *)src);
142                 SPLITCOLOR15(red, green, blue, pixel);
143                 pixel = COLOR24RGB(red, green, blue);
144                 *((tui32 *)dst) = pixel;
145                 src += 2;
146                 dst += 4;
147             }
148         }
149 
150         return out;
151     }
152 
153     if ((in_bpp == 15) && (out_bpp == 15))
154     {
155         return bmpdata;
156     }
157 
158     if ((in_bpp == 16) && (out_bpp == 16))
159     {
160         return bmpdata;
161     }
162 
163     if ((in_bpp == 16) && (out_bpp == 24))
164     {
165         out = (char *)g_malloc(width * height * 4, 0);
166         src = bmpdata;
167         dst = out;
168 
169         for (i = 0; i < height; i++)
170         {
171             for (j = 0; j < width; j++)
172             {
173                 pixel = *((tui16 *)src);
174                 SPLITCOLOR16(red, green, blue, pixel);
175                 pixel = COLOR24RGB(red, green, blue);
176                 *((tui32 *)dst) = pixel;
177                 src += 2;
178                 dst += 4;
179             }
180         }
181 
182         return out;
183     }
184 
185     if ((in_bpp == 24) && (out_bpp == 24))
186     {
187         out = (char *)g_malloc(width * height * 4, 0);
188         src = bmpdata;
189         dst = out;
190 
191         for (i = 0; i < height; i++)
192         {
193             for (j = 0; j < width; j++)
194             {
195                 blue = *((tui8 *)src);
196                 src++;
197                 green = *((tui8 *)src);
198                 src++;
199                 red = *((tui8 *)src);
200                 src++;
201                 pixel = COLOR24RGB(red, green, blue);
202                 *((tui32 *)dst) = pixel;
203                 dst += 4;
204             }
205         }
206 
207         return out;
208     }
209 
210     if ((in_bpp == 32) && (out_bpp == 24))
211     {
212         return bmpdata;
213     }
214 
215     if ((in_bpp == 32) && (out_bpp == 32))
216     {
217         return bmpdata;
218     }
219 
220     if ((in_bpp == 16) && (out_bpp == 32))
221     {
222         out = (char *)g_malloc(width * height * 4, 0);
223         src = bmpdata;
224         dst = out;
225 
226         for (i = 0; i < height; i++)
227         {
228             for (j = 0; j < width; j++)
229             {
230                 pixel = *((tui16 *)src);
231                 SPLITCOLOR16(red, green, blue, pixel);
232                 pixel = COLOR24RGB(red, green, blue);
233                 *((tui32 *)dst) = pixel;
234                 src += 2;
235                 dst += 4;
236             }
237         }
238 
239         return out;
240     }
241 
242     LOG(LOG_LEVEL_WARNING, "convert_bitmap: error unknown conversion from %d to %d",
243         in_bpp, out_bpp);
244     return 0;
245 }
246 
247 /*****************************************************************************/
248 /* returns color or 0 */
249 int
convert_color(int in_bpp,int out_bpp,int in_color,int * palette)250 convert_color(int in_bpp, int out_bpp, int in_color, int *palette)
251 {
252     int pixel;
253     int red;
254     int green;
255     int blue;
256 
257     if ((in_bpp == 1) && (out_bpp == 24))
258     {
259         pixel = in_color == 0 ? 0 : 0xffffff;
260         return pixel;
261     }
262 
263     if ((in_bpp == 8) && (out_bpp == 8))
264     {
265         pixel = palette[in_color];
266         SPLITCOLOR32(red, green, blue, pixel);
267         pixel = COLOR8(red, green, blue);
268         return pixel;
269     }
270 
271     if ((in_bpp == 8) && (out_bpp == 16))
272     {
273         pixel = palette[in_color];
274         SPLITCOLOR32(red, green, blue, pixel);
275         pixel = COLOR16(red, green, blue);
276         return pixel;
277     }
278 
279     if ((in_bpp == 8) && (out_bpp == 24))
280     {
281         pixel = palette[in_color];
282         SPLITCOLOR32(red, green, blue, pixel);
283         pixel = COLOR24BGR(red, green, blue);
284         return pixel;
285     }
286 
287     if ((in_bpp == 15) && (out_bpp == 16))
288     {
289         pixel = in_color;
290         SPLITCOLOR15(red, green, blue, pixel);
291         pixel = COLOR16(red, green, blue);
292         return pixel;
293     }
294 
295     if ((in_bpp == 15) && (out_bpp == 24))
296     {
297         pixel = in_color;
298         SPLITCOLOR15(red, green, blue, pixel);
299         pixel = COLOR24BGR(red, green, blue);
300         return pixel;
301     }
302 
303     if ((in_bpp == 15) && (out_bpp == 15))
304     {
305         return in_color;
306     }
307 
308     if ((in_bpp == 16) && (out_bpp == 16))
309     {
310         return in_color;
311     }
312 
313     if ((in_bpp == 16) && (out_bpp == 24))
314     {
315         pixel = in_color;
316         SPLITCOLOR16(red, green, blue, pixel);
317         pixel = COLOR24BGR(red, green, blue);
318         return pixel;
319     }
320 
321     if ((in_bpp == 16) && (out_bpp == 32))
322     {
323         pixel = in_color;
324         SPLITCOLOR16(red, green, blue, pixel);
325         pixel = COLOR24BGR(red, green, blue);
326         return pixel;
327     }
328 
329     if ((in_bpp == 24) && (out_bpp == 24))
330     {
331         return in_color;
332     }
333 
334     if ((in_bpp == 32) && (out_bpp == 24))
335     {
336         return in_color;
337     }
338 
339     if ((in_bpp == 32) && (out_bpp == 32))
340     {
341         return in_color;
342     }
343 
344     LOG(LOG_LEVEL_WARNING, "convert_color: error unknown conversion from %d to %d",
345         in_bpp, out_bpp);
346     return 0;
347 }
348