comparison mupdf-source/thirdparty/zint/backend/tests/test_bmp.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /*
2 libzint - the open source barcode library
3 Copyright (C) 2020-2024 Robin Stuart <rstuart114@gmail.com>
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 3. Neither the name of the project nor the names of its contributors
15 may be used to endorse or promote products derived from this software
16 without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 SUCH DAMAGE.
29 */
30 /* SPDX-License-Identifier: BSD-3-Clause */
31
32 #include "testcommon.h"
33 #include <sys/stat.h>
34
35 INTERNAL int bmp_pixel_plot(struct zint_symbol *symbol, unsigned char *pixelbuf);
36
37 static void test_pixel_plot(const testCtx *const p_ctx) {
38 int debug = p_ctx->debug;
39
40 struct item {
41 int width;
42 int height;
43 char *pattern;
44 int repeat;
45 int ret;
46 };
47 /* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
48 static const struct item data[] = {
49 /* 0*/ { 1, 1, "1", 0, 0 },
50 /* 1*/ { 2, 1, "11", 0, 0 },
51 /* 2*/ { 2, 2, "10", 1, 0 },
52 /* 3*/ { 3, 1, "101", 0, 0 },
53 /* 4*/ { 3, 2, "101010", 0, 0 },
54 /* 5*/ { 3, 3, "101010101", 0, 0 },
55 /* 6*/ { 4, 1, "1001", 0, 0 },
56 /* 7*/ { 4, 3, "1001", 1, 0 },
57 /* 8*/ { 5, 1, "10101", 0, 0 },
58 /* 9*/ { 8, 2, "CBMWKRYGGYRKWMBC", 0, 0 },
59 };
60 const int data_size = ARRAY_SIZE(data);
61 int i, ret;
62 struct zint_symbol *symbol;
63
64 char *bmp = "out.bmp";
65
66 char data_buf[8 * 2 + 1];
67
68 const char *const have_identify = testUtilHaveIdentify();
69
70 testStart("test_pixel_plot");
71
72 for (i = 0; i < data_size; i++) {
73 int size;
74
75 if (testContinue(p_ctx, i)) continue;
76
77 symbol = ZBarcode_Create();
78 assert_nonnull(symbol, "Symbol not created\n");
79
80 strcpy(symbol->outfile, bmp);
81
82 symbol->bitmap_width = data[i].width;
83 symbol->bitmap_height = data[i].height;
84 symbol->debug |= debug;
85
86 size = data[i].width * data[i].height;
87 assert_nonzero(size < (int) sizeof(data_buf), "i:%d bmp_pixel_plot size %d < sizeof(data_buf) %d\n", i, size, (int) sizeof(data_buf));
88
89 if (data[i].repeat) {
90 testUtilStrCpyRepeat(data_buf, data[i].pattern, size);
91 } else {
92 strcpy(data_buf, data[i].pattern);
93 }
94 assert_equal(size, (int) strlen(data_buf), "i:%d bmp_pixel_plot size %d != strlen(data_buf) %d\n", i, size, (int) strlen(data_buf));
95
96 if (*data_buf > '9') {
97 symbol->symbology = BARCODE_ULTRA;
98 }
99
100 symbol->bitmap = (unsigned char *) data_buf;
101
102 ret = bmp_pixel_plot(symbol, (unsigned char *) data_buf);
103 assert_equal(ret, data[i].ret, "i:%d bmp_pixel_plot ret %d != %d (%s)\n", i, ret, data[i].ret, symbol->errtxt);
104
105 if (ret < ZINT_ERROR) {
106 if (have_identify) {
107 ret = testUtilVerifyIdentify(have_identify, symbol->outfile, debug);
108 assert_zero(ret, "i:%d identify %s ret %d != 0\n", i, symbol->outfile, ret);
109 }
110 if (!(debug & ZINT_DEBUG_TEST_KEEP_OUTFILE)) {
111 assert_zero(testUtilRemove(symbol->outfile), "i:%d testUtilRemove(%s) != 0\n", i, symbol->outfile);
112 }
113 } else {
114 if (!(debug & ZINT_DEBUG_TEST_KEEP_OUTFILE)) {
115 (void) testUtilRemove(symbol->outfile);
116 }
117 }
118
119 symbol->bitmap = NULL;
120
121 ZBarcode_Delete(symbol);
122 }
123
124 testFinish();
125 }
126
127 static void test_print(const testCtx *const p_ctx) {
128 int debug = p_ctx->debug;
129
130 struct item {
131 int symbology;
132 int border_width;
133 int output_options;
134 int whitespace_width;
135 int whitespace_height;
136 int option_1;
137 int option_2;
138 char *fgcolour;
139 char *bgcolour;
140 char *data;
141 char *expected_file;
142 };
143 static const struct item data[] = {
144 /* 0*/ { BARCODE_PDF417, -1, -1, 5, -1, -1, -1, "147AD0", "FC9630", "123", "pdf417_fg_bg.bmp" },
145 /* 1*/ { BARCODE_ULTRA, -1, -1, 5, -1, -1, -1, "147AD0", "FC9630", "123", "ultracode_fg_bg.bmp" },
146 /* 2*/ { BARCODE_ULTRA, 1, BARCODE_BOX, 1, 1, -1, -1, "147AD0", "FC9630", "123", "ultracode_fg_bg_hvwsp1_box1.bmp" },
147 /* 3*/ { BARCODE_PDF417COMP, -1, -1, 2, 2, -1, -1, "", "", "123", "pdf417comp_hvwsp2.bmp" },
148 };
149 const int data_size = ARRAY_SIZE(data);
150 int i, length, ret;
151 struct zint_symbol *symbol;
152
153 const char *data_dir = "/backend/tests/data/bmp";
154 const char *bmp = "out.bmp";
155 char expected_file[4096];
156 char escaped[1024];
157 int escaped_size = 1024;
158 unsigned char filebuf[32768];
159 int filebuf_size;
160
161 const char *const have_identify = testUtilHaveIdentify();
162
163 testStart("test_print");
164
165 if (p_ctx->generate) {
166 char data_dir_path[1024];
167 assert_nonzero(testUtilDataPath(data_dir_path, sizeof(data_dir_path), data_dir, NULL), "testUtilDataPath(%s) == 0\n", data_dir);
168 if (!testUtilDirExists(data_dir_path)) {
169 ret = testUtilMkDir(data_dir_path);
170 assert_zero(ret, "testUtilMkDir(%s) ret %d != 0 (%d: %s)\n", data_dir_path, ret, errno, strerror(errno));
171 }
172 }
173
174 for (i = 0; i < data_size; i++) {
175
176 if (testContinue(p_ctx, i)) continue;
177
178 symbol = ZBarcode_Create();
179 assert_nonnull(symbol, "Symbol not created\n");
180
181 length = testUtilSetSymbol(symbol, data[i].symbology, -1 /*input_mode*/, -1 /*eci*/, data[i].option_1, data[i].option_2, -1, data[i].output_options, data[i].data, -1, debug);
182 if (data[i].border_width != -1) {
183 symbol->border_width = data[i].border_width;
184 }
185 if (data[i].whitespace_width != -1) {
186 symbol->whitespace_width = data[i].whitespace_width;
187 }
188 if (data[i].whitespace_height != -1) {
189 symbol->whitespace_height = data[i].whitespace_height;
190 }
191 if (*data[i].fgcolour) {
192 strcpy(symbol->fgcolour, data[i].fgcolour);
193 }
194 if (*data[i].bgcolour) {
195 strcpy(symbol->bgcolour, data[i].bgcolour);
196 }
197
198 ret = ZBarcode_Encode(symbol, (unsigned char *) data[i].data, length);
199 assert_zero(ret, "i:%d %s ZBarcode_Encode ret %d != 0 %s\n", i, testUtilBarcodeName(data[i].symbology), ret, symbol->errtxt);
200
201 strcpy(symbol->outfile, bmp);
202 ret = ZBarcode_Print(symbol, 0);
203 assert_zero(ret, "i:%d %s ZBarcode_Print %s ret %d != 0\n", i, testUtilBarcodeName(data[i].symbology), symbol->outfile, ret);
204
205 assert_nonzero(testUtilDataPath(expected_file, sizeof(expected_file), data_dir, data[i].expected_file), "i:%d testUtilDataPath == 0\n", i);
206
207 if (p_ctx->generate) {
208 printf(" /*%3d*/ { %s, %d, %s, %d, %d, %d, %d, \"%s\", \"%s\", \"%s\", \"%s\"},\n",
209 i, testUtilBarcodeName(data[i].symbology), data[i].border_width, testUtilOutputOptionsName(data[i].output_options),
210 data[i].whitespace_width, data[i].whitespace_height,
211 data[i].option_1, data[i].option_2, data[i].fgcolour, data[i].bgcolour,
212 testUtilEscape(data[i].data, length, escaped, escaped_size), data[i].expected_file);
213 ret = testUtilRename(symbol->outfile, expected_file);
214 assert_zero(ret, "i:%d testUtilRename(%s, %s) ret %d != 0 (%d: %s)\n", i, symbol->outfile, expected_file, ret, errno, strerror(errno));
215 if (have_identify) {
216 ret = testUtilVerifyIdentify(have_identify, expected_file, debug);
217 assert_zero(ret, "i:%d %s identify %s ret %d != 0\n", i, testUtilBarcodeName(data[i].symbology), expected_file, ret);
218 }
219 } else {
220 assert_nonzero(testUtilExists(symbol->outfile), "i:%d testUtilExists(%s) == 0\n", i, symbol->outfile);
221 assert_nonzero(testUtilExists(expected_file), "i:%d testUtilExists(%s) == 0\n", i, expected_file);
222
223 ret = testUtilCmpBins(symbol->outfile, expected_file);
224 assert_zero(ret, "i:%d %s testUtilCmpBins(%s, %s) %d != 0\n", i, testUtilBarcodeName(data[i].symbology), symbol->outfile, expected_file, ret);
225
226 ret = testUtilReadFile(symbol->outfile, filebuf, sizeof(filebuf), &filebuf_size); /* For BARCODE_MEMORY_FILE */
227 assert_zero(ret, "i:%d %s testUtilReadFile(%s) %d != 0\n", i, testUtilBarcodeName(data[i].symbology), symbol->outfile, ret);
228
229 if (!(debug & ZINT_DEBUG_TEST_KEEP_OUTFILE)) {
230 assert_zero(testUtilRemove(symbol->outfile), "i:%d testUtilRemove(%s) != 0\n", i, symbol->outfile);
231 }
232
233 symbol->output_options |= BARCODE_MEMORY_FILE;
234 ret = ZBarcode_Print(symbol, 0);
235 assert_zero(ret, "i:%d %s ZBarcode_Print %s ret %d != 0 (%s)\n",
236 i, testUtilBarcodeName(data[i].symbology), symbol->outfile, ret, symbol->errtxt);
237 assert_nonnull(symbol->memfile, "i:%d %s memfile NULL\n", i, testUtilBarcodeName(data[i].symbology));
238 assert_equal(symbol->memfile_size, filebuf_size, "i:%d %s memfile_size %d != %d\n",
239 i, testUtilBarcodeName(data[i].symbology), symbol->memfile_size, filebuf_size);
240 assert_zero(memcmp(symbol->memfile, filebuf, symbol->memfile_size), "i:%d %s memcmp(memfile, filebuf) != 0\n",
241 i, testUtilBarcodeName(data[i].symbology));
242 }
243
244 ZBarcode_Delete(symbol);
245 }
246
247 testFinish();
248 }
249
250 static void test_outfile(const testCtx *const p_ctx) {
251 int ret;
252 int skip_readonly_test = 0;
253 struct zint_symbol symbol = {0};
254 unsigned char data[] = { "1" };
255
256 (void)p_ctx;
257
258 testStart("test_outfile");
259
260 symbol.symbology = BARCODE_CODE128;
261 symbol.bitmap = data;
262 symbol.bitmap_width = symbol.bitmap_height = 1;
263
264 strcpy(symbol.outfile, "test_bmp_out.bmp");
265 #ifndef _WIN32
266 skip_readonly_test = getuid() == 0; /* Skip if running as root on Unix as can't create read-only file */
267 #endif
268 if (!skip_readonly_test) {
269 static char expected_errtxt[] = "601: Could not open BMP output file ("; /* Excluding OS-dependent `errno` stuff */
270
271 (void) testUtilRmROFile(symbol.outfile); /* In case lying around from previous fail */
272 assert_nonzero(testUtilCreateROFile(symbol.outfile), "bmp_pixel_plot testUtilCreateROFile(%s) fail (%d: %s)\n", symbol.outfile, errno, strerror(errno));
273
274 ret = bmp_pixel_plot(&symbol, data);
275 assert_equal(ret, ZINT_ERROR_FILE_ACCESS, "bmp_pixel_plot ret %d != ZINT_ERROR_FILE_ACCESS (%d) (%s)\n", ret, ZINT_ERROR_FILE_ACCESS, symbol.errtxt);
276 assert_zero(testUtilRmROFile(symbol.outfile), "bmp_pixel_plot testUtilRmROFile(%s) != 0 (%d: %s)\n", symbol.outfile, errno, strerror(errno));
277 assert_zero(strncmp(symbol.errtxt, expected_errtxt, sizeof(expected_errtxt) - 1), "strncmp(%s, %s) != 0\n", symbol.errtxt, expected_errtxt);
278 }
279
280 symbol.output_options |= BARCODE_STDOUT;
281
282 ret = bmp_pixel_plot(&symbol, data);
283 printf(" - ignore (BMP to stdout)\n"); fflush(stdout);
284 assert_zero(ret, "bmp_pixel_plot ret %d != 0 (%s)\n", ret, symbol.errtxt);
285
286 testFinish();
287 }
288
289 int main(int argc, char *argv[]) {
290
291 testFunction funcs[] = { /* name, func */
292 { "test_pixel_plot", test_pixel_plot },
293 { "test_print", test_print },
294 { "test_outfile", test_outfile },
295 };
296
297 testRun(argc, argv, funcs, ARRAY_SIZE(funcs));
298
299 testReport();
300
301 return 0;
302 }
303
304 /* vim: set ts=4 sw=4 et : */