Debian Codegraph @

cuneiform_src/Kern/lns32/src/sweeper.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 #include "lnslang.h"
58
59 #include "lns.h"
60
61 #include <string.h>
62
63 #include "sbambuk.h"
64 #include "sweeper.h"
65
66 #include "memops.h"
67
68
69 static TImageOpen __f_op = NULL;
70 static TImageRead __f_rd = NULL;
71 static TImageClose __f_cl = NULL;
72
73 //*******************Rom********************
74 RomBubble* Romptr;// = new RomBubble ;
75 //Romptr->next = NULL;
76 RomBubble* Romptr2 = NULL;
77 RomBubble* Romptr3 = Romptr;
78 uchar tmp_w8;
79 //div_t div_result;
80 //******************************************
81
82 #define Tiger_Callback_ImageOpen (*__f_op)
83 #define Tiger_Callback_ImageRead (*__f_rd)
84 #define Tiger_Callback_ImageClose (*__f_cl)
85
86 #define BUF_LEN ((int32_t)0x7FFF)
87
88 //***********Rom**************
89 void SetRomptr ( RomBubble* rptr)
90 {
91 Romptr = rptr;
92 }
93 //****************************
94
95 void InitSweeperCallbacks(
96 TImageOpen f_op,
97 TImageRead f_rd,
98 TImageClose f_cl
99 )
100 {
101 __f_op = f_op;
102 __f_rd = f_rd;
103 __f_cl = f_cl;
104 }
105
106 static THVSegBambuk* hvSB = NULL;
107 void InitSweeperData( THVSegBambuk* sb )
108 {
109 hvSB=sb;
110 }
111
112 static Bool MustCloseImage = FALSE;
113 static Tiger_ImageInfo ImageInfo;
114
115 //static uchar* ImageBuf = NULL;
116 static uchar* LinesBuf = NULL;
117 //static uchar* ResultBuf = NULL;
118 static int32_t CurLine = 0;
119 static Bool Ready = FALSE;
120
121 Bool Sweeper_ImageOpen (Tiger_ImageInfo* lpImageInfo)
122 {
123
124 if (hvSB == NULL)
125 return WRONG();
126 if ((__f_op == NULL) || (__f_rd == NULL) || (__f_cl == NULL))
127 return WRONG();
128
129 assert(LinesBuf == NULL);
130 assert(CurLine == 0);
131 assert(!Ready);
132 if ( (LinesBuf != NULL) ||
133 (CurLine != 0) ||
134 (Ready)
135 )
136 return WRONG();
137
138
139 LinesBuf = (uchar*)malloc( BUF_LEN );
140 if (LinesBuf == NULL) return WRONG();
141
142 MustCloseImage = TRUE;
143 if (!Tiger_Callback_ImageOpen(lpImageInfo))
144 return WRONG();
145 ImageInfo = *lpImageInfo;
146 if (ImageInfo.wImageByteWidth * 4 >= BUF_LEN )
147 return WRONG();
148 memset( LinesBuf, 0 , BUF_LEN );
149 Ready = TRUE;
150 return TRUE;
151 }
152
153 static void unpack_segments( uchar* dstprev, uchar* dst, uchar* dstnext, int32_t line )
154 {
155 // after unpack segments black pixels are 1, white 0
156 // initially all filled by 0
157 TSegBambuk& sb = hvSB->hBambuk;
158 if (line > sb.lastEntry() )
159 return;
160
161 BHandle sh = sb.firstEntryMember( line );
162
163 while ( sh != NULLBHandle )
164 {
165 TBlackSeg& bs = sb[sh];
166 if (bs.right < bs.left)
167 {
168 int right = bs.left+1;
169 int left = bs.right;
170 int leftoff = left >> 3;
171 uchar* pb = dst + leftoff;
172 uchar* prev = dstprev + leftoff;
173 uchar* next = dstnext + leftoff;
174
175 uchar left_byte = ( 0xFF >> ( left - (leftoff<<3) ) );
176 *pb++ |= left_byte;
177 *prev++ |= left_byte;
178 *next++ |= left_byte;
179
180 int rightoff = right >> 3;
181 for ( int i = rightoff - leftoff -1; i > 0; i-- )
182 {
183 *pb ++ |= 0xFF;
184 *prev++ |= 0xFF;
185 *next++ |= 0xFF;
186 }
187 assert( (pb - dst) == rightoff );
188 uchar right_byte = ~( 0xFF >> ( right - (rightoff<<3) ) );
189 *pb |= right_byte;
190 *prev |= right_byte;
191 *next |= right_byte;;
192 }
193 sh = sb.nextMember( sh );
194 }
195 }
196
197 int16_t Sweeper_ImageRead (uchar* lpImage, uint16_t wMaxSize)
198 {
199 if (!Ready)
200 return 0;
201
202 int32_t max_read_rqst = BUF_LEN - 4*ImageInfo.wImageByteWidth;
203 int32_t size_read = (int32_t)Tiger_Callback_ImageRead( (uchar*)lpImage, (uint16_t) MIN( wMaxSize, max_read_rqst ) );
204 if (size_read ==0)
205 return 0;
206 assert( size_read <= max_read_rqst );
207 assert( (size_read % ImageInfo.wImageByteWidth) == 0 );
208
209 int32_t dw_size = ( size_read + 3 ) >> 2;
210 int lines_read = size_read / ImageInfo.wImageByteWidth;
211 assert( (ImageInfo.wAddX % 8) == 0 );
212 Romptr2 = Romptr->next; //********Rom
213 Romptr3 = Romptr; //********Rom
214 uchar* scn = LinesBuf + (ImageInfo.wAddX >> 3);
215 /*
216 if (CurLine == 0) // first call
217 { unpack_segments( scn, scn, scn + ImageInfo.wImageByteWidth, CurLine );
218 CurLine++;
219 }
220 */
221
222 memset( LinesBuf, 0 , (lines_read+2) * ImageInfo.wImageByteWidth );
223
224 if (CurLine > 0) // unpack upper line to suppress dusts under it
225 unpack_segments( scn, scn, scn + ImageInfo.wImageByteWidth, CurLine-1 );
226
227 scn += ImageInfo.wImageByteWidth; // now scn shifted 1 line down;
228
229 for ( int i=0; i<lines_read; i++, CurLine++, scn += ImageInfo.wImageByteWidth )
230 {
231 unpack_segments( scn-ImageInfo.wImageByteWidth, // prev line
232 scn, // cur line
233 scn+ImageInfo.wImageByteWidth, // next line
234 CurLine
235 );
236 // after unpack segments black pixels are 1, white 0
237 }
238
239 ////// VP, 17 jun 99
240 if (CurLine < ImageInfo.wImageHeight-1)
241 unpack_segments( scn-ImageInfo.wImageByteWidth, scn, scn, CurLine+1 );
242 //////////////////
243
244 if (ImageInfo.bFotoMetrics)
245 { // white pixel is 1
246
247 __CopyOR( lpImage, LinesBuf + ImageInfo.wImageByteWidth, size_read );
248
249 //*******************Rom***************
250 for (int j=0; j<lines_read; j++)
251 {
252 while (( Romptr2 != NULL )&&(Romptr2->Begin <= CurLine-lines_read+1+j))
253 {
254 if(Romptr2->Begin <= CurLine-lines_read+1+j)
255 {
256 if (Romptr2->End < CurLine-lines_read+1+j)
257 {
258 Romptr3->next = Romptr2->next;
259 //delete Romptr2;
260 Romptr2 = Romptr3;
261 }
262 else
263 {
264 // div_result = div ( Romptr2->Column, 8);
265 // tmp_w8 = lbit_byte[div_result.rem];
266 // lpImage[j*ImageInfo.wImageByteWidth+div_result.quot] |= tmp_w8;
267 int off = j*ImageInfo.wImageByteWidth + (Romptr2->Column >> 3);
268 int pix = Romptr2->Column & 7;
269 lpImage[off] |= ( 1 << (7-pix) );
270 int off1 = j*ImageInfo.wImageByteWidth + ((Romptr2->Column+1) >> 3);
271 int pix1 = (Romptr2->Column+1) & 7;
272 lpImage[off1] |= ( 1 << (7-pix1) );
273 int off2 = j*ImageInfo.wImageByteWidth + ((Romptr2->Column-1) >> 3);
274 int pix2 = (Romptr2->Column-1) & 7;
275 lpImage[off2] |= ( 1 << (7-pix2) );
276 }
277 }
278 Romptr3 = Romptr2;
279 if (Romptr3!=NULL) Romptr2 = Romptr3->next;
280 }
281 Romptr2 = Romptr->next; //********Rom
282 Romptr3 = Romptr; //********Rom
283 }
284 //**************************************
285 }
286 else
287 { // white pixel is 0
288 __InvertSelf( LinesBuf + ImageInfo.wImageByteWidth, size_read );
289 __CopyAND( lpImage, LinesBuf + ImageInfo.wImageByteWidth, size_read );
290 //*******************Rom***************
291 for (int j=0; j<lines_read; j++)
292 {
293 while (( Romptr2 != NULL )&&(Romptr2->Begin <= CurLine-lines_read+1+j))
294 {
295 if(Romptr2->Begin <= CurLine-lines_read+1+j)
296 {
297 if (Romptr2->End < CurLine-lines_read+1+j)
298 {
299 Romptr3->next = Romptr2->next;
300 //delete Romptr2;
301 Romptr2 = Romptr3;
302 }
303 else
304 {
305 // div_result = div ( Romptr2->Column, 8);
306 // tmp_w8 = ~lbit_byte[div_result.rem];
307 // lpImage[j*ImageInfo.wImageByteWidth+div_result.quot] &= tmp_w8;
308 int off = j*ImageInfo.wImageByteWidth + (Romptr2->Column >> 3);
309 int pix = Romptr2->Column & 7;
310 lpImage[off] &= ~( 1 << (7-pix) );
311 int off1 = j*ImageInfo.wImageByteWidth + ((Romptr2->Column+1) >> 3);
312 int pix1 = (Romptr2->Column+1) & 7;
313 lpImage[off1] &= ~( 1 << (7-pix1) );
314 int off2 = j*ImageInfo.wImageByteWidth + ((Romptr2->Column-1) >> 3);
315 int pix2 = (Romptr2->Column-1) & 7;
316 lpImage[off2] &= ~( 1 << (7-pix2) );
317 }
318 }
319 Romptr3 = Romptr2;
320 if (Romptr3!=NULL) Romptr2 = Romptr3->next;
321 }
322 Romptr2 = Romptr->next; //********Rom
323 Romptr3 = Romptr; //********Rom
324 }
325 //**************************************
326
327 }
328 // pop up two last lines in lines buf
329 /*
330 memcpy( LinesBuf, scn-(ImageInfo.wAddX % 8), ImageInfo.wImageByteWidth*2);
331 memset( LinesBuf+ImageInfo.wImageByteWidth*2, 0 , size_read + ImageInfo.wImageByteWidth );
332 */
333
334 return (int16_t)size_read;
335 }
336
337 Bool Sweeper_ImageClose (void)
338 { //...
339 if (LinesBuf)
340 free(LinesBuf );
341 LinesBuf = NULL;
342 CurLine = 0;
343 Ready = FALSE;
344 //...
345 Bool ret = TRUE;
346 if (MustCloseImage)
347 { ret = Tiger_Callback_ImageClose();
348 MustCloseImage = FALSE;
349 }
350 return ret;
351 }