Land camera-native raw profiles, real ICC soft proofing and 16-bit proxies.
Camera profiles, proofing tables and the half-float sidecar all have to agree with the pixels the editor actually grades, so they land together with the preset filter menu that was unusable while thumbnails streamed in. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { dcpRgbCam, parseDcp, rgbCamFromColorMatrix } from './dcp';
|
||||
|
||||
/**
|
||||
* The camera matrix LibRaw reported for one of the repository's D700 raws, and
|
||||
* the `rgb_cam` it rendered that same file with. The recipe has to land on the
|
||||
* second from the first: that is the whole point of reading a DCP.
|
||||
*/
|
||||
const D700_CAM_XYZ = [
|
||||
0.8139, -0.2171, -0.0663, -0.8747, 1.6541, 0.2295, -0.1925, 0.2008, 0.8093,
|
||||
];
|
||||
const D700_RGB_CAM = [
|
||||
1.66943598, -0.4847292, -0.18470675, -0.00096075, 1.25268972, -0.25172901, 0.04713935,
|
||||
-0.30006605, 1.25292671,
|
||||
];
|
||||
|
||||
function maxDiff(a: number[], b: number[]) {
|
||||
let diff = 0;
|
||||
for (let i = 0; i < 9; i++) diff = Math.max(diff, Math.abs(a[i] - b[i]));
|
||||
return diff;
|
||||
}
|
||||
|
||||
interface DcpTag {
|
||||
tag: number;
|
||||
type: number;
|
||||
values: number[] | string;
|
||||
}
|
||||
|
||||
interface PlacedTag {
|
||||
tag: number;
|
||||
type: number;
|
||||
count: number;
|
||||
bytes: Uint8Array;
|
||||
offset: number;
|
||||
}
|
||||
|
||||
/** Big-endian TIFF-style tag table behind the 8-byte DCP header. */
|
||||
function buildDcp(tags: DcpTag[]): Uint8Array {
|
||||
const blobs = tags.map((entry): Omit<PlacedTag, 'offset'> => {
|
||||
if (typeof entry.values === 'string') {
|
||||
const text = new TextEncoder().encode(`${entry.values}\0`);
|
||||
return { tag: entry.tag, type: entry.type, count: text.length, bytes: text };
|
||||
}
|
||||
const buffer = new Uint8Array(entry.values.length * 8);
|
||||
const view = new DataView(buffer.buffer);
|
||||
entry.values.forEach((value, index) => {
|
||||
if (entry.type === 3) {
|
||||
view.setUint16(index * 2, value, false);
|
||||
} else {
|
||||
// SRATIONAL: store with a fixed denominator of 10,000.
|
||||
view.setInt32(index * 8, Math.round(value * 10000), false);
|
||||
view.setInt32(index * 8 + 4, 10000, false);
|
||||
}
|
||||
});
|
||||
const size = entry.type === 3 ? entry.values.length * 2 : entry.values.length * 8;
|
||||
return { tag: entry.tag, type: entry.type, count: entry.values.length, bytes: buffer.subarray(0, size) };
|
||||
});
|
||||
|
||||
let cursor = 8 + 2 + blobs.length * 12 + 4;
|
||||
const layout: PlacedTag[] = blobs.map((blob) => {
|
||||
const placed = { ...blob, offset: cursor };
|
||||
cursor += (blob.bytes.length + 3) & ~3;
|
||||
return placed;
|
||||
});
|
||||
|
||||
const out = new Uint8Array(cursor);
|
||||
const view = new DataView(out.buffer);
|
||||
out.set(new TextEncoder().encode('DCP\0'), 0);
|
||||
view.setUint32(4, 0x01000000, false);
|
||||
view.setUint16(8, layout.length, false);
|
||||
layout.forEach((entry, index) => {
|
||||
const at = 10 + index * 12;
|
||||
view.setUint16(at, entry.tag, false);
|
||||
view.setUint16(at + 2, entry.type, false);
|
||||
view.setUint32(at + 4, entry.count, false);
|
||||
if (entry.bytes.length <= 4) {
|
||||
out.set(entry.bytes, at + 8);
|
||||
} else {
|
||||
view.setUint32(at + 8, entry.offset, false);
|
||||
out.set(entry.bytes, entry.offset);
|
||||
}
|
||||
});
|
||||
return out;
|
||||
}
|
||||
|
||||
describe('rgbCamFromColorMatrix', () => {
|
||||
it('reproduces LibRaw rgb_cam from the body matrix', () => {
|
||||
const matrix = rgbCamFromColorMatrix(D700_CAM_XYZ);
|
||||
expect(matrix).not.toBeNull();
|
||||
expect(maxDiff(matrix!, D700_RGB_CAM)).toBeLessThan(1e-4);
|
||||
});
|
||||
|
||||
it('keeps white neutral by row-normalising before the inversion', () => {
|
||||
const matrix = rgbCamFromColorMatrix(D700_CAM_XYZ)!;
|
||||
for (let row = 0; row < 3; row++) {
|
||||
const sum = matrix[row * 3] + matrix[row * 3 + 1] + matrix[row * 3 + 2];
|
||||
expect(sum).toBeCloseTo(1, 5);
|
||||
}
|
||||
});
|
||||
|
||||
it('accepts rows and rejects a singular matrix', () => {
|
||||
const rows = [
|
||||
[0.8139, -0.2171, -0.0663],
|
||||
[-0.8747, 1.6541, 0.2295],
|
||||
[-0.1925, 0.2008, 0.8093],
|
||||
];
|
||||
expect(maxDiff(rgbCamFromColorMatrix(rows)!, D700_RGB_CAM)).toBeLessThan(1e-4);
|
||||
expect(rgbCamFromColorMatrix([0, 0, 0, 0, 0, 0, 0, 0, 0])).toBeNull();
|
||||
expect(rgbCamFromColorMatrix([1, 2, 3, 2, 4, 6, 3, 6, 9])).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('parseDcp', () => {
|
||||
it('reads the colour matrices and the profile name', () => {
|
||||
const bytes = buildDcp([
|
||||
{ tag: 1, type: 2, values: 'Nikon D700' },
|
||||
{ tag: 3, type: 2, values: 'Camera Standard' },
|
||||
{ tag: 4, type: 10, values: D700_CAM_XYZ },
|
||||
{ tag: 5, type: 10, values: [0.7, -0.1, -0.05, -0.8, 1.5, 0.2, -0.15, 0.15, 0.75] },
|
||||
{ tag: 6, type: 3, values: [21] },
|
||||
{ tag: 7, type: 3, values: [17] },
|
||||
]);
|
||||
const profile = parseDcp(bytes);
|
||||
expect(profile).not.toBeNull();
|
||||
expect(profile!.name).toBe('Camera Standard');
|
||||
expect(profile!.camera).toBe('Nikon D700');
|
||||
expect(profile!.illuminant1).toBe(21);
|
||||
expect(profile!.illuminant2).toBe(17);
|
||||
expect(profile!.colorMatrix1).toHaveLength(9);
|
||||
expect(maxDiff(dcpRgbCam(profile!)!, D700_RGB_CAM)).toBeLessThan(1e-4);
|
||||
});
|
||||
|
||||
it('falls back to the second illuminant when only that matrix exists', () => {
|
||||
const bytes = buildDcp([{ tag: 5, type: 10, values: D700_CAM_XYZ }]);
|
||||
const profile = parseDcp(bytes)!;
|
||||
expect(profile.colorMatrix1).toBeNull();
|
||||
expect(maxDiff(dcpRgbCam(profile)!, D700_RGB_CAM)).toBeLessThan(1e-4);
|
||||
});
|
||||
|
||||
it('refuses a file that is not a DCP', () => {
|
||||
expect(parseDcp(new Uint8Array(64))).toBeNull();
|
||||
const png = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
||||
expect(parseDcp(png)).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user