areaScaleFastFragmentShader.glsl 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /*
  3. * This file is part of the LibreOffice project.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. */
  9. /* TODO Use textureOffset for newest version of GLSL */
  10. #version 130
  11. uniform sampler2D sampler;
  12. uniform int xscale;
  13. uniform int yscale;
  14. uniform float xstep;
  15. uniform float ystep;
  16. uniform float ratio; // = 1.0/(xscale*yscale)
  17. varying vec2 tex_coord;
  18. // This mode makes the scaling work like maskedTextureFragmentShader.glsl
  19. // (instead of like plain textureVertexShader.glsl).
  20. #ifdef MASKED
  21. varying vec2 mask_coord;
  22. uniform sampler2D mask;
  23. #endif
  24. /*
  25. Just make the resulting color the average of all the source pixels
  26. (which is an area (xscale)x(yscale) ).
  27. */
  28. void main(void)
  29. {
  30. vec4 sum = vec4( 0.0, 0.0, 0.0, 0.0 );
  31. vec2 offset = vec2( 0.0, 0.0 );
  32. for( int y = 0; y < yscale; ++y )
  33. {
  34. for( int x = 0; x < xscale; ++x )
  35. {
  36. #ifndef MASKED
  37. sum += texture2D( sampler, tex_coord.st + offset );
  38. #else
  39. vec4 texel;
  40. texel = texture2D( sampler, tex_coord.st + offset );
  41. texel.a = 1.0 - texture2D( mask, mask_coord.st + offset ).r;
  42. sum += texel;
  43. #endif
  44. offset.x += xstep;
  45. }
  46. offset.y += ystep;
  47. offset.x = 0.0;
  48. }
  49. sum *= ratio;
  50. gl_FragColor = sum;
  51. }
  52. /* vim:set shiftwidth=4 softtabstop=4 expandtab: */