Sign in to follow this  
lazE

bmp to jpeg conversion...am I missing something?

Recommended Posts

Grrr!! I have several images in JPEG format that I would like to use in my small game but, if I convert them to BMPs and then use my BMP loading code I see a really distorted picture. Is there something special I need to do to load BMP files that have been converted from JPEG?

Share this post


Link to post
Share on other sites
And what is the distortion you are seeing? Is there skewing (probably a padding issue). Is the distortion in 8x8 blocks (an error in your decoding routine somewhere). Are the colours wrong? Post a shot!

Share this post


Link to post
Share on other sites
images can be found at bingweb.binghamton.edu/~nschwar1

blue_flare.jpg is the jpg
blue_flare.bmp is the converted jpg
distort.bmp is what the flare looks like when I try to load it

for the sake of completeness, here is my code that loads a bmp:

// adopted from OpenGL Game Programming by LaMothe
unsigned char * load_bmp(const string & filename, BITMAPINFOHEADER * bmpInfoHeader) {
FILE * file; // a pointer to the file
BITMAPFILEHEADER bmpFileHeader; // header for the file
unsigned char * imageData; // where the bitmap data is stored
unsigned char RGB; // allows us to swap red and blue (necessary for OpenGL)


// attempt to open the bitmap
file = fopen(filename.c_str(), "rb");
if (file == NULL) {
cerr << "unable to open " << filename << ".";
return NULL;
}

// read the file header into our struct
fread(&bmpFileHeader, sizeof(BITMAPFILEHEADER), 1, file);
if (bmpFileHeader.bfType != BITMAP_ID) {
cerr << "Warning! " << filename << " is not a valid bitmap file!";
fclose(file);
return NULL;
}

// read the info header into our struct
fread(bmpInfoHeader, sizeof(BITMAPINFOHEADER), 1, file);

// seek to the beginning of bitmap data (0 + bfOffBits)
fseek(file, bmpFileHeader.bfOffBits, SEEK_SET);
imageData = new unsigned char[bmpInfoHeader->biSizeImage];
if (!imageData) {
delete [] imageData;
imageData = NULL;
fclose(file);
cerr << "Warning! aurTerrain's loadBitmap function failed to allocate memory for bmp data.";
return NULL;
}

// read the data into our struct
fread(imageData, 1, bmpInfoHeader->biSizeImage, file);
if (imageData == NULL) {
delete [] imageData;
imageData = NULL;
cerr << "Warning! For whatever reason, the data could not be read from the file and into the char array!";
fclose(file);
return NULL;
}

for(unsigned char * c = imageData; c < imageData + (bmpInfoHeader->biSizeImage); c+=3) {
RGB = *c;
*c = *(c + 2);
*(c + 2) = RGB;
}
fclose(file);

return imageData;
};




and here is the code that adds an alpha channel of one or zero depending on whether or not the color is equal to trans_color:

unsigned char * image = load_bmp(filename, bmpInfoHeader);
unsigned char * alpha_image = new unsigned char[bmpInfoHeader->biSizeImage * 4 / 3];

if (image == NULL || alpha_image == NULL) {
cerr << "unable to allocate memory while trying to load a bitmap with alpha.\n";
return NULL;
}

// loop through the bitmap data
for (unsigned int src = 0, dst = 0; src < bmpInfoHeader->biSizeImage; src +=3, dst +=4) {
// if the pixel is black, set the alpha to 0. Otherwise, set it to 255.
if (image[src] == trans_color && image[src + 1] == trans_color && image[src + 2] == trans_color)
alpha_image[dst + 3] = 0;
else
alpha_image[dst + 3] = 0xFF;

// copy pixel data over
alpha_image[dst] = image[src];
alpha_image[dst + 1] = image[src + 1];
alpha_image[dst + 2] = image[src + 2];
}

delete [] image;
image = NULL;

return alpha_image;
};



loading the BMP without the alpha channel results in a completely incoherent image. also note that this code works on other BMPs with no problems.

thanks!

Share this post


Link to post
Share on other sites
okay, i checked the bitmap (distort.bmp)
it is uncompressed 24bit/pixel
the header size is 0x36 bytes
no compression
then 0xc0000 bytes of data
(that is 512x512 pixel with 3 bytes each)

and blue_flare.bmp
24bit/pixel
header size is 0x36 bytes
resolution: 0xea (234) times 0xe7 (231) pixel
no compression
0x27b40 bytes of data 3 bytes/pixel

link

so maybe your display function expects another resolution ?

Share this post


Link to post
Share on other sites
that distort.bmp is just a screeny i took using print screen.


the real thing to check out is the blue_flare.bmp image which is whats showing up as distorted in my program...

thanks

Share this post


Link to post
Share on other sites
see above, i'm not that fast ;-)

your original jpg has a resolution of 265x253 while your converted bmp version has 243x231, is this the problem ?


edit:

besides, what is "bmpInfoHeader->biSizeImage" ?
is it the value at offset 0x22 in the file ?

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