/**
   * Vergleicht das angegebene Bild mit diesem Bild und liefert true wenn Höhe und Breite
   * übereinstimmen und der Pixelwert an jeder Position des angegebenen Bildes mit dem in diesem
   * Bild übereinstimmt. Ansonsten wird false geliefert.
   *
   * @param o other object to compare with this
   */
  public boolean equals(Object o) {
    if (o == this) {
      return true;
    }
    if (o == null) {
      return false;
    }
    if (o.getClass() != this.getClass()) {
      return false;
    }

    AsciiImage other = (AsciiImage) o;

    if (!other.charset.equals(this.charset)) {
      return false;
    } else if (other.imageHeight != this.imageHeight) {
      return false;
    } else if (other.imageWidth != this.imageWidth) {
      return false;
    }

    for (int y = 0; y < imageHeight; y++) {
      for (int x = 0; x < imageWidth; x++) {
        if (other.getPixel(x, y) != this.getPixel(x, y)) {
          return false;
        }
      }
    }

    return true;
  }
Example #2
0
 public AsciiImage execute(AsciiImage img) throws OperationException {
   image = new AsciiImage(img);
   for (int i = 0; i < img.getHeight(); i++) {
     for (int j = 0; j < img.getWidth(); j++) {
       image.setPixel(j, i, img.getCharset().charAt(img.getCharset().length() - 1));
     }
   }
   return image;
 }
  /**
   * Executes this ReplaceOperation and returns as new AsciiImage where all occurrences of the
   * oldChar are replaced by the newChar. Other chars remain unchanged.
   *
   * @param img The AsciiImage to use as basis for executing the Operation, it will remain unchanged
   * @return A new AsciiImage reflecting the result of the executed Operation
   * @throws OperationException Thrown if the newChar is not part of the AsciiImage's charset
   */
  public AsciiImage execute(AsciiImage img) throws OperationException {

    if (img.getCharset().indexOf(newChar) < 0) {
      throw new OperationException("Invalid char");
    }

    AsciiImage result = new AsciiImage(img);

    ArrayList<AsciiPoint> region = img.getPointList(oldChar);
    for (AsciiPoint p : region) {
      result.setPixel(p, newChar);
    }

    return result;
  }
 public int distance(AsciiImage i1, AsciiImage i2) {
   int diff = 0;
   if ((i1.getWidth() * i1.getHeight()) == (i2.getWidth() * i2.getHeight())) {
     return 0;
   } else {
     diff = ((i1.getWidth() * i1.getHeight()) - (i2.getWidth() * i2.getHeight()));
     return diff < 0 ? (diff * -1) : diff;
   }
 }
Example #5
0
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in); // Init der lokalen Variablen
    String a, b;
    AsciiImage image, imagecp;
    AsciiPoint p;
    AsciiStack stack = new AsciiStack(3);
    int width, height, x0, x1, y0, y1;
    char c, c1;

    a = in.next(); // einlesen des create-Befehls
    if (a.equals("create")) {
      width = in.nextInt(); // einlesen 1. Parameter - breite
      if (width <= 0) { // unpassende breite - Fehlerbehandlung
        panic();
        return;
      }
      height = in.nextInt(); // einlesen 2. Parameter - höhe
      if (height <= 0) { // unpassende höhe - Fehlerbehandlung
        panic();
        return;
      }
      image = new AsciiImage(width, height); // Aufruf Konstruktor
    } else { // Wenn 1. Befehl nicht create
      panic();
      return;
    }

    while (in.hasNext()) { // solange Eingabe
      a = in.next(); // Speichern um zu vergleichen
      if (a.equals("clear")) {
        imagecp = new AsciiImage(image);
        stack.push(imagecp);
        image.clear();
      } else if (a.equals("line")) {
        imagecp = new AsciiImage(image);
        stack.push(imagecp);
        if (!in.hasNextInt()) { // 1. Parameter kein Integer - Fehlerbehandlung
          panic();
          return;
        }
        x0 = in.nextInt();
        if (!in.hasNextInt()) { // 2. Parameter kein Integer - Fehlerbehandlung
          panic();
          return;
        }
        y0 = in.nextInt();
        if (!in.hasNextInt()) { // 3. Parameter kein Integer - Fehlerbehandlung
          panic();
          return;
        }
        x1 = in.nextInt();
        if (!in.hasNextInt()) { // 4. Parameter kein Integer - Fehlerbehandlung
          panic();
          return;
        }
        y1 = in.nextInt();
        if (!in.hasNext()) { // 5. Parameter nicht vorhanden - Fehlerbehandlung
          panic();
          return;
        }
        a = in.next();
        c = a.charAt(0); // char aus der Eingabe rausfiltern
        image.drawLine(x0, y0, x1, y1, c); // Methodenaufruf
      } else if (a.equals("load")) {
        imagecp = new AsciiImage(image);
        stack.push(imagecp);
        a = in.next(); // EoF String einlesen
        int j = 0;
        while (in.hasNext()
            && j < image.getHeight()) { // solange Eingabe und Höhe nicht überschritten
          b = in.next(); // einlesen Zeile
          if (b == a
              || b.length()
                  != image
                      .getWidth()) { // Höhe zu klein oder breite zu klein/groß - Fehlerbehandlung
            panic();
            return;
          }
          for (int i = 0; i < image.getWidth(); i++) { // Zeilen in Image speichern
            image.setPixel(i, j, b.charAt(i));
          }
          j++; // Höhe inkrementieren
        }
        b = in.next(); // Wenn das Bild die richtige Höhe hat
        if (!b.equals(a)) { // aber das EoF String nicht kommt - Fehlerbehandlung
          panic();
          return;
        }
      } else if (a.equals("print")) {
        System.out.println(image.toString());
      } else if (a.equals("replace")) {
        imagecp = new AsciiImage(image);
        stack.push(imagecp);
        if (!in.hasNext()) { // 1. Parameter nicht vorhanden - Fehlerbehandlung
          panic();
          return;
        }
        a = in.next(); // char aus der Eingabe rausfiltern
        c = a.charAt(0);
        if (!in.hasNext()) { // 2. Parameter nicht vorhanden - Fehlerbehandlung
          panic();
          return;
        }
        a = in.next(); // char aus der Eingabe rausfiltern
        c1 = a.charAt(0);
        image.replace(c, c1); // Methodenaufruf
      } else if (a.equals("transpose")) {
        imagecp = new AsciiImage(image);
        stack.push(imagecp);
        image.transpose();
      } else if (a.equals("fill")) {
        if (!in.hasNextInt()) { // 1. Parameter kein Int - Fehlerbehandlung
          panic();
          return;
        }
        x0 = in.nextInt();
        if (x0 > image.getWidth() || x0 < 0) { // Bereichsüberprüfung
          System.out.println("OPERATION FAILED");
          return;
        }
        if (!in.hasNextInt()) { // 2. Parameter kein Int - Fehlerbehandlung
          panic();
          return;
        }
        y0 = in.nextInt();
        if (y0 > image.getHeight() || y0 < 0) { // Bereichsüberprüfung
          System.out.println("OPERATION FAILED");
          return;
        }
        if (!in.hasNext()) { // 3. Parameter nicht vorhanden - Fehlerbehandlung
          panic();
          return;
        }
        a = in.next();
        c = a.charAt(0); // char aus der Eingabe rausfiltern
        image.fill(x0, y0, c);
      } else if (a.equals("centroid")) {
        if (!in.hasNext()) { // kein Parameter - Fehlerbehandlung
          panic();
          return;
        }
        a = in.next();
        c = a.charAt(0); // Char aus der EIngabe rausfiltern
        p = image.getCentroid(c);
        System.out.println(p.toString()); // Ausgabe des Schwerpunktes
      } else if (a.equals("grow")) {
        imagecp = new AsciiImage(image); // Kopie erstellen für "undo"
        stack.push(imagecp); // Kopie auf Stack
        if (!in.hasNext()) { // kein Parameter - Fehlerbehandlung
          panic();
          return;
        }
        a = in.next();
        c = a.charAt(0); // Char aus der EIngabe rausfiltern
        image.growRegion(c);
      } else if (a.equals("undo")) {
        if (stack.size() == 0) { // WEnn Stack leer
          System.out.println("STACK EMPTY");
        } else { // von Stack die alte version des Bildes holen
          image = stack.pop();
          System.out.println(
              "STACK USAGE "
                  + stack.size()
                  + "/"
                  + stack.capacity()); // Ausgabe des StacksSpeicherZustdands
        }
      } else { // Wenn kein gültiger Befehl
        System.out.println("UNKNOWN COMMAND");
        return;
      }
    }
  }
Example #6
0
  /**
   * liest die Daten und Befehle ein und gibt das Ergebnis aus. Allein diese Methode liest direkt
   * von System.in ein und gibt direkt auf System.out aus.
   *
   * @param args
   */
  public static void main(String[] args) {

    AsciiImage image;
    Scanner sc = new Scanner(System.in);
    int height = 0;
    int width = 0;
    String command;
    boolean inputMismatchError = false;
    boolean unkownCommandError = false;
    boolean oparationFailedError = false;
    AsciiStack as = new AsciiStack();
    String charset = "";
    HashMap<String, Factory> hm = new HashMap<String, Factory>();
    hm.put("clear", new ClearFactory());
    hm.put("binary", new BinaryFactory());
    hm.put("filter", new FilterFactory());
    hm.put("load", new LoadFactory());
    hm.put("replace", new ReplaceFactory());

    // create
    if (sc.hasNext()) {

      if (sc.next().contentEquals("create") && !inputMismatchError) {

        if (sc.hasNextInt() && !inputMismatchError) {

          width = sc.nextInt();

          if ((width <= 0) && !inputMismatchError) inputMismatchError = true;

        } else inputMismatchError = true;

        if (sc.hasNextInt() && !inputMismatchError) {

          height = sc.nextInt();

          if ((height <= 0) && !inputMismatchError) inputMismatchError = true;

        } else inputMismatchError = true;

        if (sc.hasNext() && !inputMismatchError) {
          charset = sc.next();
        } else inputMismatchError = true;
      } else inputMismatchError = true;
    } else inputMismatchError = true;

    if (inputMismatchError) {
      System.out.println("INPUT MISMATCH");
      return;
    }
    sc.nextLine();

    image = new AsciiImage(width, height, charset);

    // read commands
    try {
      while (sc.hasNext() && !inputMismatchError && !unkownCommandError && !oparationFailedError) {

        command = sc.next();

        if (command.contentEquals("print")) {
          System.out.println(image.toString());
        } else if (command.contentEquals("undo")) {
          if (as.empty()) System.out.println("STACK EMPTY");
          else {
            image = as.pop();
          }

        } else if (hm.get(command) != null) {
          as.push(new AsciiImage(image));
          image = hm.get(command).create(sc).execute(image);
        } else unkownCommandError = true;
      }
    } catch (OperationException oe) {
      oparationFailedError = true;
    } catch (FactoryException fe) {
      inputMismatchError = true;
    }

    if (oparationFailedError) {
      System.out.println("OPERATION FAILED");
    }
    if (unkownCommandError) {
      System.out.println("UNKNOWN COMMAND");
    }
    if (inputMismatchError) {
      System.out.println("INPUT MISMATCH");
    }
  }
Example #7
0
  /**
   * Verarbeitet ein Kommando indem es die entsprechende Funktion aufruft.
   *
   * <p>Gibt das gelesene Bild nach erfolgreicher Bearbeitung über die Standardausgabe aus und
   * kümmert sich um die Fehlerbehandlung.
   *
   * @throws InputMismatchException Wenn ein ungültiges Kommando übergeben wurde.
   * @throws OperationFailedException Wenn fill nicht aufgerufen werden kann.
   */
  public static void main(String[] args) throws InputMismatchException, OperationFailedException {
    try {
      AsciiImage image = new AsciiImage();

      Scanner sc = new Scanner(System.in);
      Scanner commandScanner = new Scanner(sc.nextLine());
      String command = commandScanner.next();

      if (command.equals("read")) {
        int n = commandScanner.nextInt();

        for (int i = 0; i < n; i++) {
          if (image.addLine(sc.nextLine()) == false) {
            throw new InputMismatchException();
          }
        }
      } else {
        throw new InputMismatchException();
      }

      while (sc.hasNextLine()) {
        commandScanner = new Scanner(sc.nextLine());
        command = commandScanner.next();

        if (command.equals("uniqueChars")) {
          System.out.println(image.getUniqueChars());
        } else if (command.equals("flip-v")) {
          image.flipV();
        } else if (command.equals("transpose")) {
          image.transpose();
        } else if (command.equals("fill")) {
          int x = commandScanner.nextInt();
          int y = commandScanner.nextInt();
          char c = commandScanner.next().charAt(0);

          if (x >= 0 && y >= 0 && x < image.getWidth() && y < image.getHeight()) {
            image.fill(x, y, c);
          } else {
            throw new OperationFailedException();
          }
        } else if (command.equals("symmetric-h")) {
          System.out.println(image.isSymmetricH());
        } else {
          throw new InputMismatchException();
        }
      }
      System.out.println(image.toString());
      System.out.println(image.getWidth() + " " + image.getHeight());
    } catch (OperationFailedException e) {
      System.out.println("OPERATION FAILED");
    } catch (RuntimeException e) {
      System.out.println("INPUT MISMATCH");
    }
  }
Example #8
0
 /**
  * Diese Methode entscheidet welche Werte für jene Pixel im Block gewählt werden, welche außerhalb
  * des Bildbereichs liegen.
  *
  * @param img Das AsciiImage
  * @param x Die x-Koordinate außerhalb des Bilds.
  * @param y Die y-Koordinate außerhalb des Bilds.
  * @return Der Helligkeitswert für die angegebene Position.
  */
 public int getOutOfBoundsValue(AsciiImage img, int x, int y) {
   return img.getCharset().indexOf(img.getBackgroundColor());
 }