protected override void CreateInternal(byte[] data)
        {
            byte[] pixels  = null;
            var    width   = 0;
            var    height  = 0;
            var    flipped = false; // Whether the image was uploaded flipped - top to bottom.

            PerfProfiler.ProfilerEventStart("Decoding Image", "Loading");

            // Check if PNG.
            if (PngFormat.IsPng(data))
            {
                pixels = PngFormat.Decode(data, out PngFileHeader header);
                width  = header.Width;
                height = header.Height;
            }
            // Check if BMP.
            else if (BmpFormat.IsBmp(data))
            {
                pixels  = BmpFormat.Decode(data, out BmpFileHeader header);
                width   = header.Width;
                height  = header.Height;
                flipped = true;
            }

            if (pixels == null || width == 0 || height == 0)
            {
                Engine.Log.Warning($"Couldn't load texture - {Name}.", MessageSource.AssetLoader);
                return;
            }

            PerfProfiler.ProfilerEventEnd("Decoding Image", "Loading");
            UploadTexture(new Vector2(width, height), pixels, flipped);
        }
Ejemplo n.º 2
0
        public void DecodePngInterlaced()
        {
            string png = Path.Join("Assets", "Images", "spritesheetAnimation.png");

            byte[] bytes = File.ReadAllBytes(png);
            Assert.True(PngFormat.IsPng(bytes));

            byte[] decodedPixelData = PngFormat.Decode(bytes, out PngFileHeader fileHeader);
            Assert.True(decodedPixelData != null);
            Assert.True(fileHeader.InterlaceMethod == 1);

            Runner.ExecuteAsLoop(_ =>
            {
                var r = new Texture(
                    fileHeader.Size,
                    decodedPixelData,
                    fileHeader.PixelFormat);

                RenderComposer composer = Engine.Renderer.StartFrame();
                composer.RenderSprite(Vector3.Zero, fileHeader.Size, Color.White, r);
                Engine.Renderer.EndFrame();
                Runner.VerifyScreenshot(ResultDb.PngDecodeInterlaced);
                r.Dispose();
            }).WaitOne();
        }
Ejemplo n.º 3
0
        private void LoadFile(OtherAsset f)
        {
            _status = "Loading...";

            byte[] data = f.Content;
            if (!PngFormat.IsPng(data))
            {
                _status = $"The provided file {f.Name} is not a PNG file.";
                return;
            }

            byte[] pixels = PngFormat.Decode(data, out PngFileHeader header);
            byte[] output = PngFormat.Encode(pixels, header.Width, header.Height);

            bool saved = Engine.AssetLoader.Save(output, "Player" + "/" + f.Name, false);

            _status = saved ? "Done!" : "Error when saving the file. Check logs.";
        }
Ejemplo n.º 4
0
        public static void LoadCache()
        {
            string folderName = Path.Join("Assets", "CachedResults");

            if (!Directory.Exists(folderName))
            {
                return;
            }

            string[] files = Directory.GetFiles(folderName);

            foreach (string file in files)
            {
                byte[] fileData = File.ReadAllBytes(file);
                string fileName = Path.GetFileNameWithoutExtension(file);
                byte[] pixels   = PngFormat.Decode(fileData, out PngFileHeader header);
                ImageUtil.FlipImageY(pixels, (int)header.Size.Y);
                CachedResults.Add(fileName, pixels);
            }
        }
Ejemplo n.º 5
0
        protected override void CreateInternal(ReadOnlyMemory <byte> data)
        {
            byte[]  pixels  = null;
            Vector2 size    = Vector2.Zero;
            var     flipped = false; // Whether the image was uploaded flipped - top to bottom.
            var     format  = PixelFormat.Unknown;

            PerfProfiler.ProfilerEventStart("Decoding Image", "Loading");

            // Magic number check to find out format.
            if (PngFormat.IsPng(data))
            {
                pixels = PngFormat.Decode(data, out PngFileHeader header);
                size   = header.Size;
                format = header.PixelFormat;
            }
            else if (BmpFormat.IsBmp(data))
            {
                pixels  = BmpFormat.Decode(data, out BmpFileHeader header);
                size    = new Vector2(header.Width, header.HeaderSize);
                flipped = true;
                format  = PixelFormat.Bgra;
            }
            else if (ImgBinFormat.IsImgBin(data))
            {
                pixels = ImgBinFormat.Decode(data, out ImgBinFileHeader header);
                size   = header.Size;
                format = header.Format;
            }

            if (pixels == null || size.X == 0 || size.Y == 0)
            {
                Texture = Texture.EmptyWhiteTexture;
                Engine.Log.Warning($"Couldn't load texture - {Name}.", MessageSource.AssetLoader);
                return;
            }

            ByteSize = pixels.Length;
            PerfProfiler.ProfilerEventEnd("Decoding Image", "Loading");
            UploadTexture(size, pixels, flipped, format);
        }