/* This program is a contrast filter. Given an image (24-bit uncompressed bmp) and a cutoff value on the command line, the program computes the average deviation of the intensities of all of the points in the image. If this deviation is above the cutoff value, the result is black. If not, the result is white. The output bitmap is returned in stdout. */ #define DEFAULTDEV 5 #include #include #include #include typedef unsigned short int16; typedef unsigned long int32; struct SBMPFileHeader { int16 bfType; int32 bfSize; int32 bfReserved; int32 bfDataOffset; } __attribute__ ((packed)); struct SBMPInfoHeader { int32 biSize; int32 biWidth; int32 biHeight; int16 biPlanes; int16 biBitCount; int32 biCompression; int32 biSizeImage; int32 biXPelsPerMeter; int32 biYPelsPerMeter; int32 biClrUsed; int32 biClrImportant; } __attribute__ ((packed)); inline unsigned short abs(short x) { return x>0 ? x : -x; } unsigned char getValue(int32 x, int32 y, char i, unsigned char *pBitmap, SBMPInfoHeader *pHeader, unsigned char *pNumPoints) { if(x<0 || x>=pHeader->biWidth || y<0 || y>=pHeader->biHeight) { (*pNumPoints)--; return '\0'; } return pBitmap[y * (3 * pHeader->biWidth) + x*3]; } int main(int argc, char* argv[]) { SBMPFileHeader BMPFileHeader; SBMPInfoHeader BMPInfoHeader; unsigned char *pBitmap; unsigned short limDeviation = DEFAULTDEV; if(argc < 2) { cerr << "Must specify file name on the command line"; return 1; } if(argc >= 3) { limDeviation = atoi(argv[2]); } ifstream input(argv[1]); if(!input.is_open()) { cerr << "Couldn't open input file"; return 1; } input.read(&BMPFileHeader, sizeof(BMPFileHeader)); input.read(&BMPInfoHeader, sizeof(BMPInfoHeader)); BMPInfoHeader.biSizeImage = BMPFileHeader.bfSize - BMPFileHeader.bfDataOffset; if(BMPInfoHeader.biBitCount != 24) { cerr << "Bit depths other than 24-bit aren't supported\n"; return 1; } input.seekg(BMPFileHeader.bfDataOffset); pBitmap = new unsigned char[BMPInfoHeader.biSizeImage]; input.read(pBitmap, BMPInfoHeader.biSizeImage); cout.write(&BMPFileHeader, sizeof(BMPFileHeader)); cout.write(&BMPInfoHeader, sizeof(BMPInfoHeader)); cout.write(&BMPInfoHeader, BMPFileHeader.bfDataOffset - sizeof(BMPFileHeader) - sizeof(BMPInfoHeader)); for(int32 y=0; y