TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
mmask.c
Go to the documentation of this file.
1 /*
2  * qrencode - QR Code encoder
3  *
4  * Masking for Micro QR Code.
5  * Copyright (C) 2006-2017 Kentaro Fukuchi <kentaro@fukuchi.org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "config.h"
23 #include <stdlib.h>
24 #include <string.h>
25 #include <limits.h>
26 #include <errno.h>
27 
28 #include "qrencode.h"
29 #include "mqrspec.h"
30 #include "mmask.h"
31 
32 STATIC_IN_RELEASE void MMask_writeFormatInformation(int version, int width, unsigned char *frame, int mask, QRecLevel level)
33 {
34  unsigned int format;
35  unsigned char v;
36  int i;
37 
38  format = MQRspec_getFormatInfo(mask, version, level);
39 
40  for(i = 0; i < 8; i++) {
41  v = 0x84 | (format & 1);
42  frame[width * (i + 1) + 8] = v;
43  format = format >> 1;
44  }
45  for(i = 0; i < 7; i++) {
46  v = 0x84 | (format & 1);
47  frame[width * 8 + 7 - i] = v;
48  format = format >> 1;
49  }
50 }
51 
52 #define MASKMAKER(__exp__) \
53  int x, y;\
54 \
55  for(y = 0; y < width; y++) {\
56  for(x = 0; x < width; x++) {\
57  if(*s & 0x80) {\
58  *d = *s;\
59  } else {\
60  *d = *s ^ ((__exp__) == 0);\
61  }\
62  s++; d++;\
63  }\
64  }
65 
66 static void Mask_mask0(int width, const unsigned char *s, unsigned char *d)
67 {
68  MASKMAKER(y&1)
69 }
70 
71 static void Mask_mask1(int width, const unsigned char *s, unsigned char *d)
72 {
73  MASKMAKER(((y/2)+(x/3))&1)
74 }
75 
76 static void Mask_mask2(int width, const unsigned char *s, unsigned char *d)
77 {
78  MASKMAKER((((x*y)&1)+(x*y)%3)&1)
79 }
80 
81 static void Mask_mask3(int width, const unsigned char *s, unsigned char *d)
82 {
83  MASKMAKER((((x+y)&1)+((x*y)%3))&1)
84 }
85 
86 #define maskNum (4)
87 typedef void MaskMaker(int, const unsigned char *, unsigned char *);
90 };
91 
92 #ifdef WITH_TESTS
93 unsigned char *MMask_makeMaskedFrame(int width, unsigned char *frame, int mask)
94 {
95  unsigned char *masked;
96 
97  masked = (unsigned char *)malloc((size_t)(width * width));
98  if(masked == NULL) return NULL;
99 
100  maskMakers[mask](width, frame, masked);
101 
102  return masked;
103 }
104 #endif
105 
106 unsigned char *MMask_makeMask(int version, unsigned char *frame, int mask, QRecLevel level)
107 {
108  unsigned char *masked;
109  int width;
110 
111  if(mask < 0 || mask >= maskNum) {
112  errno = EINVAL;
113  return NULL;
114  }
115 
116  width = MQRspec_getWidth(version);
117  masked = (unsigned char *)malloc((size_t)(width * width));
118  if(masked == NULL) return NULL;
119 
120  maskMakers[mask](width, frame, masked);
121  MMask_writeFormatInformation(version, width, masked, mask, level);
122 
123  return masked;
124 }
125 
126 STATIC_IN_RELEASE int MMask_evaluateSymbol(int width, unsigned char *frame)
127 {
128  int x, y;
129  unsigned char *p;
130  int sum1 = 0, sum2 = 0;
131 
132  p = frame + width * (width - 1);
133  for(x = 1; x < width; x++) {
134  sum1 += (p[x] & 1);
135  }
136 
137  p = frame + width * 2 - 1;
138  for(y = 1; y < width; y++) {
139  sum2 += (*p & 1);
140  p += width;
141  }
142 
143  return (sum1 <= sum2)?(sum1 * 16 + sum2):(sum2 * 16 + sum1);
144 }
145 
146 unsigned char *MMask_mask(int version, unsigned char *frame, QRecLevel level)
147 {
148  int i;
149  unsigned char *mask, *bestMask;
150  int maxScore = 0;
151  int score;
152  int width;
153 
154  width = MQRspec_getWidth(version);
155 
156  mask = (unsigned char *)malloc((size_t)(width * width));
157  if(mask == NULL) return NULL;
158  bestMask = NULL;
159 
160  for(i = 0; i < maskNum; i++) {
161  score = 0;
162  maskMakers[i](width, frame, mask);
163  MMask_writeFormatInformation(version, width, mask, i, level);
164  score = MMask_evaluateSymbol(width, mask);
165  if(score > maxScore) {
166  maxScore = score;
167  free(bestMask);
168  bestMask = mask;
169  mask = (unsigned char *)malloc((size_t)(width * width));
170  if(mask == NULL) break;
171  }
172  }
173  free(mask);
174  return bestMask;
175 }
static void Mask_mask1(int width, const unsigned char *s, unsigned char *d)
Definition: mmask.c:71
STATIC_IN_RELEASE int MMask_evaluateSymbol(int width, unsigned char *frame)
Definition: mmask.c:126
static void Mask_mask3(int width, const unsigned char *s, unsigned char *d)
Definition: mmask.c:81
#define STATIC_IN_RELEASE
Definition: config.h:95
unsigned char * MMask_mask(int version, unsigned char *frame, QRecLevel level)
Definition: mmask.c:146
QRecLevel
Level of error correction.
Definition: qrencode.h:126
unsigned char * MMask_makeMask(int version, unsigned char *frame, int mask, QRecLevel level)
Definition: mmask.c:106
void MaskMaker(int, const unsigned char *, unsigned char *)
Definition: mmask.c:87
#define MASKMAKER(__exp__)
Definition: mmask.c:52
static void Mask_mask0(int width, const unsigned char *s, unsigned char *d)
Definition: mmask.c:66
STATIC_IN_RELEASE void MMask_writeFormatInformation(int version, int width, unsigned char *frame, int mask, QRecLevel level)
Definition: mmask.c:32
unsigned int MQRspec_getFormatInfo(int mask, int version, QRecLevel level)
Return BCH encoded format information pattern.
Definition: mqrspec.c:137
#define maskNum
Definition: mmask.c:86
void free(voidpf ptr)
static void Mask_mask2(int width, const unsigned char *s, unsigned char *d)
Definition: mmask.c:76
int MQRspec_getWidth(int version)
Return the width of the symbol for the version.
Definition: mqrspec.c:78
voidp malloc(uInt size)
static MaskMaker * maskMakers[maskNum]
Definition: mmask.c:88