该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
#include
#include
typedef unsigned char BYTE;
#define RGB 3.0
#define MAX 255.0
struct BitMap
{
short Type;
long Size;
short Reserve1;
short Reserve2;
long Offset;
long FileSize;
long Width;
long Height;
short ClrPlanes;
short BitsPerPix;
long Compression;
long ImSize;
long XPixPerMeter;
long YPixPerMeter;
long ClrTBClr;
long ImpClr;
} Header;
typedef struct
{
BYTE Blue;
BYTE Green;
BYTE Red;
} __attribute__((__packed__))
RGBTRIPLE;
int main(int argc, char **argv) {
FILE *inptr, *outptr;
float threshold;
threshold = atoi(argv[2]);
if(argc < 3) {
printf("Usage : ./a.out threshold\n");
exit(0);
}
else inptr = fopen(argv[1], "rb");
outptr = fopen ("out.bmp", "wb");
memset(&Header, 0, sizeof(Header));
fread(&Header.Type, 2, 1, inptr);
fwrite(&Header.Type, 2, 1, outptr);
fread(&Header.Size, 4, 1, inptr);
fwrite(&Header.Size, 4, 1, outptr);
fread(&Header.Reserve1, 2, 1, inptr);
fwrite(&Header.Reserve1, 2, 1, outptr);
fread(&Header.Reserve2, 2, 1, inptr);
fwrite(&Header.Reserve2, 2, 1, outptr);
fread(&Header.Offset, 4, 1, inptr);
fwrite(&Header.Offset, 4, 1, outptr);
fread(&Header.FileSize, 4, 1, inptr);
fwrite(&Header.FileSize, 4, 1, outptr);
fread(&Header.Width, 4, 1, inptr);
fwrite(&Header.Width, 4, 1, outptr);
fread(&Header.Height, 4, 1, inptr);
fwrite(&Header.Height, 4, 1, outptr);
fread(&Header.ClrPlanes, 2, 1, inptr);
fwrite(&Header.ClrPlanes, 2, 1, outptr);
fread(&Header.BitsPerPix, 2, 1, inptr);
fwrite(&Header.BitsPerPix, 2, 1, outptr);
fread(&Header.Compression, 4, 1, inptr);
fwrite(&Header.Compression, 4, 1, outptr);
fread(&Header.ImSize, 4, 1, inptr);
fwrite(&Header.ImSize, 4, 1, outptr);
fread(&Header.XPixPerMeter, 4, 1, inptr);
fwrite(&Header.XPixPerMeter, 4, 1, outptr);
fread(&Header.YPixPerMeter, 4, 1, inptr);
fwrite(&Header.YPixPerMeter, 4, 1, outptr);
fread(&Header.ClrTBClr, 4, 1, inptr);
fwrite(&Header.ClrTBClr, 4, 1, outptr);
fread(&Header.ImpClr, 4, 1, inptr);
fwrite(&Header.ImpClr, 4, 1, outptr);
// iterate over infile's scanlines
int i,j;
if (Header.Compression!=24) {
printf("Error: This program only deals with 24 compressed bmp file\n");
return 1;
}
if (threshold <0 || threshold >1.0) {
printf("Error: Threshold value is not valid\n");
return 1;
}
else for (i=0; i
{
// iterate over pixels in scanline
for (j=0; j
{
// temporary storage
RGBTRIPLE triple, tripleout;
int sum = 0;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
sum = triple.Red+triple.Green+triple.Blue;
float ratio = sum/(RGB*MAX);
printf("(%d, %d, %d)\n", triple.Red, triple.Green, triple.Blue);
// prints rgb values for each pixel (used to check if the colours were right)
printf("ratio is: %f\n", ratio);
// gives the ratio
if (ratio
tripleout.Red = 0;
tripleout.Green = 0;
tripleout.Blue = 0;
}
// this part needs to be done
else {
tripleout.Red = 255;
tripleout.Green = 255;
tripleout.Blue = 255;
}
// write RGB triple to outfile
fwrite(&tripleout, sizeof(RGBTRIPLE), 1, outptr);
}
}
fclose(inptr);
fclose(outptr);
return 0;
}
/* To use compile: gcc -Wall -o check2 check2.c
After compiling
./check filename.bmp thresholdvalue
*/