@scandit/web-datacapture-core
Version:
Scandit Data Capture SDK for the Web
35 lines (30 loc) • 6.03 kB
JavaScript
import {b,a as a$4}from'./chunk-SK6L34IF.js';import {a as a$2}from'./chunk-DZKZ6RMW.js';import {a}from'./chunk-NQF3GUK2.js';import {a as a$3}from'./chunk-QHBIT2WY.js';import {a as a$1}from'./chunk-XR65N6EG.js';var v=`
attribute vec2 a_position;
varying vec2 v_texcoord;
void main() {
gl_Position = vec4(a_position, 0, 1);
v_texcoord = a_position * 0.5 + 0.5;
}
`,R=`
precision mediump float;
varying vec2 v_texcoord;
uniform sampler2D u_texture;
uniform float x_spacing; // Uniform for x-spacing between pixels
// Function to calculate grayscale
float toGray(vec4 color) {
return dot(color.rgb, vec3(0.299, 0.587, 0.114));
}
void main() {
// Sample adjacent pixels with spacing of 1/width
vec4 a = texture2D(u_texture, v_texcoord + vec2(0.0 * x_spacing, 0.0));
vec4 b = texture2D(u_texture, v_texcoord + vec2(1.0 * x_spacing, 0.0));
vec4 c = texture2D(u_texture, v_texcoord + vec2(2.0 * x_spacing, 0.0));
vec4 d = texture2D(u_texture, v_texcoord + vec2(3.0 * x_spacing, 0.0));
// Calculate grayscale values
float grayscaleA = toGray(a);
float grayscaleB = toGray(b);
float grayscaleC = toGray(c);
float grayscaleD = toGray(d);
// Set the final color with grayscale values
gl_FragColor = vec4(grayscaleA, grayscaleB, grayscaleC, grayscaleD);
}`,p=class extends a{constructor(t,e){super(t,e);this.colorType="GRAYSCALE";this._useRgbaFallback=false;this._positions=new Float32Array([-1,-1,-1,1,1,-1,1,-1,-1,1,1,1]);this._canvas=document.createElement("canvas");}get _webglContext(){var t;if(!this._ctx){if(this._ctx=(t=this._canvas)==null?void 0:t.getContext("webgl",{antialias:false}),!this._ctx)throw new Error("Cannot get WebGL context");this.setup();}return this._ctx}readFromSource(t){var c,g;let e=(c=t==null?void 0:t.videoWidth)!=null?c:this._contextWebGL.drawingBufferWidth,i=e%4!==0;if(i!==this._useRgbaFallback&&(i?(a$1.debug("[GrayScaleFrameReader] Video width not divisible by 4, falling back to RGBA reader for better performance",{width:e}),this._rgbaFallbackReader||(this._rgbaFallbackReader=new a$2(this._contextWebGL,{minPoolCapacity:this._minPoolCapacity,maxPoolCapacity:this._maxPoolCapacity}),this._rgbaFallbackReader.setup())):a$1.log(a$1.Level.Info,"[GrayScaleFrameReader] Video width now divisible by 4, using optimized grayscale reader",{width:e}),this._useRgbaFallback=i),this._useRgbaFallback&&this._rgbaFallbackReader)return this._rgbaFallbackReader.readFromSource(t);this.updateCanvasSizeIfNeeded(),this.updateFrameSizeIfNeeded();let{drawingBufferWidth:n,drawingBufferHeight:r,TEXTURE_2D:u,RGBA:s,UNSIGNED_BYTE:h,TRIANGLES:m}=this._webglContext;this._webglContext.viewport(0,0,n,r);let w=this._webglContext.getUniformLocation(this._shaderProgram,"x_spacing"),C=1/e;this._webglContext.uniform1f(w,C),this._webglContext.texImage2D(u,0,s,s,h,t),this._webglContext.drawArrays(m,0,6);let a=(g=this._framePool)==null?void 0:g.pop();if(!a)throw new Error("Frame pool found empty!");let l=n*r*4;return a.length!==l&&(a$1.warn("[GrayScaleFrameReader] Buffer from pool has wrong size, allocating new one",{bufferLength:a.length,requiredSize:l}),a=new Uint8ClampedArray(l)),this._webglContext.readPixels(0,0,n,r,s,h,a),{width:e,height:r,data:new Uint8ClampedArray(a.buffer,0,e*r),colorType:"GRAYSCALE"}}setup(){this.initShaderProgram(),this.initBuffers(),this.initTexture();}dispose(){var t;this._ctx&&(this._shaderProgram&&(this._ctx.deleteProgram(this._shaderProgram),this._shaderProgram=null),this._positionBuffer&&(this._ctx.deleteBuffer(this._positionBuffer),this._positionBuffer=null),this._texture&&(this._ctx.deleteTexture(this._texture),this._texture=null),this._ctx=null),this._rgbaFallbackReader&&(this._rgbaFallbackReader.dispose(),this._rgbaFallbackReader=null),(t=this._framePool)==null||t.empty(),this._canvas=void 0;}updateCanvasSizeIfNeeded(){if(!this._canvas)return;let t=Math.ceil(this._contextWebGL.drawingBufferWidth/4);(this._canvas.width!==t||this._canvas.height!==this._contextWebGL.drawingBufferHeight)&&(this._canvas.width=t,this._canvas.height=this._contextWebGL.drawingBufferHeight);}updateFrameSizeIfNeeded(){let{drawingBufferWidth:t,drawingBufferHeight:e}=this._webglContext,i=t*e*4;this._frameSize!==i&&(this._frameSize=i,this._framePool&&this._framePool.empty(),this._framePool=new a$3({capacity:this._maxPoolCapacity,lowWaterMark:this._minPoolCapacity,createItem:()=>new Uint8ClampedArray(this._frameSize)}));}initShaderProgram(){let t=b(this._webglContext,this._webglContext.VERTEX_SHADER,v),e=b(this._webglContext,this._webglContext.FRAGMENT_SHADER,R);if(!t||!e)throw new Error("Shaders cannot be created");this._shaderProgram=a$4(this._webglContext,t,e),this._webglContext.useProgram(this._shaderProgram);}initBuffers(){this._positionBuffer=this._webglContext.createBuffer(),this._webglContext.bindBuffer(this._webglContext.ARRAY_BUFFER,this._positionBuffer),this._webglContext.bufferData(this._webglContext.ARRAY_BUFFER,this._positions,this._webglContext.STATIC_DRAW);let t=this._webglContext.getAttribLocation(this._shaderProgram,"a_position");this._webglContext.enableVertexAttribArray(t),this._webglContext.bindBuffer(this._webglContext.ARRAY_BUFFER,this._positionBuffer),this._webglContext.vertexAttribPointer(t,2,this._webglContext.FLOAT,false,0,0);}initTexture(){this._texture=this._webglContext.createTexture(),this._webglContext.bindTexture(this._webglContext.TEXTURE_2D,this._texture),this._webglContext.texParameteri(this._webglContext.TEXTURE_2D,this._webglContext.TEXTURE_WRAP_S,this._webglContext.CLAMP_TO_EDGE),this._webglContext.texParameteri(this._webglContext.TEXTURE_2D,this._webglContext.TEXTURE_WRAP_T,this._webglContext.CLAMP_TO_EDGE),this._webglContext.texParameteri(this._webglContext.TEXTURE_2D,this._webglContext.TEXTURE_MIN_FILTER,this._webglContext.LINEAR),this._webglContext.texParameteri(this._webglContext.TEXTURE_2D,this._webglContext.TEXTURE_MAG_FILTER,this._webglContext.LINEAR);let t=this._webglContext.getUniformLocation(this._shaderProgram,"u_texture");this._webglContext.uniform1i(t,0);}};export{p as a};