Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

69 righe
1.5 KiB

  1. /*
  2. * libpipi Pathetic image processing interface library
  3. * Copyright (c) 2004-2009 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * crop.c: image cropping functions
  16. */
  17. #include "config.h"
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include "pipi.h"
  21. #include "pipi_internals.h"
  22. pipi_image_t *pipi_crop(pipi_image_t *src, int w, int h, int dx, int dy)
  23. {
  24. float *srcdata, *dstdata;
  25. pipi_image_t *dst;
  26. pipi_pixels_t *srcp, *dstp;
  27. int y, off, len;
  28. srcp = pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
  29. srcdata = (float *)srcp->pixels;
  30. dst = pipi_new(w, h);
  31. dstp = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
  32. dstdata = (float *)dstp->pixels;
  33. off = dx;
  34. len = w;
  35. if(dx < 0)
  36. {
  37. len += dx;
  38. dx = 0;
  39. }
  40. if(dx + len > srcp->w)
  41. len = srcp->w - dx;
  42. if(len > 0)
  43. {
  44. for(y = 0; y < h; y++)
  45. {
  46. if(y + dy < 0 || y + dy >= srcp->h)
  47. continue;
  48. memcpy(dstdata + y * w * 4,
  49. srcdata + ((y + dy) * srcp->w + dx) * 4,
  50. len * 4 * sizeof(float));
  51. }
  52. }
  53. return dst;
  54. }