Coverage Report

Created: 2026-04-12 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/gdi/graphics.c
Line
Count
Source
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 = nullptr;
47
0
  BYTE* pDstData = nullptr;
48
0
  HGDI_BITMAP bitmap = nullptr;
49
50
0
  if (!gdi)
51
0
    return nullptr;
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 nullptr;
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 nullptr;
68
0
  }
69
70
0
  bitmap = gdi_CreateBitmap(nWidth, nHeight, gdi->dstFormat, pDstData);
71
0
  if (!bitmap)
72
0
    winpr_aligned_free(pDstData);
73
0
  return bitmap;
74
0
}
75
76
static BOOL gdi_Bitmap_New(rdpContext* context, rdpBitmap* bitmap)
77
0
{
78
0
  gdiBitmap* gdi_bitmap = nullptr;
79
0
  rdpGdi* gdi = context->gdi;
80
0
  gdi_bitmap = (gdiBitmap*)bitmap;
81
0
  gdi_bitmap->hdc = gdi_CreateCompatibleDC(gdi->hdc);
82
83
0
  if (!gdi_bitmap->hdc)
84
0
    return FALSE;
85
86
0
  if (!bitmap->data)
87
0
    gdi_bitmap->bitmap = gdi_CreateCompatibleBitmap(gdi->hdc, bitmap->width, bitmap->height);
88
0
  else
89
0
  {
90
0
    UINT32 format = bitmap->format;
91
0
    gdi_bitmap->bitmap =
92
0
        gdi_create_bitmap(gdi, bitmap->width, bitmap->height, format, bitmap->data);
93
0
  }
94
95
0
  if (!gdi_bitmap->bitmap)
96
0
  {
97
0
    gdi_DeleteDC(gdi_bitmap->hdc);
98
0
    gdi_bitmap->hdc = nullptr;
99
0
    return FALSE;
100
0
  }
101
102
0
  gdi_bitmap->hdc->format = gdi_bitmap->bitmap->format;
103
0
  gdi_SelectObject(gdi_bitmap->hdc, (HGDIOBJECT)gdi_bitmap->bitmap);
104
0
  gdi_bitmap->org_bitmap = nullptr;
105
0
  return TRUE;
106
0
}
107
108
static void gdi_Bitmap_Free(WINPR_ATTR_UNUSED rdpContext* context, rdpBitmap* bitmap)
109
0
{
110
0
  gdiBitmap* gdi_bitmap = (gdiBitmap*)bitmap;
111
112
0
  if (gdi_bitmap)
113
0
  {
114
0
    if (gdi_bitmap->hdc)
115
0
      gdi_SelectObject(gdi_bitmap->hdc, (HGDIOBJECT)gdi_bitmap->org_bitmap);
116
117
0
    gdi_DeleteObject((HGDIOBJECT)gdi_bitmap->bitmap);
118
0
    gdi_DeleteDC(gdi_bitmap->hdc);
119
0
    winpr_aligned_free(bitmap->data);
120
0
  }
121
122
0
  free(bitmap);
123
0
}
124
125
static BOOL gdi_Bitmap_Paint(rdpContext* context, rdpBitmap* bitmap)
126
0
{
127
0
  gdiBitmap* gdi_bitmap = (gdiBitmap*)bitmap;
128
0
  UINT32 width = bitmap->right - bitmap->left + 1;
129
0
  UINT32 height = bitmap->bottom - bitmap->top + 1;
130
0
  return gdi_BitBlt(context->gdi->primary->hdc, WINPR_ASSERTING_INT_CAST(int, bitmap->left),
131
0
                    WINPR_ASSERTING_INT_CAST(int, bitmap->top),
132
0
                    WINPR_ASSERTING_INT_CAST(int, width), WINPR_ASSERTING_INT_CAST(int, height),
133
0
                    gdi_bitmap->hdc, 0, 0, GDI_SRCCOPY, &context->gdi->palette);
134
0
}
135
136
static BOOL gdi_Bitmap_Decompress(rdpContext* context, rdpBitmap* bitmap, const BYTE* pSrcData,
137
                                  UINT32 DstWidth, UINT32 DstHeight, UINT32 bpp, UINT32 length,
138
                                  BOOL compressed, UINT32 codecId)
139
0
{
140
0
  UINT32 SrcSize = length;
141
0
  rdpGdi* gdi = context->gdi;
142
0
  UINT32 size = DstWidth * DstHeight;
143
0
  bitmap->compressed = FALSE;
144
0
  bitmap->format = gdi->dstFormat;
145
146
0
  if ((FreeRDPGetBytesPerPixel(bitmap->format) == 0) || (DstWidth == 0) || (DstHeight == 0) ||
147
0
      (DstWidth > UINT32_MAX / DstHeight) ||
148
0
      (size > (UINT32_MAX / FreeRDPGetBytesPerPixel(bitmap->format))))
149
0
  {
150
0
    WLog_ERR(TAG, "invalid input data");
151
0
    return FALSE;
152
0
  }
153
154
0
  size *= FreeRDPGetBytesPerPixel(bitmap->format);
155
0
  bitmap->length = size;
156
0
  bitmap->data = (BYTE*)winpr_aligned_malloc(bitmap->length, 16);
157
158
0
  if (!bitmap->data)
159
0
    return FALSE;
160
161
0
  if (compressed)
162
0
  {
163
0
    if ((codecId == RDP_CODEC_ID_REMOTEFX) || (codecId == RDP_CODEC_ID_IMAGE_REMOTEFX))
164
0
    {
165
0
      REGION16 invalidRegion = WINPR_C_ARRAY_INIT;
166
0
      region16_init(&invalidRegion);
167
168
0
      const BOOL rc =
169
0
          rfx_process_message(context->codecs->rfx, pSrcData, SrcSize, bitmap->left,
170
0
                              bitmap->top, bitmap->data, bitmap->format, gdi->stride,
171
0
                              WINPR_ASSERTING_INT_CAST(UINT32, gdi->height), &invalidRegion);
172
0
      region16_uninit(&invalidRegion);
173
174
0
      if (!rc)
175
0
      {
176
0
        WLog_ERR(TAG, "rfx_process_message failed");
177
0
        return FALSE;
178
0
      }
179
0
    }
180
0
    else if (codecId == RDP_CODEC_ID_NSCODEC)
181
0
    {
182
0
      const int status = nsc_process_message(
183
0
          context->codecs->nsc, 32, DstWidth, DstHeight, pSrcData, SrcSize, bitmap->data,
184
0
          bitmap->format, 0, 0, 0, DstWidth, DstHeight, FREERDP_FLIP_VERTICAL);
185
186
0
      if (status < 1)
187
0
      {
188
0
        WLog_ERR(TAG, "nsc_process_message failed");
189
0
        return FALSE;
190
0
      }
191
192
0
      return freerdp_image_copy_no_overlap(bitmap->data, bitmap->format, 0, 0, 0, DstWidth,
193
0
                                           DstHeight, pSrcData, PIXEL_FORMAT_XRGB32, 0, 0, 0,
194
0
                                           &gdi->palette, FREERDP_FLIP_VERTICAL);
195
0
    }
196
0
    else if (bpp < 32)
197
0
    {
198
0
      if (!interleaved_decompress(context->codecs->interleaved, pSrcData, SrcSize, DstWidth,
199
0
                                  DstHeight, bpp, bitmap->data, bitmap->format, 0, 0, 0,
200
0
                                  DstWidth, DstHeight, &gdi->palette))
201
0
      {
202
0
        WLog_ERR(TAG, "interleaved_decompress failed");
203
0
        return FALSE;
204
0
      }
205
0
    }
206
0
    else
207
0
    {
208
0
      const BOOL fidelity =
209
0
          freerdp_settings_get_bool(context->settings, FreeRDP_DrawAllowDynamicColorFidelity);
210
0
      freerdp_planar_switch_bgr(context->codecs->planar, fidelity);
211
0
      if (!freerdp_bitmap_decompress_planar(context->codecs->planar, pSrcData, SrcSize,
212
0
                                            DstWidth, DstHeight, bitmap->data, bitmap->format,
213
0
                                            0, 0, 0, DstWidth, DstHeight, TRUE))
214
0
      {
215
0
        WLog_ERR(TAG, "freerdp_bitmap_decompress_planar failed");
216
0
        return FALSE;
217
0
      }
218
0
    }
219
0
  }
220
0
  else
221
0
  {
222
0
    const UINT32 SrcFormat = gdi_get_pixel_format(bpp);
223
0
    const size_t sbpp = FreeRDPGetBytesPerPixel(SrcFormat);
224
0
    const size_t dbpp = FreeRDPGetBytesPerPixel(bitmap->format);
225
226
0
    if ((sbpp == 0) || (dbpp == 0))
227
0
      return FALSE;
228
0
    else
229
0
    {
230
0
      const size_t dstSize = SrcSize * dbpp / sbpp;
231
232
0
      if (dstSize < bitmap->length)
233
0
      {
234
0
        WLog_ERR(TAG, "dstSize %" PRIuz " < bitmap->length %" PRIu32, dstSize,
235
0
                 bitmap->length);
236
0
        return FALSE;
237
0
      }
238
0
    }
239
240
0
    if (!freerdp_image_copy_no_overlap(bitmap->data, bitmap->format, 0, 0, 0, DstWidth,
241
0
                                       DstHeight, pSrcData, SrcFormat, 0, 0, 0, &gdi->palette,
242
0
                                       FREERDP_FLIP_VERTICAL))
243
0
    {
244
0
      WLog_ERR(TAG, "freerdp_image_copy failed");
245
0
      return FALSE;
246
0
    }
247
0
  }
248
249
0
  return TRUE;
250
0
}
251
252
static BOOL gdi_Bitmap_SetSurface(rdpContext* context, rdpBitmap* bitmap, BOOL primary)
253
0
{
254
0
  rdpGdi* gdi = nullptr;
255
256
0
  if (!context)
257
0
    return FALSE;
258
259
0
  gdi = context->gdi;
260
261
0
  if (!gdi)
262
0
    return FALSE;
263
264
0
  if (primary)
265
0
    gdi->drawing = gdi->primary;
266
0
  else
267
0
    gdi->drawing = (gdiBitmap*)bitmap;
268
269
0
  return TRUE;
270
0
}
271
272
/* Glyph Class */
273
static BOOL gdi_Glyph_New(rdpContext* context, rdpGlyph* glyph)
274
0
{
275
0
  if (!context || !glyph)
276
0
    return FALSE;
277
278
0
  gdiGlyph* gdi_glyph = (gdiGlyph*)glyph;
279
0
  gdi_glyph->hdc = gdi_GetDC();
280
281
0
  if (!gdi_glyph->hdc)
282
0
    return FALSE;
283
284
0
  gdi_glyph->hdc->format = PIXEL_FORMAT_MONO;
285
0
  BYTE* data = freerdp_glyph_convert_ex(glyph->cx, glyph->cy, glyph->aj, glyph->cb);
286
287
0
  if (!data)
288
0
  {
289
0
    gdi_DeleteDC(gdi_glyph->hdc);
290
0
    return FALSE;
291
0
  }
292
293
0
  gdi_glyph->bitmap = gdi_CreateBitmap(glyph->cx, glyph->cy, PIXEL_FORMAT_MONO, data);
294
295
0
  if (!gdi_glyph->bitmap)
296
0
  {
297
0
    gdi_DeleteDC(gdi_glyph->hdc);
298
0
    winpr_aligned_free(data);
299
0
    return FALSE;
300
0
  }
301
302
0
  gdi_SelectObject(gdi_glyph->hdc, (HGDIOBJECT)gdi_glyph->bitmap);
303
0
  gdi_glyph->org_bitmap = nullptr;
304
0
  return TRUE;
305
0
}
306
307
static void gdi_Glyph_Free(WINPR_ATTR_UNUSED rdpContext* context, rdpGlyph* glyph)
308
0
{
309
0
  gdiGlyph* gdi_glyph = nullptr;
310
0
  gdi_glyph = (gdiGlyph*)glyph;
311
312
0
  if (gdi_glyph)
313
0
  {
314
0
    gdi_SelectObject(gdi_glyph->hdc, (HGDIOBJECT)gdi_glyph->org_bitmap);
315
0
    gdi_DeleteObject((HGDIOBJECT)gdi_glyph->bitmap);
316
0
    gdi_DeleteDC(gdi_glyph->hdc);
317
0
    free(glyph->aj);
318
0
    free(glyph);
319
0
  }
320
0
}
321
322
static BOOL gdi_Glyph_Draw(rdpContext* context, const rdpGlyph* glyph, INT32 x, INT32 y, INT32 w,
323
                           INT32 h, INT32 sx, INT32 sy, BOOL fOpRedundant)
324
0
{
325
0
  const gdiGlyph* gdi_glyph = nullptr;
326
0
  rdpGdi* gdi = nullptr;
327
0
  HGDI_BRUSH brush = nullptr;
328
0
  BOOL rc = FALSE;
329
330
0
  if (!context || !glyph)
331
0
    return FALSE;
332
333
0
  gdi = context->gdi;
334
0
  gdi_glyph = (const gdiGlyph*)glyph;
335
336
0
  if (!fOpRedundant)
337
0
  {
338
0
    GDI_RECT rect = WINPR_C_ARRAY_INIT;
339
340
0
    if (x > 0)
341
0
      rect.left = x;
342
343
0
    if (y > 0)
344
0
      rect.top = y;
345
346
0
    if (x + w > 0)
347
0
      rect.right = x + w - 1;
348
349
0
    if (y + h > 0)
350
0
      rect.bottom = y + h - 1;
351
352
0
    if ((rect.left < rect.right) && (rect.top < rect.bottom))
353
0
    {
354
0
      brush = gdi_CreateSolidBrush(gdi->drawing->hdc->bkColor);
355
356
0
      if (!brush)
357
0
        return FALSE;
358
359
0
      const BOOL res = gdi_FillRect(gdi->drawing->hdc, &rect, brush);
360
0
      gdi_DeleteObject((HGDIOBJECT)brush);
361
0
      if (!res)
362
0
        return res;
363
0
    }
364
0
  }
365
366
0
  brush = gdi_CreateSolidBrush(gdi->drawing->hdc->textColor);
367
368
0
  if (!brush)
369
0
    return FALSE;
370
371
0
  gdi_SelectObject(gdi->drawing->hdc, (HGDIOBJECT)brush);
372
0
  rc = gdi_BitBlt(gdi->drawing->hdc, x, y, w, h, gdi_glyph->hdc, sx, sy, GDI_GLYPH_ORDER,
373
0
                  &context->gdi->palette);
374
0
  gdi_DeleteObject((HGDIOBJECT)brush);
375
0
  return rc;
376
0
}
377
378
static BOOL gdi_Glyph_BeginDraw(rdpContext* context, INT32 x, INT32 y, INT32 width, INT32 height,
379
                                UINT32 bgcolor, UINT32 fgcolor, BOOL fOpRedundant)
380
0
{
381
0
  if (!context || !context->gdi)
382
0
    return FALSE;
383
384
0
  rdpGdi* gdi = context->gdi;
385
386
0
  if (!gdi->drawing || !gdi->drawing->hdc)
387
0
    return FALSE;
388
389
0
  if (!fOpRedundant)
390
0
  {
391
0
    if (!gdi_decode_color(gdi, bgcolor, &bgcolor, nullptr))
392
0
      return FALSE;
393
394
0
    if (!gdi_decode_color(gdi, fgcolor, &fgcolor, nullptr))
395
0
      return FALSE;
396
397
0
    if (!gdi_SetClipRgn(gdi->drawing->hdc, x, y, width, height))
398
0
      return FALSE;
399
400
0
    gdi_SetTextColor(gdi->drawing->hdc, bgcolor);
401
0
    gdi_SetBkColor(gdi->drawing->hdc, fgcolor);
402
403
0
    {
404
0
      GDI_RECT rect = WINPR_C_ARRAY_INIT;
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
      BOOL res = TRUE;
420
0
      if ((x + width > rect.left) && (y + height > rect.top))
421
0
        res = gdi_FillRect(gdi->drawing->hdc, &rect, brush);
422
423
0
      gdi_DeleteObject((HGDIOBJECT)brush);
424
0
      if (!res)
425
0
        return FALSE;
426
0
    }
427
428
0
    return gdi_SetNullClipRgn(gdi->drawing->hdc);
429
0
  }
430
431
0
  return TRUE;
432
0
}
433
434
static BOOL gdi_Glyph_EndDraw(rdpContext* context, WINPR_ATTR_UNUSED INT32 x,
435
                              WINPR_ATTR_UNUSED INT32 y, WINPR_ATTR_UNUSED INT32 width,
436
                              WINPR_ATTR_UNUSED INT32 height, WINPR_ATTR_UNUSED UINT32 bgcolor,
437
                              WINPR_ATTR_UNUSED UINT32 fgcolor)
438
0
{
439
0
  rdpGdi* gdi = nullptr;
440
441
0
  if (!context || !context->gdi)
442
0
    return FALSE;
443
444
0
  gdi = context->gdi;
445
446
0
  if (!gdi->drawing || !gdi->drawing->hdc)
447
0
    return FALSE;
448
449
0
  return gdi_SetNullClipRgn(gdi->drawing->hdc);
450
0
}
451
452
/* Graphics Module */
453
BOOL gdi_register_graphics(rdpGraphics* graphics)
454
0
{
455
0
  rdpBitmap bitmap = WINPR_C_ARRAY_INIT;
456
0
  rdpGlyph glyph = WINPR_C_ARRAY_INIT;
457
0
  bitmap.size = sizeof(gdiBitmap);
458
0
  bitmap.New = gdi_Bitmap_New;
459
0
  bitmap.Free = gdi_Bitmap_Free;
460
0
  bitmap.Paint = gdi_Bitmap_Paint;
461
0
  bitmap.Decompress = gdi_Bitmap_Decompress;
462
0
  bitmap.SetSurface = gdi_Bitmap_SetSurface;
463
0
  graphics_register_bitmap(graphics, &bitmap);
464
0
  glyph.size = sizeof(gdiGlyph);
465
0
  glyph.New = gdi_Glyph_New;
466
0
  glyph.Free = gdi_Glyph_Free;
467
0
  glyph.Draw = gdi_Glyph_Draw;
468
0
  glyph.BeginDraw = gdi_Glyph_BeginDraw;
469
0
  glyph.EndDraw = gdi_Glyph_EndDraw;
470
0
  graphics_register_glyph(graphics, &glyph);
471
0
  return TRUE;
472
0
}