summaryrefslogtreecommitdiffstats
path: root/test/test_proc.c
blob: 46eb99211b86076e60c344ce8163a196b57b0764 (plain)
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
/*------------------------------------------------------------------------
 *  Copyright 2007-2009 (c) Jeff Brown <spadix@users.sourceforge.net>
 *
 *  This file is part of the ZBar Bar Code Reader.
 *
 *  The ZBar Bar Code Reader is free software; you can redistribute it
 *  and/or modify it under the terms of the GNU Lesser Public License as
 *  published by the Free Software Foundation; either version 2.1 of
 *  the License, or (at your option) any later version.
 *
 *  The ZBar Bar Code Reader is distributed in the hope that it will be
 *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 *  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser Public License
 *  along with the ZBar Bar Code Reader; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 *  Boston, MA  02110-1301  USA
 *
 *  http://sourceforge.net/projects/zbar
 *------------------------------------------------------------------------*/

#include "config.h"
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <zbar.h>
#include "test_images.h"

zbar_processor_t *proc = NULL;
int use_threads = 1, use_window = 1;

int input_wait(int timeout)
{
    if (timeout >= 0)
	fprintf(stderr, "waiting %d.%03ds for input...", timeout / 1000,
		timeout % 1000);
    else
	fprintf(stderr, "waiting indefinitely for input...");
    fflush(stderr);
    int rc = zbar_processor_user_wait(proc, timeout);
    if (rc > 0)
	fprintf(stderr, "got input (%02x)\n", rc);
    else if (!rc)
	fprintf(stderr, "timed out\n");
    else if (zbar_processor_get_error_code(proc) == ZBAR_ERR_CLOSED)
	use_window = rc = 0;
    fflush(stderr);
    return (rc);
}

int main(int argc, char **argv)
{
    zbar_set_verbosity(127);
    char *video_dev = NULL;
    uint32_t fmt    = 0;
    int i, j = 0;
    for (i = 1; i < argc; i++) {
	if (argv[i][0] == '-') {
	    if (!strncmp(argv[i], "-thr", 4))
		use_threads = 0;
	    else if (!strncmp(argv[i], "-win", 4))
		use_window = 0;
	    else
		return (1);
	} else if (!j++)
	    video_dev = argv[i];
	else if (j++ == 1) {
	    int n = strlen(argv[i]);
	    if (n > 4)
		n = 4;
	    memcpy((char *)&fmt, argv[i], n);
	}
    }
    if (!fmt)
	fmt = fourcc('B', 'G', 'R', '3');

    proc = zbar_processor_create(use_threads);
    assert(proc);
    fprintf(stderr, "created processor (%sthreaded)\n",
	    (!use_threads) ? "un" : "");
    fflush(stderr);

    if (zbar_processor_init(proc, NULL, 0))
	return (2);
    fprintf(stderr, "initialized (video=disabled, window=disabled)\n");
    fflush(stderr);

    zbar_image_t *img = zbar_image_create();
    zbar_image_set_size(img, 640, 480);
    zbar_image_set_format(img, fmt);
    test_image_bars(img);

    if (zbar_process_image(proc, img) < 0)
	return (3);
    fprintf(stderr, "processed test image\n");
    fflush(stderr);

    if (zbar_processor_init(proc, video_dev, use_window))
	return (2);
    fprintf(stderr, "reinitialized (video=%s, window=%s)\n",
	    (video_dev) ? video_dev : "disabled",
	    (use_window) ? "enabled" : "disabled");
    fflush(stderr);

    if (use_window) {
	if (zbar_processor_set_visible(proc, 1))
	    return (4);
	fprintf(stderr, "window visible\n");
	fflush(stderr);
    }

    if (input_wait((use_window && !video_dev) ? -1 : 2000) < 0)
	return (5);

    if (zbar_process_image(proc, img) < 0)
	return (3);
    fprintf(stderr, "processed test image\n");
    fflush(stderr);
    zbar_image_destroy(img);

    if (input_wait((use_window && !video_dev) ? -1 : 3333) < 0)
	return (5);

    if (video_dev) {
	if (zbar_processor_set_active(proc, 1))
	    return (3);
	fprintf(stderr, "video activated\n");
	fflush(stderr);

	if (input_wait((use_window) ? -1 : 4000) < 0)
	    return (5);

	if (zbar_processor_set_active(proc, 0))
	    return (3);
	fprintf(stderr, "video deactivated\n");
	fflush(stderr);

	if (input_wait((use_window) ? -1 : 4000) < 0)
	    return (5);

	/* FIXME test process_one() */
    }

    if (zbar_process_image(proc, NULL))
	return (3);
    fprintf(stderr, "flushed image\n");
    fflush(stderr);

    if (input_wait((use_window && !video_dev) ? -1 : 2500) < 0)
	return (5);

    fprintf(stderr, "cleaning up...\n");
    fflush(stderr);

    zbar_processor_destroy(proc);
    proc = NULL;
    if (test_image_check_cleanup())
	return (32);
    return (0);
}