/**
   * Hides the given message inside the specified image.
   *
   * <p>The image will be given to you as a GImage of some size, and the message will be specified
   * as a boolean array of pixels, where each white pixel is denoted false and each black pixel is
   * denoted true.
   *
   * <p>The message should be hidden in the image by adjusting the red channel of all the pixels in
   * the original image. For each pixel in the original image, you should make the red channel an
   * even number if the message color is white at that position, and odd otherwise.
   *
   * <p>You can assume that the dimensions of the message and the image are the same.
   *
   * <p>
   *
   * @param message The message to hide.
   * @param source The source image.
   * @return A GImage whose pixels have the message hidden within it.
   */
  public static GImage hideMessage(boolean[][] message, GImage source) {
    int[][] pixels = source.getPixelArray();

    for (int row = 0; row < pixels.length; row++) {
      for (int col = 0; col < pixels[row].length; col++) {
        /* Extract the green and blue components and write them back, changing the
         * red component.
         */
        int red = GImage.getRed(pixels[row][col]);
        int green = GImage.getGreen(pixels[row][col]);
        int blue = GImage.getBlue(pixels[row][col]);
        if (message[row][col]) {
          if (red % 2 == 0) { // If the secret pixel is black, make the red channel odd
            red++;
          }
        } else {
          if (red % 2 != 0) { // If the secret pixel is white, make the red channel even
            red++;
          }
          if (red > 255) {
            red -= 2;
          }
        }

        pixels[row][col] = GImage.createRGBPixel(red, green, blue);
      }
    }
    return new GImage(pixels);
  }
  private static int redToEven(int pixel) { // в НЕЧЕТНЫЙ
    int oldRed = GImage.getRed(pixel);
    int newRed;

    if (oldRed % 2 == 1) { // нечетный
      newRed = oldRed;
    } else { // если четный
      newRed = oldRed + 1;
    }

    return GImage.createRGBPixel(
        newRed, GImage.getGreen(pixel), GImage.getBlue(pixel), GImage.getAlpha(pixel));
  }
  public static GImage toGrayscale(GImage image) {
    int[][] pixels = image.getPixelArray();

    for (int row = 0; row < pixels.length; ++row) {
      for (int col = 0; col < pixels[row].length; ++col) {
        int intensity =
            (int)
                (0.3D * (double) GImage.getRed(pixels[row][col])
                    + 0.59D * (double) GImage.getGreen(pixels[row][col])
                    + 0.11D * (double) GImage.getBlue(pixels[row][col])
                    + 0.5D);
        pixels[row][col] = GImage.createRGBPixel(intensity, intensity, intensity);
      }
    }

    return new GImage(pixels);
  }