



export namespace ImageShaders {



  export const LineArtShader = /*glsl*/ `

  precision highp float;
  uniform sampler2D u_image;
  uniform float u_scale;
  uniform vec2 u_texSize;
  varying vec2 v_texCoord;
  
  const float w1 = 0.147761;
  const float w2 = 0.118318; 
  const float w3 = 0.0947416; 
  
  
  vec4 GaussianBlur(in sampler2D image, in vec2 texCoord, in vec2 pixelSize) {
    vec4 C00 = texture2D(image, texCoord + vec2(-pixelSize.x, -pixelSize.y)) * w3 ;
    vec4 C01 = texture2D(image, texCoord + vec2(0.0, -pixelSize.y)) * w2;
    vec4 C02 = texture2D(image, texCoord + vec2(pixelSize.x, -pixelSize.y)) * w3 ;
  
    vec4 C10 = texture2D(image, texCoord + vec2(-pixelSize.x, 0.0)) * w2;
    vec4 C11 = texture2D(image, texCoord + vec2(0.0, 0.0)) * w1;
    vec4 C12 = texture2D(image, texCoord + vec2(pixelSize.x, 0.0)) * w2;
  
    vec4 C20 = texture2D(image, texCoord + vec2(-pixelSize.x, pixelSize.y)) * w3;
    vec4 C21 = texture2D(image, texCoord + vec2(0.0, pixelSize.y)) * w2;
    vec4 C22 = texture2D(image, texCoord + vec2(pixelSize.x, pixelSize.y)) * w3;
  
    return 
      C00 + C01 + C02 +
      C10 + C11 + C12 +
      C20 + C21 + C22 ;
  }
  
  void main() {
  
    vec4 color = GaussianBlur(u_image, v_texCoord, u_texSize);
    //gl_FragColor = color;
    if(color.w >=0.4) {
      gl_FragColor = vec4(0.15, 0.15, 0.15, 1);
    }else{
        gl_FragColor = vec4(0, 0, 0, 0);
    }
  }
  
  `;



  export const BaseShader = /*glsl*/ `
        precision mediump float;
        uniform sampler2D u_image;
        uniform float u_scale;
        varying vec2 v_texCoord;
        void main() {
             vec4 color = texture2D(u_image, v_texCoord);
             if(u_scale > 1.0 ) {
                if(color.w >=0.4) {
                    gl_FragColor = vec4(0.15, 0.15, 0.15, 1);
                }else{
                    gl_FragColor = vec4(0, 0, 0, 0);
                }
             }else{
                gl_FragColor = vec4(0.15, 0.15, 0.15, color.a);
             }
        }
  `;

  export const Avg9 = /*glsl*/ `

    precision highp float;
    uniform sampler2D u_image;
    uniform float u_scale;
    uniform vec2 u_texSize;
    varying vec2 v_texCoord;
    
    float sample(in sampler2D image, in vec2 texCoord, in vec2 texSize) {
        const int n = 1;
        float total = 0.0;
        float alpha = 0.0;
        float base = 0.0;
        
        for(int i = -n; i<=n ; i++) {
            float ii = float(i);
            for(int j = -n; j<=n ; j++) {
                float jj = float(j);
                vec2 coord = texCoord + vec2(ii * texSize.x, jj * texSize.y);
                vec4 cc = texture2D(image, coord);
                if(i==0  && j==0) base = cc.a;
                total += 1.0;
                alpha += cc.a;
            }
        }
        alpha = alpha /  total;
        return max(alpha, base);
    }
    
    
    void main() {
        if(u_scale > 1.0 ) {
            float alpha = sample(u_image, v_texCoord, u_texSize);
            if(alpha >=0.4) {
                gl_FragColor = vec4(0.15, 0.15, 0.15, 1);
            }else{
                gl_FragColor = vec4(0, 0, 0, 0);
            }
        }else{
            vec4 color = texture2D(u_image, v_texCoord);
            gl_FragColor = vec4(0.15, 0.15, 0.15, color.a);
        }
        
    }

  `;


  export const BilinearManual = /*glsl*/ `

        precision highp float;
        uniform sampler2D u_image;
        uniform float u_scale;
        uniform vec2 u_texSize;
        varying vec2 v_texCoord;
        
        
        void main() {
            vec2 textureSize = 1. / u_texSize;
            vec2 f = fract(v_texCoord * textureSize + .5);
            vec2 uv = (floor(v_texCoord * textureSize - .5) + .5 ) / textureSize ;
            
            vec4 tl = texture2D(u_image, uv);
            vec4 tr = texture2D(u_image, uv + vec2(u_texSize.x, 0.0));
            vec4 bl = texture2D(u_image, uv + vec2(0.0, u_texSize.y));
            vec4 br = texture2D(u_image, uv + vec2(u_texSize.x, u_texSize.y));
            vec4 tA = mix( tl, tr, f.x );
            vec4 tB = mix( bl, br, f.x );
            vec4 result = mix( tA, tB, f.y );
            gl_FragColor = result;
        }

  `;


  export const BicubicLagrange = /*glsl*/ `
  
  precision highp float;
  uniform sampler2D u_image;
  uniform float u_scale;
  uniform vec2 u_texSize;
  varying vec2 v_texCoord;
  
  
  
  float c_x0 = -1.0;
  float c_x1 =  0.0;
  float c_x2 =  1.0;
  float c_x3 =  2.0;
  
  
  
  
  vec4 CubicLagrange(vec4 A, vec4 B, vec4 C, vec4 D, float t) {
    return A *
      ((t - c_x1) / (c_x0 - c_x1) *
      (t - c_x2) / (c_x0 - c_x2) *
      (t - c_x3) / (c_x0 - c_x3)) +
      B *
      ((t - c_x0) / (c_x1 - c_x0) *
      (t - c_x2) / (c_x1 - c_x2) *
      (t - c_x3) / (c_x1 - c_x3)) +
      C *
      ((t - c_x0) / (c_x2 - c_x0) *
      (t - c_x1) / (c_x2 - c_x1) *
      (t - c_x3) / (c_x2 - c_x3)) +
      D *
      ((t - c_x0) / (c_x3 - c_x0) *
      (t - c_x1) / (c_x3 - c_x1) *
      (t - c_x2) / (c_x3 - c_x2));
  }
  
  vec4 BicubicLagrange(sampler2D image, vec2 texCoord, vec2 pixelSize) {
  
    vec2 textureSize = 1. / pixelSize;
    vec2 f = fract(texCoord * textureSize + .5);
    vec2 uv = (floor(texCoord * textureSize - .5) + .5) / textureSize;
  
    vec4 C00 = texture2D(image, uv + vec2(-pixelSize.x, -pixelSize.y));
    vec4 C10 = texture2D(image, uv + vec2(0.0, -pixelSize.y));
    vec4 C20 = texture2D(image, uv + vec2(pixelSize.x, -pixelSize.y));
    vec4 C30 = texture2D(image, uv + vec2(2.0 * pixelSize.x, -pixelSize.y));
  
    vec4 C01 = texture2D(image, uv + vec2(-pixelSize.x, 0.0));
    vec4 C11 = texture2D(image, uv + vec2(0.0, 0.0));
    vec4 C21 = texture2D(image, uv + vec2(pixelSize.x, 0.0));
    vec4 C31 = texture2D(image, uv + vec2(2.0 * pixelSize.x, 0.0));
  
    vec4 C02 = texture2D(image, uv + vec2(-pixelSize.x, pixelSize.y));
    vec4 C12 = texture2D(image, uv + vec2(0.0, pixelSize.y));
    vec4 C22 = texture2D(image, uv + vec2(pixelSize.x, pixelSize.y));
    vec4 C32 = texture2D(image, uv + vec2(2.0 * pixelSize.x, pixelSize.y));
  
    vec4 C03 = texture2D(image, uv + vec2(-pixelSize.x, 2.0 * pixelSize.y));
    vec4 C13 = texture2D(image, uv + vec2(0.0, 2.0 * pixelSize.y));
    vec4 C23 = texture2D(image, uv + vec2(pixelSize.x, 2.0 * pixelSize.y));
    vec4 C33 = texture2D(image, uv + vec2(2.0 * pixelSize.x, 2.0 * pixelSize.y));
  
    vec4 CP0X = CubicLagrange(C00, C10, C20, C30, f.x);
    vec4 CP1X = CubicLagrange(C01, C11, C21, C31, f.x);
    vec4 CP2X = CubicLagrange(C02, C12, C22, C32, f.x);
    vec4 CP3X = CubicLagrange(C03, C13, C23, C33, f.x);
  
    return CubicLagrange(CP0X, CP1X, CP2X, CP3X, f.y);
  
  }
  
  void main() {
    gl_FragColor = BicubicLagrange(u_image, v_texCoord, u_texSize);
  }
  


  `;





  export const CommonAvg9 = /*glsl*/ `

  precision highp float;
  uniform sampler2D u_image;
  uniform float u_scale;
  uniform vec2 u_texSize;
  varying vec2 v_texCoord;
  
  vec4 AvgN(in sampler2D image, in vec2 texCoord, in vec2 pixelSize) {
    const int n = 1;
    float total = 0.0;
    vec4 avg = vec4(0,0,0,0);
    float base = 0.0;
  
    for(int i = -n; i <= n; i++) {
      float ii = float(i);
      for(int j = -n; j <= n; j++) {
        float jj = float(j);
        vec2 coord = texCoord + vec2(ii * pixelSize.x, jj * pixelSize.y);
        vec4 cc = texture2D(image, coord);
        total += 1.0;
        avg += cc;
      }
    }
    avg = avg / total;
    return avg;
  }
  
  void main() {
    gl_FragColor = AvgN(u_image, v_texCoord, u_texSize);
  }

`;

  export const CommonAvg5 = /*glsl*/ `

precision highp float;
uniform sampler2D u_image;
uniform float u_scale;
uniform vec2 u_texSize;
varying vec2 v_texCoord;

vec4 Avg5(in sampler2D image, in vec2 texCoord, in vec2 pixelSize) {
  vec4 C0 = texture2D(image, texCoord );
  vec4 C1 = texture2D(image, texCoord + vec2(pixelSize.x, pixelSize.y));
  vec4 C2 = texture2D(image, texCoord + vec2(pixelSize.x, -pixelSize.y));
  vec4 C3 = texture2D(image, texCoord + vec2(-pixelSize.x, pixelSize.y));
  vec4 C4 = texture2D(image, texCoord + vec2(-pixelSize.x, -pixelSize.y));
  return (C0 + C1 + C2 + C3 + C4) / 5.0;
}

void main() {
  gl_FragColor = Avg5(u_image, v_texCoord, u_texSize);
}

`;






  export const CommonBlur = /*glsl*/ `

  precision highp float;
  uniform sampler2D u_image;
  uniform float u_scale;
  uniform vec2 u_texSize;
  varying vec2 v_texCoord;
  
  vec4 Blur(in sampler2D image, in vec2 texCoord, in vec2 pixelSize) {
    const int n = 1;
    float total = 0.0;
    vec4 avg = vec4(0,0,0,0);
    float base = 0.0;
  
    for(int i = -n; i <= n; i++) {
      float ii = float(i);
      for(int j = -n; j <= n; j++) {
        float jj = float(j);
        vec2 coord = texCoord + vec2(ii * pixelSize.x, jj * pixelSize.y);
        vec4 cc = texture2D(image, coord);
        total += 1.0;
        avg += cc;
      }
    }
    avg = avg / total;
    return avg;
  }
  
  void main() {
    vec4 color = Blur(u_image, v_texCoord, u_texSize);
    gl_FragColor = color;
  }

`;




  export const CommonGaussianBlur = /*glsl*/ `

precision highp float;
uniform sampler2D u_image;
uniform float u_scale;
uniform vec2 u_texSize;
varying vec2 v_texCoord;

const float w1 = 0.147761;
const float w2 = 0.118318; 
const float w3 = 0.0947416; 


vec4 GaussianBlur(in sampler2D image, in vec2 texCoord, in vec2 pixelSize) {
  vec4 C00 = texture2D(image, texCoord + vec2(-pixelSize.x, -pixelSize.y)) * w3 ;
  vec4 C01 = texture2D(image, texCoord + vec2(0.0, -pixelSize.y)) * w2;
  vec4 C02 = texture2D(image, texCoord + vec2(pixelSize.x, -pixelSize.y)) * w3 ;

  vec4 C10 = texture2D(image, texCoord + vec2(-pixelSize.x, 0.0)) * w2;
  vec4 C11 = texture2D(image, texCoord + vec2(0.0, 0.0)) * w1;
  vec4 C12 = texture2D(image, texCoord + vec2(pixelSize.x, 0.0)) * w2;

  vec4 C20 = texture2D(image, texCoord + vec2(-pixelSize.x, pixelSize.y)) * w3;
  vec4 C21 = texture2D(image, texCoord + vec2(0.0, pixelSize.y)) * w2;
  vec4 C22 = texture2D(image, texCoord + vec2(pixelSize.x, pixelSize.y)) * w3;

  return 
    C00 + C01 + C02 +
    C10 + C11 + C12 +
    C20 + C21 + C22 ;
}

void main() {

  vec4 color = GaussianBlur(u_image, v_texCoord, u_texSize);
  gl_FragColor = color;
}

`;


export const CommonGaussianBlurCut = /*glsl*/ `

precision highp float;
uniform sampler2D u_image;
uniform float u_scale;
uniform vec2 u_texSize;
varying vec2 v_texCoord;

const float w1 = 0.147761;
const float w2 = 0.118318; 
const float w3 = 0.0947416; 


vec4 GaussianBlur(in sampler2D image, in vec2 texCoord, in vec2 pixelSize) {
  vec4 C00 = texture2D(image, texCoord + vec2(-pixelSize.x, -pixelSize.y)) * w3 ;
  vec4 C01 = texture2D(image, texCoord + vec2(0.0, -pixelSize.y)) * w2;
  vec4 C02 = texture2D(image, texCoord + vec2(pixelSize.x, -pixelSize.y)) * w3 ;

  vec4 C10 = texture2D(image, texCoord + vec2(-pixelSize.x, 0.0)) * w2;
  vec4 C11 = texture2D(image, texCoord + vec2(0.0, 0.0)) * w1;
  vec4 C12 = texture2D(image, texCoord + vec2(pixelSize.x, 0.0)) * w2;

  vec4 C20 = texture2D(image, texCoord + vec2(-pixelSize.x, pixelSize.y)) * w3;
  vec4 C21 = texture2D(image, texCoord + vec2(0.0, pixelSize.y)) * w2;
  vec4 C22 = texture2D(image, texCoord + vec2(pixelSize.x, pixelSize.y)) * w3;

  return 
    C00 + C01 + C02 +
    C10 + C11 + C12 +
    C20 + C21 + C22 ;
}

void main() {

  vec4 color = GaussianBlur(u_image, v_texCoord, u_texSize);
  gl_FragColor = color;

  if(color.a >= 0.4) {
    gl_FragColor = color;
  }else{
    gl_FragColor = vec4(0,0,0,0);
  }
  
}

`;



  export const BicubicHermite = /*glsl*/ `

precision highp float;
uniform sampler2D u_image;
uniform float u_scale;
uniform vec2 u_texSize;
varying vec2 v_texCoord;


vec4 CubicHermite(vec4 A, vec4 B, vec4 C, vec4 D, float t) {
  float t2 = t * t;
  float t3 = t * t * t;
  vec4 a = -A / 2.0 + (3.0 * B) / 2.0 - (3.0 * C) / 2.0 + D / 2.0;
  vec4 b = A - (5.0 * B) / 2.0 + 2.0 * C - D / 2.0;
  vec4 c = -A / 2.0 + C / 2.0;
  vec4 d = B;

  return a * t3 + b * t2 + c * t + d;
}

vec4 BicubicHermite(sampler2D image, vec2 texCoord, vec2 pixelSize) {

  vec2 textureSize = 1. / pixelSize;
  vec2 f = fract(texCoord * textureSize + .5);
  vec2 uv = (floor(texCoord * textureSize - .5) + .5) / textureSize;

  vec4 C00 = texture2D(image, uv + vec2(-pixelSize.x, -pixelSize.y));
  vec4 C10 = texture2D(image, uv + vec2(0.0, -pixelSize.y));
  vec4 C20 = texture2D(image, uv + vec2(pixelSize.x, -pixelSize.y));
  vec4 C30 = texture2D(image, uv + vec2(2.0 * pixelSize.x, -pixelSize.y));

  vec4 C01 = texture2D(image, uv + vec2(-pixelSize.x, 0.0));
  vec4 C11 = texture2D(image, uv + vec2(0.0, 0.0));
  vec4 C21 = texture2D(image, uv + vec2(pixelSize.x, 0.0));
  vec4 C31 = texture2D(image, uv + vec2(2.0 * pixelSize.x, 0.0));

  vec4 C02 = texture2D(image, uv + vec2(-pixelSize.x, pixelSize.y));
  vec4 C12 = texture2D(image, uv + vec2(0.0, pixelSize.y));
  vec4 C22 = texture2D(image, uv + vec2(pixelSize.x, pixelSize.y));
  vec4 C32 = texture2D(image, uv + vec2(2.0 * pixelSize.x, pixelSize.y));

  vec4 C03 = texture2D(image, uv + vec2(-pixelSize.x, 2.0 * pixelSize.y));
  vec4 C13 = texture2D(image, uv + vec2(0.0, 2.0 * pixelSize.y));
  vec4 C23 = texture2D(image, uv + vec2(pixelSize.x, 2.0 * pixelSize.y));
  vec4 C33 = texture2D(image, uv + vec2(2.0 * pixelSize.x, 2.0 * pixelSize.y));

  vec4 CP0X = CubicHermite(C00, C10, C20, C30, f.x);
  vec4 CP1X = CubicHermite(C01, C11, C21, C31, f.x);
  vec4 CP2X = CubicHermite(C02, C12, C22, C32, f.x);
  vec4 CP3X = CubicHermite(C03, C13, C23, C33, f.x);

  return CubicHermite(CP0X, CP1X, CP2X, CP3X, f.y);

}

void main() {
  gl_FragColor = BicubicHermite(u_image, v_texCoord, u_texSize);
}


`;


}
