Skip to content

Commit 48b3d0b

Browse files
authored
Merge pull request #4339 from melissalinkert/fake-informative-planes
Fake: add `labelPlanes` option to add human-readable labels to planes
2 parents 653561f + ed020a8 commit 48b3d0b

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

components/formats-bsd/src/loci/formats/in/FakeReader.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
import static ome.xml.model.Pixels.getPhysicalSizeYUnitXsdDefault;
3939
import static ome.xml.model.Pixels.getPhysicalSizeZUnitXsdDefault;
4040

41+
import java.awt.Font;
42+
import java.awt.Graphics2D;
43+
import java.awt.geom.Rectangle2D;
44+
import java.awt.image.BufferedImage;
45+
4146
import java.io.File;
4247
import java.io.IOException;
4348
import java.util.ArrayList;
@@ -64,6 +69,7 @@
6469
import loci.formats.FormatTools;
6570
import loci.formats.MetadataTools;
6671
import loci.formats.ResourceNamer;
72+
import loci.formats.gui.AWTImageTools;
6773
import loci.formats.meta.MetadataStore;
6874
import loci.formats.ome.OMEXMLMetadata;
6975
import loci.formats.services.OMEXMLService;
@@ -223,6 +229,7 @@ public class FakeReader extends FormatReader {
223229
// Misc. debugging
224230
private int sleepOpenBytes = 0;
225231
private int sleepInitFile = 0;
232+
private boolean labelPlanes = false;
226233

227234
static void sleep(String msg, int ms) {
228235
if (ms <= 0) return; // EARLY EXIT
@@ -418,6 +425,70 @@ public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h)
418425
}
419426
}
420427

428+
// if requested, add human-readable dimension and index data to the image
429+
if (labelPlanes) {
430+
final BufferedImage plane = AWTImageTools.openImage(buf, this, w, h);
431+
final Graphics2D g = plane.createGraphics();
432+
433+
// build list of text lines from planar information
434+
final ArrayList<TextLine> lines = new ArrayList<TextLine>();
435+
final Font font = g.getFont();
436+
lines.add(new TextLine(new Location(getCurrentFile()).getName(),
437+
font.deriveFont(16f), 5, -5));
438+
lines.add(new TextLine("Core index " + getCoreIndex(),
439+
font.deriveFont(16f), 5, 5));
440+
lines.add(new TextLine(getSizeX() + " x " + getSizeY(),
441+
font.deriveFont(Font.ITALIC, 16f), 20, 10));
442+
lines.add(new TextLine(getDimensionOrder(),
443+
font.deriveFont(Font.ITALIC, 14f), 30, 5));
444+
int space = 5;
445+
if (getSizeZ() > 1) {
446+
lines.add(new TextLine(
447+
"Focal plane = " + (zIndex + 1) + "/" + getSizeZ(), font, 20, space));
448+
space = 2;
449+
}
450+
if (getSizeC() > 1) {
451+
lines.add(new TextLine("Channel = " + (cIndex + 1) + "/" + getSizeC(),
452+
font, 20, space));
453+
space = 2;
454+
}
455+
if (getSizeT() > 1) {
456+
lines.add(new TextLine("Time point = " + (tIndex + 1) + "/" + getSizeT(),
457+
font, 20, space));
458+
space = 2;
459+
}
460+
461+
// draw text lines to image
462+
g.setColor(java.awt.Color.white);
463+
int yoff = BOX_SIZE; // start drawing below special pixels
464+
for (TextLine text : lines) {
465+
g.setFont(text.font);
466+
final Rectangle2D r =
467+
g.getFont().getStringBounds(text.line, g.getFontRenderContext());
468+
yoff += (int) r.getHeight() + text.ypad;
469+
g.drawString(text.line, text.xoff, yoff);
470+
}
471+
g.dispose();
472+
473+
// unpack pixel bytes from BufferedImage
474+
byte[][] pixelBytes = AWTImageTools.getPixelBytes(plane, little);
475+
if (interleaved) {
476+
for (int i=0; i<pixelBytes[0].length; i+=bpp) {
477+
for (int j=0; j<pixelBytes.length; j++) {
478+
System.arraycopy(pixelBytes[j], i, buf,
479+
i * pixelBytes.length + j * bpp, bpp);
480+
}
481+
}
482+
}
483+
else {
484+
for (int i=0; i<pixelBytes.length; i++) {
485+
System.arraycopy(pixelBytes[i], 0, buf,
486+
i * pixelBytes[0].length, pixelBytes[i].length);
487+
}
488+
}
489+
pixelBytes = null;
490+
}
491+
421492
return buf;
422493
}
423494

@@ -515,6 +586,7 @@ public void close(boolean fileOnly) throws IOException {
515586
plateCols = 0;
516587
fields = 0;
517588
plateAcqs = 0;
589+
labelPlanes = false;
518590
excitationWavelengths.clear();
519591
emissionWavelengths.clear();
520592
super.close(fileOnly);
@@ -783,6 +855,8 @@ else if (key.startsWith("color_")) {
783855
sleepOpenBytes = intValue;
784856
} else if (key.equals("sleepInitFile")) {
785857
sleepInitFile = intValue;
858+
} else if (key.equals("labelPlanes")) {
859+
labelPlanes = boolValue;
786860
}
787861
}
788862

@@ -1589,4 +1663,24 @@ private Length parseWavelength(String s, String defaultUnit) {
15891663
return wavelength;
15901664
}
15911665

1666+
1667+
// -- Helper classes --
1668+
1669+
private static class TextLine {
1670+
1671+
final String line;
1672+
final Font font;
1673+
final int xoff;
1674+
final int ypad;
1675+
1676+
TextLine(final String line, final Font font, final int xoff, final int ypad)
1677+
{
1678+
this.line = line;
1679+
this.font = font;
1680+
this.xoff = xoff;
1681+
this.ypad = ypad;
1682+
}
1683+
1684+
}
1685+
15921686
}

0 commit comments

Comments
 (0)