fast bresenham bitmap scaler in C


/ Published in: C
Save to your folder(s)

simple and fast bresenham image scaler, no interpolation


Copy this code and paste it in your HTML
  1. // This code snippet demonstrates the simplest bresenham image scaling algorithm.
  2. // I use it for fast, non-interpolated image upsampling on an 16bit 20MHz MCU.
  3. // You will notice that all inner loops are quite easy on cpu - there are
  4. // only additions and a few cmp's.
  5. // Also, all memory addressing (in inner loops) is done indirectly, which is
  6. // in general much faster than recalculating absolute addresses.
  7. // Note that in my case pixels are not 24 bit RGB triplets, but 8bit 422 packed
  8. // format. You can easily extend this to 3 lookups and writes instead of 1 (for
  9. // genuine 24bit RGB).
  10. //
  11. // Hope it helps :)
  12. // written by akiko / agt, 2012
  13. // http://dev.modmancer.com
  14.  
  15. #define DEMO_WIDTH 64
  16. #define DEMO_HEIGHT 64
  17. #define FB_WIDTH 320
  18. #define FB_HEIGHT 240
  19.  
  20. unsigned int pixels_written = 0;
  21. UINT8 *src = framebuffer; // input buffer of size DEMO_WIDTH * DEMO_HEIGHT
  22. UINT8 *dst = buffer; // output buffer of size FB_WIDTH * FB_HEIGHT
  23.  
  24. int xint_part = DEMO_WIDTH / FB_WIDTH;
  25. int xfrac_part = DEMO_WIDTH % FB_WIDTH;
  26.  
  27. int yint_part = DEMO_HEIGHT / FB_HEIGHT;
  28. int yfrac_part = DEMO_HEIGHT % FB_HEIGHT;
  29. int ye = 0;
  30.  
  31. int dsty = 0;
  32. int xe = 0;
  33.  
  34. while (dsty < FB_HEIGHT)
  35. {
  36. int num_pixels = FB_WIDTH;
  37. while (num_pixels-- > 0)
  38. {
  39. *dst++ = *src;
  40. src += xint_part;
  41. xe += xfrac_part;
  42. if (xe >= FB_WIDTH)
  43. {
  44. xe -= FB_WIDTH;
  45. src++;
  46. }
  47. }
  48.  
  49. // use bresenham to increase line counter
  50. dsty += yint_part;
  51. ye += yfrac_part;
  52. if(ye >= FB_HEIGHT)
  53. {
  54. ye -= FB_HEIGHT;
  55. dsty++;
  56. }
  57. }

URL: bresenham scaling

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.