0

Here is my part of code. I am looking for a way to reduce the redundancy to get red, green, and blue by three big parts but looks like similar calculation. Is any better way to make it work? For loop? or any idea? Is anyone can give me an idea? please!!!!! Thank you!!!

for(int row = 1; row < pi.getHeight() - 1; row++) {
    for(int col = 1; col < pi.getWidth() - 1; col++) {
            int red_P_C = 4 * data[row][col].red;
            int red_P_1 = 1 * data[row-1][col-1].red;
            int red_P_2 = 2 * data[row-1][col].red;
            int red_P_3 = 1 * data[row-1][col+1].red;
            int red_P_4 = 2 * data[row][col-1].red;
            int red_P_5 = 2 * data[row][col+1].red;
            int red_P_6 = 1 * data[row+1][col-1].red;
            int red_P_7 = 2 * data[row+1][col].red;
            int red_P_8 = 1 * data[row+1][col+1].red;
            int redTotal = red_P_C + red_P_1 + red_P_2 + red_P_3 + red_P_4 + red_P_5 + red_P_6 + red_P_7 + red_P_8;
            int redActual = redTotal/16;

            int g_P_C = 4 * data[row][col].green;
            int g_P_1 = data[row-1][col-1].green;
            int g_P_2 = 2 * data[row-1][col].green;
            int g_P_3 = data[row-1][col+1].green;
            int g_P_4 = 2 * data[row][col-1].green;
            int g_P_5 = 2 * data[row][col+1].green;
            int g_P_6 = data[row+1][col-1].green;
            int g_P_7 = 2 * data[row+1][col].green;
            int g_P_8 = data[row+1][col+1].green;

            int gTotal = g_P_C + g_P_1 + g_P_2 + g_P_3 + g_P_4 + g_P_5 + g_P_6 + g_P_7 + g_P_8;
            int gActual = gTotal/16;

            int blu_P_C = data[row][col].blue * 4;
            int blu_P_1 = data[row-1][col-1].blue;
            int blu_P_2 = 2 * data[row-1][col].blue;
            int blu_P_3 = data[row-1][col+1].blue;
            int blu_P_4 = 2 * data[row][col-1].blue;
            int blu_P_5 = 2 * data[row][col+1].blue;
            int blu_P_6 = data[row+1][col-1].blue;
            int blu_P_7 = 2 * data[row+1][col].blue;
            int blu_P_8 = data[row+1][col+1].blue;

            int bluTotal = blu_P_C + blu_P_1 + blu_P_2 + blu_P_3 + blu_P_4 + blu_P_5 + blu_P_6 + blu_P_7 + blu_P_8;
            int bluActual = bluTotal/16;

            newData[row][col].red = redActual;
            newData[row][col].green = gActual;
            newData[row][col].blue = bluActual; 
Bun
  • 3
  • 2
  • Actually it is a 2-D array. Each array contains a color which has three number inside the color. – Bun Mar 13 '15 at 07:23
  • What are you using the variables for? Are those just arbitrary values you're multiplying by? – MeetTitan Mar 13 '15 at 08:34
  • What are you trying to do here? Which type do `data`, `newData` and `pi` have? Is the code in a method body? Your question is unclear to me. – Binkan Salaryman Mar 13 '15 at 08:40
  • I your comment you probably meant "each array *element* contains a color with red, green, blue color components" – Binkan Salaryman Mar 13 '15 at 08:54
  • @ MeetTitan Like color(int,int,int) stored in every 2d Array in my code. I have to get out variables to do a calculation like above and put it back to every int in color. – Bun Mar 13 '15 at 20:03
  • @Binkan Salaryman data and newData is a 2d-array, each index inside the array contains a color(which is Color(int, int, int), so it means has three different integers inside each singe index of 2d array), I have to get it out like to do a calculation like what my code doing. – Bun Mar 13 '15 at 20:06
  • @ Binkan Salaryman I'm reading your code. thanks for the coding. – Bun Mar 13 '15 at 20:07

2 Answers2

1

You can make an array of naybor offsets. Simplified:

int[][] nayborOffsets = new int[][] {
    { -1, -1 }, { 0, -1 }, { 1, -1 },
    { -1, 0 }, { 1, 0},
    { -1, 1 }, { 0, 1 }, { 1, 1 }
};

MyColor[] nayborRGBs = new MyColor[nayborOffsets.length];
for (int i = 0; i < nayborOffsets.length; ++i) {
    int[] offsets = nayborOffsets[i];
    nayborRGBs[i] = data[row + offsets[0]][col + offsets[1]];
}

where MyColor should be the class of a data element.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

You could try to refactor the parts for red, green, blue to something like this:
Note: RGB refers to your unspecified type of data

public int getActualSubPixel(/*this*/ RGB[][] data, int subPixelType, int row, int col) {
    int P_C = 4 * getSubPixel(data[row][col], subPixelType);
    int P_1 = getSubPixel(data[row - 1][col - 1], subPixelType);
    int P_2 = 2 * getSubPixel(data[row - 1][col], subPixelType);
    int P_3 = getSubPixel(data[row - 1][col + 1], subPixelType);
    int P_4 = 2 * getSubPixel(data[row][col - 1], subPixelType);
    int P_5 = 2 * getSubPixel(data[row][col + 1], subPixelType);
    int P_6 = getSubPixel(data[row + 1][col - 1], subPixelType);
    int P_7 = 2 * getSubPixel(data[row + 1][col], subPixelType);
    int P_8 = getSubPixel(data[row + 1][col + 1], subPixelType);
    int Total = P_C + P_1 + P_2 + P_3 + P_4 + P_5 + P_6 + P_7 + P_8;
    int Actual = Total / 16;
    return Actual;
}

//possible member method
public int getSubPixel(/*this*/ RGB rgb, int subPixelType) {
    switch (subPixelType) {
        case 0:
            return rgb.red;
        case 1:
            return rgb.green;
        case 2:
            return rgb.blue;
        default:
            throw new IllegalArgumentException();
    }
}

//possible member method
public void setSubPixel(/*this*/ RGB rgb, int subPixelType, int value) {
    switch (subPixelType) {
        case 0:
            rgb.red = value;
            return;
        case 1:
            rgb.green = value;
            return;
        case 2:
            rgb.blue = value;
            return;
        default:
            throw new IllegalArgumentException();
    }
}

Then call it in your code:

for (int row = 1; row < pi.getHeight() - 1; row++) {
    for (int col = 1; col < pi.getWidth() - 1; col++) {
        for (int i = 0; i < 3; i++) {
             setSubPixel(newData[row][col], i, getActualSubPixel(data, i, row, col));  
        }
    }
}
Binkan Salaryman
  • 3,008
  • 1
  • 17
  • 29