예제 #1
0
        public static int stbi_write_hdr_core(stbi__write_context s, int x, int y, int comp, float *data)
        {
            if ((y <= 0) || (x <= 0) || (data == null))
            {
                return(0);
            }

            var scratch = (byte *)(CRuntime.malloc((ulong)(x * 4)));

            int i;
            var header = "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n";
            var bytes  = Encoding.UTF8.GetBytes(header);

            fixed(byte *ptr = bytes)
            {
                s.func(s.context, ((sbyte *)ptr), bytes.Length);
            }

            var str = string.Format("EXPOSURE=          1.0000000000000\n\n-Y {0} +X {1}\n", y, x);

            bytes = Encoding.UTF8.GetBytes(str);
            fixed(byte *ptr = bytes)
            {
                s.func(s.context, ((sbyte *)ptr), bytes.Length);
            }

            for (i = 0; i < y; i++)
            {
                stbiw__write_hdr_scanline(s, x, comp, scratch, data + comp * i * x);
            }
            CRuntime.free(scratch);
            return(1);
        }
예제 #2
0
        public static Image LoadFromMemory(byte[] bytes, int req_comp = STBI_default)
        {
            byte *result;
            int   x, y, comp;

            fixed(byte *b = &bytes[0])
            {
                result = stbi_load_from_memory(b, bytes.Length, &x, &y, &comp, req_comp);
            }

            var image = new Image
            {
                Width      = x,
                Height     = y,
                SourceComp = comp,
                Comp       = req_comp == STBI_default ? comp : req_comp
            };

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

            // Convert to array
            var data = new byte[x * y * image.Comp];

            Marshal.Copy(new IntPtr(result), data, 0, data.Length);
            CRuntime.free(result);

            image.Data = data;

            return(image);
        }
예제 #3
0
        public AnimatedGifFrame[] ReadAnimatedGif(Stream stream, out int x, out int y, out int comp, int req_comp)
        {
            try
            {
                x = y = comp = 0;

                var res = new List <AnimatedGifFrame>();
                _stream = stream;

                var context = new StbImage.stbi__context();
                StbImage.stbi__start_callbacks(context, _callbacks, null);

                if (StbImage.stbi__gif_test(context) == 0)
                {
                    throw new Exception("Input stream is not GIF file.");
                }

                var g = new StbImage.stbi__gif();

                do
                {
                    int ccomp;
                    var result = StbImage.stbi__gif_load_next(context, g, &ccomp, req_comp);
                    if (result == null)
                    {
                        break;
                    }

                    comp = ccomp;
                    var c    = req_comp != 0 ? req_comp : comp;
                    var data = new byte[g.w * g.h * c];
                    Marshal.Copy(new IntPtr(result), data, 0, data.Length);
                    CRuntime.free(result);

                    var frame = new AnimatedGifFrame
                    {
                        Data  = data,
                        Delay = g.delay
                    };
                    res.Add(frame);
                } while (true);

                CRuntime.free(g._out_);

                if (res.Count > 0)
                {
                    x = g.w;
                    y = g.h;
                }

                return(res.ToArray());
            }
            finally
            {
                _stream = null;
            }
        }
예제 #4
0
        public static int stbi_write_png_to_func(WriteCallback func,
                                                 void *context,
                                                 int x,
                                                 int y,
                                                 int comp,
                                                 void *data,
                                                 int stride_bytes
                                                 )
        {
            int len;
            var png = stbi_write_png_to_mem((byte *)(data), stride_bytes, x, y, comp, &len);

            if (png == null)
            {
                return(0);
            }
            func(context, png, len);
            CRuntime.free(png);
            return(1);
        }
예제 #5
0
        public static Image LoadFromMemory(byte[] bytes, int req_comp = STBI_default)
        {
            Image image;
            byte *result = null;
            int   x, y, comp;

            try
            {
                fixed(byte *b = bytes)
                {
                    result = stbi_load_from_memory(b, bytes.Length, &x, &y, &comp, req_comp);
                }

                if (result == null)
                {
                    throw new InvalidOperationException(LastError);
                }

                image = new Image
                {
                    Width      = x,
                    Height     = y,
                    SourceComp = comp,
                    Comp       = req_comp == STBI_default ? comp : req_comp
                };

                // Convert to array
                image.Data = new byte[x * y * image.Comp];
                Marshal.Copy(new IntPtr(result), image.Data, 0, image.Data.Length);
            }
            finally
            {
                if (result != null)
                {
                    CRuntime.free(result);
                }
            }

            return(image);
        }
예제 #6
0
        public static short[] decode_vorbis_from_memory(byte[] input, out int sampleRate, out int chan)
        {
            short *result = null;
            int    length = 0;

            fixed(byte *b = input)
            {
                int c, s;

                length = stb_vorbis_decode_memory(b, input.Length, &c, &s, ref result);

                chan       = c;
                sampleRate = s;
            }

            var output = new short[length];

            Marshal.Copy(new IntPtr(result), output, 0, output.Length);
            CRuntime.free(result);

            return(output);
        }
예제 #7
0
        public Image Read(Stream stream, int req_comp = StbImage.STBI_default)
        {
            _stream = stream;

            try
            {
                int x, y, comp;
                var result = StbImage.stbi_load_from_callbacks(_callbacks, null, &x, &y, &comp, req_comp);

                var image = new Image
                {
                    Width      = x,
                    Height     = y,
                    SourceComp = comp,
                    Comp       = req_comp == StbImage.STBI_default ? comp : req_comp
                };

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

                // Convert to array
                var data = new byte[x * y * image.Comp];
                Marshal.Copy(new IntPtr(result), data, 0, data.Length);
                CRuntime.free(result);

                image.Data = data;

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