Search code examples
javaarrayspixel

How would I go about scaling a pixel array?


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

Each tile is 8*8

  • xp = X position
  • yp = Y position
  • Tile = tile
  • location color = color of pixels

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;

        }
    }
}

EDIT:

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

Solution

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

    The program output is:

    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