Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9142627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:49:30+00:00 2026-06-17T09:49:30+00:00

I am using WebGL to resize images clientside very quickly within an app I

  • 0

I am using WebGL to resize images clientside very quickly within an app I am working on. I have written a GLSL shader that performs simple bilinear filtering on the images that I am downsizing.

It works fine for the most part but there are many occasions where the resize is huge e.g. from a 2048×2048 image down to 110×110 in order to generate a thumbnail. In these instances the quality is poor and far too blurry.

My current GLSL shader is as follows:

uniform float textureSizeWidth;\
uniform float textureSizeHeight;\
uniform float texelSizeX;\
uniform float texelSizeY;\
varying mediump vec2 texCoord;\
uniform sampler2D texture;\
\
vec4 tex2DBiLinear( sampler2D textureSampler_i, vec2 texCoord_i )\
{\
    vec4 p0q0 = texture2D(textureSampler_i, texCoord_i);\
    vec4 p1q0 = texture2D(textureSampler_i, texCoord_i + vec2(texelSizeX, 0));\
\
    vec4 p0q1 = texture2D(textureSampler_i, texCoord_i + vec2(0, texelSizeY));\
    vec4 p1q1 = texture2D(textureSampler_i, texCoord_i + vec2(texelSizeX , texelSizeY));\
\
    float a = fract( texCoord_i.x * textureSizeWidth );\
\
    vec4 pInterp_q0 = mix( p0q0, p1q0, a );\
    vec4 pInterp_q1 = mix( p0q1, p1q1, a );\
\
    float b = fract( texCoord_i.y * textureSizeHeight );\
    return mix( pInterp_q0, pInterp_q1, b );\
}\
void main() { \
\
    gl_FragColor = tex2DBiLinear(texture,texCoord);\
}');

TexelsizeX and TexelsizeY are simply (1.0 / texture width) and height respectively…

I would like to implement a higher quality filtering technique, ideally a [Lancosz][1] filter which should produce far better results but I cannot seem to get my head around how to implement the algorithm with GLSL as I am very new to WebGL and GLSL in general.

Could anybody point me in the right direction?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:49:31+00:00Added an answer on June 17, 2026 at 9:49 am

    If you’re looking for Lanczos resampling, the following is the shader program I use in my open source GPUImage library:

    Vertex shader:

     attribute vec4 position;
     attribute vec2 inputTextureCoordinate;
    
     uniform float texelWidthOffset;
     uniform float texelHeightOffset;
    
     varying vec2 centerTextureCoordinate;
     varying vec2 oneStepLeftTextureCoordinate;
     varying vec2 twoStepsLeftTextureCoordinate;
     varying vec2 threeStepsLeftTextureCoordinate;
     varying vec2 fourStepsLeftTextureCoordinate;
     varying vec2 oneStepRightTextureCoordinate;
     varying vec2 twoStepsRightTextureCoordinate;
     varying vec2 threeStepsRightTextureCoordinate;
     varying vec2 fourStepsRightTextureCoordinate;
    
     void main()
     {
         gl_Position = position;
    
         vec2 firstOffset = vec2(texelWidthOffset, texelHeightOffset);
         vec2 secondOffset = vec2(2.0 * texelWidthOffset, 2.0 * texelHeightOffset);
         vec2 thirdOffset = vec2(3.0 * texelWidthOffset, 3.0 * texelHeightOffset);
         vec2 fourthOffset = vec2(4.0 * texelWidthOffset, 4.0 * texelHeightOffset);
    
         centerTextureCoordinate = inputTextureCoordinate;
         oneStepLeftTextureCoordinate = inputTextureCoordinate - firstOffset;
         twoStepsLeftTextureCoordinate = inputTextureCoordinate - secondOffset;
         threeStepsLeftTextureCoordinate = inputTextureCoordinate - thirdOffset;
         fourStepsLeftTextureCoordinate = inputTextureCoordinate - fourthOffset;
         oneStepRightTextureCoordinate = inputTextureCoordinate + firstOffset;
         twoStepsRightTextureCoordinate = inputTextureCoordinate + secondOffset;
         threeStepsRightTextureCoordinate = inputTextureCoordinate + thirdOffset;
         fourStepsRightTextureCoordinate = inputTextureCoordinate + fourthOffset;
     }
    

    Fragment shader:

     precision highp float;
    
     uniform sampler2D inputImageTexture;
    
     varying vec2 centerTextureCoordinate;
     varying vec2 oneStepLeftTextureCoordinate;
     varying vec2 twoStepsLeftTextureCoordinate;
     varying vec2 threeStepsLeftTextureCoordinate;
     varying vec2 fourStepsLeftTextureCoordinate;
     varying vec2 oneStepRightTextureCoordinate;
     varying vec2 twoStepsRightTextureCoordinate;
     varying vec2 threeStepsRightTextureCoordinate;
     varying vec2 fourStepsRightTextureCoordinate;
    
     // sinc(x) * sinc(x/a) = (a * sin(pi * x) * sin(pi * x / a)) / (pi^2 * x^2)
     // Assuming a Lanczos constant of 2.0, and scaling values to max out at x = +/- 1.5
    
     void main()
     {
         lowp vec4 fragmentColor = texture2D(inputImageTexture, centerTextureCoordinate) * 0.38026;
    
         fragmentColor += texture2D(inputImageTexture, oneStepLeftTextureCoordinate) * 0.27667;
         fragmentColor += texture2D(inputImageTexture, oneStepRightTextureCoordinate) * 0.27667;
    
         fragmentColor += texture2D(inputImageTexture, twoStepsLeftTextureCoordinate) * 0.08074;
         fragmentColor += texture2D(inputImageTexture, twoStepsRightTextureCoordinate) * 0.08074;
    
         fragmentColor += texture2D(inputImageTexture, threeStepsLeftTextureCoordinate) * -0.02612;
         fragmentColor += texture2D(inputImageTexture, threeStepsRightTextureCoordinate) * -0.02612;
    
         fragmentColor += texture2D(inputImageTexture, fourStepsLeftTextureCoordinate) * -0.02143;
         fragmentColor += texture2D(inputImageTexture, fourStepsRightTextureCoordinate) * -0.02143;
    
         gl_FragColor = fragmentColor;
     }
    

    This is applied in two passes, with the first performing a horizontal downsampling and the second a vertical downsampling. The texelWidthOffset and texelHeightOffset uniforms are alternately set to 0.0 and the width fraction or height fraction of a single pixel in the image.

    I hard-calculate the texel offsets in the vertex shader because this avoids dependent texture reads on the mobile devices I’m targeting with this, leading to significantly better performance there. It is a little verbose, though.

    Results from this Lanczos resampling:

    Lanczos

    Normal bilinear downsampling:

    Bilinear

    Nearest-neighbor downsampling:

    Nearest-neighbor

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a small game using webgl. Within this game I have
I have a WebGL fragment shader that I am using to do raytracing. I
I have developed a WebGL bike configurator demo using Three.js. Given the fact that
I'm writing a fragment shader for WebGL(GLSL ES 1.0) using the latest version of
Environment: WebGL, Chrome. I have the following behavior when using transparent png's as textures
I have a few shader scripts written in ARB assemby which are supposed to
I have learned a bit of webGL, using three.js mainly. I load .obj files
Is possible resize geometric shapes on the fly using JavaScript? Example: I have a
I have got myself a nice shape using webGL, but when i use the
I am working on drawing large directed acyclic graphs in WebGL using the gwt-g3d

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.