Git Product home page Git Product logo

Comments (1)

saucecontrol avatar saucecontrol commented on May 29, 2024

I'm not sure what the use case is for such a thing, but I can give some detail for why that doesn't work.

One of the reasons MagicScaler is faster and more memory-efficient than other libraries is that it operates on a pull pipeline model. No pixels exist in the pipeline until they are requested by something. In the case of ProcessImage, it is the encoder that requests the pixels, so if you have no encoder, nothing requests any pixels, and nothing happens. The whole thing would be a no-op, so the reasonable thing to do there is to throw an exception.

If you have need to access the processed pixels without ending in an encoder, you can do that with a custom pipeline. Here is an example that constructs a pipeline using the built-in TestPatternPixelSource to generate a large test pattern image and then resizes it, catching the output in a buffer.

using System;
using System.Buffers;
using System.Drawing;
using PhotoSauce.MagicScaler;

class Program
{
    static void Main()
    {
        const int OriginalWidth = 6000;
        const int OriginalHeight = 4000;
        const int ResizedWidth = 600;
        const int ResizedHeight = 400;

        // stride is width * bytes-per-pixel (3 for BGR), rounded up to the nearest multiple of 4
        const uint stride = ResizedWidth * 3u + 3u & ~3u;
        const uint bufflen = ResizedHeight * stride;

        var settings = new ProcessImageSettings
        {
            Width = ResizedWidth,
            Height = ResizedHeight,
            Sharpen = false
        };

        // This IPixelSource implementation generates a pre-defined test pattern.
        // You could alternatively provide an existing image file to BuildPipeline.
        using (var pixels = new TestPatternPixelSource(OriginalWidth, OriginalHeight, PixelFormats.Bgr24bpp))
        using (var pipeline = MagicImageProcessor.BuildPipeline(pixels, settings))
        {
            // Here we use a rented buffer to hold the ouput.
            // This could also be a locked buffer from a System.Drawing.Bitmap or from
            // another library.
            var buffer = ArrayPool<byte>.Shared.Rent((int)bufflen);

            // This rectangle defines the pixels we are requesting from the pipeline.
            // This example requests them all at once for simplicity, but it would be
            // more memory-efficient to request a single output line at a time in a loop,
            // assuming the target is compatible with that method of operation.
            var rect = new Rectangle(0, 0, ResizedWidth, ResizedHeight);

            // All the work is done here.
            pipeline.PixelSource.CopyPixels(rect, (int)stride, new Span<byte>(buffer, 0, (int)bufflen));

            ArrayPool<byte>.Shared.Return(buffer);
        }
    }
}

from photosauce.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.