//////////////////////////////////////////////////////////////
//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
评论3
最新资源