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>
56WINPR_ATTR_MALLOC(free, 1)
57static
char* get_dump_name(
char* prefix,
size_t len)
59 static uint64_t count = 0;
60 if (_snprintf(prefix, len,
"cursor_dump_%08" PRIx64, count++) < 0)
63 return GetCombinedPath(CURSOR_DUMP_DIR, prefix);
66static void dump_binary_data(FILE* fp,
const uint8_t* data,
size_t len)
69 (void)fprintf(fp,
"0x%02" PRIx8, data[0]);
70 for (
size_t x = 1; x < len; x++)
72 (void)fprintf(fp,
", 0x%02" PRIx8, data[x]);
76static void dump_uint_data(FILE* fp,
const uint32_t* data,
size_t len)
79 (void)fprintf(fp,
"0x%08" PRIx32, data[0]);
80 for (
size_t x = 1; x < len; x++)
82 (void)fprintf(fp,
", 0x%08" PRIx32, data[x]);
86static void dump_write_header(
const char* path,
const char* prefix)
88 char* header =
nullptr;
90 winpr_asprintf(&header, &headerlen,
"%s.h", path);
93 FILE* fp = fopen(header,
"w");
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);
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,
121 const uint32_t format = PIXEL_FORMAT_BGRA32;
122 const uint32_t bpp = FreeRDPGetBytesPerPixel(format);
123 const uint32_t step = nWidth * bpp;
125 char* header =
nullptr;
126 size_t headerlen = 0;
127 winpr_asprintf(&header, &headerlen,
"%s.c", path);
130 FILE* fp = fopen(header,
"w");
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");
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");
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);
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");
161 (
void)fprintf(fp,
"0");
163 (void)fprintf(fp,
"};\n");
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");
193 wImage* img = winpr_image_new();
196 img->data = WINPR_CAST_CONST_PTR_AWAY(bmp, BYTE*);
197 img->bitsPerPixel = 32;
198 img->bytesPerPixel = 4;
199 img->height = nHeight;
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);
207 winpr_image_write(img, imgname);
209 winpr_image_free(img, FALSE);
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)
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));
233 if (!freerdp_image_copy_from_pointer_data_int(bmp, format, step, 0, 0, nWidth, nHeight, xorMask,
234 xorMaskLength, andMask, andMaskLength, xorBpp,
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);
248#if !defined(WITHOUT_FREERDP_3x_DEPRECATED)
249BYTE* freerdp_glyph_convert(UINT32 width, UINT32 height,
const BYTE* WINPR_RESTRICT data)
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);
257BYTE* freerdp_glyph_convert_ex(UINT32 width, UINT32 height,
const BYTE* WINPR_RESTRICT data,
265 const size_t scanline = (width + 7ull) / 8ull;
266 const size_t required = scanline * height;
270 if ((len == 0) || (width == 0) || (height == 0))
275 BYTE* dstData = (BYTE*)winpr_aligned_calloc(width, height, 16);
280 ZeroMemory(dstData, 1ULL * width * height);
281 BYTE* dstp = dstData;
283 for (UINT32 y = 0; y < height; y++)
285 const BYTE* srcp = &data[1ull * y * scanline];
287 for (UINT32 x = 0; x < width; x++)
289 if ((*srcp & (0x80 >> (x % 8))) != 0)
294 if (((x + 1) % 8 == 0) && x != 0)
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)
308 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
310 const UINT32 dstBytesPerPixel = FreeRDPGetBytesPerPixel(DstFormat);
312 if (!pDstData || !pSrcData || !palette)
316 nDstStep = dstBytesPerPixel * nWidth;
318 WINPR_ASSERT(1ull * nXDst + nWidth <= UINT32_MAX);
319 WINPR_ASSERT(nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= nXDst + nWidth);
321 const UINT32 monoStep = (nWidth + 7) / 8;
323 for (
size_t y = 0; y < nHeight; y++)
325 BYTE* pDstLine = &pDstData[((nYDst + y) * nDstStep)];
326 UINT32 monoBit = 0x80;
327 const BYTE* monoBits = &pSrcData[monoStep * y];
329 for (
size_t x = 0; x < nWidth; x++)
331 BYTE* pDstPixel = &pDstLine[((nXDst + x) * FreeRDPGetBytesPerPixel(DstFormat))];
332 BOOL monoPixel = (*monoBits & monoBit) != 0;
334 if (!(monoBit >>= 1))
342 if (!FreeRDPWriteColor_int(pDstPixel, DstFormat, backColor))
345 else if (!FreeRDPWriteColor_int(pDstPixel, DstFormat, foreColor))
353static inline UINT32 freerdp_image_inverted_pointer_color(UINT32 x, UINT32 y, UINT32 format)
364 BYTE fill = (x + y) & 1 ? 0x00 : 0xFF;
365 return FreeRDPGetColor(format, fill, fill, fill, 0xFF);
372static void fill_gdi_palette_for_icon(
const BYTE* colorTable, UINT16 cbColorTable,
375 WINPR_ASSERT(palette);
377 palette->format = PIXEL_FORMAT_BGRX32;
378 ZeroMemory(palette->palette,
sizeof(palette->palette));
383 if ((cbColorTable % 4 != 0) || (cbColorTable / 4 > 256))
385 WLog_WARN(TAG,
"weird palette size: %u", cbColorTable);
389 for (UINT16 i = 0; i < cbColorTable / 4; i++)
391 palette->palette[i] = FreeRDPReadColor_int(&colorTable[4ULL * i], palette->format);
395static inline UINT32 div_ceil(UINT32 a, UINT32 b)
397 return (a + (b - 1)) / b;
400static inline UINT32 round_up(UINT32 a, UINT32 b)
402 return b * div_ceil(a, b);
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)
415 if (!pDstData || !bitsColor)
418 if ((nWidth == 0) || (nHeight == 0))
420 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
423 WINPR_ASSERT((nDstStep == 0) || (nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= nWidth));
441 WLog_WARN(TAG,
"1bpp and 4bpp icons are not supported");
445 format = PIXEL_FORMAT_RGB8;
449 format = PIXEL_FORMAT_RGB15;
453 format = PIXEL_FORMAT_RGB24;
457 format = PIXEL_FORMAT_BGRA32;
461 WLog_WARN(TAG,
"invalid icon bpp: %" PRIu32, bpp);
466 if ((1ul * cbBitsColor) / nHeight < (1ul * FreeRDPGetBytesPerPixel(format) * nWidth))
469 "cbBitsColor{%" PRIu16
"} < nWidth{%" PRIu16
"} * nHeight{%" PRIu16
470 "} * bpp{%" PRIu32
"}",
471 cbBitsColor, nWidth, nHeight, FreeRDPGetBytesPerPixel(format));
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))
481 if (FreeRDPColorHasAlpha(DstFormat) && !FreeRDPColorHasAlpha(format) && (cbBitsMask > 0))
483 BYTE* dstBuf = pDstData;
484 UINT32 dstBpp = FreeRDPGetBytesPerPixel(DstFormat);
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)
495 WLog_ERR(TAG,
"cbBitsMask{%" PRIu32
"} < required mask size{%" PRIuz
"}", cbBitsMask,
500 for (UINT32 y = 0; y < nHeight; y++)
502 const BYTE* maskByte = &bitsMask[stride * (nHeight - 1ULL - y)];
505 for (UINT32 x = 0; x < nWidth; x++)
510 BYTE alpha = (*maskByte & nextBit) ? 0x00 : 0xFF;
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))
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)
546 vFlip = (xorBpp != 1);
547 andStep = (nWidth + 7) / 8;
548 andStep += (andStep % 2);
550 if (!xorMask || (xorMaskLength == 0))
552 if (!andMask || (andMaskLength == 0))
555 xorStep = (nWidth + 7) / 8;
556 xorStep += (xorStep % 2);
558 if (xorStep * nHeight > xorMaskLength)
561 if (andStep * nHeight > andMaskLength)
564 for (UINT32 y = 0; y < nHeight; y++)
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;
574 xorBits = &xorMask[1ULL * xorStep * y];
575 andBits = &andMask[1ULL * andStep * y];
579 xorBits = &xorMask[1ULL * xorStep * (nHeight - y - 1)];
580 andBits = &andMask[1ULL * andStep * (nHeight - y - 1)];
583 for (UINT32 x = 0; x < nWidth; x++)
586 xorPixel = (*xorBits & xorBit) ? 1 : 0;
594 andPixel = (*andBits & andBit) ? 1 : 0;
602 if (!andPixel && !xorPixel)
603 color = FreeRDPGetColor(DstFormat, 0, 0, 0, 0xFF);
604 else if (!andPixel && xorPixel)
605 color = FreeRDPGetColor(DstFormat, 0xFF, 0xFF, 0xFF, 0xFF);
606 else if (andPixel && !xorPixel)
607 color = FreeRDPGetColor(DstFormat, 0, 0, 0, 0);
608 else if (andPixel && xorPixel)
609 color = freerdp_image_inverted_pointer_color(x, y, DstFormat);
611 if (!FreeRDPWriteColor_int(pDstPixel, DstFormat, color))
613 pDstPixel += FreeRDPGetBytesPerPixel(DstFormat);
622static BOOL freerdp_image_palette_is_valid(
const gdiPalette* WINPR_RESTRICT palette)
626 WLog_ERR(TAG,
"%s data requires a palette", FreeRDPGetColorFormatName(PIXEL_FORMAT_RGB8));
630 if (FreeRDPGetBitsPerPixel(palette->format) <= 8)
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));
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)
653 UINT32 dstBitsPerPixel = 0;
654 UINT32 xorBytesPerPixel = 0;
655 dstBitsPerPixel = FreeRDPGetBitsPerPixel(DstFormat);
657 vFlip = (xorBpp != 1);
658 andStep = (nWidth + 7) / 8;
659 andStep += (andStep % 2);
661 if (!xorMask || (xorMaskLength == 0))
664 xorBytesPerPixel = xorBpp >> 3;
665 xorStep = 1ULL * nWidth * xorBytesPerPixel;
666 xorStep += (xorStep % 2);
668 if (xorBpp == 8 && !freerdp_image_palette_is_valid(palette))
670 WLog_ERR(TAG,
"invalid palette in conversion from %" PRIu32
" bpp to %" PRIu32
" bpp",
671 xorBpp, dstBitsPerPixel);
675 if (xorStep * nHeight > xorMaskLength)
680 if (andStep * nHeight > andMaskLength)
684 for (UINT32 y = 0; y < nHeight; y++)
686 const BYTE* xorBits =
nullptr;
687 const BYTE* andBits =
nullptr;
688 BYTE* pDstPixel = &pDstData[((1ULL * nYDst + y) * nDstStep) +
689 (1ULL * nXDst * FreeRDPGetBytesPerPixel(DstFormat))];
695 andBits = &andMask[andStep * y];
697 xorBits = &xorMask[xorStep * y];
702 andBits = &andMask[1ULL * andStep * (nHeight - y - 1)];
704 xorBits = &xorMask[1ULL * xorStep * (nHeight - y - 1)];
707 for (UINT32 x = 0; x < nWidth; x++)
709 UINT32 pixelFormat = 0;
716 andPixel = (*andBits & andBit) ? 1 : 0;
727 pixelFormat = PIXEL_FORMAT_BGRA32;
728 xorPixel = FreeRDPReadColor_int(xorBits, pixelFormat);
730 else if (xorBpp == 16)
732 pixelFormat = PIXEL_FORMAT_RGB15;
733 xorPixel = FreeRDPReadColor_int(xorBits, pixelFormat);
735 else if (xorBpp == 8)
737 pixelFormat = palette->format;
738 xorPixel = palette->palette[xorBits[0]];
742 pixelFormat = PIXEL_FORMAT_BGR24;
743 xorPixel = FreeRDPReadColor_int(xorBits, pixelFormat);
747 if (xorPixel == 0x00FFFFFFUL)
748 xorPixel |= 0xFF000000UL;
753 xorPixel |= 0xFF000000UL;
756 pixelFormat = PIXEL_FORMAT_ABGR32;
759 xorPixel = FreeRDPConvertColor(xorPixel, pixelFormat, PIXEL_FORMAT_ARGB32, palette);
760 xorBits += xorBytesPerPixel;
764 if (xorPixel == 0xFF000000UL)
765 xorPixel = 0x00000000;
766 else if (xorPixel == 0xFFFFFFFFUL)
767 xorPixel = freerdp_image_inverted_pointer_color(x, y, PIXEL_FORMAT_ARGB32);
770 color = FreeRDPConvertColor(xorPixel, PIXEL_FORMAT_ARGB32, DstFormat, palette);
771 if (!FreeRDPWriteColor_int(pDstPixel, DstFormat, color))
773 pDstPixel += FreeRDPGetBytesPerPixel(DstFormat);
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)
794 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
797 UINT32 dstBitsPerPixel = FreeRDPGetBitsPerPixel(DstFormat);
798 UINT32 dstBytesPerPixel = FreeRDPGetBytesPerPixel(DstFormat);
800 WINPR_ASSERT(1ull * nXDst + nWidth <= UINT32_MAX);
802 nDstStep = dstBytesPerPixel * (1ull * nXDst + nWidth);
804 WINPR_ASSERT(nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= (1ull * nXDst + nWidth));
806 for (UINT32 y = nYDst; y < nHeight; y++)
808 BYTE* WINPR_RESTRICT pDstLine = &pDstData[y * nDstStep + nXDst * dstBytesPerPixel];
809 memset(pDstLine, 0, 1ull * dstBytesPerPixel * (nWidth - nXDst));
815 return freerdp_image_copy_from_pointer_data_1bpp(
816 pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, xorMask,
817 xorMaskLength, andMask, andMaskLength, xorBpp);
823 return freerdp_image_copy_from_pointer_data_xbpp(
824 pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, xorMask,
825 xorMaskLength, andMask, andMaskLength, xorBpp, palette);
828 WLog_ERR(TAG,
"failed to convert from %" PRIu32
" bpp to %" PRIu32
" bpp", xorBpp,
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)
841#if defined(WITH_CURSOR_DUMP)
842 dump_pointer_data(nXDst, nYDst, nWidth, nHeight, xorMask, xorMaskLength, andMask, andMaskLength,
845 return freerdp_image_copy_from_pointer_data_int(pDstData, DstFormat, nDstStep, nXDst, nYDst,
846 nWidth, nHeight, xorMask, xorMaskLength,
847 andMask, andMaskLength, xorBpp, palette);
850static inline BOOL overlapping(
const BYTE* pDstData, UINT32 nYDst, UINT32 nDstStep,
851 const BYTE* pSrcData, UINT32 nYSrc, UINT32 nSrcStep, UINT32 nHeight)
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;
860 WINPR_ASSERT(nDstStep > 0);
861 WINPR_ASSERT(nSrcStep > 0);
862 if ((dststart >= srcstart) && (dststart < srcend))
865 if ((dstend > srcstart) && (dstend <= srcend))
871static inline BOOL freerdp_image_copy_bgr24_bgrx32(BYTE* WINPR_RESTRICT pDstData, UINT32 nDstStep,
872 UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
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)
880 const int64_t srcByte = 3;
881 const int64_t dstByte = 4;
883 for (int64_t y = 0; y < nHeight; y++)
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];
890 for (int64_t x = 0; x < nWidth; x++)
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];
901static inline BOOL freerdp_image_copy_bgrx32_bgrx32(BYTE* WINPR_RESTRICT pDstData, UINT32 nDstStep,
902 UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
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)
910 const int64_t srcByte = 4;
911 const int64_t dstByte = 4;
913 for (int64_t y = 0; y < nHeight; y++)
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];
920 for (int64_t x = 0; x < nWidth; x++)
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];
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)
938 const int64_t srcByte = 4;
939 const int64_t dstByte = 4;
941 for (int64_t y = 0; y < nHeight; y++)
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];
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))
954 for (int64_t x = 1; x < nWidth; x++)
956 color = FreeRDPReadColor_int(&srcLine[(x + nXSrc) * srcByte], SrcFormat);
957 if (color == oldColor)
959 if (!FreeRDPWriteColorIgnoreAlpha_int(&dstLine[(x + nXDst) * dstByte], DstFormat,
966 dstColor = FreeRDPConvertColor(color, SrcFormat, DstFormat, palette);
967 if (!FreeRDPWriteColorIgnoreAlpha_int(&dstLine[(x + nXDst) * dstByte], DstFormat,
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)
983 WINPR_ASSERT(pDstData);
984 WINPR_ASSERT(pSrcData);
988 case PIXEL_FORMAT_BGR24:
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);
1000 case PIXEL_FORMAT_BGRX32:
1001 case PIXEL_FORMAT_BGRA32:
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);
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);
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)
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;
1038 if ((nWidth == 0) || (nHeight == 0))
1041 if (FreeRDPGetBitsPerPixel(SrcFormat) == 0)
1043 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
1045 if ((SrcFormat == PIXEL_FORMAT_RGB8) && (SrcFormat != DstFormat) &&
1046 !freerdp_image_palette_is_valid(palette))
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));
1055 if ((nWidth == 0) || (nHeight == 0))
1058 if ((nHeight > INT32_MAX) || (nWidth > INT32_MAX))
1061 if (!pDstData || !pSrcData)
1065 nDstStep = (nXDst + nWidth) * FreeRDPGetBytesPerPixel(DstFormat);
1068 nSrcStep = (nXSrc + nWidth) * FreeRDPGetBytesPerPixel(SrcFormat);
1072 srcVOffset = (nHeight - 1ll) * nSrcStep;
1073 srcVMultiplier = -1;
1076 if (((flags & FREERDP_KEEP_DST_ALPHA) != 0) && FreeRDPColorHasAlpha(DstFormat))
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,
1083 else if (FreeRDPAreColorFormatsEqualNoAlpha_int(SrcFormat, DstFormat))
1088 for (int64_t y = 0; y < nHeight; y++)
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);
1097 else if (nYDst > nYSrc)
1099 for (int64_t y = nHeight - 1; y >= 0; y--)
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);
1108 else if (nXSrc > nXDst)
1110 for (int64_t y = 0; y < nHeight; y++)
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);
1119 else if (nXSrc < nXDst)
1121 for (int64_t y = nHeight - 1; y >= 0; y--)
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);
1136 for (int64_t y = 0; y < nHeight; y++)
1138 const BYTE* srcLine = &pSrcData[srcVMultiplier * (y + nYSrc) * nSrcStep + srcVOffset];
1139 BYTE* dstLine = &pDstData[dstVMultiplier * (y + nYDst) * nDstStep + dstVOffset];
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))
1147 for (int64_t x = 1; x < nWidth; x++)
1149 color = FreeRDPReadColor_int(&srcLine[(x + nXSrc) * srcByte], SrcFormat);
1150 if (color == oldColor)
1152 if (!FreeRDPWriteColor_int(&dstLine[(x + nXDst) * dstByte], DstFormat,
1159 dstColor = FreeRDPConvertColor(color, SrcFormat, DstFormat, palette);
1160 if (!FreeRDPWriteColor_int(&dstLine[(x + nXDst) * dstByte], DstFormat,
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)
1176 if ((nHeight > INT32_MAX) || (nWidth > INT32_MAX))
1179 if (!pDstData || !pSrcData)
1182 if ((nWidth == 0) || (nHeight == 0))
1185 if (FreeRDPGetBitsPerPixel(SrcFormat) == 0)
1187 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
1190 WINPR_ASSERT(1ull * nXDst + nWidth <= UINT32_MAX);
1191 WINPR_ASSERT(1ull * nXSrc + nWidth <= UINT32_MAX);
1193 nDstStep = (nXDst + nWidth) * FreeRDPGetBytesPerPixel(DstFormat);
1196 nSrcStep = (nXSrc + nWidth) * FreeRDPGetBytesPerPixel(SrcFormat);
1198 WINPR_ASSERT(nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= nXDst + nWidth);
1199 WINPR_ASSERT(nSrcStep / FreeRDPGetBytesPerPixel(SrcFormat) >= nXSrc + nWidth);
1201 const BOOL ovl = overlapping(pDstData, nYDst, nDstStep, pSrcData, nYSrc, nSrcStep, nHeight);
1203 return freerdp_image_copy_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth,
1204 nHeight, pSrcData, SrcFormat, nSrcStep, nXSrc, nYSrc,
1206 return freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth,
1207 nHeight, pSrcData, SrcFormat, nSrcStep, nXSrc, nYSrc,
1211BOOL freerdp_image_fill(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep,
1212 UINT32 nXDst, UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, UINT32 color)
1214 if ((nWidth == 0) || (nHeight == 0))
1217 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
1220 const UINT32 bpp = FreeRDPGetBytesPerPixel(DstFormat);
1221 BYTE* WINPR_RESTRICT pFirstDstLine =
nullptr;
1222 BYTE* WINPR_RESTRICT pFirstDstLineXOffset =
nullptr;
1224 WINPR_ASSERT(1ull * nXDst + nWidth <= UINT32_MAX);
1226 nDstStep = (nXDst + nWidth) * FreeRDPGetBytesPerPixel(DstFormat);
1228 WINPR_ASSERT(nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= nXDst + nWidth);
1229 pFirstDstLine = &pDstData[1ULL * nYDst * nDstStep];
1230 pFirstDstLineXOffset = &pFirstDstLine[1ULL * nXDst * bpp];
1232 for (
size_t x = 0; x < nWidth; x++)
1234 BYTE* pDst = &pFirstDstLine[(x + nXDst) * bpp];
1235 if (!FreeRDPWriteColor_int(pDst, DstFormat, color))
1239 for (
size_t y = 1; y < nHeight; y++)
1241 BYTE* pDstLine = &pDstData[(y + nYDst) * nDstStep + 1ULL * nXDst * bpp];
1242 memcpy(pDstLine, pFirstDstLineXOffset, 1ull * nWidth * bpp);
1248BOOL freerdp_image_fill_ex(BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat, UINT32 nDstStep,
1249 UINT32 nXDst, UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, UINT32 color,
1252 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
1255 WINPR_ASSERT(pDstData);
1256 WINPR_ASSERT(1ull * nXDst + nWidth <= UINT32_MAX);
1257 WINPR_ASSERT(nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= nXDst + nWidth);
1259 if (FreeRDPColorHasAlpha(DstFormat) && ((flags & FREERDP_IMAGE_FILL_IGNORE_ALPHA) != 0))
1261 const UINT32 bpp = FreeRDPGetBytesPerPixel(DstFormat);
1265 FreeRDPSplitColor(color, DstFormat, &r, &g, &b,
nullptr,
nullptr);
1267 for (
size_t y = 0; y < nHeight; y++)
1269 BYTE* WINPR_RESTRICT line = &pDstData[(y + nYDst) * nDstStep];
1271 for (
size_t x = 0; x < nWidth; x++)
1273 BYTE* WINPR_RESTRICT dst = &line[x * bpp];
1274 const UINT32 dcolor = FreeRDPReadColor_int(dst, DstFormat);
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))
1284 return freerdp_image_fill(pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, color);
1287#if defined(WITH_SWSCALE)
1288static enum AVPixelFormat av_format_for_buffer(UINT32 format)
1292 case PIXEL_FORMAT_ARGB32:
1293 return AV_PIX_FMT_BGRA;
1295 case PIXEL_FORMAT_XRGB32:
1296 return AV_PIX_FMT_BGR0;
1298 case PIXEL_FORMAT_BGRA32:
1299 return AV_PIX_FMT_RGBA;
1301 case PIXEL_FORMAT_BGRX32:
1302 return AV_PIX_FMT_RGB0;
1305 return AV_PIX_FMT_NONE;
1310static freerdp_image_scale_fkt s_scale_fkt =
nullptr;
1311static void* s_scale_user_arg =
nullptr;
1313BOOL freerdp_set_image_scaling_function(freerdp_image_scale_fkt scale,
void* user)
1315 s_scale_fkt = scale;
1316 s_scale_user_arg = user;
1320BOOL freerdp_image_scaling_supported(
void)
1325#if defined(WITH_SWSCALE) || defined(WITH_CAIRO)
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)
1338 return s_scale_fkt(pDstData, DstFormat, nDstStep, nXDst, nYDst, nDstWidth, nDstHeight,
1339 pSrcData, SrcFormat, nSrcStep, nXSrc, nYSrc, nSrcWidth, nSrcHeight,
1343 if (FreeRDPGetBitsPerPixel(SrcFormat) == 0)
1345 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
1348 WINPR_ASSERT(1ull * nXDst + nDstWidth <= UINT32_MAX);
1349 WINPR_ASSERT(1ull * nXSrc + nSrcWidth <= UINT32_MAX);
1351 nDstStep = (nXDst + nDstWidth) * FreeRDPGetBytesPerPixel(DstFormat);
1354 nSrcStep = (nXSrc + nSrcWidth) * FreeRDPGetBytesPerPixel(SrcFormat);
1356 WINPR_ASSERT(nDstStep / FreeRDPGetBytesPerPixel(DstFormat) >= nXDst + nDstWidth);
1357 WINPR_ASSERT(nSrcStep / FreeRDPGetBytesPerPixel(SrcFormat) >= nXSrc + nSrcWidth);
1359#if defined(WITH_SWSCALE) || defined(WITH_CAIRO)
1361 &pSrcData[1ULL * nXSrc * FreeRDPGetBytesPerPixel(SrcFormat) + 1ULL * nYSrc * nSrcStep];
1363 &pDstData[1ULL * nXDst * FreeRDPGetBytesPerPixel(DstFormat) + 1ULL * nYDst * nDstStep];
1367 if ((nDstWidth == nSrcWidth) && (nDstHeight == nSrcHeight))
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);
1374#if defined(WITH_SWSCALE)
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 };
1383 if ((srcFormat == AV_PIX_FMT_NONE) || (dstFormat == AV_PIX_FMT_NONE))
1386 if (!freerdp_swscale_available())
1388 WLog_WARN(TAG,
"swscale not available");
1395 if ((1ULL * nSrcStep * nSrcHeight > INT32_MAX) ||
1396 (1ULL * nDstStep * nDstHeight > INT32_MAX))
1399 "image too large to scale: source %" PRIu32
" x %" PRIu32
1400 " bytes, destination %" PRIu32
" x %" PRIu32
" bytes",
1401 nSrcStep, nSrcHeight, nDstStep, nDstHeight);
1405 resize = freerdp_sws_getContext((
int)nSrcWidth, (
int)nSrcHeight, srcFormat, (
int)nDstWidth,
1406 (
int)nDstHeight, dstFormat, SWS_BILINEAR,
nullptr,
nullptr,
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 };
1418 res = freerdp_sws_scale(resize, srcSlice, srcSteps, 0, (
int)nSrcHeight, dstSlice, dstSteps);
1419 rc = (res == ((int)nDstHeight));
1421 freerdp_sws_freeContext(resize);
1424#elif defined(WITH_CAIRO)
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;
1431 if ((nSrcWidth > INT_MAX) || (nSrcHeight > INT_MAX) || (nSrcStep > INT_MAX))
1434 if ((nDstWidth > INT_MAX) || (nDstHeight > INT_MAX) || (nDstStep > INT_MAX))
1438 if ((1ULL * nSrcStep * nSrcHeight > INT_MAX) || (1ULL * nDstStep * nDstHeight > INT_MAX))
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);
1449 cairo_context = cairo_create(cdst);
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);
1460 cairo_destroy(cairo_context);
1462 cairo_surface_destroy(csrc);
1463 cairo_surface_destroy(cdst);
1467 WLog_WARN(TAG,
"SmartScaling requested but compiled without libcairo support!");
1473DWORD FreeRDPAreColorFormatsEqualNoAlpha(DWORD first, DWORD second)
1475 return FreeRDPAreColorFormatsEqualNoAlpha_int(first, second);
1478const char* FreeRDPGetColorFormatName(UINT32 format)
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)
1514uint32_t FreeRDPGetColorFromatFromName(
const char* name)
1517 if (strcmp(name, #x) == 0) \
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)
1549void FreeRDPSplitColor(UINT32 color, UINT32 format, BYTE* _r, BYTE* _g, BYTE* _b, BYTE* _a,
1550 const gdiPalette* palette)
1557 case PIXEL_FORMAT_ARGB32:
1559 *_a = (BYTE)(color >> 24);
1562 *_r = (BYTE)(color >> 16);
1565 *_g = (BYTE)(color >> 8);
1572 case PIXEL_FORMAT_XRGB32:
1574 *_r = (BYTE)(color >> 16);
1577 *_g = (BYTE)(color >> 8);
1587 case PIXEL_FORMAT_ABGR32:
1589 *_a = (BYTE)(color >> 24);
1592 *_b = (BYTE)(color >> 16);
1595 *_g = (BYTE)(color >> 8);
1602 case PIXEL_FORMAT_XBGR32:
1604 *_b = (BYTE)(color >> 16);
1607 *_g = (BYTE)(color >> 8);
1617 case PIXEL_FORMAT_RGBA32:
1619 *_r = (BYTE)(color >> 24);
1622 *_g = (BYTE)(color >> 16);
1625 *_b = (BYTE)(color >> 8);
1632 case PIXEL_FORMAT_RGBX32:
1634 *_r = (BYTE)(color >> 24);
1637 *_g = (BYTE)(color >> 16);
1640 *_b = (BYTE)(color >> 8);
1647 case PIXEL_FORMAT_BGRA32:
1649 *_b = (BYTE)(color >> 24);
1652 *_g = (BYTE)(color >> 16);
1655 *_r = (BYTE)(color >> 8);
1662 case PIXEL_FORMAT_BGRX32:
1664 *_b = (BYTE)(color >> 24);
1667 *_g = (BYTE)(color >> 16);
1670 *_r = (BYTE)(color >> 8);
1678 case PIXEL_FORMAT_RGB24:
1680 *_r = (BYTE)(color >> 16);
1683 *_g = (BYTE)(color >> 8);
1693 case PIXEL_FORMAT_BGR24:
1695 *_b = (BYTE)(color >> 16);
1698 *_g = (BYTE)(color >> 8);
1709 case PIXEL_FORMAT_RGB16:
1712 const UINT32 c = (color >> 11) & 0x1F;
1713 const UINT32 val = (c << 3) + c / 4;
1714 *_r = (BYTE)(val > 255 ? 255 : val);
1719 const UINT32 c = (color >> 5) & 0x3F;
1720 const UINT32 val = (c << 2) + c / 4 / 2;
1721 *_g = (BYTE)(val > 255 ? 255 : val);
1726 const UINT32 c = (color)&0x1F;
1727 const UINT32 val = (c << 3) + c / 4;
1728 *_b = (BYTE)(val > 255 ? 255 : val);
1736 case PIXEL_FORMAT_BGR16:
1739 const UINT32 c = (color)&0x1F;
1740 const UINT32 val = (c << 3) + c / 4;
1741 *_r = (BYTE)(val > 255 ? 255 : val);
1746 const UINT32 c = (color >> 5) & 0x3F;
1747 const UINT32 val = (c << 2) + c / 4 / 2;
1748 *_g = (BYTE)(val > 255 ? 255 : val);
1753 const UINT32 c = (color >> 11) & 0x1F;
1754 const UINT32 val = (c << 3) + c / 4;
1755 *_b = (BYTE)(val > 255 ? 255 : val);
1763 case PIXEL_FORMAT_ARGB15:
1766 const UINT32 c = (color >> 10) & 0x1F;
1767 const UINT32 val = (c << 3) + c / 4;
1768 *_r = (BYTE)(val > 255 ? 255 : val);
1773 const UINT32 c = (color >> 5) & 0x1F;
1774 const UINT32 val = (c << 3) + c / 4;
1775 *_g = (BYTE)(val > 255 ? 255 : val);
1780 const UINT32 c = (color)&0x1F;
1781 const UINT32 val = (c << 3) + c / 4;
1782 *_b = (BYTE)(val > 255 ? 255 : val);
1786 *_a = color & 0x8000 ? 0xFF : 0x00;
1790 case PIXEL_FORMAT_ABGR15:
1793 const UINT32 c = (color)&0x1F;
1794 const UINT32 val = (c << 3) + c / 4;
1795 *_r = (BYTE)(val > 255 ? 255 : val);
1800 const UINT32 c = (color >> 5) & 0x1F;
1801 const UINT32 val = (c << 3) + c / 4;
1802 *_g = (BYTE)(val > 255 ? 255 : val);
1807 const UINT32 c = (color >> 10) & 0x1F;
1808 const UINT32 val = (c << 3) + c / 4;
1809 *_b = (BYTE)(val > 255 ? 255 : val);
1813 *_a = color & 0x8000 ? 0xFF : 0x00;
1818 case PIXEL_FORMAT_RGB15:
1821 const UINT32 c = (color >> 10) & 0x1F;
1822 const UINT32 val = (c << 3) + c / 4;
1823 *_r = (BYTE)(val > 255 ? 255 : val);
1828 const UINT32 c = (color >> 5) & 0x1F;
1829 const UINT32 val = (c << 3) + c / 4;
1830 *_g = (BYTE)(val > 255 ? 255 : val);
1835 const UINT32 c = (color)&0x1F;
1836 const UINT32 val = (c << 3) + c / 4;
1837 *_b = (BYTE)(val > 255 ? 255 : val);
1845 case PIXEL_FORMAT_BGR15:
1848 const UINT32 c = (color)&0x1F;
1849 const UINT32 val = (c << 3) + c / 4;
1850 *_r = (BYTE)(val > 255 ? 255 : val);
1855 const UINT32 c = (color >> 5) & 0x1F;
1856 const UINT32 val = (c << 3) + c / 4;
1857 *_g = (BYTE)(val > 255 ? 255 : val);
1862 const UINT32 c = (color >> 10) & 0x1F;
1863 const UINT32 val = (c << 3) + c / 4;
1864 *_b = (BYTE)(val > 255 ? 255 : val);
1873 case PIXEL_FORMAT_RGB8:
1876 tmp = palette->palette[color];
1877 FreeRDPSplitColor(tmp, palette->format, _r, _g, _b, _a,
nullptr);
1897 case PIXEL_FORMAT_MONO:
1899 *_r = (color) ? 0xFF : 0x00;
1902 *_g = (color) ? 0xFF : 0x00;
1905 *_b = (color) ? 0xFF : 0x00;
1908 *_a = (color) ? 0xFF : 0x00;
1913 case PIXEL_FORMAT_A4:
1927 WLog_ERR(TAG,
"Unsupported format %s", FreeRDPGetColorFormatName(format));
1932BOOL FreeRDPWriteColorIgnoreAlpha(BYTE* WINPR_RESTRICT dst, UINT32 format, UINT32 color)
1934 return FreeRDPWriteColorIgnoreAlpha_int(dst, format, color);
1937BOOL FreeRDPWriteColor(BYTE* WINPR_RESTRICT dst, UINT32 format, UINT32 color)
1939 return FreeRDPWriteColor_int(dst, format, color);
1942UINT32 FreeRDPReadColor(
const BYTE* WINPR_RESTRICT src, UINT32 format)
1944 return FreeRDPReadColor_int(src, format);
1947UINT32 FreeRDPGetColor(UINT32 format, BYTE r, BYTE g, BYTE b, BYTE a)
1958 case PIXEL_FORMAT_ARGB32:
1959 return (_a << 24) | (_r << 16) | (_g << 8) | _b;
1961 case PIXEL_FORMAT_XRGB32:
1962 return (_r << 16) | (_g << 8) | _b;
1964 case PIXEL_FORMAT_ABGR32:
1965 return (_a << 24) | (_b << 16) | (_g << 8) | _r;
1967 case PIXEL_FORMAT_XBGR32:
1968 return (_b << 16) | (_g << 8) | _r;
1970 case PIXEL_FORMAT_RGBA32:
1971 return (_r << 24) | (_g << 16) | (_b << 8) | _a;
1973 case PIXEL_FORMAT_RGBX32:
1974 return (_r << 24) | (_g << 16) | (_b << 8) | _a;
1976 case PIXEL_FORMAT_BGRA32:
1977 return (_b << 24) | (_g << 16) | (_r << 8) | _a;
1979 case PIXEL_FORMAT_BGRX32:
1980 return (_b << 24) | (_g << 16) | (_r << 8) | _a;
1982 case PIXEL_FORMAT_RGBX32_DEPTH30:
1984 t = (_r << 22) | (_g << 12) | (_b << 2);
1986 return ((t & 0xff) << 24) | (((t >> 8) & 0xff) << 16) | (((t >> 16) & 0xff) << 8) |
1989 case PIXEL_FORMAT_BGRX32_DEPTH30:
1991 t = (_r << 22) | (_g << 12) | (_b << 2);
1993 return ((t & 0xff) << 24) | (((t >> 8) & 0xff) << 16) | (((t >> 16) & 0xff) << 8) |
1997 case PIXEL_FORMAT_RGB24:
1998 return (_r << 16) | (_g << 8) | _b;
2000 case PIXEL_FORMAT_BGR24:
2001 return (_b << 16) | (_g << 8) | _r;
2004 case PIXEL_FORMAT_RGB16:
2005 return (((_r >> 3) & 0x1F) << 11) | (((_g >> 2) & 0x3F) << 5) | ((_b >> 3) & 0x1F);
2007 case PIXEL_FORMAT_BGR16:
2008 return (((_b >> 3) & 0x1F) << 11) | (((_g >> 2) & 0x3F) << 5) | ((_r >> 3) & 0x1F);
2010 case PIXEL_FORMAT_ARGB15:
2011 return (((_r >> 3) & 0x1F) << 10) | (((_g >> 3) & 0x1F) << 5) | ((_b >> 3) & 0x1F) |
2012 (_a ? 0x8000 : 0x0000);
2014 case PIXEL_FORMAT_ABGR15:
2015 return (((_b >> 3) & 0x1F) << 10) | (((_g >> 3) & 0x1F) << 5) | ((_r >> 3) & 0x1F) |
2016 (_a ? 0x8000 : 0x0000);
2019 case PIXEL_FORMAT_RGB15:
2020 return (((_r >> 3) & 0x1F) << 10) | (((_g >> 3) & 0x1F) << 5) | ((_b >> 3) & 0x1F);
2022 case PIXEL_FORMAT_BGR15:
2023 return (((_b >> 3) & 0x1F) << 10) | (((_g >> 3) & 0x1F) << 5) | ((_r >> 3) & 0x1F);
2026 case PIXEL_FORMAT_RGB8:
2029 case PIXEL_FORMAT_A4:
2032 case PIXEL_FORMAT_MONO:
2034 WLog_ERR(TAG,
"Unsupported format %s", FreeRDPGetColorFormatName(format));
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)
2047 prims = primitives_get();
2049 if (FreeRDPGetBitsPerPixel(SrcFormat) == 0)
2051 if (FreeRDPGetBitsPerPixel(DstFormat) == 0)
2053 if ((SrcFormat == PIXEL_FORMAT_RGB8) && (SrcFormat != DstFormat) &&
2054 !freerdp_image_palette_is_valid(palette))
2057 if ((nWidth == 0) || (nHeight == 0))
2060 WINPR_ASSERT(1ull * nXDst + nWidth <= UINT32_MAX);
2061 WINPR_ASSERT(1ull * nXSrc + nWidth <= UINT32_MAX);
2063 nDstStep = (nXDst + nWidth) * FreeRDPGetBytesPerPixel(DstFormat);
2065 nSrcStep = (nXSrc + nWidth) * FreeRDPGetBytesPerPixel(SrcFormat);
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);
2072 return prims->
copy_no_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight,
2073 pSrcData, SrcFormat, nSrcStep, nXSrc, nYSrc, palette,
2074 flags) == PRIMITIVES_SUCCESS;
WINPR_ATTR_NODISCARD fn_copy_no_overlap_t copy_no_overlap