Example #1
0
        public byte[] Read(Stream stream, out int x, out int y, out int comp, int req_comp)
        {
            _stream = stream;

            try
            {
                int xx, yy, ccomp;
                var result = StbImage.stbi_load_from_callbacks(_callbacks, null, &xx, &yy, &ccomp, req_comp);

                x    = xx;
                y    = yy;
                comp = ccomp;

                if (result == null)
                {
                    throw new Exception(StbImage.LastError);
                }

                // Convert to array
                var c    = req_comp != 0 ? req_comp : comp;
                var data = new byte[x * y * c];
                Marshal.Copy(new IntPtr(result), data, 0, data.Length);

                CRuntime.free(result);

                return(data);
            }
            finally
            {
                _stream = null;
            }
        }
Example #2
0
        public Color[] Read(Stream stream, out int x, out int y, out int comp)
        {
            _stream = stream;

            try
            {
                int xx, yy, ccomp;
                var result = StbImage.stbi_load_from_callbacks(_callbacks, null, &xx, &yy, &ccomp, StbImage.STBI_rgb_alpha);

                x    = xx;
                y    = yy;
                comp = ccomp;

                if (result == null)
                {
                    throw new Exception(StbImage.LastError);
                }

                // Convert to color array
                var data = new Color[x * y];
                var src  = result;
                fixed(Color *dest = data)
                {
                    for (var i = 0; i < data.Length; ++i)
                    {
                        dest[i].R = *src++;
                        dest[i].G = *src++;
                        dest[i].B = *src++;
                        dest[i].A = *src++;
                    }
                }

                CRuntime.free(result);

                return(data);
            }
            finally
            {
                _stream = null;
            }
        }