⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 compress.c

📁 图像处理入门书
💻 C
📖 第 1 页 / 共 3 页
字号:
//////////////////////////////////////////////////////////////

//Name:compress.c

//Purpose: Run length algorithm and Jpeg decoding

//Author: phoenix, CS, TshingHua, Beijing, P.R.C.

//Email: bjlufengjun@www.163.net or lufengjun@hotmail.com

//Date:April 3, 1998



//header file

#include "bmp.h"

#include "jpeg.h"

#include "memory.h"

#include "math.h"

#include "stdio.h"

//macro definition

#define WIDTHBYTES(i)    ((i+31)/32*4)

#define PI 3.1415926535

//define return value of function

#define FUNC_OK 0

#define FUNC_MEMORY_ERROR 1

#define FUNC_FILE_ERROR 2

#define FUNC_FORMAT_ERROR 3



//function declaration

int PASCAL WinMain (HANDLE, HANDLE, LPSTR, int);

LRESULT CALLBACK MainWndProc(HWND , UINT,WPARAM, LPARAM);

BOOL LoadPcxFile(HWND hWnd,char *BmpFileName);

void ReadPcxLine(unsigned char *p,FILE *fp);

//////////////////////////////////////////////////

//Jpeg functions

BOOL LoadJpegFile(HWND hWnd,char *BmpFileName);

void showerror(int funcret);

int  InitTag();

void InitTable();

int  Decode();

int  DecodeMCUBlock();

int  HufBlock(BYTE dchufindex,BYTE achufindex);

int  DecodeElement();

void IQtIZzMCUComponent(short flag);

void IQtIZzBlock(short  *s ,int * d,short flag);

void GetYUV(short flag);

void StoreBuffer();

BYTE ReadByte();

void Initialize_Fast_IDCT();

void Fast_IDCT(int * block);

void idctrow(int * blk);

void idctcol(int * blk);

//////////////////////////////////////////////////

//global variable declaration

BITMAPFILEHEADER   bf;

BITMAPINFOHEADER   bi;

HPALETTE           hPalette=NULL;

HBITMAP            hBitmap=NULL;

HGLOBAL            hImgData=NULL;

DWORD              NumColors;

DWORD              LineBytes;

DWORD              ImgWidth=0 , ImgHeight=0;

unsigned int       PcxBytesPerLine;

LPSTR              lpPtr;

//////////////////////////////////////////////////

//variables used in jpeg function

short			SampRate_Y_H,SampRate_Y_V;

short			SampRate_U_H,SampRate_U_V;

short			SampRate_V_H,SampRate_V_V;

short			H_YtoU,V_YtoU,H_YtoV,V_YtoV;

short			Y_in_MCU,U_in_MCU,V_in_MCU;

unsigned char   *lpJpegBuf;

unsigned char   *lp;

short			qt_table[3][64];

short			comp_num;

BYTE			comp_index[3];

BYTE		    YDcIndex,YAcIndex,UVDcIndex,UVAcIndex;

BYTE			HufTabIndex;

short		    *YQtTable,*UQtTable,*VQtTable;

BYTE			And[9]={0,1,3,7,0xf,0x1f,0x3f,0x7f,0xff};

short		    code_pos_table[4][16],code_len_table[4][16];

unsigned short	code_value_table[4][256];

unsigned short	huf_max_value[4][16],huf_min_value[4][16];

short			BitPos,CurByte;

short			rrun,vvalue;

short			MCUBuffer[10*64];

int				QtZzMCUBuffer[10*64];

short			BlockBuffer[64];

short			ycoef,ucoef,vcoef;

BOOL			IntervalFlag;

short			interval=0;

int				Y[4*64],U[4*64],V[4*64];

DWORD		    sizei,sizej;

short 			restart;

static  long	iclip[1024];

static  long	*iclp;

///////////////////////////////////////////////////////////

int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,

		    LPSTR lpszCmdLine, int nCmdShow)

{

	MSG       msg;

	WNDCLASS  wndclass;

	HWND      hWnd;



	if ( ! hPrevInstance ){

		wndclass.style = CS_HREDRAW | CS_VREDRAW;

		wndclass.lpfnWndProc = MainWndProc;

		wndclass.cbClsExtra = 0;

		wndclass.cbWndExtra = 0;

		wndclass.hInstance = hInstance;

		wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);

		wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);

		wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);

		wndclass.lpszMenuName =  "BMPMENU";

		wndclass.lpszClassName = "phoenix ip system";

    }



    if ( ! RegisterClass (&wndclass) )

	    return FALSE;



	hWnd = CreateWindow ("phoenix ip system","Run length algorithm and Jpeg decoding",

						 WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,

						 CW_USEDEFAULT,	CW_USEDEFAULT, NULL,NULL,

						 hInstance,	NULL);

	if (!hWnd)

		return FALSE;

	ShowWindow (hWnd, SW_SHOWMAXIMIZED);

	UpdateWindow (hWnd);

	

	while ( GetMessage (&msg, NULL, 0, 0) ){

		TranslateMessage (&msg);

		DispatchMessage (&msg);

    }

	return msg.wParam;

}

////////////////////////////////////////////////////////////////

LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam)

{

    static  HDC    hDC,hMemDC;

    PAINTSTRUCT    ps;



    switch (message){

    case WM_PAINT:

	{         

		hDC = BeginPaint(hWnd, &ps);

		if (hBitmap)

		{ 

			hMemDC = CreateCompatibleDC(hDC);

			if (hPalette)

			{           

				SelectPalette (hDC, hPalette, FALSE); 

				SelectPalette (hMemDC, hPalette, FALSE);

				RealizePalette (hDC);

			}   

			SelectObject(hMemDC, hBitmap); 

			BitBlt(hDC, 0, 0, ImgWidth,ImgHeight, hMemDC, 0, 0, SRCCOPY);

			DeleteDC(hMemDC);

		}

		EndPaint(hWnd, &ps);

		break;

	}

	case WM_DESTROY: //注意释放内存和位图,调色板句柄

        if(hBitmap!=NULL)

	        DeleteObject(hBitmap);

	    

		if(hPalette!=NULL)                     

	        DeleteObject(hPalette);

		

		if(hImgData!=NULL){

			GlobalUnlock(hImgData);

			GlobalFree(hImgData);

		}	        

	    PostQuitMessage (0);

	    return 0;

	case WM_COMMAND:

	    switch (wParam){

		case IDM_RUNLENGTH:

			//注意重新分配内存和调色板,位图句柄时,先释放原来的

	        if(hBitmap!=NULL){     

				DeleteObject(hBitmap);

	            hBitmap=NULL;

	        }

	        if(hPalette!=NULL){                     

				DeleteObject(hPalette);

	            hPalette=NULL;

	        }

			if(hImgData!=NULL){

				GlobalUnlock(hImgData);

				GlobalFree(hImgData);  

				hImgData=NULL;

			}	        

			if(LoadPcxFile(hWnd,"c:\\test.pcx")) //成功,则重画窗口

                InvalidateRect(hWnd,NULL,TRUE);

	        break;

		case IDM_JPEG:

			//注意重新分配内存和调色板,位图句柄时,先释放原来的

	        if(hBitmap!=NULL){     

				DeleteObject(hBitmap);

	            hBitmap=NULL;

	        }

	        if(hPalette!=NULL){                     

				DeleteObject(hPalette);

	            hPalette=NULL;

	        }

			if(hImgData!=NULL){

				GlobalUnlock(hImgData);

				GlobalFree(hImgData);  

				hImgData=NULL;

			}	        

			if(LoadJpegFile(hWnd,"c:\\test.jpg")) //成功,则重画窗口

                InvalidateRect(hWnd,NULL,TRUE);

	        break;

		case IDM_EXIT:

	        SendMessage(hWnd,WM_DESTROY,0,0L);

	        break;

	    }

	break;                

    }

    return DefWindowProc (hWnd, message, wParam, lParam);

}

////////////////////////////////////////////////////////////////

BOOL LoadPcxFile (HWND hWnd,char *PcxFileName)

{   

//pcx header

typedef struct{

		 char manufacturer;

		 char version;

		 char encoding;

		 char bits_per_pixel;

		 WORD xmin,ymin;

		 WORD xmax,ymax;

		 WORD hres;

		 WORD vres;

		 char palette[48];

		 char reserved;

		 char colour_planes;

		 WORD bytes_per_line;

		 WORD palette_type;

		 char filler[58];

	      } PCXHEAD;

	FILE               *PCXfp;

	PCXHEAD			   header;

    LOGPALETTE         *pPal;

    HPALETTE           hPrevPalette; 

    HDC                hDc;

	HLOCAL             hPal;

	DWORD 		       ImgSize;

	DWORD              BufSize;

	LPBITMAPINFOHEADER lpImgData;	

	DWORD              i;

	LONG               x,y;

	int                PcxTag;

	unsigned char      LineBuffer[6400];

	LPSTR              lpPtr;

	HFILE              hfbmp;



    if((PCXfp=fopen(PcxFileName,"rb"))==NULL){

        MessageBox(hWnd,"File c:\\test.pcx not found!","Error Message",MB_OK|MB_ICONEXCLAMATION);

        return FALSE;

	}

    fread((char*)&header,1,sizeof(PCXHEAD),PCXfp);

    if(header.manufacturer!=0x0a){

	    MessageBox(hWnd,"Not a valid Pcx file!","Error Message",MB_OK|MB_ICONEXCLAMATION);

	    fclose(PCXfp);

	    return FALSE;

	}

	fseek(PCXfp,-769L,SEEK_END);

	PcxTag=fgetc(PCXfp)&0xff;

	if(PcxTag!=12){

	    MessageBox(hWnd,"Not a 256 colors Pcx file!","Error Message",MB_OK|MB_ICONEXCLAMATION);

		fclose(PCXfp);

		return FALSE;

	}

	//create new bitmapfileheader and bitmapinfoheader

	memset((char *)&bf,0,sizeof(BITMAPFILEHEADER));	

	memset((char *)&bi,0,sizeof(BITMAPINFOHEADER));



	bi.biSize=sizeof(BITMAPINFOHEADER);

	bi.biWidth=header.xmax-header.xmin+1;

	bi.biHeight=header.ymax-header.ymin+1;

	bi.biPlanes=1;

	bi.biBitCount=8;

	bi.biCompression=BI_RGB;

	ImgWidth=bi.biWidth;

	ImgHeight=bi.biHeight;

	NumColors=256;

	LineBytes=(DWORD)WIDTHBYTES(bi.biWidth*bi.biBitCount);

	ImgSize=(DWORD)LineBytes*bi.biHeight;



	bf.bfType=0x4d42;

	bf.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+NumColors*sizeof(RGBQUAD)+ImgSize;

	bf.bfOffBits=(DWORD)(NumColors*sizeof(RGBQUAD)+sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER));

	if((hImgData=GlobalAlloc(GHND,(DWORD)(sizeof(BITMAPINFOHEADER)+

						     NumColors*sizeof(RGBQUAD)+ImgSize)))==NULL)

	{

    	MessageBox(hWnd,"Error alloc memory!","ErrorMessage",MB_OK|

                   MB_ICONEXCLAMATION);

	    fclose(PCXfp);

		return FALSE;

	}

	lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData); 

	memcpy(lpImgData,(char *)&bi,sizeof(BITMAPINFOHEADER));

	lpPtr=(char *)lpImgData+sizeof(BITMAPINFOHEADER);



    hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));

    pPal =(LOGPALETTE *)LocalLock(hPal);

    pPal->palNumEntries =256;

	pPal->palVersion    = 0x300;

	for (i = 0; i < 256; i++) {

   		pPal->palPalEntry[i].peRed=(BYTE)fgetc(PCXfp);

		pPal->palPalEntry[i].peGreen=(BYTE)fgetc(PCXfp);

		pPal->palPalEntry[i].peBlue=(BYTE)fgetc(PCXfp);

		pPal->palPalEntry[i].peFlags=(BYTE)0;

		*(lpPtr++)=(unsigned char)pPal->palPalEntry[i].peBlue;

		*(lpPtr++)=(unsigned char)pPal->palPalEntry[i].peGreen;

		*(lpPtr++)=(unsigned char)pPal->palPalEntry[i].peRed;

		*(lpPtr++)=0;

	}

	hPalette=CreatePalette(pPal);

	LocalUnlock(hPal);

	LocalFree(hPal);



	hDc=GetDC(hWnd);

	if(hPalette){

        hPrevPalette=SelectPalette(hDc,hPalette,FALSE);

		RealizePalette(hDc);

	}

	PcxBytesPerLine=(unsigned int)header.bytes_per_line;

	fseek(PCXfp,(LONG)sizeof(PCXHEAD),SEEK_SET);

	BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);



	for(y=0;y<bi.biHeight;y++){

		lpPtr=(char *)lpImgData+BufSize-LineBytes-y*LineBytes;

		ReadPcxLine(LineBuffer,PCXfp);

		for(x=0;x<bi.biWidth;x++)

			*(lpPtr++)=LineBuffer[x];

	}



	hBitmap=CreateDIBitmap(hDc,	(LPBITMAPINFOHEADER)lpImgData, (LONG)CBM_INIT,

				(LPSTR)lpImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),

   				(LPBITMAPINFO)lpImgData, DIB_RGB_COLORS);



	if(hPalette && hPrevPalette){

		SelectPalette(hDc,hPrevPalette,FALSE);

		RealizePalette(hDc);

	}



	hfbmp=_lcreat("c:\\pcx2bmp.bmp",0);



	_lwrite(hfbmp,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 

	_lwrite(hfbmp,(LPSTR)lpImgData,BufSize);

	_lclose(hfbmp);



	fclose(PCXfp); 

	ReleaseDC(hWnd,hDc);

	GlobalUnlock(hImgData);

	return TRUE; 

}

////////////////////////////////////////////////////////////////

void ReadPcxLine(unsigned char *p,FILE *fp) //decode one line of pcx file

{

	unsigned int n=0,i;

	char c;



	memset(p,0,PcxBytesPerLine);

	do{

		//get a key byte

		c=fgetc(fp)&0xff;

		//if it's a run of BytePerLine field

		if((c&0xc0)==0xc0){

			//mask off the high bits

			i=c&0x3f;

			//get the run byte

			c=fgetc(fp);

			//run the byte  

			while(i--) p[n++]=c;

		}

		else p[n++]=c;

	}while (n<PcxBytesPerLine);

}

////////////////////////////////////////////////////////////////

BOOL LoadJpegFile (HWND hWnd,char *JpegFileName)

{

	HFILE			   hfjpg;

    HDC                hDc;

	DWORD 		       ImgSize;

	DWORD              BufSize,JpegBufSize;

	HFILE              hfbmp;

	HGLOBAL			   hJpegBuf;

	int				   funcret;

	LPBITMAPINFOHEADER lpImgData;



	if((hfjpg=_lopen(JpegFileName,OF_READ))==HFILE_ERROR){

		showerror(FUNC_FILE_ERROR);

		return FALSE;

	}

	//get jpg file length

	JpegBufSize=_llseek(hfjpg,0L,SEEK_END);

	//rewind to the beginning of the file

	_llseek(hfjpg,0L,SEEK_SET);



	if((hJpegBuf=GlobalAlloc(GHND,JpegBufSize))==NULL){

		_lclose(hfjpg);

		showerror(FUNC_MEMORY_ERROR);

		return FALSE;

	}

	lpJpegBuf=(unsigned char  *)GlobalLock(hJpegBuf);

	_hread(hfjpg,(unsigned char  *)lpJpegBuf,JpegBufSize);

	_lclose(hfjpg);

	

	InitTable();



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -