Skip to content
← Public packages

@kentcdodds/photo-portfolio

A fast, photo-first portfolio site you update by chatting with your agent: galleries, about, contact, and one-step rollback.

FollowFork

Files

pixels.ts

229 lines · 6.2 KB · TypeScript
import jpeg from 'jpeg-js'
import { decode as decodeWebp } from '@stacksjs/ts-webp'
import { maxSourceMegapixels } from '../domain/image-plan.ts'
import { decodePng, readPngSize } from './png.ts'

/** Interleaved 8-bit pixels. `channels` is 3 (RGB) or 4 (RGBA). */
export type Pixels = {
	data: Uint8Array
	width: number
	height: number
	channels: 3 | 4
}

export type SourceFormat = 'jpeg' | 'png' | 'webp'

export function sniffFormat(bytes: Uint8Array): SourceFormat {
	if (bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff) return 'jpeg'
	if (
		bytes[0] === 0x89 &&
		bytes[1] === 0x50 &&
		bytes[2] === 0x4e &&
		bytes[3] === 0x47
	)
		return 'png'
	const ascii = (start: number, end: number) =>
		String.fromCharCode(...bytes.subarray(start, end))
	if (ascii(0, 4) === 'RIFF' && ascii(8, 12) === 'WEBP') return 'webp'
	if (ascii(4, 8) === 'ftyp') {
		throw new Error(
			'HEIC/AVIF photos are not supported yet. Export or convert the photo to JPEG first.',
		)
	}
	throw new Error('Unsupported image type. Send JPEG, PNG, or WebP.')
}

/** Read width/height from a JPEG's SOF marker without decoding pixels. */
export function readJpegSize(bytes: Uint8Array): {
	width: number
	height: number
} {
	let offset = 2
	while (offset + 9 < bytes.length) {
		if (bytes[offset] !== 0xff) {
			offset += 1
			continue
		}
		const marker = bytes[offset + 1]!
		const length = (bytes[offset + 2]! << 8) | bytes[offset + 3]!
		const isSof =
			marker >= 0xc0 &&
			marker <= 0xcf &&
			marker !== 0xc4 &&
			marker !== 0xc8 &&
			marker !== 0xcc
		if (isSof) {
			return {
				height: (bytes[offset + 5]! << 8) | bytes[offset + 6]!,
				width: (bytes[offset + 7]! << 8) | bytes[offset + 8]!,
			}
		}
		offset += 2 + length
	}
	throw new Error('Could not read the JPEG dimensions. Is the file complete?')
}

function assertDecodable(width: number, height: number) {
	if ((width * height) / 1_000_000 > maxSourceMegapixels) {
		throw new Error(
			`Image is ${width}×${height}. Send a web export of at most ${maxSourceMegapixels} megapixels (for example 4200×2800 or smaller).`,
		)
	}
}

export async function decodeImage(bytes: Uint8Array): Promise<Pixels> {
	const format = sniffFormat(bytes)
	if (format === 'jpeg') {
		const size = readJpegSize(bytes)
		assertDecodable(size.width, size.height)
		const image = jpeg.decode(bytes, {
			useTArray: true,
			formatAsRGBA: false,
			maxResolutionInMP: maxSourceMegapixels + 1,
			maxMemoryUsageInMB: 256,
		})
		return {
			data: image.data,
			width: image.width,
			height: image.height,
			channels: 3,
		}
	}
	if (format === 'png') {
		const size = readPngSize(bytes)
		assertDecodable(size.width, size.height)
		return { ...(await decodePng(bytes)), channels: 4 }
	}
	const webp = decodeWebp(bytes)
	assertDecodable(webp.width, webp.height)
	return {
		data: webp.data,
		width: webp.width,
		height: webp.height,
		channels: 4,
	}
}

/** Orientations 5–8 swap width and height. */
export function orientedSize(
	width: number,
	height: number,
	orientation: number,
) {
	return orientation >= 5 ? { width: height, height: width } : { width, height }
}

/**
 * Area-average downscale to RGBA. Every output pixel averages the exact source
 * area it covers (with fractional edge weights), which avoids the aliasing of
 * nearest/bilinear sampling when shrinking photos. Works one output row at a
 * time so memory stays at one source row of floats.
 */
export function resize(source: Pixels, width: number, height: number): Pixels {
	const { data, channels } = source
	const sw = source.width
	const sh = source.height
	const xScale = sw / width
	const yScale = sh / height
	const columns = Array.from({ length: width }, (_, x) => {
		const start = x * xScale
		const end = start + xScale
		const first = Math.floor(start)
		const last = Math.min(sw - 1, Math.ceil(end) - 1)
		const weights = new Float32Array(last - first + 1)
		for (let sx = first; sx <= last; sx += 1) {
			weights[sx - first] =
				(Math.min(end, sx + 1) - Math.max(start, sx)) / xScale
		}
		return { first, weights }
	})
	const row = new Float32Array(sw * 3)
	const out = new Uint8Array(width * height * 4)
	for (let y = 0; y < height; y += 1) {
		row.fill(0)
		const start = y * yScale
		const end = start + yScale
		const first = Math.floor(start)
		const last = Math.min(sh - 1, Math.ceil(end) - 1)
		for (let sy = first; sy <= last; sy += 1) {
			const weight = (Math.min(end, sy + 1) - Math.max(start, sy)) / yScale
			let i = sy * sw * channels
			for (let sx = 0, r = 0; sx < sw; sx += 1, r += 3, i += channels) {
				row[r] += data[i]! * weight
				row[r + 1] += data[i + 1]! * weight
				row[r + 2] += data[i + 2]! * weight
			}
		}
		let o = y * width * 4
		for (let x = 0; x < width; x += 1, o += 4) {
			const { first: fx, weights } = columns[x]!
			let r = 0
			let g = 0
			let b = 0
			for (let k = 0, j = fx * 3; k < weights.length; k += 1, j += 3) {
				const w = weights[k]!
				r += row[j]! * w
				g += row[j + 1]! * w
				b += row[j + 2]! * w
			}
			out[o] = Math.round(r)
			out[o + 1] = Math.round(g)
			out[o + 2] = Math.round(b)
			out[o + 3] = 255
		}
	}
	return { data: out, width, height, channels: 4 }
}

/** Apply an EXIF orientation (1–8) to RGBA pixels so they display upright. */
export function applyOrientation(source: Pixels, orientation: number): Pixels {
	if (orientation <= 1 || orientation > 8) return source
	const { width: w, height: h, data } = source
	const swap = orientation >= 5
	const ow = swap ? h : w
	const oh = swap ? w : h
	const out = new Uint8Array(ow * oh * 4)
	for (let y = 0; y < h; y += 1) {
		for (let x = 0; x < w; x += 1) {
			let nx: number
			let ny: number
			switch (orientation) {
				case 2:
					nx = w - 1 - x
					ny = y
					break
				case 3:
					nx = w - 1 - x
					ny = h - 1 - y
					break
				case 4:
					nx = x
					ny = h - 1 - y
					break
				case 5:
					nx = y
					ny = x
					break
				case 6:
					nx = h - 1 - y
					ny = x
					break
				case 7:
					nx = h - 1 - y
					ny = w - 1 - x
					break
				default:
					nx = y
					ny = w - 1 - x
					break
			}
			const i = (y * w + x) * 4
			const o = (ny * ow + nx) * 4
			out[o] = data[i]!
			out[o + 1] = data[i + 1]!
			out[o + 2] = data[i + 2]!
			out[o + 3] = data[i + 3]!
		}
	}
	return { data: out, width: ow, height: oh, channels: 4 }
}