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.

ScaleFilters.cxx 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Copyright (C) 2006 TightVNC Team. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #include <string.h>
  19. #include <assert.h>
  20. #include <math.h>
  21. #include <rfb/Rect.h>
  22. #include <rfb/ScaleFilters.h>
  23. using namespace rfb;
  24. //
  25. // -=- 1-D filters functions
  26. //
  27. // Nearest neighbor filter function
  28. double nearest_neighbor(double x) {
  29. if (x < -0.5) return 0.0;
  30. if (x < 0.5) return 1.0;
  31. return 0.0;
  32. }
  33. // Linear filter function
  34. double linear(double x) {
  35. if (x < -1.0) return 0.0;
  36. if (x < 0.0) return 1.0+x;
  37. if (x < 1.0) return 1.0-x;
  38. return 0.0;
  39. }
  40. // Cubic filter functions
  41. double cubic(double x) {
  42. double t;
  43. if (x < -2.0) return 0.0;
  44. if (x < -1.0) {t = 2.0+x; return t*t*t/6.0;}
  45. if (x < 0.0) return (4.0+x*x*(-6.0+x*-3.0))/6.0;
  46. if (x < 1.0) return (4.0+x*x*(-6.0+x*3.0))/6.0;
  47. if (x < 2.0) {t = 2.0-x; return t*t*t/6.0;}
  48. return 0.0;
  49. }
  50. //
  51. // -=- ScaleFilters class
  52. //
  53. SFilter &ScaleFilters::operator[](unsigned int filter_id) {
  54. assert(filter_id <= scaleFilterMaxNumber);
  55. return filters[filter_id];
  56. }
  57. int ScaleFilters::getFilterIdByName(char *filterName) {
  58. for (unsigned int i = 0; i <= scaleFilterMaxNumber; i++) {
  59. if (strcasecmp(filters[i].name, filterName) == 0) return i;
  60. }
  61. return -1;
  62. }
  63. void ScaleFilters::initFilters() {
  64. filters[scaleFilterNearestNeighbor] = create("Nearest neighbor", 0.5, nearest_neighbor);
  65. filters[scaleFilterBilinear] = create("Bilinear", 1, linear);
  66. filters[scaleFilterBicubic] = create("Bicubic", 2, cubic);
  67. }
  68. SFilter ScaleFilters::create(const char *name_, double radius_, filter_func func_) {
  69. SFilter filter;
  70. strncpy(filter.name, name_, sizeof(filter.name)-1);
  71. filter.name[sizeof(filter.name)-1] = '\0';
  72. filter.radius = radius_;
  73. filter.func = func_;
  74. return filter;
  75. }
  76. void ScaleFilters::makeWeightTabs(int filter_id, int src_x, int dst_x, SFilterWeightTab **pWeightTabs) {
  77. double sxc;
  78. double offset = 0.5;
  79. double ratio = (double)dst_x / src_x;
  80. double sourceScale = __rfbmax(1.0, 1.0/ratio);
  81. double sourceRadius = __rfbmax(0.5, sourceScale * filters[filter_id].radius);
  82. double sum, nc;
  83. int i, ci;
  84. SFilter sFilter = filters[filter_id];
  85. *pWeightTabs = new SFilterWeightTab[dst_x];
  86. SFilterWeightTab *weightTabs = *pWeightTabs;
  87. // Make the weight tab for the each dest x position
  88. for (int x = 0; x < dst_x; x++) {
  89. sxc = (double(x)+offset) / ratio;
  90. // Calculate the scale filter interval, [i0, i1)
  91. int i0 = int(__rfbmax(sxc-sourceRadius+0.5, 0));
  92. int i1 = int(__rfbmin(sxc+sourceRadius+0.5, src_x));
  93. weightTabs[x].i0 = i0; weightTabs[x].i1 = i1;
  94. weightTabs[x].weight = new short[i1-i0];
  95. // Calculate coeff to normalize the filter weights
  96. for (sum = 0, i = i0; i < i1; i++) sum += sFilter.func((double(i)-sxc+0.5)/sourceScale);
  97. if (sum == 0.) nc = (double)(WEIGHT_OF_ONE); else nc = (double)(WEIGHT_OF_ONE)/sum;
  98. // Calculate the weight coeffs on the scale filter interval
  99. for (ci = 0, i = i0; i < i1; i++) {
  100. weightTabs[x].weight[ci++] = (short)floor((sFilter.func((double(i)-sxc+0.5)/sourceScale) * nc) + 0.5);
  101. }
  102. }
  103. }