Пример #1
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);
 }
Пример #2
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);
            }
        }
Пример #3
0
        /*
         * Render the given  string using the given font.   Transfer the image
         * to a  surface of  power-of-2 size large  enough to fit  the string.
         * Return an OpenGL texture object.
         */
        //  Image
        public static int make_image_from_font(
            ref int w, ref int h,
            string text, Alt.Sketch.Font font)
        {
            int o = 0;

            /* Render the text. */

            if (!string.IsNullOrEmpty(text))
            {
                Alt.Sketch.SizeI size = gui.gui_measure(text, font);

                Alt.Sketch.Bitmap bitmap = new Alt.Sketch.Bitmap(size, Alt.Sketch.PixelFormat.Format32bppArgb);
                using (Alt.Sketch.Graphics graphics = Alt.Sketch.Graphics.FromImage(bitmap))
                {
                    graphics.Clear(Alt.Sketch.Color.Empty);
                    graphics.DrawString(text, font, Alt.Sketch.Brushes.White, Alt.Sketch.Point.Zero);
                }
                {
                    w = bitmap.PixelWidth;
                    h = bitmap.PixelHeight;

                    int  trw = 0;
                    bool f   = false;
                    //  left
                    for (int x = 0; x < w; x++)
                    {
                        for (int y = 0; y < h; y++)
                        {
                            if (bitmap.GetPixel(x, y).A != 0)
                            {
                                f = true;
                                break;
                            }
                        }

                        if (f)
                        {
                            break;
                        }

                        trw++;
                    }

                    //  right
                    f = false;
                    for (int x = w - 1; x >= 0; x--)
                    {
                        for (int y = 0; y < h; y++)
                        {
                            if (bitmap.GetPixel(x, y).A != 0)
                            {
                                f = true;
                                break;
                            }
                        }

                        if (f)
                        {
                            break;
                        }

                        trw++;
                    }

                    //  Pad the text to power-of-two

                    Alt.Sketch.BitmapData bdata = bitmap.LockBits(Alt.Sketch.ImageLockMode.ReadOnly);

                    int    w2 = 0;
                    int    h2 = 0;
                    byte[] p  = image_next2(bdata.Scan0, bdata.PixelWidth, bdata.PixelHeight, bdata.ByteDepth, trw, ref w2, ref h2);

                    bitmap.UnlockBits(bdata);


                    /* Saturate the color channels.  Modulate ONLY in alpha. */

                    Image.image_white(p, w2, h2, bitmap.ByteDepth);

                    /* Create the OpenGL texture object. */

                    o = Image.make_texture(p, w2, h2, bitmap.ByteDepth);
                }
            }
            else
            {
                /* Empty string. */

                //if (w) *
                w = 0;
                //if (h) *
                h = 0;
                //if (W) *W = 0;
                //if (H) *H = 0;
            }

            return(o);
        }