Exemple #1
0
 private static float[] stbi__loadf_main(stbi__context s, out int x, out int y, out int comp, int req_comp)
 {
     char[] data;
     ////#ifndef STBI_NO_HDR
     //if (stbi__hdr_test(s))
     //{
     //    stbi__result_info ri;
     //    float* hdr_data = stbi__hdr_load(s, x, y, comp, req_comp, &ri);
     //    if (hdr_data)
     //        stbi__float_postprocess(hdr_data, x, y, comp, req_comp);
     //    return hdr_data;
     //}
     ////#endif
     //data = stbi__load_and_postprocess_8bit(s, x, y, comp, req_comp);
     //if (data)
     //    return stbi__ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);
     //return stbi__errpf("unknown image type", "Image not of any known type, or corrupt");
     //throw new NotImplementedException();
     if (stbi__hdr_test(s))
     {
         var     ri       = new stbi__result_info();
         float[] hdr_data = stbi__hdr_load(s, x, y, comp, req_comp, ri);
         if (hdr_data)
         {
             stbi__float_postprocess(hdr_data, x, y, comp, req_comp);
         }
         return(hdr_data);
     }
     //#endif
     data = stbi__load_and_postprocess_8bit(s, x, y, comp, req_comp);
     return(stbi__ldr_to_hdr(data, x, y, (req_comp ? req_comp : comp)));
 }
        public static byte *stbi__load_and_postprocess_8bit(stbi__context s, int *x, int *y, int *comp, int req_comp)
        {
            var ri     = new stbi__result_info();
            var result = stbi__load_main(s, x, y, comp, req_comp, &ri, 8);

            if (result == null)
            {
                return(null);
            }
            if (ri.bits_per_channel != 8)
            {
                result = stbi__convert_16_to_8((ushort *)result, *x, *y, req_comp == 0 ? *comp : req_comp);
                ri.bits_per_channel = 8;
            }

            if ((stbi__vertically_flip_on_load_set != 0
                                ? stbi__vertically_flip_on_load_local
                                : stbi__vertically_flip_on_load_global) != 0)
            {
                var channels = req_comp != 0 ? req_comp : *comp;
                stbi__vertical_flip(result, *x, *y, channels * sizeof(byte));
            }

            return((byte *)result);
        }
Exemple #3
0
                    public static ushort *stbi__load_and_postprocess_16bit(stbi__context s, int *x, int *y, int *comp, int req_comp)
                    {
                        var ri     = new stbi__result_info();
                        var result = stbi__load_main(s, x, y, comp, req_comp, &ri, 16);

                        if (result == null)
                        {
                            return(null);
                        }
                        if (ri.bits_per_channel != 16)
                        {
                            result = stbi__convert_8_to_16((byte *)result, *x, *y, (req_comp == 0) ? *comp : req_comp);
                            ri.bits_per_channel = 16;
                        }
                        if (stbi__vertically_flip_on_load != 0)
                        {
                            var channels = (req_comp != 0) ? req_comp : *comp;
                            stbi__vertical_flip(result, *x, *y, channels * sizeof(ushort));
                        }
                        return((ushort *)result);
                    }
        public static float *stbi__loadf_main(stbi__context s, int *x, int *y, int *comp, int req_comp)
        {
            byte *data;

            if (stbi__hdr_test(s) != 0)
            {
                var ri       = new stbi__result_info();
                var hdr_data = stbi__hdr_load(s, x, y, comp, req_comp, &ri);
                if (hdr_data != null)
                {
                    stbi__float_postprocess(hdr_data, x, y, comp, req_comp);
                }
                return(hdr_data);
            }

            data = stbi__load_and_postprocess_8bit(s, x, y, comp, req_comp);
            if (data != null)
            {
                return(stbi__ldr_to_hdr(data, *x, *y, req_comp != 0 ? req_comp : *comp));
            }
            return((float *)(ulong)(stbi__err("unknown image type") != 0 ? (byte *)null : null));
        }
Exemple #5
0
        private static float[] stbi__hdr_load(stbi__context s, int x, int y, int comp, int req_comp, stbi__result_info ri)
        {
            var buffer = new char[STBI__HDR_BUFLEN];

            char[] token;
            int    valid = 0;
            int    width, height;

            char[]  scanline;
            float[] hdr_data;
            int     len;
            char    count, value;
            int     i, j, k, c1, c2, z;

            char[] headerToken;
            //STBI_NOTUSED(ri);

            // Check identifier
            headerToken = stbi__hdr_gettoken(s, buffer);
            //if (strcmp(headerToken, "#?RADIANCE") != 0 && strcmp(headerToken, "#?RGBE") != 0)
            //return stbi__errpf("not HDR", "Corrupt HDR image");

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

            // Parse width and height
            // can't use sscanf() if we're not using stdio!
            token = stbi__hdr_gettoken(s, buffer);
            if (strncmp(token, "-Y ", 3))
            {
                return(stbi__errpf("unsupported data layout", "Unsupported HDR format"));
            }
            token += 3;
            height = (int)strtol(token, &token, 10);
            while (*token == ' ')
            {
                ++token;
            }
            if (strncmp(token, "+X ", 3))
            {
                return(stbi__errpf("unsupported data layout", "Unsupported HDR format"));
            }
            token += 3;
            width  = (int)strtol(token, NULL, 10);

            *x = width;
            *y = height;

            if (comp)
            {
                *comp = 3;
            }
            if (req_comp == 0)
            {
                req_comp = 3;
            }

            if (!stbi__mad4sizes_valid(width, height, req_comp, sizeof(float), 0))
            {
                return(stbi__errpf("too large", "HDR image is too large"));
            }

            // Read data
            hdr_data = (float *)stbi__malloc_mad4(width, height, req_comp, sizeof(float), 0);
            if (!hdr_data)
            {
                return(stbi__errpf("outofmem", "Out of memory"));
            }

            // Load image data
            // image data is stored as some number of sca
            // Read RLE-encoded data
            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))
                {
                    // not run-length encoded, so we have to actually use THIS data as a decoded
                    // pixel (note this can't be a valid pixel--one of RGB must be >= 128)
                    var rgbe = new char[4];
                    rgbe[0] = (char)c1;
                    rgbe[1] = (char)c2;
                    rgbe[2] = (char)len;
                    rgbe[3] = (char)stbi__get8(s);
                    stbi__hdr_convert(hdr_data, rgbe, req_comp);
                    i = 1;
                    j = 0;
                    STBI_FREE(scanline);
                    goto main_decode_loop; // yes, this makes no sense
                }
                len <<= 8;
                len  |= stbi__get8(s);
                if (len != width)
                {
                    STBI_FREE(hdr_data); STBI_FREE(scanline); return(stbi__errpf("invalid decoded scanline length", "corrupt HDR"));
                }
                if (scanline == NULL)
                {
                    scanline = (char *)stbi__malloc_mad2(width, 4, 0);
                    if (!scanline)
                    {
                        STBI_FREE(hdr_data);
                        return(stbi__errpf("outofmem", "Out of memory"));
                    }
                }

                for (k = 0; k < 4; ++k)
                {
                    int nleft;
                    i = 0;
                    while ((nleft = width - i) > 0)
                    {
                        count = stbi__get8(s);
                        if (count > 128)
                        {
                            // Run
                            value  = stbi__get8(s);
                            count -= 128;
                            if (count > nleft)
                            {
                                STBI_FREE(hdr_data); STBI_FREE(scanline); return(stbi__errpf("corrupt", "bad RLE data in HDR"));
                            }
                            for (z = 0; z < count; ++z)
                            {
                                scanline[i++ *4 + k] = value;
                            }
                        }
                        else
                        {
                            // Dump
                            if (count > nleft)
                            {
                                STBI_FREE(hdr_data); STBI_FREE(scanline); return(stbi__errpf("corrupt", "bad RLE data in HDR"));
                            }
                            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)
            {
                STBI_FREE(scanline);
            }

            return(hdr_data);
        }