Crop.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * The Python Imaging Library
  3. * $Id$
  4. *
  5. * cut region from image
  6. *
  7. * history:
  8. * 95-11-27 fl Created
  9. * 98-07-10 fl Fixed "null result" error
  10. * 99-02-05 fl Rewritten to use Paste primitive
  11. *
  12. * Copyright (c) Secret Labs AB 1997-99.
  13. * Copyright (c) Fredrik Lundh 1995.
  14. *
  15. * See the README file for information on usage and redistribution.
  16. */
  17. #include "Imaging.h"
  18. Imaging
  19. ImagingCrop(Imaging imIn, int sx0, int sy0, int sx1, int sy1)
  20. {
  21. Imaging imOut;
  22. int xsize, ysize;
  23. int dx0, dy0, dx1, dy1;
  24. INT32 zero = 0;
  25. if (!imIn)
  26. return (Imaging) ImagingError_ModeError();
  27. xsize = sx1 - sx0;
  28. if (xsize < 0)
  29. xsize = 0;
  30. ysize = sy1 - sy0;
  31. if (ysize < 0)
  32. ysize = 0;
  33. imOut = ImagingNewDirty(imIn->mode, xsize, ysize);
  34. if (!imOut)
  35. return NULL;
  36. ImagingCopyPalette(imOut, imIn);
  37. if (sx0 < 0 || sy0 < 0 || sx1 > imIn->xsize || sy1 > imIn->ysize)
  38. (void) ImagingFill(imOut, &zero);
  39. dx0 = -sx0;
  40. dy0 = -sy0;
  41. dx1 = imIn->xsize - sx0;
  42. dy1 = imIn->ysize - sy0;
  43. /* paste the source image on top of the output image!!! */
  44. if (ImagingPaste(imOut, imIn, NULL, dx0, dy0, dx1, dy1) < 0) {
  45. ImagingDelete(imOut);
  46. return NULL;
  47. }
  48. return imOut;
  49. }