2 This is a Optical-Character-Recognition program
3 Copyright (C) 2000-2007 Joerg Schulenburg
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 see README for EMAIL-address
28 /* measure mean thickness as an criteria for big chars */
29 int mean_thickness( struct box *box2 ){
30 int mt=0, i, y, dx=box2->x1-box2->x0+1, dy;
31 for (y=box2->y0+1; y<box2->y1; y++) {
32 i=loop(box2->p,box2->x0+0,y,dx,JOB->cfg.cs,0,RI);
33 i=loop(box2->p,box2->x0+i,y,dx,JOB->cfg.cs,1,RI);
36 dy = box2->y1 - box2->y0 - 1;
37 if (dy) mt=(mt+dy/2)/dy;
41 /* ---- remove dust ---------------------------------
42 What is dust? I think, this is a very small pixel cluster without
43 neighbours. Of course not all dust clusters can be detected correct.
44 This feature should be possible to switch off via option.
45 -> may be, all clusters should be stored here?
46 speed is very slow, I know, but I am happy that it is working well
48 int remove_dust( job_t *job ){
49 /* new dust removing */
50 /* FIXME jb:remove pp */
51 pix *pp = &job->src.p;
52 int i1,i,j,x,y,x0,x1,y0,y1,nC,sX,sY,sP, cs,vvv=job->cfg.verbose;
54 #define HISTSIZE 220 /* histogramm */
56 cs=job->cfg.cs; sP=sX=sY=nC=0;
58 * count number of black pixels within a box and store it in .dots
59 * later .dots is re-used for number of objects belonging to the character
60 * should be done in the flood-fill algorithm
61 * volume of white pixels is estimated to big here (left/right rot)
62 * ToDo: mean thickness of char lines?
63 * or interval nesting (minP..maxP) to remove outriders
66 for (i1=0;i1<HISTSIZE;i1++) histo[i1]=0;
67 /* mean value over every black object which is big enough */
68 for_each_data(&(job->res.boxlist)) {
69 box2 = (struct box *)list_get_current(&(job->res.boxlist));
70 if (!box2->num_frames) continue;
71 if (box2->frame_vol[0]<0) continue; /* don't count inner holes */
72 j = abs(box2->frame_vol[0]);
73 if ((box2->y1-box2->y0+1)>3) {
74 nC++; /* only count potential chars v0.42 */
75 sX+=box2->x1 - box2->x0 + 1;
76 sY+=box2->y1 - box2->y0 + 1;
79 if (j<HISTSIZE) histo[j]++;
80 } end_for_each(&(job->res.boxlist));
82 if (job->cfg.dust_size < 0 && nC > 0) { /* auto detection */
83 /* this formula is empirically, high resolution scans have bigger dust */
84 /* maximum allowed dustsize (min=4*7 ca. 32)
85 * does not work for background pattern!
87 job->cfg.dust_size = ( ( sX/nC ) * ( sY/nC ) + 16) / 32;
88 if (vvv) fprintf(stderr, "# dust size detection, vol num"
89 " #obj=%d maxDust=%d mpixel= %3d mxy= %2d %2d",
90 nC, job->cfg.dust_size, sP/nC, sX/nC, sY/nC);
91 /* we assume that for random dust applies histo[i+1]<histo[i] */
92 for (i=1;i+3<HISTSIZE;i++){
93 if (vvv) fprintf(stderr,"\n# dust size histogram %3d %5d",i,histo[i]);
94 if (histo[i]>=nC) continue; /* v0.42 lot of pixels -> bg pattern < 3 */
95 if (i>=job->cfg.dust_size) break; /* maximum = mean size / 32 */
96 if (histo[i+1]==0) break; /* bad statistic */
97 if ((histo[i+2]+histo[i+3])
98 >=(histo[i] +histo[i+1])) break; /* no noise, but to late? */
99 if ( histo[i-1] > 1024*histo[i] &&
100 2*histo[i+1] >=histo[i]) break; /* bg pattern */
102 if (vvv) fprintf(stderr," break");
103 if (vvv) for (i1=0,j=i+1;j<HISTSIZE;j++) {
104 /* compressed, output only if something is changing */
105 if (j==HISTSIZE-1 || histo[j]!=histo[j-1] || histo[j]!=histo[j+1]) {
106 fprintf(stderr,"\n# dust size histogram %3d %5d",j,histo[j]);
107 if (++i1>20) break; /* dont do excessive output */
110 job->cfg.dust_size=i-1;
111 /* what is the statistic of random dust?
112 * if we have p pixels on a x*y image we should have
113 * (p/(x*y))^1 * (x*y) = p singlets
114 * (p/(x*y))^2 * (x*y) = p^2/(x*y) doublets and
115 * (p/(x*y))^3 * (x*y) = p^3/(x*y)^2 triplets
117 if (vvv) fprintf(stderr,"\n# auto dust size = %d nC= %3d .. %3d"
118 " avD= %2d %2d .. %2d %2d\n",
119 job->cfg.dust_size, nC, job->res.numC,
120 (job->res.sumX+job->res.numC/2)/job->res.numC,
121 (job->res.sumY+job->res.numC/2)/job->res.numC, sX/nC, sY/nC);
123 if (job->cfg.dust_size)
126 fprintf(stderr,"# remove dust of size %2d",job->cfg.dust_size);
127 /* Warning: better use (1/(x*y))^2 as 1/((x*y)^2),
128 * because (x*y)^2 may overflow */
129 fprintf(stderr," histo=%d,%d(?=%d),%d(?=%d),...\n# ...",
130 histo[1],histo[2],histo[1]*histo[1]/(pp->x*pp->y),
131 histo[3], histo[1]*histo[1]/(pp->x*pp->y)
132 *histo[1]/(pp->x*pp->y));
135 for_each_data(&(job->res.boxlist)) {
136 box2 = (struct box *)list_get_current(&(job->res.boxlist));
137 x0=box2->x0;x1=box2->x1;y0=box2->y0;y1=box2->y1; /* box */
138 j=abs(box2->frame_vol[0]);
139 if(j<=job->cfg.dust_size) /* remove this tiny object */
140 { /* here we should distinguish dust and i-dots,
141 * may be we should sort out dots to a seperate dot list and
142 * after line detection decide, which is dust and which not
143 * dust should be removed to make recognition easier (ToDo)
146 if(get_bw((3*x0+x1)/4,(x0+3*x1)/4,y1+y1-y0+1,y1+8*(y1-y0+1),pp,cs,1))
147 continue; /* this idea was to simple, see kscan003.jpg sample */
149 /* remove from average */
151 job->res.sumX-=x1-x0+1;
152 job->res.sumY-=y1-y0+1;
153 /* remove pixels (should only be done with dust) */
155 for(y=y0;y<=y1;y++){ put(pp,x,y,0,255&~7); }
156 /* remove from list */
157 list_del(&(job->res.boxlist),box2);
160 i++; /* count as dust particle */
163 } end_for_each(&(job->res.boxlist));
164 if(vvv)fprintf(stderr," %3d cluster removed, nC= %3d\n",i,job->res.numC);
166 /* reset dots to 0 and remove white pixels (new) */
168 for_each_data(&(job->res.boxlist)) {
169 box2 = ((struct box *)list_get_current(&(job->res.boxlist)));
170 if (box2->frame_vol[0]<0) continue; /* for black areas only */
171 x0=box2->x0;x1=box2->x1;y0=box2->y0;y1=box2->y1; /* box */
172 if (x1-x0>16 && y1-y0>30) /* only on large enough chars */
173 for(x=x0+1;x<=x1-1;x++)
174 for(y=y0+1;y<=y1-1;y++){
175 if( pixel_atp(pp,x ,y )>=cs
176 && pixel_atp(pp,x-1,y ) <cs
177 && pixel_atp(pp,x+1,y ) <cs
178 && pixel_atp(pp,x ,y-1) <cs
179 && pixel_atp(pp,x ,y+1) <cs ) /* remove it */
181 put(pp,x,y,0,0); i++; /* (x and 0) or 0 */
184 } end_for_each(&(job->res.boxlist));
185 if (vvv) fprintf(stderr,"# ... %3d white pixels removed, cs=%d nC= %3d\n",
190 /* ---- smooth big chars ---------------------------------
191 * Big chars often do not have smooth borders, which let fail
192 * the engine. Here we smooth the borders of big chars (>7x16).
193 * Smoothing is important for b/w scans, where we often have
194 * comb like pattern on a vertikal border. I also received
195 * samples with lot of white pixels (sample: 04/02/25).
196 * ToDo: obsolete if vector code is complete
198 int smooth_borders( job_t *job ){
199 pix *pp = &job->src.p;
200 int ii=0,x,y,x0,x1,y0,y1,dx,dy,cs,i0,i1,i2,i3,i4,n1,n2,
201 cn[8],cm,vvv=job->cfg.verbose; /* dust found */
203 cs=job->cfg.cs; n1=n2=0;
204 if(vvv){ fprintf(stderr,"# smooth big chars 7x16 cs=%d",cs); }
205 /* filter for each big box */
206 for_each_data(&(job->res.boxlist)) { n2++; /* count boxes */
207 box2 = (struct box *)list_get_current(&(job->res.boxlist));
208 /* do not touch small characters! but how we define small characters? */
209 if (box2->x1-box2->x0+1<7 || box2->y1-box2->y0+1<16 ) continue;
210 if (box2->c==PICTURE) continue;
211 if (mean_thickness(box2)<3) continue;
212 n1++; /* count boxes matching big-char criteria */
213 x0=box2->x0; y0=box2->y0;
214 x1=box2->x1; y1=box2->y1;
215 dx=x1-x0+1; dy=y1-y0-1;
217 * dont change to much! only change if absolutely sure!
221 * we should also avoid removing lines by sytematic remove
222 * from left end to the right, so we concern also about distance>1
224 for(x=box2->x0;x<=box2->x1;x++)
225 for(y=box2->y0;y<=box2->y1;y++){ /* filter out high frequencies */
226 /* this is a very primitive solution, only for learning */
227 cn[0]=getpixel(pp,x-1,y);
228 cn[4]=getpixel(pp,x+1,y); /* horizontal */
229 cn[2]=getpixel(pp,x,y-1);
230 cn[6]=getpixel(pp,x,y+1); /* vertical */
231 cn[1]=getpixel(pp,x-1,y-1);
232 cn[3]=getpixel(pp,x+1,y-1); /* diagonal */
233 cn[7]=getpixel(pp,x-1,y+1);
234 cn[5]=getpixel(pp,x+1,y+1);
236 /* check for 5 other and 3 same surrounding pixels */
238 if ((cn[i0 ]<cs)==(cm<cs)
239 && (cn[(i0+7) & 7]<cs)!=(cm<cs)) break; /* first same */
241 if ((cn[(i0+i1) & 7]<cs)!=(cm<cs)) break; /* num same */
243 if ((cn[(i0+i1+i2) & 7]<cs)==(cm<cs)) break; /* num other */
244 cn[0]=getpixel(pp,x-2,y);
245 cn[4]=getpixel(pp,x+2,y); /* horizontal */
246 cn[2]=getpixel(pp,x,y-2);
247 cn[6]=getpixel(pp,x,y+2); /* vertical */
248 cn[1]=getpixel(pp,x-2,y-2);
249 cn[3]=getpixel(pp,x+2,y-2); /* diagonal */
250 cn[7]=getpixel(pp,x-2,y+2);
251 cn[5]=getpixel(pp,x+2,y+2);
252 /* check for 5 other and 3 same surrounding pixels */
254 if ((cn[i0 ]<cs)==(cm<cs)
255 && (cn[(i0+7) & 7]<cs)!=(cm<cs)) break; /* first same */
257 if ((cn[(i0+i3) & 7]<cs)!=(cm<cs)) break; /* num same */
259 if ((cn[(i0+i3+i4) & 7]<cs)==(cm<cs)) break; /* num other */
260 if (i1<=3 && i2>=5 && i3>=3 && i4>=3) { /* change only on borders */
261 ii++; /* white : black */
262 put(pp,x,y,7,((cm<cs)?(cs|32):cs/2)&~7);
264 printf(" x y i0 i1 i2 i3 i4 cm new cs %3d %3d"
265 " %3d %3d %3d %3d %3d %3d %3d %3d\n",
266 x-box2->x0,y-box2->y0,i0,i1,i2,i3,i3,cm,getpixel(pp,x,y),cs);
270 #if 0 /* debugging */
273 } end_for_each(&(job->res.boxlist));
274 if(vvv)fprintf(stderr," ... %3d changes in %d of %d\n",ii,n1,n2);
278 /* test if a corner of box1 is within box2 */
279 int box_nested( struct box *box1, struct box *box2){
280 /* box1 in box2, +1..-1 frame for pixel-patterns */
281 if ( ( ( box1->x0>=box2->x0-1 && box1->x0<=box2->x1+1 )
282 || ( box1->x1>=box2->x0-1 && box1->x1<=box2->x1+1 ) )
283 && ( ( box1->y0>=box2->y0-1 && box1->y0<=box2->y1+1 )
284 || ( box1->y1>=box2->y0-1 && box1->y1<=box2->y1+1 ) ) )
289 /* test if box1 is within box2 */
290 int box_covered( struct box *box1, struct box *box2){
291 /* box1 in box2, +1..-1 frame for pixel-patterns */
292 if ( ( box1->x0>=box2->x0-1 && box1->x1<=box2->x1+1 )
293 && ( box1->y0>=box2->y0-1 && box1->y1<=box2->y1+1 ) )
298 /* ---- remove pictures ------------------------------------------
299 * may be, not deleting or moving to another list is much better!
300 * should be renamed to remove_pictures and border boxes
302 int remove_pictures( job_t *job){
303 struct box *box4,*box2;
304 int j=0, j2=0, num_del=0;
306 if (job->cfg.verbose)
307 fprintf(stderr, "# "__FILE__" L%d: remove pictures\n# ...",
310 /* ToDo: output a list for picture handle scripts */
313 for_each_data(&(job->res.boxlist)) {
314 box4 = (struct box *)list_get_current(&(job->res.boxlist));
315 if (box4->c==PICTURE) j++; else j2++;
316 } end_for_each(&(job->res.boxlist));
317 if (job->cfg.verbose)
318 fprintf(stderr," status: pictures= %d other= %d nC= %d\n# ...",
319 j, j2, job->res.numC);
321 /* remove table frames */
322 if (job->res.numC > 8)
323 for_each_data(&(job->res.boxlist)) {
324 box2 = (struct box *)list_get_current(&(job->res.boxlist));
326 && box2->x1-box2->x0+1>box2->p->x/2 /* big table? */
327 && box2->y1-box2->y0+1>box2->p->y/2 ){ j=0;
328 /* count boxes nested with the picture */
329 for_each_data(&(job->res.boxlist)) {
330 box4 = (struct box *)list_get_current(&(job->res.boxlist));
331 if( box4 != box2 ) /* not count itself */
332 if (box_nested(box4,box2)) j++; /* box4 in box2 */
333 } end_for_each(&(job->res.boxlist));
334 if( j>8 ){ /* remove box if more than 8 chars are within box */
335 list_del(&(job->res.boxlist), box2); /* does not work proper ?! */
336 free_box(box2); num_del++;
339 } end_for_each(&(job->res.boxlist));
340 if (job->cfg.verbose)
341 fprintf(stderr, " deleted= %d pictures (table frames)\n# ...",
345 /* remove dark-border-boxes (typical for hard copy of book site,
346 * or spam random border) */
347 if (job->res.numC > 1) /* dont remove the only char */
348 for_each_data(&(job->res.boxlist)) {
349 box2 = (struct box *)list_get_current(&(job->res.boxlist));
350 if (box2->c!=PICTURE) continue; // ToDo: PICTUREs set already?
351 if ( box2->x1-box2->x0+1 > box2->p->x/2
352 && box2->y1-box2->y0+1 > box2->p->y/2 ) continue;
354 if (box2->x0==0) j++;
355 if (box2->y0==0) j++; /* on border? */
356 if (box2->x1==box2->p->x-1) j++;
357 if (box2->y1==box2->p->y-1) j++;
358 if (j>2){ /* ToDo: check corner pixel */
361 if (getpixel(box2->p,box2->x0,box2->y0)<cs) j++;
362 if (getpixel(box2->p,box2->x1,box2->y0)<cs) j++;
363 if (getpixel(box2->p,box2->x0,box2->y1)<cs) j++;
364 if (getpixel(box2->p,box2->x1,box2->y1)<cs) j++;
366 list_del(&(job->res.boxlist), box2);
367 free_box(box2); num_del++;
370 } end_for_each(&(job->res.boxlist));
371 if (job->cfg.verbose)
372 fprintf(stderr, " deleted= %d pictures (on border)\n# ...",
378 for_each_data(&(job->res.boxlist)) {
379 box4 = (struct box *)list_get_current(&(job->res.boxlist));
380 if( box4->c==PICTURE ) j++; else j2++;
381 } end_for_each(&(job->res.boxlist));
382 if (job->cfg.verbose)
383 fprintf(stderr," status: pictures= %d other= %d nC= %d\n# ...",
384 j, j2, job->res.numC);
386 for(j=1;j;){ j=0; /* this is only because list_del does not work */
387 /* can be slow on gray images */
388 for_each_data(&(job->res.boxlist)) {
389 box2 = (struct box *)list_get_current(&(job->res.boxlist));
390 if( box2->c==PICTURE && box2->num_ac==0)
391 for(j=1;j;){ /* let it grow to max before leave */
393 /* find boxes nested with the picture and remove */
394 /* its for pictures build by compounds */
395 for_each_data(&(job->res.boxlist)) {
396 box4 = (struct box *)list_get_current(&(job->res.boxlist));
397 if( box4!=box2 /* not destroy self */
398 && (box4->num_ac==0) /* dont remove barcodes etc. */
399 && (/* box4->c==UNKNOWN || */
400 box4->c==PICTURE) ) /* dont remove valid chars */
402 /* box4 in box2, +1..-1 frame for pixel-patterns */
403 box_nested(box4,box2)
404 /* or box2 in box4 */
405 || box_nested(box2,box4) /* same? */
407 if ( box4->x1-box4->x0+1>2*job->res.avX
408 || box4->x1-box4->x0+1<job->res.avX/2
409 || box4->y1-box4->y0+1>2*job->res.avY
410 || box4->y1-box4->y0+1<job->res.avY/2
411 || box_covered(box4,box2) ) /* box4 completely within box2 */
412 /* dont remove chars! see rotate45.fig */
414 /* do not remove boxes in inner loop (bug?) ToDo: check why! */
415 /* instead we leave inner loop and mark box4 as valid */
416 if( box4->x0<box2->x0 ) box2->x0=box4->x0;
417 if( box4->x1>box2->x1 ) box2->x1=box4->x1;
418 if( box4->y0<box2->y0 ) box2->y0=box4->y0;
419 if( box4->y1>box2->y1 ) box2->y1=box4->y1;
420 j=1; /* mark box4 as valid */
421 break; /* and leave inner loop */
423 } end_for_each(&(job->res.boxlist));
424 if (j!=0 && box4!=NULL) { /* check for valid box4 */
426 list_del(&(job->res.boxlist), box4); /* does not work proper ?! */
427 free_box(box4); /* break; ToDo: necessary to leave after del??? */
432 } end_for_each(&(job->res.boxlist));
435 if (job->cfg.verbose)
436 fprintf(stderr, " deleted= %d nested pictures\n# ...", num_del);
438 /* output a list for picture handle scripts */
441 for_each_data(&(job->res.boxlist)) {
442 box4 = (struct box *)list_get_current(&(job->res.boxlist));
443 if( box4->c==PICTURE ) {
444 fprintf(stderr," found picture at %4d %4d size %4d %4d\n# ...",
445 box4->x0, box4->y0, box4->x1-box4->x0+1, box4->y1-box4->y0+1 );
448 } end_for_each(&(job->res.boxlist));
449 if (job->cfg.verbose)
450 fprintf(stderr," status: pictures= %d other= %d nC= %d\n",
451 j, j2, job->res.numC);
457 /* ---- remove melted serifs --------------------------------- v0.2.5
459 ##########.######## <-y0
460 ################### like X VW etc.
461 ...###.......###... <-y
464 - can generate new boxes if two characters were glued
466 int remove_melted_serifs( pix *pp ){
467 int x,y,j1,j2,j3,j4,i2,i3,i,ii,ni,cs,x0,x1,xa,xb,y0,y1,vvv=JOB->cfg.verbose;
468 struct box *box2, *box3;
469 progress_counter_t *pc = NULL;
471 cs=JOB->cfg.cs; i=0; ii=0; ni=0;
472 for_each_data(&(JOB->res.boxlist)) {
474 } end_for_each(&(JOB->res.boxlist));
475 pc = open_progress(ni,"remove_melted_serifs");
478 if(vvv){ fprintf(stderr,"# searching melted serifs ..."); }
479 for_each_data(&(JOB->res.boxlist)) {
480 box2 = (struct box *)list_get_current(&(JOB->res.boxlist));
481 if (box2->c != UNKNOWN) continue; /* dont try on pictures */
482 x0=box2->x0; x1=box2->x1;
483 y0=box2->y0; y1=box2->y1; /* box */
486 j1+=loop(pp,j1,y0 ,x1-x0,cs,0,RI);
487 x =loop(pp,j1,y0 ,x1-x0,cs,1,RI); if(j1+x>x1+1) break;
488 y =loop(pp,j1,y0+1,x1-x0,cs,1,RI); if(y>x) x=y; if(j1+x>x1+1) break;
489 /* measure mean thickness of serif */
490 for(j2=j3=j4=0,i2=j1;i2<j1+x;i2++){
491 i3 =loop(pp,j1,y0 ,y1-y0,cs,0,DO); if(8*i3>y1-y0) break;
492 i3+=loop(pp,j1,y0+i3,y1-y0,cs,1,DO); if(8*i3>y1-y0) break;
493 if(8*i3<y1-y0){ j2+=i3; j3++; }
494 } if(j3==0){ j1+=x; continue; }
495 y = y0+(j2+j3-1)/j3+(y1-y0+1)/32;
497 /* check if really melted serifs */
498 if( loop(pp,j1,y,x1-x0,cs,0,RI)<1 ) { j1+=x; continue; }
499 if(num_cross(j1 ,j1+x,y,y,pp,cs) < 2 ){ j1+=x;continue; }
500 j2 = j1 + loop(pp,j1,y,x1-x0,cs,0,RI);
501 j2 = j2 + loop(pp,j2,y,x1-x0,cs,1,RI);
502 i3 = loop(pp,j2,y,x1-x0,cs,0,RI); if(i3<2){j1+=x;continue;}
504 j3 = j2 + loop(pp,j2,y ,x1-j2,cs,0,RI);
505 i3 = j2 + loop(pp,j2,y+1,x1-j2,cs,0,RI); if(i3>j3)j3=i3;
506 j3 = j3 + loop(pp,j3,y ,x1-j3,cs,1,RI);
507 i3 = loop(pp,j3,y ,x1-j3,cs,0,RI);
508 if(i3<2 || j3>=j1+x){j1+=x;continue;}
514 for(y=0;y<(y1-y0+1+4)/8;y++)put(pp,j2,y0+y,255,128+64); /* clear highest bit */
516 fprintf(stderr,"\n");
518 fprintf(stderr,"# melted serifs corrected on %d %d j1=%d j3=%d",
519 j2-x0, y, j1-x0, j3-x0);
521 for(xb=0,xa=0;xa<(x1-x0+4)/8;xa++){ /* detect vertical gap */
523 if(box2->m3>y0 && 2*y1>box2->m3+box2->m4) i3=box2->m3; /* some IJ */
524 if( loop(pp,j2-xa,i3,i3-y0,cs,0,UP) > (y1-y0+1)/2
525 && loop(pp,j2,(y0+y1)/2,xa+1,cs,0,LE) >=xa ){ xb=-xa; break; }
526 if( loop(pp,j2+xa,i3,i3-y0,cs,0,UP) > (y1-y0+1)/2
527 && loop(pp,j2,(y0+y1)/2,xa+1,cs,0,RI) >=xa ){ xb= xa; break; }
529 if( get_bw(j2 ,j2 ,y0,(y0+y1)/2,pp,cs,1) == 0
530 && get_bw(j2+xb,j2+xb,(y0+y1)/2,i3,pp,cs,1) == 0 )
532 box3=malloc_box(box2);
534 box2->x0=j2+1; x1=box2->x1;
535 cut_box(box2); /* cut vectors outside the box */
537 box3->num=JOB->res.numC;
538 list_ins(&(JOB->res.boxlist),box2,box3); JOB->res.numC++; ii++; /* insert box3 before box2 */
539 if(vvv&4) fprintf(stderr," => splitted");
540 j1=x0=box2->x0; x=0; /* hopefully ok, UVW */
545 /* same on lower serifs -- change this later to better function
548 // #################### <-y1
552 j1+=loop(pp,j1,y1 ,x1-x0,cs,0,RI);
553 x =loop(pp,j1,y1 ,x1-x0,cs,1,RI); if(j1+x>x1+1) break;
554 y =loop(pp,j1,y1-1,x1-x0,cs,1,RI); if(y>x) x=y; if(j1+x>x1+1) break;
555 /* measure mean thickness of serif */
556 for(j2=j3=j4=0,i2=j1;i2<j1+x;i2++){
557 i3 =loop(pp,j1,y1 ,y1-y0,cs,0,UP); if(8*i3>y1-y0) break;
558 i3+=loop(pp,j1,y1-i3,y1-y0,cs,1,UP); if(8*i3>y1-y0) break;
559 if(8*i3<y1-y0){ j2+=i3; j3++; }
560 } if(j3==0){ j1+=x; continue; }
561 y = y1-(j2+j3-1)/j3-(y1-y0+1)/32;
563 /* check if really melted serifs */
564 if( loop(pp,j1,y,x1-x0,cs,0,RI)<1 ) { j1+=x; continue; }
565 if(num_cross(j1 ,j1+x,y,y,pp,cs) < 2 ){ j1+=x;continue; }
566 j2 = j1 + loop(pp,j1,y,x1-x0,cs,0,RI);
567 j2 = j2 + loop(pp,j2,y,x1-x0,cs,1,RI);
568 i3 = loop(pp,j2,y,x1-x0,cs,0,RI); if(i3<2){j1+=x;continue;}
570 j3 = j2 + loop(pp,j2,y ,x1-j2,cs,0,RI);
571 i3 = j2 + loop(pp,j2,y-1,x1-j2,cs,0,RI); if(i3>j3)j3=i3;
572 j3 = j3 + loop(pp,j3,y ,x1-j3,cs,1,RI);
573 i3 = loop(pp,j3,y,x1-j3,cs,0,RI);
574 if(i3<2 || j3>=j1+x){j1+=x;continue;}
577 /* y =y1-(y1-y0+1+4)/8; */
581 for(i3=0;i3<(y1-y0+1+4)/8;i3++)
582 put(pp,j2,y1-i3,255,128+64); /* clear highest bit */
584 fprintf(stderr,"\n");
586 fprintf(stderr,"# melted serifs corrected on %d %d j1=%d j3=%d",j2-x0,y-y0,j1-x0,j3-x0);
588 for(xb=0,xa=0;xa<(x1-x0+4)/8;xa++){ /* detect vertical gap */
589 if( loop(pp,j2-xa,y0,y1-y0,cs,0,DO) > (y1-y0+1)/2
590 && loop(pp,j2,(y0+y1)/2,xa+1,cs,0,LE) >=xa ){ xb=-xa; break; }
591 if( loop(pp,j2+xa,y0,y1-y0,cs,0,DO) > (y1-y0+1)/2
592 && loop(pp,j2,(y0+y1)/2,xa+1,cs,0,RI) >=xa ){ xb= xa; break; }
594 if( get_bw(j2 ,j2 ,(y0+y1)/2,y1,pp,cs,1) == 0
595 && get_bw(j2+xb,j2+xb,y0,(y0+y1)/2,pp,cs,1) == 0 )
597 box3=malloc_box(box2);
599 box2->x0=j2; x1=box2->x1;
600 cut_box(box2); /* cut vectors outside the box */
602 box3->num=JOB->res.numC;
603 list_ins(&(JOB->res.boxlist),box2,box3); JOB->res.numC++; ii++;
604 /* box3,box2 in correct order??? */
605 if(vvv&4) fprintf(stderr," => splitted");
606 j1=x0=box2->x0; x=0; /* hopefully ok, NMK */
612 } end_for_each(&(JOB->res.boxlist));
614 if(vvv)fprintf(stderr," %3d cluster corrected, %d new boxes\n",i,ii);
618 /* remove black borders often seen on bad scanned copies of books
619 - dust around the border
621 int remove_rest_of_dust() {
622 int i1, i2, vvv = JOB->cfg.verbose, x0, x1, y0, y1, cnt=0;
623 struct box *box2, *box4;
624 progress_counter_t *pc = NULL;
626 i1 = i2 = 0; /* counter for removed boxes */
628 fprintf(stderr, "# detect dust (avX,nC), ... ");
629 /* remove fragments from border */
630 for_each_data(&(JOB->res.boxlist)) {
631 box2 = (struct box *)list_get_current(&(JOB->res.boxlist));
632 if (box2->c == UNKNOWN) {
633 x0 = box2->x0; x1 = box2->x1;
634 y0 = box2->y0; y1 = box2->y1; /* box */
635 /* box in char ??? */
636 if ( 2 * JOB->res.numC * (y1 - y0 + 1) < 3 * JOB->res.sumY
637 && ( y1 < box2->p->y/4 || y0 > 3*box2->p->y/4 ) /* not single line */
638 && JOB->res.numC > 1 /* do not remove everything */
639 && ( box2->m4 == 0 ) ) /* remove this */
641 JOB->res.numC--; /* ToDo: dont count tiny pixels */
642 /* ToDo: res.sumX,Y must also be corrected */
644 list_del(&(JOB->res.boxlist), box2);
648 } end_for_each(&(JOB->res.boxlist));
650 pc = open_progress(JOB->res.boxlist.n,"remove_dust2");
651 for_each_data(&(JOB->res.boxlist)) {
652 box2 = (struct box *)list_get_current(&(JOB->res.boxlist));
654 if (box2->c == PICTURE) continue;
655 x0 = box2->x0; x1 = box2->x1;
656 y0 = box2->y0; y1 = box2->y1; /* box */
657 /* remove tiny box2 if to far away from bigger boxes */
658 /* ToDo: remove clouds of tiny pixels (count near small, compare with num bigger) */
659 /* 0.42: remove far away pixel? ToDo: do it at earlier? */
660 if (x1-x0+1<3 && y1-y0+1<3){
662 int found=0; /* nearest bigger box */
663 /* search near bigger box */
664 for_each_data(&(JOB->res.boxlist)) {
665 box4 = (struct box *)list_get_current(&(JOB->res.boxlist));
666 if (found || box4 == box2) continue;
667 if (box4->x1-box4->x0+1<3 && box4->y1-box4->y0+1<3) continue;
668 xs = box4->x1-box4->x0+1;
669 ys = box4->y1-box4->y0+1;
670 xn = abs((box4->x0+box4->x1)/2 - box2->x0);
671 yn = abs((box4->y0+box4->y1)/2 - box2->y0);
672 if (2*xn < 3*xs && 2*yn < 3*ys) { found=1; }
673 } end_for_each(&(JOB->res.boxlist));
674 if (!found) { /* found nothing, box2 to far from big boxes */
676 list_del(&(JOB->res.boxlist), box2);
680 } end_for_each(&(JOB->res.boxlist));
683 fprintf(stderr, " %3d + %3d boxes deleted, nC= %d ?\n",
684 i1, i2, JOB->res.numC);