[ 
https://issues.apache.org/jira/browse/IMAGING-251?focusedWorklogId=424534&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-424534
 ]

ASF GitHub Bot logged work on IMAGING-251:
------------------------------------------

                Author: ASF GitHub Bot
            Created on: 18/Apr/20 00:26
            Start Date: 18/Apr/20 00:26
    Worklog Time Spent: 10m 
      Work Description: kinow commented on pull request #72: IMAGING-251 
support for TIFF floating-point formats
URL: https://github.com/apache/commons-imaging/pull/72#discussion_r410523769
 
 

 ##########
 File path: 
src/test/java/org/apache/commons/imaging/examples/tiff/ReadAndRenderFloatingPoint.java
 ##########
 @@ -0,0 +1,173 @@
+/*
+ * 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.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.nio.ByteOrder;
+import java.util.HashMap;
+import javax.imageio.ImageIO;
+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.TiffReader;
+import org.apache.commons.imaging.formats.tiff.constants.TiffConstants;
+import org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants;
+import 
org.apache.commons.imaging.formats.tiff.photometricinterpreters.fp.PhotometricInterpreterFloat;
+
+/**
+ * A simple example application that reads the content of a TIFF file 
containing
+ * floating-point data and renders it using a gray-scale palette. TIFF files 
are
+ * sometimes used to store non-image information for scientific and geophysical
+ * data products, including terrestrial elevation and ocean depth data.
+ *
+ */
+public class ReadAndRenderFloatingPoint {
+
+    private static final String[] USAGE = {
+        "Usage ReadAndRendserFloatingPoint <input file>  [output file]",
+        "   input file:  Mandatory file to be read",
+        "   output file: Optional output path for file to be written.",
+        "                This name should not include a file extension.",
+        "                Output data will be written using the JPEG format."
+    };
+
+    /**
+     * Reads the content of a TIFF file containing floating-point data and
+     * renders it using a gray-scale palette.
+     *
+     * @param args the command line arguments giving the path to an input TIFF
+     * file and an output JPEG.
+     * @throws org.apache.commons.imaging.ImageReadException in the event of an
+     * internal data format or version compatibility error reading the image.
+     * @throws java.io.IOException in the event of an I/O error.
+     */
+    public static void main(String[] args) throws ImageReadException, 
IOException {
+        if (args.length == 0) {
+            // Print usage and exit
+            for (String s : USAGE) {
+                System.err.println(s);
+            }
+            System.exit(0);
+        }
+
+        File target = new File(args[0]);
+        String outputPath = null;
+        if (args.length == 2) {
+            outputPath = args[1];
+        }
+        boolean optionalImageWritingEnabled
+            = outputPath != null && !outputPath.isEmpty();
+
+        ByteSourceFile byteSource = new ByteSourceFile(target);
+
+        // 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.
+        TiffReader tiffReader = new TiffReader(true);
+
+        // Read the directories in the TIFF file.  Directories are the
+        // main data element of a TIFF file. They usually include an image
+        // element, but sometimes just carry metadata. This example
+        // reads all the directories in the file, but if we were interested
+        // in just the first element, Commons Imaging provides alternate API's
+        // that would be more efficient.
+        TiffContents contents = tiffReader.readDirectories(
+            byteSource,
+            true, // indicates that application should read image data, if 
present
+            FormatCompliance.getDefault());
+        ByteOrder byteOrder = tiffReader.getByteOrder();
+
+        // Render the first directory in the file.  A practical implementation
+        // could use any of the directories in the file. This demo uses the
+        // first one just for simplicity.
+        TiffDirectory directory = contents.directories.get(0);
+        // Render the first directory in the file
+        if (!directory.hasTiffImageData()) {
+            System.err.println("First directory in file does not have image");
+            System.exit(-1);
+        }
+
+        // Obtain metadata about what kind of data is in the product.
+        // Because this demo is designed for floating-point data,
+        // it will expect the sample format to include only one element
+        // of type floating point.   Beyond that, the TIFF specification
+        // allows a large number of variations in format (16, 24, or 32 byte
+        // floats; tiles versus strips; etc.).  Unfortunately the test data
+        // for the less common variations was not available when Commons 
Imaging
+        // was implemented and so the API will not be able to handle all
+        // of them.  This test will simply allow the API to throw an exception
+        // if an unsupported format is encountered.
+        //    The getFieldValue call allows an application to provide a
+        // boolean indicating that the field must be present for processing
+        // to continue. If it does not, an exception is thrown.
+        short[] sampleFormat = 
directory.getFieldValue(TiffTagConstants.TIFF_TAG_SAMPLE_FORMAT, true);
+        short samplesPerPixel = 
directory.getFieldValue(TiffTagConstants.TIFF_TAG_SAMPLES_PER_PIXEL);
+        short[] bitsPerPixel = 
directory.getFieldValue(TiffTagConstants.TIFF_TAG_BITS_PER_SAMPLE, true);
+        if (sampleFormat[0] != 
TiffTagConstants.SAMPLE_FORMAT_VALUE_IEEE_FLOATING_POINT) {
+            System.err.println("This example requires a data source with a 
floating-point format");
+            System.exit(-1);
+        }
+
+        System.out.print("Bits per pixel: ");
+        for (int i = 0; i < samplesPerPixel; i++) {
+            System.out.format("%s%d", i > 0 ? ", " : "", bitsPerPixel[i]);
+        }
+        System.out.println("");
+
+        // Create a TIFF Photometric Interpreter to perform a grayscale
+        // rendering.  A Photometric Interpreter is a class that maps a
+        // floating-point value to a pixel (RGB) equivalent.
+        // Because there is no way to know the range of
+        // values in the TIFF before it is read.  So the special-purpose
+        // interpreter permits an application to extract the
+        // min and maximum bounds of the data as an auxiliary function
+        // of interpreting the data. Unfortunately, if we wish for a
+        // good interpretation, we need to read the data twice.
+        //    For this demo, we store the Photometric Interpreter instance
+        // as a option-parameter to be passed into the read-image method.
+        PhotometricInterpreterFloat pi = new PhotometricInterpreterFloat(0.0f, 
1.0f);
+        HashMap<String, Object> params = new HashMap<>();
+        params.put(TiffConstants.PARAM_KEY_CUSTOM_PHOTOMETRIC_INTERPRETER, pi);
+        System.out.println("Reading image");
 
 Review comment:
   Unless really necessary, it would be better to remove the string printed to 
stdout, as they can get in the way when troubleshooting failing tests.
 
----------------------------------------------------------------
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:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 424534)
    Time Spent: 5h  (was: 4h 50m)

> Support TIFF standard floating point data
> -----------------------------------------
>
>                 Key: IMAGING-251
>                 URL: https://issues.apache.org/jira/browse/IMAGING-251
>             Project: Commons Imaging
>          Issue Type: New Feature
>          Components: Format: TIFF
>    Affects Versions: 1.x
>            Reporter: Gary Lucas
>            Priority: Major
>             Fix For: 1.x
>
>         Attachments: Imaging252_USGS_n38w077.jpg
>
>          Time Spent: 5h
>  Remaining Estimate: 0h
>
> Commons Imaging does not support the floating-point format included in the 
> TIFF specification. There are prominent data sources that issue products in 
> this format. The ability to support this information would open up new 
> application areas for Commons Imaging.
> TIFF is often used as a mechanism for distributing data from geophysical 
> applications in the form of GeoTIFF files.  Some of this is not imagery, but 
> data. For example, the US Geological Survey is currently releasing 
> high-resolution elevation data grids for the 3DEP program under the name 
> Cloud-Optimized GeoTIFF (COG). It is a substantial data set with significant 
> potential commercial and academic applications.
> To access this data means modifying the TIFF DataReaderStrips and 
> DataReaderTile classes to recognize floating point data (which is typically 
> indicated using TIFF tag #339, SampleFormat). Also, returning the data in the 
> form of a BufferedImage makes no sense at all, so the API on the 
> TiffImageParser and supporting classes would need additional methods to 
> return arrays of floats.  The good news here is that that requirement would 
> mean adding new methods to the classes rather than making significant changes 
> to existing classes. So the probability of unintended consequences or new 
> bugs in existing code would be minimized.
> Specification details for floating-point are given in the main TIFF-6 
> documentations and Adobe Photoshop TIFF Technical Note 3.
>  
> I am willing to volunteer to make these changes provided that there is 
> interest and a high probability that my contributions would be evaluated and, 
> if suitable, integrated into the Commons Imaging code base. 
> Thank you for your attention in this matter.
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to