資源簡介
讀bmp圖片,將rgb存入txt 并再輸出bmp圖片
代碼片段和文件信息
#include?
#include?
#define?BITMAPFILEHEADERLENGTH?14???//?The?bmp?FileHeader?length?is?14
#define?BM?19778????????????????????//?The?ASCII?code?for?BM
/*?Test?the?file?is?bmp?file?or?not?*/
void?bmpFileTest(FILE*?fpbmp);
/*?To?get?the?OffSet?of?header?to?data?part?*/
void?bmpHeaderPartLength(FILE*?fpbmp);
/*?To?get?the?width?and?height?of?the?bmp?file?*/
void?BmpWidthHeight(FILE*?fpbmp);
//get?rgb?data
void?bmpDataPart(FILE*?fpbmp);
//?output?data?to?corresponding?txt?file
void?bmpoutput(FILE?*fpout);
unsigned?int?OffSet?=?0;????//?OffSet?from?Header?part?to?Data?Part
long?width?;??????????//?The?Width?of?the?Data?Part
long?height?;?????????//?The?Height?of?the?Data?Part
unsigned?char?r[2000][2000]output_r[2000][2000];
unsigned?char?g[2000][2000]output_g[2000][2000];
unsigned?char?b[2000][2000]output_b[2000][2000];
int?main(int?argc?char*?argv[])
{
?????/*?Open?bmp?file?*/
unsigned?char?*fp_temp;
?????FILE?*fpbmp;
?????FILE?*fpout;
?????fpbmp=?fopen(“1.bmp“?“rb“);
?????if?(fpbmp?==?NULL)
?????{
?printf(“Open?bmp?failed!!!\n“);
?return?1;
?????}
?????fpout=?fopen(“out.bmp“?“wb+“);
?????if?(fpout?==?NULL)
?????{
?printf(“Open?out.bmp?failed!!!\n“);
?return?1;
?????}
?????
?????bmpFileTest(fpbmp);????????????????//Test?the?file?is?bmp?file?or?not
?????bmpHeaderPartLength(fpbmp);????????//Get?the?length?of?Header?Part
?????BmpWidthHeight(fpbmp);?????????????//Get?the?width?and?width?of?the?Data?Part
?????
?????
//
fseek(fpbmp?0L?SEEK_SET);
fseek(fpout?0L?SEEK_SET);
?
fp_temp=(unsigned?char*)malloc(OffSet);
?????????fread(fp_temp?1?OffSet?fpbmp);
fwrite(fp_temp1OffSetfpout);
?????
bmpDataPart(fpbmp);????????????????//Reserve?the?data?to?file?
?????
/*
?
?
?如果您想對圖片進(jìn)行處理,請您再這里插入處理函數(shù)!!!!!!!!!!!!!!!!!!
?
*/
bmpoutput(fpout);
fclose(fpbmp);
fclose(fpout);
?????????return?0;
}
void?bmpoutput(FILE*?fpout)
{
?????????int?i?j=0;
?????????int?stride;
unsigned?char*?pixout=NULL;
???
stride=(24*width+31)/8;
stride=stride/4*4;
pixout=(unsigned?char*)malloc(stride);
?
fseek(fpout?OffSet?SEEK_SET);
for(j=0;j {
???for(i=0;i ????????{
?????????????pixout[i*3+2]=output_r[j][i];
????????????pixout[i*3+1]=output_g[j][i];
????????????pixout[i*3]??=output_b[j][i];
????????????
??????????//??pixout[i*3+2]=output_r[height-1-j][i];
?????????//???pixout[i*3+1]=output_g[height-1-j][i];
?????????//???pixout[i*3]??=output_b[height-1-j][i];
????????}
fwrite(pixout
評論
共有 條評論