Aiv.Fast2D.Texture.LoadImage C# (CSharp) Method

LoadImage() private method

private LoadImage ( string fileName, int &width, int &height ) : byte[]
fileName string
width int
height int
return byte[]
        private byte[] LoadImage(string fileName, out int width, out int height)
        {
            byte[] bitmap = null;
            Stream imageStream = null;

            #if !__MOBILE__
            Bitmap image = null;

            Assembly assembly = Assembly.GetEntryAssembly();

            // if the file in included in the resources, load it as stream
            if (assembly.GetManifestResourceNames().Contains<string>(fileName))
            {
                imageStream = assembly.GetManifestResourceStream(fileName);
            }

            if (imageStream != null)
            {
                image = new Bitmap(imageStream);
            }
            else {
                image = new Bitmap(fileName);
            }

            using (image)
            {
                // to avoid losing a ref
                Bitmap _image = image;
                bitmap = new byte[image.Width * image.Height * 4];
                width = image.Width;
                height = image.Height;
                // if the image is not rgba, let's fix it
                if (image.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb)
                {
                    Bitmap tmpBitmap = new Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                    using (Graphics gfx = Graphics.FromImage(tmpBitmap))
                    {
                        gfx.DrawImageUnscaled(image, 0, 0);
                    }
                    _image = tmpBitmap;
                }

                System.Drawing.Imaging.BitmapData data = _image.LockBits(new Rectangle(0, 0, width, height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                Marshal.Copy(data.Scan0, bitmap, 0, bitmap.Length);
                _image.UnlockBits(data);

                for (int y = 0; y < _image.Height; y++)
                {
                    for (int x = 0; x < _image.Width; x++)
                    {
                        int position = (y * _image.Width * 4) + (x * 4);
                        // bgra -> rgba
                        byte b = bitmap[position];
                        byte r = bitmap[position + 2];
                        bitmap[position] = r;
                        bitmap[position + 2] = b;
                        // premultiply
                        byte a = bitmap[position + 3];
                        bitmap[position] = (byte) (bitmap[position] * (a / 255f));
                        bitmap[position+1] = (byte)(bitmap[position+1] * (a / 255f));
                        bitmap[position+2] = (byte)(bitmap[position+2] * (a / 255f));
                    }
                }
                premultiplied = true;
            }
            #else
            Bitmap image = null;
            if (imageStream == null)
            {
                if (fileName.StartsWith("Assets/"))
                {
                    string newFileName = fileName.Substring(7);
                    imageStream = Window.Assets.Open(newFileName);
                }
            }

            BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
            bitmapOptions.InPreferredConfig = global::Android.Graphics.Bitmap.Config.Argb8888;
            image = BitmapFactory.DecodeStream(imageStream, new global::Android.Graphics.Rect(0, 0, 0, 0), bitmapOptions);

            width = image.Width;
            height = image.Height;

            bitmap = new byte[width * height * 4];

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    int pixel = image.GetPixel(x, y);

                    byte a = (byte)((pixel >> 24) & 0xff);
                    byte r = (byte)((pixel >> 16) & 0xff);
                    byte g = (byte)((pixel >> 8) & 0xff);
                    byte b = (byte)(pixel & 0xff);
                    // premultiply;
                    int position = (y * width * 4) + (x * 4);
                    bitmap[position] = (byte)(r * (a / 255f));
                    bitmap[position + 1] = (byte)(g * (a / 255f));
                    bitmap[position + 2] = (byte)(b * (a / 255f));
                    bitmap[position + 3] = a;

                }
            }
            premultiplied = true;
            #endif

            return bitmap;
        }