-
Notifications
You must be signed in to change notification settings - Fork 47
/
lib.js
232 lines (210 loc) · 6.86 KB
/
lib.js
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
const Jimp = require('jimp');
const requireVm = require('require-vm');
let libConfig={
opencvJsPath:''
};
function getConfig() {
return libConfig
}
function setConfig(configData) {
libConfig = configData
}
function isReadyFunc () {
return new Promise((reslove,reject)=>{
const context = {
module:{exports:{}},
Module:{
onRuntimeInitialized() {
context.cv = context.module.exports();
const cv = context.cv;
cv.idft = function(src, dst, flags, nonzero_rows ) {
cv.dft( src, dst, flags | cv.DFT_INVERSE, nonzero_rows );
}
return reslove(context);
},
onAbort() {
return reject(new Error('loading opencv error'))
}
},
print:console.log
}
requireVm(libConfig.opencvJsPath || './opencv.js',context,{},{},true)
})
}
function shiftDFT(cv, mag) {
let rect = new cv.Rect(0, 0, mag.cols & (-2), mag.rows & (-2));
mag.roi(rect);
let cx = mag.cols / 2;
let cy = mag.rows / 2;
let q0 = mag.roi(new cv.Rect(0, 0, cx, cy));
let q1 = mag.roi(new cv.Rect(cx, 0, cx, cy));
let q2 = mag.roi(new cv.Rect(0, cy, cx, cy));
let q3 = mag.roi(new cv.Rect(cx, cy, cx, cy));
let tmp = new cv.Mat();
q0.copyTo(tmp);
q3.copyTo(q0);
tmp.copyTo(q3);
q1.copyTo(tmp);
q2.copyTo(q1);
tmp.copyTo(q2);
tmp.delete()
q0.delete()
q1.delete()
q2.delete()
q3.delete()
}
function getBlueChannel(cv, image)
{
let nextImg = image;
let channel = new cv.MatVector();
cv.split(nextImg, channel);
return channel.get(0);
}
function getDftMat(cv, padded)
{
let planes = new cv.MatVector();
planes.push_back(padded);
let matZ = new cv.Mat.zeros(padded.size(), cv.CV_32F)
planes.push_back(matZ);
let comImg = new cv.Mat();
cv.merge(planes,comImg);
cv.dft(comImg, comImg);
matZ.delete();
return comImg;
}
function addTextByMat(cv, comImg,watermarkText,point,fontSize)
{
cv.putText(comImg, watermarkText, point, cv.FONT_HERSHEY_DUPLEX, fontSize, cv.Scalar.all(0),2);
cv.flip(comImg, comImg, -1);
cv.putText(comImg, watermarkText, point, cv.FONT_HERSHEY_DUPLEX, fontSize, cv.Scalar.all(0),2);
cv.flip(comImg, comImg, -1);
}
function transFormMatWithText(cv, srcImg, watermarkText,fontSize) {
let padded = getBlueChannel(cv, srcImg);
padded.convertTo(padded, cv.CV_32F);
let comImg = getDftMat(cv, padded);
// add text
let center = new cv.Point(padded.cols/2, padded.rows/2);
addTextByMat(cv, comImg,watermarkText,center,fontSize);
let outer = new cv.Point (45, 45);
addTextByMat(cv, comImg,watermarkText,outer,fontSize);
//back image
let invDFT = new cv.Mat();
cv.idft(comImg, invDFT, cv.DFT_SCALE | cv.DFT_REAL_OUTPUT, 0);
let restoredImage = new cv.Mat();
invDFT.convertTo(restoredImage, cv.CV_8U);
let backPlanes = new cv.MatVector();
cv.split(srcImg, backPlanes);
// backPlanes.erase(backPlanes.get(0));
// backPlanes.insert(backPlanes.get(0), restoredImage);
backPlanes.set(0,restoredImage)
let backImage = new cv.Mat();
cv.merge(backPlanes,backImage);
padded.delete();
comImg.delete();
invDFT.delete();
restoredImage.delete()
return backImage;
}
function getTextFormMat(cv, backImage) {
let padded= getBlueChannel(cv, backImage);
padded.convertTo(padded, cv.CV_32F);
let comImg = getDftMat(cv, padded);
let backPlanes = new cv.MatVector();
// split the comples image in two backPlanes
cv.split(comImg, backPlanes);
let mag = new cv.Mat();
// compute the magnitude
cv.magnitude(backPlanes.get(0), backPlanes.get(1), mag);
// move to a logarithmic scale
let matOne = cv.Mat.ones(mag.size(), cv.CV_32F)
cv.add(matOne, mag, mag);
cv.log(mag, mag);
shiftDFT(cv, mag);
mag.convertTo(mag, cv.CV_8UC1);
cv.normalize(mag, mag, 0, 255, cv.NORM_MINMAX, cv.CV_8UC1);
padded.delete();
comImg.delete();
matOne.delete();
return mag;
}
function matToBuffer(cv, mat){
if(!(mat instanceof cv.Mat)){
throw new Error("Please input the valid new cv.Mat instance.");
}
var img=new cv.Mat();
var depth=mat.type()%8;
var scale=depth<=cv.CV_8S?1:depth<=cv.CV_32S?1/256:255;
var shift=depth===cv.CV_8S||depth===cv.CV_16S?128:0;
mat.convertTo(img,cv.CV_8U,scale,shift);
switch(img.type()){
case cv.CV_8UC1:cv.cvtColor(img,img,cv.COLOR_GRAY2RGBA);break;
case cv.CV_8UC3:cv.cvtColor(img,img,cv.COLOR_RGB2RGBA);break;
case cv.CV_8UC4:break;
default:throw new Error("Bad number of channels (Source image must have 1, 3 or 4 channels)");
}
var imgData=Buffer.from(img.data);
img.delete()
return imgData
}
async function transformImageWithText(srcFileName,watermarkText,fontSize,enCodeFileName='') {
const context = await isReadyFunc ()
const cv = context.cv;
if((typeof srcFileName)!='string' && (!(srcFileName instanceof Buffer))) {
throw new Error('fileName must be string or Buffer')
}
if((typeof watermarkText)!='string') {
throw new Error('waterMarkText must be string')
}
if((typeof fontSize)!='number') {
throw new Error('fontSize must be number')
}
if((typeof enCodeFileName)!='string') {
throw new Error('outFileName must be string')
}
let jimpSrc = await Jimp.read(srcFileName);
let srcImg = new cv.matFromImageData(jimpSrc.bitmap);
if (srcImg.empty()){throw new Error("read image failed");}
let comImg = transFormMatWithText(cv, srcImg, watermarkText, fontSize);
const imgRes = new Jimp({
width: comImg.cols,
height: comImg.rows,
data: matToBuffer(cv, comImg)
});
srcImg.delete();
comImg.delete();
if(enCodeFileName) {
return await imgRes.writeAsync(enCodeFileName);
} else {
return imgRes
}
}
async function getTextFormImage(enCodeFileName,deCodeFileName='') {
const context = await isReadyFunc ()
const cv = context.cv;
if((typeof enCodeFileName)!='string' && (!(enCodeFileName instanceof Buffer))) {
throw new Error('fileName must be string or Buffer')
}
if((typeof deCodeFileName)!='string') {
throw new Error('backFileName must be string')
}
let jimpSrc = await Jimp.read(enCodeFileName);
let comImg = new cv.matFromImageData(jimpSrc.bitmap);
let backImage = getTextFormMat(cv, comImg);
const imgRes = await new Jimp({
width: backImage.cols,
height: backImage.rows,
data: matToBuffer(cv, backImage)
})
comImg.delete();
backImage.delete();
if(deCodeFileName) {
return await imgRes.writeAsync(deCodeFileName);
} else {
return imgRes
}
}
module.exports.transformImageWithText = transformImageWithText;
module.exports.getTextFormImage = getTextFormImage;
module.exports.getConfig = getConfig;
module.exports.setConfig = setConfig;