| 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 "internal.h" |
| 58 |
#pragma hdrstop |
| 59 |
#include "lns.h" |
| 60 |
#include "hliner.h" |
| 61 |
#include "xmatrix.h" |
| 62 |
#include "xstack.h" |
| 63 |
#include "imgaccs.h" |
| 64 |
|
| 65 |
|
| 66 |
///////////////////////////////////////////////////// |
| 67 |
// Snp section |
| 68 |
#include "snptools.h" |
| 69 |
static SnpTreeNode stnHLiner; |
| 70 |
LNS_FUNC( void ) LnsRegisterSnpTree( |
| 71 |
SnpTreeNode* parent, // parent Snp Node, may be NULL |
| 72 |
__SnpToolBox* p_snp_tools // tools complect, may be NULL |
| 73 |
) |
| 74 |
{ |
| 75 |
SnpSetTools( p_snp_tools ); // may be NULL, it's OK |
| 76 |
SnpAddNode( &stnHLiner,"HLiner", parent); |
| 77 |
} |
| 78 |
////////////////////////////////////////////////////// |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
static int32_t nByteWidth; |
| 83 |
static int32_t nLine=0; |
| 84 |
|
| 85 |
//static Bool bReady = FALSE; |
| 86 |
|
| 87 |
static XMatrix< uchar > xmImageMap; |
| 88 |
static XStack<Line16> xsLines; |
| 89 |
extern const uchar bit1_cnt[256]; |
| 90 |
extern const uchar bit0_cnt[256]; |
| 91 |
static const uchar* BlackBitsCountTbl=bit1_cnt; |
| 92 |
static Tiger_ImageInfo ImageInfo={0}; |
| 93 |
|
| 94 |
|
| 95 |
Bool32 HLiner_Setup( Tiger_ImageInfo* pTgInfo ) |
| 96 |
{ |
| 97 |
ImageInfo = *pTgInfo; |
| 98 |
|
| 99 |
if (!xmImageMap.Create( (ImageInfo.wImageWidth+15) / 16, (ImageInfo.wImageHeight+1) / 2 )) |
| 100 |
return FALSE; |
| 101 |
xmImageMap.MemSet(0); |
| 102 |
|
| 103 |
nLine=0; |
| 104 |
|
| 105 |
//if (ImageInfo.bFotoMetrics != 0) // white pixel is 0 |
| 106 |
// BlackBitsCountTbl = bit1_cnt; |
| 107 |
//else |
| 108 |
BlackBitsCountTbl = bit0_cnt; |
| 109 |
|
| 110 |
return TRUE; |
| 111 |
} |
| 112 |
|
| 113 |
Bool32 HLiner_Init( void ) |
| 114 |
{ |
| 115 |
HLiner_Done(); |
| 116 |
return TRUE; |
| 117 |
} |
| 118 |
|
| 119 |
void HLiner_Done( void ) |
| 120 |
{ |
| 121 |
xsLines.Destroy(); |
| 122 |
xmImageMap.Destroy(); |
| 123 |
Set0(ImageInfo); |
| 124 |
nLine=0; |
| 125 |
} |
| 126 |
|
| 127 |
inline uchar* GetMapLine( int n ) |
| 128 |
{ |
| 129 |
return &(xmImageMap.Get( n, 0 )); |
| 130 |
} |
| 131 |
|
| 132 |
Bool32 HLiner_AddImageLine( uchar* pLine ) |
| 133 |
{ |
| 134 |
if (nLine < 0 || nLine > ImageInfo.wImageHeight-1) |
| 135 |
RET_FALSE; |
| 136 |
|
| 137 |
uchar* res = GetMapLine(nLine/2); |
| 138 |
int nWords = (ImageInfo.wImageWidth+15) / 16; //Almi: Вася нашёл эту ошибку 21 mar 2002 - было: = ImageInfo.wImageByteWidth >> 1; |
| 139 |
while (nWords--) |
| 140 |
{ |
| 141 |
*res += BlackBitsCountTbl[*pLine++]; // get 16 bits |
| 142 |
*res += BlackBitsCountTbl[*pLine++]; |
| 143 |
res++; |
| 144 |
} |
| 145 |
nLine++; |
| 146 |
return TRUE; |
| 147 |
} |
| 148 |
|
| 149 |
Bool32 _TraceLine(int i, int j, Line16& ln ) |
| 150 |
{ |
| 151 |
ln.A.x = (j << 4) + 8; // 16x2 |
| 152 |
ln.A.y = i << 1; |
| 153 |
|
| 154 |
// trace up |
| 155 |
int i_top=i; int i_bot=i+1; |
| 156 |
|
| 157 |
while ( GetMapLine(i_top)[j] == 100 ) |
| 158 |
{ |
| 159 |
GetMapLine(i_top)[j] = 102; // marked |
| 160 |
i_top --; // up |
| 161 |
} |
| 162 |
|
| 163 |
while ( GetMapLine(i_bot)[j] == 100 ) |
| 164 |
{ |
| 165 |
GetMapLine(i_bot)[j] = 102; // marked |
| 166 |
i_bot ++; // down |
| 167 |
} |
| 168 |
|
| 169 |
int ii = (i_top + i_bot) >> 1; |
| 170 |
ln.A.y = i_top + i_bot; // 16x2 |
| 171 |
|
| 172 |
int thick = 0; |
| 173 |
int jj = j; |
| 174 |
do |
| 175 |
{ jj ++; // step right |
| 176 |
thick = 0; |
| 177 |
|
| 178 |
if (GetMapLine(ii)[jj] != 100) // try step up or down |
| 179 |
{ |
| 180 |
if (GetMapLine(ii-1)[jj] == 100) |
| 181 |
ii--; |
| 182 |
else if (GetMapLine(ii+1)[jj] == 100) |
| 183 |
ii++; |
| 184 |
else // line ended |
| 185 |
{ |
| 186 |
ln.B.y = ii << 1; // 16x2 |
| 187 |
ln.B.x = (jj << 4) - 8; // 16x2, prev cell |
| 188 |
break; // line stopped |
| 189 |
} |
| 190 |
}; |
| 191 |
|
| 192 |
// update center of line and sweep |
| 193 |
i_top=ii; i_bot=ii+1; |
| 194 |
while ( GetMapLine(i_top)[jj] == 100 ) |
| 195 |
{ |
| 196 |
GetMapLine(i_top)[jj] = 102; // marked |
| 197 |
i_top --; // up |
| 198 |
} |
| 199 |
while ( GetMapLine(i_bot)[jj] == 100 ) |
| 200 |
{ |
| 201 |
GetMapLine(i_bot)[jj] = 102; // marked |
| 202 |
i_bot ++; // down |
| 203 |
} |
| 204 |
ii = (i_top + i_bot) >> 1; |
| 205 |
} while (1); |
| 206 |
|
| 207 |
return (jj - j) > 3; |
| 208 |
} |
| 209 |
|
| 210 |
int32_t HLiner_Analyze(void) // returns count of extracted lines |
| 211 |
{ |
| 212 |
int gap = 3; // count of zero cells on up and down direction |
| 213 |
int wid = 3; // count of notzero cells on left or right side |
| 214 |
int nHeight = xmImageMap.GetHeight(); |
| 215 |
int nWidth = xmImageMap.GetWidth(); |
| 216 |
|
| 217 |
int i(0); |
| 218 |
for (i=gap; i<nHeight-gap-1; i++) // by lines |
| 219 |
{ |
| 220 |
uchar* line_hi = GetMapLine(i - gap); |
| 221 |
uchar* line_on = GetMapLine(i); |
| 222 |
uchar* line_lo = GetMapLine(i + gap); |
| 223 |
for (int j=wid; j<nWidth-wid-1; j++) // first column |
| 224 |
{ |
| 225 |
if (!line_on[j]) |
| 226 |
continue; |
| 227 |
|
| 228 |
if ( !(line_hi[j]+line_lo[j]) |
| 229 |
&& (line_on[j-wid] + line_on[j+wid]) |
| 230 |
) |
| 231 |
{ |
| 232 |
line_on[j] = 100; // seems to be line fragment |
| 233 |
} |
| 234 |
else |
| 235 |
{ |
| 236 |
line_on[j] = 101; |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
// замазываем дырки и убиваем одиночек |
| 242 |
for (i=gap; i<nHeight-gap-1; i++) // by lines |
| 243 |
{ |
| 244 |
uchar* line_on = GetMapLine(i); |
| 245 |
for (int j=wid; j<nWidth-wid-1; j++) // first column |
| 246 |
{ |
| 247 |
if (line_on[j] == 100) |
| 248 |
{ |
| 249 |
if (line_on[j-1] != 100 && line_on[j+1] != 100) |
| 250 |
line_on[j] = 101; // kill |
| 251 |
} |
| 252 |
else |
| 253 |
{ |
| 254 |
if (line_on[j-1] == 100 && line_on[j+1]==100) |
| 255 |
line_on[j] = 100; // recover |
| 256 |
// else if (line_on[j-2] == 100 && line_on[j+2]==100) |
| 257 |
// line_on[j] = 100; // recover |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
// now link lines from start to end |
| 263 |
// |
| 264 |
uchar* line_on = NULL; |
| 265 |
Line16 ln={0}; |
| 266 |
for (i=gap; i<nHeight-gap-1; i++) // by lines |
| 267 |
{ |
| 268 |
uchar* line_on = GetMapLine(i); |
| 269 |
for (int j=wid; j<nWidth-wid-1; j++) // first column |
| 270 |
{ |
| 271 |
if (line_on[j] != 100) |
| 272 |
continue; |
| 273 |
// reach line cell |
| 274 |
if ( GetMapLine(i+1)[j-1] != 100 && |
| 275 |
GetMapLine(i)[j-1] != 100 && |
| 276 |
GetMapLine(i-1)[j-1] != 100 |
| 277 |
) |
| 278 |
{ // start new line |
| 279 |
if (!_TraceLine(i,j,ln)) |
| 280 |
continue; |
| 281 |
if (!xsLines.Push(&ln, NULL)) |
| 282 |
break; |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
if (!SnpSkip(&stnHLiner)) |
| 288 |
{ |
| 289 |
for (i=0; i<xsLines.GetCurCnt(); i++) |
| 290 |
{ |
| 291 |
Line16& li= xsLines[i]; |
| 292 |
SnpDrawLine( &li.A,&li.B,0,wRGB(255,0,255),2, &stnHLiner); |
| 293 |
} |
| 294 |
SnpWaitUserInput(&stnHLiner); |
| 295 |
SnpHideLines(&stnHLiner); |
| 296 |
} |
| 297 |
|
| 298 |
xmImageMap.Destroy(); // free, currently not used later |
| 299 |
|
| 300 |
return xsLines.GetCurCnt(); |
| 301 |
} |
| 302 |
|
| 303 |
int32_t HLiner_GetCount(void) |
| 304 |
{ |
| 305 |
return xsLines.GetCurCnt(); |
| 306 |
} |
| 307 |
|
| 308 |
Line16* HLiner_GetLine( int32_t nLineIndex ) |
| 309 |
{ |
| 310 |
return &(xsLines[nLineIndex]); |
| 311 |
} // 0..count-1 |
| 312 |
// NULL - wrong index or not ready |
| 313 |
|