我怎样才能以更紧凑的方式在java中获取Color号

时间:2015-03-13 07:22:08

标签: java colors redundancy

这是我的代码部分。我正在寻找一种方法来减少冗余,通过三个大部分来获得红色,绿色和蓝色,但看起来像是类似的计算。有没有更好的方法使它工作?对于循环?或任何想法?有人能给我一个主意吗?请!!!!!谢谢!!!

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; 

2 个答案:

答案 0 :(得分:1)

您可以制作一系列naybor偏移量。简化为:

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]];
}

其中MyColor应该是data元素的类。

答案 1 :(得分:0)

您可以尝试将红色,绿色,蓝色的部件重构为以下内容:
注意: RGB是指未指定类型的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();
    }
}

然后在你的代码中调用它:

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));  
        }
    }
}