Program: 91846
Project: 91846
Buggy source:
/** 
 * Creates a transform for the same mathematic than the given   {@code step}but producing only the given dimensions as outputs. This method is invoked by   {@link #separate()} when user-specified target dimensions need to be taken in account.The given  {@code step} and {@code dimensions} are typically the values of{@link #transform} and {@link #targetDimensions} fields respectively, but not necessarily.<p>Subclasses can override this method if they need to handle some  {@code MathTransform} implementationsin a special way. However all implementations of this method shall obey to the following contract:</p> <ul> <li> {@link #sourceDimensions} and {@link #targetDimensions} should not be assumed accurate.</li><li> {@link #sourceDimensions} should not be modified by this method.</li><li> {@link #targetDimensions} should not be modified by this method.</li></ul> The number and nature of inputs stay unchanged. For example if the supplied  {@code transform}has (<var>longitude</var>, <var>latitude</var>, <var>height</var>) outputs, then a filtered transform may keep only the (<var>longitude</var>, <var>latitude</var>) part for the same inputs. In most cases, the filtered transform is non-invertible since it loose informations.
 * @param step The transform for which to retain only a subset of the target dimensions.
 * @param dimensions Indices of the target dimensions of {@code step} to retain.
 * @return A transform producing only the given target dimensions.
 * @throws FactoryException if the given transform is not separable.
 */
protected MathTransform filterTargetDimensions(MathTransform step,final int[] dimensions) throws FactoryException {
  final int numSrc=step.getSourceDimensions();
  int numTgt=step.getTargetDimensions();
  final int lower=dimensions[0];
  final int upper=dimensions[dimensions.length - 1];
  if (lower == 0 && upper == numTgt && dimensions.length == numTgt) {
    return step;
  }
  int removeAt=0;
  int numRemoved=0;
  if (step instanceof PassThroughTransform) {
    final PassThroughTransform passThrough=(PassThroughTransform)step;
    final int subLower=passThrough.firstAffectedOrdinate;
    final int numSubTgt=passThrough.subTransform.getTargetDimensions();
    if (!containsAny(dimensions,subLower,subLower + numSubTgt)) {
      step=IdentityTransform.create(numTgt=numSrc);
      removeAt=subLower;
      numRemoved=numSubTgt - passThrough.subTransform.getSourceDimensions();
    }
  }
  final Matrix matrix=Matrices.createZero(dimensions.length + 1,numTgt + 1);
  for (int j=0; j < dimensions.length; j++) {
    int i=dimensions[j];
    if (i >= removeAt) {
      i-=numRemoved;
    }
    matrix.setElement(j,i,1);
  }
  matrix.setElement(dimensions.length,numTgt,1);
  return factory.createConcatenatedTransform(step,factory.createAffineTransform(matrix));
}

Fixed version (ground truth):
/** 
 * Creates a transform for the same mathematic than the given   {@code step}but producing only the given dimensions as outputs. This method is invoked by   {@link #separate()} when user-specified target dimensions need to be taken in account.The given  {@code step} and {@code dimensions} are typically the values of{@link #transform} and {@link #targetDimensions} fields respectively, but not necessarily.<p>Subclasses can override this method if they need to handle some  {@code MathTransform} implementationsin a special way. However all implementations of this method shall obey to the following contract:</p> <ul> <li> {@link #sourceDimensions} and {@link #targetDimensions} should not be assumed accurate.</li><li> {@link #sourceDimensions} should not be modified by this method.</li><li> {@link #targetDimensions} should not be modified by this method.</li></ul> The number and nature of inputs stay unchanged. For example if the supplied  {@code transform}has (<var>longitude</var>, <var>latitude</var>, <var>height</var>) outputs, then a filtered transform may keep only the (<var>longitude</var>, <var>latitude</var>) part for the same inputs. In most cases, the filtered transform is non-invertible since it loose informations.
 * @param step  the transform for which to retain only a subset of the target dimensions.
 * @param dimensions  indices of the target dimensions of {@code step} to retain.
 * @return a transform producing only the given target dimensions.
 * @throws FactoryException if the given transform is not separable.
 */
protected MathTransform filterTargetDimensions(MathTransform step,final int[] dimensions) throws FactoryException {
  final int numSrc=step.getSourceDimensions();
  int numTgt=step.getTargetDimensions();
  final int lower=dimensions[0];
  final int upper=dimensions[dimensions.length - 1];
  if (lower == 0 && upper == numTgt && dimensions.length == numTgt) {
    return step;
  }
  int removeAt=0;
  int numRemoved=0;
  if (step instanceof PassThroughTransform) {
    final PassThroughTransform passThrough=(PassThroughTransform)step;
    final int subLower=passThrough.firstAffectedOrdinate;
    final int numSubTgt=passThrough.subTransform.getTargetDimensions();
    if (!containsAny(dimensions,subLower,subLower + numSubTgt)) {
      step=IdentityTransform.create(numTgt=numSrc);
      removeAt=subLower;
      numRemoved=numSubTgt - passThrough.subTransform.getSourceDimensions();
    }
  }
  final Matrix matrix=Matrices.createZero(dimensions.length + 1,numTgt + 1);
  for (int j=0; j < dimensions.length; j++) {
    int i=dimensions[j];
    if (i >= removeAt) {
      i-=numRemoved;
    }
    matrix.setElement(j,i,1);
  }
  matrix.setElement(dimensions.length,numTgt,1);
  return factory.createConcatenatedTransform(step,factory.createAffineTransform(matrix));
}