Jump to content
  • Advertisement
Sign in to follow this  

easy,fast resizing image to bypass "not a power of 2" problem [+contribution]

This topic is 4838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

here is the function that does it, I wrote it based on a microsoft article:
void ResizeImage(int Width, int Height, unsigned char *RGB, int *NewWidth, int *NewHeight, unsigned char **NewRGB, int MaxRes)
   GLint glMaxTexDim;
   double xPow2, yPow2;
   int ixPow2, iyPow2;
   int xSize2, ySize2;
   unsigned char *Data = NULL;

   glGetIntegerv(GL_MAX_TEXTURE_SIZE, &glMaxTexDim);
   if(glMaxTexDim > MaxRes) glMaxTexDim = MaxRes;

   if (Width <= glMaxTexDim)
      xPow2 = log((double)Width) / log(2.0);
      xPow2 = log((double)glMaxTexDim) / log(2.0);

   if (Height <= glMaxTexDim)
      yPow2 = log((double)Height) / log(2.0);
      yPow2 = log((double)glMaxTexDim) / log(2.0);

   ixPow2 = (int)xPow2;
   iyPow2 = (int)yPow2;

   if (xPow2 != (double)ixPow2)
   if (yPow2 != (double)iyPow2)

   xSize2 = 1 << ixPow2;
   ySize2 = 1 << iyPow2;

   Data = (unsigned char*)malloc(xSize2 * ySize2 * 3);
   gluScaleImage(GL_RGB, Width, Height, GL_UNSIGNED_BYTE, RGB, xSize2, ySize2, GL_UNSIGNED_BYTE, Data);
   *NewRGB = Data;
   *NewWidth = xSize2 ;
   *NewHeight = ySize2 ;
its pretty much plug and play. Width, Height and RGB are the "in" parameters. RGB is a buffer with RGB data. NewWidth, NewHeight and NewRGB are "out" parameters, all of them pointers to the variables where the info will be dumped. NewRGB is actually allocated by the function so you have to take care of freeing the memory once you're done with it "ermm... ok. so how do I use it?" just a sample:
static void GenTextures(void)
    int bmWidth = 0, bmHeight = 0, error = 0;
    unsigned char *Data = NULL;
    unsigned int newWidth = 0, newHeight = 0;
    unsigned char *newData = NULL;
    error = LoadBMP("background.bmp", &bmWidth, &bmHeight, &Data);
        sprintf(ErrorTxt, "Could not load background.bmp error: %d", error);
    ResizeImage(bmWidth, bmHeight, Data, &newWidth, &newHeight, &newData, 800);
    glGenTextures(1, &texture[0]);
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, newWidth, newHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, newData);
hope it helps -gR

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!