22#include <freerdp/config.h>
30#include <freerdp/log.h>
31#include <freerdp/freerdp.h>
32#include <freerdp/primitives.h>
34#if defined(WITH_CAIRO)
38#if defined(WITH_SWSCALE)
39#include "image_ffmpeg.h"
44#define TAG FREERDP_TAG("color")
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);
52#if defined(WITH_CURSOR_DUMP)
53#include <winpr/path.h>
54#include <winpr/image.h>
55static char* get_dump_name(
char* prefix,
size_t len)
57 static uint64_t count = 0;
58 _snprintf(prefix, len,
"cursor_dump_%08" PRIx64, count++);
60 return GetCombinedPath(CURSOR_DUMP_DIR, prefix);
63static void dump_binary_data(FILE* fp,
const uint8_t* data,
size_t len)
66 (void)fprintf(fp,
"0x%02" PRIx8, data[0]);
67 for (
size_t x = 1; x < len; x++)
69 (void)fprintf(fp,
", 0x%02" PRIx8, data[x]);
73static void dump_uint_data(FILE* fp,
const uint32_t* data,
size_t len)
76 (void)fprintf(fp,
"0x%08" PRIx32, data[0]);
77 for (
size_t x = 1; x < len; x++)
79 (void)fprintf(fp,
", 0x%08" PRIx32, data[x]);
83static void dump_write_header(
const char* path,
const char* prefix)
85 char* header =
nullptr;
87 winpr_asprintf(&header, &headerlen,
"%s.h", path);
90 FILE* fp = fopen(header,
"w");
96 (void)fprintf(fp,
"/* FreeRDP cursor dump to use for unit tests\n");
97 (void)fprintf(fp,
" * this file was auto generated by %s\n", __func__);
98 (void)fprintf(fp,
" * do not modify manually\n");
99 (void)fprintf(fp,
" */\n");
100 (void)fprintf(fp,
"\n");
101 (void)fprintf(fp,
"#pragma once\n");
102 (void)fprintf(fp,
"\n");
103 (void)fprintf(fp,
"#include <freerdp/codec/color.h>\n");
104 (void)fprintf(fp,
"#include <freerdp/graphics.h>\n");
105 (void)fprintf(fp,
"\n");
106 (void)fprintf(fp,
"extern const gdiPalette %s_palette;\n", prefix);
107 (void)fprintf(fp,
"extern const rdpPointer %s_pointer;\n", prefix);
108 (void)fprintf(fp,
"extern const uint8_t %s_image_bgra32[];\n", prefix);
112static void dump_write_c_file(
const char* path,
const char* prefix, UINT32 nXDst, UINT32 nYDst,
113 UINT32 nWidth, UINT32 nHeight,
const BYTE* WINPR_RESTRICT xorMask,
114 UINT32 xorMaskLength,
const BYTE* WINPR_RESTRICT andMask,
115 UINT32 andMaskLength, UINT32 xorBpp,
const gdiPalette* palette,
118 const uint32_t format = PIXEL_FORMAT_BGRA32;
119 const uint32_t bpp = FreeRDPGetBytesPerPixel(format);
120 const uint32_t step = nWidth * bpp;
122 char* header =
nullptr;
123 size_t headerlen = 0;
124 winpr_asprintf(&header, &headerlen,
"%s.c", path);
127 FILE* fp = fopen(header,
"w");
132 (void)fprintf(fp,
"/* FreeRDP cursor dump to use for unit tests\n");
133 (void)fprintf(fp,
" * this file was auto generated by %s\n", __func__);
134 (void)fprintf(fp,
" * do not modify manually\n");
135 (void)fprintf(fp,
" */\n");
136 (void)fprintf(fp,
"\n");
137 (void)fprintf(fp,
"#include \"%s.h\"\n", prefix);
138 (void)fprintf(fp,
"\n");
140 (void)fprintf(fp,
"static const uint8_t andmask[] = {\n");
141 dump_binary_data(fp, andMask, andMaskLength);
142 (void)fprintf(fp,
"};\n");
143 (void)fprintf(fp,
"\n");
145 (void)fprintf(fp,
"static const uint8_t xormask[] = {\n");
146 dump_binary_data(fp, xorMask, xorMaskLength);
147 (void)fprintf(fp,
"};\n");
148 (void)fprintf(fp,
"\n");
149 (void)fprintf(fp,
"const gdiPalette %s_palette = {\n", prefix);
152 (void)fprintf(fp,
".format=%" PRIu32
",", palette->format);
153 (void)fprintf(fp,
".palette={");
154 dump_uint_data(fp, palette->palette, ARRAYSIZE(palette->palette));
155 (void)fprintf(fp,
"}\n");
158 (
void)fprintf(fp,
"0");
160 (void)fprintf(fp,
"};\n");
162 (void)fprintf(fp,
"\n");
163 (void)fprintf(fp,
"const rdpPointer %s_pointer = {\n", prefix);
164 (void)fprintf(fp,
".size = 0,\n");
165 (void)fprintf(fp,
".New = nullptr,\n");
166 (void)fprintf(fp,
".Free = nullptr,\n");
167 (void)fprintf(fp,
".Set = nullptr,\n");
168 (void)fprintf(fp,
".SetNull = nullptr,\n");
169 (void)fprintf(fp,
".SetDefault = nullptr,\n");
170 (void)fprintf(fp,
".SetPosition = nullptr,\n");
171 (void)fprintf(fp,
".paddingA = {0},\n");
172 (void)fprintf(fp,
".xPos = %" PRIu32
",\n", nXDst);
173 (void)fprintf(fp,
".yPos = %" PRIu32
",\n", nYDst);
174 (void)fprintf(fp,
".width = %" PRIu32
",\n", nWidth);
175 (void)fprintf(fp,
".height = %" PRIu32
",\n", nHeight);
176 (void)fprintf(fp,
".xorBpp = %" PRIu32
",\n", xorBpp);
177 (void)fprintf(fp,
".lengthAndMask = ARRAYSIZE(andmask),\n");
178 (void)fprintf(fp,
".lengthXorMask = ARRAYSIZE(xormask),\n");
179 (void)fprintf(fp,
".xorMaskData = xormask,\n");
180 (void)fprintf(fp,
".andMaskData = andmask,\n");
181 (void)fprintf(fp,
".paddingB = {0}\n");
182 (void)fprintf(fp,
"};\n");
183 (void)fprintf(fp,
"\n");
184 (void)fprintf(fp,
"const uint8_t %s_image_bgra32[]={\n", prefix);
185 dump_binary_data(fp, bmp, step * nHeight);
186 (void)fprintf(fp,
"};\n");
190 wImage* img = winpr_image_new();
193 img->data = WINPR_CAST_CONST_PTR_AWAY(bmp, BYTE*);
194 img->bitsPerPixel = 32;
195 img->bytesPerPixel = 4;
196 img->height = nHeight;
198 img->scanline = step;
199 img->type = WINPR_IMAGE_PNG;
200 char* imgname =
nullptr;
201 size_t imgnamesize = 0;
202 winpr_asprintf(&imgname, &imgnamesize,
"%s.png", path);
204 winpr_image_write(img, imgname);
206 winpr_image_free(img, FALSE);
215static void dump_pointer_data(UINT32 nXDst, UINT32 nYDst, UINT32 nWidth, UINT32 nHeight,
216 const BYTE* WINPR_RESTRICT xorMask, UINT32 xorMaskLength,
217 const BYTE* WINPR_RESTRICT andMask, UINT32 andMaskLength,
218 UINT32 xorBpp,
const gdiPalette* palette)
220 const uint32_t format = PIXEL_FORMAT_BGRA32;
221 const uint32_t bpp = FreeRDPGetBytesPerPixel(format);
222 const uint32_t step = nWidth * bpp;
223 BYTE* bmp = calloc(step * 2, nHeight);
224 char prefix[64] = WINPR_C_ARRAY_INIT;
225 char* path = get_dump_name(prefix,
sizeof(prefix));
230 if (!freerdp_image_copy_from_pointer_data_int(bmp, format, step, 0, 0, nWidth, nHeight, xorMask,
231 xorMaskLength, andMask, andMaskLength, xorBpp,
235 dump_write_header(path, prefix);
236 dump_write_c_file(path, prefix, nXDst, nYDst, nWidth, nHeight, xorMask, xorMaskLength, andMask,
237 andMaskLength, xorBpp, palette, bmp);
245#if !defined(WITHOUT_FREERDP_3x_DEPRECATED)
246BYTE* freerdp_glyph_convert(UINT32 width, UINT32 height,
const BYTE* WINPR_RESTRICT data)
248 const size_t scanline = (width + 7ull) / 8ull;
249 const size_t required = scanline * height;
250 return freerdp_glyph_convert_ex(width, height, data, required);
254BYTE* freerdp_glyph_convert_ex(UINT32 width, UINT32 height,
const BYTE* WINPR_RESTRICT data,
262 const size_t scanline = (width + 7ull) / 8ull;
263 const size_t required = scanline * height;
267 if ((len == 0) || (width == 0) || (height == 0))
272 BYTE* dstData = (BYTE*)winpr_aligned_malloc(1ull * width * height, 16);
277 ZeroMemory(dstData, 1ULL * width * height);
278 BYTE* dstp = dstData;
280 for (UINT32 y = 0; y < height; y++)
282 const BYTE* srcp = &data[1ull * y * scanline];
284 for (UINT32 x = 0; x < width; x++)
286 if ((*srcp & (0x80 >> (x % 8))) != 0)
291 if (((x + 1) % 8 == 0) && x != 0)
299BOOL freerdp_image_copy_from_monochrome(BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
300 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
301 UINT32 nHeight,
const BYTE* WINPR_RESTRICT pSrcData,
302 UINT32 backColor, UINT32 foreColor,
303 const gdiPalette* WINPR_RESTRICT palette)
305 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
307 const UINT32 dstBytesPerPixel = FreeRDPGetBytesPerPixel(DstFormat);
309 if (!pDstData || !pSrcData || !palette)
313 nDstStep = dstBytesPerPixel * nWidth;
315 WINPR_ASSERT(1ull * nXDst + nWidth <= UINT32_MAX);
316 WINPR_ASSERT(nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= nXDst + nWidth);
318 const UINT32 monoStep = (nWidth + 7) / 8;
320 for (
size_t y = 0; y < nHeight; y++)
322 BYTE* pDstLine = &pDstData[((nYDst + y) * nDstStep)];
323 UINT32 monoBit = 0x80;
324 const BYTE* monoBits = &pSrcData[monoStep * y];
326 for (
size_t x = 0; x < nWidth; x++)
328 BYTE* pDstPixel = &pDstLine[((nXDst + x) * FreeRDPGetBytesPerPixel(DstFormat))];
329 BOOL monoPixel = (*monoBits & monoBit) != 0;
331 if (!(monoBit >>= 1))
339 if (!FreeRDPWriteColor_int(pDstPixel, DstFormat, backColor))
342 else if (!FreeRDPWriteColor_int(pDstPixel, DstFormat, foreColor))
350static inline UINT32 freerdp_image_inverted_pointer_color(UINT32 x, UINT32 y, UINT32 format)
361 BYTE fill = (x + y) & 1 ? 0x00 : 0xFF;
362 return FreeRDPGetColor(format, fill, fill, fill, 0xFF);
369static void fill_gdi_palette_for_icon(
const BYTE* colorTable, UINT16 cbColorTable,
372 WINPR_ASSERT(palette);
374 palette->format = PIXEL_FORMAT_BGRX32;
375 ZeroMemory(palette->palette,
sizeof(palette->palette));
380 if ((cbColorTable % 4 != 0) || (cbColorTable / 4 > 256))
382 WLog_WARN(TAG,
"weird palette size: %u", cbColorTable);
386 for (UINT16 i = 0; i < cbColorTable / 4; i++)
388 palette->palette[i] = FreeRDPReadColor_int(&colorTable[4ULL * i], palette->format);
392static inline UINT32 div_ceil(UINT32 a, UINT32 b)
394 return (a + (b - 1)) / b;
397static inline UINT32 round_up(UINT32 a, UINT32 b)
399 return b * div_ceil(a, b);
402BOOL freerdp_image_copy_from_icon_data(BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
403 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, UINT16 nWidth,
404 UINT16 nHeight,
const BYTE* WINPR_RESTRICT bitsColor,
405 UINT16 cbBitsColor,
const BYTE* WINPR_RESTRICT bitsMask,
406 UINT16 cbBitsMask,
const BYTE* WINPR_RESTRICT colorTable,
407 UINT16 cbColorTable, UINT32 bpp)
412 if (!pDstData || !bitsColor)
415 if ((nWidth == 0) || (nHeight == 0))
417 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
420 WINPR_ASSERT((nDstStep == 0) || (nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= nWidth));
438 WLog_WARN(TAG,
"1bpp and 4bpp icons are not supported");
442 format = PIXEL_FORMAT_RGB8;
446 format = PIXEL_FORMAT_RGB15;
450 format = PIXEL_FORMAT_RGB24;
454 format = PIXEL_FORMAT_BGRA32;
458 WLog_WARN(TAG,
"invalid icon bpp: %" PRIu32, bpp);
463 if (cbBitsColor / nHeight < FreeRDPGetBytesPerPixel(format) * nWidth)
466 "cbBitsColor{%" PRIu32
"} < nWidth{%" PRIu32
"} * nHeight{%" PRIu32
467 "} * bpp{%" PRIu32
"}",
468 cbBitsColor, nWidth, nHeight, FreeRDPGetBytesPerPixel(format));
472 fill_gdi_palette_for_icon(colorTable, cbColorTable, &palette);
473 if (!freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight,
474 bitsColor, format, 0, 0, 0, &palette, FREERDP_FLIP_VERTICAL))
478 if (FreeRDPColorHasAlpha(DstFormat) && (cbBitsMask > 0))
480 BYTE* dstBuf = pDstData;
481 UINT32 dstBpp = FreeRDPGetBytesPerPixel(DstFormat);
488 const size_t stride = round_up(div_ceil(nWidth, 8), 4);
489 const size_t maskSize = stride * (nHeight - 1ULL) + div_ceil(nWidth, 8);
490 if (cbBitsMask < maskSize)
492 WLog_ERR(TAG,
"cbBitsMask{%" PRIu32
"} < required mask size{%" PRIuz
"}", cbBitsMask,
497 for (UINT32 y = 0; y < nHeight; y++)
499 const BYTE* maskByte = &bitsMask[stride * (nHeight - 1ULL - y)];
502 for (UINT32 x = 0; x < nWidth; x++)
507 BYTE alpha = (*maskByte & nextBit) ? 0x00 : 0xFF;
510 UINT32 color = FreeRDPReadColor_int(dstBuf, DstFormat);
511 FreeRDPSplitColor(color, DstFormat, &r, &g, &b,
nullptr, &palette);
512 color = FreeRDPGetColor(DstFormat, r, g, b, alpha);
513 if (!FreeRDPWriteColor_int(dstBuf, DstFormat, color))
530static BOOL freerdp_image_copy_from_pointer_data_1bpp(
531 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
532 UINT32 nWidth, UINT32 nHeight,
const BYTE* WINPR_RESTRICT xorMask, UINT32 xorMaskLength,
533 const BYTE* WINPR_RESTRICT andMask, UINT32 andMaskLength, UINT32 xorBpp)
543 vFlip = (xorBpp != 1);
544 andStep = (nWidth + 7) / 8;
545 andStep += (andStep % 2);
547 if (!xorMask || (xorMaskLength == 0))
549 if (!andMask || (andMaskLength == 0))
552 xorStep = (nWidth + 7) / 8;
553 xorStep += (xorStep % 2);
555 if (xorStep * nHeight > xorMaskLength)
558 if (andStep * nHeight > andMaskLength)
561 for (UINT32 y = 0; y < nHeight; y++)
563 const BYTE* andBits =
nullptr;
564 const BYTE* xorBits =
nullptr;
565 BYTE* pDstPixel = &pDstData[((1ULL * nYDst + y) * nDstStep) +
566 (1ULL * nXDst * FreeRDPGetBytesPerPixel(DstFormat))];
567 xorBit = andBit = 0x80;
571 xorBits = &xorMask[1ULL * xorStep * y];
572 andBits = &andMask[1ULL * andStep * y];
576 xorBits = &xorMask[1ULL * xorStep * (nHeight - y - 1)];
577 andBits = &andMask[1ULL * andStep * (nHeight - y - 1)];
580 for (UINT32 x = 0; x < nWidth; x++)
583 xorPixel = (*xorBits & xorBit) ? 1 : 0;
591 andPixel = (*andBits & andBit) ? 1 : 0;
599 if (!andPixel && !xorPixel)
600 color = FreeRDPGetColor(DstFormat, 0, 0, 0, 0xFF);
601 else if (!andPixel && xorPixel)
602 color = FreeRDPGetColor(DstFormat, 0xFF, 0xFF, 0xFF, 0xFF);
603 else if (andPixel && !xorPixel)
604 color = FreeRDPGetColor(DstFormat, 0, 0, 0, 0);
605 else if (andPixel && xorPixel)
606 color = freerdp_image_inverted_pointer_color(x, y, DstFormat);
608 if (!FreeRDPWriteColor_int(pDstPixel, DstFormat, color))
610 pDstPixel += FreeRDPGetBytesPerPixel(DstFormat);
617static BOOL freerdp_image_copy_from_pointer_data_xbpp(
618 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
619 UINT32 nWidth, UINT32 nHeight,
const BYTE* WINPR_RESTRICT xorMask, UINT32 xorMaskLength,
620 const BYTE* WINPR_RESTRICT andMask, UINT32 andMaskLength, UINT32 xorBpp,
621 const gdiPalette* palette)
629 UINT32 dstBitsPerPixel = 0;
630 UINT32 xorBytesPerPixel = 0;
631 dstBitsPerPixel = FreeRDPGetBitsPerPixel(DstFormat);
633 vFlip = (xorBpp != 1);
634 andStep = (nWidth + 7) / 8;
635 andStep += (andStep % 2);
637 if (!xorMask || (xorMaskLength == 0))
640 xorBytesPerPixel = xorBpp >> 3;
641 xorStep = 1ULL * nWidth * xorBytesPerPixel;
642 xorStep += (xorStep % 2);
644 if (xorBpp == 8 && !palette)
646 WLog_ERR(TAG,
"null palette in conversion from %" PRIu32
" bpp to %" PRIu32
" bpp", xorBpp,
651 if (xorStep * nHeight > xorMaskLength)
656 if (andStep * nHeight > andMaskLength)
660 for (UINT32 y = 0; y < nHeight; y++)
662 const BYTE* xorBits =
nullptr;
663 const BYTE* andBits =
nullptr;
664 BYTE* pDstPixel = &pDstData[((1ULL * nYDst + y) * nDstStep) +
665 (1ULL * nXDst * FreeRDPGetBytesPerPixel(DstFormat))];
671 andBits = &andMask[andStep * y];
673 xorBits = &xorMask[xorStep * y];
678 andBits = &andMask[1ULL * andStep * (nHeight - y - 1)];
680 xorBits = &xorMask[1ULL * xorStep * (nHeight - y - 1)];
683 for (UINT32 x = 0; x < nWidth; x++)
685 UINT32 pixelFormat = 0;
692 andPixel = (*andBits & andBit) ? 1 : 0;
703 pixelFormat = PIXEL_FORMAT_BGRA32;
704 xorPixel = FreeRDPReadColor_int(xorBits, pixelFormat);
706 else if (xorBpp == 16)
708 pixelFormat = PIXEL_FORMAT_RGB15;
709 xorPixel = FreeRDPReadColor_int(xorBits, pixelFormat);
711 else if (xorBpp == 8)
713 pixelFormat = palette->format;
714 xorPixel = palette->palette[xorBits[0]];
718 pixelFormat = PIXEL_FORMAT_BGR24;
719 xorPixel = FreeRDPReadColor_int(xorBits, pixelFormat);
723 if (xorPixel == 0x00FFFFFFUL)
724 xorPixel |= 0xFF000000UL;
729 xorPixel |= 0xFF000000UL;
732 pixelFormat = PIXEL_FORMAT_ABGR32;
735 xorPixel = FreeRDPConvertColor(xorPixel, pixelFormat, PIXEL_FORMAT_ARGB32, palette);
736 xorBits += xorBytesPerPixel;
740 if (xorPixel == 0xFF000000UL)
741 xorPixel = 0x00000000;
742 else if (xorPixel == 0xFFFFFFFFUL)
743 xorPixel = freerdp_image_inverted_pointer_color(x, y, PIXEL_FORMAT_ARGB32);
746 color = FreeRDPConvertColor(xorPixel, PIXEL_FORMAT_ARGB32, DstFormat, palette);
747 if (!FreeRDPWriteColor_int(pDstPixel, DstFormat, color))
749 pDstPixel += FreeRDPGetBytesPerPixel(DstFormat);
764BOOL freerdp_image_copy_from_pointer_data_int(
765 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
766 UINT32 nWidth, UINT32 nHeight,
const BYTE* WINPR_RESTRICT xorMask, UINT32 xorMaskLength,
767 const BYTE* WINPR_RESTRICT andMask, UINT32 andMaskLength, UINT32 xorBpp,
768 const gdiPalette* WINPR_RESTRICT palette)
770 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
773 UINT32 dstBitsPerPixel = FreeRDPGetBitsPerPixel(DstFormat);
774 UINT32 dstBytesPerPixel = FreeRDPGetBytesPerPixel(DstFormat);
776 WINPR_ASSERT(1ull * nXDst + nWidth <= UINT32_MAX);
778 nDstStep = dstBytesPerPixel * (1ull * nXDst + nWidth);
780 WINPR_ASSERT(nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= (1ull * nXDst + nWidth));
782 for (UINT32 y = nYDst; y < nHeight; y++)
784 BYTE* WINPR_RESTRICT pDstLine = &pDstData[y * nDstStep + nXDst * dstBytesPerPixel];
785 memset(pDstLine, 0, 1ull * dstBytesPerPixel * (nWidth - nXDst));
791 return freerdp_image_copy_from_pointer_data_1bpp(
792 pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, xorMask,
793 xorMaskLength, andMask, andMaskLength, xorBpp);
799 return freerdp_image_copy_from_pointer_data_xbpp(
800 pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, xorMask,
801 xorMaskLength, andMask, andMaskLength, xorBpp, palette);
804 WLog_ERR(TAG,
"failed to convert from %" PRIu32
" bpp to %" PRIu32
" bpp", xorBpp,
810BOOL freerdp_image_copy_from_pointer_data(BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
811 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
812 UINT32 nWidth, UINT32 nHeight,
813 const BYTE* WINPR_RESTRICT xorMask, UINT32 xorMaskLength,
814 const BYTE* WINPR_RESTRICT andMask, UINT32 andMaskLength,
815 UINT32 xorBpp,
const gdiPalette* WINPR_RESTRICT palette)
817#if defined(WITH_CURSOR_DUMP)
818 dump_pointer_data(nXDst, nYDst, nWidth, nHeight, xorMask, xorMaskLength, andMask, andMaskLength,
821 return freerdp_image_copy_from_pointer_data_int(pDstData, DstFormat, nDstStep, nXDst, nYDst,
822 nWidth, nHeight, xorMask, xorMaskLength,
823 andMask, andMaskLength, xorBpp, palette);
826static inline BOOL overlapping(
const BYTE* pDstData, UINT32 nYDst, UINT32 nDstStep,
827 const BYTE* pSrcData, UINT32 nYSrc, UINT32 nSrcStep, UINT32 nHeight)
829 const uintptr_t src = (uintptr_t)pSrcData;
830 const uintptr_t srcstart = src + 1ULL * nSrcStep * nYSrc;
831 const uintptr_t srcend = srcstart + 1ULL * nSrcStep * nHeight;
832 const uintptr_t dst = (uintptr_t)pDstData;
833 const uintptr_t dststart = dst + 1ULL * nDstStep * nYDst;
834 const uintptr_t dstend = dststart + 1ULL * nDstStep * nHeight;
836 WINPR_ASSERT(nDstStep > 0);
837 WINPR_ASSERT(nSrcStep > 0);
838 if ((dststart >= srcstart) && (dststart < srcend))
841 if ((dstend > srcstart) && (dstend <= srcend))
847static inline BOOL freerdp_image_copy_bgr24_bgrx32(BYTE* WINPR_RESTRICT pDstData, UINT32 nDstStep,
848 UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
850 const BYTE* WINPR_RESTRICT pSrcData,
851 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
852 int64_t srcVMultiplier, int64_t srcVOffset,
853 int64_t dstVMultiplier, int64_t dstVOffset)
856 const int64_t srcByte = 3;
857 const int64_t dstByte = 4;
859 for (int64_t y = 0; y < nHeight; y++)
861 const BYTE* WINPR_RESTRICT srcLine =
862 &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
863 BYTE* WINPR_RESTRICT dstLine =
864 &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
866 for (int64_t x = 0; x < nWidth; x++)
868 dstLine[(x + nXDst) * dstByte + 0] = srcLine[(x + nXSrc) * srcByte + 0];
869 dstLine[(x + nXDst) * dstByte + 1] = srcLine[(x + nXSrc) * srcByte + 1];
870 dstLine[(x + nXDst) * dstByte + 2] = srcLine[(x + nXSrc) * srcByte + 2];
877static inline BOOL freerdp_image_copy_bgrx32_bgrx32(BYTE* WINPR_RESTRICT pDstData, UINT32 nDstStep,
878 UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
880 const BYTE* WINPR_RESTRICT pSrcData,
881 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
882 int64_t srcVMultiplier, int64_t srcVOffset,
883 int64_t dstVMultiplier, int64_t dstVOffset)
886 const int64_t srcByte = 4;
887 const int64_t dstByte = 4;
889 for (int64_t y = 0; y < nHeight; y++)
891 const BYTE* WINPR_RESTRICT srcLine =
892 &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
893 BYTE* WINPR_RESTRICT dstLine =
894 &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
896 for (int64_t x = 0; x < nWidth; x++)
898 dstLine[(x + nXDst) * dstByte + 0] = srcLine[(x + nXSrc) * srcByte + 0];
899 dstLine[(x + nXDst) * dstByte + 1] = srcLine[(x + nXSrc) * srcByte + 1];
900 dstLine[(x + nXDst) * dstByte + 2] = srcLine[(x + nXSrc) * srcByte + 2];
907static inline BOOL freerdp_image_copy_generic(
908 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
909 UINT32 nWidth, UINT32 nHeight,
const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcFormat,
910 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
const gdiPalette* WINPR_RESTRICT palette,
911 int64_t srcVMultiplier, int64_t srcVOffset, int64_t dstVMultiplier, int64_t dstVOffset)
914 const int64_t srcByte = 4;
915 const int64_t dstByte = 4;
917 for (int64_t y = 0; y < nHeight; y++)
919 const BYTE* WINPR_RESTRICT srcLine =
920 &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
921 BYTE* WINPR_RESTRICT dstLine =
922 &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
924 UINT32 color = FreeRDPReadColor_int(&srcLine[nXSrc * srcByte], SrcFormat);
925 UINT32 oldColor = color;
926 UINT32 dstColor = FreeRDPConvertColor(color, SrcFormat, DstFormat, palette);
927 if (!FreeRDPWriteColorIgnoreAlpha_int(&dstLine[nXDst * dstByte], DstFormat, dstColor))
930 for (int64_t x = 1; x < nWidth; x++)
932 color = FreeRDPReadColor_int(&srcLine[(x + nXSrc) * srcByte], SrcFormat);
933 if (color == oldColor)
935 if (!FreeRDPWriteColorIgnoreAlpha_int(&dstLine[(x + nXDst) * dstByte], DstFormat,
942 dstColor = FreeRDPConvertColor(color, SrcFormat, DstFormat, palette);
943 if (!FreeRDPWriteColorIgnoreAlpha_int(&dstLine[(x + nXDst) * dstByte], DstFormat,
953static inline BOOL freerdp_image_copy_no_overlap_dst_alpha(
954 BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
955 UINT32 nWidth, UINT32 nHeight,
const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
956 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
const gdiPalette* WINPR_RESTRICT palette,
957 int64_t srcVMultiplier, int64_t srcVOffset, int64_t dstVMultiplier, int64_t dstVOffset)
959 WINPR_ASSERT(pDstData);
960 WINPR_ASSERT(pSrcData);
964 case PIXEL_FORMAT_BGR24:
967 case PIXEL_FORMAT_BGRX32:
968 case PIXEL_FORMAT_BGRA32:
969 return freerdp_image_copy_bgr24_bgrx32(
970 pDstData, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, nSrcStep,
971 nXSrc, nYSrc, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
976 case PIXEL_FORMAT_BGRX32:
977 case PIXEL_FORMAT_BGRA32:
980 case PIXEL_FORMAT_BGRX32:
981 case PIXEL_FORMAT_BGRA32:
982 return freerdp_image_copy_bgrx32_bgrx32(
983 pDstData, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, nSrcStep,
984 nXSrc, nYSrc, srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
993 return freerdp_image_copy_generic(pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight,
994 pSrcData, SrcFormat, nSrcStep, nXSrc, nYSrc, palette,
995 srcVMultiplier, srcVOffset, dstVMultiplier, dstVOffset);
998BOOL freerdp_image_copy_overlap(BYTE* pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst,
999 UINT32 nYDst, UINT32 nWidth, UINT32 nHeight,
const BYTE* pSrcData,
1000 DWORD SrcFormat, UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
1001 const gdiPalette* WINPR_RESTRICT palette, UINT32 flags)
1003 const UINT32 dstByte = FreeRDPGetBytesPerPixel(DstFormat);
1004 const UINT32 srcByte = FreeRDPGetBytesPerPixel(SrcFormat);
1005 const UINT32 copyDstWidth = nWidth * dstByte;
1006 const UINT32 xSrcOffset = nXSrc * srcByte;
1007 const UINT32 xDstOffset = nXDst * dstByte;
1008 const BOOL vSrcVFlip = (flags & FREERDP_FLIP_VERTICAL) != 0;
1009 int64_t srcVOffset = 0;
1010 int64_t srcVMultiplier = 1;
1011 int64_t dstVOffset = 0;
1012 int64_t dstVMultiplier = 1;
1014 if ((nWidth == 0) || (nHeight == 0))
1017 if (FreeRDPGetBitsPerPixel(SrcFormat) == 0)
1019 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
1022 WINPR_ASSERT(1ull * nXDst + nWidth <= UINT32_MAX);
1023 WINPR_ASSERT(1ull * nXSrc + nWidth <= UINT32_MAX);
1024 WINPR_ASSERT(nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= nXDst + nWidth);
1025 WINPR_ASSERT(nSrcStep / FreeRDPGetBytesPerPixel(SrcFormat) >= nXSrc + nWidth);
1026 WINPR_ASSERT(overlapping(pDstData, nYDst, nDstStep, pSrcData, nYSrc, nSrcStep, nHeight));
1028 if ((nWidth == 0) || (nHeight == 0))
1031 if ((nHeight > INT32_MAX) || (nWidth > INT32_MAX))
1034 if (!pDstData || !pSrcData)
1038 nDstStep = (nXDst + nWidth) * FreeRDPGetBytesPerPixel(DstFormat);
1041 nSrcStep = (nXSrc + nWidth) * FreeRDPGetBytesPerPixel(SrcFormat);
1045 srcVOffset = (nHeight - 1ll) * nSrcStep;
1046 srcVMultiplier = -1;
1049 if (((flags & FREERDP_KEEP_DST_ALPHA) != 0) && FreeRDPColorHasAlpha(DstFormat))
1051 return freerdp_image_copy_no_overlap_dst_alpha(
1052 pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, pSrcData, SrcFormat,
1053 nSrcStep, nXSrc, nYSrc, palette, srcVMultiplier, srcVOffset, dstVMultiplier,
1056 else if (FreeRDPAreColorFormatsEqualNoAlpha_int(SrcFormat, DstFormat))
1061 for (int64_t y = 0; y < nHeight; y++)
1063 const BYTE* srcLine =
1064 &pSrcData[(y + nYSrc) * nSrcStep * srcVMultiplier + srcVOffset];
1065 BYTE* dstLine = &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
1066 memcpy(&dstLine[xDstOffset], &srcLine[xSrcOffset], copyDstWidth);
1070 else if (nYDst > nYSrc)
1072 for (int64_t y = nHeight - 1; y >= 0; y--)
1074 const BYTE* srcLine =
1075 &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
1076 BYTE* dstLine = &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
1077 memcpy(&dstLine[xDstOffset], &srcLine[xSrcOffset], copyDstWidth);
1081 else if (nXSrc > nXDst)
1083 for (int64_t y = 0; y < nHeight; y++)
1085 const BYTE* srcLine =
1086 &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
1087 BYTE* dstLine = &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
1088 memmove(&dstLine[xDstOffset], &srcLine[xSrcOffset], copyDstWidth);
1092 else if (nXSrc < nXDst)
1094 for (int64_t y = nHeight - 1; y >= 0; y--)
1096 const BYTE* srcLine =
1097 &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
1098 BYTE* dstLine = &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
1099 memmove(&dstLine[xDstOffset], &srcLine[xSrcOffset], copyDstWidth);
1109 for (int64_t y = 0; y < nHeight; y++)
1111 const BYTE* srcLine = &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
1112 BYTE* dstLine = &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
1114 UINT32 color = FreeRDPReadColor_int(&srcLine[1ULL * nXSrc * srcByte], SrcFormat);
1115 UINT32 oldColor = color;
1116 UINT32 dstColor = FreeRDPConvertColor(color, SrcFormat, DstFormat, palette);
1117 if (!FreeRDPWriteColor_int(&dstLine[1ULL * nXDst * dstByte], DstFormat, dstColor))
1120 for (int64_t x = 1; x < nWidth; x++)
1122 color = FreeRDPReadColor_int(&srcLine[(x + nXSrc) * srcByte], SrcFormat);
1123 if (color == oldColor)
1125 if (!FreeRDPWriteColor_int(&dstLine[(x + nXDst) * dstByte], DstFormat,
1132 dstColor = FreeRDPConvertColor(color, SrcFormat, DstFormat, palette);
1133 if (!FreeRDPWriteColor_int(&dstLine[(x + nXDst) * dstByte], DstFormat,
1144BOOL freerdp_image_copy(BYTE* pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nXDst,
1145 UINT32 nYDst, UINT32 nWidth, UINT32 nHeight,
const BYTE* pSrcData,
1146 DWORD SrcFormat, UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
1147 const gdiPalette* WINPR_RESTRICT palette, UINT32 flags)
1149 if ((nHeight > INT32_MAX) || (nWidth > INT32_MAX))
1152 if (!pDstData || !pSrcData)
1155 if ((nWidth == 0) || (nHeight == 0))
1158 if (FreeRDPGetBitsPerPixel(SrcFormat) == 0)
1160 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
1163 WINPR_ASSERT(1ull * nXDst + nWidth <= UINT32_MAX);
1164 WINPR_ASSERT(1ull * nXSrc + nWidth <= UINT32_MAX);
1166 nDstStep = (nXDst + nWidth) * FreeRDPGetBytesPerPixel(DstFormat);
1169 nSrcStep = (nXSrc + nWidth) * FreeRDPGetBytesPerPixel(SrcFormat);
1171 WINPR_ASSERT(nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= nXDst + nWidth);
1172 WINPR_ASSERT(nSrcStep / FreeRDPGetBytesPerPixel(SrcFormat) >= nXSrc + nWidth);
1174 const BOOL ovl = overlapping(pDstData, nYDst, nDstStep, pSrcData, nYSrc, nSrcStep, nHeight);
1176 return freerdp_image_copy_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth,
1177 nHeight, pSrcData, SrcFormat, nSrcStep, nXSrc, nYSrc,
1179 return freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth,
1180 nHeight, pSrcData, SrcFormat, nSrcStep, nXSrc, nYSrc,
1184BOOL freerdp_image_fill(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep,
1185 UINT32 nXDst, UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, UINT32 color)
1187 if ((nWidth == 0) || (nHeight == 0))
1190 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
1193 const UINT32 bpp = FreeRDPGetBytesPerPixel(DstFormat);
1194 BYTE* WINPR_RESTRICT pFirstDstLine =
nullptr;
1195 BYTE* WINPR_RESTRICT pFirstDstLineXOffset =
nullptr;
1197 WINPR_ASSERT(1ull * nXDst + nWidth <= UINT32_MAX);
1199 nDstStep = (nXDst + nWidth) * FreeRDPGetBytesPerPixel(DstFormat);
1201 WINPR_ASSERT(nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= nXDst + nWidth);
1202 pFirstDstLine = &pDstData[1ULL * nYDst * nDstStep];
1203 pFirstDstLineXOffset = &pFirstDstLine[1ULL * nXDst * bpp];
1205 for (
size_t x = 0; x < nWidth; x++)
1207 BYTE* pDst = &pFirstDstLine[(x + nXDst) * bpp];
1208 if (!FreeRDPWriteColor_int(pDst, DstFormat, color))
1212 for (
size_t y = 1; y < nHeight; y++)
1214 BYTE* pDstLine = &pDstData[(y + nYDst) * nDstStep + 1ULL * nXDst * bpp];
1215 memcpy(pDstLine, pFirstDstLineXOffset, 1ull * nWidth * bpp);
1221BOOL freerdp_image_fill_ex(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep,
1222 UINT32 nXDst, UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, UINT32 color,
1225 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
1228 WINPR_ASSERT(pDstData);
1229 WINPR_ASSERT(1ull * nXDst + nWidth <= UINT32_MAX);
1230 WINPR_ASSERT(nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= nXDst + nWidth);
1232 if (FreeRDPColorHasAlpha(DstFormat) && ((flags & FREERDP_IMAGE_FILL_IGNORE_ALPHA) != 0))
1234 const UINT32 bpp = FreeRDPGetBytesPerPixel(DstFormat);
1238 FreeRDPSplitColor(color, DstFormat, &r, &g, &b,
nullptr,
nullptr);
1240 for (
size_t y = 0; y < nHeight; y++)
1242 BYTE* WINPR_RESTRICT line = &pDstData[(y + nYDst) * nDstStep];
1244 for (
size_t x = 0; x < nWidth; x++)
1246 BYTE* WINPR_RESTRICT dst = &line[x * bpp];
1247 const UINT32 dcolor = FreeRDPReadColor_int(dst, DstFormat);
1249 FreeRDPSplitColor(dcolor, DstFormat,
nullptr,
nullptr,
nullptr, &a,
nullptr);
1250 const UINT32 scolor = FreeRDPGetColor(DstFormat, r, g, b, a);
1251 if (!FreeRDPWriteColor_int(dst, DstFormat, scolor))
1257 return freerdp_image_fill(pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, color);
1260#if defined(WITH_SWSCALE)
1261static enum AVPixelFormat av_format_for_buffer(UINT32 format)
1265 case PIXEL_FORMAT_ARGB32:
1266 return AV_PIX_FMT_BGRA;
1268 case PIXEL_FORMAT_XRGB32:
1269 return AV_PIX_FMT_BGR0;
1271 case PIXEL_FORMAT_BGRA32:
1272 return AV_PIX_FMT_RGBA;
1274 case PIXEL_FORMAT_BGRX32:
1275 return AV_PIX_FMT_RGB0;
1278 return AV_PIX_FMT_NONE;
1283BOOL freerdp_image_scale(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep,
1284 UINT32 nXDst, UINT32 nYDst, UINT32 nDstWidth, UINT32 nDstHeight,
1285 const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat, UINT32 nSrcStep,
1286 UINT32 nXSrc, UINT32 nYSrc, UINT32 nSrcWidth, UINT32 nSrcHeight)
1289 if (FreeRDPGetBitsPerPixel(SrcFormat) == 0)
1291 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
1294 WINPR_ASSERT(1ull * nXDst + nDstWidth <= UINT32_MAX);
1295 WINPR_ASSERT(1ull * nXSrc + nSrcWidth <= UINT32_MAX);
1297 nDstStep = (nXDst + nDstWidth) * FreeRDPGetBytesPerPixel(DstFormat);
1300 nSrcStep = (nXSrc + nSrcWidth) * FreeRDPGetBytesPerPixel(SrcFormat);
1302 WINPR_ASSERT(nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= nXDst + nDstWidth);
1303 WINPR_ASSERT(nSrcStep / FreeRDPGetBytesPerPixel(SrcFormat) >= nXSrc + nSrcWidth);
1305#if defined(WITH_SWSCALE) || defined(WITH_CAIRO)
1306 const BYTE* src = &pSrcData[nXSrc * FreeRDPGetBytesPerPixel(SrcFormat) + nYSrc * nSrcStep];
1307 BYTE* dst = &pDstData[nXDst * FreeRDPGetBytesPerPixel(DstFormat) + nYDst * nDstStep];
1311 if ((nDstWidth == nSrcWidth) && (nDstHeight == nSrcHeight))
1313 return freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, nDstWidth,
1314 nDstHeight, pSrcData, SrcFormat, nSrcStep, nXSrc,
1315 nYSrc,
nullptr, FREERDP_FLIP_NONE);
1318#if defined(WITH_SWSCALE)
1321 struct SwsContext* resize =
nullptr;
1322 enum AVPixelFormat srcFormat = av_format_for_buffer(SrcFormat);
1323 enum AVPixelFormat dstFormat = av_format_for_buffer(DstFormat);
1324 const int srcStep[1] = { (int)nSrcStep };
1325 const int dstStep[1] = { (int)nDstStep };
1327 if ((srcFormat == AV_PIX_FMT_NONE) || (dstFormat == AV_PIX_FMT_NONE))
1330 if (!freerdp_swscale_available())
1332 WLog_WARN(TAG,
"swscale not available");
1336 resize = freerdp_sws_getContext((
int)nSrcWidth, (
int)nSrcHeight, srcFormat, (
int)nDstWidth,
1337 (
int)nDstHeight, dstFormat, SWS_BILINEAR,
nullptr,
nullptr,
1344 const BYTE* srcSlice[4] = { src,
nullptr,
nullptr,
nullptr };
1345 BYTE* dstSlice[4] = { dst,
nullptr,
nullptr,
nullptr };
1346 const int srcSteps[4] = { srcStep[0], 0, 0, 0 };
1347 const int dstSteps[4] = { dstStep[0], 0, 0, 0 };
1349 res = freerdp_sws_scale(resize, srcSlice, srcSteps, 0, (
int)nSrcHeight, dstSlice, dstSteps);
1350 rc = (res == ((int)nDstHeight));
1352 freerdp_sws_freeContext(resize);
1355#elif defined(WITH_CAIRO)
1357 const double sx = (double)nDstWidth / (
double)nSrcWidth;
1358 const double sy = (double)nDstHeight / (
double)nSrcHeight;
1359 cairo_t* cairo_context;
1360 cairo_surface_t *csrc, *cdst;
1362 if ((nSrcWidth > INT_MAX) || (nSrcHeight > INT_MAX) || (nSrcStep > INT_MAX))
1365 if ((nDstWidth > INT_MAX) || (nDstHeight > INT_MAX) || (nDstStep > INT_MAX))
1368 csrc = cairo_image_surface_create_for_data((
void*)src, CAIRO_FORMAT_ARGB32, (
int)nSrcWidth,
1369 (
int)nSrcHeight, (
int)nSrcStep);
1370 cdst = cairo_image_surface_create_for_data(dst, CAIRO_FORMAT_ARGB32, (
int)nDstWidth,
1371 (
int)nDstHeight, (
int)nDstStep);
1376 cairo_context = cairo_create(cdst);
1381 cairo_scale(cairo_context, sx, sy);
1382 cairo_set_operator(cairo_context, CAIRO_OPERATOR_SOURCE);
1383 cairo_set_source_surface(cairo_context, csrc, 0, 0);
1384 cairo_paint(cairo_context);
1387 cairo_destroy(cairo_context);
1389 cairo_surface_destroy(csrc);
1390 cairo_surface_destroy(cdst);
1394 WLog_WARN(TAG,
"SmartScaling requested but compiled without libcairo support!");
1400DWORD FreeRDPAreColorFormatsEqualNoAlpha(DWORD first, DWORD second)
1402 return FreeRDPAreColorFormatsEqualNoAlpha_int(first, second);
1405const char* FreeRDPGetColorFormatName(UINT32 format)
1413 ENTRY(PIXEL_FORMAT_ARGB32)
1414 ENTRY(PIXEL_FORMAT_XRGB32)
1415 ENTRY(PIXEL_FORMAT_ABGR32)
1416 ENTRY(PIXEL_FORMAT_XBGR32)
1417 ENTRY(PIXEL_FORMAT_BGRA32)
1418 ENTRY(PIXEL_FORMAT_BGRX32)
1419 ENTRY(PIXEL_FORMAT_RGBA32)
1420 ENTRY(PIXEL_FORMAT_RGBX32)
1421 ENTRY(PIXEL_FORMAT_BGRX32_DEPTH30)
1422 ENTRY(PIXEL_FORMAT_RGBX32_DEPTH30)
1423 ENTRY(PIXEL_FORMAT_RGB24)
1424 ENTRY(PIXEL_FORMAT_BGR24)
1425 ENTRY(PIXEL_FORMAT_RGB16)
1426 ENTRY(PIXEL_FORMAT_BGR16)
1427 ENTRY(PIXEL_FORMAT_ARGB15)
1428 ENTRY(PIXEL_FORMAT_RGB15)
1429 ENTRY(PIXEL_FORMAT_ABGR15)
1430 ENTRY(PIXEL_FORMAT_BGR15)
1431 ENTRY(PIXEL_FORMAT_RGB8)
1432 ENTRY(PIXEL_FORMAT_A4)
1433 ENTRY(PIXEL_FORMAT_MONO)
1441uint32_t FreeRDPGetColorFromatFromName(
const char* name)
1444 if (strcmp(name, #x) == 0) \
1450 ENTRY(PIXEL_FORMAT_ARGB32)
1451 ENTRY(PIXEL_FORMAT_XRGB32)
1452 ENTRY(PIXEL_FORMAT_ABGR32)
1453 ENTRY(PIXEL_FORMAT_XBGR32)
1454 ENTRY(PIXEL_FORMAT_BGRA32)
1455 ENTRY(PIXEL_FORMAT_BGRX32)
1456 ENTRY(PIXEL_FORMAT_RGBA32)
1457 ENTRY(PIXEL_FORMAT_RGBX32)
1458 ENTRY(PIXEL_FORMAT_BGRX32_DEPTH30)
1459 ENTRY(PIXEL_FORMAT_RGBX32_DEPTH30)
1460 ENTRY(PIXEL_FORMAT_RGB24)
1461 ENTRY(PIXEL_FORMAT_BGR24)
1462 ENTRY(PIXEL_FORMAT_RGB16)
1463 ENTRY(PIXEL_FORMAT_BGR16)
1464 ENTRY(PIXEL_FORMAT_ARGB15)
1465 ENTRY(PIXEL_FORMAT_RGB15)
1466 ENTRY(PIXEL_FORMAT_ABGR15)
1467 ENTRY(PIXEL_FORMAT_BGR15)
1468 ENTRY(PIXEL_FORMAT_RGB8)
1469 ENTRY(PIXEL_FORMAT_A4)
1470 ENTRY(PIXEL_FORMAT_MONO)
1476void FreeRDPSplitColor(UINT32 color, UINT32 format, BYTE* _r, BYTE* _g, BYTE* _b, BYTE* _a,
1477 const gdiPalette* palette)
1484 case PIXEL_FORMAT_ARGB32:
1486 *_a = (BYTE)(color >> 24);
1489 *_r = (BYTE)(color >> 16);
1492 *_g = (BYTE)(color >> 8);
1499 case PIXEL_FORMAT_XRGB32:
1501 *_r = (BYTE)(color >> 16);
1504 *_g = (BYTE)(color >> 8);
1514 case PIXEL_FORMAT_ABGR32:
1516 *_a = (BYTE)(color >> 24);
1519 *_b = (BYTE)(color >> 16);
1522 *_g = (BYTE)(color >> 8);
1529 case PIXEL_FORMAT_XBGR32:
1531 *_b = (BYTE)(color >> 16);
1534 *_g = (BYTE)(color >> 8);
1544 case PIXEL_FORMAT_RGBA32:
1546 *_r = (BYTE)(color >> 24);
1549 *_g = (BYTE)(color >> 16);
1552 *_b = (BYTE)(color >> 8);
1559 case PIXEL_FORMAT_RGBX32:
1561 *_r = (BYTE)(color >> 24);
1564 *_g = (BYTE)(color >> 16);
1567 *_b = (BYTE)(color >> 8);
1574 case PIXEL_FORMAT_BGRA32:
1576 *_b = (BYTE)(color >> 24);
1579 *_g = (BYTE)(color >> 16);
1582 *_r = (BYTE)(color >> 8);
1589 case PIXEL_FORMAT_BGRX32:
1591 *_b = (BYTE)(color >> 24);
1594 *_g = (BYTE)(color >> 16);
1597 *_r = (BYTE)(color >> 8);
1605 case PIXEL_FORMAT_RGB24:
1607 *_r = (BYTE)(color >> 16);
1610 *_g = (BYTE)(color >> 8);
1620 case PIXEL_FORMAT_BGR24:
1622 *_b = (BYTE)(color >> 16);
1625 *_g = (BYTE)(color >> 8);
1636 case PIXEL_FORMAT_RGB16:
1639 const UINT32 c = (color >> 11) & 0x1F;
1640 const UINT32 val = (c << 3) + c / 4;
1641 *_r = (BYTE)(val > 255 ? 255 : val);
1646 const UINT32 c = (color >> 5) & 0x3F;
1647 const UINT32 val = (c << 2) + c / 4 / 2;
1648 *_g = (BYTE)(val > 255 ? 255 : val);
1653 const UINT32 c = (color)&0x1F;
1654 const UINT32 val = (c << 3) + c / 4;
1655 *_b = (BYTE)(val > 255 ? 255 : val);
1663 case PIXEL_FORMAT_BGR16:
1666 const UINT32 c = (color)&0x1F;
1667 const UINT32 val = (c << 3) + c / 4;
1668 *_r = (BYTE)(val > 255 ? 255 : val);
1673 const UINT32 c = (color >> 5) & 0x3F;
1674 const UINT32 val = (c << 2) + c / 4 / 2;
1675 *_g = (BYTE)(val > 255 ? 255 : val);
1680 const UINT32 c = (color >> 11) & 0x1F;
1681 const UINT32 val = (c << 3) + c / 4;
1682 *_b = (BYTE)(val > 255 ? 255 : val);
1690 case PIXEL_FORMAT_ARGB15:
1693 const UINT32 c = (color >> 10) & 0x1F;
1694 const UINT32 val = (c << 3) + c / 4;
1695 *_r = (BYTE)(val > 255 ? 255 : val);
1700 const UINT32 c = (color >> 5) & 0x1F;
1701 const UINT32 val = (c << 3) + c / 4;
1702 *_g = (BYTE)(val > 255 ? 255 : val);
1707 const UINT32 c = (color)&0x1F;
1708 const UINT32 val = (c << 3) + c / 4;
1709 *_b = (BYTE)(val > 255 ? 255 : val);
1713 *_a = color & 0x8000 ? 0xFF : 0x00;
1717 case PIXEL_FORMAT_ABGR15:
1720 const UINT32 c = (color)&0x1F;
1721 const UINT32 val = (c << 3) + c / 4;
1722 *_r = (BYTE)(val > 255 ? 255 : val);
1727 const UINT32 c = (color >> 5) & 0x1F;
1728 const UINT32 val = (c << 3) + c / 4;
1729 *_g = (BYTE)(val > 255 ? 255 : val);
1734 const UINT32 c = (color >> 10) & 0x1F;
1735 const UINT32 val = (c << 3) + c / 4;
1736 *_b = (BYTE)(val > 255 ? 255 : val);
1740 *_a = color & 0x8000 ? 0xFF : 0x00;
1745 case PIXEL_FORMAT_RGB15:
1748 const UINT32 c = (color >> 10) & 0x1F;
1749 const UINT32 val = (c << 3) + c / 4;
1750 *_r = (BYTE)(val > 255 ? 255 : val);
1755 const UINT32 c = (color >> 5) & 0x1F;
1756 const UINT32 val = (c << 3) + c / 4;
1757 *_g = (BYTE)(val > 255 ? 255 : val);
1762 const UINT32 c = (color)&0x1F;
1763 const UINT32 val = (c << 3) + c / 4;
1764 *_b = (BYTE)(val > 255 ? 255 : val);
1772 case PIXEL_FORMAT_BGR15:
1775 const UINT32 c = (color)&0x1F;
1776 const UINT32 val = (c << 3) + c / 4;
1777 *_r = (BYTE)(val > 255 ? 255 : val);
1782 const UINT32 c = (color >> 5) & 0x1F;
1783 const UINT32 val = (c << 3) + c / 4;
1784 *_g = (BYTE)(val > 255 ? 255 : val);
1789 const UINT32 c = (color >> 10) & 0x1F;
1790 const UINT32 val = (c << 3) + c / 4;
1791 *_b = (BYTE)(val > 255 ? 255 : val);
1800 case PIXEL_FORMAT_RGB8:
1803 tmp = palette->palette[color];
1804 FreeRDPSplitColor(tmp, palette->format, _r, _g, _b, _a,
nullptr);
1824 case PIXEL_FORMAT_MONO:
1826 *_r = (color) ? 0xFF : 0x00;
1829 *_g = (color) ? 0xFF : 0x00;
1832 *_b = (color) ? 0xFF : 0x00;
1835 *_a = (color) ? 0xFF : 0x00;
1840 case PIXEL_FORMAT_A4:
1854 WLog_ERR(TAG,
"Unsupported format %s", FreeRDPGetColorFormatName(format));
1859BOOL FreeRDPWriteColorIgnoreAlpha(BYTE* WINPR_RESTRICT dst, UINT32 format, UINT32 color)
1861 return FreeRDPWriteColorIgnoreAlpha_int(dst, format, color);
1864BOOL FreeRDPWriteColor(BYTE* WINPR_RESTRICT dst, UINT32 format, UINT32 color)
1866 return FreeRDPWriteColor_int(dst, format, color);
1869UINT32 FreeRDPReadColor(
const BYTE* WINPR_RESTRICT src, UINT32 format)
1871 return FreeRDPReadColor_int(src, format);
1874UINT32 FreeRDPGetColor(UINT32 format, BYTE r, BYTE g, BYTE b, BYTE a)
1885 case PIXEL_FORMAT_ARGB32:
1886 return (_a << 24) | (_r << 16) | (_g << 8) | _b;
1888 case PIXEL_FORMAT_XRGB32:
1889 return (_r << 16) | (_g << 8) | _b;
1891 case PIXEL_FORMAT_ABGR32:
1892 return (_a << 24) | (_b << 16) | (_g << 8) | _r;
1894 case PIXEL_FORMAT_XBGR32:
1895 return (_b << 16) | (_g << 8) | _r;
1897 case PIXEL_FORMAT_RGBA32:
1898 return (_r << 24) | (_g << 16) | (_b << 8) | _a;
1900 case PIXEL_FORMAT_RGBX32:
1901 return (_r << 24) | (_g << 16) | (_b << 8) | _a;
1903 case PIXEL_FORMAT_BGRA32:
1904 return (_b << 24) | (_g << 16) | (_r << 8) | _a;
1906 case PIXEL_FORMAT_BGRX32:
1907 return (_b << 24) | (_g << 16) | (_r << 8) | _a;
1909 case PIXEL_FORMAT_RGBX32_DEPTH30:
1911 t = (_r << 22) | (_g << 12) | (_b << 2);
1913 return ((t & 0xff) << 24) | (((t >> 8) & 0xff) << 16) | (((t >> 16) & 0xff) << 8) |
1916 case PIXEL_FORMAT_BGRX32_DEPTH30:
1918 t = (_r << 22) | (_g << 12) | (_b << 2);
1920 return ((t & 0xff) << 24) | (((t >> 8) & 0xff) << 16) | (((t >> 16) & 0xff) << 8) |
1924 case PIXEL_FORMAT_RGB24:
1925 return (_r << 16) | (_g << 8) | _b;
1927 case PIXEL_FORMAT_BGR24:
1928 return (_b << 16) | (_g << 8) | _r;
1931 case PIXEL_FORMAT_RGB16:
1932 return (((_r >> 3) & 0x1F) << 11) | (((_g >> 2) & 0x3F) << 5) | ((_b >> 3) & 0x1F);
1934 case PIXEL_FORMAT_BGR16:
1935 return (((_b >> 3) & 0x1F) << 11) | (((_g >> 2) & 0x3F) << 5) | ((_r >> 3) & 0x1F);
1937 case PIXEL_FORMAT_ARGB15:
1938 return (((_r >> 3) & 0x1F) << 10) | (((_g >> 3) & 0x1F) << 5) | ((_b >> 3) & 0x1F) |
1939 (_a ? 0x8000 : 0x0000);
1941 case PIXEL_FORMAT_ABGR15:
1942 return (((_b >> 3) & 0x1F) << 10) | (((_g >> 3) & 0x1F) << 5) | ((_r >> 3) & 0x1F) |
1943 (_a ? 0x8000 : 0x0000);
1946 case PIXEL_FORMAT_RGB15:
1947 return (((_r >> 3) & 0x1F) << 10) | (((_g >> 3) & 0x1F) << 5) | ((_b >> 3) & 0x1F);
1949 case PIXEL_FORMAT_BGR15:
1950 return (((_b >> 3) & 0x1F) << 10) | (((_g >> 3) & 0x1F) << 5) | ((_r >> 3) & 0x1F);
1953 case PIXEL_FORMAT_RGB8:
1956 case PIXEL_FORMAT_A4:
1959 case PIXEL_FORMAT_MONO:
1961 WLog_ERR(TAG,
"Unsupported format %s", FreeRDPGetColorFormatName(format));
1966BOOL freerdp_image_copy_no_overlap(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep,
1967 UINT32 nXDst, UINT32 nYDst, UINT32 nWidth, UINT32 nHeight,
1968 const BYTE* WINPR_RESTRICT pSrcData, DWORD SrcFormat,
1969 UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
1970 const gdiPalette* WINPR_RESTRICT palette, UINT32 flags)
1974 prims = primitives_get();
1976 if (FreeRDPGetBitsPerPixel(SrcFormat) == 0)
1978 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
1981 if ((nWidth == 0) || (nHeight == 0))
1984 WINPR_ASSERT(1ull * nXDst + nWidth <= UINT32_MAX);
1985 WINPR_ASSERT(1ull * nXSrc + nWidth <= UINT32_MAX);
1987 nDstStep = (nXDst + nWidth) * FreeRDPGetBytesPerPixel(DstFormat);
1989 nSrcStep = (nXSrc + nWidth) * FreeRDPGetBytesPerPixel(SrcFormat);
1991 WINPR_ASSERT(nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= nXDst + nWidth);
1992 WINPR_ASSERT(nSrcStep / FreeRDPGetBytesPerPixel(SrcFormat) >= nXSrc + nWidth);
1993 WINPR_ASSERT(!overlapping(pDstData, nYDst, nDstStep, pSrcData, nYSrc, nSrcStep, nHeight));
1994 WINPR_ASSERT(prims);
1996 return prims->
copy_no_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight,
1997 pSrcData, SrcFormat, nSrcStep, nXSrc, nYSrc, palette,
1998 flags) == PRIMITIVES_SUCCESS;
WINPR_ATTR_NODISCARD fn_copy_no_overlap_t copy_no_overlap