7

What would be a fast method to copy/convert an array of Color32[] values to a byte[] buffer? Color32 is a struct from Unity 3D containing 4 bytes, R, G, B and A respectively. What I'm trying to accomplish is to send the rendered image from unity through a pipe to another application (Windows Forms). Currently I'm using this code:

private static byte[] Color32ArrayToByteArray(Color32[] colors)
{
    int length = 4 * colors.Length;
    byte[] bytes = new byte[length];
    IntPtr ptr = Marshal.AllocHGlobal(length);
    Marshal.StructureToPtr(colors, ptr, true);
    Marshal.Copy(ptr, bytes, 0, length);
    Marshal.FreeHGlobal(ptr);
    return bytes;
}

Thankyou and sorry, I'm new to StackOverflow. Marinescu Alexandru

Steven
  • 166,672
  • 24
  • 332
  • 435
user3263058
  • 191
  • 1
  • 1
  • 5
  • So you've got that code... does it do what you want it to? – Jon Skeet Feb 02 '14 at 15:25
  • Would you please share your result, problems and your question clearly?! – Saleh Parsa Feb 02 '14 at 15:26
  • It would seem so. But I was wondering whether a faster method exists... – user3263058 Feb 02 '14 at 15:27
  • Any speed gain in mind? – L.B Feb 02 '14 at 15:34
  • I'm sending every frame rendered from Unity through a pipe to another application. That's why I wanted to know whether a faster method exists. Currently I'm getting around 11 msec per frame for converting the Color32[] array to a byte[] array. Previously I used the EncodeToPNG() method which took around 85 msec per frame. – user3263058 Feb 02 '14 at 15:40
  • You are copying the data 3 times. First to HGlobal, then to byte[], then to the pipe buffer. Try copying it only 1 time by not converting at all. Use BinaryWriter. – Hans Passant Feb 02 '14 at 16:48
  • i seen someting here http://answers.unity3d.com/questions/190340/how-can-i-send-a-render-texture-over-the-network.html – JRowan Feb 04 '14 at 03:19

3 Answers3

11

I ended up using this code:

using System.Runtime.InteropServices;

private static byte[] Color32ArrayToByteArray(Color32[] colors)
{
    if (colors == null || colors.Length == 0)
        return null;

    int lengthOfColor32 = Marshal.SizeOf(typeof(Color32));
    int length = lengthOfColor32 * colors.Length;
    byte[] bytes = new byte[length];

    GCHandle handle = default(GCHandle);
    try
    {
        handle = GCHandle.Alloc(colors, GCHandleType.Pinned);
        IntPtr ptr = handle.AddrOfPinnedObject();
        Marshal.Copy(ptr, bytes, 0, length);
    }
    finally
    {
        if (handle != default(GCHandle))
            handle.Free();
    }

    return bytes;
}

Which is fast enough for my needs.

Soylent Graham
  • 847
  • 1
  • 12
  • 22
user3263058
  • 191
  • 1
  • 1
  • 5
  • i used this script to make a webcam screenshot method: `static byte[] ScreenshotWebcam(WebCamTexture wct) { Texture2D colorTex = new Texture2D(wct.width, wct.height, TextureFormat.RGBA32, false); colorTex.LoadRawTextureData(Color32ArrayToByteArray(wct.GetPixels32())); colorTex.Apply(); return colorTex.EncodeToPNG(); }` – Vlad Feb 15 '20 at 08:36
3

With modern .NET, you can use spans for this:

var bytes = MemoryMarshal.Cast<Color32, byte>(colors);

This gives you a Span<byte> that covers the same data. The API is directly comparable to using vectors (byte[]), but it isn't actually a vector, and there is no copy: you are directly accessing the original data. It is like an unsafe pointer coercion, but: entirely safe.

If you need it as a vector, ToArray and copy methods exist for that.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
-2

Well why do you work with Color32?

byte[] Bytes = tex.GetRawTextureData(); . . . Tex.LoadRawTextureData(Bytes); Tex.Apply();

mahdi bazei
  • 1
  • 1
  • 1