Crop.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. Imaging imOut;
  21. int xsize, ysize;
  22. int dx0, dy0, dx1, dy1;
  23. INT32 zero = 0;
  24. if (!imIn) {
  25. return (Imaging)ImagingError_ModeError();
  26. }
  27. xsize = sx1 - sx0;
  28. if (xsize < 0) {
  29. xsize = 0;
  30. }
  31. ysize = sy1 - sy0;
  32. if (ysize < 0) {
  33. ysize = 0;
  34. }
  35. imOut = ImagingNewDirty(imIn->mode, xsize, ysize);
  36. if (!imOut) {
  37. return NULL;
  38. }
  39. ImagingCopyPalette(imOut, imIn);
  40. if (sx0 < 0 || sy0 < 0 || sx1 > imIn->xsize || sy1 > imIn->ysize) {
  41. (void)ImagingFill(imOut, &zero);
  42. }
  43. dx0 = -sx0;
  44. dy0 = -sy0;
  45. dx1 = imIn->xsize - sx0;
  46. dy1 = imIn->ysize - sy0;
  47. /* paste the source image on top of the output image!!! */
  48. if (ImagingPaste(imOut, imIn, NULL, dx0, dy0, dx1, dy1) < 0) {
  49. ImagingDelete(imOut);
  50. return NULL;
  51. }
  52. return imOut;
  53. }