2 * This tool creates a frame pattern data for debug purpose used by
3 * test_qrspec. test_qrspec and create_frame_pattern uses the same function
4 * of libqrencode. This means the test is meaningless if test_qrspec is run
5 * with a pattern data created by create_frame_pattern of the same version.
6 * In order to test it correctly, create a pattern data by the tool of the
7 * previous version, or use the frame data attached to the package.
14 #include "../qrspec.h"
16 void append_pattern(int version, FILE *fp)
21 frame = QRspec_newFrame(version);
22 width = QRspec_getWidth(version);
23 fwrite(frame, 1, width * width, fp);
27 static int writePNG(unsigned char *frame, int width, const char *outfile)
32 unsigned char *row, *p, *q;
33 int x, y, xx, yy, bit;
38 realwidth = (width + margin * 2) * size;
39 row = (unsigned char *)malloc((realwidth + 7) / 8);
41 fprintf(stderr, "Failed to allocate memory.\n");
45 if(outfile[0] == '-' && outfile[1] == '\0') {
48 fp = fopen(outfile, "wb");
50 fprintf(stderr, "Failed to create file: %s\n", outfile);
56 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
59 fprintf(stderr, "Failed to initialize PNG writer.\n");
63 info_ptr = png_create_info_struct(png_ptr);
64 if(info_ptr == NULL) {
66 fprintf(stderr, "Failed to initialize PNG write.\n");
70 if(setjmp(png_jmpbuf(png_ptr))) {
71 png_destroy_write_struct(&png_ptr, &info_ptr);
73 fprintf(stderr, "Failed to write PNG image.\n");
77 png_init_io(png_ptr, fp);
78 png_set_IHDR(png_ptr, info_ptr,
83 PNG_COMPRESSION_TYPE_DEFAULT,
84 PNG_FILTER_TYPE_DEFAULT);
85 png_write_info(png_ptr, info_ptr);
88 memset(row, 0xff, (realwidth + 7) / 8);
89 for(y=0; y<margin * size; y++) {
90 png_write_row(png_ptr, row);
95 for(y=0; y<width; y++) {
97 memset(row, 0xff, (realwidth + 7) / 8);
99 q += margin * size / 8;
100 bit = 7 - (margin * size % 8);
101 for(x=0; x<width; x++) {
102 for(xx=0; xx<size; xx++) {
103 *q ^= (*p & 1) << bit;
112 for(yy=0; yy<size; yy++) {
113 png_write_row(png_ptr, row);
117 memset(row, 0xff, (realwidth + 7) / 8);
118 for(y=0; y<margin * size; y++) {
119 png_write_row(png_ptr, row);
122 png_write_end(png_ptr, info_ptr);
123 png_destroy_write_struct(&png_ptr, &info_ptr);
131 void write_pattern_image(int version, const char *filename)
134 unsigned char *frame;
135 static char str[256];
137 frame = QRspec_newFrame(version);
138 width = QRspec_getWidth(version);
140 snprintf(str, 256, "%s-%d.png", filename, version);
141 writePNG(frame, width, str);
145 void write_pattern(const char *filename)
150 fp = fopen(filename, "wb");
152 perror("Failed to open a file to write:");
155 for(i=1; i<=QRSPEC_VERSION_MAX; i++) {
156 append_pattern(i, fp);
157 write_pattern_image(i, filename);
162 int main(int argc, char **argv)
165 printf("Create empty frame patterns.\nUsage: %s FILENAME\n", argv[0]);
168 write_pattern(argv[1]);