18.06.07 rpcl.cpp
上传用户:looem2003
上传日期:2014-07-20
资源大小:13733k
文件大小:13k
- /*
- Copyright (C) 2007 Ibadov Tariel <itk@bk.ru>
- */
- #include "rpcl.h"
- #define FAX_WIDTH 1728
- #define FAX_HEIGTH 2292
- #define RPCL_WIDTH 800
- #define RPCL_HEIGTH 1100
- //static unsigned char bits[1100][2400];
- static unsigned char bits[RPCL_HEIGTH][RPCL_WIDTH*3];
- 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;
- void set_bit()
- {
- for (int i_i =0;i_i<=RPCL_HEIGTH-1;i_i++){
- for (int i_i1 =0;i_i1<=(RPCL_WIDTH*3)-1;i_i1++){
- bits[i_i][i_i1]=0x0ff;
- }
- }
- }
- int start_convert (char *NamePcl, char *NameBmp ,int img_format, int img_format_ext, char* extApp)
- {
- i_count=0;
- bmpinfo.bits = (unsigned char *)bits;
- //bmpinfo.width = 800;
- //bmpinfo.height = 1100;
- bmpinfo.width = RPCL_WIDTH;
- bmpinfo.height = RPCL_HEIGTH;
- bmpinfo.colours = 24;
- bmpinfo.palette = NULL;
- memset( bits, 0, sizeof(bits) );
- pcl2bmp(NamePcl,NameBmp,img_format, img_format_ext, extApp);
- return i_count;
- }
- int pcl2bmp( char *NamePcl, char *NameBmp,int img_format, int img_format_ext, char* extApp)
- {
- set_bit();
- ifstream ifPcl( NamePcl, ios::in | ios::binary );
- ifPcl.setf( ios::hex, ios::dec );
- int ch;
- ch = ifPcl.get();
- 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());
- }
- ///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 /6;
- b_row=true;
- i_h++;
- }
- if (ch==0x58){
- i_width=atoi((char *)s_size.c_str());
- }
- if(s_size=="PORTRAITx0"){
- bmpinfo.height = /*1100*/RPCL_HEIGTH;
- }
- 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){
- if (b_row) b_row=false; else i_row++;
- int i_tmp=0;
- std::string s_size;
- while ( (i_tmp<4 )&&(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;
- char pdata[8000];
- ch = ifPcl.get();
- ifPcl.read( pdata,i_size);
- for ( qq=0; qq < i_size; qq++) {
- if (img_format!=4){
- if (q==3){
- q=bits[i_row][i-3];
- bits[i_row][i-3]=bits[i_row][i-1];
- bits[i_row][i-1]=q;
- q=0;
- } q++;
- }
- if ((i_row<RPCL_HEIGTH/*1100*/)&&(i<(3*RPCL_WIDTH)))
- bits[i_row][i++] =pdata[qq];
- }
- }
- } // 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++;
- 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)
- {
- if( (COMPRESSION_CCITTFAX4 == img_format_ext)||(COMPRESSION_CCITTFAX3 == img_format_ext) )
- convert_bitmap_to_monochrome( PageNameBmp, PageNameBmp );
- 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);
- }
- memset( bits, 0, sizeof(bits) );
- i_row=0; PageNo++;
- set_bit();
- }
- }
- }
- }///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 = 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(bmpinfo.bits) free(bmpinfo.bits);
- return 0;
- }
- BOOL convert_bitmap_to_monochrome( const char* inBmp, const char* outBmp )
- {
- 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 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; //
- long imgsize = 0; // Various sizes needed to build the file
- long pixels = 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
- );
- //if( (info->biBitCount!=4) && (info->biBitCount!=8) && (info->biBitCount!=24) )
- // info->biBitCount = 24;
- info->biBitCount = 1;
- info->biCompression = BI_RGB;
- pixels = info->biWidth*info->biHeight;
- switch( info->biBitCount )
- {
- case 1:ctsize=2;imgsize=pixels/8;break;
- case 4:ctsize=16;imgsize=pixels/2;break;
- case 8:ctsize=256;imgsize=pixels;break;
- case 16:ctsize=0;imgsize=pixels*2;break;
- case 24:ctsize=0;imgsize=pixels*3;break;
- case 32:ctsize=0;imgsize=pixels*4;break;
- default:
- ReleaseDC( hwndScreen, hdcScreen );
- return FALSE;
- }
- bits = (unsigned char*)GlobalAlloc(GMEM_FIXED,imgsize);
- 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+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, imgsize, &dw, NULL);
- CloseHandle( fp );
- GlobalFree( info );
- GlobalFree( bits );
- ReleaseDC( hwndScreen,hdcScreen );
- return TRUE;
- }