Skip to content

Instantly share code, notes, and snippets.

Created December 8, 2014 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/f2f580cd5c8ab115eea9 to your computer and use it in GitHub Desktop.
Save anonymous/f2f580cd5c8ab115eea9 to your computer and use it in GitHub Desktop.
/**
* This class represents a colorful image,defined by two-dimensional array of RGBColor type,
* that represents colors and their coordinates on the image.
*/
import java.lang.Math;
public class RGBImage {
private RGBColor[][] _pixels;
final int Min=0;
/**
* Construct a new RGBImage with the given number of number of pixels.
* @param rows The image's amount of horizontal pixels, i.e the image's height.
* @param cols The image's amount of vertical pixels, i.e the image's width.
*/
public RGBImage(int rows, int cols){
_pixels = new RGBColor[rows][cols];
}
/**
* Construct a new RGBImage whose pixels' colors in each coordinates
* are derived from a given RGBColor 2-dimensional array.
* @param pixels Two-dimensional array of colors and its coordinates.
*/
public RGBImage(RGBColor[][] pixels){
_pixels = new RGBColor[pixels.length][pixels[Min].length];
for (int i=0 ; i < pixels.length ; i++){
for (int j=0 ; j < pixels[0].length; j++)
_pixels[i][j] = new RGBColor(pixels[i][j]);
}
}
/**
* Construct a new RGBImage that is a copy of the given image.
* @param other The given image to copy.
*/
public RGBImage(RGBImage other){
_pixels = new RGBColor[other._pixels.length][other._pixels[Min].length];
for (int i=0 ; i < other._pixels.length ; i++){
for (int j=0 ; j < other._pixels[Min].length; j++)
_pixels[i][j] = new RGBColor(other._pixels[i][j]) ;
}
}
/**
* Rotates the image clockwise by 90 degrees.
*/
public void rotateClockwise(){
RGBImage rotatedCW = new RGBImage(_pixels[Min].length, _pixels.length);
for (int j=0; j<_pixels[Min].length ; j++){
for (int i=0; i<_pixels.length ; i++)
rotatedCW._pixels[j][i] = new RGBColor(_pixels[_pixels.length-1-i][j]);
}
}
/**
* Rotates the image counter clockwise by 90 degrees, by rotating it clockwise three times.
*/
public void rotateCounterClockwise(){
for (int i=0 ; i<3 ; i++)
rotateClockwise();
}
/**
* Shifts the image sideways depending on the offset value that is given.
* If the absolute value of offset is smaller than the length of columns,it shifts rightwards (for positive value of offset)
* according to offset's value. Otherwise - leftwards.
* If the absolute value of offset is equal to the length of columns , it paints the image black.
* In any other scenario, no action is performed.
*/
public void shiftCol (int offset){
RGBImage shiftedCol = new RGBImage(_pixels.length,_pixels[Min].length);
if (Math.abs(offset)<_pixels[0].length){
for (int i=0; i<_pixels.length ; i++){
for (int j=0; j < (_pixels[Min].length - Math.abs(offset)) ; j++){
shiftedCol._pixels[i][j+offset] = new RGBColor(_pixels[i][j]);
shiftedCol._pixels[i][j] = new RGBColor();
}
}
}
if (Math.abs(offset)==_pixels.length)
for (int i=0; i<_pixels.length ; i++){
for (int j=0; j<_pixels[Min].length ; j++)
shiftedCol._pixels[i][j] = new RGBColor(); // paints the imge black.
}
}
/**
* Shifts the image sideways depending on the offset value that is given.
* If the absolute value of offset is smaller than the length of columns,it shifts rightwards (for positive value of offset)
* according to offset's value. Otherwise - leftwards.
* If the absolute value of offset is equal to the length of columns , it paints the image black.
* In any other scenario, no action is performed.
*/
public void shiftRow(int offset){
RGBImage shiftedRow = new RGBImage(_pixels.length,_pixels[Min].length);
if (Math.abs(offset)<_pixels.length){
for (int i=0; i < (Math.abs(offset) - _pixels.length) ; i++){
for (int j=0; j<_pixels[Min].length ; j++){
shiftedRow._pixels[i][j+offset] = new RGBColor(_pixels[i][j]);
shiftedRow._pixels[i][j] = new RGBColor(); //paints black the "empty" rows after the shifting.
}
}
}
if (Math.abs(offset)==_pixels.length)
for (int i=0; i<_pixels.length ; i++){
for (int j=0; j<_pixels[Min].length ; j++)
shiftedRow._pixels[i][j] = new RGBColor(); // paints the image black.
}
}
/**
* Returns the grayscale value of this RGBImage.(The calculation method is described in RGBColor's API)
* @return The grayscale value of this RGBImage.
*/
public double[][] toGrayscaleArray(){
double[][] gray = null;;
for (int i=0; i<_pixels.length ; i++){
for (int j=0; j<_pixels[Min].length ; j++)
gray[i][j]=_pixels[i][j].convertToGrayscale();
}
return gray;
}
/**
* Returns a string representation of this image.
* @return a string representation of this image.
*/
public String toString(){
String image="";
for (int i=0; i<_pixels.length ; i++){
for (int j=0; j<_pixels[Min].length ; j++){
image+=_pixels[i][j]+" ";
}
image+="\n";
}
return image;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment