replaced libart with new polygon code
[swftools.git] / lib / gocr / output.c
1 /*
2 This is a Optical-Character-Recognition program
3 Copyright (C) 2000-2006  Joerg Schulenburg
4
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.
9
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.
14
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.
18
19  see README for EMAIL address
20 */
21
22 #include <string.h>
23 #include "unicode.h"
24 #include "output.h"
25 #include "gocr.h"  /* extern job_t JOB; */
26
27 /* function is only for debugging and for developing
28    it prints out a part of pixmap b at point x0,y0 to stderr
29    using dots .,; if no pixel, and @xoO for pixels
30    modify n_run and print out what would happen on 2nd, 3th loop!
31    new: output original and copied pixmap in the same figure
32  */
33 void out_b(struct box *px, pix *b, int x0, int y0, int dx, int dy, int cs ){
34   int x,y,x2,y2,yy0,tx,ty,n1,i;
35   char c1, c2;
36   yy0=y0;
37   if(px){ /* overwrite rest of arguments */
38     if (!b) {
39       b=px->p;
40       x0=px->x0; dx=px->x1-px->x0+1;
41       y0=px->y0; dy=px->y1-px->y0+1; yy0=y0;
42     }
43     if(cs==0) cs=JOB->cfg.cs;
44     fprintf(stderr,"\n# list box      x= %4d %4d d= %3d %3d r= %3d %3d"
45                     " nrun=%d p=%p", /* ToDo: r,nrun is obsolete */
46           px->x0, px->y0, px->x1 - px->x0 + 1, px->y1 - px->y0 + 1,
47           px->x - px->x0, px->y - px->y0, JOB->tmp.n_run, (void*)px);
48     fprintf(stderr,"\n#  dots=%d boxes=%d subboxes=%d c=%s mod=%s"
49             " line=%d m= %d %d %d %d",
50           px->dots, px->num_boxes, px->num_subboxes, 
51           decode(px->c,ASCII), decode(px->modifier,ASCII), px->line,
52           px->m1 - px->y0, px->m2 - px->y0, px->m3 - px->y0, px->m4 - px->y0);
53     if (px->num_frames) {
54       int i,j,jo;
55       fprintf(stderr,"\n#  frames= %d (sumvects=%d)",px->num_frames,
56          ((px->num_frames)?px->num_frame_vectors[px->num_frames-1]:-1));
57       for (jo=j=i=0; i<px->num_frames; i++, jo=j) {
58         fprintf(stderr,"\n#  frame %d (%+4d,%3d,%2d) ",
59                 i, px->frame_vol[i], px->frame_per[i],
60                    px->num_frame_vectors[i]-jo);
61         /* print only the first vectors of each frame */
62         for (;j<px->num_frame_vectors[i] && j<MaxFrameVectors; j++)
63           fprintf(stderr," #%02d %2d %2d", j,
64                             px->frame_vector[j][0] - px->x0,
65                             px->frame_vector[j][1] - px->y0);
66       }
67     }
68     if (px->num_ac){ /* output table of chars and its probabilities */
69       fprintf(stderr,"\n# list box char: ");
70       for(i=0;i<px->num_ac && i<NumAlt;i++)
71       /* output the (xml-)string (picture position, barcodes, glyphs, ...) */
72         if (px->tas[i])
73          fprintf(stderr," %s(%d)",       px->tas[i]       ,px->wac[i]);
74         else
75          fprintf(stderr," %s(%d)",decode(px->tac[i],ASCII),px->wac[i]);
76     }
77     fprintf(stderr,"\n");
78     if (px->dots && px->m2 && px->m1<y0) { yy0=px->m1; dy=px->y1-yy0+1; }
79   }
80   tx=dx/80+1;
81   ty=dy/40+1; /* step, usually 1, but greater on large maps */
82   fprintf(stderr,"# list pattern  x= %4d %4d d= %3d %3d t= %d %d\n",
83                  x0,y0,dx,dy,tx,ty);
84   if (dx>0)
85   for(y=yy0;y<yy0+dy;y+=ty) { /* reduce the output to max 78x40 */
86     /* first image is the copied and modified bitmap of the box */
87     if (px)
88     for(x=x0;x<x0+dx;x+=tx){  /* by merging sub-pixels */
89       n1=0; c1='.';
90       for(y2=y;y2<y+ty && y2<y0+dy;y2++) /* sub-pixels */
91       for(x2=x;x2<x+tx && x2<x0+dx;x2++)
92       {
93         if((getpixel(px->p,x2-x0+px->x0,
94                         y2-y0+px->y0)<cs)) c1='@';
95       }
96       if (px->num_frames) { /* mark vectors */
97         int i;
98         if (c1!='$' && c1!='S') /* dont mark twice */
99         for (i=0;i<px->num_frame_vectors[px->num_frames-1];i++)
100           if ((px->frame_vector[i][0]-px->x0)/tx==(x-x0)/tx
101            && (px->frame_vector[i][1]-px->y0)/ty==(y-y0)/ty)
102               { c1=((c1=='@')?'$':'S'); break; }
103       }
104       fprintf(stderr,"%c", c1 );
105     }
106
107     /* 2nd image is the boxframe in the original bitmap */
108     if (dx<40) fprintf(stderr,"  ");
109     if (dx<40) /* do it only, if we have enough place */
110     for(x=x0;x<x0+dx;x+=tx){  /* by merging sub-pixels */
111       c1='.';
112       for(y2=y;y2<y+ty && y2<y0+dy;y2++) /* sub-pixels */
113       for(x2=x;x2<x+tx && x2<x0+dx;x2++)
114         { if((getpixel(b,x2,y2)<cs)) c1='@'; }
115       fprintf(stderr,"%c", c1 );
116     }
117
118     c1=c2=' ';
119     /* mark lines with < */
120     if (px) if (y-y0+px->y0==px->m1 || y-y0+px->y0==px->m2
121              || y-y0+px->y0==px->m3 || y-y0+px->y0==px->m4)  c1='<';
122     if (y==y0 || y==yy0+dy-1)  c2='-';  /* boxmarks */
123         
124     fprintf(stderr,"%c%c\n",c1,c2);
125   }
126 }
127
128 /* same as out_b, but for faster use, only a box as argument
129  */
130 void out_x(struct box *px) {
131   out_b(px,NULL,0, 0, 0, 0, JOB->cfg.cs);
132 }
133
134
135 /* print out two boxes side by side, for debugging comparision algos */
136 void out_x2(struct box *box1, struct box *box2){
137   int x,y,i,tx,ty,dy;
138   /*FIXME jb static*/static char *c1="OXXXXxx@.,,,,,,,";
139   pix *b=&JOB->src.p;
140   dy=(box1->y1-box1->y0+1);
141   if(dy<box2->y1-box2->y0+1)dy=box2->y1-box2->y0+1;
142   tx=(box1->x1-box1->x0)/40+1;
143   ty=(box1->y1-box1->y0)/40+1; /* step, usually 1, but greater on large maps */
144   if(box2)fprintf(stderr,"\n# list 2 patterns");
145   for(i=0;i<dy;i+=ty) { /* reduce the output to max 78x40??? */
146     fprintf(stderr,"\n"); y=box1->y0+i;
147     for(x=box1->x0;x<=box1->x1;x+=tx) 
148     fprintf(stderr,"%c", c1[ ((getpixel(b,x,y)<JOB->cfg.cs)?0:8)+marked(b,x,y) ] );
149     if(!box2) continue;
150     fprintf(stderr,"  "); y=box2->y0+i;
151     for(x=box2->x0;x<=box2->x1;x+=tx)
152     fprintf(stderr,"%c", c1[ ((getpixel(b,x,y)<JOB->cfg.cs)?0:8)+marked(b,x,y) ] );
153   }
154 }
155
156
157 /* ---- list output ---- for debugging ---
158  * list all boxes where the results can be found within the c-option  
159  */
160 int output_list(job_t *job) {
161   int i = 0, j;
162   struct box *box2;
163   pix  *pp = &job->src.p;
164   char *lc = job->cfg.lc;
165
166   fprintf(stderr,"\n# list shape for charlist %s",lc);
167   for_each_data(&(JOB->res.boxlist)) {
168     box2 = (struct box *) list_get_current(&(JOB->res.boxlist));
169     for (j=0; j<box2->num_ac; j++) 
170       if (!lc || (box2->tac[j] && strchr(lc, box2->tac[j]))
171               || (box2->tas[j] && strstr(lc, box2->tas[j]))) break;
172     if (j<box2->num_ac)
173       fprintf(stderr,"\n#            box found in charlist");
174     if (!lc || (strchr(lc, box2->c) && box2->c < 256 && box2->c)
175             || (strchr(lc, '_') && box2->c==UNKNOWN) /* for compability */
176             || j<box2->num_ac ){  /* also list alternative chars */
177       if (!pp) pp=box2->p;
178       fprintf(stderr,
179               "\n# list shape %3d x=%4d %4d d= %3d %3d vf=%d ac=%d %04x %s",
180               i, box2->x0, box2->y0,
181               box2->x1 - box2->x0 + 1,
182               box2->y1 - box2->y0 + 1,
183               box2->num_frames, box2->num_ac, 
184               (int)box2->c,   /* wchar_t -> char ???? */
185               decode(box2->c,ASCII) );
186       if (JOB->cfg.verbose & 4) out_x(box2);
187     }
188     i++;
189   } end_for_each(&(JOB->res.boxlist));
190   fprintf(stderr,"\n");
191   return 0;
192 }
193