Bmp.c
上传用户:s6549606
上传日期:2015-11-11
资源大小:12002k
文件大小:152k
- //////////////////////////////////////////////////////////////
- //Name:bmp.c
- //Purpose: phoenix's image processing system
- //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"
- //define return value of function
- #define FUNC_OK 0
- #define FUNC_MEMORY_ERROR 1
- #define FUNC_FILE_ERROR 2
- #define FUNC_FORMAT_ERROR 3
- //owner defined stack
- typedef struct{
- HGLOBAL hMem;
- POINT *lpMyStack;
- LONG ElementsNum;
- LONG ptr;
- }MYSTACK;
- //macro definition
- #define WIDTHBYTES(i) ((i+31)/32*4)
- #define PI 3.1415926535
- #define RADIAN(angle) ((angle)*PI/180.0) //convert angle to radian
- //function declaration
- int PASCAL WinMain (HANDLE, HANDLE, LPSTR, int);
- LRESULT CALLBACK MainWndProc(HWND , UINT,WPARAM, LPARAM);
- BOOL LoadBmpFile (HWND hWnd,char *BmpFileName);
- BOOL CALLBACK InputBox( HWND hDlg, UINT message,WPARAM wParam, LPARAM lParam );
- BOOL Translation(HWND hWnd);
- BOOL Rotation(HWND hWnd);
- BOOL Mirror(HWND hWnd,BOOL XDirection);
- BOOL Transpose(HWND hWnd);
- BOOL Zoom(HWND hWnd);
- BOOL LimbPatternM3(HWND hWnd);
- BOOL LimbPatternM4(HWND hWnd);
- BOOL Steinberg(HWND hWnd);
- BOOL Bmp2Txt(HWND hWnd);
- BOOL Histogram(HWND hWnd);
- long FAR PASCAL PopupWndProc (HWND,UINT,WPARAM,LPARAM);
- BOOL ColortoGrayScale(HWND hWnd);
- BOOL Trueto256(HWND hWnd);
- BOOL Invert(HWND hWnd);
- BOOL TemplateOperation(HWND hWnd, int TemplateType);
- BOOL ContrastStretch(HWND hWnd);
- BOOL Cliping(HWND hWnd);
- BOOL Thresholding(HWND hWnd);
- BOOL Slice(HWND hWnd,BOOL WithBack);
- BOOL HistogramEqua(HWND hWnd);
- BOOL Dilation(HWND hWnd,BOOL Hori);
- BOOL Erosion(HWND hWnd,BOOL Hori);
- BOOL MorphOpen(HWND hWnd,BOOL Hori);
- BOOL MorphClose(HWND hWnd,BOOL Hori);
- BOOL Thinning(HWND hWnd);
- BOOL Outline(HWND hWnd);
- BOOL Hough(HWND hWnd);
- BOOL MedianFilter(HWND hWnd,BOOL Hori);
- BOOL LapOfGauss(HWND hWnd);
- BOOL Contour(HWND hWnd);
- BOOL IsContourP(LONG x,LONG y, char *lpPtr);
- BOOL SeedFill(HWND hWnd);
- BOOL InitStack(HWND hWnd,LONG StackLen);
- void DeInitStack();
- BOOL MyPush(POINT p);
- POINT MyPop();
- BOOL IsStackEmpty();
- BOOL Projection(HWND hWnd,BOOL Hori);
- BOOL Subtraction(HWND hWnd);
- 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;
- HINSTANCE ghInst;
- int xOffset=0,yOffset=0;
- float RotateAngle=0.0f;
- float ZoomRatio=0.25f;
- DWORD ImgWidth=0 , ImgHeight=0;
- //template array
- float Template_Smooth_Box[9]={1.0f,1.0f,1.0f,1.0f,1.0f,1.0f,1.0f,1.0f,1.0f};
- float Template_Smooth_Gauss[9]={1.0f,2.0f,1.0f,2.0f,4.0f,2.0f,1.0f,2.0f,1.0f};
- float Template_Sharpen_Laplacian[9]={-1.0f,-1.0f,-1.0f,-1.0f,9.0f,-1.0f,-1.0f,-1.0f,-1.0f};
- float Template_HSobel[9]={-1.0f,0.0f,1.0f,-2.0f,0.0f,2.0f,-1.0f,0.0f,1.0f};
- float Template_VSobel[9]={-1.0f,-2.0f,-1.0f,0.0f,0.0f,0.0f,1.0f,2.0f,1.0f};
- float Template_HIsoSobel[9]={-1.0f,0.0f,1.0f,-1.4142f,0.0f,1.4142f,-1.0f,0.0f,1.0f};
- float Template_VIsoSobel[9]={-1.0f,-1.4142f,-1.0f,0.0f,0.0f,0.0f,1.0f,1.4142f,1.0f};
- float Template_Log[25]={-2.0f,-4.0f,-4.0f,-4.0f,-2.0f,
- -4.0f,0.0f,8.0f,0.0f,-4.0f,
- -4.0f,8.0f,24.0f,8.0f,-4.0f,
- -4.0f,0.0f,8.0f,0.0f,-4.0f,
- -2.0f,-4.0f,-4.0f,-4.0f,-2.0f};
- BYTE BayerPattern[8][8]={0,32,8,40,2,34,10,42,
- 48,16,56,24,50,18,58,26,
- 12,44,4,36,14,46,6,38,
- 60,28,52,20,62,30,54,22,
- 3,35,11,43,1,33,9,41,
- 51,19,59,27,49,17,57,25,
- 15,47,7,39,13,45,5,37,
- 63,31,55,23,61,29,53,21};
- static char ch[95]= {
- ' ',
- '`','1','2','3','4','5','6','7','8','9','0','-','=','\',
- 'q','w','e','r','t','y','u','i','o','p','[',']',
- 'a','s','d','f','g','h','j','k','l',';',''',
- 'z','x','c','v','b','n','m',',','.','/',
- '~','!','@','#','$','%','^','&','*','(',')','_','+','|',
- 'Q','W','E','R','T','Y','U','I','O','P','{','}',
- 'A','S','D','F','G','H','J','K','L',':','"',
- 'Z','X','C','V','B','N','M','<','>','?'
- };
- static int gr[95]= {
- 0,
- 7,22,28,31,31,27,32,22,38,32,40, 6,12,20,38,32,26,20,24,40,
- 29,24,28,38,32,32,26,22,34,24,44,33,32,32,24,16, 6,22,26,22,
- 26,34,29,35,10, 6,20,14,22,47,42,34,40,10,35,21,22,22,16,14,
- 26,40,39,29,38,22,28,36,22,36,30,22,22,36,26,36,25,34,38,24,
- 36,22,12,12,26,30,30,34,39,42,41,18,18,22
- };
- int GrayTable[256];
- int MaxGrayNum;
- int MinGrayNum;
- int FirstPoint=0, SecondPoint=255;
- float StretchRatio=2.0f;
- int EquaScale;
- MYSTACK SeedFillStack;
- POINT SeedPoint;
- BOOL IsSelecting;
- 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;
- ghInst=hInstance;
- 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;
- wndclass.style = CS_HREDRAW | CS_VREDRAW;
- wndclass.lpfnWndProc = PopupWndProc;
- wndclass.cbClsExtra = 0;
- wndclass.cbWndExtra = 0;
- wndclass.hInstance = ghInst;
- wndclass.hIcon = NULL;
- wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
- wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
- wndclass.lpszMenuName = NULL;
- wndclass.lpszClassName = "PopupWindowClass";
- if ( ! RegisterClass (&wndclass) )
- return FALSE;
-
- hWnd = CreateWindow ("phoenix ip system","Image Processing System",
- 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);
- }
- IsSelecting=FALSE;
- 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_LBUTTONDOWN:
- if(IsSelecting){
- SeedPoint.x = LOWORD(lParam);
- SeedPoint.y = HIWORD(lParam);
- IsSelecting=FALSE;
- if(SeedFill(hWnd))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- break;
- case WM_COMMAND:
- switch (wParam){
- case IDM_LOADBMP:
- //注意重新分配内存和调色板,位图句柄时,先释放原来的
- if(hBitmap!=NULL){
- DeleteObject(hBitmap);
- hBitmap=NULL;
- }
- if(hPalette!=NULL){
- DeleteObject(hPalette);
- hPalette=NULL;
- }
- if(hImgData!=NULL){
- GlobalUnlock(hImgData);
- GlobalFree(hImgData);
- hImgData=NULL;
- }
- if(LoadBmpFile(hWnd,"c:\test.bmp")) //成功,则重画窗口
- InvalidateRect(hWnd,NULL,TRUE);
- break;
- case IDM_TRANSLATION:
- if(hImgData!=NULL){
- if(Translation(hWnd))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_ROTATION:
- if(hImgData!=NULL){
- if(Rotation(hWnd))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_MIRRORX:
- if(hImgData!=NULL){
- if(Mirror(hWnd,TRUE))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_MIRRORY:
- if(hImgData!=NULL){
- if(Mirror(hWnd,FALSE))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_TRANSPOSE:
- if(hImgData!=NULL){
- if(Transpose(hWnd))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_ZOOM:
- if(hImgData!=NULL){
- if(Zoom(hWnd))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_SMOOTHING_BOX:
- if(hImgData!=NULL){
- if(TemplateOperation(hWnd,TEMPLATE_SMOOTH_BOX))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_SMOOTHING_GAUSS:
- if(hImgData!=NULL){
- if(TemplateOperation(hWnd,TEMPLATE_SMOOTH_GAUSS))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_SHARPENING_LAPLACIAN:
- if(hImgData!=NULL){
- if(TemplateOperation(hWnd,TEMPLATE_SHARPEN_LAPLACIAN))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_LIMBPATTERNM3:
- if(hImgData!=NULL){
- if(LimbPatternM3(hWnd))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_LIMBPATTERNM4:
- if(hImgData!=NULL){
- if(LimbPatternM4(hWnd))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_STEINBERG:
- if(hImgData!=NULL){
- if(Steinberg(hWnd));
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_BMP2TXT:
- if(hImgData!=NULL){
- if(Bmp2Txt(hWnd))
- MessageBox(hWnd,"Finished!","Success Message",MB_OK);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_HISTOGRAM:
- if(hImgData!=NULL){
- if(Histogram(hWnd))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_TRUE256:
- if(hImgData!=NULL){
- if(Trueto256(hWnd))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_CLRGRAY:
- if(hImgData!=NULL){
- if(ColortoGrayScale(hWnd))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_INVERT:
- if(hImgData!=NULL){
- if(Invert(hWnd))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_CONTSTRETCH:
- if(hImgData!=NULL){
- if (ContrastStretch(hWnd))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_CLIPING:
- if(hImgData!=NULL){
- if(Cliping(hWnd))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_THRESHOLD:
- if(hImgData!=NULL){
- if(Thresholding(hWnd))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_SLICEWITHOUTBACK:
- if(hImgData!=NULL){
- if(Slice(hWnd,FALSE))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_SLICEWITHBACK:
- if(hImgData!=NULL){
- if(Slice(hWnd,TRUE))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_HISTOGRAMEQUA:
- if(hImgData!=NULL){
- if(HistogramEqua(hWnd))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_HDILATION:
- if(hImgData!=NULL){
- if(Dilation(hWnd,TRUE))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_VDILATION:
- if(hImgData!=NULL){
- if(Dilation(hWnd,FALSE))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_HEROSION:
- if(hImgData!=NULL){
- if(Erosion(hWnd,TRUE))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_VEROSION:
- if(hImgData!=NULL){
- if(Erosion(hWnd,FALSE))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_HOPEN:
- if(hImgData!=NULL){
- if(MorphOpen(hWnd,TRUE))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_VOPEN:
- if(hImgData!=NULL){
- if(MorphOpen(hWnd,FALSE))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_HCLOSE:
- if(hImgData!=NULL){
- if(MorphClose(hWnd,TRUE))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_VCLOSE:
- if(hImgData!=NULL){
- if(MorphClose(hWnd,FALSE))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_THINNING:
- if(hImgData!=NULL){
- if(Thinning(hWnd))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_OUTLINE:
- if(hImgData!=NULL){
- if(Outline(hWnd))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_HSOBEL:
- if(hImgData!=NULL){
- if(TemplateOperation(hWnd,TEMPLATE_SOBEL_H))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_VSOBEL:
- if(hImgData!=NULL){
- if(TemplateOperation(hWnd,TEMPLATE_SOBEL_V))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_HISOSOBEL:
- if(hImgData!=NULL){
- if(TemplateOperation(hWnd,TEMPLATE_ISOSOBEL_H))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_VISOSOBEL:
- if(hImgData!=NULL){
- if(TemplateOperation(hWnd,TEMPLATE_ISOSOBEL_V))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_HOUGH:
- if(hImgData!=NULL)
- Hough(hWnd);
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_HMEDIAN:
- if(hImgData!=NULL){
- if(MedianFilter(hWnd,TRUE))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_VMEDIAN:
- if(hImgData!=NULL){
- if(MedianFilter(hWnd,FALSE))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_LOG:
- if(hImgData!=NULL){
- if(LapOfGauss(hWnd))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_CONTOUR:
- if(hImgData!=NULL){
- if(Contour(hWnd))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_SEEDFILL:
- if(hImgData!=NULL){
- IsSelecting=TRUE;
- MessageBox(hWnd,"Please click the area to be filled!","Message",MB_OK|MB_ICONEXCLAMATION);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_HPROJECTION:
- if(hImgData!=NULL){
- if(Projection(hWnd,TRUE))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_VPROJECTION:
- if(hImgData!=NULL){
- if(Projection(hWnd,FALSE))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- case IDM_SUBTRACTION:
- if(hImgData!=NULL){
- MessageBox(hWnd,"Be sure that c:\test.bmp and c:\backgnd.bmp are both 256 grayscale bitmaps ! They must be same size and have same palette!","Important",MB_OK);
- if(Subtraction(hWnd))
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else
- MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- break;
- 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 LoadBmpFile (HWND hWnd,char *BmpFileName)
- {
- HFILE hf;
- LPBITMAPINFOHEADER lpImgData;
- LOGPALETTE *pPal;
- LPRGBQUAD lpRGB;
- HPALETTE hPrevPalette;
- HDC hDc;
- HLOCAL hPal;
- DWORD ImgSize;
- DWORD i;
- if((hf=_lopen(BmpFileName,OF_READ))==HFILE_ERROR){
- MessageBox(hWnd,"File c:\test.bmp not found!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- _lread(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER));
- _lread(hf,(LPSTR)&bi,sizeof(BITMAPINFOHEADER));
- ImgWidth=bi.biWidth;
- ImgHeight=bi.biHeight;
- LineBytes=(DWORD)WIDTHBYTES(bi.biWidth*bi.biBitCount);
- ImgSize=(DWORD)LineBytes*bi.biHeight;
- if(bi.biClrUsed!=0)
- NumColors=(DWORD)bi.biClrUsed;
- else
- switch(bi.biBitCount){
- case 1:
- NumColors=2;
- break;
- case 4:
- NumColors=16;
- break;
- case 8:
- NumColors=256;
- break;
- case 24:
- NumColors=0;
- break;
- default:
- MessageBox(hWnd,"Invalid color numbers!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- _lclose(hf);
- return FALSE;
- }
- if(bf.bfOffBits!=(DWORD)(NumColors*sizeof(RGBQUAD)+sizeof(BITMAPFILEHEADER)
- +sizeof(BITMAPINFOHEADER)))
- {
- MessageBox(hWnd,"Invalid color numbers!","Error Message" ,MB_OK|
- MB_ICONEXCLAMATION);
- _lclose(hf);
- return FALSE;
- }
- bf.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+NumColors*sizeof(RGBQUAD)+ImgSize;
- if((hImgData=GlobalAlloc(GHND,(DWORD)(sizeof(BITMAPINFOHEADER)+
- NumColors*sizeof(RGBQUAD)+ImgSize)))==NULL)
- {
- MessageBox(hWnd,"Error alloc memory!","ErrorMessage",MB_OK|
- MB_ICONEXCLAMATION);
- _lclose(hf);
- return FALSE;
- }
-
- lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
- _llseek(hf,sizeof(BITMAPFILEHEADER),FILE_BEGIN);
- _hread(hf,(char *)lpImgData,(long)sizeof(BITMAPINFOHEADER)
- +(long)NumColors*sizeof(RGBQUAD)+ImgSize);
- _lclose(hf);
- if(NumColors!=0)
- {
- hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));
- pPal =(LOGPALETTE *)LocalLock(hPal);
- pPal->palNumEntries =(WORD) NumColors;
- pPal->palVersion = 0x300;
- lpRGB = (LPRGBQUAD)((LPSTR)lpImgData + (DWORD)sizeof(BITMAPINFOHEADER));
- for (i = 0; i < NumColors; i++) {
- pPal->palPalEntry[i].peRed=lpRGB->rgbRed;
- pPal->palPalEntry[i].peGreen=lpRGB->rgbGreen;
- pPal->palPalEntry[i].peBlue=lpRGB->rgbBlue;
- pPal->palPalEntry[i].peFlags=(BYTE)0;
- lpRGB++;
- }
- hPalette=CreatePalette(pPal);
- LocalUnlock(hPal);
- LocalFree(hPal);
- }
- hDc=GetDC(hWnd);
- if(hPalette){
- hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
- RealizePalette(hDc);
- }
- 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);
- }
-
- ReleaseDC(hWnd,hDc);
- GlobalUnlock(hImgData);
- return TRUE;
- }
- ////////////////////////////////////////////////////////////////
- BOOL CALLBACK InputBox( HWND hDlg, UINT message,WPARAM wParam, LPARAM lParam )
- {
- char str[80];
- switch ( message ){
- case WM_INITDIALOG:
- SetDlgItemText(hDlg,ID_XOFFSET,"0");
- SetDlgItemText(hDlg,ID_YOFFSET,"0");
- SetDlgItemText(hDlg,ID_ANGLE,"0");
- SetDlgItemText(hDlg,ID_ZOOMRATIO,"0.25");
- SetDlgItemText(hDlg,ID_FIRSTPOINT,"0");
- SetDlgItemText(hDlg,ID_SECONDPOINT,"0");
- SetDlgItemText(hDlg,ID_STRETCHRATIO,"2.0");
- SetDlgItemText(hDlg,ID_EQUASCALE,"128");
- return TRUE;
- case WM_COMMAND:
- if ( wParam == IDOK || wParam == IDCANCEL )
- {
- GetDlgItemText(hDlg,ID_XOFFSET,str,80);
- xOffset=atoi(str);
- GetDlgItemText(hDlg,ID_YOFFSET,str,80);
- yOffset=atoi(str);
- GetDlgItemText(hDlg,ID_ANGLE,str,80);
- RotateAngle=(float)atof(str);
- GetDlgItemText(hDlg,ID_ZOOMRATIO,str,80);
- ZoomRatio=(float)atof(str);
- GetDlgItemText(hDlg,ID_FIRSTPOINT,str,80);
- FirstPoint=atoi(str);
- GetDlgItemText(hDlg,ID_SECONDPOINT,str,80);
- SecondPoint=atoi(str);
- GetDlgItemText(hDlg,ID_STRETCHRATIO,str,80);
- StretchRatio=(float)atof(str);
- GetDlgItemText(hDlg,ID_EQUASCALE,str,80);
- EquaScale=atoi(str);
- EndDialog ( hDlg, TRUE );
- return TRUE;
- }
- break;
- }
- return FALSE;
- }
- ////////////////////////////////////////////////////////////////
- BOOL Translation(HWND hWnd)
- {
- DLGPROC dlgInputBox = NULL;
- DWORD OffBits,BufSize;
- LPBITMAPINFOHEADER lpImgData;
- LPSTR lpPtr;
- HLOCAL hTempImgData;
- LPBITMAPINFOHEADER lpTempImgData;
- LPSTR lpTempPtr;
- int SrcX0,SrcY0,SrcX1,SrcY1,DstX0,DstY0,DstX1,DstY1;
- int RectWidth,RectHeight;
- BOOL xVisible,yVisible;
- HDC hDc;
- HFILE hf;
- int i;
- dlgInputBox = (DLGPROC) MakeProcInstance ( (FARPROC)InputBox, ghInst );
- DialogBox (ghInst, "INPUTBOX", hWnd, dlgInputBox);
- FreeProcInstance ( (FARPROC) dlgInputBox );
- OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
- BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
- if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
- {
- MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
- lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
- lpPtr=(char *)lpImgData;
- lpTempPtr=(char *)lpTempImgData;
- memset(lpTempPtr,(BYTE)255,BufSize);
- memcpy(lpTempPtr,lpPtr,OffBits);
- xVisible=TRUE;
- if( xOffset<= -bi.biWidth )
- xVisible=FALSE;
- else if( xOffset<=0){
- DstX0=0;
- DstX1=bi.biWidth+xOffset;
- }
- else if ( xOffset<bi.biWidth){
- DstX0=xOffset;
- DstX1=bi.biWidth;
- }
- else
- xVisible=FALSE;
- SrcX0=DstX0-xOffset;
- SrcX1=DstX1-xOffset;
- RectWidth=DstX1-DstX0;
- yVisible=TRUE;
- if( yOffset<= -bi.biHeight )
- yVisible=FALSE;
- else if( yOffset<=0){
- DstY0=0;
- DstY1=bi.biHeight+yOffset;
- }
- else if ( yOffset<bi.biHeight){
- DstY0=yOffset;
- DstY1=bi.biHeight;
- }
- else
- yVisible=FALSE;
- SrcY0=DstY0-yOffset;
- SrcY1=DstY1-yOffset;
- RectHeight=DstY1-DstY0;
- if( xVisible && yVisible){
- for(i=0;i<RectHeight;i++){
- lpPtr=(char *)lpImgData+(BufSize-LineBytes-(i+SrcY0)*LineBytes)+SrcX0;
- lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-(i+DstY0)*LineBytes)+DstX0;
- memcpy(lpTempPtr,lpPtr,RectWidth);
- }
- }
- hDc=GetDC(hWnd);
- if(hBitmap!=NULL)
- DeleteObject(hBitmap);
-
- hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
- (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
- (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
- hf=_lcreat("c:\translation.bmp",0);
- _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER));
- _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
- _lclose(hf);
- ReleaseDC(hWnd,hDc);
- LocalUnlock(hTempImgData);
- LocalFree(hTempImgData);
- GlobalUnlock(hImgData);
- return TRUE;
- }
- ////////////////////////////////////////////////////////////////
- BOOL Rotation(HWND hWnd)
- {
- DLGPROC dlgInputBox = NULL;
- DWORD OffBits,SrcBufSize,DstBufSize,DstLineBytes;
- LPBITMAPINFOHEADER lpImgData;
- LPSTR lpPtr;
- HLOCAL hTempImgData;
- LPBITMAPINFOHEADER lpTempImgData;
- LPSTR lpTempPtr;
- float SrcX1,SrcY1,SrcX2,SrcY2,SrcX3,SrcY3,SrcX4,SrcY4;
- float DstX1,DstY1,DstX2,DstY2,DstX3,DstY3,DstX4,DstY4;
- DWORD Wold,Hold,Wnew,Hnew;
- HDC hDc;
- HFILE hf;
- DWORD x0,y0,x1,y1;
- float cosa,sina; //cos(a),sin(a)
- float num1,num2;
- BITMAPFILEHEADER DstBf;
- BITMAPINFOHEADER DstBi;
- dlgInputBox = (DLGPROC) MakeProcInstance ( (FARPROC)InputBox, ghInst );
- DialogBox (ghInst, "INPUTBOX", hWnd, dlgInputBox);
- FreeProcInstance ( (FARPROC) dlgInputBox );
-
- RotateAngle=(float)RADIAN(RotateAngle);
- cosa=(float)cos((double)RotateAngle);
- sina=(float)sin((double)RotateAngle);
- Wold=bi.biWidth;
- Hold=bi.biHeight;
- SrcX1=(float)(-0.5*Wold);
- SrcY1=(float)(0.5*Hold);
- SrcX2=(float)(0.5*Wold);
- SrcY2=(float)(0.5*Hold);
- SrcX3=(float)(-0.5*Wold);
- SrcY3=(float)(-0.5*Hold);
- SrcX4=(float)(0.5*Wold);
- SrcY4=(float)(-0.5*Hold);
- DstX1=cosa*SrcX1+sina*SrcY1;
- DstY1=-sina*SrcX1+cosa*SrcY1;
- DstX2=cosa*SrcX2+sina*SrcY2;
- DstY2=-sina*SrcX2+cosa*SrcY2;
- DstX3=cosa*SrcX3+sina*SrcY3;
- DstY3=-sina*SrcX3+cosa*SrcY3;
- DstX4=cosa*SrcX4+sina*SrcY4;
- DstY4=-sina*SrcX4+cosa*SrcY4;
- Wnew = (DWORD)(max(fabs(DstX4-DstX1), fabs(DstX3-DstX2))+0.5);
- Hnew = (DWORD)(max(fabs(DstY4-DstY1), fabs(DstY3-DstY2))+0.5);
- num1=(float)( -0.5*Wnew*cosa-0.5*Hnew*sina+0.5*Wold);
- num2=(float)(0.5*Wnew*sina-0.5*Hnew*cosa+0.5*Hold);
- OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
- SrcBufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
- ImgWidth=Wnew;
- ImgHeight=Hnew;
- DstLineBytes=(DWORD)WIDTHBYTES(Wnew*bi.biBitCount);
- DstBufSize=(DWORD)(sizeof(BITMAPINFOHEADER)+NumColors*sizeof(RGBQUAD)+(DWORD)DstLineBytes*Hnew);
- if((hTempImgData=LocalAlloc(LHND,DstBufSize))==NULL)
- {
- MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
- lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
- lpPtr=(char *)lpImgData;
- lpTempPtr=(char *)lpTempImgData;
- memset(lpTempPtr,(BYTE)255,DstBufSize);
- memcpy(lpTempPtr,lpPtr,OffBits);
-
- //get new bitmapfileheader and bitmapinfoheader
- memcpy((char *)&DstBf,(char *)&bf,sizeof(BITMAPFILEHEADER));
- memcpy((char *)&DstBi,(char *)&bi,sizeof(BITMAPINFOHEADER));
- DstBf.bfSize=DstBufSize+sizeof(BITMAPFILEHEADER);
- DstBi.biWidth=Wnew;
- DstBi.biHeight=Hnew;
- //overwrite bitmapinfoheader with the new one
- memcpy(lpTempPtr,(char *)&DstBi,sizeof(BITMAPINFOHEADER));
- for(y1=0;y1<Hnew;y1++)
- for(x1=0;x1<Wnew;x1++){
- x0= (DWORD)(x1*cosa+y1*sina+num1);
- y0= (DWORD)(-1.0f*x1*sina+y1*cosa+num2);
- if( (x0>=0) && (x0<Wold) && (y0>=0) && (y0<Hold))
- {
- lpPtr=(char *)lpImgData+(SrcBufSize-LineBytes-y0*LineBytes)+x0;
- lpTempPtr=(char *)lpTempImgData+(DstBufSize-DstLineBytes-y1*DstLineBytes)+x1;
- *lpTempPtr=*lpPtr;
- }
- }
- hDc=GetDC(hWnd);
- if(hBitmap!=NULL)
- DeleteObject(hBitmap);
-
- hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
- (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
- (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
- hf=_lcreat("c:\rotation.bmp",0);
- _lwrite(hf,(LPSTR)&DstBf,sizeof(BITMAPFILEHEADER));
- _lwrite(hf,(LPSTR)lpTempImgData,DstBufSize);
- _lclose(hf);
- ReleaseDC(hWnd,hDc);
- LocalUnlock(hTempImgData);
- LocalFree(hTempImgData);
- GlobalUnlock(hImgData);
- return TRUE;
- }
- ////////////////////////////////////////////////////////////////
- BOOL Mirror(HWND hWnd,BOOL XDirection)
- {
- DWORD OffBits,BufSize;
- LPBITMAPINFOHEADER lpImgData;
- LPSTR lpPtr;
- HLOCAL hTempImgData;
- LPBITMAPINFOHEADER lpTempImgData;
- LPSTR lpTempPtr;
- HDC hDc;
- HFILE hf;
- LONG x0,y0,x1,y1;
- OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
- BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
- if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
- {
- MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
- lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
- lpPtr=(char *)lpImgData;
- lpTempPtr=(char *)lpTempImgData;
- memset(lpTempPtr,(BYTE)255,BufSize);
- memcpy(lpTempPtr,lpPtr,OffBits);
- if( XDirection){
- for(y1=0;y1<bi.biHeight;y1++)
- for(x1=0;x1<bi.biWidth;x1++){
- x0=bi.biWidth-1-x1;
- y0=y1;
- lpPtr=(char *)lpImgData+(BufSize-LineBytes-y0*LineBytes)+x0;
- lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y1*LineBytes)+x1;
- *lpTempPtr=*lpPtr;
- }
- }
- else{
- for(y1=0;y1<bi.biHeight;y1++)
- for(x1=0;x1<bi.biWidth;x1++){
- x0=x1;
- y0=bi.biHeight-1-y1;
- lpPtr=(char *)lpImgData+(BufSize-LineBytes-y0*LineBytes)+x0;
- lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y1*LineBytes)+x1;
- *lpTempPtr=*lpPtr;
- }
- }
- hDc=GetDC(hWnd);
- if(hBitmap!=NULL)
- DeleteObject(hBitmap);
-
- hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
- (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
- (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
- if( XDirection)
- hf=_lcreat("c:\mirrorx.bmp",0);
- else
- hf=_lcreat("c:\mirrory.bmp",0);
- _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER));
- _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
- _lclose(hf);
- ReleaseDC(hWnd,hDc);
- LocalUnlock(hTempImgData);
- LocalFree(hTempImgData);
- GlobalUnlock(hImgData);
- return TRUE;
- }
- ////////////////////////////////////////////////////////////////
- BOOL Transpose(HWND hWnd)
- {
- DWORD OffBits,SrcBufSize,DstBufSize,DstLineBytes;
- LPBITMAPINFOHEADER lpImgData;
- LPSTR lpPtr;
- HLOCAL hTempImgData;
- LPBITMAPINFOHEADER lpTempImgData;
- LPSTR lpTempPtr;
- DWORD Wnew,Hnew;
- HDC hDc;
- HFILE hf;
- DWORD x0,y0,x1,y1;
- BITMAPFILEHEADER DstBf;
- BITMAPINFOHEADER DstBi;
- Wnew = (DWORD)bi.biHeight;
- Hnew = (DWORD)bi.biWidth;
- OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
- SrcBufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
- ImgWidth=Wnew;
- ImgHeight=Hnew;
- DstLineBytes=(DWORD)WIDTHBYTES(Wnew*bi.biBitCount);
- DstBufSize=(DWORD)(sizeof(BITMAPINFOHEADER)+NumColors*sizeof(RGBQUAD)+(DWORD)DstLineBytes*Hnew);
- if((hTempImgData=LocalAlloc(LHND,DstBufSize))==NULL)
- {
- MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
- lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
- lpPtr=(char *)lpImgData;
- lpTempPtr=(char *)lpTempImgData;
- memset(lpTempPtr,(BYTE)255,DstBufSize);
- memcpy(lpTempPtr,lpPtr,OffBits);
-
- //get new bitmapfileheader and bitmapinfoheader
- memcpy((char *)&DstBf,(char *)&bf,sizeof(BITMAPFILEHEADER));
- memcpy((char *)&DstBi,(char *)&bi,sizeof(BITMAPINFOHEADER));
- DstBf.bfSize=DstBufSize+sizeof(BITMAPFILEHEADER);
- DstBi.biWidth=Wnew;
- DstBi.biHeight=Hnew;
- //overwrite bitmapinfoheader with the new one
- memcpy(lpTempPtr,(char *)&DstBi,sizeof(BITMAPINFOHEADER));
- for(y1=0;y1<Hnew;y1++)
- for(x1=0;x1<Wnew;x1++){
- x0= y1;
- y0= x1;
- lpPtr=(char *)lpImgData+(SrcBufSize-LineBytes-y0*LineBytes)+x0;
- lpTempPtr=(char *)lpTempImgData+(DstBufSize-DstLineBytes-y1*DstLineBytes)+x1;
- *lpTempPtr=*lpPtr;
- }
-
- hDc=GetDC(hWnd);
- if(hBitmap!=NULL)
- DeleteObject(hBitmap);
-
- hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
- (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
- (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
- hf=_lcreat("c:\transpose.bmp",0);
- _lwrite(hf,(LPSTR)&DstBf,sizeof(BITMAPFILEHEADER));
- _lwrite(hf,(LPSTR)lpTempImgData,DstBufSize);
- _lclose(hf);
- ReleaseDC(hWnd,hDc);
- LocalUnlock(hTempImgData);
- LocalFree(hTempImgData);
- GlobalUnlock(hImgData);
- return TRUE;
- }
- ////////////////////////////////////////////////////////////////
- BOOL Zoom(HWND hWnd)
- {
- DLGPROC dlgInputBox = NULL;
- DWORD OffBits,SrcBufSize,DstBufSize,DstLineBytes;
- LPBITMAPINFOHEADER lpImgData;
- LPSTR lpPtr;
- HLOCAL hTempImgData;
- LPBITMAPINFOHEADER lpTempImgData;
- LPSTR lpTempPtr;
- DWORD Wold,Hold,Wnew,Hnew;
- HDC hDc;
- HFILE hf;
- DWORD x0,y0,x1,y1;
- float num1;
- BITMAPFILEHEADER DstBf;
- BITMAPINFOHEADER DstBi;
- dlgInputBox = (DLGPROC) MakeProcInstance ( (FARPROC)InputBox, ghInst );
- DialogBox (ghInst, "INPUTBOX", hWnd, dlgInputBox);
- FreeProcInstance ( (FARPROC) dlgInputBox );
-
- num1=(float)(1.0/ZoomRatio);
- Wold=bi.biWidth;
- Hold=bi.biHeight;
- Wnew = (DWORD)(Wold*ZoomRatio+0.5);
- Hnew = (DWORD)(Hold*ZoomRatio+0.5);
- OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
- SrcBufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
- ImgWidth=Wnew;
- ImgHeight=Hnew;
- DstLineBytes=(DWORD)WIDTHBYTES(Wnew*bi.biBitCount);
- DstBufSize=(DWORD)(sizeof(BITMAPINFOHEADER)+NumColors*sizeof(RGBQUAD)+(DWORD)DstLineBytes*Hnew);
- if((hTempImgData=LocalAlloc(LHND,DstBufSize))==NULL)
- {
- MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
- lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
- lpPtr=(char *)lpImgData;
- lpTempPtr=(char *)lpTempImgData;
- memset(lpTempPtr,(BYTE)255,DstBufSize);
- memcpy(lpTempPtr,lpPtr,OffBits);
-
- //get new bitmapfileheader and bitmapinfoheader
- memcpy((char *)&DstBf,(char *)&bf,sizeof(BITMAPFILEHEADER));
- memcpy((char *)&DstBi,(char *)&bi,sizeof(BITMAPINFOHEADER));
- DstBf.bfSize=DstBufSize+sizeof(BITMAPFILEHEADER);
- DstBi.biWidth=Wnew;
- DstBi.biHeight=Hnew;
- //overwrite bitmapinfoheader with the new one
- memcpy(lpTempPtr,(char *)&DstBi,sizeof(BITMAPINFOHEADER));
- for(y1=0;y1<Hnew;y1++)
- for(x1=0;x1<Wnew;x1++){
- x0= (DWORD)(x1*num1);
- y0= (DWORD)(y1*num1);
- if( (x0>=0) && (x0<Wold) && (y0>=0) && (y0<Hold))
- {
- lpPtr=(char *)lpImgData+(SrcBufSize-LineBytes-y0*LineBytes)+x0;
- lpTempPtr=(char *)lpTempImgData+(DstBufSize-DstLineBytes-y1*DstLineBytes)+x1;
- *lpTempPtr=*lpPtr;
- }
- }
- hDc=GetDC(hWnd);
- if(hBitmap!=NULL)
- DeleteObject(hBitmap);
-
- hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
- (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
- (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
- hf=_lcreat("c:\zoom.bmp",0);
- _lwrite(hf,(LPSTR)&DstBf,sizeof(BITMAPFILEHEADER));
- _lwrite(hf,(LPSTR)lpTempImgData,DstBufSize);
- _lclose(hf);
- ReleaseDC(hWnd,hDc);
- LocalUnlock(hTempImgData);
- LocalFree(hTempImgData);
- GlobalUnlock(hImgData);
- return TRUE;
- }
- ////////////////////////////////////////////////////////////////
- BOOL TemplateOperation(HWND hWnd, int TemplateType)
- {
- DWORD OffBits,BufSize;
- LPBITMAPINFOHEADER lpImgData;
- LPSTR lpPtr;
- HLOCAL hTempImgData;
- LPBITMAPINFOHEADER lpTempImgData;
- LPSTR lpTempPtr;
- HDC hDc;
- HFILE hf;
- LONG x,y;
- float coef;
- float CoefArray[9];
- float TempNum;
- char filename[80];
- switch(TemplateType){
- case TEMPLATE_SMOOTH_BOX:
- coef=(float)(1.0/9.0);
- memcpy(CoefArray,Template_Smooth_Box,9*sizeof(float));
- strcpy(filename,"c:\smbox.bmp");
- break;
- case TEMPLATE_SMOOTH_GAUSS:
- coef=(float)(1.0/16.0);
- memcpy(CoefArray,Template_Smooth_Gauss,9*sizeof(float));
- strcpy(filename,"c:\smgauss.bmp");
- break;
- case TEMPLATE_SHARPEN_LAPLACIAN:
- coef=(float)1.0;
- memcpy(CoefArray,Template_Sharpen_Laplacian,9*sizeof(float));
- strcpy(filename,"c:\shlaplac.bmp");
- break;
- case TEMPLATE_SOBEL_H:
- coef=(float)1.0;
- memcpy(CoefArray,Template_HSobel,9*sizeof(float));
- strcpy(filename,"c:\hsobel.bmp");
- break;
- case TEMPLATE_SOBEL_V:
- coef=(float)1.0;
- memcpy(CoefArray,Template_VSobel,9*sizeof(float));
- strcpy(filename,"c:\vsobel.bmp");
- break;
- case TEMPLATE_ISOSOBEL_H:
- coef=(float)1.0;
- memcpy(CoefArray,Template_HIsoSobel,9*sizeof(float));
- strcpy(filename,"c:\hisobel.bmp");
- break;
- case TEMPLATE_ISOSOBEL_V:
- coef=(float)1.0;
- memcpy(CoefArray,Template_VIsoSobel,9*sizeof(float));
- strcpy(filename,"c:\visobel.bmp");
- break;
- }
- OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
- BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
- if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
- {
- MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
- lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
- lpPtr=(char *)lpImgData;
- lpTempPtr=(char *)lpTempImgData;
- memcpy(lpTempPtr,lpPtr,BufSize);
- for(y=1;y<bi.biHeight-1;y++)
- for(x=1;x<bi.biWidth-1;x++){
- lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes)+x;
- lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes)+x;
- TempNum=(float)((unsigned char)*(lpPtr+LineBytes-1))*CoefArray[0];
- TempNum+=(float)((unsigned char)*(lpPtr+LineBytes))*CoefArray[1];
- TempNum+=(float)((unsigned char)*(lpPtr+LineBytes+1))*CoefArray[2];
- TempNum+=(float)((unsigned char)*(lpPtr-1))*CoefArray[3];
- TempNum+=(float)((unsigned char)*lpPtr)*CoefArray[4];
- TempNum+=(float)((unsigned char)*(lpPtr+1))*CoefArray[5];
- TempNum+=(float)((unsigned char)*(lpPtr-LineBytes-1))*CoefArray[6];
- TempNum+=(float)((unsigned char)*(lpPtr-LineBytes))*CoefArray[7];
- TempNum+=(float)((unsigned char)*(lpPtr-LineBytes+1))*CoefArray[8];
- TempNum*=coef;
- if(TempNum>255.0) *lpTempPtr=(BYTE)255;
- else if(TempNum<0.0)
- *lpTempPtr=(unsigned char)fabs(TempNum);
- else *lpTempPtr=(BYTE)TempNum;
- }
- hDc=GetDC(hWnd);
- if(hBitmap!=NULL)
- DeleteObject(hBitmap);
-
- hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
- (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
- (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
- hf=_lcreat(filename,0);
- _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER));
- _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
- _lclose(hf);
- ReleaseDC(hWnd,hDc);
- LocalUnlock(hTempImgData);
- LocalFree(hTempImgData);
- GlobalUnlock(hImgData);
- return TRUE;
- }
- ////////////////////////////////////////////////////////////////
- BOOL ColortoGrayScale(HWND hWnd)
- {
- DWORD SrcBufSize,DstBufSize,DstLineBytes;
- LPBITMAPINFOHEADER lpImgData;
- LPSTR lpPtr;
- HLOCAL hTempImgData;
- LPBITMAPINFOHEADER lpTempImgData;
- LPSTR lpTempPtr;
- HDC hDc;
- HFILE hf;
- LONG x,y;
- BITMAPFILEHEADER DstBf;
- BITMAPINFOHEADER DstBi;
- LOGPALETTE *pPal;
- HPALETTE hPrevPalette;
- HLOCAL hPal;
- DWORD NewNumColors;
- WORD NewBitCount;
- float Y;
- DWORD i;
- unsigned char Red,Green,Blue,Gray;
- NewNumColors=NumColors;
- NewBitCount=bi.biBitCount;
- if(NumColors==0) //true color
- {
- NewNumColors=256;
- NewBitCount=8;
- }
- DstLineBytes=(DWORD)WIDTHBYTES(bi.biWidth*NewBitCount);
- DstBufSize=(DWORD)(sizeof(BITMAPINFOHEADER)+NewNumColors*sizeof(RGBQUAD)+(DWORD)DstLineBytes*bi.biHeight);
- memcpy((char *)&DstBf,(char *)&bf,sizeof(BITMAPFILEHEADER));
- memcpy((char *)&DstBi,(char *)&bi,sizeof(BITMAPINFOHEADER));
- DstBf.bfSize=DstBufSize+sizeof(BITMAPFILEHEADER);
- DstBf.bfOffBits=(DWORD)(NewNumColors*sizeof(RGBQUAD)+sizeof(BITMAPFILEHEADER)
- +sizeof(BITMAPINFOHEADER));
- DstBi.biClrUsed=0;
- DstBi.biBitCount=NewBitCount;
- SrcBufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
- if((hTempImgData=LocalAlloc(LHND,DstBufSize))==NULL)
- {
- MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
- lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
- //copy image data
- memcpy(lpTempImgData,lpImgData,DstBufSize);
- //overwrite bitmapinfoheader with the new one
- memcpy(lpTempImgData,(char *)&DstBi,sizeof(BITMAPINFOHEADER));
- lpPtr=(char *)lpImgData+sizeof(BITMAPINFOHEADER);
- lpTempPtr=(char *)lpTempImgData+sizeof(BITMAPINFOHEADER);
- hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NewNumColors* sizeof(PALETTEENTRY));
- pPal =(LOGPALETTE *)LocalLock(hPal);
- pPal->palNumEntries =(WORD) NewNumColors;
- pPal->palVersion = 0x300;
- if(NumColors==0) //true color
- for (i = 0; i < 256; i++) {
- pPal->palPalEntry[i].peRed=(BYTE)i;
- pPal->palPalEntry[i].peGreen=(BYTE)i;
- pPal->palPalEntry[i].peBlue=(BYTE)i;
- pPal->palPalEntry[i].peFlags=(BYTE)0;
- *(lpTempPtr++)=(unsigned char)i;
- *(lpTempPtr++)=(unsigned char)i;
- *(lpTempPtr++)=(unsigned char)i;
- *(lpTempPtr++)=0;
- }
- else
- for (i = 0; i < NewNumColors; i++) {
- Blue=(unsigned char )(*lpPtr++);
- Green=(unsigned char )(*lpPtr++);
- Red=(unsigned char )(*lpPtr++);
- Y=(float)(Red*0.299+Green*0.587+Blue*0.114);
- Gray=(BYTE)Y;
- lpPtr++;
- pPal->palPalEntry[i].peRed=Gray;
- pPal->palPalEntry[i].peGreen=Gray;
- pPal->palPalEntry[i].peBlue=Gray;
- pPal->palPalEntry[i].peFlags=0;
- *(lpTempPtr++)=(unsigned char)Gray;
- *(lpTempPtr++)=(unsigned char)Gray;
- *(lpTempPtr++)=(unsigned char)Gray;
- *(lpTempPtr++)=0;
- }
- if(hPalette!=NULL)
- DeleteObject(hPalette);
-
- hPalette=CreatePalette(pPal);
- LocalUnlock(hPal);
- LocalFree(hPal);
- hDc=GetDC(hWnd);
- if(hPalette){
- hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
- RealizePalette(hDc);
- }
-
- if(NumColors==0)
- for(y=0;y<bi.biHeight;y++){
- lpPtr=(char *)lpImgData+(SrcBufSize-LineBytes-y*LineBytes);
- lpTempPtr=(char *)lpTempImgData+(DstBufSize-DstLineBytes-y*DstLineBytes);
- for(x=0;x<bi.biWidth;x++){
- Blue=(unsigned char )(*lpPtr++);
- Green=(unsigned char )(*lpPtr++);
- Red=(unsigned char )(*lpPtr++);
- Y=(float)(Red*0.299+Green*0.587+Blue*0.114);
- Gray=(BYTE)Y;
- *(lpTempPtr++)=(unsigned char)Gray;
- }
- }
- if(hBitmap!=NULL)
- DeleteObject(hBitmap);
-
- hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
- (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NewNumColors*sizeof(RGBQUAD),
- (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
- if(hPalette && hPrevPalette){
- SelectPalette(hDc,hPrevPalette,FALSE);
- RealizePalette(hDc);
- }
- hf=_lcreat("c:\gray.bmp",0);
- _lwrite(hf,(LPSTR)&DstBf,sizeof(BITMAPFILEHEADER));
- _lwrite(hf,(LPSTR)lpTempImgData,DstBufSize);
- _lclose(hf);
- ReleaseDC(hWnd,hDc);
- LocalUnlock(hTempImgData);
- LocalFree(hTempImgData);
- GlobalUnlock(hImgData);
- return TRUE;
- }
- ////////////////////////////////////////////////////////////////
- BOOL Invert(HWND hWnd)
- {
- DWORD BufSize;
- LPBITMAPINFOHEADER lpImgData;
- LPSTR lpPtr;
- HLOCAL hTempImgData;
- LPBITMAPINFOHEADER lpTempImgData;
- LPSTR lpTempPtr;
- HDC hDc;
- HFILE hf;
- LONG x,y;
- LOGPALETTE *pPal;
- HPALETTE hPrevPalette=NULL;
- HLOCAL hPal;
- DWORD i;
- unsigned char Red,Green,Blue;
- BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
- if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
- {
- MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
- lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
- //copy image data
- memcpy(lpTempImgData,lpImgData,BufSize);
- hDc=GetDC(hWnd);
- if(NumColors!=0){
- lpPtr=(char *)lpImgData+sizeof(BITMAPINFOHEADER);
- lpTempPtr=(char *)lpTempImgData+sizeof(BITMAPINFOHEADER);
- hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));
- pPal =(LOGPALETTE *)LocalLock(hPal);
- pPal->palNumEntries =(WORD) NumColors;
- pPal->palVersion = 0x300;
- for (i = 0; i < NumColors; i++) {
- Blue=(unsigned char )(*lpPtr++);
- Green=(unsigned char )(*lpPtr++);
- Red=(unsigned char )(*lpPtr++);
- lpPtr++;
- pPal->palPalEntry[i].peRed=(BYTE)(255-Red);
- pPal->palPalEntry[i].peGreen=(BYTE)(255-Green);
- pPal->palPalEntry[i].peBlue=(BYTE)(255-Blue);
- pPal->palPalEntry[i].peFlags=0;
- *(lpTempPtr++)=(unsigned char)(255-Blue);
- *(lpTempPtr++)=(unsigned char)(255-Green);
- *(lpTempPtr++)=(unsigned char)(255-Red);
- *(lpTempPtr++)=0;
- }
- if(hPalette!=NULL)
- DeleteObject(hPalette);
- hPalette=CreatePalette(pPal);
- LocalUnlock(hPal);
- LocalFree(hPal);
- if(hPalette){
- hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
- RealizePalette(hDc);
- }
- }
- else{
- for(y=0;y<bi.biHeight;y++){
- lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
- lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes);
- for(x=0;x<bi.biWidth;x++){
- Blue=(unsigned char )(*lpPtr++);
- Green=(unsigned char )(*lpPtr++);
- Red=(unsigned char )(*lpPtr++);
- *(lpTempPtr++)=(unsigned char)(255-Blue);
- *(lpTempPtr++)=(unsigned char)(255-Green);
- *(lpTempPtr++)=(unsigned char)(255-Red);
- }
- }
- }
- if(hBitmap!=NULL)
- DeleteObject(hBitmap);
-
- hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
- (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
- (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
- if(hPalette && hPrevPalette){
- SelectPalette(hDc,hPrevPalette,FALSE);
- RealizePalette(hDc);
- }
- hf=_lcreat("c:\invert.bmp",0);
- _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER));
- _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
- _lclose(hf);
- ReleaseDC(hWnd,hDc);
- LocalUnlock(hTempImgData);
- LocalFree(hTempImgData);
- GlobalUnlock(hImgData);
- return TRUE;
- }
- ////////////////////////////////////////////////////////////////
- BOOL LimbPatternM3(HWND hWnd)
- {
- DWORD BufSize;
- LPBITMAPINFOHEADER lpImgData;
- LPSTR lpPtr;
- HLOCAL hTempImgData;
- LPBITMAPINFOHEADER lpTempImgData;
- LPSTR lpTempPtr;
- HDC hDc;
- HFILE hf;
- LONG x,y;
- unsigned char num;
- BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
- if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
- {
- MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
- lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
- //copy image data
- memcpy(lpTempImgData,lpImgData,BufSize);
- for(y=0;y<bi.biHeight;y++){
- lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
- lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes);
- for(x=0;x<bi.biWidth;x++){
- num=(unsigned char)*lpPtr++;
- if ( (num>>2) > BayerPattern[y&7][x&7])
- *(lpTempPtr++)=(unsigned char)255;
- else *(lpTempPtr++)=(unsigned char)0;
- }
- }
-
- if(hBitmap!=NULL)
- DeleteObject(hBitmap);
- hDc=GetDC(hWnd);
- hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
- (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
- (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
- hf=_lcreat("c:\limbm3.bmp",0);
- _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER));
- _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
- _lclose(hf);
- ReleaseDC(hWnd,hDc);
- LocalUnlock(hTempImgData);
- LocalFree(hTempImgData);
- GlobalUnlock(hImgData);
- return TRUE;
- }
- ////////////////////////////////////////////////////////////////
- BOOL LimbPatternM4(HWND hWnd)
- {
- DWORD BufSize;
- LPBITMAPINFOHEADER lpImgData;
- LPSTR lpPtr;
- HLOCAL hTempImgData;
- LPBITMAPINFOHEADER lpTempImgData;
- LPSTR lpTempPtr;
- HDC hDc;
- HFILE hf;
- LONG x,y;
- DWORD i,j;
- unsigned char num;
- BYTE M4Pattern[16][16];
- for(i=0;i<8;i++)
- for(j=0;j<8;j++)
- M4Pattern[i][j]=4*BayerPattern[i][j];
- for(i=8;i<16;i++)
- for(j=0;j<8;j++)
- M4Pattern[i][j]=4*BayerPattern[i-8][j]+2;
- for(i=0;i<8;i++)
- for(j=8;j<16;j++)
- M4Pattern[i][j]=4*BayerPattern[i][j-8]+3;
- for(i=8;i<16;i++)
- for(j=8;j<16;j++)
- M4Pattern[i][j]=4*BayerPattern[i-8][j-8]+1;
- BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
- if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
- {
- MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
- lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
- //copy image data
- memcpy(lpTempImgData,lpImgData,BufSize);
- for(y=0;y<bi.biHeight;y++){
- lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
- lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes);
- for(x=0;x<bi.biWidth;x++){
- num=(unsigned char)*lpPtr++;
- if ( num > M4Pattern[y&15][x&15])
- *(lpTempPtr++)=(unsigned char)255;
- else *(lpTempPtr++)=(unsigned char)0;
- }
- }
-
- if(hBitmap!=NULL)
- DeleteObject(hBitmap);
- hDc=GetDC(hWnd);
- hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
- (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
- (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
- hf=_lcreat("c:\limbm4.bmp",0);
- _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER));
- _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
- _lclose(hf);
- ReleaseDC(hWnd,hDc);
- LocalUnlock(hTempImgData);
- LocalFree(hTempImgData);
- GlobalUnlock(hImgData);
- return TRUE;
- }
- ////////////////////////////////////////////////////////////////
- BOOL Steinberg(HWND hWnd)
- {
- DWORD OffBits,BufSize,IntBufSize;
- LPBITMAPINFOHEADER lpImgData;
- HLOCAL hTempImgData;
- LPBITMAPINFOHEADER lpTempImgData;
- LPSTR lpPtr;
- LPSTR lpTempPtr;
- HDC hDc;
- HFILE hf;
- LONG x,y;
- unsigned char num;
- float e,f;
- HLOCAL hIntBuf;
- int *lpIntBuf,*lpIntPtr;
- int tempnum;
- OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
- BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
- if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
- {
- MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- IntBufSize=(DWORD)bi.biHeight*LineBytes*sizeof(int);
- if((hIntBuf=LocalAlloc(LHND,IntBufSize))==NULL)
- {
- MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- LocalFree(hTempImgData);
- return FALSE;
- }
- lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
- lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
- lpIntBuf=(int *)LocalLock(hIntBuf);
- //copy header data
- memcpy(lpTempImgData,lpImgData,OffBits);
- //copy image data to int buffer
- for(y=0;y<bi.biHeight;y++){
- lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
- lpIntPtr=(int *)lpIntBuf+(bi.biHeight-1-y)*LineBytes;
- for(x=0;x<bi.biWidth;x++)
- *(lpIntPtr++)=(unsigned char)*(lpPtr++);
- }
- for(y=0;y<bi.biHeight;y++){
- for(x=0;x<bi.biWidth;x++){
- lpIntPtr=(int *)lpIntBuf+(bi.biHeight-1-y)*LineBytes+x;
- num=(unsigned char)*lpIntPtr;
- if ( num > 128 ){
- *lpIntPtr=255;
- e=(float)(num-255.0);
- }
- else{
- *lpIntPtr=0;
- e=(float)num;
- }
- if(x<bi.biWidth-1){
- f=(float)*(lpIntPtr+1);
- f+=(float)( (3.0/8.0) * e);
- *(lpIntPtr+1)=(int)f;
- }
- if(y<bi.biHeight-1){
- f=(float)*(lpIntPtr-LineBytes);
- f+=(float)( (3.0/8.0) * e);
- *(lpIntPtr-LineBytes)=(int)f;
- f=(float)*(lpIntPtr-LineBytes+1);
- f+=(float)( (1.0/4.0) * e);
- *(lpIntPtr-LineBytes+1)=(int)f;
- }
- }
- }
- //copy new image data from int buffer
- for(y=0;y<bi.biHeight;y++){
- lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes);
- lpIntPtr=(int *)lpIntBuf+(bi.biHeight-1-y)*LineBytes;
- for(x=0;x<bi.biWidth;x++){
- tempnum=*(lpIntPtr++);
- if(tempnum>255) tempnum=255;
- else if (tempnum<0) tempnum=0;
- *(lpTempPtr++)=(unsigned char)tempnum;
- }
- }
-
- if(hBitmap!=NULL)
- DeleteObject(hBitmap);
- hDc=GetDC(hWnd);
- hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
- (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
- (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
- hf=_lcreat("c:\steinberg.bmp",0);
- _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER));
- _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
- _lclose(hf);
- ReleaseDC(hWnd,hDc);
- GlobalUnlock(hImgData);
- LocalUnlock(hTempImgData);
- LocalFree(hTempImgData);
- LocalUnlock(hIntBuf);
- LocalFree(hIntBuf);
- return TRUE;
- }
- ////////////////////////////////////////////////////////////////
- BOOL Bmp2Txt(HWND hWnd)
- {
- DWORD OffBits,BufSize;
- LPBITMAPINFOHEADER lpImgData;
- LPSTR lpPtr;
- HFILE hf;
- int i, j, k,h,tint,grayindex;
- char tchar;
- int TransHeight, TransWidth;
- for(i=0;i<94;i++)
- for(j=i+1;j<95;j++){
- if(gr[i]>gr[j]){
- tchar=ch[i],tint=gr[i];
- ch[i]=ch[j],gr[i]=gr[j];
- ch[j]=tchar,gr[j]=tint;
- }
- }
- OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
- BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
- lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
- TransWidth = bi.biWidth/8;
- TransHeight = bi.biHeight/16;
- hf=_lcreat("c:\bmp2txt.txt",0);
- for(i=0;i<TransHeight;i++){
- for(j=0;j<TransWidth;j++){
- grayindex=0;
- for(k=0;k<16;k++)
- for(h=0;h<8;h++){
- lpPtr=(char *)lpImgData+BufSize-LineBytes-(i*16+k)*LineBytes+j*8+h;
- grayindex+=(unsigned char)*lpPtr;
- }
- grayindex/=16*8;
- grayindex=gr[94]*grayindex/255;
- k=0;
- while(gr[k+1]<grayindex)
- k++;
- _lwrite(hf,(char *)&ch[k],sizeof(char));
- }
- tchar=(char)13;
- _lwrite(hf,(char *)&tchar,sizeof(char));
- tchar=(char)10;
- _lwrite(hf,(char *)&tchar,sizeof(char));
- }
- _lclose(hf);
- GlobalUnlock(hImgData);
- return TRUE;
- }
- ////////////////////////////////////////////////////////////////
- BOOL Histogram(HWND hWnd)
- {
- DWORD OffBits,BufSize;
- LPBITMAPINFOHEADER lpImgData;
- LPSTR lpPtr;
- int x,y;
- int grayindex;
- HWND hPopupWnd;
- int temp;
-
- for(grayindex=0;grayindex<256;grayindex++)
- GrayTable[grayindex]=0;
- OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
- BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
- lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
- for(y=0;y<bi.biHeight;y++){
- lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
- for(x=0;x<bi.biWidth;x++){
- grayindex=(unsigned char)*(lpPtr++);
- GrayTable[grayindex]++;
- }
- }
- MaxGrayNum=0;
- MinGrayNum=65535;
- for(grayindex=0;grayindex<256;grayindex++){
- temp=GrayTable[grayindex];
- if(temp>MaxGrayNum)
- MaxGrayNum=temp;
- if( (temp<MinGrayNum) && (temp>0) )
- MinGrayNum=temp;
- }
- GlobalUnlock(hImgData);
- hPopupWnd = CreateWindow (
- "PopupWindowClass",
- "Histogram Statistic Window",
- WS_OVERLAPPEDWINDOW,
- 50,80,550,350,
- hWnd,NULL,ghInst,NULL
- );
- if (hPopupWnd){
- ShowWindow (hPopupWnd, SW_SHOW);
- UpdateWindow (hPopupWnd);
- }
- return TRUE;
- }
- ////////////////////////////////////////////////////////////////
- long FAR PASCAL PopupWndProc (HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
- {
- HDC hdc;
- PAINTSTRUCT ps;
- DWORD i;
- int xstart;
- static LOGPEN blp={PS_SOLID,1,1,RGB(0,0,255)};
- HPEN bhp;
- float a,b,temp;
- char str[10];
-
- a=(float)(265.0 /( MaxGrayNum - MinGrayNum) );
- b=(float) (270.0-a* MaxGrayNum);
- switch (message)
- {
- case WM_PAINT:
- hdc = BeginPaint(hWnd, &ps);
- bhp = CreatePenIndirect(&blp);
- SelectObject(hdc,bhp);
- MoveToEx(hdc,2,270,NULL);
- LineTo(hdc,518,270);
- xstart=2;
- for(i=0;i<256;i++){
- MoveToEx(hdc,xstart,270,NULL);
- if (GrayTable[i]!=0)
- temp=(float)(a*GrayTable[i]+b);
- else temp=0.0f;
- LineTo(hdc,xstart,270-(int)temp);
- if (i%16 ==0){
- MoveToEx(hdc,xstart,270,NULL);
- LineTo(hdc,xstart,280);
- _itoa(i,str,10);
- TextOut(hdc,xstart,285,str,strlen(str));
- }
- xstart+=2;
- }
- MoveToEx(hdc,xstart,270,NULL);
- LineTo(hdc,xstart,280);
- TextOut(hdc,xstart,285,"256",strlen("256"));
- EndPaint(hWnd,&ps);
- DeleteObject(bhp);
- break;
- default:
- break;
- }
- return DefWindowProc (hWnd, message, wParam, lParam);
- }
- ////////////////////////////////////////////////////////////////
- BOOL Trueto256(HWND hWnd)
- {
- DWORD SrcBufSize,OffBits,DstBufSize,DstLineBytes;
- LPBITMAPINFOHEADER lpImgData;
- LPSTR lpPtr;
- HLOCAL hTempImgData;
- LPBITMAPINFOHEADER lpTempImgData;
- LPSTR lpTempPtr;
- HDC hDc;
- HFILE hf;
- LONG x,y;
- BITMAPFILEHEADER DstBf;
- BITMAPINFOHEADER DstBi;
- LOGPALETTE *pPal;
- HPALETTE hPrevPalette;
- HLOCAL hPal;
- WORD i,j;
- int Red,Green,Blue,ClrIndex;
- DWORD ColorHits[4096];
- WORD ColorIndex[4096];
- DWORD PalCounts,temp;
- long ColorError1,ColorError2;
- if(NumColors!=0){
- MessageBox(hWnd,"Must be a true color bitmap!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- DstLineBytes=(DWORD)WIDTHBYTES(bi.biWidth*8);
- DstBufSize=(DWORD)(sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD)+(DWORD)DstLineBytes*bi.biHeight);
- memcpy((char *)&DstBf,(char *)&bf,sizeof(BITMAPFILEHEADER));
- memcpy((char *)&DstBi,(char *)&bi,sizeof(BITMAPINFOHEADER));
- DstBf.bfSize=DstBufSize+sizeof(BITMAPFILEHEADER);
- DstBf.bfOffBits=(DWORD)(256*sizeof(RGBQUAD)+sizeof(BITMAPFILEHEADER)
- +sizeof(BITMAPINFOHEADER));
- DstBi.biClrUsed=0;
- DstBi.biBitCount=8;
- OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
- SrcBufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
-
- if((hTempImgData=LocalAlloc(LHND,DstBufSize))==NULL)
- {
- MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
- lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
- //copy image data
- memcpy(lpTempImgData,lpImgData,OffBits);
- //overwrite bitmapinfoheader with the new one
- memcpy(lpTempImgData,(char *)&DstBi,sizeof(BITMAPINFOHEADER));
- memset(ColorHits,0,4096*sizeof(DWORD));
- memset(ColorIndex,0,4096*sizeof(WORD));
- for(y=0;y<bi.biHeight;y++){
- lpPtr=(unsigned char *)lpImgData+(SrcBufSize-LineBytes-y*LineBytes);
- for(x=0;x<bi.biWidth;x++){
- Blue=(int)(*(lpPtr++) & 0xf0);
- Green=(int)(*(lpPtr++) & 0xf0);
- Red=(int)(*(lpPtr++) & 0xf0);
- ClrIndex=(Blue<<4) + Green +(Red >>4);
- ColorHits[ClrIndex]++;
- }
- }
- PalCounts=0;
- //pack the color table
- for (ClrIndex = 0; ClrIndex < 4096; ClrIndex++)
- {
- if(ColorHits[ClrIndex]!=0){
- ColorHits[PalCounts]=ColorHits[ClrIndex];
- ColorIndex[PalCounts]=ClrIndex;
- PalCounts++;
- }
- }
- //sort the color table downsize
- for (i = 0; i < PalCounts-1; i++)
- for (j = i + 1; j < PalCounts; j++){
- if (ColorHits[j] > ColorHits[i]){
- temp = ColorHits[i];
- ColorHits[i] = ColorHits[j];
- ColorHits[j] = temp;
- temp = ColorIndex[i];
- ColorIndex[i] = ColorIndex[j];
- ColorIndex[j] = (WORD)temp;
- }
- }
-
- hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + 256* sizeof(PALETTEENTRY));
- pPal =(LOGPALETTE *)LocalLock(hPal);
- pPal->palNumEntries =(WORD) 256;
- pPal->palVersion = 0x300;
- lpTempPtr=(char *)lpTempImgData+sizeof(BITMAPINFOHEADER);
- for (i = 0; i < 256; i++) {
- pPal->palPalEntry[i].peRed=(BYTE)((ColorIndex[i] & 0x00f) << 4);
- pPal->palPalEntry[i].peGreen=(BYTE)((ColorIndex[i] & 0x0f0));
- pPal->palPalEntry[i].peBlue=(BYTE)((ColorIndex[i] & 0xf00) >> 4);
- pPal->palPalEntry[i].peFlags=(BYTE)0;
- *(lpTempPtr++)=(unsigned char)((ColorIndex[i] & 0xf00) >> 4);
- *(lpTempPtr++)=(unsigned char)((ColorIndex[i] & 0x0f0));
- *(lpTempPtr++)=(unsigned char)((ColorIndex[i] & 0x00f) << 4);
- *(lpTempPtr++)=0;
- ColorHits[i]=i;
- }
- //其余的颜色依据最小平方差近似为前256中最接近的一种
- if (PalCounts > 256){
- for (i = 256; i < PalCounts; i++){
- ColorError1=1000000000;
- Blue = (long)((ColorIndex[i] & 0xf00) >> 4);
- Green = (long)((ColorIndex[i] & 0x0f0));
- Red = (long)((ColorIndex[i] & 0x00f) << 4);
- ClrIndex = 0;
- for (j = 0; j < 256; j++){
- ColorError2=(long)(Blue-pPal->palPalEntry[j].peBlue)*(Blue-pPal->palPalEntry[j].peBlue)+
- (long)(Green-pPal->palPalEntry[j].peGreen)*(Green-pPal->palPalEntry[j].peGreen)+
- (long)(Red-pPal->palPalEntry[j].peRed)*(Red-pPal->palPalEntry[j].peRed);
- if (ColorError2 < ColorError1){
- ColorError1 = ColorError2;
- ClrIndex = j;
- }
- }
- ColorHits[i] = ClrIndex;
- }
- }
- if(hPalette!=NULL)
- DeleteObject(hPalette);
-
- //create new logic palette
- hPalette=CreatePalette(pPal);
- LocalUnlock(hPal);
- LocalFree(hPal);
- hDc=GetDC(hWnd);
- if(hPalette){
- hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
- RealizePalette(hDc);
- }
- for(y=0;y<bi.biHeight;y++){
- lpPtr=(char *)lpImgData+(SrcBufSize-LineBytes-y*LineBytes);
- lpTempPtr=(char *)lpTempImgData+(DstBufSize-DstLineBytes-y*DstLineBytes);
- for(x=0;x<bi.biWidth;x++){
- Blue=(int)(*(lpPtr++) & 0xf0);
- Green=(int)(*(lpPtr++) & 0xf0);
- Red=(int)(*(lpPtr++) & 0xf0);
- ClrIndex=(Blue<<4) + Green +(Red >>4);
- for (i = 0; i < PalCounts;i++)
- if (ClrIndex == ColorIndex[i]){
- *(lpTempPtr++)=(unsigned char)ColorHits[i];
- break;
- }
- }
- }
- if(hBitmap!=NULL)
- DeleteObject(hBitmap);
-
- hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
- (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +256*sizeof(RGBQUAD),
- (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
- if(hPalette && hPrevPalette){
- SelectPalette(hDc,hPrevPalette,FALSE);
- RealizePalette(hDc);
- }
- hf=_lcreat("c:\256.bmp",0);
- _lwrite(hf,(LPSTR)&DstBf,sizeof(BITMAPFILEHEADER));
- _lwrite(hf,(LPSTR)lpTempImgData,DstBufSize);
- _lclose(hf);
- ReleaseDC(hWnd,hDc);
- LocalUnlock(hTempImgData);
- LocalFree(hTempImgData);
- GlobalUnlock(hImgData);
- return TRUE;
- }
- ////////////////////////////////////////////////////////////////
- BOOL ContrastStretch(HWND hWnd)
- {
- DLGPROC dlgInputBox = NULL;
- DWORD BufSize;
- LPBITMAPINFOHEADER lpImgData;
- LPSTR lpPtr;
- HLOCAL hTempImgData;
- LPBITMAPINFOHEADER lpTempImgData;
- LPSTR lpTempPtr;
- HDC hDc;
- HFILE hf;
- LOGPALETTE *pPal;
- HPALETTE hPrevPalette=NULL;
- HLOCAL hPal;
- DWORD i;
- unsigned char Gray;
- float a,g1,g2,g;
- if( NumColors!=256){
- MessageBox(hWnd,"Must be a 256 grayscale bitmap!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- dlgInputBox = (DLGPROC) MakeProcInstance ( (FARPROC)InputBox, ghInst );
- DialogBox (ghInst, "INPUTBOX", hWnd, dlgInputBox);
- FreeProcInstance ( (FARPROC) dlgInputBox );
- if( StretchRatio*(SecondPoint-FirstPoint) > 255.0){
- MessageBox(hWnd,"StretchRatio*(SecondPoint-FirstPoint) can not be larger than 255!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- if( (SecondPoint-FirstPoint) >=255){
- MessageBox(hWnd,"The area you selected can not be the whole scale!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- a=(float)((255.0-StretchRatio*(SecondPoint-FirstPoint))/(255.0-(SecondPoint-FirstPoint)));
- g1=a*FirstPoint;
- g2=StretchRatio*(SecondPoint-FirstPoint)+g1;
- BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
- if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
- {
- MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
- lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
- //copy image data
- memcpy(lpTempImgData,lpImgData,BufSize);
- hDc=GetDC(hWnd);
- lpPtr=(char *)lpImgData+sizeof(BITMAPINFOHEADER);
- lpTempPtr=(char *)lpTempImgData+sizeof(BITMAPINFOHEADER);
- hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));
- pPal =(LOGPALETTE *)LocalLock(hPal);
- pPal->palNumEntries =(WORD) NumColors;
- pPal->palVersion = 0x300;
- for (i = 0; i < 256; i++) {
- Gray=(unsigned char )*lpPtr;
- lpPtr+=4;
- if(Gray<FirstPoint) g=(float)(a*Gray);
- else if (Gray<SecondPoint) g=g1+StretchRatio*(Gray-FirstPoint);
- else g=g2+a*(Gray-SecondPoint);
- pPal->palPalEntry[i].peRed=(BYTE)g;
- pPal->palPalEntry[i].peGreen=(BYTE)g;
- pPal->palPalEntry[i].peBlue=(BYTE)g;
- pPal->palPalEntry[i].peFlags=0;
- *(lpTempPtr++)=(unsigned char)g;
- *(lpTempPtr++)=(unsigned char)g;
- *(lpTempPtr++)=(unsigned char)g;
- *(lpTempPtr++)=0;
- }
- if(hPalette!=NULL)
- DeleteObject(hPalette);
- hPalette=CreatePalette(pPal);
- LocalUnlock(hPal);
- LocalFree(hPal);
- if(hPalette){
- hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
- RealizePalette(hDc);
- }
- if(hBitmap!=NULL)
- DeleteObject(hBitmap);
-
- hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
- (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
- (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
- if(hPalette && hPrevPalette){
- SelectPalette(hDc,hPrevPalette,FALSE);
- RealizePalette(hDc);
- }
- hf=_lcreat("c:\stretch.bmp",0);
- _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER));
- _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
- _lclose(hf);
- ReleaseDC(hWnd,hDc);
- LocalUnlock(hTempImgData);
- LocalFree(hTempImgData);
- GlobalUnlock(hImgData);
- return TRUE;
- }
- ////////////////////////////////////////////////////////////////
- BOOL Cliping(HWND hWnd)
- {
- DLGPROC dlgInputBox = NULL;
- DWORD BufSize;
- LPBITMAPINFOHEADER lpImgData;
- LPSTR lpPtr;
- HLOCAL hTempImgData;
- LPBITMAPINFOHEADER lpTempImgData;
- LPSTR lpTempPtr;
- HDC hDc;
- HFILE hf;
- LOGPALETTE *pPal;
- HPALETTE hPrevPalette=NULL;
- HLOCAL hPal;
- DWORD i;
- unsigned char Gray;
- float a,g;
- if( NumColors!=256){
- MessageBox(hWnd,"Must be a 256 grayscale bitmap!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- dlgInputBox = (DLGPROC) MakeProcInstance ( (FARPROC)InputBox, ghInst );
- DialogBox (ghInst, "INPUTBOX", hWnd, dlgInputBox);
- FreeProcInstance ( (FARPROC) dlgInputBox );
- if( (SecondPoint-FirstPoint) >255){
- MessageBox(hWnd,"The area you selected must be within [0-255]!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- a=(float)(255.0/(SecondPoint-FirstPoint));
- BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
- if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
- {
- MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
- lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
- //copy image data
- memcpy(lpTempImgData,lpImgData,BufSize);
- hDc=GetDC(hWnd);
- lpPtr=(char *)lpImgData+sizeof(BITMAPINFOHEADER);
- lpTempPtr=(char *)lpTempImgData+sizeof(BITMAPINFOHEADER);
- hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));
- pPal =(LOGPALETTE *)LocalLock(hPal);
- pPal->palNumEntries =(WORD) NumColors;
- pPal->palVersion = 0x300;
- for (i = 0; i < 256; i++) {
- Gray=(unsigned char )*lpPtr;
- lpPtr+=4;
- if(Gray<FirstPoint) g=0.0f;
- else if (Gray<SecondPoint) g=a*(Gray-FirstPoint);
- else g=255.0f;
- pPal->palPalEntry[i].peRed=(BYTE)g;
- pPal->palPalEntry[i].peGreen=(BYTE)g;
- pPal->palPalEntry[i].peBlue=(BYTE)g;
- pPal->palPalEntry[i].peFlags=0;
- *(lpTempPtr++)=(unsigned char)g;
- *(lpTempPtr++)=(unsigned char)g;
- *(lpTempPtr++)=(unsigned char)g;
- *(lpTempPtr++)=0;
- }
- if(hPalette!=NULL)
- DeleteObject(hPalette);
- hPalette=CreatePalette(pPal);
- LocalUnlock(hPal);
- LocalFree(hPal);
- if(hPalette){
- hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
- RealizePalette(hDc);
- }
- if(hBitmap!=NULL)
- DeleteObject(hBitmap);
-
- hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
- (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
- (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
- if(hPalette && hPrevPalette){
- SelectPalette(hDc,hPrevPalette,FALSE);
- RealizePalette(hDc);
- }
- hf=_lcreat("c:\cliping.bmp",0);
- _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER));
- _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
- _lclose(hf);
- ReleaseDC(hWnd,hDc);
- LocalUnlock(hTempImgData);
- LocalFree(hTempImgData);
- GlobalUnlock(hImgData);
- return TRUE;
- }
- ////////////////////////////////////////////////////////////////
- BOOL Thresholding(HWND hWnd)
- {
- DLGPROC dlgInputBox = NULL;
- DWORD BufSize;
- LPBITMAPINFOHEADER lpImgData;
- LPSTR lpPtr;
- HLOCAL hTempImgData;
- LPBITMAPINFOHEADER lpTempImgData;
- LPSTR lpTempPtr;
- HDC hDc;
- HFILE hf;
- LOGPALETTE *pPal;
- HPALETTE hPrevPalette=NULL;
- HLOCAL hPal;
- DWORD i;
- unsigned char Gray;
- if( NumColors!=256){
- MessageBox(hWnd,"Must be a 256 grayscale bitmap!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- dlgInputBox = (DLGPROC) MakeProcInstance ( (FARPROC)InputBox, ghInst );
- DialogBox (ghInst, "INPUTBOX", hWnd, dlgInputBox);
- FreeProcInstance ( (FARPROC) dlgInputBox );
- if( FirstPoint >255){
- MessageBox(hWnd,"The point you selected must be within [0-255]","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
- if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
- {
- MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
- lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
- //copy image data
- memcpy(lpTempImgData,lpImgData,BufSize);
- hDc=GetDC(hWnd);
- lpPtr=(char *)lpImgData+sizeof(BITMAPINFOHEADER);
- lpTempPtr=(char *)lpTempImgData+sizeof(BITMAPINFOHEADER);
- hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));
- pPal =(LOGPALETTE *)LocalLock(hPal);
- pPal->palNumEntries =(WORD) NumColors;
- pPal->palVersion = 0x300;
- for (i = 0; i < 256; i++) {
- Gray=(unsigned char )*lpPtr;
- lpPtr+=4;
- if(Gray<FirstPoint) Gray=0;
- else Gray=255;
- pPal->palPalEntry[i].peRed=Gray;
- pPal->palPalEntry[i].peGreen=Gray;
- pPal->palPalEntry[i].peBlue=Gray;
- pPal->palPalEntry[i].peFlags=0;
- *(lpTempPtr++)=(unsigned char)Gray;
- *(lpTempPtr++)=(unsigned char)Gray;
- *(lpTempPtr++)=(unsigned char)Gray;
- *(lpTempPtr++)=0;
- }
- if(hPalette!=NULL)
- DeleteObject(hPalette);
- hPalette=CreatePalette(pPal);
- LocalUnlock(hPal);
- LocalFree(hPal);
- if(hPalette){
- hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
- RealizePalette(hDc);
- }
- if(hBitmap!=NULL)
- DeleteObject(hBitmap);
-
- hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
- (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
- (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
- if(hPalette && hPrevPalette){
- SelectPalette(hDc,hPrevPalette,FALSE);
- RealizePalette(hDc);
- }
- hf=_lcreat("c:\threshold.bmp",0);
- _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER));
- _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
- _lclose(hf);
- ReleaseDC(hWnd,hDc);
- LocalUnlock(hTempImgData);
- LocalFree(hTempImgData);
- GlobalUnlock(hImgData);
- return TRUE;
- }
- ////////////////////////////////////////////////////////////////
- BOOL Slice(HWND hWnd,BOOL WithBack)
- {
- DLGPROC dlgInputBox = NULL;
- DWORD BufSize;
- LPBITMAPINFOHEADER lpImgData;
- LPSTR lpPtr;
- HLOCAL hTempImgData;
- LPBITMAPINFOHEADER lpTempImgData;
- LPSTR lpTempPtr;
- HDC hDc;
- HFILE hf;
- LOGPALETTE *pPal;
- HPALETTE hPrevPalette=NULL;
- HLOCAL hPal;
- DWORD i;
- unsigned char Gray;
- if( NumColors!=256){
- MessageBox(hWnd,"Must be a 256 grayscale bitmap!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- dlgInputBox = (DLGPROC) MakeProcInstance ( (FARPROC)InputBox, ghInst );
- DialogBox (ghInst, "INPUTBOX", hWnd, dlgInputBox);
- FreeProcInstance ( (FARPROC) dlgInputBox );
- if( (SecondPoint-FirstPoint) >255){
- MessageBox(hWnd,"The area you selected must be within [0-255]!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
- if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
- {
- MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
- lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
- //copy image data
- memcpy(lpTempImgData,lpImgData,BufSize);
- hDc=GetDC(hWnd);
- lpPtr=(char *)lpImgData+sizeof(BITMAPINFOHEADER);
- lpTempPtr=(char *)lpTempImgData+sizeof(BITMAPINFOHEADER);
- hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));
- pPal =(LOGPALETTE *)LocalLock(hPal);
- pPal->palNumEntries =(WORD) NumColors;
- pPal->palVersion = 0x300;
- for (i = 0; i < 256; i++) {
- Gray=(unsigned char )*lpPtr;
- lpPtr+=4;
- if(WithBack){
- if( (Gray>=FirstPoint) && (Gray<=SecondPoint))
- Gray=255;
- }
- else{
- if(Gray<FirstPoint) Gray=0;