cd52a2f82f
Browser-based photo catalog with an optional team server (SQLite + LibRaw). Local database, uploaded photos, build output and dependencies are gitignored. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
129 lines
4.1 KiB
TypeScript
129 lines
4.1 KiB
TypeScript
export interface Tiff16Options {
|
|
width: number;
|
|
height: number;
|
|
/** RGB samples, top-to-bottom, already in output colour space. */
|
|
pixels: Float32Array;
|
|
/** Optional embedded ICC profile. */
|
|
icc?: Uint8Array | null;
|
|
}
|
|
|
|
const LE = (() => {
|
|
const probe = new Uint8Array(new Uint16Array([1]).buffer);
|
|
return probe[0] === 1;
|
|
})();
|
|
|
|
function set16(view: DataView, at: number, value: number) {
|
|
view.setUint16(at, value & 0xffff, true);
|
|
}
|
|
|
|
function set32(view: DataView, at: number, value: number) {
|
|
view.setUint32(at, value >>> 0, true);
|
|
}
|
|
|
|
/**
|
|
* Minimal uncompressed little-endian 16-bit RGB TIFF.
|
|
*
|
|
* The profile tag (34675) is included when supplied. Uncompressed keeps the
|
|
* encoder dependency-free and lossless; the files are larger than JPEG but are
|
|
* intended as delivery masters rather than web files.
|
|
*/
|
|
export function encodeTiff16({ width, height, pixels, icc }: Tiff16Options): Blob {
|
|
const w = Math.max(1, Math.round(width));
|
|
const h = Math.max(1, Math.round(height));
|
|
const pixelBytes = w * h * 3 * 2;
|
|
const profileLength = icc?.length ?? 0;
|
|
const entries = profileLength ? 14 : 13;
|
|
|
|
// IFD starts after the 8-byte header. Extras follow the IFD and are aligned.
|
|
const ifdAt = 8;
|
|
const ifdBytes = 2 + entries * 12 + 4;
|
|
let extraAt = ifdAt + ifdBytes;
|
|
const align4 = (n: number) => (n + 3) & ~3;
|
|
extraAt = align4(extraAt);
|
|
|
|
const bitsAt = extraAt;
|
|
const xResAt = bitsAt + 8;
|
|
const yResAt = xResAt + 8;
|
|
const profileAt = yResAt + 8;
|
|
const dataAt = align4(profileAt + profileLength);
|
|
const total = dataAt + pixelBytes;
|
|
const out = new Uint8Array(total);
|
|
const view = new DataView(out.buffer);
|
|
|
|
// TIFF header.
|
|
out[0] = 0x49;
|
|
out[1] = 0x49;
|
|
set16(view, 2, 42);
|
|
set32(view, 4, ifdAt);
|
|
|
|
// IFD entries must be sorted by tag.
|
|
set16(view, ifdAt, entries);
|
|
let at = ifdAt + 2;
|
|
const entry = (tag: number, type: number, count: number, value: number, inline = true) => {
|
|
set16(view, at, tag);
|
|
set16(view, at + 2, type);
|
|
set32(view, at + 4, count);
|
|
if (inline) set32(view, at + 8, value);
|
|
else {
|
|
set16(view, at + 8, value & 0xffff);
|
|
set16(view, at + 10, value >>> 16);
|
|
}
|
|
at += 12;
|
|
};
|
|
|
|
entry(256, 4, 1, w);
|
|
entry(257, 4, 1, h);
|
|
entry(258, 3, 3, bitsAt, false);
|
|
entry(259, 3, 1, 1);
|
|
entry(262, 3, 1, 2);
|
|
entry(273, 4, 1, dataAt);
|
|
entry(277, 3, 1, 3);
|
|
entry(278, 4, 1, h);
|
|
entry(279, 4, 1, pixelBytes);
|
|
entry(282, 5, 1, xResAt, false);
|
|
entry(283, 5, 1, yResAt, false);
|
|
entry(284, 3, 1, 1);
|
|
entry(296, 3, 1, 2);
|
|
if (profileLength) entry(34675, 7, profileLength, profileAt, false);
|
|
set32(view, at, 0); // next IFD
|
|
|
|
// BitsPerSample = 16,16,16; rational resolution = 300/1.
|
|
set16(view, bitsAt, 16);
|
|
set16(view, bitsAt + 2, 16);
|
|
set16(view, bitsAt + 4, 16);
|
|
set32(view, xResAt, 300);
|
|
set32(view, xResAt + 4, 1);
|
|
set32(view, yResAt, 300);
|
|
set32(view, yResAt + 4, 1);
|
|
if (icc && profileLength) out.set(icc, profileAt);
|
|
|
|
// WebGL's readPixels starts at the bottom-left; TIFF rows run top-to-bottom.
|
|
if (LE) {
|
|
const words = new Uint16Array(out.buffer, dataAt, w * h * 3);
|
|
for (let y = 0; y < h; y++) {
|
|
const srcY = h - 1 - y;
|
|
for (let x = 0; x < w; x++) {
|
|
const src = (srcY * w + x) * 4;
|
|
const dst = (y * w + x) * 3;
|
|
words[dst] = Math.round(Math.max(0, Math.min(1, pixels[src])) * 65535);
|
|
words[dst + 1] = Math.round(Math.max(0, Math.min(1, pixels[src + 1])) * 65535);
|
|
words[dst + 2] = Math.round(Math.max(0, Math.min(1, pixels[src + 2])) * 65535);
|
|
}
|
|
}
|
|
} else {
|
|
// Rare big-endian browser: keep the file little-endian explicitly.
|
|
for (let y = 0; y < h; y++) {
|
|
const srcY = h - 1 - y;
|
|
for (let x = 0; x < w; x++) {
|
|
const src = (srcY * w + x) * 4;
|
|
const dst = dataAt + (y * w + x) * 6;
|
|
set16(view, dst, Math.round(Math.max(0, Math.min(1, pixels[src])) * 65535));
|
|
set16(view, dst + 2, Math.round(Math.max(0, Math.min(1, pixels[src + 1])) * 65535));
|
|
set16(view, dst + 4, Math.round(Math.max(0, Math.min(1, pixels[src + 2])) * 65535));
|
|
}
|
|
}
|
|
}
|
|
|
|
return new Blob([out], { type: 'image/tiff' });
|
|
}
|