2011-07-09 23:48:18 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2014-09-10 08:42:41 +04:00
|
|
|
* Interleaved RLE Bitmap Codec
|
2011-07-09 23:48:18 +04:00
|
|
|
*
|
2014-09-10 08:42:41 +04:00
|
|
|
* Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2015-05-29 14:46:50 +03:00
|
|
|
* Copyright 2015 Thincast Technologies GmbH
|
|
|
|
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
|
2016-04-05 18:07:45 +03:00
|
|
|
* Copyright 2016 Armin Novak <armin.novak@thincast.com>
|
|
|
|
* Copyright 2016 Thincast Technologies GmbH
|
2011-07-09 23:48:18 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 15:28:36 +03:00
|
|
|
#include <winpr/assert.h>
|
2022-02-16 13:20:38 +03:00
|
|
|
#include <freerdp/config.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2014-09-10 08:42:41 +04:00
|
|
|
#include <freerdp/codec/interleaved.h>
|
2014-11-16 14:21:38 +03:00
|
|
|
#include <freerdp/log.h>
|
|
|
|
|
|
|
|
#define TAG FREERDP_TAG("codec")
|
2011-07-25 22:50:26 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
#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 \
|
|
|
|
{ \
|
2018-11-09 13:32:28 +03:00
|
|
|
UNROLL_MULTIPLE(_condition, _exp, 16); \
|
2019-11-06 17:24:51 +03:00
|
|
|
UNROLL_MULTIPLE(_condition, _exp, 4); \
|
|
|
|
UNROLL_MULTIPLE(_condition, _exp, 1); \
|
|
|
|
} while (FALSE)
|
2018-11-09 13:32:28 +03:00
|
|
|
|
2011-07-09 23:48:18 +04:00
|
|
|
/*
|
|
|
|
RLE Compressed Bitmap Stream (RLE_BITMAP_STREAM)
|
|
|
|
http://msdn.microsoft.com/en-us/library/cc240895%28v=prot.10%29.aspx
|
|
|
|
pseudo-code
|
|
|
|
http://msdn.microsoft.com/en-us/library/dd240593%28v=prot.10%29.aspx
|
|
|
|
*/
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
#define REGULAR_BG_RUN 0x00
|
|
|
|
#define MEGA_MEGA_BG_RUN 0xF0
|
|
|
|
#define REGULAR_FG_RUN 0x01
|
|
|
|
#define MEGA_MEGA_FG_RUN 0xF1
|
|
|
|
#define LITE_SET_FG_FG_RUN 0x0C
|
|
|
|
#define MEGA_MEGA_SET_FG_RUN 0xF6
|
|
|
|
#define LITE_DITHERED_RUN 0x0E
|
|
|
|
#define MEGA_MEGA_DITHERED_RUN 0xF8
|
|
|
|
#define REGULAR_COLOR_RUN 0x03
|
|
|
|
#define MEGA_MEGA_COLOR_RUN 0xF3
|
|
|
|
#define REGULAR_FGBG_IMAGE 0x02
|
|
|
|
#define MEGA_MEGA_FGBG_IMAGE 0xF2
|
|
|
|
#define LITE_SET_FG_FGBG_IMAGE 0x0D
|
|
|
|
#define MEGA_MEGA_SET_FGBG_IMAGE 0xF7
|
|
|
|
#define REGULAR_COLOR_IMAGE 0x04
|
|
|
|
#define MEGA_MEGA_COLOR_IMAGE 0xF4
|
|
|
|
#define SPECIAL_FGBG_1 0xF9
|
|
|
|
#define SPECIAL_FGBG_2 0xFA
|
|
|
|
#define SPECIAL_WHITE 0xFD
|
|
|
|
#define SPECIAL_BLACK 0xFE
|
2011-09-12 12:40:57 +04:00
|
|
|
|
|
|
|
#define BLACK_PIXEL 0x000000
|
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
typedef UINT32 PIXEL;
|
2011-09-12 12:40:57 +04:00
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
static const BYTE g_MaskSpecialFgBg1 = 0x03;
|
|
|
|
static const BYTE g_MaskSpecialFgBg2 = 0x05;
|
2011-09-12 12:40:57 +04:00
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
static const BYTE g_MaskRegularRunLength = 0x1F;
|
|
|
|
static const BYTE g_MaskLiteRunLength = 0x0F;
|
2011-09-12 12:40:57 +04:00
|
|
|
|
2022-12-12 17:44:09 +03:00
|
|
|
static const char* rle_code_str(UINT32 code)
|
|
|
|
{
|
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
case REGULAR_BG_RUN:
|
|
|
|
return "REGULAR_BG_RUN";
|
|
|
|
case MEGA_MEGA_BG_RUN:
|
|
|
|
return "MEGA_MEGA_BG_RUN";
|
|
|
|
case REGULAR_FG_RUN:
|
|
|
|
return "REGULAR_FG_RUN";
|
|
|
|
case MEGA_MEGA_FG_RUN:
|
|
|
|
return "MEGA_MEGA_FG_RUN";
|
|
|
|
case LITE_SET_FG_FG_RUN:
|
|
|
|
return "LITE_SET_FG_FG_RUN";
|
|
|
|
case MEGA_MEGA_SET_FG_RUN:
|
|
|
|
return "MEGA_MEGA_SET_FG_RUN";
|
|
|
|
case LITE_DITHERED_RUN:
|
|
|
|
return "LITE_DITHERED_RUN";
|
|
|
|
case MEGA_MEGA_DITHERED_RUN:
|
|
|
|
return "MEGA_MEGA_DITHERED_RUN";
|
|
|
|
case REGULAR_COLOR_RUN:
|
|
|
|
return "REGULAR_COLOR_RUN";
|
|
|
|
case MEGA_MEGA_COLOR_RUN:
|
|
|
|
return "MEGA_MEGA_COLOR_RUN";
|
|
|
|
case REGULAR_FGBG_IMAGE:
|
|
|
|
return "REGULAR_FGBG_IMAGE";
|
|
|
|
case MEGA_MEGA_FGBG_IMAGE:
|
|
|
|
return "MEGA_MEGA_FGBG_IMAGE";
|
|
|
|
case LITE_SET_FG_FGBG_IMAGE:
|
|
|
|
return "LITE_SET_FG_FGBG_IMAGE";
|
|
|
|
case MEGA_MEGA_SET_FGBG_IMAGE:
|
|
|
|
return "MEGA_MEGA_SET_FGBG_IMAGE";
|
|
|
|
case REGULAR_COLOR_IMAGE:
|
|
|
|
return "REGULAR_COLOR_IMAGE";
|
|
|
|
case MEGA_MEGA_COLOR_IMAGE:
|
|
|
|
return "MEGA_MEGA_COLOR_IMAGE";
|
|
|
|
case SPECIAL_FGBG_1:
|
|
|
|
return "SPECIAL_FGBG_1";
|
|
|
|
case SPECIAL_FGBG_2:
|
|
|
|
return "SPECIAL_FGBG_2";
|
|
|
|
case SPECIAL_WHITE:
|
|
|
|
return "SPECIAL_WHITE";
|
|
|
|
case SPECIAL_BLACK:
|
|
|
|
return "SPECIAL_BLACK";
|
|
|
|
default:
|
|
|
|
return "UNKNOWN";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* rle_code_str_buffer(UINT32 code, char* buffer, size_t size)
|
|
|
|
{
|
|
|
|
const char* str = rle_code_str(code);
|
|
|
|
_snprintf(buffer, size, "%s [0x%08" PRIx32 "]", str, code);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2022-12-12 15:28:36 +03:00
|
|
|
#define buffer_within_range(pbSrc, pbEnd) \
|
|
|
|
buffer_within_range_((pbSrc), (pbEnd), __FUNCTION__, __FILE__, __LINE__)
|
|
|
|
static INLINE BOOL buffer_within_range_(const void* pbSrc, const void* pbEnd, const char* fkt,
|
|
|
|
const char* file, size_t line)
|
|
|
|
{
|
|
|
|
WINPR_UNUSED(file);
|
|
|
|
if (pbSrc >= pbEnd)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "[%s:%" PRIuz "] pbSrc=%p >= pbEnd=%p", fkt, line, pbSrc, pbEnd);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-09-12 12:40:57 +04:00
|
|
|
/**
|
|
|
|
* Reads the supplied order header and extracts the compression
|
|
|
|
* order code ID.
|
|
|
|
*/
|
2014-07-03 00:30:04 +04:00
|
|
|
static INLINE UINT32 ExtractCodeId(BYTE bOrderHdr)
|
2011-09-12 12:40:57 +04:00
|
|
|
{
|
2016-07-18 12:11:59 +03:00
|
|
|
if ((bOrderHdr & 0xC0U) != 0xC0U)
|
|
|
|
{
|
2014-07-03 00:30:04 +04:00
|
|
|
/* REGULAR orders
|
|
|
|
* (000x xxxx, 001x xxxx, 010x xxxx, 011x xxxx, 100x xxxx)
|
|
|
|
*/
|
|
|
|
return bOrderHdr >> 5;
|
2011-09-12 12:40:57 +04:00
|
|
|
}
|
2016-07-18 12:11:59 +03:00
|
|
|
else if ((bOrderHdr & 0xF0U) == 0xF0U)
|
|
|
|
{
|
2014-07-03 00:30:04 +04:00
|
|
|
/* MEGA and SPECIAL orders (0xF*) */
|
|
|
|
return bOrderHdr;
|
|
|
|
}
|
2016-07-18 12:11:59 +03:00
|
|
|
else
|
|
|
|
{
|
2014-07-03 00:30:04 +04:00
|
|
|
/* LITE orders
|
|
|
|
* 1100 xxxx, 1101 xxxx, 1110 xxxx)
|
|
|
|
*/
|
|
|
|
return bOrderHdr >> 4;
|
2011-09-12 12:40:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract the run length of a compression order.
|
|
|
|
*/
|
2022-12-12 18:01:59 +03:00
|
|
|
static UINT ExtractRunLengthRegularFgBg(const BYTE* pbOrderHdr, const BYTE* pbEnd, UINT32* advance)
|
|
|
|
{
|
|
|
|
UINT runLength;
|
|
|
|
|
|
|
|
WINPR_ASSERT(pbOrderHdr);
|
|
|
|
WINPR_ASSERT(pbEnd);
|
|
|
|
WINPR_ASSERT(advance);
|
|
|
|
|
|
|
|
runLength = (*pbOrderHdr) & g_MaskRegularRunLength;
|
|
|
|
if (runLength == 0)
|
|
|
|
{
|
|
|
|
if (!buffer_within_range(pbOrderHdr + 1, pbEnd))
|
|
|
|
return 0;
|
|
|
|
runLength = *(pbOrderHdr + 1) + 1;
|
|
|
|
(*advance)++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
runLength = runLength * 8;
|
|
|
|
|
|
|
|
return runLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT ExtractRunLengthLiteFgBg(const BYTE* pbOrderHdr, const BYTE* pbEnd, UINT32* advance)
|
|
|
|
{
|
|
|
|
UINT runLength;
|
|
|
|
|
|
|
|
WINPR_ASSERT(pbOrderHdr);
|
|
|
|
WINPR_ASSERT(pbEnd);
|
|
|
|
WINPR_ASSERT(advance);
|
|
|
|
|
|
|
|
runLength = *pbOrderHdr & g_MaskLiteRunLength;
|
|
|
|
if (runLength == 0)
|
|
|
|
{
|
|
|
|
if (!buffer_within_range(pbOrderHdr + 1, pbEnd))
|
|
|
|
return 0;
|
|
|
|
runLength = *(pbOrderHdr + 1) + 1;
|
|
|
|
(*advance)++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
runLength = runLength * 8;
|
|
|
|
|
|
|
|
return runLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT ExtractRunLengthRegular(const BYTE* pbOrderHdr, const BYTE* pbEnd, UINT32* advance)
|
|
|
|
{
|
|
|
|
UINT runLength;
|
|
|
|
|
|
|
|
WINPR_ASSERT(pbOrderHdr);
|
|
|
|
WINPR_ASSERT(pbEnd);
|
|
|
|
WINPR_ASSERT(advance);
|
|
|
|
|
|
|
|
runLength = *pbOrderHdr & g_MaskRegularRunLength;
|
|
|
|
if (runLength == 0)
|
|
|
|
{
|
|
|
|
if (!buffer_within_range(pbOrderHdr + 1, pbEnd))
|
|
|
|
return 0;
|
|
|
|
runLength = *(pbOrderHdr + 1) + 32;
|
|
|
|
(*advance)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return runLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT ExtractRunLengthMegaMega(const BYTE* pbOrderHdr, const BYTE* pbEnd, UINT32* advance)
|
|
|
|
{
|
|
|
|
UINT runLength;
|
|
|
|
|
|
|
|
WINPR_ASSERT(pbOrderHdr);
|
|
|
|
WINPR_ASSERT(pbEnd);
|
|
|
|
WINPR_ASSERT(advance);
|
|
|
|
|
|
|
|
if (!buffer_within_range(pbOrderHdr + 2, pbEnd))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
runLength = ((UINT16)pbOrderHdr[1]) | (((UINT16)pbOrderHdr[2]) << 8);
|
|
|
|
(*advance) += 2;
|
|
|
|
|
|
|
|
return runLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT ExtractRunLengthLite(const BYTE* pbOrderHdr, const BYTE* pbEnd, UINT32* advance)
|
|
|
|
{
|
|
|
|
UINT runLength;
|
|
|
|
|
|
|
|
WINPR_ASSERT(pbOrderHdr);
|
|
|
|
WINPR_ASSERT(pbEnd);
|
|
|
|
WINPR_ASSERT(advance);
|
|
|
|
|
|
|
|
runLength = *pbOrderHdr & g_MaskLiteRunLength;
|
|
|
|
if (runLength == 0)
|
|
|
|
{
|
|
|
|
if (!buffer_within_range(pbOrderHdr + 1, pbEnd))
|
|
|
|
return 0;
|
|
|
|
runLength = *(pbOrderHdr + 1) + 16;
|
|
|
|
(*advance)++;
|
|
|
|
}
|
|
|
|
return runLength;
|
|
|
|
}
|
|
|
|
|
2021-07-02 08:42:43 +03:00
|
|
|
static INLINE UINT32 ExtractRunLength(UINT32 code, const BYTE* pbOrderHdr, const BYTE* pbEnd,
|
|
|
|
UINT32* advance)
|
2011-09-12 12:40:57 +04:00
|
|
|
{
|
2021-09-14 08:38:25 +03:00
|
|
|
UINT32 runLength = 0;
|
|
|
|
UINT32 ladvance = 1;
|
2016-07-18 12:11:59 +03:00
|
|
|
|
2022-12-12 15:28:36 +03:00
|
|
|
WINPR_ASSERT(pbOrderHdr);
|
|
|
|
WINPR_ASSERT(pbEnd);
|
|
|
|
WINPR_ASSERT(advance);
|
|
|
|
|
|
|
|
if (!buffer_within_range(pbOrderHdr, pbEnd))
|
2021-07-02 08:42:43 +03:00
|
|
|
return 0;
|
|
|
|
|
2011-09-12 12:40:57 +04:00
|
|
|
switch (code)
|
|
|
|
{
|
2016-07-18 12:11:59 +03:00
|
|
|
case REGULAR_FGBG_IMAGE:
|
2022-12-12 18:01:59 +03:00
|
|
|
runLength = ExtractRunLengthRegularFgBg(pbOrderHdr, pbEnd, &ladvance);
|
2016-07-18 12:11:59 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LITE_SET_FG_FGBG_IMAGE:
|
2022-12-12 18:01:59 +03:00
|
|
|
runLength = ExtractRunLengthLiteFgBg(pbOrderHdr, pbEnd, &ladvance);
|
2016-07-18 12:11:59 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case REGULAR_BG_RUN:
|
|
|
|
case REGULAR_FG_RUN:
|
|
|
|
case REGULAR_COLOR_RUN:
|
|
|
|
case REGULAR_COLOR_IMAGE:
|
2022-12-12 18:01:59 +03:00
|
|
|
runLength = ExtractRunLengthRegular(pbOrderHdr, pbEnd, &ladvance);
|
2016-07-18 12:11:59 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LITE_SET_FG_FG_RUN:
|
|
|
|
case LITE_DITHERED_RUN:
|
2022-12-12 18:01:59 +03:00
|
|
|
runLength = ExtractRunLengthLite(pbOrderHdr, pbEnd, &ladvance);
|
2016-07-18 12:11:59 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MEGA_MEGA_BG_RUN:
|
|
|
|
case MEGA_MEGA_FG_RUN:
|
|
|
|
case MEGA_MEGA_SET_FG_RUN:
|
|
|
|
case MEGA_MEGA_DITHERED_RUN:
|
|
|
|
case MEGA_MEGA_COLOR_RUN:
|
|
|
|
case MEGA_MEGA_FGBG_IMAGE:
|
|
|
|
case MEGA_MEGA_SET_FGBG_IMAGE:
|
|
|
|
case MEGA_MEGA_COLOR_IMAGE:
|
2022-12-12 18:01:59 +03:00
|
|
|
runLength = ExtractRunLengthMegaMega(pbOrderHdr, pbEnd, &ladvance);
|
|
|
|
break;
|
2021-07-02 08:42:43 +03:00
|
|
|
|
2022-12-12 18:01:59 +03:00
|
|
|
default:
|
|
|
|
runLength = 0;
|
|
|
|
ladvance = 0;
|
2016-07-18 12:11:59 +03:00
|
|
|
break;
|
2011-09-12 12:40:57 +04:00
|
|
|
}
|
2016-07-18 12:11:59 +03:00
|
|
|
|
2011-09-12 12:40:57 +04:00
|
|
|
*advance = ladvance;
|
|
|
|
return runLength;
|
|
|
|
}
|
|
|
|
|
2022-12-12 15:28:36 +03:00
|
|
|
#define ensure_capacity(start, end, size, base) \
|
|
|
|
ensure_capacity_((start), (end), (size), (base), __FUNCTION__, __FILE__, __LINE__)
|
|
|
|
static INLINE BOOL ensure_capacity_(const BYTE* start, const BYTE* end, size_t size, size_t base,
|
|
|
|
const char* fkt, const char* file, size_t line)
|
2018-10-23 11:33:45 +03:00
|
|
|
{
|
2018-11-07 14:00:49 +03:00
|
|
|
const size_t available = (uintptr_t)end - (uintptr_t)start;
|
|
|
|
const BOOL rc = available >= size * base;
|
2022-12-12 15:28:36 +03:00
|
|
|
const BOOL res = rc && (start <= end);
|
|
|
|
|
|
|
|
if (!res)
|
|
|
|
WLog_ERR(TAG,
|
|
|
|
"[%s:%" PRIuz "] failed: start=%p <= end=%p, available=%" PRIuz " >= size=%" PRIuz
|
|
|
|
" * base=%" PRIuz,
|
|
|
|
fkt, line, start, end, available, size, base);
|
|
|
|
return res;
|
2018-11-07 14:00:49 +03:00
|
|
|
}
|
2018-10-23 11:33:45 +03:00
|
|
|
|
2018-11-07 14:00:49 +03:00
|
|
|
static INLINE void write_pixel_8(BYTE* _buf, BYTE _pix)
|
|
|
|
{
|
2022-12-12 15:28:36 +03:00
|
|
|
WINPR_ASSERT(_buf);
|
2018-10-23 11:33:45 +03:00
|
|
|
*_buf = _pix;
|
|
|
|
}
|
|
|
|
|
2018-11-07 14:00:49 +03:00
|
|
|
static INLINE void write_pixel_24(BYTE* _buf, UINT32 _pix)
|
2018-10-23 11:33:45 +03:00
|
|
|
{
|
2022-12-12 15:28:36 +03:00
|
|
|
WINPR_ASSERT(_buf);
|
2018-10-23 11:33:45 +03:00
|
|
|
(_buf)[0] = (BYTE)(_pix);
|
|
|
|
(_buf)[1] = (BYTE)((_pix) >> 8);
|
|
|
|
(_buf)[2] = (BYTE)((_pix) >> 16);
|
|
|
|
}
|
|
|
|
|
2018-11-07 14:00:49 +03:00
|
|
|
static INLINE void write_pixel_16(BYTE* _buf, UINT16 _pix)
|
2018-10-23 11:33:45 +03:00
|
|
|
{
|
2022-12-12 15:28:36 +03:00
|
|
|
WINPR_ASSERT(_buf);
|
2020-06-08 08:09:07 +03:00
|
|
|
_buf[0] = _pix & 0xFF;
|
|
|
|
_buf[1] = (_pix >> 8) & 0xFF;
|
2018-10-23 11:33:45 +03:00
|
|
|
}
|
|
|
|
|
2011-07-09 23:48:18 +04:00
|
|
|
#undef DESTWRITEPIXEL
|
|
|
|
#undef DESTREADPIXEL
|
|
|
|
#undef SRCREADPIXEL
|
|
|
|
#undef DESTNEXTPIXEL
|
|
|
|
#undef SRCNEXTPIXEL
|
|
|
|
#undef WRITEFGBGIMAGE
|
|
|
|
#undef WRITEFIRSTLINEFGBGIMAGE
|
|
|
|
#undef RLEDECOMPRESS
|
|
|
|
#undef RLEEXTRA
|
2018-10-23 11:33:45 +03:00
|
|
|
#undef WHITE_PIXEL
|
|
|
|
#define WHITE_PIXEL 0xFF
|
2018-11-07 14:00:49 +03:00
|
|
|
#define DESTWRITEPIXEL(_buf, _pix) write_pixel_8(_buf, _pix)
|
2011-07-09 23:48:18 +04:00
|
|
|
#define DESTREADPIXEL(_pix, _buf) _pix = (_buf)[0]
|
|
|
|
#define SRCREADPIXEL(_pix, _buf) _pix = (_buf)[0]
|
|
|
|
#define DESTNEXTPIXEL(_buf) _buf += 1
|
|
|
|
#define SRCNEXTPIXEL(_buf) _buf += 1
|
|
|
|
#define WRITEFGBGIMAGE WriteFgBgImage8to8
|
|
|
|
#define WRITEFIRSTLINEFGBGIMAGE WriteFirstLineFgBgImage8to8
|
|
|
|
#define RLEDECOMPRESS RleDecompress8to8
|
|
|
|
#define RLEEXTRA
|
2018-11-07 14:00:49 +03:00
|
|
|
#undef ENSURE_CAPACITY
|
|
|
|
#define ENSURE_CAPACITY(_start, _end, _size) ensure_capacity(_start, _end, _size, 1)
|
2011-10-03 04:28:20 +04:00
|
|
|
#include "include/bitmap.c"
|
2011-07-09 23:48:18 +04:00
|
|
|
|
|
|
|
#undef DESTWRITEPIXEL
|
|
|
|
#undef DESTREADPIXEL
|
|
|
|
#undef SRCREADPIXEL
|
|
|
|
#undef DESTNEXTPIXEL
|
|
|
|
#undef SRCNEXTPIXEL
|
|
|
|
#undef WRITEFGBGIMAGE
|
|
|
|
#undef WRITEFIRSTLINEFGBGIMAGE
|
|
|
|
#undef RLEDECOMPRESS
|
|
|
|
#undef RLEEXTRA
|
2018-10-23 11:33:45 +03:00
|
|
|
#undef WHITE_PIXEL
|
|
|
|
#define WHITE_PIXEL 0xFFFF
|
2018-11-07 14:00:49 +03:00
|
|
|
#define DESTWRITEPIXEL(_buf, _pix) write_pixel_16(_buf, _pix)
|
2012-10-09 11:01:37 +04:00
|
|
|
#define DESTREADPIXEL(_pix, _buf) _pix = ((UINT16*)(_buf))[0]
|
2015-02-05 19:46:56 +03:00
|
|
|
#define SRCREADPIXEL(_pix, _buf) _pix = (_buf)[0] | ((_buf)[1] << 8)
|
2011-07-09 23:48:18 +04:00
|
|
|
#define DESTNEXTPIXEL(_buf) _buf += 2
|
|
|
|
#define SRCNEXTPIXEL(_buf) _buf += 2
|
|
|
|
#define WRITEFGBGIMAGE WriteFgBgImage16to16
|
|
|
|
#define WRITEFIRSTLINEFGBGIMAGE WriteFirstLineFgBgImage16to16
|
|
|
|
#define RLEDECOMPRESS RleDecompress16to16
|
|
|
|
#define RLEEXTRA
|
2018-11-07 14:00:49 +03:00
|
|
|
#undef ENSURE_CAPACITY
|
|
|
|
#define ENSURE_CAPACITY(_start, _end, _size) ensure_capacity(_start, _end, _size, 2)
|
2011-10-03 04:28:20 +04:00
|
|
|
#include "include/bitmap.c"
|
2011-07-09 23:48:18 +04:00
|
|
|
|
|
|
|
#undef DESTWRITEPIXEL
|
|
|
|
#undef DESTREADPIXEL
|
|
|
|
#undef SRCREADPIXEL
|
|
|
|
#undef DESTNEXTPIXEL
|
|
|
|
#undef SRCNEXTPIXEL
|
|
|
|
#undef WRITEFGBGIMAGE
|
|
|
|
#undef WRITEFIRSTLINEFGBGIMAGE
|
|
|
|
#undef RLEDECOMPRESS
|
|
|
|
#undef RLEEXTRA
|
2018-10-23 11:33:45 +03:00
|
|
|
#undef WHITE_PIXEL
|
|
|
|
#define WHITE_PIXEL 0xFFFFFF
|
2019-11-06 17:24:51 +03:00
|
|
|
#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) | ((_buf)[2] << 16)
|
2011-07-09 23:48:18 +04:00
|
|
|
#define DESTNEXTPIXEL(_buf) _buf += 3
|
|
|
|
#define SRCNEXTPIXEL(_buf) _buf += 3
|
|
|
|
#define WRITEFGBGIMAGE WriteFgBgImage24to24
|
|
|
|
#define WRITEFIRSTLINEFGBGIMAGE WriteFirstLineFgBgImage24to24
|
|
|
|
#define RLEDECOMPRESS RleDecompress24to24
|
|
|
|
#define RLEEXTRA
|
2018-11-07 14:00:49 +03:00
|
|
|
#undef ENSURE_CAPACITY
|
|
|
|
#define ENSURE_CAPACITY(_start, _end, _size) ensure_capacity(_start, _end, _size, 3)
|
2011-10-03 04:28:20 +04:00
|
|
|
#include "include/bitmap.c"
|
2011-07-09 23:48:18 +04:00
|
|
|
|
2022-02-14 16:59:22 +03:00
|
|
|
struct S_BITMAP_INTERLEAVED_CONTEXT
|
|
|
|
{
|
|
|
|
BOOL Compressor;
|
|
|
|
|
|
|
|
UINT32 TempSize;
|
|
|
|
BYTE* TempBuffer;
|
|
|
|
|
|
|
|
wStream* bts;
|
|
|
|
};
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
BOOL interleaved_decompress(BITMAP_INTERLEAVED_CONTEXT* interleaved, const BYTE* pSrcData,
|
|
|
|
UINT32 SrcSize, UINT32 nSrcWidth, UINT32 nSrcHeight, UINT32 bpp,
|
|
|
|
BYTE* pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst,
|
|
|
|
UINT32 nYDst, UINT32 nDstWidth, UINT32 nDstHeight,
|
2016-10-14 11:01:02 +03:00
|
|
|
const gdiPalette* palette)
|
2011-07-10 06:55:11 +04:00
|
|
|
{
|
2016-04-05 18:07:45 +03:00
|
|
|
UINT32 scanline;
|
2014-09-12 06:29:09 +04:00
|
|
|
UINT32 SrcFormat;
|
2014-09-10 08:42:41 +04:00
|
|
|
UINT32 BufferSize;
|
|
|
|
|
2018-10-16 18:23:24 +03:00
|
|
|
if (!interleaved || !pSrcData || !pDstData)
|
2022-12-12 15:28:36 +03:00
|
|
|
{
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_ERR(TAG, "invalid arguments: interleaved=%p, pSrcData=%p, pDstData=%p", interleaved,
|
|
|
|
pSrcData, pDstData);
|
2016-07-18 12:11:59 +03:00
|
|
|
return FALSE;
|
2022-12-12 15:28:36 +03:00
|
|
|
}
|
2014-09-10 08:42:41 +04:00
|
|
|
|
2016-07-18 12:11:59 +03:00
|
|
|
switch (bpp)
|
2011-07-10 06:55:11 +04:00
|
|
|
{
|
2016-07-18 12:11:59 +03:00
|
|
|
case 24:
|
2016-07-19 17:15:38 +03:00
|
|
|
scanline = nSrcWidth * 3;
|
2016-10-14 11:01:02 +03:00
|
|
|
SrcFormat = PIXEL_FORMAT_BGR24;
|
2016-07-18 12:11:59 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 16:
|
2016-07-19 17:15:38 +03:00
|
|
|
scanline = nSrcWidth * 2;
|
2016-10-14 11:01:02 +03:00
|
|
|
SrcFormat = PIXEL_FORMAT_RGB16;
|
2016-07-18 12:11:59 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 15:
|
2016-07-19 17:15:38 +03:00
|
|
|
scanline = nSrcWidth * 2;
|
2016-10-14 11:01:02 +03:00
|
|
|
SrcFormat = PIXEL_FORMAT_RGB15;
|
2016-07-18 12:11:59 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
2016-07-19 17:15:38 +03:00
|
|
|
scanline = nSrcWidth;
|
2016-10-14 11:01:02 +03:00
|
|
|
SrcFormat = PIXEL_FORMAT_RGB8;
|
2016-07-18 12:11:59 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_ERR(TAG, "Invalid color depth %" PRIu32 "", bpp);
|
2016-07-18 12:11:59 +03:00
|
|
|
return FALSE;
|
2016-04-13 13:32:51 +03:00
|
|
|
}
|
2014-09-10 08:42:41 +04:00
|
|
|
|
2016-07-19 17:15:38 +03:00
|
|
|
BufferSize = scanline * nSrcHeight;
|
2016-07-18 12:11:59 +03:00
|
|
|
|
2016-04-13 13:32:51 +03:00
|
|
|
if (BufferSize > interleaved->TempSize)
|
|
|
|
{
|
2022-05-11 12:28:55 +03:00
|
|
|
interleaved->TempBuffer = winpr_aligned_realloc(interleaved->TempBuffer, BufferSize, 16);
|
2016-04-13 13:32:51 +03:00
|
|
|
interleaved->TempSize = BufferSize;
|
|
|
|
}
|
2014-09-12 06:29:09 +04:00
|
|
|
|
2016-04-13 13:32:51 +03:00
|
|
|
if (!interleaved->TempBuffer)
|
2022-12-12 15:28:36 +03:00
|
|
|
{
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_ERR(TAG, "interleaved->TempBuffer=%p", interleaved->TempBuffer);
|
2016-07-18 12:11:59 +03:00
|
|
|
return FALSE;
|
2022-12-12 15:28:36 +03:00
|
|
|
}
|
2014-09-12 06:29:09 +04:00
|
|
|
|
2016-07-18 12:11:59 +03:00
|
|
|
switch (bpp)
|
2011-07-10 06:55:11 +04:00
|
|
|
{
|
2016-07-18 12:11:59 +03:00
|
|
|
case 24:
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!RleDecompress24to24(pSrcData, SrcSize, interleaved->TempBuffer, scanline,
|
|
|
|
nSrcWidth, nSrcHeight))
|
2022-12-12 15:28:36 +03:00
|
|
|
{
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_ERR(TAG, "RleDecompress24to24 failed");
|
2018-10-16 18:23:24 +03:00
|
|
|
return FALSE;
|
2022-12-12 15:28:36 +03:00
|
|
|
}
|
2018-10-16 18:23:24 +03:00
|
|
|
|
2016-07-18 12:11:59 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 16:
|
|
|
|
case 15:
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!RleDecompress16to16(pSrcData, SrcSize, interleaved->TempBuffer, scanline,
|
|
|
|
nSrcWidth, nSrcHeight))
|
2022-12-12 15:28:36 +03:00
|
|
|
{
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_ERR(TAG, "RleDecompress16to16 failed");
|
2018-10-16 18:23:24 +03:00
|
|
|
return FALSE;
|
2022-12-12 15:28:36 +03:00
|
|
|
}
|
2018-10-16 18:23:24 +03:00
|
|
|
|
2016-07-18 12:11:59 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!RleDecompress8to8(pSrcData, SrcSize, interleaved->TempBuffer, scanline, nSrcWidth,
|
|
|
|
nSrcHeight))
|
2022-12-12 15:28:36 +03:00
|
|
|
{
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_ERR(TAG, "RleDecompress8to8 failed");
|
2018-10-16 18:23:24 +03:00
|
|
|
return FALSE;
|
2022-12-12 15:28:36 +03:00
|
|
|
}
|
2018-10-16 18:23:24 +03:00
|
|
|
|
2016-07-18 12:11:59 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_ERR(TAG, "Invalid color depth %" PRIu32 "", bpp);
|
2016-07-18 12:11:59 +03:00
|
|
|
return FALSE;
|
2011-07-10 06:55:11 +04:00
|
|
|
}
|
2014-09-10 08:42:41 +04:00
|
|
|
|
2022-12-12 15:28:36 +03:00
|
|
|
if (!freerdp_image_copy(pDstData, DstFormat, nDstStep, nXDst, nYDst, nDstWidth, nDstHeight,
|
|
|
|
interleaved->TempBuffer, SrcFormat, scanline, 0, 0, palette,
|
2023-01-09 10:07:43 +03:00
|
|
|
FREERDP_FLIP_VERTICAL | FREERDP_KEEP_DST_ALPHA))
|
2022-12-12 15:28:36 +03:00
|
|
|
{
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_ERR(TAG, "freerdp_image_copy failed");
|
2022-12-12 15:28:36 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
2014-09-10 08:42:41 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
BOOL interleaved_compress(BITMAP_INTERLEAVED_CONTEXT* interleaved, BYTE* pDstData, UINT32* pDstSize,
|
|
|
|
UINT32 nWidth, UINT32 nHeight, const BYTE* pSrcData, UINT32 SrcFormat,
|
|
|
|
UINT32 nSrcStep, UINT32 nXSrc, UINT32 nYSrc, const gdiPalette* palette,
|
|
|
|
UINT32 bpp)
|
2014-09-20 23:25:33 +04:00
|
|
|
{
|
2018-11-08 19:21:28 +03:00
|
|
|
BOOL status;
|
2014-09-20 23:25:33 +04:00
|
|
|
wStream* s;
|
|
|
|
UINT32 DstFormat = 0;
|
2018-11-07 14:00:49 +03:00
|
|
|
const size_t maxSize = 64 * 64 * 4;
|
2014-09-20 23:25:33 +04:00
|
|
|
|
2018-11-08 19:21:28 +03:00
|
|
|
if (!interleaved || !pDstData || !pSrcData)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if ((nWidth == 0) || (nHeight == 0))
|
|
|
|
return FALSE;
|
|
|
|
|
2014-09-20 23:25:33 +04:00
|
|
|
if (nWidth % 4)
|
|
|
|
{
|
2014-11-16 14:21:38 +03:00
|
|
|
WLog_ERR(TAG, "interleaved_compress: width is not a multiple of 4");
|
2016-04-05 18:07:45 +03:00
|
|
|
return FALSE;
|
2014-09-20 23:25:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((nWidth > 64) || (nHeight > 64))
|
|
|
|
{
|
2016-07-18 12:11:59 +03:00
|
|
|
WLog_ERR(TAG,
|
2019-11-06 17:24:51 +03:00
|
|
|
"interleaved_compress: width (%" PRIu32 ") or height (%" PRIu32
|
|
|
|
") is greater than 64",
|
|
|
|
nWidth, nHeight);
|
2016-04-05 18:07:45 +03:00
|
|
|
return FALSE;
|
2014-09-20 23:25:33 +04:00
|
|
|
}
|
|
|
|
|
2018-11-08 19:21:28 +03:00
|
|
|
switch (bpp)
|
|
|
|
{
|
|
|
|
case 24:
|
|
|
|
DstFormat = PIXEL_FORMAT_BGRX32;
|
|
|
|
break;
|
2014-09-20 23:25:33 +04:00
|
|
|
|
2018-11-08 19:21:28 +03:00
|
|
|
case 16:
|
|
|
|
DstFormat = PIXEL_FORMAT_RGB16;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 15:
|
|
|
|
DstFormat = PIXEL_FORMAT_RGB15;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-09-20 23:25:33 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!freerdp_image_copy(interleaved->TempBuffer, DstFormat, 0, 0, 0, nWidth, nHeight, pSrcData,
|
2023-01-09 10:07:43 +03:00
|
|
|
SrcFormat, nSrcStep, nXSrc, nYSrc, palette, FREERDP_KEEP_DST_ALPHA))
|
2017-11-14 15:59:15 +03:00
|
|
|
return FALSE;
|
|
|
|
|
2018-11-08 19:21:28 +03:00
|
|
|
s = Stream_New(pDstData, *pDstSize);
|
2014-09-20 23:25:33 +04:00
|
|
|
|
|
|
|
if (!s)
|
2016-04-05 18:07:45 +03:00
|
|
|
return FALSE;
|
2014-09-20 23:25:33 +04:00
|
|
|
|
2018-11-08 19:21:28 +03:00
|
|
|
Stream_SetPosition(interleaved->bts, 0);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (freerdp_bitmap_compress(interleaved->TempBuffer, nWidth, nHeight, s, bpp, maxSize,
|
|
|
|
nHeight - 1, interleaved->bts, 0) < 0)
|
2018-11-08 19:21:28 +03:00
|
|
|
status = FALSE;
|
|
|
|
else
|
|
|
|
status = TRUE;
|
|
|
|
|
2014-09-20 23:25:33 +04:00
|
|
|
Stream_SealLength(s);
|
2019-11-06 17:24:51 +03:00
|
|
|
*pDstSize = (UINT32)Stream_Length(s);
|
2014-09-20 23:25:33 +04:00
|
|
|
Stream_Free(s, FALSE);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2016-03-01 18:17:28 +03:00
|
|
|
BOOL bitmap_interleaved_context_reset(BITMAP_INTERLEAVED_CONTEXT* interleaved)
|
2014-09-12 22:57:44 +04:00
|
|
|
{
|
2016-03-01 18:17:28 +03:00
|
|
|
if (!interleaved)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
2014-09-12 22:57:44 +04:00
|
|
|
}
|
|
|
|
|
2014-09-10 08:42:41 +04:00
|
|
|
BITMAP_INTERLEAVED_CONTEXT* bitmap_interleaved_context_new(BOOL Compressor)
|
|
|
|
{
|
|
|
|
BITMAP_INTERLEAVED_CONTEXT* interleaved;
|
2019-11-06 17:24:51 +03:00
|
|
|
interleaved = (BITMAP_INTERLEAVED_CONTEXT*)calloc(1, sizeof(BITMAP_INTERLEAVED_CONTEXT));
|
2014-09-10 08:42:41 +04:00
|
|
|
|
|
|
|
if (interleaved)
|
2011-07-10 06:55:11 +04:00
|
|
|
{
|
2014-09-20 23:25:33 +04:00
|
|
|
interleaved->TempSize = 64 * 64 * 4;
|
2022-05-11 12:28:55 +03:00
|
|
|
interleaved->TempBuffer = winpr_aligned_malloc(interleaved->TempSize, 16);
|
2016-07-18 12:11:59 +03:00
|
|
|
|
2015-05-29 14:46:50 +03:00
|
|
|
if (!interleaved->TempBuffer)
|
|
|
|
{
|
|
|
|
free(interleaved);
|
|
|
|
WLog_ERR(TAG, "_aligned_malloc failed!");
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-07-18 12:11:59 +03:00
|
|
|
|
2014-09-20 23:25:33 +04:00
|
|
|
interleaved->bts = Stream_New(NULL, interleaved->TempSize);
|
2015-05-29 14:46:50 +03:00
|
|
|
|
|
|
|
if (!interleaved->bts)
|
|
|
|
{
|
2022-05-11 12:28:55 +03:00
|
|
|
winpr_aligned_free(interleaved->TempBuffer);
|
2015-05-29 14:46:50 +03:00
|
|
|
free(interleaved);
|
|
|
|
WLog_ERR(TAG, "Stream_New failed!");
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-07-09 23:48:18 +04:00
|
|
|
}
|
2011-07-25 22:50:26 +04:00
|
|
|
|
2014-09-10 08:42:41 +04:00
|
|
|
return interleaved;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bitmap_interleaved_context_free(BITMAP_INTERLEAVED_CONTEXT* interleaved)
|
|
|
|
{
|
|
|
|
if (!interleaved)
|
|
|
|
return;
|
|
|
|
|
2022-05-11 12:28:55 +03:00
|
|
|
winpr_aligned_free(interleaved->TempBuffer);
|
2014-09-20 23:25:33 +04:00
|
|
|
Stream_Free(interleaved->bts, TRUE);
|
2014-09-10 08:42:41 +04:00
|
|
|
free(interleaved);
|
2011-07-09 23:48:18 +04:00
|
|
|
}
|