Sign in to follow this  
Crusable77

Image Scaling Problems

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

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

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