Offset.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. int x, y;
  20. Imaging imOut;
  21. if (!im) {
  22. return (Imaging)ImagingError_ModeError();
  23. }
  24. imOut = ImagingNewDirty(im->mode, im->xsize, im->ysize);
  25. if (!imOut) {
  26. return NULL;
  27. }
  28. ImagingCopyPalette(imOut, im);
  29. /* make offsets positive to avoid negative coordinates */
  30. xoffset %= im->xsize;
  31. xoffset = im->xsize - xoffset;
  32. if (xoffset < 0) {
  33. xoffset += im->xsize;
  34. }
  35. yoffset %= im->ysize;
  36. yoffset = im->ysize - yoffset;
  37. if (yoffset < 0) {
  38. yoffset += im->ysize;
  39. }
  40. #define OFFSET(image) \
  41. for (y = 0; y < im->ysize; y++) { \
  42. for (x = 0; x < im->xsize; x++) { \
  43. int yi = (y + yoffset) % im->ysize; \
  44. int xi = (x + xoffset) % im->xsize; \
  45. imOut->image[y][x] = im->image[yi][xi]; \
  46. } \
  47. }
  48. if (im->image8) {
  49. OFFSET(image8)
  50. } else {
  51. OFFSET(image32)
  52. }
  53. return imOut;
  54. }