📄 winimage.c
字号:
/********************************************
WINIMAGE.C -- 演示程序主程序和窗口函数源程序
华东理工大学信息学院 陆宗骐 编制
2004.03.24
********************************************/
#include <stdio.h>
#include <string.h>
#include <dos.h>
#include <math.h>
#include <windows.h>
#include <commdlg.h>
#include "WinImage.h"
#include "image32.h"
#define PI 3.14159265
void WindowInitial(HINSTANCE hInstance,HINSTANCE hPrevInstance,int nCmdShow);
// 窗口主函数初始化程序
long FAR PASCAL WndProc(HWND,WORD,WORD,LONG);
char szAppName [] = "WinImage";
extern char szName[256];
extern struct IMAGE *image;
extern int nHorz,nVert,nPal_f,nBits,nColors;
extern HDC hMemDC;
extern HBITMAP hBitmap;
extern LOGPEN lppen[];
struct IMAGE *Image[10];
HWND hwnd;
int wid,hei;
int pic_x,pic_y;
int T_flag;
struct DOT pxy[5];
int pxy_n;
int capt_tab[5][1500];
int pxy_f=0;
COLORREF color_tab[8]={ RGB(255,0,0),RGB(255,255,0),
RGB(0,255,0),RGB(0,255,255),
RGB(0,0,255),RGB(255,0,255),
RGB(255,255,255),RGB(0,0,0)};
int PASCAL WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpszCmdLine,int nCmdShow)
{
MSG msg;
if (lpszCmdLine==0) return 0;
WindowInitial(hInstance,hPrevInstance,nCmdShow);
while(GetMessage(&msg,NULL,0,0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
void WindowInitial(HINSTANCE hInstance,
HINSTANCE hPrevInstance,int nCmdShow)
{
HDC hDC;
HWND hWnd;
WNDCLASS wndclass;
if (!hPrevInstance) {
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = (WNDPROC)WndProc;
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = szAppName;
wndclass.lpszClassName = szAppName;
RegisterClass(&wndclass);
}
hDC = GetDC(NULL);
GetSysParam(hDC);
ReleaseDC(NULL,hDC);
hWnd = CreateWindow(szAppName,
"Windows C 图像处理演示程序",
WS_OVERLAPPEDWINDOW | WS_MAXIMIZEBOX | WS_MAXIMIZE,
0,0,
nHorz,nVert-30,
NULL,NULL,hInstance,NULL);
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
}
long FAR PASCAL WndProc(HWND hWnd,WORD message,
WORD wParam,LONG lParam)
{
int i,k;
PAINTSTRUCT ps;
HMENU hMenu;
hwnd=hWnd;
hMenu = GetMenu (hwnd) ;
switch(message) {
case WM_CREATE:
strcpy(szName,"");
for (i=0;i<10;i++) {
Image[i]=(struct IMAGE * ) malloc(sizeof(struct IMAGE));
Image[i]->hdib=NULL;
}
hBitmap=CreateDDB(hWnd,nHorz,nVert);
ClearDDB();
image=Image[0];
return 0L;
case WM_MOUSEMOVE:
MouseMove(hWnd,message,wParam,lParam);
return 0L;
case WM_LBUTTONDOWN:
LButtonDown(hWnd,message,wParam,lParam);
return 0L;
case WM_LBUTTONUP:
LButtonUp(hWnd,message,wParam,lParam);
return 0L;
case WM_COMMAND:
T_flag=0;
strcpy(szName,"");
switch (wParam) {
/*** 图象文件菜单 ***/
case IDM_OPEN: // 打开图象文件
CMUFileOpen(hWnd,"OPENFILENAME");
if (strlen(szName)) {
ClearDDB();
k=ImageOpen(image,szName);
if (k==2) {
MessageBox(hWnd,"本软件不支持压缩模式!","警 告",MB_OK);
return 0;
};
wid=image->wid;
hei=image->hei;
for (i=1;i<9;i++)
if (Image[i]->hdib) ImageClose(Image[i]);
ImageCopy(0,9);
WriteDDB(Image[0],0,0,0,0,wid,hei);
InvalidateRect(hWnd,NULL,FALSE);
}
return 0;
case IDM_REFRESH: // 图象复原
if (Image[9]->hdib) {
ClearDDB();
ImageCopy(9,0);
wid=Image[0]->wid;
hei=Image[0]->hei;
WriteDDB(Image[0],0,0,0,0,wid,hei);
InvalidateRect(hWnd,NULL,FALSE);
}
return 0;
case IDM_SAVE: // 存储图象
if (Image[0]->hdib) {
CMUSaveFile(hWnd,"SAVEFILENAME");
if (strlen(szName))
ImageSave(Image[0],szName);
}
return 0;
case IDM_POINT: // 单点数据
if ((Image[0]->hdib==NULL)||(image->bitcount<8)) return 1;
SendMessage(hWnd, WM_COMMAND,IDM_REFRESH,0L);
T_flag=wParam;
return 0;
case IDM_LINE: // 线段数据
if ((Image[0]->hdib==NULL)||(image->bitcount<8)) return 1;
SendMessage(hWnd, WM_COMMAND,IDM_REFRESH,0L);
T_flag=wParam;
pxy_n=0; pxy_f=0;
return 0;
case IDM_WINCAPT: // 截取窗口
if (Image[0]->hdib==NULL) return 1;
T_flag=wParam;
pxy_n=0; pxy_f=0;
return 0;
case IDM_DEVICEPARAM: // 显示设备参数
DisplayDeviceParam(hWnd);
return 0;
/*** 几何变换 ***/
case IDM_FLIP_H: // 水平镜像
if (Image[0]->hdib==NULL) return 1;
Flip(1);
return 0;
case IDM_FLIP_V: // 垂直镜像
if (Image[0]->hdib==NULL) return 1;
Flip(2);
return 0;
case IDM_ROTATE180: // 转180度
if (Image[0]->hdib==NULL) return 1;
Flip(3);
return 0;
case IDM_ROTATE30: // 转180度
if (Image[0]->hdib==NULL) return 1;
ImageRotate(image,Image[1],PI*30/180);
if (wid<350) {
WriteDDB(Image[1],wid+10,0,0,0,nHorz,nVert);
}
else {
ClearDDB();
WriteDDB(Image[1],0,0,0,0,nHorz,nVert);
}
InvalidateRect(hwnd,NULL,FALSE);
return 0;
case IDM_MOLTIPICTURE: // 显示 RGB 分量
if (Image[0]->hdib==NULL) return 1;
MultiPictureDisp();
return 0;
case IDM_IMAGEBROWSE: // 显示 RGB 分量
if (Image[0]->hdib==NULL) return 1;
T_flag=wParam;
pic_x=pic_y=0;
return 0;
case IDM_EXIT: // 退出
SendMessage(hWnd,WM_CLOSE,0,0L);
return 0;
case IDM_ABOUT: // 版权信息
MessageBox(hWnd,"华东理工大学信息学院 陆宗骐 编制","版权信息",MB_OK);
return 0;
case IDM_GRAPH1:
Graph1(hWnd);
return 0;
case IDM_GRAPH2:
Graph2(hWnd);
return 0;
case IDM_GRAPH3:
Graph3(hWnd);
return 0;
case IDM_GRAPH4:
Graph4(hWnd);
return 0;
case IDM_GRAPH5:
Graph5(hWnd);
return 0;
case IDM_GRAPH6:
Graph6(hWnd);
return 0;
case IDM_CLEARA:
ClearA(hWnd);
return 0;
case IDM_CLEAR:
Clear(hWnd);
return 0;
case IDM_GRAPHB1:
GraphB1(hWnd);
return 0;
case IDM_GRAPHB2:
GraphB2(hWnd);
return 0;
case IDM_GRAPHB3:
GraphB3(hWnd);
return 0;
case IDM_GRAPHB4:
GraphB4(hWnd);
return 0;
case IDM_GRAPHB5:
GraphB5(hWnd);
return 0;
case IDM_GRAPHB6:
GraphB6(hWnd);
return 0;
case IDM_PROJECT:
Project(hWnd);
return 0;
case IDM_LAPLACIANSKETCH:
LaplacianSketch(hWnd);
return 0;
case IDM_THRESHOLD:
Threshold(hWnd);
return 0;
case IDM_CONTOURTRACK:
ContourTrack(hWnd);
return 0;
case IDM_LINETHINNING:
LineThinning(hWnd);
return 0;
}
break ;
case WM_PAINT:
BeginPaint(hWnd,&ps);
DisplayDDB(hWnd);
EndPaint(hWnd,&ps);
break;
case WM_DESTROY:
for (i=0;i<10;i++) {
if (Image[i]->hdib) ImageClose(Image[i]);
free(Image[i]);
}
DeleteDDB(hBitmap);
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd,message,wParam,lParam);
}
void FillDdbWin(int x,int y,int Dx,int Dy,
COLORREF clrref1,COLORREF clrref2)
{
LOGPEN lppena = {PS_SOLID,1,1,RGB(0,0,0)};
HPEN hPen;
HBRUSH hBrush;
hMemDC=CreateMemDC();
lppena.lopnColor=clrref1;
hPen=(HPEN) SelectObject(hMemDC,CreatePenIndirect(&lppena));
hBrush=(HBRUSH) SelectObject(hMemDC,CreateSolidBrush(clrref2));
Rectangle(hMemDC,x,y,x+Dx,y+Dy);
DeleteObject(SelectObject(hMemDC,hPen));
DeleteObject(SelectObject(hMemDC,hBrush));
DeleteMemDC(hMemDC);
}
int LineCoor(int x1,int y1,int x2,int y2,struct DOT *dot)
{
int t,dis,dx,dy,xerr=1,yerr=1,incx,incy;
dx=x2-x1; dy=y2-y1;
if (dx>0) incx=1;
else if (dx==0) incx=0;
else incx=-1;
if (dy>0) incy=1;
else if (dy==0) incy=0;
else incy=-1;
dx=abs(dx); dy=abs(dy);
if (dx>dy) dis=dx;
else dis=dy;
for (t=0;t<dis+1;t++) {
dot->x=x1; dot->y=y1;
dot++;
xerr+=dx; yerr+=dy;
if (xerr>dis) {
xerr-=dis; x1+=incx;
}
if (yerr>dis) {
yerr-=dis; y1+=incy;
}
}
return(dis+1);
}
void DrawLine(int x1,int y1,int x2,int y2,COLORREF clrref)
{
int i,n,dx,dy;
struct DOT *dot;
hMemDC=CreateMemDC();
dx=abs(x1-x2); dy=abs(y1-y2);
dot=(struct DOT *) malloc(sizeof(struct DOT)*(dx+dy+2));
n=LineCoor(x1,y1,x2,y2,dot);
for (i=0;i<n;i++) {
SetPixel(hMemDC,dot[i].x,dot[i].y,clrref);
}
free(dot);
DeleteMemDC(hMemDC);
}
void LineData(int k,int flag)
{
int i,n,dx,dy;
struct DOT *dot;
int x1,y1,x2,y2;
COLORREF clrref;
x1=pxy[k-1].x; y1=pxy[k-1].y;
x2=pxy[k].x; y2=pxy[k].y;
hMemDC=CreateMemDC();
dx=abs(x1-x2); dy=abs(y1-y2);
dot=(struct DOT *) malloc(sizeof(struct DOT)*(dx+dy+2));
n=LineCoor(x1,y1,x2,y2,dot);
for (i=0;i<n;i++) {
if (flag==0) {
clrref=GetPixel(hMemDC,dot[i].x,dot[i].y);
capt_tab[k][i]=(DWORD) clrref;
}
else {
clrref=capt_tab[k][i];
SetPixel(hMemDC,dot[i].x,dot[i].y,clrref);
}
}
free(dot);
DeleteMemDC(hMemDC);
}
void DisplayDeviceParam(HWND hWnd)
{
char StringT[30];
ClearDDB();
hMemDC=CreateMemDC();
TextOut(hMemDC,165,40,"当前设备参数",12);
sprintf(StringT,"%5d",nHorz);
TextOut(hMemDC,100,90,"水平分辨率",10);
TextOut(hMemDC,300,90,StringT,strlen(StringT));
sprintf(StringT,"%5d",nVert);
TextOut(hMemDC,100,120,"垂直分辨率",10);
TextOut(hMemDC,300,120,StringT,strlen(StringT));
sprintf(StringT,"%5d",nBits);
TextOut(hMemDC,100,150,"每像素位数",10);
TextOut(hMemDC,300,150,StringT,strlen(StringT));
if (nPal_f) {
sprintf(StringT,"%5d",nColors);
TextOut(hMemDC,100,200,"总彩色数",8);
TextOut(hMemDC,300,200,StringT,strlen(StringT));
}
else {
if (nBits<24)
TextOut(hMemDC,100,200,"高彩色显示模式",14);
else
TextOut(hMemDC,100,200,"真彩色显示模式",14);
}
sprintf(StringT,"%5d",(nBits*nHorz+15)/16*2);
TextOut(hMemDC,100,230,"每行字节数",10);
TextOut(hMemDC,300,230,StringT,strlen(StringT));
DeleteMemDC(hMemDC);
InvalidateRect(hWnd,NULL,FALSE);
}
void WinCapt(HWND hWnd,struct IMAGE* image,int x,int y,int Dx,int Dy)
{
HDC hDC,hMemDC_n;
HBITMAP hBitmap_n;
hDC = GetDC(hWnd);
hMemDC =CreateMemDC();
hMemDC_n =CreateCompatibleDC(hDC);
hBitmap_n=CreateCompatibleBitmap(hDC,Dx,Dy);
SelectObject(hMemDC_n,hBitmap_n);
BitBlt(hMemDC_n,0,0,Dx,Dy,hMemDC,x,y,SRCCOPY);
ImageCreate(image,Dx,Dy,24);
GetDIBits(hMemDC_n,hBitmap_n,0,Dy,image->lpbits,
image->lpbi,DIB_RGB_COLORS);
DeleteObject(hBitmap_n);
DeleteObject(hMemDC_n);
DeleteDC(hMemDC_n);
ReleaseDC(hWnd,hDC);
DeleteMemDC(hMemDC);
}
void ImageCopy(int m,int n) // 图象复制
{
long num,size1,size2;
int wid,hei,bpl,bits;
if (Image[m]->hdib==NULL) return;
wid =Image[m]->wid; hei =Image[m]->hei;
bpl =Image[m]->bpl; bits=Image[m]->bitcount;
ImageCreate(Image[n],wid,hei,bits);
if (bits > 8) num=0;
else num = 1 << bits;
size1 = 40 + 4*num;
size2 = (long) bpl * hei;
memcpy(Image[n]->lpbi,Image[m]->lpbi,size1);
memcpy(Image[n]->lpbits,Image[m]->lpbits,size2);
}
void Flip(int n) // 镜像
{
int i,bpp,bpl,num;
BYTE *buff1,*buff2;
ClearDDB();
image=Image[0];
bpp=image->bpp;
bpl=image->bpl;
num=(hei*bpp+3)/4*4;
if (num<bpl) num=bpl;
buff1=(BYTE*) malloc(num);
buff2=(BYTE*) malloc(num);
if ((n&0x1)==1) {
for (i=0;i<wid/2;i++) {
getimage(image,i ,0,1,hei,buff1);
getimage(image,wid-1-i,0,1,hei,buff2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -