Image looks corrupted when modifying and then loading back raw texture data

Hey everyone, I am working on this project where I modify textures / images.

I have this class that holds information of my final image, when creating a class I get raw texture data from the image and I store all the pixels to the NativeArray.

public class PImage
{
    public readonly string      ID              = string.Empty;

    public Texture2D            processed       = null;
    public NativeArray<Color32> pixels          = new NativeArray<Color32>();

    public PImage(Texture2D tex)
    {
        this.ID = Util.GenerateID();

        this.processed = tex;
        this.pixels = this.processed.GetRawTextureData<Color32>();
    }
    public void SetPixels(NativeArray<Color32> pixels)
    {
        this.pixels = pixels;
        this.processed.LoadRawTextureData<Color32>(this.pixels);
        this.processed.Apply();
    }
}

I have some features in my project, one feature is whenever I press a button I call the method that inverts all image pixels.

public void InvertColors(NativeArray<Color32> pixels) 
{
    if (pixels.Length <= 0) { return; }

    for (int i = 0; i < pixels.Length; i++)
    {
        byte r = (byte)Truncate(255 - pixels[i].r);
        byte g = (byte)Truncate(255 - pixels[i].g);
        byte b = (byte)Truncate(255 - pixels[i].b);
        byte a = (byte)Truncate(255 - pixels[i].a);

        pixels[i] = new Color32(r, g, b, a);
    }

    //Main is a main class that holds a reference to the active image.
    //GetActiveImage is just a Func<PImage> that returns active PImage reference.

    Main.GetActiveImage().SetPixels(pixels);
}
private float Truncate(float value)
{
    if (value < 0) { return 0; }
    if (value > 255) { return 255; }

    return value;
}

This should work fine, but the image looks corrupted after change.

Although once I press invert again to invert back the image goes back to normal without any sort of corruption.

I use LoadRawTextureData and GetRawTextureData, because of its efficiency. I found that using these methods is faster than using SetPixels and GetPixels.

Can someone give me some advice on why this might be happening?

I don’t think you want to be inverting the alpha. Wouldn’t you just want to leave that alone? A fully-opaque JPG/PNG would simply become a fully-transparent invisible clear picture…

Try leaving the alpha alone, see what you get back.