Пример #1
0
        public static void SetTextureData(UnityEngine.Texture2D texture, Alt.Sketch.Bitmap src, Alt.Sketch.RectI srcRect, bool flip)
        {
            if (texture == null ||
                src == null ||
                !src.IsValid)
            {
                return;
            }


            Alt.Sketch.BitmapData srcData = src.LockBits(Sketch.ImageLockMode.ReadOnly);
            if (srcData == null)
            {
                return;
            }


            int src_x = srcRect.X;
            int src_y = srcRect.Y;
            //int src_w, src_h;
            int src_width =             //src_w =
                            srcRect.Width;
            int src_height =            //src_h =
                             srcRect.Height;

            int dest_x = 0;
            int dest_y = 0;

            int dest_w = texture.width;
            int dest_h = texture.height;

            if (dest_x > (dest_w - 1) ||
                dest_y > (dest_h - 1))
            {
                src.UnlockBits(srcData);

                return;
            }

            if ((dest_x + src_width) > dest_w)
            {
                src_width = dest_w - dest_x;
            }

            if ((dest_y + src_height) > dest_h)
            {
                src_height = dest_h - dest_y;
            }


            byte[] src_Buffer    = srcData.Scan0;
            int    src_stride    = srcData.Stride;
            int    src_ByteDepth = srcData.ByteDepth;


            try
            {
                bool fProcessed = false;

                switch (srcData.PixelFormat)
                {
                case Sketch.PixelFormat.Format24bppRgb:
                {
                    switch (texture.format)
                    {
                    case TextureFormat.RGB24:
                    {
                        if (src_width == dest_w &&
                            src_height == dest_h)
                        {
                            //texture.LoadRawTextureData(src_Buffer);

                            byte[] colors = src.TextureTempBuffer;
                            if (colors == null)
                            {
                                colors = new byte[src_height * src_width * 3];
                            }
                            int colors_index = 0;

                            if (flip)
                            {
                                for (int y = src_height - 1; y >= 0; y--)
                                {
                                    int src_Index = src_x * src_ByteDepth + (src_y + y) * src_stride;

                                    for (int x = 0; x < src_width; x++)
                                    {
                                        colors[colors_index++] = src_Buffer[src_Index + 2];
                                        colors[colors_index++] = src_Buffer[src_Index + 1];
                                        colors[colors_index++] = src_Buffer[src_Index];
                                        src_Index += 3;
                                    }
                                }
                            }
                            else
                            {
                                for (int y = 0; y < src_height; y++)
                                {
                                    int src_Index = src_x * src_ByteDepth + (src_y + y) * src_stride;

                                    for (int x = 0; x < src_width; x++)
                                    {
                                        colors[colors_index++] = src_Buffer[src_Index + 2];
                                        colors[colors_index++] = src_Buffer[src_Index + 1];
                                        colors[colors_index++] = src_Buffer[src_Index];
                                        src_Index += 3;
                                    }
                                }
                            }

                            texture.LoadRawTextureData(colors);
                        }
                        else
                        {
                            UnityEngine.Color[] colors = new UnityEngine.Color[src_height * src_width];
                            int colors_index           = 0;

                            if (flip)
                            {
                                for (int y = src_height - 1; y >= 0; y--)
                                {
                                    int src_Index = src_x * src_ByteDepth + (src_y + y) * src_stride;

                                    for (int x = 0; x < src_width; x++)
                                    {
                                        colors[colors_index++] = new Color32(
                                            src_Buffer[src_Index + 2],
                                            src_Buffer[src_Index + 1],
                                            src_Buffer[src_Index],
                                            255);
                                        src_Index += 3;
                                    }
                                }
                            }
                            else
                            {
                                for (int y = 0; y < src_height; y++)
                                {
                                    int src_Index = src_x * src_ByteDepth + (src_y + y) * src_stride;

                                    for (int x = 0; x < src_width; x++)
                                    {
                                        colors[colors_index++] = new Color32(
                                            src_Buffer[src_Index + 2],
                                            src_Buffer[src_Index + 1],
                                            src_Buffer[src_Index],
                                            255);
                                        src_Index += 3;
                                    }
                                }
                            }

                            texture.SetPixels(src_x, src_y, src_width, src_height, colors);
                        }

                        fProcessed = true;

                        break;
                    }

                    case TextureFormat.RGBA32:
                    {
                        if (src_width == dest_w &&
                            src_height == dest_h)
                        {
                            byte[] colors       = new byte[src_height * src_width * 4];
                            int    colors_index = 0;

                            if (flip)
                            {
                                for (int y = src_height - 1; y >= 0; y--)
                                {
                                    int src_Index = src_x * src_ByteDepth + (src_y + y) * src_stride;

                                    for (int x = 0; x < src_width; x++)
                                    {
                                        colors[colors_index++] = src_Buffer[src_Index + 2];
                                        colors[colors_index++] = src_Buffer[src_Index + 1];
                                        colors[colors_index++] = src_Buffer[src_Index];
                                        colors[colors_index++] = 255;
                                        src_Index += 3;
                                    }
                                }
                            }
                            else
                            {
                                for (int y = 0; y < src_height; y++)
                                {
                                    int src_Index = src_x * src_ByteDepth + (src_y + y) * src_stride;

                                    for (int x = 0; x < src_width; x++)
                                    {
                                        colors[colors_index++] = src_Buffer[src_Index + 2];
                                        colors[colors_index++] = src_Buffer[src_Index + 1];
                                        colors[colors_index++] = src_Buffer[src_Index];
                                        colors[colors_index++] = 255;
                                        src_Index += 3;
                                    }
                                }
                            }

                            texture.LoadRawTextureData(colors);
                        }
                        else
                        {
                            UnityEngine.Color[] colors = new UnityEngine.Color[src_height * src_width];
                            int colors_index           = 0;

                            if (flip)
                            {
                                for (int y = src_height - 1; y >= 0; y--)
                                {
                                    int src_Index = src_x * src_ByteDepth + (src_y + y) * src_stride;

                                    for (int x = 0; x < src_width; x++)
                                    {
                                        colors[colors_index++] = new Color32(
                                            src_Buffer[src_Index + 2],
                                            src_Buffer[src_Index + 1],
                                            src_Buffer[src_Index],
                                            255);
                                        src_Index += 3;
                                    }
                                }
                            }
                            else
                            {
                                for (int y = 0; y < src_height; y++)
                                {
                                    int src_Index = src_x * src_ByteDepth + (src_y + y) * src_stride;

                                    for (int x = 0; x < src_width; x++)
                                    {
                                        colors[colors_index++] = new Color32(
                                            src_Buffer[src_Index + 2],
                                            src_Buffer[src_Index + 1],
                                            src_Buffer[src_Index],
                                            255);
                                        src_Index += 3;
                                    }
                                }
                            }

                            texture.SetPixels(src_x, src_y, src_width, src_height, colors);
                        }

                        fProcessed = true;

                        break;
                    }
                    }

                    break;
                }

                case Sketch.PixelFormat.Format32bppArgb:
                {
                    switch (texture.format)
                    {
                    case TextureFormat.RGB24:
                    {
                        //  We can't be here

                        //int dest_ByteDepth = 3;

                        break;
                    }

                    case TextureFormat.RGBA32:
                    {
                        if (src_width == dest_w &&
                            src_height == dest_h)
                        {
                            //texture.LoadRawTextureData(src_Buffer);

                            byte[] colors = src.TextureTempBuffer;
                            if (colors == null)
                            {
                                colors = new byte[src_height * src_width * 4];
                            }
                            int colors_index = 0;

                            if (flip)
                            {
                                for (int y = src_height - 1; y >= 0; y--)
                                {
                                    int src_Index = src_x * src_ByteDepth + (src_y + y) * src_stride;

                                    for (int x = 0; x < src_width; x++)
                                    {
                                        colors[colors_index++] = src_Buffer[src_Index + 2];
                                        colors[colors_index++] = src_Buffer[src_Index + 1];
                                        colors[colors_index++] = src_Buffer[src_Index];
                                        colors[colors_index++] = src_Buffer[src_Index + 3];
                                        src_Index += 4;
                                    }
                                }
                            }
                            else
                            {
                                for (int y = 0; y < src_height; y++)
                                {
                                    int src_Index = src_x * src_ByteDepth + (src_y + y) * src_stride;

                                    for (int x = 0; x < src_width; x++)
                                    {
                                        colors[colors_index++] = src_Buffer[src_Index + 2];
                                        colors[colors_index++] = src_Buffer[src_Index + 1];
                                        colors[colors_index++] = src_Buffer[src_Index];
                                        colors[colors_index++] = src_Buffer[src_Index + 3];
                                        src_Index += 4;
                                    }
                                }
                            }

                            texture.LoadRawTextureData(colors);
                        }
                        else
                        {
                            UnityEngine.Color[] colors = new UnityEngine.Color[src_height * src_width];
                            int colors_index           = 0;

                            if (flip)
                            {
                                for (int y = src_height - 1; y >= 0; y--)
                                {
                                    int src_Index = src_x * src_ByteDepth + (src_y + y) * src_stride;

                                    for (int x = 0; x < src_width; x++)
                                    {
                                        colors[colors_index++] = new Color32(
                                            src_Buffer[src_Index + 2],
                                            src_Buffer[src_Index + 1],
                                            src_Buffer[src_Index],
                                            src_Buffer[src_Index + 3]);
                                        src_Index += 4;
                                    }
                                }
                            }
                            else
                            {
                                for (int y = 0; y < src_height; y++)
                                {
                                    int src_Index = src_x * src_ByteDepth + (src_y + y) * src_stride;

                                    for (int x = 0; x < src_width; x++)
                                    {
                                        colors[colors_index++] = new Color32(
                                            src_Buffer[src_Index + 2],
                                            src_Buffer[src_Index + 1],
                                            src_Buffer[src_Index],
                                            src_Buffer[src_Index + 3]);
                                        src_Index += 4;
                                    }
                                }
                            }

                            texture.SetPixels(src_x, src_y, src_width, src_height, colors);
                        }

                        fProcessed = true;

                        break;
                    }
                    }

                    break;
                }

                case Sketch.PixelFormat.Format8bppAlpha:
                {
                    //  not used yet

                    break;
                }
                }


                //  universal method
                if (!fProcessed)
                {
                    UnityEngine.Color[] colors = new UnityEngine.Color[src_height * src_width];
                    int colors_index           = 0;

                    for (int y = 0; y < src_height; y++)
                    {
                        for (int x = 0; x < src_width; x++)
                        {
                            Alt.Sketch.Color color = src.GetPixel(x, y);

                            colors[colors_index++] = new Color32(
                                color.R,
                                color.G,
                                color.B,
                                color.A);
                        }
                    }

                    texture.SetPixels(src_x, src_y, src_width, src_height, colors);

                    fProcessed = true;
                }

                if (fProcessed)
                {
                    texture.Apply(false);
                }
            }
            catch             //(System.Exception ex)
            {
                //Console.WriteLine(ex.ToString());
            }
            finally
            {
                src.UnlockBits(srcData);
            }
        }
Пример #2
0
 //static byte[] tempArray = new byte[1000000];
 public static void SetTextureData(UnityEngine.Texture2D texture, Alt.Sketch.Bitmap src, Alt.Sketch.RectI srcRect)
 {
     SetTextureData(texture, src, srcRect, false);
 }