public static sbyte *stbi_zlib_decode_noheader_malloc(sbyte *buffer, int len, int *outlen)
                    {
                        var a = new stbi__zbuf();
                        var p = (sbyte *)stbi__malloc((ulong)16384);

                        if (p == null)
                        {
                            return(null);
                        }
                        a.zbuffer     = (byte *)buffer;
                        a.zbuffer_end = (byte *)buffer + len;
                        if (stbi__do_zlib(&a, p, 16384, 1, 0) != 0)
                        {
                            if (outlen != null)
                            {
                                *outlen = (int)(a.zout - a.zout_start);
                            }
                            return(a.zout_start);
                        }
                        CRuntime.free(a.zout_start);
                        return(null);
                    }
Esempio n. 2
0
        public int InsertNode(int idx, int x, int y, int w)
        {
            if (NodesNumber + 1 > NodesCount)
            {
                NodesCount = NodesCount == 0 ? 8 : NodesCount * 2;
                Nodes      = (FontAtlasNode *)CRuntime.realloc(Nodes, (ulong)(sizeof(FontAtlasNode) * NodesCount));
                if (Nodes == null)
                {
                    return(0);
                }
            }

            for (var i = NodesNumber; i > idx; i--)
            {
                Nodes[i] = Nodes[i - 1];
            }
            Nodes[idx].X     = (short)x;
            Nodes[idx].Y     = (short)y;
            Nodes[idx].Width = (short)w;
            NodesNumber++;
            return(1);
        }
                    public static sbyte *stbi_zlib_decode_malloc_guesssize_headerflag(sbyte *buffer, int len, int initial_size, int *outlen, int parse_header)
                    {
                        var a = new stbi__zbuf();
                        var p = (sbyte *)stbi__malloc((ulong)initial_size);

                        if (p == null)
                        {
                            return(null);
                        }
                        a.zbuffer     = (byte *)buffer;
                        a.zbuffer_end = (byte *)buffer + len;
                        if (stbi__do_zlib(&a, p, initial_size, 1, parse_header) != 0)
                        {
                            if (outlen != null)
                            {
                                *outlen = (int)(a.zout - a.zout_start);
                            }
                            return(a.zout_start);
                        }
                        CRuntime.free(a.zout_start);
                        return(null);
                    }
Esempio n. 4
0
                    public static void stbi__vertical_flip(void *image, int w, int h, int bytes_per_pixel)
                    {
                        var row           = 0;
                        var bytes_per_row = (ulong)(w * bytes_per_pixel);
                        var temp          = stackalloc byte[2048];
                        var bytes         = (byte *)image;

                        for (row = 0; row < h >> 1; row++)
                        {
                            var row0       = bytes + (ulong)row * bytes_per_row;
                            var row1       = bytes + (ulong)(h - row - 1) * bytes_per_row;
                            var bytes_left = bytes_per_row;
                            while (bytes_left != 0)
                            {
                                var bytes_copy = (bytes_left < 2048) ? bytes_left : 2048;
                                CRuntime.memcpy(temp, row0, bytes_copy);
                                CRuntime.memcpy(row0, row1, bytes_copy);
                                CRuntime.memcpy(row1, temp, bytes_copy);
                                row0       += bytes_copy;
                                row1       += bytes_copy;
                                bytes_left -= bytes_copy;
                            }
                        }
                    }
Esempio n. 5
0
                    public static void *stbi__psd_load(stbi__context s, int *x, int *y, int *comp,
                                                       int req_comp, stbi__result_info *ri, int bpc)
                    {
                        var   pixelCount   = 0;
                        var   channelCount = 0;
                        var   compression  = 0;
                        var   channel      = 0;
                        var   i            = 0;
                        var   bitdepth     = 0;
                        var   w            = 0;
                        var   h            = 0;
                        byte *_out_;

                        if (stbi__get32be(s) != 0x38425053)
                        {
                            return((byte *)(ulong)((stbi__err("not PSD") != 0) ? (byte *)null : null));
                        }
                        if (stbi__get16be(s) != 1)
                        {
                            return((byte *)(ulong)((stbi__err("wrong version") != 0) ? (byte *)null : null));
                        }
                        stbi__skip(s, 6);
                        channelCount = stbi__get16be(s);
                        if ((channelCount < 0) || (channelCount > 16))
                        {
                            return((byte *)(ulong)((stbi__err("wrong channel count") != 0) ? (byte *)null : null));
                        }
                        h        = (int)stbi__get32be(s);
                        w        = (int)stbi__get32be(s);
                        bitdepth = stbi__get16be(s);
                        if ((bitdepth != 8) && (bitdepth != 16))
                        {
                            return((byte *)(ulong)((stbi__err("unsupported bit depth") != 0) ? (byte *)null : null));
                        }
                        if (stbi__get16be(s) != 3)
                        {
                            return((byte *)(ulong)((stbi__err("wrong color format") != 0) ? (byte *)null : null));
                        }
                        stbi__skip(s, (int)stbi__get32be(s));
                        stbi__skip(s, (int)stbi__get32be(s));
                        stbi__skip(s, (int)stbi__get32be(s));
                        compression = stbi__get16be(s);
                        if (compression > 1)
                        {
                            return((byte *)(ulong)((stbi__err("bad compression") != 0) ? (byte *)null : null));
                        }
                        if (stbi__mad3sizes_valid(4, w, h, 0) == 0)
                        {
                            return((byte *)(ulong)((stbi__err("too large") != 0) ? (byte *)null : null));
                        }
                        if ((compression == 0) && (bitdepth == 16) && (bpc == 16))
                        {
                            _out_ = (byte *)stbi__malloc_mad3(8, w, h, 0);
                            ri->bits_per_channel = 16;
                        }
                        else
                        {
                            _out_ = (byte *)stbi__malloc((ulong)(4 * w * h));
                        }
                        if (_out_ == null)
                        {
                            return((byte *)(ulong)((stbi__err("outofmem") != 0) ? (byte *)null : null));
                        }
                        pixelCount = w * h;
                        if (compression != 0)
                        {
                            stbi__skip(s, h * channelCount * 2);
                            for (channel = 0; channel < 4; channel++)
                            {
                                byte *p;
                                p = _out_ + channel;
                                if (channel >= channelCount)
                                {
                                    for (i = 0; i < pixelCount; i++, p += 4)
                                    {
                                        *p = (byte)((channel == 3) ? 255 : 0);
                                    }
                                }
                                else
                                {
                                    if (stbi__psd_decode_rle(s, p, pixelCount) == 0)
                                    {
                                        CRuntime.free(_out_);
                                        return((byte *)(ulong)((stbi__err("corrupt") != 0) ? (byte *)null : null));
                                    }
                                }
                            }
                        }
                        else
                        {
                            for (channel = 0; channel < 4; channel++)
                            {
                                if (channel >= channelCount)
                                {
                                    if ((bitdepth == 16) && (bpc == 16))
                                    {
                                        var q   = (ushort *)_out_ + channel;
                                        var val = (ushort)((channel == 3) ? 65535 : 0);
                                        for (i = 0; i < pixelCount; i++, q += 4)
                                        {
                                            *q = val;
                                        }
                                    }
                                    else
                                    {
                                        var p   = _out_ + channel;
                                        var val = (byte)((channel == 3) ? 255 : 0);
                                        for (i = 0; i < pixelCount; i++, p += 4)
                                        {
                                            *p = val;
                                        }
                                    }
                                }
                                else
                                {
                                    if (ri->bits_per_channel == 16)
                                    {
                                        var q = (ushort *)_out_ + channel;
                                        for (i = 0; i < pixelCount; i++, q += 4)
                                        {
                                            *q = (ushort)stbi__get16be(s);
                                        }
                                    }
                                    else
                                    {
                                        var p = _out_ + channel;
                                        if (bitdepth == 16)
                                        {
                                            for (i = 0; i < pixelCount; i++, p += 4)
                                            {
                                                *p = (byte)(stbi__get16be(s) >> 8);
                                            }
                                        }
                                        else
                                        {
                                            for (i = 0; i < pixelCount; i++, p += 4)
                                            {
                                                *p = stbi__get8(s);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (channelCount >= 4)
                        {
                            if (ri->bits_per_channel == 16)
                            {
                                for (i = 0; i < w * h; ++i)
                                {
                                    var pixel = (ushort *)_out_ + 4 * i;
                                    if ((pixel[3] != 0) && (pixel[3] != 65535))
                                    {
                                        var a     = pixel[3] / 65535.0f;
                                        var ra    = 1.0f / a;
                                        var inv_a = 65535.0f * (1 - ra);
                                        pixel[0] = (ushort)(pixel[0] * ra + inv_a);
                                        pixel[1] = (ushort)(pixel[1] * ra + inv_a);
                                        pixel[2] = (ushort)(pixel[2] * ra + inv_a);
                                    }
                                }
                            }
                            else
                            {
                                for (i = 0; i < w * h; ++i)
                                {
                                    var pixel = _out_ + 4 * i;
                                    if ((pixel[3] != 0) && (pixel[3] != 255))
                                    {
                                        var a     = pixel[3] / 255.0f;
                                        var ra    = 1.0f / a;
                                        var inv_a = 255.0f * (1 - ra);
                                        pixel[0] = (byte)(pixel[0] * ra + inv_a);
                                        pixel[1] = (byte)(pixel[1] * ra + inv_a);
                                        pixel[2] = (byte)(pixel[2] * ra + inv_a);
                                    }
                                }
                            }
                        }
                        if ((req_comp != 0) && (req_comp != 4))
                        {
                            if (ri->bits_per_channel == 16)
                            {
                                _out_ = (byte *)stbi__convert_format16((ushort *)_out_, 4, req_comp, (uint)w, (uint)h);
                            }
                            else
                            {
                                _out_ = stbi__convert_format(_out_, 4, req_comp, (uint)w, (uint)h);
                            }
                            if (_out_ == null)
                            {
                                return(_out_);
                            }
                        }
                        if (comp != null)
                        {
                            *comp = 4;
                        }
                        *y = h;
                        *x = w;
                        return(_out_);
                    }
 public static void *stbi__load_gif_main(stbi__context s, int **delays, int *x, int *y, int *z, int *comp, int req_comp)
 {
     if (stbi__gif_test(s) != 0)
     {
         var   layers   = 0;
         byte *u        = null;
         byte *_out_    = null;
         byte *two_back = null;
         var   g        = new stbi__gif();
         var   stride   = 0;
         if (delays != null)
         {
             *delays = null;
         }
         do
         {
             u = stbi__gif_load_next(s, g, comp, req_comp, two_back);
             if (u != null)
             {
                 *x = g.w;
                 *y = g.h;
                 ++layers;
                 stride = g.w * g.h * 4;
                 if (_out_ != null)
                 {
                     _out_ = (byte *)CRuntime.realloc(_out_, (ulong)(layers * stride));
                     if (delays != null)
                     {
                         *delays = (int *)CRuntime.realloc(*delays, (ulong)(sizeof(int) * layers));
                     }
                 }
                 else
                 {
                     _out_ = (byte *)stbi__malloc((ulong)(layers * stride));
                     if (delays != null)
                     {
                         *delays = (int *)stbi__malloc((ulong)(layers * sizeof(int)));
                     }
                 }
                 CRuntime.memcpy(_out_ + (layers - 1) * stride, u, (ulong)stride);
                 if (layers >= 2)
                 {
                     two_back = _out_ - 2 * stride;
                 }
                 if (delays != null)
                 {
                     (*delays)[layers - 1U] = g.delay;
                 }
             }
         }while(u != null);
         CRuntime.free(g._out_);
         CRuntime.free(g.history);
         CRuntime.free(g.background);
         if ((req_comp != 0) && (req_comp != 4))
         {
             _out_ = stbi__convert_format(_out_, 4, req_comp, (uint)(layers * g.w), (uint)g.h);
         }
         *z = layers;
         return(_out_);
     }
     return((byte *)(ulong)((stbi__err("not GIF") != 0) ? (byte *)null : null));
 }
                    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) ? (byte *)null : null));
                            }
                            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) ? (byte *)null : null));
                            }
                            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 (; ;)
                        {
                            var tag = (int)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) ? (byte *)null : null));
                                }
                                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 = g.lpal;
                                }
                                else
                                {
                                    if ((g.flags & 0x80) != 0)
                                    {
                                        g.color_table = g.pal;
                                    }
                                    else
                                    {
                                        return((byte *)(ulong)((stbi__err("missing color table") != 0) ? (byte *)null : null));
                                    }
                                }
                                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 * 4 + 3] = 255;
                                            CRuntime.memcpy(&g._out_[pi * 4], &g.pal[g.bgindex], (ulong)4);
                                        }
                                    }
                                }
                                return(o);
                            }

                            case 0x21:
                            {
                                var len = 0;
                                var ext = (int)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 * 4 + 3] = 255;
                                        }
                                        if ((g.eflags & 0x01) != 0)
                                        {
                                            g.transparent = stbi__get8(s);
                                            if (g.transparent >= 0)
                                            {
                                                g.pal[g.transparent * 4 + 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) ? (byte *)null : null));
                            }
                            }
                        }
                    }
Esempio n. 8
0
        public void ExpandAtlas(int width, int height)
        {
            var i    = 0;
            var maxy = 0;

            width  = Math.Max(width, _params_.Width);
            height = Math.Max(height, _params_.Height);
            if (width == _params_.Width && height == _params_.Height)
            {
                return;
            }

            Texture = null;

            var data = new byte[width * height];

            for (i = 0; i < _params_.Height; i++)
            {
                fixed(byte *dst = &data[i *width])
                {
                    fixed(byte *src = &_texData[i *_params_.Width])
                    {
                        CRuntime.memcpy(dst, src, (ulong)_params_.Width);
                        if (width > _params_.Width)
                        {
                            CRuntime.memset(dst + _params_.Width, 0, (ulong)(width - _params_.Width));
                        }
                    }
                }
            }

            if (height > _params_.Height)
            {
                Array.Clear(data, _params_.Height * width, (height - _params_.Height) * width);
            }

            _texData = data;

            _colorData = new Color[width * height];
            for (i = 0; i < width * height; ++i)
            {
                _colorData[i].R = _texData[i];
                _colorData[i].G = _texData[i];
                _colorData[i].B = _texData[i];
                _colorData[i].A = _texData[i];
            }

            _atlas.Expand(width, height);
            for (i = 0; i < _atlas.NodesNumber; i++)
            {
                maxy = Math.Max(maxy, _atlas.Nodes[i].Y);
            }
            _dirtyRect[0]   = 0;
            _dirtyRect[1]   = 0;
            _dirtyRect[2]   = _params_.Width;
            _dirtyRect[3]   = maxy;
            _params_.Width  = width;
            _params_.Height = height;
            _itw            = 1.0f / _params_.Width;
            _ith            = 1.0f / _params_.Height;
        }
Esempio n. 9
0
        public static void *stbi__load_gif_main(StbiContext s, int **delays, int *x, int *y, int *z, int *comp,
                                                int reqComp)
        {
            if (stbi__gif_test(s) != 0)
            {
                var   layers   = 0;
                byte *u        = null;
                byte *out_     = null;
                byte *two_back = null;
                var   g        = new StbiGif();
                var   stride   = 0;
                if (delays != null)
                {
                    *delays = null;
                }
                do
                {
                    u = stbi__gif_load_next(s, g, comp, reqComp, two_back);
                    if (u != null)
                    {
                        *x = g.W;
                        *y = g.H;
                        ++layers;
                        stride = g.W * g.H * 4;
                        if (out_ != null)
                        {
                            out_ = (byte *)CRuntime.Realloc(out_, (ulong)(layers * stride));
                            if (delays != null)
                            {
                                *delays = (int *)CRuntime.Realloc(*delays, (ulong)(sizeof(int) * layers));
                            }
                        }
                        else
                        {
                            out_ = (byte *)stbi__malloc((ulong)(layers * stride));
                            if (delays != null)
                            {
                                *delays = (int *)stbi__malloc((ulong)(layers * sizeof(int)));
                            }
                        }

                        CRuntime.Memcpy(out_ + (layers - 1) * stride, u, (ulong)stride);
                        if (layers >= 2)
                        {
                            two_back = out_ - 2 * stride;
                        }
                        if (delays != null)
                        {
                            (*delays)[layers - 1U] = g.Delay;
                        }
                    }
                } while (u != null);

                CRuntime.Free(g.Out);
                CRuntime.Free(g.History);
                CRuntime.Free(g.Background);
                if (reqComp != 0 && reqComp != 4)
                {
                    out_ = stbi__convert_format(out_, 4, reqComp, (uint)(layers * g.W), (uint)g.H);
                }
                *z = layers;
                return(out_);
            }

            return((byte *)(ulong)(stbi__err("not GIF") != 0 ? (byte *)null : null));
        }
        public static IMemoryHolder Load(BinReader s, ReadState ri)
        {
            var info = new PsdInfo();

            if (!ParseHeader(s, ri, ref info))
            {
                return(null);
            }

            if (AreValidMad3Sizes(4, ri.Width, ri.Height, 0) == 0)
            {
                s.Error(ErrorCode.TooLarge);
                return(null);
            }

            byte *_out_ = (byte *)MAllocMad3((4 * ri.OutDepth + 7) / 8, ri.Width, ri.Height, 0);

            if (_out_ == null)
            {
                s.Error(ErrorCode.OutOfMemory);
                return(null);
            }

            int pixelCount = ri.Width * ri.Height;

            if (info.compression != 0)
            {
                s.Skip(ri.Height * info.channelCount * 2);

                for (int channel = 0; channel < 4; channel++)
                {
                    byte *dst;
                    dst = _out_ + channel;
                    if (channel >= info.channelCount)
                    {
                        for (int i = 0; i < pixelCount; i++, dst += 4)
                        {
                            *dst = (byte)(channel == 3 ? 255 : 0);
                        }
                    }
                    else
                    {
                        if (!DecodeRLE(s, dst, pixelCount))
                        {
                            CRuntime.Free(_out_);
                            throw new StbImageReadException(ErrorCode.Corrupt);
                        }
                    }
                }
            }
            else
            {
                for (int channel = 0; channel < 4; channel++)
                {
                    if (channel >= info.channelCount)
                    {
                        if (ri.Depth == 16)
                        {
                            ushort *q   = ((ushort *)_out_) + channel;
                            ushort  val = (ushort)(channel == 3 ? 65535 : 0);
                            for (int i = 0; i < pixelCount; i++, q += 4)
                            {
                                *q = val;
                            }
                        }
                        else
                        {
                            byte *p   = _out_ + channel;
                            byte  val = (byte)(channel == 3 ? 255 : 0);
                            for (int i = 0; i < pixelCount; i++, p += 4)
                            {
                                *p = val;
                            }
                        }
                    }
                    else
                    {
                        if (ri.OutDepth == 16)
                        {
                            ushort *q = ((ushort *)_out_) + channel;
                            for (int i = 0; i < pixelCount; i++, q += 4)
                            {
                                *q = (ushort)s.ReadInt16BE();
                            }
                        }
                        else
                        {
                            byte *p = _out_ + channel;
                            if (ri.OutDepth == 16)
                            {
                                for (int i = 0; i < pixelCount; i++, p += 4)
                                {
                                    *p = (byte)(s.ReadInt16BE() >> 8);
                                }
                            }
                            else
                            {
                                for (int i = 0; i < pixelCount; i++, p += 4)
                                {
                                    *p = s.ReadByte();
                                }
                            }
                        }
                    }
                }
            }

            if (info.channelCount >= 4)
            {
                if (ri.OutDepth == 16)
                {
                    for (int i = 0; i < pixelCount; ++i)
                    {
                        ushort *pixel = (ushort *)_out_ + 4 * i;
                        if ((pixel[3] != 0) && (pixel[3] != 65535))
                        {
                            float a     = pixel[3] / 65535.0f;
                            float ra    = 1f / a;
                            float inv_a = 65535.0f * (1 - ra);
                            pixel[0] = (ushort)(pixel[0] * ra + inv_a);
                            pixel[1] = (ushort)(pixel[1] * ra + inv_a);
                            pixel[2] = (ushort)(pixel[2] * ra + inv_a);
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < pixelCount; ++i)
                    {
                        byte *pixel = _out_ + 4 * i;
                        if ((pixel[3] != 0) && (pixel[3] != 255))
                        {
                            float a     = pixel[3] / 255f;
                            float ra    = 1f / a;
                            float inv_a = 255f * (1 - ra);
                            pixel[0] = (byte)(pixel[0] * ra + inv_a);
                            pixel[1] = (byte)(pixel[1] * ra + inv_a);
                            pixel[2] = (byte)(pixel[2] * ra + inv_a);
                        }
                    }
                }
            }

            IMemoryHolder result = new HGlobalMemoryHolder(
                _out_, (ri.Width * ri.Height * ri.OutComponents * ri.OutDepth + 7) / 8);

            var errorCode = ConvertFormat(result, ri, out var convertedResult);

            if (errorCode != ErrorCode.Ok)
            {
                return(null);
            }
            return(convertedResult);
        }
        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 ? (byte *)null : null));
            }
            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 ? (byte *)null : null));
            }
            token = stbi__hdr_gettoken(s, buffer);
            if (CRuntime.strncmp(token, "-Y ", (ulong)3) != 0)
            {
                return((float *)(ulong)(stbi__err("unsupported data layout") != 0 ? (byte *)null : null));
            }
            token += 3;
            height = (int)CRuntime.strtol(token, &token, 10);
            while (*token == ' ')
            {
                ++token;
            }
            if (CRuntime.strncmp(token, "+X ", (ulong)3) != 0)
            {
                return((float *)(ulong)(stbi__err("unsupported data layout") != 0 ? (byte *)null : null));
            }
            token += 3;
            width  = (int)CRuntime.strtol(token, null, 10);
            *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 ? (byte *)null : null));
            }
            hdr_data = (float *)stbi__malloc_mad4(width, height, req_comp, sizeof(float), 0);
            if (hdr_data == null)
            {
                return((float *)(ulong)(stbi__err("outofmem") != 0 ? (byte *)null : null));
            }

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;
            }
            else
            {
                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);

                        enterMainDecode = true;
                        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
                                                        ? (byte *)null
                                                        : null));
                    }

                    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 ? (byte *)null : null));
                        }
                    }

                    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 ? (byte *)null : null));
                                }

                                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 ? (byte *)null : null));
                                }

                                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);
        }
                    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);
                    }
                    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);
                    }
Esempio n. 14
0
 internal static float __modf(float a, float b)
 {
     return((float)(CRuntime.fmod((float)(a), (float)(b))));
 }
Esempio n. 15
0
                    public static ushort *stbi__convert_format16(ushort *data, int img_n, int req_comp, uint x, uint y)
                    {
                        var     i = 0;
                        var     j = 0;
                        ushort *good;

                        if (req_comp == img_n)
                        {
                            return(data);
                        }
                        good = (ushort *)stbi__malloc((ulong)(req_comp * x * y * 2));
                        if (good == null)
                        {
                            CRuntime.free(data);
                            return((ushort *)(byte *)(ulong)((stbi__err("outofmem") != 0) ? (byte *)null : null));
                        }
                        for (j = 0; j < (int)y; ++j)
                        {
                            var src  = data + j * x * img_n;
                            var dest = good + j * x * req_comp;
                            switch (img_n * 8 + req_comp)
                            {
                            case (1 * 8 + 2):
                            {
                                for (i = (int)(x - 1); i >= 0; --i, src += 1, dest += 2)
                                {
                                    dest[0] = src[0];
                                    dest[1] = 0xffff;
                                }
                                break;
                            }

                            case (1 * 8 + 3):
                            {
                                for (i = (int)(x - 1); i >= 0; --i, src += 1, dest += 3)
                                {
                                    dest[0] = dest[1] = dest[2] = src[0];
                                }
                                break;
                            }

                            case (1 * 8 + 4):
                            {
                                for (i = (int)(x - 1); i >= 0; --i, src += 1, dest += 4)
                                {
                                    dest[0] = dest[1] = dest[2] = src[0];
                                    dest[3] = 0xffff;
                                }
                                break;
                            }

                            case (2 * 8 + 1):
                            {
                                for (i = (int)(x - 1); i >= 0; --i, src += 2, dest += 1)
                                {
                                    dest[0] = src[0];
                                }
                                break;
                            }

                            case (2 * 8 + 3):
                            {
                                for (i = (int)(x - 1); i >= 0; --i, src += 2, dest += 3)
                                {
                                    dest[0] = dest[1] = dest[2] = src[0];
                                }
                                break;
                            }

                            case (2 * 8 + 4):
                            {
                                for (i = (int)(x - 1); i >= 0; --i, src += 2, dest += 4)
                                {
                                    dest[0] = dest[1] = dest[2] = src[0];
                                    dest[3] = src[1];
                                }
                                break;
                            }

                            case (3 * 8 + 4):
                            {
                                for (i = (int)(x - 1); i >= 0; --i, src += 3, dest += 4)
                                {
                                    dest[0] = src[0];
                                    dest[1] = src[1];
                                    dest[2] = src[2];
                                    dest[3] = 0xffff;
                                }
                                break;
                            }

                            case (3 * 8 + 1):
                            {
                                for (i = (int)(x - 1); i >= 0; --i, src += 3, dest += 1)
                                {
                                    dest[0] = stbi__compute_y_16(src[0], src[1], src[2]);
                                }
                                break;
                            }

                            case (3 * 8 + 2):
                            {
                                for (i = (int)(x - 1); i >= 0; --i, src += 3, dest += 2)
                                {
                                    dest[0] = stbi__compute_y_16(src[0], src[1], src[2]);
                                    dest[1] = 0xffff;
                                }
                                break;
                            }

                            case (4 * 8 + 1):
                            {
                                for (i = (int)(x - 1); i >= 0; --i, src += 4, dest += 1)
                                {
                                    dest[0] = stbi__compute_y_16(src[0], src[1], src[2]);
                                }
                                break;
                            }

                            case (4 * 8 + 2):
                            {
                                for (i = (int)(x - 1); i >= 0; --i, src += 4, dest += 2)
                                {
                                    dest[0] = stbi__compute_y_16(src[0], src[1], src[2]);
                                    dest[1] = src[3];
                                }
                                break;
                            }

                            case (4 * 8 + 3):
                            {
                                for (i = (int)(x - 1); i >= 0; --i, src += 4, dest += 3)
                                {
                                    dest[0] = src[0];
                                    dest[1] = src[1];
                                    dest[2] = src[2];
                                }
                                break;
                            }

                            default:
                            {
                                return((ushort *)(byte *)(ulong)((stbi__err("0") != 0) ? (byte *)null : null));
                            }
                            }
                        }
                        CRuntime.free(data);
                        return(good);
                    }
        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 ", (ulong)3) != 0)
            {
                stbi__rewind(s);
                return(0);
            }

            token += 3;
            *y = (int)CRuntime.strtol(token, &token, 10);
            while (*token == ' ')
            {
                ++token;
            }
            if (CRuntime.strncmp(token, "+X ", (ulong)3) != 0)
            {
                stbi__rewind(s);
                return(0);
            }

            token += 3;
            *x    = (int)CRuntime.strtol(token, null, 10);
            *comp = 3;
            return(1);
        }
                    public static void *stbi__tga_load(stbi__context s, int *x, int *y, int *comp,
                                                       int req_comp, stbi__result_info *ri)
                    {
                        var   tga_offset         = (int)stbi__get8(s);
                        var   tga_indexed        = (int)stbi__get8(s);
                        var   tga_image_type     = (int)stbi__get8(s);
                        var   tga_is_RLE         = 0;
                        var   tga_palette_start  = stbi__get16le(s);
                        var   tga_palette_len    = stbi__get16le(s);
                        var   tga_palette_bits   = (int)stbi__get8(s);
                        var   tga_x_origin       = stbi__get16le(s);
                        var   tga_y_origin       = stbi__get16le(s);
                        var   tga_width          = stbi__get16le(s);
                        var   tga_height         = stbi__get16le(s);
                        var   tga_bits_per_pixel = (int)stbi__get8(s);
                        var   tga_comp           = 0;
                        var   tga_rgb16          = 0;
                        var   tga_inverted       = (int)stbi__get8(s);
                        byte *tga_data;
                        byte *tga_palette = null;
                        var   i           = 0;
                        var   j           = 0;
                        var   raw_data    = stackalloc byte[4];

                        raw_data[0] = 0;
                        var RLE_count       = 0;
                        var RLE_repeating   = 0;
                        var read_next_pixel = 1;

                        if (tga_image_type >= 8)
                        {
                            tga_image_type -= 8;
                            tga_is_RLE      = 1;
                        }
                        tga_inverted = 1 - ((tga_inverted >> 5) & 1);
                        if (tga_indexed != 0)
                        {
                            tga_comp = stbi__tga_get_comp(tga_palette_bits, 0, &tga_rgb16);
                        }
                        else
                        {
                            tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3) ? 1 : 0, &tga_rgb16);
                        }
                        if (tga_comp == 0)
                        {
                            return((byte *)(ulong)((stbi__err("bad format") != 0) ? (byte *)null : null));
                        }
                        *x = tga_width;
                        *y = tga_height;
                        if (comp != null)
                        {
                            *comp = tga_comp;
                        }
                        if (stbi__mad3sizes_valid(tga_width, tga_height, tga_comp, 0) == 0)
                        {
                            return((byte *)(ulong)((stbi__err("too large") != 0) ? (byte *)null : null));
                        }
                        tga_data = (byte *)stbi__malloc_mad3(tga_width, tga_height, tga_comp, 0);
                        if (tga_data == null)
                        {
                            return((byte *)(ulong)((stbi__err("outofmem") != 0) ? (byte *)null : null));
                        }
                        stbi__skip(s, tga_offset);
                        if ((tga_indexed == 0) && (tga_is_RLE == 0) && (tga_rgb16 == 0))
                        {
                            for (i = 0; i < tga_height; ++i)
                            {
                                var row     = tga_inverted != 0 ? tga_height - i - 1 : i;
                                var tga_row = tga_data + row * tga_width * tga_comp;
                                stbi__getn(s, tga_row, tga_width * tga_comp);
                            }
                        }
                        else
                        {
                            if (tga_indexed != 0)
                            {
                                stbi__skip(s, tga_palette_start);
                                tga_palette = (byte *)stbi__malloc_mad2(tga_palette_len, tga_comp, 0);
                                if (tga_palette == null)
                                {
                                    CRuntime.free(tga_data);
                                    return((byte *)(ulong)((stbi__err("outofmem") != 0) ? (byte *)null : null));
                                }
                                if (tga_rgb16 != 0)
                                {
                                    var pal_entry = tga_palette;
                                    for (i = 0; i < tga_palette_len; ++i)
                                    {
                                        stbi__tga_read_rgb16(s, pal_entry);
                                        pal_entry += tga_comp;
                                    }
                                }
                                else
                                {
                                    if (stbi__getn(s, tga_palette, tga_palette_len * tga_comp) == 0)
                                    {
                                        CRuntime.free(tga_data);
                                        CRuntime.free(tga_palette);
                                        return((byte *)(ulong)((stbi__err("bad palette") != 0) ? (byte *)null : null));
                                    }
                                }
                            }
                            for (i = 0; i < tga_width * tga_height; ++i)
                            {
                                if (tga_is_RLE != 0)
                                {
                                    if (RLE_count == 0)
                                    {
                                        var RLE_cmd = (int)stbi__get8(s);
                                        RLE_count       = 1 + (RLE_cmd & 127);
                                        RLE_repeating   = RLE_cmd >> 7;
                                        read_next_pixel = 1;
                                    }
                                    else
                                    {
                                        if (RLE_repeating == 0)
                                        {
                                            read_next_pixel = 1;
                                        }
                                    }
                                }
                                else
                                {
                                    read_next_pixel = 1;
                                }
                                if (read_next_pixel != 0)
                                {
                                    if (tga_indexed != 0)
                                    {
                                        var pal_idx = (tga_bits_per_pixel == 8) ? stbi__get8(s) : stbi__get16le(s);
                                        if (pal_idx >= tga_palette_len)
                                        {
                                            pal_idx = 0;
                                        }
                                        pal_idx *= tga_comp;
                                        for (j = 0; j < tga_comp; ++j)
                                        {
                                            raw_data[j] = tga_palette[pal_idx + j];
                                        }
                                    }
                                    else
                                    {
                                        if (tga_rgb16 != 0)
                                        {
                                            stbi__tga_read_rgb16(s, raw_data);
                                        }
                                        else
                                        {
                                            for (j = 0; j < tga_comp; ++j)
                                            {
                                                raw_data[j] = stbi__get8(s);
                                            }
                                        }
                                    }
                                    read_next_pixel = 0;
                                }
                                for (j = 0; j < tga_comp; ++j)
                                {
                                    tga_data[i * tga_comp + j] = raw_data[j];
                                }
                                --RLE_count;
                            }
                            if (tga_inverted != 0)
                            {
                                for (j = 0; j * 2 < tga_height; ++j)
                                {
                                    var index1 = j * tga_width * tga_comp;
                                    var index2 = (tga_height - 1 - j) * tga_width * tga_comp;
                                    for (i = tga_width * tga_comp; i > 0; --i)
                                    {
                                        var temp = tga_data[index1];
                                        tga_data[index1] = tga_data[index2];
                                        tga_data[index2] = temp;
                                        ++index1;
                                        ++index2;
                                    }
                                }
                            }
                            if (tga_palette != null)
                            {
                                CRuntime.free(tga_palette);
                            }
                        }
                        if ((tga_comp >= 3) && (tga_rgb16 == 0))
                        {
                            var tga_pixel = tga_data;
                            for (i = 0; i < tga_width * tga_height; ++i)
                            {
                                var temp = tga_pixel[0];
                                tga_pixel[0] = tga_pixel[2];
                                tga_pixel[2] = temp;
                                tga_pixel   += tga_comp;
                            }
                        }
                        if (req_comp != 0 && req_comp != tga_comp)
                        {
                            tga_data = stbi__convert_format(tga_data, tga_comp, req_comp, (uint)tga_width, (uint)tga_height);
                        }
                        tga_palette_start = tga_palette_len = tga_palette_bits = tga_x_origin = tga_y_origin = 0;
                        return(tga_data);
                    }
Esempio n. 18
0
                    public static void *stbi__bmp_load(stbi__context s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
                    {
                        byte *_out_;
                        var   mr              = (uint)0;
                        var   mg              = (uint)0;
                        var   mb              = (uint)0;
                        var   ma              = (uint)0;
                        uint  all_a           = 0;
                        var   pal             = stackalloc byte[256 * 4];
                        var   psize           = 0;
                        var   i               = 0;
                        var   j               = 0;
                        var   width           = 0;
                        var   flip_vertically = 0;
                        var   pad             = 0;
                        var   target          = 0;
                        var   info            = new stbi__bmp_data();

                        info.all_a = 255;
                        if (stbi__bmp_parse_header(s, &info) == null)
                        {
                            return(null);
                        }
                        flip_vertically = ((int)s.img_y > 0) ? 1 : 0;
                        s.img_y         = (uint)CRuntime.abs((int)s.img_y);
                        mr    = info.mr;
                        mg    = info.mg;
                        mb    = info.mb;
                        ma    = info.ma;
                        all_a = info.all_a;
                        if (info.hsz == 12)
                        {
                            if (info.bpp < 24)
                            {
                                psize = (info.offset - 14 - 24) / 3;
                            }
                        }
                        else
                        {
                            if (info.bpp < 16)
                            {
                                psize = (info.offset - 14 - info.hsz) >> 2;
                            }
                        }
                        s.img_n = (ma != 0) ? 4 : 3;
                        if ((req_comp != 0) && (req_comp >= 3))
                        {
                            target = req_comp;
                        }
                        else
                        {
                            target = s.img_n;
                        }
                        if (stbi__mad3sizes_valid(target, (int)s.img_x, (int)s.img_y, 0) == 0)
                        {
                            return((byte *)(ulong)((stbi__err("too large") != 0) ? (byte *)null : null));
                        }
                        _out_ = (byte *)stbi__malloc_mad3(target, (int)s.img_x, (int)s.img_y, 0);
                        if (_out_ == null)
                        {
                            return((byte *)(ulong)((stbi__err("outofmem") != 0) ? (byte *)null : null));
                        }
                        if (info.bpp < 16)
                        {
                            var z = 0;
                            if ((psize == 0) || (psize > 256))
                            {
                                CRuntime.free(_out_);
                                return((byte *)(ulong)((stbi__err("invalid") != 0) ? (byte *)null : null));
                            }
                            for (i = 0; i < psize; ++i)
                            {
                                pal[i * 4 + 2] = stbi__get8(s);
                                pal[i * 4 + 1] = stbi__get8(s);
                                pal[i * 4 + 0] = stbi__get8(s);
                                if (info.hsz != 12)
                                {
                                    stbi__get8(s);
                                }
                                pal[i * 4 + 3] = 255;
                            }
                            stbi__skip(s, info.offset - 14 - info.hsz - psize * ((info.hsz == 12) ? 3 : 4));
                            if (info.bpp == 1)
                            {
                                width = (int)((s.img_x + 7) >> 3);
                            }
                            else
                            {
                                if (info.bpp == 4)
                                {
                                    width = (int)((s.img_x + 1) >> 1);
                                }
                                else
                                {
                                    if (info.bpp == 8)
                                    {
                                        width = (int)s.img_x;
                                    }
                                    else
                                    {
                                        CRuntime.free(_out_);
                                        return((byte *)(ulong)((stbi__err("bad bpp") != 0) ? (byte *)null : null));
                                    }
                                }
                            }
                            pad = -width & 3;
                            if (info.bpp == 1)
                            {
                                for (j = 0; j < (int)s.img_y; ++j)
                                {
                                    var bit_offset = 7;
                                    var v          = (int)stbi__get8(s);
                                    for (i = 0; i < (int)s.img_x; ++i)
                                    {
                                        var color = (v >> bit_offset) & 0x1;
                                        _out_[z++] = pal[color * 4 + 0];
                                        _out_[z++] = pal[color * 4 + 1];
                                        _out_[z++] = pal[color * 4 + 2];
                                        if (target == 4)
                                        {
                                            _out_[z++] = 255;
                                        }
                                        if ((i + 1) == (int)s.img_x)
                                        {
                                            break;
                                        }
                                        if ((--bit_offset) < 0)
                                        {
                                            bit_offset = 7;
                                            v          = stbi__get8(s);
                                        }
                                    }
                                    stbi__skip(s, pad);
                                }
                            }
                            else
                            {
                                for (j = 0; j < (int)s.img_y; ++j)
                                {
                                    for (i = 0; i < (int)s.img_x; i += 2)
                                    {
                                        var v  = (int)stbi__get8(s);
                                        var v2 = 0;
                                        if (info.bpp == 4)
                                        {
                                            v2  = v & 15;
                                            v >>= 4;
                                        }
                                        _out_[z++] = pal[v * 4 + 0];
                                        _out_[z++] = pal[v * 4 + 1];
                                        _out_[z++] = pal[v * 4 + 2];
                                        if (target == 4)
                                        {
                                            _out_[z++] = 255;
                                        }
                                        if ((i + 1) == (int)s.img_x)
                                        {
                                            break;
                                        }
                                        v          = (info.bpp == 8) ? stbi__get8(s) : v2;
                                        _out_[z++] = pal[v * 4 + 0];
                                        _out_[z++] = pal[v * 4 + 1];
                                        _out_[z++] = pal[v * 4 + 2];
                                        if (target == 4)
                                        {
                                            _out_[z++] = 255;
                                        }
                                    }
                                    stbi__skip(s, pad);
                                }
                            }
                        }
                        else
                        {
                            var rshift = 0;
                            var gshift = 0;
                            var bshift = 0;
                            var ashift = 0;
                            var rcount = 0;
                            var gcount = 0;
                            var bcount = 0;
                            var acount = 0;
                            var z      = 0;
                            var easy   = 0;
                            stbi__skip(s, info.offset - 14 - info.hsz);
                            if (info.bpp == 24)
                            {
                                width = (int)(3 * s.img_x);
                            }
                            else
                            {
                                if (info.bpp == 16)
                                {
                                    width = (int)(2 * s.img_x);
                                }
                                else
                                {
                                    width = 0;
                                }
                            }
                            pad = -width & 3;
                            if (info.bpp == 24)
                            {
                                easy = 1;
                            }
                            else
                            {
                                if (info.bpp == 32)
                                {
                                    if ((mb == 0xff) && (mg == 0xff00) && (mr == 0x00ff0000) && (ma == 0xff000000))
                                    {
                                        easy = 2;
                                    }
                                }
                            }
                            if (easy == 0)
                            {
                                if ((mr == 0) || (mg == 0) || (mb == 0))
                                {
                                    CRuntime.free(_out_);
                                    return((byte *)(ulong)((stbi__err("bad masks") != 0) ? (byte *)null : null));
                                }
                                rshift = stbi__high_bit(mr) - 7;
                                rcount = stbi__bitcount(mr);
                                gshift = stbi__high_bit(mg) - 7;
                                gcount = stbi__bitcount(mg);
                                bshift = stbi__high_bit(mb) - 7;
                                bcount = stbi__bitcount(mb);
                                ashift = stbi__high_bit(ma) - 7;
                                acount = stbi__bitcount(ma);
                            }
                            for (j = 0; j < (int)s.img_y; ++j)
                            {
                                if (easy != 0)
                                {
                                    for (i = 0; i < (int)s.img_x; ++i)
                                    {
                                        byte a = 0;
                                        _out_[z + 2] = stbi__get8(s);
                                        _out_[z + 1] = stbi__get8(s);
                                        _out_[z + 0] = stbi__get8(s);
                                        z           += 3;
                                        a            = (byte)((easy == 2) ? stbi__get8(s) : 255);
                                        all_a       |= a;
                                        if (target == 4)
                                        {
                                            _out_[z++] = a;
                                        }
                                    }
                                }
                                else
                                {
                                    var bpp = info.bpp;
                                    for (i = 0; i < (int)s.img_x; ++i)
                                    {
                                        var  v = (bpp == 16) ? (uint)stbi__get16le(s) : stbi__get32le(s);
                                        uint a = 0;
                                        _out_[z++] = (byte)(stbi__shiftsigned(v & mr, rshift, rcount) & 255);
                                        _out_[z++] = (byte)(stbi__shiftsigned(v & mg, gshift, gcount) & 255);
                                        _out_[z++] = (byte)(stbi__shiftsigned(v & mb, bshift, bcount) & 255);
                                        a          = (uint)((ma != 0) ? stbi__shiftsigned(v & ma, ashift, acount) : 255);
                                        all_a     |= a;
                                        if (target == 4)
                                        {
                                            _out_[z++] = (byte)(a & 255);
                                        }
                                    }
                                }
                                stbi__skip(s, pad);
                            }
                        }
                        if ((target == 4) && (all_a == 0))
                        {
                            for (i = (int)(4 * s.img_x * s.img_y - 1); i >= 0; i -= 4)
                            {
                                _out_[i] = 255;
                            }
                        }
                        if (flip_vertically != 0)
                        {
                            byte t = 0;
                            for (j = 0; j < (int)s.img_y >> 1; ++j)
                            {
                                var p1 = _out_ + j * s.img_x * target;
                                var p2 = _out_ + (s.img_y - 1 - j) * s.img_x * target;
                                for (i = 0; i < (int)s.img_x * target; ++i)
                                {
                                    t     = p1[i];
                                    p1[i] = p2[i];
                                    p2[i] = t;
                                }
                            }
                        }
                        if ((req_comp != 0) && (req_comp != target))
                        {
                            _out_ = stbi__convert_format(_out_, target, req_comp, s.img_x, s.img_y);
                            if (_out_ == null)
                            {
                                return(_out_);
                            }
                        }
                        *x = (int)s.img_x;
                        *y = (int)s.img_y;
                        if (comp != null)
                        {
                            *comp = s.img_n;
                        }
                        return(_out_);
                    }
Esempio n. 19
0
 private static void *stbi__malloc(int size)
 {
     return(CRuntime.malloc((ulong)size));
 }
Esempio n. 20
0
        public static byte *stbi__gif_load_next(StbiContext s, StbiGif g, int *comp, int reqComp, byte *twoBack)
        {
            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 ? (byte *)null : null));
                }
                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 ? (byte *)null : null));
                }
                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 && twoBack == null)
                {
                    dispose = 2;
                }
                if (dispose == 3)
                {
                    for (pi = 0; pi < pcount; ++pi)
                    {
                        if (g.History[pi] != 0)
                        {
                            CRuntime.Memcpy(&g.Out[pi * 4], &twoBack[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 (; ;)
            {
                var tag = (int)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 ? (byte *)null : null));
                    }
                    g.LineSize = g.W * 4;
                    g.StartX   = x * 4;
                    g.StartY   = y * g.LineSize;
                    g.MaxX     = g.StartX + w * 4;
                    g.MaxY     = g.StartY + h * g.LineSize;
                    g.CurX     = g.StartX;
                    g.CurY     = g.StartY;
                    if (w == 0)
                    {
                        g.CurY = g.MaxY;
                    }
                    g.Lflags = stbi__get8(s);
                    if ((g.Lflags & 0x40) != 0)
                    {
                        g.Step  = 8 * g.LineSize;
                        g.Parse = 3;
                    }
                    else
                    {
                        g.Step  = g.LineSize;
                        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.ColorTable = g.Lpal;
                    }
                    else if ((g.Flags & 0x80) != 0)
                    {
                        g.ColorTable = g.Pal;
                    }
                    else
                    {
                        return((byte *)(ulong)(stbi__err("missing color table") != 0 ? (byte *)null : null));
                    }

                    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 * 4 + 3] = 255;
                                CRuntime.Memcpy(&g.Out[pi * 4], &g.Pal[g.Bgindex], (ulong)4);
                            }
                        }
                    }

                    return(o);
                }

                case 0x21:
                {
                    var len = 0;
                    var ext = (int)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 * 4 + 3] = 255;
                            }
                            if ((g.Eflags & 0x01) != 0)
                            {
                                g.Transparent = stbi__get8(s);
                                if (g.Transparent >= 0)
                                {
                                    g.Pal[g.Transparent * 4 + 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 ? (byte *)null : null));
                }
            }
        }