Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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