📄 winimage.c
字号:
#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
#ifdef Capture
#define GX 0
#define GY 0
#define DX 768
#define DY 512
#endif
void WindowInitial(HINSTANCE hInstance,HINSTANCE hPrevInstance,int nCmdShow);
// 窗口主函数初始化程序
long FAR PASCAL WndProc(HWND,WORD,WORD,LONG);
int OpenCard(HWND hWnd,int x,int y,int *Dx,int *Dy);
void CloseCard(void);
void SetSize(HWND hWnd,int x,int y,int Dx,int Dy);
void CaptToVideo(int port);
void CaptToBuffer(int port);
void CaptPause(void);
void CaptStop(void);
void VideoToDib(BYTE* lpbits,int Dx,int Dy,int Bits);
void BufferToDib(BYTE* lpbits,int Dx,int Dy,int Bits);
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 CaptX,CaptY;
int Capt_flag;
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,
"张任奇--图象边缘检测程序", //窗口标题
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_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_SAVE: // 存储图象
if (Image[0]->hdib) {
CMUSaveFile(hWnd,"SAVEFILENAME");
if (strlen(szName))
ImageSave(Image[1],szName);
}
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_EXIT: // 退出
SendMessage(hWnd,WM_CLOSE,0,0L);
return 0;
case IDM_CLEARA:
ClearA(hWnd);
return 0;
case IDM_CLEAR:
Clear(hWnd);
return 0;
case IDM_EX3:
EX3(hWnd);
return 0;
case IDM_EX4:
EX4(hWnd);
return 0;
case IDM_EX5:
EX5(hWnd);
return 0;
case IDM_EX6:
EX6(hWnd);
return 0;
case IDM_EX7:
EX7(hWnd);
return 0;
case IDM_EX8:
EX8(hWnd);
return 0;
case IDM_EX9:
EX9(hWnd);
return 0;
case IDM_EX10:
EX10(hWnd);
return 0;
case IDM_EX11:
EX11(hWnd);
return 0;
case IDM_EX12:
EX12(hWnd);
return 0;
case IDM_EX13:
EX13(hWnd);
return 0;
case IDM_EX:
Ex(hWnd);
return 0;
case IDM_EX2:
EX2(hWnd);
return 0;
case IDM_EX14:
EX14(hWnd);
return 0;
case IDM_EX15:
EX15(hWnd);
return 0;
case IDM_EX16:
EX16(hWnd);
return 0;
case IDM_EX17:
EX17(hWnd);
return 0;
case IDM_EX18:
EX18(hWnd);
return 0;
case IDM_EX19:
EX19(hWnd);
return 0;
case IDM_EX20:
EX20(hWnd);
return 0;
}
break ;
#ifdef Capture
case WM_CLOSE:
if (Capt_flag)
CloseCard();
break;
#endif
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 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);
setimage(image,i ,0,1,hei,buff2);
setimage(image,wid-1-i,0,1,hei,buff1);
}
}
if ((n&0x2)==2) {
for (i=0;i<hei/2;i++) {
getimage(image,0,i ,wid,1,buff1);
getimage(image,0,hei-1-i,wid,1,buff2);
setimage(image,0,i ,wid,1,buff2);
setimage(image,0,hei-1-i,wid,1,buff1);
}
}
free(buff1);
free(buff2);
WriteDDB(image,0,0,0,0,wid,hei);
InvalidateRect(hwnd,NULL,FALSE);
}
void ImageBrowse(int x,int y) // 鼠标漫游与移动
{
int f=0;
if ((175<x)&&(x<195)&&(wid-pic_x>195)) {
pic_x+=3; f=1;
}
else if ((x<20)&&(wid>198)&&(wid-pic_x>0)) {
pic_x-=3; f=1;
}
if ((260<y)&&(y<280)&&(hei-pic_y>280)) {
pic_y+=3; f=1;
}
else if ((y<20)&&(hei>283)&&(hei-pic_y>0)) {
pic_y-=3; f=1;
}
if (pic_x+195>wid) pic_x=wid-195;
if (pic_x<0) pic_x=0;
if (pic_y+280>hei) pic_y=hei-280;
if (pic_y<0) pic_y=0;
if (f==1) {
WriteDDB(Image[0], 0, 0,pic_x,pic_y,195,270);
WriteDDB(Image[1],200, 0,pic_x,pic_y,195,270);
WriteDDB(Image[2],400, 0,pic_x,pic_y,195,270);
WriteDDB(Image[3],600, 0,pic_x,pic_y,195,270);
WriteDDB(Image[4], 0,275,pic_x,pic_y,195,270);
WriteDDB(Image[5],200,275,pic_x,pic_y,195,270);
WriteDDB(Image[6],400,275,pic_x,pic_y,195,270);
WriteDDB(Image[7],600,275,pic_x,pic_y,195,270);
}
InvalidateRect(hwnd,NULL,FALSE);
}
void ColorToGray(int m,int n) // 变灰阶
{
BYTE *buff,*p,gray[256];
int i,j,k,r,g,b;
if (Image[m]->hdib==NULL) return;
ImageCreate(Image[n],wid,hei,8);
buff=(BYTE*) malloc(Image[m]->bpl);
for (i=0;i<256;i++) {
setpalette(Image[n],i,i,i,i);
}
if (Image[m]->bitcount==8) {
for (i=0;i<256;i++) {
getpalette(Image[m],i,&r,&g,&b);
gray[i]=(BYTE) ((30*r+59*g+11*b+50)/100);
}
for (i=0;i<hei;i++) {
getimage(Image[m],0,i,wid,1,buff);
for (j=0;j<wid;j++) {
buff[j]=gray[buff[j]];
}
setimage(Image[n],0,i,wid,1,buff);
}
}
else if (Image[m]->bitcount==24) {
p=buff;
for (i=0;i<hei;i++) {
getimage(Image[m],0,i,wid,1,buff);
for (j=0,k=0;j<wid;j++,k+=3) {
buff[j]=(BYTE) ((11*p[k]+59*p[k+1]+30*p[k+2]+50)/100);
}
setimage(Image[n],0,i,wid,1,buff);
}
}
free(buff);
}
void ImageRotate(struct IMAGE *img1,struct IMAGE *img2,double alpha)
{
int i,j,x,y,ww,r,g,b;
int Yd,Dx,Dy,bpl,bpp,bits,max,maxn;
double centerx,centery,sintheta,costheta;
double X1,Y1,X2,Y2,theta,xx,yy,rr;
BYTE *sc,*p;
char **list;
Dx=img1->wid; Dy=img1->hei;
bpl=(int) img1->bpl;
bpp=(int) img1->bpp;
bits=img1->bitcount;
sc=(BYTE*) malloc(2*bpl);
list=(char**)malloc(Dy*sizeof(char*));
for (i=0;i<Dy;i++) list[i]=img1->lpbits+(Dy-1-i)*bpl;
centerx=Dx/2; centery=Dy/2;
rr=sqrt(centerx*centerx+centery*centery);
theta=atan((double) centery/(double) centerx);
X1=fabs(rr*cos(alpha+theta));
Y1=fabs(rr*sin(alpha+theta));
X2=fabs(rr*cos(alpha-theta));
Y2=fabs(rr*sin(alpha-theta));
if (X2>X1) X1=X2;
if (Y2>Y1) Y1=Y2;
ww=(int) (2*X1);
ImageCreate(img2,ww,(int) (2*Y1),bits);
if (bits==8) {
max=maxn=0;
for (i=0;i<256;i++) {
getpalette(img1,i,&r,&g,&b);
setpalette(img2,i,r,g,b);
if (r+g+b>max) {
max=r+g+b; maxn=i;
}
}
}
sintheta=sin(alpha);
costheta=cos(alpha);
for (j=(int)(centery-Y1),Yd=0;j<=centery+Y1;j++,Yd++) {
if (bits==8)
memset(sc,maxn,ww);
else
memset(sc,220,ww*bpp);
for (i=(int)(centerx-X1),p=sc;i<=centerx+X1;i++,p+=bpp) {
xx=centerx+costheta*(i-centerx)+sintheta*(j-centery);
yy=centery-sintheta*(i-centerx)+costheta*(j-centery);
x=(int) xx; y=(int) yy;
if ((x>=0)&&(x<Dx)&&(y>=0)&&(y<Dy)) {
memcpy(p,&list[y][bpp*x],bpp);
}
}
setimage(img2,0,Yd,ww,1,sc);
}
free(list);
free(sc);
}
void Clear(HWND hWnd)
{
int i;
for (i=0;i<9;i++)
if (Image[i]->hdib) ImageClose(Image[i]);
ClearDDB();
InvalidateRect(hWnd,NULL,FALSE);
}
void ClearA(HWND hWnd)
{
InvalidateRect(hWnd,NULL,FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -