2017-07-07 31 views
1
int main(void) { 
    setvbuf(stdout, NULL, _IONBF, 0); 

    //Input File 
    FILE* infile; 
    infile = fopen("test.bmp", "rb"); 

    //Vars for image 
    char bm[2]; 
    int imageSize; 
    int fileSize; 
    int width, height; 
    char restOfDataOne[12]; 
    char restOfDataTwo[28]; 

    //Read Header Info 
    fread(bm, 1, 2, infile); 
    fread(&fileSize, 1, 4, infile); 
    imageSize = fileSize - 54; 
    fread(restOfDataOne, 1, 12, infile); 
    fread(&width, sizeof(int), 1, infile); 
    fread(&height, sizeof(int), 1, infile); 
    fread(restOfDataTwo, 1, 28, infile); 
    int rowWidth = width * 3; 
    //Read Image Data 
    unsigned char image[height][(width * 3)]; 
    fread(image, sizeof(char), imageSize, infile); 

    //Close 
    fclose(infile); 

    //################################################# 
    //Small BMP 
    //################################################# 
    FILE* smallOut; 
    smallOut = fopen("small.bmp", "wb"); 

    //Small Vars 
    int imageSizeSmall = imageSize/4; 
    int fileSizeSmall = imageSizeSmall + 54; 
    int widthSmall = width/2; 
    int heightSmall = height/2; 
    int smallRowWidth = widthSmall * 3; 
    unsigned char imageSmall[heightSmall][smallRowWidth]; 

    //Image Data 
    int c, d, e; 
    //For every 4 pixels.. store one in small image 
    for(c = 0; c < rowWidth; c++) { 
     for(d = 0; d < height; d++) { 
      //imageSmall[d/2][c/2] = image[d][c]; 
      for(e = 0; e < 3; e++) { 
       //grab every 1 out of 4 and place into small? 
      } 
     } 
    } 

所以我有下面的代碼讀取bmp圖像,然後我需要縮小它,並輸出到較小的版本,這是寬度的一半,高度的一半,因此總共小4倍。所以我知道我必須從每3個像素中抓取1個?並把它放到我的新的smallImage中,但我已經嘗試了多個嵌套for循環的東西,並且無法使算法失效。我在stackexchange上查看過多個帖子,但是人們正在使用庫,我不能使用它。 (家庭作業)。我不是在尋找某人爲我做這件事,而只是尋找某人爲我指出正確的方向或給我一個提示? 完整代碼在這裏。 https://pastebin.com/ZVmXtmCx縮小C中的BMP圖像

+0

那麼我需要每4個像素中的一個,每個像素是3個字節?我認爲 – Jacob

+0

你試圖實現哪一種[圖像縮放算法](https://en.wikipedia.org/wiki/Image_scaling)? –

+0

我想簡單地將圖像縮小到一半寬度和一半大小。因此,在我的2d數組(它保存了我的原始圖像數據而沒有BMP頭文件)上運行循環,然後將每個第4個像素放入我的新小圖像數組中。我還需要通過創建一個更大的圖像來做同樣的事情(4倍大小) – Jacob

回答

1

你必須在以下地址一個很好的和詳細的研究您的問題:

http://www.davdata.nl/math/bmresize.html

我引述:

這是通過掃描這些像素完成的,從左到右,從頂部到底部 ,同時將目標像素投影到源位圖上。

+0

我已經通讀了這篇文章,因爲這是我的第一個想法。我只是有麻煩翻譯成我的二維數組格式與我的for循環 – Jacob

+0

然後看看https://stackoverflow.com/questions/24672744/how-to-scale-a-bmp-image-using-c-並且只是這個iostream和stdio標題 – Fabien

+0

@ Fabien--你沒有提供答案,你只是找到鏈接。這些應該是評論。 –