-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathRenderedCanvasMipmapSource.java
496 lines (416 loc) · 23.1 KB
/
RenderedCanvasMipmapSource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
package org.janelia.alignment.mipmap;
import ij.process.ByteProcessor;
import ij.process.ColorProcessor;
import ij.process.FloatProcessor;
import ij.process.ImageProcessor;
import ij.process.ShortProcessor;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import mpicbg.models.AffineModel2D;
import mpicbg.models.CoordinateTransform;
import mpicbg.models.CoordinateTransformList;
import mpicbg.trakem2.transform.TransformMeshMappingWithMasks.ImageProcessorWithMasks;
import org.janelia.alignment.ChannelMap;
import org.janelia.alignment.RenderParameters;
import org.janelia.alignment.RenderTransformMesh;
import org.janelia.alignment.RenderTransformMeshMappingWithMasks;
import org.janelia.alignment.TransformableCanvas;
import org.janelia.alignment.Utils;
import org.janelia.alignment.mapper.MultiChannelMapper;
import org.janelia.alignment.mapper.MultiChannelWithAlphaMapper;
import org.janelia.alignment.mapper.MultiChannelWithBinaryMaskMapper;
import org.janelia.alignment.mapper.PixelMapper;
import org.janelia.alignment.mapper.SingleChannelMapper;
import org.janelia.alignment.mapper.SingleChannelWithAlphaMapper;
import org.janelia.alignment.mapper.SingleChannelWithBinaryMaskMapper;
import org.janelia.alignment.mapper.SingleColorChannelWithAlphaMapper;
import org.janelia.alignment.spec.TileSpec;
import org.janelia.alignment.util.ImageProcessorCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A {@link MipmapSource} implementation that renders a canvas composed from
* a list of other {@link TransformableCanvas transformed sources}.
*
* @author Stephan Saalfeld
* @author Eric Trautman
*/
public class RenderedCanvasMipmapSource
implements MipmapSource {
private final String canvasName;
private final Set<String> channelNames;
private final List<TransformableCanvas> canvasList;
private final double x;
private final double y;
private final int fullScaleWidth;
private final int fullScaleHeight;
private final double meshCellSize;
private final double levelZeroScale;
private final int numberOfMappingThreads;
private final boolean skipInterpolation;
private final boolean hasMasks;
private final boolean binaryMask;
/**
* Constructs a canvas based upon {@link RenderParameters} that is dynamically
* rendered when {@link #getChannels} is called.
*
* @param renderParameters parameters specifying tiles, transformations, and render context.
* @param imageProcessorCache cache of previously loaded pixel data (or null if caching is not desired).
*/
public RenderedCanvasMipmapSource(final RenderParameters renderParameters,
final ImageProcessorCache imageProcessorCache) {
this("canvas",
renderParameters.getChannelNames(),
buildCanvasList(renderParameters, imageProcessorCache),
renderParameters.getX(),
renderParameters.getY(),
renderParameters.getWidth(),
renderParameters.getHeight(),
renderParameters.getRes(renderParameters.getScale()),
renderParameters.getScale(),
renderParameters.getNumberOfThreads(),
renderParameters.skipInterpolation(),
renderParameters.hasMasks(),
renderParameters.binaryMask());
}
/**
* Constructs a canvas composed of {@link TransformableCanvas transformed sources}
* that is dynamically rendered when {@link #getChannels} is called.
*
* @param canvasName name of this canvas.
* @param channelNames names of channels to include in this canvas.
* @param canvasList list of transformed components to render.
* @param x left coordinate for this canvas.
* @param y top coordinate for this canvas.
* @param fullScaleWidth canvas width at mipmap level 0.
* @param fullScaleHeight canvas height at mipmap level 0.
* @param meshCellSize desired size of a mesh cell (triangle) in pixels.
* @param levelZeroScale scale factor for transformed components at mipmap level 0 of this canvas.
* @param numberOfMappingThreads number of threads to use for pixel mapping.
* @param skipInterpolation enable sloppy but fast rendering by skipping interpolation.
* @param hasMasks true if this canvas contains at least one source with a mask.
* @param binaryMask render only 100% opaque pixels.
*/
public RenderedCanvasMipmapSource(final String canvasName,
final Set<String> channelNames,
final List<TransformableCanvas> canvasList,
final double x,
final double y,
final int fullScaleWidth,
final int fullScaleHeight,
final double meshCellSize,
final double levelZeroScale,
final int numberOfMappingThreads,
final boolean skipInterpolation,
final boolean hasMasks,
final boolean binaryMask) {
this.canvasName = canvasName;
this.channelNames = channelNames;
this.canvasList = canvasList;
this.x = x;
this.y = y;
this.fullScaleWidth = fullScaleWidth;
this.fullScaleHeight = fullScaleHeight;
this.meshCellSize = meshCellSize;
this.levelZeroScale = levelZeroScale;
this.numberOfMappingThreads = numberOfMappingThreads;
this.skipInterpolation = skipInterpolation;
this.hasMasks = hasMasks;
this.binaryMask = binaryMask;
}
@Override
public String getSourceName() {
return canvasName;
}
@Override
public int getFullScaleWidth() {
return fullScaleWidth;
}
@Override
public int getFullScaleHeight() {
return fullScaleHeight;
}
@Override
public ChannelMap getChannels(final int mipmapLevel)
throws IllegalArgumentException {
final ChannelMap targetChannels = new ChannelMap();
final double levelScale = (1.0 / Math.pow(2.0, mipmapLevel)) * levelZeroScale;
long totalScaleDerivationTime = 0;
for (final TransformableCanvas canvas : canvasList) {
final long scaleDerivationStart = System.currentTimeMillis();
final CoordinateTransformList<CoordinateTransform> renderTransformList =
addRenderScaleAndOffset(canvas.getTransformList(), levelZeroScale, levelScale, x, y);
final MipmapSource source = canvas.getSource();
final double averageScale = Utils.sampleAverageScale(renderTransformList,
source.getFullScaleWidth(),
source.getFullScaleHeight(),
meshCellSize);
final int componentMipmapLevel = Utils.bestMipmapLevel(averageScale);
totalScaleDerivationTime += (System.currentTimeMillis() - scaleDerivationStart);
final ChannelMap sourceChannels = source.getChannels(componentMipmapLevel);
// setup target channels based upon first source channel
if ((targetChannels.size() == 0) && (sourceChannels.size() > 0)) {
setupTargetChannels(sourceChannels, levelScale, targetChannels);
}
mapPixels(source,
sourceChannels,
componentMipmapLevel,
renderTransformList,
meshCellSize,
hasMasks,
binaryMask,
numberOfMappingThreads,
skipInterpolation,
targetChannels);
}
LOG.debug("getChannels: deriving average scale for {} canvases took {} milliseconds",
canvasList.size(),
totalScaleDerivationTime);
return targetChannels;
}
private void setupTargetChannels(final ChannelMap sourceChannels,
final double levelScale,
final ChannelMap targetChannels) {
final int levelWidth = (int) ((fullScaleWidth * levelScale) + 0.5);
final int levelHeight = (int) ((fullScaleHeight * levelScale) + 0.5);
for (final String channelName : channelNames) {
final ImageProcessorWithMasks sourceChannel = sourceChannels.get(channelName);
final ImageProcessor sourceProcessor = sourceChannel.ip;
final ImageProcessor targetProcessor;
if (sourceProcessor instanceof ByteProcessor) {
targetProcessor = new ByteProcessor(levelWidth, levelHeight);
} else if (sourceProcessor instanceof ShortProcessor) {
targetProcessor = new ShortProcessor(levelWidth, levelHeight);
} else if (sourceProcessor instanceof FloatProcessor) {
targetProcessor = new FloatProcessor(levelWidth, levelHeight);
} else if (sourceProcessor instanceof ColorProcessor) {
targetProcessor = new ColorProcessor(levelWidth, levelHeight);
} else {
throw new IllegalArgumentException("conversion to " + sourceProcessor.getClass() + " is not supported");
}
final ImageProcessor mask = hasMasks ? new ByteProcessor(levelWidth, levelHeight) : null;
targetChannels.put(channelName,
new ImageProcessorWithMasks(targetProcessor,
mask,
null));
}
}
/**
* @return a list of {@link TransformableCanvas} objects for the specified parameters.
*/
public static List<TransformableCanvas> buildCanvasList(final RenderParameters renderParameters,
final ImageProcessorCache imageProcessorCache) {
final Set<String> channelNames = renderParameters.getChannelNames();
final List<TransformableCanvas> canvasList = new ArrayList<>(renderParameters.numberOfTileSpecs());
MipmapSource source;
for (final TileSpec tileSpec : renderParameters.getTileSpecs()) {
source = new UrlMipmapSource("tile '" + tileSpec.getTileId() + "'",
tileSpec.getWidth(),
tileSpec.getHeight(),
tileSpec.getChannels(channelNames),
renderParameters.getMinIntensity(),
renderParameters.getMaxIntensity(),
renderParameters.excludeMask(),
imageProcessorCache);
if (renderParameters.hasFilters()) {
source = new FilteredMipmapSource("filtered " + source.getSourceName(),
source,
renderParameters.getFilters());
}
canvasList.add(new TransformableCanvas(source, tileSpec.getTransforms().getNewInstanceAsList()));
}
return canvasList;
}
/**
* Modifies the specified full scale transform list for the current render context by
* adding a transform for bounding box offset, scale, and an area offset (for scaled mipmaps).
*
* @param renderTransformList list of transforms for full scale (and un-clipped) canvas.
* @param levelZeroScale scale factor for transformed components at mipmap level 0.
* @param actualMipmapScale scale factor for transformed components at desired mipmap level.
* @param x target image left coordinate.
* @param y target image top coordinate.
*/
public static CoordinateTransformList<CoordinateTransform> addRenderScaleAndOffset(
final CoordinateTransformList<CoordinateTransform> renderTransformList,
final double levelZeroScale,
final double actualMipmapScale,
final double x,
final double y) {
final AffineModel2D scaleAndOffset = new AffineModel2D();
// always calculate areaOffset for mipmaps
final double areaOffset = (1 - (actualMipmapScale / levelZeroScale)) * 0.5;
scaleAndOffset.set(actualMipmapScale,
0,
0,
actualMipmapScale,
-(x * actualMipmapScale + areaOffset),
-(y * actualMipmapScale + areaOffset));
renderTransformList.add(scaleAndOffset);
return renderTransformList;
}
/**
* Creates a mesh that incorporates a scale transform based upon the mipmap level
* along with the transforms for the render context.
*
* @param mipmapLevel source mipmap level.
* @param renderTransformList list of transforms for the render context.
* @param fullScaleWidth full scale width of the source.
* @param meshCellSize desired size of a mesh cell (triangle) in pixels.
* @param mipmapWidth width of the source mipmap.
* @param mipmapHeight height of the source mipmap.
*
* @return mesh for mapping pixels.
*/
public static RenderTransformMesh createRenderMesh(final int mipmapLevel,
final CoordinateTransformList<CoordinateTransform> renderTransformList,
final int fullScaleWidth,
final double meshCellSize,
final int mipmapWidth,
final int mipmapHeight) {
// attach mipmap transformation
final CoordinateTransformList<CoordinateTransform> mipmapLevelTransformList = new CoordinateTransformList<>();
mipmapLevelTransformList.add(Utils.createScaleLevelTransform(mipmapLevel));
mipmapLevelTransformList.add(renderTransformList);
// create mesh
final RenderTransformMesh mesh = new RenderTransformMesh(
mipmapLevelTransformList,
(int) (fullScaleWidth / meshCellSize + 0.5),
mipmapWidth,
mipmapHeight);
mesh.updateAffines();
return mesh;
}
/**
* Maps pixels from a source to a target.
*
* @param source source pixel data.
* @param sourceChannels channels extracted from the source.
* @param mipmapLevel source mipmap level.
* @param renderTransformList list of transforms for the render context.
* @param meshCellSize desired size of a mesh cell (triangle) in pixels.
* @param canvasHasMasks true if at least one source in the larger canvas being rendered has a mask
* (even if this source does not have a mask).
* @param binaryMask render only 100% opaque pixels.
* @param numberOfMappingThreads number of threads to use for pixel mapping.
* @param skipInterpolation enable sloppy but fast rendering by skipping interpolation.
* @param targetChannels target channels for mapped results.
*/
private static void mapPixels(final MipmapSource source,
final ChannelMap sourceChannels,
final int mipmapLevel,
final CoordinateTransformList<CoordinateTransform> renderTransformList,
final double meshCellSize,
final boolean canvasHasMasks,
final boolean binaryMask,
final int numberOfMappingThreads,
final boolean skipInterpolation,
final ChannelMap targetChannels) {
if (sourceChannels.size() > 0) {
final long mapStart = System.currentTimeMillis();
// all channels should have same size, so we only need to look at the first channel
final ImageProcessorWithMasks firstChannel = sourceChannels.getFirstChannel();
final int mipmapWidth = firstChannel.ip.getWidth();
final int mipmapHeight = firstChannel.ip.getHeight();
if (canvasHasMasks) {
// add empty (inverted) source mask for each channel if it does not already exist
for (final ImageProcessorWithMasks sourceChannel : sourceChannels.values()) {
if (sourceChannel.mask == null) {
sourceChannel.mask = new ByteProcessor(sourceChannel.ip.getWidth(),
sourceChannel.ip.getHeight());
sourceChannel.mask.invert();
}
}
}
final PixelMapper tilePixelMapper = getPixelMapper(sourceChannels,
canvasHasMasks,
binaryMask,
skipInterpolation,
targetChannels);
final RenderTransformMesh mesh = createRenderMesh(mipmapLevel,
renderTransformList,
source.getFullScaleWidth(),
meshCellSize,
mipmapWidth,
mipmapHeight);
final long meshCreationStop = System.currentTimeMillis();
final RenderTransformMeshMappingWithMasks mapping = new RenderTransformMeshMappingWithMasks(mesh);
final String mapType = skipInterpolation ? "" : " interpolated";
mapping.map(tilePixelMapper, numberOfMappingThreads);
// apply source channel intensity ranges to corresponding target channels
for (final String channelName : targetChannels.names()) {
final ImageProcessorWithMasks sourceChannel = sourceChannels.get(channelName);
final ImageProcessorWithMasks targetChannel = targetChannels.get(channelName);
// TODO: is this the right thing to do when source tiles have different min/max range?
targetChannel.ip.setMinAndMax(sourceChannel.ip.getMin(), sourceChannel.ip.getMax());
}
final long mapStop = System.currentTimeMillis();
LOG.debug("mapPixels: mapping of {} took {} milliseconds to process (mesh:{}, map{}:{})",
source.getSourceName(),
mapStop - mapStart,
meshCreationStop - mapStart,
mapType,
mapStop - meshCreationStop);
}
}
/**
* @return {@link PixelMapper} instance "optimized" for mapping source channel(s) for
* a specific render context.
*/
private static PixelMapper getPixelMapper(final ChannelMap sourceChannels,
final boolean hasMask,
final boolean binaryMask,
final boolean skipInterpolation,
final ChannelMap targetChannels) {
final PixelMapper tilePixelMapper;
if (sourceChannels.size() > 1) {
if (hasMask) {
if (binaryMask) {
tilePixelMapper = new MultiChannelWithBinaryMaskMapper(sourceChannels,
targetChannels,
(! skipInterpolation));
} else {
tilePixelMapper = new MultiChannelWithAlphaMapper(sourceChannels,
targetChannels,
(! skipInterpolation));
}
} else {
tilePixelMapper = new MultiChannelMapper(sourceChannels,
targetChannels,
(! skipInterpolation));
}
} else {
final String channelName = sourceChannels.getFirstChannelName();
final ImageProcessorWithMasks sourceChannel = sourceChannels.get(channelName);
final ImageProcessorWithMasks targetChannel = targetChannels.get(channelName);
if (targetChannel != null) {
if (hasMask) {
if (binaryMask) {
tilePixelMapper = new SingleChannelWithBinaryMaskMapper(sourceChannel,
targetChannel,
(! skipInterpolation));
} else if (targetChannel.ip instanceof ColorProcessor) {
tilePixelMapper = new SingleColorChannelWithAlphaMapper(sourceChannel,
targetChannel,
(! skipInterpolation));
} else {
tilePixelMapper = new SingleChannelWithAlphaMapper(sourceChannel,
targetChannel,
(! skipInterpolation));
}
} else {
tilePixelMapper = new SingleChannelMapper(sourceChannel,
targetChannel,
(! skipInterpolation));
}
} else {
throw new IllegalArgumentException("The sole source channel (" + channelName +
") is missing from specified target channels (" +
targetChannels + ").");
}
}
return tilePixelMapper;
}
private static final Logger LOG = LoggerFactory.getLogger(RenderedCanvasMipmapSource.class);
}