Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-gdi / graphics.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * Graphical Objects
4  *
5  * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
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 #include <freerdp/gdi/dc.h>
21 #include <freerdp/gdi/brush.h>
22 #include <freerdp/gdi/shape.h>
23 #include <freerdp/gdi/region.h>
24 #include <freerdp/gdi/bitmap.h>
25 #include <freerdp/gdi/drawing.h>
26 #include <freerdp/gdi/clipping.h>
27 #include <freerdp/codec/color.h>
28 #include <freerdp/codec/bitmap.h>
29 #include <freerdp/utils/memory.h>
30 #include <freerdp/codec/bitmap.h>
31 #include <freerdp/cache/glyph.h>
32
33 #include "graphics.h"
34
35 /* Bitmap Class */
36
37 HGDI_BITMAP gdi_create_bitmap(rdpGdi* gdi, int width, int height, int bpp, uint8* data)
38 {
39         uint8* bmpData;
40         HGDI_BITMAP bitmap;
41
42         bmpData = freerdp_image_convert(data, NULL, width, height, gdi->srcBpp, bpp, gdi->clrconv);
43         bitmap = gdi_CreateBitmap(width, height, gdi->dstBpp, bmpData);
44
45         return bitmap;
46 }
47
48 void gdi_Bitmap_New(rdpContext* context, rdpBitmap* bitmap)
49 {
50         gdiBitmap* gdi_bitmap;
51         rdpGdi* gdi = context->gdi;
52
53         gdi_bitmap = (gdiBitmap*) bitmap;
54         gdi_bitmap->hdc = gdi_CreateCompatibleDC(gdi->hdc);
55
56         if (bitmap->data == NULL)
57                 gdi_bitmap->bitmap = gdi_CreateCompatibleBitmap(gdi->hdc, bitmap->width, bitmap->height);
58         else
59                 gdi_bitmap->bitmap = gdi_create_bitmap(gdi, bitmap->width, bitmap->height, gdi->dstBpp, bitmap->data);
60
61         gdi_SelectObject(gdi_bitmap->hdc, (HGDIOBJECT) gdi_bitmap->bitmap);
62         gdi_bitmap->org_bitmap = NULL;
63 }
64
65 void gdi_Bitmap_Free(rdpContext* context, rdpBitmap* bitmap)
66 {
67         gdiBitmap* gdi_bitmap = (gdiBitmap*) bitmap;
68
69         if (gdi_bitmap != NULL)
70         {
71                 gdi_SelectObject(gdi_bitmap->hdc, (HGDIOBJECT) gdi_bitmap->org_bitmap);
72                 gdi_DeleteObject((HGDIOBJECT) gdi_bitmap->bitmap);
73                 gdi_DeleteDC(gdi_bitmap->hdc);
74         }
75 }
76
77 void gdi_Bitmap_Paint(rdpContext* context, rdpBitmap* bitmap)
78 {
79         int width, height;
80         gdiBitmap* gdi_bitmap = (gdiBitmap*) bitmap;
81
82         width = bitmap->right - bitmap->left + 1;
83         height = bitmap->bottom - bitmap->top + 1;
84
85         gdi_BitBlt(context->gdi->primary->hdc, bitmap->left, bitmap->top,
86                         width, height, gdi_bitmap->hdc, 0, 0, GDI_SRCCOPY);
87 }
88
89 void gdi_Bitmap_Decompress(rdpContext* context, rdpBitmap* bitmap,
90                 uint8* data, int width, int height, int bpp, int length, boolean compressed)
91 {
92         uint16 size;
93
94         size = width * height * (bpp + 7) / 8;
95
96         if (bitmap->data == NULL)
97                 bitmap->data = (uint8*) xmalloc(size);
98         else
99                 bitmap->data = (uint8*) xrealloc(bitmap->data, size);
100
101         if (compressed)
102         {
103                 boolean status;
104
105                 status = bitmap_decompress(data, bitmap->data, width, height, length, bpp, bpp);
106
107                 if (status != true)
108                 {
109                         printf("Bitmap Decompression Failed\n");
110                 }
111         }
112         else
113         {
114                 freerdp_image_flip(data, bitmap->data, width, height, bpp);
115
116         }
117
118         bitmap->width = width;
119         bitmap->height = height;
120         bitmap->compressed = false;
121         bitmap->length = size;
122         bitmap->bpp = bpp;
123 }
124
125 void gdi_Bitmap_SetSurface(rdpContext* context, rdpBitmap* bitmap, boolean primary)
126 {
127         rdpGdi* gdi = context->gdi;
128
129         if (primary)
130                 gdi->drawing = gdi->primary;
131         else
132                 gdi->drawing = (gdiBitmap*) bitmap;
133 }
134
135 /* Glyph Class */
136
137 void gdi_Glyph_New(rdpContext* context, rdpGlyph* glyph)
138 {
139         uint8* data;
140         gdiGlyph* gdi_glyph;
141
142         gdi_glyph = (gdiGlyph*) glyph;
143
144         gdi_glyph->hdc = gdi_GetDC();
145         gdi_glyph->hdc->bytesPerPixel = 1;
146         gdi_glyph->hdc->bitsPerPixel = 1;
147
148         data = freerdp_glyph_convert(glyph->cx, glyph->cy, glyph->aj);
149         gdi_glyph->bitmap = gdi_CreateBitmap(glyph->cx, glyph->cy, 1, data);
150         gdi_glyph->bitmap->bytesPerPixel = 1;
151         gdi_glyph->bitmap->bitsPerPixel = 1;
152
153         gdi_SelectObject(gdi_glyph->hdc, (HGDIOBJECT) gdi_glyph->bitmap);
154         gdi_glyph->org_bitmap = NULL;
155 }
156
157 void gdi_Glyph_Free(rdpContext* context, rdpGlyph* glyph)
158 {
159         gdiGlyph* gdi_glyph;
160
161         gdi_glyph = (gdiGlyph*) glyph;
162
163         if (gdi_glyph != 0)
164         {
165                 gdi_SelectObject(gdi_glyph->hdc, (HGDIOBJECT) gdi_glyph->org_bitmap);
166                 gdi_DeleteObject((HGDIOBJECT) gdi_glyph->bitmap);
167                 gdi_DeleteDC(gdi_glyph->hdc);
168         }
169 }
170
171 void gdi_Glyph_Draw(rdpContext* context, rdpGlyph* glyph, int x, int y)
172 {
173         gdiGlyph* gdi_glyph;
174         rdpGdi* gdi = context->gdi;
175
176         gdi_glyph = (gdiGlyph*) glyph;
177
178         gdi_BitBlt(gdi->drawing->hdc, x, y, gdi_glyph->bitmap->width,
179                         gdi_glyph->bitmap->height, gdi_glyph->hdc, 0, 0, GDI_DSPDxax);
180 }
181
182 void gdi_Glyph_BeginDraw(rdpContext* context, int x, int y, int width, int height, uint32 bgcolor, uint32 fgcolor)
183 {
184         GDI_RECT rect;
185         HGDI_BRUSH brush;
186         rdpGdi* gdi = context->gdi;
187
188         bgcolor = freerdp_color_convert_var_bgr(bgcolor, gdi->srcBpp, 32, gdi->clrconv);
189         fgcolor = freerdp_color_convert_var_bgr(fgcolor, gdi->srcBpp, 32, gdi->clrconv);
190
191         gdi_CRgnToRect(x, y, width, height, &rect);
192
193         brush = gdi_CreateSolidBrush(fgcolor);
194
195         gdi_FillRect(gdi->drawing->hdc, &rect, brush);
196
197         gdi->textColor = gdi_SetTextColor(gdi->drawing->hdc, bgcolor);
198 }
199
200 void gdi_Glyph_EndDraw(rdpContext* context, int x, int y, int width, int height, uint32 bgcolor, uint32 fgcolor)
201 {
202         rdpGdi* gdi = context->gdi;
203
204         bgcolor = freerdp_color_convert_var_bgr(bgcolor, gdi->srcBpp, 32, gdi->clrconv);
205         gdi->textColor = gdi_SetTextColor(gdi->drawing->hdc, bgcolor);
206 }
207
208 /* Graphics Module */
209
210 void gdi_register_graphics(rdpGraphics* graphics)
211 {
212         rdpBitmap* bitmap;
213         rdpGlyph* glyph;
214
215         bitmap = xnew(rdpBitmap);
216         bitmap->size = sizeof(gdiBitmap);
217
218         bitmap->New = gdi_Bitmap_New;
219         bitmap->Free = gdi_Bitmap_Free;
220         bitmap->Paint = gdi_Bitmap_Paint;
221         bitmap->Decompress = gdi_Bitmap_Decompress;
222         bitmap->SetSurface = gdi_Bitmap_SetSurface;
223
224         graphics_register_bitmap(graphics, bitmap);
225         xfree(bitmap);
226
227         glyph = xnew(rdpGlyph);
228         glyph->size = sizeof(gdiGlyph);
229
230         glyph->New = gdi_Glyph_New;
231         glyph->Free = gdi_Glyph_Free;
232         glyph->Draw = gdi_Glyph_Draw;
233         glyph->BeginDraw = gdi_Glyph_BeginDraw;
234         glyph->EndDraw = gdi_Glyph_EndDraw;
235
236         graphics_register_glyph(graphics, glyph);
237         xfree(glyph);
238 }
239