Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import mmn12.RGBColor;
- /**
- * The RGBImage class represents an image made out of pixels.<br>
- * The RGB image contains a two-dimensional array containing pixels of type RGBImage, defined in mmn12.<br><br>
- * The class has methods to get the properties of the image, such as its width, height,
- * a certain pixel or the entire pixels array.<br>
- * The class also has methods to alter, transform or rotate the image such as set a certain pixel,
- * invert the colors of the image, rotate the image, flip the image or shift rows or columns.<br>
- * The class also has general purpose methods such as equals or toString.
- *
- * @author Barack Levy, 203123898
- *
- */
- public class RGBImage {
- private RGBColor[][] _imagePixels;
- /**
- * The constructor creates a black image instance in a given size.<br>
- *
- * @param rows - the height of the image to be instantiated. Assumed to be a positive value.
- * @param cols - the width of the image to be instantiated. Assumed to be a positive value.
- */
- public RGBImage(int rows, int cols) {
- _imagePixels = new RGBColor[rows][cols];
- for (int row = 0; row < rows; row++) {
- for (int col = 0; col < cols; col++) {
- _imagePixels[row][col] = new RGBColor();
- }
- }
- }
- /**
- * Copy constructor - creates a new image out of an existing one.<br>
- * The method assumes the other object is not null, with valid values.
- *
- * @param other - the image to copy.
- */
- public RGBImage(RGBImage other) {
- this(other._imagePixels);
- }
- /**
- * The constructor creates an image out of a given colors array.<br>
- * The method assumes the colors array isn't null, with valid values.
- *
- * @param pixels - the colors array
- */
- public RGBImage(RGBColor[][] pixels) {
- _imagePixels = new RGBColor[pixels.length][pixels[0].length];
- for (int i = 0; i < pixels.length; i++) {
- for (int j = 0; j < pixels[i].length; j++) {
- _imagePixels[i][j] = new RGBColor(pixels[i][j]);
- }
- }
- }
- /**
- * Gets the height of the image.
- *
- * @return the height of the image
- */
- public int getHeight() {
- return _imagePixels.length;
- }
- /**
- * Gets the width of the image.
- *
- * @return the width of the image
- */
- public int getWidth() {
- // Note that image is guaranteed to be a rectangle,
- // therefore the width of the image is constant among different rows.
- return _imagePixels[0].length;
- }
- /**
- * A private method that checks if a given position is inside an image or not.
- *
- * @param row - the row of the position
- * @param col - the column of the position
- *
- * @return true if and only if the the position is inside the image
- */
- private boolean isValidPixelPosition(int row, int col) {
- return row >= 0 && row < getHeight() && col >= 0 && col < getWidth();
- }
- /**
- * Gets a certain pixel.
- *
- * @param row - the row of the pixel to get.
- * @param col - the column of the pixel to get.
- *
- * @return the pixel if the position of the pixel is a valid one. A black pixel otherwise.
- */
- public RGBColor getPixel(int row, int col) {
- // If the position of the pixel isn't valid...
- if (!isValidPixelPosition(row, col)) {
- // Return a black color
- return new RGBColor();
- }
- // Otherwise, return a copy of the pixel.
- return new RGBColor(_imagePixels[row][col]);
- }
- /**
- * Sets a certain pixel to a given pixel.
- *
- * @param row - the row of the pixel to set.
- * @param col - the column of the pixel to set.
- * @param pixel - the new pixel color
- */
- public void setPixel(int row, int col, RGBColor pixel) {
- // Only set the pixel if the position is a valid one and if the pixel is not null
- if (pixel != null && isValidPixelPosition(row, col)) {
- _imagePixels[row][col] = new RGBColor(pixel);
- }
- }
- /**
- * Compares this image to the specified image. The result is true if and only if
- * the pixels array of this image is equivalent to the given image's pixels array.
- *
- * @param other - the image to compare this image against
- * @return true if the given image represents an image equivalent to this image, false otherwise
- */
- public boolean equals(RGBImage other) {
- // If the other object is null, we'll return false
- if (other == null) {
- return false;
- }
- // If the size of each image is different, then clearly they're different.
- if (getWidth() != other.getWidth() || getHeight() != other.getHeight()) {
- return false;
- }
- // Go through each pixel
- for (int row = 0; row < getHeight(); row++) {
- for (int col = 0; col < getWidth(); col++) {
- // Check if one of the pixels in this image is different than the pixel in the other image
- if (!getPixel(row, col).equals(other.getPixel(row, col))) {
- return false;
- }
- }
- }
- return true;
- }
- /**
- * The method flips the image horizontally.
- */
- public void flipHorizontal() {
- // Go through half of the columns and swap them with columns from the end, respectively.
- for (int row = 0; row < getHeight(); row++) {
- for (int col = 0; col <= getWidth() / 2; col++) {
- RGBColor temp = _imagePixels[row][col];
- _imagePixels[row][col] = _imagePixels[row][getWidth() - 1 - col];
- _imagePixels[row][getWidth() - 1 - col] = temp;
- }
- }
- }
- /**
- * The method flips the image vertically.
- */
- public void flipVertical() {
- // Go through half of the rows and swap them with rows from the end, respectively.
- for (int row = 0; row <= getHeight() / 2; row++) {
- for (int col = 0; col < getWidth(); col++) {
- RGBColor temp = _imagePixels[row][col];
- _imagePixels[row][col] = _imagePixels[getHeight() - 1 - row][col];
- _imagePixels[getHeight() - 1 - row][col] = temp;
- }
- }
- }
- /**
- * The method inverts the colors of each of the pixels in image.
- */
- public void invertColors() {
- for (int row = 0; row < getHeight(); row++) {
- for (int col = 0; col < getWidth(); col++) {
- _imagePixels[row][col].invert();
- }
- }
- }
- /**
- * The method rotates the image clockwise.
- */
- public void rotateClockwise() {
- // The height of the new image is set to the width of the former one.
- // The width of the new image is set to the height of the former one.
- RGBColor[][] newImage = new RGBColor[getWidth()][getHeight()];
- for (int row = getHeight() - 1; row >= 0; row--) {
- for (int col = 0; col < getWidth(); col++) {
- // Some fancy math to rotate an image
- newImage[col][getHeight() - 1 - row] = _imagePixels[row][col];
- }
- }
- // Set the pixels array of this image to the new one
- _imagePixels = newImage;
- }
- /**
- * The method rotates the image counter-clockwise.
- */
- public void rotateCounterClockwise() {
- // The height of the new image is set to the width of the former one.
- // The width of the new image is set to the height of the former one.
- RGBColor[][] newImage = new RGBColor[getWidth()][getHeight()];
- for (int col = getWidth() - 1; col >= 0; col--) {
- for (int row = 0; row < getHeight(); row++) {
- // Some fancy math to rotate an image
- newImage[getWidth() - 1 - col][row] = _imagePixels[row][col];
- }
- }
- // Set the pixels array of this image to the new one
- _imagePixels = newImage;
- }
- /**
- * A private method that swaps one pixel with another
- *
- * @param row1 - the row of the first pixel
- * @param col1 - the column of the first pixel
- * @param row2 - the row of the second pixel
- * @param col2 - the column of the second pixel
- */
- private void swapPixel(int row1, int col1, int row2, int col2) {
- RGBColor temp = _imagePixels[row1][col1];
- _imagePixels[row1][col1] = _imagePixels[row2][col2];
- _imagePixels[row2][col2] = temp;
- }
- /**
- * A private method that swaps one column with another
- *
- * @param col1 - the index of the first column
- * @param col2 - the index of the second column
- */
- private void swapColumns(int col1, int col2) {
- for (int row = 0; row < getHeight(); row++) {
- swapPixel(row, col1, row, col2);
- }
- }
- /**
- * A private method that swaps one row with another
- *
- * @param row1 - the index of the first row
- * @param row2 - the index of the second row
- */
- private void swapRows(int row1, int row2) {
- for (int col = 0; col < getWidth(); col++) {
- swapPixel(row1, col, row2, col);
- }
- }
- /**
- * The method shifts the columns of this image some pixels in a given direction.
- *
- * @param offset - the number of pixels to move the image.<br>
- * If the offset is positive, the image will be shifted to the right.<br>
- * If the offset is negative, the image will be shifted to the left.<br>
- * If the offset is zero, the method will do nothing.
- */
- public void shiftCol(int offset) {
- // If the offset is greater than the width, the method will do nothing
- // Note that the offset can also be negative, therefore we'll take the absolute value of the offset
- if (offset == 0 || Math.abs(offset) > getWidth()) {
- return;
- }
- // If the offset is positive, we'll move everything to the right
- if (offset > 0) {
- // Swap each column with the column to its left by [offset] 'steps'
- for (int col = getWidth() - 1; col >= offset; col--) {
- swapColumns(col, col - offset);
- }
- // Change the [offset] left-most columns to black
- for (int col = offset - 1; col >= 0; col--) {
- for (int row = 0; row < getHeight(); row++) {
- _imagePixels[row][col] = new RGBColor();
- }
- }
- }
- else {
- // Swap each column with the column to its right by [-offset] 'steps' (offset < 0)
- for (int i = 0; i < getWidth() + offset; i++) {
- swapColumns(i, i - offset);
- }
- // Change the [offset] right-most columns to black
- for (int col = getWidth() + offset; col < getWidth(); col++) {
- for (int row = 0; row < getHeight(); row++) {
- _imagePixels[row][col] = new RGBColor();
- }
- }
- }
- }
- /**
- * The method shifts the rows of this image some pixels in a given direction.
- *
- * @param offset - the number of pixels to move the image.<br>
- * If the offset is positive, the image will be shifted to the top.<br>
- * If the offset is negative, the image will be shifted to the bottom.<br>
- * If the offset is zero, the method will do nothing.
- */
- public void shiftRow(int offset) {
- // If the offset is greater than the height, the method will do nothing
- // Note that the offset can also be negative, therefore we'll take the absolute value of the offset
- if (Math.abs(offset) > getHeight()) {
- return;
- }
- // If the offset is positive, we'll move everything down
- if (offset > 0) {
- // Swap each row with the row above it by [offset] 'steps'
- for (int row = getHeight() - 1; row >= offset; row--) {
- swapRows(row, row - offset);
- }
- // Change the [offset] bottom columns to black
- for (int row = offset - 1; row >= 0; row--) {
- for (int col = 0; col < getWidth(); col++) {
- _imagePixels[row][col] = new RGBColor();
- }
- }
- }
- else {
- // Swap each row with the row below it by [offset] 'steps'
- for (int row = 0; row < getHeight() + offset; row++) {
- swapRows(row, row - offset);
- }
- // Change the [offset] top columns to black
- for (int row = getHeight() + offset; row < getHeight(); row++) {
- for (int col = 0; col < getWidth(); col++) {
- _imagePixels[row][col] = new RGBColor();
- }
- }
- }
- }
- /**
- * Converts this image to a grayscale array representing the grayscale values of each pixel in this image
- *
- * @return a two-dimensional array representing the grayscale values of each pixels in this image
- */
- public double[][] toGrayscaleArray() {
- double[][] grayscaleArray = new double[getHeight()][getWidth()];
- // Go through all of the pixels and calculate their grayscale values
- for (int row = 0; row < grayscaleArray.length; row++) {
- for (int col = 0; col < grayscaleArray[row].length; col++) {
- grayscaleArray[row][col] = _imagePixels[row][col].convertToGrayscale();
- }
- }
- return grayscaleArray;
- }
- /**
- * Returns a string representation of the object in the following format:<br>
- *<pre> p11 p12 ... p1h<br>
- * p21 p22 ... p2h<br>
- * . . . .<br>
- * . . . .<br>
- * . . . .<br>
- * pw1 pw2 ... pwh<br></pre>
- *
- * where h and w are the height and width of this image, respectively,
- * and each p[i,j] is the pixel in the i'th row and j'th column.<br>
- * Each pixel is displayed in the following format: (red,green,blue)
- *
- * @return the string representation of the object.
- */
- @Override
- public String toString() {
- StringBuilder builder = new StringBuilder();
- // Go through each pixel and add its string representation to the builder
- for (int row = 0; row < getHeight(); row++) {
- for (int col = 0; col < getWidth(); col++) {
- // If we're not in the last pixels of the line, add a space
- if (col != getWidth() - 1) {
- builder.append(_imagePixels[row][col] + " ");
- }
- else {
- builder.append(_imagePixels[row][col]);
- }
- }
- // Add a new line after each row
- builder.append('\n');
- }
- return builder.toString();
- }
- /**
- * Clones the pixels array
- *
- * @return the cloned pixels array
- */
- public RGBColor[][] toRGBColorArray() {
- RGBColor[][] copy = new RGBColor[getHeight()][getWidth()];
- // Go through each of the pixels, and create a copy of each one using the copy constructor
- for (int row = 0; row < getHeight(); row++) {
- for (int col = 0; col < getWidth(); col++) {
- copy[row][col] = new RGBColor(_imagePixels[row][col]);
- }
- }
- return copy;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement