Resize

The Resize kernel scales an image horizontally or vertically according to the value of the Horizontal parameter. The Scale can also be controlled by the user.

Chain two BlinkScript nodes containing this kernel together, one with Horizontal checked and one without, for a two-dimensional resize.

// Copyright (c) 2024 The Foundry Visionmongers Ltd.  All Rights Reserved.
// WARNING: This file is referenced within Images/ImageAccess.rst. Line number references will need updating if changes are made to this file.
// Resize kernel: scale the input horizontally or vertically using a bilinear filter. 
kernel Resize : ImageComputationKernel<eComponentWise>
{
  Image<eRead, eAccessRandom, eEdgeConstant> src; // Output will be black outside the original image
  Image<eWrite, eAccessPoint> dst;

param:
  float externalScale;
  bool horizontal;

local:
  float scale;

  void define()
  {
    defineParam(externalScale, "Scale", 0.5f);
    defineParam(horizontal, "Horizontal", true);
  }

  void init()
  {
    scale = 1.0f / externalScale;  // invert the scale as we back-map from dst to src
  }

  void process(int2 pos)
  {
    // Work out the scaled src position.
    const float xPos = (horizontal ? (float)pos.x * scale : (float)pos.x);
    const float yPos = (horizontal ? pos.y : (float)pos.y * scale);

    // Use a bilinear filter to get the value at that src position.
    dst() = bilinear(src, xPos, yPos);    
  }
};