Search Unity

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

Discussion in 'Scripting' started by Rukas90, Sep 28, 2019.

  1. Rukas90

    Rukas90

    Joined:
    Sep 20, 2015
    Posts:
    169
    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.

    Code (CSharp):
    1. public class PImage
    2. {
    3.     public readonly string      ID              = string.Empty;
    4.  
    5.     public Texture2D            processed       = null;
    6.     public NativeArray<Color32> pixels          = new NativeArray<Color32>();
    7.  
    8.     public PImage(Texture2D tex)
    9.     {
    10.         this.ID = Util.GenerateID();
    11.  
    12.         this.processed = tex;
    13.         this.pixels = this.processed.GetRawTextureData<Color32>();
    14.     }
    15.     public void SetPixels(NativeArray<Color32> pixels)
    16.     {
    17.         this.pixels = pixels;
    18.         this.processed.LoadRawTextureData<Color32>(this.pixels);
    19.         this.processed.Apply();
    20.     }
    21. }
    I have some features in my project, one feature is whenever I press a button I call the method that inverts all image pixels.

    Code (CSharp):
    1. public void InvertColors(NativeArray<Color32> pixels)
    2. {
    3.     if (pixels.Length <= 0) { return; }
    4.  
    5.     for (int i = 0; i < pixels.Length; i++)
    6.     {
    7.         byte r = (byte)Truncate(255 - pixels[i].r);
    8.         byte g = (byte)Truncate(255 - pixels[i].g);
    9.         byte b = (byte)Truncate(255 - pixels[i].b);
    10.         byte a = (byte)Truncate(255 - pixels[i].a);
    11.  
    12.         pixels[i] = new Color32(r, g, b, a);
    13.     }
    14.  
    15.     //Main is a main class that holds a reference to the active image.
    16.     //GetActiveImage is just a Func<PImage> that returns active PImage reference.
    17.  
    18.     Main.GetActiveImage().SetPixels(pixels);
    19. }
    20. private float Truncate(float value)
    21. {
    22.     if (value < 0) { return 0; }
    23.     if (value > 255) { return 255; }
    24.  
    25.     return value;
    26. }
    This should work fine, but the image looks corrupted after change.

    IMG.png

    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?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    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.