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

120 lines
3.2 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.h"
  22. #include "pipi_internals.h"
  23. pipi_image_t *pipi_resize(pipi_image_t *src, int w, int h)
  24. {
  25. float *srcdata, *dstdata, *aline, *line;
  26. pipi_image_t *dst;
  27. pipi_pixels_t *srcp, *dstp;
  28. int x, y, x0, y0, sw, dw, sh, dh, remy;
  29. srcp = pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
  30. srcdata = (float *)srcp->pixels;
  31. dst = pipi_new(w, h);
  32. dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  33. dstdata = (float *)dstp->pixels;
  34. sw = src->w; sh = src->h;
  35. dw = dst->w; dh = dst->h;
  36. aline = malloc(3 * dw * sizeof(float));
  37. line = malloc(3 * dw * sizeof(float));
  38. memset(line, 0, 3 * dw * sizeof(float));
  39. remy = 0;
  40. for(y = 0, y0 = 0; y < dh; y++)
  41. {
  42. int toty = 0, ny;
  43. memset(aline, 0, 3 * dw * sizeof(float));
  44. while(toty < sh)
  45. {
  46. if(remy == 0)
  47. {
  48. float r = 0, g = 0, b = 0;
  49. int remx = 0;
  50. for(x = 0, x0 = 0; x < dw; x++)
  51. {
  52. float ar = 0, ag = 0, ab = 0;
  53. int totx = 0, nx;
  54. while(totx < sw)
  55. {
  56. if(remx == 0)
  57. {
  58. r = srcdata[(y0 * sw + x0) * 4];
  59. g = srcdata[(y0 * sw + x0) * 4 + 1];
  60. b = srcdata[(y0 * sw + x0) * 4 + 2];
  61. x0++;
  62. remx = dw;
  63. }
  64. nx = (totx + remx <= sw) ? remx : sw - totx;
  65. ar += nx * r; ag += nx * g; ab += nx * b;
  66. totx += nx;
  67. remx -= nx;
  68. }
  69. line[3 * x] = ar;
  70. line[3 * x + 1] = ag;
  71. line[3 * x + 2] = ab;
  72. }
  73. y0++;
  74. remy = dh;
  75. }
  76. ny = (toty + remy <= sh) ? remy : sh - toty;
  77. for(x = 0; x < dw; x++)
  78. {
  79. aline[3 * x] += ny * line[3 * x];
  80. aline[3 * x + 1] += ny * line[3 * x + 1];
  81. aline[3 * x + 2] += ny * line[3 * x + 2];
  82. }
  83. toty += ny;
  84. remy -= ny;
  85. }
  86. for(x = 0; x < dw; x++)
  87. {
  88. dstdata[(y * dw + x) * 4] = aline[3 * x] / (sw * sh);
  89. dstdata[(y * dw + x) * 4 + 1] = aline[3 * x + 1] / (sw * sh);
  90. dstdata[(y * dw + x) * 4 + 2] = aline[3 * x + 2] / (sw * sh);
  91. }
  92. }
  93. free(aline);
  94. free(line);
  95. return dst;
  96. }