#include #include #include #pragma pack(1) typedef struct { char signature[2]; unsigned int fileSize; unsigned int reserved; unsigned int offset; unsigned int headerSize; unsigned int width; unsigned int height; unsigned short planeCount; unsigned short bitDepth; unsigned int compression; unsigned int compressedImageSize; unsigned int horizontalResolution; unsigned int verticalResolution; unsigned int numColors; unsigned int importantColors; } BmpHeader; typedef struct { unsigned char blue; unsigned char green; unsigned char red; //unsigned char reserved; Removed for convenience in fread; info.bitDepth/8 doesn't seem to work for some reason } Rgb; int main( int argc, char **argv,unsigned char *rgb ) { FILE *inFile; long bytesPerLine,i,k,ipos; BmpHeader header; Rgb *palette; unsigned char *line; inFile = fopen( "aa.bmp", "rb" ); if( !inFile ) { printf( "Error opening file %s.\n", argv[1] ); return -1; } if( fread(&header, 1, sizeof(BmpHeader), inFile) != sizeof(BmpHeader) ) { printf( "Error reading bmp header.\n" ); return -1; } bytesPerLine = (3 * (header.width + 1) / 4) * 4; FILE *outFile = fopen( "bb.bmp", "wb" ); if( !outFile ) { printf( "Error opening outputfile.\n" ); return -1; } fwrite(&header.signature, 2, 1, outFile); fwrite(&header.fileSize, 4, 1, outFile); fwrite(&header.reserved, 4, 1, outFile); fwrite(&header.offset, 4, 1, outFile); fwrite(&header.headerSize, 4, 1, outFile); fwrite(&header.width, 4, 1, outFile); fwrite(&header.height, 4, 1, outFile); fwrite(&header.planeCount, 2, 1, outFile); fwrite(&header.bitDepth, 2, 1, outFile); fwrite(&header.compression, 4, 1, outFile); fwrite(&header.compressedImageSize, 4, 1, outFile); fwrite(&header.horizontalResolution, 4, 1, outFile); fwrite(&header.verticalResolution, 4, 1, outFile); fwrite(&header.numColors, 4, 1, outFile); fwrite(&header.importantColors, 4, 1, outFile); line = new unsigned char[bytesPerLine]; for (i = header.height - 1; i >= 0; i--) { for (k = 0; k < header.width; k++) { ipos = 3 * (header.width * i + k); line[3*k] = rgb[ipos + 2]; line[3*k+1] = rgb[ipos + 1]; line[3*k+2] = rgb[ipos]; } fwrite(line, bytesPerLine, 1, outFile); } printf( "Done.\n" ); fclose(inFile); fclose(outFile); printf( "\nBMP-Info:\n" ); printf( "Width x Height: %i x %i\n", header.width, header.height ); printf( "Depth: %i\n", (int)header.bitDepth ); return 0; }