Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
7c7c61d819
|
|||
|
996c143cca
|
|||
|
e1d24bd35d
|
|||
|
01bdbb5d68
|
|||
|
2946707155
|
|||
| 0b3938c13f | |||
| dceb58cf82 | |||
| bc4be44174 | |||
|
f244530932
|
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@@ -8,7 +8,7 @@
|
|||||||
},
|
},
|
||||||
// Extensions - Code Spell Checker
|
// Extensions - Code Spell Checker
|
||||||
"cSpell.ignoreWords": ["endianness", "noflat", "nopix", "omexml"],
|
"cSpell.ignoreWords": ["endianness", "noflat", "nopix", "omexml"],
|
||||||
"cSpell.words": ["bioformats", "imageinfo", "neurodata", "riken"],
|
"cSpell.words": ["appassembler", "bioformats", "codehaus", "imageinfo", "neurodata", "riken", "unidata"],
|
||||||
// Extentions - Prettier
|
// Extentions - Prettier
|
||||||
"prettier.printWidth": 120,
|
"prettier.printWidth": 120,
|
||||||
"prettier.singleQuote": true,
|
"prettier.singleQuote": true,
|
||||||
|
|||||||
169
README.md
169
README.md
@@ -1,32 +1,96 @@
|
|||||||
# BioFormatsImageInfo
|
# BioFormatsImageInfo
|
||||||
|
|
||||||
Metadata extraction tool based on Bio-Formats
|
[](https://www.oracle.com/java/)
|
||||||
|
[](https://www.openmicroscopy.org/bio-formats/)
|
||||||
|
|
||||||
## make package
|
BioFormatsImageInfo is a metadata extraction and thumbnail generation tool for biomedical images, based on the [Bio-Formats](https://www.openmicroscopy.org/bio-formats/) library. It supports over 150 image formats and can be used both as a command-line tool and as a Java library.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- **Wide Format Support**: Leverages Bio-Formats' extensive format support
|
||||||
|
- **Metadata Extraction**: Outputs detailed image information in JSON format
|
||||||
|
- **Thumbnail Generation**: Creates high-quality thumbnail images
|
||||||
|
- **Command-Line Ready**: Suitable for batch processing and scripting
|
||||||
|
- **Library Integration**: Can be embedded into Java applications
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
|
||||||
|
- Java 1.8 or higher
|
||||||
|
- Maven 3.0 or higher (for building)
|
||||||
|
|
||||||
|
## Installation & Build
|
||||||
|
|
||||||
|
Clone and build the project:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
|
git clone [repository-url]
|
||||||
|
cd bioformats-imageinfo
|
||||||
mvn package
|
mvn package
|
||||||
```
|
```
|
||||||
|
|
||||||
## run
|
After the build completes, the following files will be generated:
|
||||||
|
|
||||||
|
- `target/dist/` - Executable distribution
|
||||||
|
- `target/bioformats-imageinfo-1.2.6.jar` - Standard JAR file
|
||||||
|
- `target/bioformats-imageinfo-1.2.6-jar-with-dependencies.jar` - Fat JAR with dependencies
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
### Command Line Execution
|
||||||
|
|
||||||
|
#### Using Executable Script (Recommended)
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
./target/dist/bin/bioformats-imageinfo "[path to image file]"
|
./target/dist/bin/bioformats-imageinfo [OPTION] [path to image file]
|
||||||
```
|
```
|
||||||
|
|
||||||
### run by jar
|
#### Running from JAR File
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
java -jar ./target/dist/lib/bioformats-imageinfo-1.2.2.jar "[path to image file]"
|
java -jar ./target/dist/lib/bioformats-imageinfo-1.2.6.jar [OPTION] [path to image file]
|
||||||
```
|
```
|
||||||
|
|
||||||
### run by fat jar
|
#### Running from Fat JAR
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
java -jar ./target/bioformats-imageinfo-1.2.2-jar-with-dependencies.jar "[path to image file]"
|
java -jar ./target/bioformats-imageinfo-1.2.6-jar-with-dependencies.jar [OPTION] [path to image file]
|
||||||
```
|
```
|
||||||
|
|
||||||
## library usage
|
### Options
|
||||||
|
|
||||||
|
| Option | Description |
|
||||||
|
| ------ | ------------------------------------ |
|
||||||
|
| `-M` | Output metadata in JSON format |
|
||||||
|
| `-T` | Generate and output thumbnail images |
|
||||||
|
|
||||||
|
### Usage Examples
|
||||||
|
|
||||||
|
```shell
|
||||||
|
# Extract metadata
|
||||||
|
./target/dist/bin/bioformats-imageinfo -M sample.tif
|
||||||
|
|
||||||
|
# Generate thumbnail
|
||||||
|
./target/dist/bin/bioformats-imageinfo -T sample.tif
|
||||||
|
|
||||||
|
# Output both metadata and thumbnail
|
||||||
|
./target/dist/bin/bioformats-imageinfo -M -T sample.tif
|
||||||
|
```
|
||||||
|
|
||||||
|
## Library Usage
|
||||||
|
|
||||||
|
Example of using as a library in Java applications:
|
||||||
|
|
||||||
|
### Adding Dependencies
|
||||||
|
|
||||||
|
```xml
|
||||||
|
<dependency>
|
||||||
|
<groupId>jp.riken.neurodata.tools.BioFormatsImageInfo</groupId>
|
||||||
|
<artifactId>bioformats-imageinfo</artifactId>
|
||||||
|
<version>1.2.6</version>
|
||||||
|
</dependency>
|
||||||
|
```
|
||||||
|
|
||||||
|
### Sample Code
|
||||||
|
|
||||||
```java
|
```java
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
@@ -35,16 +99,89 @@ import jp.riken.neurodata.tools.BioFormatsImageInfo;
|
|||||||
import jp.riken.neurodata.tools.BioFormatsImageException;
|
import jp.riken.neurodata.tools.BioFormatsImageException;
|
||||||
import jp.riken.neurodata.tools.BioFormatsImageThumbnail;
|
import jp.riken.neurodata.tools.BioFormatsImageThumbnail;
|
||||||
|
|
||||||
String path = "[path to image file]";
|
// Path to image file
|
||||||
|
String path = "path/to/your/image.tif";
|
||||||
String format = "";
|
String format = "";
|
||||||
Map<String, Object> metadata = new LinkedHashMap<String, Object>();
|
Map<String, Object> metadata = new LinkedHashMap<>();
|
||||||
Map<String, Object> thumbnail = new LinkedHashMap<String, Object>();
|
Map<String, Object> thumbnail = new LinkedHashMap<>();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
format = BioFormatsImageInfo.readMetadata(path, metadata);
|
// Read metadata
|
||||||
BioFormatsImageThumbnail.readThumbnail(path, thumbnail);
|
format = BioFormatsImageInfo.readMetadata(path, metadata);
|
||||||
|
|
||||||
|
// Generate thumbnail
|
||||||
|
BioFormatsImageThumbnail.readThumbnail(path, thumbnail);
|
||||||
|
|
||||||
|
// Use results
|
||||||
|
System.out.println("Image format: " + format);
|
||||||
|
System.out.println("Metadata: " + metadata);
|
||||||
|
System.out.println("Thumbnail info: " + thumbnail);
|
||||||
|
|
||||||
} catch (BioFormatsImageException e) {
|
} catch (BioFormatsImageException e) {
|
||||||
// error occurred
|
// Error handling
|
||||||
e.printStackTrace();
|
System.err.println("Error occurred while processing image: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Supported Image Formats
|
||||||
|
|
||||||
|
As this tool uses the Bio-Formats library, it supports a wide variety of biomedical image formats including:
|
||||||
|
|
||||||
|
- **Microscopy Images**: LSM, CZI, ND2, OIB, OIF, LIF, etc.
|
||||||
|
- **Common Images**: TIFF, JPEG, PNG, BMP, etc.
|
||||||
|
- **Medical Images**: DICOM
|
||||||
|
- **Others**: Over 150 formats
|
||||||
|
|
||||||
|
For detailed format support information, please refer to the [Bio-Formats official website](https://docs.openmicroscopy.org/bio-formats/latest/supported-formats.html).
|
||||||
|
|
||||||
|
## Output Formats
|
||||||
|
|
||||||
|
### Metadata Output
|
||||||
|
|
||||||
|
- Structured metadata in JSON format
|
||||||
|
- Image size, pixel information, acquisition conditions, etc.
|
||||||
|
- Detailed information for each series
|
||||||
|
|
||||||
|
### Thumbnail Output
|
||||||
|
|
||||||
|
- Thumbnail images in JPEG format
|
||||||
|
- Base64 encoding support for embedding
|
||||||
|
- Adjustable quality (default: 85%)
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
Please refer to the `LICENSE.txt` file for the license of this project.
|
||||||
|
|
||||||
|
## Development & Contribution
|
||||||
|
|
||||||
|
Developed by the Neuroinformatics Unit, RIKEN Center for Brain Science. Contributions are welcome via pull requests or issues on the repository.
|
||||||
|
|
||||||
|
- **Developer**: Neuroinformatics Unit, RIKEN Center for Brain Science
|
||||||
|
- **Project URL**: https://neurodata.riken.jp
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
### Common Issues
|
||||||
|
|
||||||
|
1. **Out of Memory Error**
|
||||||
|
|
||||||
|
```shell
|
||||||
|
java -Xmx4g -jar bioformats-imageinfo-1.2.6-jar-with-dependencies.jar -M image.tif
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Processing Large Files**
|
||||||
|
|
||||||
|
- Disable Bio-Formats debug mode to improve processing speed
|
||||||
|
- Adjust JVM heap size as needed
|
||||||
|
|
||||||
|
3. **Format Support**
|
||||||
|
- For unsupported formats, check the latest Bio-Formats version for support status
|
||||||
|
|
||||||
|
### Log Output Configuration
|
||||||
|
|
||||||
|
When detailed logs are needed:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
java -Dloci.common.DebugTools.enableLogging=true -jar bioformats-imageinfo-1.2.6-jar-with-dependencies.jar -M image.tif
|
||||||
|
```
|
||||||
|
|||||||
26
pom.xml
26
pom.xml
@@ -5,17 +5,18 @@
|
|||||||
<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.2.2</version>
|
<version>1.2.6</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>7.1.0</bio-formats.version>
|
<bio-formats.version>8.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>
|
<java.version>1.8</java.version>
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||||
|
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||||
|
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
@@ -39,7 +40,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-enforcer-plugin</artifactId>
|
<artifactId>maven-enforcer-plugin</artifactId>
|
||||||
<version>3.4.1</version>
|
<version>3.6.2</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>enforce-maven</id>
|
<id>enforce-maven</id>
|
||||||
@@ -49,7 +50,7 @@
|
|||||||
<configuration>
|
<configuration>
|
||||||
<rules>
|
<rules>
|
||||||
<requireMavenVersion>
|
<requireMavenVersion>
|
||||||
<version>3.2.5</version>
|
<version>3.6.3</version>
|
||||||
</requireMavenVersion>
|
</requireMavenVersion>
|
||||||
</rules>
|
</rules>
|
||||||
</configuration>
|
</configuration>
|
||||||
@@ -59,7 +60,7 @@
|
|||||||
<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.6.1</version>
|
<version>3.9.0</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>copy-dependencies</id>
|
<id>copy-dependencies</id>
|
||||||
@@ -76,7 +77,7 @@
|
|||||||
<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.3.0</version>
|
<version>3.4.2</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<archive>
|
<archive>
|
||||||
<manifest>
|
<manifest>
|
||||||
@@ -113,7 +114,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-assembly-plugin</artifactId>
|
<artifactId>maven-assembly-plugin</artifactId>
|
||||||
<version>3.6.0</version>
|
<version>3.7.1</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<descriptorRefs>
|
<descriptorRefs>
|
||||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||||
@@ -140,7 +141,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.owasp</groupId>
|
<groupId>org.owasp</groupId>
|
||||||
<artifactId>dependency-check-maven</artifactId>
|
<artifactId>dependency-check-maven</artifactId>
|
||||||
<version>9.0.9</version>
|
<version>12.1.8</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<goals>
|
<goals>
|
||||||
@@ -156,7 +157,6 @@
|
|||||||
<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://repo1.maven.org/maven2/</url>
|
<url>https://repo1.maven.org/maven2/</url>
|
||||||
<layout>default</layout>
|
<layout>default</layout>
|
||||||
</pluginRepository>
|
</pluginRepository>
|
||||||
@@ -166,7 +166,6 @@
|
|||||||
<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://repo1.maven.org/maven2/</url>
|
<url>https://repo1.maven.org/maven2/</url>
|
||||||
</repository>
|
</repository>
|
||||||
<repository>
|
<repository>
|
||||||
@@ -176,7 +175,8 @@
|
|||||||
</repository>
|
</repository>
|
||||||
<repository>
|
<repository>
|
||||||
<id>unidata.releases</id>
|
<id>unidata.releases</id>
|
||||||
<url>https://artifacts.unidata.ucar.edu/content/repositories/unidata-releases</url>
|
<name>Unidata Releases</name>
|
||||||
|
<url>https://artifacts.unidata.ucar.edu/content/repositories/unidata-releases/</url>
|
||||||
</repository>
|
</repository>
|
||||||
</repositories>
|
</repositories>
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ public class BioFormatsImageException extends Exception {
|
|||||||
super(message);
|
super(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public BioFormatsImageException(Throwable cause) {
|
public BioFormatsImageException(Exception cause) {
|
||||||
super(cause.getMessage());
|
super(cause.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package jp.riken.neurodata.tools;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Hashtable;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -16,7 +16,6 @@ import loci.formats.FormatException;
|
|||||||
import loci.formats.FormatTools;
|
import loci.formats.FormatTools;
|
||||||
import loci.formats.IFormatReader;
|
import loci.formats.IFormatReader;
|
||||||
import loci.formats.ImageReader;
|
import loci.formats.ImageReader;
|
||||||
import loci.formats.MetadataTools;
|
|
||||||
import loci.formats.Modulo;
|
import loci.formats.Modulo;
|
||||||
import loci.formats.meta.MetadataRetrieve;
|
import loci.formats.meta.MetadataRetrieve;
|
||||||
import loci.formats.meta.MetadataStore;
|
import loci.formats.meta.MetadataStore;
|
||||||
@@ -25,7 +24,7 @@ import loci.formats.tools.ImageInfo;
|
|||||||
public class BioFormatsImageInfo {
|
public class BioFormatsImageInfo {
|
||||||
|
|
||||||
protected static Map<String, Object> readImages(final IFormatReader reader) throws FormatException, IOException {
|
protected static Map<String, Object> readImages(final IFormatReader reader) throws FormatException, IOException {
|
||||||
final Map<String, Object> metadata = new LinkedHashMap<String, Object>();
|
final Map<String, Object> metadata = new LinkedHashMap<>();
|
||||||
|
|
||||||
// read basic metadata
|
// read basic metadata
|
||||||
final int seriesCount = reader.getSeriesCount();
|
final int seriesCount = reader.getSeriesCount();
|
||||||
@@ -35,7 +34,7 @@ public class BioFormatsImageInfo {
|
|||||||
|
|
||||||
// read basic metadata for series #j
|
// read basic metadata for series #j
|
||||||
reader.setSeries(j);
|
reader.setSeries(j);
|
||||||
final Map<String, Object> seriesMetadata = new LinkedHashMap<String, Object>();
|
final Map<String, Object> seriesMetadata = new LinkedHashMap<>();
|
||||||
|
|
||||||
final int imageCount = reader.getImageCount();
|
final int imageCount = reader.getImageCount();
|
||||||
final int resolutionCount = reader.getResolutionCount();
|
final int resolutionCount = reader.getResolutionCount();
|
||||||
@@ -46,7 +45,7 @@ public class BioFormatsImageInfo {
|
|||||||
|
|
||||||
// resolutions
|
// resolutions
|
||||||
if (resolutionCount > 1) {
|
if (resolutionCount > 1) {
|
||||||
final Map<String, Object> resolutionsMetadata = new LinkedHashMap<String, Object>();
|
final Map<String, Object> resolutionsMetadata = new LinkedHashMap<>();
|
||||||
for (int i = 0; i < resolutionCount; i++) {
|
for (int i = 0; i < resolutionCount; i++) {
|
||||||
reader.setResolution(i);
|
reader.setResolution(i);
|
||||||
resolutionsMetadata.put(String.format("resolution[%d]", i),
|
resolutionsMetadata.put(String.format("resolution[%d]", i),
|
||||||
@@ -62,7 +61,7 @@ public class BioFormatsImageInfo {
|
|||||||
|
|
||||||
// indexed
|
// indexed
|
||||||
{
|
{
|
||||||
final List<Object> indexed = new ArrayList<Object>();
|
final List<Object> indexed = new ArrayList<>();
|
||||||
indexed.add(reader.isIndexed());
|
indexed.add(reader.isIndexed());
|
||||||
indexed.add(String.format("%b color", !reader.isFalseColor()));
|
indexed.add(String.format("%b color", !reader.isFalseColor()));
|
||||||
final byte[][] table8 = reader.get8BitLookupTable();
|
final byte[][] table8 = reader.get8BitLookupTable();
|
||||||
@@ -115,7 +114,7 @@ public class BioFormatsImageInfo {
|
|||||||
for (int i = 0; i < indices.length; i++) {
|
for (int i = 0; i < indices.length; i++) {
|
||||||
zct[i] = reader.getZCTCoords(indices[i]);
|
zct[i] = reader.getZCTCoords(indices[i]);
|
||||||
indices2[i] = reader.getIndex(zct[i][0], zct[i][1], zct[i][2]);
|
indices2[i] = reader.getIndex(zct[i][0], zct[i][1], zct[i][2]);
|
||||||
final Map<String, Object> planeMetadata = new LinkedHashMap<String, Object>();
|
final Map<String, Object> planeMetadata = new LinkedHashMap<>();
|
||||||
planeMetadata.put("Z", zct[i][0]);
|
planeMetadata.put("Z", zct[i][0]);
|
||||||
planeMetadata.put("C", zct[i][1]);
|
planeMetadata.put("C", zct[i][1]);
|
||||||
planeMetadata.put("T", zct[i][2]);
|
planeMetadata.put("T", zct[i][2]);
|
||||||
@@ -132,22 +131,21 @@ public class BioFormatsImageInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected static Map<String, Object> readAnnotations(final IFormatReader reader) {
|
protected static Map<String, Object> readAnnotations(final IFormatReader reader) {
|
||||||
final Map<String, Object> metadata = new LinkedHashMap<String, Object>();
|
final Map<String, Object> metadata = new LinkedHashMap<>();
|
||||||
// global metadata
|
// global metadata
|
||||||
final Hashtable<String, Object> globalMetadata = reader.getGlobalMetadata();
|
final HashMap<String, Object> globalMetadata = new HashMap<>(reader.getGlobalMetadata());
|
||||||
for (String key : MetadataTools.keys(globalMetadata)) {
|
for (Map.Entry<String, Object> entry : globalMetadata.entrySet()) {
|
||||||
metadata.put(key, globalMetadata.get(key));
|
metadata.put(entry.getKey(), entry.getValue());
|
||||||
}
|
}
|
||||||
// original metadata
|
// original metadata
|
||||||
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> seriesMetadata = reader.getSeriesMetadata();
|
final HashMap<String, Object> seriesMetadata = new HashMap<>(reader.getSeriesMetadata());
|
||||||
if (!seriesMetadata.isEmpty()) {
|
if (!seriesMetadata.isEmpty()) {
|
||||||
final Map<String, Object> originalMetadata = new LinkedHashMap<String, Object>();
|
final Map<String, Object> originalMetadata = new LinkedHashMap<>();
|
||||||
final String[] keys = MetadataTools.keys(seriesMetadata);
|
for (Map.Entry<String, Object> entry : seriesMetadata.entrySet()) {
|
||||||
for (int i = 0; i < keys.length; i++) {
|
originalMetadata.put(entry.getKey(), entry.getValue());
|
||||||
originalMetadata.put(keys[i], seriesMetadata.get(keys[i]));
|
|
||||||
}
|
}
|
||||||
metadata.put(String.format("series[%d]", j), originalMetadata);
|
metadata.put(String.format("series[%d]", j), originalMetadata);
|
||||||
}
|
}
|
||||||
@@ -161,7 +159,7 @@ public class BioFormatsImageInfo {
|
|||||||
if (size == effSize && mLength == 1) {
|
if (size == effSize && mLength == 1) {
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
final List<Object> ret = new ArrayList<Object>();
|
final List<Object> ret = new ArrayList<>();
|
||||||
ret.add(size);
|
ret.add(size);
|
||||||
if (size != effSize) {
|
if (size != effSize) {
|
||||||
ret.add(String.format("(effectively %d)", effSize));
|
ret.add(String.format("(effectively %d)", effSize));
|
||||||
@@ -174,7 +172,7 @@ public class BioFormatsImageInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected static Map<String, Object> makeRectangle(final int width, final int height) {
|
protected static Map<String, Object> makeRectangle(final int width, final int height) {
|
||||||
final Map<String, Object> ret = new LinkedHashMap<String, Object>();
|
final Map<String, Object> ret = new LinkedHashMap<>();
|
||||||
ret.put("width", width);
|
ret.put("width", width);
|
||||||
ret.put("height", height);
|
ret.put("height", height);
|
||||||
|
|
||||||
@@ -185,9 +183,8 @@ public class BioFormatsImageInfo {
|
|||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
String json = null;
|
String json = null;
|
||||||
try {
|
try {
|
||||||
// json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(map);
|
|
||||||
json = mapper.writeValueAsString(map);
|
json = mapper.writeValueAsString(map);
|
||||||
} catch (final Throwable e) {
|
} catch (final Exception e) {
|
||||||
// return "null" if conversion error occurred
|
// return "null" if conversion error occurred
|
||||||
json = "null";
|
json = "null";
|
||||||
}
|
}
|
||||||
@@ -197,15 +194,13 @@ public class BioFormatsImageInfo {
|
|||||||
|
|
||||||
public static String readMetadata(final String path, final Map<String, Object> metadata)
|
public static String readMetadata(final String path, final Map<String, Object> metadata)
|
||||||
throws BioFormatsImageException {
|
throws BioFormatsImageException {
|
||||||
if (FilenameUtils.getExtension(path).toLowerCase().equals("zip")) {
|
if (FilenameUtils.getExtension(path).equalsIgnoreCase("zip")) {
|
||||||
// ignore if file is zip archive
|
// ignore if file is zip archive
|
||||||
throw new BioFormatsImageException("Zip is not supported");
|
throw new BioFormatsImageException("Zip is not supported");
|
||||||
}
|
}
|
||||||
String format = "";
|
String format = "";
|
||||||
final ImageInfo info = new ImageInfo();
|
final ImageInfo info = new ImageInfo();
|
||||||
final IFormatReader reader = new ImageReader();
|
try (final IFormatReader reader = new ImageReader()) {
|
||||||
try {
|
|
||||||
// info.parseArgs(new String[] { path, "-nopix", "-noflat", "-omexml" });
|
|
||||||
info.parseArgs(new String[] { path, "-nopix", "-noflat" });
|
info.parseArgs(new String[] { path, "-nopix", "-noflat" });
|
||||||
info.setReader(reader);
|
info.setReader(reader);
|
||||||
info.mapLocation();
|
info.mapLocation();
|
||||||
@@ -216,28 +211,17 @@ public class BioFormatsImageInfo {
|
|||||||
format = reader.getFormat();
|
format = reader.getFormat();
|
||||||
info.configureReaderPostInit();
|
info.configureReaderPostInit();
|
||||||
info.checkWarnings();
|
info.checkWarnings();
|
||||||
// info.printOMEXML();
|
|
||||||
// info.readCoreMetadata();
|
|
||||||
metadata.put("images", readImages(reader));
|
metadata.put("images", readImages(reader));
|
||||||
info.initPreMinMaxValues();
|
info.initPreMinMaxValues();
|
||||||
reader.setSeries(0);
|
reader.setSeries(0);
|
||||||
reader.setResolution(0);
|
reader.setResolution(0);
|
||||||
// info.printGlobalMetadata();
|
|
||||||
// info.printOriginalMetadata();
|
|
||||||
final Map<String, Object> annotations = readAnnotations(reader);
|
final Map<String, Object> annotations = readAnnotations(reader);
|
||||||
if (!annotations.isEmpty()) {
|
if (!annotations.isEmpty()) {
|
||||||
metadata.put("annotations", annotations);
|
metadata.put("annotations", annotations);
|
||||||
}
|
}
|
||||||
} catch (final Throwable e) {
|
} catch (final Exception e) {
|
||||||
throw new BioFormatsImageException(e);
|
throw new BioFormatsImageException(e);
|
||||||
} finally {
|
|
||||||
if (reader != null) {
|
|
||||||
try {
|
|
||||||
reader.close();
|
|
||||||
} catch (final IOException e) {
|
|
||||||
throw new BioFormatsImageException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return format;
|
return format;
|
||||||
@@ -248,26 +232,43 @@ public class BioFormatsImageInfo {
|
|||||||
boolean status = true;
|
boolean status = true;
|
||||||
String message = "";
|
String message = "";
|
||||||
String format = "";
|
String format = "";
|
||||||
final Map<String, Object> metadata = new LinkedHashMap<String, Object>();
|
boolean metadataON = true;
|
||||||
final Map<String, Object> thumbnail = new LinkedHashMap<String, Object>();
|
boolean thumbnailON = true;
|
||||||
if (args.length != 1) {
|
String path = "";
|
||||||
status = false;
|
final Map<String, Object> metadata = new LinkedHashMap<>();
|
||||||
message = "filename argument required";
|
final Map<String, Object> thumbnail = new LinkedHashMap<>();
|
||||||
|
if (args.length == 1) {
|
||||||
|
path = args[0];
|
||||||
|
} 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("OFF");
|
DebugTools.enableLogging("OFF");
|
||||||
format = readMetadata(path, metadata);
|
if (metadataON) {
|
||||||
BioFormatsImageThumbnail.readThumbnail(path, thumbnail);
|
format = readMetadata(path, metadata);
|
||||||
} catch (Throwable t) {
|
}
|
||||||
|
if (thumbnailON) {
|
||||||
|
BioFormatsImageThumbnail.readThumbnail(path, thumbnail);
|
||||||
|
}
|
||||||
|
} catch (final Exception e) {
|
||||||
status = false;
|
status = false;
|
||||||
message = t.getMessage();
|
message = e.getMessage();
|
||||||
metadata.clear();
|
metadata.clear();
|
||||||
thumbnail.clear();
|
thumbnail.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final Map<String, Object> results = new LinkedHashMap<String, Object>();
|
final Map<String, Object> results = new LinkedHashMap<>();
|
||||||
results.put("status", status);
|
results.put("status", status);
|
||||||
results.put("message", message);
|
results.put("message", message);
|
||||||
results.put("format", format);
|
results.put("format", format);
|
||||||
|
|||||||
@@ -47,8 +47,8 @@ public class BioFormatsImageThumbnail {
|
|||||||
Image resizeImage = image;
|
Image resizeImage = image;
|
||||||
if (scale != 1.0) {
|
if (scale != 1.0) {
|
||||||
// resize image if dimension is different with requested dimension.
|
// resize image if dimension is different with requested dimension.
|
||||||
resizeWidth = (int) (scale * (double) imageWidth);
|
resizeWidth = (int) (scale * imageWidth);
|
||||||
resizeHeight = (int) (scale * (double) imageHeight);
|
resizeHeight = (int) (scale * imageHeight);
|
||||||
resizeImage = image.getScaledInstance(resizeWidth, resizeHeight, Image.SCALE_AREA_AVERAGING);
|
resizeImage = image.getScaledInstance(resizeWidth, resizeHeight, Image.SCALE_AREA_AVERAGING);
|
||||||
}
|
}
|
||||||
final int type = hasAlpha ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
|
final int type = hasAlpha ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
|
||||||
@@ -57,10 +57,6 @@ public class BioFormatsImageThumbnail {
|
|||||||
final Color c = new Color(image.getRGB(0, 0), hasAlpha);
|
final Color c = new Color(image.getRGB(0, 0), hasAlpha);
|
||||||
final int offsetX = (width - resizeWidth) / 2;
|
final int offsetX = (width - resizeWidth) / 2;
|
||||||
final int offsetY = (height - resizeHeight) / 2;
|
final int offsetY = (height - resizeHeight) / 2;
|
||||||
// System.out.println("original:" + imageWidth + "," + imageHeight);
|
|
||||||
// System.out.println("resize:" + resizeWidth + "," + resizeHeight);
|
|
||||||
// System.out.println("target:" + width + "," + height);
|
|
||||||
// System.out.println("offset:" + offsetX + "," + offsetY);
|
|
||||||
g.setColor(c);
|
g.setColor(c);
|
||||||
g.fillRect(0, 0, width, height);
|
g.fillRect(0, 0, width, height);
|
||||||
g.drawImage(resizeImage, offsetX, offsetY, resizeWidth, resizeHeight, null);
|
g.drawImage(resizeImage, offsetX, offsetY, resizeWidth, resizeHeight, null);
|
||||||
@@ -141,26 +137,18 @@ public class BioFormatsImageThumbnail {
|
|||||||
if (bytes.length == 0) {
|
if (bytes.length == 0) {
|
||||||
throw new BioFormatsImageException("Failed to convert to " + mimeType + " format");
|
throw new BioFormatsImageException("Failed to convert to " + mimeType + " format");
|
||||||
}
|
}
|
||||||
final String dataUri = "data:" + mimeType + ";base64," + Base64.getEncoder().encodeToString(bytes);
|
|
||||||
|
|
||||||
return dataUri;
|
return "data:" + mimeType + ";base64," + Base64.getEncoder().encodeToString(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BufferedImage getImageFromBioFormatsFile(final String path) throws BioFormatsImageException {
|
public static BufferedImage getImageFromBioFormatsFile(final String path) throws BioFormatsImageException {
|
||||||
BufferedImage ret = null;
|
BufferedImage ret = null;
|
||||||
final BufferedImageReader reader = new BufferedImageReader(new ImageReader());
|
try (final BufferedImageReader reader = new BufferedImageReader(new ImageReader())) {
|
||||||
reader.setFlattenedResolutions(false);
|
reader.setFlattenedResolutions(false);
|
||||||
reader.setNormalized(true);
|
reader.setNormalized(true);
|
||||||
reader.setOriginalMetadataPopulated(false);
|
reader.setOriginalMetadataPopulated(false);
|
||||||
reader.setMetadataFiltered(true);
|
reader.setMetadataFiltered(true);
|
||||||
try {
|
|
||||||
reader.setId(path);
|
reader.setId(path);
|
||||||
} catch (FormatException | IOException e) {
|
|
||||||
throw new BioFormatsImageException("unsupported file: " + path);
|
|
||||||
} catch (Throwable t) {
|
|
||||||
throw new BioFormatsImageException(t);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
int series = 0;
|
int series = 0;
|
||||||
final int seriesCount = reader.getSeriesCount();
|
final int seriesCount = reader.getSeriesCount();
|
||||||
for (int i = 0; i < seriesCount; i++) {
|
for (int i = 0; i < seriesCount; i++) {
|
||||||
@@ -175,10 +163,7 @@ public class BioFormatsImageThumbnail {
|
|||||||
final int t = reader.getSizeT() / 2; // middle of frames
|
final int t = reader.getSizeT() / 2; // middle of frames
|
||||||
final int c = 0; // first channel
|
final int c = 0; // first channel
|
||||||
final int no = reader.getIndex(z, c, t);
|
final int no = reader.getIndex(z, c, t);
|
||||||
// System.out.println("series count: " + seriesCount);
|
|
||||||
// System.out.println("image count: " + reader.getImageCount());
|
|
||||||
// System.out.println("resolution: " + reader.getResolutionCount());
|
|
||||||
// System.out.println("thumbnail series: " + series);
|
|
||||||
ret = reader.openThumbImage(no);
|
ret = reader.openThumbImage(no);
|
||||||
final boolean is16bit = reader.getBitsPerPixel() > 8;
|
final boolean is16bit = reader.getBitsPerPixel() > 8;
|
||||||
final boolean isGrayScale = ret.getSampleModel().getNumBands() == 1;
|
final boolean isGrayScale = ret.getSampleModel().getNumBands() == 1;
|
||||||
@@ -186,19 +171,12 @@ public class BioFormatsImageThumbnail {
|
|||||||
// perform auto scaling if 16bit or gray scale thumbnail image.
|
// perform auto scaling if 16bit or gray scale thumbnail image.
|
||||||
ret = AWTImageTools.autoscale(ret);
|
ret = AWTImageTools.autoscale(ret);
|
||||||
}
|
}
|
||||||
} catch (final Throwable t) {
|
} catch (final OutOfMemoryError e) {
|
||||||
for (Throwable e = t; e != null; e = e.getCause()) {
|
throw new BioFormatsImageException("out of memory");
|
||||||
if (e instanceof OutOfMemoryError) {
|
} catch (final FormatException | IOException e) {
|
||||||
throw new BioFormatsImageException("out of memory");
|
throw new BioFormatsImageException("unsupported file: " + path);
|
||||||
}
|
} catch (final Exception e) {
|
||||||
}
|
throw new BioFormatsImageException(e);
|
||||||
throw new BioFormatsImageException(t);
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
reader.close();
|
|
||||||
} catch (final IOException e) {
|
|
||||||
throw new BioFormatsImageException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@@ -222,7 +200,7 @@ public class BioFormatsImageThumbnail {
|
|||||||
if (!matcher.find()) {
|
if (!matcher.find()) {
|
||||||
throw new BioFormatsImageException("Unsupported data URI format");
|
throw new BioFormatsImageException("Unsupported data URI format");
|
||||||
}
|
}
|
||||||
final byte[] bytes = matcher.group(2) == "utf8" ? matcher.group(3).getBytes()
|
final byte[] bytes = "utf8".equals(matcher.group(2)) ? matcher.group(3).getBytes()
|
||||||
: Base64.getDecoder().decode(matcher.group(3));
|
: Base64.getDecoder().decode(matcher.group(3));
|
||||||
try (final FileOutputStream fos = new FileOutputStream(path);) {
|
try (final FileOutputStream fos = new FileOutputStream(path);) {
|
||||||
fos.write(bytes);
|
fos.write(bytes);
|
||||||
@@ -243,7 +221,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 Throwable t) {
|
} catch (final Exception e) {
|
||||||
// ignore
|
// ignore
|
||||||
ret = false;
|
ret = false;
|
||||||
}
|
}
|
||||||
@@ -261,13 +239,12 @@ public class BioFormatsImageThumbnail {
|
|||||||
DebugTools.enableLogging("OFF");
|
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);
|
|
||||||
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 Throwable t) {
|
} catch (final Exception e) {
|
||||||
// t.printStackTrace();
|
e.printStackTrace();
|
||||||
System.out.println(t.getMessage());
|
System.out.println(e.getMessage());
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user