src/mmask.c

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 48 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 110 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Imager/QRCode.pm ↑ Summary src/mqrspec.c »
1 /*
2 * qrencode - QR Code encoder
3 *
4 * Masking for Micro QR Code.
5 * Copyright (C) 2006-2011 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 #if HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25 #include <stdlib.h>
26 #include <string.h>
27 #include <limits.h>
28 #include <errno.h>
29  
30 #include "qrencode.h"
31 #include "mqrspec.h"
32 #include "mmask.h"
33  
34 0 __STATIC void MMask_writeFormatInformation(int version, int width, unsigned char *frame, int mask, QRecLevel level)
35 {
36 unsigned int format;
37 unsigned char v;
38 int i;
39  
40 0 format = MQRspec_getFormatInfo(mask, version, level);
41  
42 0 ▶ for(i=0; i<8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<8; i++) { 0 0 0
43 0 v = 0x84 | (format & 1);
44 0 frame[width * (i + 1) + 8] = v;
45 0 format = format >> 1;
46 }
47 0 ▶ for(i=0; i<7; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<7; i++) { 0 0 0
48 0 v = 0x84 | (format & 1);
49 0 frame[width * 8 + 7 - i] = v;
50 0 format = format >> 1;
51 }
52 0 }
53  
54 #define MASKMAKER(__exp__) \
55 int x, y;\
56 \
57 for(y=0; y<width; y++) {\
58 for(x=0; x<width; x++) {\
59 if(*s & 0x80) {\
60 *d = *s;\
61 } else {\
62 *d = *s ^ ((__exp__) == 0);\
63 }\
64 s++; d++;\
65 }\
66 }
67  
68 0 static void Mask_mask0(int width, const unsigned char *s, unsigned char *d)
69 {
70 0 ▶ MASKMAKER(y&1)
Branch: 3 branches
BranchTrueFalseTotal
MASKMAKER(y&1) 0 0 0
MASKMAKER(y&1) 0 0 0
MASKMAKER(y&1) 0 0 0
71 0 }
72  
73 0 static void Mask_mask1(int width, const unsigned char *s, unsigned char *d)
74 {
75 0 ▶ MASKMAKER(((y/2)+(x/3))&1)
Branch: 3 branches
BranchTrueFalseTotal
MASKMAKER(((y/2)+(x/3))&1) 0 0 0
MASKMAKER(((y/2)+(x/3))&1) 0 0 0
MASKMAKER(((y/2)+(x/3))&1) 0 0 0
76 0 }
77  
78 0 static void Mask_mask2(int width, const unsigned char *s, unsigned char *d)
79 {
80 0 ▶ MASKMAKER((((x*y)&1)+(x*y)%3)&1)
Branch: 3 branches
BranchTrueFalseTotal
MASKMAKER((((x*y)&1)+(x*y)%3)&1) 0 0 0
MASKMAKER((((x*y)&1)+(x*y)%3)&1) 0 0 0
MASKMAKER((((x*y)&1)+(x*y)%3)&1) 0 0 0
81 0 }
82  
83 0 static void Mask_mask3(int width, const unsigned char *s, unsigned char *d)
84 {
85 0 ▶ MASKMAKER((((x+y)&1)+((x*y)%3))&1)
Branch: 3 branches
BranchTrueFalseTotal
MASKMAKER((((x+y)&1)+((x*y)%3))&1) 0 0 0
MASKMAKER((((x+y)&1)+((x*y)%3))&1) 0 0 0
MASKMAKER((((x+y)&1)+((x*y)%3))&1) 0 0 0
86 0 }
87  
88 #define maskNum (4)
89 typedef void MaskMaker(int, const unsigned char *, unsigned char *);
90 static MaskMaker *maskMakers[maskNum] = {
91 Mask_mask0, Mask_mask1, Mask_mask2, Mask_mask3
92 };
93  
94 #ifdef WITH_TESTS
95 unsigned char *MMask_makeMaskedFrame(int width, unsigned char *frame, int mask)
96 {
97 unsigned char *masked;
98  
99 masked = (unsigned char *)malloc(width * width);
100 if(masked == NULL) return NULL;
101  
102 maskMakers[mask](width, frame, masked);
103  
104 return masked;
105 }
106 #endif
107  
108 0 unsigned char *MMask_makeMask(int version, unsigned char *frame, int mask, QRecLevel level)
109 {
110 unsigned char *masked;
111 int width;
112  
113 0 ▶ if(mask < 0 || mask >= maskNum) {
Branch: 2 branches
BranchTrueFalseTotal
if(mask < 0 || mask >= maskNum) { 0 0 0
if(mask < 0 || mask >= maskNum) { 0 0 0
114 0 errno = EINVAL;
115 0 return NULL;
116 }
117  
118 0 width = MQRspec_getWidth(version);
119 0 masked = (unsigned char *)malloc(width * width);
120 0 ▶ if(masked == NULL) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if(masked == NULL) return NULL; 0 0 0
121  
122 0 maskMakers[mask](width, frame, masked);
123 0 MMask_writeFormatInformation(version, width, masked, mask, level);
124  
125 0 return masked;
126 }
127  
128 0 __STATIC int MMask_evaluateSymbol(int width, unsigned char *frame)
129 {
130 int x, y;
131 unsigned char *p;
132 0 int sum1 = 0, sum2 = 0;
133  
134 0 p = frame + width * (width - 1);
135 0 ▶ for(x=1; x<width; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for(x=1; x<width; x++) { 0 0 0
136 0 sum1 += (p[x] & 1);
137 }
138  
139 0 p = frame + width * 2 - 1;
140 0 ▶ for(y=1; y<width; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for(y=1; y<width; y++) { 0 0 0
141 0 sum2 += (*p & 1);
142 0 p += width;
143 }
144  
145 0 ▶ return (sum1 <= sum2)?(sum1 * 16 + sum2):(sum2 * 16 + sum1);
Branch: 1 branch
BranchTrueFalseTotal
return (sum1 <= sum2)?(sum1 * 16 + sum2):(sum2 * 16 + sum1); 0 0 0
146 }
147  
148 0 unsigned char *MMask_mask(int version, unsigned char *frame, QRecLevel level)
149 {
150 int i;
151 unsigned char *mask, *bestMask;
152 0 int maxScore = 0;
153 int score;
154 int width;
155  
156 0 width = MQRspec_getWidth(version);
157  
158 0 mask = (unsigned char *)malloc(width * width);
159 0 ▶ if(mask == NULL) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if(mask == NULL) return NULL; 0 0 0
160 0 bestMask = NULL;
161  
162 0 ▶ for(i=0; i<maskNum; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<maskNum; i++) { 0 0 0
163 0 score = 0;
164 0 maskMakers[i](width, frame, mask);
165 0 MMask_writeFormatInformation(version, width, mask, i, level);
166 0 score = MMask_evaluateSymbol(width, mask);
167 0 ▶ if(score > maxScore) {
Branch: 1 branch
BranchTrueFalseTotal
if(score > maxScore) { 0 0 0
168 0 maxScore = score;
169 0 free(bestMask);
170 0 bestMask = mask;
171 0 mask = (unsigned char *)malloc(width * width);
172 0 ▶ if(mask == NULL) break;
Branch: 1 branch
BranchTrueFalseTotal
if(mask == NULL) break; 0 0 0
173 }
174 }
175 0 free(mask);
176 0 return bestMask;
177 }
« blib/lib/Imager/QRCode.pm ↑ Summary src/mqrspec.c »