我将如何缩放像素阵列? [英] How would I go about scaling a pixel array?

查看:53
本文介绍了我将如何缩放像素阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有此功能可以将图块从图像渲染到像素阵列.我正在尝试创建一个新功能,将图像从7 * 7放大到14 * 14

So I have this function to render tiles from an image to a pixel array. I am trying to make a new function to scale the image up from say 7*7 to 14*14

每个图块是8 * 8

Each tile is 8*8

  • xp = X位置
  • yp = Y位置
  • 瓷砖=瓷砖
  • 位置颜色=像素颜色

我相当确定这将是一件容易的事,因为我认为我只需要缩放数组并共享位以填补空白.

I am fairly sure this would be an easy task as I think I would have to just scale the array and share bits to fill in the gaps.

如果您需要更多信息或需要我进一步简化代码,请告诉我.

Please let me know if you need any more info or if you need me to simplify the code more.

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;

        }
    }
}

这有效.感谢GamerJosh.我不知道这是否是最有效的方法,但是它有效!

This works. Thanks to GamerJosh. I don't know if its the most efficient way of doing it but it works!

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

推荐答案

使用字符串数组,下面的代码段显示了可能的(尽管非常基础)数组扩展算法.您应该能够根据需要对其进行修改.

Using an array of Strings, here is a code snippet showing a possible (albeit extremely basic) array up-scaling algorithm. You should be able to modify this to your needs.

// 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("");
}

程序输出为:

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

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

这篇关于我将如何缩放像素阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆