Jump to content

  • Log In with Google      Sign In   
  • Create Account

Image Scaling Problems


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Crusable   Members   -  Reputation: 594

Like
0Likes
Like

Posted 28 March 2013 - 07:22 AM

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.

Attached Thumbnails

  • image.PNG


Sponsor:

#2 Servant of the Lord   Crossbones+   -  Reputation: 19556

Like
0Likes
Like

Posted 28 March 2013 - 03:54 PM

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, 28 March 2013 - 03:55 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#3 Crusable   Members   -  Reputation: 594

Like
0Likes
Like

Posted 28 March 2013 - 10:21 PM

Well thank you kind sir :)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS