Jump to content
  • Advertisement
Sign in to follow this  
Crusable77

Image Scaling Problems

This topic is 1942 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

Hello, I created a function to scale my images and it is not working and I am not sure why. 

 

code:

 

 
SDL_Surface* ImageSystem::ScaleSurface(SDL_Surface* Surface, int Scale){
 
SDL_Surface* tempScaledSurface = NULL;
if(Surface == NULL || Scale == NULL) {
 
std::cout << "Could not find the image to scale or you set the scale to 0\n";
return NULL;
}
 
int Width = Surface->w * Scale;
int Height = Surface->h * Scale;
 
tempScaledSurface = SDL_CreateRGBSurface(Surface->flags, Width, Height, Surface->format->BitsPerPixel, Surface->format->Rmask, Surface->format->Gmask, Surface->format->Bmask, Surface->format->Amask);
 
if(SDL_MUSTLOCK(tempScaledSurface)) SDL_LockSurface(tempScaledSurface);
if(SDL_MUSTLOCK(Surface)) SDL_LockSurface(Surface);
 
 
for(int y = 0; y != Surface->h; ++y)
for(int x = 0; x != Surface->w; ++x)
for(int sy = 0; sy != Scale; ++sy)
for(int sx = 0; sx != Scale; ++sx)
PutPixel(tempScaledSurface, (x + sx), (y + sy), GetPixel(Surface, x, y)); 
 
if(SDL_MUSTLOCK(tempScaledSurface)) SDL_UnlockSurface(tempScaledSurface);
if(SDL_MUSTLOCK(Surface)) SDL_UnlockSurface(Surface);
 
SDL_FreeSurface(Surface);
return tempScaledSurface;
}
 
Uint32 ImageSystem::GetPixel(SDL_Surface* Surface, int x, int y){
 
Uint32 *pixels = (Uint32 *)Surface->pixels;
    return pixels[ ( y * Surface->w ) + x ];
}
 
void ImageSystem::PutPixel(SDL_Surface* Surface, int x, int y, Uint32 Pixel){
 
Uint8 * pixel = (Uint8*)Surface->pixels;
    pixel += (y * Surface->pitch) + (x * sizeof(Uint32));
    *((Uint32*)pixel) = Pixel;
}
 

I don't know how to do this image in the post so i will attach the image below.

In the image I scaled the image by 7. The black is where the image should be but it does not scale. Thank you for any help.

Share this post


Link to post
Share on other sites
Advertisement

The innards of your for() statement should be ((x * scale) + sx) not (x + sx)

Otherwise, you're overwriting most of your pixels multiple times. smile.png

 

for(int y = 0; y != Surface->h; ++y)
{
    for(int x = 0; x != Surface->w; ++x)
    {
        for(int sy = 0; sy != Scale; ++sy)
        {
            for(int sx = 0; sx != Scale; ++sx)
            {
                PutPixel(tempScaledSurface, ((x * scale) + sx), ((y * scale) + sy), GetPixel(Surface, x, y));
            }
        }
    }
}
Edited by Servant of the Lord

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!