Skip to main content
Glama

robust_median_stretch

Stretch a linear view in place so its median lands on a target value, using robust MAD statistics to set black point and midtones transfer. Supports linked channels, multiple passes, and highlight compression.

Instructions

Stretch a linear view in place so its median lands on target_median. Per pass: from the median M and the unnormalized median absolute deviation D of the image (range clipping off, alpha excluded), sigma = 1.4826·D and the black point c0 = max(0, M − black_point_sigma·sigma); samples are rescaled to u = clamp((x − c0)/(1 − c0), 0, 1), and the midtones transfer function MTF(m, u) = (m − 1)·u / ((2m − 1)·u − m) is applied with the balance m solved so that MTF(m, u(M)) = target_median exactly. linked true takes one set of statistics over all channels jointly; false takes them per channel. Each further pass repeats this on the previous output, re-measuring the statistics; passes before the last run on a scratch copy, so the view is written once and is left unmodified when any pass cannot be solved (a median on the black point, e.g. D = 0). With highlight_knee h and highlight_midtones mh, values y above h become h + (1 − h)·MTF(mh, (y − h)/(1 − h)) after the last pass. The result is JSON: per pass and per statistics group M, D, sigma, c0, the rescaled median xbar and m.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
linkedNoColour images only, required for them: true = joint statistics over all channels and one transform for all; false = statistics and transform per channel. Ignored for a grayscale image.
passesNoNumber of times the whole procedure runs, each on the previous output (>= 1; omitted = 1)
view_idYesView to stretch (modified in place)
target_medianYesMedian of the output, strictly between 0 and 1
highlight_kneeNoOutput level above which values are compressed, strictly between 0 and 1. Given together with highlight_midtones; omitted = no highlight compression.
black_point_sigmaYesDistance of the black point below the median, in units of 1.4826·MAD (>= 0)
highlight_midtonesNoMTF balance applied to the segment above highlight_knee, strictly between 0.5 and 1. Given together with highlight_knee.

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observedv1.2.1

TDQS

A4.6/5.0
Behavior5/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure, and it does so thoroughly. It discloses that the view is modified in place, that passes before the last run on a scratch copy, that the view is left unmodified when any pass cannot be solved (e.g., D = 0), and that the result is JSON with per-pass statistics. It also explains the exact mathematical transform, including the MTF function and highlight compression. This is far beyond what annotations would typically provide.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is dense and information-rich, with the core purpose front-loaded in the first sentence. Every sentence adds technical detail that an agent needs to understand the algorithm. It is long, but the complexity of the tool justifies the length. It could be slightly more concise by trimming some mathematical notation, but the structure is logical: purpose, algorithm, parameters, edge cases, output.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a complex 7-parameter image processing tool with no annotations and no output schema, the description is remarkably complete. It covers the algorithm, parameter semantics, edge cases (D = 0, unsolvable passes), the in-place mutation behavior, the scratch copy behavior, and the JSON return format. An agent could invoke this tool correctly with high confidence based on the description alone.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents all 7 parameters. The description adds significant meaning beyond the schema: it explains how black_point_sigma relates to sigma = 1.4826·D, how passes interact with the previous output, how linked affects statistics grouping, and how highlight_knee/highlight_midtones are applied after the last pass. The only minor gap is that the description doesn't explicitly restate each parameter's constraints, but the schema already does that.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description opens with a specific verb and resource: 'Stretch a linear view in place so its median lands on target_median.' It names the exact operation, the in-place mutation, and the target condition. It also distinguishes itself from siblings like run_curves, auto_stretch, and stretch_stars by describing a precise median-targeting algorithm rather than a generic stretch.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description explains the algorithm's behavior in detail, including per-pass statistics, the linked parameter for color vs grayscale, and the highlight_knee/highlight_midtones optional compression. It does not explicitly name sibling alternatives or state when to choose this over run_curves or auto_stretch, but the detailed mathematical behavior gives an agent enough context to know when this tool is appropriate. The 'linked true takes one set of statistics over all channels jointly; false takes them per channel' line is a clear usage condition for color images.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.