1、/用 c 语言进行数字图像处理 其实,数字图像处理有几步呢?一共三步。第一步,读入图片。第二步,处理图片。第三步,保存图片 。 而第二步主要涉及的是处理图像的算法,所以,我在这里就不多说了。而第一步和第三步是为第二步做 位图文件结构的声明:BMP.h #ifndef BMP_H_INCLUDED #define BMP_H_INCLUDED typedef unsigned short WORD; typedef unsigned long DWORD; typedef long LONG; typedef unsigned char BYTE; typedef struct tagBITMA
2、PFILEHEADER / bmfh WORD bfType; DWORD bfSize; WORD bfReserved1; WORD bfReserved2; DWORD bfOffBits; BITMAPFILEHEADER; typedef struct tagBITMAPINFOHEADER / bmih DWORD biSize; LONG biWidth; LONG biHeight; WORD biPlanes; WORD biBitCount; DWORD biCompression; DWORD biSizeImage; LONG biXPelsPerMeter; LONG
3、 biYPelsPerMeter; DWORD biClrUsed; DWORD biClrImportant; /BITMAPINFOHEADER; typedef struct tagRGBQUAD / rgbq BYTE rgbBlue; BYTE rgbGreen; BYTE rgbRed; BYTE rgbReserved; RGBQUAD; typedef struct tagBITMAPINFO BITMAPINFOHEADER bmiHeader; RGBQUAD bmiColors1; BITMAPINFO; #endif / BMP_H_INCLUDED 主程序:main.
4、c #include #include #include #include #include #include #include “BMP.h“ BITMAPFILEHEADER bmfh; BITMAPINFOHEADER bmih; BYTE *imgData; bool bReadBMFH=false; bool bReadBMIH=false; bool bReadPixel=false; /检查路径是否合法:文件能打开;以 bmp 为后缀名 int CheckFilePath(char *filepath); /读入位图的文件头 int ReadFileHeader(char *fi
5、lepath,BITMAPFILEHEADER *bmfh); /打印位图的文件头 void PrintFileHeader(BITMAPFILEHEADER *bmfh); /读入位图的信息头 int ReadInfoHeader(char *filepath,BITMAPINFOHEADER *bmih); /打印位图的信息头 void PrintInfoHeader(BITMAPINFOHEADER *bmih); /创建 8 位位图的调色板 /int CreatePalette(RGBQUAD pal); /读入位图的像素数据 int ReadPixelData(char *filep
6、ath,BYTE *imgData); /计算每行像素所占的字节数 LONG GetLineBytes(int imgWidth,int bitCount); /打印位图的像素数据 void PrintPixelData(BYTE *imgData,int width,int height,int bitCount); /打印菜单选项 void PrintMenu(); /另存为位图 int SaveAsImage(char *filepath); /显示位图 void ShowImage(char * filepath); /保存文件头 int SaveFileHeader(FILE* fp
7、); /保存信息头 int SaveInfoHeader(FILE* fp); /保存调色板 int SaveColorPalette(FILE *fp); /保存像素数据 int SavePixelData(FILE* fp); int main() char filepath256; char saveasfilepath256; int i; int width; int height; int bitCount; DWORD dwLineBytes; int select; int q=0; system(“echo off“); system(“color 2“); printf(“
8、-TIMimage-n“); printf(“Input the path of the BMP file:n“); gets(filepath); i=CheckFilePath(filepath); if(i=-1) return -1; do /PrintMenu(); scanf(“%u“, switch(select) case 0: printf(“Input the path of the BMP file:n“); scanf(“%s“,filepath); CheckFilePath(filepath); break; case 1: i=ReadFileHeader(fil
9、epath, if(i!=-1) printf(“Read the file header successfully.n“); bReadBMFH=true; break; else printf(“Read the file header failed.n“); bReadBMFH=false; q=1; break; case 2: i=ReadInfoHeader(filepath, if(i!=-1) printf(“Read the info header successfully.n“); bReadBMIH=true; break; else printf(“Read the i
10、nfo header failed.n“); bReadBMIH=false; q=1; break; / case 3: if(!bReadBMIH) printf(“Please read the info header at first.n“); break; height=bmih.biHeight; width=bmih.biWidth; bitCount=bmih.biBitCount; dwLineBytes=GetLineBytes(width,bitCount); imgData=(BYTE*)malloc(dwLineBytes*height*sizeof(BYTE); i
11、f(!imgData) printf(“Can not allocate memory for the image.n“); q=1; break; i=ReadPixelData(filepath,imgData); if(i=-1) printf(“Read the pixel data failed.n“); bReadPixel=false; q=1; break; else printf(“Read the pixel data successfully.n“); bReadPixel=true; break; case 4: if(bReadBMFH) PrintFileHeade
12、r( break; else printf(“Please read the file header at first.n“); /break; case 5: if(bReadBMIH) PrintInfoHeader( break; else printf(“Please read the info header at first.n“); break; case 6: if(bReadPixel) PrintPixelData(imgData,width,height,bitCount); break; else printf(“Please read the pixel data at
13、 first.n“); break; case 7: ShowImage(filepath); break; case 8: printf(“Input the path(ex. d:/poon.bmp) you want to save:n“); scanf(“%s“,saveasfilepath); i=SaveAsImage(saveasfilepath); if(i=-1) printf(“Error: failed to save the image.n“); break; /break; default: q=1; break; select=9527; while (q=0);
14、return 0; int ReadFileHeader(char *filepath,BITMAPFILEHEADER *bmfh) FILE *fp; fp=fopen(filepath,“rb“); if(!fp) printf(“Can not open the file:%sn“,filepath); return -1; if(fread( fclose(fp); return -1; if(fread( fclose(fp); return -1; if(fread( fclose(fp); return -1; if(fread( fclose(fp); return -1;
15、/if(fread( fclose(fp); return -1; fclose(fp); return 0; int ReadInfoHeader(char *filepath,BITMAPINFOHEADER *bmih) FILE *fp; fp=fopen(filepath,“rb“); if(!fp) printf(“Can not open the file:%sn“,filepath); return -1; fseek(fp,14,SEEK_SET); if(fread( fclose(fp); return -1; if(fread( fclose(fp); return -
16、1; if(fread( fclose(fp); return -1; if(fread( fclose(fp); return -1; /if(fread( fclose(fp); return -1; if(fread( fclose(fp); return -1; if(fread( fclose(fp); return -1; if(fread( fclose(fp); return -1; if(fread( fclose(fp); return -1; if(fread( fclose(fp); return -1; if(fread( fclose(fp); return -1;
17、 fclose(fp); /return 0; int CreatePalette(RGBQUAD pal) int i; if(sizeof(pal)/sizeof(RGBQUAD)!=256) printf(“The size of the palette must be 256.n“); return -1; for(i=0;ibfOffBits); printf(“bfReserved1: %ldn“,bmfh-bfReserved1); printf(“bfReserved2: %ldn“,bmfh-bfReserved2); printf(“bfSize: %ldn“,bmfh-b
18、fSize); printf(“bfType: %ldn“,bmfh-bfType); void PrintInfoHeader(BITMAPINFOHEADER *bmih) printf(“The content in the info header of the BMP file:n“); printf(“biBitCount: %ldn“,bmih-biBitCount); printf(“biClrImportant: %ldn“,bmih-biClrImportant); printf(“biClrUsed: %ldn“,bmih-biClrUsed); printf(“biCom
19、pression: %ldn“,bmih-biCompression); printf(“biHeight: %ldn“,bmih-biHeight); printf(“biPlanes: %ldn“,bmih-biPlanes); printf(“biSize: %ldn“,bmih-biSize); printf(“biSizeImage: %ldn“,bmih-biSizeImage); printf(“biWidth: %ldn“,bmih-biWidth); printf(“biXPelsPerMeter: %ldn“,bmih-biXPelsPerMeter); printf(“b
20、iYPelsPerMeter: %ldn“,bmih-biYPelsPerMeter); LONG GetLineBytes(int imgWidth,int bitCount) return (imgWidth*bitCount+31)/32*4; void PrintPixelData(BYTE *imgData,int width,int height,int bitCount) int i; /int j ; int p; DWORD dwLineBytes=GetLineBytes(width,bitCount); if(bitCount=8) for(i=0;iheight;i+)
21、 for(j=0;jwidth;j+) p=*(imgData+dwLineBytes*(height-1-i)+j); printf(“%d,“,p); printf(“n“); else if(bitCount=24) for(i=0;iheight;i+) for(j=0;jwidth*3;j+) printf(“(“); p=*(imgData+dwLineBytes*(height-1-i)+j); printf(“%d,“,p); j+ ; p=*(imgData+dwLineBytes*(height-1-i)+j); printf(“%d,“,p); j+ ; p=*(imgD
22、ata+dwLineBytes*(height-1-i)+j); printf(“%d) “,p); printf(“n“); else printf(“Only supported: 8 or 24 bits.n“); int CheckFilePath(char *filepath) FILE *fp; int len=strlen(filepath)/sizeof(char); /char ext3; if(filepath0!=int(“) strncpy(ext, if(!(ext0=b printf(“Error: The extention of the filename mus
23、t be bmp,not BMP n“); return -1; fp=fopen(filepath,“r“); if(!fp) printf(“Error: The path is not correct.n“); return -1; fclose(fp); else printf(“Error: The path must not include blank space.n“); return -1; return 0; void PrintMenu() printf(“ -Choose Your Operation-n“); printf(“ | 0-Input the image p
24、ath |n“); printf(“ | 1-Read the file header |n“); printf(“ | 2-Read the info header |n“); printf(“ | 3-Read the pixel data |n“); printf(“ | 4-Print the file header |n“); printf(“ | 5-Print the info header |n“); printf(“ | 6-Print the pixel data |n“); printf(“ | 7-View the original image |n“); printf
25、(“ | 8-Save as the image |n“); printf(“ | other-Exit the program |n“); printf(“ -n“); int SaveAsImage(char *filepath) FILE *fp; /fp=fopen(filepath,“wb“); if(!fp) printf(“Error: can not create the file.n“); return -1; SaveFileHeader(fp); SaveInfoHeader(fp); if(bmih.biBitCount=8) SaveColorPalette(fp);
26、 SavePixelData(fp); fclose(fp); printf(“Save As the image successfully.n“);return 0; void ShowImage(char * filepath) char cmd266; strcpy(cmd,“start “); strcat(cmd,filepath); printf(“%sn“,cmd); system(cmd); int SaveFileHeader(FILE *fp) if(!bReadBMFH) printf(“Please read the file header at first.n“);
27、return -1; if(fwrite( fclose(fp); return -1; if(fwrite( fclose(fp); return -1; /if(fwrite( fclose(fp); return -1; if(fwrite( fclose(fp); return -1; if(fwrite( fclose(fp); return -1; return 0; int SaveInfoHeader(FILE *fp) if(!bReadBMIH) printf(“Please read the info header at first.n“); return -1; if(
28、fwrite( fclose(fp); return -1; if(fwrite( fclose(fp); return -1; if(fwrite( fclose(fp); return -1; / if(fwrite( fclose(fp); return -1; if(fwrite( fclose(fp); return -1; if(fwrite( fclose(fp); return -1; if(fwrite( fclose(fp); return -1; if(fwrite( fclose(fp); return -1; if(fwrite( fclose(fp); return
29、 -1; if(fwrite( fclose(fp); return -1; if(fwrite( fclose(fp); return -1; return 0; int SaveColorPalette(FILE *fp) int i; RGBQUAD pal256; if(!bReadBMIH) printf(“Please read the info header at first.n“); return -1; if(bmih.biBitCount!=8) printf(“Only 8 bits image hase color palette.n“); return -1; for
30、(i=0;i256;i+) pali.rgbReserved=0; pali.rgbBlue=i; pali.rgbGreen=i; pali.rgbRed=i; if(fwrite(pal,sizeof(RGBQUAD),256,fp)!=256) printf(“Error: can not write the color palette.n“); fclose(fp); return -1; return 0; int SavePixelData(FILE* fp) int height=bmih.biHeight; DWORD dwLineBytes=GetLineBytes(bmih.biWidth,bmih.biBitCount); if(!bReadPixel) printf(“Please read the pixel data at first.n“); return -1; / if(fwrite(imgData,height*dwLineBytes,1,fp)!=1) printf(“Error: can not write the pixel data.n“); fclose(fp); return -1; return 0;