Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
0b3938c13f | |||
dceb58cf82 | |||
bc4be44174 | |||
f244530932 | |||
2448d489ad | |||
e84c50fcff | |||
4b56cfd9d8 | |||
82e1d3844c | |||
4c581052ab | |||
705e7f7e7d | |||
b00a67c5d0 | |||
e415ead1ec | |||
b8bfa77cc2 | |||
0df492ff14 | |||
4ea2dc7110 | |||
a4bd4b0b96 | |||
dab69b174f | |||
aa61113357 |
9
.vscode/extensions.json
vendored
Normal file
9
.vscode/extensions.json
vendored
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"recommendations": [
|
||||||
|
"esbenp.prettier-vscode",
|
||||||
|
"mosapride.zenkaku",
|
||||||
|
"redhat.fabric8-analytics",
|
||||||
|
"streetsidesoftware.code-spell-checker",
|
||||||
|
"vscjava.vscode-java-pack"
|
||||||
|
]
|
||||||
|
}
|
18
.vscode/settings.json
vendored
Normal file
18
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"editor.formatOnSave": true,
|
||||||
|
"editor.codeActionsOnSave": {
|
||||||
|
"source.organizeImports": "explicit"
|
||||||
|
},
|
||||||
|
"[jsonc]": {
|
||||||
|
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
||||||
|
},
|
||||||
|
// Extensions - Code Spell Checker
|
||||||
|
"cSpell.ignoreWords": ["endianness", "noflat", "nopix", "omexml"],
|
||||||
|
"cSpell.words": ["bioformats", "imageinfo", "neurodata", "riken"],
|
||||||
|
// Extentions - Prettier
|
||||||
|
"prettier.printWidth": 120,
|
||||||
|
"prettier.singleQuote": true,
|
||||||
|
"prettier.tabWidth": 4,
|
||||||
|
"java.configuration.updateBuildConfiguration": "interactive",
|
||||||
|
"java.compile.nullAnalysis.mode": "automatic"
|
||||||
|
}
|
25
README.md
25
README.md
@ -1,27 +1,40 @@
|
|||||||
# BioFormatsImageInfo
|
# BioFormatsImageInfo
|
||||||
|
|
||||||
Metadata extraction tool based on Bio-Formats
|
Metadata extraction tool based on Bio-Formats
|
||||||
|
|
||||||
### make package
|
## make package
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
mvn package
|
mvn package
|
||||||
```
|
```
|
||||||
|
|
||||||
### run
|
## run
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
./target/dist/bin/bioformats-imageinfo "[path to image file]"
|
./target/dist/bin/bioformats-imageinfo "[OPTION]" "[path to image file]"
|
||||||
```
|
```
|
||||||
|
|
||||||
### run by jar
|
### run by jar
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
java -jar ./target/dist/lib/bioformats-imageinfo-1.1.0.jar "[path to image file]"
|
java -jar ./target/dist/lib/bioformats-imageinfo-1.2.4.jar "[OPTION]" "[path to image file]"
|
||||||
```
|
```
|
||||||
|
|
||||||
### run by fat jar
|
### run by fat jar
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
java -jar ./target/bioformats-imageinfo-1.1.0-jar-with-dependencies.jar "[path to image file]"
|
java -jar ./target/bioformats-imageinfo-1.2.4-jar-with-dependencies.jar "[OPTION]" "[path to image file]"
|
||||||
```
|
```
|
||||||
|
|
||||||
### library usage
|
### OPTION
|
||||||
|
|
||||||
|
```shell
|
||||||
|
-M output metadata
|
||||||
|
-T output thumbnail
|
||||||
|
```
|
||||||
|
|
||||||
|
## library usage
|
||||||
|
|
||||||
```java
|
```java
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
113
pom.xml
113
pom.xml
@ -1,17 +1,17 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>jp.riken.neurodata.tools.BioFormatsImageInfo</groupId>
|
<groupId>jp.riken.neurodata.tools.BioFormatsImageInfo</groupId>
|
||||||
<artifactId>bioformats-imageinfo</artifactId>
|
<artifactId>bioformats-imageinfo</artifactId>
|
||||||
<version>1.1.0</version>
|
<version>1.2.4</version>
|
||||||
|
|
||||||
<name>bioformats-imageinfo</name>
|
<name>bioformats-imageinfo</name>
|
||||||
<url>https://neurodata.riken.jp</url>
|
<url>https://neurodata.riken.jp</url>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<bio-formats.version>6.10.1</bio-formats.version>
|
<bio-formats.version>7.3.0</bio-formats.version>
|
||||||
<bioformats-imageinfo.mainClass>jp.riken.neurodata.tools.BioFormatsImageInfo</bioformats-imageinfo.mainClass>
|
<bioformats-imageinfo.mainClass>jp.riken.neurodata.tools.BioFormatsImageInfo</bioformats-imageinfo.mainClass>
|
||||||
|
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
@ -27,14 +27,39 @@
|
|||||||
<artifactId>bio-formats-tools</artifactId>
|
<artifactId>bio-formats-tools</artifactId>
|
||||||
<version>${bio-formats.version}</version>
|
<version>${bio-formats.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-nop</artifactId>
|
||||||
|
<version>2.0.9</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-enforcer-plugin</artifactId>
|
||||||
|
<version>3.5.0</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>enforce-maven</id>
|
||||||
|
<goals>
|
||||||
|
<goal>enforce</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<rules>
|
||||||
|
<requireMavenVersion>
|
||||||
|
<version>3.2.5</version>
|
||||||
|
</requireMavenVersion>
|
||||||
|
</rules>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-dependency-plugin</artifactId>
|
<artifactId>maven-dependency-plugin</artifactId>
|
||||||
<version>3.3.0</version>
|
<version>3.6.1</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>copy-dependencies</id>
|
<id>copy-dependencies</id>
|
||||||
@ -44,59 +69,35 @@
|
|||||||
</goals>
|
</goals>
|
||||||
<configuration>
|
<configuration>
|
||||||
<overWriteIfNewer>true</overWriteIfNewer>
|
<overWriteIfNewer>true</overWriteIfNewer>
|
||||||
</configuration>
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-jar-plugin</artifactId>
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
<version>3.2.2</version>
|
<version>3.4.1</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<archive>
|
<archive>
|
||||||
<manifest>
|
<manifest>
|
||||||
<addClasspath>true</addClasspath>
|
<addClasspath>true</addClasspath>
|
||||||
<mainClass>${bioformats-imageinfo.mainClass}</mainClass>
|
<mainClass>${bioformats-imageinfo.mainClass}</mainClass>
|
||||||
</manifest>
|
</manifest>
|
||||||
</archive>
|
</archive>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-assembly-plugin</artifactId>
|
|
||||||
<version>3.4.1</version>
|
|
||||||
<configuration>
|
|
||||||
<descriptorRefs>
|
|
||||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
|
||||||
</descriptorRefs>
|
|
||||||
<archive>
|
|
||||||
<manifest>
|
|
||||||
<mainClass>${bioformats-imageinfo.mainClass}</mainClass>
|
|
||||||
</manifest>
|
|
||||||
</archive>
|
|
||||||
</configuration>
|
|
||||||
<executions>
|
|
||||||
<execution>
|
|
||||||
<id>make-assembly</id>
|
|
||||||
<phase>package</phase>
|
|
||||||
<goals>
|
|
||||||
<goal>single</goal>
|
|
||||||
</goals>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.codehaus.mojo</groupId>
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
<artifactId>appassembler-maven-plugin</artifactId>
|
<artifactId>appassembler-maven-plugin</artifactId>
|
||||||
<version>1.10</version>
|
<version>2.1.0</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<assembleDirectory>${project.build.directory}/dist</assembleDirectory>
|
<assembleDirectory>${project.build.directory}/dist</assembleDirectory>
|
||||||
<repositoryLayout>flat</repositoryLayout>
|
<repositoryLayout>flat</repositoryLayout>
|
||||||
<repositoryName>lib</repositoryName>
|
<repositoryName>lib</repositoryName>
|
||||||
<programs>
|
<programs>
|
||||||
<program>
|
<program>
|
||||||
<mainClass>${bioformats-imageinfo.mainClass}</mainClass>
|
<mainClass>${bioformats-imageinfo.mainClass}</mainClass>
|
||||||
<id>${project.name}</id>
|
<id>${project.name}</id>
|
||||||
</program>
|
</program>
|
||||||
</programs>
|
</programs>
|
||||||
</configuration>
|
</configuration>
|
||||||
@ -109,10 +110,37 @@
|
|||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-assembly-plugin</artifactId>
|
||||||
|
<version>3.7.1</version>
|
||||||
|
<configuration>
|
||||||
|
<descriptorRefs>
|
||||||
|
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||||
|
</descriptorRefs>
|
||||||
|
<archive>
|
||||||
|
<manifest>
|
||||||
|
<mainClass>${bioformats-imageinfo.mainClass}</mainClass>
|
||||||
|
</manifest>
|
||||||
|
</archive>
|
||||||
|
<descriptors>
|
||||||
|
<descriptor>src/main/assembly/assembly.xml</descriptor>
|
||||||
|
</descriptors>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>make-assembly</id>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>single</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.owasp</groupId>
|
<groupId>org.owasp</groupId>
|
||||||
<artifactId>dependency-check-maven</artifactId>
|
<artifactId>dependency-check-maven</artifactId>
|
||||||
<version>7.1.1</version>
|
<version>9.0.9</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<goals>
|
<goals>
|
||||||
@ -128,15 +156,18 @@
|
|||||||
<pluginRepository>
|
<pluginRepository>
|
||||||
<id>central</id>
|
<id>central</id>
|
||||||
<name>Central Repository</name>
|
<name>Central Repository</name>
|
||||||
<url>https://repo.maven.apache.org/maven2</url>
|
<!-- <url>https://repo.maven.apache.org/maven2</url> -->
|
||||||
|
<url>https://repo1.maven.org/maven2/</url>
|
||||||
<layout>default</layout>
|
<layout>default</layout>
|
||||||
</pluginRepository>
|
</pluginRepository>
|
||||||
</pluginRepositories>
|
</pluginRepositories>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
<repository>
|
<repository>
|
||||||
<id>central</id>
|
<id>central</id>
|
||||||
<name>Central Repository</name>
|
<name>Central Repository</name>
|
||||||
<url>https://repo.maven.apache.org/maven2</url>
|
<!-- <url>https://repo.maven.apache.org/maven2</url> -->
|
||||||
|
<url>https://repo1.maven.org/maven2/</url>
|
||||||
</repository>
|
</repository>
|
||||||
<repository>
|
<repository>
|
||||||
<id>ome</id>
|
<id>ome</id>
|
||||||
@ -149,4 +180,4 @@
|
|||||||
</repository>
|
</repository>
|
||||||
</repositories>
|
</repositories>
|
||||||
|
|
||||||
</project>
|
</project>
|
36
src/main/assembly/assembly.xml
Normal file
36
src/main/assembly/assembly.xml
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
|
||||||
|
<id>bin</id>
|
||||||
|
<formats>
|
||||||
|
<format>tar.gz</format>
|
||||||
|
<format>tar.bz2</format>
|
||||||
|
<format>zip</format>
|
||||||
|
</formats>
|
||||||
|
<fileSets>
|
||||||
|
<fileSet>
|
||||||
|
<directory>target/dist/bin</directory>
|
||||||
|
<outputDirectory>bin</outputDirectory>
|
||||||
|
<excludes>
|
||||||
|
<exclude>*.bat</exclude>
|
||||||
|
</excludes>
|
||||||
|
<directoryMode>0755</directoryMode>
|
||||||
|
<fileMode>0755</fileMode>
|
||||||
|
</fileSet>
|
||||||
|
<fileSet>
|
||||||
|
<directory>target/dist/bin</directory>
|
||||||
|
<outputDirectory>bin</outputDirectory>
|
||||||
|
<includes>
|
||||||
|
<include>*.bat</include>
|
||||||
|
</includes>
|
||||||
|
<directoryMode>0755</directoryMode>
|
||||||
|
<fileMode>0644</fileMode>
|
||||||
|
</fileSet>
|
||||||
|
</fileSets>
|
||||||
|
<dependencySets>
|
||||||
|
<dependencySet>
|
||||||
|
<outputDirectory>lib</outputDirectory>
|
||||||
|
</dependencySet>
|
||||||
|
</dependencySets>
|
||||||
|
</assembly>
|
@ -142,14 +142,14 @@ public class BioFormatsImageInfo {
|
|||||||
final int seriesCount = reader.getSeriesCount();
|
final int seriesCount = reader.getSeriesCount();
|
||||||
for (int j = 0; j < seriesCount; j++) {
|
for (int j = 0; j < seriesCount; j++) {
|
||||||
reader.setSeries(j);
|
reader.setSeries(j);
|
||||||
final Hashtable<String, Object> seriesMagedata = reader.getSeriesMetadata();
|
final Hashtable<String, Object> seriesMetadata = reader.getSeriesMetadata();
|
||||||
if (!seriesMagedata.isEmpty()) {
|
if (!seriesMetadata.isEmpty()) {
|
||||||
final Map<String, Object> seriesMetadata = new LinkedHashMap<String, Object>();
|
final Map<String, Object> originalMetadata = new LinkedHashMap<String, Object>();
|
||||||
final String[] keys = MetadataTools.keys(seriesMagedata);
|
final String[] keys = MetadataTools.keys(seriesMetadata);
|
||||||
for (int i = 0; i < keys.length; i++) {
|
for (int i = 0; i < keys.length; i++) {
|
||||||
seriesMetadata.put(keys[i], seriesMagedata.get(keys[i]));
|
originalMetadata.put(keys[i], seriesMetadata.get(keys[i]));
|
||||||
}
|
}
|
||||||
metadata.put(String.format("series[%d]", j), seriesMetadata);
|
metadata.put(String.format("series[%d]", j), originalMetadata);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -188,7 +188,7 @@ public class BioFormatsImageInfo {
|
|||||||
// json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(map);
|
// json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(map);
|
||||||
json = mapper.writeValueAsString(map);
|
json = mapper.writeValueAsString(map);
|
||||||
} catch (final Throwable e) {
|
} catch (final Throwable e) {
|
||||||
// return "null" if conversion error occured
|
// return "null" if conversion error occurred
|
||||||
json = "null";
|
json = "null";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -248,20 +248,37 @@ public class BioFormatsImageInfo {
|
|||||||
boolean status = true;
|
boolean status = true;
|
||||||
String message = "";
|
String message = "";
|
||||||
String format = "";
|
String format = "";
|
||||||
|
boolean metadataON = true;
|
||||||
|
boolean thumbnailON = true;
|
||||||
|
String path = "";
|
||||||
final Map<String, Object> metadata = new LinkedHashMap<String, Object>();
|
final Map<String, Object> metadata = new LinkedHashMap<String, Object>();
|
||||||
final Map<String, Object> thumbnail = new LinkedHashMap<String, Object>();
|
final Map<String, Object> thumbnail = new LinkedHashMap<String, Object>();
|
||||||
if (args.length != 1) {
|
if (args.length == 1) {
|
||||||
status = false;
|
path = args[0];
|
||||||
message = "filename argument required";
|
} else if (args.length == 2) {
|
||||||
|
if (args[0].startsWith("-")) {
|
||||||
|
metadataON = args[0].toUpperCase().contains("M");
|
||||||
|
thumbnailON = args[0].toUpperCase().contains("T");
|
||||||
|
}
|
||||||
|
path = args[1];
|
||||||
} else {
|
} else {
|
||||||
final String path = args[0];
|
status = false;
|
||||||
|
message = "invalid parameters";
|
||||||
|
message += "\nUsage ./target/dist/bin/bioformats-imageinfo \"[OPTION]\" \"[path to image file]\"";
|
||||||
|
message += "\n-M output metadata\n-T output thumbnail";
|
||||||
|
}
|
||||||
|
if (status) {
|
||||||
try {
|
try {
|
||||||
DebugTools.enableLogging("ERROR");
|
DebugTools.enableLogging("OFF");
|
||||||
format = readMetadata(path, metadata);
|
if (metadataON) {
|
||||||
BioFormatsImageThumbnail.readThumbnail(path, thumbnail);
|
format = readMetadata(path, metadata);
|
||||||
} catch (BioFormatsImageException e) {
|
}
|
||||||
|
if (thumbnailON) {
|
||||||
|
BioFormatsImageThumbnail.readThumbnail(path, thumbnail);
|
||||||
|
}
|
||||||
|
} catch (Throwable t) {
|
||||||
status = false;
|
status = false;
|
||||||
message = e.getMessage();
|
message = t.getMessage();
|
||||||
metadata.clear();
|
metadata.clear();
|
||||||
thumbnail.clear();
|
thumbnail.clear();
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@ import java.awt.Color;
|
|||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.Image;
|
import java.awt.Image;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.BufferedOutputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
@ -23,11 +22,12 @@ import javax.imageio.stream.ImageOutputStream;
|
|||||||
import loci.common.DebugTools;
|
import loci.common.DebugTools;
|
||||||
import loci.formats.FormatException;
|
import loci.formats.FormatException;
|
||||||
import loci.formats.ImageReader;
|
import loci.formats.ImageReader;
|
||||||
|
import loci.formats.gui.AWTImageTools;
|
||||||
import loci.formats.gui.BufferedImageReader;
|
import loci.formats.gui.BufferedImageReader;
|
||||||
|
|
||||||
public class BioFormatsImageThumbnail {
|
public class BioFormatsImageThumbnail {
|
||||||
|
|
||||||
protected static final float JPEG_QUORITY = 0.85f;
|
protected static final float JPEG_QUALITY = 0.85f;
|
||||||
protected static final int BACKGROUND_COLOR = 0x000000;
|
protected static final int BACKGROUND_COLOR = 0x000000;
|
||||||
protected static final double MAXIMUM_SCALE = 2.0;
|
protected static final double MAXIMUM_SCALE = 2.0;
|
||||||
|
|
||||||
@ -39,14 +39,14 @@ public class BioFormatsImageThumbnail {
|
|||||||
final double scaleHeight = (double) height / (double) imageHeight;
|
final double scaleHeight = (double) height / (double) imageHeight;
|
||||||
double scale = Math.min(scaleWidth, scaleHeight);
|
double scale = Math.min(scaleWidth, scaleHeight);
|
||||||
if (scale > MAXIMUM_SCALE) {
|
if (scale > MAXIMUM_SCALE) {
|
||||||
// limit scalling size to maximum scale
|
// limit scaling size to maximum scale
|
||||||
scale = MAXIMUM_SCALE;
|
scale = MAXIMUM_SCALE;
|
||||||
}
|
}
|
||||||
int resizeWidth = imageWidth;
|
int resizeWidth = imageWidth;
|
||||||
int resizeHeight = imageHeight;
|
int resizeHeight = imageHeight;
|
||||||
Image resizeImage = image;
|
Image resizeImage = image;
|
||||||
if (scale != 1.0) {
|
if (scale != 1.0) {
|
||||||
// resize image if dimension is different with requrested dimension.
|
// resize image if dimension is different with requested dimension.
|
||||||
resizeWidth = (int) (scale * (double) imageWidth);
|
resizeWidth = (int) (scale * (double) imageWidth);
|
||||||
resizeHeight = (int) (scale * (double) imageHeight);
|
resizeHeight = (int) (scale * (double) imageHeight);
|
||||||
resizeImage = image.getScaledInstance(resizeWidth, resizeHeight, Image.SCALE_AREA_AVERAGING);
|
resizeImage = image.getScaledInstance(resizeWidth, resizeHeight, Image.SCALE_AREA_AVERAGING);
|
||||||
@ -86,16 +86,18 @@ public class BioFormatsImageThumbnail {
|
|||||||
protected static byte[] getJpegByteArray(final BufferedImage image, final float quality, final int matColor)
|
protected static byte[] getJpegByteArray(final BufferedImage image, final float quality, final int matColor)
|
||||||
throws BioFormatsImageException {
|
throws BioFormatsImageException {
|
||||||
byte[] ret = null;
|
byte[] ret = null;
|
||||||
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
try (final ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||||
final ImageOutputStream ios = ImageIO.createImageOutputStream(baos);) {
|
final ImageOutputStream ios = ImageIO.createImageOutputStream(bos);) {
|
||||||
final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
|
final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
|
||||||
final ImageWriteParam param = writer.getDefaultWriteParam();
|
final ImageWriteParam param = writer.getDefaultWriteParam();
|
||||||
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
|
if (param.canWriteCompressed()) {
|
||||||
param.setCompressionQuality(quality);
|
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
|
||||||
|
param.setCompressionQuality(quality);
|
||||||
|
}
|
||||||
writer.setOutput(ios);
|
writer.setOutput(ios);
|
||||||
writer.write(null, new IIOImage(removeAlphaChannel(image, matColor), null, null), param);
|
writer.write(null, new IIOImage(removeAlphaChannel(image, matColor), null, null), param);
|
||||||
writer.dispose();
|
writer.dispose();
|
||||||
ret = baos.toByteArray();
|
ret = bos.toByteArray();
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
throw new BioFormatsImageException(e);
|
throw new BioFormatsImageException(e);
|
||||||
}
|
}
|
||||||
@ -105,13 +107,18 @@ public class BioFormatsImageThumbnail {
|
|||||||
|
|
||||||
protected static byte[] getPngByteArray(final BufferedImage image) throws BioFormatsImageException {
|
protected static byte[] getPngByteArray(final BufferedImage image) throws BioFormatsImageException {
|
||||||
byte[] ret = null;
|
byte[] ret = null;
|
||||||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
try (final ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||||
BufferedOutputStream bos = new BufferedOutputStream(baos);) {
|
final ImageOutputStream ios = ImageIO.createImageOutputStream(bos);) {
|
||||||
image.flush();
|
final ImageWriter writer = ImageIO.getImageWritersByFormatName("png").next();
|
||||||
ImageIO.write(image, "png", bos);
|
final ImageWriteParam param = writer.getDefaultWriteParam();
|
||||||
bos.flush();
|
if (param.canWriteCompressed()) {
|
||||||
bos.close();
|
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
|
||||||
ret = baos.toByteArray();
|
param.setCompressionQuality(0.0f);
|
||||||
|
}
|
||||||
|
writer.setOutput(ios);
|
||||||
|
writer.write(null, new IIOImage(image, null, null), param);
|
||||||
|
writer.dispose();
|
||||||
|
ret = bos.toByteArray();
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
throw new BioFormatsImageException(e);
|
throw new BioFormatsImageException(e);
|
||||||
}
|
}
|
||||||
@ -126,11 +133,14 @@ public class BioFormatsImageThumbnail {
|
|||||||
bytes = getPngByteArray(image);
|
bytes = getPngByteArray(image);
|
||||||
break;
|
break;
|
||||||
case "image/jpeg":
|
case "image/jpeg":
|
||||||
bytes = getJpegByteArray(image, JPEG_QUORITY, BACKGROUND_COLOR);
|
bytes = getJpegByteArray(image, JPEG_QUALITY, BACKGROUND_COLOR);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new BioFormatsImageException("Unsupported image format: " + mimeType);
|
throw new BioFormatsImageException("Unsupported image format: " + mimeType);
|
||||||
}
|
}
|
||||||
|
if (bytes.length == 0) {
|
||||||
|
throw new BioFormatsImageException("Failed to convert to " + mimeType + " format");
|
||||||
|
}
|
||||||
final String dataUri = "data:" + mimeType + ";base64," + Base64.getEncoder().encodeToString(bytes);
|
final String dataUri = "data:" + mimeType + ";base64," + Base64.getEncoder().encodeToString(bytes);
|
||||||
|
|
||||||
return dataUri;
|
return dataUri;
|
||||||
@ -147,6 +157,8 @@ public class BioFormatsImageThumbnail {
|
|||||||
reader.setId(path);
|
reader.setId(path);
|
||||||
} catch (FormatException | IOException e) {
|
} catch (FormatException | IOException e) {
|
||||||
throw new BioFormatsImageException("unsupported file: " + path);
|
throw new BioFormatsImageException("unsupported file: " + path);
|
||||||
|
} catch (Throwable t) {
|
||||||
|
throw new BioFormatsImageException(t);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
int series = 0;
|
int series = 0;
|
||||||
@ -159,19 +171,28 @@ public class BioFormatsImageThumbnail {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
reader.setSeries(series);
|
reader.setSeries(series);
|
||||||
final int no = 0;
|
final int z = reader.getSizeZ() / 2; // middle of slices
|
||||||
|
final int t = reader.getSizeT() / 2; // middle of frames
|
||||||
|
final int c = 0; // first channel
|
||||||
|
final int no = reader.getIndex(z, c, t);
|
||||||
// System.out.println("series count: " + seriesCount);
|
// System.out.println("series count: " + seriesCount);
|
||||||
// System.out.println("image count: " + reader.getImageCount());
|
// System.out.println("image count: " + reader.getImageCount());
|
||||||
// System.out.println("resolution: " + reader.getResolutionCount());
|
// System.out.println("resolution: " + reader.getResolutionCount());
|
||||||
// System.out.println("thubmail series: " + series);
|
// System.out.println("thumbnail series: " + series);
|
||||||
ret = reader.openThumbImage(no);
|
ret = reader.openThumbImage(no);
|
||||||
} catch (final Throwable e) {
|
final boolean is16bit = reader.getBitsPerPixel() > 8;
|
||||||
for (Throwable t = e; t != null; t = t.getCause()) {
|
final boolean isGrayScale = ret.getSampleModel().getNumBands() == 1;
|
||||||
if (t instanceof OutOfMemoryError) {
|
if (is16bit || isGrayScale) {
|
||||||
|
// perform auto scaling if 16bit or gray scale thumbnail image.
|
||||||
|
ret = AWTImageTools.autoscale(ret);
|
||||||
|
}
|
||||||
|
} catch (final Throwable t) {
|
||||||
|
for (Throwable e = t; e != null; e = e.getCause()) {
|
||||||
|
if (e instanceof OutOfMemoryError) {
|
||||||
throw new BioFormatsImageException("out of memory");
|
throw new BioFormatsImageException("out of memory");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new BioFormatsImageException(e);
|
throw new BioFormatsImageException(t);
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
reader.close();
|
reader.close();
|
||||||
@ -222,7 +243,7 @@ public class BioFormatsImageThumbnail {
|
|||||||
thumbnail.put("height", height);
|
thumbnail.put("height", height);
|
||||||
thumbnail.put("mimeType", mimeType);
|
thumbnail.put("mimeType", mimeType);
|
||||||
thumbnail.put("url", dataUri);
|
thumbnail.put("url", dataUri);
|
||||||
} catch (final BioFormatsImageException e) {
|
} catch (final Throwable t) {
|
||||||
// ignore
|
// ignore
|
||||||
ret = false;
|
ret = false;
|
||||||
}
|
}
|
||||||
@ -230,22 +251,23 @@ public class BioFormatsImageThumbnail {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(final String[] args) throws BioFormatsImageException {
|
public static void main(final String[] args) {
|
||||||
if (args.length != 1) {
|
if (args.length != 1) {
|
||||||
System.out.println("filename argument required");
|
System.out.println("filename argument required");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
final String path = args[0];
|
final String path = args[0];
|
||||||
try {
|
try {
|
||||||
DebugTools.enableLogging("ERROR");
|
DebugTools.enableLogging("OFF");
|
||||||
BufferedImage image = getImageFromBioFormatsFile(path);
|
BufferedImage image = getImageFromBioFormatsFile(path);
|
||||||
final String format = image.getColorModel().hasAlpha() ? "png" : "jpeg";
|
final String format = image.getColorModel().hasAlpha() ? "png" : "jpeg";
|
||||||
// image = resize(image, 160, 160);
|
// image = resize(image, 160, 160);
|
||||||
final String dataUri = getDataUri(image, "image/" + format);
|
final String dataUri = getDataUri(image, "image/" + format);
|
||||||
System.out.println(dataUri);
|
System.out.println(dataUri);
|
||||||
// saveFile(dataUri, "/var/tmp/thumbnail." + format);
|
// saveFile(dataUri, "/var/tmp/thumbnail." + format);
|
||||||
} catch (final BioFormatsImageException e) {
|
} catch (final Throwable t) {
|
||||||
System.out.println(e.getMessage());
|
// t.printStackTrace();
|
||||||
|
System.out.println(t.getMessage());
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user