~ubuntu-branches/debian/wheezy/cuneiform/wheezy

« back to all changes in this revision

Viewing changes to cuneiform_src/Kern/evn32/src/locompmn.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2009-07-10 17:58:10 UTC
  • Revision ID: james.westby@ubuntu.com-20090710175810-rqc89d2i3tki9m89
Tags: upstream-0.7.0+dfsg
Import upstream version 0.7.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
58
#include <setjmp.h>
 
59
#include <stdlib.h>
 
60
#include "struct.h"
 
61
#include "v1comp.h"
 
62
 
 
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);
 
65
//      Memory service
 
66
#define MAX_BOX_NUMB            100*4
 
67
#define MAX_INT_NUMB            32*4
 
68
#define LINE_POOL_LENGTH        512*4
 
69
 
 
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;
 
76
static Int16 ol, nl;
 
77
static Int16 rast_lc;
 
78
static Int16 lineno;
 
79
 
 
80
 
 
81
static BWSS lines[LINE_POOL_LENGTH+9];
 
82
static Word8 boxes[BOXSIZE * MAX_BOX_NUMB];
 
83
static MN main_numbers[MAX_INT_NUMB];
 
84
 
 
85
   // Oleg : 18-08-1994 : link DIFFRV.C
 
86
   // Vald: 10-03-96 07:18pm set it const
 
87
Word8* segment_pool=(Word8 *)lines;
 
88
 
 
89
#define end_line_pool  (lines + LINE_POOL_LENGTH)
 
90
#define box_alloc(bp) bp = boxalloc; boxalloc = (BOX *)((Word8*)boxalloc + BOXSIZE)
 
91
 
 
92
static jmp_buf locomp_err;
 
93
 
 
94
//      Internal functions
 
95
 
 
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();
 
103
 
 
104
MN * c_locomp (Word8* raster, Int32 bw, Int32 h, Int16 upper, Int16 left)
 
105
{
 
106
 Int32  h1=h+1;
 
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);
 
110
 locomp_begin();
 
111
 do {
 
112
    lineno++;
 
113
    h1--;
 
114
    analize();
 
115
    } while (h1 && np != segm_repr_end);
 
116
 return first_dead_comp;
 
117
}
 
118
 
 
119
static void locomp_begin()
 
120
{
 
121
 Int16 i;
 
122
 MN * mn;
 
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++)
 
128
      mn->mnnext = mn+1;
 
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);
 
132
}
 
133
 
 
134
static void analize()
 
135
{
 
136
 ol = nl = rast_lc;
 
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;
 
141
 
 
142
gencase:
 
143
 if (ol != nl) { if (ol > nl) goto actnew; else goto actold; }
 
144
 ol += op->b; nl += np->b; simple_cont();
 
145
 
 
146
intersect_step:
 
147
 if (ol != nl) { if (ol > nl) goto nextnewsegm; else goto nextoldsegm; }
 
148
 
 
149
 nl += (np++)->w; if (nl < 0) goto restoldline;
 
150
 ol += (op++)->w; if (ol > 0) goto gencase; goto restnewline_b;
 
151
 
 
152
nextoldsegm:
 
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;
 
156
 
 
157
nextnewsegm:
 
158
 nl += (np++)->w; if (nl < 0) goto restoldline;
 
159
 if (nl > ol) goto old_segm_step;
 
160
 new_line_cont(); goto intersect_step;
 
161
 
 
162
linedies:       dead_line();
 
163
old_segm_step:
 
164
 ol += (op++)->w; if (ol > 0) goto gencase; goto restnewline_b;
 
165
 
 
166
newcomp:        new_line();
 
167
new_segm_step:
 
168
 nl += (np++)->w; if (nl > 0) goto gencase; goto restoldline_b;
 
169
 
 
170
actold:
 
171
 ol += op->b; if (nl > ol) goto linedies;
 
172
 nl += np->b; simple_cont(); goto intersect_step;
 
173
 
 
174
actnew:
 
175
 nl += np->b; if (ol > nl) goto newcomp;
 
176
 ol += op->b; simple_cont(); goto intersect_step;
 
177
 
 
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;
 
181
 return;
 
182
 
 
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;
 
186
 return;
 
187
}
 
188
 
 
189
static void simple_cont()
 
190
{
 
191
 BOX * bp, *bpw;
 
192
 BOXINT * ip;
 
193
 MN *mn;
 
194
 bp = op->box; ip = (BOXINT *)((Word8*)bp + bp->boxptr);
 
195
 if (bp->boxptr > BOXBOUNDARY) goto fullbox; bp->boxptr += sizeof (*ip);
 
196
resume:
 
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;
 
200
 return;
 
201
fullbox:
 
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;
 
210
}
 
211
 
 
212
static void new_line()
 
213
{
 
214
 BOX *bp;
 
215
 MN *mn;
 
216
 LNSTRT *lp;
 
217
 box_alloc(bp);
 
218
 if (boxalloc == boxallocend)
 
219
    longjmp(locomp_err,1);
 
220
 if ((mn = mainalloc) == NULL)
 
221
    longjmp(locomp_err,3);
 
222
 mainalloc = mn->mnnext;
 
223
 
 
224
 mn->mnfirstbox = bp; mn->mncounter = mn->mnboxcnt = 1; mn->mnflag = 0;
 
225
 mn->mnlines = mn->mnbegs = 1; mn->mnends = 0;
 
226
 np->box = bp;
 
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;
 
231
}
 
232
 
 
233
static void new_line_cont()
 
234
{
 
235
 BOX *bp, *bwp;
 
236
 MN * mn;
 
237
 LNSTRT * lp;
 
238
 bwp = (np-1)->box; mn = bwp->boxmain;
 
239
 box_alloc(bp);
 
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);
 
248
}
 
249
 
 
250
static void merge_line()
 
251
{
 
252
 MN *mn, *mno, *mnw;
 
253
 BOX *bp, *bpo;
 
254
 BOXINT *ip;
 
255
 Int16 n;
 
256
 
 
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;
 
260
 
 
261
 bp = np->box; mn = bp->boxmain; mno = bpo->boxmain;
 
262
 if (mn == mno) { mn->mncounter--; return; }
 
263
 n = mno->mnboxcnt;
 
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;
 
274
}
 
275
 
 
276
static void dead_line()
 
277
{
 
278
 BOX *bp;
 
279
 MN *mn;
 
280
 BOXINT *ip;
 
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;
 
287
}