Skip to content
Hokusai2.2.2
ivantokar.com

Composite order

A layer is placed in the coordinates of the pipeline at the moment of composite, so anything that changes the size afterwards changes the layer too.

let photo = try Hokusai(url: URL(fileURLWithPath: "scene.png"))
let logo = try Hokusai(url: URL(fileURLWithPath: "emblem.png")).resize(width: 200)
let layer = CompositeLayer(logo, x: 40, y: 40)
// A: composite on the full-size photo, then shrink: the logo shrinks with it.
let compositeThenResize = try photo.composite([layer]).resize(width: 400)
// B: shrink the photo, then composite at the same coordinates: the logo keeps its size.
let resizeThenComposite = try photo.resize(width: 400).composite([layer])
// C: blur after compositing blurs the logo too; blurring only the photo keeps the logo sharp.
let blurAll = try photo.composite([layer]).resize(width: 400).blur(sigma: 3)
let blurPhotoOnly = try photo.resize(width: 400).blur(sigma: 3).composite([CompositeLayer(logo.resize(width: 60), x: 12, y: 12)])
let all: [(String, Hokusai)] = [("composite-then-resize", compositeThenResize), ("resize-then-composite", resizeThenComposite),
("blur-all", blurAll), ("blur-photo-only", blurPhotoOnly)]
for (name, pipeline) in all {
try await pipeline.webp(quality: 85).write(to: URL(fileURLWithPath: "\(name).webp"))
}
  • The landscape at 400 pixels wide with a small emblem: the logo was shrunk with the photo.
    composite → resizeWEBP · 400 × 250 · 7 KB
  • The landscape at 400 pixels wide with a large emblem: the logo kept its 200-pixel size.
    resize → compositeWEBP · 400 × 250 · 8 KB
  • The whole picture blurred, including the emblem.
    composite → resize → blurWEBP · 400 × 250 · 3 KB
  • A blurred landscape with a sharp emblem on top.
    blur the photo, then compositeWEBP · 400 × 250 · 3 KB
  • composite → resize: the logo at (40, 40), 200 px wide on a 1600-px photo, becomes 50 px on the 400-px result, placed at (10, 10).
  • resize → composite: the same layer at (40, 40) on the 400-px photo covers half the width.
  • Blur: effects after composite act on the logo too. To keep a logo crisp, apply the effect to the photo first and composite last, as in the fourth image (where the logo was also resized to suit).

Layers are drawn in array order, later over earlier (Composition). A layer is a pipeline of its own, so its transformations (resize, blur, negate) happen before it is composed; the order of operations inside the layer is the same story as anywhere else.