Beispiel #1
0
        /*
         #?RADIANCE
         # Made with FreeImage 3.9.3
         # FORMAT=32-bit_rle_rgbe
         # GAMMA=1
         # EXPOSURE=0
         #
         # -Y 800 +X 1600
         */
        private Texture LoadHdrEnvironmentMap(string filename)
        {
            HdrFile hdrFile = HdrAssetImporter.ReadHdrFile(filename);

            Pixel[] pixels = hdrFile.Colors;
            byte[]  bytes  = new byte[pixels.Length * 4];
            for (int i = 0; i < pixels.Length; i++)
            {
                //bytes[i * 4 + 0] = pixels[i].r;
                //bytes[i * 4 + 1] = pixels[i].g;
                //bytes[i * 4 + 2] = pixels[i].b;
                //bytes[i * 4 + 3] = pixels[i].a;
            }
            var storage = new TexImage2D(TexImage2D.Target.Texture2D, GL.GL_RGB16F, hdrFile.Width, hdrFile.Height, GL.GL_RGBA, GL.GL_BYTE, new ArrayDataProvider <byte>(bytes));
            var texture = new Texture(storage,
                                      new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_CLAMP_TO_EDGE),
                                      new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_CLAMP_TO_EDGE),
                                      new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_LINEAR),
                                      new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_LINEAR));

            texture.Initialize();

            var file = new FileInfo(filename);

            texture.GetImage(hdrFile.Width, hdrFile.Height).Save(
                string.Format("{0}.GetImage.png", file.Name));

            return(texture);
        }
Beispiel #2
0
        public static HdrFile ReadHdrFile(string path)
        {
            if (!File.Exists(path))
            {
                throw new System.Exception("File does not exist");
            }

            var hdr = new HdrFile();

            using (var stream = new FileStream(path, FileMode.Open))
            {
                using (var sr = new StreamReader(stream))
                {
                    string header = sr.ReadLine().Trim();
                    if (header != "#?RADIANCE")
                    {
                        throw new System.Exception("File is not in Radiance HDR format.");
                    }
                    while (!sr.EndOfStream)
                    {
                        string line = sr.ReadLine();
                        if (line.StartsWith("FORMAT"))
                        {
                            switch (line.Substring(7))
                            {
                            case "32-bit_rle_rgbe":
                                hdr.format = HdrFile.Format.RGBE;
                                break;

                            case "32-bit_rle_xyze":
                                hdr.format = HdrFile.Format.XYZE;
                                throw new System.Exception("XYZE format is not supported.");

                            default:
                                throw new System.Exception("HDR file is of an unknown format.");
                            }
                        }
                        // end of header
                        if (line == string.Empty)
                        {
                            break;
                        }
                    }
                    // Read the dimensions
                    string[] size = sr.ReadLine().Split(' ');
                    // FIXME assuming that it's -Y +X layout
                    int     width  = int.Parse(size[1]);
                    int     height = int.Parse(size[3]);
                    Pixel[] buffer = new Pixel[width * height];

                    hdr.SetDimensions(width, height);

                    var sb        = new StringBuilder();
                    var lastColor = new Pixel(0, 0, 0, 0);
                    int cursor    = 0;

                    while (cursor < buffer.Length)
                    {
                        byte[] rgbe = new byte[4];
                        stream.Read(rgbe, 0, 4);

                        if (width < 8 || width > 32767)
                        {
                            hdr.compression = HdrFile.Compression.Uncompressed;
                        }
                        else if (cursor == 0 && rgbe[0] == 2 && rgbe[1] == 2)
                        {
                            hdr.compression = HdrFile.Compression.AdaptiveRLE;
                        }

                        Pixel c = new Pixel(rgbe[0], rgbe[1], rgbe[2], rgbe[3]);
                        if (hdr.compression == HdrFile.Compression.AdaptiveRLE)
                        {
                            // TODO
                        }
                        else if (c.r == 255 && c.g == 255 && c.b == 255)
                        {
                            // Old run-length encoding
                            hdr.compression = HdrFile.Compression.RLE;
                            cursor++;
                            for (int i = 0; i < c.a; i++)
                            {
                                buffer[cursor] = lastColor;
                                cursor++;
                            }
                        }
                        else
                        {
                            buffer[cursor] = c;
                        }
                        lastColor = c;
                        cursor++;
                    }

                    hdr.Colors = buffer;
                }
            }
            return(hdr);
        }