Permalink
Browse files

removed last multiply/divide, removed memmove shift in ecc

  • Loading branch information...
1 parent 0453ffa commit c722636f76ac3d011fead9f16060123a308c2fb6 tz committed Feb 26, 2010
Showing with 76 additions and 96 deletions.
  1. +41 −64 qrencode.c
  2. +35 −32 qrframe.c
View
@@ -36,9 +36,7 @@ static void appendrs(unsigned char *data, unsigned char dsize, unsigned char *ec
unsigned i, j;
unsigned char fb;
// use qrframe as buffer space
- unsigned char *exp = qrframe, *log = &qrframe[256], *genpoly = &qrframe[512];
-
- memset(ecbuf, 0, ecsize);
+ unsigned char *exp = qrframe, *log = &qrframe[256], *genpoly = &qrframe[512], *iecbuf;
log[0] = 255;
exp[255] = 0;
@@ -67,18 +65,22 @@ static void appendrs(unsigned char *data, unsigned char dsize, unsigned char *ec
for (i = 0; i <= ecsize; i++)
genpoly[i] = log[genpoly[i]];
+ memset(ecbuf, 0, ecsize);
+ iecbuf = ecbuf;
for (i = 0; i < dsize; i++) {
- fb = log[data[i] ^ ecbuf[0]];
+ fb = log[data[i] ^ iecbuf[0]];
if (fb != 255) /* fb term is non-zero */
for (j = 1; j < ecsize; j++)
- ecbuf[j] ^= exp[modnn(fb + genpoly[ecsize - j])];
- /* Shift */
- memmove(&ecbuf[0], &ecbuf[1], ecsize - 1);
+ iecbuf[j] ^= exp[modnn(fb + genpoly[ecsize - j])];
+ // Shift - use bigger buffer and move pointer instead of:
+ // memmove(&iecbuf[0], &iecbuf[1], ecsize - 1);
+ iecbuf++;
if (fb != 255)
- ecbuf[ecsize - 1] = exp[modnn(fb + genpoly[0])];
+ iecbuf[ecsize - 1] = exp[modnn(fb + genpoly[0])];
else
- ecbuf[ecsize - 1] = 0;
+ iecbuf[ecsize - 1] = 0;
}
+ memmove(ecbuf, iecbuf, ecsize);
}
//========================================================================
@@ -163,12 +165,15 @@ static unsigned char ismasked(unsigned char x, unsigned char y)
y = bt;
}
bt = y;
+ bt += y * y;
+#if 0
// bt += y*y;
unsigned s = 1;
while (y--) {
bt += s;
s += 2;
}
+#endif
bt >>= 1;
bt += x;
return (__LPM(&framask[bt >> 3]) >> (7 - (bt & 7))) & 1;
@@ -229,33 +234,22 @@ static void fillframe(void)
//========================================================================
// Masking
-static unsigned applymask(unsigned char m)
+static void applymask(unsigned char m)
{
unsigned char x, y, r3x, r3y;
- int b = 0;
switch (m) {
case 0:
for (y = 0; y < WD; y++)
- for (x = 0; x < WD; x++) {
+ for (x = 0; x < WD; x++)
if (!((x + y) & 1) && !ismasked(x, y))
TOGQRBIT(x, y);
- if (QRBIT(x, y)) // count excess whites v.s blacks
- b++;
- else
- b--;
- }
break;
case 1:
for (y = 0; y < WD; y++)
- for (x = 0; x < WD; x++) {
+ for (x = 0; x < WD; x++)
if (!(y & 1) && !ismasked(x, y))
TOGQRBIT(x, y);
- if (QRBIT(x, y)) // count excess whites v.s blacks
- b++;
- else
- b--;
- }
break;
case 2:
for (y = 0; y < WD; y++)
@@ -264,10 +258,6 @@ static unsigned applymask(unsigned char m)
r3x = 0;
if (!r3x && !ismasked(x, y))
TOGQRBIT(x, y);
- if (QRBIT(x, y)) // count excess whites v.s blacks
- b++;
- else
- b--;
}
break;
case 3:
@@ -279,26 +269,18 @@ static unsigned applymask(unsigned char m)
r3x = 0;
if (!r3x && !ismasked(x, y))
TOGQRBIT(x, y);
- if (QRBIT(x, y)) // count excess whites v.s blacks
- b++;
- else
- b--;
}
}
break;
case 4:
for (y = 0; y < WD; y++)
- for (r3x = 0, r3y = ((y>>1)&1), x = 0; x < WD; x++, r3x++) {
+ for (r3x = 0, r3y = ((y >> 1) & 1), x = 0; x < WD; x++, r3x++) {
if (r3x == 3) {
r3x = 0;
r3y = !r3y;
}
if (!r3y && !ismasked(x, y))
TOGQRBIT(x, y);
- if (QRBIT(x, y)) // count excess whites v.s blacks
- b++;
- else
- b--;
}
break;
case 5:
@@ -310,10 +292,6 @@ static unsigned applymask(unsigned char m)
r3x = 0;
if (!((x & y & 1) + !(!r3x | !r3y)) && !ismasked(x, y))
TOGQRBIT(x, y);
- if (QRBIT(x, y)) // count excess whites v.s blacks
- b++;
- else
- b--;
}
}
break;
@@ -326,10 +304,6 @@ static unsigned applymask(unsigned char m)
r3x = 0;
if (!(((x & y & 1) + (r3x && (r3x == r3y))) & 1) && !ismasked(x, y))
TOGQRBIT(x, y);
- if (QRBIT(x, y)) // count excess whites v.s blacks
- b++;
- else
- b--;
}
}
break;
@@ -342,19 +316,11 @@ static unsigned applymask(unsigned char m)
r3x = 0;
if (!(((r3x && (r3x == r3y)) + ((x + y) & 1)) & 1) && !ismasked(x, y))
TOGQRBIT(x, y);
- if (QRBIT(x, y)) // count excess whites v.s blacks
- b++;
- else
- b--;
}
}
break;
}
-
- if (b < 0)
- b = -b;
-
- return b;
+ return;
}
// Badness coefficients.
@@ -387,11 +353,11 @@ static unsigned badruns(unsigned char length)
static int badcheck()
{
- unsigned char x, y, h, b;
+ unsigned char x, y, h, b, b1;
unsigned thisbad = 0;
+ int bw = 0;
// blocks of same color.
-
for (y = 0; y < WD - 1; y++)
for (x = 0; x < WD - 1; x++)
if ((QRBIT(x, y) && QRBIT(x + 1, y) && QRBIT(x, y + 1) && QRBIT(x + 1, y + 1)) // all black
@@ -402,23 +368,37 @@ static int badcheck()
for (y = 0; y < WD; y++) {
rlens[0] = 0;
for (h = b = x = 0; x < WD; x++) {
- if (QRBIT(x, y) == b)
+ if ((b1 = QRBIT(x, y)) == b)
rlens[h]++;
else
rlens[++h] = 1;
- b = QRBIT(x, y);
+ b = b1;
+ bw += b ? 1 : -1;
}
thisbad += badruns(h);
}
+
+ // black/white imbalance
+ if (bw < 0)
+ bw = -bw;
+
+ unsigned long big = bw;
+ unsigned count = 0;
+ big += big << 2;
+ big <<= 1;
+ while (big > WD * WD)
+ big -= WD * WD, count++;
+ thisbad += count * N4;
+
// Y runs
for (x = 0; x < WD; x++) {
rlens[0] = 0;
for (h = b = y = 0; y < WD; y++) {
- if (QRBIT(x, y) == b)
+ if ((b1 = QRBIT(x, y)) == b)
rlens[h]++;
else
rlens[++h] = 1;
- b = QRBIT(x, y);
+ b = b1;
}
thisbad += badruns(h);
}
@@ -471,11 +451,8 @@ void qrencode()
fillframe(); // Inisde loop to avoid having separate mask buffer
memcpy(strinbuf, qrframe, WD * WDB);
for (i = 0; i < 8; i++) {
- badness = applymask(i); // returns black-white imbalance
- badness *= 10;
- badness /= (WD * WD);
- badness *= N4;
- badness += badcheck();
+ applymask(i); // returns black-white imbalance
+ badness = badcheck();
#if 0 //ndef PUREBAD
if (badness < WD * WD * 5 / 4) { // good enough - masks grow in compute complexity
best = i;
View
@@ -5,14 +5,15 @@ unsigned char *framebase;
unsigned char *framask;
unsigned char *rlens;
unsigned char VERSION;
-unsigned char WD, WDB; // filled in from verison by initframe
+unsigned char WD, WDB; // filled in from verison by initframe
#define QRBIT(x,y) ( ( framebase[((x)>>3) + (y) * WDB] >> (7-((x) & 7 ))) & 1 )
#define SETQRBIT(x,y) framebase[((x)>>3) + (y) * WDB] |= 0x80 >> ((x) & 7)
-static void setmask(unsigned char x,unsigned char y) {
+static void setmask(unsigned char x, unsigned char y)
+{
unsigned bt;
- if( x > y ) {
+ if (x > y) {
bt = x;
x = y;
y = bt;
@@ -69,11 +70,11 @@ static void putalign(int x, int y)
SETQRBIT(x + 2, y + j);
SETQRBIT(x + j + 1, y + 2);
}
- for( j = 0 ; j < 2 ; j++ ) {
- setmask(x-1, y+j);
- setmask(x+1, y-j);
- setmask(x-j, y-1);
- setmask(x+j, y+1);
+ for (j = 0; j < 2; j++) {
+ setmask(x - 1, y + j);
+ setmask(x + 1, y - j);
+ setmask(x - j, y - 1);
+ setmask(x + j, y + 1);
}
}
@@ -126,23 +127,22 @@ static void putvpat(void)
bc = 17;
for (x = 0; x < 6; x++)
for (y = 0; y < 3; y++, bc--)
- if (1&(bc > 11 ? vers >> (bc - 12) : verinfo >> bc)) {
- SETQRBIT( 5-x,2-y+WD-11);
- SETQRBIT( 2-y+WD-11,5-x);
- }
- else {
- setmask( 5-x,2-y+WD-11);
- setmask( 2-y+WD-11,5-x);
+ if (1 & (bc > 11 ? vers >> (bc - 12) : verinfo >> bc)) {
+ SETQRBIT(5 - x, 2 - y + WD - 11);
+ SETQRBIT(2 - y + WD - 11, 5 - x);
+ } else {
+ setmask(5 - x, 2 - y + WD - 11);
+ setmask(2 - y + WD - 11, 5 - x);
}
}
void initframe()
{
unsigned x, y;
- framebase = calloc(WDB*WD,1);
- framask = calloc(((WD*(WD+1)/2)+7)/8,1);
- rlens = malloc(WD+1);
+ framebase = calloc(WDB * WD, 1);
+ framask = calloc(((WD * (WD + 1) / 2) + 7) / 8, 1);
+ rlens = malloc(WD + 1);
// finders
putfind();
// alignment blocks
@@ -181,17 +181,17 @@ void initframe()
// version block
putvpat();
- for( y = 0 ; y < WD; y++ )
+ for (y = 0; y < WD; y++)
for (x = 0; x <= y; x++)
- if( QRBIT(x,y) )
- setmask(x,y);
+ if (QRBIT(x, y))
+ setmask(x, y);
}
unsigned char *strinbuf;
unsigned char *qrframe;
unsigned char ECCLEVEL;
unsigned char neccblk1;
-unsigned char neccblk2 ;
+unsigned char neccblk2;
unsigned char datablkw;
unsigned char eccblkwid;
@@ -205,24 +205,27 @@ unsigned char eccblkwid;
#include "ecctable.h"
-unsigned initecc(unsigned char ecc, unsigned char vers) {
+unsigned initecc(unsigned char ecc, unsigned char vers)
+{
VERSION = vers;
WD = 17 + 4 * vers;
WDB = (WD + 7) / 8;
- unsigned fsz = WD*WDB;
- if( fsz < 768 ) // for ECC math buffers
+ unsigned fsz = WD * WDB;
+ if (fsz < 768) // for ECC math buffers
fsz = 768;
- qrframe = malloc( fsz );
+ qrframe = malloc(fsz);
ECCLEVEL = ecc;
unsigned eccindex = (ecc - 1) * 4 + (vers - 1) * 16;
- neccblk1 = eccblocks[eccindex++] ;
- neccblk2 = eccblocks[eccindex++] ;
- datablkw = eccblocks[eccindex++] ;
- eccblkwid = eccblocks[eccindex++] ;
+ neccblk1 = eccblocks[eccindex++];
+ neccblk2 = eccblocks[eccindex++];
+ datablkw = eccblocks[eccindex++];
+ eccblkwid = eccblocks[eccindex++];
- strinbuf = malloc(WD*WDB);//(datablkw + eccblkwid) * (neccblk1 + neccblk2) + neccblk2);
- return datablkw * (neccblk1 + neccblk2) + neccblk2 - 2; //-3 if vers > 9!
+ if (fsz < datablkw + (datablkw + eccblkwid) * (neccblk1 + neccblk2) + neccblk2)
+ fsz = datablkw + (datablkw + eccblkwid) * (neccblk1 + neccblk2) + neccblk2;
+ strinbuf = malloc(fsz);
+ return datablkw * (neccblk1 + neccblk2) + neccblk2 - 2; //-3 if vers > 9!
}

0 comments on commit c722636

Please sign in to comment.