Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <Windows.h>
- int main( void ){
- // READ IN DATA FROM FILE
- char *buf;
- {
- FILE *file = fopen( "in.bmp", "rb" );
- fseek( file, 0, SEEK_END );
- long size = ftell( file );
- rewind( file );
- buf = ( char * )malloc( size );
- fread( buf, 1, size, file );
- fclose( file );
- }
- //DISPLAY HEADER INFO
- BITMAPFILEHEADER *fh;
- BITMAPINFOHEADER *ih;
- {
- fh = ( BITMAPFILEHEADER * )buf;
- ih = (BITMAPINFOHEADER *)(fh + 1);
- char *type = (char *)&fh->bfType;
- if( strncmp( type, "BM", 2 ) )
- exit( 1 );
- printf("type: %.2s\n", type);
- printf("size: %u\n", fh->bfSize);
- printf("offbits: %u\n", fh->bfOffBits);
- printf("BITMAPINFOHEADER: %d\n", sizeof(BITMAPINFOHEADER));
- printf("BITMAPV4HEADER: %d\n", sizeof(BITMAPV4HEADER));
- printf("BITMAPV5HEADER: %d\n", sizeof(BITMAPV5HEADER));
- printf("size: %u\n", ih->biSize);
- printf("width: %u\n", ih->biWidth);
- printf("height: %u\n", ih->biHeight);
- printf("planes: %hu\n", ih->biPlanes);
- printf("bitcount: %hu\n", ih->biBitCount);
- printf("compression: %u\n", ih->biCompression);
- printf("sizeimage: %u\n", ih->biSizeImage);
- printf("xpelspermeter: %u\n", ih->biXPelsPerMeter);
- printf("ypelspermeter: %u\n", ih->biYPelsPerMeter);
- printf("clrused: %u\n", ih->biClrUsed);
- printf("clrimportant: %u\n", ih->biClrImportant);
- }
- do{
- //SANITY CHECKS (these should pass the default output of BMP3 from ImageMagick )
- if( ih->biSize != sizeof(BITMAPINFOHEADER) )
- break;
- if( ih->biBitCount != 24 )
- break;
- if( fh->bfOffBits + ih->biSizeImage != fh->bfSize )
- break;
- if( ih->biCompression )
- break;
- if( ih->biWidth * ih->biHeight * 3 != ih->biSizeImage )
- break;
- //CREATE NEW FILE
- FILE *file = fopen("out.bmp", "wb");
- //EDIT HEADERS
- fh->bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + ih->biWidth * ih->biHeight * 4;
- ih->biBitCount = 32;
- ih->biSizeImage = ih->biWidth * ih->biHeight * 4;
- //WRITE HEADERS
- fwrite(fh, sizeof(BITMAPFILEHEADER), 1, file);
- fwrite(ih, sizeof(BITMAPINFOHEADER), 1, file);
- FILE *rgbafile = fopen( "in.rgba", "rb" );
- fseek( rgbafile, 0, SEEK_END );
- long rgbasize = ftell( rgbafile );
- if( rgbasize != ih->biWidth * ih->biHeight * 4 )
- break;
- rewind( rgbafile );
- char *rgbabuf = ( char * )malloc( ih->biWidth * ih->biHeight * 4 );
- fread( rgbabuf, 1, ih->biWidth * ih->biHeight * 4, rgbafile );
- fclose( rgbafile );
- char *new_rgbabuf = ( char * )malloc( ih->biWidth * ih->biHeight * 4 );
- for( int i = 0; i != ih->biHeight; i++ )
- for( int j = 0; j != ih->biWidth; j++ ){
- char *a = &new_rgbabuf[ 4 * (ih->biWidth*i + j) ];
- char *b = &rgbabuf[ 4 * ( ih->biWidth*(ih->biHeight-i-1)+j ) ];
- a[ 0 ] = b[ 2 ];
- a[ 1 ] = b[ 1 ];
- a[ 2 ] = b[ 0 ];
- a[ 3 ] = b[ 3 ];
- }
- fwrite( new_rgbabuf, 1, ih->biWidth * ih->biHeight * 4, file );
- free( new_rgbabuf );
- free( rgbabuf );
- fclose( file );
- printf( "Wrote to out.bmp.\n" );
- }while( 0 );
- free( buf );
- system( "pause" );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement