Пример #1
0
        public byte[] OptimizeImage(byte[] inputImage, ImageOptimizeParams imageOptimizeParams = null)
        {
            if (imageOptimizeParams == null)
            {
                imageOptimizeParams = GetDefaultParams();
            }

            using (var c = new JobContext())
                using (var ms = new MemoryStream())
                {
                    c.AddInputBytesPinned(0, inputImage);
                    c.AddOutputBuffer(1);
                    //string commandString = $"dpi={72}&maxwidth={1024}&maxheight={1024}&quality={80}";
                    string commandString = $"dpi={imageOptimizeParams.Dpi}&maxwidth={imageOptimizeParams.MaxWidth}&maxheight={imageOptimizeParams.MaxHeight}&quality={imageOptimizeParams.QualityPercent}";
                    var    response      = c.ExecuteImageResizer4CommandString(0, 1, commandString);

                    var data         = response.DeserializeDynamic();
                    var outputStream = c.GetOutputBuffer(1);

                    //Assert.Equal(200, (int)data.code);
                    //Assert.Equal(true, (bool)data.success);

                    outputStream.CopyTo(ms);
                    return(ms.ToArray());

                    //httpContext.Response.ContentType = "image/png";//cbb.Properties.ContentType;
                    //await outputStream.CopyToAsync(httpContext.Response.Body);
                }
        }
Пример #2
0
        public async Task <BuildJobResult> FinishAsync(CancellationToken cancellationToken)
        {
            var inputByteArrays = await Task.WhenAll(_inputs.Select(async pair => new KeyValuePair <int, ArraySegment <byte> >(pair.Key, await pair.Value.GetBytesAsync(cancellationToken))));

            using (var ctx = new JobContext())
            {
                foreach (var pair in inputByteArrays)
                {
                    ctx.AddInputBytesPinned(pair.Key, pair.Value);
                }

                foreach (var outId in _outputs.Keys)
                {
                    ctx.AddOutputBuffer(outId);
                }
                //TODO: Use a Semaphore to limit concurrency; and move work to threadpool

                var response = ctx.Execute(new
                {
                    framewise = ToFramewise()
                });

                foreach (var pair in _outputs)
                {
                    using (var stream = ctx.GetOutputBuffer(pair.Key))
                    {
                        await pair.Value.CopyFromStreamAsync(stream, cancellationToken);
                    }
                }
                return(BuildJobResult.From(response, _outputs));
            }
        }
Пример #3
0
        public void TestExecute()
        {
            using (var c = new JobContext())
            {
                c.AddInputBytesPinned(0,
                                      Convert.FromBase64String(
                                          "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg=="));

                c.AddOutputBuffer(1);

                var message = new
                {
                    framewise = new
                    {
                        steps = new object[]
                        {
                            new
                            {
                                decode = new
                                {
                                    io_id = 0
                                }
                            },
                            "flip_v",
                            new
                            {
                                encode = new
                                {
                                    io_id  = 1,
                                    preset = new
                                    {
                                        libjpegturbo = new
                                        {
                                            quality = 90
                                        }
                                    }
                                }
                            }
                        }
                    }
                };

                var response = c.SendMessage("v0.1/execute", message);

                var data = response.DeserializeDynamic();

                _output.WriteLine(response.GetString());

                Assert.Equal(200, (int)data.code);
                Assert.Equal(true, (bool)data.success);
            }
        }
Пример #4
0
        public void TestIr4Execute()
        {
            using (var c = new JobContext())
            {
                c.AddInputBytesPinned(0,
                                      Convert.FromBase64String(
                                          "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg=="));
                c.AddOutputBuffer(1);
                var response = c.ExecuteImageResizer4CommandString(0, 1, "w=200&h=200&scale=both&format=jpg");

                var data = response.DeserializeDynamic();

                _output.WriteLine(response.GetString());

                Assert.Equal(200, (int)data.code);
                Assert.Equal(true, (bool)data.success);
            }
        }
Пример #5
0
        internal async Task <BuildJobResult> FinishAsync(JobExecutionOptions executionOptions, SecurityOptions securityOptions, CancellationToken cancellationToken)
        {
            var inputByteArrays = await Task.WhenAll(_inputs.Select(async pair => new KeyValuePair <int, ArraySegment <byte> >(pair.Key, await pair.Value.GetBytesAsync(cancellationToken))));

            using (var ctx = new JobContext())
            {
                foreach (var pair in inputByteArrays)
                {
                    ctx.AddInputBytesPinned(pair.Key, pair.Value);
                }

                foreach (var outId in _outputs.Keys)
                {
                    ctx.AddOutputBuffer(outId);
                }

                //TODO: Use a Semaphore to limit concurrency

                var message = new
                {
                    security  = securityOptions?.ToImageflowDynamic(),
                    framewise = ToFramewise()
                };

                var response = executionOptions.OffloadCpuToThreadPool
                    ? await Task.Run(() => ctx.Execute(message), cancellationToken)
                    : ctx.Execute(message);

                using (response)
                {
                    foreach (var pair in _outputs)
                    {
                        using (var stream = ctx.GetOutputBuffer(pair.Key))
                        {
                            await pair.Value.CopyFromStreamAsync(stream, cancellationToken);
                        }
                    }
                    return(BuildJobResult.From(response, _outputs));
                }
            }
        }