25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

111 lines
2.8 KiB

  1. /*
  2. * libpipi Proper image processing implementation 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. * resize.c: image resizing functions
  16. */
  17. #include "config.h"
  18. #include "common.h"
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "pipi_internals.h"
  22. #include "pipi.h"
  23. pipi_image_t *pipi_resize(pipi_image_t const *src, int w, int h)
  24. {
  25. int *aline, *line;
  26. pipi_image_t *dst;
  27. int x, y, x0, y0, sw, dw, sh, dh, remy;
  28. dst = pipi_new(w, h);
  29. sw = src->width; sh = src->height;
  30. dw = dst->width; dh = dst->height;
  31. aline = malloc(3 * dw * sizeof(int));
  32. line = malloc(3 * dw * sizeof(int));
  33. memset(line, 0, 3 * dw * sizeof(int));
  34. remy = 0;
  35. for(y = 0, y0 = 0; y < dst->height; y++)
  36. {
  37. int toty = 0, ny;
  38. memset(aline, 0, 3 * dw * sizeof(int));
  39. while(toty < sh)
  40. {
  41. if(remy == 0)
  42. {
  43. int r = 0, g = 0, b = 0;
  44. int remx = 0;
  45. for(x = 0, x0 = 0; x < dst->width; x++)
  46. {
  47. int ar = 0, ag = 0, ab = 0;
  48. int totx = 0, nx;
  49. while(totx < sw)
  50. {
  51. if(remx == 0)
  52. {
  53. pipi_getpixel(src, x0, y0, &r, &g, &b);
  54. x0++;
  55. remx = dw;
  56. }
  57. nx = (totx + remx <= sw) ? remx : sw - totx;
  58. ar += nx * r; ag += nx * g; ab += nx * b;
  59. totx += nx;
  60. remx -= nx;
  61. }
  62. line[3 * x] = ar;
  63. line[3 * x + 1] = ag;
  64. line[3 * x + 2] = ab;
  65. }
  66. y0++;
  67. remy = dh;
  68. }
  69. ny = (toty + remy <= sh) ? remy : sh - toty;
  70. for(x = 0; x < dst->width; x++)
  71. {
  72. aline[3 * x] += ny * line[3 * x];
  73. aline[3 * x + 1] += ny * line[3 * x + 1];
  74. aline[3 * x + 2] += ny * line[3 * x + 2];
  75. }
  76. toty += ny;
  77. remy -= ny;
  78. }
  79. for(x = 0; x < dst->width; x++)
  80. pipi_setpixel(dst, x, y,
  81. aline[3 * x] / (sw * sh),
  82. aline[3 * x + 1] / (sw * sh),
  83. aline[3 * x + 2] / (sw * sh));
  84. }
  85. free(aline);
  86. free(line);
  87. return dst;
  88. }