I had succes reading binary files from BMP file using Vectors to stores the datas before write it on a new file...follow below the code. I realized that this Code could be improved and I found some solutions checking books and Internet. Lots of people wrote the 2nd code below, the problem is that for me it not run well and make any BMP file to single color file. I`m using Xcode5 do compile this program.
for (int i = 0; i <= imageW - 1; i++)
{
for (int j = 0; j <= imageH - 1; j++)
{
fread(&rgb[i][j], sizeof(RGBTRIPLE), 1, datas);
}
}
// open output file
FILE *bmp_blue = fopen("newfile.bmp", "w");
if (bmp_blue == NULL)
{
printf("Could not create\n");
return 3;
}
// Write output file BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, bmp_blue);
// Write output file BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, bmp_blue);
// go Black
for (int i = 0; i <= imageW - 1; i++)
{
for (int j = 0; j <= imageH - 1; j++)
{
if (rgb[i][j].rgbtRed == 255)
{
rgb[i][j].rgbtGreen = 0;
rgb[i][j].rgbtRed = 0;
rgb[i][j].rgbtBlue = 0;
}
}
}
for (int i = 0; i <= imageW - 1; i++)
{
for (int j = 0; j <= imageH - 1; j++)
{
printf("%d B %d",i,rgb[i][j].rgbtBlue);
printf(" G %d",rgb[i][j].rgbtGreen);
printf(" R %d\n",rgb[i][j].rgbtRed);
}
}
// Write output file RGBTRIPLE
for (int i = 0; i <= imageW - 1; i++)
{
for (int j = 0; j <= imageH - 1; j++)
{
fwrite(&rgb[i][j], sizeof(RGBTRIPLE), 1, bmp_blue);
}
}
Code w/o using Vector and iterating the binary file directly with FOR statement.
// Write output file BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, bmp_blue);
// Write output file BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, bmp_blue);
bi.biHeight = abs(bi.biHeight);
// If Red >>> Go Black
// iterate over infile's scanlines
for (int i = 0 ; i < bi.biHeight; i++)
{
// iterate over pixels in scanline
for (int j = 0; j < bi.biWidth; j++)
{
// temporary storage
RGBTRIPLE rgb;
// read RGB triple from infile
fread(&rgb, sizeof(RGBTRIPLE), 1, bmp_blue);
// crank down blue in all pixels
if(rgb.rgbtRed == 255)
{
rgb.rgbtBlue = 0;
rgb.rgbtRed = 0;
rgb.rgbtGreen = 0;
}
// write RGB triple to outfile
fwrite(&rgb, sizeof(RGBTRIPLE), 1, bmp_blue);