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

168 行
4.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. * 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. /* FIXME: the algorithm does not handle alpha components properly. Resulting
  23. * alpha should be the mean alpha value of the neightbouring pixels, but
  24. * the colour components should be weighted with the alpha value. */
  25. pipi_image_t *pipi_resize(pipi_image_t *src, int w, int h)
  26. {
  27. float *srcdata, *dstdata, *aline, *line;
  28. pipi_image_t *dst;
  29. pipi_pixels_t *srcp, *dstp;
  30. int x, y, x0, y0, sw, dw, sh, dh, remy;
  31. srcp = pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
  32. srcdata = (float *)srcp->pixels;
  33. dst = pipi_new(w, h);
  34. dstp = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
  35. dstdata = (float *)dstp->pixels;
  36. sw = src->w; sh = src->h;
  37. dw = dst->w; dh = dst->h;
  38. aline = malloc(4 * dw * sizeof(float));
  39. line = malloc(4 * dw * sizeof(float));
  40. memset(line, 0, 4 * dw * sizeof(float));
  41. remy = 0;
  42. for(y = 0, y0 = 0; y < dh; y++)
  43. {
  44. int toty = 0, ny;
  45. memset(aline, 0, 4 * dw * sizeof(float));
  46. while(toty < sh)
  47. {
  48. if(remy == 0)
  49. {
  50. float r = 0, g = 0, b = 0, a = 0;
  51. int remx = 0;
  52. for(x = 0, x0 = 0; x < dw; x++)
  53. {
  54. float ar = 0, ag = 0, ab = 0, aa = 0;
  55. int totx = 0, nx;
  56. while(totx < sw)
  57. {
  58. if(remx == 0)
  59. {
  60. r = srcdata[(y0 * sw + x0) * 4];
  61. g = srcdata[(y0 * sw + x0) * 4 + 1];
  62. b = srcdata[(y0 * sw + x0) * 4 + 2];
  63. a = srcdata[(y0 * sw + x0) * 4 + 3];
  64. x0++;
  65. remx = dw;
  66. }
  67. nx = (totx + remx <= sw) ? remx : sw - totx;
  68. ar += nx * r; ag += nx * g; ab += nx * b; aa += nx * a;
  69. totx += nx;
  70. remx -= nx;
  71. }
  72. line[4 * x] = ar;
  73. line[4 * x + 1] = ag;
  74. line[4 * x + 2] = ab;
  75. line[4 * x + 3] = aa;
  76. }
  77. y0++;
  78. remy = dh;
  79. }
  80. ny = (toty + remy <= sh) ? remy : sh - toty;
  81. for(x = 0; x < dw; x++)
  82. {
  83. aline[4 * x] += ny * line[4 * x];
  84. aline[4 * x + 1] += ny * line[4 * x + 1];
  85. aline[4 * x + 2] += ny * line[4 * x + 2];
  86. aline[4 * x + 3] += ny * line[4 * x + 3];
  87. }
  88. toty += ny;
  89. remy -= ny;
  90. }
  91. for(x = 0; x < dw; x++)
  92. {
  93. dstdata[(y * dw + x) * 4] = aline[4 * x] / (sw * sh);
  94. dstdata[(y * dw + x) * 4 + 1] = aline[4 * x + 1] / (sw * sh);
  95. dstdata[(y * dw + x) * 4 + 2] = aline[4 * x + 2] / (sw * sh);
  96. dstdata[(y * dw + x) * 4 + 3] = aline[4 * x + 3] / (sw * sh);
  97. }
  98. }
  99. free(aline);
  100. free(line);
  101. return dst;
  102. }
  103. pipi_image_t *pipi_crop(pipi_image_t *src, int w, int h, int dx, int dy)
  104. {
  105. float *srcdata, *dstdata;
  106. pipi_image_t *dst;
  107. pipi_pixels_t *srcp, *dstp;
  108. int y, off, len;
  109. srcp = pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
  110. srcdata = (float *)srcp->pixels;
  111. dst = pipi_new(w, h);
  112. dstp = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
  113. dstdata = (float *)dstp->pixels;
  114. off = dx;
  115. len = w;
  116. if(dx < 0)
  117. {
  118. len += dx;
  119. dx = 0;
  120. }
  121. if(dx + len > srcp->w)
  122. len = srcp->w - dx;
  123. if(len > 0)
  124. {
  125. for(y = 0; y < h; y++)
  126. {
  127. if(y + dy < 0 || y + dy >= srcp->h)
  128. continue;
  129. memcpy(dstdata + y * w * 4,
  130. srcdata + ((y + dy) * srcp->w + dx) * 4,
  131. len * 4 * sizeof(float));
  132. }
  133. }
  134. return dst;
  135. }