TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
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 #include "config.h"
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <errno.h>
33 
34 #include "mqrspec.h"
35 
36 /******************************************************************************
37  * Version and capacity
38  *****************************************************************************/
39 
40 typedef struct {
41  int width;
42  int ec[4];
44 
50  { 0, {0, 0, 0, 0}},
51  { 11, {2, 0, 0, 0}},
52  { 13, {5, 6, 0, 0}},
53  { 15, {6, 8, 0, 0}},
54  { 17, {8, 10, 14, 0}}
55 };
56 
57 int MQRspec_getDataLengthBit(int version, QRecLevel level)
58 {
59  int w;
60  int ecc;
61 
62  w = mqrspecCapacity[version].width - 1;
63  ecc = mqrspecCapacity[version].ec[level];
64  if(ecc == 0) return 0;
65  return w * w - 64 - ecc * 8;
66 }
67 
68 int MQRspec_getDataLength(int version, QRecLevel level)
69 {
70  return (MQRspec_getDataLengthBit(version, level) + 4) / 8;
71 }
72 
73 int MQRspec_getECCLength(int version, QRecLevel level)
74 {
75  return mqrspecCapacity[version].ec[level];
76 }
77 
78 int MQRspec_getWidth(int version)
79 {
80  return mqrspecCapacity[version].width;
81 }
82 
83 /******************************************************************************
84  * Length indicator
85  *****************************************************************************/
86 
90 static const int lengthTableBits[4][4] = {
91  { 3, 4, 5, 6},
92  { 0, 3, 4, 5},
93  { 0, 0, 4, 5},
94  { 0, 0, 3, 4}
95 };
96 
98 {
99  return lengthTableBits[mode][version - 1];
100 }
101 
103 {
104  int bits;
105  int words;
106 
107  bits = lengthTableBits[mode][version - 1];
108  words = (1 << bits) - 1;
109  if(mode == QR_MODE_KANJI) {
110  words *= 2; // the number of bytes is required
111  }
112 
113  return words;
114 }
115 
116 /******************************************************************************
117  * Format information
118  *****************************************************************************/
119 
120 /* See calcFormatInfo in tests/test_mqrspec.c */
121 static const unsigned int formatInfo[4][8] = {
122  {0x4445, 0x55ae, 0x6793, 0x7678, 0x06de, 0x1735, 0x2508, 0x34e3},
123  {0x4172, 0x5099, 0x62a4, 0x734f, 0x03e9, 0x1202, 0x203f, 0x31d4},
124  {0x4e2b, 0x5fc0, 0x6dfd, 0x7c16, 0x0cb0, 0x1d5b, 0x2f66, 0x3e8d},
125  {0x4b1c, 0x5af7, 0x68ca, 0x7921, 0x0987, 0x186c, 0x2a51, 0x3bba}
126 };
127 
128 /* See Table 10 of Appendix 1. (pp.115) */
129 static const int typeTable[MQRSPEC_VERSION_MAX + 1][3] = {
130  {-1, -1, -1},
131  { 0, -1, -1},
132  { 1, 2, -1},
133  { 3, 4, -1},
134  { 5, 6, 7}
135 };
136 
137 unsigned int MQRspec_getFormatInfo(int mask, int version, QRecLevel level)
138 {
139  int type;
140 
141  if(mask < 0 || mask > 3) return 0;
142  if(version <= 0 || version > MQRSPEC_VERSION_MAX) return 0;
143  if(level == QR_ECLEVEL_H) return 0;
144  type = typeTable[version][level];
145  if(type < 0) return 0;
146 
147  return formatInfo[mask][type];
148 }
149 
150 /******************************************************************************
151  * Frame
152  *****************************************************************************/
153 
160 static void putFinderPattern(unsigned char *frame, int width, int ox, int oy)
161 {
162  static const unsigned char finder[] = {
163  0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1,
164  0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1,
165  0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
166  0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
167  0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
168  0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1,
169  0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1,
170  };
171  int x, y;
172  const unsigned char *s;
173 
174  frame += oy * width + ox;
175  s = finder;
176  for(y = 0; y < 7; y++) {
177  for(x = 0; x < 7; x++) {
178  frame[x] = s[x];
179  }
180  frame += width;
181  s += 7;
182  }
183 }
184 
185 static unsigned char *MQRspec_createFrame(int version)
186 {
187  unsigned char *frame, *p, *q;
188  int width;
189  int x, y;
190 
191  width = mqrspecCapacity[version].width;
192  frame = (unsigned char *)malloc((size_t)(width * width));
193  if(frame == NULL) return NULL;
194 
195  memset(frame, 0, (size_t)(width * width));
196  /* Finder pattern */
197  putFinderPattern(frame, width, 0, 0);
198  /* Separator */
199  p = frame;
200  for(y = 0; y < 7; y++) {
201  p[7] = 0xc0;
202  p += width;
203  }
204  memset(frame + width * 7, 0xc0, 8);
205  /* Mask format information area */
206  memset(frame + width * 8 + 1, 0x84, 8);
207  p = frame + width + 8;
208  for(y = 0; y < 7; y++) {
209  *p = 0x84;
210  p += width;
211  }
212  /* Timing pattern */
213  p = frame + 8;
214  q = frame + width * 8;
215  for(x = 1; x < width-7; x++) {
216  *p = 0x90 | (x & 1);
217  *q = 0x90 | (x & 1);
218  p++;
219  q += width;
220  }
221 
222  return frame;
223 }
224 
225 unsigned char *MQRspec_newFrame(int version)
226 {
227  if(version < 1 || version > MQRSPEC_VERSION_MAX) return NULL;
228 
229  return MQRspec_createFrame(version);
230 }
int width
Edge length of the symbol.
Definition: mqrspec.c:41
highest
Definition: qrencode.h:130
static unsigned char * MQRspec_createFrame(int version)
Definition: mqrspec.c:185
static const int lengthTableBits[4][4]
See Table 3 (pp.107) of Appendix 1, JIS X0510:2004.
Definition: mqrspec.c:90
static const unsigned int formatInfo[4][8]
Definition: mqrspec.c:121
int ec[4]
Number of ECC code (bytes)
Definition: mqrspec.c:42
QRecLevel
Level of error correction.
Definition: qrencode.h:126
int MQRspec_lengthIndicator(QRencodeMode mode, int version)
Return the size of length indicator for the mode and version.
Definition: mqrspec.c:97
#define MQRSPEC_VERSION_MAX
Maximum version (size) of QR-code symbol.
Definition: qrencode.h:141
static void putFinderPattern(unsigned char *frame, int width, int ox, int oy)
Put a finder pattern.
Definition: mqrspec.c:160
QRencodeMode
Encoding mode.
Definition: qrencode.h:111
unsigned char * MQRspec_newFrame(int version)
Return a copy of initialized frame.
Definition: mqrspec.c:225
unsigned int MQRspec_getFormatInfo(int mask, int version, QRecLevel level)
Return BCH encoded format information pattern.
Definition: mqrspec.c:137
Kanji (shift-jis) mode.
Definition: qrencode.h:116
int MQRspec_getDataLengthBit(int version, QRecLevel level)
Return maximum data code length (bits) for the version.
Definition: mqrspec.c:57
int MQRspec_maximumWords(QRencodeMode mode, int version)
Return the maximum length for the mode and version.
Definition: mqrspec.c:102
const char int mode
Definition: ioapi.h:135
static const MQRspec_Capacity mqrspecCapacity[MQRSPEC_VERSION_MAX+1]
Table of the capacity of symbols See Table 1 (pp.106) and Table 8 (pp.113) of Appendix 1...
Definition: mqrspec.c:49
int MQRspec_getECCLength(int version, QRecLevel level)
Return maximum error correction code length (bytes) for the version.
Definition: mqrspec.c:73
int MQRspec_getWidth(int version)
Return the width of the symbol for the version.
Definition: mqrspec.c:78
voidp malloc(uInt size)
int MQRspec_getDataLength(int version, QRecLevel level)
Return maximum data code length (bytes) for the version.
Definition: mqrspec.c:68
static const int typeTable[MQRSPEC_VERSION_MAX+1][3]
Definition: mqrspec.c:129