/* Set the resolution of the HDR image */
 public void SetResolution(int w, int h)
 {
     width  = w;
     height = h;
     pixels = new HDRPixel[width * height];
     for (int i = 0; i < pixels.Length; i++)
     {
         pixels[i] = new HDRPixel(0, 0, 0, 0);
     }
 }
        /* Load in a HDR file */
        public void Open(string filename, bool can_reparse_hdr = true, bool can_reparse_exr = true)
        {
            //Check sanity of input
            if (!(Path.GetExtension(filename) == ".hdr" || Path.GetExtension(filename) == ".exr"))
            {
                throw new System.FormatException("Trying to load a non-HDR image!");
            }
            if (!File.Exists(filename))
            {
                throw new System.FormatException("Requested to open a file that doesn't exist!");
            }

            //If given an EXR, we need it in HDR format
            if (Path.GetExtension(filename) == ".exr")
            {
                if (!can_reparse_exr)
                {
                    throw new System.FormatException("Failed to load HDR, incorrect format!");
                }
                string HDRConverterPath = AppDomain.CurrentDomain.BaseDirectory + Properties.Resources.Library_EXR2HDR;
                File.Copy(filename, HDRConverterPath + "input.exr", true);
                ProcessStartInfo processInfo = new ProcessStartInfo("cmd.exe", "/c \"" + HDRConverterPath + "run.bat\"");
                processInfo.WorkingDirectory = HDRConverterPath;
                processInfo.CreateNoWindow   = true;
                processInfo.UseShellExecute  = false;
                Process process = Process.Start(processInfo);
                process.WaitForExit();
                process.Close();
                File.Delete(HDRConverterPath + "input.exr");
                if (!File.Exists(HDRConverterPath + "output.hdr"))
                {
                    throw new System.FormatException("Failed to convert EXR to correct format!");
                }
                Open(HDRConverterPath + "output.hdr", true, false);
                File.Delete(HDRConverterPath + "output.hdr");
                return;
            }

            BinaryReader InFile = new BinaryReader(File.OpenRead(filename));

            //Read the header
            string thisHeaderLine = "";

            while (true)
            {
                byte thisByte = InFile.ReadByte();
                if (thisByte == 0x0A)
                {
                    if (thisHeaderLine.Length >= 6 && thisHeaderLine.Substring(0, 6).ToUpper() == "FORMAT")
                    {
                        if (thisHeaderLine.Substring(7) != "32-bit_rle_rgbe")
                        {
                            throw new System.FormatException("Can only read 32-bit RGBE HDR images!");
                        }
                    }
                    else if (thisHeaderLine.Length >= 2 && thisHeaderLine.Substring(0, 2).ToUpper() == "-Y")
                    {
                        string[] sizeData = thisHeaderLine.Split(' ');
                        int      Y        = Convert.ToInt32(sizeData[1]);
                        int      X        = Convert.ToInt32(sizeData[3]);
                        SetResolution(X, Y);
                        break; //Resolution should always be last param!
                    }
                    thisHeaderLine = "";
                }
                else
                {
                    thisHeaderLine += (char)thisByte;
                }
            }
            int headerLen = (int)InFile.BaseStream.Position;

            //Some HDRs use scanline compression - convert it for us, and try again
            if (InFile.BaseStream.Length < (width * height * 4))
            {
                if (!can_reparse_hdr)
                {
                    throw new System.FormatException("Failed to load HDR, incorrect format!");
                }
                string HDRConverterPath = AppDomain.CurrentDomain.BaseDirectory + Properties.Resources.Library_HDRConverter;
                File.Copy(filename, HDRConverterPath + "input.hdr", true);
                ProcessStartInfo processInfo = new ProcessStartInfo("cmd.exe", "/c \"" + HDRConverterPath + "run.bat\"");
                processInfo.WorkingDirectory = HDRConverterPath;
                processInfo.CreateNoWindow   = true;
                processInfo.UseShellExecute  = false;
                Process process = Process.Start(processInfo);
                process.WaitForExit();
                process.Close();
                File.Delete(HDRConverterPath + "input.hdr");
                if (!File.Exists(HDRConverterPath + "output.hdr"))
                {
                    throw new System.FormatException("Failed to convert HDR to correct format!");
                }
                Open(HDRConverterPath + "output.hdr", false);
                File.Delete(HDRConverterPath + "output.hdr");
                InFile.Close();
                return;
            }

            //Read RGBE data
            for (int i = 0; i < (InFile.BaseStream.Length - headerLen) / 4; i++)
            {
                HDRPixel newPixel = new HDRPixel();
                byte[]   values   = InFile.ReadBytes(4);
                newPixel.R = (int)values[0];
                newPixel.G = (int)values[1];
                newPixel.B = (int)values[2];
                newPixel.E = (int)values[3];
                pixels[i]  = newPixel;
            }

            InFile.Close();
        }
 /* Set a pixel by X,Y position */
 public void SetPixel(int x, int y, HDRPixel p)
 {
     pixels[(y * Width) + x] = p;
 }