Sign in to follow this  
Ganoosh_

Monocolor/8bit BMPs

Recommended Posts

Ganoosh_    173
I'm having problems loading 1bit and 8bit bmps. The colors don't load correctly. With 1 bit images, I get big black vertical lines down the image. For example I use this image: It comes out like this when i load it: For 8 bit it comes out as a monochrome image, usingblack and whatever color. I used this image: I comes out like this: The code I used was this: >
if (image.bpp == 1) {
        palette = new unsigned char[2*4];
        fread(palette, 1, 8, file);
        unsigned char imaged[infoheader.imagesize];
        fseek(file, header.offset, SEEK_SET);
        fread(imaged, 1, infoheader.imagesize, file);
        image.image = new unsigned char[(infoheader.imagesize*8)*3];
        for (int i=0;i<infoheader.imagesize;i++) {
            for (int j=7;j>=0;j--) {
                int set = ((int)imaged[i] & (2^j)) >> j;
                image.image[(i*24)+((7-j)*3)] = palette[(set*4)+2];
                image.image[(i*24)+((7-j)*3)+1] = palette[(set*4)+1];
                image.image[(i*24)+((7-j)*3)+2] = palette[set*4];
            }
        }       
    } else if (image.bpp == 8) {
        if (infoheader.numcolors == 0)
            palette = new unsigned char[256*4];
        else
            palette = new unsigned char[infoheader.numcolors*4];
        fread(palette, 1, infoheader.numcolors*4, file);
        unsigned char imaged[infoheader.imagesize];
        fseek(file, header.offset, SEEK_SET);
        fread(imaged, 1, infoheader.imagesize, file);
        image.image = new unsigned char[infoheader.imagesize*3];   
        for (int i=0;i<infoheader.imagesize;i++) {
            for (int j=i*3;j<i*3+3;j++) {
                image.image[j] = palette[(int)imaged[i]*4+(j-(i*3))];
            }
        }
    }






Anyone know what I'm doing wrong, and how i can fix it? By the way, I'm reading the data into 24 bit data. Thanks in advance

Share this post


Link to post
Share on other sites
Endurion    5411
2^j does not what you think it does. ^ is the binary XOR operator. You want to do this: 1 << j

For the 8 bit BMP, this looks like it takes only one byte of the rgb-value from the palette instead of all three. You need to assign all three bytes as in the monochrome case.

Share this post


Link to post
Share on other sites
Ganoosh_    173
Wow, how did I forget that? I guess a 2 month break wasn't a good idea lol. Anyway that fixed the problem with 1bits, thanks, but I still can't get the 8bits working, I'm pretty sure it's getting all 3 bytes, but I'll work on it.

Share this post


Link to post
Share on other sites
Endurion    5411
Urgh, i think i spot the error now for 8bit:

You're right, you're using all the 3 bytes. Your j for loop just seems awkwardly formulated, actually it is doing from 0 to 2.

The problem is the palette reading:

You correctly interpret the infoheader.numcolors == 0 case and allocate a full 256 entry palette. But then you go ahead and call fread(palette, 1, infoheader.numcolors * 4, file); when infoheader.numcolors is still zero.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this