You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

101 lines
2.5 KiB

  1. /*
  2. * libpipi Pathetic image processing interface library
  3. * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
  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. * wave.c: wave and other warping effects
  16. */
  17. #include "config.h"
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #ifndef M_PI
  23. # define M_PI 3.14159265358979323846
  24. #endif
  25. #include "pipi.h"
  26. #include "pipi_internals.h"
  27. pipi_image_t *pipi_rotate(pipi_image_t *src, double a)
  28. {
  29. pipi_image_t *dst;
  30. pipi_pixels_t *srcp, *dstp;
  31. float *srcdata, *dstdata;
  32. double sina, cosa, cx, cy;
  33. int x, y, w, h, i, gray;
  34. w = src->w;
  35. h = src->h;
  36. gray = (src->last_modified == PIPI_PIXELS_Y_F32);
  37. srcp = gray ? pipi_get_pixels(src, PIPI_PIXELS_Y_F32)
  38. : pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
  39. srcdata = (float *)srcp->pixels;
  40. dst = pipi_new(w, h);
  41. dstp = gray ? pipi_get_pixels(dst, PIPI_PIXELS_Y_F32)
  42. : pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
  43. dstdata = (float *)dstp->pixels;
  44. sina = sin(a * M_PI / 180.0);
  45. cosa = cos(a * M_PI / 180.0);
  46. cx = (double)w / 2.0;
  47. cy = (double)h / 2.0;
  48. for(y = 0; y < h; y++)
  49. {
  50. for(x = 0; x < w; x++)
  51. {
  52. double dx, dy;
  53. int x2, y2;
  54. dx = ((double)x - cx) * cosa - ((double)y - cy) * sina;
  55. dy = ((double)y - cy) * cosa + ((double)x - cx) * sina;
  56. x2 = (int)(cx + dx + 0.5);
  57. y2 = (int)(cy + dy + 0.5);
  58. if(gray)
  59. {
  60. if(x2 < 0 || y2 < 0 || x2 >= w || y2 >= h)
  61. ;
  62. else
  63. dstdata[y * w + x] = srcdata[y2 * w + x2];
  64. }
  65. else
  66. {
  67. if(x2 < 0 || y2 < 0 || x2 >= w || y2 >= h)
  68. {
  69. dstdata[4 * (y * w + x) + 3] = 0.0f;
  70. }
  71. else
  72. {
  73. for(i = 0; i < 4; i++)
  74. {
  75. dstdata[4 * (y * w + x) + i]
  76. = srcdata[4 * (y2 * w + x2) + i];
  77. }
  78. }
  79. }
  80. }
  81. }
  82. return dst;
  83. }