trimStretchRegion(stage) {
    if (!stage.srcCtx) {
      return;
    }

    const srcData = stage.srcCtx.getImageData(0, 0, stage.srcSize.w, stage.srcSize.h);

    let collapseX = stage.stretchRect.w > 4; // generally going to start as true
    let collapseY = stage.stretchRect.h > 4; // generally going to start as true
    let x, y;

    // See if collapse is possible in either direction by comparing row/column sums.
    const summer = new Summer();

    // See if can be horizontally collapsed.
    let first = true;
    let firstSum = -1;
    for (x = stage.stretchRect.x; x < (stage.stretchRect.x + stage.stretchRect.w); x++) {
      // Compute column
      summer.reset();
      for (y = 0; y < stage.srcSize.h; y++) {
        summer.addNext(getPixel_(stage, srcData, x, y));
      }
      if (first) {
        firstSum = summer.compute();
        first = false;
      } else if (summer.compute() != firstSum) {
        collapseX = false;
        break;
      }
    }

    first = true;
    for (y = stage.stretchRect.y; y < (stage.stretchRect.y + stage.stretchRect.h); y++) {
      // Compute row
      summer.reset();
      for (x = 0; x < stage.srcSize.w; x++) {
        summer.addNext(getPixel_(stage, srcData, x, y));
      }
      if (first) {
        firstSum = summer.compute();
        first = false;
      } else if (summer.compute() != firstSum) {
        collapseY = false;
        break;
      }
    }

    if (!collapseX && !collapseY) {
      // No-op
      return;
    }

    const fixed = {
      l: stage.stretchRect.x,
      t: stage.stretchRect.y,
      r: stage.srcSize.w - stage.stretchRect.x - stage.stretchRect.w,
      b: stage.srcSize.h - stage.stretchRect.y - stage.stretchRect.h
    };

    const middle = {
      w: collapseX ? 4 : stage.stretchRect.w,
      h: collapseY ? 4 : stage.stretchRect.h
    };

    const size = {
      w: fixed.l + middle.w + fixed.r,
      h: fixed.t + middle.h + fixed.b
    };

    // Redraw components
    const ctx = studio.Drawing.context(size);

    // TL
    if (fixed.l && fixed.t)
      ctx.drawImage(stage.srcCtx.canvas,
          0, 0, fixed.l, fixed.t,
          0, 0, fixed.l, fixed.t);

    // BL
    if (fixed.l && fixed.b)
      ctx.drawImage(stage.srcCtx.canvas,
          0, stage.srcSize.h - fixed.b, fixed.l, fixed.b,
          0, size.h - fixed.b, fixed.l, fixed.b);

    // TR
    if (fixed.r && fixed.t)
      ctx.drawImage(stage.srcCtx.canvas,
          stage.srcSize.w - fixed.r, 0, fixed.r, fixed.t,
          size.w - fixed.r, 0, fixed.r, fixed.t);

    // BR
    if (fixed.r && fixed.b)
      ctx.drawImage(stage.srcCtx.canvas,
          stage.srcSize.w - fixed.r, stage.srcSize.h - fixed.b, fixed.r, fixed.b,
          size.w - fixed.r, size.h - fixed.b, fixed.r, fixed.b);

    // Top
    if (fixed.t)
      ctx.drawImage(stage.srcCtx.canvas,
          fixed.l, 0, stage.stretchRect.w, fixed.t,
          fixed.l, 0, size.w - fixed.l - fixed.r, fixed.t);

    // Left
    if (fixed.l)
      ctx.drawImage(stage.srcCtx.canvas,
          0, fixed.t, fixed.l, stage.stretchRect.h,
          0, fixed.t, fixed.l, size.h - fixed.t - fixed.b);

    // Right
    if (fixed.r)
      ctx.drawImage(stage.srcCtx.canvas,
          stage.srcSize.w - fixed.r, fixed.t, fixed.r, stage.stretchRect.h,
          size.w - fixed.r, fixed.t, fixed.r, size.h - fixed.t - fixed.b);

    // Bottom
    if (fixed.b)
      ctx.drawImage(stage.srcCtx.canvas,
          fixed.l, stage.srcSize.h - fixed.b, stage.stretchRect.w, fixed.b,
          fixed.l, size.h - fixed.b, size.w - fixed.l - fixed.r, fixed.b);

    // Middle
    ctx.drawImage(stage.srcCtx.canvas,
        fixed.l, fixed.t, stage.stretchRect.w, stage.stretchRect.h,
        fixed.l, fixed.t, size.w - fixed.l - fixed.r, size.h - fixed.t - fixed.b);

    const rects = {
      stretchRect: {
        x: stage.stretchRect.x,
        y: stage.stretchRect.y,
        w: middle.w,
        h: middle.h
      },
      contentRect: {
        x: stage.contentRect.x,
        y: stage.contentRect.y,
        w: stage.contentRect.w + middle.w - stage.stretchRect.w,
        h: stage.contentRect.h + middle.h - stage.stretchRect.h
      },
      opticalBoundsRect: {
        x: stage.opticalBoundsRect.x,
        y: stage.opticalBoundsRect.y,
        w: stage.opticalBoundsRect.w + middle.w - stage.stretchRect.w,
        h: stage.opticalBoundsRect.h + middle.h - stage.stretchRect.h
      }
    };

    stage.name = `${stage.name}-STRETCH_TRIMMED`;
    stage.loadSourceImage(ctx, rects);
  }
Trims excess rows and columns from the stretch region of the given
nine patch stage.