forked from simon987/imagehash-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransforms.js
More file actions
310 lines (265 loc) · 12.2 KB
/
transforms.js
File metadata and controls
310 lines (265 loc) · 12.2 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
function setSmoothScaling(ctx) {
ctx.webkitImageSmoothingEnabled = true;
ctx.msImageSmoothingEnabled = true;
ctx.mozImageSmoothingEnabled = true;
ctx.imageSmoothingEnabled = true;
ctx.imageSmoothingQuality = "high";
}
function setRoughScaling(ctx) {
ctx.webkitImageSmoothingEnabled = false;
ctx.msImageSmoothingEnabled = false;
ctx.mozImageSmoothingEnabled = false;
ctx.imageSmoothingEnabled = false;
}
function browserCreateCanvas(width, height) {
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
return canvas;
}
async function transformImageCrop(imgSrc) {
return new Promise((resolve, reject) => {
let img = new Image();
img.setAttribute("crossOrigin", "Anonymous");
img.onload = async () => {
const width = Math.floor(img.naturalWidth * 0.9);
const height = Math.floor(img.naturalHeight * 0.85);
pica = new $ed498a97b604fad5$var$Pica();
canvas = browserCreateCanvas(width, height);
const fromCtx = canvas.getContext("2d", {willReadFrequently: true});
fromCtx.drawImage(img, Math.floor(-width * 0.06), Math.floor(-height * 0.1));
resolve(canvas.toDataURL());
}
img.onerror = reject;
img.src = imgSrc;
})
}
async function transformImageForPreHashing(imgSrc, size) {
return new Promise((resolve, reject) => {
let img = new Image();
img.setAttribute("crossOrigin", "Anonymous");
img.onload = async () => {
// Add solid background to ensure PNG with alpha compares correctly against JPG
let canvas = browserCreateCanvas(img.naturalWidth, img.naturalHeight);
const ctx = canvas.getContext("2d", {willReadFrequently: true});
ctx.fillStyle = "black";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0);
// Check for portrait mobile screenshot
let portraitRatio = img.naturalHeight / img.naturalWidth;
if (portraitRatio > 1.7 && portraitRatio < 2.2) {
// potentially mobile phone screenshot, check for embedded image
const SMALL_CANVAS_WIDTH = 128;
let smallCanvas = browserCreateCanvas(SMALL_CANVAS_WIDTH, Math.floor(SMALL_CANVAS_WIDTH * portraitRatio));
const sCtx = smallCanvas.getContext("2d", {willReadFrequently: true});
sCtx.drawImage(canvas, 0, 0, smallCanvas.width, smallCanvas.height);
let imageData = sCtx.getImageData(0, 0, smallCanvas.width, smallCanvas.height).data;
let row = 0;
let col = 0
let solidPixelsInRow = 0;
let solidRowPixelDiffs = 0;
let solidRows = [];
let rowColor = -1;
// Scan for solid rows down the image
for(let i = 0; i < imageData.length; i += 4) {
if (solidPixelsInRow >= 0) {
const red = imageData[i];
const green = imageData[i + 1];
const blue = imageData[i + 2];
const alpha = imageData[i + 3];
let newRowColor = red + green + blue + alpha;
if (rowColor < 0) {
rowColor = newRowColor;
} else {
let sufficientlyDifferent = Math.abs(newRowColor - rowColor) < 2;
rowColor = newRowColor;
if (sufficientlyDifferent) {
solidPixelsInRow++;
} else {
console.log("diff@",col,rowColor,"vs new",newRowColor,"(",red,green,blue,alpha,")");
solidRowPixelDiffs++;
if (solidRowPixelDiffs > SMALL_CANVAS_WIDTH * 0.1) {
solidPixelsInRow = -1;
console.log("Marking row non-solid");
}
}
}
}
col++;
if (col >= smallCanvas.width) {
if (solidPixelsInRow > Math.floor(SMALL_CANVAS_WIDTH * 0.9)) {
solidRows.push(row);
}
col = 0;
row++;
solidPixelsInRow = 0;
console.log("row", row, "col", col, "SolidRowPixelDiffs:",solidRowPixelDiffs);
solidRowPixelDiffs = 0;
}
}
let solidBorders = 0;
let solidCenters = 0;
for (let y = 0; y < Math.floor(smallCanvas.height * 0.25); y++) {
if (solidRows.includes(y)) {
solidBorders++;
}
}
for (let y = Math.floor(smallCanvas.height * 0.75); y < smallCanvas.height; y++) {
if (solidRows.includes(y)) {
solidBorders++;
}
}
for (let y = Math.floor(smallCanvas.height * 0.25); y < Math.floor(smallCanvas.height * 0.75); y++) {
if (solidRows.includes(y)) {
solidCenters++;
}
}
console.log("SOlid rows:",solidRows,"solid borders",solidBorders,"solid centers",solidCenters);
if (solidRows.length > Math.floor(smallCanvas.height * 0.1) && solidBorders > solidCenters * 0.5) {
let screenshotDetected = true;
// detected potential screenshot, find inside image boundaries
let topVal = 0;
let jumped = false;
for (let y = 0; y < solidRows.length - 1; y++) {
if (solidRows[y + 1] < solidRows[y] + Math.floor(SMALL_CANVAS_WIDTH / 10)) {
topVal = y + 1;
} else if (solidRows[y + 1] < solidRows[y] + Math.floor(SMALL_CANVAS_WIDTH / 5) && !jumped) {
topVal = y + 1;
console.log("Jumped down");
jumped = true;
} else {
break;
}
console.log(y, solidRows.length - 1);
if (y == solidRows.length - 1) {
screenshotDetected = false;
}
}
if (screenshotDetected) {
let topRow = solidRows[topVal];
let botVal = 0;
let botRow = Math.floor(smallCanvas.height) - topRow;
if (solidRows.includes(Math.floor(smallCanvas.height) - 1)) {
for (let y = solidRows.length - 1; y > 1; y--) {
if (solidRows[y - 1] > solidRows[y] - Math.floor(SMALL_CANVAS_WIDTH / 5)) {
botVal = y - 1;
} else {
break;
}
}
botRow = solidRows[botVal];
}
console.log("Cutting",topRow,botRow);
let hScaleFactor = (canvas.height / smallCanvas.height);
let newHeight = Math.floor((botRow - topRow) * hScaleFactor);
let originalCanvas = canvas;
canvas = browserCreateCanvas(canvas.width, newHeight);
const newCtx = canvas.getContext("2d", {willReadFrequently: true});
newCtx.drawImage(originalCanvas, 0, -Math.floor(topRow * hScaleFactor));
}
}
}
let dataUrl = canvas.toDataURL();
resolve(dataUrl);
}
img.onerror = reject;
img.src = imgSrc;
})
}
async function transformImageResize(imgSrc, ratio) {
return new Promise((resolve, reject) => {
let img = new Image();
img.setAttribute("crossOrigin", "Anonymous");
img.onload = async () => {
if (ratio > 2) {
ratio = ratio / img.naturalWidth;
}
const width = Math.floor(img.naturalWidth * ratio);
const height = Math.floor(img.naturalHeight * ratio);
pica = new $ed498a97b604fad5$var$Pica();
toCanvas = browserCreateCanvas(width, height);
fromCanvas = browserCreateCanvas(img.naturalWidth, img.naturalHeight);
const fromCtx = fromCanvas.getContext("2d", {willReadFrequently: true});
fromCtx.drawImage(img, 0, 0);
await pica.resize(fromCanvas, toCanvas);
resolve(toCanvas.toDataURL());
}
img.onerror = reject;
img.src = imgSrc;
})
}
async function transformImageOffset(imgSrc) {
return new Promise((resolve, reject) => {
let img = new Image();
img.setAttribute("crossOrigin", "Anonymous");
img.onload = () => {
const width = img.naturalWidth;
const height = img.naturalHeight;
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d", {willReadFrequently: true});
setSmoothScaling(ctx);
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, width * 0.15, height * 0.35, width * 0.6, height * 0.6);
resolve(canvas.toDataURL());
}
img.onerror = reject;
img.src = imgSrc;
})
}
async function transformImageJpeg(imgSrc, quality) {
return new Promise((resolve, reject) => {
let img = new Image();
img.setAttribute("crossOrigin", "Anonymous");
img.onload = () => {
const width = img.naturalWidth;
const height = img.naturalHeight;
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d", {willReadFrequently: true});
ctx.drawImage(img, 0, 0, width, height);
resolve(canvas.toDataURL("image/jpeg", quality));
}
img.onerror = reject;
img.src = imgSrc;
})
}
async function transformImagePortraitScreenshot(imgSrc, backgroundSrc = null) {
return new Promise((resolve, reject) => {
let img = new Image();
img.setAttribute("crossOrigin", "Anonymous");
img.onload = async () => {
const aspectRatio = img.naturalWidth / img.naturalHeight;
const iPhoneWidth = 1170;
const iPhoneHeight = 2532;
const canvas = document.createElement("canvas");
canvas.width = iPhoneWidth / 3;
canvas.height = iPhoneHeight / 3;
const ctx = canvas.getContext("2d", {willReadFrequently: true});
if (backgroundSrc) {
let backgroundImg = new Image();
backgroundImg.setAttribute("crossOrigin", "Anonymous");
var onloadPromise = new Promise((onloadResolve, onloadReject) => {
backgroundImg.onload = () => {
onloadResolve();
}
backgroundImg.src = backgroundSrc;
});
await onloadPromise;
ctx.drawImage(backgroundImg, 0, 0, canvas.width, canvas.height);
} else {
ctx.fillStyle = "black";
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
const height = canvas.width / aspectRatio;
setSmoothScaling(ctx);
ctx.drawImage(img, 0, canvas.height / 2 - height / 2, canvas.width, height);
resolve(canvas.toDataURL());
}
img.onerror = reject;
img.src = imgSrc;
})
}