コード例 #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 Image Read(Stream stream, int req_comp = STBImage.STBI_rgb_alpha)
        {
            _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
                };

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

                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);

                image.Data = data;

                return(image);
            }
            finally
            {
                _stream = null;
            }
        }
コード例 #3
0
        public AnimatedGifFrame[] ReadAnimatedGif(Stream stream, out int w, out int h,
                                                  int req_comp = STBImage.STBI_default)
        {
            try
            {
                w = h = 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;
                    }

                    var c    = req_comp != 0 ? req_comp : ccomp;
                    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)
                {
                    w = g.w;
                    h = 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);
        }