PDA

View Full Version : (*info = (BITMAPINFO *)malloc(infosize)) == NULL)



01-25-2003, 02:16 PM
I have seen this example in many places but this cast gibes me errors can anyone help?

Bob
01-25-2003, 02:28 PM
Don't you think it would help to at least say what kind of error it is?

jebus
01-25-2003, 02:31 PM
my guess would be that info is not a double pointer. info is probably just a single pointer to a BITMAPINFO structure. i used the words "guess" and "probably" because you didn't share any source code http://www.opengl.org/discussion_boards/ubb/smile.gif

jebus

01-25-2003, 03:01 PM
Originally posted by Trying_To_LoadBitmap:
I have seen this example in many places but this cast gibes me errors can anyone help?

FILE * fp;
BITMAPFILEHEADER header;
BITMAPINFO ** info;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
GLubyte * BitmapBits[1]; /* Bitmap data */


int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
int infosize,
bitsize;

fp = fopen("NeHe.bmp", "rb");
if (fp == NULL)
return (NULL);
fread(&header, sizeof(BITMAPFILEHEADER), 1, fp);
if(header.bfType != 'MB')
{
fclose(fp);
return(NULL);
}
infosize = header.bfOffBits - sizeof(BITMAPFILEHEADER);
*info = (BITMAPINFO *)malloc(infosize);
if ((*info = (BITMAPINFO *)malloc(infosize)) == NULL)
{
/* Couldn't allocate memory for bitmap info - return NULL... */
fclose(fp);
return (NULL);
}

fread(&info, 1, infosize, fp);
if ((bitsize = (*info)->bmiHeader.biSizeImage) == 0)
bitsize = ((*info)->bmiHeader.biWidth * (*info)->bmiHeader.biBitCount + 7 / 8 *
abs((*info)->bmiHeader.biHeight));
glViewport(0,0,Width,Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,(GLfloat)Width, 0.0, (GLfloat)Height, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glRasterPos2i(0,0);
glDrawPixels((*info)->bmiHeader.biWidth, (*info)->bmiHeader.biHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, BitmapBits);
return TRUE; // Keep Going
}

01-25-2003, 03:03 PM
Sorry for the lack of info that is what my code looks like and the error is a casting error.

Bob
01-25-2003, 04:05 PM
and the error is a casting error.

What is the exact error message? Error casting from what type to what type?


*info = (BITMAPINFO *)malloc(infosize);
if ((*info = (BITMAPINFO *)malloc(infosize)) == NULL)

Why allocate memory twice? You have a memory leak here. Does it give the cast error on one of these lines or both of them (I assume that's the line you're talking about)?

01-25-2003, 04:19 PM
No those lines were repeated to see if it was the logical comparision causing the problem however I did figure out my problem.

this
BITMAPINFO ** info;
should have been
BITMAPINFO *info[1];
that.

I do still have problems but now I am past that part.

DFrey
01-25-2003, 04:42 PM
I'm confused as to why info even needs to be a pointer to a pointer. You were getting errors before because you were trying to store the pointer return by malloc into nonexistant memory via the uninitialised pointer to a pointer. You fixed it by simply changing it to an array of a single pointer, that which of course did at least make the pointer valid. I personally would just have made info a pointer to a BITMAPINFO and been done with it. http://www.opengl.org/discussion_boards/ubb/wink.gif

01-25-2003, 05:38 PM
Ok I agree with you advice about it working as only a pointer. I combined the examples of two different examples and mix matched the wrong portions. One had multipule images. However I still get an error with the BitmapBits. It is failing on the glDrawPixels. Any suggestions?
FILE * fp;
BITMAPFILEHEADER header;
BITMAPINFO *info;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
GLubyte * BitmapBits; /* Bitmap data */

int Width;
int Height;

int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
return TRUE; // Initialization Went OK
}

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
int infosize,
bitsize;

fp = fopen("NeHe.bmp", "rb");
if (fp == NULL)
return (NULL);
fread(&header, sizeof(BITMAPFILEHEADER), 1, fp);
if(header.bfType != 'MB')
{
fclose(fp);
return(NULL);
}
infosize = header.bfOffBits - sizeof(BITMAPFILEHEADER);
if ((info = (BITMAPINFO *)malloc(infosize)) == NULL)
{
/* Couldn't allocate memory for bitmap info - return NULL... */
fclose(fp);
return (NULL);
}

// fread(&info, 1, infosize, fp);
if (fread(info, 1, infosize, fp) < infosize)
{
/* Couldn't read the bitmap header - return NULL... */
free(info);
fclose(fp);
return (NULL);
}

if ((bitsize = (info)->bmiHeader.biSizeImage) == 0)
bitsize = ((info)->bmiHeader.biWidth * (info)->bmiHeader.biBitCount + 7 / 8 *
abs((info)->bmiHeader.biHeight));
if ((BitmapBits = (GLubyte *)malloc(bitsize)) == NULL)
{
/* Couldn't allocate memory - return NULL! */
free(info);
fclose(fp);
return (NULL);
}

if (fread(BitmapBits, 1, bitsize, fp) < bitsize)
{
/* Couldn't read bitmap - free memory and return NULL! */
free(info);
free(BitmapBits);
fclose(fp);
return (NULL);
}

/* OK, everything went fine - return the allocated bitmap... */
fclose(fp);

glViewport(0,0,Width,Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,(GLfloat)Width, 0.0, (GLfloat)Height, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glRasterPos2i(0,0);
glDrawPixels((info)->bmiHeader.biWidth, (info)->bmiHeader.biHeight, GL_BGRA_EXT, GL_UNSIGNED_BYTE, BitmapBits);
return TRUE; // Keep Going
}

Bob
01-26-2003, 01:10 AM
However I still get an error with the BitmapBits. It is failing on the glDrawPixels. Any suggestions?

Come on, don't you realize you have to at least say what i wrong, instead of just saying something is wrong? What happens? Does it compile, does it give you an error message while compiling, does it draw an image but the colors are wrong, or is the image distorted, do you get an access violation? We can't help unless you say what's wrong.