-
Notifications
You must be signed in to change notification settings - Fork 122
/
detectSubsampling.ts
53 lines (45 loc) · 1.55 KB
/
detectSubsampling.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
let canvas: HTMLCanvasElement
let context: CanvasRenderingContext2D
function detectionContext() {
if (context) {
return context
}
if (!canvas) {
canvas = document.createElement('canvas')
}
if (!canvas || !canvas.getContext) {
return null
}
context = canvas.getContext('2d')
return context
}
/**
* Idea taken from https://github.com/stomita/ios-imagefile-megapixel
* Detects whether the image has been sub sampled by the browser and does not have its original dimensions.
* This method unfortunately does not work for images that have transparent background.
*/
export function detectSubsampling(img: HTMLImageElement, width: number, height: number) {
if (!detectionContext()) {
return false
}
// sub sampling happens on images above 1 megapixel
if (width * height <= 1024 * 1024) {
return false
}
// set canvas to 1x1 pixel size and fill it with magenta color
canvas.width = canvas.height = 1
context.fillStyle = '#FF00FF'
context.fillRect(0, 0, 1, 1)
// render the image with a negative offset to the left so that it would
// fill the canvas pixel with the top right pixel of the image.
context.drawImage(img, -width + 1, 0)
// check color value to confirm image is covering edge pixel or not.
// if color still magenta, the image is assumed to be sub sampled.
try {
const dat = context.getImageData(0, 0, 1, 1).data
return (dat[0] === 255) && (dat[1] === 0) && (dat[2] === 255)
} catch (err) {
// avoids cross origin exception for chrome when code runs without a server
return false
}
}