Search code examples
c#mathmatrixbitmap

How to increase the size of a picture without using bitmap?


I need to recreate the class Bitmap in C# for a project. One of my goals is to increase the size of an image. To do this, I had to transform the image into a byte matrix and now I have to increase the size of the matrix and duplicates its values. Since the matrix is a representation of a picture, each group of 3 values represents a pixel (BGR, B blue G green and R red). Hence, the matrix's columns will always be a multiple of 3. If I went to increase the size of this matrix by an integer, I will have to duplicate those groups of 3 values in the correct order to actually increase the size the image.

So this is my code, as you can see I modified it by changing the type of the matrix and giving it a smaller size, basic values etc.. so I can check where is the problem in my code. But I can't figure out how to correctly write the for loops and their content.

int coeff = 2;
            int index = 1;
            int line = 3;
            int column = 6;

            int[,] m = new int[line, column];


            for (int i = 0; i < m.GetLength(0); i++)
            {
                for (int j = 0; j < m.GetLength(1); j++)
                {
                    m[i, j] = index;
                    index++;
                    Console.Write(m[i, j] + " ");
                }
                Console.WriteLine();
            }

            int[,] matrix = new int[m.GetLength(0) * coeff, m.GetLength(1) * coeff];

            for (int i = 0; i < m.GetLength(0); i++)
            {
                for (int j = 0; j < m.GetLength(1); j++)
                {
                    for (int k = i * coeff; k < i * coeff + coeff; k++) \\the issue is here
                    {
                        for (int l = j * coeff; l < j * coeff + coeff; l++) \\and here
                        {
                            matrix[k, l] = m[i, j];
                        }

                    }
                }
            }

If I run this code with a byte matrix and an image, yes it will increase the image but it won't put the correct colors (I actually don't even understand how it actually gives me the correct image with different color, for me it should have given me a different image)

For example if i have the matrix with a coefficient of 2: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

I am expecting : 1 2 3 1 2 3 4 5 6 4 5 6 1 2 3 1 2 3 4 5 6 4 5 6 7 8 9 7 8 9 10 11 12 10 11 12 7 8 9 7 8 9 10 11 12 10 11 12 13 14 15 13 14 15 16 17 18 16 17 18 13 14 15 13 14 15 16 17 18 16 17 18

But I actually get this : 1 1 2 2 3 3 4 4 5 5 6 6 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 13 13 14 14 15 15 16 16 17 17 18 18

Could you help me resolve this issue ?


Solution

  • I don't know how exactly you got the initial matrix. But if you get a correct image with different colors after scaling, then each number in the matrix represents a pixel, not a color component of the pixel. And you lost the colors before you even built the initial matrix.

    Also, don't use multidimensional arrays in critical code, they have a very slow implementation. Use nested arrays instead. And don't use the GetLength method inside the for condition, it is called on every iteration.

    If you really want to do the matrix transformation you described in the question, then just increment j and l by 3 instead of 1, and work with triplets instead of single numbers.

    int coeff = 2;
    int index = 1;
    int line = 3;
    int column = 6;
    
    int[][] m = new int[line][];
    
    
    for (int i = 0; i < line; i++)
    {
        m[i] = new int[column];
        for (int j = 0; j < column; j++)
        {
            m[i][j] = index;
            index++;
            Console.Write(m[i][j] + " ");
        }
        Console.WriteLine();
    }
    
    int[][] matrix = new int[line * coeff][];
    
    for (int i = 0; i < line; i++)
    {
        for (int k = i * coeff; k < i * coeff + coeff; k++)
        {
            matrix[k] = new int[column * coeff];
        }
        
        for (int j = 0; j < column; j += 3)
        {
            for (int k = i * coeff; k < i * coeff + coeff; k++)
            {
                for (int l = j * coeff; l < j * coeff + coeff * 3; l += 3)
                {
                    int[] matrixLine = matrix[k];
                    int[] mLine = m[i];
                    
                    matrixLine[l + 0] = mLine[j + 0];
                    matrixLine[l + 1] = mLine[j + 1];
                    matrixLine[l + 2] = mLine[j + 2];
                }
    
            }
        }
    }
    
    Console.WriteLine();
    for (int i = 0; i < line * coeff; i++)
    {
        for (int j = 0; j < column * coeff; j++)
        {
            Console.Write(matrix[i][j] + " ");
        }
        Console.WriteLine();
    }