Archived

This topic is now archived and is closed to further replies.

Mithoric

Bitmap loading , crash on converting from BGR to RGB

Recommended Posts

I''m writing a bitmap loader, it loads alright however I have run into a problem when trying to swap the R''s and B''s so I can use GL_RGB rather than GL_BGR_EXT ...
BITMAPFILEHEADER bmfHeader;
    BITMAPINFOHEADER bmiHeader;
    GLubyte *bitmap;
    
    FILE *file = fopen(fileName,"rb");
    
    fread(&bmfHeader, 1, 14, file);
    fread(&bmiHeader, 1, 40, file);
    
    int memory;
    if(bmiHeader.biSizeImage == 0)
    {
        memory = bmiHeader.biWidth*bmiHeader.biHeight*4;
    }else{
        memory = bmiHeader.biSizeImage;
    }
    bitmap = (GLubyte*)malloc(memory);
    fread(bitmap, 1, memory, file);
    fclose(file);
    GLuint temp;
    for(int i=0; i<int(memory); i+=3)
	{
		temp=bitmap[i];
		bitmap[i] = bitmap[i + 2];
		bitmap[i + 2] = temp;
	}
    glGenTextures(1,&texture.texID);
    glBindTexture(GL_TEXTURE_2D,texture.texID);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glTexImage2D(GL_TEXTURE_2D,0,4,bmiHeader.biWidth,bmiHeader.biHeight,0,GL_RGB,GL_UNSIGNED_BYTE,bitmap);
I need to tidy up a lot of the variables in there as well but it''s functual if I don''t have the for loop swapping the bits.

Share this post


Link to post
Share on other sites
Try this:


int bpp = bmiHeader.biBitCount >> 3;
int memory = width * height * bpp;
bitmap = (GLubyte*)malloc( memory );

fread(bitmap, 1, memory, file);

GLuint temp;
for(int i=0; i < int(memory); i+=bpp )
{
temp=bitmap;
bitmap[i] = bitmap[i + 2];
bitmap[i + 2] = temp;
}


Kory


[edited by - kspansel on October 5, 2003 6:06:12 PM]

Share this post


Link to post
Share on other sites
I figured it out to be the fact that I should have been multiplying by 3 not 4, although I was sure 4 to be correct. :s

I didn''t even expect a post so soon, nice code too, I replaced mine with that because it''s much smaller plus the bitshift is quicker.

Thanks!

Share this post


Link to post
Share on other sites

/* LoadBitmap() */
void ImageLoader::LoadBMP(FILE* file, Image* image) {
BITMAPFILEHEADER fileHeader;
BITMAPINFOHEADER infoHeader;


// open file, read headers

fread(&fileHeader, sizeof(fileHeader), 1, file);
fread(&infoHeader, sizeof(infoHeader), 1, file);


// retrieve image information

int imagePixelWidth = infoHeader.biWidth;
int imagePixelHeight = infoHeader.biHeight;
int imagePixels = imagePixelWidth * imagePixelHeight;


// store image information

image->width = imagePixelWidth;
image->height = imagePixelHeight;


// read image data into buffer

RGB* imageBuffer = new RGB[imagePixels];
fread(imageBuffer, sizeof(RGB), imagePixels, file);


// allocate memory for image

image->pixels = new RGBA[imagePixels];


// copy and flip the bitmap

for(int height = 0; height < imagePixelHeight; height++) {
int imageRowIndex = imagePixelWidth * height;
int bufferRowIndex = (imagePixelHeight - height - 1) * imagePixelWidth;


for(int width = 0; width < imagePixelWidth; width++) {
image->pixels[imageRowIndex + width].r = imageBuffer[bufferRowIndex + width].b;
image->pixels[imageRowIndex + width].g = imageBuffer[bufferRowIndex + width].g;
image->pixels[imageRowIndex + width].b = imageBuffer[bufferRowIndex + width].r;
image->pixels[imageRowIndex + width].a = (unsigned char) 255;
}
}


// clean up

delete imageBuffer;
}


[edited by - _wintermute on October 5, 2003 9:04:47 PM]

Share this post


Link to post
Share on other sites
This is Gamedev''s vs, it uses EX OR.

for(GLuint cswap = 0; cswap < (int)tga.imageSize; cswap += tga.bytesPerPixel)
{
texture->imageData[cswap] ^= texture->imageData[cswap+2] ^=
texture->imageData[cswap] ^= texture->imageData[cswap+2];
}


Share this post


Link to post
Share on other sites