예제 #1
0
    static HdrFile ReadHdrFile(string path)
    {
        if (!File.Exists(path))
        {
            throw new System.Exception("File does not exist");
        }

        HdrFile hdr = new HdrFile();

        using (FileStream stream = new FileStream(path, FileMode.Open)) {
            using (StreamReader sr = new StreamReader(stream)) {
                string header = sr.ReadLine().Trim();
                Debug.Log(header);
                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]);
                Color32[] buffer = new Color32[width * height];

                hdr.SetDimensions(width, height);

                StringBuilder sb        = new StringBuilder();
                Color         lastColor = Color.black;
                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;
                    }

                    Color32 c = new Color32(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.SetColors(buffer);
            }
        }
        return(hdr);
    }