Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Create Image from 2D array  (Read 6185 times)

0 Members and 1 Guest are viewing this topic.

mike919

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Create Image from 2D array
« on: April 18, 2012, 09:34:19 pm »
I was having a problem creating an image from a 2D color array.  After some troubleshooting I think I narrowed it down to a bug.  If I do the following:

Code: [Select]
            SFML.Graphics.Color[,] imageColor = new SFML.Graphics.Color[256, 256];

            for (int j = 0; j < imageColor.GetLength(0); j++)
            {
                for (int i = 0; i < imageColor.GetLength(1); i++)
                {
                    imageColor[j, i] = new SFML.Graphics.Color((byte)i, (byte)j, 0);
                }
            }

            SFML.Graphics.Image image= new SFML.Graphics.Image(imageColor);

            Texture texture = new Texture(image);

            Sprite sprite = new Sprite(texture);

            app.Draw(sprite);

            app.Display();

I get a nice square image with the red color increasing in the X direction and the green color increasing in Y.  But if I change the array dimension so that it is not square the image is no longer drawn correctly.

It seems like the image constructor is mixing up the dimensions, when the array is not square it sets X size equal to dimension(0) and Y size equal to dimension(1).  This should be the opposite correct?  The data seems to be copied correctly, but because the width and height are wrong the image is not drawn right.

The code above was from the 2.0 RC but 1.6 seems to have the same behavior.  Am I doing something wrong here or is this really a bug?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Create Image from 2D array
« Reply #1 on: April 18, 2012, 10:38:07 pm »
Why would indices be [y, x] and not [x, y]?
Laurent Gomila - SFML developer

mike919

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Create Image from 2D array
« Reply #2 on: April 20, 2012, 04:59:22 am »
Isn't this the convention that normally used?  Though I'm not saying that it is just a matter of convention, the underlying code seems to expect [y,x] but the .Net constructor uses [x,y].  If this is the case than this constructor wouldn't work for anything that is not a square 2D array.  I am not able to get it to work with nonsquare arrays.

I did manage to get it working by using the other constructor for a flattened byte array instead of the 2D color array.  If this flattened array were reformed into a 2D it would follow [y,x] convention.  Heres the code:

Code: [Select]
int sizex = 127;
            int sizey = 256;

            byte[] imageColor = new byte[sizex * sizey * 4];

            for (int j = 0; j < sizey; j++)
            {
                for (int i = 0; i < sizex; i++)
                {
                    //red channel
                    imageColor[j * sizex * 4 + 4 * i] = (byte)i;

                    //green channel
                    imageColor[j * sizex * 4 + 4 * i + 1] = (byte)j;

                    //blue channel
                    imageColor[j * sizex * 4 + 4 * i + 2] = 0;

                    //alpha channel
                    imageColor[j * sizex * 4 + 4 * i + 3] = 255;
                }
            }           

            SFML.Graphics.Image image = new SFML.Graphics.Image((uint)sizex, (uint)sizey, imageColor);

            Texture texture = new Texture(image);

            Sprite sprite = new Sprite(texture);

            app.Draw(sprite);

            while (true)
            {

                app.Clear();
               
                app.Draw(sprite);
               
                app.Display();

            }

I did try to have a look through the source code to try to get an idea of whats going on.  The only thing I could find is the constructor in Image.cpp, but by that point width and height are already separately and probably correctly :) defined.  Where would I find the code for the .Net constructor if I wanted to take a look at that?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Create Image from 2D array
« Reply #3 on: April 20, 2012, 08:21:42 am »
Hmm ok I see. Indeed there's probably a bug then.
Laurent Gomila - SFML developer