FreeRDP
Loading...
Searching...
No Matches
color.c
1
21
22#include <freerdp/config.h>
23
24#include <stdio.h>
25#include <string.h>
26#include <stdlib.h>
27
28#include <winpr/crt.h>
29
30#include <freerdp/log.h>
31#include <freerdp/freerdp.h>
32#include <freerdp/primitives.h>
33
34#if defined(WITH_CAIRO)
35#include <cairo.h>
36#endif
37
38#if defined(WITH_SWSCALE)
39#include "image_ffmpeg.h"
40#endif
41
42#include "color.h"
43
44#define TAG FREERDP_TAG("color")
45
46static BOOL freerdp_image_copy_from_pointer_data_int(
47 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
48 UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT xorMask, UINT32 xorMaskLength,
49 const BYTE* WINPR_RESTRICT andMask, UINT32 andMaskLength, UINT32 xorBpp,
50 const gdiPalette* WINPR_RESTRICT palette);
51
52#if defined(WITH_CURSOR_DUMP)
53#include <winpr/path.h>
54#include <winpr/image.h>
55
56WINPR_ATTR_MALLOC(free, 1)
57static char* get_dump_name(char* prefix, size_t len)
58{
59 static uint64_t count = 0;
60 if (_snprintf(prefix, len, "cursor_dump_%08" PRIx64, count++) < 0)
61 return nullptr;
62
63 return GetCombinedPath(CURSOR_DUMP_DIR, prefix);
64}
65
66static void dump_binary_data(FILE* fp, const uint8_t* data, size_t len)
67{
68 if (len > 0)
69 (void)fprintf(fp, "0x%02" PRIx8, data[0]);
70 for (size_t x = 1; x < len; x++)
71 {
72 (void)fprintf(fp, ", 0x%02" PRIx8, data[x]);
73 }
74}
75
76static void dump_uint_data(FILE* fp, const uint32_t* data, size_t len)
77{
78 if (len > 0)
79 (void)fprintf(fp, "0x%08" PRIx32, data[0]);
80 for (size_t x = 1; x < len; x++)
81 {
82 (void)fprintf(fp, ", 0x%08" PRIx32, data[x]);
83 }
84}
85
86static void dump_write_header(const char* path, const char* prefix)
87{
88 char* header = nullptr;
89 size_t headerlen = 0;
90 winpr_asprintf(&header, &headerlen, "%s.h", path);
91 if (!header)
92 return;
93 FILE* fp = fopen(header, "w");
94 free(header);
95
96 if (!fp)
97 return;
98
99 (void)fprintf(fp, "/* FreeRDP cursor dump to use for unit tests\n");
100 (void)fprintf(fp, " * this file was auto generated by %s\n", __func__);
101 (void)fprintf(fp, " * do not modify manually\n");
102 (void)fprintf(fp, " */\n");
103 (void)fprintf(fp, "\n");
104 (void)fprintf(fp, "#pragma once\n");
105 (void)fprintf(fp, "\n");
106 (void)fprintf(fp, "#include <freerdp/codec/color.h>\n");
107 (void)fprintf(fp, "#include <freerdp/graphics.h>\n");
108 (void)fprintf(fp, "\n");
109 (void)fprintf(fp, "extern const gdiPalette %s_palette;\n", prefix);
110 (void)fprintf(fp, "extern const rdpPointer %s_pointer;\n", prefix);
111 (void)fprintf(fp, "extern const uint8_t %s_image_bgra32[];\n", prefix);
112 fclose(fp);
113}
114
115static void dump_write_c_file(const char* path, const char* prefix, UINT32 nXDst, UINT32 nYDst,
116 UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT xorMask,
117 UINT32 xorMaskLength, const BYTE* WINPR_RESTRICT andMask,
118 UINT32 andMaskLength, UINT32 xorBpp, const gdiPalette* palette,
119 const uint8_t* bmp)
120{
121 const uint32_t format = PIXEL_FORMAT_BGRA32;
122 const uint32_t bpp = FreeRDPGetBytesPerPixel(format);
123 const uint32_t step = nWidth * bpp;
124
125 char* header = nullptr;
126 size_t headerlen = 0;
127 winpr_asprintf(&header, &headerlen, "%s.c", path);
128 if (!header)
129 return;
130 FILE* fp = fopen(header, "w");
131 free(header);
132
133 if (fp)
134 {
135 (void)fprintf(fp, "/* FreeRDP cursor dump to use for unit tests\n");
136 (void)fprintf(fp, " * this file was auto generated by %s\n", __func__);
137 (void)fprintf(fp, " * do not modify manually\n");
138 (void)fprintf(fp, " */\n");
139 (void)fprintf(fp, "\n");
140 (void)fprintf(fp, "#include \"%s.h\"\n", prefix);
141 (void)fprintf(fp, "\n");
142
143 (void)fprintf(fp, "static const uint8_t andmask[] = {\n");
144 dump_binary_data(fp, andMask, andMaskLength);
145 (void)fprintf(fp, "};\n");
146 (void)fprintf(fp, "\n");
147
148 (void)fprintf(fp, "static const uint8_t xormask[] = {\n");
149 dump_binary_data(fp, xorMask, xorMaskLength);
150 (void)fprintf(fp, "};\n");
151 (void)fprintf(fp, "\n");
152 (void)fprintf(fp, "const gdiPalette %s_palette = {\n", prefix);
153 if (palette)
154 {
155 (void)fprintf(fp, ".format=%" PRIu32 ",", palette->format);
156 (void)fprintf(fp, ".palette={");
157 dump_uint_data(fp, palette->palette, ARRAYSIZE(palette->palette));
158 (void)fprintf(fp, "}\n");
159 }
160 else
161 (void)fprintf(fp, "0");
162
163 (void)fprintf(fp, "};\n");
164
165 (void)fprintf(fp, "\n");
166 (void)fprintf(fp, "const rdpPointer %s_pointer = {\n", prefix);
167 (void)fprintf(fp, ".size = 0,\n");
168 (void)fprintf(fp, ".New = nullptr,\n");
169 (void)fprintf(fp, ".Free = nullptr,\n");
170 (void)fprintf(fp, ".Set = nullptr,\n");
171 (void)fprintf(fp, ".SetNull = nullptr,\n");
172 (void)fprintf(fp, ".SetDefault = nullptr,\n");
173 (void)fprintf(fp, ".SetPosition = nullptr,\n");
174 (void)fprintf(fp, ".paddingA = {0},\n");
175 (void)fprintf(fp, ".xPos = %" PRIu32 ",\n", nXDst);
176 (void)fprintf(fp, ".yPos = %" PRIu32 ",\n", nYDst);
177 (void)fprintf(fp, ".width = %" PRIu32 ",\n", nWidth);
178 (void)fprintf(fp, ".height = %" PRIu32 ",\n", nHeight);
179 (void)fprintf(fp, ".xorBpp = %" PRIu32 ",\n", xorBpp);
180 (void)fprintf(fp, ".lengthAndMask = ARRAYSIZE(andmask),\n");
181 (void)fprintf(fp, ".lengthXorMask = ARRAYSIZE(xormask),\n");
182 (void)fprintf(fp, ".xorMaskData = xormask,\n");
183 (void)fprintf(fp, ".andMaskData = andmask,\n");
184 (void)fprintf(fp, ".paddingB = {0}\n");
185 (void)fprintf(fp, "};\n");
186 (void)fprintf(fp, "\n");
187 (void)fprintf(fp, "const uint8_t %s_image_bgra32[]={\n", prefix);
188 dump_binary_data(fp, bmp, step * nHeight);
189 (void)fprintf(fp, "};\n");
190 fclose(fp);
191 }
192
193 wImage* img = winpr_image_new();
194 if (img)
195 {
196 img->data = WINPR_CAST_CONST_PTR_AWAY(bmp, BYTE*);
197 img->bitsPerPixel = 32;
198 img->bytesPerPixel = 4;
199 img->height = nHeight;
200 img->width = nWidth;
201 img->scanline = step;
202 img->type = WINPR_IMAGE_PNG;
203 char* imgname = nullptr;
204 size_t imgnamesize = 0;
205 winpr_asprintf(&imgname, &imgnamesize, "%s.png", path);
206 if (imgname)
207 winpr_image_write(img, imgname);
208 free(imgname);
209 winpr_image_free(img, FALSE);
210 }
211}
212
218static void dump_pointer_data(UINT32 nXDst, UINT32 nYDst, UINT32 nWidth, UINT32 nHeight,
219 const BYTE* WINPR_RESTRICT xorMask, UINT32 xorMaskLength,
220 const BYTE* WINPR_RESTRICT andMask, UINT32 andMaskLength,
221 UINT32 xorBpp, const gdiPalette* palette)
222{
223 const uint32_t format = PIXEL_FORMAT_BGRA32;
224 const uint32_t bpp = FreeRDPGetBytesPerPixel(format);
225 const uint32_t step = nWidth * bpp;
226 BYTE* bmp = calloc(step * 2, nHeight);
227 char prefix[64] = WINPR_C_ARRAY_INIT;
228 char* path = get_dump_name(prefix, sizeof(prefix));
229
230 if (!bmp || !path)
231 goto fail;
232
233 if (!freerdp_image_copy_from_pointer_data_int(bmp, format, step, 0, 0, nWidth, nHeight, xorMask,
234 xorMaskLength, andMask, andMaskLength, xorBpp,
235 palette))
236 goto fail;
237
238 dump_write_header(path, prefix);
239 dump_write_c_file(path, prefix, nXDst, nYDst, nWidth, nHeight, xorMask, xorMaskLength, andMask,
240 andMaskLength, xorBpp, palette, bmp);
241
242fail:
243 free(bmp);
244 free(path);
245}
246#endif
247
248#if !defined(WITHOUT_FREERDP_3x_DEPRECATED)
249BYTE* freerdp_glyph_convert(UINT32 width, UINT32 height, const BYTE* WINPR_RESTRICT data)
250{
251 const size_t scanline = (width + 7ull) / 8ull;
252 const size_t required = scanline * height;
253 return freerdp_glyph_convert_ex(width, height, data, required);
254}
255#endif
256
257BYTE* freerdp_glyph_convert_ex(UINT32 width, UINT32 height, const BYTE* WINPR_RESTRICT data,
258 size_t len)
259{
260 /*
261 * converts a 1-bit-per-pixel glyph to a one-byte-per-pixel glyph:
262 * this approach uses a little more memory, but provides faster
263 * means of accessing individual pixels in blitting operations
264 */
265 const size_t scanline = (width + 7ull) / 8ull;
266 const size_t required = scanline * height;
267 if (len < required)
268 return nullptr;
269
270 if ((len == 0) || (width == 0) || (height == 0))
271 return nullptr;
272
273 WINPR_ASSERT(data);
274
275 BYTE* dstData = (BYTE*)winpr_aligned_calloc(width, height, 16);
276
277 if (!dstData)
278 return nullptr;
279
280 ZeroMemory(dstData, 1ULL * width * height);
281 BYTE* dstp = dstData;
282
283 for (UINT32 y = 0; y < height; y++)
284 {
285 const BYTE* srcp = &data[1ull * y * scanline];
286
287 for (UINT32 x = 0; x < width; x++)
288 {
289 if ((*srcp & (0x80 >> (x % 8))) != 0)
290 *dstp = 0xFF;
291
292 dstp++;
293
294 if (((x + 1) % 8 == 0) && x != 0)
295 srcp++;
296 }
297 }
298
299 return dstData;
300}
301
302BOOL freerdp_image_copy_from_monochrome(BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
303 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
304 UINT32 nHeight, const BYTE* WINPR_RESTRICT pSrcData,
305 UINT32 backColor, UINT32 foreColor,
306 const gdiPalette* WINPR_RESTRICT palette)
307{
308 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
309 return FALSE;
310 const UINT32 dstBytesPerPixel = FreeRDPGetBytesPerPixel(DstFormat);
311
312 if (!pDstData || !pSrcData || !palette)
313 return FALSE;
314
315 if (nDstStep == 0)
316 nDstStep = dstBytesPerPixel * nWidth;
317
318 WINPR_ASSERT(1ull * nXDst + nWidth <= UINT32_MAX);
319 WINPR_ASSERT(nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= nXDst + nWidth);
320
321 const UINT32 monoStep = (nWidth + 7) / 8;
322
323 for (size_t y = 0; y < nHeight; y++)
324 {
325 BYTE* pDstLine = &pDstData[((nYDst + y) * nDstStep)];
326 UINT32 monoBit = 0x80;
327 const BYTE* monoBits = &pSrcData[monoStep * y];
328
329 for (size_t x = 0; x < nWidth; x++)
330 {
331 BYTE* pDstPixel = &pDstLine[((nXDst + x) * FreeRDPGetBytesPerPixel(DstFormat))];
332 BOOL monoPixel = (*monoBits & monoBit) != 0;
333
334 if (!(monoBit >>= 1))
335 {
336 monoBits++;
337 monoBit = 0x80;
338 }
339
340 if (monoPixel)
341 {
342 if (!FreeRDPWriteColor_int(pDstPixel, DstFormat, backColor))
343 return FALSE;
344 }
345 else if (!FreeRDPWriteColor_int(pDstPixel, DstFormat, foreColor))
346 return FALSE;
347 }
348 }
349
350 return TRUE;
351}
352
353static inline UINT32 freerdp_image_inverted_pointer_color(UINT32 x, UINT32 y, UINT32 format)
354{
364 BYTE fill = (x + y) & 1 ? 0x00 : 0xFF;
365 return FreeRDPGetColor(format, fill, fill, fill, 0xFF);
366}
367
368/*
369 * DIB color palettes are arrays of RGBQUAD structs with colors in BGRX format.
370 * They are used only by 1, 2, 4, and 8-bit bitmaps.
371 */
372static void fill_gdi_palette_for_icon(const BYTE* colorTable, UINT16 cbColorTable,
373 gdiPalette* palette)
374{
375 WINPR_ASSERT(palette);
376
377 palette->format = PIXEL_FORMAT_BGRX32;
378 ZeroMemory(palette->palette, sizeof(palette->palette));
379
380 if (!cbColorTable)
381 return;
382
383 if ((cbColorTable % 4 != 0) || (cbColorTable / 4 > 256))
384 {
385 WLog_WARN(TAG, "weird palette size: %u", cbColorTable);
386 return;
387 }
388
389 for (UINT16 i = 0; i < cbColorTable / 4; i++)
390 {
391 palette->palette[i] = FreeRDPReadColor_int(&colorTable[4ULL * i], palette->format);
392 }
393}
394
395static inline UINT32 div_ceil(UINT32 a, UINT32 b)
396{
397 return (a + (b - 1)) / b;
398}
399
400static inline UINT32 round_up(UINT32 a, UINT32 b)
401{
402 return b * div_ceil(a, b);
403}
404
405BOOL freerdp_image_copy_from_icon_data(BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
406 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, UINT16 nWidth,
407 UINT16 nHeight, const BYTE* WINPR_RESTRICT bitsColor,
408 UINT16 cbBitsColor, const BYTE* WINPR_RESTRICT bitsMask,
409 UINT16 cbBitsMask, const BYTE* WINPR_RESTRICT colorTable,
410 UINT16 cbColorTable, UINT32 bpp)
411{
412 DWORD format = 0;
413 gdiPalette palette;
414
415 if (!pDstData || !bitsColor)
416 return FALSE;
417
418 if ((nWidth == 0) || (nHeight == 0))
419 return TRUE;
420 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
421 return FALSE;
422
423 WINPR_ASSERT((nDstStep == 0) || (nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= nWidth));
424
425 /*
426 * Color formats used by icons are DIB bitmap formats (2-bit format
427 * is not used by MS-RDPERP). Note that 16-bit is RGB555, not RGB565,
428 * and that 32-bit format uses BGRA order.
429 */
430 switch (bpp)
431 {
432 case 1:
433 case 4:
434 /*
435 * These formats are not supported by freerdp_image_copy().
436 * PIXEL_FORMAT_MONO and PIXEL_FORMAT_A4 are *not* correct
437 * color formats for this. Please fix freerdp_image_copy()
438 * if you came here to fix a broken icon of some weird app
439 * that still uses 1 or 4bpp format in the 21st century.
440 */
441 WLog_WARN(TAG, "1bpp and 4bpp icons are not supported");
442 return FALSE;
443
444 case 8:
445 format = PIXEL_FORMAT_RGB8;
446 break;
447
448 case 16:
449 format = PIXEL_FORMAT_RGB15;
450 break;
451
452 case 24:
453 format = PIXEL_FORMAT_RGB24;
454 break;
455
456 case 32:
457 format = PIXEL_FORMAT_BGRA32;
458 break;
459
460 default:
461 WLog_WARN(TAG, "invalid icon bpp: %" PRIu32, bpp);
462 return FALSE;
463 }
464
465 /* Ensure we have enough source data bytes for image copy. */
466 if ((1ul * cbBitsColor) / nHeight < (1ul * FreeRDPGetBytesPerPixel(format) * nWidth))
467 {
468 WLog_ERR(TAG,
469 "cbBitsColor{%" PRIu16 "} < nWidth{%" PRIu16 "} * nHeight{%" PRIu16
470 "} * bpp{%" PRIu32 "}",
471 cbBitsColor, nWidth, nHeight, FreeRDPGetBytesPerPixel(format));
472 return FALSE;
473 }
474
475 fill_gdi_palette_for_icon(colorTable, cbColorTable, &palette);
476 if (!freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight,
477 bitsColor, format, 0, 0, 0, &palette, FREERDP_FLIP_VERTICAL))
478 return FALSE;
479
480 /* apply alpha mask, skip if we already got alpha in the bitmap */
481 if (FreeRDPColorHasAlpha(DstFormat) && !FreeRDPColorHasAlpha(format) && (cbBitsMask > 0))
482 {
483 BYTE* dstBuf = pDstData;
484 UINT32 dstBpp = FreeRDPGetBytesPerPixel(DstFormat);
485
486 /*
487 * Each byte encodes 8 adjacent pixels (with LSB padding as needed).
488 * And due to hysterical raisins, stride of DIB bitmaps must be
489 * a multiple of 4 bytes.
490 */
491 const size_t stride = round_up(div_ceil(nWidth, 8), 4);
492 const size_t maskSize = stride * (nHeight - 1ULL) + div_ceil(nWidth, 8);
493 if (cbBitsMask < maskSize)
494 {
495 WLog_ERR(TAG, "cbBitsMask{%" PRIu32 "} < required mask size{%" PRIuz "}", cbBitsMask,
496 maskSize);
497 return FALSE;
498 }
499
500 for (UINT32 y = 0; y < nHeight; y++)
501 {
502 const BYTE* maskByte = &bitsMask[stride * (nHeight - 1ULL - y)];
503 BYTE nextBit = 0x80;
504
505 for (UINT32 x = 0; x < nWidth; x++)
506 {
507 BYTE r = 0;
508 BYTE g = 0;
509 BYTE b = 0;
510 BYTE alpha = (*maskByte & nextBit) ? 0x00 : 0xFF;
511
512 /* read color back, add alpha and write it back */
513 UINT32 color = FreeRDPReadColor_int(dstBuf, DstFormat);
514 FreeRDPSplitColor(color, DstFormat, &r, &g, &b, nullptr, &palette);
515 color = FreeRDPGetColor(DstFormat, r, g, b, alpha);
516 if (!FreeRDPWriteColor_int(dstBuf, DstFormat, color))
517 return FALSE;
518
519 nextBit >>= 1;
520 dstBuf += dstBpp;
521 if (!nextBit)
522 {
523 nextBit = 0x80;
524 maskByte++;
525 }
526 }
527 }
528 }
529
530 return TRUE;
531}
532
533static BOOL freerdp_image_copy_from_pointer_data_1bpp(
534 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
535 UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT xorMask, UINT32 xorMaskLength,
536 const BYTE* WINPR_RESTRICT andMask, UINT32 andMaskLength, UINT32 xorBpp)
537{
538 BOOL vFlip = 0;
539 UINT32 xorStep = 0;
540 UINT32 andStep = 0;
541 UINT32 xorBit = 0;
542 UINT32 andBit = 0;
543 UINT32 xorPixel = 0;
544 UINT32 andPixel = 0;
545
546 vFlip = (xorBpp != 1);
547 andStep = (nWidth + 7) / 8;
548 andStep += (andStep % 2);
549
550 if (!xorMask || (xorMaskLength == 0))
551 return FALSE;
552 if (!andMask || (andMaskLength == 0))
553 return FALSE;
554
555 xorStep = (nWidth + 7) / 8;
556 xorStep += (xorStep % 2);
557
558 if (xorStep * nHeight > xorMaskLength)
559 return FALSE;
560
561 if (andStep * nHeight > andMaskLength)
562 return FALSE;
563
564 for (UINT32 y = 0; y < nHeight; y++)
565 {
566 const BYTE* andBits = nullptr;
567 const BYTE* xorBits = nullptr;
568 BYTE* pDstPixel = &pDstData[((1ULL * nYDst + y) * nDstStep) +
569 (1ULL * nXDst * FreeRDPGetBytesPerPixel(DstFormat))];
570 xorBit = andBit = 0x80;
571
572 if (!vFlip)
573 {
574 xorBits = &xorMask[1ULL * xorStep * y];
575 andBits = &andMask[1ULL * andStep * y];
576 }
577 else
578 {
579 xorBits = &xorMask[1ULL * xorStep * (nHeight - y - 1)];
580 andBits = &andMask[1ULL * andStep * (nHeight - y - 1)];
581 }
582
583 for (UINT32 x = 0; x < nWidth; x++)
584 {
585 UINT32 color = 0;
586 xorPixel = (*xorBits & xorBit) ? 1 : 0;
587
588 if (!(xorBit >>= 1))
589 {
590 xorBits++;
591 xorBit = 0x80;
592 }
593
594 andPixel = (*andBits & andBit) ? 1 : 0;
595
596 if (!(andBit >>= 1))
597 {
598 andBits++;
599 andBit = 0x80;
600 }
601
602 if (!andPixel && !xorPixel)
603 color = FreeRDPGetColor(DstFormat, 0, 0, 0, 0xFF); /* black */
604 else if (!andPixel && xorPixel)
605 color = FreeRDPGetColor(DstFormat, 0xFF, 0xFF, 0xFF, 0xFF); /* white */
606 else if (andPixel && !xorPixel)
607 color = FreeRDPGetColor(DstFormat, 0, 0, 0, 0); /* transparent */
608 else if (andPixel && xorPixel)
609 color = freerdp_image_inverted_pointer_color(x, y, DstFormat); /* inverted */
610
611 if (!FreeRDPWriteColor_int(pDstPixel, DstFormat, color))
612 return FALSE;
613 pDstPixel += FreeRDPGetBytesPerPixel(DstFormat);
614 }
615 }
616
617 return TRUE;
618}
619
620/* 8bpp data is resolved through the palette for every single pixel, so check the palette once
621 * per image instead of logging an error for each pixel with an unusable palette. */
622static BOOL freerdp_image_palette_is_valid(const gdiPalette* WINPR_RESTRICT palette)
623{
624 if (!palette)
625 {
626 WLog_ERR(TAG, "%s data requires a palette", FreeRDPGetColorFormatName(PIXEL_FORMAT_RGB8));
627 return FALSE;
628 }
629
630 if (FreeRDPGetBitsPerPixel(palette->format) <= 8)
631 {
632 WLog_ERR(TAG, "%s data requires a palette with a true color format, got %s",
633 FreeRDPGetColorFormatName(PIXEL_FORMAT_RGB8),
634 FreeRDPGetColorFormatName(palette->format));
635 return FALSE;
636 }
637
638 return TRUE;
639}
640
641static BOOL freerdp_image_copy_from_pointer_data_xbpp(
642 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
643 UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT xorMask, UINT32 xorMaskLength,
644 const BYTE* WINPR_RESTRICT andMask, UINT32 andMaskLength, UINT32 xorBpp,
645 const gdiPalette* palette)
646{
647 BOOL vFlip = 0;
648 size_t xorStep = 0;
649 size_t andStep = 0;
650 UINT32 andBit = 0;
651 UINT32 xorPixel = 0;
652 UINT32 andPixel = 0;
653 UINT32 dstBitsPerPixel = 0;
654 UINT32 xorBytesPerPixel = 0;
655 dstBitsPerPixel = FreeRDPGetBitsPerPixel(DstFormat);
656
657 vFlip = (xorBpp != 1);
658 andStep = (nWidth + 7) / 8;
659 andStep += (andStep % 2);
660
661 if (!xorMask || (xorMaskLength == 0))
662 return FALSE;
663
664 xorBytesPerPixel = xorBpp >> 3;
665 xorStep = 1ULL * nWidth * xorBytesPerPixel;
666 xorStep += (xorStep % 2);
667
668 if (xorBpp == 8 && !freerdp_image_palette_is_valid(palette))
669 {
670 WLog_ERR(TAG, "invalid palette in conversion from %" PRIu32 " bpp to %" PRIu32 " bpp",
671 xorBpp, dstBitsPerPixel);
672 return FALSE;
673 }
674
675 if (xorStep * nHeight > xorMaskLength)
676 return FALSE;
677
678 if (andMask)
679 {
680 if (andStep * nHeight > andMaskLength)
681 return FALSE;
682 }
683
684 for (UINT32 y = 0; y < nHeight; y++)
685 {
686 const BYTE* xorBits = nullptr;
687 const BYTE* andBits = nullptr;
688 BYTE* pDstPixel = &pDstData[((1ULL * nYDst + y) * nDstStep) +
689 (1ULL * nXDst * FreeRDPGetBytesPerPixel(DstFormat))];
690 andBit = 0x80;
691
692 if (!vFlip)
693 {
694 if (andMask)
695 andBits = &andMask[andStep * y];
696
697 xorBits = &xorMask[xorStep * y];
698 }
699 else
700 {
701 if (andMask)
702 andBits = &andMask[1ULL * andStep * (nHeight - y - 1)];
703
704 xorBits = &xorMask[1ULL * xorStep * (nHeight - y - 1)];
705 }
706
707 for (UINT32 x = 0; x < nWidth; x++)
708 {
709 UINT32 pixelFormat = 0;
710 UINT32 color = 0;
711
712 andPixel = 0;
713
714 if (andMask)
715 {
716 andPixel = (*andBits & andBit) ? 1 : 0;
717
718 if (!(andBit >>= 1))
719 {
720 andBits++;
721 andBit = 0x80;
722 }
723 }
724
725 if (xorBpp == 32)
726 {
727 pixelFormat = PIXEL_FORMAT_BGRA32;
728 xorPixel = FreeRDPReadColor_int(xorBits, pixelFormat);
729 }
730 else if (xorBpp == 16)
731 {
732 pixelFormat = PIXEL_FORMAT_RGB15;
733 xorPixel = FreeRDPReadColor_int(xorBits, pixelFormat);
734 }
735 else if (xorBpp == 8)
736 {
737 pixelFormat = palette->format;
738 xorPixel = palette->palette[xorBits[0]];
739 }
740 else
741 {
742 pixelFormat = PIXEL_FORMAT_BGR24;
743 xorPixel = FreeRDPReadColor_int(xorBits, pixelFormat);
744
745 if (andPixel)
746 {
747 if (xorPixel == 0x00FFFFFFUL)
748 xorPixel |= 0xFF000000UL;
749 else
750 xorPixel = 0;
751 }
752 else
753 xorPixel |= 0xFF000000UL;
754
755 // We need a transparent default alpha value, so use a different format here
756 pixelFormat = PIXEL_FORMAT_ABGR32;
757 }
758
759 xorPixel = FreeRDPConvertColor(xorPixel, pixelFormat, PIXEL_FORMAT_ARGB32, palette);
760 xorBits += xorBytesPerPixel;
761
762 if (andPixel)
763 {
764 if (xorPixel == 0xFF000000UL) /* black -> transparent */
765 xorPixel = 0x00000000;
766 else if (xorPixel == 0xFFFFFFFFUL) /* white -> inverted */
767 xorPixel = freerdp_image_inverted_pointer_color(x, y, PIXEL_FORMAT_ARGB32);
768 }
769
770 color = FreeRDPConvertColor(xorPixel, PIXEL_FORMAT_ARGB32, DstFormat, palette);
771 if (!FreeRDPWriteColor_int(pDstPixel, DstFormat, color))
772 return FALSE;
773 pDstPixel += FreeRDPGetBytesPerPixel(DstFormat);
774 }
775 }
776
777 return TRUE;
778}
779
787
788BOOL freerdp_image_copy_from_pointer_data_int(
789 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
790 UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT xorMask, UINT32 xorMaskLength,
791 const BYTE* WINPR_RESTRICT andMask, UINT32 andMaskLength, UINT32 xorBpp,
792 const gdiPalette* WINPR_RESTRICT palette)
793{
794 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
795 return FALSE;
796
797 UINT32 dstBitsPerPixel = FreeRDPGetBitsPerPixel(DstFormat);
798 UINT32 dstBytesPerPixel = FreeRDPGetBytesPerPixel(DstFormat);
799
800 WINPR_ASSERT(1ull * nXDst + nWidth <= UINT32_MAX);
801 if (nDstStep <= 0)
802 nDstStep = dstBytesPerPixel * (1ull * nXDst + nWidth);
803
804 WINPR_ASSERT(nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= (1ull * nXDst + nWidth));
805
806 for (UINT32 y = nYDst; y < nHeight; y++)
807 {
808 BYTE* WINPR_RESTRICT pDstLine = &pDstData[y * nDstStep + nXDst * dstBytesPerPixel];
809 memset(pDstLine, 0, 1ull * dstBytesPerPixel * (nWidth - nXDst));
810 }
811
812 switch (xorBpp)
813 {
814 case 1:
815 return freerdp_image_copy_from_pointer_data_1bpp(
816 pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, xorMask,
817 xorMaskLength, andMask, andMaskLength, xorBpp);
818
819 case 8:
820 case 16:
821 case 24:
822 case 32:
823 return freerdp_image_copy_from_pointer_data_xbpp(
824 pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, xorMask,
825 xorMaskLength, andMask, andMaskLength, xorBpp, palette);
826
827 default:
828 WLog_ERR(TAG, "failed to convert from %" PRIu32 " bpp to %" PRIu32 " bpp", xorBpp,
829 dstBitsPerPixel);
830 return FALSE;
831 }
832}
833
834BOOL freerdp_image_copy_from_pointer_data(BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
835 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
836 UINT32 nWidth, UINT32 nHeight,
837 const BYTE* WINPR_RESTRICT xorMask, UINT32 xorMaskLength,
838 const BYTE* WINPR_RESTRICT andMask, UINT32 andMaskLength,
839 UINT32 xorBpp, const gdiPalette* WINPR_RESTRICT palette)
840{
841#if defined(WITH_CURSOR_DUMP)
842 dump_pointer_data(nXDst, nYDst, nWidth, nHeight, xorMask, xorMaskLength, andMask, andMaskLength,
843 xorBpp, palette);
844#endif
845 return freerdp_image_copy_from_pointer_data_int(pDstData, DstFormat, nDstStep, nXDst, nYDst,
846 nWidth, nHeight, xorMask, xorMaskLength,
847 andMask, andMaskLength, xorBpp, palette);
848}
849
850static inline BOOL overlapping(const BYTE* pDstData, UINT32 nYDst, UINT32 nDstStep,
851 const BYTE* pSrcData, UINT32 nYSrc, UINT32 nSrcStep, UINT32 nHeight)
852{
853 const uintptr_t src = (uintptr_t)pSrcData;
854 const uintptr_t srcstart = src + 1ULL * nSrcStep * nYSrc;
855 const uintptr_t srcend = srcstart + 1ULL * nSrcStep * nHeight;
856 const uintptr_t dst = (uintptr_t)pDstData;
857 const uintptr_t dststart = dst + 1ULL * nDstStep * nYDst;
858 const uintptr_t dstend = dststart + 1ULL * nDstStep * nHeight;
859
860 WINPR_ASSERT(nDstStep > 0);
861 WINPR_ASSERT(nSrcStep > 0);
862 if ((dststart >= srcstart) && (dststart < srcend))
863 return TRUE;
864
865 if ((dstend > srcstart) && (dstend <= srcend))
866 return TRUE;
867
868 return FALSE;
869}
870
871static inline BOOL freerdp_image_copy_bgr24_bgrx32(BYTE* WINPR_RESTRICT pDstData, UINT32 nDstStep,
872 UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
873 UINT32 nHeight,
874 const BYTE* WINPR_RESTRICT pSrcData,
875 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
876 int64_t srcVMultiplier, int64_t srcVOffset,
877 int64_t dstVMultiplier, int64_t dstVOffset)
878{
879
880 const int64_t srcByte = 3;
881 const int64_t dstByte = 4;
882
883 for (int64_t y = 0; y < nHeight; y++)
884 {
885 const BYTE* WINPR_RESTRICT srcLine =
886 &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
887 BYTE* WINPR_RESTRICT dstLine =
888 &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
889
890 for (int64_t x = 0; x < nWidth; x++)
891 {
892 dstLine[(x + nXDst) * dstByte + 0] = srcLine[(x + nXSrc) * srcByte + 0];
893 dstLine[(x + nXDst) * dstByte + 1] = srcLine[(x + nXSrc) * srcByte + 1];
894 dstLine[(x + nXDst) * dstByte + 2] = srcLine[(x + nXSrc) * srcByte + 2];
895 }
896 }
897
898 return TRUE;
899}
900
901static inline BOOL freerdp_image_copy_bgrx32_bgrx32(BYTE* WINPR_RESTRICT pDstData, UINT32 nDstStep,
902 UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
903 UINT32 nHeight,
904 const BYTE* WINPR_RESTRICT pSrcData,
905 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
906 int64_t srcVMultiplier, int64_t srcVOffset,
907 int64_t dstVMultiplier, int64_t dstVOffset)
908{
909
910 const int64_t srcByte = 4;
911 const int64_t dstByte = 4;
912
913 for (int64_t y = 0; y < nHeight; y++)
914 {
915 const BYTE* WINPR_RESTRICT srcLine =
916 &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
917 BYTE* WINPR_RESTRICT dstLine =
918 &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
919
920 for (int64_t x = 0; x < nWidth; x++)
921 {
922 dstLine[(x + nXDst) * dstByte + 0] = srcLine[(x + nXSrc) * srcByte + 0];
923 dstLine[(x + nXDst) * dstByte + 1] = srcLine[(x + nXSrc) * srcByte + 1];
924 dstLine[(x + nXDst) * dstByte + 2] = srcLine[(x + nXSrc) * srcByte + 2];
925 }
926 }
927
928 return TRUE;
929}
930
931static inline BOOL freerdp_image_copy_generic(
932 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
933 UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcFormat,
934 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc, const gdiPalette* WINPR_RESTRICT palette,
935 int64_t srcVMultiplier, int64_t srcVOffset, int64_t dstVMultiplier, int64_t dstVOffset)
936{
937
938 const int64_t srcByte = 4;
939 const int64_t dstByte = 4;
940
941 for (int64_t y = 0; y < nHeight; y++)
942 {
943 const BYTE* WINPR_RESTRICT srcLine =
944 &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
945 BYTE* WINPR_RESTRICT dstLine =
946 &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
947
948 UINT32 color = FreeRDPReadColor_int(&srcLine[nXSrc * srcByte], SrcFormat);
949 UINT32 oldColor = color;
950 UINT32 dstColor = FreeRDPConvertColor(color, SrcFormat, DstFormat, palette);
951 if (!FreeRDPWriteColorIgnoreAlpha_int(&dstLine[nXDst * dstByte], DstFormat, dstColor))
952 return FALSE;
953
954 for (int64_t x = 1; x < nWidth; x++)
955 {
956 color = FreeRDPReadColor_int(&srcLine[(x + nXSrc) * srcByte], SrcFormat);
957 if (color == oldColor)
958 {
959 if (!FreeRDPWriteColorIgnoreAlpha_int(&dstLine[(x + nXDst) * dstByte], DstFormat,
960 dstColor))
961 return FALSE;
962 }
963 else
964 {
965 oldColor = color;
966 dstColor = FreeRDPConvertColor(color, SrcFormat, DstFormat, palette);
967 if (!FreeRDPWriteColorIgnoreAlpha_int(&dstLine[(x + nXDst) * dstByte], DstFormat,
968 dstColor))
969 return FALSE;
970 }
971 }
972 }
973
974 return TRUE;
975}
976
977static inline BOOL freerdp_image_copy_no_overlap_dst_alpha(
978 BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
979 UINT32 nWidth, UINT32 nHeight, const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
980 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc, const gdiPalette* WINPR_RESTRICT palette,
981 int64_t srcVMultiplier, int64_t srcVOffset, int64_t dstVMultiplier, int64_t dstVOffset)
982{
983 WINPR_ASSERT(pDstData);
984 WINPR_ASSERT(pSrcData);
985
986 switch (SrcFormat)
987 {
988 case PIXEL_FORMAT_BGR24:
989 switch (DstFormat)
990 {
991 case PIXEL_FORMAT_BGRX32:
992 case PIXEL_FORMAT_BGRA32:
993 return freerdp_image_copy_bgr24_bgrx32(
994 pDstData, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, nSrcStep,
995 nXSrc, nYSrc, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
996 default:
997 break;
998 }
999 break;
1000 case PIXEL_FORMAT_BGRX32:
1001 case PIXEL_FORMAT_BGRA32:
1002 switch (DstFormat)
1003 {
1004 case PIXEL_FORMAT_BGRX32:
1005 case PIXEL_FORMAT_BGRA32:
1006 return freerdp_image_copy_bgrx32_bgrx32(
1007 pDstData, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, nSrcStep,
1008 nXSrc, nYSrc, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
1009 default:
1010 break;
1011 }
1012 break;
1013 default:
1014 break;
1015 }
1016
1017 return freerdp_image_copy_generic(pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight,
1018 pSrcData, SrcFormat, nSrcStep, nXSrc, nYSrc, palette,
1019 srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
1020}
1021
1022BOOL freerdp_image_copy_overlap(BYTE* pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst,
1023 UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, const BYTE* pSrcData,
1024 DWORD SrcFormat, UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
1025 const gdiPalette* WINPR_RESTRICT palette, UINT32 flags)
1026{
1027 const UINT32 dstByte = FreeRDPGetBytesPerPixel(DstFormat);
1028 const UINT32 srcByte = FreeRDPGetBytesPerPixel(SrcFormat);
1029 const UINT32 copyDstWidth = nWidth * dstByte;
1030 const UINT32 xSrcOffset = nXSrc * srcByte;
1031 const UINT32 xDstOffset = nXDst * dstByte;
1032 const BOOL vSrcVFlip = (flags & FREERDP_FLIP_VERTICAL) != 0;
1033 int64_t srcVOffset = 0;
1034 int64_t srcVMultiplier = 1;
1035 int64_t dstVOffset = 0;
1036 int64_t dstVMultiplier = 1;
1037
1038 if ((nWidth == 0) || (nHeight == 0))
1039 return TRUE;
1040
1041 if (FreeRDPGetBitsPerPixel(SrcFormat) == 0)
1042 return FALSE;
1043 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
1044 return FALSE;
1045 if ((SrcFormat == PIXEL_FORMAT_RGB8) && (SrcFormat != DstFormat) &&
1046 !freerdp_image_palette_is_valid(palette))
1047 return FALSE;
1048
1049 WINPR_ASSERT(1ull * nXDst + nWidth <= UINT32_MAX);
1050 WINPR_ASSERT(1ull * nXSrc + nWidth <= UINT32_MAX);
1051 WINPR_ASSERT(nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= nXDst + nWidth);
1052 WINPR_ASSERT(nSrcStep / FreeRDPGetBytesPerPixel(SrcFormat) >= nXSrc + nWidth);
1053 WINPR_ASSERT(overlapping(pDstData, nYDst, nDstStep, pSrcData, nYSrc, nSrcStep, nHeight));
1054
1055 if ((nWidth == 0) || (nHeight == 0))
1056 return TRUE;
1057
1058 if ((nHeight > INT32_MAX) || (nWidth > INT32_MAX))
1059 return FALSE;
1060
1061 if (!pDstData || !pSrcData)
1062 return FALSE;
1063
1064 if (nDstStep == 0)
1065 nDstStep = (nXDst + nWidth) * FreeRDPGetBytesPerPixel(DstFormat);
1066
1067 if (nSrcStep == 0)
1068 nSrcStep = (nXSrc + nWidth) * FreeRDPGetBytesPerPixel(SrcFormat);
1069
1070 if (vSrcVFlip)
1071 {
1072 srcVOffset = (nHeight - 1ll) * nSrcStep;
1073 srcVMultiplier = -1;
1074 }
1075
1076 if (((flags & FREERDP_KEEP_DST_ALPHA) != 0) && FreeRDPColorHasAlpha(DstFormat))
1077 {
1078 return freerdp_image_copy_no_overlap_dst_alpha(
1079 pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, SrcFormat,
1080 nSrcStep, nXSrc, nYSrc, palette, srcVMultiplier, srcVOffset, dstVMultiplier,
1081 dstVOffset);
1082 }
1083 else if (FreeRDPAreColorFormatsEqualNoAlpha_int(SrcFormat, DstFormat))
1084 {
1085 /* Copy down */
1086 if (nYDst < nYSrc)
1087 {
1088 for (int64_t y = 0; y < nHeight; y++)
1089 {
1090 const BYTE* srcLine =
1091 &pSrcData[(y + nYSrc) * nSrcStep * srcVMultiplier + srcVOffset];
1092 BYTE* dstLine = &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
1093 memcpy(&dstLine[xDstOffset], &srcLine[xSrcOffset], copyDstWidth);
1094 }
1095 }
1096 /* Copy up */
1097 else if (nYDst > nYSrc)
1098 {
1099 for (int64_t y = nHeight - 1; y >= 0; y--)
1100 {
1101 const BYTE* srcLine =
1102 &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
1103 BYTE* dstLine = &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
1104 memcpy(&dstLine[xDstOffset], &srcLine[xSrcOffset], copyDstWidth);
1105 }
1106 }
1107 /* Copy left */
1108 else if (nXSrc > nXDst)
1109 {
1110 for (int64_t y = 0; y < nHeight; y++)
1111 {
1112 const BYTE* srcLine =
1113 &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
1114 BYTE* dstLine = &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
1115 memmove(&dstLine[xDstOffset], &srcLine[xSrcOffset], copyDstWidth);
1116 }
1117 }
1118 /* Copy right */
1119 else if (nXSrc < nXDst)
1120 {
1121 for (int64_t y = nHeight - 1; y >= 0; y--)
1122 {
1123 const BYTE* srcLine =
1124 &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
1125 BYTE* dstLine = &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
1126 memmove(&dstLine[xDstOffset], &srcLine[xSrcOffset], copyDstWidth);
1127 }
1128 }
1129 /* Source and destination are equal... */
1130 else
1131 {
1132 }
1133 }
1134 else
1135 {
1136 for (int64_t y = 0; y < nHeight; y++)
1137 {
1138 const BYTE* srcLine = &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
1139 BYTE* dstLine = &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
1140
1141 UINT32 color = FreeRDPReadColor_int(&srcLine[1ULL * nXSrc * srcByte], SrcFormat);
1142 UINT32 oldColor = color;
1143 UINT32 dstColor = FreeRDPConvertColor(color, SrcFormat, DstFormat, palette);
1144 if (!FreeRDPWriteColor_int(&dstLine[1ULL * nXDst * dstByte], DstFormat, dstColor))
1145 return FALSE;
1146
1147 for (int64_t x = 1; x < nWidth; x++)
1148 {
1149 color = FreeRDPReadColor_int(&srcLine[(x + nXSrc) * srcByte], SrcFormat);
1150 if (color == oldColor)
1151 {
1152 if (!FreeRDPWriteColor_int(&dstLine[(x + nXDst) * dstByte], DstFormat,
1153 dstColor))
1154 return FALSE;
1155 }
1156 else
1157 {
1158 oldColor = color;
1159 dstColor = FreeRDPConvertColor(color, SrcFormat, DstFormat, palette);
1160 if (!FreeRDPWriteColor_int(&dstLine[(x + nXDst) * dstByte], DstFormat,
1161 dstColor))
1162 return FALSE;
1163 }
1164 }
1165 }
1166 }
1167
1168 return TRUE;
1169}
1170
1171BOOL freerdp_image_copy(BYTE* pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst,
1172 UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, const BYTE* pSrcData,
1173 DWORD SrcFormat, UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
1174 const gdiPalette* WINPR_RESTRICT palette, UINT32 flags)
1175{
1176 if ((nHeight > INT32_MAX) || (nWidth > INT32_MAX))
1177 return FALSE;
1178
1179 if (!pDstData || !pSrcData)
1180 return FALSE;
1181
1182 if ((nWidth == 0) || (nHeight == 0))
1183 return TRUE;
1184
1185 if (FreeRDPGetBitsPerPixel(SrcFormat) == 0)
1186 return FALSE;
1187 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
1188 return FALSE;
1189
1190 WINPR_ASSERT(1ull * nXDst + nWidth <= UINT32_MAX);
1191 WINPR_ASSERT(1ull * nXSrc + nWidth <= UINT32_MAX);
1192 if (nDstStep == 0)
1193 nDstStep = (nXDst + nWidth) * FreeRDPGetBytesPerPixel(DstFormat);
1194
1195 if (nSrcStep == 0)
1196 nSrcStep = (nXSrc + nWidth) * FreeRDPGetBytesPerPixel(SrcFormat);
1197
1198 WINPR_ASSERT(nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= nXDst + nWidth);
1199 WINPR_ASSERT(nSrcStep / FreeRDPGetBytesPerPixel(SrcFormat) >= nXSrc + nWidth);
1200
1201 const BOOL ovl = overlapping(pDstData, nYDst, nDstStep, pSrcData, nYSrc, nSrcStep, nHeight);
1202 if (ovl)
1203 return freerdp_image_copy_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth,
1204 nHeight, pSrcData, SrcFormat, nSrcStep, nXSrc, nYSrc,
1205 palette, flags);
1206 return freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth,
1207 nHeight, pSrcData, SrcFormat, nSrcStep, nXSrc, nYSrc,
1208 palette, flags);
1209}
1210
1211BOOL freerdp_image_fill(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep,
1212 UINT32 nXDst, UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, UINT32 color)
1213{
1214 if ((nWidth == 0) || (nHeight == 0))
1215 return TRUE;
1216
1217 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
1218 return FALSE;
1219
1220 const UINT32 bpp = FreeRDPGetBytesPerPixel(DstFormat);
1221 BYTE* WINPR_RESTRICT pFirstDstLine = nullptr;
1222 BYTE* WINPR_RESTRICT pFirstDstLineXOffset = nullptr;
1223
1224 WINPR_ASSERT(1ull * nXDst + nWidth <= UINT32_MAX);
1225 if (nDstStep == 0)
1226 nDstStep = (nXDst + nWidth) * FreeRDPGetBytesPerPixel(DstFormat);
1227
1228 WINPR_ASSERT(nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= nXDst + nWidth);
1229 pFirstDstLine = &pDstData[1ULL * nYDst * nDstStep];
1230 pFirstDstLineXOffset = &pFirstDstLine[1ULL * nXDst * bpp];
1231
1232 for (size_t x = 0; x < nWidth; x++)
1233 {
1234 BYTE* pDst = &pFirstDstLine[(x + nXDst) * bpp];
1235 if (!FreeRDPWriteColor_int(pDst, DstFormat, color))
1236 return FALSE;
1237 }
1238
1239 for (size_t y = 1; y < nHeight; y++)
1240 {
1241 BYTE* pDstLine = &pDstData[(y + nYDst) * nDstStep + 1ULL * nXDst * bpp];
1242 memcpy(pDstLine, pFirstDstLineXOffset, 1ull * nWidth * bpp);
1243 }
1244
1245 return TRUE;
1246}
1247
1248BOOL freerdp_image_fill_ex(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep,
1249 UINT32 nXDst, UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, UINT32 color,
1250 UINT32 flags)
1251{
1252 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
1253 return FALSE;
1254
1255 WINPR_ASSERT(pDstData);
1256 WINPR_ASSERT(1ull * nXDst + nWidth <= UINT32_MAX);
1257 WINPR_ASSERT(nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= nXDst + nWidth);
1258
1259 if (FreeRDPColorHasAlpha(DstFormat) && ((flags & FREERDP_IMAGE_FILL_IGNORE_ALPHA) != 0))
1260 {
1261 const UINT32 bpp = FreeRDPGetBytesPerPixel(DstFormat);
1262 BYTE r = 0;
1263 BYTE g = 0;
1264 BYTE b = 0;
1265 FreeRDPSplitColor(color, DstFormat, &r, &g, &b, nullptr, nullptr);
1266
1267 for (size_t y = 0; y < nHeight; y++)
1268 {
1269 BYTE* WINPR_RESTRICT line = &pDstData[(y + nYDst) * nDstStep];
1270
1271 for (size_t x = 0; x < nWidth; x++)
1272 {
1273 BYTE* WINPR_RESTRICT dst = &line[x * bpp];
1274 const UINT32 dcolor = FreeRDPReadColor_int(dst, DstFormat);
1275 BYTE a = 0;
1276 FreeRDPSplitColor(dcolor, DstFormat, nullptr, nullptr, nullptr, &a, nullptr);
1277 const UINT32 scolor = FreeRDPGetColor(DstFormat, r, g, b, a);
1278 if (!FreeRDPWriteColor_int(dst, DstFormat, scolor))
1279 return FALSE;
1280 }
1281 }
1282 return TRUE;
1283 }
1284 return freerdp_image_fill(pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, color);
1285}
1286
1287#if defined(WITH_SWSCALE)
1288static enum AVPixelFormat av_format_for_buffer(UINT32 format)
1289{
1290 switch (format)
1291 {
1292 case PIXEL_FORMAT_ARGB32:
1293 return AV_PIX_FMT_BGRA;
1294
1295 case PIXEL_FORMAT_XRGB32:
1296 return AV_PIX_FMT_BGR0;
1297
1298 case PIXEL_FORMAT_BGRA32:
1299 return AV_PIX_FMT_RGBA;
1300
1301 case PIXEL_FORMAT_BGRX32:
1302 return AV_PIX_FMT_RGB0;
1303
1304 default:
1305 return AV_PIX_FMT_NONE;
1306 }
1307}
1308#endif
1309
1310static freerdp_image_scale_fkt s_scale_fkt = nullptr;
1311static void* s_scale_user_arg = nullptr;
1312
1313BOOL freerdp_set_image_scaling_function(freerdp_image_scale_fkt scale, void* user)
1314{
1315 s_scale_fkt = scale;
1316 s_scale_user_arg = user;
1317 return TRUE;
1318}
1319
1320BOOL freerdp_image_scaling_supported(void)
1321{
1322 if (s_scale_fkt)
1323 return TRUE;
1324
1325#if defined(WITH_SWSCALE) || defined(WITH_CAIRO)
1326 return TRUE;
1327#else
1328 return FALSE;
1329#endif
1330}
1331
1332BOOL freerdp_image_scale(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep,
1333 UINT32 nXDst, UINT32 nYDst, UINT32 nDstWidth, UINT32 nDstHeight,
1334 const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat, UINT32 nSrcStep,
1335 UINT32 nXSrc, UINT32 nYSrc, UINT32 nSrcWidth, UINT32 nSrcHeight)
1336{
1337 if (s_scale_fkt)
1338 return s_scale_fkt(pDstData, DstFormat, nDstStep, nXDst, nYDst, nDstWidth, nDstHeight,
1339 pSrcData, SrcFormat, nSrcStep, nXSrc, nYSrc, nSrcWidth, nSrcHeight,
1340 s_scale_user_arg);
1341
1342 BOOL rc = FALSE;
1343 if (FreeRDPGetBitsPerPixel(SrcFormat) == 0)
1344 return FALSE;
1345 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
1346 return FALSE;
1347
1348 WINPR_ASSERT(1ull * nXDst + nDstWidth <= UINT32_MAX);
1349 WINPR_ASSERT(1ull * nXSrc + nSrcWidth <= UINT32_MAX);
1350 if (nDstStep == 0)
1351 nDstStep = (nXDst + nDstWidth) * FreeRDPGetBytesPerPixel(DstFormat);
1352
1353 if (nSrcStep == 0)
1354 nSrcStep = (nXSrc + nSrcWidth) * FreeRDPGetBytesPerPixel(SrcFormat);
1355
1356 WINPR_ASSERT(nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= nXDst + nDstWidth);
1357 WINPR_ASSERT(nSrcStep / FreeRDPGetBytesPerPixel(SrcFormat) >= nXSrc + nSrcWidth);
1358
1359#if defined(WITH_SWSCALE) || defined(WITH_CAIRO)
1360 const BYTE* src =
1361 &pSrcData[1ULL * nXSrc * FreeRDPGetBytesPerPixel(SrcFormat) + 1ULL * nYSrc * nSrcStep];
1362 BYTE* dst =
1363 &pDstData[1ULL * nXDst * FreeRDPGetBytesPerPixel(DstFormat) + 1ULL * nYDst * nDstStep];
1364#endif
1365
1366 /* direct copy is much faster than scaling, so check if we can simply copy... */
1367 if ((nDstWidth == nSrcWidth) && (nDstHeight == nSrcHeight))
1368 {
1369 return freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, nDstWidth,
1370 nDstHeight, pSrcData, SrcFormat, nSrcStep, nXSrc,
1371 nYSrc, nullptr, FREERDP_FLIP_NONE);
1372 }
1373 else
1374#if defined(WITH_SWSCALE)
1375 {
1376 int res = 0;
1377 struct SwsContext* resize = nullptr;
1378 enum AVPixelFormat srcFormat = av_format_for_buffer(SrcFormat);
1379 enum AVPixelFormat dstFormat = av_format_for_buffer(DstFormat);
1380 const int srcStep[1] = { (int)nSrcStep };
1381 const int dstStep[1] = { (int)nDstStep };
1382
1383 if ((srcFormat == AV_PIX_FMT_NONE) || (dstFormat == AV_PIX_FMT_NONE))
1384 return FALSE;
1385
1386 if (!freerdp_swscale_available())
1387 {
1388 WLog_WARN(TAG, "swscale not available");
1389 return FALSE;
1390 }
1391
1392 /* swscale addresses lines as line * stride in int arithmetic, every byte of an image must
1393 * be addressable with a signed int (see av_image_check_size). For larger images the
1394 * offset wraps and the last lines are read or written 4GiB in front of the buffer. */
1395 if ((1ULL * nSrcStep * nSrcHeight > INT32_MAX) ||
1396 (1ULL * nDstStep * nDstHeight > INT32_MAX))
1397 {
1398 WLog_ERR(TAG,
1399 "image too large to scale: source %" PRIu32 " x %" PRIu32
1400 " bytes, destination %" PRIu32 " x %" PRIu32 " bytes",
1401 nSrcStep, nSrcHeight, nDstStep, nDstHeight);
1402 return FALSE;
1403 }
1404
1405 resize = freerdp_sws_getContext((int)nSrcWidth, (int)nSrcHeight, srcFormat, (int)nDstWidth,
1406 (int)nDstHeight, dstFormat, SWS_BILINEAR, nullptr, nullptr,
1407 nullptr);
1408
1409 if (!resize)
1410 goto fail;
1411
1412 /* sws_scale expects arrays of pointers, not pointer-to-pointer */
1413 const BYTE* srcSlice[4] = { src, nullptr, nullptr, nullptr };
1414 BYTE* dstSlice[4] = { dst, nullptr, nullptr, nullptr };
1415 const int srcSteps[4] = { srcStep[0], 0, 0, 0 };
1416 const int dstSteps[4] = { dstStep[0], 0, 0, 0 };
1417
1418 res = freerdp_sws_scale(resize, srcSlice, srcSteps, 0, (int)nSrcHeight, dstSlice, dstSteps);
1419 rc = (res == ((int)nDstHeight));
1420 fail:
1421 freerdp_sws_freeContext(resize);
1422 }
1423
1424#elif defined(WITH_CAIRO)
1425 {
1426 const double sx = (double)nDstWidth / (double)nSrcWidth;
1427 const double sy = (double)nDstHeight / (double)nSrcHeight;
1428 cairo_t* cairo_context;
1429 cairo_surface_t *csrc, *cdst;
1430
1431 if ((nSrcWidth > INT_MAX) || (nSrcHeight > INT_MAX) || (nSrcStep > INT_MAX))
1432 return FALSE;
1433
1434 if ((nDstWidth > INT_MAX) || (nDstHeight > INT_MAX) || (nDstStep > INT_MAX))
1435 return FALSE;
1436
1437 /* cairo (pixman) addresses lines as line * stride in int arithmetic as well */
1438 if ((1ULL * nSrcStep * nSrcHeight > INT_MAX) || (1ULL * nDstStep * nDstHeight > INT_MAX))
1439 return FALSE;
1440
1441 csrc = cairo_image_surface_create_for_data((void*)src, CAIRO_FORMAT_ARGB32, (int)nSrcWidth,
1442 (int)nSrcHeight, (int)nSrcStep);
1443 cdst = cairo_image_surface_create_for_data(dst, CAIRO_FORMAT_ARGB32, (int)nDstWidth,
1444 (int)nDstHeight, (int)nDstStep);
1445
1446 if (!csrc || !cdst)
1447 goto fail;
1448
1449 cairo_context = cairo_create(cdst);
1450
1451 if (!cairo_context)
1452 goto fail2;
1453
1454 cairo_scale(cairo_context, sx, sy);
1455 cairo_set_operator(cairo_context, CAIRO_OPERATOR_SOURCE);
1456 cairo_set_source_surface(cairo_context, csrc, 0, 0);
1457 cairo_paint(cairo_context);
1458 rc = TRUE;
1459 fail2:
1460 cairo_destroy(cairo_context);
1461 fail:
1462 cairo_surface_destroy(csrc);
1463 cairo_surface_destroy(cdst);
1464 }
1465#else
1466 {
1467 WLog_WARN(TAG, "SmartScaling requested but compiled without libcairo support!");
1468 }
1469#endif
1470 return rc;
1471}
1472
1473DWORD FreeRDPAreColorFormatsEqualNoAlpha(DWORD first, DWORD second)
1474{
1475 return FreeRDPAreColorFormatsEqualNoAlpha_int(first, second);
1476}
1477
1478const char* FreeRDPGetColorFormatName(UINT32 format)
1479{
1480#define ENTRY(x) \
1481 case x: \
1482 return #x;
1483
1484 switch (format)
1485 {
1486 ENTRY(PIXEL_FORMAT_ARGB32)
1487 ENTRY(PIXEL_FORMAT_XRGB32)
1488 ENTRY(PIXEL_FORMAT_ABGR32)
1489 ENTRY(PIXEL_FORMAT_XBGR32)
1490 ENTRY(PIXEL_FORMAT_BGRA32)
1491 ENTRY(PIXEL_FORMAT_BGRX32)
1492 ENTRY(PIXEL_FORMAT_RGBA32)
1493 ENTRY(PIXEL_FORMAT_RGBX32)
1494 ENTRY(PIXEL_FORMAT_BGRX32_DEPTH30)
1495 ENTRY(PIXEL_FORMAT_RGBX32_DEPTH30)
1496 ENTRY(PIXEL_FORMAT_RGB24)
1497 ENTRY(PIXEL_FORMAT_BGR24)
1498 ENTRY(PIXEL_FORMAT_RGB16)
1499 ENTRY(PIXEL_FORMAT_BGR16)
1500 ENTRY(PIXEL_FORMAT_ARGB15)
1501 ENTRY(PIXEL_FORMAT_RGB15)
1502 ENTRY(PIXEL_FORMAT_ABGR15)
1503 ENTRY(PIXEL_FORMAT_BGR15)
1504 ENTRY(PIXEL_FORMAT_RGB8)
1505 ENTRY(PIXEL_FORMAT_A4)
1506 ENTRY(PIXEL_FORMAT_MONO)
1507
1508 default:
1509 return "UNKNOWN";
1510 }
1511#undef ENTRY
1512}
1513
1514uint32_t FreeRDPGetColorFromatFromName(const char* name)
1515{
1516#define ENTRY(x) \
1517 if (strcmp(name, #x) == 0) \
1518 return x;
1519
1520 if (!name)
1521 return 0;
1522
1523 ENTRY(PIXEL_FORMAT_ARGB32)
1524 ENTRY(PIXEL_FORMAT_XRGB32)
1525 ENTRY(PIXEL_FORMAT_ABGR32)
1526 ENTRY(PIXEL_FORMAT_XBGR32)
1527 ENTRY(PIXEL_FORMAT_BGRA32)
1528 ENTRY(PIXEL_FORMAT_BGRX32)
1529 ENTRY(PIXEL_FORMAT_RGBA32)
1530 ENTRY(PIXEL_FORMAT_RGBX32)
1531 ENTRY(PIXEL_FORMAT_BGRX32_DEPTH30)
1532 ENTRY(PIXEL_FORMAT_RGBX32_DEPTH30)
1533 ENTRY(PIXEL_FORMAT_RGB24)
1534 ENTRY(PIXEL_FORMAT_BGR24)
1535 ENTRY(PIXEL_FORMAT_RGB16)
1536 ENTRY(PIXEL_FORMAT_BGR16)
1537 ENTRY(PIXEL_FORMAT_ARGB15)
1538 ENTRY(PIXEL_FORMAT_RGB15)
1539 ENTRY(PIXEL_FORMAT_ABGR15)
1540 ENTRY(PIXEL_FORMAT_BGR15)
1541 ENTRY(PIXEL_FORMAT_RGB8)
1542 ENTRY(PIXEL_FORMAT_A4)
1543 ENTRY(PIXEL_FORMAT_MONO)
1544
1545 return 0;
1546#undef ENTRY
1547}
1548
1549void FreeRDPSplitColor(UINT32 color, UINT32 format, BYTE* _r, BYTE* _g, BYTE* _b, BYTE* _a,
1550 const gdiPalette* palette)
1551{
1552 UINT32 tmp = 0;
1553
1554 switch (format)
1555 {
1556 /* 32bpp formats */
1557 case PIXEL_FORMAT_ARGB32:
1558 if (_a)
1559 *_a = (BYTE)(color >> 24);
1560
1561 if (_r)
1562 *_r = (BYTE)(color >> 16);
1563
1564 if (_g)
1565 *_g = (BYTE)(color >> 8);
1566
1567 if (_b)
1568 *_b = (BYTE)color;
1569
1570 break;
1571
1572 case PIXEL_FORMAT_XRGB32:
1573 if (_r)
1574 *_r = (BYTE)(color >> 16);
1575
1576 if (_g)
1577 *_g = (BYTE)(color >> 8);
1578
1579 if (_b)
1580 *_b = (BYTE)color;
1581
1582 if (_a)
1583 *_a = 0xFF;
1584
1585 break;
1586
1587 case PIXEL_FORMAT_ABGR32:
1588 if (_a)
1589 *_a = (BYTE)(color >> 24);
1590
1591 if (_b)
1592 *_b = (BYTE)(color >> 16);
1593
1594 if (_g)
1595 *_g = (BYTE)(color >> 8);
1596
1597 if (_r)
1598 *_r = (BYTE)color;
1599
1600 break;
1601
1602 case PIXEL_FORMAT_XBGR32:
1603 if (_b)
1604 *_b = (BYTE)(color >> 16);
1605
1606 if (_g)
1607 *_g = (BYTE)(color >> 8);
1608
1609 if (_r)
1610 *_r = (BYTE)color;
1611
1612 if (_a)
1613 *_a = 0xFF;
1614
1615 break;
1616
1617 case PIXEL_FORMAT_RGBA32:
1618 if (_r)
1619 *_r = (BYTE)(color >> 24);
1620
1621 if (_g)
1622 *_g = (BYTE)(color >> 16);
1623
1624 if (_b)
1625 *_b = (BYTE)(color >> 8);
1626
1627 if (_a)
1628 *_a = (BYTE)color;
1629
1630 break;
1631
1632 case PIXEL_FORMAT_RGBX32:
1633 if (_r)
1634 *_r = (BYTE)(color >> 24);
1635
1636 if (_g)
1637 *_g = (BYTE)(color >> 16);
1638
1639 if (_b)
1640 *_b = (BYTE)(color >> 8);
1641
1642 if (_a)
1643 *_a = 0xFF;
1644
1645 break;
1646
1647 case PIXEL_FORMAT_BGRA32:
1648 if (_b)
1649 *_b = (BYTE)(color >> 24);
1650
1651 if (_g)
1652 *_g = (BYTE)(color >> 16);
1653
1654 if (_r)
1655 *_r = (BYTE)(color >> 8);
1656
1657 if (_a)
1658 *_a = (BYTE)color;
1659
1660 break;
1661
1662 case PIXEL_FORMAT_BGRX32:
1663 if (_b)
1664 *_b = (BYTE)(color >> 24);
1665
1666 if (_g)
1667 *_g = (BYTE)(color >> 16);
1668
1669 if (_r)
1670 *_r = (BYTE)(color >> 8);
1671
1672 if (_a)
1673 *_a = 0xFF;
1674
1675 break;
1676
1677 /* 24bpp formats */
1678 case PIXEL_FORMAT_RGB24:
1679 if (_r)
1680 *_r = (BYTE)(color >> 16);
1681
1682 if (_g)
1683 *_g = (BYTE)(color >> 8);
1684
1685 if (_b)
1686 *_b = (BYTE)color;
1687
1688 if (_a)
1689 *_a = 0xFF;
1690
1691 break;
1692
1693 case PIXEL_FORMAT_BGR24:
1694 if (_b)
1695 *_b = (BYTE)(color >> 16);
1696
1697 if (_g)
1698 *_g = (BYTE)(color >> 8);
1699
1700 if (_r)
1701 *_r = (BYTE)color;
1702
1703 if (_a)
1704 *_a = 0xFF;
1705
1706 break;
1707
1708 /* 16bpp formats */
1709 case PIXEL_FORMAT_RGB16:
1710 if (_r)
1711 {
1712 const UINT32 c = (color >> 11) & 0x1F;
1713 const UINT32 val = (c << 3) + c / 4;
1714 *_r = (BYTE)(val > 255 ? 255 : val);
1715 }
1716
1717 if (_g)
1718 {
1719 const UINT32 c = (color >> 5) & 0x3F;
1720 const UINT32 val = (c << 2) + c / 4 / 2;
1721 *_g = (BYTE)(val > 255 ? 255 : val);
1722 }
1723
1724 if (_b)
1725 {
1726 const UINT32 c = (color)&0x1F;
1727 const UINT32 val = (c << 3) + c / 4;
1728 *_b = (BYTE)(val > 255 ? 255 : val);
1729 }
1730
1731 if (_a)
1732 *_a = 0xFF;
1733
1734 break;
1735
1736 case PIXEL_FORMAT_BGR16:
1737 if (_r)
1738 {
1739 const UINT32 c = (color)&0x1F;
1740 const UINT32 val = (c << 3) + c / 4;
1741 *_r = (BYTE)(val > 255 ? 255 : val);
1742 }
1743
1744 if (_g)
1745 {
1746 const UINT32 c = (color >> 5) & 0x3F;
1747 const UINT32 val = (c << 2) + c / 4 / 2;
1748 *_g = (BYTE)(val > 255 ? 255 : val);
1749 }
1750
1751 if (_b)
1752 {
1753 const UINT32 c = (color >> 11) & 0x1F;
1754 const UINT32 val = (c << 3) + c / 4;
1755 *_b = (BYTE)(val > 255 ? 255 : val);
1756 }
1757
1758 if (_a)
1759 *_a = 0xFF;
1760
1761 break;
1762
1763 case PIXEL_FORMAT_ARGB15:
1764 if (_r)
1765 {
1766 const UINT32 c = (color >> 10) & 0x1F;
1767 const UINT32 val = (c << 3) + c / 4;
1768 *_r = (BYTE)(val > 255 ? 255 : val);
1769 }
1770
1771 if (_g)
1772 {
1773 const UINT32 c = (color >> 5) & 0x1F;
1774 const UINT32 val = (c << 3) + c / 4;
1775 *_g = (BYTE)(val > 255 ? 255 : val);
1776 }
1777
1778 if (_b)
1779 {
1780 const UINT32 c = (color)&0x1F;
1781 const UINT32 val = (c << 3) + c / 4;
1782 *_b = (BYTE)(val > 255 ? 255 : val);
1783 }
1784
1785 if (_a)
1786 *_a = color & 0x8000 ? 0xFF : 0x00;
1787
1788 break;
1789
1790 case PIXEL_FORMAT_ABGR15:
1791 if (_r)
1792 {
1793 const UINT32 c = (color)&0x1F;
1794 const UINT32 val = (c << 3) + c / 4;
1795 *_r = (BYTE)(val > 255 ? 255 : val);
1796 }
1797
1798 if (_g)
1799 {
1800 const UINT32 c = (color >> 5) & 0x1F;
1801 const UINT32 val = (c << 3) + c / 4;
1802 *_g = (BYTE)(val > 255 ? 255 : val);
1803 }
1804
1805 if (_b)
1806 {
1807 const UINT32 c = (color >> 10) & 0x1F;
1808 const UINT32 val = (c << 3) + c / 4;
1809 *_b = (BYTE)(val > 255 ? 255 : val);
1810 }
1811
1812 if (_a)
1813 *_a = color & 0x8000 ? 0xFF : 0x00;
1814
1815 break;
1816
1817 /* 15bpp formats */
1818 case PIXEL_FORMAT_RGB15:
1819 if (_r)
1820 {
1821 const UINT32 c = (color >> 10) & 0x1F;
1822 const UINT32 val = (c << 3) + c / 4;
1823 *_r = (BYTE)(val > 255 ? 255 : val);
1824 }
1825
1826 if (_g)
1827 {
1828 const UINT32 c = (color >> 5) & 0x1F;
1829 const UINT32 val = (c << 3) + c / 4;
1830 *_g = (BYTE)(val > 255 ? 255 : val);
1831 }
1832
1833 if (_b)
1834 {
1835 const UINT32 c = (color)&0x1F;
1836 const UINT32 val = (c << 3) + c / 4;
1837 *_b = (BYTE)(val > 255 ? 255 : val);
1838 }
1839
1840 if (_a)
1841 *_a = 0xFF;
1842
1843 break;
1844
1845 case PIXEL_FORMAT_BGR15:
1846 if (_r)
1847 {
1848 const UINT32 c = (color)&0x1F;
1849 const UINT32 val = (c << 3) + c / 4;
1850 *_r = (BYTE)(val > 255 ? 255 : val);
1851 }
1852
1853 if (_g)
1854 {
1855 const UINT32 c = (color >> 5) & 0x1F;
1856 const UINT32 val = (c << 3) + c / 4;
1857 *_g = (BYTE)(val > 255 ? 255 : val);
1858 }
1859
1860 if (_b)
1861 {
1862 const UINT32 c = (color >> 10) & 0x1F;
1863 const UINT32 val = (c << 3) + c / 4;
1864 *_b = (BYTE)(val > 255 ? 255 : val);
1865 }
1866
1867 if (_a)
1868 *_a = 0xFF;
1869
1870 break;
1871
1872 /* 8bpp formats */
1873 case PIXEL_FORMAT_RGB8:
1874 if (color <= 0xFF)
1875 {
1876 tmp = palette->palette[color];
1877 FreeRDPSplitColor(tmp, palette->format, _r, _g, _b, _a, nullptr);
1878 }
1879 else
1880 {
1881 if (_r)
1882 *_r = 0x00;
1883
1884 if (_g)
1885 *_g = 0x00;
1886
1887 if (_b)
1888 *_b = 0x00;
1889
1890 if (_a)
1891 *_a = 0x00;
1892 }
1893
1894 break;
1895
1896 /* 1bpp formats */
1897 case PIXEL_FORMAT_MONO:
1898 if (_r)
1899 *_r = (color) ? 0xFF : 0x00;
1900
1901 if (_g)
1902 *_g = (color) ? 0xFF : 0x00;
1903
1904 if (_b)
1905 *_b = (color) ? 0xFF : 0x00;
1906
1907 if (_a)
1908 *_a = (color) ? 0xFF : 0x00;
1909
1910 break;
1911
1912 /* 4 bpp formats */
1913 case PIXEL_FORMAT_A4:
1914 default:
1915 if (_r)
1916 *_r = 0x00;
1917
1918 if (_g)
1919 *_g = 0x00;
1920
1921 if (_b)
1922 *_b = 0x00;
1923
1924 if (_a)
1925 *_a = 0x00;
1926
1927 WLog_ERR(TAG, "Unsupported format %s", FreeRDPGetColorFormatName(format));
1928 break;
1929 }
1930}
1931
1932BOOL FreeRDPWriteColorIgnoreAlpha(BYTE* WINPR_RESTRICT dst, UINT32 format, UINT32 color)
1933{
1934 return FreeRDPWriteColorIgnoreAlpha_int(dst, format, color);
1935}
1936
1937BOOL FreeRDPWriteColor(BYTE* WINPR_RESTRICT dst, UINT32 format, UINT32 color)
1938{
1939 return FreeRDPWriteColor_int(dst, format, color);
1940}
1941
1942UINT32 FreeRDPReadColor(const BYTE* WINPR_RESTRICT src, UINT32 format)
1943{
1944 return FreeRDPReadColor_int(src, format);
1945}
1946
1947UINT32 FreeRDPGetColor(UINT32 format, BYTE r, BYTE g, BYTE b, BYTE a)
1948{
1949 UINT32 _r = r;
1950 UINT32 _g = g;
1951 UINT32 _b = b;
1952 UINT32 _a = a;
1953 UINT32 t = 0;
1954
1955 switch (format)
1956 {
1957 /* 32bpp formats */
1958 case PIXEL_FORMAT_ARGB32:
1959 return (_a << 24) | (_r << 16) | (_g << 8) | _b;
1960
1961 case PIXEL_FORMAT_XRGB32:
1962 return (_r << 16) | (_g << 8) | _b;
1963
1964 case PIXEL_FORMAT_ABGR32:
1965 return (_a << 24) | (_b << 16) | (_g << 8) | _r;
1966
1967 case PIXEL_FORMAT_XBGR32:
1968 return (_b << 16) | (_g << 8) | _r;
1969
1970 case PIXEL_FORMAT_RGBA32:
1971 return (_r << 24) | (_g << 16) | (_b << 8) | _a;
1972
1973 case PIXEL_FORMAT_RGBX32:
1974 return (_r << 24) | (_g << 16) | (_b << 8) | _a;
1975
1976 case PIXEL_FORMAT_BGRA32:
1977 return (_b << 24) | (_g << 16) | (_r << 8) | _a;
1978
1979 case PIXEL_FORMAT_BGRX32:
1980 return (_b << 24) | (_g << 16) | (_r << 8) | _a;
1981
1982 case PIXEL_FORMAT_RGBX32_DEPTH30:
1983 // TODO: Not tested
1984 t = (_r << 22) | (_g << 12) | (_b << 2);
1985 // NOTE: Swapping byte-order because FreeRDPWriteColor written UINT32 in big-endian
1986 return ((t & 0xff) << 24) | (((t >> 8) & 0xff) << 16) | (((t >> 16) & 0xff) << 8) |
1987 (t >> 24);
1988
1989 case PIXEL_FORMAT_BGRX32_DEPTH30:
1990 // NOTE: Swapping b and r channel (unknown reason)
1991 t = (_r << 22) | (_g << 12) | (_b << 2);
1992 // NOTE: Swapping byte-order because FreeRDPWriteColor written UINT32 in big-endian
1993 return ((t & 0xff) << 24) | (((t >> 8) & 0xff) << 16) | (((t >> 16) & 0xff) << 8) |
1994 (t >> 24);
1995
1996 /* 24bpp formats */
1997 case PIXEL_FORMAT_RGB24:
1998 return (_r << 16) | (_g << 8) | _b;
1999
2000 case PIXEL_FORMAT_BGR24:
2001 return (_b << 16) | (_g << 8) | _r;
2002
2003 /* 16bpp formats */
2004 case PIXEL_FORMAT_RGB16:
2005 return (((_r >> 3) & 0x1F) << 11) | (((_g >> 2) & 0x3F) << 5) | ((_b >> 3) & 0x1F);
2006
2007 case PIXEL_FORMAT_BGR16:
2008 return (((_b >> 3) & 0x1F) << 11) | (((_g >> 2) & 0x3F) << 5) | ((_r >> 3) & 0x1F);
2009
2010 case PIXEL_FORMAT_ARGB15:
2011 return (((_r >> 3) & 0x1F) << 10) | (((_g >> 3) & 0x1F) << 5) | ((_b >> 3) & 0x1F) |
2012 (_a ? 0x8000 : 0x0000);
2013
2014 case PIXEL_FORMAT_ABGR15:
2015 return (((_b >> 3) & 0x1F) << 10) | (((_g >> 3) & 0x1F) << 5) | ((_r >> 3) & 0x1F) |
2016 (_a ? 0x8000 : 0x0000);
2017
2018 /* 15bpp formats */
2019 case PIXEL_FORMAT_RGB15:
2020 return (((_r >> 3) & 0x1F) << 10) | (((_g >> 3) & 0x1F) << 5) | ((_b >> 3) & 0x1F);
2021
2022 case PIXEL_FORMAT_BGR15:
2023 return (((_b >> 3) & 0x1F) << 10) | (((_g >> 3) & 0x1F) << 5) | ((_r >> 3) & 0x1F);
2024
2025 /* 8bpp formats */
2026 case PIXEL_FORMAT_RGB8:
2027
2028 /* 4 bpp formats */
2029 case PIXEL_FORMAT_A4:
2030
2031 /* 1bpp formats */
2032 case PIXEL_FORMAT_MONO:
2033 default:
2034 WLog_ERR(TAG, "Unsupported format %s", FreeRDPGetColorFormatName(format));
2035 return 0;
2036 }
2037}
2038
2039BOOL freerdp_image_copy_no_overlap(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep,
2040 UINT32 nXDst, UINT32 nYDst, UINT32 nWidth, UINT32 nHeight,
2041 const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
2042 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
2043 const gdiPalette* WINPR_RESTRICT palette, UINT32 flags)
2044{
2045 static primitives_t* prims = nullptr;
2046 if (!prims)
2047 prims = primitives_get();
2048
2049 if (FreeRDPGetBitsPerPixel(SrcFormat) == 0)
2050 return FALSE;
2051 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
2052 return FALSE;
2053 if ((SrcFormat == PIXEL_FORMAT_RGB8) && (SrcFormat != DstFormat) &&
2054 !freerdp_image_palette_is_valid(palette))
2055 return FALSE;
2056
2057 if ((nWidth == 0) || (nHeight == 0))
2058 return TRUE;
2059
2060 WINPR_ASSERT(1ull * nXDst + nWidth <= UINT32_MAX);
2061 WINPR_ASSERT(1ull * nXSrc + nWidth <= UINT32_MAX);
2062 if (nDstStep == 0)
2063 nDstStep = (nXDst + nWidth) * FreeRDPGetBytesPerPixel(DstFormat);
2064 if (nSrcStep == 0)
2065 nSrcStep = (nXSrc + nWidth) * FreeRDPGetBytesPerPixel(SrcFormat);
2066
2067 WINPR_ASSERT(nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= nXDst + nWidth);
2068 WINPR_ASSERT(nSrcStep / FreeRDPGetBytesPerPixel(SrcFormat) >= nXSrc + nWidth);
2069 WINPR_ASSERT(!overlapping(pDstData, nYDst, nDstStep, pSrcData, nYSrc, nSrcStep, nHeight));
2070 WINPR_ASSERT(prims);
2071 WINPR_ASSERT(prims->copy_no_overlap);
2072 return prims->copy_no_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight,
2073 pSrcData, SrcFormat, nSrcStep, nXSrc, nYSrc, palette,
2074 flags) == PRIMITIVES_SUCCESS;
2075}
WINPR_ATTR_NODISCARD fn_copy_no_overlap_t copy_no_overlap
Definition primitives.h:304