Merge pull request #4960 from akallabeth/interleaved_fix

Fixed #4954: Check destination buffer bounds.
This commit is contained in:
Bernhard Miklautz 2018-11-09 12:25:55 +00:00 committed by GitHub
commit c5c1bac31a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 1460 additions and 1352 deletions

View File

@ -32,8 +32,8 @@
extern "C" {
#endif
FREERDP_API int freerdp_bitmap_compress(const char* in_data, int width, int height,
wStream* s, int bpp, int byte_limit, int start_line, wStream* temp_s, int e);
FREERDP_API SSIZE_T freerdp_bitmap_compress(const void* in_data, UINT32 width, UINT32 height,
wStream* s, UINT32 bpp, UINT32 byte_limit, UINT32 start_line, wStream* temp_s, UINT32 e);
#ifdef __cplusplus
}

File diff suppressed because it is too large Load Diff

View File

@ -24,142 +24,32 @@
/**
* Write a foreground/background image to a destination buffer.
*/
static INLINE BYTE* WRITEFGBGIMAGE(BYTE* pbDest, UINT32 rowDelta,
static INLINE BYTE* WRITEFGBGIMAGE(BYTE* pbDest, const BYTE* pbDestEnd, UINT32 rowDelta,
BYTE bitmask, PIXEL fgPel, INT32 cBits)
{
PIXEL xorPixel;
DESTREADPIXEL(xorPixel, pbDest - rowDelta);
BYTE mask = 0x01;
if (bitmask & g_MaskBit0)
{
DESTWRITEPIXEL(pbDest, xorPixel ^ fgPel);
}
else
{
DESTWRITEPIXEL(pbDest, xorPixel);
}
if (cBits > 8)
return NULL;
DESTNEXTPIXEL(pbDest);
cBits = cBits - 1;
if (!ENSURE_CAPACITY(pbDest, pbDestEnd, cBits))
return NULL;
if (cBits > 0)
UNROLL(cBits,
{
UINT32 data;
DESTREADPIXEL(xorPixel, pbDest - rowDelta);
if (bitmask & g_MaskBit1)
{
DESTWRITEPIXEL(pbDest, xorPixel ^ fgPel);
}
if (bitmask & mask)
data = xorPixel ^ fgPel;
else
{
DESTWRITEPIXEL(pbDest, xorPixel);
}
data = xorPixel;
DESTWRITEPIXEL(pbDest, data);
DESTNEXTPIXEL(pbDest);
cBits = cBits - 1;
if (cBits > 0)
{
DESTREADPIXEL(xorPixel, pbDest - rowDelta);
if (bitmask & g_MaskBit2)
{
DESTWRITEPIXEL(pbDest, xorPixel ^ fgPel);
}
else
{
DESTWRITEPIXEL(pbDest, xorPixel);
}
DESTNEXTPIXEL(pbDest);
cBits = cBits - 1;
if (cBits > 0)
{
DESTREADPIXEL(xorPixel, pbDest - rowDelta);
if (bitmask & g_MaskBit3)
{
DESTWRITEPIXEL(pbDest, xorPixel ^ fgPel);
}
else
{
DESTWRITEPIXEL(pbDest, xorPixel);
}
DESTNEXTPIXEL(pbDest);
cBits = cBits - 1;
if (cBits > 0)
{
DESTREADPIXEL(xorPixel, pbDest - rowDelta);
if (bitmask & g_MaskBit4)
{
DESTWRITEPIXEL(pbDest, xorPixel ^ fgPel);
}
else
{
DESTWRITEPIXEL(pbDest, xorPixel);
}
DESTNEXTPIXEL(pbDest);
cBits = cBits - 1;
if (cBits > 0)
{
DESTREADPIXEL(xorPixel, pbDest - rowDelta);
if (bitmask & g_MaskBit5)
{
DESTWRITEPIXEL(pbDest, xorPixel ^ fgPel);
}
else
{
DESTWRITEPIXEL(pbDest, xorPixel);
}
DESTNEXTPIXEL(pbDest);
cBits = cBits - 1;
if (cBits > 0)
{
DESTREADPIXEL(xorPixel, pbDest - rowDelta);
if (bitmask & g_MaskBit6)
{
DESTWRITEPIXEL(pbDest, xorPixel ^ fgPel);
}
else
{
DESTWRITEPIXEL(pbDest, xorPixel);
}
DESTNEXTPIXEL(pbDest);
cBits = cBits - 1;
if (cBits > 0)
{
DESTREADPIXEL(xorPixel, pbDest - rowDelta);
if (bitmask & g_MaskBit7)
{
DESTWRITEPIXEL(pbDest, xorPixel ^ fgPel);
}
else
{
DESTWRITEPIXEL(pbDest, xorPixel);
}
DESTNEXTPIXEL(pbDest);
}
}
}
}
}
}
}
mask = mask << 1;
});
return pbDest;
}
@ -167,125 +57,30 @@ static INLINE BYTE* WRITEFGBGIMAGE(BYTE* pbDest, UINT32 rowDelta,
* Write a foreground/background image to a destination buffer
* for the first line of compressed data.
*/
static INLINE BYTE* WRITEFIRSTLINEFGBGIMAGE(BYTE* pbDest, BYTE bitmask,
static INLINE BYTE* WRITEFIRSTLINEFGBGIMAGE(BYTE* pbDest, const BYTE* pbDestEnd, BYTE bitmask,
PIXEL fgPel, UINT32 cBits)
{
if (bitmask & g_MaskBit0)
{
DESTWRITEPIXEL(pbDest, fgPel);
}
else
{
DESTWRITEPIXEL(pbDest, BLACK_PIXEL);
}
BYTE mask = 0x01;
DESTNEXTPIXEL(pbDest);
cBits = cBits - 1;
if (cBits > 8)
return NULL;
if (cBits > 0)
if (!ENSURE_CAPACITY(pbDest, pbDestEnd, cBits))
return NULL;
UNROLL(cBits,
{
if (bitmask & g_MaskBit1)
{
DESTWRITEPIXEL(pbDest, fgPel);
}
UINT32 data;
if (bitmask & mask)
data = fgPel;
else
{
DESTWRITEPIXEL(pbDest, BLACK_PIXEL);
}
data = BLACK_PIXEL;
DESTWRITEPIXEL(pbDest, data);
DESTNEXTPIXEL(pbDest);
cBits = cBits - 1;
if (cBits > 0)
{
if (bitmask & g_MaskBit2)
{
DESTWRITEPIXEL(pbDest, fgPel);
}
else
{
DESTWRITEPIXEL(pbDest, BLACK_PIXEL);
}
DESTNEXTPIXEL(pbDest);
cBits = cBits - 1;
if (cBits > 0)
{
if (bitmask & g_MaskBit3)
{
DESTWRITEPIXEL(pbDest, fgPel);
}
else
{
DESTWRITEPIXEL(pbDest, BLACK_PIXEL);
}
DESTNEXTPIXEL(pbDest);
cBits = cBits - 1;
if (cBits > 0)
{
if (bitmask & g_MaskBit4)
{
DESTWRITEPIXEL(pbDest, fgPel);
}
else
{
DESTWRITEPIXEL(pbDest, BLACK_PIXEL);
}
DESTNEXTPIXEL(pbDest);
cBits = cBits - 1;
if (cBits > 0)
{
if (bitmask & g_MaskBit5)
{
DESTWRITEPIXEL(pbDest, fgPel);
}
else
{
DESTWRITEPIXEL(pbDest, BLACK_PIXEL);
}
DESTNEXTPIXEL(pbDest);
cBits = cBits - 1;
if (cBits > 0)
{
if (bitmask & g_MaskBit6)
{
DESTWRITEPIXEL(pbDest, fgPel);
}
else
{
DESTWRITEPIXEL(pbDest, BLACK_PIXEL);
}
DESTNEXTPIXEL(pbDest);
cBits = cBits - 1;
if (cBits > 0)
{
if (bitmask & g_MaskBit7)
{
DESTWRITEPIXEL(pbDest, fgPel);
}
else
{
DESTWRITEPIXEL(pbDest, BLACK_PIXEL);
}
DESTNEXTPIXEL(pbDest);
}
}
}
}
}
}
}
mask = mask << 1;
});
return pbDest;
}
@ -298,6 +93,7 @@ static INLINE BOOL RLEDECOMPRESS(const BYTE* pbSrcBuffer, UINT32 cbSrcBuffer,
{
const BYTE* pbSrc = pbSrcBuffer;
const BYTE* pbEnd = pbSrcBuffer + cbSrcBuffer;
const BYTE* pbDestEnd = pbDestBuffer + rowDelta * height;
BYTE* pbDest = pbDestBuffer;
PIXEL temp;
PIXEL fgPel = WHITE_PIXEL;
@ -310,6 +106,9 @@ static INLINE BOOL RLEDECOMPRESS(const BYTE* pbSrcBuffer, UINT32 cbSrcBuffer,
UINT32 advance;
RLEEXTRA
if ((rowDelta == 0) || (rowDelta < width))
return FALSE;
if (!pbSrcBuffer || !pbDestBuffer)
return FALSE;
@ -341,52 +140,46 @@ static INLINE BOOL RLEDECOMPRESS(const BYTE* pbSrcBuffer, UINT32 cbSrcBuffer,
{
if (fInsertFgPel)
{
if (!ENSURE_CAPACITY(pbDest, pbDestEnd, 1))
return FALSE;
DESTWRITEPIXEL(pbDest, fgPel);
DESTNEXTPIXEL(pbDest);
runLength = runLength - 1;
}
while (runLength >= UNROLL_COUNT)
{
UNROLL(
DESTWRITEPIXEL(pbDest, BLACK_PIXEL);
DESTNEXTPIXEL(pbDest););
runLength = runLength - UNROLL_COUNT;
}
if (!ENSURE_CAPACITY(pbDest, pbDestEnd, runLength))
return FALSE;
while (runLength > 0)
UNROLL(runLength,
{
DESTWRITEPIXEL(pbDest, BLACK_PIXEL);
DESTNEXTPIXEL(pbDest);
runLength = runLength - 1;
}
});
}
else
{
if (fInsertFgPel)
{
DESTREADPIXEL(temp, pbDest - rowDelta);
if (!ENSURE_CAPACITY(pbDest, pbDestEnd, 1))
return FALSE;
DESTWRITEPIXEL(pbDest, temp ^ fgPel);
DESTNEXTPIXEL(pbDest);
runLength = runLength - 1;
runLength--;
}
while (runLength >= UNROLL_COUNT)
{
UNROLL(
DESTREADPIXEL(temp, pbDest - rowDelta);
DESTWRITEPIXEL(pbDest, temp);
DESTNEXTPIXEL(pbDest););
runLength = runLength - UNROLL_COUNT;
}
if (!ENSURE_CAPACITY(pbDest, pbDestEnd, runLength))
return FALSE;
while (runLength > 0)
UNROLL(runLength,
{
DESTREADPIXEL(temp, pbDest - rowDelta);
DESTWRITEPIXEL(pbDest, temp);
DESTNEXTPIXEL(pbDest);
runLength = runLength - 1;
}
});
}
/* A follow-on background run order will need a foreground pel inserted. */
@ -414,41 +207,25 @@ static INLINE BOOL RLEDECOMPRESS(const BYTE* pbSrcBuffer, UINT32 cbSrcBuffer,
SRCNEXTPIXEL(pbSrc);
}
if (!ENSURE_CAPACITY(pbDest, pbDestEnd, runLength))
return FALSE;
if (fFirstLine)
{
while (runLength >= UNROLL_COUNT)
{
UNROLL(
DESTWRITEPIXEL(pbDest, fgPel);
DESTNEXTPIXEL(pbDest););
runLength = runLength - UNROLL_COUNT;
}
while (runLength > 0)
UNROLL(runLength,
{
DESTWRITEPIXEL(pbDest, fgPel);
DESTNEXTPIXEL(pbDest);
runLength = runLength - 1;
}
});
}
else
{
while (runLength >= UNROLL_COUNT)
{
UNROLL(
DESTREADPIXEL(temp, pbDest - rowDelta);
DESTWRITEPIXEL(pbDest, temp ^ fgPel);
DESTNEXTPIXEL(pbDest););
runLength = runLength - UNROLL_COUNT;
}
while (runLength > 0)
UNROLL(runLength,
{
DESTREADPIXEL(temp, pbDest - rowDelta);
DESTWRITEPIXEL(pbDest, temp ^ fgPel);
DESTNEXTPIXEL(pbDest);
runLength = runLength - 1;
}
});
}
break;
@ -463,25 +240,16 @@ static INLINE BOOL RLEDECOMPRESS(const BYTE* pbSrcBuffer, UINT32 cbSrcBuffer,
SRCREADPIXEL(pixelB, pbSrc);
SRCNEXTPIXEL(pbSrc);
while (runLength >= UNROLL_COUNT)
{
UNROLL(
DESTWRITEPIXEL(pbDest, pixelA);
DESTNEXTPIXEL(pbDest);
DESTWRITEPIXEL(pbDest, pixelB);
DESTNEXTPIXEL(pbDest););
runLength = runLength - UNROLL_COUNT;
}
if (!ENSURE_CAPACITY(pbDest, pbDestEnd, runLength * 2))
return FALSE;
while (runLength > 0)
UNROLL(runLength,
{
DESTWRITEPIXEL(pbDest, pixelA);
DESTNEXTPIXEL(pbDest);
DESTWRITEPIXEL(pbDest, pixelB);
DESTNEXTPIXEL(pbDest);
runLength = runLength - 1;
}
});
break;
/* Handle Color Run Orders. */
@ -492,21 +260,14 @@ static INLINE BOOL RLEDECOMPRESS(const BYTE* pbSrcBuffer, UINT32 cbSrcBuffer,
SRCREADPIXEL(pixelA, pbSrc);
SRCNEXTPIXEL(pbSrc);
while (runLength >= UNROLL_COUNT)
{
UNROLL(
DESTWRITEPIXEL(pbDest, pixelA);
DESTNEXTPIXEL(pbDest););
runLength = runLength - UNROLL_COUNT;
}
if (!ENSURE_CAPACITY(pbDest, pbDestEnd, runLength))
return FALSE;
while (runLength > 0)
UNROLL(runLength,
{
DESTWRITEPIXEL(pbDest, pixelA);
DESTNEXTPIXEL(pbDest);
runLength = runLength - 1;
}
});
break;
/* Handle Foreground/Background Image Orders. */
@ -529,7 +290,11 @@ static INLINE BOOL RLEDECOMPRESS(const BYTE* pbSrcBuffer, UINT32 cbSrcBuffer,
{
bitmask = *pbSrc;
pbSrc = pbSrc + 1;
pbDest = WRITEFIRSTLINEFGBGIMAGE(pbDest, bitmask, fgPel, 8);
pbDest = WRITEFIRSTLINEFGBGIMAGE(pbDest, pbDestEnd, bitmask, fgPel, 8);
if (!pbDest)
return FALSE;
runLength = runLength - 8;
}
}
@ -539,7 +304,11 @@ static INLINE BOOL RLEDECOMPRESS(const BYTE* pbSrcBuffer, UINT32 cbSrcBuffer,
{
bitmask = *pbSrc;
pbSrc = pbSrc + 1;
pbDest = WRITEFGBGIMAGE(pbDest, rowDelta, bitmask, fgPel, 8);
pbDest = WRITEFGBGIMAGE(pbDest, pbDestEnd, rowDelta, bitmask, fgPel, 8);
if (!pbDest)
return FALSE;
runLength = runLength - 8;
}
}
@ -551,12 +320,15 @@ static INLINE BOOL RLEDECOMPRESS(const BYTE* pbSrcBuffer, UINT32 cbSrcBuffer,
if (fFirstLine)
{
pbDest = WRITEFIRSTLINEFGBGIMAGE(pbDest, bitmask, fgPel, runLength);
pbDest = WRITEFIRSTLINEFGBGIMAGE(pbDest, pbDestEnd, bitmask, fgPel, runLength);
}
else
{
pbDest = WRITEFGBGIMAGE(pbDest, rowDelta, bitmask, fgPel, runLength);
pbDest = WRITEFGBGIMAGE(pbDest, pbDestEnd, rowDelta, bitmask, fgPel, runLength);
}
if (!pbDest)
return FALSE;
}
break;
@ -566,26 +338,13 @@ static INLINE BOOL RLEDECOMPRESS(const BYTE* pbSrcBuffer, UINT32 cbSrcBuffer,
case MEGA_MEGA_COLOR_IMAGE:
runLength = ExtractRunLength(code, pbSrc, &advance);
pbSrc = pbSrc + advance;
while (runLength >= UNROLL_COUNT)
{
UNROLL(
SRCREADPIXEL(temp, pbSrc);
SRCNEXTPIXEL(pbSrc);
DESTWRITEPIXEL(pbDest, temp);
DESTNEXTPIXEL(pbDest););
runLength = runLength - UNROLL_COUNT;
}
while (runLength > 0)
UNROLL(runLength,
{
SRCREADPIXEL(temp, pbSrc);
SRCNEXTPIXEL(pbSrc);
DESTWRITEPIXEL(pbDest, temp);
DESTNEXTPIXEL(pbDest);
runLength = runLength - 1;
}
});
break;
/* Handle Special Order 1. */
@ -594,13 +353,16 @@ static INLINE BOOL RLEDECOMPRESS(const BYTE* pbSrcBuffer, UINT32 cbSrcBuffer,
if (fFirstLine)
{
pbDest = WRITEFIRSTLINEFGBGIMAGE(pbDest, g_MaskSpecialFgBg1, fgPel, 8);
pbDest = WRITEFIRSTLINEFGBGIMAGE(pbDest, pbDestEnd, g_MaskSpecialFgBg1, fgPel, 8);
}
else
{
pbDest = WRITEFGBGIMAGE(pbDest, rowDelta, g_MaskSpecialFgBg1, fgPel, 8);
pbDest = WRITEFGBGIMAGE(pbDest, pbDestEnd, rowDelta, g_MaskSpecialFgBg1, fgPel, 8);
}
if (!pbDest)
return FALSE;
break;
/* Handle Special Order 2. */
@ -609,18 +371,25 @@ static INLINE BOOL RLEDECOMPRESS(const BYTE* pbSrcBuffer, UINT32 cbSrcBuffer,
if (fFirstLine)
{
pbDest = WRITEFIRSTLINEFGBGIMAGE(pbDest, g_MaskSpecialFgBg2, fgPel, 8);
pbDest = WRITEFIRSTLINEFGBGIMAGE(pbDest, pbDestEnd, g_MaskSpecialFgBg2, fgPel, 8);
}
else
{
pbDest = WRITEFGBGIMAGE(pbDest, rowDelta, g_MaskSpecialFgBg2, fgPel, 8);
pbDest = WRITEFGBGIMAGE(pbDest, pbDestEnd, rowDelta, g_MaskSpecialFgBg2, fgPel, 8);
}
if (!pbDest)
return FALSE;
break;
/* Handle White Order. */
case SPECIAL_WHITE:
pbSrc = pbSrc + 1;
if (!ENSURE_CAPACITY(pbDest, pbDestEnd, 1))
return FALSE;
DESTWRITEPIXEL(pbDest, WHITE_PIXEL);
DESTNEXTPIXEL(pbDest);
break;
@ -628,6 +397,10 @@ static INLINE BOOL RLEDECOMPRESS(const BYTE* pbSrcBuffer, UINT32 cbSrcBuffer,
/* Handle Black Order. */
case SPECIAL_BLACK:
pbSrc = pbSrc + 1;
if (!ENSURE_CAPACITY(pbDest, pbDestEnd, 1))
return FALSE;
DESTWRITEPIXEL(pbDest, BLACK_PIXEL);
DESTNEXTPIXEL(pbDest);
break;

View File

@ -30,6 +30,34 @@
#define TAG FREERDP_TAG("codec")
#define UNROLL_BODY(_exp, _count) do \
{ \
size_t x; \
for (x=0; x<(_count); x++) \
{ \
do _exp while(FALSE); \
} \
} \
while (FALSE)
#define UNROLL_MULTIPLE(_condition, _exp, _count) do \
{ \
while ((_condition) >= _count) \
{ \
UNROLL_BODY(_exp, _count); \
(_condition) -= _count; \
} \
} \
while (FALSE)
#define UNROLL(_condition, _exp) do \
{ \
UNROLL_MULTIPLE(_condition, _exp, 16); \
UNROLL_MULTIPLE(_condition, _exp, 4); \
UNROLL_MULTIPLE(_condition, _exp, 1); \
} \
while (FALSE)
/*
RLE Compressed Bitmap Stream (RLE_BITMAP_STREAM)
http://msdn.microsoft.com/en-us/library/cc240895%28v=prot.10%29.aspx
@ -59,19 +87,9 @@
#define SPECIAL_BLACK 0xFE
#define BLACK_PIXEL 0x000000
#define WHITE_PIXEL 0xFFFFFF
typedef UINT32 PIXEL;
static const BYTE g_MaskBit0 = 0x01; /* Least significant bit */
static const BYTE g_MaskBit1 = 0x02;
static const BYTE g_MaskBit2 = 0x04;
static const BYTE g_MaskBit3 = 0x08;
static const BYTE g_MaskBit4 = 0x10;
static const BYTE g_MaskBit5 = 0x20;
static const BYTE g_MaskBit6 = 0x40;
static const BYTE g_MaskBit7 = 0x80; /* Most significant bit */
static const BYTE g_MaskSpecialFgBg1 = 0x03;
static const BYTE g_MaskSpecialFgBg2 = 0x05;
@ -193,8 +211,29 @@ static INLINE UINT32 ExtractRunLength(UINT32 code, const BYTE* pbOrderHdr,
return runLength;
}
#define UNROLL_COUNT 4
#define UNROLL(_exp) do { _exp _exp _exp _exp } while (0)
static INLINE BOOL ensure_capacity(const BYTE* start, const BYTE* end, size_t size, size_t base)
{
const size_t available = (uintptr_t)end - (uintptr_t)start;
const BOOL rc = available >= size * base;
return rc;
}
static INLINE void write_pixel_8(BYTE* _buf, BYTE _pix)
{
*_buf = _pix;
}
static INLINE void write_pixel_24(BYTE* _buf, UINT32 _pix)
{
(_buf)[0] = (BYTE)(_pix);
(_buf)[1] = (BYTE)((_pix) >> 8);
(_buf)[2] = (BYTE)((_pix) >> 16);
}
static INLINE void write_pixel_16(BYTE* _buf, UINT16 _pix)
{
*(UINT16*)_buf = _pix;
}
#undef DESTWRITEPIXEL
#undef DESTREADPIXEL
@ -205,7 +244,9 @@ static INLINE UINT32 ExtractRunLength(UINT32 code, const BYTE* pbOrderHdr,
#undef WRITEFIRSTLINEFGBGIMAGE
#undef RLEDECOMPRESS
#undef RLEEXTRA
#define DESTWRITEPIXEL(_buf, _pix) (_buf)[0] = (BYTE)(_pix)
#undef WHITE_PIXEL
#define WHITE_PIXEL 0xFF
#define DESTWRITEPIXEL(_buf, _pix) write_pixel_8(_buf, _pix)
#define DESTREADPIXEL(_pix, _buf) _pix = (_buf)[0]
#define SRCREADPIXEL(_pix, _buf) _pix = (_buf)[0]
#define DESTNEXTPIXEL(_buf) _buf += 1
@ -214,6 +255,8 @@ static INLINE UINT32 ExtractRunLength(UINT32 code, const BYTE* pbOrderHdr,
#define WRITEFIRSTLINEFGBGIMAGE WriteFirstLineFgBgImage8to8
#define RLEDECOMPRESS RleDecompress8to8
#define RLEEXTRA
#undef ENSURE_CAPACITY
#define ENSURE_CAPACITY(_start, _end, _size) ensure_capacity(_start, _end, _size, 1)
#include "include/bitmap.c"
#undef DESTWRITEPIXEL
@ -225,7 +268,9 @@ static INLINE UINT32 ExtractRunLength(UINT32 code, const BYTE* pbOrderHdr,
#undef WRITEFIRSTLINEFGBGIMAGE
#undef RLEDECOMPRESS
#undef RLEEXTRA
#define DESTWRITEPIXEL(_buf, _pix) ((UINT16*)(_buf))[0] = (UINT16)(_pix)
#undef WHITE_PIXEL
#define WHITE_PIXEL 0xFFFF
#define DESTWRITEPIXEL(_buf, _pix) write_pixel_16(_buf, _pix)
#define DESTREADPIXEL(_pix, _buf) _pix = ((UINT16*)(_buf))[0]
#ifdef HAVE_ALIGNED_REQUIRED
#define SRCREADPIXEL(_pix, _buf) _pix = (_buf)[0] | ((_buf)[1] << 8)
@ -238,6 +283,8 @@ static INLINE UINT32 ExtractRunLength(UINT32 code, const BYTE* pbOrderHdr,
#define WRITEFIRSTLINEFGBGIMAGE WriteFirstLineFgBgImage16to16
#define RLEDECOMPRESS RleDecompress16to16
#define RLEEXTRA
#undef ENSURE_CAPACITY
#define ENSURE_CAPACITY(_start, _end, _size) ensure_capacity(_start, _end, _size, 2)
#include "include/bitmap.c"
#undef DESTWRITEPIXEL
@ -249,8 +296,9 @@ static INLINE UINT32 ExtractRunLength(UINT32 code, const BYTE* pbOrderHdr,
#undef WRITEFIRSTLINEFGBGIMAGE
#undef RLEDECOMPRESS
#undef RLEEXTRA
#define DESTWRITEPIXEL(_buf, _pix) do { (_buf)[0] = (BYTE)(_pix); \
(_buf)[1] = (BYTE)((_pix) >> 8); (_buf)[2] = (BYTE)((_pix) >> 16); } while (0)
#undef WHITE_PIXEL
#define WHITE_PIXEL 0xFFFFFF
#define DESTWRITEPIXEL(_buf, _pix) write_pixel_24(_buf, _pix)
#define DESTREADPIXEL(_pix, _buf) _pix = (_buf)[0] | ((_buf)[1] << 8) | \
((_buf)[2] << 16)
#define SRCREADPIXEL(_pix, _buf) _pix = (_buf)[0] | ((_buf)[1] << 8) | \
@ -261,6 +309,8 @@ static INLINE UINT32 ExtractRunLength(UINT32 code, const BYTE* pbOrderHdr,
#define WRITEFIRSTLINEFGBGIMAGE WriteFirstLineFgBgImage24to24
#define RLEDECOMPRESS RleDecompress24to24
#define RLEEXTRA
#undef ENSURE_CAPACITY
#define ENSURE_CAPACITY(_start, _end, _size) ensure_capacity(_start, _end, _size, 3)
#include "include/bitmap.c"
BOOL interleaved_decompress(BITMAP_INTERLEAVED_CONTEXT* interleaved,
@ -359,10 +409,16 @@ BOOL interleaved_compress(BITMAP_INTERLEAVED_CONTEXT* interleaved,
UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc,
const gdiPalette* palette, UINT32 bpp)
{
int status;
BOOL status;
wStream* s;
UINT32 DstFormat = 0;
int maxSize = 64 * 64 * 4;
const size_t maxSize = 64 * 64 * 4;
if (!interleaved || !pDstData || !pSrcData)
return FALSE;
if ((nWidth == 0) || (nHeight == 0))
return FALSE;
if (nWidth % 4)
{
@ -378,31 +434,43 @@ BOOL interleaved_compress(BITMAP_INTERLEAVED_CONTEXT* interleaved,
return FALSE;
}
if (bpp == 24)
DstFormat = PIXEL_FORMAT_BGRX32;
else if (bpp == 16)
DstFormat = PIXEL_FORMAT_RGB16;
else if (bpp == 15)
DstFormat = PIXEL_FORMAT_RGB15;
else if (bpp == 8)
DstFormat = PIXEL_FORMAT_RGB8;
switch (bpp)
{
case 24:
DstFormat = PIXEL_FORMAT_BGRX32;
break;
if (!DstFormat)
return FALSE;
case 16:
DstFormat = PIXEL_FORMAT_RGB16;
break;
case 15:
DstFormat = PIXEL_FORMAT_RGB15;
break;
default:
return FALSE;
}
if (!freerdp_image_copy(interleaved->TempBuffer, DstFormat, 0, 0, 0, nWidth,
nHeight,
pSrcData, SrcFormat, nSrcStep, nXSrc, nYSrc, palette, FREERDP_FLIP_NONE))
nHeight, pSrcData, SrcFormat, nSrcStep, nXSrc, nYSrc,
palette, FREERDP_FLIP_NONE))
return FALSE;
s = Stream_New(pDstData, maxSize);
s = Stream_New(pDstData, *pDstSize);
if (!s)
return FALSE;
status = freerdp_bitmap_compress((char*) interleaved->TempBuffer, nWidth,
nHeight,
s, bpp, maxSize, nHeight - 1, interleaved->bts, 0);
Stream_SetPosition(interleaved->bts, 0);
if (freerdp_bitmap_compress(interleaved->TempBuffer, nWidth, nHeight,
s, bpp, maxSize, nHeight - 1,
interleaved->bts, 0) < 0)
status = FALSE;
else
status = TRUE;
Stream_SealLength(s);
*pDstSize = (UINT32) Stream_Length(s);
Stream_Free(s, FALSE);

View File

@ -12,6 +12,7 @@ set(${MODULE_PREFIX}_TESTS
TestFreeRDPCodecZGfx.c
TestFreeRDPCodecPlanar.c
TestFreeRDPCodecClear.c
TestFreeRDPCodecInterleaved.c
TestFreeRDPCodecProgressive.c
TestFreeRDPCodecRemoteFX.c)

View File

@ -0,0 +1,179 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <math.h>
#include <winpr/crt.h>
#include <winpr/print.h>
#include <freerdp/freerdp.h>
#include <freerdp/codec/color.h>
#include <freerdp/codec/bitmap.h>
#include <freerdp/codec/interleaved.h>
#include <winpr/crypto.h>
#include <freerdp/utils/profiler.h>
static BOOL run_encode_decode_single(UINT16 bpp, BITMAP_INTERLEAVED_CONTEXT* encoder,
BITMAP_INTERLEAVED_CONTEXT* decoder
#if defined(WITH_PROFILER)
, PROFILER* profiler_comp, PROFILER* profiler_decomp
#endif
)
{
BOOL rc2 = FALSE;
BOOL rc;
UINT32 i, j;
const UINT32 w = 64;
const UINT32 h = 64;
const UINT32 x = 0;
const UINT32 y = 0;
const UINT32 format = PIXEL_FORMAT_RGBX32;
const UINT32 bstep = GetBytesPerPixel(format);
const size_t step = (w + 13) * 4;
const size_t SrcSize = step * h;
const float maxDiff = 4.0f * ((bpp < 24) ? 2.0f : 1.0f);
UINT32 DstSize = SrcSize;
BYTE* pSrcData = malloc(SrcSize);
BYTE* pDstData = malloc(SrcSize);
BYTE* tmp = malloc(SrcSize);
if (!pSrcData || !pDstData || !tmp)
goto fail;
winpr_RAND(pSrcData, SrcSize);
if (!bitmap_interleaved_context_reset(encoder) || !bitmap_interleaved_context_reset(decoder))
goto fail;
PROFILER_ENTER(profiler_comp);
rc = interleaved_compress(encoder, tmp, &DstSize, w, h, pSrcData,
format, step, x, y, NULL, bpp);
PROFILER_EXIT(profiler_comp);
if (!rc)
goto fail;
PROFILER_ENTER(profiler_decomp);
rc = interleaved_decompress(decoder, tmp, DstSize, w, h, bpp, pDstData,
format, step, x, y, w, h, NULL);
PROFILER_EXIT(profiler_decomp);
if (!rc)
goto fail;
for (i = 0; i < h; i++)
{
const BYTE* srcLine = &pSrcData[i * step];
const BYTE* dstLine = &pDstData[i * step];
for (j = 0; j < w; j++)
{
BYTE r, g, b, dr, dg, db;
const UINT32 srcColor = ReadColor(&srcLine[j * bstep], format);
const UINT32 dstColor = ReadColor(&dstLine[j * bstep], format);
SplitColor(srcColor, format, &r, &g, &b, NULL, NULL);
SplitColor(dstColor, format, &dr, &dg, &db, NULL, NULL);
if (fabsf((float)r - dr) > maxDiff)
goto fail;
if (fabsf((float)g - dg) > maxDiff)
goto fail;
if (fabsf((float)b - db) > maxDiff)
goto fail;
}
}
rc2 = TRUE;
fail:
free(pSrcData);
free(pDstData);
free(tmp);
return rc2;
}
static const char* get_profiler_name(BOOL encode, UINT16 bpp)
{
switch (bpp)
{
case 24:
if (encode)
return "interleaved_compress 24bpp";
else
return "interleaved_decompress 24bpp";
case 16:
if (encode)
return "interleaved_compress 16bpp";
else
return "interleaved_decompress 16bpp";
case 15:
if (encode)
return "interleaved_compress 15bpp";
else
return "interleaved_decompress 15bpp";
default:
return "configuration error!";
}
}
static BOOL run_encode_decode(UINT16 bpp, BITMAP_INTERLEAVED_CONTEXT* encoder,
BITMAP_INTERLEAVED_CONTEXT* decoder)
{
BOOL rc = FALSE;
UINT32 x;
PROFILER_DEFINE(profiler_comp);
PROFILER_DEFINE(profiler_decomp);
PROFILER_CREATE(profiler_comp, get_profiler_name(TRUE, bpp))
PROFILER_CREATE(profiler_decomp, get_profiler_name(FALSE, bpp))
for (x = 0; x < 500; x++)
{
if (!run_encode_decode_single(bpp, encoder, decoder
#if defined(WITH_PROFILER)
, profiler_comp, profiler_decomp
#endif
))
goto fail;
}
rc = TRUE;
fail:
PROFILER_PRINT_HEADER
PROFILER_PRINT(profiler_comp);
PROFILER_PRINT(profiler_decomp);
PROFILER_PRINT_FOOTER
PROFILER_FREE(profiler_comp);
PROFILER_FREE(profiler_decomp);
return rc;
}
int TestFreeRDPCodecInterleaved(int argc, char* argv[])
{
BITMAP_INTERLEAVED_CONTEXT* encoder, * decoder;
int rc = -1;
encoder = bitmap_interleaved_context_new(TRUE);
decoder = bitmap_interleaved_context_new(FALSE);
if (!encoder || !decoder)
goto fail;
if (!run_encode_decode(24, encoder, decoder))
goto fail;
if (!run_encode_decode(16, encoder, decoder))
goto fail;
if (!run_encode_decode(15, encoder, decoder))
goto fail;
rc = 0;
fail:
bitmap_interleaved_context_free(encoder);
bitmap_interleaved_context_free(decoder);
return rc;
}