PhotoSauce.MagicScaler

repository·master·Indexed 20 days ago

https://github.com/saucecontrol/photosauce

A high-performance image processing pipeline for .NET designed for speed and efficiency. It features high-quality image resizing using linear light processing, sharpening, and advanced resampling. The library is cross-platform (Windows and Linux) and supports various image formats through plugins such as PhotoSauce.NativeCodecs for HEIC, AVIF, JPEG, PNG, GIF, and JPEG XL.

Tokens
6.1K
Snippets
23
Records
38
Agent score
72%

What's inside PhotoSauce.MagicScaler

  1. Overview of PhotoSauce.MagicScaler

    master
    PhotoSauce.MagicScaler is a high-performance image processing pipeline for .NET designed for speed and efficiency. It focuses on high-quality image resizing using best-of-breed algorithms, linear light processing, and sharpening. It is suitable for complex imaging tasks where performance is a priority.
  2. Configure Animated PNG (APNG) support

    master

    APNG decoding is enabled by default. However, APNG encoding is disabled by default due to an intermittent memory corruption bug in APNG-patched libpng.

    To enable APNG encoding for testing, set the PhotoSauce.NativeCodecs.Libpng.EnableApngEncode switch at application startup using AppContext.SetSwitch or via your runtimeconfig.json file.

  3. How MagicScaler achieves high image quality

    master

    MagicScaler uses several advanced techniques to ensure superior resizing quality compared to standard libraries:

    1. Linear Light Processing: It performs resampling in linear light, which preserves highlights and prevents color shifts during blending.
    2. High-Quality Resampling: It defaults to high-quality resampling algorithms to prevent blurriness.
    3. Post-Resizing Sharpening: It automatically applies a sharpening step to compensate for the natural blurring that occurs during the resizing process.
  4. Understand MagicScaler's color management

    master
    MagicScaler handles embedded color profiles (like Display P3) to maximize compatibility while preserving the original gamut. By default, it embeds the ICC profile in the output image (Option 2), which preserves color accuracy but may result in larger file sizes compared to converting to sRGB. This prevents the 'washed-out' color issues common in libraries that ignore color profiles.
  5. Register the Giflib codec

    master

    To enable GIF support via Giflib, call the UseGiflib extension method within your CodecManager.Configure action during application startup.

    Note: By default, this plugin will remove or replace the built-in Windows GIF codec if it is detected in the pipeline.

    using PhotoSauce.MagicScaler;
    using PhotoSauce.NativeCodecs.Giflib;
    
    CodecManager.Configure(codecs => {
        codecs.UseGiflib();
    });
  6. Requirements for PhotoSauce.WebRSize

    master

    To run PhotoSauce.WebRSize, your hosting environment must meet the following criteria:

    • IIS Version: IIS 7 or higher.
    • ASP.NET Version: ASP.NET 4.6.1 or higher (compatible with WebForms or MVC).
    • App Pool Mode: The host Application Pool must be running in Integrated Pipeline Mode.
  7. Enable kernel caching for WebRSize processed images

    master

    By default, WebRSize requests are ineligible for http.sys kernel caching because they use query strings and URL rewriting. To achieve performance parity with static image files, you must explicitly configure a kernelCachePolicy in your IIS configuration.

    This allows processed images to be served directly from the kernel cache once they have been written to the disk cache.

    To enable this for .jpg files, add a profile to your web.config using the following configuration:

    <system.webServer>
      <caching>
        <profiles>
          <add extension=".jpg" kernelCachePolicy="CacheUntilChange" location="Any" />
        </profiles>
      </caching>
    </system.webServer>
  8. Register the WebRSizeModule for request interception

    master

    The WebRSizeModule (an IHttpModule) manages processing and caching. Because image file extensions are often mapped to the unmanaged IIS static file handler, the module might not automatically receive event notifications via PreApplicationStartMethodAttribute.

    To ensure the module intercepts requests, use one of these two methods:

    Register the module explicitly in the system.webServer section and omit the preCondition attribute. This ensures the module sees all requests, managed or not.

    <system.webServer>
      <modules>
        <add name="WebRSize" type="PhotoSauce.WebRSize.WebRSizeModule" />
      </modules>
    </system.webServer>

    2. Explicit Handler Mapping

    Map specific image extensions to a managed IHttpHandler (like System.Web.StaticFileHandler). This allows the self-registered module to intercept the requests.

  9. Install and configure PhotoSauce.NativeCodecs.Libpng

    master

    PhotoSauce.NativeCodecs.Libpng is a MagicScaler plugin that wraps the libpng native codec. It provides PNG support for non-Windows platforms or enhanced capabilities on Windows.

    Requirements

    • A compatible native binary must be present. The NuGet package includes binaries for:
      • Windows: x86, x64, and ARM64
      • Linux: glibc x64 and ARM64
    • If you need to build for an unsupported platform, you must use a custom build of libpng. A specialized vcpkg port is available in the PhotoSauce GitHub repository under build/vcpkg/ports/pspng.
    <!-- No installation command provided in source, but refers to NuGet package -->
  10. Requirements for PhotoSauce.NativeCodecs.Giflib

    master

    To use this plugin, a compatible native binary must be present. The NuGet package provides native binaries for the following platforms:

    • Windows: x86, x64, and ARM64
    • Linux: glibc x64 and ARM64

    This plugin is primarily used to provide GIF support on non-Windows platforms where the auto-discoverable Windows GIF codec is unavailable.

  11. Configure WebRSize in ASP.NET (IIS Integrated Pipeline)

    master

    WebRSize requires explicit opt-in for image folders and a registered ConfigSection. Note that WebRSize currently only works with ASP.NET hosted with IIS Integrated Pipeline Mode; ASP.NET Core support is pending.

    To enable WebRSize, first register the webrsize section in your web.config:

    <configSections>
      <section name="webrsize" type="PhotoSauce.WebRSize.WebRSizeSection" />
    </configSections>

    Then, provide a minimal configuration specifying a diskCache path and at least one imageFolders entry. The diskCache folder must exist and the App Pool identity must have write access to it.

    <configSections>
      <section name="webrsize" type="PhotoSauce.WebRSize.WebRSizeSection" />
    </configSections>
    
    <webrsize>
      <diskCache path="/webrsizecache" />
      <imageFolders>
        <add name="images" path="/images/" />
      </imageFolders>
    </webrsize>