TheRare Visuals

Lightroom in a browser tab: turning camera JPEGs into deliverables

How Apply TheRare look reads Lightroom presets out of XMP and DNG files and renders them with a WebGL shader, tile by tile so a 24 MP photo fits on a phone's graphics chip, keeping the camera's EXIF on the way out.

TheRare Engineering24 October 20267 min readAdvanced

The problem

A wedding produces a few thousand camera JPEGs. The studio's look lives in Lightroom presets: a warm "Ivory" for the main day, a deep "Noir" for receptions, a soft film look for pre-wedding stories. Turning camera files into deliverables meant opening Lightroom, importing, applying, exporting, and uploading the results to the booking.

Two groups wanted something faster:

  • Editors wanted to apply a TheRare look to a batch of camera JPEGs on the booking page and download the finished files, without a round trip through Lightroom for every quick turnaround.
  • Customers wanted to try the looks on their own raw photos before choosing one, and the public "Try TheRare looks" page needed the same thing for visitors.

Sending full-size photos to a server to be processed would have meant uploading gigabytes, paying for image servers, and making people wait. Every phone and laptop already has a graphics chip that is very good at doing the same small sum to millions of pixels. So "Apply TheRare look" runs entirely in the browser.

Step one: reading a preset

A Lightroom preset isn't an image filter. It's a list of slider values: Exposure +0.3, Contrast +12, Highlights -40, a tone curve, hue and saturation shifts for eight colour bands, colour grading for shadows, midtones and highlights, a vignette, grain.

Those values live in XMP, a block of XML that Lightroom writes into the preset file. A desktop preset is a .xmp file. A preset made in Lightroom mobile is a .dng image with the same XML tucked inside it. PresetLookParser handles both by searching the raw bytes for the XMP packet:

Java
// XMP is plain UTF-8 inside the file (DNGs keep it in a TIFF tag), so a Latin-1 view keeps
// byte offsets while letting the regex find it; the packet itself is re-decoded as UTF-8.
String view = new String(bytes, StandardCharsets.ISO_8859_1);
Matcher m = XMP_PACKET.matcher(view);

Reading bytes as Latin-1 is a small trick worth knowing: every byte maps to exactly one character, so a regular expression can scan a binary file without the decoder choking on invalid UTF-8. Once the packet is found, it's decoded properly.

From the packet we pull the crs: (Camera Raw Settings) values the engine understands, such as Exposure2012, HueAdjustmentRed or ColorGradeShadowHue, into a small JSON shape:

TypeScript
export interface LookPreset {
  id: string;
  name: string;
  basic: Partial<Record<"Exposure" | "Contrast" | "Highlights" | "Shadows" | ..., number>>;
  curve: [number, number][];
  hsl?: Partial<Record<HslColor, { hue?: number; sat?: number; lum?: number }>>;
  grade?: { shadows?: number[]; midtones?: number[]; highlights?: number[]; ... };
  effects?: { vignette?: number; grain?: number[] };
}

The studio's own 52 looks are built into therare-presets.json when the site is built. Presets uploaded later (whole packs at once, from Admin -> Presets) go through the same parser on the server and are stored as the same JSON.

Step two: a shader

The look itself is one WebGL fragment shader: a small program the graphics chip runs once for every pixel, in parallel. It applies the sliders in roughly the order Lightroom does:

  1. White balance and exposure, in linear light. Camera JPEGs are gamma-encoded, so "twice as bright" isn't "twice the number". The shader converts to linear light, multiplies, and converts back.
  2. The tone sliders on brightness only. Highlights, Shadows, Whites, Blacks and Contrast reshape the pixel's luminance, then the colour channels are scaled by the same ratio so the hue doesn't shift.
  3. HSL, per colour band. Each of Lightroom's eight bands (Red, Orange, Yellow, Green, Aqua, Blue, Purple, Magenta) has a centre hue and a width. A pixel's hue gives it a weight in each band, so a skin tone that sits between Red and Orange gets a blend of both adjustments rather than a hard jump.
  4. Colour grading. A tint per tonal zone, mixed by how bright the pixel is, with the luminance kept the same so a warm highlight tint doesn't brighten the photo.
  5. The tone curve.
  6. Vignette and grain, measured against the whole photo.

Two details took the longest.

Narrower bands where Lightroom's sliders sit close together. Red, Orange and Yellow are only 30 degrees apart, and those are the colours of skin. With bands as wide as the blue ones, an Orange adjustment meant for skin would reach well into the yellows, so those three use narrower bands:

GLSL
float bandWidth(int i) {
  // Narrower bands where Lightroom's sliders sit close together (red-orange-yellow).
  if (i <= 2) return 32.0; if (i == 6) return 34.0; return 48.0;
}

Curves as a lookup texture. A Lightroom curve is a handful of points. The engine fits a smooth curve through them on the CPU (a monotone cubic, so the curve never wiggles above or below its points and creates odd colour reversals), samples it to 256 values, and uploads that as a 256 x 1 texture. The master curve and the per-channel red, green and blue curves are baked into one RGB texture, so the shader does a single lookup per channel no matter how many curves the preset has.

We were honest about what doesn't port. Texture, Clarity and Dehaze look at neighbouring pixels to find edges, which a one-pixel-at-a-time shader can't do cheaply. They're left out (Dehaze becomes a gentle contrast lift), and the engine's header says plainly that Lightroom is still the reference for hero images.

Step three: 24 megapixels on a small graphics chip

A modern camera JPEG is 6000 x 4000 pixels. Graphics chips have a maximum texture size, and on many phones and older laptops it's 4096 pixels or less. Upload a 6000-pixel-wide image and WebGL either refuses or quietly gives you garbage.

So the renderer works in tiles:

TypeScript
const tile = this.maxTile; // min(4096, the GPU's limits)
for (let ty = 0; ty < H; ty += tile) {
  for (let tx = 0; tx < W; tx += tile) {
    // copy this tile of the photo, upload it, run the shader, draw the result back
    gl.uniform4f(gl.getUniformLocation(this.program, "uTile"), tx, ty, tw, th);
    gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
    ctx.drawImage(this.canvas, 0, 0, tw, th, tx, ty, tw, th);
  }
}

Tiling has a trap: anything that depends on where a pixel is. A shader that only sees one tile would darken the edges of each tile, and a large photo would come out with a faint grid of dark corners. That's what uTile and uFull are for: the shader knows where its tile sits in the whole photo and measures the vignette and the grain against the full frame, so tiles join without a seam.

Memory was the other limit (Part 3 is all about how fast a phone runs out of it). Previews are rendered from a copy scaled down to fit the screen. Only the final export decodes the photo at full size, and every canvas is shrunk to zero and every bitmap closed as soon as it's done, so the browser can free the memory before the next photo.

Step four: keeping the camera's details

A browser that re-encodes a JPEG throws away all its metadata: capture date, camera, lens. For a wedding that's a real loss, because albums and galleries sort by capture time. So the export copies the original's EXIF block into the new file:

TypeScript
// Browser JPEGs start SOI then usually a JFIF APP0; EXIF goes straight after SOI.
return new Blob([out.slice(0, 2), exif, out.slice(2)], { type: "image/jpeg" });

One field has to change. The browser already rotated the pixels upright when it decoded the photo, so the copied Orientation tag is reset to 1 ("as stored"). Leave it alone and every portrait photo gets rotated twice.

If anything about the metadata fails, the photo is still returned without it. Metadata is a nicety, never a reason to lose the photo.

Who gets which looks

Presets are the studio's creative work, so they're not all public. The library has a per-editor "presets access" switch (Admin -> Presets), and each look can be marked customer-visible or not. Customers and visitors on the public page see the curated set; editors with access see everything the studio has imported.

Takeaways

  • Do pixel work where the pixels are. A browser's graphics chip turned a server-side processing pipeline into a page that needs no servers at all.
  • Port the model, not the program. We didn't try to clone Lightroom. We read its slider values and rebuilt the parts that matter for camera JPEGs, and wrote down what we left out.
  • Every device has limits; find them and tile. Ask WebGL for its maximum texture size instead of assuming.
  • Effects that depend on position must know the whole frame, or tiling shows up as seams.
  • Re-encoding is lossy in more ways than one. Carry the metadata across, and fix the fields the browser already acted on.

Found this useful?

Built something with what you learned here, or spotted a better way? Tell us. We work with freelance developers on Shootlo.