コード例 #1
0
 private void ReadRgbe(System.IO.Stream src, Vector3Half[] ret)
 {
     for (int i = 0; i < ret.Length; i++)
     {
         var item = new Rgbe(src);
         ret[i] = item.ToVector3Half();
     }
 }
コード例 #2
0
        /*
         *      Retrieved from the source code written by Greg Ward for reading Radiance HDR images.
         */
        private void ReadRgbeRle(System.IO.Stream src, Vector3Half[] ret, int width, int height)
        {
            byte[] rgbe = new byte[4]; byte[] scanline_buffer; int ptr, ptr_end;
            int    i, count, index = 0;

            byte[] buf = new byte[2];

            if ((width < 8) || (width > 0x7fff))
            {
                /* run length encoding is not allowed so read flat*/
                ReadRgbe(src, ret);
                return;
            }
            scanline_buffer = null;
            int num_scanlines = height;
            /* read in each successive scanline */
            long start = src.Position;

            while (num_scanlines > 0)
            {
                if (src.Read(rgbe, 0, 4) < 1)
                {
                    throw new FormatException();
                }
                if ((rgbe[0] != 2) || (rgbe[1] != 2) || ((rgbe[2] & 0x80) == 0x80))
                {
                    /* this file is not run length encoded */
                    //rgbe2float(&data[0],&data[1],&data[2],rgbe);
                    //data += RGBE_DATA_SIZE;
                    //free(scanline_buffer);
                    //return RGBE_ReadPixels(fp,data,scanline_width*num_scanlines-1);
                    //src.Position = start;
                    //ReadRgbe(src, ret);
                    //return;
                }
                //if ((((int)rgbe[2]) << 8 | rgbe[3]) != width)
                //{
//					throw new FormatException();
                //}
                if (scanline_buffer == null)
                {
                    scanline_buffer = new byte[4 * width];
                }

                ptr = 0;
                /* read each of the four channels for the scanline into the buffer */
                for (i = 0; i < 4; i++)
                {
                    ptr_end = (i + 1) * width;
                    while (ptr < ptr_end)
                    {
                        if (src.Read(buf, 0, 2) < 1)
                        {
                            throw new EndOfStreamException();
                        }
                        if (buf[0] > 128)
                        {
                            /* a run of the same value */
                            count = buf[0] - 128;
                            if ((count == 0) || (count > ptr_end - ptr))
                            {
                                throw new FormatException();
                            }
                            while (count-- > 0)
                            {
                                scanline_buffer[ptr++] = buf[1];
                            }
                        }
                        else
                        {
                            /* a non-run */
                            count = buf[0];
                            if ((count == 0) || (count > ptr_end - ptr))
                            {
                                throw new FormatException();
                            }
                            scanline_buffer[ptr++] = buf[1];
                            if (--count > 0)
                            {
                                if (src.Read(scanline_buffer, ptr, count) < 1)
                                {
                                    throw new EndOfStreamException();
                                }
                                ptr += count;
                            }
                        }
                    }
                }
                /* now convert data from buffer into floats */

                for (i = 0; i < width; i++)
                {
                    Rgbe r = new Rgbe()
                    {
                        red   = scanline_buffer[i],
                        green = scanline_buffer[i + width],
                        blue  = scanline_buffer[i + 2 * width],
                        exp   = scanline_buffer[i + 3 * width]
                    };
                    ret[index] = r.ToVector3Half();
                }
                num_scanlines--;
            }
        }