Skip to content

Commit

Permalink
Merge pull request #22 from ebremer/develop
Browse files Browse the repository at this point in the history
Add error.log file
  • Loading branch information
ebremer authored Feb 22, 2024
2 parents f54a55c + 3aba468 commit 3470352
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 69 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Hatch 3.1.2

# Hatch 3.2.0

This tool converts the largest image in a VSI, SVS, or TIF image into a new TIFF image with a freshly created image pyramid with each scaling 1/2 dimensions each scale.

Expand Down
7 changes: 4 additions & 3 deletions dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>edu.stonybrook.bmi</groupId>
<artifactId>hatch</artifactId>
<version>3.1.2</version>
<version>3.2.0</version>
<build>
<resources>
<resource>
Expand Down Expand Up @@ -45,7 +45,7 @@
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<version>3.5.1</version>
<executions>
<execution>
<phase>package</phase>
Expand Down Expand Up @@ -87,7 +87,7 @@
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>0.9.27</version>
<version>0.10.0</version>
<extensions>true</extensions>
<executions>
<execution>
Expand Down Expand Up @@ -130,6 +130,7 @@
</repositories>
<properties>
<maven.compiler.target>21</maven.compiler.target>
<twelvemonkeys.version>3.10.1</twelvemonkeys.version>
<maven.compiler.source>21</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<ver.bioformats>7.0.0</ver.bioformats>
Expand Down
12 changes: 9 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
<modelVersion>4.0.0</modelVersion>
<groupId>edu.stonybrook.bmi</groupId>
<artifactId>hatch</artifactId>
<version>3.1.2</version>
<version>3.2.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<ver.bioformats>7.0.0</ver.bioformats>
<twelvemonkeys.version>3.10.1</twelvemonkeys.version>
</properties>
<dependencies>
<dependency>
Expand All @@ -29,6 +30,11 @@
<artifactId>jcommander</artifactId>
<version>1.82</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-tiff</artifactId>
<version>${twelvemonkeys.version}</version>
</dependency>
</dependencies>
<build>
<resources>
Expand Down Expand Up @@ -87,7 +93,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<version>3.5.1</version>
<configuration>
<shadedArtifactAttached>false</shadedArtifactAttached>
<transformers>
Expand Down Expand Up @@ -129,7 +135,7 @@
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>0.9.27</version>
<version>0.10.0</version>
<extensions>true</extensions>
<executions>
<execution>
Expand Down
78 changes: 54 additions & 24 deletions src/main/java/edu/stonybrook/bmi/hatch/Hatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.stream.Stream;

Expand All @@ -20,14 +21,29 @@
* @author erich
*/
public class Hatch {
public static String software = "hatch 3.1.2 by Wing-n-Beak";
public static String software = "hatch 3.2.0 by Wing-n-Beak";
private static final String[] ext = new String[] {".vsi", ".svs", ".tif"};
private static final String errorlog = "error.log";
private static Logger LOGGER;
public static final String HELP = Hatch.software+"\n"+
"""
usage: hatch <src> <dest>
-v : verbose
""";

public Hatch() {
LOGGER = Logger.getLogger(Hatch.class.getName());
}

static {
try {
LogManager.getLogManager().readConfiguration(Hatch.class.getResourceAsStream("/logging.properties"));
} catch (IOException | SecurityException | ExceptionInInitializerError ex) {
Logger.getLogger(Hatch.class.getName()).log(Level.SEVERE, "Failed to read logging.properties file", ex);
}
LOGGER = Logger.getLogger(Hatch.class.getName());
}

private static void Traverse(HatchParameters params) {
Path s = params.src.toPath();
Path d = params.dest.toPath();
Expand All @@ -48,28 +64,23 @@ private static void Traverse(HatchParameters params) {
String frag = s.relativize(f).toString();
frag = frag.substring(0,frag.length()-4)+".tif";
Path t = Path.of(d.toString(), frag);
if (!t.toFile().exists()) {
System.out.println("PROCESSING FILE --> "+f);
engine.submit(new FileProcessor(params, f, t));
} else if (t.toFile().length()==0) {
System.out.println("ZERO LENGTH FILE --> "+f);
if (t.toFile().exists()&&params.overwrite) {
t.toFile().delete();
System.out.println("RE-PROCESSING FILE --> "+f);
engine.submit(new FileProcessor(params, f, t));
}
engine.submit(new FileProcessor(params, f, t));
});
} catch (IOException ex) {
Logger.getLogger(Hatch.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
LOGGER.severe("FILE PROCESSOR ERROR --> "+params.src.toString()+" "+params.dest.toString()+" "+ex.toString());
}
int cc = engine.getActiveCount()+engine.getQueue().size();
while ((engine.getActiveCount()+engine.getQueue().size())>0) {
int curr = engine.getActiveCount()+engine.getQueue().size();
if (cc!=curr) {
cc=curr;
if (params.verbose) System.out.println("All jobs submitted...waiting for "+curr);
if (params.verbose) LOGGER.log(Level.INFO,"All jobs submitted...waiting for "+curr);
}
}
System.out.println("Engine shutdown");
LOGGER.info("Engine shutdown");
engine.shutdown();
}

Expand All @@ -79,13 +90,17 @@ private static String getFileNameBase(File file) {
}

public static void main(String[] args) {
LOGGER.setLevel(Level.SEVERE);
loci.common.DebugTools.setRootLevel("WARN");
HatchParameters params = new HatchParameters();
JCommander jc = JCommander.newBuilder().addObject(params).build();
jc.setProgramName(Hatch.software+"\nhatch");
try {
jc.parse(args);
System.out.println(params);
LOGGER.log(Level.INFO,params.toString());
if (params.verbose) {
LOGGER.setLevel(Level.INFO);
}
if (params.isHelp()) {
jc.usage();
System.exit(0);
Expand Down Expand Up @@ -118,7 +133,7 @@ public static void main(String[] args) {
try (X2TIF v2t = new X2TIF(params, params.src.toString(), params.dest.toString(), series)){
v2t.Execute();
} catch (Exception ex) {
Logger.getLogger(Hatch.class.getName()).log(Level.SEVERE, null, ex);
LOGGER.severe("FILE PROCESSOR ERROR --> "+params.src.toString()+" "+params.dest.toString()+" "+ex.toString());
}
}
} else {
Expand All @@ -128,15 +143,15 @@ public static void main(String[] args) {
try (X2TIF v2t = new X2TIF(params, params.src.toString(), dest.toString(), null);) {
v2t.Execute();
} catch (Exception ex) {
Logger.getLogger(Hatch.class.getName()).log(Level.SEVERE, null, ex);
LOGGER.severe("FILE PROCESSOR ERROR --> "+params.src.toString()+" "+params.dest.toString()+" "+ex.toString());
}
} else {
params.series.forEach(s->{
File dest = Path.of(params.dest.toString(),getFileNameBase(params.src)+"-series-"+s+".tif").toFile();
try (X2TIF v2t = new X2TIF(params, params.src.toString(), dest.toString(), Integer.valueOf(s));) {
v2t.Execute();
} catch (Exception ex) {
Logger.getLogger(Hatch.class.getName()).log(Level.SEVERE, null, ex);
LOGGER.severe("FILE PROCESSOR ERROR --> "+params.src.toString()+" "+params.dest.toString()+" "+ex.toString());
}
});
}
Expand All @@ -147,15 +162,15 @@ public static void main(String[] args) {
try (X2TIF v2t = new X2TIF(params, params.src.toString(), params.dest.toString(), null);) {
v2t.Execute();
} catch (Exception ex) {
Logger.getLogger(Hatch.class.getName()).log(Level.SEVERE, null, ex);
LOGGER.severe("FILE PROCESSOR ERROR --> "+params.src.toString()+" "+params.dest.toString()+" "+ex.toString());
}
}
}
} else {
System.out.println(params.src.toString()+" does not exist!");
LOGGER.severe(params.src.toString()+" does not exist!");
}
} catch (ParameterException ex) {
System.out.println(ex.getMessage());
LOGGER.severe("FILE PROCESSOR ERROR --> "+params.src.toString()+" "+params.dest.toString()+" "+ex.toString());
}
}
}
Expand All @@ -164,25 +179,40 @@ class FileProcessor implements Callable<String> {
private final HatchParameters params;
private final File src;
private final File dest;
private static Logger LOGGER;

public FileProcessor(HatchParameters params, Path src, Path dest) {
this.params = params;
this.src = src.toFile();
this.dest = dest.toFile();
LOGGER = Logger.getLogger(Hatch.class.getName());
}

@Override
public String call() {
if (dest.exists()) {
dest.delete();
} else {
dest.getParentFile().mkdirs();
if (params.overwrite) {
dest.delete();
} else {
if (params.retry) {
if (!Validate2.file(dest.toPath())) {
dest.delete();
}
} else {
if (params.validate) {
Validate2.file(dest.toPath());
}
return null;
}
}
}
dest.getParentFile().mkdirs();
try (X2TIF v2t = new X2TIF(params, src.toString(), dest.toString(), null)) {
v2t.Execute();
} catch (Exception ex) {
System.out.println("FILE PROCESSOR ERROR --> "+src+" "+dest+" "+ex.toString());
}
LOGGER.severe("FILE PROCESSOR ERROR --> "+src+" "+dest+" "+ex.toString());
}
Validate2.file(dest.toPath());
return null;
}
}
9 changes: 9 additions & 0 deletions src/main/java/edu/stonybrook/bmi/hatch/HatchParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ public boolean isHelp() {
@Parameter(names = {"-v","-verbose"})
public boolean verbose = false;

@Parameter(names = {"-o","-overwrite"})
public boolean overwrite = false;

@Parameter(names = {"-r","-retry"})
public boolean retry = false;

@Parameter(names = {"-validate"})
public boolean validate = false;

@Parameter(names = "-jp2", hidden = true)
public boolean jp2 = false;

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/edu/stonybrook/bmi/hatch/SingleLineFormatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package edu.stonybrook.bmi.hatch;

import java.util.logging.Formatter;
import java.util.logging.LogRecord;

public class SingleLineFormatter extends Formatter {

@Override
public String format(LogRecord record) {
return String.format("%1$tF %1$tT %2$s %3$s: %4$s %n",
record.getMillis(),
record.getLevel().getLocalizedName(),
record.getSourceClassName() + "." + record.getSourceMethodName(),
formatMessage(record));
}
}

74 changes: 74 additions & 0 deletions src/main/java/edu/stonybrook/bmi/hatch/Validate2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package edu.stonybrook.bmi.hatch;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;

/**
*
* @author erich
*/
public class Validate2 {

public static boolean file(Path path) {
Logger LOGGER = Logger.getLogger(Validate2.class.getName());
javax.imageio.ImageReader reader = null;
ImageInputStream input;
try {
input = ImageIO.createImageInputStream(path.toFile());
Iterator<javax.imageio.ImageReader> readers = ImageIO.getImageReadersByFormatName("tif");
javax.imageio.ImageReader ir = null;
while (readers.hasNext()) {
ir = readers.next();
//System.out.println("IMAGE CLASS --> "+ir.getClass().getCanonicalName());
if ("com.twelvemonkeys.imageio.plugins.tiff.TIFFImageReader".equals(ir.getClass().getCanonicalName())) {
//System.out.println("YES! : "+ir.getClass().getCanonicalName());
reader = ir;
} else {
//System.out.println("NOPE : "+ir.getClass().getCanonicalName());
}
}
if (ir==null) {
throw new IllegalArgumentException("No reader for: " + path);
}
//System.out.println("READER IS ---> "+reader.getClass().getCanonicalName());
reader.setInput(input);
try {
if (reader.getWidth(0)==0) {
LOGGER.severe(path.toString()+" X dimension is ZERO");
return false;
}
} catch (IOException ex) {
Logger.getLogger(Validate2.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
try {
if (reader.getHeight(0)==0) {
LOGGER.severe(path.toString()+" Y dimension is ZERO");
return false;
}
} catch (IOException ex) {
LOGGER.severe(ex.getMessage());
return false;
}
try {
int last = reader.getNumImages(true)-1;
if ((reader.getWidth(last)>1024)||(reader.getHeight(last)>1024)) {
LOGGER.severe(path.toString()+" smallest scaled image ("+reader.getNumImages(true)+" - "+reader.getWidth(last)+"x"+reader.getHeight(last)+") must be less than 1024x1024");
return false;
}
} catch (IOException ex) {
LOGGER.severe(ex.getMessage());
return false;
}
} catch (IOException ex) {
LOGGER.severe(ex.getMessage());
return false;
}
return true;
}
}
Loading

0 comments on commit 3470352

Please sign in to comment.