Skip to content

Retouching and selections

Level a tilted shot, neutralise a colour cast - automatically or from a colour you pick - lift a product off a plain backdrop and trim to it, recolour one region with the magic wand and paint by hand - then save the whole edit as a project file that reopens with every step still editable.

Try it

A generated product shot, a little crooked, warm from tungsten light and on an off-white backdrop. Each control changes one value in the document, and the document is rendered again.

Live
A generated 900 × 640 product shot.
Took —
Output 821 × 584
Last project —

Geometry and colour

White balance

Selections

Label colour, by magic wand

Painting

Colour from a sample, edges from the content

Two measurements turn into edits. ColorSampler.Sample is the eyedropper: it averages a few pixels around a point so sensor noise does not decide the colour, and WhiteBalance scales the channels so that colour becomes neutral - or AutoWhiteBalance finds the neutral itself. ContentBounds.Find measures the transparent or uniform border around the subject, and a Crop to what it returns is the trim. The editor's Adjust tab has the same picker, and its Transform tab the trim.

// White balance from a colour that should be grey: a 9 × 9 average around a point on the backdrop.
var neutral = ColorSampler.Sample(photo, new Point(40, 40), radius: 4);
pipeline = pipeline.WhiteBalance(neutral);           // or .AutoWhiteBalance(0.9f)

// Trim: render, measure the border, crop to the content - keeping layers and paint where they were.
using var untrimmed = await new DocumentRenderer().RenderAsync(document, photo);
if (ContentBounds.Find(untrimmed, new TrimOptions { Padding = 16 }) is { } content)
    document = document.AddOperation(new CropOperation(content), keepOverlaysAttached: true);

// A trim is a measurement and then a crop, because an operation's output size must be known without the pixels.

Selections that read the image

A FloodMask is the magic wand: from each seed it spreads to the neighbouring pixels within a tolerance of the seed's colour, with an anti-aliased edge. A MorphedMask grows, shrinks or outlines any mask by an exact distance. Both are values like every other mask, so they rescale for the editor's proxy and replay at full resolution.

// Make a plain backdrop transparent, flooding in from the four corners.
pipeline.RemoveBackground(tolerance: 0.12f);

// Recolour one region: the wand from a point inside it, grown a pixel to take its anti-aliased edge.
document = document.AddLayer(new FillLayer(new Rgba32(20, 170, 160))
{
    BlendMode = BlendMode.Color,
    Mask = new FloodMask([new(450, 360)]) { Tolerance = 0.2f }.Expand(1),
});

// Select by colour anywhere, contract the selection, outline it.
var reds = new FloodMask([seed]) { Contiguous = false }.Contract(2);
var outline = reds.Border(6);

Painting is strokes, not pixels

A PaintLayer keeps every stroke - its points, pen pressures, size, hardness and colour - and stamps them when it is composited. An erasing stroke takes the layer's own paint away and never the image below, and twenty strokes of history cost kilobytes.

var layer = new PaintLayer()
    .Add(new PaintStroke(points, size: 14, color: new Rgba32(230, 120, 40)) { Hardness = 0.6f, Pressures = penPressures })
    .Add(new PaintStroke(eraserPoints, size: 8, color: default) { Erase = true });
document = document.AddLayer(layer);

// Straighten the photo afterwards and the paint stays where it was painted.
document = document.AddOperation(new StraightenOperation(4), keepOverlaysAttached: true);

Projects

BmageProject writes the original, the document and every image it refers to into one ZIP file, and reads it back with each layer, mask, adjustment and stroke still editable. Reading one is safe on untrusted files: only the files it names are read, sizes are bounded while unpacking, and images are checked against the limits before they are decoded.

await using var file = File.Create("product.bmage");
await BmageProject.SaveAsync(file, original, document);

using var project = await BmageProject.LoadAsync(File.OpenRead("product.bmage"));
using var rendered = new DocumentRenderer().Render(project.Document, project.Original);

// In the editor: Export > Save project, and Open project - or from code:
await editor.SaveProjectAsync("product");
await editor.OpenProjectAsync(stream);
An unhandled error has occurred. Reload 🗙