Monocolor/8bit BMPs

Started by
2 comments, last by Endurion 18 years, 5 months ago
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 & (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*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
Advertisement
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.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

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.
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.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement