尝试将.bmp转换为灰度时获取黑色图像

时间:2018-02-10 17:09:32

标签: c file fread bmp fseek

对于bmp中的每个像素,我尝试通过某种计算来改变其蓝色,绿色和红色。但它给了我一个完全黑色的图像。

以下是我在main()中的代码的一部分:

fseek(bmpfile, offset, SEEK_SET);
for(int i = 0; i < height; i++){  //loop for every row
    for(int j = 0; j < width; j++){    //loop for every pixel in every row
        Pixel p; //Pixel is a construct that has 3 char fields of b, g, r.
        fread(&p, sizeof(p), 1, file);
        if(strcmp(argv[1], "-g") == 0){
            float b1 = p.b/255;
            float g1 = p.g/255;
            float r1 = p.r/255;     

            float gr = 0.2 * r1 + 0.7 * g1 + 0.07* b1;
            if (gr <= 0.003){
                gr = 13*gr;                 
            }else{
                gr = (1.1*pow(gr,1/2.4)-1);     
            }
            p.b = (char)(gr*255);
            p.g = (char)(gr*255);
            p.r = (char)(gr*255);

        }
        fseek(bmpfile, -(sizeof(p)), SEEK_CUR); 
        fwrite(&p, sizeof(p), 1, bmpfile);
    }   
    fseek (bmpfile, padding, SEEK_CUR);

0 个答案:

没有答案