Exemplo n.º 1
0
        /// <summary>
        /// Creates and loads and new image
        /// </summary>
        /// <param name="fileLocation">Location of the image</param>
        /// <param name="filp">Filp the image on load</param>
        /// <exception cref="FileNotFoundException"></exception>
        public Image(string fileLocation, bool filp)
        {
            if (!File.Exists(fileLocation))
            {
                throw new FileNotFoundException("Image doesn't exist!", fileLocation);
            }

            //Filp image on load, if we want to filp it
            if (filp)
            {
                StbImage.stbi_set_flip_vertically_on_load(1);
            }

            //Open stream
            FileStream imageStream = File.OpenRead(fileLocation);

            //Create image
            ImageResult image = ImageResult.FromStream(imageStream);

            Data   = image.Data;
            Width  = image.Width;
            Height = image.Height;

            //IDK if this was purposely done, but the enum number matches to the channels count of what the enum is
            Channels = (int)image.SourceComp;
        }
Exemplo n.º 2
0
        public static ImageResult LoadImage(string imgPath)
        {
#if DEBUG
            using Profiler fullProfiler = new Profiler(typeof(ImageHelper));
#endif
            StbImage.stbi_set_flip_vertically_on_load(1);
            return(ImageResult.FromMemory(ReadFile(imgPath), ColorComponents.RedGreenBlueAlpha));
        }
Exemplo n.º 3
0
        public void reloadtexture(string texturePath)
        {
            byte[] buffer = File.ReadAllBytes(texturePath);
            StbImage.stbi_set_flip_vertically_on_load(1);
            ImageResult image = ImageResult.FromMemory(buffer, ColorComponents.RedGreenBlueAlpha);

            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, image.Width, image.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, image.Data);
            GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
        }
Exemplo n.º 4
0
        private int TextureFromFile(string path, bool gamma = false)
        {
            string fullPath = $"{Directory}/{path}";

            int texID = GL.GenTexture();

            StbImage.stbi_set_flip_vertically_on_load(0);
            var img = ImageResult.FromMemory(File.ReadAllBytes(fullPath));

            if (img != null)
            {
                PixelInternalFormat format   = 0;
                PixelFormat         pxFormat = 0;
                if (img.Comp == ColorComponents.Grey)
                {
                    format   = PixelInternalFormat.R8;
                    pxFormat = PixelFormat.Red;
                }
                else if (img.Comp == ColorComponents.RedGreenBlue)
                {
                    format   = PixelInternalFormat.Rgb;
                    pxFormat = PixelFormat.Rgb;
                }
                else if (img.Comp == ColorComponents.RedGreenBlueAlpha)
                {
                    format   = PixelInternalFormat.Rgba;
                    pxFormat = PixelFormat.Rgba;
                }
                GL.BindTexture(TextureTarget.Texture2D, texID);
                GL.TexImage2D(TextureTarget.Texture2D, 0, format, img.Width, img.Height, 0, pxFormat, PixelType.UnsignedByte, img.Data);
                GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);

                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)All.ClampToEdge);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)All.ClampToEdge);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.LinearMipmapLinear);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear);
            }
            else
            {
                throw new Exception("Texture failed to load at " + fullPath);
            }
            return(texID);
        }
Exemplo n.º 5
0
        private static Texture2D LoadTextureFromFile(string texPath, bool alpha)
        {
            Texture2D tex = new Texture2D();

            if (alpha)
            {
                tex.InternalFormat = PixelInternalFormat.Rgba;
                tex.ImageFormat    = PixelFormat.Rgba;
            }

            StbImage.stbi_set_flip_vertically_on_load(0);
            var      img         = ImageResult.FromMemory(File.ReadAllBytes(Path.Combine(AppContext.BaseDirectory, texPath)), alpha ? ColorComponents.RedGreenBlueAlpha : ColorComponents.RedGreenBlue);
            GCHandle pinnedArray = GCHandle.Alloc(img.Data, GCHandleType.Pinned);
            IntPtr   pointer     = pinnedArray.AddrOfPinnedObject();

            tex.Generate(img.Width, img.Height, pointer);
            pinnedArray.Free();
            return(tex);
        }
Exemplo n.º 6
0
 public static unsafe Texture FromStream(Stream stream, bool flip = true)
 {
     if (DDS.StreamIsDDS(stream))
     {
         return(DDS.FromStream(stream));
     }
     else
     {
         /* Read full stream */
         int    len = (int)stream.Length;
         byte[] b   = new byte[len];
         int    pos = 0;
         int    r   = 0;
         while ((r = stream.Read(b, pos, len - pos)) > 0)
         {
             pos += r;
         }
         /* stb_image it */
         int x = 0, y = 0;
         StbImage.stbi_set_flip_vertically_on_load(flip ? 1 : 0);
         ImageResult image = ImageResult.FromMemory(b, ColorComponents.RedGreenBlueAlpha);
         x = image.Width;
         y = image.Height;
         var data       = image.Data;
         int dataLength = x * y * 4;
         int j          = 0;
         for (int i = 0; i < dataLength; i += 4)
         {
             var R = data[i];
             var G = data[i + 1];
             var B = data[i + 2];
             var A = data[i + 3];
             data[j++] = B;
             data[j++] = G;
             data[j++] = R;
             data[j++] = A;
         }
         var t = new Texture2D(x, y, false, SurfaceFormat.Color);
         t.SetData(data);
         return(t);
     }
 }
Exemplo n.º 7
0
        public static LoadResult BytesFromStream(Stream stream, bool flip = false)
        {
            int len = (int)stream.Length;

            byte[] b   = new byte[len];
            int    pos = 0;
            int    r   = 0;

            while ((r = stream.Read(b, pos, len - pos)) > 0)
            {
                pos += r;
            }
            /* stb_image it */
            StbImage.stbi_set_flip_vertically_on_load(flip ? 1 : 0);
            ImageResult image = ImageResult.FromMemory(b, ColorComponents.RedGreenBlueAlpha);

            return(new LoadResult()
            {
                Width = image.Width, Height = image.Height, Data = image.Data
            });
        }