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
27 /* do we need #include <math.h>? conflicts with INFINITY in unicode.h */
31 /* for sorting letters by position on the image
32 / ToDo: - use function same line like this or include lines.m1 etc. */
33 int box_gt(struct box *box1, struct box *box2) {
35 if (box1->line > box2->line)
37 if (box1->line < box2->line)
39 if (box1->x0 > box2->x1) // before
41 if (box1->x1 < box2->x0) // before
43 if (box1->x0 > box2->x0) // before, overlapping!
49 /* --- copy part of pix p into new pix b ---- len=10000
50 * Returns: 0 on success, 1 on error.
51 * naming it as copybox isnt very clever, because it dont have to do with the
52 * char boxes (struct box)
54 int copybox (pix * p, int x0, int y0, int dx, int dy, pix * b, int len) {
58 if (b->p == NULL || dx < 0 || dy < 0 || dx * dy > len) {
59 fprintf(stderr, " error-copybox x=%5d %5d d=%5d %5d\n", x0, y0, dx, dy);
66 #ifdef FASTER_INCOMPLETE
67 for (y = 0; y < dy; y++)
68 memcpy(&pixel_atp(b, 0, y), &pixel_atp(p, x0, y + y0 ), dx);
71 for (y = 0; y < dy; y++)
72 for (x = 0; x < dx; x++)
73 pixel_atp(b, x, y) = getpixel(p, x + x0, y + y0);
79 /* reset table of alternative chars (and free memory) */
80 int reset_box_ac(struct box *box){
82 for (i=0; i<box->num_ac; i++)
84 /* fprintf(stderr,"DBG free_s[%d] %p %s\n",i,box->tas[i],box->tas[i]); */
86 box->tas[i]=0; /* prevent double freeing */
88 box->num_ac=0; /* mark as freed */
92 /* ini or copy a box: get memory for box and initialize the memory */
93 struct box *malloc_box (struct box *inibox) {
97 buf = (struct box *) malloc(sizeof(struct box));
101 memcpy(buf, inibox, sizeof(struct box));
102 /* only pointer are copied, we want to copy the contents too */
103 for (i=0;i<inibox->num_ac;i++) {
104 if (inibox->tas[i]) {
105 buf->tas[i]=(char *)malloc(strlen(inibox->tas[i])+1);
106 memcpy(buf->tas[i], inibox->tas[i], strlen(inibox->tas[i])+1);
110 else { /* ToDo: init it */
114 /* fprintf(stderr,"\nDBG ini_box %p",buf); */
118 /* free memory of box */
119 int free_box (struct box *box) {
121 /* fprintf(stderr,"DBG free_box %p\n",box); out_x(box); */
122 reset_box_ac(box); /* free alternative char table */
123 free(box); /* free the box memory */
127 /* simplify the vectorgraph,
128 * but what is the best way?
129 * a) melting two neighbouring vectors with nearly same direction?
130 * (nearest angle to pi)
131 * b) melting three neigbours with smallest area?
133 * mode = 0 - only lossless
134 * mode = 1 - reduce one vector, smallest possible loss
135 * mode = 2 - remove jitter (todo, or somewhere else)
136 * ToDo: include also loop around (last - first element)
137 * ToDo: reduce by 10..50%
139 int reduce_vectors ( struct box *box1, int mode ) {
140 int i1, i2, nx, ny, mx, my, len,
141 minlen=1024, /* minlength of to neighbouring vectors */
142 besti1=0, /* frame for best reduction */
143 besti2=2; /* vector replacing its predecessor */
144 double sprod, maxsprod=-1;
145 if (mode!=1) fprintf(stderr,"ERR not supported yet, ToDo\n");
146 for (i2=1,i1=0; i1<box1->num_frames; i1++) { /* every frame */
147 for (;i2<box1->num_frame_vectors[i1]-1; i2++) { /* every vector */
149 nx = box1->frame_vector[i2-0][0] - box1->frame_vector[i2-1][0];
150 ny = box1->frame_vector[i2-0][1] - box1->frame_vector[i2-1][1];
152 mx = box1->frame_vector[i2+1][0] - box1->frame_vector[i2-0][0];
153 my = box1->frame_vector[i2+1][1] - box1->frame_vector[i2-0][1];
154 /* angle is w = a*b/(|a|*|b|) = 1 means parallel */
155 /* normalized: minimize w^2 = (a*b/(|a|*|b|)-1)^2 */
156 /* -1=90grd, 0=0grd, -2=180grd */
157 sprod = /* fabs */(abs(nx*mx+ny*my)*(nx*mx+ny*my)
158 /(1.*(nx*nx+ny*ny)*(mx*mx+my*my))-1);
159 /* we dont include math.h because INFINITY conflicts to unicode,h */
160 if (sprod<0) sprod=-sprod;
161 len = (mx*mx+my*my)*(nx*nx+ny*ny); /* sum lengths^2 */
162 // ..c ###c ... .. ...
163 // .b. len=2+2 #b.. len=2+5 #bc len=1+2 bc len=1+1 b#a len=4+5
164 // a.. spr=0 a... spr=1/10 a.. spr=1/4 a. spr=1 ##c spr=9/5
166 if ( len* sprod* sprod* sprod* sprod
167 <minlen*maxsprod*maxsprod*maxsprod*maxsprod
168 || maxsprod<0) /* Bad! ToDo! */
169 { maxsprod=sprod; besti1=i1; besti2=i2; minlen=len; }
172 if (box1->num_frames>0)
173 for (i2=besti2; i2<box1->num_frame_vectors[ box1->num_frames-1 ]-1; i2++) {
174 box1->frame_vector[i2][0]=box1->frame_vector[i2+1][0];
175 box1->frame_vector[i2][1]=box1->frame_vector[i2+1][1];
177 for (i1=besti1; i1<box1->num_frames; i1++)
178 box1->num_frame_vectors[i1]--;
179 // fprintf(stderr,"\nDBG_reduce_vectors i= %d nv= %d sprod=%f len2=%d\n# ...",
180 // besti2,box1->num_frame_vectors[ box1->num_frames-1 ],maxsprod,minlen);
185 /* add the contents of box2 to box1
186 * especially add vectors of box2 to box1
188 int merge_boxes( struct box *box1, struct box *box2 ) {
190 struct box tmpbox, *bsmaller, *bbigger; /* for mixing and sorting */
191 /* DEBUG, use valgrind to check uninitialized memory */
193 fprintf(stderr,"\nDBG merge_boxes_input:"); out_x(box1); out_x(box2);
195 /* pair distance is to expendable, taking borders is easier */
196 if ((box2->x1 - box2->x0)*(box2->y1 - box2->y0)
197 >(box1->x1 - box1->x0)*(box1->y1 - box1->y0)) {
198 bbigger=box2; bsmaller=box1; }
200 bbigger=box1; bsmaller=box2; }
201 /* ToDo: does not work if a third box is added */
202 if (box2->y0>box1->y1 || box2->y1<box1->y0
203 || box2->x0>box1->x1 || box2->x1<box1->x0) {
204 box1->num_boxes += box2->num_boxes; /* num seperate objects 2=ij */
206 if (box2->num_boxes>box1->num_boxes) box1->num_boxes=box2->num_boxes;
207 box1->num_subboxes += box2->num_subboxes+1; /* num holes 1=abdepq 2=B */
209 box1->dots += box2->dots; /* num i-dots */
210 if ( box2->x0 < box1->x0 ) box1->x0 = box2->x0;
211 if ( box2->x1 > box1->x1 ) box1->x1 = box2->x1;
212 if ( box2->y0 < box1->y0 ) box1->y0 = box2->y0;
213 if ( box2->y1 > box1->y1 ) box1->y1 = box2->y1;
215 if (bbigger->num_frames)
216 i1 = bbigger->num_frame_vectors[ bbigger->num_frames - 1 ];
217 if (bsmaller->num_frames)
218 i2 = bsmaller->num_frame_vectors[ bsmaller->num_frames - 1 ];
219 while (i1+i2 > MaxFrameVectors) {
220 if (i1>i2) { reduce_vectors( bbigger, 1 ); i1--; }
221 else { reduce_vectors( bsmaller, 1 ); i2--; }
223 /* if i1+i2>MaxFrameVectors simplify the vectorgraph */
224 /* if sum num_frames>MaxNumFrames through shortest graph away and warn */
225 /* first copy the bigger box */
226 memcpy(&tmpbox, bbigger, sizeof(struct box));
227 /* attach the smaller box */
228 for (i4=i3=0; i3<bsmaller->num_frames; i3++) {
229 if (tmpbox.num_frames>=MaxNumFrames) break;
231 for (; i4<bsmaller->num_frame_vectors[i3]; i4++) {
232 memcpy(tmpbox.frame_vector[i1],
233 bsmaller->frame_vector[i4],2*sizeof(int));
236 tmpbox.num_frame_vectors[ tmpbox.num_frames ] = i1;
237 tmpbox.frame_vol[ tmpbox.num_frames ] = bsmaller->frame_vol[ i3 ];
238 tmpbox.frame_per[ tmpbox.num_frames ] = bsmaller->frame_per[ i3 ];
240 if (tmpbox.num_frames>=MaxNumFrames) {
241 if (JOB->cfg.verbose)
242 fprintf(stderr,"\nDBG merge_boxes MaxNumFrames reached");
246 /* copy tmpbox to destination */
247 box1->num_frames = tmpbox.num_frames;
248 memcpy(box1->num_frame_vectors,
249 tmpbox.num_frame_vectors,sizeof(int)*MaxNumFrames);
250 memcpy(box1->frame_vol,
251 tmpbox.frame_vol,sizeof(int)*MaxNumFrames);
252 memcpy(box1->frame_per,
253 tmpbox.frame_per,sizeof(int)*MaxNumFrames);
254 memcpy(box1->frame_vector,
255 tmpbox.frame_vector,sizeof(int)*2*MaxFrameVectors);
257 if (JOB->cfg.verbose)
258 fprintf(stderr,"\nDBG merge_boxes_result:"); out_x(box1);
263 /* used for division of glued chars
264 * after a box is splitted into 2, where vectors are copied to both,
265 * vectors outside the new box are cutted and thrown away,
267 * - 1st remove outside vectors with outside neighbours (complete frames?)
268 * add vector on outside vector with inside neighbours
269 * care about connections through box between outside vectors
270 * - 2nd reduce outside crossings (inclusive splitting frames if necessary)
271 * depending on direction (rotation) of outside connections
272 * - 3th shift outside vectors to crossing points
273 * - split add this points, connect only in-out...out-in,
274 * - cutting can result in more objects
275 * ToDo: dont connect --1---2--------3----4-- new-y1 (inside above not drawn)
276 * \ \->>>>-/ / outside
277 * \----<<<<-----/ old-y1
278 * |======| subtractable?
280 * only connect --1---2--------3----4-- new-y1
281 * \>>/ \>>>/ old-y1 outside
285 int cut_box( struct box *box1) {
286 int i1, i2, i3, i4, x, y, lx, ly, dbg=0;
287 if (JOB->cfg.verbose) dbg=1; // debug level, enlarge to get more output
288 if (dbg) fprintf(stderr,"\n cut box x= %3d %3d", box1->x0, box1->y0);
289 /* check if complete frames are outside the box */
290 for (i1=0; i1<box1->num_frames; i1++){
291 if (dbg>2) fprintf(stderr,"\n checking frame %d outside", i1);
292 i2 = ((i1)?box1->num_frame_vectors[ i1-1 ]:0); // this frame
293 i3 = box1->num_frame_vectors[ i1 ]; // next frame
294 for (i4=i2; i4 < i3; i4++) {
295 x = box1->frame_vector[i4][0];
296 y = box1->frame_vector[i4][1];
297 /* break, if one vector is lying inside */
298 if (x>=box1->x0 && x<=box1->x1 && y>=box1->y0 && y<=box1->y1) break;
300 if (i4==i3) { /* all vectors outside */
301 if (dbg>1) fprintf(stderr,"\n remove frame %d",i1);
302 /* replace all frames i1,i1+1,... by i1+1,i1+2,... */
303 /* replace (x,y) pairs first */
304 for (i4=i2; i4<box1->num_frame_vectors[ box1->num_frames-1 ]-(i3-i2);
306 box1->frame_vector[i4][0] = box1->frame_vector[i4+i3-i2][0];
307 box1->frame_vector[i4][1] = box1->frame_vector[i4+i3-i2][1];
309 /* replace the num_frame_vectors */
310 for (i4=i1; i4<box1->num_frames-1; i4++)
311 box1->num_frame_vectors[ i4 ] =
312 box1->num_frame_vectors[ i4+1 ]-(i3-i2);
313 box1->num_frames--; i1--;
316 /* remove vectors outside the box */
318 for (i1=0; i1<box1->num_frames; i1++){
319 if (dbg>2) fprintf(stderr,"\n check cutting vectors on frame %d", i1);
320 x = box1->frame_vector[0][0]; /* last x */
321 y = box1->frame_vector[0][1]; /* last y */
322 /* ToDo: start inside to get a closed object */
323 if (x<box1->x0 || x>box1->x1 || y<box1->y0 || y>box1->y1) i3=1;
324 for (i2=0; i2<box1->num_frame_vectors[ i1 ]; i2++) {
327 x = box1->frame_vector[i2][0];
328 y = box1->frame_vector[i2][1];
329 // fprintf(stderr,"DBG LEV3 i2= %3d xy= %3d %3d",i2,x,y);
330 /* check if outside */
331 if (x<box1->x0 || x>box1->x1 || y<box1->y0 || y>box1->y1) {
332 /* replace by nearest point at border, ToDo: better crossingpoint */
333 if (i3==0) { /* wrong if it starts outside */
334 if (x < box1->x0) x = box1->frame_vector[i2][0] = box1->x0;
335 if (x > box1->x1) x = box1->frame_vector[i2][0] = box1->x1;
336 if (y < box1->y0) y = box1->frame_vector[i2][1] = box1->y0;
337 if (y > box1->y1) y = box1->frame_vector[i2][1] = box1->y1;
340 if (dbg>1) fprintf(stderr,"\n remove vector[%d][%d] x= %2d %2d",i1,i2,x-box1->x0,y-box1->y0);
341 for (i4=i2;i4<box1->num_frame_vectors[ box1->num_frames-1 ]-1;i4++) {
342 box1->frame_vector[i4][0] = box1->frame_vector[i4+1][0];
343 box1->frame_vector[i4][1] = box1->frame_vector[i4+1][1];
345 for (i4=i1; i4<box1->num_frames; i4++)
346 box1->num_frame_vectors[ i4 ]--;
347 i2--; /* next element is shiftet now, setting back the counter */
350 // fprintf(stderr," outside i3= %d\n",i3);
353 // fprintf(stderr," inside i3= %d",i3);
354 if (i3) { /* ToDo: better crossing point last vector and border */
355 if (lx < box1->x0) lx = box1->x0;
356 if (lx > box1->x1) lx = box1->x1;
357 if (ly < box1->y0) ly = box1->y0;
358 if (ly > box1->y1) ly = box1->y1;
359 x = box1->frame_vector[i2][0] = lx;
360 y = box1->frame_vector[i2][1] = ly;
363 // fprintf(stderr," xy= %3d %3d\n",x,y);
366 //if (dbg>2) { fprintf(stderr,"\nDBG cut_box_result:"); out_x(box1); }