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.
 
 
 
 
 
 

132 lines
3.9 KiB

  1. /*
  2. * libpipi Pathetic image processing interface library
  3. * Copyright (c) 2004-2009 Sam Hocevar <sam@hocevar.net>
  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. * bresenham.c: Bresenham 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. /* This is Bresenham resizing. I rediscovered it independently but it was
  23. * actually first described in 1995 by Tim Kientzle in "Scaling Bitmaps
  24. * with Bresenham". */
  25. /* FIXME: the algorithm does not handle alpha components properly. Resulting
  26. * alpha should be the mean alpha value of the neightbouring pixels, but
  27. * the colour components should be weighted with the alpha value. */
  28. pipi_image_t *pipi_resize_bresenham(pipi_image_t *src, int w, int h)
  29. {
  30. float *srcdata, *dstdata, *aline, *line;
  31. pipi_image_t *dst;
  32. pipi_pixels_t *srcp, *dstp;
  33. int x, y, x0, y0, sw, dw, sh, dh, remy;
  34. float invswsh;
  35. srcp = pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
  36. srcdata = (float *)srcp->pixels;
  37. dst = pipi_new(w, h);
  38. dstp = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
  39. dstdata = (float *)dstp->pixels;
  40. sw = src->w; sh = src->h;
  41. dw = dst->w; dh = dst->h;
  42. invswsh = 1.0f / (sw * sh);
  43. aline = malloc(4 * dw * sizeof(float));
  44. line = malloc(4 * dw * sizeof(float));
  45. memset(line, 0, 4 * dw * sizeof(float));
  46. remy = 0;
  47. for(y = 0, y0 = 0; y < dh; y++)
  48. {
  49. int toty = 0, ny;
  50. memset(aline, 0, 4 * dw * sizeof(float));
  51. while(toty < sh)
  52. {
  53. if(remy == 0)
  54. {
  55. float r = 0, g = 0, b = 0, a = 0;
  56. int remx = 0;
  57. for(x = 0, x0 = 0; x < dw; x++)
  58. {
  59. float ar = 0, ag = 0, ab = 0, aa = 0;
  60. int totx = 0, nx;
  61. while(totx < sw)
  62. {
  63. if(remx == 0)
  64. {
  65. r = srcdata[(y0 * sw + x0) * 4];
  66. g = srcdata[(y0 * sw + x0) * 4 + 1];
  67. b = srcdata[(y0 * sw + x0) * 4 + 2];
  68. a = srcdata[(y0 * sw + x0) * 4 + 3];
  69. x0++;
  70. remx = dw;
  71. }
  72. nx = (totx + remx <= sw) ? remx : sw - totx;
  73. ar += nx * r; ag += nx * g; ab += nx * b; aa += nx * a;
  74. totx += nx;
  75. remx -= nx;
  76. }
  77. line[4 * x] = ar;
  78. line[4 * x + 1] = ag;
  79. line[4 * x + 2] = ab;
  80. line[4 * x + 3] = aa;
  81. }
  82. y0++;
  83. remy = dh;
  84. }
  85. ny = (toty + remy <= sh) ? remy : sh - toty;
  86. for(x = 0; x < dw; x++)
  87. {
  88. aline[4 * x] += ny * line[4 * x];
  89. aline[4 * x + 1] += ny * line[4 * x + 1];
  90. aline[4 * x + 2] += ny * line[4 * x + 2];
  91. aline[4 * x + 3] += ny * line[4 * x + 3];
  92. }
  93. toty += ny;
  94. remy -= ny;
  95. }
  96. for(x = 0; x < dw; x++)
  97. {
  98. dstdata[(y * dw + x) * 4] = aline[4 * x] * invswsh;
  99. dstdata[(y * dw + x) * 4 + 1] = aline[4 * x + 1] * invswsh;
  100. dstdata[(y * dw + x) * 4 + 2] = aline[4 * x + 2] * invswsh;
  101. dstdata[(y * dw + x) * 4 + 3] = aline[4 * x + 3] * invswsh;
  102. }
  103. }
  104. free(aline);
  105. free(line);
  106. return dst;
  107. }