[SDL] Somethng wrong with my scaling function?

mir_ctx at imap.cc mir_ctx at imap.cc
Sat Dec 15 09:01:09 PST 2007


On Fri, 14 Dec 2007 21:22:27 -0500, "Jonathan Dearborn"
<grimfang4 at hotmail.com> said:
> Hey,
> 
> I think something is wrong with this line:
> src_y = (y * src_w) * scale_y;
> Here, you're scaling the source y-coord, but you should be more careful. 
> The part in parentheses is giving you an integer that represents the
> index of the beginning of a line.  Then you scale this index by a double.
>  That will end up giving you fractions of a line as the apparent
> beginning of the line.  You would probably be best off by scaling the
> y-coordinate, rounding or flooring it, then finding the start of the
> line.
> Try replacing that line similar to this:
> src_y = int(y * scale_y) * src_w;
> 

Hello.

Yes, you're most likely right. I gave up trying it this way and
ended up with the following code instead (which works flawlessly):

static inline void
scale(SDL_Surface *dst, const SDL_Surface *src, double scale_x, double
scale_y)
{
  unsigned int dst_w = dst->w;
  unsigned int dst_h = dst->h;
  unsigned int src_w = src->w;
  unsigned int x;
  unsigned int y;
  unsigned int sx;
  unsigned int sy;
  unsigned int s_off;
  unsigned int d_off;
  unsigned int dy_off;
  unsigned int sy_off;
  uint32 *dst_pix = dst->pixels;
  uint32 *src_pix = src->pixels;

  scale_x = 1 / scale_x;
  scale_y = 1 / scale_y;

  for (y = 0; y < dst_h; ++y) {
    sy = FLOATCAST_DTL(floor(y * scale_y));
    sy_off = sy * src_w;
    dy_off = y * dst_w;
    for (x = 0; x < dst_w; ++x) {
      sx = FLOATCAST_DTL(floor(x * scale_x));
      d_off = dy_off + x;
      s_off = sy_off + sx;
      dst_pix[d_off] = src_pix[s_off];
    }
  }
}


FLOATCAST_DTL() is a macro which evaluates to 'lrintf()' if the
host system has it, or a plain cast from double to long otherwise.

thanks for listening!
-- 
  
  mir_ctx at imap.cc

-- 
http://www.fastmail.fm - One of many happy users:
  http://www.fastmail.fm/docs/quotes.html



More information about the SDL mailing list