Skip to content

Free open-source image editor for the browser

3.4 KB
Raw Blame
import { z } from "zod/mini";
import {
  type BrushStroke,
  findLayer,
  type HealPatch,
  type Scene,
} from "@/core/document";
import { strokeSchema } from "@/core/document/brush";
import type { Point } from "@/core/image/frame";
import { change, point, unit } from "@/lib/parse";
import { fieldLongSide } from "./inpaint";

export function findHealPatch(scene: Scene, layerId: string, patchId: string) {
  const layer = findLayer(scene.layers, layerId);
  if (layer?.kind !== "heal") return;
  return layer.patches.find((patch) => patch.id === patchId);
}

/** Whether a dab reaches the image at all; a stroke starting further away has nothing to repair. */
export function dabTouchesImage(
  [x, y]: readonly number[],
  radius: number,
  size: readonly number[],
) {
  return (
    x + radius > 0 &&
    y + radius > 0 &&
    x - radius < size[0] &&
    y - radius < size[1]
  );
}

/** Bounds include the whole soft brush edge, with room for a known boundary. */
export function patchBounds(
  strokes: readonly BrushStroke[],
  size: readonly number[],
  margin = 0,
) {
  let left = size[0];
  let top = size[1];
  let right = 0;
  let bottom = 0;
  for (const stroke of strokes) {
    if (stroke.mode === "erase") continue;
    const radius = stroke.size / 2 + margin + 4;
    for (const [x, y] of stroke.points) {
      left = Math.min(left, x - radius);
      top = Math.min(top, y - radius);
      right = Math.max(right, x + radius);
      bottom = Math.max(bottom, y + radius);
    }
  }
  const origin: Point = [
    Math.max(0, Math.floor(left)),
    Math.max(0, Math.floor(top)),
  ];
  const extent: Point = [
    Math.max(1, Math.min(size[0], Math.ceil(right)) - origin[0]),
    Math.max(1, Math.min(size[1], Math.ceil(bottom)) - origin[1]),
  ];
  return { origin, extent };
}

/** A square around the patch with a little margin, so a thumbnail keeps the stroke's proportions. */
export function patchThumbnailRegion(
  strokes: readonly BrushStroke[],
  size: readonly number[],
): { origin: Point; extent: Point } {
  const { origin, extent } = patchBounds(strokes, size);
  const side = Math.max(extent[0], extent[1]) * 1.1;
  return {
    origin: [
      origin[0] + (extent[0] - side) / 2,
      origin[1] + (extent[1] - side) / 2,
    ],
    extent: [side, side],
  };
}

export const patchStroke = strokeSchema.check(
  z.refine(
    (stroke) => stroke.mode === "paint",
    "Heal patches use painted strokes",
  ),
);

export const patchBlend = change(z.object({ feather: unit, opacity: unit }));

export const donorModeSchema = z.enum(["heal", "clone"]);

const patchShape = {
  id: z.string().check(z.minLength(1)),
  feather: unit,
  strokes: z.array(strokeSchema).check(
    z.minLength(1),
    z.refine(
      (strokes) => strokes[0]?.mode === "paint",
      "Start a patch with paint",
    ),
  ),
  opacity: unit,
};

const fieldSide = z.int().check(z.minimum(1), z.maximum(fieldLongSide));

/** Where a Remove field's texels sit in the photo, as a scene file stores it beside the texels. */
export const fieldLatticeSchema = z.object({
  origin: point,
  scale: z.number().check(z.minimum(1)),
  size: z.tuple([fieldSide, fieldSide]),
});

export const healPatchSchema = z.union([
  z.object({
    ...patchShape,
    mode: z._default(donorModeSchema, "heal"),
    offset: point,
  }),
  z.object({
    ...patchShape,
    mode: z.literal("remove"),
    field: z.string().check(z.minLength(1)),
  }),
]) satisfies z.ZodMiniType<HealPatch>;