2
Copyright (c) 1993-2008, Cognitive Technologies
5
����������� ��������� ��������������� � ������������� ��� � ���� ��������� ����,
6
��� � � �������� �����, � ����������� ��� ���, ��� ���������� ��������� �������:
8
* ��� ��������� ��������������� ��������� ���� ������ ���������� ���������
9
���� ����������� �� ��������� �����, ���� ������ ������� � �����������
11
* ��� ��������� ��������������� ��������� ���� � ������������ �/��� �
12
������ ����������, ������������ ��� ���������������, ������ �����������
13
��������� ���� ���������� �� ��������� �����, ���� ������ ������� �
14
����������� ����� �� ��������.
15
* �� �������� Cognitive Technologies, �� ����� �� ����������� �� �����
16
���� ������������ � �������� �������� ��������� �/��� �����������
17
���������, ���������� �� ���� ��, ��� ���������������� �����������
20
��� ��������� ������������� ����������� ��������� ���� �/��� ������� ������ "���
21
��� ����" ��� ������-���� ���� ��������, ���������� ���� ��� ���������������,
22
������� �������� ������������ �������� � ����������� ��� ���������� ����, �� ��
23
������������� ���. �� �������� ��������� ���� � �� ���� ������ ����, �������
24
����� �������� �/��� �������� �������������� ���������, �� � ���� ������ ��
25
��Ѩ� ���������������, ������� ����� �����, ���������, ����������� ���
26
������������� ������, ��������� � �������������� ��� ���������� ����������
27
������������� ������������� ��������� (������� ������ ������, ��� ������,
28
������� ���������, ��� ������ �/��� ������ �������, ���������� ��-�� ��������
29
������� ��� �/��� ������ ��������� �������� ��������� � ������� �����������,
30
�� �� ������������� ����� ��������), �� �� ������������� ���, ���� ���� �����
31
�������� ��� ������ ���� ���� �������� � ����������� ����� ������� � ������.
33
Redistribution and use in source and binary forms, with or without modification,
34
are permitted provided that the following conditions are met:
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.
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.
63
BWSS *locomp_seglist(Word8* raster, BWSS *bwsp, BWSS *bwe, Int32 height, Int32 width);
64
MN * c_locomp (Word8* raster, Int32 bw, Int32 h, Int16 upper, Int16 left);
66
#define MAX_BOX_NUMB 100*4
67
#define MAX_INT_NUMB 32*4
68
#define LINE_POOL_LENGTH 512*4
70
static BOX * boxalloc;
71
static BOX * boxallocend;
72
static MN * mainalloc;
73
static MN * first_dead_comp;
74
static BWSS * segm_repr_end;
75
static BWSS * op, *np;
81
static BWSS lines[LINE_POOL_LENGTH+9];
82
static Word8 boxes[BOXSIZE * MAX_BOX_NUMB];
83
static MN main_numbers[MAX_INT_NUMB];
85
// Oleg : 18-08-1994 : link DIFFRV.C
86
// Vald: 10-03-96 07:18pm set it const
87
Word8* segment_pool=(Word8 *)lines;
89
#define end_line_pool (lines + LINE_POOL_LENGTH)
90
#define box_alloc(bp) bp = boxalloc; boxalloc = (BOX *)((Word8*)boxalloc + BOXSIZE)
92
static jmp_buf locomp_err;
96
static void locomp_begin();
97
static void analize();
98
static void simple_cont();
99
static void new_line();
100
static void new_line_cont();
101
static void merge_line();
102
static void dead_line();
104
MN * c_locomp (Word8* raster, Int32 bw, Int32 h, Int16 upper, Int16 left)
107
lineno = upper-1; rast_lc = left;
108
if (setjmp(locomp_err)) return NULL;
109
segm_repr_end = locomp_seglist (raster,lines,end_line_pool,h,bw);
115
} while (h1 && np != segm_repr_end);
116
return first_dead_comp;
119
static void locomp_begin()
123
first_dead_comp = NULL;
124
segm_repr_end->b = 0; (segm_repr_end++)->w = -0x7000;
125
if (segm_repr_end >= end_line_pool)
126
longjmp(locomp_err,3);
127
for (i = 0, mn = mainalloc = main_numbers; i < MAX_INT_NUMB-1; i++, mn++)
129
mn->mnnext = NULL; np = (op = lines) + 1;
130
boxalloc = (BOX *) boxes; // Vald 06-15-96 07:06pm corr old Talalay error
131
boxallocend = (BOX *)(boxes + MAX_BOX_NUMB * BOXSIZE);
134
static void analize()
137
if (op->b == 0) ol += (op++)->w;
138
if (np->b == 0) nl += (np++)->w;
139
if (ol < 0) goto restnewline_b;
140
if (nl < 0) goto restoldline_b;
143
if (ol != nl) { if (ol > nl) goto actnew; else goto actold; }
144
ol += op->b; nl += np->b; simple_cont();
147
if (ol != nl) { if (ol > nl) goto nextnewsegm; else goto nextoldsegm; }
149
nl += (np++)->w; if (nl < 0) goto restoldline;
150
ol += (op++)->w; if (ol > 0) goto gencase; goto restnewline_b;
153
ol += (op++)->w; if (ol < 0) goto restnewline;
154
if (ol > nl) goto new_segm_step;
155
ol += op->b; merge_line(); goto intersect_step;
158
nl += (np++)->w; if (nl < 0) goto restoldline;
159
if (nl > ol) goto old_segm_step;
160
new_line_cont(); goto intersect_step;
162
linedies: dead_line();
164
ol += (op++)->w; if (ol > 0) goto gencase; goto restnewline_b;
168
nl += (np++)->w; if (nl > 0) goto gencase; goto restoldline_b;
171
ol += op->b; if (nl > ol) goto linedies;
172
nl += np->b; simple_cont(); goto intersect_step;
175
nl += np->b; if (ol > nl) goto newcomp;
176
ol += op->b; simple_cont(); goto intersect_step;
178
restnewline_b: if (nl < 0) return;
179
restnewline_1: nl += np->b; new_line();
180
restnewline: nl += (np++)->w; if (nl > 0) goto restnewline_1;
183
restoldline_b: if (ol < 0) return;
184
restoldline_1: ol += op->b; dead_line();
185
restoldline: ol += (op++)->w; if (ol > 0) goto restoldline_1;
189
static void simple_cont()
194
bp = op->box; ip = (BOXINT *)((Word8*)bp + bp->boxptr);
195
if (bp->boxptr > BOXBOUNDARY) goto fullbox; bp->boxptr += sizeof (*ip);
197
np->box = bp; ip->l = np->b; ip->d = nl - ol;
198
if (bp->boxright < nl) bp->boxright = nl;
199
if (bp->boxleft > nl - ip->l) bp->boxleft = nl - ip->l;
202
ip->l = -1; box_alloc(bpw);
203
if (boxalloc == boxallocend)
204
longjmp(locomp_err,1);
205
bpw->boxmain = mn = bp->boxmain; mn->mnboxcnt++;
206
bpw->boxnext = bp->boxnext; bp->boxnext = bpw;
207
bp = bpw; ip = (BOXINT *)((Word8*)bp + sizeof(BOX));
208
bp->boxptr = sizeof(BOX) + sizeof(BOXINT); bp->boxflag = 0;
209
bp->boxleft = bp->boxright = nl; goto resume;
212
static void new_line()
218
if (boxalloc == boxallocend)
219
longjmp(locomp_err,1);
220
if ((mn = mainalloc) == NULL)
221
longjmp(locomp_err,3);
222
mainalloc = mn->mnnext;
224
mn->mnfirstbox = bp; mn->mncounter = mn->mnboxcnt = 1; mn->mnflag = 0;
225
mn->mnlines = mn->mnbegs = 1; mn->mnends = 0;
227
bp->boxnext = NULL; bp->boxflag = BOXFREEBEG;
228
bp->boxptr = sizeof(BOX) + sizeof(LNSTRT); lp = (LNSTRT *)(bp+1);
229
lp->y = mn->mnupper = lineno; bp->boxmain = mn;
230
bp->boxleft = nl - (lp->l = np->b); bp->boxright = lp->x = nl;
233
static void new_line_cont()
238
bwp = (np-1)->box; mn = bwp->boxmain;
240
if (boxalloc == boxallocend)
241
longjmp (locomp_err,1);
242
np->box = bp; bp->boxnext = bwp->boxnext; bwp->boxnext = bp;
243
mn->mnlines++; mn->mncounter++; mn->mnboxcnt++;
244
bp->boxptr = sizeof(BOX) + sizeof(LNSTRT); lp = (LNSTRT *)(bp+1);
245
bp->boxmain = mn; bp->boxflag = BOXBEG;
246
lp->y = lineno; bp->boxleft = nl;
247
bp->boxright = lp->x = nl += (lp->l = np->b);
250
static void merge_line()
257
bpo = op->box; bpo->boxflag |= BOXEND;
258
ip = (BOXINT *)((Word8*)bpo + bpo->boxptr); ip->l = 0; bpo->boxptr++;
259
bpo->boxey = lineno; bpo->boxex = ol; bpo->boxel = op->b;
261
bp = np->box; mn = bp->boxmain; mno = bpo->boxmain;
262
if (mn == mno) { mn->mncounter--; return; }
264
if ((n > usual_box_count) && (n > mn->mnboxcnt))
265
{ n = mn->mnboxcnt; mnw = mno; mno = mn; mn = mnw; }
266
mn->mnflag |= mno->mnflag; mn->mncounter += mno->mncounter - 1;
267
mn->mnlines += mno->mnlines; mn->mnbegs += mno->mnbegs;
268
mn->mnends += mno->mnends; mn->mnboxcnt += n;
269
if (mno->mnupper < mn->mnupper) mn->mnupper = mno->mnupper;
270
bp = mn->mnfirstbox; mn->mnfirstbox = bpo = mno->mnfirstbox;
271
mno->mnnext = mainalloc; mainalloc = mno;
272
while (--n) { bpo->boxmain = mn; bpo = bpo->boxnext;}
273
bpo->boxmain = mn; bpo->boxnext = bp;
276
static void dead_line()
281
bp = op->box; ip = (BOXINT *)((Word8*)bp + bp->boxptr++); ip->l = 0;
282
bp->boxey = lineno; bp->boxex = ol; bp->boxel = op->b;
283
bp->boxflag |= BOXFREEEND;
284
mn = bp->boxmain; mn->mnends++; if (--(mn->mncounter)) return;
285
mn->mnnext = first_dead_comp; first_dead_comp = mn;
286
mn->mnlower = lineno;