Skip to content

Commit

Permalink
Backported r6714, r6716, r6948, and r6949 to 4.2. See #557.
Browse files Browse the repository at this point in the history
  • Loading branch information
melissalinkert committed Sep 16, 2010
1 parent 962e6c6 commit 1e57e92
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 27 deletions.
35 changes: 33 additions & 2 deletions components/loci-plugins/src/loci/plugins/in/ImagePlusReader.java
Expand Up @@ -55,6 +55,7 @@ Data Browser and Stack Slicer. Copyright (C) 2005-@year@ Melissa Linkert,
import loci.plugins.Slicer;
import loci.plugins.util.BFVirtualStack;
import loci.plugins.util.ImageProcessorReader;
import loci.plugins.util.LuraWave;
import loci.plugins.util.VirtualImagePlus;

/**
Expand Down Expand Up @@ -348,8 +349,7 @@ private ImageStack readPlanes(ImportProcess process, int s, List<LUT> luts)
updateTiming(s, i, current++, total);

// get image processor for ith plane
final ImageProcessor[] p = reader.openProcessors(i,
region.x, region.y, region.width, region.height);
final ImageProcessor[] p = readProcessors(process, i, region);
if (p == null || p.length == 0) {
throw new FormatException("Cannot read plane #" + i);
}
Expand All @@ -366,6 +366,37 @@ private ImageStack readPlanes(ImportProcess process, int s, List<LUT> luts)
return createStack(procs, labels, luts);
}

/**
* HACK: This method mainly exists to prompt the user for a missing
* LuraWave license code, in the case of LWF-compressed Flex.
*
* @see ImportProcess#setId()
*/
private ImageProcessor[] readProcessors(ImportProcess process,
int no, Region r) throws FormatException, IOException
{
final ImageProcessorReader reader = process.getReader();
final ImporterOptions options = process.getOptions();

boolean first = true;
for (int i=0; i<LuraWave.MAX_TRIES; i++) {
String code = LuraWave.initLicenseCode();
try {
return reader.openProcessors(no, r.x, r.y, r.width, r.height);
}
catch (FormatException exc) {
if (options.isQuiet() || options.isWindowless()) throw exc;
if (!LuraWave.isLicenseCodeException(exc)) throw exc;

// prompt user for LuraWave license code
code = LuraWave.promptLicenseCode(code, first);
if (code == null) throw exc;
if (first) first = false;
}
}
throw new FormatException(LuraWave.TOO_MANY_TRIES);
}

// -- Helper methods - image post processing --

private List<ImagePlus> concatenate(List<ImagePlus> imps) {
Expand Down
37 changes: 35 additions & 2 deletions components/loci-plugins/src/loci/plugins/in/ImportProcess.java
Expand Up @@ -53,6 +53,7 @@ Data Browser and Stack Slicer. Copyright (C) 2005-@year@ Melissa Linkert,
import loci.plugins.util.IJStatusEchoer;
import loci.plugins.util.ImageProcessorReader;
import loci.plugins.util.LociPrefs;
import loci.plugins.util.LuraWave;
import loci.plugins.util.VirtualReader;
import loci.plugins.util.WindowTools;
import ome.xml.model.enums.DimensionOrder;
Expand Down Expand Up @@ -467,7 +468,7 @@ private void initializeStack() throws FormatException, IOException {
r = minMaxCalculator = new MinMaxCalculator(r);
r = virtualReader = new VirtualReader(r);
reader = new ImageProcessorReader(r);
reader.setId(options.getId());
setId();

computeSeriesLabels(reader);
}
Expand Down Expand Up @@ -540,7 +541,9 @@ private void createBaseReader() throws FormatException, IOException {
if (options.isLocal() || options.isHTTP()) {
BF.status(options.isQuiet(), "Identifying " + idName);
imageReader = LociPrefs.makeImageReader();
try { baseReader = imageReader.getReader(options.getId()); }
try {
baseReader = imageReader.getReader(options.getId());
}
catch (FormatException exc) {
WindowTools.reportException(exc, options.isQuiet(),
"Sorry, there was an error reading the file.");
Expand All @@ -558,6 +561,8 @@ private void createBaseReader() throws FormatException, IOException {
cancel();
return;
}

// attach OME-XML metadata store
Exception exc = null;
try {
ServiceFactory factory = new ServiceFactory();
Expand Down Expand Up @@ -591,6 +596,34 @@ private void saveDefaults() {

// -- Helper methods -- ImportStep.STACK --

/**
* HACK: This method mainly exists to prompt the user for a missing
* LuraWave license code, in the case of LWF-compressed Flex.
*
* @see ImagePlusReader#readProcessors(ImportProcess, int, Region)
*/
private void setId() throws FormatException, IOException {
boolean first = true;
for (int i=0; i<LuraWave.MAX_TRIES; i++) {
String code = LuraWave.initLicenseCode();
try {
reader.setId(options.getId());
return;
}
catch (FormatException exc) {
if (options.isQuiet() || options.isWindowless()) throw exc;
if (!LuraWave.isLicenseCodeException(exc)) throw exc;

// prompt user for LuraWave license code
code = LuraWave.promptLicenseCode(code, first);
if (code == null) throw exc;
if (first) first = false;
reader.close();
}
}
throw new FormatException(LuraWave.TOO_MANY_TRIES);
}

private void computeSeriesLabels(IFormatReader r) {
final int seriesCount = r.getSeriesCount();
seriesLabels = new String[seriesCount];
Expand Down
Expand Up @@ -94,28 +94,7 @@ public ImageProcessor[] openProcessors(int no, int x, int y, int w, int h)
throws FormatException, IOException
{
// read byte array
byte[] b = null;
boolean first = true;
while (true) {
// TODO: This is the wrong place to prompt for the LuraWave code.
// This logic should be moved to a higher, GUI-specific level.

// read LuraWave license code, if available
String code = LuraWave.initLicenseCode();
try {
b = openBytes(no, x, y, w, h);
break;
}
catch (FormatException exc) {
if (LuraWave.isLicenseCodeException(exc)) {
// prompt user for LuraWave license code
code = LuraWave.promptLicenseCode(code, first);
if (code == null) return null;
if (first) first = false;
}
else throw exc;
}
}
byte[] b = openBytes(no, x, y, w, h);

int c = getRGBChannelCount();
int type = getPixelType();
Expand Down
8 changes: 7 additions & 1 deletion components/loci-plugins/src/loci/plugins/util/LuraWave.java
Expand Up @@ -39,6 +39,12 @@ Data Browser and Stack Slicer. Copyright (C) 2005-@year@ Melissa Linkert,
*/
public final class LuraWave {

// -- Constants --

public static final int MAX_TRIES = 5;
public static final String TOO_MANY_TRIES =
"Too many LuraWave license code attempts; giving up.";

// -- Constructor --

private LuraWave() { }
Expand All @@ -48,7 +54,7 @@ private LuraWave() { }
/** Reads LuraWave license code from ImageJ preferences, if available. */
public static String initLicenseCode() {
String code = Prefs.get(LuraWaveServiceImpl.LICENSE_PROPERTY, null);
if (code != null) {
if (code != null && code.length() >= 6) {
System.setProperty(LuraWaveServiceImpl.LICENSE_PROPERTY, code);
}
return code;
Expand Down

0 comments on commit 1e57e92

Please sign in to comment.