Shuffle

The Shuffle kernel rearranges or copies components from two input images, src0 and src1, into the output image. The reordering is controlled by the inputShuffle and componentShuffle parameters.

kernel Shuffle : ImageComputationKernel<ePixelWise>
{
  Image<eRead, eAccessPoint, eEdgeClamped> src0;
  Image<eRead, eAccessPoint, eEdgeClamped> src1;
  Image<eWrite, eAccessPoint> dst;

  param:
    int4 inputShuffle;        // Set to 0 to copy from src0 or 1 to copy from src1
    int4 componentShuffle;    // Set to the index of component in src0 or src1 you want to copy

  void define()
  {
    defineParam(inputShuffle, "inputShuffle", int4(0));
    defineParam(componentShuffle, "componentShuffle", int4(0, 1, 2, 3));
  }

  void process()
  {
    // Limit the number of shuffle components to 4 (as per int4 maximum size)
    const int numShuffleComps = (dst.kComps < 4) ? dst.kComps : 4; 

    for (int k = 0; k < numShuffleComps; k++) {
      int i = inputShuffle[k];
      int c = componentShuffle[k];

      // Initialize dst(k) to a default value (e.g., 0)
      dst(k) = 0.0f;

      if (i == 0) {
        // Safety check for src0
        if (c >= 0 && c < src0.kComps) {
          dst(k) = src0(c);
        } 
        else {
          // Handle invalid component index for src0
          dst(k) = 0.0f;
        }
      } 
      else if (i == 1) {
        // Safety check for src1
        if (c >= 0 && c < src1.kComps) {
          dst(k) = src1(c);
        } 
        else {
          // Handle invalid component index for src1
          dst(k) = 0.0f;
        }
      } 
      else {
        // Handle invalid input source index
        dst(k) = 0.0f;
      }
    }

    // Assign default value (0.0f) to components in dst beyond those specified by inputShuffle and componentShuffle
    for (int k = numShuffleComps; k < dst.kComps; k++) {
      // Assign default values to remaining components
      dst(k) = 0.0f;
    }
    
  }
};