/src/FreeRDP/libfreerdp/gdi/bitmap.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * GDI Bitmap 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/api.h> |
29 | | #include <freerdp/freerdp.h> |
30 | | #include <freerdp/gdi/gdi.h> |
31 | | #include <freerdp/codec/color.h> |
32 | | |
33 | | #include <freerdp/gdi/region.h> |
34 | | #include <freerdp/gdi/bitmap.h> |
35 | | #include <freerdp/log.h> |
36 | | #include <freerdp/gdi/shape.h> |
37 | | |
38 | | #include "brush.h" |
39 | | #include "clipping.h" |
40 | | #include "../gdi/gdi.h" |
41 | | |
42 | | #define TAG FREERDP_TAG("gdi.bitmap") |
43 | | |
44 | | /** |
45 | | * Get pixel at the given coordinates. msdn{dd144909} |
46 | | * @param hdc device context |
47 | | * @param nXPos pixel x position |
48 | | * @param nYPos pixel y position |
49 | | * @return pixel color |
50 | | */ |
51 | | |
52 | | UINT32 gdi_GetPixel(HGDI_DC hdc, UINT32 nXPos, UINT32 nYPos) |
53 | 0 | { |
54 | 0 | HGDI_BITMAP hBmp = (HGDI_BITMAP)hdc->selectedObject; |
55 | 0 | BYTE* data = |
56 | 0 | &(hBmp->data[(nYPos * hBmp->scanline) + nXPos * FreeRDPGetBytesPerPixel(hBmp->format)]); |
57 | 0 | return FreeRDPReadColor(data, hBmp->format); |
58 | 0 | } |
59 | | |
60 | | BYTE* gdi_GetPointer(HGDI_BITMAP hBmp, UINT32 X, UINT32 Y) |
61 | 0 | { |
62 | 0 | UINT32 bpp = FreeRDPGetBytesPerPixel(hBmp->format); |
63 | 0 | return &hBmp->data[(Y * WINPR_ASSERTING_INT_CAST(uint32_t, hBmp->width) * bpp) + X * bpp]; |
64 | 0 | } |
65 | | |
66 | | /** |
67 | | * Set pixel at the given coordinates. msdn{dd145078} |
68 | | * |
69 | | * @param hBmp device context |
70 | | * @param X pixel x position |
71 | | * @param Y pixel y position |
72 | | * @param crColor new pixel color |
73 | | * @return the color written |
74 | | */ |
75 | | |
76 | | static INLINE UINT32 gdi_SetPixelBmp(HGDI_BITMAP hBmp, UINT32 X, UINT32 Y, UINT32 crColor) |
77 | 0 | { |
78 | 0 | BYTE* p = &hBmp->data[(Y * hBmp->scanline) + X * FreeRDPGetBytesPerPixel(hBmp->format)]; |
79 | 0 | FreeRDPWriteColor(p, hBmp->format, crColor); |
80 | 0 | return crColor; |
81 | 0 | } |
82 | | |
83 | | UINT32 gdi_SetPixel(HGDI_DC hdc, UINT32 X, UINT32 Y, UINT32 crColor) |
84 | 0 | { |
85 | 0 | HGDI_BITMAP hBmp = (HGDI_BITMAP)hdc->selectedObject; |
86 | 0 | return gdi_SetPixelBmp(hBmp, X, Y, crColor); |
87 | 0 | } |
88 | | |
89 | | /** |
90 | | * Create a new bitmap with the given width, height, color format and pixel buffer. msdn{dd183485} |
91 | | * |
92 | | * @param nWidth width |
93 | | * @param nHeight height |
94 | | * @param format the color format used |
95 | | * @param data pixel buffer |
96 | | * @return new bitmap |
97 | | */ |
98 | | |
99 | | HGDI_BITMAP gdi_CreateBitmap(UINT32 nWidth, UINT32 nHeight, UINT32 format, BYTE* data) |
100 | 0 | { |
101 | 0 | return gdi_CreateBitmapEx(nWidth, nHeight, format, 0, data, winpr_aligned_free); |
102 | 0 | } |
103 | | |
104 | | /** |
105 | | * Create a new bitmap with the given width, height, color format and pixel buffer. msdn{dd183485} |
106 | | * |
107 | | * @param nWidth width |
108 | | * @param nHeight height |
109 | | * @param format the color format used |
110 | | * @param data pixel buffer |
111 | | * @param fkt_free The function used for deallocation of the buffer, NULL for none. |
112 | | * @return new bitmap |
113 | | */ |
114 | | |
115 | | HGDI_BITMAP gdi_CreateBitmapEx(UINT32 nWidth, UINT32 nHeight, UINT32 format, UINT32 stride, |
116 | | BYTE* data, void (*fkt_free)(void*)) |
117 | 0 | { |
118 | 0 | HGDI_BITMAP hBitmap = (HGDI_BITMAP)calloc(1, sizeof(GDI_BITMAP)); |
119 | |
|
120 | 0 | if (!hBitmap) |
121 | 0 | return NULL; |
122 | | |
123 | 0 | hBitmap->objectType = GDIOBJECT_BITMAP; |
124 | 0 | hBitmap->format = format; |
125 | |
|
126 | 0 | if (stride > 0) |
127 | 0 | hBitmap->scanline = stride; |
128 | 0 | else |
129 | 0 | hBitmap->scanline = nWidth * FreeRDPGetBytesPerPixel(hBitmap->format); |
130 | |
|
131 | 0 | hBitmap->width = WINPR_ASSERTING_INT_CAST(int, nWidth); |
132 | 0 | hBitmap->height = WINPR_ASSERTING_INT_CAST(int, nHeight); |
133 | 0 | hBitmap->data = data; |
134 | 0 | hBitmap->free = fkt_free; |
135 | 0 | return hBitmap; |
136 | 0 | } |
137 | | |
138 | | /** |
139 | | * Create a new bitmap of the given width and height compatible with the current device context. |
140 | | * msdn{dd183488} |
141 | | * |
142 | | * @param hdc device context |
143 | | * @param nWidth width |
144 | | * @param nHeight height |
145 | | * |
146 | | * @return new bitmap |
147 | | */ |
148 | | |
149 | | HGDI_BITMAP gdi_CreateCompatibleBitmap(HGDI_DC hdc, UINT32 nWidth, UINT32 nHeight) |
150 | 0 | { |
151 | 0 | HGDI_BITMAP hBitmap = (HGDI_BITMAP)calloc(1, sizeof(GDI_BITMAP)); |
152 | |
|
153 | 0 | if (!hBitmap) |
154 | 0 | return NULL; |
155 | | |
156 | 0 | hBitmap->objectType = GDIOBJECT_BITMAP; |
157 | 0 | hBitmap->format = hdc->format; |
158 | 0 | WINPR_ASSERT(nWidth <= INT32_MAX); |
159 | 0 | hBitmap->width = (INT32)nWidth; |
160 | |
|
161 | 0 | WINPR_ASSERT(nHeight <= INT32_MAX); |
162 | 0 | hBitmap->height = (INT32)nHeight; |
163 | |
|
164 | 0 | size_t size = 1ull * nWidth * nHeight * FreeRDPGetBytesPerPixel(hBitmap->format); |
165 | 0 | hBitmap->data = winpr_aligned_malloc(size, 16); |
166 | 0 | hBitmap->free = winpr_aligned_free; |
167 | |
|
168 | 0 | if (!hBitmap->data) |
169 | 0 | { |
170 | 0 | free(hBitmap); |
171 | 0 | return NULL; |
172 | 0 | } |
173 | | |
174 | | /* Initialize with 0xff */ |
175 | 0 | memset(hBitmap->data, 0xff, size); |
176 | 0 | hBitmap->scanline = nWidth * FreeRDPGetBytesPerPixel(hBitmap->format); |
177 | 0 | return hBitmap; |
178 | 0 | } |
179 | | |
180 | | static BOOL op_not(UINT32* stack, const UINT32* stackp) |
181 | 0 | { |
182 | 0 | if (!stack || !stackp) |
183 | 0 | return FALSE; |
184 | | |
185 | 0 | if (*stackp < 1) |
186 | 0 | return FALSE; |
187 | | |
188 | 0 | stack[(*stackp) - 1] = ~stack[(*stackp) - 1]; |
189 | 0 | return TRUE; |
190 | 0 | } |
191 | | |
192 | | static BOOL op_and(UINT32* stack, UINT32* stackp) |
193 | 0 | { |
194 | 0 | if (!stack || !stackp) |
195 | 0 | return FALSE; |
196 | | |
197 | 0 | if (*stackp < 2) |
198 | 0 | return FALSE; |
199 | | |
200 | 0 | (*stackp)--; |
201 | 0 | stack[(*stackp) - 1] &= stack[(*stackp)]; |
202 | 0 | return TRUE; |
203 | 0 | } |
204 | | |
205 | | static BOOL op_or(UINT32* stack, UINT32* stackp) |
206 | 0 | { |
207 | 0 | if (!stack || !stackp) |
208 | 0 | return FALSE; |
209 | | |
210 | 0 | if (*stackp < 2) |
211 | 0 | return FALSE; |
212 | | |
213 | 0 | (*stackp)--; |
214 | 0 | stack[(*stackp) - 1] |= stack[(*stackp)]; |
215 | 0 | return TRUE; |
216 | 0 | } |
217 | | |
218 | | static BOOL op_xor(UINT32* stack, UINT32* stackp) |
219 | 0 | { |
220 | 0 | if (!stack || !stackp) |
221 | 0 | return FALSE; |
222 | | |
223 | 0 | if (*stackp < 2) |
224 | 0 | return FALSE; |
225 | | |
226 | 0 | (*stackp)--; |
227 | 0 | stack[(*stackp) - 1] ^= stack[(*stackp)]; |
228 | 0 | return TRUE; |
229 | 0 | } |
230 | | |
231 | | static UINT32 process_rop(UINT32 src, UINT32 dst, UINT32 pat, const char* rop, UINT32 format) |
232 | 0 | { |
233 | 0 | UINT32 stack[10] = { 0 }; |
234 | 0 | UINT32 stackp = 0; |
235 | |
|
236 | 0 | while (*rop != '\0') |
237 | 0 | { |
238 | 0 | char op = *rop++; |
239 | |
|
240 | 0 | switch (op) |
241 | 0 | { |
242 | 0 | case '0': |
243 | 0 | stack[stackp++] = FreeRDPGetColor(format, 0, 0, 0, 0xFF); |
244 | 0 | break; |
245 | | |
246 | 0 | case '1': |
247 | 0 | stack[stackp++] = FreeRDPGetColor(format, 0xFF, 0xFF, 0xFF, 0xFF); |
248 | 0 | break; |
249 | | |
250 | 0 | case 'D': |
251 | 0 | stack[stackp++] = dst; |
252 | 0 | break; |
253 | | |
254 | 0 | case 'S': |
255 | 0 | stack[stackp++] = src; |
256 | 0 | break; |
257 | | |
258 | 0 | case 'P': |
259 | 0 | stack[stackp++] = pat; |
260 | 0 | break; |
261 | | |
262 | 0 | case 'x': |
263 | 0 | op_xor(stack, &stackp); |
264 | 0 | break; |
265 | | |
266 | 0 | case 'a': |
267 | 0 | op_and(stack, &stackp); |
268 | 0 | break; |
269 | | |
270 | 0 | case 'o': |
271 | 0 | op_or(stack, &stackp); |
272 | 0 | break; |
273 | | |
274 | 0 | case 'n': |
275 | 0 | op_not(stack, &stackp); |
276 | 0 | break; |
277 | | |
278 | 0 | default: |
279 | 0 | break; |
280 | 0 | } |
281 | 0 | } |
282 | | |
283 | 0 | return stack[0]; |
284 | 0 | } |
285 | | |
286 | | static INLINE BOOL BitBlt_write(HGDI_DC hdcDest, HGDI_DC hdcSrc, INT32 nXDest, INT32 nYDest, |
287 | | INT32 nXSrc, INT32 nYSrc, INT32 x, INT32 y, BOOL useSrc, |
288 | | BOOL usePat, UINT32 style, const char* rop, |
289 | | const gdiPalette* palette) |
290 | 0 | { |
291 | 0 | UINT32 dstColor = 0; |
292 | 0 | UINT32 colorA = 0; |
293 | 0 | UINT32 colorB = 0; |
294 | 0 | UINT32 colorC = 0; |
295 | 0 | const INT32 dstX = nXDest + x; |
296 | 0 | const INT32 dstY = nYDest + y; |
297 | 0 | BYTE* dstp = gdi_get_bitmap_pointer(hdcDest, dstX, dstY); |
298 | |
|
299 | 0 | if (!dstp) |
300 | 0 | { |
301 | 0 | WLog_ERR(TAG, "dstp=%p", (const void*)dstp); |
302 | 0 | return FALSE; |
303 | 0 | } |
304 | | |
305 | 0 | colorA = FreeRDPReadColor(dstp, hdcDest->format); |
306 | |
|
307 | 0 | if (useSrc) |
308 | 0 | { |
309 | 0 | const BYTE* srcp = gdi_get_bitmap_pointer(hdcSrc, nXSrc + x, nYSrc + y); |
310 | |
|
311 | 0 | if (!srcp) |
312 | 0 | { |
313 | 0 | WLog_ERR(TAG, "srcp=%p", (const void*)srcp); |
314 | 0 | return FALSE; |
315 | 0 | } |
316 | | |
317 | 0 | colorC = FreeRDPReadColor(srcp, hdcSrc->format); |
318 | 0 | colorC = FreeRDPConvertColor(colorC, hdcSrc->format, hdcDest->format, palette); |
319 | 0 | } |
320 | | |
321 | 0 | if (usePat) |
322 | 0 | { |
323 | 0 | switch (style) |
324 | 0 | { |
325 | 0 | case GDI_BS_SOLID: |
326 | 0 | colorB = hdcDest->brush->color; |
327 | 0 | break; |
328 | | |
329 | 0 | case GDI_BS_HATCHED: |
330 | 0 | case GDI_BS_PATTERN: |
331 | 0 | { |
332 | 0 | const BYTE* patp = |
333 | 0 | gdi_get_brush_pointer(hdcDest, WINPR_ASSERTING_INT_CAST(uint32_t, nXDest + x), |
334 | 0 | WINPR_ASSERTING_INT_CAST(uint32_t, nYDest + y)); |
335 | | |
336 | 0 | if (!patp) |
337 | 0 | { |
338 | 0 | WLog_ERR(TAG, "patp=%p", (const void*)patp); |
339 | 0 | return FALSE; |
340 | 0 | } |
341 | | |
342 | 0 | colorB = FreeRDPReadColor(patp, hdcDest->format); |
343 | 0 | } |
344 | 0 | break; |
345 | | |
346 | 0 | default: |
347 | 0 | break; |
348 | 0 | } |
349 | 0 | } |
350 | | |
351 | 0 | dstColor = process_rop(colorC, colorA, colorB, rop, hdcDest->format); |
352 | 0 | return FreeRDPWriteColor(dstp, hdcDest->format, dstColor); |
353 | 0 | } |
354 | | |
355 | | static BOOL adjust_src_coordinates(HGDI_DC hdcSrc, INT32 nWidth, INT32 nHeight, INT32* px, |
356 | | INT32* py) |
357 | 0 | { |
358 | 0 | HGDI_BITMAP hSrcBmp = NULL; |
359 | 0 | INT32 nXSrc = 0; |
360 | 0 | INT32 nYSrc = 0; |
361 | |
|
362 | 0 | if (!hdcSrc || (nWidth < 0) || (nHeight < 0) || !px || !py) |
363 | 0 | return FALSE; |
364 | | |
365 | 0 | hSrcBmp = (HGDI_BITMAP)hdcSrc->selectedObject; |
366 | 0 | nXSrc = *px; |
367 | 0 | nYSrc = *py; |
368 | |
|
369 | 0 | if (!hSrcBmp) |
370 | 0 | return FALSE; |
371 | | |
372 | 0 | if (nYSrc < 0) |
373 | 0 | { |
374 | 0 | nYSrc = 0; |
375 | 0 | nHeight = nHeight + nYSrc; |
376 | 0 | } |
377 | |
|
378 | 0 | if ((nXSrc) < 0) |
379 | 0 | { |
380 | 0 | nXSrc = 0; |
381 | 0 | nWidth = nWidth + nXSrc; |
382 | 0 | } |
383 | |
|
384 | 0 | if (hSrcBmp->width < (nXSrc + nWidth)) |
385 | 0 | nXSrc = hSrcBmp->width - nWidth; |
386 | |
|
387 | 0 | if (hSrcBmp->height < (nYSrc + nHeight)) |
388 | 0 | nYSrc = hSrcBmp->height - nHeight; |
389 | |
|
390 | 0 | if ((nXSrc < 0) || (nYSrc < 0)) |
391 | 0 | return FALSE; |
392 | | |
393 | 0 | *px = nXSrc; |
394 | 0 | *py = nYSrc; |
395 | 0 | return TRUE; |
396 | 0 | } |
397 | | |
398 | | static BOOL adjust_src_dst_coordinates(HGDI_DC hdcDest, INT32* pnXSrc, INT32* pnYSrc, INT32* pnXDst, |
399 | | INT32* pnYDst, INT32* pnWidth, INT32* pnHeight) |
400 | 0 | { |
401 | 0 | HGDI_BITMAP hDstBmp = NULL; |
402 | 0 | volatile INT32 diffX = 0; |
403 | 0 | volatile INT32 diffY = 0; |
404 | 0 | volatile INT32 nXSrc = 0; |
405 | 0 | volatile INT32 nYSrc = 0; |
406 | 0 | volatile INT32 nXDst = 0; |
407 | 0 | volatile INT32 nYDst = 0; |
408 | 0 | volatile INT32 nWidth = 0; |
409 | 0 | volatile INT32 nHeight = 0; |
410 | |
|
411 | 0 | if (!hdcDest || !pnXSrc || !pnYSrc || !pnXDst || !pnYDst || !pnWidth || !pnHeight) |
412 | 0 | return FALSE; |
413 | | |
414 | 0 | hDstBmp = (HGDI_BITMAP)hdcDest->selectedObject; |
415 | 0 | nXSrc = *pnXSrc; |
416 | 0 | nYSrc = *pnYSrc; |
417 | 0 | nXDst = *pnXDst; |
418 | 0 | nYDst = *pnYDst; |
419 | 0 | nWidth = *pnWidth; |
420 | 0 | nHeight = *pnHeight; |
421 | |
|
422 | 0 | if (!hDstBmp) |
423 | 0 | return FALSE; |
424 | | |
425 | 0 | if (nXDst < 0) |
426 | 0 | { |
427 | 0 | nXSrc -= nXDst; |
428 | 0 | nWidth += nXDst; |
429 | 0 | nXDst = 0; |
430 | 0 | } |
431 | |
|
432 | 0 | if (nYDst < 0) |
433 | 0 | { |
434 | 0 | nYSrc -= nYDst; |
435 | 0 | nHeight += nYDst; |
436 | 0 | nYDst = 0; |
437 | 0 | } |
438 | |
|
439 | 0 | diffX = hDstBmp->width - nXDst - nWidth; |
440 | |
|
441 | 0 | if (diffX < 0) |
442 | 0 | nWidth += diffX; |
443 | |
|
444 | 0 | diffY = hDstBmp->height - nYDst - nHeight; |
445 | |
|
446 | 0 | if (diffY < 0) |
447 | 0 | nHeight += diffY; |
448 | |
|
449 | 0 | if ((nXDst < 0) || (nYDst < 0) || (nWidth < 0) || (nHeight < 0)) |
450 | 0 | { |
451 | 0 | nXDst = 0; |
452 | 0 | nYDst = 0; |
453 | 0 | nWidth = 0; |
454 | 0 | nHeight = 0; |
455 | 0 | } |
456 | |
|
457 | 0 | *pnXSrc = nXSrc; |
458 | 0 | *pnYSrc = nYSrc; |
459 | 0 | *pnXDst = nXDst; |
460 | 0 | *pnYDst = nYDst; |
461 | 0 | *pnWidth = nWidth; |
462 | 0 | *pnHeight = nHeight; |
463 | 0 | return TRUE; |
464 | 0 | } |
465 | | |
466 | | static BOOL BitBlt_process(HGDI_DC hdcDest, INT32 nXDest, INT32 nYDest, INT32 nWidth, INT32 nHeight, |
467 | | HGDI_DC hdcSrc, INT32 nXSrc, INT32 nYSrc, const char* rop, |
468 | | const gdiPalette* palette) |
469 | 0 | { |
470 | 0 | UINT32 style = 0; |
471 | 0 | BOOL useSrc = FALSE; |
472 | 0 | BOOL usePat = FALSE; |
473 | 0 | const char* iter = rop; |
474 | |
|
475 | 0 | while (*iter != '\0') |
476 | 0 | { |
477 | 0 | switch (*iter++) |
478 | 0 | { |
479 | 0 | case 'P': |
480 | 0 | usePat = TRUE; |
481 | 0 | break; |
482 | | |
483 | 0 | case 'S': |
484 | 0 | useSrc = TRUE; |
485 | 0 | break; |
486 | | |
487 | 0 | default: |
488 | 0 | break; |
489 | 0 | } |
490 | 0 | } |
491 | | |
492 | 0 | if (!hdcDest) |
493 | 0 | return FALSE; |
494 | | |
495 | 0 | if (!adjust_src_dst_coordinates(hdcDest, &nXSrc, &nYSrc, &nXDest, &nYDest, &nWidth, &nHeight)) |
496 | 0 | return FALSE; |
497 | | |
498 | 0 | if (useSrc && !hdcSrc) |
499 | 0 | return FALSE; |
500 | | |
501 | 0 | if (useSrc) |
502 | 0 | { |
503 | 0 | if (!adjust_src_coordinates(hdcSrc, nWidth, nHeight, &nXSrc, &nYSrc)) |
504 | 0 | return FALSE; |
505 | 0 | } |
506 | | |
507 | 0 | if (usePat) |
508 | 0 | { |
509 | 0 | style = gdi_GetBrushStyle(hdcDest); |
510 | |
|
511 | 0 | switch (style) |
512 | 0 | { |
513 | 0 | case GDI_BS_SOLID: |
514 | 0 | case GDI_BS_HATCHED: |
515 | 0 | case GDI_BS_PATTERN: |
516 | 0 | break; |
517 | | |
518 | 0 | default: |
519 | 0 | WLog_ERR(TAG, "Invalid brush!!"); |
520 | 0 | return FALSE; |
521 | 0 | } |
522 | 0 | } |
523 | | |
524 | 0 | if ((nXDest > nXSrc) && (nYDest > nYSrc)) |
525 | 0 | { |
526 | 0 | for (INT32 y = nHeight - 1; y >= 0; y--) |
527 | 0 | { |
528 | 0 | for (INT32 x = nWidth - 1; x >= 0; x--) |
529 | 0 | { |
530 | 0 | if (!BitBlt_write(hdcDest, hdcSrc, nXDest, nYDest, nXSrc, nYSrc, x, y, useSrc, |
531 | 0 | usePat, style, rop, palette)) |
532 | 0 | return FALSE; |
533 | 0 | } |
534 | 0 | } |
535 | 0 | } |
536 | 0 | else if (nXDest > nXSrc) |
537 | 0 | { |
538 | 0 | for (INT32 y = 0; y < nHeight; y++) |
539 | 0 | { |
540 | 0 | for (INT32 x = nWidth - 1; x >= 0; x--) |
541 | 0 | { |
542 | 0 | if (!BitBlt_write(hdcDest, hdcSrc, nXDest, nYDest, nXSrc, nYSrc, x, y, useSrc, |
543 | 0 | usePat, style, rop, palette)) |
544 | 0 | return FALSE; |
545 | 0 | } |
546 | 0 | } |
547 | 0 | } |
548 | 0 | else if (nYDest > nYSrc) |
549 | 0 | { |
550 | 0 | for (INT32 y = nHeight - 1; y >= 0; y--) |
551 | 0 | { |
552 | 0 | for (INT32 x = 0; x < nWidth; x++) |
553 | 0 | { |
554 | 0 | if (!BitBlt_write(hdcDest, hdcSrc, nXDest, nYDest, nXSrc, nYSrc, x, y, useSrc, |
555 | 0 | usePat, style, rop, palette)) |
556 | 0 | return FALSE; |
557 | 0 | } |
558 | 0 | } |
559 | 0 | } |
560 | 0 | else |
561 | 0 | { |
562 | 0 | for (INT32 y = 0; y < nHeight; y++) |
563 | 0 | { |
564 | 0 | for (INT32 x = 0; x < nWidth; x++) |
565 | 0 | { |
566 | 0 | if (!BitBlt_write(hdcDest, hdcSrc, nXDest, nYDest, nXSrc, nYSrc, x, y, useSrc, |
567 | 0 | usePat, style, rop, palette)) |
568 | 0 | return FALSE; |
569 | 0 | } |
570 | 0 | } |
571 | 0 | } |
572 | | |
573 | 0 | return TRUE; |
574 | 0 | } |
575 | | |
576 | | /** |
577 | | * Perform a bit blit operation on the given pixel buffers. |
578 | | * msdn{dd183370} |
579 | | * |
580 | | * @param hdcDest destination device context |
581 | | * @param nXDest destination x1 |
582 | | * @param nYDest destination y1 |
583 | | * @param nWidth width |
584 | | * @param nHeight height |
585 | | * @param hdcSrc source device context |
586 | | * @param nXSrc source x1 |
587 | | * @param nYSrc source y1 |
588 | | * @param rop raster operation code |
589 | | * @return 0 on failure, non-zero otherwise |
590 | | */ |
591 | | BOOL gdi_BitBlt(HGDI_DC hdcDest, INT32 nXDest, INT32 nYDest, INT32 nWidth, INT32 nHeight, |
592 | | HGDI_DC hdcSrc, INT32 nXSrc, INT32 nYSrc, DWORD rop, const gdiPalette* palette) |
593 | 0 | { |
594 | 0 | HGDI_BITMAP hSrcBmp = NULL; |
595 | 0 | HGDI_BITMAP hDstBmp = NULL; |
596 | |
|
597 | 0 | if (!hdcDest) |
598 | 0 | return FALSE; |
599 | | |
600 | 0 | if (!gdi_ClipCoords(hdcDest, &nXDest, &nYDest, &nWidth, &nHeight, &nXSrc, &nYSrc)) |
601 | 0 | return TRUE; |
602 | | |
603 | | /* Check which ROP should be performed. |
604 | | * Some specific ROP are used heavily and are resource intensive, |
605 | | * add optimized versions for these here. |
606 | | * |
607 | | * For all others fall back to the generic implementation. |
608 | | */ |
609 | 0 | switch (rop) |
610 | 0 | { |
611 | 0 | case GDI_SRCCOPY: |
612 | 0 | if (!hdcSrc) |
613 | 0 | return FALSE; |
614 | | |
615 | 0 | if (!adjust_src_dst_coordinates(hdcDest, &nXSrc, &nYSrc, &nXDest, &nYDest, &nWidth, |
616 | 0 | &nHeight)) |
617 | 0 | return FALSE; |
618 | | |
619 | 0 | if (!adjust_src_coordinates(hdcSrc, nWidth, nHeight, &nXSrc, &nYSrc)) |
620 | 0 | return FALSE; |
621 | | |
622 | 0 | hSrcBmp = (HGDI_BITMAP)hdcSrc->selectedObject; |
623 | 0 | hDstBmp = (HGDI_BITMAP)hdcDest->selectedObject; |
624 | |
|
625 | 0 | if (!hSrcBmp || !hDstBmp) |
626 | 0 | return FALSE; |
627 | | |
628 | 0 | if (!freerdp_image_copy( |
629 | 0 | hDstBmp->data, hDstBmp->format, hDstBmp->scanline, |
630 | 0 | WINPR_ASSERTING_INT_CAST(UINT32, nXDest), |
631 | 0 | WINPR_ASSERTING_INT_CAST(UINT32, nYDest), |
632 | 0 | WINPR_ASSERTING_INT_CAST(UINT32, nWidth), |
633 | 0 | WINPR_ASSERTING_INT_CAST(UINT32, nHeight), hSrcBmp->data, hSrcBmp->format, |
634 | 0 | hSrcBmp->scanline, WINPR_ASSERTING_INT_CAST(UINT32, nXSrc), |
635 | 0 | WINPR_ASSERTING_INT_CAST(UINT32, nYSrc), palette, FREERDP_FLIP_NONE)) |
636 | 0 | return FALSE; |
637 | | |
638 | 0 | break; |
639 | | |
640 | 0 | case GDI_DSTCOPY: |
641 | 0 | hSrcBmp = (HGDI_BITMAP)hdcDest->selectedObject; |
642 | 0 | hDstBmp = (HGDI_BITMAP)hdcDest->selectedObject; |
643 | |
|
644 | 0 | if (!adjust_src_dst_coordinates(hdcDest, &nXSrc, &nYSrc, &nXDest, &nYDest, &nWidth, |
645 | 0 | &nHeight)) |
646 | 0 | return FALSE; |
647 | | |
648 | 0 | if (!adjust_src_coordinates(hdcDest, nWidth, nHeight, &nXSrc, &nYSrc)) |
649 | 0 | return FALSE; |
650 | | |
651 | 0 | if (!hSrcBmp || !hDstBmp) |
652 | 0 | return FALSE; |
653 | | |
654 | 0 | if (!freerdp_image_copy( |
655 | 0 | hDstBmp->data, hDstBmp->format, hDstBmp->scanline, |
656 | 0 | WINPR_ASSERTING_INT_CAST(UINT32, nXDest), |
657 | 0 | WINPR_ASSERTING_INT_CAST(UINT32, nYDest), |
658 | 0 | WINPR_ASSERTING_INT_CAST(UINT32, nWidth), |
659 | 0 | WINPR_ASSERTING_INT_CAST(UINT32, nHeight), hSrcBmp->data, hSrcBmp->format, |
660 | 0 | hSrcBmp->scanline, WINPR_ASSERTING_INT_CAST(UINT32, nXSrc), |
661 | 0 | WINPR_ASSERTING_INT_CAST(UINT32, nYSrc), palette, FREERDP_FLIP_NONE)) |
662 | 0 | return FALSE; |
663 | | |
664 | 0 | break; |
665 | | |
666 | 0 | default: |
667 | 0 | if (!BitBlt_process(hdcDest, nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, |
668 | 0 | gdi_rop_to_string(rop), palette)) |
669 | 0 | return FALSE; |
670 | | |
671 | 0 | break; |
672 | 0 | } |
673 | | |
674 | 0 | if (!gdi_InvalidateRegion(hdcDest, nXDest, nYDest, nWidth, nHeight)) |
675 | 0 | return FALSE; |
676 | | |
677 | 0 | return TRUE; |
678 | 0 | } |