File: common.c

package info (click to toggle)
qrencode 4.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,992 kB
  • sloc: ansic: 11,778; sh: 4,926; makefile: 112
file content (290 lines) | stat: -rw-r--r-- 5,857 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <stdio.h>
#include <stdlib.h>
#include "common.h"

static int tests = 0;
static int failed = 0;
int assertionFailed = 0;
int assertionNum = 0;
static const char *testName = NULL;
static const char *testFunc = NULL;

const char levelChar[4] = {'L', 'M', 'Q', 'H'};
const char *modeStr[5] = {"nm", "an", "8", "kj", "st"};

int ncmpBin(char *correct, BitStream *bstream, size_t len)
{
	int bit;
	size_t i;
	char *p;

	if(len != BitStream_size(bstream)) {
		printf("Length is not match: %zu, %zu expected.\n", BitStream_size(bstream), len);
		return -1;
	}

	p = correct;
	i = 0;
	while(*p != '\0') {
		while(*p == ' ') {
			p++;
		}
		bit = (*p == '1')?1:0;
		if(bstream->data[i] != bit) return -1;
		i++;
		p++;
		if(i == len) break;
	}

	return 0;
}

int cmpBin(char *correct, BitStream *bstream)
{
	size_t len = 0;
	char *p;


	for(p = correct; *p != '\0'; p++) {
		if(*p != ' ') len++;
	}
	return ncmpBin(correct, bstream, len);
}

void testInit(int tests)
{
	printf("1..%d\n", tests);
}

void testStartReal(const char *func, const char *name)
{
	tests++;
	testName = name;
	testFunc = func;
	assertionFailed = 0;
	assertionNum = 0;
	//printf("_____%d: %s: %s...\n", tests, func, name);
}

void testEnd(int result)
{
	if(result) {
		printf("not ok %d %s: %s\n", tests, testFunc, testName);
		failed++;
	} else {
		printf("ok %d %s: %s\n", tests, testFunc, testName);
	}
}

void testFinish(void)
{
	if(assertionFailed) {
		printf("not ok %d %s: %s (%d assertions failed.)\n", tests, testFunc, testName, assertionFailed);
		failed++;
	} else {
		printf("ok %d %s: %s (%d assertions passed.)\n", tests, testFunc, testName, assertionNum);
	}
}

void testReport(int expectedTests)
{
	printf("Total %d tests, %d fails.\n", tests, failed);
	if(failed) exit(-1);
	if(expectedTests != tests) {
		printf("WARNING: the number of the executed tests (%d) is not equal to the expecetd (%d).\n", tests, expectedTests);
	}
}

int testNum(void)
{
	return tests;
}

void printBinary(unsigned char *data, int length)
{
	int i;

	for(i=0; i<length; i++) {
		printf(data[i]?"1":"0");
	}
	printf("\n");
}

void printBstream(BitStream *bstream)
{
	printBinary(bstream->data, BitStream_size(bstream));
}

void printQRinput(QRinput *input)
{
	QRinput_List *list;
	int i;

	list = input->head;
	while(list != NULL) {
		for(i=0; i<list->size; i++) {
			printf("0x%02x,", list->data[i]);
		}
		list = list->next;
	}
	printf("\n");
}

void printQRinputInfo(QRinput *input)
{
	QRinput_List *list;
	BitStream *b;
	int i, ret;

	printf("QRinput info:\n");
	printf(" version: %d\n", input->version);
	printf(" level  : %c\n", levelChar[input->level]);
	list = input->head;
	i = 0;
	while(list != NULL) {
		i++;
		list = list->next;
	}
	printf("  chunks: %d\n", i);
	b = BitStream_new();
	ret = QRinput_mergeBitStream(input, b);
	if(ret == 0) {
		printf("  bitstream-size: %zu\n", BitStream_size(b));
		BitStream_free(b);
	}

	list = input->head;
	i = 0;
	while(list != NULL) {
		printf("\t#%d: mode = %s, size = %d\n", i, modeStr[list->mode], list->size);
		i++;
		list = list->next;
	}
}

void printQRinputStruct(QRinput_Struct *s)
{
	QRinput_InputList *list;
	int i = 1;

	printf("Struct size: %d\n", s->size);
	printf("Struct parity: %08x\n", s->parity);
	for(list = s->head; list != NULL; list = list->next) {
		printf("Symbol %d - ", i);
		printQRinputInfo(list->input);
		i++;
	}
}

void printFrame(int width, unsigned char *frame)
{
	int x, y;

	for(y=0; y<width; y++) {
		for(x=0; x<width; x++) {
			printf("%02x ", *frame++);
		}
		printf("\n");
	}
}

void printQRcode(QRcode *code)
{
	printFrame(code->width, code->data);
}

void printQRRawCodeFromQRinput(QRinput *input)
{
	QRRawCode *raw;
	int i;

	puts("QRRawCode dump image:");
	raw = QRraw_new(input);
	if(raw == NULL) {
		puts("Failed to generate QRRawCode from this input.\n");
		return;
	}
	for(i=0; i<raw->dataLength; i++) {
		printf(" %02x", raw->datacode[i]);
	}
	for(i=0; i<raw->eccLength; i++) {
		printf(" %02x", raw->ecccode[i]);
	}
	printf("\n");
	QRraw_free(raw);
}

#if HAVE_SDL
/* Experimental debug function */
/* You can call show_QRcode(QRcode *code) to display the QR Code from anywhere
 * in test code using SDL. */
#include <SDL.h>

static void draw_QRcode(QRcode *qrcode, int ox, int oy, int margin, int size, SDL_Surface *surface)
{
	int x, y, width;
	unsigned char *p;
	SDL_Rect rect;
	Uint32 color[2];

	color[0] = SDL_MapRGBA(surface->format, 255, 255, 255, 255);
	color[1] = SDL_MapRGBA(surface->format, 0, 0, 0, 255);
	SDL_FillRect(surface, NULL, color[0]);

	ox += margin * size;
	oy += margin * size;
	width = qrcode->width;
	p = qrcode->data;
	for(y=0; y<width; y++) {
		for(x=0; x<width; x++) {
			rect.x = ox + x * size;
			rect.y = oy + y * size;
			rect.w = size;
			rect.h = size;
			SDL_FillRect(surface, &rect, color[*p&1]);
			p++;
		}
	}
}

void show_QRcode(QRcode *qrcode)
{
	SDL_Event event;
	SDL_Window *window;
	SDL_Renderer *renderer;
	SDL_Surface *surface;
	SDL_Texture *texture;

	if(!SDL_WasInit(SDL_INIT_VIDEO)) {
		SDL_Init(SDL_INIT_VIDEO);
		atexit(SDL_Quit);
	}
	int width = (qrcode->width + 4 * 2) * 4; //maring = 4, size = 4
	SDL_CreateWindowAndRenderer(width, width, SDL_WINDOW_SHOWN, &window, &renderer);
	SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
	SDL_RenderClear(renderer);
	surface = SDL_CreateRGBSurface(0, width, width, 32, 0, 0, 0, 0);

	draw_QRcode(qrcode, 0, 0, 4, 4, surface);

	texture = SDL_CreateTextureFromSurface(renderer, surface);
	SDL_RenderCopy(renderer, texture, NULL, NULL);
	SDL_RenderPresent(renderer);
	fprintf(stderr, "Press any key on the QR Code window to proceed.\n");

	int loop = 1;
	while(loop) {
		SDL_WaitEvent(&event);
		if(event.type == SDL_KEYDOWN) {
			loop = 0;
		}
	}

	SDL_FreeSurface(surface);
	SDL_DestroyTexture(texture);
	SDL_DestroyRenderer(renderer);
}
#else
void show_QRcode(QRcode *qrcode) {
}
#endif