#定義圖像_H
void image _ info(FILE * FILE);
void image _ save(FILE * FILE);
void image _ gray();
void image _ binary ization();
void image _ opposite();
void image _ channel();//提取RGB通道
void image _ bright();//更改圖像的亮度
typedef結構BMP
{
//14字節
無符號短bfType//文件ID 2字節必須是BM。
無符號int bfSize//文件大小4字節
無符號短整型bfreserved 1;//保留,每字節用“00”填充2個字節。
無符號短整型bfReserved2//同2字節。
無符號int bfOffBits//記錄圖像數據區的起始位置(圖像數據相對於文件頭字節的偏移量)。4字節
//40字節
無符號int biSize//表示這個4字節結構的大小。
int雙寬度;//位圖的寬度是4個字節
int biHeight//位圖的高度為4個字節。
無符號短雙翼機;//總是1,2字節。
無符號短biBitCount//位圖分為1 4 8 16 24 32 2字節。
無符號整數雙壓縮;//壓縮描述4字節
無符號int biSizeImage//指示位圖數據區的大小為4字節。
int biXPelsPerMeter//以像素/米為單位的水平分辨率為4字節。
int biYPelsPerMeter//以像素/米為單位的垂直分辨率為4字節。
無符號int biClrUsed//位圖使用的顏色索引數為4字節。
無符號int biClrImportant//對圖像顯示有重要影響的顏色索引個數為4字節。
} BMP
int line _ byte
無符號char * imagedata
外部骨形態發生蛋白;
extern int line _ byte
extern無符號char * imagedata
#endif
//image_rw.c文件
# include & ltstdio.h & gt
# include & ltstdlib.h & gt
#包含“image.h”
void image_info(FILE *file)
{
int乘以= 3;//輸入文件名的數量。
char BMP _ name[10];//文件名
printf(" \ n請輸入文件名以便讀取:");
做
{
if(times & lt;3)
{
printf(" \ n請輸入文件名以便再次讀取:");
}
fflush(stdin);
獲取(BMP _ name);
//printf("\n%s ",BMP _ name);
file=fopen(bmp_name," r b+ ");//打開壹個文件進行讀寫。
-時代;
if (file==NULL)
{
printf(" \ n打開%s進行讀取時出錯!",BMP _ name);
}
其他
{
打破;
}
}
而(次!=0);
if (times==0)
{
printf(" \ n抱歉,關機!");
退出(1);
}
//讀取圖像信息
fseek(文件,0L,0);//讀取圖像文件類型
弗雷德(& ampbmp,sizeof(BMP),1,file);
printf("\n bmp tpye: %u ",BMP . BF type);
printf("\n bmp size: %u ",BMP . bfsize);
printf(" \ n BMP reserved 1:% u ",BMP . BF reserved 1);
printf("\n bmp reserved2: %u ",BMP . bfreserved 2);
printf("\n bmp offBits: %u ",BMP . bfoffbits);
printf("\n bmp bisize: %u ",BMP . bisize);
printf("\n bmp biWidth: %d ",BMP . biWidth);
printf(" \ n BMP BIH height:% d ",BMP . BIH height);
printf("\n bmp雙平面:%u ",BMP . biplanes);
printf("\n bmp biBitCount: %u ",BMP . biBitCount);
printf("\n bmp biCompression: %u ",BMP . biCompression);
printf("\n bmp biSizeImage: %u ",BMP . bisize image);
printf(" \ n BMP biXPelsPerMeter:% d ",BMP . biXPelsPerMeter);
printf(" \ n BMP biYPelsPerMeter:% d ",BMP . biYPelsPerMeter);
printf("\n bmp biClrUsed: %u ",BMP . biclr used);
printf(" \ n BMP biclr important:% u \ n ",BMP . biclr important);
line _ byte =(BMP . bi width * BMP . bibitcount/8+3)/4 * 4;//獲取每壹行圖像數據的數據號。
//printf("dfsa%u ",BMP . line _ byte);
//BMP . image data = NULL;
imagedata =(unsigned char *)malloc(BMP . bisize image);
fseek(file,(long)bmp.bfOffBits,0);
fread(imagedata,sizeof(無符號字符),bmp.biSizeImage,file);
fclose(文件);
}
//保存圖像
void image_save(FILE *file)
{
int乘以= 3;//輸入文件名的數量。
char BMP _ name[10];//文件名
//int I;//記錄數據區的數量
printf(" \ n請輸入要寫入的文件名:");
做
{
if(times & lt;3)
{
printf(" \ n請輸入要再次寫入的文件名:");
}
fflush(stdin);
獲取(BMP _ name);
printf("\n%s ",BMP _ name);
file=fopen(bmp_name," w b+ ");//打開壹個文件進行讀寫。
-時代;
if (file==NULL)
{
printf(" \ n打開%s進行寫入時出錯",BMP _ name);
}
其他
{
打破;
}
}
而(次!=0);
if (times==0)
{
printf(" \ n抱歉,關機!");
退出(1);
}
//寫文件頭
printf("\n%s ",BMP _ name);
fseek(文件,0L,0);//圖像文件類型
fwrite(& amp;(bmp.bfType),sizeof(short),1,file);
printf("\n bmp tpye: %d ",BMP . BF type);
fseek(文件,2L,0);//圖像文件大小
fwrite(& amp;(bmp.bfSize),sizeof(int),1,file);
printf("\n bmp大小:%d ",BMP . bfsize);
fseek(文件,6L,0);//圖像文件保留字1
fwrite(& amp;(bmp.bfReserved1),sizeof(short),1,file);
printf(" \ n BMP reserved 1:% d ",BMP . BF reserved 1);
fseek(文件,8L,0);//圖像文件保留字2
fwrite(& amp;(bmp.bfReserved2),sizeof(short),1,file);
printf("\n bmp reserved2: %d ",BMP . bfreserved 2);
fseek(file,10L,0);//數據區的偏移量
fwrite(& amp;(bmp.bfOffBits),sizeof(short),1,file);
printf("\n bmp offBits: %d ",BMP . bfoffbits);
fseek(file,14L,0);//文件頭結構大小
fwrite(& amp;(bmp.biSize),sizeof(int),1,file);
printf("\n bmp bisize: %d ",BMP . bisize);
fseek(file,18L,0);//圖像的寬度
fwrite(& amp;(bmp.biWidth),sizeof(int),1,file);
printf("\n bmp biWidth: %d ",BMP . biWidth);
fseek(文件,22L,0);//圖像的高度
fwrite(& amp;(bmp.biHeight),sizeof(int),1,file);
printf(" \ n BMP BIH height:% d ",BMP . BIH height);
fseek(file,24L,0);//圖像的面數
fwrite(& amp;(bmp.biPlanes),sizeof(short),1,file);
printf("\n bmp雙平面:%d ",BMP . biplanes);
fseek(文件,28L,0);//圖像的壹個像素中的字節數
fwrite(& amp;(bmp.biBitCount),sizeof(short),1,file);
printf("\n bmp biBitCount: %d ",BMP . biBitCount);
fseek(file,30L,0);//圖像壓縮信息
fwrite(& amp;(bmp.biCompression),sizeof(short),1,file);
printf("\n bmp雙壓縮:%d ",BMP . biCompression);
fseek(文件,34L,0);//圖像數據區的大小
fwrite(& amp;(bmp.biSizeImage),sizeof(int),1,file);
printf("\n bmp biSizeImage: %d ",BMP . bisize image);
fseek(文件,38L,0);//水平分辨率
fwrite(& amp;(bmp.biXPelsPerMeter),sizeof(int),1,file);
printf(" \ n BMP biXPelsPerMeter:% d ",BMP . biXPelsPerMeter);
fseek(文件,42L,0);//垂直分辨率
fwrite(& amp;(bmp.biYPelsPerMeter),sizeof(int),1,file);
printf(" \ n BMP biYPelsPerMeter:% d ",BMP . biYPelsPerMeter);
fseek(文件,46L,0);//顏色索引的數量
fwrite(& amp;(bmp.biClrUsed),sizeof(int),1,file);
printf("\n bmp biClrUsed: %d ",BMP . biclr used);
fseek(file,50L,0);//重要顏色索引的數量
fwrite(& amp;(bmp.biClrImportant),sizeof(int),1,file);
printf(" \ n BMP biclr important:% d \ n ",BMP . biclr important);
fseek(file,(long)(bmp.bfOffBits),0);
fwrite(imagedata,sizeof(無符號字符),bmp.biSizeImage,file);
fclose(文件);
}
//pixProcess.c文件
# include & ltstdio.h & gt
# include & ltstdlib.h & gt
# include & ltmath.h & gt
#包含“image.h”
//變灰
void image_gray()
{
int i,j;
無符號字符tmp
for(I = 0;我& ltBMP . BIH height;i++)
{
for(j = 0;j & ltline _ byte/3;j++)
{
tmp = 0.11 *(imagedata+I * line _ byte+j * 3+0))+0.59 *(imagedata+I * line _ byte+j * 3+1))+0.3 *(imagedata+I * line _ byte+j * 3+2);
imagedata[I * line _ byte+j * 3+0]= tmp;
imagedata[I * line _ byte+j * 3+1]= tmp;
imagedata[I * line _ byte+j * 3+2]= tmp;
//printf("\nnidsfh%d %d ",I,j);
}
}
}
//二值化
void image _二值化()
{
int i,j;
for(I = 0;我& ltBMP . BIH height;i++)
{
for(j = 0;j & ltline _ bytej++)
{
if((*(imagedata+I * line _ byte+j))& lt;128)
{
imagedata[I * line _ byte+j]= 0;
}
其他
{
imagedata[I * line _ byte+j]= 255;
}
}
}
}
Void image_opposite() //反轉
{
int i,j;
for(I = 0;我& ltBMP . BIH height;i++)
{
for(j = 0;j & ltline _ bytej++)
{
imagedata[I * line _ byte+j]= ABS(255-imagedata[I * line _ byte+j]);
}
}
}
Void image_channel() //提取RGB通道。
{
int i,j;
char rgb
printf(" \ n請輸入壹個字符(r/g/b):");
fflush(stdin);
scanf("%c ",& ampRGB);
if (rgb=='b ')
{
for(I = 0;我& ltBMP . BIH height;i++)
{
for(j = 0;j & ltline _ byte/3;j++)
{
imagedata[I * line _ byte+3 * j+1]= 0;
imagedata[I * line _ byte+3 * j+2]= 0;
}
}
}
else if(rgb=='g ')
{
for(I = 0;我& ltBMP . BIH height;i++)
{
for(j = 0;j & ltline _ byte/3;j++)
{
imagedata[I * line _ byte+3 * j]= 0;
imagedata[I * line _ byte+3 * j+2]= 0;
}
}
}
其他
{
for(I = 0;我& ltBMP . BIH height;i++)
{
for(j = 0;j & ltline _ byte/3;j++)
{
imagedata[I * line _ byte+3 * j]= 0;
imagedata[I * line _ byte+3 * j+1]= 0;
}
}
}
}
Void image_bright()//改變圖像的亮度。
{
int級別;
int i,j;
printf("\n請輸入亮度等級[-255到255]:);
fflush(stdin);
scanf("%d ",& amp水平);
for(I = 0;我& ltBMP . BIH height;i++)
{
for(j = 0;j & ltline _ bytej++)
{
if(level & gt;=0)
{
if((imagedata[I * line _ byte+j]+level)& gt;255)
imagedata[I * line _ byte+j]= 255;
其他
imagedata[I * line _ byte+j]+= level;
}
其他
{
if((imagedata[I * line _ byte+j]-ABS(level))& lt;0)
imagedata[I * line _ byte+j]= 0;
其他
imagedata[I * line _ byte+j]+= level;
}
}
}
}
//VoidiImage _ Create()//創建壹個24位的BMP圖像文件。
//{
//main.c文件
# include & ltstdio.h & gt
# include & ltstdlib.h & gt
# include & ltstring.h & gt
# include & ltconio.h & gt
#包含“image.h”
BMP bmp
int main()
{
FILE * file = NULL
int choose
查爾戈諾;
做
{
image_info(文件);//imagedata已經分配了動態內存,但是沒有釋放。
printf(" \ n 1 . image _ opposite ");
printf(" \ N2 . image _ gray ");
printf(" \ n 3 . image _ binary ization ");
printf(" \ n 4 . image _ channel ");
printf(" \ n 5 . image _ brightness ");
//printf(" 6 . image _ opposite ");
//printf(" 7 . image _ opposite ");
printf(" \ n選擇您的選項:");
fflush(stdin);
scanf("%d ",& amp選擇);
切換(選擇)
{
案例1:
image _ opposite();
image_save(文件);
免費(imagedata);
打破;
案例二:
image _ gray();
image_save(文件);
免費(imagedata);
打破;
案例三:
image _ binary ization();
image_save(文件);
免費(imagedata);
打破;
案例4:
image_channel()。
image_save(文件);
免費(imagedata);
打破;
案例5:
image _ bright();
image_save(文件);
免費(imagedata);
打破;
默認值:
printf("\n錯誤的選擇!");
}
printf(" \ n我們繼續?(是/否):);
fflush(stdin);
scanf("%c ",& ampGono);
if (gono=='n ')
{
printf(" \ n再見!");
打破;
}
}
while(1);
返回0;
}