Skip to content

Adds Perceptual Quantizer (PQ) transfer functions #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/hdr/transfer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
https://en.wikipedia.org/wiki/Transfer_functions_in_imaging
*/

export const M1 = 0.1593017578125;
export const M2 = 78.84375;
export const C1 = 0.8359375;
export const C2 = 18.8515625;
export const C3 = 18.6875;

/*
Perceptual Quantizer, as defined in Rec. BT 2100-2 (2018)

* https://www.itu.int/rec/R-REC-BT.2100-2-201807-I/en
* https://en.wikipedia.org/wiki/Perceptual_quantizer
*/

/* PQ EOTF, defined for `v` in [0,1]. */
export function transferPqDecode(v) {
if (v < 0) return 0;
const c = Math.pow(v, 1 / M2);
return 1e4 * Math.pow(Math.max(0, c - C1) / (C2 - C3 * c), 1 / M1);
}

/* PQ EOTF^-1, defined for `v` in [0, 1e4]. */
export function transferPqEncode(v) {
if (v < 0) return 0;
const c = Math.pow(v / 1e4, M1);
return Math.pow((C1 + C2 * c) / (1 + C3 * c), M2);
}
22 changes: 12 additions & 10 deletions src/jab/convertJabToXyz65.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
const n = 0.1593017578125; // = 2610 / Math.pow(2, 14);
import { M1 as n, C1, C2, C3 } from '../hdr/transfer.js';
const p = 134.03437499999998; // = 1.7 * 2523 / Math.pow(2, 5);
const c1 = 0.8359375; // = 3424 / Math.pow(2, 12);
const c2 = 18.8515625; // = 2413 / Math.pow(2, 7);
const c3 = 18.6875; // = 2392 / Math.pow(2, 7);
const d0 = 1.6295499532821566e-11;

/* `v` may be negative, in which case return 0 instead of NaN */
const pq_inv = v => {
const npow = (v, exp) => Math.sign(v) * Math.pow(Math.abs(v), exp);

/*
The encoding function is derived from Perceptual Quantizer.
*/
const jabPqDecode = v => {
if (v < 0) return 0;
let vp = Math.pow(v, 1 / p);
return 10000 * Math.pow((c1 - vp) / (c3 * vp - c2), 1 / n) || 0;
return 10000 * Math.pow((C1 - vp) / (C3 * vp - C2), 1 / n);
};

const rel = v => v / 203;

const convertJabToXyz65 = ({ j, a, b, alpha }) => {
let i = (j + d0) / (0.44 + 0.56 * (j + d0));

let l = pq_inv(i + 0.13860504 * a + 0.058047316 * b);
let m = pq_inv(i - 0.13860504 * a - 0.058047316 * b);
let s = pq_inv(i - 0.096019242 * a - 0.8118919 * b);
let l = jabPqDecode(i + 0.13860504 * a + 0.058047316 * b);
let m = jabPqDecode(i - 0.13860504 * a - 0.058047316 * b);
let s = jabPqDecode(i - 0.096019242 * a - 0.8118919 * b);

let res = {
mode: 'xyz65',
Expand Down
20 changes: 10 additions & 10 deletions src/jab/convertXyz65ToJab.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const n = 0.1593017578125; // = 2610 / Math.pow(2, 14);
import { M1 as n, C1, C2, C3 } from '../hdr/transfer.js';
const p = 134.03437499999998; // = 1.7 * 2523 / Math.pow(2, 5);
const c1 = 0.8359375; // = 3424 / Math.pow(2, 12);
const c2 = 18.8515625; // = 2413 / Math.pow(2, 7);
const c3 = 18.6875; // = 2392 / Math.pow(2, 7);
const d0 = 1.6295499532821566e-11;

/* `v` may be negative, in which case return 0 instead of NaN */
const pq = v => {
/*
The encoding function is derived from Perceptual Quantizer.
*/
const jabPqEncode = v => {
if (v < 0) return 0;
let vn = Math.pow(v / 10000, n);
return Math.pow((c1 + c2 * vn) / (1 + c3 * vn), p) || 0;
return Math.pow((C1 + C2 * vn) / (1 + C3 * vn), p);
};

// Convert to Absolute XYZ
Expand All @@ -22,9 +22,9 @@ const convertXyz65ToJab = ({ x, y, z, alpha }) => {
let xp = 1.15 * x - 0.15 * z;
let yp = 0.66 * y + 0.34 * x;

let l = pq(0.41478972 * xp + 0.579999 * yp + 0.014648 * z);
let m = pq(-0.20151 * xp + 1.120649 * yp + 0.0531008 * z);
let s = pq(-0.0166008 * xp + 0.2648 * yp + 0.6684799 * z);
let l = jabPqEncode(0.41478972 * xp + 0.579999 * yp + 0.014648 * z);
let m = jabPqEncode(-0.20151 * xp + 1.120649 * yp + 0.0531008 * z);
let s = jabPqEncode(-0.0166008 * xp + 0.2648 * yp + 0.6684799 * z);

let i = (l + m) / 2;

Expand Down