comparison mupdf-source/thirdparty/zxing-cpp/wrappers/c/ZXingCTest.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 * Copyright 2023 siiky
3 * Copyright 2023 Axel Waggershauser
4 */
5 // SPDX-License-Identifier: Apache-2.0
6
7 #include "ZXingC.h"
8
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #define STB_IMAGE_IMPLEMENTATION
13 #define STBI_NO_LINEAR // prevent dependency on -lm
14 #define STBI_NO_HDR
15 #include <stb_image.h>
16
17 int usage(char* pname)
18 {
19 fprintf(stderr, "ZXingCTest %s, usage: %s FILE [FORMATS]\n", ZXing_Version(), pname);
20 return 1;
21 }
22
23 bool parse_args(int argc, char** argv, char** filename, ZXing_BarcodeFormats* formats)
24 {
25 if (argc < 2)
26 return false;
27 *filename = argv[1];
28 if (argc >= 3) {
29 *formats = ZXing_BarcodeFormatsFromString(argv[2]);
30 if (*formats == ZXing_BarcodeFormat_Invalid) {
31 fprintf(stderr, "%s\n", ZXing_LastErrorMsg());
32 return false;
33 }
34 }
35 return true;
36 }
37
38 void printF(const char* fmt, char* text)
39 {
40 if (!text)
41 return;
42 if (*text)
43 printf(fmt, text);
44 ZXing_free(text);
45 }
46
47 #define CHECK(GOOD) \
48 if (!(GOOD)) { \
49 char* error = ZXing_LastErrorMsg(); \
50 fprintf(stderr, "CHECK(%s) failed: %s\n", #GOOD, error); \
51 ZXing_free(error); \
52 return 2; \
53 }
54
55 int main(int argc, char** argv)
56 {
57 int ret = 0;
58 char* filename = NULL;
59 ZXing_BarcodeFormats formats = ZXing_BarcodeFormat_None;
60
61 if (!parse_args(argc, argv, &filename, &formats))
62 return usage(argv[0]);
63
64 int width = 0;
65 int height = 0;
66 int channels = 0;
67 stbi_uc* data = stbi_load(filename, &width, &height, &channels, STBI_grey);
68
69 ZXing_ImageView* iv = NULL;
70 ZXing_Image* img = NULL;
71
72 if (data) {
73 iv = ZXing_ImageView_new(data, width, height, ZXing_ImageFormat_Lum, 0, 0);
74 CHECK(iv)
75 } else {
76 fprintf(stderr, "Could not read image '%s'\n", filename);
77 #if defined(ZXING_EXPERIMENTAL_API) && defined(ZXING_WRITERS)
78 if (formats == ZXing_BarcodeFormat_Invalid)
79 return 2;
80 fprintf(stderr, "Using '%s' as text input to create barcode\n", filename);
81 ZXing_CreatorOptions* cOpts = ZXing_CreatorOptions_new(formats);
82 CHECK(cOpts)
83 ZXing_Barcode* barcode = ZXing_CreateBarcodeFromText(filename, 0, cOpts);
84 CHECK(barcode)
85 img = ZXing_WriteBarcodeToImage(barcode, NULL);
86 CHECK(img)
87 ZXing_CreatorOptions_delete(cOpts);
88 ZXing_Barcode_delete(barcode);
89 #else
90 return 2;
91 #endif
92 }
93
94 ZXing_ReaderOptions* opts = ZXing_ReaderOptions_new();
95 ZXing_ReaderOptions_setTextMode(opts, ZXing_TextMode_HRI);
96 ZXing_ReaderOptions_setEanAddOnSymbol(opts, ZXing_EanAddOnSymbol_Ignore);
97 ZXing_ReaderOptions_setFormats(opts, formats);
98 ZXing_ReaderOptions_setReturnErrors(opts, true);
99
100 ZXing_Barcodes* barcodes = ZXing_ReadBarcodes(iv ? iv : (ZXing_ImageView*)img, opts);
101 CHECK(barcodes)
102
103 ZXing_ImageView_delete(iv);
104 ZXing_Image_delete(img);
105 ZXing_ReaderOptions_delete(opts);
106 stbi_image_free(data);
107
108 for (int i = 0, n = ZXing_Barcodes_size(barcodes); i < n; ++i) {
109 const ZXing_Barcode* barcode = ZXing_Barcodes_at(barcodes, i);
110
111 printF("Text : %s\n", ZXing_Barcode_text(barcode));
112 printF("BytesECI : %s\n", (char*)ZXing_Barcode_bytesECI(barcode, NULL));
113 printF("Format : %s\n", ZXing_BarcodeFormatToString(ZXing_Barcode_format(barcode)));
114 printF("Content : %s\n", ZXing_ContentTypeToString(ZXing_Barcode_contentType(barcode)));
115 printF("Identifier : %s\n", ZXing_Barcode_symbologyIdentifier(barcode));
116 printf("HasECI : %d\n", ZXing_Barcode_hasECI(barcode));
117 printF("EC Level : %s\n", ZXing_Barcode_ecLevel(barcode));
118 printF("Error : %s\n", ZXing_Barcode_errorMsg(barcode));
119 printF("Position : %s\n", ZXing_PositionToString(ZXing_Barcode_position(barcode)));
120 printf("Rotation : %d\n", ZXing_Barcode_orientation(barcode));
121 printf("IsMirrored : %d\n", ZXing_Barcode_isMirrored(barcode));
122 printf("IsInverted : %d\n", ZXing_Barcode_isInverted(barcode));
123
124 if (i < n-1)
125 printf("\n");
126 }
127
128 if (ZXing_Barcodes_size(barcodes) == 0)
129 printf("No barcode found\n");
130
131 ZXing_Barcodes_delete(barcodes);
132
133 return ret;
134 }