Offset.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * The Python Imaging Library
  3. * $Id$
  4. *
  5. * offset an image in x and y directions
  6. *
  7. * history:
  8. * 96-07-22 fl: Created
  9. * 98-11-01 cgw@pgt.com: Fixed negative-array index bug
  10. *
  11. * Copyright (c) Fredrik Lundh 1996.
  12. * Copyright (c) Secret Labs AB 1997.
  13. *
  14. * See the README file for information on usage and redistribution.
  15. */
  16. #include "Imaging.h"
  17. Imaging
  18. ImagingOffset(Imaging im, int xoffset, int yoffset)
  19. {
  20. int x, y;
  21. Imaging imOut;
  22. if (!im)
  23. return (Imaging) ImagingError_ModeError();
  24. imOut = ImagingNewDirty(im->mode, im->xsize, im->ysize);
  25. if (!imOut)
  26. return NULL;
  27. ImagingCopyPalette(imOut, im);
  28. /* make offsets positive to avoid negative coordinates */
  29. xoffset %= im->xsize;
  30. xoffset = im->xsize - xoffset;
  31. if (xoffset < 0)
  32. xoffset += im->xsize;
  33. yoffset %= im->ysize;
  34. yoffset = im->ysize - yoffset;
  35. if (yoffset < 0)
  36. yoffset += im->ysize;
  37. #define OFFSET(image)\
  38. for (y = 0; y < im->ysize; y++)\
  39. for (x = 0; x < im->xsize; x++) {\
  40. int yi = (y + yoffset) % im->ysize;\
  41. int xi = (x + xoffset) % im->xsize;\
  42. imOut->image[y][x] = im->image[yi][xi];\
  43. }
  44. if (im->image8)
  45. OFFSET(image8)
  46. else
  47. OFFSET(image32)
  48. return imOut;
  49. }