/* * qrencode - QR Code encoder * * Masking. * Copyright (C) 2006-2011 Kentaro Fukuchi * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #if HAVE_CONFIG_H # include "config.h" #endif #include #include #include #include #include "qrencode.h" #include "qrspec.h" #include "mask.h" __STATIC int Mask_writeFormatInformation(int width, unsigned char *frame, int mask, QRecLevel level) { unsigned int format; unsigned char v; int i; int blacks = 0; format = QRspec_getFormatInfo(mask, level); for(i=0; i<8; i++) { if(format & 1) { blacks += 2; v = 0x85; } else { v = 0x84; } frame[width * 8 + width - 1 - i] = v; if(i < 6) { frame[width * i + 8] = v; } else { frame[width * (i + 1) + 8] = v; } format= format >> 1; } for(i=0; i<7; i++) { if(format & 1) { blacks += 2; v = 0x85; } else { v = 0x84; } frame[width * (width - 7 + i) + 8] = v; if(i == 0) { frame[width * 8 + 7] = v; } else { frame[width * 8 + 6 - i] = v; } format= format >> 1; } return blacks; } /** * Demerit coefficients. * See Section 8.8.2, pp.45, JIS X0510:2004. */ #define N1 (3) #define N2 (3) #define N3 (40) #define N4 (10) #define MASKMAKER(__exp__) \ int x, y;\ int b = 0;\ \ for(y=0; y= maskNum) { errno = EINVAL; return NULL; } masked = (unsigned char *)malloc(width * width); if(masked == NULL) return NULL; maskMakers[mask](width, frame, masked); Mask_writeFormatInformation(width, masked, mask, level); return masked; } //static int n1; //static int n2; //static int n3; //static int n4; __STATIC int Mask_calcN1N3(int length, int *runLength) { int i; int demerit = 0; int fact; for(i=0; i= 5) { demerit += N1 + (runLength[i] - 5); //n1 += N1 + (runLength[i] - 5); } if((i & 1)) { if(i >= 3 && i < length-2 && (runLength[i] % 3) == 0) { fact = runLength[i] / 3; if(runLength[i-2] == fact && runLength[i-1] == fact && runLength[i+1] == fact && runLength[i+2] == fact) { if(i == 3 || runLength[i-3] >= 4 * fact) { demerit += N3; //n3 += N3; } else if(i+4 >= length || runLength[i+3] >= 4 * fact) { demerit += N3; //n3 += N3; } } } } } return demerit; } __STATIC int Mask_calcN2(int width, unsigned char *frame) { int x, y; unsigned char *p; unsigned char b22, w22; int demerit = 0; p = frame + width + 1; for(y=1; y