Debian Codegraph @

cuneiform_src/Kern/rimage/sources/main/deza/cdezabinarizator.cpp

raw
1 /*
2 Copyright (c) 1993-2008, Cognitive Technologies
3 All rights reserved.
4
5 Разрешается повторное распространение и использование как в виде исходного кода,
6 так и в двоичной форме, с изменениями или без, при соблюдении следующих условий:
7
8 * При повторном распространении исходного кода должны оставаться указанное
9 выше уведомление об авторском праве, этот список условий и последующий
10 отказ от гарантий.
11 * При повторном распространении двоичного кода в документации и/или в
12 других материалах, поставляемых при распространении, должны сохраняться
13 указанная выше информация об авторском праве, этот список условий и
14 последующий отказ от гарантий.
15 * Ни название Cognitive Technologies, ни имена ее сотрудников не могут
16 быть использованы в качестве средства поддержки и/или продвижения
17 продуктов, основанных на этом ПО, без предварительного письменного
18 разрешения.
19
20 ЭТА ПРОГРАММА ПРЕДОСТАВЛЕНА ВЛАДЕЛЬЦАМИ АВТОРСКИХ ПРАВ И/ИЛИ ДРУГИМИ ЛИЦАМИ "КАК
21 ОНА ЕСТЬ" БЕЗ КАКОГО-ЛИБО ВИДА ГАРАНТИЙ, ВЫРАЖЕННЫХ ЯВНО ИЛИ ПОДРАЗУМЕВАЕМЫХ,
22 ВКЛЮЧАЯ ГАРАНТИИ КОММЕРЧЕСКОЙ ЦЕННОСТИ И ПРИГОДНОСТИ ДЛЯ КОНКРЕТНОЙ ЦЕЛИ, НО НЕ
23 ОГРАНИЧИВАЯСЬ ИМИ. НИ ВЛАДЕЛЕЦ АВТОРСКИХ ПРАВ И НИ ОДНО ДРУГОЕ ЛИЦО, КОТОРОЕ
24 МОЖЕТ ИЗМЕНЯТЬ И/ИЛИ ПОВТОРНО РАСПРОСТРАНЯТЬ ПРОГРАММУ, НИ В КОЕМ СЛУЧАЕ НЕ
25 НЕСЁТ ОТВЕТСТВЕННОСТИ, ВКЛЮЧАЯ ЛЮБЫЕ ОБЩИЕ, СЛУЧАЙНЫЕ, СПЕЦИАЛЬНЫЕ ИЛИ
26 ПОСЛЕДОВАВШИЕ УБЫТКИ, СВЯЗАННЫЕ С ИСПОЛЬЗОВАНИЕМ ИЛИ ПОНЕСЕННЫЕ ВСЛЕДСТВИЕ
27 НЕВОЗМОЖНОСТИ ИСПОЛЬЗОВАНИЯ ПРОГРАММЫ (ВКЛЮЧАЯ ПОТЕРИ ДАННЫХ, ИЛИ ДАННЫЕ,
28 СТАВШИЕ НЕГОДНЫМИ, ИЛИ УБЫТКИ И/ИЛИ ПОТЕРИ ДОХОДОВ, ПОНЕСЕННЫЕ ИЗ-ЗА ДЕЙСТВИЙ
29 ТРЕТЬИХ ЛИЦ И/ИЛИ ОТКАЗА ПРОГРАММЫ РАБОТАТЬ СОВМЕСТНО С ДРУГИМИ ПРОГРАММАМИ,
30 НО НЕ ОГРАНИЧИВАЯСЬ ЭТИМИ СЛУЧАЯМИ), НО НЕ ОГРАНИЧИВАЯСЬ ИМИ, ДАЖЕ ЕСЛИ ТАКОЙ
31 ВЛАДЕЛЕЦ ИЛИ ДРУГОЕ ЛИЦО БЫЛИ ИЗВЕЩЕНЫ О ВОЗМОЖНОСТИ ТАКИХ УБЫТКОВ И ПОТЕРЬ.
32
33 Redistribution and use in source and binary forms, with or without modification,
34 are permitted provided that the following conditions are met:
35
36 * Redistributions of source code must retain the above copyright notice,
37 this list of conditions and the following disclaimer.
38 * Redistributions in binary form must reproduce the above copyright notice,
39 this list of conditions and the following disclaimer in the documentation
40 and/or other materials provided with the distribution.
41 * Neither the name of the Cognitive Technologies nor the names of its
42 contributors may be used to endorse or promote products derived from this
43 software without specific prior written permission.
44
45 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
46 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
47 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
49 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
51 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
52 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
54 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 */
56
57 // CDezaBinarizator.cpp: implementation of the CDezaBinarizator class.
58 //
59 //////////////////////////////////////////////////////////////////////
60
61 #include "cdezabinarizator.h"
62 #include "cribinarizator.h"
63 #include "crimemory.h"
64
65 //////////////////////////////////////////////////////////////////////
66 // Construction/Destruction
67 //////////////////////////////////////////////////////////////////////
68
69 CDezaBinarizator::CDezaBinarizator()
70 {
71 mpBinarizatorControl = NULL;
72 bWhitePixel = 0;
73 ptbtek = NULL;
74 ptbosn = NULL;
75 ptb = NULL;
76 ptgrey = NULL;
77
78 }
79
80 CDezaBinarizator::~CDezaBinarizator()
81 {
82
83 }
84
85 uint32_t CDezaBinarizator::OpenTrackBin(PCTBINTigerImageInfo Info, CRIBinarizator *pCaller, uint32_t tip1)
86 {
87 int32_t lenpor;
88 int32_t i;
89
90 mpBinarizatorControl = pCaller;
91
92 urov[0] = urov[1] = 0;
93 x = y = lg = lgn = indbl = 0;
94 ypor = shed = sheds = 0;
95 spx = spy = spx1 = spx2 = lg0i = 0;
96 sdvig = sy1 = nstr = nstrb = 0;
97 indbeg = indend = 0;
98 lg0 = 0x00;
99
100 ////////////////////////////////////////////
101 // AK add
102 xx = 0;
103 ////////////////////////////////////////////
104 for (i=0; i<SB; i++)
105 chet_color[i]=0L;
106
107 colall = 0L;
108 tip = tip1; /* вЁЇ бЄ ­Ёа®ў ­Ёп 0-б« Ў®, 1-б।­Ґ, 2-¦Ёа­® */
109 bWhitePixel = Info->bFotoMetrics;
110 spx = Info->wImageWidth;
111 spy = Info->wImageHeight;
112 spx2 = (spx>>1)+1;
113 spx1 = (spx+7)>>3;
114 sdvig = (8-(spx & 0x0007)) & 0x0007;
115 lenpor = LONGBUF/spx;
116 nstr = lenpor;
117 lenpor = lenpor*spx;
118
119 if ((ptgrey=(puchar)DB_MALLOC(lenpor))==NULL)
120 return(0);
121
122 lenpor = LONGBUF / spx1;
123 nstrb = lenpor;
124 lenpor = lenpor * spx1;
125
126 if ((ptbosn=(puchar)DB_MALLOC(lenpor))==NULL)
127 return(0);
128
129 if (Read_por_first()==0)
130 return(0);
131
132 sy1 = indbl = 0;
133 ptbtek = ptbosn;
134 return(1);
135 }
136
137 puchar CDezaBinarizator::Black_file(puchar anPblack, int32_t stry)
138 {
139 uint32_t ir = 0;
140 puchar regPtb = ptb;
141 puchar ptbend;
142 puchar pblack = anPblack;
143 int32_t shby = 0, shift = 0;
144
145 for ( regPtb = ptgrey, ptbend = regPtb+spx*stry; regPtb != ptbend; regPtb++ )
146 {
147 ir = *regPtb ? (ir<<1)+1 : ir<<1;
148
149 if ( ++shby==spx )
150 {
151 shby = 0;
152 ir <<= sdvig;
153 *pblack++=(uchar)ir;
154 shift = 0;
155 }
156 else
157 if ( ++shift == 8 )
158 {
159 *pblack++=(uchar)ir;
160 shift = 0;
161 }
162 }
163 return(pblack);
164 /*
165 unsigned short int ir, l ,len;
166 short int shby, shift;
167 unsigned char *ptbend;
168 shby=shift=0;
169 for (ptb=ptgrey, ptbend=ptb+spx*stry; ptb!=ptbend; ptb++)
170 { ir<<=1; if (*ptb) ir++;
171 if (++shby==spx) { shby=0; ir<<=sdvig; shift=7; }
172 if (!(++shift&0x8FF8)) continue;
173 *pblack++=(unsigned char)ir;
174 shift=0;
175 }
176 return(pblack);
177 */
178 }
179
180 int32_t CDezaBinarizator::Our1(int32_t spr)
181 {
182 puchar regPtb;
183 uchar regUrov;
184 int32_t regSpx = spx;
185
186 ypor=spr;
187 bufmark[0]=bufmark[1]=0;
188
189 do //CLB:
190 {
191 x=bufmark[0];
192 y=bufmark[1];
193 regPtb = ptb=ptgrey+y*spx+x; // use register instead of static
194 regUrov = (uchar)urov[1]; // see above Stas!!!
195
196 while ( *regPtb < regUrov )
197 {
198 if (++x < regSpx)
199 {
200 regPtb++;
201 continue;
202 }
203 else
204 if (++y>=ypor)
205 {
206 ptb = regPtb; // synchronize
207 return(0);
208 }
209 else
210 {
211 x=0;
212 regPtb++;
213 }
214 }
215
216 ptb = regPtb; // synchronize
217 lg=urov[0];
218 bufmark[0]=x;
219 bufmark[1]=y;
220 *ptb=0x00;
221 indbeg=indend=0;
222 st[indend++]=x;
223 st[indend++]=y;
224 ptb++;
225 Right_str();
226 st[indend++]=shed;
227
228 while (indbeg!=indend)
229 {
230 Elem_st();
231 indbeg+=3;
232 }
233 }
234 while (TRUE); //goto CLB;
235 /*
236 ypor=spr; bufmark[0]=bufmark[1]=0;
237 CLB: x=bufmark[0]; y=bufmark[1]; ptb=ptgrey+y*spx+x;
238 while ( *ptb<urov[1] )
239 {
240 if (++x<spx) { ptb++; continue; }
241 if (++y>=ypor) return(0);
242 x=0; ptb++;
243 }
244 lg=urov[0]; bufmark[0]=x; bufmark[1]=y;
245 *ptb=0x00; indbeg=indend=0;
246 st[indend++]=x; st[indend++]=y; ptb++;
247 Right_str(); st[indend++]=shed;
248 while (indbeg!=indend) { Elem_st(); indbeg+=3; }
249 goto CLB;
250 */
251 }
252
253 int32_t CDezaBinarizator::Xygo(int32_t x1, int32_t y1)
254 {
255 if (y1 >= ypor || y1 < 0 || x1 >= spx || x1 < 0)
256 return(0);
257
258 x = x1;
259 y = y1;
260 ptb = ptgrey + y * spx + x;
261 return(1);
262 }
263
264 int32_t CDezaBinarizator::Grey_black()
265 {
266 int32_t spypic, sy2, spybg;
267
268 spybg = spypic = ((sy1 + nstrb) <= spy) ? nstrb : (spy-sy1);
269 sy2 = ((sy1 + nstr) <= spy) ? nstr : (spy - sy1);
270
271 while(spypic!=0)
272 {
273 if (sy2 > spypic)
274 sy2 = spypic;
275
276 if ( DB_GREYREAD(ptgrey, sy1, sy2) != sy2)
277 return(0);
278
279 Raspred(sy2);
280 sy1 += sy2;
281 Get1();
282 Our1(sy2);
283 ptbtek = Black_file(ptbtek,sy2);
284 spypic -= sy2;
285 }
286
287 ptbtek = ptbosn;
288 indbl = 0;
289 return(spybg);
290 }
291
292 int32_t CDezaBinarizator::Elem_st()
293 {
294 int32_t i;
295
296 if (Xygo(st[indbeg], st[indbeg+1]+1)==0)
297 return(0);
298
299 bufmark[2]=x;
300 bufmark[3]=y;
301 Left_str();
302
303 if (sheds)
304 {
305 Stek();
306 st[indend++]=x;
307 st[indend++]=y;
308 st[indend++]=sheds;
309 x=bufmark[2];
310 y=bufmark[3];
311 ptb=ptgrey+y*spx+x;
312 }
313
314 i=0;
315
316 while (i <= st[indbeg+2])
317 {
318 i++;
319
320 if (*ptb>=lg)
321 {
322 Stek();
323 st[indend++]=x;
324 st[indend++]=y;
325 *ptb++=0x00;
326 Right_str();
327 i+=shed;
328 st[indend++]=shed;
329 }
330 else
331 {
332 if (++x<spx)
333 ptb++;
334 else
335 {
336 x--;
337 break;
338 }
339 }
340 }
341 return(1);
342 }
343
344 int32_t CDezaBinarizator::Read_por_first()
345 {
346 int32_t str, i, j;
347
348 i = spy>>1;
349 str = (nstr<i) ? nstr:i;
350 str=(str<=spy/6) ? str:spy/6;
351 colall=1L*spx*str;
352 j=spy/2;
353
354 if (tip==2)
355 j=spy-str;
356
357 for (i=0; i<j; i+=str)
358 {
359 if (DB_GREYREAD(ptgrey, i, str) != str)
360 return(0);
361
362 Ras1_pred(str);
363 }
364
365 if (DB_GREYREAD(ptgrey,spy - str,str) != str)
366 return(0);
367
368 Ras1_pred(str);
369
370 for (lg0i=1; lg0i<SB; lg0i++)
371 if (chet_color[lg0i]!=0L)
372 break;
373
374 lgn = lg0i+1;
375 lg0 = (uchar)lg0i;
376 chet_color[lg0i] += chet_color[0];
377 chet_color[0] = 0L;
378 colall -= chet_color[lg0i];
379 return(1);
380 }
381
382 int32_t CDezaBinarizator::Get1()
383 {
384 int32_t i, j, imax, lg1, pg1, lg2, pg2, pgn;
385 int32_t irl, srz, chet[256], srz1, srz2;
386
387 for (i=0; i<256; i++)
388 chet[i]=chet_color[i];
389
390 srz=colall/100L;
391 irl=0L;
392
393 for (pgn=255; irl<srz; pgn--)
394 irl+=chet[pgn];
395
396 for (i=lgn,srz=0L; i<=pgn; i++)
397 srz+=chet[i];
398
399 if (lgn==pgn)
400 pgn++;
401
402 srz=(srz+(pgn-lgn)/2)/(pgn-lgn);
403
404 for (imax=lgn; chet[imax]<srz; imax++) ;
405
406 for ( ; imax<=pgn; imax++)
407 if (chet[imax]>chet[imax+1])
408 break;
409
410 for (i=imax+1,srz=0L; i<=pgn; i++)
411 srz+=chet[i];
412
413 if (pgn==imax)
414 pgn++;
415
416 if ((pgn-lgn)<128) /* § «ҐЇ  ¤«п б« Ўле ⥪бв®ў */
417 {
418 srz1=chet[lg0i]*100L/colall;
419
420 if (srz1<500L)
421 {
422 srz=colall/10L;
423
424 for (urov[0]=lgn, irl=0L; irl<srz; urov[0]++)
425 irl+=chet[urov[0]];
426
427 urov[1]=urov[0]+10;
428 return(0);
429 }
430 }
431
432 for (lg1=imax+1,srz=(srz+(pgn-imax)/2)/(pgn-imax);lg1<=pgn;lg1++)
433 if (chet[lg1]<=srz && chet[lg1+1]<=srz)
434 break;
435
436 for (i=lg1+1,srz=0L;i<=pgn;i++)
437 srz+=chet[i];
438
439 if (pgn==lg1)
440 pgn++;
441
442 for (pg2=pgn,srz=(srz+(pgn-lg1)/2)/(pgn-lg1); pg2>lg1; pg2--)
443 if (chet[pg2]>=srz && chet[pg2-1]>=srz && chet[pg2-2]>=srz)
444 break;
445
446 for (; pg2>lg1; pg2--)
447 if (chet[pg2]<=srz && chet[pg2-1]<=srz && chet[pg2-2]<=srz)
448 break;
449
450 for (i=lg1+1,srz=0L; i<=pg2; i++)
451 srz+=chet[i];
452
453 if (pg2==lg1)
454 pg2++;
455
456 for (pg1=lg1,srz=(srz+(pg2-lg1)/2)/(pg2-lg1); pg1<pg2; pg1++)
457 if (chet[pg1]<=srz && chet[pg1+1]<=srz && chet[pg1+2]<=srz)
458 break;
459
460 for (i=pg1+1,srz=0L; i<=pg2; i++)
461 srz+=chet[i];
462
463 if (pg2==pg1)
464 pg2++;
465
466 for (lg2=pg2,srz=(srz+(pg2-pg1)/2)/(pg2-pg1); lg2>pg1; lg2--)
467 if (chet[lg2]<=srz && chet[lg2-1]<=srz && chet[lg2-2]<=srz)
468 break;
469
470 if (lg2==pg1)
471 {
472 xx=pg1;
473 //goto Gt1;
474 }
475 else
476 {
477 for (i=pg1, irl=2L*chet[pg1]; i<=lg2; i++)
478 if ((srz=labs(chet[i]-2L*chet[pg1]*(lg2-i)/(lg2-pg1)))<=irl)
479 {
480 irl=srz;
481 xx=i;
482 }
483 }
484 //Gt1:
485 srz=colall/25L;
486
487 if ((pgn-lgn)<128 && xx>128)
488 tip=0;
489
490 for (j=xx, irl=0L; irl<srz; j--)
491 irl+=chet[j];
492
493 urov[0]=j;
494 srz=colall/50L;
495
496 for (j=xx, irl=0L; irl<srz; j++)
497 irl+=chet[j];
498
499 urov[1]=j;
500
501 if (tip==0)
502 {
503 srz1=colall/20L;
504 srz2=colall/10L;
505
506 for (xx=lgn, irl=0L; irl<srz1; xx++)
507 irl+=chet[xx];
508
509 urov[0]=xx;
510
511 for (j=xx, irl=0L; irl<srz2; j++)
512 irl+=chet[j];
513
514 urov[1]=j;
515 }
516 return(0);
517 }
518
519 void CDezaBinarizator::Right_str()
520 {
521 shed=0;
522
523 if (++x<spx)
524 while (*ptb>=lg)
525 {
526 *ptb++=0x00;
527 shed++;
528
529 if (++x>=spx)
530 break;
531 }
532
533 x--;
534 ptb--;
535 }
536
537 void CDezaBinarizator::Left_str()
538 {
539 sheds=0;
540 ptb--;
541
542 if (--x>=0)
543 while (*ptb>=lg)
544 {
545 *ptb--=0x00;
546 sheds++;
547
548 if (--x<0)
549 break;
550 }
551
552 x++;
553 ptb++;
554 }
555
556 void CDezaBinarizator::Stek()
557 {
558 int32_t r, ir;
559
560 if (indend==BUFS)
561 {
562 if (indbeg==BUFS)
563 indend=0;
564 else
565 {
566 for (r = indbeg, ir = 0; r < (int32_t)indend; r++, ir++)
567 st[ir]=st[r];
568
569 indend-=indbeg;
570 }
571
572 indbeg=0;
573 }
574 }
575
576 void CDezaBinarizator::Raspred(int32_t str)
577 {
578 puchar regPtb;
579 uchar regLg0 = lg0;
580 uint32_t i, lineCount;
581 int32_t l;
582
583 l = chet_color[lg0i];
584
585 for ( lineCount = 0, regPtb=ptgrey; lineCount < (uint32_t)str ; lineCount++ )
586 {
587 for( i = 0 ; i <= (uint32_t)spx2 ; i++, regPtb++ )
588 {
589 if ( *regPtb < regLg0 )
590 *regPtb=regLg0;
591
592 chet_color[*regPtb]++;
593 }
594
595 for( ; i < (uint32_t)spx ; i++, regPtb++ )
596 {
597 if ( *regPtb < regLg0 )
598 *regPtb=regLg0;
599 }
600 }
601
602 colall = colall + 1L * spx2 * str - (chet_color[lg0i] - l);
603 ptb = regPtb;
604 /*
605 unsigned short int len, i;
606 long l;
607 l=chet_color[lg0i]; len=spx*str;
608 for (i=0, ptb=ptgrey; i<len; i++, ptb++)
609 { if (*ptb<lg0) *ptb=lg0;
610 if ( (i%spx) <=spx2) chet_color[*ptb]++;
611 }
612 colall=colall+1L*spx2*str-(chet_color[lg0i]-l);
613 */
614 }
615
616 void CDezaBinarizator::Ras1_pred(int32_t str)
617 {
618 puchar regPtb;
619 int32_t i;
620 int32_t regSpx2 = spx2;
621 int32_t lineCount;
622
623 for ( lineCount = str, regPtb=ptgrey; lineCount > 0 ; lineCount-- )
624 {
625 for( i=0 ; i <= regSpx2 ; i++, regPtb++ )
626 {
627 chet_color[*regPtb]++;
628 }
629
630 i += (spx - spx2 - 1);
631 regPtb += (spx - spx2 - 1);
632 }
633
634 colall += 1L * spx2 * str;
635 /*
636 unsigned short int len, i;
637 len=spx*str;
638
639 for (i=0, ptb=ptgrey; i<len; i++,ptb++)
640 if ( (i%spx) <=spx2) chet_color[*ptb]++;
641
642 colall+=1L*spx2*str;
643 */
644 }
645
646 int32_t CDezaBinarizator::GetBinarized(puchar ptbl, uint32_t lenbl)
647 {
648 int32_t spybr, ret, nstrb1;
649 puchar ptbend;
650
651 spybr = lenbl/spx1;
652 nstrb1 = spybr+indbl-nstrb;
653 spybr = (nstrb1<=(spy-sy1)) ? spybr:(spy-sy1+nstrb-indbl);
654 ret = 0;
655
656 do //GB1:
657 {
658 if (ptbtek==ptbosn)
659 if ((nstrb=Grey_black())==0)
660 return(ret);
661
662 nstrb1 = nstrb-indbl;
663
664 if (spybr<=nstrb1)
665 {
666 if(bWhitePixel)// Piter see flag invert
667 for (ptbend=ptbtek+spx1*spybr; ptbtek!=ptbend;)
668 *ptbl++=~*ptbtek++;
669 else
670 for (ptbend=ptbtek+spx1*spybr; ptbtek!=ptbend;)
671 *ptbl++=*ptbtek++;
672
673 indbl += spybr;
674
675 if (indbl==nstrb/*1*/)
676 ptbtek=ptbosn;// Piter new
677
678 // indbl+=spybr; if (indbl==nstrb1) ptbtek=ptbosn;// Piter Old string
679 return(ret+spybr);
680 }
681
682 if(bWhitePixel)// Piter see flag invert
683 for (ptbend=ptbtek+spx1*nstrb1; ptbtek!=ptbend;)
684 *ptbl++=~*ptbtek++;
685 else
686 for (ptbend=ptbtek+spx1*nstrb1; ptbtek!=ptbend;)
687 *ptbl++=*ptbtek++;
688
689 ptbtek = ptbosn;
690 spybr -= nstrb1;
691 ret += nstrb1;
692 } while( TRUE ); //goto GB1;
693 }
694
695 int32_t CDezaBinarizator::CloseTrackBin()
696 {
697 DB_FREE(ptgrey);
698 DB_FREE(ptbosn);
699 ptgrey = NULL;
700 ptbosn = NULL;
701 return(1);
702 }