Showing posts with label Naughty Java Code. Show all posts
Showing posts with label Naughty Java Code. Show all posts

Sunday, 3 March 2013

Naughty Java

                       Padding 2D array by replicating the border elements: 

public static int [][] ImagepadWithSameInt(int data[][], int Width, int Height,int nPixAround) {
/* Input Parameters*/
// data ---> The array to be padded
// Width --> Unpadded Width
// Height ---> Unpadded Height
// nPixAround ---> No. of cells around to be padded
/* Output Parameters*/
//paddedArr ---> The padded array with increased size
int[][] paddedArr = new int[Width+(nPixAround*2)] [Height+(nPixAround*2)];
int getDuplicate1 = 0;
int getDuplicate3 = ((Width+(nPixAround*2))-nPixAround);
for (int cols = 0; cols < Width; cols++) {
// For top padding
for (int rows = 0; rows < nPixAround; rows++) {
   paddedArr[cols+nPixAround][rows] = data[cols][rows];
}
// For bottom padding
getDuplicate1 = ((Height+(nPixAround*2))-nPixAround);
for (int rows = Height-nPixAround; rows < Height; rows++) {
   paddedArr[cols+nPixAround][getDuplicate1] = data[cols][rows];
getDuplicate1++;
}
// For left padding 
if(cols < nPixAround){
for (int rows = 0; rows < Height; rows++) {
   paddedArr[cols][rows+nPixAround] = data[cols][rows];
}
}
// For right padding
if((cols >= (Width-nPixAround)) && (cols < Width)){
for (int rows = 0; rows < Height; rows++) {
   paddedArr[getDuplicate3][rows+nPixAround] = data[cols][rows];
}
getDuplicate3++;
}
}
int getDuplicate41 = ((Height+(nPixAround*2))-nPixAround);
int getDuplicate5 = ((Width+(nPixAround*2))-nPixAround);
int getDuplicate6 = getDuplicate41;
int getDuplicate7 = getDuplicate5;
// For top left and bottom left padding
for (int topLeftCols = 0; topLeftCols < nPixAround; topLeftCols++) {
   getDuplicate41 = ((Height+(nPixAround*2))-nPixAround);
for (int topLeftRows = 0; topLeftRows < nPixAround; topLeftRows++) {
   paddedArr[topLeftCols][topLeftRows] = data[topLeftCols][topLeftRows];
}
for (int bottomLeftRows = (Height-nPixAround); bottomLeftRows < Height; bottomLeftRows++) {
  paddedArr[topLeftCols][getDuplicate41] = data[topLeftCols][bottomLeftRows];
getDuplicate41++;
}
}
// For top right and bottom right padding
getDuplicate5 = ((Width+(nPixAround*2))-nPixAround);
for (int topRightCols = Width-nPixAround; topRightCols < Width; topRightCols++) {
//getDuplicate41 = ((Height+(nPixAround*2))-nPixAround);
  getDuplicate6 = ((Height+(nPixAround*2))-nPixAround);
//getDuplicate7 = getDuplicate5;
for (int topRightRows = 0; topRightRows < nPixAround; topRightRows++) {
  paddedArr[getDuplicate5][topRightRows] = data[topRightCols][topRightRows];
}
getDuplicate5++;
for (int bottomRightRows = (Height-nPixAround); bottomRightRows < Height; bottomRightRows++) {
  paddedArr[getDuplicate7][getDuplicate6] = data[topRightCols]   [bottomRightRows];
getDuplicate6++;
}
getDuplicate7++;
}

// For main body padding
for (int cols = 0; cols < Width; cols++) {
  for (int rows = 0; rows < Height; rows++) {
  paddedArr[cols+nPixAround][rows+nPixAround] = data[cols][rows];
  }
}
return paddedArr;
}


                                            Padding 2D array by Zero: 

public static int [][] ImagepadWithZeroInt(int data[][], int Width, int Height,int nPixAround) {
int[][] paddedArr = new int[Width+(nPixAround*2)] [Height+(nPixAround*2)];
int[][] paddedWithZeroArr = InitializeArrayInt(paddedArr,(Width+(nPixAround*2)),(Height+(nPixAround*2)),0);
paddedArr = null;
for (int cols = 0; cols < Width; cols++) {
    for (int rows = 0; rows < Height; rows++) {
          paddedWithZeroArr[cols+nPixAround][rows+nPixAround] = data[cols][rows];
    }
}

return paddedArr;
}