22 #include <freerdp/config.h>
24 #include <winpr/crt.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
55 struct S_CLEAR_CONTEXT
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];
72 static 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
83 static const BYTE CLEAR_8BIT_MASKS[9] = { 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF };
85 static 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;
108 static 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));
116 static 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);
133 static 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);
150 static 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 %" PRIu32
" + 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 %" PRIu32
" + 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 %" PRIdz
" != pixelCount %" PRIu32
"", pixelIndex, pixelCount);
329 static BOOL clear_resize_buffer(CLEAR_CONTEXT* WINPR_RESTRICT clear, UINT32 width, UINT32 height)
336 size = ((width + 16) * (height + 16) * FreeRDPGetBytesPerPixel(clear->format));
338 if (size > clear->TempSize)
340 BYTE* tmp = (BYTE*)winpr_aligned_recalloc(clear->TempBuffer, size,
sizeof(BYTE), 32);
344 WLog_ERR(TAG,
"clear->TempBuffer winpr_aligned_recalloc failed for %" PRIu32
" bytes",
349 clear->TempSize = size;
350 clear->TempBuffer = tmp;
356 static BOOL clear_decompress_residual_data(CLEAR_CONTEXT* WINPR_RESTRICT clear,
357 wStream* WINPR_RESTRICT s, UINT32 residualByteCount,
358 UINT32 nWidth, UINT32 nHeight,
359 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
360 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
361 UINT32 nDstWidth, UINT32 nDstHeight,
362 const gdiPalette* WINPR_RESTRICT palette)
365 UINT32 suboffset = 0;
366 BYTE* dstBuffer = NULL;
367 UINT32 pixelIndex = 0;
368 UINT32 pixelCount = 0;
370 if (!Stream_CheckAndLogRequiredLength(TAG, s, residualByteCount))
375 pixelCount = nWidth * nHeight;
377 if (!clear_resize_buffer(clear, nWidth, nHeight))
380 dstBuffer = clear->TempBuffer;
382 while (suboffset < residualByteCount)
387 UINT32 runLengthFactor = 0;
390 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
393 Stream_Read_UINT8(s, b);
394 Stream_Read_UINT8(s, g);
395 Stream_Read_UINT8(s, r);
396 Stream_Read_UINT8(s, runLengthFactor);
398 color = FreeRDPGetColor(clear->format, r, g, b, 0xFF);
400 if (runLengthFactor >= 0xFF)
402 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
405 Stream_Read_UINT16(s, runLengthFactor);
408 if (runLengthFactor >= 0xFFFF)
410 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
413 Stream_Read_UINT32(s, runLengthFactor);
418 if ((pixelIndex >= pixelCount) || (runLengthFactor > (pixelCount - pixelIndex)))
421 "pixelIndex %" PRIu32
" + runLengthFactor %" PRIu32
" > pixelCount %" PRIu32
423 pixelIndex, runLengthFactor, pixelCount);
427 for (UINT32 i = 0; i < runLengthFactor; i++)
429 FreeRDPWriteColor(dstBuffer, clear->format, color);
430 dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
433 pixelIndex += runLengthFactor;
436 nSrcStep = nWidth * FreeRDPGetBytesPerPixel(clear->format);
438 if (pixelIndex != pixelCount)
440 WLog_ERR(TAG,
"pixelIndex %" PRIu32
" != pixelCount %" PRIu32
"", pixelIndex, pixelCount);
444 return convert_color(pDstData, nDstStep, DstFormat, nXDst, nYDst, nWidth, nHeight,
445 clear->TempBuffer, nSrcStep, clear->format, nDstWidth, nDstHeight,
449 static BOOL clear_decompress_subcodecs_data(CLEAR_CONTEXT* WINPR_RESTRICT clear,
450 wStream* WINPR_RESTRICT s, UINT32 subcodecByteCount,
451 UINT32 nWidth, UINT32 nHeight,
452 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
453 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
454 UINT32 nDstWidth, UINT32 nDstHeight,
455 const gdiPalette* WINPR_RESTRICT palette)
461 UINT32 bitmapDataByteCount = 0;
463 UINT32 suboffset = 0;
465 if (!Stream_CheckAndLogRequiredLength(TAG, s, subcodecByteCount))
470 while (suboffset < subcodecByteCount)
475 if (!Stream_CheckAndLogRequiredLength(TAG, s, 13))
478 Stream_Read_UINT16(s, xStart);
479 Stream_Read_UINT16(s, yStart);
480 Stream_Read_UINT16(s, width);
481 Stream_Read_UINT16(s, height);
482 Stream_Read_UINT32(s, bitmapDataByteCount);
483 Stream_Read_UINT8(s, subcodecId);
486 if (!Stream_CheckAndLogRequiredLength(TAG, s, bitmapDataByteCount))
489 nXDstRel = nXDst + xStart;
490 nYDstRel = nYDst + yStart;
492 if (1ull * xStart + width > nWidth)
494 WLog_ERR(TAG,
"xStart %" PRIu16
" + width %" PRIu16
" > nWidth %" PRIu32
"", xStart,
498 if (1ull * yStart + height > nHeight)
500 WLog_ERR(TAG,
"yStart %" PRIu16
" + height %" PRIu16
" > nHeight %" PRIu32
"", yStart,
505 if (!clear_resize_buffer(clear, width, height))
512 const UINT32 nSrcStep = width * FreeRDPGetBytesPerPixel(PIXEL_FORMAT_BGR24);
513 const size_t nSrcSize = 1ull * nSrcStep * height;
515 if (bitmapDataByteCount != nSrcSize)
517 WLog_ERR(TAG,
"bitmapDataByteCount %" PRIu32
" != nSrcSize %" PRIuz
"",
518 bitmapDataByteCount, nSrcSize);
522 if (!convert_color(pDstData, nDstStep, DstFormat, nXDstRel, nYDstRel, width, height,
523 Stream_Pointer(s), nSrcStep, PIXEL_FORMAT_BGR24, nDstWidth,
524 nDstHeight, palette))
527 Stream_Seek(s, bitmapDataByteCount);
532 if (!clear_decompress_nscodec(clear->nsc, width, height, s, bitmapDataByteCount,
533 pDstData, DstFormat, nDstStep, nXDstRel, nYDstRel))
539 if (!clear_decompress_subcode_rlex(s, bitmapDataByteCount, width, height, pDstData,
540 DstFormat, nDstStep, nXDstRel, nYDstRel,
541 nDstWidth, nDstHeight))
547 WLog_ERR(TAG,
"Unknown subcodec ID %" PRIu8
"", subcodecId);
551 suboffset += bitmapDataByteCount;
557 static BOOL resize_vbar_entry(CLEAR_CONTEXT* WINPR_RESTRICT clear,
558 CLEAR_VBAR_ENTRY* WINPR_RESTRICT vBarEntry)
560 if (vBarEntry->count > vBarEntry->size)
562 const UINT32 bpp = FreeRDPGetBytesPerPixel(clear->format);
563 const UINT32 oldPos = vBarEntry->size * bpp;
564 const UINT32 diffSize = (vBarEntry->count - vBarEntry->size) * bpp;
566 vBarEntry->size = vBarEntry->count;
568 (BYTE*)winpr_aligned_recalloc(vBarEntry->pixels, vBarEntry->count, 1ull * bpp, 32);
572 WLog_ERR(TAG,
"vBarEntry->pixels winpr_aligned_recalloc %" PRIu32
" failed",
573 vBarEntry->count * bpp);
577 memset(&tmp[oldPos], 0, diffSize);
578 vBarEntry->pixels = tmp;
581 if (!vBarEntry->pixels && vBarEntry->size)
583 WLog_ERR(TAG,
"vBarEntry->pixels is NULL but vBarEntry->size is %" PRIu32
"",
591 static BOOL clear_decompress_bands_data(CLEAR_CONTEXT* WINPR_RESTRICT clear,
592 wStream* WINPR_RESTRICT s, UINT32 bandsByteCount,
593 UINT32 nWidth, UINT32 nHeight,
594 BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
595 UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
596 UINT32 nDstWidth, UINT32 nDstHeight)
598 UINT32 suboffset = 0;
600 if (!Stream_CheckAndLogRequiredLength(TAG, s, bandsByteCount))
603 while (suboffset < bandsByteCount)
613 UINT16 vBarHeader = 0;
616 UINT32 vBarCount = 0;
617 UINT32 vBarPixelCount = 0;
618 UINT32 vBarShortPixelCount = 0;
620 if (!Stream_CheckAndLogRequiredLength(TAG, s, 11))
623 Stream_Read_UINT16(s, xStart);
624 Stream_Read_UINT16(s, xEnd);
625 Stream_Read_UINT16(s, yStart);
626 Stream_Read_UINT16(s, yEnd);
627 Stream_Read_UINT8(s, cb);
628 Stream_Read_UINT8(s, cg);
629 Stream_Read_UINT8(s, cr);
631 colorBkg = FreeRDPGetColor(clear->format, cr, cg, cb, 0xFF);
635 WLog_ERR(TAG,
"xEnd %" PRIu16
" < xStart %" PRIu16
"", xEnd, xStart);
641 WLog_ERR(TAG,
"yEnd %" PRIu16
" < yStart %" PRIu16
"", yEnd, yStart);
645 vBarCount = (xEnd - xStart) + 1;
647 for (UINT32 i = 0; i < vBarCount; i++)
649 UINT32 vBarHeight = 0;
650 CLEAR_VBAR_ENTRY* vBarEntry = NULL;
651 CLEAR_VBAR_ENTRY* vBarShortEntry = NULL;
652 BOOL vBarUpdate = FALSE;
653 const BYTE* cpSrcPixel = NULL;
655 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
658 Stream_Read_UINT16(s, vBarHeader);
660 vBarHeight = (yEnd - yStart + 1);
664 WLog_ERR(TAG,
"vBarHeight (%" PRIu32
") > 52", vBarHeight);
668 if ((vBarHeader & 0xC000) == 0x4000)
670 const UINT16 vBarIndex = (vBarHeader & 0x3FFF);
671 vBarShortEntry = &(clear->ShortVBarStorage[vBarIndex]);
675 WLog_ERR(TAG,
"missing vBarShortEntry %" PRIu16
"", vBarIndex);
679 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
682 Stream_Read_UINT8(s, vBarYOn);
684 vBarShortPixelCount = vBarShortEntry->count;
687 else if ((vBarHeader & 0xC000) == 0x0000)
689 vBarYOn = (vBarHeader & 0xFF);
690 vBarYOff = ((vBarHeader >> 8) & 0x3F);
692 if (vBarYOff < vBarYOn)
694 WLog_ERR(TAG,
"vBarYOff %" PRIu16
" < vBarYOn %" PRIu16
"", vBarYOff, vBarYOn);
698 vBarShortPixelCount = (vBarYOff - vBarYOn);
700 if (vBarShortPixelCount > 52)
702 WLog_ERR(TAG,
"vBarShortPixelCount %" PRIu32
" > 52", vBarShortPixelCount);
706 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, vBarShortPixelCount, 3ull))
709 if (clear->ShortVBarStorageCursor >= CLEARCODEC_VBAR_SHORT_SIZE)
712 "clear->ShortVBarStorageCursor %" PRIu32
713 " >= CLEARCODEC_VBAR_SHORT_SIZE (%" PRIu32
")",
714 clear->ShortVBarStorageCursor, CLEARCODEC_VBAR_SHORT_SIZE);
718 vBarShortEntry = &(clear->ShortVBarStorage[clear->ShortVBarStorageCursor]);
719 vBarShortEntry->count = vBarShortPixelCount;
721 if (!resize_vbar_entry(clear, vBarShortEntry))
724 for (
size_t y = 0; y < vBarShortPixelCount; y++)
730 &vBarShortEntry->pixels[y * FreeRDPGetBytesPerPixel(clear->format)];
732 Stream_Read_UINT8(s, b);
733 Stream_Read_UINT8(s, g);
734 Stream_Read_UINT8(s, r);
735 color = FreeRDPGetColor(clear->format, r, g, b, 0xFF);
737 if (!FreeRDPWriteColor(dstBuffer, clear->format, color))
741 suboffset += (vBarShortPixelCount * 3);
742 clear->ShortVBarStorageCursor =
743 (clear->ShortVBarStorageCursor + 1) % CLEARCODEC_VBAR_SHORT_SIZE;
746 else if ((vBarHeader & 0x8000) == 0x8000)
748 const UINT16 vBarIndex = (vBarHeader & 0x7FFF);
749 vBarEntry = &(clear->VBarStorage[vBarIndex]);
752 if (vBarEntry->size == 0)
754 WLog_WARN(TAG,
"Empty cache index %" PRIu16
", filling dummy data", vBarIndex);
755 vBarEntry->count = vBarHeight;
757 if (!resize_vbar_entry(clear, vBarEntry))
763 WLog_ERR(TAG,
"invalid vBarHeader 0x%04" PRIX16
"", vBarHeader);
769 BYTE* pSrcPixel = NULL;
770 BYTE* dstBuffer = NULL;
772 if (clear->VBarStorageCursor >= CLEARCODEC_VBAR_SIZE)
775 "clear->VBarStorageCursor %" PRIu32
" >= CLEARCODEC_VBAR_SIZE %" PRIu32
777 clear->VBarStorageCursor, CLEARCODEC_VBAR_SIZE);
781 vBarEntry = &(clear->VBarStorage[clear->VBarStorageCursor]);
782 vBarPixelCount = vBarHeight;
783 vBarEntry->count = vBarPixelCount;
785 if (!resize_vbar_entry(clear, vBarEntry))
788 dstBuffer = vBarEntry->pixels;
791 UINT32 count = vBarYOn;
793 if ((y + count) > vBarPixelCount)
794 count = (vBarPixelCount > y) ? (vBarPixelCount - y) : 0;
800 FreeRDPWriteColor(dstBuffer, clear->format, colorBkg);
801 dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
810 count = vBarShortPixelCount;
812 if ((y + count) > vBarPixelCount)
813 count = (vBarPixelCount > y) ? (vBarPixelCount - y) : 0;
817 const size_t offset =
818 (1ull * y - vBarYOn) * FreeRDPGetBytesPerPixel(clear->format);
819 pSrcPixel = &vBarShortEntry->pixels[offset];
820 if (offset + count > vBarShortEntry->count)
822 WLog_ERR(TAG,
"offset + count > vBarShortEntry->count");
826 for (
size_t x = 0; x < count; x++)
829 color = FreeRDPReadColor(&pSrcPixel[x * FreeRDPGetBytesPerPixel(clear->format)],
832 if (!FreeRDPWriteColor(dstBuffer, clear->format, color))
835 dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
839 y = vBarYOn + vBarShortPixelCount;
840 count = (vBarPixelCount > y) ? (vBarPixelCount - y) : 0;
846 if (!FreeRDPWriteColor(dstBuffer, clear->format, colorBkg))
849 dstBuffer += FreeRDPGetBytesPerPixel(clear->format);
853 vBarEntry->count = vBarPixelCount;
854 clear->VBarStorageCursor = (clear->VBarStorageCursor + 1) % CLEARCODEC_VBAR_SIZE;
857 if (vBarEntry->count != vBarHeight)
859 WLog_ERR(TAG,
"vBarEntry->count %" PRIu32
" != vBarHeight %" PRIu32
"",
860 vBarEntry->count, vBarHeight);
861 vBarEntry->count = vBarHeight;
863 if (!resize_vbar_entry(clear, vBarEntry))
867 const UINT32 nXDstRel = nXDst + xStart;
868 const UINT32 nYDstRel = nYDst + yStart;
869 cpSrcPixel = vBarEntry->pixels;
873 UINT32 count = vBarEntry->count;
878 if (nXDstRel + i > nDstWidth)
881 for (UINT32 y = 0; y < count; y++)
883 if (nYDstRel + y > nDstHeight)
887 &pDstData[((nYDstRel + y) * nDstStep) +
888 ((nXDstRel + i) * FreeRDPGetBytesPerPixel(DstFormat))];
889 UINT32 color = FreeRDPReadColor(cpSrcPixel, clear->format);
890 color = FreeRDPConvertColor(color, clear->format, DstFormat, NULL);
892 if (!FreeRDPWriteColor(pDstPixel8, DstFormat, color))
895 cpSrcPixel += FreeRDPGetBytesPerPixel(clear->format);
904 static BOOL clear_decompress_glyph_data(CLEAR_CONTEXT* WINPR_RESTRICT clear,
905 wStream* WINPR_RESTRICT s, UINT32 glyphFlags, UINT32 nWidth,
906 UINT32 nHeight, BYTE* WINPR_RESTRICT pDstData,
907 UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst,
908 UINT32 nYDst, UINT32 nDstWidth, UINT32 nDstHeight,
909 const gdiPalette* WINPR_RESTRICT palette,
910 BYTE** WINPR_RESTRICT ppGlyphData)
912 UINT16 glyphIndex = 0;
917 if ((glyphFlags & CLEARCODEC_FLAG_GLYPH_HIT) && !(glyphFlags & CLEARCODEC_FLAG_GLYPH_INDEX))
919 WLog_ERR(TAG,
"Invalid glyph flags %08" PRIX32
"", glyphFlags);
923 if ((glyphFlags & CLEARCODEC_FLAG_GLYPH_INDEX) == 0)
926 if ((nWidth * nHeight) > (1024 * 1024))
928 WLog_ERR(TAG,
"glyph too large: %" PRIu32
"x%" PRIu32
"", nWidth, nHeight);
932 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
935 Stream_Read_UINT16(s, glyphIndex);
937 if (glyphIndex >= 4000)
939 WLog_ERR(TAG,
"Invalid glyphIndex %" PRIu16
"", glyphIndex);
943 if (glyphFlags & CLEARCODEC_FLAG_GLYPH_HIT)
946 CLEAR_GLYPH_ENTRY* glyphEntry = &(clear->GlyphCache[glyphIndex]);
947 BYTE* glyphData = NULL;
951 WLog_ERR(TAG,
"clear->GlyphCache[%" PRIu16
"]=NULL", glyphIndex);
955 glyphData = (BYTE*)glyphEntry->pixels;
959 WLog_ERR(TAG,
"clear->GlyphCache[%" PRIu16
"]->pixels=NULL", glyphIndex);
963 if ((nWidth * nHeight) > glyphEntry->count)
966 "(nWidth %" PRIu32
" * nHeight %" PRIu32
") > glyphEntry->count %" PRIu32
"",
967 nWidth, nHeight, glyphEntry->count);
971 nSrcStep = nWidth * FreeRDPGetBytesPerPixel(clear->format);
972 return convert_color(pDstData, nDstStep, DstFormat, nXDst, nYDst, nWidth, nHeight,
973 glyphData, nSrcStep, clear->format, nDstWidth, nDstHeight, palette);
976 if (glyphFlags & CLEARCODEC_FLAG_GLYPH_INDEX)
978 const UINT32 bpp = FreeRDPGetBytesPerPixel(clear->format);
979 CLEAR_GLYPH_ENTRY* glyphEntry = &(clear->GlyphCache[glyphIndex]);
980 glyphEntry->count = nWidth * nHeight;
982 if (glyphEntry->count > glyphEntry->size)
985 winpr_aligned_recalloc(glyphEntry->pixels, glyphEntry->count, 1ull * bpp, 32);
989 WLog_ERR(TAG,
"glyphEntry->pixels winpr_aligned_recalloc %" PRIu32
" failed!",
990 glyphEntry->count * bpp);
994 glyphEntry->size = glyphEntry->count;
995 glyphEntry->pixels = (UINT32*)tmp;
998 if (!glyphEntry->pixels)
1000 WLog_ERR(TAG,
"glyphEntry->pixels=NULL");
1005 *ppGlyphData = (BYTE*)glyphEntry->pixels;
1013 static INLINE BOOL updateContextFormat(CLEAR_CONTEXT* WINPR_RESTRICT clear, UINT32 DstFormat)
1015 if (!clear || !clear->nsc)
1018 clear->format = DstFormat;
1019 return nsc_context_set_parameters(clear->nsc, NSC_COLOR_FORMAT, DstFormat);
1022 INT32 clear_decompress(CLEAR_CONTEXT* WINPR_RESTRICT clear,
const BYTE* WINPR_RESTRICT pSrcData,
1023 UINT32 SrcSize, UINT32 nWidth, UINT32 nHeight, BYTE* WINPR_RESTRICT pDstData,
1024 UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
1025 UINT32 nDstWidth, UINT32 nDstHeight,
1026 const gdiPalette* WINPR_RESTRICT palette)
1030 BYTE glyphFlags = 0;
1031 UINT32 residualByteCount = 0;
1032 UINT32 bandsByteCount = 0;
1033 UINT32 subcodecByteCount = 0;
1036 BYTE* glyphData = NULL;
1041 if ((nDstWidth == 0) || (nDstHeight == 0))
1044 if ((nWidth > 0xFFFF) || (nHeight > 0xFFFF))
1047 s = Stream_StaticConstInit(&sbuffer, pSrcData, SrcSize);
1052 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
1055 if (!updateContextFormat(clear, DstFormat))
1058 Stream_Read_UINT8(s, glyphFlags);
1059 Stream_Read_UINT8(s, seqNumber);
1061 if (!clear->seqNumber && seqNumber)
1062 clear->seqNumber = seqNumber;
1064 if (seqNumber != clear->seqNumber)
1066 WLog_ERR(TAG,
"Sequence number unexpected %" PRIu8
" - %" PRIu32
"", seqNumber,
1068 WLog_ERR(TAG,
"seqNumber %" PRIu8
" != clear->seqNumber %" PRIu32
"", seqNumber,
1073 clear->seqNumber = (seqNumber + 1) % 256;
1075 if (glyphFlags & CLEARCODEC_FLAG_CACHE_RESET)
1077 clear_reset_vbar_storage(clear, FALSE);
1080 if (!clear_decompress_glyph_data(clear, s, glyphFlags, nWidth, nHeight, pDstData, DstFormat,
1081 nDstStep, nXDst, nYDst, nDstWidth, nDstHeight, palette,
1084 WLog_ERR(TAG,
"clear_decompress_glyph_data failed!");
1089 if (Stream_GetRemainingLength(s) < 12)
1091 const UINT32 mask = (CLEARCODEC_FLAG_GLYPH_HIT | CLEARCODEC_FLAG_GLYPH_INDEX);
1093 if ((glyphFlags & mask) == mask)
1097 "invalid glyphFlags, missing flags: 0x%02" PRIx8
" & 0x%02" PRIx32
1099 glyphFlags, mask, glyphFlags & mask);
1103 Stream_Read_UINT32(s, residualByteCount);
1104 Stream_Read_UINT32(s, bandsByteCount);
1105 Stream_Read_UINT32(s, subcodecByteCount);
1107 if (residualByteCount > 0)
1109 if (!clear_decompress_residual_data(clear, s, residualByteCount, nWidth, nHeight, pDstData,
1110 DstFormat, nDstStep, nXDst, nYDst, nDstWidth,
1111 nDstHeight, palette))
1113 WLog_ERR(TAG,
"clear_decompress_residual_data failed!");
1118 if (bandsByteCount > 0)
1120 if (!clear_decompress_bands_data(clear, s, bandsByteCount, nWidth, nHeight, pDstData,
1121 DstFormat, nDstStep, nXDst, nYDst, nDstWidth, nDstHeight))
1123 WLog_ERR(TAG,
"clear_decompress_bands_data failed!");
1128 if (subcodecByteCount > 0)
1130 if (!clear_decompress_subcodecs_data(clear, s, subcodecByteCount, nWidth, nHeight, pDstData,
1131 DstFormat, nDstStep, nXDst, nYDst, nDstWidth,
1132 nDstHeight, palette))
1134 WLog_ERR(TAG,
"clear_decompress_subcodecs_data failed!");
1141 if (!freerdp_image_copy_no_overlap(glyphData, clear->format, 0, 0, 0, nWidth, nHeight,
1142 pDstData, DstFormat, nDstStep, nXDst, nYDst, palette,
1143 FREERDP_KEEP_DST_ALPHA))
1153 int clear_compress(CLEAR_CONTEXT* WINPR_RESTRICT clear,
const BYTE* WINPR_RESTRICT pSrcData,
1154 UINT32 SrcSize, BYTE** WINPR_RESTRICT ppDstData, UINT32* WINPR_RESTRICT pDstSize)
1156 WLog_ERR(TAG,
"TODO: not implemented!");
1160 BOOL clear_context_reset(CLEAR_CONTEXT* WINPR_RESTRICT clear)
1169 clear->seqNumber = 0;
1173 CLEAR_CONTEXT* clear_context_new(BOOL Compressor)
1175 CLEAR_CONTEXT* clear = (CLEAR_CONTEXT*)winpr_aligned_calloc(1,
sizeof(CLEAR_CONTEXT), 32);
1180 clear->Compressor = Compressor;
1181 clear->nsc = nsc_context_new();
1186 if (!updateContextFormat(clear, PIXEL_FORMAT_BGRX32))
1189 if (!clear_resize_buffer(clear, 512, 512))
1192 if (!clear->TempBuffer)
1195 if (!clear_context_reset(clear))
1200 WINPR_PRAGMA_DIAG_PUSH
1201 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1202 clear_context_free(clear);
1203 WINPR_PRAGMA_DIAG_POP
1207 void clear_context_free(CLEAR_CONTEXT* clear)
1212 nsc_context_free(clear->nsc);
1213 winpr_aligned_free(clear->TempBuffer);
1215 clear_reset_vbar_storage(clear, TRUE);
1216 clear_reset_glyph_cache(clear);
1218 winpr_aligned_free(clear);