-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathblendimage_filter.class.ts
250 lines (233 loc) · 6.93 KB
/
blendimage_filter.class.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// @ts-nocheck
import { Image } from '../shapes/image.class';
import { TClassProperties } from '../typedefs';
import { createCanvasElement } from '../util/misc/dom';
import { AbstractBaseFilter } from './base_filter.class';
import {
T2DPipelineState,
TWebGLPipelineState,
TWebGLUniformLocationMap,
} from './typedefs';
import { WebGLFilterBackend } from './webgl_backend.class';
/**
* Image Blend filter class
* @example
* const filter = new filters.BlendColor({
* color: '#000',
* mode: 'multiply'
* });
*
* const filter = new BlendImage({
* image: fabricImageObject,
* mode: 'multiply',
* alpha: 0.5
* });
* object.filters.push(filter);
* object.applyFilters();
* canvas.renderAll();
*/
export class BlendImage extends AbstractBaseFilter<Record<string, string>> {
/**
* Color to make the blend operation with. default to a reddish color since black or white
* gives always strong result.
**/
image: Image;
mode: 'multiply' | 'mask';
/**
* alpha value. represent the strength of the blend image operation.
* not implemented.
**/
alpha: number;
getCacheKey() {
return `${this.type}_${this.mode}`;
}
getFragmentSource(): string {
return this.fragmentSource[this.mode];
}
applyToWebGL(options: TWebGLPipelineState) {
const gl = options.context,
texture = this.createTexture(options.filterBackend, this.image);
this.bindAdditionalTexture(gl, texture, gl.TEXTURE1);
super.applyToWebGL(options);
this.unbindAdditionalTexture(gl, gl.TEXTURE1);
}
createTexture(backend: WebGLFilterBackend, image: Image) {
return backend.getCachedTexture(image.cacheKey, image.getElement());
}
/**
* Calculate a transformMatrix to adapt the image to blend over
* @param {Object} options
* @param {WebGLRenderingContext} options.context The GL context used for rendering.
* @param {Object} options.programCache A map of compiled shader programs, keyed by filter type.
*/
calculateMatrix() {
const image = this.image,
{ width, height } = image.getElement();
return [
1 / image.scaleX,
0,
0,
0,
1 / image.scaleY,
0,
-image.left / width,
-image.top / height,
1,
];
}
/**
* Apply the Blend operation to a Uint8ClampedArray representing the pixels of an image.
*
* @param {Object} options
* @param {ImageData} options.imageData The Uint8ClampedArray to be filtered.
*/
applyTo2d({
imageData: { data, width, height },
filterBackend: { resources },
}: T2DPipelineState) {
const image = this.image;
if (!resources.blendImage) {
resources.blendImage = createCanvasElement();
}
const canvas1 = resources.blendImage;
const context = canvas1.getContext('2d');
if (canvas1.width !== width || canvas1.height !== height) {
canvas1.width = width;
canvas1.height = height;
} else {
context.clearRect(0, 0, width, height);
}
context.setTransform(
image.scaleX,
0,
0,
image.scaleY,
image.left,
image.top
);
context.drawImage(image.getElement(), 0, 0, width, height);
const blendData = context.getImageData(0, 0, width, height).data;
for (let i = 0; i < data.length; i += 4) {
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];
const a = data[i + 3];
const tr = blendData[i];
const tg = blendData[i + 1];
const tb = blendData[i + 2];
const ta = blendData[i + 3];
switch (this.mode) {
case 'multiply':
data[i] = (r * tr) / 255;
data[i + 1] = (g * tg) / 255;
data[i + 2] = (b * tb) / 255;
data[i + 3] = (a * ta) / 255;
break;
case 'mask':
data[i + 3] = ta;
break;
}
}
}
/**
* Return WebGL uniform locations for this filter's shader.
*
* @param {WebGLRenderingContext} gl The GL canvas context used to compile this filter's shader.
* @param {WebGLShaderProgram} program This filter's compiled shader program.
*/
getUniformLocations(
gl: WebGLRenderingContext,
program: WebGLProgram
): TWebGLUniformLocationMap {
return {
uTransformMatrix: gl.getUniformLocation(program, 'uTransformMatrix'),
uImage: gl.getUniformLocation(program, 'uImage'),
};
}
/**
* Send data from this filter to its shader program's uniforms.
*
* @param {WebGLRenderingContext} gl The GL canvas context used to compile this filter's shader.
* @param {Object} uniformLocations A map of string uniform names to WebGLUniformLocation objects
*/
sendUniformData(
gl: WebGLRenderingContext,
uniformLocations: TWebGLUniformLocationMap
) {
const matrix = this.calculateMatrix();
gl.uniform1i(uniformLocations.uImage, 1); // texture unit 1.
gl.uniformMatrix3fv(uniformLocations.uTransformMatrix, false, matrix);
}
/**
* Returns object representation of an instance
* @return {Object} Object representation of an instance
*/
toObject() {
return {
type: this.type,
image: this.image && this.image.toObject(),
mode: this.mode,
alpha: this.alpha,
};
}
/**
* Create filter instance from an object representation
* @static
* @param {object} object Object to create an instance from
* @param {object} [options]
* @param {AbortSignal} [options.signal] handle aborting image loading, see https://developer.mozilla.org/en-US/docs/Web/API/AbortController/signal
* @returns {Promise<BlendImage>}
*/
static fromObject(object, options) {
return Image.fromObject(object.image, options).then(
(image) => new BlendImage({ ...object, image })
);
}
}
export const blendImageDefaultValues: Partial<TClassProperties<BlendImage>> = {
type: 'BlendImage',
mode: 'multiply',
alpha: 1,
vertexSource: `
attribute vec2 aPosition;
varying vec2 vTexCoord;
varying vec2 vTexCoord2;
uniform mat3 uTransformMatrix;
void main() {
vTexCoord = aPosition;
vTexCoord2 = (uTransformMatrix * vec3(aPosition, 1.0)).xy;
gl_Position = vec4(aPosition * 2.0 - 1.0, 0.0, 1.0);
}
`,
fragmentSource: {
multiply: `
precision highp float;
uniform sampler2D uTexture;
uniform sampler2D uImage;
uniform vec4 uColor;
varying vec2 vTexCoord;
varying vec2 vTexCoord2;
void main() {
vec4 color = texture2D(uTexture, vTexCoord);
vec4 color2 = texture2D(uImage, vTexCoord2);
color.rgba *= color2.rgba;
gl_FragColor = color;
}
`,
mask: `
precision highp float;
uniform sampler2D uTexture;
uniform sampler2D uImage;
uniform vec4 uColor;
varying vec2 vTexCoord;
varying vec2 vTexCoord2;
void main() {
vec4 color = texture2D(uTexture, vTexCoord);
vec4 color2 = texture2D(uImage, vTexCoord2);
color.a = color2.a;
gl_FragColor = color;
}
`,
},
};
Object.assign(BlendImage.prototype, blendImageDefaultValues);