Esempio n. 1
0
        public void Info(string filename, int headerSize, int width, int height, ColorComponents colorComponents, bool is16bit)
        {
            ImageInfo?result;

            var data = new byte[headerSize];

            using (var stream = _assembly.OpenResourceStream(filename))
            {
                stream.Read(data, 0, data.Length);
            }

            using (var stream = new MemoryStream(data))
            {
                result = ImageInfo.FromStream(stream);
            }

            Assert.IsNotNull(result);

            var info = result.Value;

            Assert.AreEqual(info.Width, width);
            Assert.AreEqual(info.Height, height);
            Assert.AreEqual(info.ColorComponents, colorComponents);
            Assert.AreEqual(info.BitsPerChannel, is16bit ? 16 : 8);
        }
Esempio n. 2
0
        public static async Task <ImageInfo> GetImageInfoFrom(Stream source)
        {
            source = await source.CopyToSeekableStreamIfNeeded(disposeOriginalStream : true);

            try {
                try {
                    source.Seek(0, SeekOrigin.Begin);
                    return(ImageInfo.FromStream(source).Value);
                }
                catch (Exception e) { Log.w("ImageInfo.FromStream failed", e); }
                source.Seek(0, SeekOrigin.Begin);
                var img = ImageResult.FromStream(source);
                return(new ImageInfo()
                {
                    Width = img.Width, Height = img.Height
                });
            }
            catch (Exception e) { Log.w("ImageResult.FromStream failed", e); }
            source.Seek(0, SeekOrigin.Begin);
            // ExifLib.ExifReader closes the stream on error so has to be tried last
            return(GetInfoFromJpgExifReader(source));
        }
Esempio n. 3
0
        private bool LoadCubemapLevel(ref Texture texture, out PixelBufferDescriptor outBuffer, out FaceOffsets outOffsets, string path, int level, string levelPrefix)
        {
            int size      = 0;
            int numLevels = 1;

            outBuffer  = null;
            outOffsets = new FaceOffsets();

            {
                string faceName = levelPrefix + FaceSuffix[0] + ".rgb32f";
                string facePath = Path.Combine(path, faceName);

                if (!File.Exists(facePath))
                {
                    Console.WriteLine("The face {0} does not exist", facePath);
                    return(false);
                }

                var imageInfo = ImageInfo.FromStream(File.OpenRead(facePath));

                if (imageInfo.Value.Width != imageInfo.Value.Height)
                {
                    Console.WriteLine("width != height");
                    return(false);
                }

                size      = imageInfo.Value.Width;
                numLevels = 1;

                if (string.IsNullOrEmpty(levelPrefix))
                {
                    numLevels = (int)MathF.Log2(size) + 1;
                }

                if (level == 0)
                {
                    texture = TextureBuilder.Create()
                              .WithWidth(size)
                              .WithHeight(size)
                              .WithLevels(numLevels)
                              .WithFormat(TextureFormat.R11F_G11F_B10F)
                              .WithSampler(TextureSamplerType.Cubemap)
                              .Build(_engine);
                }
            }

            // RGB_10_11_11_REV encoding: 4 bytes per pixel
            var faceSize = size * size * Marshal.SizeOf <uint>();

            FaceOffsets offsets = new FaceOffsets();

            byte[] pixelBuffer = new byte[faceSize * 6];
            var    success     = true;

            for (var j = 0; j < 6; j++)
            {
                outOffsets[j] = faceSize * j;

                string faceName = levelPrefix + FaceSuffix[j] + ".rgb32f";
                string facePath = Path.Combine(path, faceName);

                if (!File.Exists(facePath))
                {
                    Console.WriteLine("The face {0} does not exist", facePath);
                    success = false;
                    break;
                }

                var imageResult = ImageResult.FromStream(File.OpenRead(facePath), ColorComponents.RedGreenBlueAlpha);

                if (imageResult.Width != imageResult.Height || imageResult.Width != size)
                {
                    Console.WriteLine("Face {0} has a wrong size {1}x{2}, instead of {3}x{3}", faceName, imageResult.Width, imageResult.Height, size);
                    success = false;
                    break;
                }

                if (imageResult.SourceComp != ColorComponents.RedGreenBlueAlpha)
                {
                    Console.WriteLine("Could not decode face {0}", faceName);
                    success = false;
                    break;
                }

                Array.Copy(imageResult.Data, 0, pixelBuffer, offsets[j], imageResult.Width * imageResult.Height * Marshal.SizeOf <uint>());
            }

            if (!success)
            {
                return(false);
            }


            outBuffer = new PixelBufferDescriptor(pixelBuffer, PixelDataFormat.Rgb, PixelDataType.UInt_10F_11F_11F_Rev);

            return(true);
        }