| 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 |
#ifndef __BLCKWHTE_H |
| 60 |
# include "blckwhte.h" |
| 61 |
#endif |
| 62 |
|
| 63 |
#ifndef __SBAMBUK_H |
| 64 |
# include "sbambuk.h" |
| 65 |
#endif |
| 66 |
|
| 67 |
void SetupHConverter( int right, |
| 68 |
int left, |
| 69 |
int filter_len, |
| 70 |
TSegBambuk* bambuk, |
| 71 |
Bool include_borders |
| 72 |
); |
| 73 |
|
| 74 |
void ConvertBWLine( void* image_line_ptr ); |
| 75 |
|
| 76 |
|
| 77 |
#define Word16_BIT_SIZE 16 |
| 78 |
|
| 79 |
#ifndef NDEBUG |
| 80 |
int32_t tot_seg_extracted = 0; |
| 81 |
#endif |
| 82 |
|
| 83 |
static uint16_t left_pixel_ = 1; // left border in pixels [1..) |
| 84 |
static uint16_t right_pixel_ = 0; // right border (included) in pixels [16..) |
| 85 |
static uint16_t left_word_offset_ = 0; // offset from start of line in word (0..); |
| 86 |
static uint16_t left_pixel_offset_ = 0; // pixel in word from wich starts line |
| 87 |
// [0..Word16_BIT_SIZE-1] |
| 88 |
static uint16_t right_pixel_offset_ = 0; |
| 89 |
static Bool include_border_segments_ = TRUE; // if FALSE - segments touching border are ignored |
| 90 |
static TSegBambuk* bambuk_ptr_ = NULL; // all line segments |
| 91 |
static int filter_len_ = 0; // segments shorter (or equal) filter_len |
| 92 |
// are ignored |
| 93 |
|
| 94 |
static inline void new_segment( int left, int right ); |
| 95 |
|
| 96 |
#ifndef NDEBUG |
| 97 |
// extern word seg_len_histo[34]; |
| 98 |
#endif |
| 99 |
|
| 100 |
|
| 101 |
void SetupHConverter( int left, |
| 102 |
int right, |
| 103 |
int filter_len, |
| 104 |
TSegBambuk* bambuk, |
| 105 |
Bool include_borders |
| 106 |
) |
| 107 |
{ |
| 108 |
assert( right > left ); |
| 109 |
assert( filter_len >= 0 ); |
| 110 |
assert( bambuk != NULL ); |
| 111 |
assert( bambuk->isOk() ); |
| 112 |
|
| 113 |
left_pixel_ = left; |
| 114 |
right_pixel_ = right; |
| 115 |
left_word_offset_ = left / Word16_BIT_SIZE; |
| 116 |
left_pixel_offset_ = left % Word16_BIT_SIZE; |
| 117 |
right_pixel_offset_ = right % Word16_BIT_SIZE; |
| 118 |
include_border_segments_ = include_borders; |
| 119 |
|
| 120 |
bambuk_ptr_ = bambuk; |
| 121 |
filter_len_ = filter_len; |
| 122 |
} |
| 123 |
|
| 124 |
#define NO_DOTMATRIX |
| 125 |
static int prev_left=0; |
| 126 |
static int prev_right=0; |
| 127 |
|
| 128 |
static inline void line_start() |
| 129 |
{ |
| 130 |
prev_right = 0; |
| 131 |
prev_left = 0; |
| 132 |
} |
| 133 |
|
| 134 |
static inline void line_finish() |
| 135 |
{ |
| 136 |
#ifndef NO_DOTMATRIX // old style processing |
| 137 |
if (prev_right) |
| 138 |
if ((prev_right-prev_left) >= filter_len_) |
| 139 |
bambuk_ptr_->addHorisontal( prev_left, prev_right ); |
| 140 |
#endif |
| 141 |
} |
| 142 |
|
| 143 |
static inline void new_segment( int left, int right ) |
| 144 |
{ |
| 145 |
|
| 146 |
#ifdef NO_DOTMATRIX // old style processing |
| 147 |
|
| 148 |
if ((right-left) >= filter_len_) |
| 149 |
bambuk_ptr_->addHorisontal( left, right ); |
| 150 |
|
| 151 |
#else // DOTMATRIX |
| 152 |
|
| 153 |
if (prev_right) // not first segment |
| 154 |
{ |
| 155 |
if (left - prev_right < 4) // small gap? |
| 156 |
prev_right = right; // expand right |
| 157 |
else // big gap - register prev segment and save new one |
| 158 |
{ |
| 159 |
if ((prev_right-prev_left) >= filter_len_) |
| 160 |
bambuk_ptr_->addHorisontal( prev_left, prev_right ); |
| 161 |
prev_left = left; |
| 162 |
prev_right=right; |
| 163 |
} |
| 164 |
} |
| 165 |
else |
| 166 |
{ // first segment |
| 167 |
prev_left = left; |
| 168 |
prev_right=right; |
| 169 |
} |
| 170 |
|
| 171 |
#endif // NO_DOTMATRIX |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
inline uint16_t GetWord16( uint16_t* pw ) |
| 176 |
#ifndef BIG_ENDIAN |
| 177 |
{ return *pw; } |
| 178 |
#else |
| 179 |
{ return (*pw << 8 ) | (*pw >> 8); } |
| 180 |
#endif |
| 181 |
|
| 182 |
|
| 183 |
const int trail_zeros[256] = // byte=>count of trailed zeros |
| 184 |
{ |
| 185 |
/* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ |
| 186 |
/*0*/ 8,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, |
| 187 |
/*1*/ 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, |
| 188 |
/*2*/ 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, |
| 189 |
/*3*/ 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, |
| 190 |
/*4*/ 6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, |
| 191 |
/*5*/ 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, |
| 192 |
/*6*/ 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, |
| 193 |
/*7*/ 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, |
| 194 |
/*8*/ 7,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, |
| 195 |
/*9*/ 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, |
| 196 |
/*a*/ 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, |
| 197 |
/*b*/ 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, |
| 198 |
/*c*/ 6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, |
| 199 |
/*d*/ 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, |
| 200 |
/*e*/ 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, |
| 201 |
/*f*/ 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0 |
| 202 |
}; |
| 203 |
|
| 204 |
|
| 205 |
#include "fillgap3.h" |
| 206 |
void ConvertBWLine( void* image_line_ptr |
| 207 |
) |
| 208 |
{ |
| 209 |
#ifndef BLACK_IS_ZERO |
| 210 |
#error Wow, !BLACK_IS_ZERO, Correct, me please! |
| 211 |
#endif |
| 212 |
|
| 213 |
image_line_ptr = FillGap3_SubstLine( (uchar*)image_line_ptr ); |
| 214 |
|
| 215 |
register uchar* pb = (uchar*)image_line_ptr + left_pixel_/8; |
| 216 |
uchar* pfirst = (uchar*)image_line_ptr; |
| 217 |
uchar* plast = (uchar*)image_line_ptr + right_pixel_/8; |
| 218 |
uchar last_byte = *plast; |
| 219 |
int left, right; |
| 220 |
line_start(); |
| 221 |
#ifdef NO_DOTMATRIX |
| 222 |
for (;;) |
| 223 |
{ |
| 224 |
// loop on white area |
| 225 |
*plast = 0x00; |
| 226 |
while (*++pb){;} // skip for entire black segment |
| 227 |
if (!(plast-pb)) // stopped on last byte? |
| 228 |
break; |
| 229 |
// started black area... |
| 230 |
left = ((pb-pfirst)<<3) - trail_zeros[*(pb-1)]; |
| 231 |
*plast = 0xff; |
| 232 |
while (!*++pb){;} |
| 233 |
if (!(plast-pb)) // stopped on last byte? |
| 234 |
{ |
| 235 |
right = ((pb-pfirst)<<3) + first_bit1_pos[ last_byte ]; |
| 236 |
new_segment(left, right); |
| 237 |
break; |
| 238 |
}; |
| 239 |
right = ((pb-pfirst)<<3) + first_bit1_pos[*pb]; |
| 240 |
new_segment(left, right); |
| 241 |
}; |
| 242 |
#else // DOTMATRIX |
| 243 |
while (plast!=pb) // not the last byte |
| 244 |
{ |
| 245 |
if (white) |
| 246 |
{ |
| 247 |
*plast = 0x00; |
| 248 |
while (*++pb==0xff){;} // skip clean white bytes |
| 249 |
if (!(plast-pb)) // stopped on last byte? |
| 250 |
break; |
| 251 |
|
| 252 |
AlgStatus& as= stat_tbl[*pb]; |
| 253 |
if (as.next) |
| 254 |
{ *pb++; |
| 255 |
continue; |
| 256 |
} |
| 257 |
else |
| 258 |
{ |
| 259 |
right = as.right; |
| 260 |
*pb = as. |
| 261 |
|
| 262 |
|
| 263 |
white |
| 264 |
|
| 265 |
|
| 266 |
left = first_bit0_pos[*pb]+((pb-pfirst)<<3); |
| 267 |
pb += |
| 268 |
|
| 269 |
} |
| 270 |
else |
| 271 |
{ |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
for (;;) |
| 277 |
{ |
| 278 |
// loop on white area |
| 279 |
*plast = 0x00; |
| 280 |
while (*++pb==0xff){;} // skip clean white bytes |
| 281 |
if (!(plast-pb)) // stopped on last byte? |
| 282 |
break; |
| 283 |
// started black area... |
| 284 |
left = ((pb-pfirst)<<3) - trail_zeros[*(pb-1)]; |
| 285 |
*plast = 0xff; |
| 286 |
while (!*++pb){;} |
| 287 |
if (!(plast-pb)) // stopped on last byte? |
| 288 |
{ |
| 289 |
right = ((pb-pfirst)<<3) + first_bit1_pos[ last_byte ]; |
| 290 |
new_segment(left, right); |
| 291 |
break; |
| 292 |
}; |
| 293 |
right = ((pb-pfirst)<<3) + first_bit1_pos[*pb]; |
| 294 |
new_segment(left, right); |
| 295 |
}; |
| 296 |
#endif |
| 297 |
|
| 298 |
line_finish(); |
| 299 |
} |