using System; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices;
// Fondos hechos de baldosas (codigo propio, 21-11): juntar, medir costuras y volver a cortar.
public static class Mosaico {
  public static int[] Leer(string f, out int w, out int h) {
    using (var b0 = new Bitmap(f)) { w = b0.Width; h = b0.Height;
      using (var b = b0.Clone(new Rectangle(0, 0, w, h), PixelFormat.Format32bppArgb)) {
        var bd = b.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
        var p = new int[w * h]; Marshal.Copy(bd.Scan0, p, 0, p.Length); b.UnlockBits(bd); return p; } }
  }
  public static void Guardar(string f, int[] p, int w, int h) {
    using (var b = new Bitmap(w, h, PixelFormat.Format32bppArgb)) {
      var bd = b.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
      Marshal.Copy(p, 0, bd.Scan0, p.Length); b.UnlockBits(bd); b.Save(f, ImageFormat.Png); }
  }
  static int Dif(int a, int b) { return Math.Abs(((a >> 16) & 255) - ((b >> 16) & 255)) + Math.Abs(((a >> 8) & 255) - ((b >> 8) & 255)) + Math.Abs((a & 255) - (b & 255)); }
  // Diferencia media entre el borde derecho de a y el izquierdo de b (horiz) o el de abajo de a y el de arriba de b.
  // Se compara con la que hay entre dos columnas vecinas dentro de la baldosa (sin costura: cociente ~1).
  public static double Borde(int[] a, int[] b, int t, bool horiz) {
    double s = 0, dentro = 0;
    for (int k = 0; k < t; k++) {
      if (horiz) { s += Dif(a[k * t + t - 1], b[k * t]); dentro += Dif(a[k * t + t - 2], a[k * t + t - 1]) + Dif(b[k * t], b[k * t + 1]); }
      else { s += Dif(a[(t - 1) * t + k], b[k]); dentro += Dif(a[(t - 2) * t + k], a[(t - 1) * t + k]) + Dif(b[k], b[t + k]); }
    }
    return s / (dentro / 2 + t);
  }
  // Junta n baldosas t x t en una rejilla de cols columnas (orden por filas).
  public static int[] Juntar(int[][] bal, int t, int cols, int filas) {
    int W = cols * t, H = filas * t; var q = new int[W * H];
    for (int k = 0; k < bal.Length && k < cols * filas; k++) { if (bal[k] == null) continue; int bx = (k % cols) * t, by = (k / cols) * t;
      for (int y = 0; y < t; y++) Array.Copy(bal[k], y * t, q, (by + y) * W + bx, t); }
    return q;
  }
  // Baldosa t x t con un margen m tomado de sus vecinas (v: 0 arriba-izq, 1 arriba, 2 arriba-der, 3 izq, 4 der,
  // 5 abajo-izq, 6 abajo, 7 abajo-der; null = no hay): donde falta, se repite el borde de la propia baldosa.
  public static int[] ConMargen(int[] c, int[][] v, int t, int m) {
    int W = t + 2 * m; var q = new int[W * W];
    for (int y = 0; y < W; y++) for (int x = 0; x < W; x++) {
      int sx = x - m, sy = y - m; int cx = sx < 0 ? 0 : sx >= t ? 2 : 1, cy = sy < 0 ? 0 : sy >= t ? 2 : 1;
      int[] src = null; int ux = sx, uy = sy;
      if (cx == 1 && cy == 1) src = c;
      else {
        int k = cy == 0 ? cx : cy == 1 ? (cx == 0 ? 3 : 4) : 5 + cx;
        src = v[k];
        if (src != null) { ux = sx < 0 ? sx + t : sx >= t ? sx - t : sx; uy = sy < 0 ? sy + t : sy >= t ? sy - t : sy; }
        else {
          // Sin esa vecina: la del lado (para las esquinas) o el borde de la propia baldosa.
          if (cx != 1 && cy != 1) {
            int[] lado = v[cx == 0 ? 3 : 4], arr = v[cy == 0 ? 1 : 6];
            if (lado != null) { src = lado; ux = sx < 0 ? sx + t : sx - t; uy = Math.Max(0, Math.Min(t - 1, sy)); }
            else if (arr != null) { src = arr; uy = sy < 0 ? sy + t : sy - t; ux = Math.Max(0, Math.Min(t - 1, sx)); }
          }
          if (src == null) { src = c; ux = Math.Max(0, Math.Min(t - 1, sx)); uy = Math.Max(0, Math.Min(t - 1, sy)); }
        }
      }
      q[y * W + x] = src[uy * t + ux];
    }
    return q;
  }
  public static int[] Centro(int[] p, int W, int m, int t) {
    var q = new int[t * t]; for (int y = 0; y < t; y++) Array.Copy(p, (y + m) * W + m, q, y * t, t); return q;
  }
  public static int[] Cortar(int[] p, int W, int t, int k, int cols) {
    var q = new int[t * t]; int bx = (k % cols) * t, by = (k / cols) * t;
    for (int y = 0; y < t; y++) Array.Copy(p, (by + y) * W + bx, q, y * t, t); return q;
  }
}
