rpcl.cpp
上传用户:looem2003
上传日期:2014-07-20
资源大小:13733k
文件大小:16k
- /*
- Copyright (C) 2007 Ibadov Tariel <itk@bk.ru>
- */
- #include "rpcl.h"
- #define FAX_WIDTH 1728
- #define FAX_HEIGTH 2292
- class IfStream
- {
- public:
- IfStream( const char* fileName );
- int get();
- BOOL read( char* buff, int len );
- ~IfStream();
- private:
- HANDLE hFile;
- };
- static BMPINFO bmpinfo;
- //----------------------------------------------------------------
- static long PageNo = 1;
- static long oldPageNo = 1;
- static char PageNameBmp[1024];
- static int i_count;
- static int i_dpi;
- static int i_hegint;
- static int i_h;
- static int i_width;
- static int i_size;
- static int i_row;
- static bool b_row;
- int start_convert (char *NamePcl, char *NameBmp ,int img_format, int img_format_ext, BOOL dither, char* extApp)
- {
- i_count = i_dpi = i_hegint = i_h = i_width = i_size = i_row = 0;
- b_row = false;
- bmpinfo.width = 0;
- bmpinfo.height = 0;
- bmpinfo.colours = 24;
- bmpinfo.palette = NULL;
- bmpinfo.bits = NULL;
- pcl2bmp( NamePcl, NameBmp, img_format, img_format_ext, dither, extApp);
- return i_count;
- }
- int pcl2bmp( char *NamePcl, char *NameBmp,int img_format, int img_format_ext, BOOL dither, char* extApp)
- {
- //std::ifstream ifPcl( NamePcl, std::ios::in | std::ios::binary );
- IfStream ifPcl( NamePcl );
- //ifPcl.setf( std::ios::hex, std::ios::dec );
- int ch = ifPcl.get();
- unsigned char* bits = NULL;
- int div = 6;
- int bmp_height = 0;
- int bmp_width = 0;
- while( ch != EOF )
- {
- switch( ch )
- {
- case 0x2A:
- {
- ch = ifPcl.get();
- ///read dpi
- if( ( ch == 0x74 )&&( i_dpi == 0 ) )
- {
- int i_tmp = 0;
- std::string s_dpi;
- while( ( ch != 82 )&&( i_tmp < 6 ) )
- {
- ch = ifPcl.get();
- s_dpi += ch;
- i_tmp++;
- }
- i_dpi = atoi((char *)s_dpi.c_str());
- switch( i_dpi )
- {
- case 100:
- div = 6;
- bmp_height = 1100;
- bmp_width = 800;
- //bits = new unsigned char[bmp_height*bmp_width*3];
- break;
- case 200:
- div = 3;
- bmp_height = 2300;
- bmp_width = 1640;
- //bits = (unsigned char*)bits_200dpi;
- break;
- case 300:
- div = 2;
- bmp_height = 3507;
- bmp_width = 2480;
- /*bits = (unsigned char*)bits_300dpi;*/
- break;
- case 600:
- div = 1;
- bmp_height = 7014;
- bmp_width = 4960;
- /*bits = (unsigned char*)bits_600dpi;*/
- break;
- }
- if( ( 0 != bmp_width ) && ( 0 != bmp_height ) )
- {
- bmpinfo.height = bmp_height;
- bmpinfo.width = bmp_width;
- bmpinfo.bits = (bits = new unsigned char[bmp_height*bmp_width*3]);
- memset( bits, 0xff, bmp_height*bmp_width*3 );
- }
- }
- ///read hegint and width
- if( ch == 0x70 )
- {
- int i_tmp = 0;
- std::string s_size;
- while( ( ch != 0x59 )&&( i_tmp < 10 )&&( ch != 0x58 ) )
- {
- ch = ifPcl.get();
- s_size += ch;
- i_tmp++;
- }
- if( ( ch == 0x59 )&&( s_size != "CC90x0Y" ) )
- {
- i_hegint = atoi((char *)s_size.c_str());
- i_row = i_hegint / div;
- b_row = true;
- i_h++;
- }
- if( ch == 0x58 )
- {
- i_width = atoi((char *)s_size.c_str());
- }
- if( s_size == "PORTRAITx0" )
- {
- bmpinfo.height = bmp_height;
- }
- if( s_size == "CC90x0Y" )
- {
- bmpinfo.height = 600;
- }
- }
- if( ch == 0x63 )
- {
- int i_tmp = 0;
- std::string s_size;
- while( i_tmp < 10 )
- {
- ch = ifPcl.get();
- s_size += ch;
- i_tmp++;
- }
- if( s_size == "0t8129x571" )
- {
- bmpinfo.height = 600;
- }
- }
- /// Begin raster graphics at the current cursor position.
- if( ch == 0x62 )
- {
- int i_tmp = 0;
- std::string s_size;
- if( b_row )
- b_row = false;
- else
- i_row++;
- while( ( i_tmp < 5 )&&( ch != 0x57 )&&( ch != 'M' ) )
- {
- ch = ifPcl.get();
- s_size += ch;
- i_tmp++;
- }
- if( ch != 'M' )
- {
- i_size = atoi((char *)s_size.c_str());
- int q = 0,i = 0, qq = 0;
- char* pdata = new char[i_size];
- ch = ifPcl.get();
- if( NULL != pdata )
- {
- ifPcl.read( pdata,i_size);
- for( qq = 0; qq < i_size; qq++)
- {
- if( img_format != 4 )
- {
- if( q == 3 )
- {
- q = (int)(bits+i_row*(3*bmp_width)+i-3)[0];
- (bits+i_row*(3*bmp_width)+i-3)[0] = (bits+i_row*(3*bmp_width)+i-1)[0];
- (bits+i_row*(3*bmp_width)+i-1)[0] = (unsigned char)q;
- q = 0;
- }
- q++;
- }
- if( ( i_row < bmp_height)&&( i < (3*bmp_width) ) )
- (bits+i_row*(3*bmp_width)+i++)[0] = pdata[qq];
- }
- delete [] pdata;
- }
- }
- } // end graphics
- if( ch == 0x72 )
- {///end page
- ch = ifPcl.get();
- if( ( ch == 0x43 )||( ch == 0x42 ) )
- {
- ch = ifPcl.get();
- if( ch == 0x0C )
- {
- sprintf( PageNameBmp, "%s%d", NameBmp, PageNo );
- i_count++;
- write_log( 3, "pcl2bmp", "%d %d %s", PageNo, i_count, PageNameBmp);
- if (img_format==4)
- pdf_save( PageNameBmp, &bmpinfo );
- else
- {
- bmp_save( PageNameBmp, &bmpinfo );
- if (img_format==0)
- start_toJPG(PageNameBmp,PageNameBmp);
- if (img_format==1)
- {
- write_log( 3, "pcl2bmp", "tiff");
- if( (COMPRESSION_CCITTFAX4 == img_format_ext)||(COMPRESSION_CCITTFAX3 == img_format_ext) )
- {
- write_log( 3, "pcl2bmp", "tiff g3");
- if( dither )
- {
- const char* regPath = "Software\Microsoft\Windows\CurrentVersion\App Paths\PopFax.exe";
- HKEY hKey = HKEY_LOCAL_MACHINE;
- char ditherApp[4*MAX_PATH] = "";
- DWORD pathSize = 4*MAX_PATH;
- write_log( 3, "pcl2bmp", "dither");
- if( ERROR_SUCCESS == RegOpenKeyExA( hKey, regPath, 0, KEY_READ, &hKey ) )
- {
- RegQueryValueExA( hKey, NULL, NULL, NULL, (LPBYTE)ditherApp, &pathSize );
- RegCloseKey( hKey );
- }
- if( strlen(ditherApp) )
- {
- char* buffStr = strrchr( ditherApp, '\' );
- if( NULL != buffStr )
- {
- STARTUPINFOA si;
- PROCESS_INFORMATION pi;
- std::string names;
- strcpy( buffStr+1, "dither.exe" );
- names = ditherApp;
- names += " /infile "";
- names += PageNameBmp;
- names += """;
- names += " /outfile "";
- names += PageNameBmp;
- names += "dither";
- names += """;//invert invert
- ZeroMemory( &si, sizeof(si) );
- si.cb = sizeof(si);
- ZeroMemory( &pi, sizeof(pi) );
- write_log( 3, "pcl2bmp", "%s", names.c_str());
- if( CreateProcessA( NULL, (char*)names.c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ) )
- {
- // Wait until child process exits.
- WaitForSingleObject( pi.hProcess, INFINITE );
- // Close process and thread handles.
- CloseHandle( pi.hProcess );
- CloseHandle( pi.hThread );
- }
- remove( PageNameBmp );
- names = PageNameBmp;
- names += "dither";
- //MoveFileA( names.c_str(), PageNameBmp );
- CopyFileA( names.c_str(), PageNameBmp, FALSE );
- }
- }
- }
- //else
- convert_bitmap_to_monochrome( PageNameBmp, PageNameBmp, TRUE );
- }
- start_toTiff( PageNameBmp, PageNameBmp, img_format_ext);
- }
- if (img_format==2)
- start_toPNG(PageNameBmp,PageNameBmp);
- if (img_format==3){
- std::string namebmp;
- namebmp=PageNameBmp;
- namebmp +=".bmp";
- rename(PageNameBmp,(const char *)namebmp.c_str());
- }//else remove(PageNameBmp);
- }
- i_row = 0;
- PageNo++;
- memset( bits, 0xff, bmp_height*bmp_width*3 );
- }
- }
- }
- }///end case 0x1B
- }///end switch( ch )
- ch = ifPcl.get();
- } //while
- if( (COMPRESSION_CCITTFAX4 == img_format_ext)||(COMPRESSION_CCITTFAX3 == img_format_ext) )
- {
- STARTUPINFOA si;
- PROCESS_INFORMATION pi;
- std::string names;
- char nstr[100] = "";
- if( COMPRESSION_CCITTFAX4 == img_format_ext )
- names = "tiffcp.exe -c g4";
- else
- names = "tiffcp.exe -c g3:2d";
- for( int i = oldPageNo; i <= PageNo; i++)
- {
- names += " "";
- names += NameBmp;
- sprintf( nstr, "%d", i );
- names += nstr;
- names += ".tif"";
- }
- names += " "";
- names += NameBmp;
- names += "_res_";
- sprintf( nstr, "%d", oldPageNo );
- names += nstr;
- names += "_";
- sprintf( nstr, "%d", PageNo - oldPageNo );
- names += nstr;
- names += ".tif"";
- ZeroMemory( &si, sizeof(si) );
- si.cb = sizeof(si);
- ZeroMemory( &pi, sizeof(pi) );
- //ShellExecuteA( NULL, "open", "tiffcp.exe", names.c_str(), NULL, SW_HIDE);
- if( CreateProcessA( NULL, (char*)names.c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ) )
- {
- // Wait until child process exits.
- WaitForSingleObject( pi.hProcess, INFINITE );
- // Close process and thread handles.
- CloseHandle( pi.hProcess );
- CloseHandle( pi.hThread );
- }
- /* for( int i = oldPageNo; i <= PageNo; i++)
- {
- names = NameBmp;
- sprintf( nstr, "%d", i );
- names += nstr;
- names += ".tif";
- DeleteFileA( names.c_str() );
- }*/
- const char* regPath = "Software\Microsoft\Windows\CurrentVersion\App Paths\PopFax.exe";
- HKEY hKey = HKEY_LOCAL_MACHINE;
- char popFaxPath[4*MAX_PATH] = "";
- DWORD pathSize = 4*MAX_PATH;
- if( ERROR_SUCCESS == RegOpenKeyExA( hKey, regPath, 0, KEY_READ, &hKey ) )
- {
- RegQueryValueExA( hKey, NULL, NULL, NULL, (LPBYTE)popFaxPath, &pathSize );
- RegCloseKey( hKey );
- }
- if( strlen(popFaxPath) )
- {
- names = """;
- names += NameBmp;
- names += "_res_";
- sprintf( nstr, "%d", oldPageNo );
- names += nstr;
- names += "_";
- sprintf( nstr, "%d", PageNo - oldPageNo );
- names += nstr;
- names += ".tif"";
- ShellExecuteA( NULL, "open", popFaxPath, names.c_str(), NULL, SW_SHOW);
- }
- }
- oldPageNo = PageNo;
- if( bits )
- delete [] bits;
- return 0;
- }
- //BOOL convert_bitmap_to_monochrome( const char* inBmp, const char* outBmp, BOOL dither, BOOL invert )
- //{
- // BOOL ret = FALSE;
- // HBITMAP hBmp = NULL;
- //
- // if( NULL != (hBmp = (HBITMAP)LoadImageA( NULL, inBmp, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE|LR_LOADFROMFILE|LR_MONOCHROME) ) )
- // {
- // BITMAP bm;
- // HDC hdcSrc = CreateCompatibleDC( NULL );
- // HDC hdcDest = CreateCompatibleDC( hdcSrc );
- // SelectObject( hdcSrc, hBmp );
- // GetObject( hBmp, sizeof(BITMAP), &bm );
- //
- // HBITMAP hBmpDest = CreateBitmap( FAX_WIDTH, FAX_HEIGTH, 1, 1, NULL);
- // SelectObject( hdcDest, hBmpDest );
- // StretchBlt( hdcDest, 0, 0, FAX_WIDTH, FAX_HEIGTH, hdcSrc, 0, 0, bm.bmWidth, bm.bmHeight, NOTSRCCOPY);
- // DeleteObject( hBmp );
- //
- // ret = save_bitmap( outBmp, hBmpDest );
- //
- // DeleteObject( hBmpDest );
- // DeleteDC( hdcSrc );
- // DeleteDC( hdcDest );
- //
- // }
- // return ret;
- //}
- BOOL convert_bitmap_to_monochrome( const char* inBmp, const char* outBmp, BOOL invert )
- {
- BOOL ret = FALSE;
- HBITMAP hBmp = NULL;
- BITMAP bm;
- HDC hdcSrc = NULL;
- HDC hdcDest = NULL;
- HBITMAP hBmpDest = NULL;
- hBmp = (HBITMAP)LoadImageA( NULL, inBmp, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE|LR_LOADFROMFILE|LR_MONOCHROME);
- hdcSrc = CreateCompatibleDC( NULL );
- hdcDest = CreateCompatibleDC( hdcSrc );
- SelectObject( hdcSrc, hBmp );
- GetObject( hBmp, sizeof(BITMAP), &bm );
- hBmpDest = CreateBitmap( FAX_WIDTH, FAX_HEIGTH, 1, 1, NULL);
- SelectObject( hdcDest, hBmpDest );
-
- StretchBlt( hdcDest, 0, 0, FAX_WIDTH, FAX_HEIGTH, hdcSrc, 0, 0, bm.bmWidth, bm.bmHeight, invert?NOTSRCCOPY:SRCCOPY);
- ret = save_bitmap( outBmp, (invert)?hBmpDest:hBmp );
- (NULL!=hBmpDest)?DeleteObject( hBmpDest ):0;
- (NULL!=hBmp)?DeleteObject( hBmp ):0;
- (NULL!=hdcSrc)?DeleteDC( hdcSrc ):0;
- (NULL!=hdcDest)?DeleteDC( hdcDest ):0;
- return ret;
- }
- BOOL save_bitmap( const char* name, HBITMAP bitmap)
- {
- HDC hdcScreen = NULL; // Device context of the screen
- HWND hwndScreen = NULL; // Desktop window handle
- int i = 0; // Counts the number of scanlines
- BYTE *bits = NULL; // Pointer to the bitmap's bits
- BITMAPINFOHEADER *info; // Info structure about this bitmap
- HANDLE fp = NULL; // A file handle
- BITMAPFILEHEADER finfo; // File info header
- long ctsize = 0; //
- DWORD dw = 0; // Doubleword return code
- char *ptr = NULL; // Pointer used to write the opening BM
-
- hwndScreen = GetDesktopWindow();
- hdcScreen = GetDC( hwndScreen );
- info = (BITMAPINFOHEADER*)GlobalAlloc(GMEM_FIXED,sizeof(BITMAPFILEHEADER)+(1024*sizeof(RGBQUAD)));
- info->biSize = sizeof(BITMAPINFOHEADER);
- info->biBitCount = 0;
- i = GetDIBits( hdcScreen, // handle of device context
- bitmap, // handle of bitmap
- 0, // first scan line to set in destination bitmap
- 0, // number of scan lines to copy
- NULL, // address of array for bitmap bits
- (BITMAPINFO*)info, // address of structure with bitmap data
- DIB_RGB_COLORS // RGB or palette index
- );
- switch( info->biBitCount )
- {
- case 1:ctsize=2;break;
- case 4:ctsize=16;break;
- case 8:ctsize=256;break;
- case 16:ctsize=0;break;
- case 24:ctsize=0;break;
- case 32:ctsize=0;break;
- default:
- ReleaseDC( hwndScreen, hdcScreen );
- return FALSE;
- }
- bits = (unsigned char*)GlobalAlloc( GMEM_FIXED, info->biSizeImage);
- i = GetDIBits( hdcScreen, // handle of device context
- bitmap, // handle of bitmap
- 0, // first scan line to set in destination bitmap
- info->biHeight, // number of scan lines to copy
- bits, // address of array for bitmap bits
- (BITMAPINFO*)info, // address of structure with bitmap data
- DIB_RGB_COLORS // RGB or palette index
- );
- ptr = (char *)&finfo.bfType;
- ptr[0]='B';
- ptr[1]='M';
- finfo.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+(sizeof(RGBQUAD)*ctsize);
- finfo.bfSize = finfo.bfOffBits+info->biSizeImage/*imgsize*/;
- finfo.bfReserved1 = 0;
- finfo.bfReserved2 = 0;
- fp = CreateFileA(name,// pointer to name of the file
- GENERIC_WRITE, // access (read-write) mode
- 0, // share mode
- NULL, // pointer to security attributes
- CREATE_ALWAYS,// how to create
- FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,// file attributes
- NULL // handle to file with attributes to copy
- );
- WriteFile( fp, &finfo, sizeof(BITMAPFILEHEADER), &dw, NULL);
- WriteFile( fp, info, sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*ctsize, &dw, NULL);
- WriteFile( fp, bits, info->biSizeImage/*imgsize*/, &dw, NULL);
- CloseHandle( fp );
- GlobalFree( info );
- GlobalFree( bits );
- ReleaseDC( hwndScreen,hdcScreen );
- return TRUE;
- }
- IfStream::IfStream( const char* fileName )
- {
- hFile = CreateFileA( fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
- write_log( 3, "IfStream::IfStream", "%d %s", hFile, fileName );
- }
- int IfStream::get()
- {
- char ch = EOF;
- int ret = EOF;
- DWORD dwReaded = 0;
- if( INVALID_HANDLE_VALUE != hFile )
- {
- ret = 0;
- ReadFile( hFile, &ch, sizeof(ch), &dwReaded, 0);
- memcpy( &ret, &ch, sizeof(ch) );
- if( 0 == dwReaded )
- ret = EOF;
- }
- return ret;
- }
- BOOL IfStream::read( char* buff, int len )
- {
- BOOL ret = FALSE;
- DWORD dwReaded = 0;
- if( INVALID_HANDLE_VALUE != hFile )
- {
- ret = ReadFile( hFile, buff, len, &dwReaded, 0);
- if( 0 == dwReaded )
- ret = FALSE;
- }
- return ret;
- }
- IfStream::~IfStream()
- {
- if( INVALID_HANDLE_VALUE != hFile )
- CloseHandle( hFile );
- }