public static int stbi__hdr_info(stbi__context s, int *x, int *y, int *comp)
        {
            var    buffer = stackalloc sbyte[1024];
            sbyte *token;
            var    valid = 0;
            var    dummy = 0;

            if (x == null)
            {
                x = &dummy;
            }
            if (y == null)
            {
                y = &dummy;
            }
            if (comp == null)
            {
                comp = &dummy;
            }
            if (stbi__hdr_test(s) == 0)
            {
                stbi__rewind(s);
                return(0);
            }

            for (; ;)
            {
                token = stbi__hdr_gettoken(s, buffer);
                if (token[0] == 0)
                {
                    break;
                }
                if (CRuntime.strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0)
                {
                    valid = 1;
                }
            }

            if (valid == 0)
            {
                stbi__rewind(s);
                return(0);
            }

            token = stbi__hdr_gettoken(s, buffer);
            if (CRuntime.strncmp(token, "-Y ", 3) != 0)
            {
                stbi__rewind(s);
                return(0);
            }

            token += 3;
            *y = (int)CRuntime.strtol(token, &token, 10);
            while (*token == 32)
            {
                ++token;
            }

            if (CRuntime.strncmp(token, "+X ", 3) != 0)
            {
                stbi__rewind(s);
                return(0);
            }

            token += 3;
            *x    = (int)CRuntime.strtol(token, null, 10);
            *comp = 3;
            return(1);
        }
        public static float *stbi__hdr_load(stbi__context s, int *x, int *y, int *comp, int req_comp,
                                            stbi__result_info *ri)
        {
            var    buffer = stackalloc sbyte[1024];
            sbyte *token;
            var    valid  = 0;
            var    width  = 0;
            var    height = 0;
            byte * scanline;
            float *hdr_data;
            var    len   = 0;
            byte   count = 0;
            byte   value = 0;
            var    i     = 0;
            var    j     = 0;
            var    k     = 0;
            var    c1    = 0;
            var    c2    = 0;
            var    z     = 0;
            sbyte *headerToken;

            headerToken = stbi__hdr_gettoken(s, buffer);
            if (CRuntime.strcmp(headerToken, "#?RADIANCE") != 0 && CRuntime.strcmp(headerToken, "#?RGBE") != 0)
            {
                return((float *)(ulong)(stbi__err("not HDR") != 0 ? 0 : 0));
            }
            for (; ;)
            {
                token = stbi__hdr_gettoken(s, buffer);
                if (token[0] == 0)
                {
                    break;
                }
                if (CRuntime.strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0)
                {
                    valid = 1;
                }
            }

            if (valid == 0)
            {
                return((float *)(ulong)(stbi__err("unsupported format") != 0 ? 0 : 0));
            }
            token = stbi__hdr_gettoken(s, buffer);
            if (CRuntime.strncmp(token, "-Y ", 3) != 0)
            {
                return((float *)(ulong)(stbi__err("unsupported data layout") != 0 ? 0 : 0));
            }
            token += 3;
            height = (int)CRuntime.strtol(token, &token, 10);
            while (*token == 32)
            {
                ++token;
            }

            if (CRuntime.strncmp(token, "+X ", 3) != 0)
            {
                return((float *)(ulong)(stbi__err("unsupported data layout") != 0 ? 0 : 0));
            }
            token += 3;
            width  = (int)CRuntime.strtol(token, null, 10);
            if (height > 1 << 24)
            {
                return((float *)(ulong)(stbi__err("too large") != 0 ? 0 : 0));
            }
            if (width > 1 << 24)
            {
                return((float *)(ulong)(stbi__err("too large") != 0 ? 0 : 0));
            }
            *x = width;
            *y = height;
            if (comp != null)
            {
                *comp = 3;
            }
            if (req_comp == 0)
            {
                req_comp = 3;
            }
            if (stbi__mad4sizes_valid(width, height, req_comp, sizeof(float), 0) == 0)
            {
                return((float *)(ulong)(stbi__err("too large") != 0 ? 0 : 0));
            }
            hdr_data = (float *)stbi__malloc_mad4(width, height, req_comp, sizeof(float), 0);
            if (hdr_data == null)
            {
                return((float *)(ulong)(stbi__err("outofmem") != 0 ? 0 : 0));
            }

main_decode_loop:
            var enterMainDecode = false;

            if (enterMainDecode)
            {
                for (; j < height; ++j)
                {
                    for (; i < width; ++i)
                    {
                        var rgbe = stackalloc byte[4];
                        stbi__getn(s, rgbe, 4);
                        stbi__hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp);
                    }
                }

                goto finish;
            }

            if (width < 8 || width >= 32768)
            {
                i = j = 0;

                enterMainDecode = true;
                goto main_decode_loop;
            }

            scanline = null;
            for (j = 0; j < height; ++j)
            {
                c1  = stbi__get8(s);
                c2  = stbi__get8(s);
                len = stbi__get8(s);
                if (c1 != 2 || c2 != 2 || (len & 0x80) != 0)
                {
                    var rgbe = stackalloc byte[4];
                    rgbe[0] = (byte)c1;
                    rgbe[1] = (byte)c2;
                    rgbe[2] = (byte)len;
                    rgbe[3] = stbi__get8(s);
                    stbi__hdr_convert(hdr_data, rgbe, req_comp);
                    i = 1;
                    j = 0;
                    CRuntime.free(scanline);
                    goto main_decode_loop;
                }

                len <<= 8;
                len  |= stbi__get8(s);
                if (len != width)
                {
                    CRuntime.free(hdr_data);
                    CRuntime.free(scanline);
                    return((float *)(ulong)(stbi__err("invalid decoded scanline length") != 0 ? 0 : 0));
                }

                if (scanline == null)
                {
                    scanline = (byte *)stbi__malloc_mad2(width, 4, 0);
                    if (scanline == null)
                    {
                        CRuntime.free(hdr_data);
                        return((float *)(ulong)(stbi__err("outofmem") != 0 ? 0 : 0));
                    }
                }

                for (k = 0; k < 4; ++k)
                {
                    var nleft = 0;
                    i = 0;
                    while ((nleft = width - i) > 0)
                    {
                        count = stbi__get8(s);
                        if (count > 128)
                        {
                            value  = stbi__get8(s);
                            count -= 128;
                            if (count > nleft)
                            {
                                CRuntime.free(hdr_data);
                                CRuntime.free(scanline);
                                return((float *)(ulong)(stbi__err("corrupt") != 0 ? 0 : 0));
                            }

                            for (z = 0; z < count; ++z)
                            {
                                scanline[i++ *4 + k] = value;
                            }
                        }
                        else
                        {
                            if (count > nleft)
                            {
                                CRuntime.free(hdr_data);
                                CRuntime.free(scanline);
                                return((float *)(ulong)(stbi__err("corrupt") != 0 ? 0 : 0));
                            }

                            for (z = 0; z < count; ++z)
                            {
                                scanline[i++ *4 + k] = stbi__get8(s);
                            }
                        }
                    }
                }

                for (i = 0; i < width; ++i)
                {
                    stbi__hdr_convert(hdr_data + (j * width + i) * req_comp, scanline + i * 4, req_comp);
                }
            }

            if (scanline != null)
            {
                CRuntime.free(scanline);
            }

finish:
            return(hdr_data);
        }