Advertisement
Guest User

Untitled

a guest
Jul 17th, 2016
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <Windows.h>
  5.  
  6. int main( void ){
  7. // READ IN DATA FROM FILE
  8. char *buf;
  9. {
  10. FILE *file = fopen( "in.bmp", "rb" );
  11. fseek( file, 0, SEEK_END );
  12. long size = ftell( file );
  13. rewind( file );
  14. buf = ( char * )malloc( size );
  15. fread( buf, 1, size, file );
  16. fclose( file );
  17. }
  18.  
  19. //DISPLAY HEADER INFO
  20. BITMAPFILEHEADER *fh;
  21. BITMAPINFOHEADER *ih;
  22. {
  23. fh = ( BITMAPFILEHEADER * )buf;
  24. ih = (BITMAPINFOHEADER *)(fh + 1);
  25.  
  26. char *type = (char *)&fh->bfType;
  27. if( strncmp( type, "BM", 2 ) )
  28. exit( 1 );
  29.  
  30. printf("type: %.2s\n", type);
  31. printf("size: %u\n", fh->bfSize);
  32. printf("offbits: %u\n", fh->bfOffBits);
  33. printf("BITMAPINFOHEADER: %d\n", sizeof(BITMAPINFOHEADER));
  34. printf("BITMAPV4HEADER: %d\n", sizeof(BITMAPV4HEADER));
  35. printf("BITMAPV5HEADER: %d\n", sizeof(BITMAPV5HEADER));
  36. printf("size: %u\n", ih->biSize);
  37. printf("width: %u\n", ih->biWidth);
  38. printf("height: %u\n", ih->biHeight);
  39. printf("planes: %hu\n", ih->biPlanes);
  40. printf("bitcount: %hu\n", ih->biBitCount);
  41. printf("compression: %u\n", ih->biCompression);
  42. printf("sizeimage: %u\n", ih->biSizeImage);
  43. printf("xpelspermeter: %u\n", ih->biXPelsPerMeter);
  44. printf("ypelspermeter: %u\n", ih->biYPelsPerMeter);
  45. printf("clrused: %u\n", ih->biClrUsed);
  46. printf("clrimportant: %u\n", ih->biClrImportant);
  47. }
  48.  
  49. do{
  50. //SANITY CHECKS (these should pass the default output of BMP3 from ImageMagick )
  51. if( ih->biSize != sizeof(BITMAPINFOHEADER) )
  52. break;
  53. if( ih->biBitCount != 24 )
  54. break;
  55. if( fh->bfOffBits + ih->biSizeImage != fh->bfSize )
  56. break;
  57. if( ih->biCompression )
  58. break;
  59. if( ih->biWidth * ih->biHeight * 3 != ih->biSizeImage )
  60. break;
  61.  
  62. //CREATE NEW FILE
  63. FILE *file = fopen("out.bmp", "wb");
  64.  
  65. //EDIT HEADERS
  66. fh->bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + ih->biWidth * ih->biHeight * 4;
  67. ih->biBitCount = 32;
  68. ih->biSizeImage = ih->biWidth * ih->biHeight * 4;
  69.  
  70. //WRITE HEADERS
  71. fwrite(fh, sizeof(BITMAPFILEHEADER), 1, file);
  72. fwrite(ih, sizeof(BITMAPINFOHEADER), 1, file);
  73.  
  74. FILE *rgbafile = fopen( "in.rgba", "rb" );
  75. fseek( rgbafile, 0, SEEK_END );
  76. long rgbasize = ftell( rgbafile );
  77. if( rgbasize != ih->biWidth * ih->biHeight * 4 )
  78. break;
  79. rewind( rgbafile );
  80. char *rgbabuf = ( char * )malloc( ih->biWidth * ih->biHeight * 4 );
  81. fread( rgbabuf, 1, ih->biWidth * ih->biHeight * 4, rgbafile );
  82. fclose( rgbafile );
  83.  
  84. char *new_rgbabuf = ( char * )malloc( ih->biWidth * ih->biHeight * 4 );
  85. for( int i = 0; i != ih->biHeight; i++ )
  86. for( int j = 0; j != ih->biWidth; j++ ){
  87. char *a = &new_rgbabuf[ 4 * (ih->biWidth*i + j) ];
  88. char *b = &rgbabuf[ 4 * ( ih->biWidth*(ih->biHeight-i-1)+j ) ];
  89. a[ 0 ] = b[ 2 ];
  90. a[ 1 ] = b[ 1 ];
  91. a[ 2 ] = b[ 0 ];
  92. a[ 3 ] = b[ 3 ];
  93. }
  94. fwrite( new_rgbabuf, 1, ih->biWidth * ih->biHeight * 4, file );
  95. free( new_rgbabuf );
  96.  
  97. free( rgbabuf );
  98.  
  99. fclose( file );
  100. printf( "Wrote to out.bmp.\n" );
  101. }while( 0 );
  102.  
  103. free( buf );
  104. system( "pause" );
  105. return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement