1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
/*
This file is part of Page Layout Detection Tools.
This code is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This code 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this code; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <raster/deskewer.h>
#include <util/bitutil.h>
#include <math.h>
#include <memory.h>
namespace pagetools{
unsigned int
Deskewer::next_pow2(unsigned int n){
unsigned int retval=1;
while(retval<n){
retval<<=1;
}
return retval;
}
double
Deskewer::findSkew(const BWImage& img){
unsigned int w2=next_pow2(img.bytewidth());
unsigned int ssize=2*w2-1; // Size of sharpness table
unsigned int *sharpness=new unsigned int[ssize];
radon(img, 1, sharpness);
radon(img, -1, sharpness);
unsigned int i, imax=0;
unsigned int vmax=0;
double sum=0.;
for(i=0; i<ssize; i++){
unsigned int s=sharpness[i];
if(s>vmax){
imax=i;
vmax=s;
}
sum+=s;
}
unsigned int h=img.height();
if(vmax<=3*sum/h){ // Heuristics !!!
return 0;
}
int iskew= (int)imax-int(w2)+1;
delete[] sharpness;
return atan((double)iskew/(8*w2));
}
void
Deskewer::radon(const BWImage& img, int sign, unsigned int sharpness[]){
unsigned short int *p1_, *p2_; // Stored columnwise
unsigned int w2=next_pow2(img.bytewidth());
unsigned int w=img.bytewidth();
unsigned int h=img.height();
unsigned int s=h*w2;
p1_=new unsigned short int[s];
p2_=new unsigned short int[s];
// Fill in the first table
memset(p1_, 0, sizeof(unsigned short int)*s);
unsigned int ir, ic;
unsigned char const *bitcount=BitUtil::bitcount();
for(ir=0; ir<h; ir++){
unsigned char const *scl=img.scanline(ir);
for(ic=0; ic<w; ic++){
if(sign>0){
p1_[h*ic+ir]=bitcount[scl[w-1-ic]];
}else{
p1_[h*ic+ir]=bitcount[scl[ic]];
}
}
}
// Iterate
unsigned short int *x1=p1_;
unsigned short int *x2=p2_;
unsigned int step=1;
for(;;){
unsigned int i;
for(i=0; i<w2; i+=2*step){
unsigned int j;
for(j=0; j<step; j++){
// Columns-sources:
unsigned short int *s1=x1+h*(i+j);
unsigned short int *s2=x1+h*(i+j+step);
// Columns-targets:
unsigned short int *t1=x2+h*(i+2*j);
unsigned short int *t2=x2+h*(i+2*j+1);
unsigned int m;
for(m=0; m<h; m++){
t1[m]=s1[m];
t2[m]=s1[m];
if(m+j<h){
t1[m]+=s2[m+j];
}
if(m+j+1<h){
t2[m]+=s2[m+j+1];
}
}
}
}
// Swap the tables:
unsigned short int *aux=x1;
x1=x2;
x2=aux;
// Increase the step:
step*=2;
if(step>=w2) break;
}
// Now, compute the sum of squared finite differences:
for(ic=0; ic<w2; ic++){
unsigned int acc=0;
unsigned short int *col=x1+h*ic;
for(ir=0; ir+1<h; ir++){
int diff=(int)(col[ir])-(int)(col[ir+1]);
acc+=diff*diff;
}
sharpness[w2-1+sign*ic]=acc;
}
delete[] p1_;
delete[] p2_;
}
}//namespace pagetools
|