Home > Back-end >  C language image problem
C language image problem

Time:11-26

Has the great god please help to solve, very urgent!!!!!!!!!!
C language processing BMP image grayscale + binarization, but some pictures will be a problem, the inside of the great god help me to change the function code,,
The code is as follows:
# include
# include
# include
Int Gray (the FILE * fp1, FILE * fp2);
Int threshold (the FILE * fp2, FILE * fp3);
The # pragma pack (1)
Typedef struct tagBITMAPFILEHEADER
{
Unsigned short bfType;//file format
Unsigned long bfSize;//file size
Unsigned short bfReserved1;//keep
Unsigned short bfReserved2;
Unsigned long bfOffBits;//DIB data in the file offset
} fileHeader.
# pragma pack ()
/*
The bitmap data information structure
*/
The # pragma pack (1)
Typedef struct tagBITMAPINFOHEADER
{
Unsigned long biSize;//the size of the structure of the
Long biWidth;//file width
Long biHeight;//file height
Unsigned short biPlanes;//number of plane
Unsigned short biBitCount;//color digits
Unsigned long biCompression;//the compression type
Unsigned long biSizeImage;//DIB data area size
Long biXPixPerMeter;
Long biYPixPerMeter;
Unsigned long biClrUsed;//how many color index table
Unsigned long biClrImporant; How many important color//
} the fileInfo;
# pragma pack ()
/*
Palette structure
*/
The # pragma pack (1)
Typedef struct tagRGBQUAD
{
Unsigned char rgbBlue;//blue component brightness
Unsigned char rgbGreen;//green component brightness
Unsigned char rgbRed;//red component brightness
Unsigned char rgbReserved;
} RGBQ;
# pragma pack ()
//other data
Typedef struct OtherData
{
Unsigned char extradata;
Struct OtherData * next;
} OtherData;
//gray function
Int Gray (the FILE * fp1, FILE * fp2)
{
Fh fileHeader *;
The fileInfo * fi;
Int I, j, k=0;
//store the BMP one line of pixels
Unsigned char ImgData [3000] [3].
//to save gray image as a one-dimensional array
Unsigned char ImgData2 [3000].
Fq RGBQ *;
Fh=(fileHeader *) malloc (sizeof (fileHeader));
Fi=(*) the fileInfo malloc (sizeof (the fileInfo));
//read bitmap head structure and information
Fread (fh, sizeof (fileHeader), 1, fp1);
Fread (fi, sizeof (the fileInfo), 1, fp1);
//modify header
Fi - & gt; BiBitCount=8;
Fi - & gt; BiSizeImage=((fi - & gt; BiWidth * 3 + 3)/4 * 4 * fi - & gt; BiHeight;
Fi - & gt; BiSizeImage=fi - & gt; BiHeight * fi - & gt; BiWidth;
//modify the file header
Fh - & gt; BfOffBits=sizeof (fileHeader) + sizeof (the fileInfo) + 256 * sizeof (RGBQ);
Fh - & gt; BfSize=fh - & gt; BfOffBits + fi - & gt; BiSizeImage;
Fh - & gt; BfSize=fi - & gt; BiSize;
//create a palette
Fq=(RGBQ *) malloc (256 * sizeof (RGBQ));
For (I=0; i<256; I++)
{
Fq [I] rgbBlue=fq [I] rgbGreen=fq [I] rgbRed=I;
}
//write file header information header, palette
Fwrite (fh, sizeof (fileHeader), 1, fp2);
Fwrite (fi, sizeof (the fileInfo), 1, fp2);
Fwrite (fq, sizeof (RGBQ), 256, fp2);
//gray level change
For (I=0; i<(fi - & gt; BiHeight); I++)
{
For (j=0; j<(fi - & gt; BiWidth + 3)/4 * 4; J++)
{
For (k=0; k<3; K++)
Fread (& amp; ImgData [j] [k], 1, 1, fp1);
}
For (j=0; j<(fi - & gt; BiWidth + 3)/4 * 4; J++)
{
ImgData2 [j]=/* int */(float) ImgData [j] [0] * 0.114 + (float) ImgData [j] [1] * 0.587 + (float) ImgData [j] [2] * 0.299;
}
//the grayscale information written
Fwrite (ImgData2, j, 1, fp2);
}
Free (fh);
Free (fi);
Free (fq);
The fclose (fp1);
The fclose (fp2);
return 1;
}
//binary function
Int threshold (the FILE * fp2, FILE * fp3)
{
int i,j;
Unsigned char ImgData [1000].
Fh fileHeader *;
The fileInfo * fi;
Fq RGBQ *;
If (fp2==NULL)
{
Printf (" failed to open the file ");
exit(0);
}
If (fp3==NULL)
{
Printf (" failed to create file ");
exit(0);
}
//read grayscale data
Fh=(fileHeader *) malloc (sizeof (fileHeader));
Fi=(*) the fileInfo malloc (sizeof (the fileInfo));
Fq=(RGBQ *) malloc (256 * sizeof (RGBQ));
Fread (fh, sizeof (fileHeader), 1, fp2);
Fread (fi, sizeof (the fileInfo), 1, fp2);
Fread (fq, sizeof (RGBQ), 256, fp2);
//head information write
Fwrite (fh, sizeof (fileHeader), 1, fp3);
Fwrite (fi, sizeof (the fileInfo), 1, fp3);
Fwrite (fq, sizeof (RGBQ), 256, fp3);
//grey value is lower than the threshold is set 0
for(i=0; i<(fi - & gt; BiHeight); I++)
{
for(j=0; (j<(fi - & gt; BiWidth + 3)/4 * 4); J++)
{
Fread (& amp; , fp2 ImgData [j], 1, 1);
If (ImgData [j] & gt; 45)
ImgData [j]=255;
The else
ImgData [j]=0;
}
Fwrite (ImgData, 1, j, fp3);
}
Free (fh);
Free (fi);
Free (fq);
The fclose (fp3);
The fclose (fp2);
Printf (" success \ n ");
return 0;
}
Void main (int arg c, char * argv [])
{
Fo the FILE * fi, * and * fc;//I/O file
Char fin [80], fon [80], [80] f;//I/O file name
char a;
If (argc<3)
{
Printf (" dorginfile name: ");
The scanf (" % s ", a fin);
Printf (" resultfile name: ");
The scanf (" % s ", fon);
} else {
The scanf (argv [1], "% s", a fin);
The scanf (argv [2], "% s", fon);
}
If (((fi=fopen (fin, "rb"))==NULL) | | ((fo=fopen (fon, "wb"))==NULL))
{
Puts (" \ nfile open failed ");
return;
}
Gray (fi, fo);
System (won);
Printf (" thresholdresult name: ");
The scanf (" % s ", f);
If (((fo=fopen (fin, "rb"))==NULL) | | ((fc=fopen (f, "wb"))==NULL))
{
Puts (" \ nfile open failed ");
return;
}
Threshold (fo, fc);
System (f);
}nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related