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,
137 UINT32 nDstWidth, UINT32 nDstHeight)
141 if (!Stream_CheckAndLogRequiredLength(TAG, s, bitmapDataByteCount))
144 rc = nsc_process_message(nsc, 32, width, height, Stream_Pointer(s), bitmapDataByteCount,
145 pDstData, DstFormat, nDstStep, nXDstRel, nYDstRel, nDstWidth,
146 nDstHeight, FREERDP_FLIP_NONE);
147 Stream_Seek(s, bitmapDataByteCount);
151static BOOL clear_decompress_subcode_rlex(
wStream* WINPR_RESTRICT s, UINT32 bitmapDataByteCount,
152 UINT32 width, UINT32 height,
153 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
154 UINT32 nDstStep, UINT32 nXDstRel, UINT32 nYDstRel,
155 UINT32 nDstWidth, UINT32 nDstHeight)
159 UINT32 pixelCount = 0;
160 UINT32 bitmapDataOffset = 0;
161 size_t pixelIndex = 0;
167 BYTE paletteCount = 0;
168 UINT32 palette[128] = WINPR_C_ARRAY_INIT;
170 if (!Stream_CheckAndLogRequiredLength(TAG, s, bitmapDataByteCount))
173 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
175 Stream_Read_UINT8(s, paletteCount);
176 bitmapDataOffset = 1 + (paletteCount * 3);
178 if ((paletteCount > 127) || (paletteCount < 1))
180 WLog_ERR(TAG,
"paletteCount %" PRIu8
"", paletteCount);
184 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, paletteCount, 3ull))
187 for (UINT32 i = 0; i < paletteCount; i++)
192 Stream_Read_UINT8(s, b);
193 Stream_Read_UINT8(s, g);
194 Stream_Read_UINT8(s, r);
195 palette[i] = FreeRDPGetColor(DstFormat, r, g, b, 0xFF);
199 pixelCount = width * height;
200 numBits = CLEAR_LOG2_FLOOR[paletteCount - 1] + 1;
202 while (bitmapDataOffset < bitmapDataByteCount)
206 UINT32 runLengthFactor = 0;
208 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
211 Stream_Read_UINT8(s, tmp);
212 Stream_Read_UINT8(s, runLengthFactor);
213 bitmapDataOffset += 2;
214 suiteDepth = (tmp >> numBits) & CLEAR_8BIT_MASKS[(8 - numBits)];
215 stopIndex = tmp & CLEAR_8BIT_MASKS[numBits];
216 startIndex = stopIndex - suiteDepth;
218 if (runLengthFactor >= 0xFF)
220 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
223 Stream_Read_UINT16(s, runLengthFactor);
224 bitmapDataOffset += 2;
226 if (runLengthFactor >= 0xFFFF)
228 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
231 Stream_Read_UINT32(s, runLengthFactor);
232 bitmapDataOffset += 4;
236 if (startIndex >= paletteCount)
238 WLog_ERR(TAG,
"startIndex %" PRIu8
" > paletteCount %" PRIu8
"]", startIndex,
243 if (stopIndex >= paletteCount)
245 WLog_ERR(TAG,
"stopIndex %" PRIu8
" > paletteCount %" PRIu8
"]", stopIndex,
250 suiteIndex = startIndex;
252 if (suiteIndex > 127)
254 WLog_ERR(TAG,
"suiteIndex %" PRIu8
" > 127]", suiteIndex);
258 color = palette[suiteIndex];
260 if ((pixelIndex + runLengthFactor) > pixelCount)
263 "pixelIndex %" PRIuz
" + runLengthFactor %" PRIu32
" > pixelCount %" PRIu32
"",
264 pixelIndex, runLengthFactor, pixelCount);
268 for (UINT32 i = 0; i < runLengthFactor; i++)
270 BYTE* pTmpData = &pDstData[(nXDstRel + x) * FreeRDPGetBytesPerPixel(DstFormat) +
271 (nYDstRel + y) * nDstStep];
273 if ((nXDstRel + x < nDstWidth) && (nYDstRel + y < nDstHeight))
274 FreeRDPWriteColor(pTmpData, DstFormat, color);
283 pixelIndex += runLengthFactor;
285 if ((pixelIndex + (suiteDepth + 1)) > pixelCount)
288 "pixelIndex %" PRIuz
" + suiteDepth %" PRIu8
" + 1 > pixelCount %" PRIu32
"",
289 pixelIndex, suiteDepth, pixelCount);
293 for (UINT32 i = 0; i <= suiteDepth; i++)
295 BYTE* pTmpData = &pDstData[(nXDstRel + x) * FreeRDPGetBytesPerPixel(DstFormat) +
296 (nYDstRel + y) * nDstStep];
297 UINT32 ccolor = palette[suiteIndex];
299 if (suiteIndex > 127)
301 WLog_ERR(TAG,
"suiteIndex %" PRIu8
" > 127", suiteIndex);
307 if ((nXDstRel + x < nDstWidth) && (nYDstRel + y < nDstHeight))
308 FreeRDPWriteColor(pTmpData, DstFormat, ccolor);
317 pixelIndex += (suiteDepth + 1);
320 if (pixelIndex != pixelCount)
322 WLog_ERR(TAG,
"pixelIndex %" PRIuz
" != pixelCount %" PRIu32
"", pixelIndex, pixelCount);
329static BOOL clear_resize_buffer(CLEAR_CONTEXT* WINPR_RESTRICT clear, UINT32 width, UINT32 height)
334 const UINT64 size = 1ull * (width + 16ull) * (height + 16ull);
335 const size_t bpp = FreeRDPGetBytesPerPixel(clear->format);
336 if (size > UINT32_MAX / bpp)
339 if (size > clear->TempSize / bpp)
341 BYTE* tmp = (BYTE*)winpr_aligned_recalloc(clear->TempBuffer,
342 WINPR_ASSERTING_INT_CAST(
size_t, size), bpp, 32);
346 WLog_ERR(TAG,
"clear->TempBuffer winpr_aligned_recalloc failed for %" PRIu64
" bytes",
351 clear->TempSize = WINPR_ASSERTING_INT_CAST(
size_t, size* bpp);
352 clear->TempBuffer = tmp;
358static BOOL clear_decompress_residual_data(CLEAR_CONTEXT* WINPR_RESTRICT clear,
359 wStream* WINPR_RESTRICT s, UINT32 residualByteCount,
360 UINT32 nWidth, UINT32 nHeight,
361 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
362 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
363 UINT32 nDstWidth, UINT32 nDstHeight,
364 const gdiPalette* WINPR_RESTRICT palette)
367 UINT32 suboffset = 0;
368 BYTE* dstBuffer =
nullptr;
369 UINT32 pixelIndex = 0;
370 UINT32 pixelCount = 0;
372 if (!Stream_CheckAndLogRequiredLength(TAG, s, residualByteCount))
377 pixelCount = nWidth * nHeight;
379 if (!clear_resize_buffer(clear, nWidth, nHeight))
382 dstBuffer = clear->TempBuffer;
384 while (suboffset < residualByteCount)
389 UINT32 runLengthFactor = 0;
392 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
395 Stream_Read_UINT8(s, b);
396 Stream_Read_UINT8(s, g);
397 Stream_Read_UINT8(s, r);
398 Stream_Read_UINT8(s, runLengthFactor);
400 color = FreeRDPGetColor(clear->format, r, g, b, 0xFF);
402 if (runLengthFactor >= 0xFF)
404 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
407 Stream_Read_UINT16(s, runLengthFactor);
410 if (runLengthFactor >= 0xFFFF)
412 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
415 Stream_Read_UINT32(s, runLengthFactor);
420 if ((pixelIndex >= pixelCount) || (runLengthFactor > (pixelCount - pixelIndex)))
423 "pixelIndex %" PRIu32
" + runLengthFactor %" PRIu32
" > pixelCount %" PRIu32
425 pixelIndex, runLengthFactor, pixelCount);
429 for (UINT32 i = 0; i < runLengthFactor; i++)
431 FreeRDPWriteColor(dstBuffer, clear->format, color);
432 dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
435 pixelIndex += runLengthFactor;
438 nSrcStep = nWidth * FreeRDPGetBytesPerPixel(clear->format);
440 if (pixelIndex != pixelCount)
442 WLog_ERR(TAG,
"pixelIndex %" PRIu32
" != pixelCount %" PRIu32
"", pixelIndex, pixelCount);
446 return convert_color(pDstData, nDstStep, DstFormat, nXDst, nYDst, nWidth, nHeight,
447 clear->TempBuffer, nSrcStep, clear->format, nDstWidth, nDstHeight,
451static BOOL clear_decompress_subcodecs_data(CLEAR_CONTEXT* WINPR_RESTRICT clear,
452 wStream* WINPR_RESTRICT s, UINT32 subcodecByteCount,
453 UINT32 nWidth, UINT32 nHeight,
454 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
455 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
456 UINT32 nDstWidth, UINT32 nDstHeight,
457 const gdiPalette* WINPR_RESTRICT palette)
459 UINT32 suboffset = 0;
461 if (!Stream_CheckAndLogRequiredLength(TAG, s, subcodecByteCount))
464 while (suboffset < subcodecByteCount)
466 if (!Stream_CheckAndLogRequiredLength(TAG, s, 13))
469 const UINT16 xStart = Stream_Get_UINT16(s);
470 const UINT16 yStart = Stream_Get_UINT16(s);
471 const UINT16 width = Stream_Get_UINT16(s);
472 const UINT16 height = Stream_Get_UINT16(s);
473 const UINT32 bitmapDataByteCount = Stream_Get_UINT32(s);
474 const UINT8 subcodecId = Stream_Get_UINT8(s);
477 if (!Stream_CheckAndLogRequiredLength(TAG, s, bitmapDataByteCount))
480 const UINT32 nXDstRel = nXDst + xStart;
481 const UINT32 nYDstRel = nYDst + yStart;
482 if (1ull * nXDstRel + width > nDstWidth)
484 WLog_ERR(TAG,
"nXDstRel %" PRIu32
" + width %" PRIu16
" > nDstWidth %" PRIu32
"",
485 nXDstRel, width, nDstWidth);
488 if (1ull * nYDstRel + height > nDstHeight)
490 WLog_ERR(TAG,
"nYDstRel %" PRIu32
" + height %" PRIu16
" > nDstHeight %" PRIu32
"",
491 nYDstRel, height, nDstHeight);
495 if (1ull * xStart + width > nWidth)
497 WLog_ERR(TAG,
"xStart %" PRIu16
" + width %" PRIu16
" > nWidth %" PRIu32
"", xStart,
501 if (1ull * yStart + height > nHeight)
503 WLog_ERR(TAG,
"yStart %" PRIu16
" + height %" PRIu16
" > nHeight %" PRIu32
"", yStart,
508 if (!clear_resize_buffer(clear, width, height))
515 const UINT32 nSrcStep = width * FreeRDPGetBytesPerPixel(PIXEL_FORMAT_BGR24);
516 const size_t nSrcSize = 1ull * nSrcStep * height;
518 if (bitmapDataByteCount != nSrcSize)
520 WLog_ERR(TAG,
"bitmapDataByteCount %" PRIu32
" != nSrcSize %" PRIuz
"",
521 bitmapDataByteCount, nSrcSize);
525 if (!convert_color(pDstData, nDstStep, DstFormat, nXDstRel, nYDstRel, width, height,
526 Stream_Pointer(s), nSrcStep, PIXEL_FORMAT_BGR24, nDstWidth,
527 nDstHeight, palette))
530 Stream_Seek(s, bitmapDataByteCount);
535 if (!clear_decompress_nscodec(clear->nsc, width, height, s, bitmapDataByteCount,
536 pDstData, DstFormat, nDstStep, nXDstRel, nYDstRel,
537 nDstWidth, nDstHeight))
543 if (!clear_decompress_subcode_rlex(s, bitmapDataByteCount, width, height, pDstData,
544 DstFormat, nDstStep, nXDstRel, nYDstRel,
545 nDstWidth, nDstHeight))
551 WLog_ERR(TAG,
"Unknown subcodec ID %" PRIu8
"", subcodecId);
555 suboffset += bitmapDataByteCount;
561static BOOL resize_vbar_entry(CLEAR_CONTEXT* WINPR_RESTRICT clear,
562 CLEAR_VBAR_ENTRY* WINPR_RESTRICT vBarEntry)
564 if (vBarEntry->count > vBarEntry->size)
566 const UINT32 bpp = FreeRDPGetBytesPerPixel(clear->format);
567 const UINT32 oldPos = vBarEntry->size * bpp;
568 const UINT32 diffSize = (vBarEntry->count - vBarEntry->size) * bpp;
570 vBarEntry->size = vBarEntry->count;
572 (BYTE*)winpr_aligned_recalloc(vBarEntry->pixels, vBarEntry->count, 1ull * bpp, 32);
576 WLog_ERR(TAG,
"vBarEntry->pixels winpr_aligned_recalloc %" PRIu32
" failed",
577 vBarEntry->count * bpp);
581 memset(&tmp[oldPos], 0, diffSize);
582 vBarEntry->pixels = tmp;
585 if (!vBarEntry->pixels && vBarEntry->size)
587 WLog_ERR(TAG,
"vBarEntry->pixels is nullptr but vBarEntry->size is %" PRIu32
"",
595static BOOL clear_decompress_bands_data(CLEAR_CONTEXT* WINPR_RESTRICT clear,
596 wStream* WINPR_RESTRICT s, UINT32 bandsByteCount,
597 UINT32 nWidth, UINT32 nHeight,
598 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
599 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
600 UINT32 nDstWidth, UINT32 nDstHeight)
602 UINT32 suboffset = 0;
604 if (!Stream_CheckAndLogRequiredLength(TAG, s, bandsByteCount))
607 while (suboffset < bandsByteCount)
617 UINT16 vBarHeader = 0;
620 UINT32 vBarCount = 0;
621 UINT32 vBarPixelCount = 0;
622 UINT32 vBarShortPixelCount = 0;
624 if (!Stream_CheckAndLogRequiredLength(TAG, s, 11))
627 Stream_Read_UINT16(s, xStart);
628 Stream_Read_UINT16(s, xEnd);
629 Stream_Read_UINT16(s, yStart);
630 Stream_Read_UINT16(s, yEnd);
631 Stream_Read_UINT8(s, cb);
632 Stream_Read_UINT8(s, cg);
633 Stream_Read_UINT8(s, cr);
635 colorBkg = FreeRDPGetColor(clear->format, cr, cg, cb, 0xFF);
639 WLog_ERR(TAG,
"xEnd %" PRIu16
" < xStart %" PRIu16
"", xEnd, xStart);
645 WLog_ERR(TAG,
"yEnd %" PRIu16
" < yStart %" PRIu16
"", yEnd, yStart);
649 vBarCount = (xEnd - xStart) + 1;
651 for (UINT32 i = 0; i < vBarCount; i++)
653 UINT32 vBarHeight = 0;
654 CLEAR_VBAR_ENTRY* vBarEntry =
nullptr;
655 CLEAR_VBAR_ENTRY* vBarShortEntry =
nullptr;
656 BOOL vBarUpdate = FALSE;
657 const BYTE* cpSrcPixel =
nullptr;
659 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
662 Stream_Read_UINT16(s, vBarHeader);
664 vBarHeight = (yEnd - yStart + 1);
668 WLog_ERR(TAG,
"vBarHeight (%" PRIu32
") > 52", vBarHeight);
672 if ((vBarHeader & 0xC000) == 0x4000)
674 const UINT16 vBarIndex = (vBarHeader & 0x3FFF);
675 vBarShortEntry = &(clear->ShortVBarStorage[vBarIndex]);
679 WLog_ERR(TAG,
"missing vBarShortEntry %" PRIu16
"", vBarIndex);
683 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
686 Stream_Read_UINT8(s, vBarYOn);
688 vBarShortPixelCount = vBarShortEntry->count;
691 else if ((vBarHeader & 0xC000) == 0x0000)
693 vBarYOn = (vBarHeader & 0xFF);
694 vBarYOff = ((vBarHeader >> 8) & 0x3F);
696 if (vBarYOff < vBarYOn)
698 WLog_ERR(TAG,
"vBarYOff %" PRIu16
" < vBarYOn %" PRIu16
"", vBarYOff, vBarYOn);
702 vBarShortPixelCount = (vBarYOff - vBarYOn);
704 if (vBarShortPixelCount > 52)
706 WLog_ERR(TAG,
"vBarShortPixelCount %" PRIu32
" > 52", vBarShortPixelCount);
710 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, vBarShortPixelCount, 3ull))
713 if (clear->ShortVBarStorageCursor >= CLEARCODEC_VBAR_SHORT_SIZE)
716 "clear->ShortVBarStorageCursor %" PRIu32
717 " >= CLEARCODEC_VBAR_SHORT_SIZE (%" PRId32
")",
718 clear->ShortVBarStorageCursor, CLEARCODEC_VBAR_SHORT_SIZE);
722 vBarShortEntry = &(clear->ShortVBarStorage[clear->ShortVBarStorageCursor]);
723 vBarShortEntry->count = vBarShortPixelCount;
725 if (!resize_vbar_entry(clear, vBarShortEntry))
728 for (
size_t y = 0; y < vBarShortPixelCount; y++)
734 &vBarShortEntry->pixels[y * FreeRDPGetBytesPerPixel(clear->format)];
736 Stream_Read_UINT8(s, b);
737 Stream_Read_UINT8(s, g);
738 Stream_Read_UINT8(s, r);
739 color = FreeRDPGetColor(clear->format, r, g, b, 0xFF);
741 if (!FreeRDPWriteColor(dstBuffer, clear->format, color))
745 suboffset += (vBarShortPixelCount * 3);
746 clear->ShortVBarStorageCursor =
747 (clear->ShortVBarStorageCursor + 1) % CLEARCODEC_VBAR_SHORT_SIZE;
750 else if ((vBarHeader & 0x8000) == 0x8000)
752 const UINT16 vBarIndex = (vBarHeader & 0x7FFF);
753 vBarEntry = &(clear->VBarStorage[vBarIndex]);
756 if (vBarEntry->size == 0)
758 WLog_WARN(TAG,
"Empty cache index %" PRIu16
", filling dummy data", vBarIndex);
759 vBarEntry->count = vBarHeight;
761 if (!resize_vbar_entry(clear, vBarEntry))
767 WLog_ERR(TAG,
"invalid vBarHeader 0x%04" PRIX16
"", vBarHeader);
773 BYTE* pSrcPixel =
nullptr;
774 BYTE* dstBuffer =
nullptr;
776 if (clear->VBarStorageCursor >= CLEARCODEC_VBAR_SIZE)
779 "clear->VBarStorageCursor %" PRIu32
" >= CLEARCODEC_VBAR_SIZE %" PRId32
781 clear->VBarStorageCursor, CLEARCODEC_VBAR_SIZE);
785 vBarEntry = &(clear->VBarStorage[clear->VBarStorageCursor]);
786 vBarPixelCount = vBarHeight;
787 vBarEntry->count = vBarPixelCount;
789 if (!resize_vbar_entry(clear, vBarEntry))
792 dstBuffer = vBarEntry->pixels;
795 UINT32 count = vBarYOn;
797 if ((y + count) > vBarPixelCount)
798 count = (vBarPixelCount > y) ? (vBarPixelCount - y) : 0;
804 FreeRDPWriteColor(dstBuffer, clear->format, colorBkg);
805 dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
814 count = vBarShortPixelCount;
816 if ((y + count) > vBarPixelCount)
817 count = (vBarPixelCount > y) ? (vBarPixelCount - y) : 0;
821 const size_t offset =
822 (1ull * y - vBarYOn) * FreeRDPGetBytesPerPixel(clear->format);
823 pSrcPixel = &vBarShortEntry->pixels[offset];
824 if (offset + count > vBarShortEntry->count)
826 WLog_ERR(TAG,
"offset + count > vBarShortEntry->count");
830 for (
size_t x = 0; x < count; x++)
833 color = FreeRDPReadColor(&pSrcPixel[x * FreeRDPGetBytesPerPixel(clear->format)],
836 if (!FreeRDPWriteColor(dstBuffer, clear->format, color))
839 dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
843 y = vBarYOn + vBarShortPixelCount;
844 count = (vBarPixelCount > y) ? (vBarPixelCount - y) : 0;
850 if (!FreeRDPWriteColor(dstBuffer, clear->format, colorBkg))
853 dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
857 vBarEntry->count = vBarPixelCount;
858 clear->VBarStorageCursor = (clear->VBarStorageCursor + 1) % CLEARCODEC_VBAR_SIZE;
861 if (vBarEntry->count != vBarHeight)
863 WLog_ERR(TAG,
"vBarEntry->count %" PRIu32
" != vBarHeight %" PRIu32
"",
864 vBarEntry->count, vBarHeight);
865 vBarEntry->count = vBarHeight;
867 if (!resize_vbar_entry(clear, vBarEntry))
871 const UINT32 nXDstRel = nXDst + xStart;
872 const UINT32 nYDstRel = nYDst + yStart;
873 cpSrcPixel = vBarEntry->pixels;
877 UINT32 count = vBarEntry->count;
882 if (nXDstRel + i >= nDstWidth)
885 for (UINT32 y = 0; y < count; y++)
887 if (nYDstRel + y >= nDstHeight)
891 &pDstData[((nYDstRel + y) * nDstStep) +
892 ((nXDstRel + i) * FreeRDPGetBytesPerPixel(DstFormat))];
893 UINT32 color = FreeRDPReadColor(cpSrcPixel, clear->format);
894 color = FreeRDPConvertColor(color, clear->format, DstFormat,
nullptr);
896 if (!FreeRDPWriteColor(pDstPixel8, DstFormat, color))
899 cpSrcPixel += FreeRDPGetBytesPerPixel(clear->format);
908static BOOL clear_decompress_glyph_data(CLEAR_CONTEXT* WINPR_RESTRICT clear,
909 wStream* WINPR_RESTRICT s, UINT32 glyphFlags, UINT32 nWidth,
910 UINT32 nHeight, BYTE* WINPR_RESTRICT pDstData,
911 UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst,
912 UINT32 nYDst, UINT32 nDstWidth, UINT32 nDstHeight,
913 const gdiPalette* WINPR_RESTRICT palette,
914 BYTE** WINPR_RESTRICT ppGlyphData)
916 UINT16 glyphIndex = 0;
919 *ppGlyphData =
nullptr;
921 if ((glyphFlags & CLEARCODEC_FLAG_GLYPH_HIT) && !(glyphFlags & CLEARCODEC_FLAG_GLYPH_INDEX))
923 WLog_ERR(TAG,
"Invalid glyph flags %08" PRIX32
"", glyphFlags);
927 if ((glyphFlags & CLEARCODEC_FLAG_GLYPH_INDEX) == 0)
930 if ((nWidth * nHeight) > (1024 * 1024))
932 WLog_ERR(TAG,
"glyph too large: %" PRIu32
"x%" PRIu32
"", nWidth, nHeight);
936 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
939 Stream_Read_UINT16(s, glyphIndex);
941 if (glyphIndex >= 4000)
943 WLog_ERR(TAG,
"Invalid glyphIndex %" PRIu16
"", glyphIndex);
947 if (glyphFlags & CLEARCODEC_FLAG_GLYPH_HIT)
950 CLEAR_GLYPH_ENTRY* glyphEntry = &(clear->GlyphCache[glyphIndex]);
951 BYTE* glyphData =
nullptr;
955 WLog_ERR(TAG,
"clear->GlyphCache[%" PRIu16
"]=nullptr", glyphIndex);
959 glyphData = (BYTE*)glyphEntry->pixels;
963 WLog_ERR(TAG,
"clear->GlyphCache[%" PRIu16
"]->pixels=nullptr", glyphIndex);
967 if ((nWidth * nHeight) > glyphEntry->count)
970 "(nWidth %" PRIu32
" * nHeight %" PRIu32
") > glyphEntry->count %" PRIu32
"",
971 nWidth, nHeight, glyphEntry->count);
975 nSrcStep = nWidth * FreeRDPGetBytesPerPixel(clear->format);
976 return convert_color(pDstData, nDstStep, DstFormat, nXDst, nYDst, nWidth, nHeight,
977 glyphData, nSrcStep, clear->format, nDstWidth, nDstHeight, palette);
980 if (glyphFlags & CLEARCODEC_FLAG_GLYPH_INDEX)
982 const UINT32 bpp = FreeRDPGetBytesPerPixel(clear->format);
983 CLEAR_GLYPH_ENTRY* glyphEntry = &(clear->GlyphCache[glyphIndex]);
984 glyphEntry->count = nWidth * nHeight;
986 if (glyphEntry->count > glyphEntry->size)
989 winpr_aligned_recalloc(glyphEntry->pixels, glyphEntry->count, 1ull * bpp, 32);
993 WLog_ERR(TAG,
"glyphEntry->pixels winpr_aligned_recalloc %" PRIu32
" failed!",
994 glyphEntry->count * bpp);
998 glyphEntry->size = glyphEntry->count;
999 glyphEntry->pixels = (UINT32*)tmp;
1002 if (!glyphEntry->pixels)
1004 WLog_ERR(TAG,
"glyphEntry->pixels=nullptr");
1009 *ppGlyphData = (BYTE*)glyphEntry->pixels;
1017static inline BOOL updateContextFormat(CLEAR_CONTEXT* WINPR_RESTRICT clear, UINT32 DstFormat)
1019 if (!clear || !clear->nsc)
1022 clear->format = DstFormat;
1023 return nsc_context_set_parameters(clear->nsc, NSC_COLOR_FORMAT, DstFormat);
1026INT32 clear_decompress(CLEAR_CONTEXT* WINPR_RESTRICT clear,
const BYTE* WINPR_RESTRICT pSrcData,
1027 UINT32 SrcSize, UINT32 nWidth, UINT32 nHeight, BYTE* WINPR_RESTRICT pDstData,
1028 UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
1029 UINT32 nDstWidth, UINT32 nDstHeight,
1030 const gdiPalette* WINPR_RESTRICT palette)
1034 BYTE glyphFlags = 0;
1035 UINT32 residualByteCount = 0;
1036 UINT32 bandsByteCount = 0;
1037 UINT32 subcodecByteCount = 0;
1038 wStream sbuffer = WINPR_C_ARRAY_INIT;
1040 BYTE* glyphData =
nullptr;
1045 if ((nDstWidth == 0) || (nDstHeight == 0))
1048 if ((nWidth > 0xFFFF) || (nHeight > 0xFFFF))
1051 if (nXDst > nDstWidth)
1053 WLog_WARN(TAG,
"nXDst %" PRIu32
" > nDstWidth %" PRIu32, nXDst, nDstWidth);
1057 if (nYDst > nDstHeight)
1059 WLog_WARN(TAG,
"nYDst %" PRIu32
" > nDstHeight %" PRIu32, nYDst, nDstHeight);
1063 s = Stream_StaticConstInit(&sbuffer, pSrcData, SrcSize);
1068 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
1071 if (!updateContextFormat(clear, DstFormat))
1074 Stream_Read_UINT8(s, glyphFlags);
1075 Stream_Read_UINT8(s, seqNumber);
1077 if (!clear->seqNumber && seqNumber)
1078 clear->seqNumber = seqNumber;
1080 if (seqNumber != clear->seqNumber)
1082 WLog_ERR(TAG,
"Sequence number unexpected %" PRIu8
" - %" PRIu32
"", seqNumber,
1084 WLog_ERR(TAG,
"seqNumber %" PRIu8
" != clear->seqNumber %" PRIu32
"", seqNumber,
1089 clear->seqNumber = (seqNumber + 1) % 256;
1091 if (glyphFlags & CLEARCODEC_FLAG_CACHE_RESET)
1093 clear_reset_vbar_storage(clear, FALSE);
1096 if (!clear_decompress_glyph_data(clear, s, glyphFlags, nWidth, nHeight, pDstData, DstFormat,
1097 nDstStep, nXDst, nYDst, nDstWidth, nDstHeight, palette,
1100 WLog_ERR(TAG,
"clear_decompress_glyph_data failed!");
1105 if (Stream_GetRemainingLength(s) < 12)
1107 const UINT32 mask = (CLEARCODEC_FLAG_GLYPH_HIT | CLEARCODEC_FLAG_GLYPH_INDEX);
1109 if ((glyphFlags & mask) == mask)
1113 "invalid glyphFlags, missing flags: 0x%02" PRIx8
" & 0x%02" PRIx32
1115 glyphFlags, mask, glyphFlags & mask);
1119 Stream_Read_UINT32(s, residualByteCount);
1120 Stream_Read_UINT32(s, bandsByteCount);
1121 Stream_Read_UINT32(s, subcodecByteCount);
1123 if (residualByteCount > 0)
1125 if (!clear_decompress_residual_data(clear, s, residualByteCount, nWidth, nHeight, pDstData,
1126 DstFormat, nDstStep, nXDst, nYDst, nDstWidth,
1127 nDstHeight, palette))
1129 WLog_ERR(TAG,
"clear_decompress_residual_data failed!");
1134 if (bandsByteCount > 0)
1136 if (!clear_decompress_bands_data(clear, s, bandsByteCount, nWidth, nHeight, pDstData,
1137 DstFormat, nDstStep, nXDst, nYDst, nDstWidth, nDstHeight))
1139 WLog_ERR(TAG,
"clear_decompress_bands_data failed!");
1144 if (subcodecByteCount > 0)
1146 if (!clear_decompress_subcodecs_data(clear, s, subcodecByteCount, nWidth, nHeight, pDstData,
1147 DstFormat, nDstStep, nXDst, nYDst, nDstWidth,
1148 nDstHeight, palette))
1150 WLog_ERR(TAG,
"clear_decompress_subcodecs_data failed!");
1157 uint32_t w = MIN(nWidth, nDstWidth);
1158 if (nXDst > nDstWidth)
1160 WLog_WARN(TAG,
"glyphData copy area x exceeds destination: x=%" PRIu32
" > %" PRIu32,
1164 else if (nXDst + w > nDstWidth)
1167 "glyphData copy area x + width exceeds destination: x=%" PRIu32
" + %" PRIu32
1169 nXDst, w, nDstWidth);
1170 w = nDstWidth - nXDst;
1176 "glyphData copy area width truncated: requested=%" PRIu32
1177 ", truncated to %" PRIu32,
1181 uint32_t h = MIN(nHeight, nDstHeight);
1182 if (nYDst > nDstHeight)
1184 WLog_WARN(TAG,
"glyphData copy area y exceeds destination: y=%" PRIu32
" > %" PRIu32,
1188 else if (nYDst + h > nDstHeight)
1191 "glyphData copy area y + height exceeds destination: x=%" PRIu32
" + %" PRIu32
1193 nYDst, h, nDstHeight);
1194 h = nDstHeight - nYDst;
1200 "glyphData copy area height truncated: requested=%" PRIu32
1201 ", truncated to %" PRIu32,
1205 if (!freerdp_image_copy_no_overlap(glyphData, clear->format, 0, 0, 0, w, h, pDstData,
1206 DstFormat, nDstStep, nXDst, nYDst, palette,
1207 FREERDP_KEEP_DST_ALPHA))
1217int clear_compress(WINPR_ATTR_UNUSED CLEAR_CONTEXT* WINPR_RESTRICT clear,
1218 WINPR_ATTR_UNUSED
const BYTE* WINPR_RESTRICT pSrcData,
1219 WINPR_ATTR_UNUSED UINT32 SrcSize,
1220 WINPR_ATTR_UNUSED BYTE** WINPR_RESTRICT ppDstData,
1221 WINPR_ATTR_UNUSED UINT32* WINPR_RESTRICT pDstSize)
1223 WLog_ERR(TAG,
"TODO: not implemented!");
1227BOOL clear_context_reset(CLEAR_CONTEXT* WINPR_RESTRICT clear)
1236 clear->seqNumber = 0;
1240CLEAR_CONTEXT* clear_context_new(BOOL Compressor)
1242 CLEAR_CONTEXT* clear = (CLEAR_CONTEXT*)winpr_aligned_calloc(1,
sizeof(CLEAR_CONTEXT), 32);
1247 clear->Compressor = Compressor;
1248 clear->nsc = nsc_context_new();
1253 if (!updateContextFormat(clear, PIXEL_FORMAT_BGRX32))
1256 if (!clear_resize_buffer(clear, 512, 512))
1259 if (!clear->TempBuffer)
1262 if (!clear_context_reset(clear))
1267 WINPR_PRAGMA_DIAG_PUSH
1268 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1269 clear_context_free(clear);
1270 WINPR_PRAGMA_DIAG_POP
1274void clear_context_free(CLEAR_CONTEXT* WINPR_RESTRICT clear)
1279 nsc_context_free(clear->nsc);
1280 winpr_aligned_free(clear->TempBuffer);
1282 clear_reset_vbar_storage(clear, TRUE);
1283 clear_reset_glyph_cache(clear);
1285 winpr_aligned_free(clear);