Right-hand 90 degrees of BMP files [C Language]

Source: Internet
Author: User

# Include
 
  
# Include
  
   
# Include
   
    
Typedef unsigned short WORD; typedef unsigned int DWORD; typedef unsigned int LONG; # pragma pack (2) typedef struct tagBITMAPFILEHEADER {WORD bfType; DWORD bfSize; WORD bfReserved1; WORD bfReserved2; DWORD merge;} BITMAPFILEHEADER; # pragma pack () typedef struct tagBITMAPINFOHEADER {DWORD biSize; LONG biWidth; LONG biHeight; WORD biPlanes; WORD biBitCount; DWORD biCompression; DWORD biSizeImage; LONG history; LONG biYPelsPerMeter; DWORD biClrUsed; DWORD biClrImportant;} BITMAPINFOHEADER;/* fname-input file name foname-output file name */void cw90 (char * fname, char * foname) {BITMAPFILEHEADER fileHeader; BITMAPINFOHEADER infoHeader; unsigned char palette [8]; FILE * f = fopen (fname, "rb"); if (f = NULL) {printf ("Can not find file % s \ n", fname); return;} fread (& fileHeader, sizeof (BITMAPFILEHEADER), 1, f); fread (& infoHeader, sizeof (BITMAPINFOHEADER), 1, f); fread (palette, sizeof (unsigned char), 8, f); unsigned char * data = (unsigned char *) malloc (infoHeader. biSizeImage); fread (data, sizeof (unsigned char), infoHeader. biSizeImage, f); FILE * of = fopen (foname, "wb"); BITMAPFILEHEADER oFileHeader; BITMAPINFOHEADER oInfoHeader; int lineSize = (infoHeader. biHeight + 31)/32*4; int imageSize = lineSize * infoHeader. biWidth; // oFileHeader. bfType = 19778; oFileHeader. bfSize = 62 + imageSize; oFileHeader. bfReserved1 = 0; oFileHeader. bfReserved2 = 0; oFileHeader. bfOffBits = 62; // oInfoHeader. biSize = 40; oInfoHeader. biWidth = infoHeader. biHeight; oInfoHeader. biHeight = infoHeader. biWidth; oInfoHeader. biPlanes = 1; oInfoHeader. biBitCount = 1; oInfoHeader. biCompression = 0; oInfoHeader. biSizeImage = imageSize; oInfoHeader. biXPelsPerMeter = 0; oInfoHeader. biYPelsPerMeter = 0; oInfoHeader. biClrUsed = 0; oInfoHeader. biClrImportant = 0; // fwrite (& oFileHeader, sizeof (BITMAPFILEHEADER ), 1, of); fwrite (& oInfoHeader, sizeof (BITMAPINFOHEADER), 1, of); fwrite (palette, 1, 8, ); unsigned char * first = (unsigned char *) malloc (infoHeader. biHeight * oInfoHeader. biHeight); unsigned char * second = (unsigned char *) malloc (infoHeader. biHeight * oInfoHeader. biHeight); int myline = oInfoHeader. biSizeImage/oInfoHeader. biHeight; unsigned char * third = (unsigned char *) malloc (myline * 8 * oInfoHeader. biHeight); unsigned char * forth = (unsigned char *) malloc (myline * oInfoHeader. biHeight); unsigned char * img = (unsigned char *) malloc (oInfoHeader. biSizeImage); // first step int I, j, k, t =-1; int temp = infoHeader. biSizeImage/infoHeader. biHeight; for (I = 0; I <infoHeader. biHeight; I ++) {for (j = 0; j <temp; j ++) {for (k = 0; k <8; k ++) {if (j * 8 + k <oInfoHeader. biHeight) {first [++ t] = data [I * temp + j] >>( 8-1-k) & 1 ;}}}} // second step // rotate t =-1; for (I = 0; I <oInfoHeader. biHeight; I ++) {for (j = 0; j <infoHeader. biHeight; j ++) {second [++ t] = first [oInfoHeader. biHeight * j + oInfoHeader. biHeight-I-1] ;}/// step three // fill with 0's t =-1; for (I = 0; I <oInfoHeader. biHeight; I ++) {for (j = 0; j <infoHeader. biHeight; j ++) {third [++ t] = second [I * infoHeader. biHeight + j];} for (k = infoHeader. biHeight; k <myline * 8; k ++) {third [++ t] = 0 ;}// step four // transfer int u =-1; int m =-1; for (I = 0; I <oInfoHeader. biHeight; I ++) {for (j = 0; j <myline * 8; j ++) {int sum = 0; for (k = 0; k <8; k ++) {sum + = pow (-k-1) * third [++ m % t];} forth [++ u] = sum;} fwrite (forth, 1, myline * oInfoHeader. biHeight, of); free (img); free (data); fclose (f); fclose (of);} int main () {cw90 ("original.bmp ", "ttt.bmp"); system ("pause"); return 0 ;}
   
  
 


Before:



After:

If a running failure occurs on windows, you can change typedef unsigned int LONG to typedef unsigned long LONG.

[The above Code applies to a monochrome BMP file] (biBitCount = 1)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.