1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include "bmp_header.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
const char * fnamein = "test.bmp";
const char * fnameout = "output.bmp";
FILE *f;
f = fopen(fnamein,"rb");
if (f == NULL)
{
printf("File not open:\n %s\n\nExiting....\n", fnamein);
return 1;
}
fread(&BMP_header,sizeof(BMP_header),1,f);
fread(&BMP_info_header,sizeof(BMP_info_header),1,f);
if (BMP_info_header.bitPix != 24)
{
printf("Expected 24 bit colour. Found %d bits\nExiting....\n", BMP_info_header.bitPix);
return 1;
}
fseek(f,BMP_header.imageDataOffset,SEEK_SET);
unsigned int imageHeight = abs(BMP_info_header.height);
unsigned int imageWidth = ((BMP_info_header.width * BMP_info_header.bitPix) + 7) / 8;
imageWidth = (imageWidth + 3) / 4;
imageWidth *= 4;
unsigned int imageSize = imageHeight * imageWidth;
if (imageSize != BMP_info_header.biSizeImage)
{
printf ("Size discrepancy: expected = %d calculated = %d\n", BMP_info_header.biSizeImage, imageSize);
return 1;
}
else
{
printf ("Size is GOOD! expected = %d calculated = %d\n", BMP_info_header.biSizeImage, imageSize);
}
unsigned char *PixelArray = (unsigned char *)malloc(imageSize);
fread(PixelArray, imageSize, 1 , f);
unsigned int i;
unsigned int j;
for (i = 0; i<imageHeight; i++)
{
for (j = 0 ; j<imageWidth-2; j+= 3)
{
int offset = (i * imageWidth) + j;
if ((PixelArray[offset] != 255 ) || (PixelArray[offset+1] != 255) || (PixelArray[offset+2] != 255))
{
PixelArray[offset+0] = 127;
PixelArray[offset+1] = 50;
PixelArray[offset+2] = 0;
}
}
}
f = fopen(fnameout,"wb");
fwrite(&BMP_header,sizeof(BMP_header),1,f);
fwrite(&BMP_info_header,sizeof(BMP_info_header),1,f);
fwrite(PixelArray, imageSize, 1 , f);
fclose(f);
free(PixelArray);
return 0;
}
|