imagefilters.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. Copyright (C) 2015 Aaron Suen <warr1024@gmail.com>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License along
  12. with this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  14. */
  15. #include "imagefilters.h"
  16. #include "util/numeric.h"
  17. #include <cmath>
  18. /* Fill in RGB values for transparent pixels, to correct for odd colors
  19. * appearing at borders when blending. This is because many PNG optimizers
  20. * like to discard RGB values of transparent pixels, but when blending then
  21. * with non-transparent neighbors, their RGB values will shpw up nonetheless.
  22. *
  23. * This function modifies the original image in-place.
  24. *
  25. * Parameter "threshold" is the alpha level below which pixels are considered
  26. * transparent. Should be 127 for 3d where alpha is threshold, but 0 for
  27. * 2d where alpha is blended.
  28. */
  29. void imageCleanTransparent(video::IImage *src, u32 threshold)
  30. {
  31. core::dimension2d<u32> dim = src->getDimension();
  32. // Walk each pixel looking for fully transparent ones.
  33. // Note: loop y around x for better cache locality.
  34. for (u32 ctry = 0; ctry < dim.Height; ctry++)
  35. for (u32 ctrx = 0; ctrx < dim.Width; ctrx++) {
  36. // Ignore opaque pixels.
  37. irr::video::SColor c = src->getPixel(ctrx, ctry);
  38. if (c.getAlpha() > threshold)
  39. continue;
  40. // Sample size and total weighted r, g, b values.
  41. u32 ss = 0, sr = 0, sg = 0, sb = 0;
  42. // Walk each neighbor pixel (clipped to image bounds).
  43. for (u32 sy = (ctry < 1) ? 0 : (ctry - 1);
  44. sy <= (ctry + 1) && sy < dim.Height; sy++)
  45. for (u32 sx = (ctrx < 1) ? 0 : (ctrx - 1);
  46. sx <= (ctrx + 1) && sx < dim.Width; sx++) {
  47. // Ignore transparent pixels.
  48. irr::video::SColor d = src->getPixel(sx, sy);
  49. if (d.getAlpha() <= threshold)
  50. continue;
  51. // Add RGB values weighted by alpha.
  52. u32 a = d.getAlpha();
  53. ss += a;
  54. sr += a * d.getRed();
  55. sg += a * d.getGreen();
  56. sb += a * d.getBlue();
  57. }
  58. // If we found any neighbor RGB data, set pixel to average
  59. // weighted by alpha.
  60. if (ss > 0) {
  61. c.setRed(sr / ss);
  62. c.setGreen(sg / ss);
  63. c.setBlue(sb / ss);
  64. src->setPixel(ctrx, ctry, c);
  65. }
  66. }
  67. }
  68. /* Scale a region of an image into another image, using nearest-neighbor with
  69. * anti-aliasing; treat pixels as crisp rectangles, but blend them at boundaries
  70. * to prevent non-integer scaling ratio artifacts. Note that this may cause
  71. * some blending at the edges where pixels don't line up perfectly, but this
  72. * filter is designed to produce the most accurate results for both upscaling
  73. * and downscaling.
  74. */
  75. void imageScaleNNAA(video::IImage *src, const core::rect<s32> &srcrect, video::IImage *dest)
  76. {
  77. double sx, sy, minsx, maxsx, minsy, maxsy, area, ra, ga, ba, aa, pw, ph, pa;
  78. u32 dy, dx;
  79. video::SColor pxl;
  80. // Cache rectsngle boundaries.
  81. double sox = srcrect.UpperLeftCorner.X * 1.0;
  82. double soy = srcrect.UpperLeftCorner.Y * 1.0;
  83. double sw = srcrect.getWidth() * 1.0;
  84. double sh = srcrect.getHeight() * 1.0;
  85. // Walk each destination image pixel.
  86. // Note: loop y around x for better cache locality.
  87. core::dimension2d<u32> dim = dest->getDimension();
  88. for (dy = 0; dy < dim.Height; dy++)
  89. for (dx = 0; dx < dim.Width; dx++) {
  90. // Calculate floating-point source rectangle bounds.
  91. // Do some basic clipping, and for mirrored/flipped rects,
  92. // make sure min/max are in the right order.
  93. minsx = sox + (dx * sw / dim.Width);
  94. minsx = rangelim(minsx, 0, sw);
  95. maxsx = minsx + sw / dim.Width;
  96. maxsx = rangelim(maxsx, 0, sw);
  97. if (minsx > maxsx)
  98. SWAP(double, minsx, maxsx);
  99. minsy = soy + (dy * sh / dim.Height);
  100. minsy = rangelim(minsy, 0, sh);
  101. maxsy = minsy + sh / dim.Height;
  102. maxsy = rangelim(maxsy, 0, sh);
  103. if (minsy > maxsy)
  104. SWAP(double, minsy, maxsy);
  105. // Total area, and integral of r, g, b values over that area,
  106. // initialized to zero, to be summed up in next loops.
  107. area = 0;
  108. ra = 0;
  109. ga = 0;
  110. ba = 0;
  111. aa = 0;
  112. // Loop over the integral pixel positions described by those bounds.
  113. for (sy = floor(minsy); sy < maxsy; sy++)
  114. for (sx = floor(minsx); sx < maxsx; sx++) {
  115. // Calculate width, height, then area of dest pixel
  116. // that's covered by this source pixel.
  117. pw = 1;
  118. if (minsx > sx)
  119. pw += sx - minsx;
  120. if (maxsx < (sx + 1))
  121. pw += maxsx - sx - 1;
  122. ph = 1;
  123. if (minsy > sy)
  124. ph += sy - minsy;
  125. if (maxsy < (sy + 1))
  126. ph += maxsy - sy - 1;
  127. pa = pw * ph;
  128. // Get source pixel and add it to totals, weighted
  129. // by covered area and alpha.
  130. pxl = src->getPixel((u32)sx, (u32)sy);
  131. area += pa;
  132. ra += pa * pxl.getRed();
  133. ga += pa * pxl.getGreen();
  134. ba += pa * pxl.getBlue();
  135. aa += pa * pxl.getAlpha();
  136. }
  137. // Set the destination image pixel to the average color.
  138. if (area > 0) {
  139. pxl.setRed(ra / area + 0.5);
  140. pxl.setGreen(ga / area + 0.5);
  141. pxl.setBlue(ba / area + 0.5);
  142. pxl.setAlpha(aa / area + 0.5);
  143. } else {
  144. pxl.setRed(0);
  145. pxl.setGreen(0);
  146. pxl.setBlue(0);
  147. pxl.setAlpha(0);
  148. }
  149. dest->setPixel(dx, dy, pxl);
  150. }
  151. }