1 /**
2  * FreeRDP: A Remote Desktop Protocol Implementation
3  * GDI Library Tests
4  *
5  * Copyright 2016 Armin Novak <armin.novak@thincast.com>
6  * Copyright 2016 Thincast Technologies GmbH
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *	 http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 #include "helpers.h"
21 
test_convert_to_bitmap(const BYTE * src,UINT32 SrcFormat,UINT32 SrcStride,UINT32 xSrc,UINT32 ySrc,UINT32 DstFormat,UINT32 DstStride,UINT32 xDst,UINT32 yDst,UINT32 nWidth,UINT32 nHeight,const gdiPalette * hPalette)22 HGDI_BITMAP test_convert_to_bitmap(const BYTE* src, UINT32 SrcFormat, UINT32 SrcStride, UINT32 xSrc,
23                                    UINT32 ySrc, UINT32 DstFormat, UINT32 DstStride, UINT32 xDst,
24                                    UINT32 yDst, UINT32 nWidth, UINT32 nHeight,
25                                    const gdiPalette* hPalette)
26 {
27 	HGDI_BITMAP bmp;
28 	BYTE* data;
29 
30 	if (DstStride == 0)
31 		DstStride = nWidth * GetBytesPerPixel(DstFormat);
32 
33 	data = _aligned_malloc(DstStride * nHeight, 16);
34 
35 	if (!data)
36 		return NULL;
37 
38 	if (!freerdp_image_copy(data, DstFormat, DstStride, xDst, yDst, nWidth, nHeight, src, SrcFormat,
39 	                        SrcStride, xSrc, ySrc, hPalette, FREERDP_FLIP_NONE))
40 	{
41 		_aligned_free(data);
42 		return NULL;
43 	}
44 
45 	bmp = gdi_CreateBitmap(nWidth, nHeight, DstFormat, data);
46 
47 	if (!bmp)
48 	{
49 		_aligned_free(data);
50 		return NULL;
51 	}
52 
53 	return bmp;
54 }
55 
test_dump_data(unsigned char * p,int len,int width,const char * name)56 static void test_dump_data(unsigned char* p, int len, int width, const char* name)
57 {
58 	unsigned char* line = p;
59 	int i, thisline, offset = 0;
60 	printf("\n%s[%d][%d]:\n", name, len / width, width);
61 
62 	while (offset < len)
63 	{
64 		printf("%04x ", offset);
65 		thisline = len - offset;
66 
67 		if (thisline > width)
68 			thisline = width;
69 
70 		for (i = 0; i < thisline; i++)
71 			printf("%02x ", line[i]);
72 
73 		for (; i < width; i++)
74 			printf("   ");
75 
76 		printf("\n");
77 		offset += thisline;
78 		line += thisline;
79 	}
80 
81 	printf("\n");
82 	fflush(stdout);
83 }
84 
test_dump_bitmap(HGDI_BITMAP hBmp,const char * name)85 void test_dump_bitmap(HGDI_BITMAP hBmp, const char* name)
86 {
87 	UINT32 stride = hBmp->width * GetBytesPerPixel(hBmp->format);
88 	test_dump_data(hBmp->data, hBmp->height * stride, stride, name);
89 }
90 
CompareBitmaps(HGDI_BITMAP hBmp1,HGDI_BITMAP hBmp2,const gdiPalette * palette)91 static BOOL CompareBitmaps(HGDI_BITMAP hBmp1, HGDI_BITMAP hBmp2, const gdiPalette* palette)
92 {
93 	UINT32 x, y;
94 	const BYTE* p1 = hBmp1->data;
95 	const BYTE* p2 = hBmp2->data;
96 	UINT32 colorA, colorB;
97 	UINT32 minw = (hBmp1->width < hBmp2->width) ? hBmp1->width : hBmp2->width;
98 	UINT32 minh = (hBmp1->height < hBmp2->height) ? hBmp1->height : hBmp2->height;
99 
100 	for (y = 0; y < minh; y++)
101 	{
102 		for (x = 0; x < minw; x++)
103 		{
104 			colorA = ReadColor(p1, hBmp1->format);
105 			colorB = ReadColor(p2, hBmp2->format);
106 			p1 += GetBytesPerPixel(hBmp1->format);
107 			p2 += GetBytesPerPixel(hBmp2->format);
108 
109 			if (hBmp1->format != hBmp2->format)
110 				colorB = FreeRDPConvertColor(colorB, hBmp2->format, hBmp1->format, palette);
111 
112 			if (colorA != colorB)
113 				return FALSE;
114 		}
115 	}
116 
117 	return TRUE;
118 }
119 
test_assert_bitmaps_equal(HGDI_BITMAP hBmpActual,HGDI_BITMAP hBmpExpected,const char * name,const gdiPalette * palette)120 BOOL test_assert_bitmaps_equal(HGDI_BITMAP hBmpActual, HGDI_BITMAP hBmpExpected, const char* name,
121                                const gdiPalette* palette)
122 {
123 	BOOL bitmapsEqual = CompareBitmaps(hBmpActual, hBmpExpected, palette);
124 
125 	if (!bitmapsEqual)
126 	{
127 		printf("Testing ROP %s [%s|%s]\n", name, FreeRDPGetColorFormatName(hBmpActual->format),
128 		       FreeRDPGetColorFormatName(hBmpExpected->format));
129 		test_dump_bitmap(hBmpActual, "Actual");
130 		test_dump_bitmap(hBmpExpected, "Expected");
131 		fflush(stdout);
132 		fflush(stderr);
133 	}
134 
135 	return bitmapsEqual;
136 }
137