Sign in to follow this  

missing pixel after rotating

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

I made a "Rotate by 90 degrees"-function for my VideoEngine but one pixel is always missing. its the pixel down left in the corner. although the new position is calculated to 0,0 when rotating clockwise. it is not set. here is my rotate function
SDL_Surface* VideoManager::Surface_Rotate90(SDL_Surface* source, int mode) {
       Uint32 pixel;
       int dx,dy;
       if(mode != VideoManager::_ROTATE_CLOCKWISE && mode != VideoManager::_ROTATE_COUNTERCLOCKWISE) {
                return NULL;
       }
       SDL_Surface *dest = VideoManager::Create_Surface(source->h,source->w);
       SDL_FillRect(dest,(SDL_Rect*)NULL,SDL_MapRGB(dest->format,255,0,0));
       for(int y = 0; y < source->h; y++) {
                for(int x = 0; x < source->w; x++) {
                         pixel = VideoManager::getPixel(source, x, y);
                         if(mode == VideoManager::_ROTATE_CLOCKWISE) {
                                 dx = source->h - y;
                                 dy = x;
                         } else if(mode == VideoManager::_ROTATE_COUNTERCLOCKWISE) {
                                 dx = y;
                                 dy = source->w - x;
                         }
                         VideoManager::setUint32Pixel(dest, dx, dy, pixel);
                }
       }
       return dest;
}

Share this post


Link to post
Share on other sites
You're not just missing one pixel, but the entire left column and bottom row.

The image is defined on the domain [0, source->w - 1]×[0, source->h - 1]. Note the exclusivity. So when you compute

dx = source->h - y;

the interval becomes [1, source->h] and the row is lost. Similarly for dy. The correct code would use:

dx = source->h - y - 1;
dy = source->w - x - 1;

Share this post


Link to post
Share on other sites

This topic is 3630 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.

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