Love2dCS icon indicating copy to clipboard operation
Love2dCS copied to clipboard

creating new imageData from color[,] instead of vector4[,]

Open Shadowblitz16 opened this issue 4 years ago • 3 comments

please add these overloads

  • Love.Image.NewImageData(Love.Color[,] rawData, Love.ImageDataPixelFormat format);
  • Love.Image.NewImageData(Love.Color[][] rawData, Love.ImageDataPixelFormat format);

Shadowblitz16 avatar Jan 07 '20 22:01 Shadowblitz16

note this should probably be done with some sort of memory copying instead of looping so its faster

Shadowblitz16 avatar Jan 07 '20 22:01 Shadowblitz16

ok, your are right, this overrid is convenient. :+1: i think i will add them to next version.

but i have no idea how to use memory copy to speed up. beacuse i use 4 byte on Color(byte,byte,byte,byte) but use (4 * 4 = 16)byte on Vector4(float,float,float,float). and with more complex situation, it depending on how ImageDataPixelFormat it used,

  • If it used by RGBA8, vec4 should convert from 16byte to 4byte. Color can copy it nromally.
  • If it used by RGBA16, vec4 should convert from 16byte to 4byte. Color hould convert from 4byte to 8byte.
  • If it used by RGBA16F, vec4 should convert from 16byte to 4byte. Color hould convert from 4byte to 8byte. and need half convet.
  • If it used by RGBA32F, vec4 scan copy it nromally. Color hould convert from 4byte to 16byte.

So, In so many cases, only two are suitable for memory replication, and [x, y] and [y, x] are not considered. So I'm not interested in doing this optimization. :tired_face:

endlesstravel avatar Jan 08 '20 04:01 endlesstravel

I was using span<T> and unsafe to do conversions from Color[,] to Color[]


            public Color[] Flatten(Color[,] v)
            {
                Color[] value;
                var length = v.GetLength(0) * v.GetLength(1);

                unsafe
                {
                    fixed (Color* p = v)
                    {
                        var span = new Span<Color>(p, length);
                        var slice = span.Slice(0);
                        value = slice.ToArray();
                    }
                }
                return value;
            }

not sure if it works since I never tested it but it would require unsafe. I would suggest postponing this until we actually know that you can wrap unsafe code in a managed library without needing the unsafe option to be checked.

keep this open I will get back to you

Shadowblitz16 avatar Jan 08 '20 04:01 Shadowblitz16