use of mpicbg.trakem2.transform.CoordinateTransform in project TrakEM2 by trakem2.
the class Render method render.
/**
* Renders a patch, mapping its intensities [min, max] → [0, 1]
*
* @param patch the patch to be rendered
* @param targetImage target pixels, specifies the target box
* @param targetWeight target weight pixels, depending on alpha
* @param x target box offset in world coordinates
* @param y target box offset in world coordinates
* @param scale target scale
*/
public static final void render(final Patch patch, final int coefficientsWidth, final int coefficientsHeight, final FloatProcessor targetImage, final FloatProcessor targetWeight, final ColorProcessor targetCoefficients, final double x, final double y, final double scale) {
/* assemble coordinate transformations and add bounding box offset */
final CoordinateTransformList<CoordinateTransform> ctl = new CoordinateTransformList<CoordinateTransform>();
ctl.add(patch.getFullCoordinateTransform());
final AffineModel2D affineScale = new AffineModel2D();
affineScale.set(scale, 0, 0, scale, -x * scale, -y * scale);
ctl.add(affineScale);
/* estimate average scale and generate downsampled source */
final int width = patch.getOWidth(), height = patch.getOHeight();
final double s = sampleAverageScale(ctl, width, height, width / patch.getMeshResolution());
final int mipmapLevel = bestMipmapLevel(s);
final ImageProcessor ipMipmap = Downsampler.downsampleImageProcessor(patch.getImageProcessor(), mipmapLevel);
/* create a target */
final ImageProcessor tp = ipMipmap.createProcessor(targetImage.getWidth(), targetImage.getHeight());
/* prepare and downsample alpha mask if there is one */
final ByteProcessor bpMaskMipmap;
final ByteProcessor bpMaskTarget;
final ByteProcessor bpMask = patch.getAlphaMask();
if (bpMask == null) {
bpMaskMipmap = null;
bpMaskTarget = null;
} else {
bpMaskMipmap = bpMask == null ? null : Downsampler.downsampleByteProcessor(bpMask, mipmapLevel);
bpMaskTarget = new ByteProcessor(tp.getWidth(), tp.getHeight());
}
/* create coefficients map */
final ColorProcessor cp = new ColorProcessor(ipMipmap.getWidth(), ipMipmap.getHeight());
final int w = cp.getWidth();
final int h = cp.getHeight();
for (int yi = 0; yi < h; ++yi) {
final int yc = yi * coefficientsHeight / h;
final int ic = yc * coefficientsWidth;
final int iyi = yi * w;
for (int xi = 0; xi < w; ++xi) cp.set(iyi + xi, ic + (xi * coefficientsWidth / w) + 1);
}
/* attach mipmap transformation */
final CoordinateTransformList<CoordinateTransform> ctlMipmap = new CoordinateTransformList<CoordinateTransform>();
ctlMipmap.add(createScaleLevelTransform(mipmapLevel));
ctlMipmap.add(ctl);
/* create mesh */
final CoordinateTransformMesh mesh = new CoordinateTransformMesh(ctlMipmap, patch.getMeshResolution(), ipMipmap.getWidth(), ipMipmap.getHeight());
/* render */
final ImageProcessorWithMasks source = new ImageProcessorWithMasks(ipMipmap, bpMaskMipmap, null);
final ImageProcessorWithMasks target = new ImageProcessorWithMasks(tp, bpMaskTarget, null);
final TransformMeshMappingWithMasks<TransformMesh> mapping = new TransformMeshMappingWithMasks<TransformMesh>(mesh);
mapping.mapInterpolated(source, target, 1);
final TransformMeshMapping<TransformMesh> coefficientsMapMapping = new TransformMeshMapping<TransformMesh>(mesh);
coefficientsMapMapping.map(cp, targetCoefficients, 1);
/* set alpha channel */
final byte[] alphaPixels;
if (bpMaskTarget != null)
alphaPixels = (byte[]) bpMaskTarget.getPixels();
else
alphaPixels = (byte[]) target.outside.getPixels();
/* convert */
final double min = patch.getMin();
final double max = patch.getMax();
final double a = 1.0 / (max - min);
final double b = 1.0 / 255.0;
for (int i = 0; i < alphaPixels.length; ++i) targetImage.setf(i, (float) ((tp.getf(i) - min) * a));
for (int i = 0; i < alphaPixels.length; ++i) targetWeight.setf(i, (float) ((alphaPixels[i] & 0xff) * b));
}
Aggregations