程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> BMP文件右旋90度[c語言]

BMP文件右旋90度[c語言]

編輯:關於C語言

#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 bfOffBits;
} BITMAPFILEHEADER;
#pragma pack()

typedef struct tagBITMAPINFOHEADER{
    DWORD biSize;
    LONG biWidth;
    LONG biHeight;
    WORD biPlanes;
    WORD biBitCount;
    DWORD biCompression;
    DWORD biSizeImage;
    LONG biXPelsPerMeter;
    LONG biYPelsPerMeter;
    DWORD biClrUsed;
    DWORD biClrImportant;
} BITMAPINFOHEADER;
/* fname- 輸入文件名稱 foname-輸出文件名 */
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 , of );
    
    
    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(2,8-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:

n塊ズ燠Mt⑴跤?喎?http://www.Bkjia.com/os/windows/windows上運行失敗可將 typedef unsigned int LONG 修改成 typedef unsigned long LONG.

[上述代碼適用於單色BMP文件](biBitCount = 1)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved