{
 "id": "image-quilting",
 "title": "Image quilting",
 "aliases": [],
 "domain": "materials",
 "subdomain": "texture synthesis",
 "stage": "material",
 "realtime": "offline",
 "platforms": [
  "cpu",
  "python"
 ],
 "year_introduced": 2001,
 "inputs": [
  "texture"
 ],
 "outputs": [
  "texture"
 ],
 "style": "both",
 "dynamic": "static",
 "summary": "Synthesises a larger texture from a small exemplar by stitching overlapping patches along minimum-error seams. Preserves structure such as bricks and strokes that pixel-based synthesis blurs.",
 "how_it_works": "Efros and Freeman tile the output in raster order with square blocks taken from the exemplar. For each new block, candidates are scored by how well their overlap region matches the already-placed neighbours (sum of squared differences), and one is picked at random from those within a tolerance of the best, which adds variety. The overlap is not blended; instead a minimum-cost path through the overlap error surface, found by dynamic programming, chooses the cut that hides the seam along pixels already similar. A variant adds a correspondence map for texture transfer: blocks must also match a target image's luminance, so a photograph is re-rendered in the texture's style. Block size controls the scale of structure that survives; larger blocks reduce visible repetition less.",
 "when_to_use": "Choose it for offline generation of large, structured, seamless textures (brick, stone walls, foliage) from a single scan, or for texture transfer stylisation. Prefer `histogram-preserving-blending` or `hex-tiling` for real-time, runtime anti-tiling of stochastic textures. Prefer procedural noise (`procedural-noise-texturing`) when no exemplar exists.",
 "cost": {
  "class": "offline",
  "notes": "Seconds to minutes per output image on a CPU; not intended for per-frame use."
 },
 "failure_modes": [
  "Verbatim copying of recognisable chunks reveals the exemplar as visible repetition",
  "Fails on globally structured or semantic textures (faces, text, long straight lines crossing blocks)",
  "Block size trade-off: too small loses structure, too large repeats",
  "Seam cuts can leave staircase artefacts on smooth gradients"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1145/383259.383296",
   "type": "paper",
   "title": "Image quilting for texture synthesis and transfer",
   "authors": [
    "Alexei A. Efros",
    "William T. Freeman"
   ],
   "year": 2001,
   "venue": "Proceedings of the 28th annual conference on Computer graphics and interactive techniques",
   "url": "https://doi.org/10.1145/383259.383296",
   "ids": {
    "doi": "10.1145/383259.383296"
   },
   "license": null,
   "extra": {
    "csl_type": "proceedings-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/383259.383296",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/383259.383296",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/383259.383296",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/383259.383296",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/383259.383296",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "efros2001image"
  }
 ],
 "presets": [
  {
   "id": "image-quilting-live",
   "title": "Image quilting — straight patch seams vs minimum-error boundary-cut synthesis",
   "runtime": "glsl-fragment",
   "params": {
    "P_PATCH_SCALE": 3.6,
    "P_CUT_WIGGLE": 1,
    "P_SEAM_VIS": 0.65,
    "P_SPLIT": 0.38
   },
   "basis": "original",
   "license_note": "Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).",
   "code": "const float P_PATCH_SCALE = 3.6; // P Patch Grid [2.0, 7.0, 0.2]\nconst float P_CUT_WIGGLE = 1.0; // P Min-Error Cut [0.0, 1.8, 0.05]\nconst float P_SEAM_VIS = 0.65; // P Seam Highlight [0.0, 1.5, 0.05]\nconst float P_SPLIT = 0.38; // P Naive/Quilt Split [0.0, 1.0, 0.02]\n\nvec2 hash22(vec2 p) {\n  p = vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)));\n  return fract(sin(p) * 43758.5453);\n}\n\n// Rich procedural cobblestone & terracotta mosaic exemplar texture (returns vec4(color, mortarMask))\nvec4 sampleExemplar(vec2 p) {\n  vec2 n = floor(p);\n  vec2 f = fract(p);\n  float d1 = 8.0, d2 = 8.0;\n  vec2 bestId = vec2(0.0);\n  vec2 bestR = vec2(0.0);\n  for (int j = -1; j <= 1; j++) {\n    for (int i = -1; i <= 1; i++) {\n      vec2 g = vec2(float(i), float(j));\n      vec2 o = 0.16 + 0.68 * hash22(n + g);\n      vec2 r = g + o - f;\n      float d = dot(r, r);\n      if (d < d1) { d2 = d1; d1 = d; bestId = n + g; bestR = r; }\n      else if (d < d2) { d2 = d; }\n    }\n  }\n  float edge = sqrt(d2) - sqrt(d1);\n  vec2 h = hash22(bestId * 1.7 + 3.1);\n\n  // Warm Mediterranean limestone, slate & terracotta stone palette\n  vec3 stoneA = vec3(0.68, 0.56, 0.44);\n  vec3 stoneB = vec3(0.46, 0.49, 0.54);\n  vec3 stoneC = vec3(0.76, 0.41, 0.28);\n  vec3 baseCol = mix(stoneA, stoneB, h.x);\n  baseCol = mix(baseCol, stoneC, smoothstep(0.68, 0.92, h.y));\n\n  // Stone micro-grain + directional bevel lighting\n  float grain = 0.88 + 0.24 * sin(p.x * 19.0 + h.x * 6.0) * cos(p.y * 17.0 - h.y * 6.0);\n  float bevel = smoothstep(0.04, 0.22, edge);\n  float tilt = clamp(0.65 + 0.55 * dot(normalize(bestR + 1e-4), normalize(vec2(-0.6, 0.8))), 0.25, 1.35);\n  vec3 stoneCol = baseCol * grain * tilt * (0.62 + 0.42 * bevel);\n\n  // Dark sandy mortar groove\n  float mortar = smoothstep(0.11, 0.045, edge);\n  vec3 mortarCol = vec3(0.14, 0.13, 0.12) * (0.85 + 0.3 * sin(p.x * 45.0) * cos(p.y * 45.0));\n  vec3 col = mix(stoneCol, mortarCol, mortar);\n  return vec4(col, mortar);\n}\n\n// Compute minimum-error cut energy between two overlapping patches A and B\nfloat overlapError(vec2 uvA, vec2 uvB) {\n  vec4 a = sampleExemplar(uvA);\n  vec4 b = sampleExemplar(uvB);\n  vec3 d = a.rgb - b.rgb;\n  // Strongly prefer cuts passing through low-contrast mortar valleys\n  float valleyBonus = 1.0 - 0.55 * max(a.a, b.a);\n  return (dot(d, d) + 0.04) * valleyBonus;\n}\n\nvec3 aces(vec3 x) {\n  x = max(x, 0.0);\n  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n  vec2 screenUV = fragCoord / iResolution.xy;\n\n  float patchScale = clamp(P_PATCH_SCALE, 1.8, 8.0);\n  float cutWig = clamp(P_CUT_WIGGLE, 0.0, 2.0);\n  float seamVis = clamp(P_SEAM_VIS, 0.0, 1.8);\n  float splitX = clamp(P_SPLIT, 0.0, 1.0);\n\n  vec2 pan = vec2(iTime * 0.16, iTime * 0.09);\n  vec2 gp = uv * patchScale + pan;\n  vec2 cell = floor(gp);\n  vec2 f = fract(gp);\n\n  float texFreq = 3.2;\n\n  // Each synthesized patch selects a random exemplar offset\n  vec2 offC = hash22(cell) * 14.0;\n  vec2 offR = hash22(cell + vec2(1.0, 0.0)) * 14.0;\n  vec2 offU = hash22(cell + vec2(0.0, 1.0)) * 14.0;\n  vec2 offUR = hash22(cell + vec2(1.0, 1.0)) * 14.0;\n\n  vec2 pC = gp * texFreq + offC;\n  vec2 pR = gp * texFreq + offR;\n  vec2 pU = gp * texFreq + offU;\n  vec2 pUR = gp * texFreq + offUR;\n\n  vec3 col;\n  float seamGlow = 0.0;\n\n  if (screenUV.x < splitX) {\n    // Left side: naive unquilted square patch tiling with abrupt visible seams\n    col = sampleExemplar(pC).rgb;\n    float edge = min(min(f.x, 1.0 - f.x), min(f.y, 1.0 - f.y));\n    seamGlow = smoothstep(0.032, 0.005, edge) * seamVis;\n    col = mix(col, vec3(1.0, 0.32, 0.22), seamGlow * 0.78);\n  } else {\n    // Right side: Efros-Freeman minimum-error boundary cut across 28% overlap strips\n    // Sample error energy across horizontal & vertical overlap bands to find lowest-error cut position\n    float bestX = 0.84;\n    float minEX = 1e6;\n    float bestY = 0.84;\n    float minEY = 1e6;\n    for (int k = 0; k < 5; k++) {\n      float s = 0.72 + float(k) * 0.065;\n      // Add subtle smooth path continuity along perpendicular axis\n      vec2 probeX = vec2(cell.x + s, gp.y) * texFreq;\n      float ex = overlapError(probeX + offC, probeX + offR);\n      if (ex < minEX) { minEX = ex; bestX = s; }\n\n      vec2 probeY = vec2(gp.x, cell.y + s) * texFreq;\n      float ey = overlapError(probeY + offC, probeY + offU);\n      if (ey < minEY) { minEY = ey; bestY = s; }\n    }\n    // Smooth organic boundary curve modulated by P_CUT_WIGGLE\n    float cutX = mix(0.85, bestX + 0.045 * sin(gp.y * 9.0 + cell.x * 2.3), clamp(cutWig, 0.0, 1.5));\n    float cutY = mix(0.85, bestY + 0.045 * cos(gp.x * 9.0 + cell.y * 2.7), clamp(cutWig, 0.0, 1.5));\n\n    float wx = smoothstep(cutX - 0.025, cutX + 0.025, f.x);\n    float wy = smoothstep(cutY - 0.025, cutY + 0.025, f.y);\n\n    vec3 c00 = sampleExemplar(pC).rgb;\n    vec3 c10 = sampleExemplar(pR).rgb;\n    vec3 c01 = sampleExemplar(pU).rgb;\n    vec3 c11 = sampleExemplar(pUR).rgb;\n\n    col = mix(mix(c00, c10, wx), mix(c01, c11, wx), wy);\n\n    // Highlight the jagged minimum-error cut path in glowing turquoise-gold\n    float dCutX = abs(f.x - cutX);\n    float dCutY = abs(f.y - cutY);\n    seamGlow = max(smoothstep(0.028, 0.004, dCutX), smoothstep(0.028, 0.004, dCutY)) * seamVis;\n    col = mix(col, vec3(0.28, 0.96, 0.88), seamGlow * 0.72);\n  }\n\n  if (splitX > 0.01 && splitX < 0.99 && abs(screenUV.x - splitX) < 0.004) {\n    col = vec3(1.0, 0.86, 0.42);\n  }\n\n  fragColor = vec4(pow(clamp(aces(col * 1.15), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "conceptual-diagram",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "image-quilting",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T16:50:13.852Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.6394,
      "std": 0.1623,
      "unique_colors": 546
     },
     "image": "/specimens/image-quilting-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "histogram-preserving-blending",
  "hex-tiling",
  "procedural-noise-texturing"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/materials/image-quilting.json"
 },
 "solves": [
  "need a large texture from a tiny sample",
  "tiled textures show obvious repetition",
  "brick or stone patterns blur when synthesised pixel by pixel",
  "stitch patches into seamless structured texture",
  "transfer texture style onto another image"
 ],
 "authority": "Efros & Freeman, 2001",
 "url": "https://texelatlas.vercel.app/t/image-quilting/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 104.55,
  "tier": "extreme",
  "p50_ms": 3.4397,
  "p95_ms": 3.5559,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "image-quilting-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 90.99,
   "tier": "extreme",
   "p50_ms": 1.4467,
   "p95_ms": 1.4526,
   "speedup_x": 2.38,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 62.03,
   "tier": "extreme",
   "p50_ms": 1.7864,
   "p95_ms": 1.7921,
   "speedup_x": 1.93,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}