0

For every pixel in bmp, I tried to change its blue, green, and red color though some sort of calculation. But it gave me an image that's entirely black.

The following is part of my code in 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);
OptatootatpO
  • 103
  • 2
  • 12
  • 6
    "3 char fields of b, g, r" - if they really are `char`, then `p.b/255` will be zero in all but potentially one case due to integer division. The same is true for the other two (g and r). Divide by `255.0f` instead. – WhozCraig Feb 10 '18 at 17:12
  • I just edited my code strcmp(argv[1], "-g") == 0; This helped to solve the black image. But now the program makes no change on my bmpfile at all. I tried to use 255.0f for calculating b1, g1, and r1 but it still keeps giving me the image without any color change. – OptatootatpO Feb 10 '18 at 17:21
  • How you get 10? – OptatootatpO Feb 10 '18 at 17:22
  • Are the three `char` members signed or unsigned? – Weather Vane Feb 10 '18 at 17:23
  • It's actually smaller than that. Put in the value 255 for `p.b`, `p.g`, and `p.r` and do the calculation by hand. – user3386109 Feb 10 '18 at 17:24
  • In `fread(&pixel, sizeof(p), 1, file);` what is `pixel`? You go on to use `p` in `float b1 = p.b/255;` etc. – Weather Vane Feb 10 '18 at 17:26
  • I simply declared them as char. So I guess they are signed. – OptatootatpO Feb 10 '18 at 17:27
  • 1
    So if the colour level is 255, that will come out as -1. I suggest you make them `unsigned char`. – Weather Vane Feb 10 '18 at 17:27
  • Yes that should be &p. I modified my code a little bit before putting it here. Sorry about that. Just edited it. – OptatootatpO Feb 10 '18 at 17:29
  • 4
    Re the edit: this is one reason why the [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) is requested, so the code really is your code! – Weather Vane Feb 10 '18 at 17:29
  • After changing `strcmp(argv[1], "-g")==0)`, you need to give `-g` as the first argument to the program. Otherwise, none of the grayscale code runs. – user3386109 Feb 10 '18 at 17:37
  • On an unrelated note, your code has undefined behavior. The loop does `fread`, `fseek`, `fwrite`, and then goes back to the `fread`. See [this question](https://stackoverflow.com/questions/28228509/function-for-bmp-color-inverting-doesnt-work) for an explanation of why that's bad. – user3386109 Feb 10 '18 at 17:39
  • OT: `uint32_t int gr = 200U * p.r + 700U * p.g + 70U * p.b; if(gr <= 765) { gr = gr * 13U / 1000; } else { gr = pow(gr/255000.0)*255 }` avoids most of this floating point stuff... By adding 500 before division or 0.5 before asignment respectively, you additionally round commercially. – Aconcagua Feb 10 '18 at 17:49
  • Do you mean like putting "-g" after the filename? – OptatootatpO Feb 10 '18 at 17:49
  • No, I mean that `-g` needs to be in `argv[1]`, so the first thing on the command line after the program name. – user3386109 Feb 10 '18 at 18:07
  • @user3386109 You're right. I wrongly put "--g" in garv[1]. It works now! – OptatootatpO Feb 10 '18 at 18:07

0 Answers0