I have a texture user drawn(on a mobile device), and I want to encode that to jpg so that user could share with that but I found the tranparent part will be black in the encoded jpg:
while I want the jpg to be like this:
But I don't find any overridden method of texture2d.EncodeToJPG()
to do this.
any ideas?
NOTE
the wing of the bird was drawn specifically to Color.white
so it could be white in the encoded jpg.
Approach now
I finally manage to make this work by:
Color32[] pixels = text.GetPixels32();
Color blackTransparent = Color.black;
blackTransparent.a = 0;
for(int i = 0;i < pixels.Length; i++)
{
if(pixels[i] == blackTransparent)
{
pixels[i] = Color.white;
}
}
text.SetPixels32(pixels);
text.Apply();
But this will loop through all the pixels of the texture, if someone has a better method, plz let us know.
Problem with this approach:
we found that there will be some edge(when meeting with the black line) on the jpg using above code, maybe need some graphic processing knowledge to solve this?
Here is How we load image to texture2d(as required by comment)
Texture2D text = new Texture2D(1, 1, TextureFormat.PVRTC_RGBA4, false);
byte[] imagebytes = null;
string path = "image/path/sample.png";
if (System.IO.File.Exists(path))
{
Debug.Log(" really load file from " + path);
imagebytes = System.IO.File.ReadAllBytes(path);
}
text.LoadImage(imagebytes, false);