您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

67 行
1.5 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2004-2014 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #include <lol/engine-internal.h>
  11. #include "image-private.h"
  12. /*
  13. * Image cropping functions
  14. */
  15. namespace lol
  16. {
  17. Image Image::Crop(ibox2 box) const
  18. {
  19. ivec2 const srcsize = GetSize();
  20. ivec2 const dstsize = box.B - box.A;
  21. Image dst(dstsize);
  22. PixelFormat format = GetFormat();
  23. if (format != PixelFormat::Unknown)
  24. {
  25. dst.SetFormat(format);
  26. uint8_t const *srcp = (uint8_t const *)m_data->m_pixels[(int)format];
  27. uint8_t *dstp = (uint8_t *)dst.m_data->m_pixels[(int)format];
  28. uint8_t bpp = BytesPerPixel(format);
  29. int len = dstsize.x;
  30. if (box.A.x < 0)
  31. {
  32. len += box.A.x;
  33. box.A.x = 0;
  34. }
  35. if (box.A.x + len > srcsize.x)
  36. len = srcsize.x - box.A.x;
  37. if (len > 0)
  38. {
  39. for (int y = 0; y < dstsize.y; y++)
  40. {
  41. if (y + box.A.y < 0 || y + box.A.y >= srcsize.y)
  42. continue;
  43. memcpy(dstp + y * dstsize.x * bpp,
  44. srcp + ((y + box.A.y) * srcsize.x + box.A.x) * bpp,
  45. len * bpp);
  46. }
  47. }
  48. }
  49. return dst;
  50. }
  51. } /* namespace lol */