22#include <freerdp/config.h>
25#include <winpr/print.h>
26#include <winpr/bitstream.h>
28#include <freerdp/codec/color.h>
29#include <freerdp/codec/clear.h>
30#include <freerdp/log.h>
32#define TAG FREERDP_TAG("codec.clear")
34#define CLEARCODEC_FLAG_GLYPH_INDEX 0x01
35#define CLEARCODEC_FLAG_GLYPH_HIT 0x02
36#define CLEARCODEC_FLAG_CACHE_RESET 0x04
38#define CLEARCODEC_VBAR_SIZE 32768
39#define CLEARCODEC_VBAR_SHORT_SIZE 16384
65 CLEAR_GLYPH_ENTRY GlyphCache[4000];
66 UINT32 VBarStorageCursor;
67 CLEAR_VBAR_ENTRY VBarStorage[CLEARCODEC_VBAR_SIZE];
68 UINT32 ShortVBarStorageCursor;
69 CLEAR_VBAR_ENTRY ShortVBarStorage[CLEARCODEC_VBAR_SHORT_SIZE];
72static const UINT32 CLEAR_LOG2_FLOOR[256] = {
73 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
74 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
75 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
76 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
77 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
78 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
79 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
80 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
83static const BYTE CLEAR_8BIT_MASKS[9] = { 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF };
85static void clear_reset_vbar_storage(CLEAR_CONTEXT* WINPR_RESTRICT clear, BOOL zero)
89 for (
size_t i = 0; i < ARRAYSIZE(clear->VBarStorage); i++)
90 winpr_aligned_free(clear->VBarStorage[i].pixels);
92 ZeroMemory(clear->VBarStorage,
sizeof(clear->VBarStorage));
95 clear->VBarStorageCursor = 0;
99 for (
size_t i = 0; i < ARRAYSIZE(clear->ShortVBarStorage); i++)
100 winpr_aligned_free(clear->ShortVBarStorage[i].pixels);
102 ZeroMemory(clear->ShortVBarStorage,
sizeof(clear->ShortVBarStorage));
105 clear->ShortVBarStorageCursor = 0;
108static void clear_reset_glyph_cache(CLEAR_CONTEXT* WINPR_RESTRICT clear)
110 for (
size_t i = 0; i < ARRAYSIZE(clear->GlyphCache); i++)
111 winpr_aligned_free(clear->GlyphCache[i].pixels);
113 ZeroMemory(clear->GlyphCache,
sizeof(clear->GlyphCache));
116static BOOL convert_color(BYTE* WINPR_RESTRICT dst, UINT32 nDstStep, UINT32 DstFormat, UINT32 nXDst,
117 UINT32 nYDst, UINT32 nWidth, UINT32 nHeight,
118 const BYTE* WINPR_RESTRICT src, UINT32 nSrcStep, UINT32 SrcFormat,
119 UINT32 nDstWidth, UINT32 nDstHeight,
120 const gdiPalette* WINPR_RESTRICT palette)
122 if (nWidth + nXDst > nDstWidth)
123 nWidth = nDstWidth - nXDst;
125 if (nHeight + nYDst > nDstHeight)
126 nHeight = nDstHeight - nYDst;
128 return freerdp_image_copy_no_overlap(dst, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight,
129 src, SrcFormat, nSrcStep, 0, 0, palette,
130 FREERDP_KEEP_DST_ALPHA);
133static BOOL clear_decompress_nscodec(NSC_CONTEXT* WINPR_RESTRICT nsc, UINT32 width, UINT32 height,
134 wStream* WINPR_RESTRICT s, UINT32 bitmapDataByteCount,
135 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
136 UINT32 nDstStep, UINT32 nXDstRel, UINT32 nYDstRel)
140 if (!Stream_CheckAndLogRequiredLength(TAG, s, bitmapDataByteCount))
143 rc = nsc_process_message(nsc, 32, width, height, Stream_Pointer(s), bitmapDataByteCount,
144 pDstData, DstFormat, nDstStep, nXDstRel, nYDstRel, width, height,
146 Stream_Seek(s, bitmapDataByteCount);
150static BOOL clear_decompress_subcode_rlex(
wStream* WINPR_RESTRICT s, UINT32 bitmapDataByteCount,
151 UINT32 width, UINT32 height,
152 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
153 UINT32 nDstStep, UINT32 nXDstRel, UINT32 nYDstRel,
154 UINT32 nDstWidth, UINT32 nDstHeight)
158 UINT32 pixelCount = 0;
159 UINT32 bitmapDataOffset = 0;
160 size_t pixelIndex = 0;
166 BYTE paletteCount = 0;
167 UINT32 palette[128] = { 0 };
169 if (!Stream_CheckAndLogRequiredLength(TAG, s, bitmapDataByteCount))
172 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
174 Stream_Read_UINT8(s, paletteCount);
175 bitmapDataOffset = 1 + (paletteCount * 3);
177 if ((paletteCount > 127) || (paletteCount < 1))
179 WLog_ERR(TAG,
"paletteCount %" PRIu8
"", paletteCount);
183 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, paletteCount, 3ull))
186 for (UINT32 i = 0; i < paletteCount; i++)
191 Stream_Read_UINT8(s, b);
192 Stream_Read_UINT8(s, g);
193 Stream_Read_UINT8(s, r);
194 palette[i] = FreeRDPGetColor(DstFormat, r, g, b, 0xFF);
198 pixelCount = width * height;
199 numBits = CLEAR_LOG2_FLOOR[paletteCount - 1] + 1;
201 while (bitmapDataOffset < bitmapDataByteCount)
205 UINT32 runLengthFactor = 0;
207 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
210 Stream_Read_UINT8(s, tmp);
211 Stream_Read_UINT8(s, runLengthFactor);
212 bitmapDataOffset += 2;
213 suiteDepth = (tmp >> numBits) & CLEAR_8BIT_MASKS[(8 - numBits)];
214 stopIndex = tmp & CLEAR_8BIT_MASKS[numBits];
215 startIndex = stopIndex - suiteDepth;
217 if (runLengthFactor >= 0xFF)
219 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
222 Stream_Read_UINT16(s, runLengthFactor);
223 bitmapDataOffset += 2;
225 if (runLengthFactor >= 0xFFFF)
227 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
230 Stream_Read_UINT32(s, runLengthFactor);
231 bitmapDataOffset += 4;
235 if (startIndex >= paletteCount)
237 WLog_ERR(TAG,
"startIndex %" PRIu8
" > paletteCount %" PRIu8
"]", startIndex,
242 if (stopIndex >= paletteCount)
244 WLog_ERR(TAG,
"stopIndex %" PRIu8
" > paletteCount %" PRIu8
"]", stopIndex,
249 suiteIndex = startIndex;
251 if (suiteIndex > 127)
253 WLog_ERR(TAG,
"suiteIndex %" PRIu8
" > 127]", suiteIndex);
257 color = palette[suiteIndex];
259 if ((pixelIndex + runLengthFactor) > pixelCount)
262 "pixelIndex %" PRIuz
" + runLengthFactor %" PRIu32
" > pixelCount %" PRIu32
"",
263 pixelIndex, runLengthFactor, pixelCount);
267 for (UINT32 i = 0; i < runLengthFactor; i++)
269 BYTE* pTmpData = &pDstData[(nXDstRel + x) * FreeRDPGetBytesPerPixel(DstFormat) +
270 (nYDstRel + y) * nDstStep];
272 if ((nXDstRel + x < nDstWidth) && (nYDstRel + y < nDstHeight))
273 FreeRDPWriteColor(pTmpData, DstFormat, color);
282 pixelIndex += runLengthFactor;
284 if ((pixelIndex + (suiteDepth + 1)) > pixelCount)
287 "pixelIndex %" PRIuz
" + suiteDepth %" PRIu8
" + 1 > pixelCount %" PRIu32
"",
288 pixelIndex, suiteDepth, pixelCount);
292 for (UINT32 i = 0; i <= suiteDepth; i++)
294 BYTE* pTmpData = &pDstData[(nXDstRel + x) * FreeRDPGetBytesPerPixel(DstFormat) +
295 (nYDstRel + y) * nDstStep];
296 UINT32 ccolor = palette[suiteIndex];
298 if (suiteIndex > 127)
300 WLog_ERR(TAG,
"suiteIndex %" PRIu8
" > 127", suiteIndex);
306 if ((nXDstRel + x < nDstWidth) && (nYDstRel + y < nDstHeight))
307 FreeRDPWriteColor(pTmpData, DstFormat, ccolor);
316 pixelIndex += (suiteDepth + 1);
319 if (pixelIndex != pixelCount)
321 WLog_ERR(TAG,
"pixelIndex %" PRIuz
" != pixelCount %" PRIu32
"", pixelIndex, pixelCount);
328static BOOL clear_resize_buffer(CLEAR_CONTEXT* WINPR_RESTRICT clear, UINT32 width, UINT32 height)
333 const UINT64 size = 1ull * (width + 16ull) * (height + 16ull);
334 const size_t bpp = FreeRDPGetBytesPerPixel(clear->format);
335 if (size > UINT32_MAX / bpp)
338 if (size > clear->TempSize / bpp)
340 BYTE* tmp = (BYTE*)winpr_aligned_recalloc(clear->TempBuffer,
341 WINPR_ASSERTING_INT_CAST(
size_t, size), bpp, 32);
345 WLog_ERR(TAG,
"clear->TempBuffer winpr_aligned_recalloc failed for %" PRIu64
" bytes",
350 clear->TempSize = WINPR_ASSERTING_INT_CAST(
size_t, size* bpp);
351 clear->TempBuffer = tmp;
357static BOOL clear_decompress_residual_data(CLEAR_CONTEXT* WINPR_RESTRICT clear,
358 wStream* WINPR_RESTRICT s, UINT32 residualByteCount,
359 UINT32 nWidth, UINT32 nHeight,
360 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
361 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
362 UINT32 nDstWidth, UINT32 nDstHeight,
363 const gdiPalette* WINPR_RESTRICT palette)
366 UINT32 suboffset = 0;
367 BYTE* dstBuffer = NULL;
368 UINT32 pixelIndex = 0;
369 UINT32 pixelCount = 0;
371 if (!Stream_CheckAndLogRequiredLength(TAG, s, residualByteCount))
376 pixelCount = nWidth * nHeight;
378 if (!clear_resize_buffer(clear, nWidth, nHeight))
381 dstBuffer = clear->TempBuffer;
383 while (suboffset < residualByteCount)
388 UINT32 runLengthFactor = 0;
391 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
394 Stream_Read_UINT8(s, b);
395 Stream_Read_UINT8(s, g);
396 Stream_Read_UINT8(s, r);
397 Stream_Read_UINT8(s, runLengthFactor);
399 color = FreeRDPGetColor(clear->format, r, g, b, 0xFF);
401 if (runLengthFactor >= 0xFF)
403 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
406 Stream_Read_UINT16(s, runLengthFactor);
409 if (runLengthFactor >= 0xFFFF)
411 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
414 Stream_Read_UINT32(s, runLengthFactor);
419 if ((pixelIndex >= pixelCount) || (runLengthFactor > (pixelCount - pixelIndex)))
422 "pixelIndex %" PRIu32
" + runLengthFactor %" PRIu32
" > pixelCount %" PRIu32
424 pixelIndex, runLengthFactor, pixelCount);
428 for (UINT32 i = 0; i < runLengthFactor; i++)
430 FreeRDPWriteColor(dstBuffer, clear->format, color);
431 dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
434 pixelIndex += runLengthFactor;
437 nSrcStep = nWidth * FreeRDPGetBytesPerPixel(clear->format);
439 if (pixelIndex != pixelCount)
441 WLog_ERR(TAG,
"pixelIndex %" PRIu32
" != pixelCount %" PRIu32
"", pixelIndex, pixelCount);
445 return convert_color(pDstData, nDstStep, DstFormat, nXDst, nYDst, nWidth, nHeight,
446 clear->TempBuffer, nSrcStep, clear->format, nDstWidth, nDstHeight,
450static BOOL clear_decompress_subcodecs_data(CLEAR_CONTEXT* WINPR_RESTRICT clear,
451 wStream* WINPR_RESTRICT s, UINT32 subcodecByteCount,
452 UINT32 nWidth, UINT32 nHeight,
453 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
454 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
455 UINT32 nDstWidth, UINT32 nDstHeight,
456 const gdiPalette* WINPR_RESTRICT palette)
462 UINT32 bitmapDataByteCount = 0;
464 UINT32 suboffset = 0;
466 if (!Stream_CheckAndLogRequiredLength(TAG, s, subcodecByteCount))
471 while (suboffset < subcodecByteCount)
476 if (!Stream_CheckAndLogRequiredLength(TAG, s, 13))
479 Stream_Read_UINT16(s, xStart);
480 Stream_Read_UINT16(s, yStart);
481 Stream_Read_UINT16(s, width);
482 Stream_Read_UINT16(s, height);
483 Stream_Read_UINT32(s, bitmapDataByteCount);
484 Stream_Read_UINT8(s, subcodecId);
487 if (!Stream_CheckAndLogRequiredLength(TAG, s, bitmapDataByteCount))
490 nXDstRel = nXDst + xStart;
491 nYDstRel = nYDst + yStart;
493 if (1ull * xStart + width > nWidth)
495 WLog_ERR(TAG,
"xStart %" PRIu16
" + width %" PRIu16
" > nWidth %" PRIu32
"", xStart,
499 if (1ull * yStart + height > nHeight)
501 WLog_ERR(TAG,
"yStart %" PRIu16
" + height %" PRIu16
" > nHeight %" PRIu32
"", yStart,
506 if (!clear_resize_buffer(clear, width, height))
513 const UINT32 nSrcStep = width * FreeRDPGetBytesPerPixel(PIXEL_FORMAT_BGR24);
514 const size_t nSrcSize = 1ull * nSrcStep * height;
516 if (bitmapDataByteCount != nSrcSize)
518 WLog_ERR(TAG,
"bitmapDataByteCount %" PRIu32
" != nSrcSize %" PRIuz
"",
519 bitmapDataByteCount, nSrcSize);
523 if (!convert_color(pDstData, nDstStep, DstFormat, nXDstRel, nYDstRel, width, height,
524 Stream_Pointer(s), nSrcStep, PIXEL_FORMAT_BGR24, nDstWidth,
525 nDstHeight, palette))
528 Stream_Seek(s, bitmapDataByteCount);
533 if (!clear_decompress_nscodec(clear->nsc, width, height, s, bitmapDataByteCount,
534 pDstData, DstFormat, nDstStep, nXDstRel, nYDstRel))
540 if (!clear_decompress_subcode_rlex(s, bitmapDataByteCount, width, height, pDstData,
541 DstFormat, nDstStep, nXDstRel, nYDstRel,
542 nDstWidth, nDstHeight))
548 WLog_ERR(TAG,
"Unknown subcodec ID %" PRIu8
"", subcodecId);
552 suboffset += bitmapDataByteCount;
558static BOOL resize_vbar_entry(CLEAR_CONTEXT* WINPR_RESTRICT clear,
559 CLEAR_VBAR_ENTRY* WINPR_RESTRICT vBarEntry)
561 if (vBarEntry->count > vBarEntry->size)
563 const UINT32 bpp = FreeRDPGetBytesPerPixel(clear->format);
564 const UINT32 oldPos = vBarEntry->size * bpp;
565 const UINT32 diffSize = (vBarEntry->count - vBarEntry->size) * bpp;
567 vBarEntry->size = vBarEntry->count;
569 (BYTE*)winpr_aligned_recalloc(vBarEntry->pixels, vBarEntry->count, 1ull * bpp, 32);
573 WLog_ERR(TAG,
"vBarEntry->pixels winpr_aligned_recalloc %" PRIu32
" failed",
574 vBarEntry->count * bpp);
578 memset(&tmp[oldPos], 0, diffSize);
579 vBarEntry->pixels = tmp;
582 if (!vBarEntry->pixels && vBarEntry->size)
584 WLog_ERR(TAG,
"vBarEntry->pixels is NULL but vBarEntry->size is %" PRIu32
"",
592static BOOL clear_decompress_bands_data(CLEAR_CONTEXT* WINPR_RESTRICT clear,
593 wStream* WINPR_RESTRICT s, UINT32 bandsByteCount,
594 UINT32 nWidth, UINT32 nHeight,
595 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
596 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
597 UINT32 nDstWidth, UINT32 nDstHeight)
599 UINT32 suboffset = 0;
601 if (!Stream_CheckAndLogRequiredLength(TAG, s, bandsByteCount))
604 while (suboffset < bandsByteCount)
614 UINT16 vBarHeader = 0;
617 UINT32 vBarCount = 0;
618 UINT32 vBarPixelCount = 0;
619 UINT32 vBarShortPixelCount = 0;
621 if (!Stream_CheckAndLogRequiredLength(TAG, s, 11))
624 Stream_Read_UINT16(s, xStart);
625 Stream_Read_UINT16(s, xEnd);
626 Stream_Read_UINT16(s, yStart);
627 Stream_Read_UINT16(s, yEnd);
628 Stream_Read_UINT8(s, cb);
629 Stream_Read_UINT8(s, cg);
630 Stream_Read_UINT8(s, cr);
632 colorBkg = FreeRDPGetColor(clear->format, cr, cg, cb, 0xFF);
636 WLog_ERR(TAG,
"xEnd %" PRIu16
" < xStart %" PRIu16
"", xEnd, xStart);
642 WLog_ERR(TAG,
"yEnd %" PRIu16
" < yStart %" PRIu16
"", yEnd, yStart);
646 vBarCount = (xEnd - xStart) + 1;
648 for (UINT32 i = 0; i < vBarCount; i++)
650 UINT32 vBarHeight = 0;
651 CLEAR_VBAR_ENTRY* vBarEntry = NULL;
652 CLEAR_VBAR_ENTRY* vBarShortEntry = NULL;
653 BOOL vBarUpdate = FALSE;
654 const BYTE* cpSrcPixel = NULL;
656 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
659 Stream_Read_UINT16(s, vBarHeader);
661 vBarHeight = (yEnd - yStart + 1);
665 WLog_ERR(TAG,
"vBarHeight (%" PRIu32
") > 52", vBarHeight);
669 if ((vBarHeader & 0xC000) == 0x4000)
671 const UINT16 vBarIndex = (vBarHeader & 0x3FFF);
672 vBarShortEntry = &(clear->ShortVBarStorage[vBarIndex]);
676 WLog_ERR(TAG,
"missing vBarShortEntry %" PRIu16
"", vBarIndex);
680 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
683 Stream_Read_UINT8(s, vBarYOn);
685 vBarShortPixelCount = vBarShortEntry->count;
688 else if ((vBarHeader & 0xC000) == 0x0000)
690 vBarYOn = (vBarHeader & 0xFF);
691 vBarYOff = ((vBarHeader >> 8) & 0x3F);
693 if (vBarYOff < vBarYOn)
695 WLog_ERR(TAG,
"vBarYOff %" PRIu16
" < vBarYOn %" PRIu16
"", vBarYOff, vBarYOn);
699 vBarShortPixelCount = (vBarYOff - vBarYOn);
701 if (vBarShortPixelCount > 52)
703 WLog_ERR(TAG,
"vBarShortPixelCount %" PRIu32
" > 52", vBarShortPixelCount);
707 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, vBarShortPixelCount, 3ull))
710 if (clear->ShortVBarStorageCursor >= CLEARCODEC_VBAR_SHORT_SIZE)
713 "clear->ShortVBarStorageCursor %" PRIu32
714 " >= CLEARCODEC_VBAR_SHORT_SIZE (%" PRId32
")",
715 clear->ShortVBarStorageCursor, CLEARCODEC_VBAR_SHORT_SIZE);
719 vBarShortEntry = &(clear->ShortVBarStorage[clear->ShortVBarStorageCursor]);
720 vBarShortEntry->count = vBarShortPixelCount;
722 if (!resize_vbar_entry(clear, vBarShortEntry))
725 for (
size_t y = 0; y < vBarShortPixelCount; y++)
731 &vBarShortEntry->pixels[y * FreeRDPGetBytesPerPixel(clear->format)];
733 Stream_Read_UINT8(s, b);
734 Stream_Read_UINT8(s, g);
735 Stream_Read_UINT8(s, r);
736 color = FreeRDPGetColor(clear->format, r, g, b, 0xFF);
738 if (!FreeRDPWriteColor(dstBuffer, clear->format, color))
742 suboffset += (vBarShortPixelCount * 3);
743 clear->ShortVBarStorageCursor =
744 (clear->ShortVBarStorageCursor + 1) % CLEARCODEC_VBAR_SHORT_SIZE;
747 else if ((vBarHeader & 0x8000) == 0x8000)
749 const UINT16 vBarIndex = (vBarHeader & 0x7FFF);
750 vBarEntry = &(clear->VBarStorage[vBarIndex]);
753 if (vBarEntry->size == 0)
755 WLog_WARN(TAG,
"Empty cache index %" PRIu16
", filling dummy data", vBarIndex);
756 vBarEntry->count = vBarHeight;
758 if (!resize_vbar_entry(clear, vBarEntry))
764 WLog_ERR(TAG,
"invalid vBarHeader 0x%04" PRIX16
"", vBarHeader);
770 BYTE* pSrcPixel = NULL;
771 BYTE* dstBuffer = NULL;
773 if (clear->VBarStorageCursor >= CLEARCODEC_VBAR_SIZE)
776 "clear->VBarStorageCursor %" PRIu32
" >= CLEARCODEC_VBAR_SIZE %" PRId32
778 clear->VBarStorageCursor, CLEARCODEC_VBAR_SIZE);
782 vBarEntry = &(clear->VBarStorage[clear->VBarStorageCursor]);
783 vBarPixelCount = vBarHeight;
784 vBarEntry->count = vBarPixelCount;
786 if (!resize_vbar_entry(clear, vBarEntry))
789 dstBuffer = vBarEntry->pixels;
792 UINT32 count = vBarYOn;
794 if ((y + count) > vBarPixelCount)
795 count = (vBarPixelCount > y) ? (vBarPixelCount - y) : 0;
801 FreeRDPWriteColor(dstBuffer, clear->format, colorBkg);
802 dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
811 count = vBarShortPixelCount;
813 if ((y + count) > vBarPixelCount)
814 count = (vBarPixelCount > y) ? (vBarPixelCount - y) : 0;
818 const size_t offset =
819 (1ull * y - vBarYOn) * FreeRDPGetBytesPerPixel(clear->format);
820 pSrcPixel = &vBarShortEntry->pixels[offset];
821 if (offset + count > vBarShortEntry->count)
823 WLog_ERR(TAG,
"offset + count > vBarShortEntry->count");
827 for (
size_t x = 0; x < count; x++)
830 color = FreeRDPReadColor(&pSrcPixel[x * FreeRDPGetBytesPerPixel(clear->format)],
833 if (!FreeRDPWriteColor(dstBuffer, clear->format, color))
836 dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
840 y = vBarYOn + vBarShortPixelCount;
841 count = (vBarPixelCount > y) ? (vBarPixelCount - y) : 0;
847 if (!FreeRDPWriteColor(dstBuffer, clear->format, colorBkg))
850 dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
854 vBarEntry->count = vBarPixelCount;
855 clear->VBarStorageCursor = (clear->VBarStorageCursor + 1) % CLEARCODEC_VBAR_SIZE;
858 if (vBarEntry->count != vBarHeight)
860 WLog_ERR(TAG,
"vBarEntry->count %" PRIu32
" != vBarHeight %" PRIu32
"",
861 vBarEntry->count, vBarHeight);
862 vBarEntry->count = vBarHeight;
864 if (!resize_vbar_entry(clear, vBarEntry))
868 const UINT32 nXDstRel = nXDst + xStart;
869 const UINT32 nYDstRel = nYDst + yStart;
870 cpSrcPixel = vBarEntry->pixels;
874 UINT32 count = vBarEntry->count;
879 if (nXDstRel + i >= nDstWidth)
882 for (UINT32 y = 0; y < count; y++)
884 if (nYDstRel + y >= nDstHeight)
888 &pDstData[((nYDstRel + y) * nDstStep) +
889 ((nXDstRel + i) * FreeRDPGetBytesPerPixel(DstFormat))];
890 UINT32 color = FreeRDPReadColor(cpSrcPixel, clear->format);
891 color = FreeRDPConvertColor(color, clear->format, DstFormat, NULL);
893 if (!FreeRDPWriteColor(pDstPixel8, DstFormat, color))
896 cpSrcPixel += FreeRDPGetBytesPerPixel(clear->format);
905static BOOL clear_decompress_glyph_data(CLEAR_CONTEXT* WINPR_RESTRICT clear,
906 wStream* WINPR_RESTRICT s, UINT32 glyphFlags, UINT32 nWidth,
907 UINT32 nHeight, BYTE* WINPR_RESTRICT pDstData,
908 UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst,
909 UINT32 nYDst, UINT32 nDstWidth, UINT32 nDstHeight,
910 const gdiPalette* WINPR_RESTRICT palette,
911 BYTE** WINPR_RESTRICT ppGlyphData)
913 UINT16 glyphIndex = 0;
918 if ((glyphFlags & CLEARCODEC_FLAG_GLYPH_HIT) && !(glyphFlags & CLEARCODEC_FLAG_GLYPH_INDEX))
920 WLog_ERR(TAG,
"Invalid glyph flags %08" PRIX32
"", glyphFlags);
924 if ((glyphFlags & CLEARCODEC_FLAG_GLYPH_INDEX) == 0)
927 if ((nWidth * nHeight) > (1024 * 1024))
929 WLog_ERR(TAG,
"glyph too large: %" PRIu32
"x%" PRIu32
"", nWidth, nHeight);
933 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
936 Stream_Read_UINT16(s, glyphIndex);
938 if (glyphIndex >= 4000)
940 WLog_ERR(TAG,
"Invalid glyphIndex %" PRIu16
"", glyphIndex);
944 if (glyphFlags & CLEARCODEC_FLAG_GLYPH_HIT)
947 CLEAR_GLYPH_ENTRY* glyphEntry = &(clear->GlyphCache[glyphIndex]);
948 BYTE* glyphData = NULL;
952 WLog_ERR(TAG,
"clear->GlyphCache[%" PRIu16
"]=NULL", glyphIndex);
956 glyphData = (BYTE*)glyphEntry->pixels;
960 WLog_ERR(TAG,
"clear->GlyphCache[%" PRIu16
"]->pixels=NULL", glyphIndex);
964 if ((nWidth * nHeight) > glyphEntry->count)
967 "(nWidth %" PRIu32
" * nHeight %" PRIu32
") > glyphEntry->count %" PRIu32
"",
968 nWidth, nHeight, glyphEntry->count);
972 nSrcStep = nWidth * FreeRDPGetBytesPerPixel(clear->format);
973 return convert_color(pDstData, nDstStep, DstFormat, nXDst, nYDst, nWidth, nHeight,
974 glyphData, nSrcStep, clear->format, nDstWidth, nDstHeight, palette);
977 if (glyphFlags & CLEARCODEC_FLAG_GLYPH_INDEX)
979 const UINT32 bpp = FreeRDPGetBytesPerPixel(clear->format);
980 CLEAR_GLYPH_ENTRY* glyphEntry = &(clear->GlyphCache[glyphIndex]);
981 glyphEntry->count = nWidth * nHeight;
983 if (glyphEntry->count > glyphEntry->size)
986 winpr_aligned_recalloc(glyphEntry->pixels, glyphEntry->count, 1ull * bpp, 32);
990 WLog_ERR(TAG,
"glyphEntry->pixels winpr_aligned_recalloc %" PRIu32
" failed!",
991 glyphEntry->count * bpp);
995 glyphEntry->size = glyphEntry->count;
996 glyphEntry->pixels = (UINT32*)tmp;
999 if (!glyphEntry->pixels)
1001 WLog_ERR(TAG,
"glyphEntry->pixels=NULL");
1006 *ppGlyphData = (BYTE*)glyphEntry->pixels;
1014static inline BOOL updateContextFormat(CLEAR_CONTEXT* WINPR_RESTRICT clear, UINT32 DstFormat)
1016 if (!clear || !clear->nsc)
1019 clear->format = DstFormat;
1020 return nsc_context_set_parameters(clear->nsc, NSC_COLOR_FORMAT, DstFormat);
1023INT32 clear_decompress(CLEAR_CONTEXT* WINPR_RESTRICT clear,
const BYTE* WINPR_RESTRICT pSrcData,
1024 UINT32 SrcSize, UINT32 nWidth, UINT32 nHeight, BYTE* WINPR_RESTRICT pDstData,
1025 UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
1026 UINT32 nDstWidth, UINT32 nDstHeight,
1027 const gdiPalette* WINPR_RESTRICT palette)
1031 BYTE glyphFlags = 0;
1032 UINT32 residualByteCount = 0;
1033 UINT32 bandsByteCount = 0;
1034 UINT32 subcodecByteCount = 0;
1037 BYTE* glyphData = NULL;
1042 if ((nDstWidth == 0) || (nDstHeight == 0))
1045 if ((nWidth > 0xFFFF) || (nHeight > 0xFFFF))
1048 s = Stream_StaticConstInit(&sbuffer, pSrcData, SrcSize);
1053 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
1056 if (!updateContextFormat(clear, DstFormat))
1059 Stream_Read_UINT8(s, glyphFlags);
1060 Stream_Read_UINT8(s, seqNumber);
1062 if (!clear->seqNumber && seqNumber)
1063 clear->seqNumber = seqNumber;
1065 if (seqNumber != clear->seqNumber)
1067 WLog_ERR(TAG,
"Sequence number unexpected %" PRIu8
" - %" PRIu32
"", seqNumber,
1069 WLog_ERR(TAG,
"seqNumber %" PRIu8
" != clear->seqNumber %" PRIu32
"", seqNumber,
1074 clear->seqNumber = (seqNumber + 1) % 256;
1076 if (glyphFlags & CLEARCODEC_FLAG_CACHE_RESET)
1078 clear_reset_vbar_storage(clear, FALSE);
1081 if (!clear_decompress_glyph_data(clear, s, glyphFlags, nWidth, nHeight, pDstData, DstFormat,
1082 nDstStep, nXDst, nYDst, nDstWidth, nDstHeight, palette,
1085 WLog_ERR(TAG,
"clear_decompress_glyph_data failed!");
1090 if (Stream_GetRemainingLength(s) < 12)
1092 const UINT32 mask = (CLEARCODEC_FLAG_GLYPH_HIT | CLEARCODEC_FLAG_GLYPH_INDEX);
1094 if ((glyphFlags & mask) == mask)
1098 "invalid glyphFlags, missing flags: 0x%02" PRIx8
" & 0x%02" PRIx32
1100 glyphFlags, mask, glyphFlags & mask);
1104 Stream_Read_UINT32(s, residualByteCount);
1105 Stream_Read_UINT32(s, bandsByteCount);
1106 Stream_Read_UINT32(s, subcodecByteCount);
1108 if (residualByteCount > 0)
1110 if (!clear_decompress_residual_data(clear, s, residualByteCount, nWidth, nHeight, pDstData,
1111 DstFormat, nDstStep, nXDst, nYDst, nDstWidth,
1112 nDstHeight, palette))
1114 WLog_ERR(TAG,
"clear_decompress_residual_data failed!");
1119 if (bandsByteCount > 0)
1121 if (!clear_decompress_bands_data(clear, s, bandsByteCount, nWidth, nHeight, pDstData,
1122 DstFormat, nDstStep, nXDst, nYDst, nDstWidth, nDstHeight))
1124 WLog_ERR(TAG,
"clear_decompress_bands_data failed!");
1129 if (subcodecByteCount > 0)
1131 if (!clear_decompress_subcodecs_data(clear, s, subcodecByteCount, nWidth, nHeight, pDstData,
1132 DstFormat, nDstStep, nXDst, nYDst, nDstWidth,
1133 nDstHeight, palette))
1135 WLog_ERR(TAG,
"clear_decompress_subcodecs_data failed!");
1142 uint32_t w = MIN(nWidth, nDstWidth);
1143 if (nXDst > nDstWidth)
1145 WLog_WARN(TAG,
"glyphData copy area x exceeds destination: x=%" PRIu32
" > %" PRIu32,
1149 else if (nXDst + w > nDstWidth)
1152 "glyphData copy area x + width exceeds destination: x=%" PRIu32
" + %" PRIu32
1154 nXDst, w, nDstWidth);
1155 w = nDstWidth - nXDst;
1161 "glyphData copy area width truncated: requested=%" PRIu32
1162 ", truncated to %" PRIu32,
1166 uint32_t h = MIN(nHeight, nDstHeight);
1167 if (nYDst > nDstHeight)
1169 WLog_WARN(TAG,
"glyphData copy area y exceeds destination: y=%" PRIu32
" > %" PRIu32,
1173 else if (nYDst + h > nDstHeight)
1176 "glyphData copy area y + height exceeds destination: x=%" PRIu32
" + %" PRIu32
1178 nYDst, h, nDstHeight);
1179 h = nDstHeight - nYDst;
1185 "glyphData copy area height truncated: requested=%" PRIu32
1186 ", truncated to %" PRIu32,
1190 if (!freerdp_image_copy_no_overlap(glyphData, clear->format, 0, 0, 0, w, h, pDstData,
1191 DstFormat, nDstStep, nXDst, nYDst, palette,
1192 FREERDP_KEEP_DST_ALPHA))
1202int clear_compress(WINPR_ATTR_UNUSED CLEAR_CONTEXT* WINPR_RESTRICT clear,
1203 WINPR_ATTR_UNUSED
const BYTE* WINPR_RESTRICT pSrcData,
1204 WINPR_ATTR_UNUSED UINT32 SrcSize,
1205 WINPR_ATTR_UNUSED BYTE** WINPR_RESTRICT ppDstData,
1206 WINPR_ATTR_UNUSED UINT32* WINPR_RESTRICT pDstSize)
1208 WLog_ERR(TAG,
"TODO: not implemented!");
1212BOOL clear_context_reset(CLEAR_CONTEXT* WINPR_RESTRICT clear)
1221 clear->seqNumber = 0;
1225CLEAR_CONTEXT* clear_context_new(BOOL Compressor)
1227 CLEAR_CONTEXT* clear = (CLEAR_CONTEXT*)winpr_aligned_calloc(1,
sizeof(CLEAR_CONTEXT), 32);
1232 clear->Compressor = Compressor;
1233 clear->nsc = nsc_context_new();
1238 if (!updateContextFormat(clear, PIXEL_FORMAT_BGRX32))
1241 if (!clear_resize_buffer(clear, 512, 512))
1244 if (!clear->TempBuffer)
1247 if (!clear_context_reset(clear))
1252 WINPR_PRAGMA_DIAG_PUSH
1253 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1254 clear_context_free(clear);
1255 WINPR_PRAGMA_DIAG_POP
1259void clear_context_free(CLEAR_CONTEXT* WINPR_RESTRICT clear)
1264 nsc_context_free(clear->nsc);
1265 winpr_aligned_free(clear->TempBuffer);
1267 clear_reset_vbar_storage(clear, TRUE);
1268 clear_reset_glyph_cache(clear);
1270 winpr_aligned_free(clear);