My code is following. Feel puzzled to generate wrong result..
``` #include #include "bmp.h" #include #include int main(int argc, char *argv[]) { if (argc != 3) { fprintf(stderr, "Please enter two files name"); return 1; }
char *inputf = argv[1];
char *outputf = argv[2];
FILE *i = fopen(inputf,"r");
if (i == NULL)
{
fprintf(stderr, "fail to access file: %s\n",inputf);
fclose(i);
return 2;
}
FILE *o = fopen(outputf,"w");
if (o == NULL)
{
fprintf(stderr, "fail to creat file: %s\n",outputf);
fclose(o);
return 2;
}
//read fileheader
BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER),1,i);
BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER),1,i);
//check whther file is BMP
if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
bi.biBitCount != 24 || bi.biCompression != 0)
{
fclose(i);
fclose(o);
fprintf(stderr, "Unsupported file format.\n");
}
// Write outfile
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, o);
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, o);
//consider padding
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4 ) % 4;
for (int row = 0, biHeight = abs(bi.biHeight); row < biHeight; row++)
{
for (int col = 0; col < bi.biWidth; col++)
{
RGBTRIPLE triple;
fread(&triple, sizeof(RGBTRIPLE), 1, i);
if (triple.rgbtRed == 0x0000ff)
{
triple.rgbtRed = 0x000000;
}
fwrite(&triple, sizeof(RGBTRIPLE), 1, o);
}
//
fseek(i, padding, SEEK_CUR);
for(int k = 0; k < padding; k++)
{
fputc(0x00, o);
}
}
//close file
fclose(i);
fclose(o);
return 0;
} ```
{}
button you can click after selecting the code, this indents by four bytes, or removes them if all selected lines had them.