You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2020/10/18 06:34:11 UTC

[GitHub] [commons-imaging] kinow commented on a change in pull request #103: [Imaging-268] Add list of TIFF files and example survey application

kinow commented on a change in pull request #103:
URL: https://github.com/apache/commons-imaging/pull/103#discussion_r507021228



##########
File path: src/test/java/org/apache/commons/imaging/examples/tiff/SurveyTiffFile.java
##########
@@ -0,0 +1,454 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.imaging.examples.tiff;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.util.Formatter;
+import org.apache.commons.imaging.FormatCompliance;
+import org.apache.commons.imaging.ImageReadException;
+import org.apache.commons.imaging.common.bytesource.ByteSourceFile;
+import org.apache.commons.imaging.formats.tiff.TiffContents;
+import org.apache.commons.imaging.formats.tiff.TiffDirectory;
+import org.apache.commons.imaging.formats.tiff.TiffField;
+import org.apache.commons.imaging.formats.tiff.TiffReader;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_CCITT_1D;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_CCITT_GROUP_3;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_CCITT_GROUP_4;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_DEFLATE_ADOBE;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_DEFLATE_PKZIP;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_LZW;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_PACKBITS;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_UNCOMPRESSED;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_UNCOMPRESSED_1;
+import org.apache.commons.imaging.formats.tiff.constants.TiffEpTagConstants;
+import org.apache.commons.imaging.formats.tiff.constants.TiffPlanarConfiguration;
+import org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants;
+
+/**
+ * Provides methods to collect data for a tiff file. This class is ntended for
+ * use with
+ * SurveyTiffFolder, though it could be integrated into other applications.
+ */
+public class SurveyTiffFile {
+
+    public String surveyFile(File file, boolean csv) throws ImageReadException, IOException {
+        String delimiter = "  ";
+        if (csv) {
+            delimiter = ", ";
+        }
+
+        StringBuilder sb = new StringBuilder();
+        Formatter fmt = new Formatter(sb);
+
+        // Establish a TiffReader. This is just a simple constructor that
+        // does not actually access the file.  So the application cannot
+        // obtain the byteOrder, or other details, until the contents has

Review comment:
       s/contents has/contents have

##########
File path: src/test/java/org/apache/commons/imaging/examples/tiff/SurveyTiffFolder.java
##########
@@ -0,0 +1,237 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.imaging.examples.tiff;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import org.apache.commons.imaging.ImageReadException;
+
+/**
+ * Recursively search the specified path and list TIFF files and metadata.
+ * <p>
+ * Command-line Arguments:</p>
+ * <ol>
+ * <li>Top-level directory (mandatory)</li>
+ * <li>Output file for results (optional></li>
+ * </ol>
+ * If the optional output file has the extension ".csv", the output
+ * will be formatted as a comma-separated-value file suitable
+ * for inspection in Excel.
+ */
+public class SurveyTiffFolder {
+
+    /**
+     * @param args the command line arguments
+     */
+    public static void main(String[] args) {
+        if (args.length < 1) {
+            System.err.println("Missing directory path");
+            System.exit(-1);
+        }
+        File topLevelDir = new File(args[0]);
+        if (!topLevelDir.isDirectory() || !topLevelDir.canRead()) {
+            System.err.println("Path specification is not an accessible directory " + args[0]);
+            System.exit(-1);
+        }
+
+        // recursively survey file paths
+        String[] scratch = new String[256];
+        List<String[]> pathList = new ArrayList<>();
+        collectPaths(topLevelDir, pathList, scratch, 0);
+        pathList.sort(new PathComparator());
+
+        // find maximum lengths of each entry
+        int[] maxLen = findMaxLengths(pathList);
+
+        // If args.length is 1, write report to System.out,
+        // otherwise, write to a file.
+        if (args.length == 1) {
+            surveyFiles(topLevelDir, pathList, maxLen, false, System.out);
+        } else {
+
+            boolean csv = false;
+
+            int i = args[1].lastIndexOf('.');
+            if (i > 0) {
+                String ext = args[1].substring(i);
+                if (".csv".equalsIgnoreCase(ext)) {
+                    csv = true;
+                }
+            }
+            File reportFile = new File(args[1]);
+            try (FileOutputStream fos = new FileOutputStream(reportFile);
+                BufferedOutputStream bos = new BufferedOutputStream(fos);
+                PrintStream ps = new PrintStream(bos, true, "UTF-8");) {

Review comment:
       Unnecessary `;`

##########
File path: src/test/java/org/apache/commons/imaging/examples/tiff/SurveyTiffFolder.java
##########
@@ -0,0 +1,237 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.imaging.examples.tiff;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import org.apache.commons.imaging.ImageReadException;
+
+/**
+ * Recursively search the specified path and list TIFF files and metadata.
+ * <p>
+ * Command-line Arguments:</p>
+ * <ol>
+ * <li>Top-level directory (mandatory)</li>
+ * <li>Output file for results (optional></li>

Review comment:
       s/(optional>/(optional)

##########
File path: src/test/java/org/apache/commons/imaging/examples/tiff/SurveyTiffFile.java
##########
@@ -0,0 +1,454 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.imaging.examples.tiff;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.util.Formatter;
+import org.apache.commons.imaging.FormatCompliance;
+import org.apache.commons.imaging.ImageReadException;
+import org.apache.commons.imaging.common.bytesource.ByteSourceFile;
+import org.apache.commons.imaging.formats.tiff.TiffContents;
+import org.apache.commons.imaging.formats.tiff.TiffDirectory;
+import org.apache.commons.imaging.formats.tiff.TiffField;
+import org.apache.commons.imaging.formats.tiff.TiffReader;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_CCITT_1D;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_CCITT_GROUP_3;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_CCITT_GROUP_4;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_DEFLATE_ADOBE;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_DEFLATE_PKZIP;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_LZW;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_PACKBITS;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_UNCOMPRESSED;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_UNCOMPRESSED_1;
+import org.apache.commons.imaging.formats.tiff.constants.TiffEpTagConstants;
+import org.apache.commons.imaging.formats.tiff.constants.TiffPlanarConfiguration;
+import org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants;
+
+/**
+ * Provides methods to collect data for a tiff file. This class is ntended for
+ * use with
+ * SurveyTiffFolder, though it could be integrated into other applications.
+ */
+public class SurveyTiffFile {
+
+    public String surveyFile(File file, boolean csv) throws ImageReadException, IOException {
+        String delimiter = "  ";
+        if (csv) {
+            delimiter = ", ";
+        }
+
+        StringBuilder sb = new StringBuilder();
+        Formatter fmt = new Formatter(sb);
+
+        // Establish a TiffReader. This is just a simple constructor that
+        // does not actually access the file.  So the application cannot
+        // obtain the byteOrder, or other details, until the contents has
+        // been read.  Then read the directories associated with the
+        // file by passing in the byte source and options.
+        ByteSourceFile byteSource = new ByteSourceFile(file);
+        TiffReader tiffReader = new TiffReader(true);
+        TiffContents contents = tiffReader.readDirectories(
+            byteSource,
+            false, // read image data, if present
+            FormatCompliance.getDefault());
+
+        if (contents.directories.isEmpty()) {
+            throw new ImageReadException("No Image File Directory (IFD) found");
+        }
+        TiffDirectory directory = contents.directories.get(0);
+
+        // Get the metadata (Tags) and write them to standard output
+        boolean hasTiffImageData = directory.hasTiffImageData();
+        if (!hasTiffImageData) {
+            throw new ImageReadException("No image data in file");
+        }
+
+        final int width = directory.getSingleFieldValue(TiffTagConstants.TIFF_TAG_IMAGE_WIDTH);
+        final int height = directory.getSingleFieldValue(TiffTagConstants.TIFF_TAG_IMAGE_LENGTH);
+
+        int samplesPerPixel = 1;
+        final TiffField samplesPerPixelField = directory.findField(
+            TiffTagConstants.TIFF_TAG_SAMPLES_PER_PIXEL);
+        if (samplesPerPixelField != null) {
+            samplesPerPixel = samplesPerPixelField.getIntValue();
+        }
+        int[] bitsPerSample = {1};
+        int bitsPerPixel = samplesPerPixel;
+        final TiffField bitsPerSampleField = directory.findField(
+            TiffTagConstants.TIFF_TAG_BITS_PER_SAMPLE);
+        if (bitsPerSampleField != null) {
+            bitsPerSample = bitsPerSampleField.getIntArrayValue();
+            bitsPerPixel = bitsPerSampleField.getIntValueOrArraySum();
+        }
+        if (samplesPerPixel != bitsPerSample.length) {
+            throw new ImageReadException("Tiff: samplesPerPixel ("
+                + samplesPerPixel + ")!=fBitsPerSample.length ("
+                + bitsPerSample.length + ")");
+        }
+
+        int rowsPerStrip = 0;
+        int tileWidth = 0;
+        int tileHeight = 0;
+
+        boolean imageDataInStrips = directory.imageDataInStrips();
+        if (imageDataInStrips) {
+            final TiffField rowsPerStripField
+                = directory.findField(TiffTagConstants.TIFF_TAG_ROWS_PER_STRIP);
+            rowsPerStrip = Integer.MAX_VALUE;
+            if (null != rowsPerStripField) {
+                rowsPerStrip = rowsPerStripField.getIntValue();
+            } else {
+                final TiffField imageHeight = directory.findField(TiffTagConstants.TIFF_TAG_IMAGE_LENGTH);
+                /**

Review comment:
       `/*` as this is just a general comment.

##########
File path: src/test/java/org/apache/commons/imaging/examples/tiff/SurveyTiffFile.java
##########
@@ -0,0 +1,454 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.imaging.examples.tiff;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.util.Formatter;
+import org.apache.commons.imaging.FormatCompliance;
+import org.apache.commons.imaging.ImageReadException;
+import org.apache.commons.imaging.common.bytesource.ByteSourceFile;
+import org.apache.commons.imaging.formats.tiff.TiffContents;
+import org.apache.commons.imaging.formats.tiff.TiffDirectory;
+import org.apache.commons.imaging.formats.tiff.TiffField;
+import org.apache.commons.imaging.formats.tiff.TiffReader;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_CCITT_1D;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_CCITT_GROUP_3;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_CCITT_GROUP_4;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_DEFLATE_ADOBE;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_DEFLATE_PKZIP;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_LZW;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_PACKBITS;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_UNCOMPRESSED;
+import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_COMPRESSION_UNCOMPRESSED_1;
+import org.apache.commons.imaging.formats.tiff.constants.TiffEpTagConstants;
+import org.apache.commons.imaging.formats.tiff.constants.TiffPlanarConfiguration;
+import org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants;
+
+/**
+ * Provides methods to collect data for a tiff file. This class is ntended for

Review comment:
       s/ntended/intended




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org