Coverage Report

Created: 2026-02-26 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/gdi/clipping.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * GDI Clipping Functions
4
 *
5
 * Copyright 2010-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 <stdio.h>
25
#include <string.h>
26
#include <stdlib.h>
27
28
#include <freerdp/freerdp.h>
29
#include <freerdp/gdi/gdi.h>
30
31
#include <freerdp/gdi/region.h>
32
33
#include "clipping.h"
34
35
BOOL gdi_SetClipRgn(HGDI_DC hdc, INT32 nXLeft, INT32 nYLeft, INT32 nWidth, INT32 nHeight)
36
0
{
37
0
  return gdi_SetRgn(hdc->clip, nXLeft, nYLeft, nWidth, nHeight);
38
0
}
39
40
/**
41
 * Get the current clipping region.
42
 * msdn{dd144866}
43
 *
44
 * @param hdc device context
45
 * @return clipping region
46
 */
47
48
GDI_RGN* gdi_GetClipRgn(HGDI_DC hdc)
49
0
{
50
0
  return hdc->clip;
51
0
}
52
53
/**
54
 * Set the current clipping region to null.
55
 * @param hdc device context
56
 * @return nonzero on success, 0 otherwise
57
 */
58
59
BOOL gdi_SetNullClipRgn(HGDI_DC hdc)
60
0
{
61
0
  if (!gdi_SetClipRgn(hdc, 0, 0, 0, 0))
62
0
    return FALSE;
63
0
  hdc->clip->null = TRUE;
64
0
  return TRUE;
65
0
}
66
67
/**
68
 * Clip coordinates according to clipping region
69
 * @param hdc device context
70
 * @param x x1
71
 * @param y y1
72
 * @param w width
73
 * @param h height
74
 * @param srcx source x1
75
 * @param srcy source y1
76
 * @return nonzero if there is something to draw, 0 otherwise
77
 */
78
79
BOOL gdi_ClipCoords(HGDI_DC hdc, INT32* x, INT32* y, INT32* w, INT32* h, INT32* srcx, INT32* srcy)
80
0
{
81
0
  GDI_RECT bmp = WINPR_C_ARRAY_INIT;
82
0
  GDI_RECT clip = WINPR_C_ARRAY_INIT;
83
0
  GDI_RECT coords = WINPR_C_ARRAY_INIT;
84
0
  int dx = 0;
85
0
  int dy = 0;
86
0
  BOOL draw = TRUE;
87
88
0
  if (hdc == NULL)
89
0
    return FALSE;
90
91
0
  HGDI_BITMAP hBmp = (HGDI_BITMAP)hdc->selectedObject;
92
93
0
  if (hBmp != NULL)
94
0
  {
95
0
    if (hdc->clip->null)
96
0
    {
97
0
      if (!gdi_CRgnToRect(0, 0, hBmp->width, hBmp->height, &clip))
98
0
        return TRUE;
99
0
    }
100
0
    else
101
0
    {
102
0
      if (!gdi_RgnToRect(hdc->clip, &clip))
103
0
        return TRUE;
104
0
      if (!gdi_CRgnToRect(0, 0, hBmp->width, hBmp->height, &bmp))
105
0
        return TRUE;
106
107
0
      if (clip.left < bmp.left)
108
0
        clip.left = bmp.left;
109
110
0
      if (clip.right > bmp.right)
111
0
        clip.right = bmp.right;
112
113
0
      if (clip.top < bmp.top)
114
0
        clip.top = bmp.top;
115
116
0
      if (clip.bottom > bmp.bottom)
117
0
        clip.bottom = bmp.bottom;
118
0
    }
119
0
  }
120
0
  else
121
0
  {
122
0
    if (!gdi_RgnToRect(hdc->clip, &clip))
123
0
      return TRUE;
124
0
  }
125
126
0
  if (!gdi_CRgnToRect(*x, *y, *w, *h, &coords))
127
0
    return TRUE;
128
129
0
  if (coords.right >= clip.left && coords.left <= clip.right && coords.bottom >= clip.top &&
130
0
      coords.top <= clip.bottom)
131
0
  {
132
    /* coordinates overlap with clipping region */
133
0
    if (coords.left < clip.left)
134
0
    {
135
0
      dx = (clip.left - coords.left);
136
0
      coords.left = clip.left;
137
0
    }
138
139
0
    if (coords.right > clip.right)
140
0
      coords.right = clip.right;
141
142
0
    if (coords.top < clip.top)
143
0
    {
144
0
      dy = (clip.top - coords.top);
145
0
      coords.top = clip.top;
146
0
    }
147
148
0
    if (coords.bottom > clip.bottom)
149
0
      coords.bottom = clip.bottom;
150
0
  }
151
0
  else
152
0
  {
153
    /* coordinates do not overlap with clipping region */
154
0
    coords.left = 0;
155
0
    coords.right = 0;
156
0
    coords.top = 0;
157
0
    coords.bottom = 0;
158
0
    draw = FALSE;
159
0
  }
160
161
0
  if (srcx != NULL)
162
0
    *srcx += dx;
163
164
0
  if (srcy != NULL)
165
0
    *srcy += dy;
166
167
0
  if (!gdi_RectToCRgn(&coords, x, y, w, h))
168
0
    return FALSE;
169
0
  return draw;
170
0
}