简体   繁体   中英

C Read colors from Bitmap

Im'm loading the color of a pixel and writes a one-byte variable. When I want to read this color (for red pixel) should get the result: 255 However, the value is: 4294967296. From this it follows that the value has been recorded in 32-byte variable. Why does this happen?

struct RGBpix{
 char R;
 char G;
 char B;
}typedef RGB;

...

RGB **data=(RGB **)malloc(sizeof(RGB *)*Picture.biWidth);
    for(i = 0; i < Picture.biWidth; i++){
        data[i] = (RGB*) malloc(sizeof(RGB) *Picture.biHeight);
    }

...

for(i=0;i<Picture.biWidth;i++){
        for(j=0;j<Picture.biHeight;j++){

        fread(&data[i][j].R, 1, 1,bmpFile);
        fread(&data[i][j].G, 1, 1,bmpFile);
        fread(&data[i][j].B, 1, 1,bmpFile);
        }

    }
    printf("%Ld", data[0][1].R);

char appears to be a signed type on your machine. Use unsigned char to avoid the sign extension.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM