Rohit S Rohit S - 5 months ago 16
Java Question

ArrayIndexOutOfBounds Exception when recreating integer from pixel values

I am trying to separate the red, green and blue values in an image and do some permutation on the arrays. After doing the permutation, am trying to write the image but found an exception while writing the pixel values to an integer.

The exception is:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 272

at encryption.core.receiveFramePath(core.java:203)

at encryption.fny.jButton3ActionPerformed(fny.java:657)

at encryption.fny.access$900(fny.java:14)


The java code is:

public class core {

Random rand = new Random();

public void receiveFramePath(String[] fpath, String key)
{
System.out.println("Number of frames to be processed are " + fpath.length);
System.out.println("Key received is " + key);
int len;

len = key.length();
//String part1, part2;
String part1 = key.substring(0, 2);
String part2 = key.substring(2, len);

System.out.println("Message to be splitted is " + key);
System.out.println("The parts are " + part1 + " and " + part2);

int fC ;// this is frmaeCount
fC = fpath.length;
System.out.println(" No of strings to be processed are " + fC);
BufferedImage[] img = new BufferedImage[fC];
int i;// to run the for loop
for( i = 0; i < fC; i++ )
{
// Code to read the image pixel values

File f = new File(fpath[i]);
try {
img[i] = ImageIO.read(f);
}
catch(Exception e)
{
e.printStackTrace();
}
int[] RGBarray = null;
int c = 0;
int r = 0;
int [][] alphaPixels = null;
int [][] redPixels = null;
int [][] greenPixels = null;
int [][] bluePixels =null;

c = img[i].getWidth();
r = img[i].getHeight();

//System.out.println(" Width of the image is " + c + " The height of the image is " + r);
RGBarray = img[i].getRGB(0,0,c,r,null,0,c);

alphaPixels = new int [r][c];
redPixels = new int [r][c];
greenPixels = new int [r][c];
bluePixels = new int [r][c];
int ii = 0;// to run inside seperating a loop

for(int row=0; row<r; row++)
{
for(int col=0; col<c; col++)
{
// alphaPixels[row][col] = ((RGBarray[ii]>>24)&0xff);
redPixels[row][col] = ((RGBarray[ii]>>16)&0xff);
greenPixels[row][col] = ((RGBarray[ii]>>8)&0xff);
bluePixels[row][col] = (RGBarray[ii]&0xff);
ii++;
}
}
// code to read the image pixel values ends here

// Fisher alg starts here
// logic for permutation on red pixels
for ( int ro = 0; ro < r ; ro++)
{
int[] arr = new int[c];

for (int co = 0; co < c; co++)
{
arr[co] = redPixels[ro][co];
}

int max = arr.length - 1;
int min = 0;
int rnum;
int larr = arr.length - 1;
int[] parr = new int[arr.length];// to store the permuted array
int flag = 0;// 'flag' to flag the recurring number
int plen = parr.length - 1;

for(int z = 0 ; z < arr.length ; z++)
{
rnum = (rand.nextInt((max-min)+1) + min);// roll for the random number
parr[plen] = arr[rnum];
arr[rnum] = arr[larr];

larr--;// to reduce the size of the original array
plen--;// to make the parr to act like a stack
max--;
}
for(int z = 0; z<arr.length;z++ )
{
redPixels[ro][z] = parr[z];
}
}
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& redPixels permuatation ends here
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& redPixels permuatation ends here
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& redPixels permuatation ends here

// logic for permutation on GREEEEN pixels
// logic for permutation on GREEEEN pixels
// logic for permutation on GREEEEN pixels

for ( int ro = 0; ro < r ; ro++)
{
int[] arr = new int[c];

for (int co = 0; co < c; co++)
{
arr[co] = greenPixels[ro][co];
}

int max = arr.length - 1;
int min = 0;
int rnum;
int larr = arr.length - 1;
int[] parr = new int[arr.length];// to store the permuted array
int flag = 0;// 'flag' to flag the recurring number
int plen = parr.length - 1;

for(int z = 0 ; z < arr.length ; z++)
{
rnum = (rand.nextInt((max-min)+1) + min);// roll for the random number
parr[plen] = arr[rnum];
arr[rnum] = arr[larr];

larr--;// to reduce the size of the original array
plen--;// to make the parr to act like a stack
max--;
}
for(int z = 0; z<arr.length;z++ )
{
greenPixels[ro][z] = parr[z];
}
}
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& greenPixels permuatation ends here
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& greenPixels permuatation ends here
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& greenPixels permuatation ends here

for ( int ro = 0; ro < r ; ro++)
{
int[] arr = new int[c];

for (int co = 0; co < c; co++)
{
arr[co] = bluePixels[ro][co];
}

int max = arr.length - 1;
int min = 0;
int rnum;
int larr = arr.length - 1;
int[] parr = new int[arr.length];// to store the permuted array
int flag = 0;// 'flag' to flag the recurring number
int plen = parr.length - 1;

for(int z = 0 ; z < arr.length ; z++)
{
rnum = (rand.nextInt((max-min)+1) + min);// roll for the random number
parr[plen] = arr[rnum];
arr[rnum] = arr[larr];

larr--;// to reduce the size of the original array
plen--;// to make the parr to act like a stack
max--;
}
for(int z = 0; z<arr.length;z++ )
{
bluePixels[ro][z] = parr[z];
}
}
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& bluePixels permuatation ends here
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& bluePixels permuatation ends here
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& bluePixels permuatation ends here

int rgba;
for(int row=0; row<r; row++)
{
for(int col=0; col<r; col++)
{
rgba = (alphaPixels[row][col] & 0xff) << 24 | (redPixels[row][col] & 0xff) << 16 | (greenPixels[row][col] & 0xff) << 8 | (bluePixels[row][col] & 0xff);
img[i].setRGB(col, row, rgba);
}
}
try{
ImageIO.write(img[i], "png", new File("D:/Projects/Problem domains/Ashwini/encrypted/encr_"+i+".png"));
}
catch(Exception e)
{
e.printStackTrace();
}
}// for loop
}// method ends
}// class ends


The exception is at line

rgba = (alphaPixels[row][col] & 0xff) << 24 | (redPixels[row][col] & 0xff) << 16 | (greenPixels[row][col] & 0xff) << 8 | (bluePixels[row][col] & 0xff);


What might have gone wrong??

Answer

It looks like you have a typo:

for(int row=0; row<r; row++) {
    for(int col=0; col<r; col++)
                       ^

should be

for(int row=0; row<r; row++) {
    for(int col=0; col<c; col++)