Pillow/src/libImaging/Offset.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

65 lines
1.4 KiB
C
Raw Normal View History

2010-07-31 06:52:47 +04:00
/*
* The Python Imaging Library
* $Id$
*
* offset an image in x and y directions
*
* history:
2020-05-01 15:08:57 +03:00
* 96-07-22 fl: Created
2010-07-31 06:52:47 +04:00
* 98-11-01 cgw@pgt.com: Fixed negative-array index bug
*
* Copyright (c) Fredrik Lundh 1996.
* Copyright (c) Secret Labs AB 1997.
*
* See the README file for information on usage and redistribution.
*/
#include "Imaging.h"
Imaging
ImagingOffset(Imaging im, int xoffset, int yoffset) {
int x, y;
Imaging imOut;
2020-05-10 12:56:36 +03:00
if (!im) {
2020-05-01 15:08:57 +03:00
return (Imaging)ImagingError_ModeError();
2020-05-10 12:56:36 +03:00
}
2010-07-31 06:52:47 +04:00
imOut = ImagingNewDirty(im->mode, im->xsize, im->ysize);
2020-05-10 12:56:36 +03:00
if (!imOut) {
2020-05-01 15:08:57 +03:00
return NULL;
2020-05-10 12:56:36 +03:00
}
2010-07-31 06:52:47 +04:00
ImagingCopyPalette(imOut, im);
2010-07-31 06:52:47 +04:00
/* make offsets positive to avoid negative coordinates */
xoffset %= im->xsize;
xoffset = im->xsize - xoffset;
2020-05-10 12:56:36 +03:00
if (xoffset < 0) {
2020-05-01 15:08:57 +03:00
xoffset += im->xsize;
2020-05-10 12:56:36 +03:00
}
2010-07-31 06:52:47 +04:00
yoffset %= im->ysize;
yoffset = im->ysize - yoffset;
2020-05-10 12:56:36 +03:00
if (yoffset < 0) {
2020-05-01 15:08:57 +03:00
yoffset += im->ysize;
2020-05-10 12:56:36 +03:00
}
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
#define OFFSET(image) \
2020-05-10 12:56:36 +03:00
for (y = 0; y < im->ysize; y++) { \
2020-05-01 15:08:57 +03:00
for (x = 0; x < im->xsize; x++) { \
int yi = (y + yoffset) % im->ysize; \
int xi = (x + xoffset) % im->xsize; \
imOut->image[y][x] = im->image[yi][xi]; \
2020-05-10 12:56:36 +03:00
} \
2020-05-01 15:08:57 +03:00
}
2010-07-31 06:52:47 +04:00
2020-05-10 12:56:36 +03:00
if (im->image8) {
2020-05-01 15:08:57 +03:00
OFFSET(image8)
2020-05-10 12:56:36 +03:00
} else {
2020-05-01 15:08:57 +03:00
OFFSET(image32)
2020-05-10 12:56:36 +03:00
}
2010-07-31 06:52:47 +04:00
return imOut;
}