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

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

                Author: ASF GitHub Bot
            Created on: 08/May/20 13:02
            Start Date: 08/May/20 13:02
    Worklog Time Spent: 10m 
      Work Description: gwlucastrig commented on a change in pull request #72:
URL: https://github.com/apache/commons-imaging/pull/72#discussion_r422129686



##########
File path: 
src/test/java/org/apache/commons/imaging/formats/tiff/photometricinterpreters/fp/PhotometricInterpreterFloatTest.java
##########
@@ -0,0 +1,248 @@
+/*
+ * 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.formats.tiff.photometricinterpreters.fp;
+
+import java.awt.Color;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.commons.imaging.ImageReadException;
+import org.apache.commons.imaging.ImageWriteException;
+import org.apache.commons.imaging.common.ImageBuilder;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.*;
+
+/**
+ * Provides a unit test for the TIFF photometric interpreter used for mapping
+ * floating-point values to a color palette.
+ */
+public class PhotometricInterpreterFloatTest {
+
+    private static PhotometricInterpreterFloat pInterp;
+    private static PhotometricInterpreterFloat bandedInterp;
+    private static ImageBuilder imageBuilder;
+    private static ImageBuilder bandedImageBuilder;
+
+    private static final Color orange = new Color(255, 136, 62);
+    private static final Color green = new Color(22, 155, 98);
+
+    public PhotometricInterpreterFloatTest() {
+    }
+
+    @BeforeAll
+    public static void setUpClass() throws ImageWriteException, 
ImageReadException, IOException {
+        // the setup is to assign color (grayscale) values to the
+        // pixels along the main diagonal at coordinates
+        // (0, 0), (1, 1), ... (256, 256).
+        // The floating point values at each pixel are just the
+        // index divided by 256.
+
+        List<IPaletteEntry> paletteList = new ArrayList<>();
+        List<IPaletteEntry> reverseList = new ArrayList<>();
+        for (int i = 0; i < 256; i += 32) {
+            int i1 = i + 31;
+            float f0 = (float) i / 256f;
+            float f1 = (float) (i + 32) / 256f;
+            int argb0 = 0xff000000 | (i << 8) | i;
+            int argb1 = 0xff000000 | (i1 << 8) | i;
+            Color c0 = new Color(argb0);
+            Color c1 = new Color(argb1);
+            PaletteEntryForRange entry = new PaletteEntryForRange(f0, f1, c0, 
c1);
+            paletteList.add(entry);
+        }
+        // The interpreter is supposed to sort entries.  To test that,
+        // we copy them to a list in reverse order.
+        for (int i = paletteList.size() - 1; i >= 0; i--) {
+            IPaletteEntry entry = paletteList.get(i);
+            reverseList.add(entry);
+        }
+
+        pInterp = new PhotometricInterpreterFloat(reverseList);
+
+        // pre-populate the state data for the interpreter with
+        // some values so that we can test min/max access methods.
+        imageBuilder = new ImageBuilder(257, 257, false);
+        int[] samples = new int[1];
+        for (int i = 0; i <= 256; i++) {
+            float f = (float) i / 256f;
+            samples[0] = Float.floatToRawIntBits(f);
+            pInterp.interpretPixel(imageBuilder, samples, i, i);
+        }
+
+        // Now set up a palette than maps values in a range to a single color.
+        List<IPaletteEntry> bandedPaletteList = new ArrayList<>();
+        bandedPaletteList.add(new PaletteEntryForRange(0f, 0.33f, green));
+        bandedPaletteList.add(new PaletteEntryForRange(0.33f, 0.66f, 
Color.white));
+        bandedPaletteList.add(new PaletteEntryForRange(0.66f, 1.0f, orange));
+        bandedPaletteList.add(new PaletteEntryForValue(Float.NaN, Color.gray));
+        bandedPaletteList.add(new PaletteEntryForValue(-1, Color.gray));
+        bandedInterp = new PhotometricInterpreterFloat(bandedPaletteList);
+        bandedImageBuilder = new ImageBuilder(300, 200, false);
+        for (int j = 0; j < 300; j++) {
+            float f = (float) j / 299.0f;
+            samples[0] = Float.floatToRawIntBits(f);
+            for (int i = 0; i < 200; i++) {
+                bandedInterp.interpretPixel(bandedImageBuilder, samples, j, i);
+            }
+        }
+        samples[0] = Float.floatToRawIntBits(Float.NaN);
+        for (int i = 0; i < 200; i++) {
+            bandedInterp.interpretPixel(bandedImageBuilder, samples, 0, i);
+            bandedInterp.interpretPixel(bandedImageBuilder, samples, 299, i);
+        }
+        samples[0] = Float.floatToRawIntBits(-1);
+        for (int i = 0; i < 300; i++) {
+            bandedInterp.interpretPixel(bandedImageBuilder, samples, i, 0);
+            bandedInterp.interpretPixel(bandedImageBuilder, samples, i, 199);
+        }
+    }
+
+    /**
+     * Test of interpretPixel method, of class PhotometricInterpreterFloat.
+     */
+    @Test
+    public void testInterpretPixel() {
+        for (int i = 0; i < 256; i++) {
+            int lowTest = (i / 32) * 32;
+            int argb = imageBuilder.getRGB(i, i);
+            int b = argb & 0xff;
+            assertEquals(b, lowTest, "Invalid conversion for level " + i);
+        }
+
+        // nothing should match the i=256 case.
+        // The last entry in the palette has values
+        // in the range  224.0/256.0 <= value < 256.0/256.0.  So when it
+        // was rendered, there was not palette entry that matched it,
+        // and the corresponding pixel was set to zero.
+        int argb = imageBuilder.getRGB(256, 256);
+        assertEquals(argb, 0, "Invalid upper-bound test");
+
+        // Now inspect the banded palette case
+        argb = bandedImageBuilder.getRGB(0, 0);
+        assertEquals(Color.gray.getRGB(), argb, "Invalid mapping of NaN");
+        argb = bandedImageBuilder.getRGB(50, 10);
+        assertEquals(green.getRGB(), argb, "Invalid mapping of green range");
+        argb = bandedImageBuilder.getRGB(150, 10);
+        assertEquals(Color.white.getRGB(), argb, "Invalid mapping of white 
range");
+        argb = bandedImageBuilder.getRGB(250, 10);
+        assertEquals(orange.getRGB(), argb, "Invalid mapping of orange range");
+        // Example code to write image to PNG file for visual inspection.
+        //try {
+        //    BufferedImage bImage = bandedImageBuilder.getBufferedImage();
+        //    ImageIO.write(bImage, "PNG", new 
File("C:/Users/Public/test1.png"));
+        //} catch (IOException ioex) {
+        //    fail("IOException saving test image" + ioex.getMessage());
+        //}

Review comment:
       It's not necessary.  I left it in to help somebody who was trying to 
debug the code and wanted to inspect the result. But leaving it in the code is 
it's like leaving the scaffolding up after you've painted a house.   I would 
follow your preference on this one.




----------------------------------------------------------------
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: 432105)
    Time Spent: 8h 40m  (was: 8.5h)

> 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: 8h 40m
>  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