2017-07-07 27 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--你没有提供答案,你只是找到链接。这些应该是评论。 –