convert.cpp
上传用户:looem2003
上传日期:2014-07-20
资源大小:13733k
文件大小:30k
- #include "convert.h"
- #define FAX_WIDTH 1728
- #define FAX_HEIGTH 2292
- static unsigned char bmpHeader[62];
- static unsigned char bmpHeader1[62] =
- { 'B', 'M', 0xBE, 0x48, 0x28, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x28, 0x00,
- 0x00, 0x00, 0xA4, 0x01, 0x00, 0x00, 0x9D, 0x00,
- 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0xBC, 0x04, 0x03, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- };
- static unsigned char bmpHeader2[62] =
- { 'B', 'M', 0x3E, 0xF9, 0x15, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x28, 0x00,
- 0x00, 0x00, 0xA4, 0x01, 0x00, 0x00, 0x9D, 0x00,
- 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0xBC, 0x04, 0x03, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- };
- static BMPINFO bmpinfo;
- static long oldNo = 1,PageNo = 1;
- static TCHAR PageNameBmp[1024] = _T("");
- static int i_count = 0;
- static int i_dpi = 0;
- static double i_hegint = 0;
- static int i_h = 0;
- static int i_width = 0;
- static int i_size = 0;
- static int i_row = 0;
- static int i_row_old = -1;
- static bool b_row = false;
- const char* pjl = "@PJL";
- WCHAR marker[] = L"popfax";
- BOOL is_pjl( LPCTSTR fileName )
- {
- BOOL ret = FALSE;
- HANDLE hFile = NULL;
- char str[10] = "";
- DWORD nr = 0;
- if( (NULL != fileName)&&(0 != _tcslen(fileName))&&(-1 != _taccess( fileName, 0)) )
- {
- if( INVALID_HANDLE_VALUE != ( hFile = CreateFile( fileName, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0) ) )
- {
- ReadFile( hFile, str, (DWORD)strlen(pjl), &nr, 0 );
- if( nr == strlen(pjl ) )
- ret = ( 0 == _stricmp( str, pjl ) );
- CloseHandle( hFile );
- }
- }
- return ret;
- }
- BOOL is_marked( LPCTSTR fileName )
- {
- BOOL ret = FALSE;
- HANDLE hFile = NULL;
- WCHAR str[10] = L"";
- DWORD nr = 0;
- if( (NULL != fileName)&&(0 != _tcslen(fileName))&&(-1 != _taccess( fileName, 0)) )
- {
- if( INVALID_HANDLE_VALUE != ( hFile = CreateFile( fileName, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0) ) )
- {
- ReadFile( hFile, str, (DWORD)_tcslen(marker)*sizeof(marker[0]), &nr, 0 );
- ret = ( 0 == wcsicmp( marker, str ) );
- CloseHandle( hFile );
- }
- }
- return ret;
- }
- int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
- {
- UINT num = 0; // number of image encoders
- UINT size = 0; // size of the image encoder array in bytes
- ImageCodecInfo* pImageCodecInfo = NULL;
- GetImageEncodersSize(&num, &size);
- if(size == 0)
- return -1; // Failure
- pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
- if(pImageCodecInfo == NULL)
- return -1; // Failure
- GetImageEncoders(num, size, pImageCodecInfo);
- for(UINT j = 0; j < num; ++j)
- {
- if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
- {
- *pClsid = pImageCodecInfo[j].Clsid;
- free(pImageCodecInfo);
- return j; // Success
- }
- }
- free(pImageCodecInfo);
- return -1; // Failure
- }
- BOOL is_need_to_resize( CxImage* inImg )
- {
- BOOL ret = FALSE;
- RGBQUAD pi = {0};
- for( DWORD xx = 0; xx < inImg->GetWidth(); xx++ )
- {
- for( DWORD yy = 0; yy < min( inImg->GetHeight()-1, 5 ); yy++ )
- {
- pi = inImg->GetPixelColor( xx, yy );
- if( pi.rgbBlue == 0 )
- {
- ret = TRUE;
- break;
- }
- }
- for( DWORD yy = inImg->GetHeight()-1; yy > (inImg->GetHeight() - 5); yy-- )
- {
- pi = inImg->GetPixelColor( xx, yy );
- if( pi.rgbBlue == 0 )
- {
- ret = TRUE;
- break;
- }
- }
- }
- for( DWORD yy = 0; yy < inImg->GetHeight(); yy++ )
- {
- for( DWORD xx = 0; xx < min( inImg->GetWidth()-1, 5 ); xx++ )
- {
- pi = inImg->GetPixelColor( xx, yy );
- if( pi.rgbBlue == 0 )
- {
- ret = TRUE;
- break;
- }
- }
- for( DWORD xx = inImg->GetWidth()-1; xx > (inImg->GetWidth() - 5); xx-- )
- {
- pi = inImg->GetPixelColor( xx, yy );
- if( pi.rgbBlue == 0 )
- {
- ret = TRUE;
- break;
- }
- }
- }
- return ret;
- }
- BOOL create_tiff( LPADDITIONALCONFIG aac, DWORD index, LPCTSTR fileName, DWORD pages )
- {
- BOOL ret = FALSE;
- Bitmap *bmp = new Bitmap( fileName );
- Bitmap *bmpClone = NULL;
- WCHAR bmpNames[LONG_PATH] = L"";
- TCHAR pageNameBmp[LONG_PATH] = _T("");
- TCHAR pageNameBmpMn[LONG_PATH] = _T("");
- std::wstring names;
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
- RGBQUAD whiteColor = { 255, 255, 255, 0 };
- //BOOL doResize = FALSE;
- ZeroMemory( &si, sizeof(si) );
- si.cb = sizeof(si);
- ZeroMemory( &pi, sizeof(pi) );
- if( NULL != bmp )
- {
- CLSID bmpClsid;
- GetEncoderClsid(L"image/bmp", &bmpClsid);
- for( DWORD i = 0; i < pages; i++ )
- {
- if( NULL != (bmpClone = bmp->Clone( 0, i*(bmp->GetHeight()/pages), bmp->GetWidth(), (bmp->GetHeight()/pages), bmp->GetPixelFormat()) ) )
- {
- swprintf_s( bmpNames, ARRAYSIZE(bmpNames), L"%s_%d", fileName, i+1);
- bmpClone->Save( bmpNames, &bmpClsid);
- delete bmpClone;
- CxImage* inImg = new CxImage( bmpNames, CXIMAGE_FORMAT_BMP );
- if( NULL != inImg )
- {
- if( aac->dithering )
- inImg->Dither( 0 );
- else
- inImg->Threshold( 123 );
- _stprintf_s( pageNameBmp, ARRAYSIZE(pageNameBmp), _T("%s_dither.bmp"), bmpNames );
- if( inImg->GetHeight() < inImg->GetWidth() )//this is landscape
- inImg->Rotate( 90 );
- if( is_need_to_resize( inImg ) )//漕徉忤