FreeRDP/client/Windows/wf_graphics.c

323 lines
7.9 KiB
C
Raw Normal View History

2011-10-22 00:45:35 +04:00
/**
2012-10-09 07:02:04 +04:00
* FreeRDP: A Remote Desktop Protocol Implementation
2011-10-22 00:45:35 +04:00
* Windows Graphical Objects
*
* Copyright 2010-2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
2013-06-16 06:57:21 +04:00
#include <winpr/crt.h>
2014-09-10 19:38:38 +04:00
#include <freerdp/codecs.h>
2014-09-12 19:13:01 +04:00
#include <freerdp/log.h>
2011-10-22 00:45:35 +04:00
#include "wf_gdi.h"
#include "wf_graphics.h"
2014-09-12 19:13:01 +04:00
#define TAG CLIENT_TAG("windows")
2013-06-16 06:57:21 +04:00
HBITMAP wf_create_dib(wfContext* wfc, int width, int height, int bpp, BYTE* data, BYTE** pdata)
2011-10-22 00:45:35 +04:00
{
HDC hdc;
int negHeight;
HBITMAP bitmap;
BITMAPINFO bmi;
BYTE* cdata = NULL;
2011-10-22 00:45:35 +04:00
/**
* See: http://msdn.microsoft.com/en-us/library/dd183376
* if biHeight is positive, the bitmap is bottom-up
* if biHeight is negative, the bitmap is top-down
* Since we get top-down bitmaps, let's keep it that way
*/
negHeight = (height < 0) ? height : height * (-1);
hdc = GetDC(NULL);
bmi.bmiHeader.biSize = sizeof(BITMAPINFO);
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = negHeight;
bmi.bmiHeader.biPlanes = 1;
2013-06-16 06:57:21 +04:00
bmi.bmiHeader.biBitCount = wfc->dstBpp;
2011-10-22 00:45:35 +04:00
bmi.bmiHeader.biCompression = BI_RGB;
bitmap = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void**) &cdata, NULL, 0);
if (data != NULL)
2013-06-16 06:57:21 +04:00
freerdp_image_convert(data, cdata, width, height, bpp, wfc->dstBpp, wfc->clrconv);
2011-10-22 00:45:35 +04:00
2011-12-16 23:43:14 +04:00
if (pdata != NULL)
*pdata = cdata;
2011-10-22 00:45:35 +04:00
ReleaseDC(NULL, hdc);
GdiFlush();
return bitmap;
}
2013-06-16 06:57:21 +04:00
wfBitmap* wf_image_new(wfContext* wfc, int width, int height, int bpp, BYTE* data)
2011-10-22 00:45:35 +04:00
{
HDC hdc;
wfBitmap* image;
hdc = GetDC(NULL);
image = (wfBitmap*) malloc(sizeof(wfBitmap));
2011-10-22 00:45:35 +04:00
image->hdc = CreateCompatibleDC(hdc);
2013-06-16 06:57:21 +04:00
image->bitmap = wf_create_dib(wfc, width, height, bpp, data, &(image->pdata));
2011-10-22 00:45:35 +04:00
image->org_bitmap = (HBITMAP) SelectObject(image->hdc, image->bitmap);
ReleaseDC(NULL, hdc);
2011-10-22 00:45:35 +04:00
return image;
}
void wf_image_free(wfBitmap* image)
{
if (image != 0)
{
SelectObject(image->hdc, image->org_bitmap);
DeleteObject(image->bitmap);
DeleteDC(image->hdc);
free(image);
}
}
/* Bitmap Class */
2013-06-16 06:57:21 +04:00
void wf_Bitmap_New(wfContext* wfc, rdpBitmap* bitmap)
2011-10-22 00:45:35 +04:00
{
HDC hdc;
wfBitmap* wf_bitmap = (wfBitmap*) bitmap;
2011-10-26 23:38:50 +04:00
wf_bitmap = (wfBitmap*) bitmap;
2011-10-22 00:45:35 +04:00
hdc = GetDC(NULL);
wf_bitmap->hdc = CreateCompatibleDC(hdc);
2013-06-16 06:57:21 +04:00
if (!bitmap->data)
2011-10-22 00:45:35 +04:00
wf_bitmap->bitmap = CreateCompatibleBitmap(hdc, bitmap->width, bitmap->height);
else
2013-06-16 06:57:21 +04:00
wf_bitmap->bitmap = wf_create_dib(wfc, bitmap->width, bitmap->height, bitmap->bpp, bitmap->data, NULL);
2011-10-22 00:45:35 +04:00
wf_bitmap->org_bitmap = (HBITMAP) SelectObject(wf_bitmap->hdc, wf_bitmap->bitmap);
ReleaseDC(NULL, hdc);
}
2013-06-16 06:57:21 +04:00
void wf_Bitmap_Free(wfContext* wfc, rdpBitmap* bitmap)
2011-10-22 00:45:35 +04:00
{
wfBitmap* wf_bitmap = (wfBitmap*) bitmap;
2011-10-22 00:45:35 +04:00
if (wf_bitmap != 0)
{
SelectObject(wf_bitmap->hdc, wf_bitmap->org_bitmap);
DeleteObject(wf_bitmap->bitmap);
DeleteDC(wf_bitmap->hdc);
}
}
2013-06-16 06:57:21 +04:00
void wf_Bitmap_Paint(wfContext* wfc, rdpBitmap* bitmap)
2011-10-22 00:45:35 +04:00
{
int width, height;
wfBitmap* wf_bitmap = (wfBitmap*) bitmap;
width = bitmap->right - bitmap->left + 1;
height = bitmap->bottom - bitmap->top + 1;
2013-06-16 06:57:21 +04:00
BitBlt(wfc->primary->hdc, bitmap->left, bitmap->top,
2011-12-16 23:43:14 +04:00
width, height, wf_bitmap->hdc, 0, 0, SRCCOPY);
2011-10-22 00:45:35 +04:00
2013-06-16 06:57:21 +04:00
wf_invalidate_region(wfc, bitmap->left, bitmap->top, width, height);
2011-10-22 00:45:35 +04:00
}
2013-06-16 06:57:21 +04:00
void wf_Bitmap_Decompress(wfContext* wfc, rdpBitmap* bitmap,
BYTE* data, int width, int height, int bpp, int length, BOOL compressed, int codecId)
2011-10-22 00:45:35 +04:00
{
int status;
UINT16 size;
2014-09-12 19:34:30 +04:00
BYTE* pSrcData;
BYTE* pDstData;
UINT32 SrcSize;
UINT32 SrcFormat;
UINT32 bytesPerPixel;
2011-10-22 00:45:35 +04:00
2014-09-12 19:34:30 +04:00
bytesPerPixel = (bpp + 7) / 8;
size = width * height * 4;
2011-10-22 00:45:35 +04:00
if (!bitmap->data)
bitmap->data = (BYTE*) _aligned_malloc(size, 16);
2011-10-22 00:45:35 +04:00
else
bitmap->data = (BYTE*) _aligned_realloc(bitmap->data, size, 16);
2011-10-22 00:45:35 +04:00
2014-09-12 19:34:30 +04:00
pSrcData = data;
SrcSize = (UINT32) length;
pDstData = bitmap->data;
2011-10-26 23:38:50 +04:00
if (compressed)
2011-10-22 00:45:35 +04:00
{
if (bpp < 32)
2011-10-22 00:45:35 +04:00
{
freerdp_client_codecs_prepare(wfc->codecs, FREERDP_CODEC_INTERLEAVED);
2014-09-12 19:34:30 +04:00
status = interleaved_decompress(wfc->codecs->interleaved, pSrcData, SrcSize, bpp,
2014-09-18 02:30:09 +04:00
&pDstData, PIXEL_FORMAT_XRGB32, width * 4, 0, 0, width, height, NULL);
}
else
{
freerdp_client_codecs_prepare(wfc->codecs, FREERDP_CODEC_PLANAR);
2014-09-12 19:34:30 +04:00
status = planar_decompress(wfc->codecs->planar, pSrcData, SrcSize, &pDstData,
PIXEL_FORMAT_XRGB32, width * 4, 0, 0, width, height, TRUE);
2014-09-12 19:34:30 +04:00
}
2014-09-12 19:34:30 +04:00
if (status < 0)
{
2014-09-12 19:13:01 +04:00
WLog_ERR(TAG, "Bitmap Decompression Failed");
2014-09-12 19:34:30 +04:00
return;
2011-10-22 00:45:35 +04:00
}
}
else
{
2014-09-12 19:34:30 +04:00
SrcFormat = gdi_get_pixel_format(bpp, TRUE);
status = freerdp_image_copy(pDstData, PIXEL_FORMAT_XRGB32, width * 4, 0, 0,
2014-09-18 02:30:09 +04:00
width, height, pSrcData, SrcFormat, width * bytesPerPixel, 0, 0, NULL);
2011-10-22 00:45:35 +04:00
}
bitmap->compressed = FALSE;
2011-10-22 00:45:35 +04:00
bitmap->length = size;
2014-09-12 19:34:30 +04:00
bitmap->bpp = 32;
2011-10-22 00:45:35 +04:00
}
2013-06-16 06:57:21 +04:00
void wf_Bitmap_SetSurface(wfContext* wfc, rdpBitmap* bitmap, BOOL primary)
2011-10-22 00:45:35 +04:00
{
if (primary)
2013-06-16 06:57:21 +04:00
wfc->drawing = wfc->primary;
2011-10-22 00:45:35 +04:00
else
2013-06-16 06:57:21 +04:00
wfc->drawing = (wfBitmap*) bitmap;
2011-10-22 00:45:35 +04:00
}
/* Pointer Class */
2013-06-16 06:57:21 +04:00
void wf_Pointer_New(wfContext* wfc, rdpPointer* pointer)
2011-10-22 00:45:35 +04:00
{
HCURSOR hCur;
2012-12-21 16:29:47 +04:00
ICONINFO info;
BYTE *data;
2012-12-21 16:29:47 +04:00
info.fIcon = FALSE;
info.xHotspot = pointer->xPos;
info.yHotspot = pointer->yPos;
if (pointer->xorBpp == 1)
{
2012-12-21 16:29:47 +04:00
data = (BYTE*) malloc(pointer->lengthAndMask + pointer->lengthXorMask);
CopyMemory(data, pointer->andMaskData, pointer->lengthAndMask);
CopyMemory(data + pointer->lengthAndMask, pointer->xorMaskData, pointer->lengthXorMask);
info.hbmMask = CreateBitmap(pointer->width, pointer->height * 2, 1, 1, data);
free(data);
info.hbmColor = NULL;
}
2012-12-21 16:29:47 +04:00
else
{
2012-12-21 16:29:47 +04:00
data = (BYTE*) malloc(pointer->lengthAndMask);
freerdp_bitmap_flip(pointer->andMaskData, data, (pointer->width + 7) / 8, pointer->height);
info.hbmMask = CreateBitmap(pointer->width, pointer->height, 1, 1, data);
free(data);
data = (BYTE*) malloc(pointer->lengthXorMask);
freerdp_image_flip(pointer->xorMaskData, data, pointer->width, pointer->height, pointer->xorBpp);
info.hbmColor = CreateBitmap(pointer->width, pointer->height, 1, pointer->xorBpp, data);
free(data);
}
2012-12-21 16:29:47 +04:00
hCur = CreateIconIndirect(&info);
((wfPointer*) pointer)->cursor = hCur;
2012-12-21 16:29:47 +04:00
if (info.hbmMask)
DeleteObject(info.hbmMask);
if (info.hbmColor)
DeleteObject(info.hbmColor);
2011-10-22 00:45:35 +04:00
}
2013-06-16 06:57:21 +04:00
void wf_Pointer_Free(wfContext* wfc, rdpPointer* pointer)
2011-10-22 00:45:35 +04:00
{
HCURSOR hCur;
2011-10-22 00:45:35 +04:00
hCur = ((wfPointer*) pointer)->cursor;
2013-06-16 06:57:21 +04:00
if (hCur != 0)
2012-12-21 16:29:47 +04:00
DestroyIcon(hCur);
2011-10-22 00:45:35 +04:00
}
2013-06-16 06:57:21 +04:00
void wf_Pointer_Set(wfContext* wfc, rdpPointer* pointer)
2011-10-22 00:45:35 +04:00
{
HCURSOR hCur;
2011-10-22 00:45:35 +04:00
hCur = ((wfPointer*) pointer)->cursor;
2013-06-16 06:57:21 +04:00
if (hCur != NULL)
{
SetCursor(hCur);
2013-06-16 06:57:21 +04:00
wfc->cursor = hCur;
}
2011-10-22 00:45:35 +04:00
}
2013-06-16 06:57:21 +04:00
void wf_Pointer_SetNull(wfContext* wfc)
{
}
2013-06-16 06:57:21 +04:00
void wf_Pointer_SetDefault(wfContext* wfc)
{
}
2014-09-12 19:34:30 +04:00
void wf_register_pointer(rdpGraphics* graphics)
2011-10-22 00:45:35 +04:00
{
wfContext* wfc;
2011-10-22 00:45:35 +04:00
rdpPointer pointer;
wfc = (wfContext*) graphics->context;
2013-06-16 06:57:21 +04:00
ZeroMemory(&pointer, sizeof(rdpPointer));
2011-10-22 00:45:35 +04:00
pointer.size = sizeof(wfPointer);
2013-06-16 06:57:21 +04:00
pointer.New = (pPointer_New) wf_Pointer_New;
pointer.Free = (pPointer_Free) wf_Pointer_Free;
pointer.Set = (pPointer_Set) wf_Pointer_Set;
pointer.SetNull = (pPointer_SetNull) wf_Pointer_SetNull;
pointer.SetDefault = (pPointer_SetDefault) wf_Pointer_SetDefault;
2011-10-22 00:45:35 +04:00
graphics_register_pointer(graphics, &pointer);
}
2014-09-12 19:34:30 +04:00
/* Graphics Module */
void wf_register_graphics(rdpGraphics* graphics)
{
wfContext* wfc;
rdpBitmap bitmap;
wfc = (wfContext*) graphics->context;
ZeroMemory(&bitmap, sizeof(rdpBitmap));
bitmap.size = sizeof(wfBitmap);
bitmap.New = (pBitmap_New) wf_Bitmap_New;
bitmap.Free = (pBitmap_Free) wf_Bitmap_Free;
bitmap.Paint = (pBitmap_Paint) wf_Bitmap_Paint;
bitmap.Decompress = (pBitmap_Decompress) wf_Bitmap_Decompress;
bitmap.SetSurface = (pBitmap_SetSurface) wf_Bitmap_SetSurface;
graphics_register_bitmap(graphics, &bitmap);
}