FreeRDP
TestImage.c
1 #include <stdio.h>
2 #include <winpr/string.h>
3 #include <winpr/assert.h>
4 #include <winpr/file.h>
5 #include <winpr/path.h>
6 #include <winpr/image.h>
7 
8 static const char test_src_filename[] = TEST_SOURCE_PATH "/rgb";
9 static const char test_bin_filename[] = TEST_BINARY_PATH "/rgb";
10 
11 static BOOL test_image_equal(const wImage* imageA, const wImage* imageB)
12 {
13  return winpr_image_equal(imageA, imageB,
14  WINPR_IMAGE_CMP_IGNORE_DEPTH | WINPR_IMAGE_CMP_IGNORE_ALPHA |
15  WINPR_IMAGE_CMP_FUZZY);
16 }
17 
18 static BOOL test_equal_to(const wImage* bmp, const char* name, UINT32 format)
19 {
20  BOOL rc = FALSE;
21  wImage* cmp = winpr_image_new();
22  if (!cmp)
23  goto fail;
24 
25  char path[MAX_PATH] = { 0 };
26  (void)_snprintf(path, sizeof(path), "%s.%s", name, winpr_image_format_extension(format));
27  const int cmpSize = winpr_image_read(cmp, path);
28  if (cmpSize <= 0)
29  {
30  (void)fprintf(stderr, "[%s] winpr_image_read failed for %s", __func__, path);
31  goto fail;
32  }
33 
34  rc = test_image_equal(bmp, cmp);
35  if (!rc)
36  (void)fprintf(stderr, "[%s] winpr_image_eqal failed", __func__);
37 
38 fail:
39  winpr_image_free(cmp, TRUE);
40  return rc;
41 }
42 
43 static BOOL test_equal(void)
44 {
45  BOOL rc = FALSE;
46  wImage* bmp = winpr_image_new();
47 
48  if (!bmp)
49  goto fail;
50 
51  char path[MAX_PATH] = { 0 };
52  (void)_snprintf(path, sizeof(path), "%s.bmp", test_src_filename);
53  PathCchConvertStyleA(path, sizeof(path), PATH_STYLE_NATIVE);
54 
55  const int bmpSize = winpr_image_read(bmp, path);
56  if (bmpSize <= 0)
57  {
58  (void)fprintf(stderr, "[%s] winpr_image_read failed for %s", __func__, path);
59  goto fail;
60  }
61 
62  for (UINT32 x = 0; x < UINT8_MAX; x++)
63  {
64  if (!winpr_image_format_is_supported(x))
65  continue;
66  if (!test_equal_to(bmp, test_src_filename, x))
67  goto fail;
68  }
69 
70  rc = TRUE;
71 fail:
72  winpr_image_free(bmp, TRUE);
73 
74  return rc;
75 }
76 
77 static BOOL test_read_write_compare(const char* tname, const char* tdst, UINT32 format)
78 {
79  BOOL rc = FALSE;
80  wImage* bmp1 = winpr_image_new();
81  wImage* bmp2 = winpr_image_new();
82  wImage* bmp3 = winpr_image_new();
83  if (!bmp1 || !bmp2 || !bmp3)
84  goto fail;
85 
86  char spath[MAX_PATH] = { 0 };
87  char dpath[MAX_PATH] = { 0 };
88  char bpath1[MAX_PATH] = { 0 };
89  char bpath2[MAX_PATH] = { 0 };
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);
100 
101  const int bmpRSize = winpr_image_read(bmp1, spath);
102  if (bmpRSize <= 0)
103  {
104  (void)fprintf(stderr, "[%s] winpr_image_read failed for %s", __func__, spath);
105  goto fail;
106  }
107 
108  const int bmpWSize = winpr_image_write(bmp1, dpath);
109  if (bmpWSize <= 0)
110  {
111  (void)fprintf(stderr, "[%s] winpr_image_write failed for %s", __func__, dpath);
112  goto fail;
113  }
114 
115  const int bmp2RSize = winpr_image_read(bmp2, dpath);
116  if (bmp2RSize <= 0)
117  {
118  (void)fprintf(stderr, "[%s] winpr_image_read failed for %s", __func__, dpath);
119  goto fail;
120  }
121 
122  const int bmpSrcWSize = winpr_image_write_ex(bmp1, WINPR_IMAGE_BITMAP, bpath1);
123  if (bmpSrcWSize <= 0)
124  {
125  (void)fprintf(stderr, "[%s] winpr_image_write_ex failed for %s", __func__, bpath1);
126  goto fail;
127  }
128 
129  /* write a bitmap and read it back.
130  * this tests if we have the proper internal format */
131  const int bmpBinWSize = winpr_image_write_ex(bmp2, WINPR_IMAGE_BITMAP, bpath2);
132  if (bmpBinWSize <= 0)
133  {
134  (void)fprintf(stderr, "[%s] winpr_image_write_ex failed for %s", __func__, bpath2);
135  goto fail;
136  }
137 
138  const int bmp3RSize = winpr_image_read(bmp3, bpath2);
139  if (bmp3RSize <= 0)
140  {
141  (void)fprintf(stderr, "[%s] winpr_image_read failed for %s", __func__, bpath2);
142  goto fail;
143  }
144 
145  if (!winpr_image_equal(bmp1, bmp2,
146  WINPR_IMAGE_CMP_IGNORE_DEPTH | WINPR_IMAGE_CMP_IGNORE_ALPHA |
147  WINPR_IMAGE_CMP_FUZZY))
148  {
149  (void)fprintf(stderr, "[%s] winpr_image_eqal failed bmp1 bmp2", __func__);
150  goto fail;
151  }
152 
153  rc = winpr_image_equal(bmp3, bmp2,
154  WINPR_IMAGE_CMP_IGNORE_DEPTH | WINPR_IMAGE_CMP_IGNORE_ALPHA |
155  WINPR_IMAGE_CMP_FUZZY);
156  if (!rc)
157  (void)fprintf(stderr, "[%s] winpr_image_eqal failed bmp3 bmp2", __func__);
158 fail:
159  winpr_image_free(bmp1, TRUE);
160  winpr_image_free(bmp2, TRUE);
161  winpr_image_free(bmp3, TRUE);
162  return rc;
163 }
164 
165 static BOOL test_read_write(void)
166 {
167  BOOL rc = TRUE;
168  for (UINT32 x = 0; x < UINT8_MAX; x++)
169  {
170  if (!winpr_image_format_is_supported(x))
171  continue;
172  if (!test_read_write_compare(test_src_filename, test_bin_filename, x))
173  rc = FALSE;
174  }
175  return rc;
176 }
177 
178 static BOOL test_load_file(const char* name)
179 {
180  BOOL rc = FALSE;
181  wImage* image = winpr_image_new();
182  if (!image || !name)
183  goto fail;
184 
185  const int res = winpr_image_read(image, name);
186  rc = (res > 0) ? TRUE : FALSE;
187 
188 fail:
189  winpr_image_free(image, TRUE);
190  return rc;
191 }
192 
193 static BOOL test_load(void)
194 {
195  const char* names[] = {
196  "rgb.16a.bmp", "rgb.16a.nocolor.bmp", "rgb.16.bmp", "rgb.16.nocolor.bmp",
197  "rgb.16x.bmp", "rgb.16x.nocolor.bmp", "rgb.24.bmp", "rgb.24.nocolor.bmp",
198  "rgb.32.bmp", "rgb.32.nocolor.bmp", "rgb.32x.bmp", "rgb.32x.nocolor.bmp",
199  "rgb.bmp"
200  };
201 
202  for (size_t x = 0; x < ARRAYSIZE(names); x++)
203  {
204  const char* name = names[x];
205  char* fname = GetCombinedPath(TEST_SOURCE_PATH, name);
206  const BOOL res = test_load_file(fname);
207  free(fname);
208  if (!res)
209  return FALSE;
210  }
211 
212  return TRUE;
213 }
214 
215 int TestImage(int argc, char* argv[])
216 {
217  int rc = 0;
218 
219  WINPR_UNUSED(argc);
220  WINPR_UNUSED(argv);
221 
222  if (!test_equal())
223  rc -= 1;
224 
225  if (!test_read_write())
226  rc -= 2;
227 
228  if (!test_load())
229  rc -= 4;
230 
231  return rc;
232 }