Java 我将如何缩放像素阵列?

Java 我将如何缩放像素阵列?,java,arrays,pixel,Java,Arrays,Pixel,因此,我有这个函数来渲染从图像到像素阵列的瓷砖。我正在尝试制作一个新函数,将图像从7*7放大到14*14 每块瓷砖是8*8 xp=X位置 yp=Y位置 瓷砖=瓷砖 位置颜色=像素的颜色 我相当确信这将是一项简单的任务,因为我认为我必须缩放阵列并共享位来填补空白 请让我知道,如果你需要任何更多的信息或如果你需要我简化代码更多 public void scaledRender(int xp, int yp, int tile, int colors) { xp -= xOffset;

因此,我有这个函数来渲染从图像到像素阵列的瓷砖。我正在尝试制作一个新函数,将图像从7*7放大到14*14

每块瓷砖是8*8

  • xp=X位置
  • yp=Y位置
  • 瓷砖=瓷砖
  • 位置颜色=像素的颜色
我相当确信这将是一项简单的任务,因为我认为我必须缩放阵列并共享位来填补空白

请让我知道,如果你需要任何更多的信息或如果你需要我简化代码更多

public void scaledRender(int xp, int yp, int tile, int colors) {
    xp -= xOffset;
    yp -= yOffset;

    int xTile = tile % 32;
    int yTile = tile / 32;
    int toffs = xTile * 8 + yTile * 8 * sheet.width;

    int ys, xs;

    for (int y = 0; y < 8; y++) {
        ys = y;
        if (y + yp < 0 || y + yp >= h)
            continue;

        for (int x = 0; x < 8; x++) {
            if (x + xp < 0 || x + xp >= w)
                continue;

            xs = x;
            int col = (colors >> (sheet.pixels[xs + ys * sheet.width + toffs] * 8)) & 255;

            if (col < 255)
                pixels[((x + xp) + (y + yp) * w)] = Screen.colors[col] | 0xff000000;

        }
    }
}
public void scaledRender(int xp、int yp、int tile、int colors){
xp-=xOffset;
yp-=yOffset;
int xTile=tile%32;
int yTile=tile/32;
int-toffs=xTile*8+yTile*8*sheet.width;
int-ys,xs;
对于(int y=0;y<8;y++){
ys=y;
如果(y+yp<0 | | y+yp>=h)
继续;
对于(int x=0;x<8;x++){
如果(x+xp<0 | | x+xp>=w)
继续;
xs=x;
int col=(颜色>>(sheet.pixels[xs+ys*sheet.width+toffs]*8))&255;
如果(列<255)
像素[((x+xp)+(y+yp)*w)]=屏幕颜色[col]| 0xff000000;
}
}
}
编辑:

这很有效。多亏了GamerJosh。我不知道这是否是最有效的方法,但它是有效的

public void scaledRender(int xp, int yp, int tile, int colors) {
xp -= xOffset;
yp -= yOffset;

int xTile = tile % 32;
int yTile = tile / 32;
int toffs = xTile * 8 + yTile * 8 * sheet.width;

int ys, xs;

for (int y = 0; y < 8; y++) {
    ys = y;
    if (y + yp < 0 || y + yp >= h)
        continue;

    for (int x = 0; x < 8; x++) {
        if (x + xp < 0 || x + xp >= w)
            continue;

        xs = x;
        int col = (colors >> (sheet.pixels[xs + ys * sheet.width + toffs] * 8)
            if (col < 255) {
                pixels[((x + xp)*2  ) + ((y + yp)*2  ) * w ] = Screen.colors[col] | 0xff000000;
                pixels[((x + xp)*2+1) + ((y + yp)*2  ) * w ] = Screen.colors[col] | 0xff000000;
                pixels[((x + xp)*2  ) + ((y + yp)*2+1) * w ] = Screen.colors[col] | 0xff000000;
                pixels[((x + xp)*2+1) + ((y + yp)*2+1) * w ] = Screen.colors[col] | 0xff000000;
            }
        }
    }
}
public void scaledRender(int xp、int yp、int tile、int colors){
xp-=xOffset;
yp-=yOffset;
int xTile=tile%32;
int yTile=tile/32;
int-toffs=xTile*8+yTile*8*sheet.width;
int-ys,xs;
对于(int y=0;y<8;y++){
ys=y;
如果(y+yp<0 | | y+yp>=h)
继续;
对于(int x=0;x<8;x++){
如果(x+xp<0 | | x+xp>=w)
继续;
xs=x;
int col=(颜色>>(sheet.pixels[xs+ys*sheet.width+toffs]*8)
如果(列<255){
像素[((x+xp)*2)+((y+yp)*2)*w]=屏幕颜色[col]| 0xff000000;
像素[((x+xp)*2+1)+((y+yp)*2)*w]=屏幕颜色[col]| 0xff000000;
像素[((x+xp)*2)+((y+yp)*2+1)*w]=屏幕颜色[col]| 0xff000000;
像素[(x+xp)*2+1)+(y+yp)*2+1)*w]=屏幕颜色[col]| 0xff000000;
}
}
}
}

使用字符串数组,下面是一段代码片段,显示了一种可能的(尽管非常基本)数组放大算法。您应该能够根据需要修改此算法

// Initialize array
String [][] array = {{"00", "01", "02", "03", "04", "05", "06", "07"},
                     {"10", "11", "12", "13", "14", "15", "16", "17"},
                     {"20", "21", "22", "23", "24", "25", "26", "27"},
                     {"30", "31", "32", "33", "34", "35", "36", "37"},
                     {"40", "41", "42", "43", "44", "45", "46", "47"},
                     {"50", "51", "52", "53", "54", "55", "56", "57"},
                     {"60", "61", "62", "63", "64", "65", "66", "67"},
                     {"70", "71", "72", "73", "74", "75", "76", "77"},
                     };

// Simply output the original array so we can visually compare later
for (int i = 0; i < 8; ++i) {
    for (int j = 0; j < 8; ++j) {
        System.out.print(" " + array[i][j]);
    }
    System.out.println("");
}

// Create a new array that is twice the size as the original array
String[][] scaledArray = new String[16][16];

// Scale the original array into the new array
for (int i = 0; i < 8; ++i) {
    for (int j = 0; j < 8; ++j) {
        scaledArray[(i*2)][(j*2)] = array[i][j];
        scaledArray[(i*2) + 1][(j*2)] = array[i][j];
        scaledArray[(i*2)][(j*2) + 1] = array[i][j];
        scaledArray[(i*2) + 1][(j*2) + 1] = array[i][j];
    }
}

// Output the scaled array to see the result
System.out.println("\nSCALED: ");
for (int i = 0; i < 16; ++i) {
    for (int j = 0; j < 16; ++j) {
        System.out.print(" " + scaledArray[i][j]);
    }
    System.out.println("");
}

使用一个字符串数组,下面是一个代码片段,显示了一个可能的(尽管是非常基本的)数组放大算法。您应该能够根据需要修改它

// Initialize array
String [][] array = {{"00", "01", "02", "03", "04", "05", "06", "07"},
                     {"10", "11", "12", "13", "14", "15", "16", "17"},
                     {"20", "21", "22", "23", "24", "25", "26", "27"},
                     {"30", "31", "32", "33", "34", "35", "36", "37"},
                     {"40", "41", "42", "43", "44", "45", "46", "47"},
                     {"50", "51", "52", "53", "54", "55", "56", "57"},
                     {"60", "61", "62", "63", "64", "65", "66", "67"},
                     {"70", "71", "72", "73", "74", "75", "76", "77"},
                     };

// Simply output the original array so we can visually compare later
for (int i = 0; i < 8; ++i) {
    for (int j = 0; j < 8; ++j) {
        System.out.print(" " + array[i][j]);
    }
    System.out.println("");
}

// Create a new array that is twice the size as the original array
String[][] scaledArray = new String[16][16];

// Scale the original array into the new array
for (int i = 0; i < 8; ++i) {
    for (int j = 0; j < 8; ++j) {
        scaledArray[(i*2)][(j*2)] = array[i][j];
        scaledArray[(i*2) + 1][(j*2)] = array[i][j];
        scaledArray[(i*2)][(j*2) + 1] = array[i][j];
        scaledArray[(i*2) + 1][(j*2) + 1] = array[i][j];
    }
}

// Output the scaled array to see the result
System.out.println("\nSCALED: ");
for (int i = 0; i < 16; ++i) {
    for (int j = 0; j < 16; ++j) {
        System.out.print(" " + scaledArray[i][j]);
    }
    System.out.println("");
}

按您希望整个像素按比例缩放的比例缩放每个“像素”。是否有错误?具体是什么你需要帮助解决这个问题吗?@GamerJosh代码中没有错误。它当前渲染每个图块的大小是8*8,但我想把这个8*8图块放大到16*16。然后你在寻找一个算法?你尝试了什么?一个非常简单的算法只需将每个像素放入新的数组中两次ce(假设比例为x2,如您所述)每列和每行--您尝试过类似的操作吗?@GamerJosh是的,这就是我正在尝试的。到目前为止,我真正尝试过的唯一一件事就是将像素设置4次,并尝试移动x y值。但渲染中仍然存在间隙。它看起来像是分解的,只渲染其他像素。缩放每个“像素”通过你想要的整个比例来衡量。有错误吗?具体是什么你需要帮助解决这个问题吗?@GamerJosh代码中没有错误。它当前渲染每个图块的大小是8*8,但我想把这个8*8图块放大到16*16。然后你在寻找一个算法?你尝试了什么?一个非常简单的算法只需将每个像素放入新的数组中两次ce(假设比例为x2,如您所述)每列和每行--您尝试过类似的操作吗?@GamerJosh是的,这就是我正在尝试的。到目前为止,我唯一真正尝试过的就是将像素设置4次,并尝试移动x y值。但渲染中仍有间隙。它看起来像是爆炸式的,只渲染了其他像素。我似乎记得回到我的计算机中er图形大多数应用程序使用点积(线性代数)矩阵可以进行旋转、倾斜、转置和缩放,因为所有这些过程都可以用一个矩阵来表示,而且数学总是可以重复的,并且可以很容易地扩展到处理3个或更多维度。不过,就像20年前一样,所以我的记忆力可能很差。@CodeChimp根据您的评论,经过一点搜索,我发现其中(使用适当的辅助阵列大小时,填充1)似乎产生了与我上面的代码片段相同的结果。我没有找到与点积有关的任何东西,但它肯定不是一个彻底的搜索,所以这并不意味着我没有找到它。是的,我已经有一段时间没有在学校里思考过这些东西了,但这看起来是正确的。它可能没有用处,但我想我得提一下。如果我们必须以不成比例的比例因子(例如8x8到10x12)进行缩放,该怎么办?我似乎记得在我的计算机图形学时代,大多数应用程序都使用点积(线性代数)矩阵可以进行旋转、倾斜、转置和缩放,因为所有这些过程都可以用一个矩阵来表示,而且数学总是可以重复的,并且可以很容易地扩展到处理3个或更多维度。不过,就像20年前一样,所以我的记忆力可能很差。@CodeChimp根据您的评论,经过一点搜索,我发现这(当使用适当的辅助数组大小时,填充1)似乎产生了与上面代码片段相同的结果