[SDL] Somethng wrong with my scaling function?
Jonathan Dearborn
grimfang4 at hotmail.com
Fri Dec 14 18:22:27 PST 2007
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;
Good luck,
Jonny D
> From: mir_ctx at imap.cc
> To: sdl at lists.libsdl.org
> Date: Fri, 14 Dec 2007 08:29:12 -0800
> Subject: [SDL] Somethng wrong with my scaling function?
>
> Hi.
>
> I'm not sure if this problem is due to misunderstanding of
> the way SDL works or just a stupid error on my part. For
> some reason, this vanilla "nearest neighbour" scaling function
> I've written correctly scales in the X direction but not in
> the Y direction (the image appears interlaced or totally
> corrupted with fractional scaling values):
>
> 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 src_y;
> unsigned int src_x;
> unsigned int dst_y;
> 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) {
> src_y = (y * src_w) * scale_y;
> dst_y = y * dst_w;
> for (x = 0; x < dst_w; ++x) {
> src_x = x * scale_x;
> dst_pix[dst_y + x] = src_pix[src_y + src_x];
> }
> }
> }
>
>
> I have made sure that dst is large enough to hold the scaled
> pixels and that both surfaces are in 32 bit RGBA format.
>
> Is there some glaring error here?
>
> Any help appreciated.
> --
>
> mir_ctx at imap.cc
>
> --
> http://www.fastmail.fm - One of many happy users:
> http://www.fastmail.fm/docs/quotes.html
>
> _______________________________________________
> SDL mailing list
> SDL at lists.libsdl.org
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
_________________________________________________________________
Don't get caught with egg on your face. Play Chicktionary!
http://club.live.com/chicktionary.aspx?icid=chick_wlhmtextlink1_dec
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20071214/038418b3/attachment.htm
More information about the SDL
mailing list