?? faxfile.cpp
字號(hào):
}
else if( nBitCntr > 8 )
{
//OutputDebugString( "Whoops 2!\n" );
m_bFoundStartOfPage = false;
m_bFoundStartOfFrame = false;
}
}
}
}
}
return true;
}
//////////////////////////////////////////////////////////////////////
// ProcessECMFrame
//////////////////////////////////////////////////////////////////////
bool CFaxFile::ProcessECMFrame( void )
{
//char szMsg[160];
bool bRet = true;
if( m_ECMBuffer.CheckFCS() )
{
if( m_ECMBuffer.GetFCF() == FCD )
{
//wsprintf( szMsg, "ECM Frame %d bytes, Control=%02X FCF=%02X Seq=%d LastSeq=%d Data[0]=%02X\n", m_ECMBuffer.GetSize(),
// m_ECMBuffer.GetControl(), m_ECMBuffer.GetFCF(), m_ECMBuffer.GetSeq(), m_ECMBuffer.GetLastSeq(), m_ECMBuffer.GetData()[0] );
//OutputDebugString( szMsg );
int nGap = m_ECMBuffer.GetSeq() - m_ECMBuffer.GetLastSeq();
if( nGap < 1 )
{
//wsprintf( szMsg, "Backing up %d frames\n", 1 - nGap );
//OutputDebugString( szMsg );
m_MemFile.Seek( (nGap-1) * m_nECMFrameSize, SEEK_CUR );
//fseek( m_fp, (nGap-1) * m_nECMFrameSize, SEEK_CUR );
}
else if( nGap > 1 )
{
char Zeros[MAX_ECM_DATA];
ZeroMemory( Zeros, m_nECMFrameSize );
if( m_ECMBuffer.GetHighestSeq() > m_ECMBuffer.GetLastSeq() )
{
int nNewGap = m_ECMBuffer.GetHighestSeq() - m_ECMBuffer.GetLastSeq();
if( nNewGap > nGap )
{
nNewGap = nGap;
}
//wsprintf( szMsg, "Moving forward %d frames\n", nNewGap );
//OutputDebugString( szMsg );
m_MemFile.Seek( (nNewGap-1) * m_nECMFrameSize, SEEK_CUR );
//fseek( m_fp, (nNewGap-1) * m_nECMFrameSize, SEEK_CUR );
nGap -= nNewGap;
}
if( nGap > 0 )
{
//wsprintf( szMsg, "Skipping %d frames\n", nGap - 1 );
//OutputDebugString( szMsg );
for( int i = 1; i < nGap; i++ )
{
if( m_MemFile.Write( Zeros, m_nECMFrameSize ) == 0 )
//if( fwrite( Zeros, 1, m_nECMFrameSize, m_fp ) != m_nECMFrameSize )
{
//OutputDebugString( "Error writing to file\n" );
}
}
}
}
m_MemFile.Write( m_ECMBuffer.GetData(), m_nECMFrameSize );
//if( fwrite( m_ECMBuffer.GetData(), 1, m_nECMFrameSize, m_fp ) != m_nECMFrameSize )
{
//OutputDebugString( "Error writing to file\n" );
}
m_ECMBuffer.UpdateLastSeq();
}
else if( m_ECMBuffer.GetFCF() == RCP )
{
//OutputDebugString( "Got RCP\n" );
}
else
{
//OutputDebugString( "Unknown ECM Frame\n" );
}
}
else
{
//OutputDebugString( "Bad ECM Frame\n" );
bRet = false;
}
m_ECMBuffer.InitFrame();
return bRet;
}
//////////////////////////////////////////////////////////////////////
// NextBlock called between ECM blocks
//////////////////////////////////////////////////////////////////////
void CFaxFile::NextECMBlock(void)
{
m_MemFile.Seek( 0, SEEK_END );
m_ECMBuffer.InitFrame();
m_ECMBuffer.InitBlock();
m_bFoundStartOfPage = false;
m_bFoundStartOfFrame = false;
m_bFoundEndOfPage = false;
nZcnt = 0;
nRTCCnt = 0;
nBitCntr = 0;
nZeroBitDeleteCntr = 0;
}
//////////////////////////////////////////////////////////////////////
// NextBlock called between ECM blocks
//////////////////////////////////////////////////////////////////////
void CFaxFile::RedoECMBlock(void)
{
m_ECMBuffer.InitFrame();
m_bFoundStartOfPage = false;
m_bFoundStartOfFrame = false;
m_bFoundEndOfPage = false;
nZcnt = 0;
nRTCCnt = 0;
nBitCntr = 0;
nZeroBitDeleteCntr = 0;
}
//////////////////////////////////////////////////////////////////////
// WriteIFD prepares for the next fax page
//////////////////////////////////////////////////////////////////////
bool CFaxFile::WriteIFD(void)
{
m_bGotData = false;
m_bFoundStartOfPage = false;
m_bFoundStartOfFrame = false;
m_bFoundEndOfPage = false;
nZcnt = 0;
nRTCCnt = 0;
nBitCntr = 0;
nZeroBitDeleteCntr = 0;
bool bRet = ( WriteIfd() > 0 ) ? true : false;
if( bRet )
{
m_PageCount++;
}
return bRet;
}
//////////////////////////////////////////////////////////////////////
// WriteIfd dumps the page to disk
//////////////////////////////////////////////////////////////////////
int CFaxFile::WriteIfd (void)
{
int nRet = m_MemFile.Size();
if( length == 0 )
{
if( compression == 3 )
{
CountGroup3TiffLines();
}
else
{
CountGroup4TiffLines();
}
//char szMsg[80];
//wsprintf( szMsg, "Counted %d lines\n", length );
//OutputDebugString( szMsg );
}
if( (length < 10) || (nRet == 0 ) )
{
return 0;
}
TIFFSetField( m_tiff, TIFFTAG_SUBFILETYPE, 2 );
TIFFSetField( m_tiff, TIFFTAG_IMAGEWIDTH, width );
TIFFSetField( m_tiff, TIFFTAG_IMAGELENGTH, length);
TIFFSetField( m_tiff, TIFFTAG_PHOTOMETRIC, 0 );
TIFFSetField( m_tiff, TIFFTAG_ROWSPERSTRIP, length );
TIFFSetField( m_tiff, TIFFTAG_BITSPERSAMPLE, bitspersample);
TIFFSetField( m_tiff, TIFFTAG_FILLORDER, fillorder);
TIFFSetField( m_tiff, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH );
TIFFSetField( m_tiff, TIFFTAG_XRESOLUTION, xres );
TIFFSetField( m_tiff, TIFFTAG_YRESOLUTION, yres );
TIFFSetField( m_tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG );
TIFFSetField( m_tiff, TIFFTAG_COMPRESSION, compression );
TIFFSetField( m_tiff, TIFFTAG_PAGENUMBER, m_PageCount, 0 );
if( compression == COMPRESSION_CCITTFAX3 )
{
TIFFSetField( m_tiff, TIFFTAG_GROUP3OPTIONS, g3opts );
}
else if( compression == COMPRESSION_CCITTFAX4 )
{
TIFFSetField( m_tiff, TIFFTAG_GROUP4OPTIONS, 0 );
}
TIFFWriteRawStrip( m_tiff, 0, m_MemFile.GetData(0), m_MemFile.Size() );
TIFFWriteDirectory( m_tiff );
m_bFileHasData = true;
m_MemFile.Clear();
length = 0;
return nRet;
}
//////////////////////////////////////////////////////////////////////
// CountGroup3TiffLines
//////////////////////////////////////////////////////////////////////
void CFaxFile::CountGroup3TiffLines(void)
{
int i;
nZcnt = 0;
nRTCCnt = 0;
m_bFoundEndOfPage = false;
m_MemFile.Seek( 0, SEEK_SET );
for( i = 0; i < m_MemFile.Size(); i++ )
{
if( !m_bFoundEndOfPage )
{
BYTE b = *m_MemFile.GetData(i);
if (nZcnt)
{
nZcnt += s_LeadZero[b];
if (b && (nZcnt < 11))
{
nRTCCnt = 0;
nZcnt = s_TrailZero[b];
}
}
else
{
nZcnt = s_TrailZero[b];
if (!nZcnt) nRTCCnt = 0;
}
if( nZcnt > 10 && b != 0 )
{
nRTCCnt++;
if (nRTCCnt > 5)
{
//OutputDebugString( "Found RTC!\n" );
IncrementImageLength( -5 );
m_bFoundEndOfPage = true;
}
else
{
IncrementImageLength();
}
nZcnt = s_TrailZero[b];
}
}
}
}
//////////////////////////////////////////////////////////////////////
// ExpandLine - used by CountGroup4TiffLines
//////////////////////////////////////////////////////////////////////
void ExpandLine ( unsigned char color, char *cur_line,
int& bit_2D, int& i_2D, int runlength )
{
int copyrun=runlength ;
if (color==WHITE)
{
bit_2D-=(copyrun%8) ;
i_2D+=(copyrun/8) ;
if (bit_2D<0)
{
bit_2D+=8 ;
i_2D++ ;
}
}
else for (;;)
{
while (bit_2D>0)
{
cur_line[i_2D]|=(1<<(bit_2D-1)) ;
bit_2D-- ;
if (!(--copyrun)) break ;
}
if (copyrun==0) break ;
i_2D++ ;
for (; copyrun>8 ; copyrun-=8) cur_line[i_2D++]=(char)0xff ;
if (copyrun==0) break ;
bit_2D=8 ;
}
}
//////////////////////////////////////////////////////////////////////
// CountGroup4TiffLines
//////////////////////////////////////////////////////////////////////
void CFaxFile::CountGroup4TiffLines(void)
{
int i,j,k;
nZcnt = 0;
nRTCCnt = 0;
m_bFoundEndOfPage = false;
int dots_left= width;
int code= 0;
int resync, a0, b1, b2, horz_runs;
NODE *tree= (NODE *)&s_WTree ;
unsigned char octet, ref_color, this_bit, coding_scheme = 1;
unsigned char color;
char *cur_line;
int i_2D= 0;
int bit_2D=8 ;
char twolines[(2432/8)*2];
char *ref_line;
char *tmp_line;
ref_color = WHITE;
memset (twolines,0x00,(width/8)*2) ;
ref_line= &twolines[0] ;
cur_line= &twolines[width/8] ;
coding_scheme= 2 ;
resync= 0 ;
color= WHITE ;
dots_left= width ;
tree= (NODE *)&s_TwoTree ;
horz_runs= code= 0 ;
a0= (-1) ;
m_MemFile.Seek( 0, SEEK_SET );
for( k = 0; k < m_MemFile.Size(); k++ )
{
octet = *m_MemFile.GetData(k);
for (i= 0 ; i<8 ; i++, octet<<= 1)
{
if( !m_bFoundEndOfPage )
{
if ((horz_runs==0)&&(dots_left==0))
{
i_2D=0 ;
bit_2D=8 ;
IncrementImageLength();
coding_scheme= 2 ;
resync= 0 ;
color= WHITE ;
dots_left= width ;
tree= (NODE *)&s_TwoTree ;
tmp_line= ref_line ;
ref_line= cur_line ;
cur_line= tmp_line ;
memset (cur_line,0x00,width/8) ;
horz_runs= code= 0 ;
a0= (-1) ;
}
if (code== FAXFILE_EOL)
{
m_bFoundEndOfPage = true;
break;
}
if (code== FAXFILE_EOL2)
{
coding_scheme= ((octet&0x80) >> 7) ;
if (coding_scheme== 1) tree= (NODE *)&s_WTree ;
else
{
tree= (NODE *)&s_TwoTree ;
horz_runs= 0 ;
a0= (-1) ;
}
tmp_line= ref_line ;
ref_line= cur_line ;
cur_line= tmp_line ;
memset (cur_line,0x00,width/8) ;
code= 0 ;
continue ;
}
int nBit = (octet&0x80)>>7;
code= (*tree)[code][nBit] ;
if (code<1)
{
code= (-code) ;
if (!resync) if ((dots_left-= code)<0) resync= 1 ;
if ((!resync)&&(code!= 0)) ExpandLine( color, cur_line, bit_2D, i_2D, code) ;
if (code < 64)
{
color= (~color) ;
tree= color ? (NODE *)&s_BTree: (NODE *)&s_WTree;
if ((coding_scheme!= 1)&&(--horz_runs== 0))
{
a0= 0 ;
tree= (NODE *)&s_TwoTree ;
}
}
code= 0 ;
continue ;
}
if (code<FAXFILE_OFFTREE) continue ;
if (code== FAXFILE_OFFTREE)
{
resync= 1 ;
continue ;
}
if (code== FAXFILE_EOL)
{
continue;
}
if (code== FAXFILE_HORZ)
{
horz_runs= 2 ;
code= 0 ;
tree= color ? (NODE *)&s_BTree: (NODE *)&s_WTree;
continue ;
}
if (a0== (-1)) ref_color= WHITE ;
else
{
a0= (width-dots_left) ;
j= (a0/8) ;
this_bit= (0x80>>(a0%8)) ;
if ((unsigned char)ref_line[j]&this_bit) ref_color= BLACK ; else ref_color= WHITE ;
}
for (b1= a0+1;b1<width;b1++)
{
j= (b1/8) ;
this_bit= (0x80>>(b1%8)) ;
if (((unsigned char)ref_line[j]&this_bit)!= (ref_color&this_bit)) break ;
}
if (ref_color!= color)
{
for (b1++;b1<width;b1++)
{
j= (b1/8) ;
this_bit= (0x80>>(b1%8)) ;
if (((unsigned char)ref_line[j]&this_bit)== (ref_color&this_bit)) break ;
}
}
if (b1>width) b1= width ;
a0= (width-dots_left) ;
if (code<FAXFILE_PASS)
{
code= ((b1-a0)+(code-FAXFILE_V_0)) ; if (code < 0) resync= 1;
if (!resync) if ((dots_left-= code)<0) resync= 1 ;
if ((!resync)&&(code!= 0)) ExpandLine( color, cur_line, bit_2D, i_2D, code);
color= (~color) ;
code= 0 ;
continue ;
}
for (b2= b1+1;b2<width;b2++)
{
j= (b2/8) ;
this_bit= (0x80>>(b2%8)) ;
if (((unsigned char)ref_line[j]&this_bit)== (color&this_bit)) break ;
}
if (b2>width) b2= width ;
if (code== FAXFILE_PASS)
{
code= (b2-a0) ; if (code < 0) resync= 1;
if (!resync) if ((dots_left-= code)<0) resync= 1 ;
if ((!resync)&&(code!= 0)) ExpandLine( color, cur_line, bit_2D, i_2D, code);
code= 0 ;
continue ;
}
m_bFoundEndOfPage = true;
break;
}
}
}
}
//////////////////////////////////////////////////////////////////////
// SelectEncoding
//////////////////////////////////////////////////////////////////////
void CFaxFile::SelectEncoding( int nEncoding )
{
m_nSendEncoding = nEncoding;
}
//////////////////////////////////////////////////////////////////////
// Clear
//////////////////////////////////////////////////////////////////////
void CFaxFile::Clear(void)
{
bitspersample = 1;
width = 1728;
length = 0;
fillorder = 1;
g3opts = 0;
compression = COMPRESSION_CCITTFAX3;
xres = 204.0f;
yres = 196.0f;
m_MemFile.Clear();
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -