Skip to content
Snippets Groups Projects
Commit 44a92b69 authored by Vojtech Moravec's avatar Vojtech Moravec
Browse files

Create FileTypeInspector which determine file type based on magic value.

parent 76346d87
No related branches found
No related tags found
No related merge requests found
......@@ -7,10 +7,11 @@ import cz.it4i.qcmp.cli.CustomFunctionBase;
import cz.it4i.qcmp.cli.functions.DebugFunction;
import cz.it4i.qcmp.compression.ImageCompressor;
import cz.it4i.qcmp.compression.ImageDecompressor;
import cz.it4i.qcmp.fileformat.FileExtensions;
import cz.it4i.qcmp.fileformat.IQvcFile;
import cz.it4i.qcmp.io.FileTypeInspector;
import cz.it4i.qcmp.io.QuantizationCacheManager;
import cz.it4i.qcmp.io.QvcFileReader;
import cz.it4i.qcmp.utilities.ColorConsole;
import org.apache.commons.cli.*;
import java.io.IOException;
......@@ -108,47 +109,69 @@ public class DataCompressor {
}
break;
case InspectFile: {
if (parsedOptions.getInputDataInfo().getFilePath().endsWith(FileExtensions.CACHE_FILE_EXT)) {
QuantizationCacheManager.inspectCacheFile(parsedOptions.getInputDataInfo().getFilePath(),
parsedOptions.isVerbose());
} else {
final ImageDecompressor decompressor = new ImageDecompressor(parsedOptions);
try {
System.out.println(decompressor.inspectCompressedFile());
} catch (final IOException e) {
System.err.println("Errors occurred during inspecting file.");
System.err.println(e.getMessage());
e.printStackTrace();
final FileTypeInspector.FileType fileType = FileTypeInspector.inspectFile(parsedOptions.getInputDataInfo().getFilePath());
switch (fileType) {
case Qcmp: {
final ImageDecompressor decompressor = new ImageDecompressor(parsedOptions);
try {
System.out.println(decompressor.inspectCompressedFile());
} catch (final IOException e) {
System.err.println("Errors occurred during inspecting file.");
System.err.println(e.getMessage());
e.printStackTrace();
}
}
break;
case Qvc: {
QuantizationCacheManager.inspectCacheFile(parsedOptions.getInputDataInfo().getFilePath(),
parsedOptions.isVerbose());
}
break;
case InvalidPath:
exitWithInvalidFilePath();
break;
case Unknown:
exitWithUnknownFile();
break;
}
}
break;
case Convert: {
final boolean inPlace = parsedOptions.getOutputFilePath() == null;
// TODO(Moravec): Maybe replace with generic reader which can determine file type based on magic value and not an extension.
if (parsedOptions.getInputDataInfo().getFilePath().endsWith(FileExtensions.CACHE_FILE_EXT)) {
IQvcFile cacheFile = null;
try {
cacheFile = QvcFileReader.readCacheFile(parsedOptions.getInputDataInfo().getFilePath());
} catch (final IOException e) {
System.err.println("Unable to read QVC file. Error: " + e.getMessage());
exitApplication(1);
}
try {
assert (cacheFile != null);
cacheFile.convertToNewerVersion(inPlace, parsedOptions.getInputDataInfo().getFilePath(),
parsedOptions.getOutputFilePath());
} catch (final IOException e) {
System.err.println("Unable to convert specified QVC file. Error: " + e.getMessage());
exitApplication(1);
final FileTypeInspector.FileType fileType = FileTypeInspector.inspectFile(parsedOptions.getInputDataInfo().getFilePath());
switch (fileType) {
case Qcmp: {
System.err.println("Qcmp file conversion isn't supported yet");
}
break;
case Qvc: {
IQvcFile cacheFile = null;
try {
cacheFile = QvcFileReader.readCacheFile(parsedOptions.getInputDataInfo().getFilePath());
} catch (final IOException e) {
System.err.println("Unable to read QVC file. Error: " + e.getMessage());
exitApplication(1);
}
final boolean inPlace = parsedOptions.getOutputFilePath() == null;
try {
assert (cacheFile != null);
cacheFile.convertToNewerVersion(inPlace, parsedOptions.getInputDataInfo().getFilePath(),
parsedOptions.getOutputFilePath());
} catch (final IOException e) {
System.err.println("Unable to convert specified QVC file. Error: " + e.getMessage());
exitApplication(1);
}
if (parsedOptions.isVerbose()) {
System.err.println("Qvc file is converted.");
if (parsedOptions.isVerbose()) {
System.err.println("Qvc file is converted.");
}
}
} else {
System.err.println("Qcmp file conversion isn't supported yet");
break;
case InvalidPath:
exitWithInvalidFilePath();
break;
case Unknown:
exitWithUnknownFile();
break;
}
}
break;
......@@ -156,6 +179,21 @@ public class DataCompressor {
ScifioWrapper.dispose();
}
private static void exitWithUnknownFile() {
ColorConsole.fprintf(ColorConsole.Target.stderr,
ColorConsole.Color.Red,
"Provided file is of unknown type. Only QCMP and QVC files are supported.\n");
exitApplication(1);
}
private static void exitWithInvalidFilePath() {
ColorConsole.fprintf(ColorConsole.Target.stderr,
ColorConsole.Color.Red,
"File specified by provided path doesn't exist.\n");
exitApplication(1);
}
private static void exitApplication(final int exitCode) {
ScifioWrapper.dispose();
System.exit(exitCode);
......
......@@ -15,7 +15,7 @@ public class QCMPFileHeader implements IFileHeader, Cloneable {
//region Constants
private static final int VERSION = 1;
private static final int BASE_QCMP_HEADER_SIZE = 23;
private static final String MAGIC_VALUE = "QCMPFILE";
public static final String MAGIC_VALUE = "QCMPFILE";
//endregion
//region Header fields
......
package cz.it4i.qcmp.io;
import cz.it4i.qcmp.fileformat.QCMPFileHeader;
import cz.it4i.qcmp.fileformat.QvcHeaderV1;
import cz.it4i.qcmp.fileformat.QvcHeaderV2;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileTypeInspector {
public enum FileType {
/**
* Compressed image hyperstack.
*/
Qcmp,
/**
* Codebook cache file.
*/
Qvc,
/**
* Invalid file path.
*/
InvalidPath,
/**
* No known magic value was matched.
*/
Unknown
}
/**
* Inspect file specified by path and return its type.
*
* @param filePath Path to the file.
* @return File type.
*/
public static FileType inspectFile(final String filePath) {
// QCMPFileHeader.MAGIC_VALUE // 8 bytes
// QvcHeaderV1.MAGIC_VALUE // 9 bytes
// QvcHeaderV2.MAGIC_VALUE // 9 bytes
try (final FileInputStream stream = new FileInputStream(filePath)) {
final byte[] buf1 = new byte[QCMPFileHeader.MAGIC_VALUE.length()];
RawDataIO.readFullBuffer(stream, buf1);
if (new String(buf1).equals(QCMPFileHeader.MAGIC_VALUE)) {
return FileType.Qcmp;
}
final byte[] buf2 = new byte[QvcHeaderV1.MAGIC_VALUE.length()];
System.arraycopy(buf1, 0, buf2, 0, buf1.length);
final int read = stream.read(buf2, buf1.length, 1);
if (read != 1)
return FileType.Unknown;
final String magicValue = new String(buf2);
if (magicValue.equals(QvcHeaderV1.MAGIC_VALUE) || magicValue.equals(QvcHeaderV2.MAGIC_VALUE))
return FileType.Qvc;
} catch (final FileNotFoundException e) {
return FileType.InvalidPath;
} catch (final IOException e) {
return FileType.Unknown;
}
return FileType.Unknown;
}
}
......@@ -48,10 +48,10 @@ public final class ColorConsole {
switch (target) {
case stdout:
System.out.println(getColor(color) + string + ANSI_RESET);
System.out.print(getColor(color) + string + ANSI_RESET);
break;
case stderr:
System.err.println(getColor(color) + string + ANSI_RESET);
System.err.print(getColor(color) + string + ANSI_RESET);
break;
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment