forked from fukuchi/libqrencode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qrenc.c
1159 lines (1030 loc) · 28 KB
/
qrenc.c
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* qrencode - QR Code encoder
*
* QR Code encoding tool
* Copyright (C) 2006-2013 Kentaro Fukuchi <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library 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 General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <png.h>
#include <getopt.h>
#include "qrencode.h"
#define INCHES_PER_METER (100.0/2.54)
static int casesensitive = 1;
static int eightbit = 0;
static int version = 0;
static int size = 3;
static int margin = -1;
static int dpi = 72;
static int structured = 0;
static int rle = 0;
static int micro = 0;
static QRecLevel level = QR_ECLEVEL_L;
static QRencodeMode hint = QR_MODE_8;
static unsigned int fg_color[4] = {0, 0, 0, 255};
static unsigned int bg_color[4] = {255, 255, 255, 255};
enum imageType {
PNG_TYPE,
EPS_TYPE,
SVG_TYPE,
ANSI_TYPE,
ANSI256_TYPE,
ASCII_TYPE,
ASCIIi_TYPE,
UTF8_TYPE,
ANSIUTF8_TYPE
};
static enum imageType image_type = PNG_TYPE;
static const struct option options[] = {
{"help" , no_argument , NULL, 'h'},
{"output" , required_argument, NULL, 'o'},
{"level" , required_argument, NULL, 'l'},
{"size" , required_argument, NULL, 's'},
{"symversion" , required_argument, NULL, 'v'},
{"margin" , required_argument, NULL, 'm'},
{"dpi" , required_argument, NULL, 'd'},
{"type" , required_argument, NULL, 't'},
{"structured" , no_argument , NULL, 'S'},
{"kanji" , no_argument , NULL, 'k'},
{"casesensitive", no_argument , NULL, 'c'},
{"ignorecase" , no_argument , NULL, 'i'},
{"8bit" , no_argument , NULL, '8'},
{"rle" , no_argument , &rle, 1},
{"micro" , no_argument , NULL, 'M'},
{"foreground" , required_argument, NULL, 'f'},
{"background" , required_argument, NULL, 'b'},
{"version" , no_argument , NULL, 'V'},
{NULL, 0, NULL, 0}
};
static char *optstring = "ho:l:s:v:m:d:t:Skci8MV";
static void usage(int help, int longopt)
{
fprintf(stderr,
"qrencode version %s\n"
"Copyright (C) 2006-2013 Kentaro Fukuchi\n", QRcode_APIVersionString());
if(help) {
if(longopt) {
fprintf(stderr,
"Usage: qrencode [OPTION]... [STRING]\n"
"Encode input data in a QR Code and save as a PNG or EPS image.\n\n"
" -h, --help display the help message. -h displays only the help of short\n"
" options.\n\n"
" -o FILENAME, --output=FILENAME\n"
" write image to FILENAME. If '-' is specified, the result\n"
" will be output to standard output. If -S is given, structured\n"
" symbols are written to FILENAME-01.png, FILENAME-02.png, ...\n"
" (suffix is removed from FILENAME, if specified)\n\n"
" -s NUMBER, --size=NUMBER\n"
" specify module size in dots (pixels). (default=3)\n\n"
" -l {LMQH}, --level={LMQH}\n"
" specify error correction level from L (lowest) to H (highest).\n"
" (default=L)\n\n"
" -v NUMBER, --symversion=NUMBER\n"
" specify the version of the symbol. (default=auto)\n\n"
" -m NUMBER, --margin=NUMBER\n"
" specify the width of the margins. (default=4 (2 for Micro)))\n\n"
" -d NUMBER, --dpi=NUMBER\n"
" specify the DPI of the generated PNG. (default=72)\n\n"
" -t {PNG,EPS,SVG,ANSI,ANSI256,ASCII,ASCIIi,UTF8,ANSIUTF8}, --type={...}\n"
" specify the type of the generated image. (default=PNG)\n\n"
" -S, --structured\n"
" make structured symbols. Version must be specified.\n\n"
" -k, --kanji assume that the input text contains kanji (shift-jis).\n\n"
" -c, --casesensitive\n"
" encode lower-case alphabet characters in 8-bit mode. (default)\n\n"
" -i, --ignorecase\n"
" ignore case distinctions and use only upper-case characters.\n\n"
" -8, --8bit encode entire data in 8-bit mode. -k, -c and -i will be ignored.\n\n"
" --rle enable run-length encoding for SVG.\n\n"
" -M, --micro encode in a Micro QR Code. (experimental)\n\n"
" --foreground=RRGGBB[AA]\n"
" --background=RRGGBB[AA]\n"
" specify foreground/background color in hexadecimal notation.\n"
" 6-digit (RGB) or 8-digit (RGBA) form are supported.\n"
" Color output support available only in PNG and SVG.\n\n"
" -V, --version\n"
" display the version number and copyrights of the qrencode.\n\n"
" [STRING] input data. If it is not specified, data will be taken from\n"
" standard input.\n"
);
} else {
fprintf(stderr,
"Usage: qrencode [OPTION]... [STRING]\n"
"Encode input data in a QR Code and save as a PNG or EPS image.\n\n"
" -h display this message.\n"
" --help display the usage of long options.\n"
" -o FILENAME write image to FILENAME. If '-' is specified, the result\n"
" will be output to standard output. If -S is given, structured\n"
" symbols are written to FILENAME-01.png, FILENAME-02.png, ...\n"
" (suffix is removed from FILENAME, if specified)\n"
" -s NUMBER specify module size in dots (pixels). (default=3)\n"
" -l {LMQH} specify error correction level from L (lowest) to H (highest).\n"
" (default=L)\n"
" -v NUMBER specify the version of the symbol. (default=auto)\n"
" -m NUMBER specify the width of the margins. (default=4 (2 for Micro))\n"
" -d NUMBER specify the DPI of the generated PNG. (default=72)\n"
" -t {PNG,EPS,SVG,ANSI,ANSI256,ASCII,ASCIIi,UTF8,ANSIUTF8}\n"
" specify the type of the generated image. (default=PNG)\n"
" -S make structured symbols. Version must be specified.\n"
" -k assume that the input text contains kanji (shift-jis).\n"
" -c encode lower-case alphabet characters in 8-bit mode. (default)\n"
" -i ignore case distinctions and use only upper-case characters.\n"
" -8 encode entire data in 8-bit mode. -k, -c and -i will be ignored.\n"
" -M encode in a Micro QR Code.\n"
" -V display the version number and copyrights of the qrencode.\n"
" [STRING] input data. If it is not specified, data will be taken from\n"
" standard input.\n\n"
" Try \"qrencode --help\" for more options.\n"
);
}
}
}
static int color_set(unsigned int color[4], const char *value)
{
int len = strlen(value);
int count;
if(len == 6) {
count = sscanf(value, "%02x%02x%02x%n", &color[0], &color[1], &color[2], &len);
if(count < 3 || len != 6) {
return -1;
}
color[3] = 255;
} else if(len == 8) {
count = sscanf(value, "%02x%02x%02x%02x%n", &color[0], &color[1], &color[2], &color[3], &len);
if(count < 4 || len != 8) {
return -1;
}
} else {
return -1;
}
return 0;
}
#define MAX_DATA_SIZE (7090 * 16) /* from the specification */
static unsigned char *readStdin(int *length)
{
unsigned char *buffer;
int ret;
buffer = (unsigned char *)malloc(MAX_DATA_SIZE + 1);
if(buffer == NULL) {
fprintf(stderr, "Memory allocation failed.\n");
exit(EXIT_FAILURE);
}
ret = fread(buffer, 1, MAX_DATA_SIZE, stdin);
if(ret == 0) {
fprintf(stderr, "No input data.\n");
exit(EXIT_FAILURE);
}
if(feof(stdin) == 0) {
fprintf(stderr, "Input data is too large.\n");
exit(EXIT_FAILURE);
}
buffer[ret] = '\0';
*length = ret;
return buffer;
}
static FILE *openFile(const char *outfile)
{
FILE *fp;
if(outfile == NULL || (outfile[0] == '-' && outfile[1] == '\0')) {
fp = stdout;
} else {
fp = fopen(outfile, "wb");
if(fp == NULL) {
fprintf(stderr, "Failed to create file: %s\n", outfile);
perror(NULL);
exit(EXIT_FAILURE);
}
}
return fp;
}
static int writePNG(QRcode *qrcode, const char *outfile)
{
static FILE *fp; // avoid clobbering by setjmp.
png_structp png_ptr;
png_infop info_ptr;
png_colorp palette;
png_byte alpha_values[2];
unsigned char *row, *p, *q;
int x, y, xx, yy, bit;
int realwidth;
realwidth = (qrcode->width + margin * 2) * size;
row = (unsigned char *)malloc((realwidth + 7) / 8);
if(row == NULL) {
fprintf(stderr, "Failed to allocate memory.\n");
exit(EXIT_FAILURE);
}
if(outfile[0] == '-' && outfile[1] == '\0') {
fp = stdout;
} else {
fp = fopen(outfile, "wb");
if(fp == NULL) {
fprintf(stderr, "Failed to create file: %s\n", outfile);
perror(NULL);
exit(EXIT_FAILURE);
}
}
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(png_ptr == NULL) {
fprintf(stderr, "Failed to initialize PNG writer.\n");
exit(EXIT_FAILURE);
}
info_ptr = png_create_info_struct(png_ptr);
if(info_ptr == NULL) {
fprintf(stderr, "Failed to initialize PNG write.\n");
exit(EXIT_FAILURE);
}
if(setjmp(png_jmpbuf(png_ptr))) {
png_destroy_write_struct(&png_ptr, &info_ptr);
fprintf(stderr, "Failed to write PNG image.\n");
exit(EXIT_FAILURE);
}
palette = (png_colorp) malloc(sizeof(png_color) * 2);
if(palette == NULL) {
fprintf(stderr, "Failed to allocate memory.\n");
exit(EXIT_FAILURE);
}
palette[0].red = fg_color[0];
palette[0].green = fg_color[1];
palette[0].blue = fg_color[2];
palette[1].red = bg_color[0];
palette[1].green = bg_color[1];
palette[1].blue = bg_color[2];
alpha_values[0] = fg_color[3];
alpha_values[1] = bg_color[3];
png_set_PLTE(png_ptr, info_ptr, palette, 2);
png_set_tRNS(png_ptr, info_ptr, alpha_values, 2, NULL);
png_init_io(png_ptr, fp);
png_set_IHDR(png_ptr, info_ptr,
realwidth, realwidth,
1,
PNG_COLOR_TYPE_PALETTE,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_set_pHYs(png_ptr, info_ptr,
dpi * INCHES_PER_METER,
dpi * INCHES_PER_METER,
PNG_RESOLUTION_METER);
png_write_info(png_ptr, info_ptr);
/* top margin */
memset(row, 0xff, (realwidth + 7) / 8);
for(y=0; y<margin * size; y++) {
png_write_row(png_ptr, row);
}
/* data */
p = qrcode->data;
for(y=0; y<qrcode->width; y++) {
bit = 7;
memset(row, 0xff, (realwidth + 7) / 8);
q = row;
q += margin * size / 8;
bit = 7 - (margin * size % 8);
for(x=0; x<qrcode->width; x++) {
for(xx=0; xx<size; xx++) {
*q ^= (*p & 1) << bit;
bit--;
if(bit < 0) {
q++;
bit = 7;
}
}
p++;
}
for(yy=0; yy<size; yy++) {
png_write_row(png_ptr, row);
}
}
/* bottom margin */
memset(row, 0xff, (realwidth + 7) / 8);
for(y=0; y<margin * size; y++) {
png_write_row(png_ptr, row);
}
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
free(row);
free(palette);
return 0;
}
static int writeEPS(QRcode *qrcode, const char *outfile)
{
FILE *fp;
unsigned char *row, *p;
int x, y, yy;
int realwidth;
fp = openFile(outfile);
realwidth = (qrcode->width + margin * 2) * size;
/* EPS file header */
fprintf(fp, "%%!PS-Adobe-2.0 EPSF-1.2\n"
"%%%%BoundingBox: 0 0 %d %d\n"
"%%%%Pages: 1 1\n"
"%%%%EndComments\n", realwidth, realwidth);
/* draw point */
fprintf(fp, "/p { "
"moveto "
"0 1 rlineto "
"1 0 rlineto "
"0 -1 rlineto "
"fill "
"} bind def "
"%d %d scale ", size, size);
/* data */
p = qrcode->data;
for(y=0; y<qrcode->width; y++) {
row = (p+(y*qrcode->width));
yy = (margin + qrcode->width - y - 1);
for(x=0; x<qrcode->width; x++) {
if(*(row+x)&0x1) {
fprintf(fp, "%d %d p ", margin + x, yy);
}
}
}
fprintf(fp, "\n%%%%EOF\n");
fclose(fp);
return 0;
}
static void writeSVG_writeRect(FILE *fp, int x, int y, int width, char* col, float opacity)
{
if(fg_color[3] != 255) {
fprintf(fp, "\t\t\t<rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"1\" "\
"fill=\"#%s\" fill-opacity=\"%f\" />\n",
x, y, width, col, opacity );
} else {
fprintf(fp, "\t\t\t<rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"1\" "\
"fill=\"#%s\" />\n",
x, y, width, col );
}
}
static int writeSVG( QRcode *qrcode, const char *outfile )
{
FILE *fp;
unsigned char *row, *p;
int x, y, x0, pen;
int symwidth, realwidth;
float scale;
char fg[7], bg[7];
float fg_opacity;
float bg_opacity;
fp = openFile(outfile);
scale = dpi * INCHES_PER_METER / 100.0;
symwidth = qrcode->width + margin * 2;
realwidth = symwidth * size;
snprintf(fg, 7, "%02x%02x%02x", fg_color[0], fg_color[1], fg_color[2]);
snprintf(bg, 7, "%02x%02x%02x", bg_color[0], bg_color[1], bg_color[2]);
fg_opacity = (float)fg_color[3] / 255;
bg_opacity = (float)bg_color[3] / 255;
/* XML declaration */
fputs( "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n", fp );
/* DTD
No document type specified because "while a DTD is provided in [the SVG]
specification, the use of DTDs for validating XML documents is known to be
problematic. In particular, DTDs do not handle namespaces gracefully. It
is *not* recommended that a DOCTYPE declaration be included in SVG
documents."
http://www.w3.org/TR/2003/REC-SVG11-20030114/intro.html#Namespace
*/
/* Vanity remark */
fprintf( fp, "<!-- Created with qrencode %s (http://fukuchi.org/works/qrencode/index.html.en) -->\n",
QRcode_APIVersionString() );
/* SVG code start */
fprintf( fp, "<svg width=\"%0.2fcm\" height=\"%0.2fcm\" viewBox=\"0 0 %d %d\""\
" preserveAspectRatio=\"none\" version=\"1.1\""\
" xmlns=\"http://www.w3.org/2000/svg\">\n",
realwidth / scale, realwidth / scale, symwidth, symwidth
);
/* Make named group */
fputs( "\t<g id=\"QRcode\">\n", fp );
/* Make solid background */
if(bg_color[3] != 255) {
fprintf(fp, "\t\t<rect x=\"0\" y=\"0\" width=\"%d\" height=\"%d\" fill=\"#%s\" fill-opacity=\"%f\" />\n", symwidth, symwidth, bg, bg_opacity);
} else {
fprintf(fp, "\t\t<rect x=\"0\" y=\"0\" width=\"%d\" height=\"%d\" fill=\"#%s\" />\n", symwidth, symwidth, bg);
}
/* Create new viewbox for QR data */
fputs( "\t\t<g id=\"Pattern\">\n", fp);
/* Write data */
p = qrcode->data;
for(y=0; y<qrcode->width; y++) {
row = (p+(y*qrcode->width));
if( !rle ) {
/* no RLE */
for(x=0; x<qrcode->width; x++) {
if(*(row+x)&0x1) {
writeSVG_writeRect(fp, margin + x,
margin + y, 1,
fg, fg_opacity);
}
}
} else {
/* simple RLE */
pen = 0;
x0 = 0;
for(x=0; x<qrcode->width; x++) {
if( !pen ) {
pen = *(row+x)&0x1;
x0 = x;
} else {
if(!(*(row+x)&0x1)) {
writeSVG_writeRect(fp, x0 + margin, y + margin, x-x0, fg, fg_opacity);
pen = 0;
}
}
}
if( pen ) {
writeSVG_writeRect(fp, x0 + margin, y + margin, qrcode->width - x0, fg, fg_opacity);
}
}
}
/* Close QR data viewbox */
fputs( "\t\t</g>\n", fp );
/* Close group */
fputs( "\t</g>\n", fp );
/* Close SVG code */
fputs( "</svg>\n", fp );
fclose( fp );
return 0;
}
static void writeANSI_margin(FILE* fp, int realwidth,
char* buffer, int buffer_s,
char* white, int white_s )
{
int y;
strncpy(buffer, white, white_s);
memset(buffer + white_s, ' ', realwidth * 2);
strcpy(buffer + white_s + realwidth * 2, "\033[0m\n"); // reset to default colors
for(y=0; y<margin; y++ ){
fputs(buffer, fp);
}
}
static int writeANSI(QRcode *qrcode, const char *outfile)
{
FILE *fp;
unsigned char *row, *p;
int x, y;
int realwidth;
int last;
char *white, *black, *buffer;
int white_s, black_s, buffer_s;
if( image_type == ANSI256_TYPE ){
/* codes for 256 color compatible terminals */
white = "\033[48;5;231m";
white_s = 11;
black = "\033[48;5;16m";
black_s = 10;
} else {
white = "\033[47m";
white_s = 5;
black = "\033[40m";
black_s = 5;
}
size = 1;
fp = openFile(outfile);
realwidth = (qrcode->width + margin * 2) * size;
buffer_s = ( realwidth * white_s ) * 2;
buffer = (char *)malloc( buffer_s );
if(buffer == NULL) {
fprintf(stderr, "Failed to allocate memory.\n");
exit(EXIT_FAILURE);
}
/* top margin */
writeANSI_margin(fp, realwidth, buffer, buffer_s, white, white_s);
/* data */
p = qrcode->data;
for(y=0; y<qrcode->width; y++) {
row = (p+(y*qrcode->width));
memset(buffer, 0, buffer_s);
strncpy( buffer, white, white_s );
for(x=0; x<margin; x++ ){
strncat( buffer, " ", 2 );
}
last = 0;
for(x=0; x<qrcode->width; x++) {
if(*(row+x)&0x1) {
if( last != 1 ){
strncat( buffer, black, black_s );
last = 1;
}
} else {
if( last != 0 ){
strncat( buffer, white, white_s );
last = 0;
}
}
strncat( buffer, " ", 2 );
}
if( last != 0 ){
strncat( buffer, white, white_s );
}
for(x=0; x<margin; x++ ){
strncat( buffer, " ", 2 );
}
strncat( buffer, "\033[0m\n", 5 );
fputs( buffer, fp );
}
/* bottom margin */
writeANSI_margin(fp, realwidth, buffer, buffer_s, white, white_s);
fclose(fp);
free(buffer);
return 0;
}
static void writeUTF8_margin(FILE* fp, int realwidth,
const char* white, const char *reset,
int use_ansi)
{
int x, y;
for (y = 0; y < margin/2; y++) {
fputs(white, fp);
for (x = 0; x < realwidth; x++)
fputs("\342\226\210", fp);
fputs(reset, fp);
fputc('\n', fp);
}
}
static int writeUTF8(QRcode *qrcode, const char *outfile, int use_ansi)
{
FILE *fp;
int x, y;
int realwidth;
const char *white, *reset;
if (use_ansi){
white = "\033[40;37;1m";
reset = "\033[0m";
} else {
white = "";
reset = "";
}
fp = openFile(outfile);
realwidth = (qrcode->width + margin * 2);
/* top margin */
writeUTF8_margin(fp, realwidth, white, reset, use_ansi);
/* data */
for(y = 0; y < qrcode->width; y += 2) {
unsigned char *row1, *row2;
row1 = qrcode->data + y*qrcode->width;
row2 = row1 + qrcode->width;
fputs(white, fp);
for (x = 0; x < margin; x++)
fputs("\342\226\210", fp);
for (x = 0; x < qrcode->width; x++) {
if(row1[x] & 1) {
if(y < qrcode->width - 1 && row2[x] & 1) {
fputc(' ', fp);
} else {
fputs("\342\226\204", fp);
}
} else {
if(y < qrcode->width - 1 && row2[x] & 1) {
fputs("\342\226\200", fp);
} else {
fputs("\342\226\210", fp);
}
}
}
for (x = 0; x < margin; x++)
fputs("\342\226\210", fp);
fputs(reset, fp);
fputc('\n', fp);
}
/* bottom margin */
writeUTF8_margin(fp, realwidth, white, reset, use_ansi);
fclose(fp);
return 0;
}
static void writeASCII_margin(FILE* fp, int realwidth, char* buffer, int buffer_s, int invert)
{
int y, h;
h = margin;
memset(buffer, (invert?'#':' '), realwidth);
buffer[realwidth] = '\n';
buffer[realwidth + 1] = '\0';
for(y=0; y<h; y++ ){
fputs(buffer, fp);
}
}
static int writeASCII(QRcode *qrcode, const char *outfile, int invert)
{
FILE *fp;
unsigned char *row;
int x, y;
int realwidth;
char *buffer, *p;
int buffer_s;
char black = '#';
char white = ' ';
if(invert) {
black = ' ';
white = '#';
}
size = 1;
fp = openFile(outfile);
realwidth = (qrcode->width + margin * 2) * 2;
buffer_s = realwidth + 2;
buffer = (char *)malloc( buffer_s );
if(buffer == NULL) {
fprintf(stderr, "Failed to allocate memory.\n");
exit(EXIT_FAILURE);
}
/* top margin */
writeASCII_margin(fp, realwidth, buffer, buffer_s, invert);
/* data */
for(y=0; y<qrcode->width; y++) {
row = qrcode->data+(y*qrcode->width);
p = buffer;
memset(p, white, margin * 2);
p += margin * 2;
for(x=0; x<qrcode->width; x++) {
if(row[x]&0x1) {
*p++ = black;
*p++ = black;
} else {
*p++ = white;
*p++ = white;
}
}
memset(p, white, margin * 2);
p += margin * 2;
*p++ = '\n';
*p++ = '\0';
fputs( buffer, fp );
}
/* bottom margin */
writeASCII_margin(fp, realwidth, buffer, buffer_s, invert);
fclose(fp);
free(buffer);
return 0;
}
static QRcode *encode(const unsigned char *intext, int length)
{
QRcode *code;
if(micro) {
if(eightbit) {
code = QRcode_encodeDataMQR(length, intext, version, level);
} else {
code = QRcode_encodeStringMQR((char *)intext, version, level, hint, casesensitive);
}
} else {
if(eightbit) {
code = QRcode_encodeData(length, intext, version, level);
} else {
code = QRcode_encodeString((char *)intext, version, level, hint, casesensitive);
}
}
return code;
}
static void qrencode(const unsigned char *intext, int length, const char *outfile)
{
QRcode *qrcode;
qrcode = encode(intext, length);
if(qrcode == NULL) {
perror("Failed to encode the input data");
exit(EXIT_FAILURE);
}
switch(image_type) {
case PNG_TYPE:
writePNG(qrcode, outfile);
break;
case EPS_TYPE:
writeEPS(qrcode, outfile);
break;
case SVG_TYPE:
writeSVG(qrcode, outfile);
break;
case ANSI_TYPE:
case ANSI256_TYPE:
writeANSI(qrcode, outfile);
break;
case ASCIIi_TYPE:
writeASCII(qrcode, outfile, 1);
break;
case ASCII_TYPE:
writeASCII(qrcode, outfile, 0);
break;
case UTF8_TYPE:
writeUTF8(qrcode, outfile, 0);
break;
case ANSIUTF8_TYPE:
writeUTF8(qrcode, outfile, 1);
break;
default:
fprintf(stderr, "Unknown image type.\n");
exit(EXIT_FAILURE);
}
QRcode_free(qrcode);
}
static QRcode_List *encodeStructured(const unsigned char *intext, int length)
{
QRcode_List *list;
if(eightbit) {
list = QRcode_encodeDataStructured(length, intext, version, level);
} else {
list = QRcode_encodeStringStructured((char *)intext, version, level, hint, casesensitive);
}
return list;
}
static void qrencodeStructured(const unsigned char *intext, int length, const char *outfile)
{
QRcode_List *qrlist, *p;
char filename[FILENAME_MAX];
char *base, *q, *suffix = NULL;
const char *type_suffix;
int i = 1;
size_t suffix_size;
switch(image_type) {
case PNG_TYPE:
type_suffix = ".png";
break;
case EPS_TYPE:
type_suffix = ".eps";
break;
case SVG_TYPE:
type_suffix = ".svg";
break;
case ANSI_TYPE:
case ANSI256_TYPE:
case ASCII_TYPE:
case UTF8_TYPE:
case ANSIUTF8_TYPE:
type_suffix = ".txt";
break;
default:
fprintf(stderr, "Unknown image type.\n");
exit(EXIT_FAILURE);
}
if(outfile == NULL) {
fprintf(stderr, "An output filename must be specified to store the structured images.\n");
exit(EXIT_FAILURE);
}
base = strdup(outfile);
if(base == NULL) {
fprintf(stderr, "Failed to allocate memory.\n");
exit(EXIT_FAILURE);
}
suffix_size = strlen(type_suffix);
if(strlen(base) > suffix_size) {
q = base + strlen(base) - suffix_size;
if(strcasecmp(type_suffix, q) == 0) {
suffix = strdup(q);
*q = '\0';
}
}
qrlist = encodeStructured(intext, length);
if(qrlist == NULL) {
perror("Failed to encode the input data");
exit(EXIT_FAILURE);
}
for(p = qrlist; p != NULL; p = p->next) {
if(p->code == NULL) {
fprintf(stderr, "Failed to encode the input data.\n");
exit(EXIT_FAILURE);
}
if(suffix) {
snprintf(filename, FILENAME_MAX, "%s-%02d%s", base, i, suffix);
} else {
snprintf(filename, FILENAME_MAX, "%s-%02d", base, i);
}
switch(image_type) {
case PNG_TYPE:
writePNG(p->code, filename);
break;
case EPS_TYPE:
writeEPS(p->code, filename);
break;
case SVG_TYPE:
writeSVG(p->code, filename);
break;
case ANSI_TYPE:
case ANSI256_TYPE:
writeANSI(p->code, filename);
break;
case ASCIIi_TYPE:
writeASCII(p->code, filename, 1);
break;
case ASCII_TYPE:
writeASCII(p->code, filename, 0);
break;
case UTF8_TYPE:
writeUTF8(p->code, filename, 0);
break;
case ANSIUTF8_TYPE:
writeUTF8(p->code, filename, 0);
break;
default:
fprintf(stderr, "Unknown image type.\n");
exit(EXIT_FAILURE);
}
i++;
}
free(base);
if(suffix) {
free(suffix);
}
QRcode_List_free(qrlist);
}
int main(int argc, char **argv)
{
int opt, lindex = -1;
char *outfile = NULL;
unsigned char *intext = NULL;
int length = 0;
while((opt = getopt_long(argc, argv, optstring, options, &lindex)) != -1) {
switch(opt) {
case 'h':
if(lindex == 0) {
usage(1, 1);
} else {
usage(1, 0);
}
exit(EXIT_SUCCESS);
break;
case 'o':
outfile = optarg;
break;
case 's':
size = atoi(optarg);
if(size <= 0) {
fprintf(stderr, "Invalid size: %d\n", size);
exit(EXIT_FAILURE);
}
break;
case 'v':
version = atoi(optarg);
if(version < 0) {
fprintf(stderr, "Invalid version: %d\n", version);
exit(EXIT_FAILURE);
}
break;
case 'l':
switch(*optarg) {
case 'l':
case 'L':
level = QR_ECLEVEL_L;
break;