9 #include "../qrinput.h"
11 #include "../qrencode_inner.h"
13 static SDL_Surface *screen = NULL;
14 static int casesensitive = 1;
15 static int eightbit = 0;
16 static int version = 1;
18 static int margin = 4;
19 static int structured = 0;
20 static QRecLevel level = QR_ECLEVEL_L;
21 static QRencodeMode hint = QR_MODE_8;
26 static const struct option options[] = {
27 {"help" , no_argument , NULL, 'h'},
28 {"level" , required_argument, NULL, 'l'},
29 {"size" , required_argument, NULL, 's'},
30 {"symversion" , required_argument, NULL, 'v'},
31 {"margin" , required_argument, NULL, 'm'},
32 {"structured" , no_argument , NULL, 'S'},
33 {"kanji" , no_argument , NULL, 'k'},
34 {"casesensitive", no_argument , NULL, 'c'},
35 {"ignorecase" , no_argument , NULL, 'i'},
36 {"8bit" , no_argument , NULL, '8'},
37 {"version" , no_argument , NULL, 'V'},
41 static char *optstring = "ho:l:s:v:m:Skci8V";
43 static char levelChar[4] = {'L', 'M', 'Q', 'H'};
44 static void usage(int help, int longopt)
47 "view_qrcode version %s\n"
48 "Copyright (C) 2008, 2009 Kentaro Fukuchi\n", VERSION);
52 "Usage: view_qrcode [OPTION]... [STRING]\n"
53 "Encode input data in a QR Code and display.\n\n"
54 " -h, --help display the help message. -h displays only the help of short\n"
56 " -s NUMBER, --size=NUMBER\n"
57 " specify the size of dot (pixel). (default=3)\n\n"
58 " -l {LMQH}, --level={LMQH}\n"
59 " specify error collectin level from L (lowest) to H (highest).\n"
61 " -v NUMBER, --symversion=NUMBER\n"
62 " specify the version of the symbol. (default=auto)\n\n"
63 " -m NUMBER, --margin=NUMBER\n"
64 " specify the width of margin. (default=4)\n\n"
66 " make structured symbols. Version must be specified.\n\n"
67 " -k, --kanji assume that the input text contains kanji (shift-jis).\n\n"
68 " -c, --casesensitive\n"
69 " encode lower-case alphabet characters in 8-bit mode. (default)\n\n"
71 " ignore case distinctions and use only upper-case characters.\n\n"
72 " -8, -8bit encode entire data in 8-bit mode. -k, -c and -i will be ignored.\n\n"
74 " display the version number and copyrights of the qrencode.\n\n"
75 " [STRING] input data. If it is not specified, data will be taken from\n"
80 "Usage: view_qrcode [OPTION]... [STRING]\n"
81 "Encode input data in a QR Code and display.\n\n"
82 " -h display this message.\n"
83 " --help display the usage of long options.\n"
84 " -s NUMBER specify the size of dot (pixel). (default=3)\n"
85 " -l {LMQH} specify error collectin level from L (lowest) to H (highest).\n"
87 " -v NUMBER specify the version of the symbol. (default=auto)\n"
88 " -m NUMBER specify the width of margin. (default=4)\n"
89 " -S make structured symbols. Version must be specified.\n"
90 " -k assume that the input text contains kanji (shift-jis).\n"
91 " -c encode lower-case alphabet characters in 8-bit mode. (default)\n"
92 " -i ignore case distinctions and use only upper-case characters.\n"
93 " -8 encode entire data in 8-bit mode. -k, -c and -i will be ignored.\n"
94 " -V display the version number and copyrights of the qrencode.\n"
95 " [STRING] input data. If it is not specified, data will be taken from\n"
102 #define MAX_DATA_SIZE (7090 * 16) /* from the specification */
103 static char *readStdin(void)
108 buffer = (char *)malloc(MAX_DATA_SIZE);
109 ret = fread(buffer, 1, MAX_DATA_SIZE, stdin);
111 fprintf(stderr, "No input data.\n");
115 fprintf(stderr, "Input data is too large.\n");
124 static void draw_QRcode(QRcode *qrcode, int ox, int oy)
132 width = qrcode->width;
134 for(y=0; y<width; y++) {
135 for(x=0; x<width; x++) {
136 rect.x = ox + x * size;
137 rect.y = oy + y * size;
140 SDL_FillRect(screen, &rect, (*p&1)?0:0xffffff);
146 void draw_singleQRcode(QRinput *stream, int mask)
151 QRinput_setVersion(stream, version);
152 QRinput_setErrorCorrectionLevel(stream, level);
153 qrcode = QRcode_encodeMask(stream, mask);
154 if(qrcode == NULL) return;
156 version = qrcode->version;
157 width = (qrcode->width + margin * 2) * size;
159 screen = SDL_SetVideoMode(width, width, 32, 0);
160 SDL_FillRect(screen, NULL, 0xffffff);
161 draw_QRcode(qrcode, 0, 0);
166 void draw_structuredQRcode(QRinput_Struct *s)
168 int i, w, h, n, x, y;
170 QRcode_List *qrcodes, *p;
172 qrcodes = QRcode_encodeInputStructured(s);
173 if(qrcodes == NULL) return;
175 swidth = (qrcodes->code->width + margin * 2) * size;
176 n = QRcode_List_size(qrcodes);
180 screen = SDL_SetVideoMode(swidth * w, swidth * h, 32, 0);
181 SDL_FillRect(screen, NULL, 0xffffff);
185 x = (i % 4) * swidth;
186 y = (i / 4) * swidth;
187 draw_QRcode(p->code, x, y);
191 QRcode_List_free(qrcodes);
194 void draw_structuredQRcodeFromText(int argc, char **argv)
200 s = QRinput_Struct_new();
202 fprintf(stderr, "Failed to allocate memory.\n");
205 for(i=0; i<argc; i++) {
206 input = QRinput_new2(version, level);
208 fprintf(stderr, "Failed to allocate memory.\n");
212 ret = QRinput_append(input, QR_MODE_8, strlen(argv[i]), (unsigned char *)argv[i]);
214 ret = Split_splitStringToQRinput(argv[i], input, hint, casesensitive);
217 perror("Encoding the input string");
220 ret = QRinput_Struct_appendInput(s, input);
222 perror("Encoding the input string");
226 ret = QRinput_Struct_insertStructuredAppendHeaders(s);
228 fprintf(stderr, "Too many inputs.\n");
231 draw_structuredQRcode(s);
232 QRinput_Struct_free(s);
235 void draw_structuredQRcodeFromQRinput(QRinput *stream)
239 QRinput_setVersion(stream, version);
240 QRinput_setErrorCorrectionLevel(stream, level);
241 s = QRinput_splitQRinputToStruct(stream);
243 draw_structuredQRcode(s);
244 QRinput_Struct_free(s);
246 fprintf(stderr, "Input data is too large for this setting.\n");
250 void view(int mode, QRinput *input)
259 draw_structuredQRcodeFromText(textc, textv);
262 draw_structuredQRcodeFromQRinput(input);
264 draw_singleQRcode(input, mask);
267 if(mode || structured) {
268 printf("Version %d, Level %c.\n", version, levelChar[level]);
270 printf("Version %d, Level %c, Mask %d.\n", version, levelChar[level], mask);
275 while(SDL_PollEvent(&event)) {
276 if(event.type == SDL_KEYDOWN) {
277 switch(event.key.keysym.sym) {
280 if(version > QRSPEC_VERSION_MAX)
281 version = QRSPEC_VERSION_MAX;
296 if(size < 1) size = 1;
307 if(!mode && !structured) {
308 mask = (event.key.keysym.sym - SDLK_0);
313 if(!mode && !structured) {
319 level = QR_ECLEVEL_L;
323 level = QR_ECLEVEL_M;
327 level = QR_ECLEVEL_H;
331 level = QR_ECLEVEL_Q;
342 if(event.type == SDL_QUIT) {
351 void view_simple(const char *str)
356 input = QRinput_new2(version, level);
358 fprintf(stderr, "Memory allocation error.\n");
362 ret = QRinput_append(input, QR_MODE_8, strlen(str), (unsigned char *)str);
364 ret = Split_splitStringToQRinput(str, input, hint, casesensitive);
367 perror("Encoding the input string");
376 void view_multiText(char **argv, int argc)
384 int main(int argc, char **argv)
386 int opt, lindex = -1;
389 while((opt = getopt_long(argc, argv, optstring, options, &lindex)) != -1) {
402 fprintf(stderr, "Invalid size: %d\n", size);
407 version = atoi(optarg);
409 fprintf(stderr, "Invalid version: %d\n", version);
417 level = QR_ECLEVEL_L;
421 level = QR_ECLEVEL_M;
425 level = QR_ECLEVEL_Q;
429 level = QR_ECLEVEL_H;
432 fprintf(stderr, "Invalid level: %s\n", optarg);
438 margin = atoi(optarg);
440 fprintf(stderr, "Invalid margin: %d\n", margin);
447 hint = QR_MODE_KANJI;
463 fprintf(stderr, "Try `view_qrcode --help' for more information.\n");
474 intext = argv[optind];
477 intext = readStdin();
480 if(SDL_Init(SDL_INIT_VIDEO) < 0) {
481 fprintf(stderr, "Failed initializing SDL: %s\n", SDL_GetError());
484 if(structured && version < 1) {
485 fprintf(stderr, "Version number must be greater than 0 to encode structured symbols.\n");
488 if(structured && (argc - optind > 1)) {
489 view_multiText(argv + optind, argc - optind);