qrencode  4.1.1
About: libqrencode is a fast and compact library and command-line utility for encoding data in a QR Code symbol.
  Fossies Dox: qrencode-4.1.1.tar.bz2  ("unofficial" and yet experimental doxygen-generated source code documentation)  

mqrspec.c
Go to the documentation of this file.
1/*
2 * qrencode - QR Code encoder
3 *
4 * Micro QR Code specification in convenient format.
5 * Copyright (C) 2006-2017 Kentaro Fukuchi <kentaro@fukuchi.org>
6 *
7 * The following data / specifications are taken from
8 * "Two dimensional symbol -- QR-code -- Basic Specification" (JIS X0510:2004)
9 * or
10 * "Automatic identification and data capture techniques --
11 * QR Code 2005 bar code symbology specification" (ISO/IEC 18004:2006)
12 *
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or any later version.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 */
27
28#if HAVE_CONFIG_H
29# include "config.h"
30#endif
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <errno.h>
35
36#include "mqrspec.h"
37
38/******************************************************************************
39 * Version and capacity
40 *****************************************************************************/
41
42typedef struct {
43 int width; ///< Edge length of the symbol
44 int ec[4]; ///< Number of ECC code (bytes)
46
47/**
48 * Table of the capacity of symbols
49 * See Table 1 (pp.106) and Table 8 (pp.113) of Appendix 1, JIS X0510:2004.
50 */
52 { 0, {0, 0, 0, 0}},
53 { 11, {2, 0, 0, 0}},
54 { 13, {5, 6, 0, 0}},
55 { 15, {6, 8, 0, 0}},
56 { 17, {8, 10, 14, 0}}
57};
58
60{
61 int w;
62 int ecc;
63
66 if(ecc == 0) return 0;
67 return w * w - 64 - ecc * 8;
68}
69
71{
72 return (MQRspec_getDataLengthBit(version, level) + 4) / 8;
73}
74
76{
78}
79
81{
83}
84
85/******************************************************************************
86 * Length indicator
87 *****************************************************************************/
88
89/**
90 * See Table 3 (pp.107) of Appendix 1, JIS X0510:2004.
91 */
92static const int lengthTableBits[4][4] = {
93 { 3, 4, 5, 6},
94 { 0, 3, 4, 5},
95 { 0, 0, 4, 5},
96 { 0, 0, 3, 4}
97};
98
100{
101 return lengthTableBits[mode][version - 1];
102}
103
105{
106 int bits;
107 int words;
108
109 bits = lengthTableBits[mode][version - 1];
110 words = (1 << bits) - 1;
111 if(mode == QR_MODE_KANJI) {
112 words *= 2; // the number of bytes is required
113 }
114
115 return words;
116}
117
118/******************************************************************************
119 * Format information
120 *****************************************************************************/
121
122/* See calcFormatInfo in tests/test_mqrspec.c */
123static const unsigned int formatInfo[4][8] = {
124 {0x4445, 0x55ae, 0x6793, 0x7678, 0x06de, 0x1735, 0x2508, 0x34e3},
125 {0x4172, 0x5099, 0x62a4, 0x734f, 0x03e9, 0x1202, 0x203f, 0x31d4},
126 {0x4e2b, 0x5fc0, 0x6dfd, 0x7c16, 0x0cb0, 0x1d5b, 0x2f66, 0x3e8d},
127 {0x4b1c, 0x5af7, 0x68ca, 0x7921, 0x0987, 0x186c, 0x2a51, 0x3bba}
128};
129
130/* See Table 10 of Appendix 1. (pp.115) */
131static const int typeTable[MQRSPEC_VERSION_MAX + 1][3] = {
132 {-1, -1, -1},
133 { 0, -1, -1},
134 { 1, 2, -1},
135 { 3, 4, -1},
136 { 5, 6, 7}
137};
138
139unsigned int MQRspec_getFormatInfo(int mask, int version, QRecLevel level)
140{
141 int type;
142
143 if(mask < 0 || mask > 3) return 0;
144 if(version <= 0 || version > MQRSPEC_VERSION_MAX) return 0;
145 if(level == QR_ECLEVEL_H) return 0;
146 type = typeTable[version][level];
147 if(type < 0) return 0;
148
149 return formatInfo[mask][type];
150}
151
152/******************************************************************************
153 * Frame
154 *****************************************************************************/
155
156/**
157 * Put a finder pattern.
158 * @param frame
159 * @param width
160 * @param ox,oy upper-left coordinate of the pattern
161 */
162static void putFinderPattern(unsigned char *frame, int width, int ox, int oy)
163{
164 static const unsigned char finder[] = {
165 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1,
166 0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1,
167 0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
168 0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
169 0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
170 0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1,
171 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1,
172 };
173 int x, y;
174 const unsigned char *s;
175
176 frame += oy * width + ox;
177 s = finder;
178 for(y = 0; y < 7; y++) {
179 for(x = 0; x < 7; x++) {
180 frame[x] = s[x];
181 }
182 frame += width;
183 s += 7;
184 }
185}
186
187static unsigned char *MQRspec_createFrame(int version)
188{
189 unsigned char *frame, *p, *q;
190 int width;
191 int x, y;
192
194 frame = (unsigned char *)malloc((size_t)(width * width));
195 if(frame == NULL) return NULL;
196
197 memset(frame, 0, (size_t)(width * width));
198 /* Finder pattern */
199 putFinderPattern(frame, width, 0, 0);
200 /* Separator */
201 p = frame;
202 for(y = 0; y < 7; y++) {
203 p[7] = 0xc0;
204 p += width;
205 }
206 memset(frame + width * 7, 0xc0, 8);
207 /* Mask format information area */
208 memset(frame + width * 8 + 1, 0x84, 8);
209 p = frame + width + 8;
210 for(y = 0; y < 7; y++) {
211 *p = 0x84;
212 p += width;
213 }
214 /* Timing pattern */
215 p = frame + 8;
216 q = frame + width * 8;
217 for(x = 1; x < width-7; x++) {
218 *p = 0x90 | (x & 1);
219 *q = 0x90 | (x & 1);
220 p++;
221 q += width;
222 }
223
224 return frame;
225}
226
227unsigned char *MQRspec_newFrame(int version)
228{
229 if(version < 1 || version > MQRSPEC_VERSION_MAX) return NULL;
230
232}
static const int typeTable[4+1][3]
Definition: mqrspec.c:131
int MQRspec_getECCLength(int version, QRecLevel level)
Return maximum error correction code length (bytes) for the version.
Definition: mqrspec.c:75
static const MQRspec_Capacity mqrspecCapacity[4+1]
Table of the capacity of symbols See Table 1 (pp.106) and Table 8 (pp.113) of Appendix 1,...
Definition: mqrspec.c:51
int MQRspec_maximumWords(QRencodeMode mode, int version)
Return the maximum length for the mode and version.
Definition: mqrspec.c:104
int MQRspec_lengthIndicator(QRencodeMode mode, int version)
Length indicator.
Definition: mqrspec.c:99
unsigned int MQRspec_getFormatInfo(int mask, int version, QRecLevel level)
Format information.
Definition: mqrspec.c:139
static void putFinderPattern(unsigned char *frame, int width, int ox, int oy)
Frame.
Definition: mqrspec.c:162
static const unsigned int formatInfo[4][8]
Format information.
Definition: mqrspec.c:123
unsigned char * MQRspec_newFrame(int version)
Frame.
Definition: mqrspec.c:227
int MQRspec_getWidth(int version)
Return the width of the symbol for the version.
Definition: mqrspec.c:80
static const int lengthTableBits[4][4]
Length indicator.
Definition: mqrspec.c:92
static unsigned char * MQRspec_createFrame(int version)
Definition: mqrspec.c:187
int MQRspec_getDataLengthBit(int version, QRecLevel level)
Return maximum data code length (bits) for the version.
Definition: mqrspec.c:59
int MQRspec_getDataLength(int version, QRecLevel level)
Return maximum data code length (bytes) for the version.
Definition: mqrspec.c:70
static QRecLevel level
Definition: qrenc.c:50
static int version
Definition: qrenc.c:40
QRecLevel
Level of error correction.
Definition: qrencode.h:124
@ QR_ECLEVEL_H
highest
Definition: qrencode.h:128
QRencodeMode
Encoding mode.
Definition: qrencode.h:109
@ QR_MODE_KANJI
Kanji (shift-jis) mode.
Definition: qrencode.h:114
#define MQRSPEC_VERSION_MAX
Maximum version (size) of QR-code symbol.
Definition: qrencode.h:139
Version and capacity.
Definition: mqrspec.c:42
int ec[4]
Number of ECC code (bytes)
Definition: mqrspec.c:44
int width
Edge length of the symbol.
Definition: mqrspec.c:43