-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathjpeg.js
57 lines (49 loc) · 1.08 KB
/
jpeg.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
/**
* https://en.wikipedia.org/wiki/YCbCr#JPEG_conversion
*
* JPEG conversion without head/footroom
*
* @module color-space/jpeg
*/
import rgb from './rgb.js';
/** @type {Partial<import('./index.js').ColorSpace>} */
var jpeg = {
name: 'jpeg',
min: [0, 0, 0],
max: [255, 255, 255],
channel: ['Y','Cb','Cr'],
alias: ['JPEG']
};
export default /** @type {import('./index.js').ColorSpace} */ (jpeg);
/**
* JPEG to RGB
* transform through analog form
*
* @param {Array<number>} arr RGB values
*
* @return {Array<number>} JPEG values
*/
jpeg.rgb = function (arr) {
var y = arr[0], cb = arr[1], cr = arr[2];
return [
y + 1.402 * (cr - 128),
y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128),
y + 1.772 * (cb - 128)
]
};
/**
* RGB to JPEG
* transform through analog form
*
* @param {Array<number>} arr JPEG values
*
* @return {Array<number>} RGB values
*/
rgb.jpeg = function(arr) {
var r = arr[0], g = arr[1], b = arr[2];
return [
0.299 * r + 0.587 * g + 0.114 * b,
128 - 0.168736 * r - 0.331264 * g + 0.5 * b,
128 + 0.5 * r - 0.418688 * g - 0.081312 * b
]
};