2#include <winpr/string.h>
3#include <winpr/assert.h>
6#include <winpr/image.h>
8static const char test_src_filename[] = TEST_SOURCE_PATH
"/rgb";
9static const char test_bin_filename[] = TEST_BINARY_PATH
"/rgb";
11static BOOL test_image_equal(
const wImage* imageA,
const wImage* imageB)
13 return winpr_image_equal(imageA, imageB,
14 WINPR_IMAGE_CMP_IGNORE_DEPTH | WINPR_IMAGE_CMP_IGNORE_ALPHA |
15 WINPR_IMAGE_CMP_FUZZY);
18static BOOL test_equal_to(
const wImage* bmp,
const char* name, UINT32 format)
21 wImage* cmp = winpr_image_new();
25 char path[MAX_PATH] = WINPR_C_ARRAY_INIT;
26 (void)_snprintf(path,
sizeof(path),
"%s.%s", name, winpr_image_format_extension(format));
27 const int cmpSize = winpr_image_read(cmp, path);
30 (void)fprintf(stderr,
"[%s] winpr_image_read failed for %s", __func__, path);
34 rc = test_image_equal(bmp, cmp);
36 (void)fprintf(stderr,
"[%s] winpr_image_eqal failed", __func__);
39 winpr_image_free(cmp, TRUE);
43static BOOL test_equal(
void)
46 wImage* bmp = winpr_image_new();
51 char path[MAX_PATH] = WINPR_C_ARRAY_INIT;
52 (void)_snprintf(path,
sizeof(path),
"%s.bmp", test_src_filename);
53 PathCchConvertStyleA(path,
sizeof(path), PATH_STYLE_NATIVE);
55 const int bmpSize = winpr_image_read(bmp, path);
58 (void)fprintf(stderr,
"[%s] winpr_image_read failed for %s", __func__, path);
62 for (UINT32 x = 0; x < UINT8_MAX; x++)
64 if (!winpr_image_format_is_supported(x))
66 if (!test_equal_to(bmp, test_src_filename, x))
72 winpr_image_free(bmp, TRUE);
77static BOOL test_read_write_compare(
const char* tname,
const char* tdst, UINT32 format)
80 wImage* bmp1 = winpr_image_new();
81 wImage* bmp2 = winpr_image_new();
82 wImage* bmp3 = winpr_image_new();
83 if (!bmp1 || !bmp2 || !bmp3)
86 char spath[MAX_PATH] = WINPR_C_ARRAY_INIT;
87 char dpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
88 char bpath1[MAX_PATH] = WINPR_C_ARRAY_INIT;
89 char bpath2[MAX_PATH] = WINPR_C_ARRAY_INIT;
90 (void)_snprintf(spath,
sizeof(spath),
"%s.%s", tname, winpr_image_format_extension(format));
91 (void)_snprintf(dpath,
sizeof(dpath),
"%s.%s", tdst, winpr_image_format_extension(format));
92 (void)_snprintf(bpath1,
sizeof(bpath1),
"%s.src.%s", dpath,
93 winpr_image_format_extension(WINPR_IMAGE_BITMAP));
94 (void)_snprintf(bpath2,
sizeof(bpath2),
"%s.bin.%s", dpath,
95 winpr_image_format_extension(WINPR_IMAGE_BITMAP));
96 PathCchConvertStyleA(spath,
sizeof(spath), PATH_STYLE_NATIVE);
97 PathCchConvertStyleA(dpath,
sizeof(dpath), PATH_STYLE_NATIVE);
98 PathCchConvertStyleA(bpath1,
sizeof(bpath1), PATH_STYLE_NATIVE);
99 PathCchConvertStyleA(bpath2,
sizeof(bpath2), PATH_STYLE_NATIVE);
101 const int bmpRSize = winpr_image_read(bmp1, spath);
104 (void)fprintf(stderr,
"[%s] winpr_image_read failed for %s", __func__, spath);
108 const int bmpWSize = winpr_image_write(bmp1, dpath);
111 (void)fprintf(stderr,
"[%s] winpr_image_write failed for %s", __func__, dpath);
115 const int bmp2RSize = winpr_image_read(bmp2, dpath);
118 (void)fprintf(stderr,
"[%s] winpr_image_read failed for %s", __func__, dpath);
122 const int bmpSrcWSize = winpr_image_write_ex(bmp1, WINPR_IMAGE_BITMAP, bpath1);
123 if (bmpSrcWSize <= 0)
125 (void)fprintf(stderr,
"[%s] winpr_image_write_ex failed for %s", __func__, bpath1);
131 const int bmpBinWSize = winpr_image_write_ex(bmp2, WINPR_IMAGE_BITMAP, bpath2);
132 if (bmpBinWSize <= 0)
134 (void)fprintf(stderr,
"[%s] winpr_image_write_ex failed for %s", __func__, bpath2);
138 const int bmp3RSize = winpr_image_read(bmp3, bpath2);
141 (void)fprintf(stderr,
"[%s] winpr_image_read failed for %s", __func__, bpath2);
145 if (!winpr_image_equal(bmp1, bmp2,
146 WINPR_IMAGE_CMP_IGNORE_DEPTH | WINPR_IMAGE_CMP_IGNORE_ALPHA |
147 WINPR_IMAGE_CMP_FUZZY))
149 (void)fprintf(stderr,
"[%s] winpr_image_eqal failed bmp1 bmp2", __func__);
153 rc = winpr_image_equal(bmp3, bmp2,
154 WINPR_IMAGE_CMP_IGNORE_DEPTH | WINPR_IMAGE_CMP_IGNORE_ALPHA |
155 WINPR_IMAGE_CMP_FUZZY);
157 (void)fprintf(stderr,
"[%s] winpr_image_eqal failed bmp3 bmp2", __func__);
159 winpr_image_free(bmp1, TRUE);
160 winpr_image_free(bmp2, TRUE);
161 winpr_image_free(bmp3, TRUE);
165static BOOL test_read_write(
void)
168 for (UINT32 x = 0; x < UINT8_MAX; x++)
170 if (!winpr_image_format_is_supported(x))
172 if (!test_read_write_compare(test_src_filename, test_bin_filename, x))
178static BOOL test_load_file(
const char* name)
181 wImage* image = winpr_image_new();
185 const int res = winpr_image_read(image, name);
189 winpr_image_free(image, TRUE);
193static void put_u16(BYTE* p, UINT16 v)
195 p[0] = (BYTE)(v & 0xff);
196 p[1] = (BYTE)((v >> 8) & 0xff);
199static void put_u32(BYTE* p, UINT32 v)
201 p[0] = (BYTE)(v & 0xff);
202 p[1] = (BYTE)((v >> 8) & 0xff);
203 p[2] = (BYTE)((v >> 16) & 0xff);
204 p[3] = (BYTE)((v >> 24) & 0xff);
211static BOOL test_unaligned_no_overread(
void)
214 const UINT32 width = 1;
215 const UINT32 height = 10;
216 const UINT32 bpp = 3;
217 const UINT32 uscanline = width * bpp;
218 const UINT32 biSizeImage = uscanline * height;
219 const size_t offBits = 54;
220 const size_t size = offBits + biSizeImage;
222 wImage* image = winpr_image_new();
223 BYTE* bmp = (BYTE*)calloc(1, size);
229 put_u32(&bmp[2], (UINT32)size);
230 put_u32(&bmp[10], (UINT32)offBits);
231 put_u32(&bmp[14], 40);
232 put_u32(&bmp[18], width);
233 put_u32(&bmp[22], (UINT32)(-(INT32)height));
234 put_u16(&bmp[26], 1);
235 put_u16(&bmp[28], 24);
236 put_u32(&bmp[30], 0);
237 put_u32(&bmp[34], biSizeImage);
238 for (UINT32 i = 0; i < biSizeImage; i++)
239 bmp[offBits + i] = (BYTE)i;
241 if (winpr_image_read_buffer(image, bmp, size) <= 0)
244 if ((image->width != width) || (image->height != height))
247 for (UINT32 row = 0; row < height; row++)
249 for (UINT32 b = 0; b < uscanline; b++)
251 if (image->data[1ULL * row * image->scanline + b] != (BYTE)(row * uscanline + b))
259 winpr_image_free(image, TRUE);
263static BOOL test_load(
void)
265 const char* names[] = {
266 "rgb.16a.bmp",
"rgb.16a.nocolor.bmp",
"rgb.16.bmp",
"rgb.16.nocolor.bmp",
267 "rgb.16x.bmp",
"rgb.16x.nocolor.bmp",
"rgb.24.bmp",
"rgb.24.nocolor.bmp",
268 "rgb.32.bmp",
"rgb.32.nocolor.bmp",
"rgb.32x.bmp",
"rgb.32x.nocolor.bmp",
272 for (
size_t x = 0; x < ARRAYSIZE(names); x++)
274 const char* name = names[x];
275 char* fname = GetCombinedPath(TEST_SOURCE_PATH, name);
276 const BOOL res = test_load_file(fname);
285int TestImage(
int argc,
char* argv[])
295 if (!test_read_write())
301 if (!test_unaligned_no_overread())