#include <stdio.h>
#include <string.h>
#include <dos.h>
#include <math.h>
#include <windows.h>
#include <commdlg.h>
#include "image32.h"
#include "imgdem32.h"
#define PI 3.14159265
long FAR PASCAL WndProc(HWND,WORD,WORD,LONG);
void zoom(int x,int y);
void DrawK(int x,int y,COLORREF clrref);
int next_D(int *first,int *current,int *next,int *iop,int u);
int track_B(int i,int j,int iop,int *dot,int u);
int segment(int *dot,int *dot10,int *dot11);
void fillarea(int *dot10,int *dot11,int dot_n1,int g);
int perimeter(int *dot);
int area(int *dot10,int dot_n1);
double form_factor(int fff,int sss);
void REFEA(int x,int y,int Dx,int Dy);
void DrawContour(int *dot,COLORREF clrref);
void Trans_1to8(struct IMAGE *image,int No);
void Trans_4cto8(struct IMAGE *image,int No);
void dilate_erose(struct IMAGE * image,int x,int y,
int Dx,int Dy,int flag,int connect,int time);
void draw_border(struct IMAGE * image,int x,int y,
int Dx,int Dy,int flag,int connect);
void Hilditch(struct IMAGE * image,int x,int y,int Dx,int Dy);
int warning1(HWND hWnd);
int warning2(HWND hWnd);
void FillWindow(int x,int y,int Dx,int Dy,COLORREF clrref1,COLORREF clrref2);
void ImageCopy(int m,int n);
int LineCoor(int x,int y,int x1,int y1,POINT *dot);
void DrawLine(int x1,int y1,int x2,int y2,COLORREF clrref);
void PopLineData(int x1,int y1,int x2,int y2);
void Scale_z(double alpha);
void imagerotate(struct IMAGE * image1,struct IMAGE * image2,double theta,int sljx,int sljy);
char szAppName [] = "ImageDemo";
extern char szName[256];
extern struct IMAGE *image;
struct IMAGE *Image[10];
HWND hwnd;
int wid,hei;
int op_flag=0,GrayMax,NO;
COLORREF crColor;
int mouse_flag,pic_x,pic_y;
int T_flag,T_h,T_s,T_v,T_f,B_f;
int cur_x,cur_y,cur_f=0,copy_f;
int hsv_f;
int J_x=520,J_y1=20,J_y2=280;
int Timer_n,Timer_f;
int pxy_n,branch_n,b_Dx,b_Dy;
POINT pxy[5],branch[2];
int max_dotn=3000;
int fff,number,fmax,fmin;
long sss;
double yuand;
int *dot,dot_n,*dot10,*dot11,dot_n1;
int inc[8][2] = { +1, 0,+1,+1, 0,+1,-1,+1,
-1, 0,-1,-1, 0,-1,+1,-1 };
HPEN hPen;
HBRUSH hBrush;
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(HANDLE hInstance,HANDLE hPrevInstance,
LPSTR lpszCmdLine,int nCmdShow)
{
HWND hWnd;
MSG msg;
WNDCLASS wndclass;
if (lpszCmdLine==0) return 0;
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 = GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = szAppName;
wndclass.lpszClassName = szAppName;
RegisterClass(&wndclass);
}
hWnd = CreateWindow(szAppName,
"图 像 显 示 及 处 理 演 示 程 序",
WS_OVERLAPPEDWINDOW | WS_MAXIMIZEBOX,
0,0,
800,600,
NULL,NULL,hInstance,NULL);
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&msg,NULL,0,0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
long FAR PASCAL WndProc(HWND hWnd,WORD message,
WORD wParam,LONG lParam)
{
PAINTSTRUCT ps;
int i,j,k,r,g,b,coor_x,coor_y;
HDC hdc;
HMENU hmenu;
char StringT[20];
COLORREF clrref;
HPEN hPen;
BYTE buff[4096];
DWORD lll;
LOGPEN lppen = {PS_SOLID,1,1,RGB(0,0,0)};
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;
}
image=Image[0];
hwnd = hWnd;
Timer_f=0;
NO=0;
dot=malloc(sizeof(int)*max_dotn);
dot10=malloc(sizeof(int)*max_dotn);
dot11=malloc(sizeof(int)*max_dotn);
return 0L;
case WM_LBUTTONDOWN:
if (image->hdib) {
coor_x=LOWORD(lParam);
coor_y=HIWORD(lParam);
if ((T_flag==IDM_PASTE)&&(copy_f==1)) { // 区域粘贴处理
disp_image(hwnd,Image[8],coor_x,coor_y,0,0,b_Dx,b_Dy);
for (k=0;k<b_Dy;k++) {
get_image(Image[8],0,k,b_Dx,1,buff);
set_image(Image[0],coor_x,coor_y+k,b_Dx,1,buff);
}
return 0;
}
if ((coor_x+coor_y==0)||(coor_x>=wid)||(coor_y>=hei))
return 0;
if (T_flag==IDM_POINT) { // 单点数据处理
lll=get_pixel(Image[0],coor_x,coor_y);
if (Image[0]->bitcount==8) {
get_palette(Image[0],(int)lll,&r,&g,&b);
clrref=RGB(r,g,b);
}
else {
clrref=DWORD2CLRREF(lll);
r=(lll>>16)&0xff; g=(lll>>8)&0xff;
b=lll&0xff;
}
FillWindow(680,45,60,60,RGB(0,0,0),clrref);
FillWindow(680,120,80,100,RGB(255,255,255),RGB(255,255,255));
hdc = GetDC(hwnd);
if ((r==g)&&(r==b)) {
sprintf(StringT,"g %d",r);
TextOut(hdc,690,120,StringT,strlen(StringT));
}
else {
sprintf(StringT,"r %d",r);
TextOut(hdc,690,120,StringT,strlen(StringT));
sprintf(StringT,"g %d",g);
TextOut(hdc,690,140,StringT,strlen(StringT));
sprintf(StringT,"b %d",b);
TextOut(hdc,690,160,StringT,strlen(StringT));
}
ReleaseDC(hwnd,hdc);
}
else if (T_flag==IDM_LINE) { // 线段数据处理
if (branch_n==0) {
branch[0].x=coor_x; branch[0].y=coor_y;
cur_x=coor_x; cur_y=coor_y;
branch_n=1;
}
cur_f=1;
DrawLine(coor_x-3,coor_y ,coor_x+3,coor_y ,
color_tab[2]);
DrawLine(coor_x ,coor_y-3,coor_x ,coor_y+3,
color_tab[2]);
}
else if (T_flag==IDM_SELECT) { // 选择区域起点处理
if (pxy_n==0) {
pxy[0].x=coor_x; pxy[0].y=coor_y;
pxy_n=1;
branch[0].x=coor_x; branch[0].y=coor_y;
cur_x=coor_x; cur_y=coor_y;
}
return 0;
}
else if ((mouse_flag==2)&&(coor_x<195)&&(coor_y<270)) {
zoom(coor_x,coor_y);
}
else if (T_flag==IDM_L8) {
DrawK(coor_x,coor_y,RGB(255,0,255));
}
else if (T_flag==IDM_L9) {
DrawK(coor_x,coor_y,RGB(255,255,0));
}
else if (T_flag==IDM_L10) {
DrawK(coor_x,coor_y,RGB(255,255,255));
}
else if (T_flag==IDM_TRACK) { // 手动轮廓跟踪处理
BYTE buffer[2048];
int k;
get_image(Image[1],0,coor_y,wid,1,buffer);
for (k=coor_x;k>=0;k--)
if (buffer[k]==255) break;
k-=1; coor_y-=1;
if (k<0) k=0;
if (coor_y<0) coor_y=0;
REFEA(k,coor_y,5,5);
DrawContour(dot,RGB(255,0,0));
hdc = GetDC(hWnd);
sprintf(buffer,"%5d",fff);
TextOut(hdc,700,20," ",17);
TextOut(hdc,700,20,buffer,strlen(buffer));
sprintf(buffer,"%5ld",sss);
TextOut(hdc,700,50," ",17);
TextOut(hdc,700,50,buffer,strlen(buffer));
sprintf(buffer,"%6.2f",yuand);
TextOut(hdc,700,80," ",17);
if (fff>fmin)
TextOut(hdc,700,80,buffer,strlen(buffer));
ReleaseDC(hWnd,hdc);
}
}
return 0L;
case WM_LBUTTONUP:
if (Image[0]->hdib) {
coor_x=LOWORD(lParam);
coor_y=HIWORD(lParam);
if ((coor_x+coor_y==0)||(coor_x>=wid