2 * FreeRDP: A Remote Desktop Protocol Implementation
3 * Windows Graphical Objects
5 * Copyright 2010-2011 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.
24 #include <winpr/crt.h>
26 #include <freerdp/codecs.h>
29 #include "wf_graphics.h"
31 HBITMAP wf_create_dib(wfContext* wfc, int width, int height, int bpp, BYTE* data, BYTE** pdata)
40 * See: http://msdn.microsoft.com/en-us/library/dd183376
41 * if biHeight is positive, the bitmap is bottom-up
42 * if biHeight is negative, the bitmap is top-down
43 * Since we get top-down bitmaps, let's keep it that way
46 negHeight = (height < 0) ? height : height * (-1);
49 bmi.bmiHeader.biSize = sizeof(BITMAPINFO);
50 bmi.bmiHeader.biWidth = width;
51 bmi.bmiHeader.biHeight = negHeight;
52 bmi.bmiHeader.biPlanes = 1;
53 bmi.bmiHeader.biBitCount = wfc->dstBpp;
54 bmi.bmiHeader.biCompression = BI_RGB;
55 bitmap = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void**) &cdata, NULL, 0);
58 freerdp_image_convert(data, cdata, width, height, bpp, wfc->dstBpp, wfc->clrconv);
69 wfBitmap* wf_image_new(wfContext* wfc, int width, int height, int bpp, BYTE* data)
75 image = (wfBitmap*) malloc(sizeof(wfBitmap));
76 image->hdc = CreateCompatibleDC(hdc);
78 image->bitmap = wf_create_dib(wfc, width, height, bpp, data, &(image->pdata));
80 image->org_bitmap = (HBITMAP) SelectObject(image->hdc, image->bitmap);
86 void wf_image_free(wfBitmap* image)
90 SelectObject(image->hdc, image->org_bitmap);
91 DeleteObject(image->bitmap);
99 void wf_Bitmap_New(wfContext* wfc, rdpBitmap* bitmap)
102 wfBitmap* wf_bitmap = (wfBitmap*) bitmap;
104 wf_bitmap = (wfBitmap*) bitmap;
107 wf_bitmap->hdc = CreateCompatibleDC(hdc);
110 wf_bitmap->bitmap = CreateCompatibleBitmap(hdc, bitmap->width, bitmap->height);
112 wf_bitmap->bitmap = wf_create_dib(wfc, bitmap->width, bitmap->height, bitmap->bpp, bitmap->data, NULL);
114 wf_bitmap->org_bitmap = (HBITMAP) SelectObject(wf_bitmap->hdc, wf_bitmap->bitmap);
115 ReleaseDC(NULL, hdc);
118 void wf_Bitmap_Free(wfContext* wfc, rdpBitmap* bitmap)
120 wfBitmap* wf_bitmap = (wfBitmap*) bitmap;
124 SelectObject(wf_bitmap->hdc, wf_bitmap->org_bitmap);
125 DeleteObject(wf_bitmap->bitmap);
126 DeleteDC(wf_bitmap->hdc);
130 void wf_Bitmap_Paint(wfContext* wfc, rdpBitmap* bitmap)
133 wfBitmap* wf_bitmap = (wfBitmap*) bitmap;
135 width = bitmap->right - bitmap->left + 1;
136 height = bitmap->bottom - bitmap->top + 1;
138 BitBlt(wfc->primary->hdc, bitmap->left, bitmap->top,
139 width, height, wf_bitmap->hdc, 0, 0, SRCCOPY);
141 wf_invalidate_region(wfc, bitmap->left, bitmap->top, width, height);
144 void wf_Bitmap_Decompress(wfContext* wfc, rdpBitmap* bitmap,
145 BYTE* data, int width, int height, int bpp, int length, BOOL compressed, int codecId)
150 size = width * height * (bpp / 8);
153 bitmap->data = (BYTE*) _aligned_malloc(size, 16);
155 bitmap->data = (BYTE*) _aligned_realloc(bitmap->data, size, 16);
162 SrcSize = (UINT32) length;
163 pDstData = bitmap->data;
167 freerdp_client_codecs_prepare(wfc->codecs, FREERDP_CODEC_INTERLEAVED);
169 status = interleaved_decompress(wfc->codecs->interleaved, data, SrcSize, bpp,
170 &pDstData, PIXEL_FORMAT_XRGB32_VF, width * 4, 0, 0, width, height);
174 DEBUG_WARN("wf_Bitmap_Decompress: Bitmap Decompression Failed\n");
179 freerdp_client_codecs_prepare(wfc->codecs, FREERDP_CODEC_PLANAR);
181 status = planar_decompress(wfc->codecs->planar, data, SrcSize, &pDstData,
182 PIXEL_FORMAT_XRGB32_VF, width * 4, 0, 0, width, height);
186 DEBUG_WARN("wf_Bitmap_Decompress: Bitmap Decompression Failed\n");
192 freerdp_image_flip(data, bitmap->data, width, height, bpp);
195 bitmap->compressed = FALSE;
196 bitmap->length = size;
200 void wf_Bitmap_SetSurface(wfContext* wfc, rdpBitmap* bitmap, BOOL primary)
203 wfc->drawing = wfc->primary;
205 wfc->drawing = (wfBitmap*) bitmap;
210 void wf_Pointer_New(wfContext* wfc, rdpPointer* pointer)
217 info.xHotspot = pointer->xPos;
218 info.yHotspot = pointer->yPos;
219 if (pointer->xorBpp == 1)
221 data = (BYTE*) malloc(pointer->lengthAndMask + pointer->lengthXorMask);
222 CopyMemory(data, pointer->andMaskData, pointer->lengthAndMask);
223 CopyMemory(data + pointer->lengthAndMask, pointer->xorMaskData, pointer->lengthXorMask);
224 info.hbmMask = CreateBitmap(pointer->width, pointer->height * 2, 1, 1, data);
226 info.hbmColor = NULL;
230 data = (BYTE*) malloc(pointer->lengthAndMask);
231 freerdp_bitmap_flip(pointer->andMaskData, data, (pointer->width + 7) / 8, pointer->height);
232 info.hbmMask = CreateBitmap(pointer->width, pointer->height, 1, 1, data);
234 data = (BYTE*) malloc(pointer->lengthXorMask);
235 freerdp_image_flip(pointer->xorMaskData, data, pointer->width, pointer->height, pointer->xorBpp);
236 info.hbmColor = CreateBitmap(pointer->width, pointer->height, 1, pointer->xorBpp, data);
239 hCur = CreateIconIndirect(&info);
240 ((wfPointer*) pointer)->cursor = hCur;
242 DeleteObject(info.hbmMask);
244 DeleteObject(info.hbmColor);
247 void wf_Pointer_Free(wfContext* wfc, rdpPointer* pointer)
251 hCur = ((wfPointer*) pointer)->cursor;
257 void wf_Pointer_Set(wfContext* wfc, rdpPointer* pointer)
261 hCur = ((wfPointer*) pointer)->cursor;
270 void wf_Pointer_SetNull(wfContext* wfc)
275 void wf_Pointer_SetDefault(wfContext* wfc)
280 /* Graphics Module */
282 void wf_register_graphics(rdpGraphics* graphics)
287 wfc = (wfContext*) graphics->context;
289 if (wfc->sw_gdi == FALSE)
293 ZeroMemory(&bitmap, sizeof(rdpBitmap));
294 bitmap.size = sizeof(wfBitmap);
295 bitmap.New = (pBitmap_New) wf_Bitmap_New;
296 bitmap.Free = (pBitmap_Free) wf_Bitmap_Free;
297 bitmap.Paint = (pBitmap_Paint) wf_Bitmap_Paint;
298 bitmap.Decompress = (pBitmap_Decompress) wf_Bitmap_Decompress;
299 bitmap.SetSurface = (pBitmap_SetSurface) wf_Bitmap_SetSurface;
301 graphics_register_bitmap(graphics, &bitmap);
304 ZeroMemory(&pointer, sizeof(rdpPointer));
305 pointer.size = sizeof(wfPointer);
306 pointer.New = (pPointer_New) wf_Pointer_New;
307 pointer.Free = (pPointer_Free) wf_Pointer_Free;
308 pointer.Set = (pPointer_Set) wf_Pointer_Set;
309 pointer.SetNull = (pPointer_SetNull) wf_Pointer_SetNull;
310 pointer.SetDefault = (pPointer_SetDefault) wf_Pointer_SetDefault;
312 graphics_register_pointer(graphics, &pointer);