public static void *stbi__load_main(stbi__context s, int *x, int *y, int *comp, int req_comp,
                                            stbi__result_info *ri, int bpc)
        {
            CRuntime.memset(ri, 0, (ulong)sizeof(stbi__result_info));
            ri->bits_per_channel = 8;
            ri->channel_order    = STBI_ORDER_RGB;
            ri->num_channels     = 0;
            if (stbi__png_test(s) != 0)
            {
                return(stbi__png_load(s, x, y, comp, req_comp, ri));
            }
            if (stbi__bmp_test(s) != 0)
            {
                return(stbi__bmp_load(s, x, y, comp, req_comp, ri));
            }
            if (stbi__gif_test(s) != 0)
            {
                return(stbi__gif_load(s, x, y, comp, req_comp, ri));
            }
            if (stbi__psd_test(s) != 0)
            {
                return(stbi__psd_load(s, x, y, comp, req_comp, ri, bpc));
            }
            if (stbi__jpeg_test(s) != 0)
            {
                return(stbi__jpeg_load(s, x, y, comp, req_comp, ri));
            }
            if (stbi__hdr_test(s) != 0)
            {
                var hdr = stbi__hdr_load(s, x, y, comp, req_comp, ri);
                return(stbi__hdr_to_ldr(hdr, *x, *y, req_comp != 0 ? req_comp : *comp));
            }

            if (stbi__tga_test(s) != 0)
            {
                return(stbi__tga_load(s, x, y, comp, req_comp, ri));
            }
            return((byte *)(ulong)(stbi__err("unknown image type") != 0 ? 0 : 0));
        }
Пример #2
0
        public static byte *stbi__gif_load_next(stbi__context s, stbi__gif g, int *comp, int req_comp, byte *two_back)
        {
            var dispose     = 0;
            var first_frame = 0;
            var pi          = 0;
            var pcount      = 0;

            first_frame = 0;
            if (g._out_ == null)
            {
                if (stbi__gif_header(s, g, comp, 0) == 0)
                {
                    return(null);
                }
                if (stbi__mad3sizes_valid(4, g.w, g.h, 0) == 0)
                {
                    return((byte *)(ulong)(stbi__err("too large") != 0 ? 0 : 0));
                }
                pcount       = g.w * g.h;
                g._out_      = (byte *)stbi__malloc((ulong)(4 * pcount));
                g.background = (byte *)stbi__malloc((ulong)(4 * pcount));
                g.history    = (byte *)stbi__malloc((ulong)pcount);
                if (g._out_ == null || g.background == null || g.history == null)
                {
                    return((byte *)(ulong)(stbi__err("outofmem") != 0 ? 0 : 0));
                }
                CRuntime.memset(g._out_, 0x00, (ulong)(4 * pcount));
                CRuntime.memset(g.background, 0x00, (ulong)(4 * pcount));
                CRuntime.memset(g.history, 0x00, (ulong)pcount);
                first_frame = 1;
            }
            else
            {
                dispose = (g.eflags & 0x1C) >> 2;
                pcount  = g.w * g.h;
                if (dispose == 3 && two_back == null)
                {
                    dispose = 2;
                }

                if (dispose == 3)
                {
                    for (pi = 0; pi < pcount; ++pi)
                    {
                        if (g.history[pi] != 0)
                        {
                            CRuntime.memcpy(&g._out_[pi * 4], &two_back[pi * 4], (ulong)4);
                        }
                    }
                }
                else if (dispose == 2)
                {
                    for (pi = 0; pi < pcount; ++pi)
                    {
                        if (g.history[pi] != 0)
                        {
                            CRuntime.memcpy(&g._out_[pi * 4], &g.background[pi * 4], (ulong)4);
                        }
                    }
                }

                CRuntime.memcpy(g.background, g._out_, (ulong)(4 * g.w * g.h));
            }

            CRuntime.memset(g.history, 0x00, (ulong)(g.w * g.h));
            for (; ;)
            {
                int tag = stbi__get8(s);
                switch (tag)
                {
                case 0x2C:
                {
                    var   x = 0;
                    var   y = 0;
                    var   w = 0;
                    var   h = 0;
                    byte *o;
                    x = stbi__get16le(s);
                    y = stbi__get16le(s);
                    w = stbi__get16le(s);
                    h = stbi__get16le(s);
                    if (x + w > g.w || y + h > g.h)
                    {
                        return((byte *)(ulong)(stbi__err("bad Image Descriptor") != 0 ? 0 : 0));
                    }
                    g.line_size = g.w * 4;
                    g.start_x   = x * 4;
                    g.start_y   = y * g.line_size;
                    g.max_x     = g.start_x + w * 4;
                    g.max_y     = g.start_y + h * g.line_size;
                    g.cur_x     = g.start_x;
                    g.cur_y     = g.start_y;
                    if (w == 0)
                    {
                        g.cur_y = g.max_y;
                    }
                    g.lflags = stbi__get8(s);
                    if ((g.lflags & 0x40) != 0)
                    {
                        g.step  = 8 * g.line_size;
                        g.parse = 3;
                    }
                    else
                    {
                        g.step  = g.line_size;
                        g.parse = 0;
                    }

                    if ((g.lflags & 0x80) != 0)
                    {
                        stbi__gif_parse_colortable(s, g.lpal, 2 << (g.lflags & 7),
                                                   (g.eflags & 0x01) != 0 ? g.transparent : -1);
                        g.color_table = (byte *)g.lpal;
                    }
                    else if ((g.flags & 0x80) != 0)
                    {
                        g.color_table = (byte *)g.pal;
                    }
                    else
                    {
                        return((byte *)(ulong)(stbi__err("missing color table") != 0 ? 0 : 0));
                    }

                    o = stbi__process_gif_raster(s, g);
                    if (o == null)
                    {
                        return(null);
                    }
                    pcount = g.w * g.h;
                    if (first_frame != 0 && g.bgindex > 0)
                    {
                        for (pi = 0; pi < pcount; ++pi)
                        {
                            if (g.history[pi] == 0)
                            {
                                g.pal[g.bgindex][3] = 255;
                                CRuntime.memcpy(&g._out_[pi * 4], &g.pal[g.bgindex], (ulong)4);
                            }
                        }
                    }

                    return(o);
                }

                case 0x21:
                {
                    var len = 0;
                    int ext = stbi__get8(s);
                    if (ext == 0xF9)
                    {
                        len = stbi__get8(s);
                        if (len == 4)
                        {
                            g.eflags = stbi__get8(s);
                            g.delay  = 10 * stbi__get16le(s);
                            if (g.transparent >= 0)
                            {
                                g.pal[g.transparent][3] = 255;
                            }

                            if ((g.eflags & 0x01) != 0)
                            {
                                g.transparent = stbi__get8(s);
                                if (g.transparent >= 0)
                                {
                                    g.pal[g.transparent][3] = 0;
                                }
                            }
                            else
                            {
                                stbi__skip(s, 1);
                                g.transparent = -1;
                            }
                        }
                        else
                        {
                            stbi__skip(s, len);
                            break;
                        }
                    }

                    while ((len = stbi__get8(s)) != 0)
                    {
                        stbi__skip(s, len);
                    }

                    break;
                }

                case 0x3B:
                    return(null);

                default:
                    return((byte *)(ulong)(stbi__err("unknown code") != 0 ? 0 : 0));
                }
            }
        }
Пример #3
0
        public static int stbi__zbuild_huffman(stbi__zhuffman *z, byte *sizelist, int num)
        {
            var i         = 0;
            var k         = 0;
            var code      = 0;
            var next_code = stackalloc int[16];
            var sizes     = stackalloc int[17];

            CRuntime.memset(sizes, 0, (ulong)sizeof(int));
            CRuntime.memset(z->fast, 0, (ulong)((1 << 9) * sizeof(ushort)));
            for (i = 0; i < num; ++i)
            {
                ++sizes[sizelist[i]];
            }
            sizes[0] = 0;
            for (i = 1; i < 16; ++i)
            {
                if (sizes[i] > 1 << i)
                {
                    return(stbi__err("bad sizes"));
                }
            }
            code = 0;
            for (i = 1; i < 16; ++i)
            {
                next_code[i]      = code;
                z->firstcode[i]   = (ushort)code;
                z->firstsymbol[i] = (ushort)k;
                code = code + sizes[i];
                if (sizes[i] != 0)
                {
                    if (code - 1 >= 1 << i)
                    {
                        return(stbi__err("bad codelengths"));
                    }
                }
                z->maxcode[i] = code << (16 - i);
                code        <<= 1;
                k            += sizes[i];
            }

            z->maxcode[16] = 0x10000;
            for (i = 0; i < num; ++i)
            {
                var s = (int)sizelist[i];
                if (s != 0)
                {
                    var c     = next_code[s] - z->firstcode[s] + z->firstsymbol[s];
                    var fastv = (ushort)((s << 9) | i);
                    z->size[c]  = (byte)s;
                    z->value[c] = (ushort)i;
                    if (s <= 9)
                    {
                        var j = stbi__bit_reverse(next_code[s], s);
                        while (j < 1 << 9)
                        {
                            z->fast[j] = fastv;
                            j         += 1 << s;
                        }
                    }

                    ++next_code[s];
                }
            }

            return(1);
        }
Пример #4
0
        public static int stbi__compute_huffman_codes(stbi__zbuf *a)
        {
            var z_codelength     = new stbi__zhuffman();
            var lencodes         = stackalloc byte[286 + 32 + 137];
            var codelength_sizes = stackalloc byte[19];
            var i     = 0;
            var n     = 0;
            var hlit  = (int)(stbi__zreceive(a, 5) + 257);
            var hdist = (int)(stbi__zreceive(a, 5) + 1);
            var hclen = (int)(stbi__zreceive(a, 4) + 4);
            var ntot  = hlit + hdist;

            CRuntime.memset(codelength_sizes, 0, (ulong)(19 * sizeof(byte)));
            for (i = 0; i < hclen; ++i)
            {
                var s = (int)stbi__zreceive(a, 3);
                codelength_sizes[length_dezigzag[i]] = (byte)s;
            }

            if (stbi__zbuild_huffman(&z_codelength, codelength_sizes, 19) == 0)
            {
                return(0);
            }
            n = 0;
            while (n < ntot)
            {
                var c = stbi__zhuffman_decode(a, &z_codelength);
                if (c < 0 || c >= 19)
                {
                    return(stbi__err("bad codelengths"));
                }
                if (c < 16)
                {
                    lencodes[n++] = (byte)c;
                }
                else
                {
                    var fill = (byte)0;
                    if (c == 16)
                    {
                        c = (int)(stbi__zreceive(a, 2) + 3);
                        if (n == 0)
                        {
                            return(stbi__err("bad codelengths"));
                        }
                        fill = lencodes[n - 1];
                    }
                    else if (c == 17)
                    {
                        c = (int)(stbi__zreceive(a, 3) + 3);
                    }
                    else
                    {
                        c = (int)(stbi__zreceive(a, 7) + 11);
                    }

                    if (ntot - n < c)
                    {
                        return(stbi__err("bad codelengths"));
                    }
                    CRuntime.memset(lencodes + n, fill, (ulong)c);
                    n += c;
                }
            }

            if (n != ntot)
            {
                return(stbi__err("bad codelengths"));
            }
            if (stbi__zbuild_huffman(&a->z_length, lencodes, hlit) == 0)
            {
                return(0);
            }
            if (stbi__zbuild_huffman(&a->z_distance, lencodes + hlit, hdist) == 0)
            {
                return(0);
            }
            return(1);
        }