Coverage Report

Created: 2025-07-01 06:46

/src/FreeRDP/libfreerdp/gdi/graphics.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Graphical Objects
4
 *
5
 * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2016 Armin Novak <armin.novak@thincast.com>
7
 * Copyright 2016 Thincast Technologies GmbH
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 *     http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21
22
#include <freerdp/config.h>
23
24
#include <winpr/crt.h>
25
26
#include <freerdp/log.h>
27
#include <freerdp/freerdp.h>
28
#include <freerdp/gdi/dc.h>
29
#include <freerdp/gdi/shape.h>
30
#include <freerdp/gdi/region.h>
31
#include <freerdp/gdi/bitmap.h>
32
33
#include "clipping.h"
34
#include "drawing.h"
35
#include "brush.h"
36
#include "graphics.h"
37
38
#define TAG FREERDP_TAG("gdi")
39
/* Bitmap Class */
40
41
HGDI_BITMAP gdi_create_bitmap(rdpGdi* gdi, UINT32 nWidth, UINT32 nHeight, UINT32 SrcFormat,
42
                              BYTE* data)
43
0
{
44
0
  UINT32 nSrcStep = 0;
45
0
  UINT32 nDstStep = 0;
46
0
  BYTE* pSrcData = NULL;
47
0
  BYTE* pDstData = NULL;
48
0
  HGDI_BITMAP bitmap = NULL;
49
50
0
  if (!gdi)
51
0
    return NULL;
52
53
0
  nDstStep = nWidth * FreeRDPGetBytesPerPixel(gdi->dstFormat);
54
0
  pDstData = winpr_aligned_malloc(1ull * nHeight * nDstStep, 16);
55
56
0
  if (!pDstData)
57
0
    return NULL;
58
59
0
  pSrcData = data;
60
0
  nSrcStep = nWidth * FreeRDPGetBytesPerPixel(SrcFormat);
61
62
0
  if (!freerdp_image_copy_no_overlap(pDstData, gdi->dstFormat, nDstStep, 0, 0, nWidth, nHeight,
63
0
                                     pSrcData, SrcFormat, nSrcStep, 0, 0, &gdi->palette,
64
0
                                     FREERDP_FLIP_NONE))
65
0
  {
66
0
    winpr_aligned_free(pDstData);
67
0
    return NULL;
68
0
  }
69
70
0
  bitmap = gdi_CreateBitmap(nWidth, nHeight, gdi->dstFormat, pDstData);
71
0
  return bitmap;
72
0
}
73
74
static BOOL gdi_Bitmap_New(rdpContext* context, rdpBitmap* bitmap)
75
0
{
76
0
  gdiBitmap* gdi_bitmap = NULL;
77
0
  rdpGdi* gdi = context->gdi;
78
0
  gdi_bitmap = (gdiBitmap*)bitmap;
79
0
  gdi_bitmap->hdc = gdi_CreateCompatibleDC(gdi->hdc);
80
81
0
  if (!gdi_bitmap->hdc)
82
0
    return FALSE;
83
84
0
  if (!bitmap->data)
85
0
    gdi_bitmap->bitmap = gdi_CreateCompatibleBitmap(gdi->hdc, bitmap->width, bitmap->height);
86
0
  else
87
0
  {
88
0
    UINT32 format = bitmap->format;
89
0
    gdi_bitmap->bitmap =
90
0
        gdi_create_bitmap(gdi, bitmap->width, bitmap->height, format, bitmap->data);
91
0
  }
92
93
0
  if (!gdi_bitmap->bitmap)
94
0
  {
95
0
    gdi_DeleteDC(gdi_bitmap->hdc);
96
0
    gdi_bitmap->hdc = NULL;
97
0
    return FALSE;
98
0
  }
99
100
0
  gdi_bitmap->hdc->format = gdi_bitmap->bitmap->format;
101
0
  gdi_SelectObject(gdi_bitmap->hdc, (HGDIOBJECT)gdi_bitmap->bitmap);
102
0
  gdi_bitmap->org_bitmap = NULL;
103
0
  return TRUE;
104
0
}
105
106
static void gdi_Bitmap_Free(WINPR_ATTR_UNUSED rdpContext* context, rdpBitmap* bitmap)
107
0
{
108
0
  gdiBitmap* gdi_bitmap = (gdiBitmap*)bitmap;
109
110
0
  if (gdi_bitmap)
111
0
  {
112
0
    if (gdi_bitmap->hdc)
113
0
      gdi_SelectObject(gdi_bitmap->hdc, (HGDIOBJECT)gdi_bitmap->org_bitmap);
114
115
0
    gdi_DeleteObject((HGDIOBJECT)gdi_bitmap->bitmap);
116
0
    gdi_DeleteDC(gdi_bitmap->hdc);
117
0
    winpr_aligned_free(bitmap->data);
118
0
  }
119
120
0
  free(bitmap);
121
0
}
122
123
static BOOL gdi_Bitmap_Paint(rdpContext* context, rdpBitmap* bitmap)
124
0
{
125
0
  gdiBitmap* gdi_bitmap = (gdiBitmap*)bitmap;
126
0
  UINT32 width = bitmap->right - bitmap->left + 1;
127
0
  UINT32 height = bitmap->bottom - bitmap->top + 1;
128
0
  return gdi_BitBlt(context->gdi->primary->hdc, WINPR_ASSERTING_INT_CAST(int, bitmap->left),
129
0
                    WINPR_ASSERTING_INT_CAST(int, bitmap->top),
130
0
                    WINPR_ASSERTING_INT_CAST(int, width), WINPR_ASSERTING_INT_CAST(int, height),
131
0
                    gdi_bitmap->hdc, 0, 0, GDI_SRCCOPY, &context->gdi->palette);
132
0
}
133
134
static BOOL gdi_Bitmap_Decompress(rdpContext* context, rdpBitmap* bitmap, const BYTE* pSrcData,
135
                                  UINT32 DstWidth, UINT32 DstHeight, UINT32 bpp, UINT32 length,
136
                                  BOOL compressed, UINT32 codecId)
137
0
{
138
0
  UINT32 SrcSize = length;
139
0
  rdpGdi* gdi = context->gdi;
140
0
  UINT32 size = DstWidth * DstHeight;
141
0
  bitmap->compressed = FALSE;
142
0
  bitmap->format = gdi->dstFormat;
143
144
0
  if ((FreeRDPGetBytesPerPixel(bitmap->format) == 0) || (DstWidth == 0) || (DstHeight == 0) ||
145
0
      (DstWidth > UINT32_MAX / DstHeight) ||
146
0
      (size > (UINT32_MAX / FreeRDPGetBytesPerPixel(bitmap->format))))
147
0
  {
148
0
    WLog_ERR(TAG, "invalid input data");
149
0
    return FALSE;
150
0
  }
151
152
0
  size *= FreeRDPGetBytesPerPixel(bitmap->format);
153
0
  bitmap->length = size;
154
0
  bitmap->data = (BYTE*)winpr_aligned_malloc(bitmap->length, 16);
155
156
0
  if (!bitmap->data)
157
0
    return FALSE;
158
159
0
  if (compressed)
160
0
  {
161
0
    if ((codecId == RDP_CODEC_ID_REMOTEFX) || (codecId == RDP_CODEC_ID_IMAGE_REMOTEFX))
162
0
    {
163
0
      REGION16 invalidRegion = { 0 };
164
0
      region16_init(&invalidRegion);
165
166
0
      const BOOL rc =
167
0
          rfx_process_message(context->codecs->rfx, pSrcData, SrcSize, bitmap->left,
168
0
                              bitmap->top, bitmap->data, bitmap->format, gdi->stride,
169
0
                              WINPR_ASSERTING_INT_CAST(UINT32, gdi->height), &invalidRegion);
170
0
      region16_uninit(&invalidRegion);
171
172
0
      if (!rc)
173
0
      {
174
0
        WLog_ERR(TAG, "rfx_process_message failed");
175
0
        return FALSE;
176
0
      }
177
0
    }
178
0
    else if (codecId == RDP_CODEC_ID_NSCODEC)
179
0
    {
180
0
      const int status = nsc_process_message(
181
0
          context->codecs->nsc, 32, DstWidth, DstHeight, pSrcData, SrcSize, bitmap->data,
182
0
          bitmap->format, 0, 0, 0, DstWidth, DstHeight, FREERDP_FLIP_VERTICAL);
183
184
0
      if (status < 1)
185
0
      {
186
0
        WLog_ERR(TAG, "nsc_process_message failed");
187
0
        return FALSE;
188
0
      }
189
190
0
      return freerdp_image_copy_no_overlap(bitmap->data, bitmap->format, 0, 0, 0, DstWidth,
191
0
                                           DstHeight, pSrcData, PIXEL_FORMAT_XRGB32, 0, 0, 0,
192
0
                                           &gdi->palette, FREERDP_FLIP_VERTICAL);
193
0
    }
194
0
    else if (bpp < 32)
195
0
    {
196
0
      if (!interleaved_decompress(context->codecs->interleaved, pSrcData, SrcSize, DstWidth,
197
0
                                  DstHeight, bpp, bitmap->data, bitmap->format, 0, 0, 0,
198
0
                                  DstWidth, DstHeight, &gdi->palette))
199
0
      {
200
0
        WLog_ERR(TAG, "interleaved_decompress failed");
201
0
        return FALSE;
202
0
      }
203
0
    }
204
0
    else
205
0
    {
206
0
      const BOOL fidelity =
207
0
          freerdp_settings_get_bool(context->settings, FreeRDP_DrawAllowDynamicColorFidelity);
208
0
      freerdp_planar_switch_bgr(context->codecs->planar, fidelity);
209
0
      if (!planar_decompress(context->codecs->planar, pSrcData, SrcSize, DstWidth, DstHeight,
210
0
                             bitmap->data, bitmap->format, 0, 0, 0, DstWidth, DstHeight,
211
0
                             TRUE))
212
0
      {
213
0
        WLog_ERR(TAG, "planar_decompress failed");
214
0
        return FALSE;
215
0
      }
216
0
    }
217
0
  }
218
0
  else
219
0
  {
220
0
    const UINT32 SrcFormat = gdi_get_pixel_format(bpp);
221
0
    const size_t sbpp = FreeRDPGetBytesPerPixel(SrcFormat);
222
0
    const size_t dbpp = FreeRDPGetBytesPerPixel(bitmap->format);
223
224
0
    if ((sbpp == 0) || (dbpp == 0))
225
0
      return FALSE;
226
0
    else
227
0
    {
228
0
      const size_t dstSize = SrcSize * dbpp / sbpp;
229
230
0
      if (dstSize < bitmap->length)
231
0
      {
232
0
        WLog_ERR(TAG, "dstSize %" PRIuz " < bitmap->length %" PRIu32, dstSize,
233
0
                 bitmap->length);
234
0
        return FALSE;
235
0
      }
236
0
    }
237
238
0
    if (!freerdp_image_copy_no_overlap(bitmap->data, bitmap->format, 0, 0, 0, DstWidth,
239
0
                                       DstHeight, pSrcData, SrcFormat, 0, 0, 0, &gdi->palette,
240
0
                                       FREERDP_FLIP_VERTICAL))
241
0
    {
242
0
      WLog_ERR(TAG, "freerdp_image_copy failed");
243
0
      return FALSE;
244
0
    }
245
0
  }
246
247
0
  return TRUE;
248
0
}
249
250
static BOOL gdi_Bitmap_SetSurface(rdpContext* context, rdpBitmap* bitmap, BOOL primary)
251
0
{
252
0
  rdpGdi* gdi = NULL;
253
254
0
  if (!context)
255
0
    return FALSE;
256
257
0
  gdi = context->gdi;
258
259
0
  if (!gdi)
260
0
    return FALSE;
261
262
0
  if (primary)
263
0
    gdi->drawing = gdi->primary;
264
0
  else
265
0
    gdi->drawing = (gdiBitmap*)bitmap;
266
267
0
  return TRUE;
268
0
}
269
270
/* Glyph Class */
271
static BOOL gdi_Glyph_New(rdpContext* context, rdpGlyph* glyph)
272
0
{
273
0
  BYTE* data = NULL;
274
0
  gdiGlyph* gdi_glyph = NULL;
275
276
0
  if (!context || !glyph)
277
0
    return FALSE;
278
279
0
  gdi_glyph = (gdiGlyph*)glyph;
280
0
  gdi_glyph->hdc = gdi_GetDC();
281
282
0
  if (!gdi_glyph->hdc)
283
0
    return FALSE;
284
285
0
  gdi_glyph->hdc->format = PIXEL_FORMAT_MONO;
286
0
  data = freerdp_glyph_convert(glyph->cx, glyph->cy, glyph->aj);
287
288
0
  if (!data)
289
0
  {
290
0
    gdi_DeleteDC(gdi_glyph->hdc);
291
0
    return FALSE;
292
0
  }
293
294
0
  gdi_glyph->bitmap = gdi_CreateBitmap(glyph->cx, glyph->cy, PIXEL_FORMAT_MONO, data);
295
296
0
  if (!gdi_glyph->bitmap)
297
0
  {
298
0
    gdi_DeleteDC(gdi_glyph->hdc);
299
0
    winpr_aligned_free(data);
300
0
    return FALSE;
301
0
  }
302
303
0
  gdi_SelectObject(gdi_glyph->hdc, (HGDIOBJECT)gdi_glyph->bitmap);
304
0
  gdi_glyph->org_bitmap = NULL;
305
0
  return TRUE;
306
0
}
307
308
static void gdi_Glyph_Free(WINPR_ATTR_UNUSED rdpContext* context, rdpGlyph* glyph)
309
0
{
310
0
  gdiGlyph* gdi_glyph = NULL;
311
0
  gdi_glyph = (gdiGlyph*)glyph;
312
313
0
  if (gdi_glyph)
314
0
  {
315
0
    gdi_SelectObject(gdi_glyph->hdc, (HGDIOBJECT)gdi_glyph->org_bitmap);
316
0
    gdi_DeleteObject((HGDIOBJECT)gdi_glyph->bitmap);
317
0
    gdi_DeleteDC(gdi_glyph->hdc);
318
0
    free(glyph->aj);
319
0
    free(glyph);
320
0
  }
321
0
}
322
323
static BOOL gdi_Glyph_Draw(rdpContext* context, const rdpGlyph* glyph, INT32 x, INT32 y, INT32 w,
324
                           INT32 h, INT32 sx, INT32 sy, BOOL fOpRedundant)
325
0
{
326
0
  const gdiGlyph* gdi_glyph = NULL;
327
0
  rdpGdi* gdi = NULL;
328
0
  HGDI_BRUSH brush = NULL;
329
0
  BOOL rc = FALSE;
330
331
0
  if (!context || !glyph)
332
0
    return FALSE;
333
334
0
  gdi = context->gdi;
335
0
  gdi_glyph = (const gdiGlyph*)glyph;
336
337
0
  if (!fOpRedundant)
338
0
  {
339
0
    GDI_RECT rect = { 0 };
340
341
0
    if (x > 0)
342
0
      rect.left = x;
343
344
0
    if (y > 0)
345
0
      rect.top = y;
346
347
0
    if (x + w > 0)
348
0
      rect.right = x + w - 1;
349
350
0
    if (y + h > 0)
351
0
      rect.bottom = y + h - 1;
352
353
0
    if ((rect.left < rect.right) && (rect.top < rect.bottom))
354
0
    {
355
0
      brush = gdi_CreateSolidBrush(gdi->drawing->hdc->bkColor);
356
357
0
      if (!brush)
358
0
        return FALSE;
359
360
0
      gdi_FillRect(gdi->drawing->hdc, &rect, brush);
361
0
      gdi_DeleteObject((HGDIOBJECT)brush);
362
0
    }
363
0
  }
364
365
0
  brush = gdi_CreateSolidBrush(gdi->drawing->hdc->textColor);
366
367
0
  if (!brush)
368
0
    return FALSE;
369
370
0
  gdi_SelectObject(gdi->drawing->hdc, (HGDIOBJECT)brush);
371
0
  rc = gdi_BitBlt(gdi->drawing->hdc, x, y, w, h, gdi_glyph->hdc, sx, sy, GDI_GLYPH_ORDER,
372
0
                  &context->gdi->palette);
373
0
  gdi_DeleteObject((HGDIOBJECT)brush);
374
0
  return rc;
375
0
}
376
377
static BOOL gdi_Glyph_BeginDraw(rdpContext* context, INT32 x, INT32 y, INT32 width, INT32 height,
378
                                UINT32 bgcolor, UINT32 fgcolor, BOOL fOpRedundant)
379
0
{
380
0
  rdpGdi* gdi = NULL;
381
382
0
  if (!context || !context->gdi)
383
0
    return FALSE;
384
385
0
  gdi = context->gdi;
386
387
0
  if (!gdi->drawing || !gdi->drawing->hdc)
388
0
    return FALSE;
389
390
0
  if (!fOpRedundant)
391
0
  {
392
0
    if (!gdi_decode_color(gdi, bgcolor, &bgcolor, NULL))
393
0
      return FALSE;
394
395
0
    if (!gdi_decode_color(gdi, fgcolor, &fgcolor, NULL))
396
0
      return FALSE;
397
398
0
    gdi_SetClipRgn(gdi->drawing->hdc, x, y, width, height);
399
0
    gdi_SetTextColor(gdi->drawing->hdc, bgcolor);
400
0
    gdi_SetBkColor(gdi->drawing->hdc, fgcolor);
401
402
0
    if (1)
403
0
    {
404
0
      GDI_RECT rect = { 0 };
405
0
      HGDI_BRUSH brush = gdi_CreateSolidBrush(fgcolor);
406
407
0
      if (!brush)
408
0
        return FALSE;
409
410
0
      if (x > 0)
411
0
        rect.left = x;
412
413
0
      if (y > 0)
414
0
        rect.top = y;
415
416
0
      rect.right = x + width - 1;
417
0
      rect.bottom = y + height - 1;
418
419
0
      if ((x + width > rect.left) && (y + height > rect.top))
420
0
        gdi_FillRect(gdi->drawing->hdc, &rect, brush);
421
422
0
      gdi_DeleteObject((HGDIOBJECT)brush);
423
0
    }
424
425
0
    return gdi_SetNullClipRgn(gdi->drawing->hdc);
426
0
  }
427
428
0
  return TRUE;
429
0
}
430
431
static BOOL gdi_Glyph_EndDraw(rdpContext* context, WINPR_ATTR_UNUSED INT32 x,
432
                              WINPR_ATTR_UNUSED INT32 y, WINPR_ATTR_UNUSED INT32 width,
433
                              WINPR_ATTR_UNUSED INT32 height, WINPR_ATTR_UNUSED UINT32 bgcolor,
434
                              WINPR_ATTR_UNUSED UINT32 fgcolor)
435
0
{
436
0
  rdpGdi* gdi = NULL;
437
438
0
  if (!context || !context->gdi)
439
0
    return FALSE;
440
441
0
  gdi = context->gdi;
442
443
0
  if (!gdi->drawing || !gdi->drawing->hdc)
444
0
    return FALSE;
445
446
0
  gdi_SetNullClipRgn(gdi->drawing->hdc);
447
0
  return TRUE;
448
0
}
449
450
/* Graphics Module */
451
BOOL gdi_register_graphics(rdpGraphics* graphics)
452
0
{
453
0
  rdpBitmap bitmap = { 0 };
454
0
  rdpGlyph glyph = { 0 };
455
0
  bitmap.size = sizeof(gdiBitmap);
456
0
  bitmap.New = gdi_Bitmap_New;
457
0
  bitmap.Free = gdi_Bitmap_Free;
458
0
  bitmap.Paint = gdi_Bitmap_Paint;
459
0
  bitmap.Decompress = gdi_Bitmap_Decompress;
460
0
  bitmap.SetSurface = gdi_Bitmap_SetSurface;
461
0
  graphics_register_bitmap(graphics, &bitmap);
462
0
  glyph.size = sizeof(gdiGlyph);
463
0
  glyph.New = gdi_Glyph_New;
464
0
  glyph.Free = gdi_Glyph_Free;
465
0
  glyph.Draw = gdi_Glyph_Draw;
466
0
  glyph.BeginDraw = gdi_Glyph_BeginDraw;
467
0
  glyph.EndDraw = gdi_Glyph_EndDraw;
468
0
  graphics_register_glyph(graphics, &glyph);
469
0
  return TRUE;
470
0
}