commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dam...@apache.org
Subject svn commit: r1540252 - in /commons/proper/imaging/trunk/src: main/java/org/apache/commons/imaging/formats/tiff/taginfos/ main/java/org/apache/commons/imaging/palette/ test/data/images/ test/data/images/tiff/7/ test/java/org/apache/commons/imaging/ test...
Date Sat, 09 Nov 2013 03:18:33 GMT
Author: damjan
Date: Sat Nov  9 03:18:33 2013
New Revision: 1540252

URL: http://svn.apache.org/r1540252
Log:
Add some Javadoc and tests.
Fix a bug in string parsing from TIFF tags.
Fix a ColorGroup bug where a list that needs to be sorted is immutable.


Added:
    commons/proper/imaging/trunk/src/test/data/images/tiff/7/
    commons/proper/imaging/trunk/src/test/data/images/tiff/7/Oregon Scientific DS6639 - DSC_0307
- small - CMYK.tiff   (with props)
    commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/tiff/TiffReadWriteTagsTest.java
  (with props)
    commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/palette/
    commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/palette/PaletteQuantizationTest.java
  (with props)
Modified:
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoAscii.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/ColorGroup.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/Dithering.java
    commons/proper/imaging/trunk/src/test/data/images/info.txt
    commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/ImagingGuessFormatTest.java
    commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/tiff/TiffRoundtripTest.java

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoAscii.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoAscii.java?rev=1540252&r1=1540251&r2=1540252&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoAscii.java
(original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/tiff/taginfos/TagInfoAscii.java
Sat Nov  9 03:18:33 2013
@@ -30,7 +30,7 @@ public class TagInfoAscii extends TagInf
     }
 
     public String[] getValue(final ByteOrder byteOrder, final byte[] bytes) {
-        int nullCount = 1;
+        int nullCount = 0;
         for (int i = 0; i < bytes.length - 1; i++) {
             if (bytes[i] == 0) {
                 nullCount++;

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/ColorGroup.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/ColorGroup.java?rev=1540252&r1=1540251&r2=1540252&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/ColorGroup.java
(original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/ColorGroup.java
Sat Nov  9 03:18:33 2013
@@ -16,7 +16,6 @@
  */
 package org.apache.commons.imaging.palette;
 
-import java.util.Collections;
 import java.util.List;
 
 import org.apache.commons.imaging.ImageWriteException;
@@ -49,7 +48,7 @@ class ColorGroup {
 
     public ColorGroup(final List<ColorCount> color_counts, final boolean ignoreAlpha)
             throws ImageWriteException {
-        this.color_counts = Collections.unmodifiableList(color_counts);
+        this.color_counts = color_counts;
         this.ignoreAlpha = ignoreAlpha;
 
         if (color_counts.size() < 1) {

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/Dithering.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/Dithering.java?rev=1540252&r1=1540251&r2=1540252&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/Dithering.java
(original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/palette/Dithering.java
Sat Nov  9 03:18:33 2013
@@ -20,7 +20,18 @@ import java.awt.image.BufferedImage;
 
 import org.apache.commons.imaging.ImageWriteException;
 
+/**
+ * Dithering algorithms to use when quantizing an image to paletted form.
+ */
 public class Dithering {
+    /**
+     * Changes the given image to only use colors from the given palette,
+     * applying Floyd-Steinberg dithering in the process. Ensure that
+     * your alpha values in the image and in the palette are consistent.
+     * @param image the image to change
+     * @param palette the palette to use
+     * @throws ImageWriteException
+     */
     public static void applyFloydSteinbergDithering(final BufferedImage image, final Palette
palette) throws ImageWriteException {
         for (int y = 0; y < image.getHeight(); y++) {
             for (int x = 0; x < image.getWidth(); x++) {

Modified: commons/proper/imaging/trunk/src/test/data/images/info.txt
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/test/data/images/info.txt?rev=1540252&r1=1540251&r2=1540252&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/test/data/images/info.txt (original)
+++ commons/proper/imaging/trunk/src/test/data/images/info.txt Sat Nov  9 03:18:33 2013
@@ -208,6 +208,10 @@ tiff/6/bad-interoperability.asm
 tiff/6/bad-interoperability.tiff
 	Contributed by Damjan Jovanovic.
 	Test image with a 0 count interoperability TIFF tag.
+
+tiff/7/Oregon Scientific DS6639 - DSC_0307 - small - CMYK.tiff
+	Charles Matthew Chen's reference photo, converted by Damjan Jovanovic.
+	Test TIFF with CMYK color.
 	
 wbmp/1/Oregon Scientific DS6639 - DSC_0307 - small.wbmp
 	Charles Matthew Chen's reference photo, converted by Damjan Jovanovic.

Added: commons/proper/imaging/trunk/src/test/data/images/tiff/7/Oregon Scientific DS6639 -
DSC_0307 - small - CMYK.tiff
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/test/data/images/tiff/7/Oregon%20Scientific%20DS6639%20-%20DSC_0307%20-%20small%20-%20CMYK.tiff?rev=1540252&view=auto
==============================================================================
Binary file - no diff available.

Propchange: commons/proper/imaging/trunk/src/test/data/images/tiff/7/Oregon Scientific DS6639
- DSC_0307 - small - CMYK.tiff
------------------------------------------------------------------------------
    svn:mime-type = image/tiff

Modified: commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/ImagingGuessFormatTest.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/ImagingGuessFormatTest.java?rev=1540252&r1=1540251&r2=1540252&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/ImagingGuessFormatTest.java
(original)
+++ commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/ImagingGuessFormatTest.java
Sat Nov  9 03:18:33 2013
@@ -57,7 +57,7 @@ public class ImagingGuessFormatTest exte
         // testGuess(ImageFormat.IMAGE_FORMAT_JBIG2, JBIG2_IMAGE_FILE);
     }
 
-    public static final String UNKNOWN_IMAGE_FILE = "jpg\\1\\info.txt";
+    public static final String UNKNOWN_IMAGE_FILE = "info.txt";
 
     public void testGuess_unknown() throws Exception {
         testGuess(ImageFormats.UNKNOWN, UNKNOWN_IMAGE_FILE);

Added: commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/tiff/TiffReadWriteTagsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/tiff/TiffReadWriteTagsTest.java?rev=1540252&view=auto
==============================================================================
--- commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/tiff/TiffReadWriteTagsTest.java
(added)
+++ commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/tiff/TiffReadWriteTagsTest.java
Sat Nov  9 03:18:33 2013
@@ -0,0 +1,79 @@
+/*
+ * 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;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Map;
+import java.util.TreeMap;
+
+import org.apache.commons.imaging.FormatCompliance;
+import org.apache.commons.imaging.ImageReadException;
+import org.apache.commons.imaging.ImageWriteException;
+import org.apache.commons.imaging.common.RationalNumber;
+import org.apache.commons.imaging.common.bytesource.ByteSourceArray;
+import org.apache.commons.imaging.formats.tiff.constants.AllTagConstants;
+import org.apache.commons.imaging.formats.tiff.write.TiffImageWriterLossy;
+import org.apache.commons.imaging.formats.tiff.write.TiffOutputDirectory;
+import org.apache.commons.imaging.formats.tiff.write.TiffOutputSet;
+
+public class TiffReadWriteTagsTest extends TiffBaseTest {
+    public void testReadWriteTags() throws ImageWriteException, ImageReadException, IOException
{
+        String description = "A pretty picture";
+        short page = 1;
+        RationalNumber twoThirds = new RationalNumber(2, 3);
+        int t4Options = 0;
+        int width = 10;
+        short height = 10;
+        String area = "A good area";
+        float widthRes = 2.2f;
+        double geoDoubleParams = -8.4;
+        
+        TiffOutputSet set = new TiffOutputSet();
+        TiffOutputDirectory dir = set.getOrCreateRootDirectory();
+        dir.add(AllTagConstants.TIFF_TAG_IMAGE_DESCRIPTION, description);
+        dir.add(AllTagConstants.TIFF_TAG_PAGE_NUMBER, page, page);
+        dir.add(AllTagConstants.TIFF_TAG_YRESOLUTION, twoThirds);
+        dir.add(AllTagConstants.TIFF_TAG_T4_OPTIONS, t4Options);
+        dir.add(AllTagConstants.TIFF_TAG_IMAGE_WIDTH, width);
+        dir.add(AllTagConstants.TIFF_TAG_IMAGE_LENGTH, new short[] { height });
+        dir.add(AllTagConstants.GPS_TAG_GPS_AREA_INFORMATION, area);
+        dir.add(AllTagConstants.EXIF_TAG_WIDTH_RESOLUTION, widthRes);
+        dir.add(AllTagConstants.EXIF_TAG_GEO_DOUBLE_PARAMS_TAG, geoDoubleParams);
+        
+        TiffImageWriterLossy writer = new TiffImageWriterLossy();
+        ByteArrayOutputStream tiff = new ByteArrayOutputStream();
+        writer.write(tiff, set);
+        
+        TiffReader reader = new TiffReader(true);
+        Map<String,Object> params = new TreeMap<String,Object>();
+        FormatCompliance formatCompliance = new FormatCompliance("");
+        TiffContents contents = reader.readFirstDirectory(new ByteSourceArray(tiff.toByteArray()),
params, true, formatCompliance);
+        TiffDirectory rootDir = contents.directories.get(0);
+        assertEquals(description, rootDir.getSingleFieldValue(AllTagConstants.TIFF_TAG_IMAGE_DESCRIPTION));
+        assertEquals(page, rootDir.getFieldValue(AllTagConstants.TIFF_TAG_PAGE_NUMBER, true)[0]);
+        RationalNumber yRes = rootDir.getSingleFieldValue(AllTagConstants.TIFF_TAG_YRESOLUTION);
+        assertEquals(twoThirds.numerator, yRes.numerator);
+        assertEquals(twoThirds.divisor, yRes.divisor);
+        assertEquals(t4Options, rootDir.getSingleFieldValue(AllTagConstants.TIFF_TAG_T4_OPTIONS));
+        assertEquals(width, rootDir.getSingleFieldValue(AllTagConstants.TIFF_TAG_IMAGE_WIDTH));
+        assertEquals(width, rootDir.getSingleFieldValue(AllTagConstants.TIFF_TAG_IMAGE_LENGTH));
+        assertEquals(area, rootDir.getFieldValue(AllTagConstants.GPS_TAG_GPS_AREA_INFORMATION,
true));
+        assertEquals(widthRes, rootDir.getSingleFieldValue(AllTagConstants.EXIF_TAG_WIDTH_RESOLUTION));
+        assertEquals(geoDoubleParams, rootDir.getSingleFieldValue(AllTagConstants.EXIF_TAG_GEO_DOUBLE_PARAMS_TAG));
+    }
+}

Propchange: commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/tiff/TiffReadWriteTagsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/tiff/TiffRoundtripTest.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/tiff/TiffRoundtripTest.java?rev=1540252&r1=1540251&r2=1540252&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/tiff/TiffRoundtripTest.java
(original)
+++ commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/formats/tiff/TiffRoundtripTest.java
Sat Nov  9 03:18:33 2013
@@ -27,6 +27,7 @@ import org.apache.commons.imaging.ImageF
 import org.apache.commons.imaging.ImageInfo;
 import org.apache.commons.imaging.Imaging;
 import org.apache.commons.imaging.common.IImageMetadata;
+import org.apache.commons.imaging.formats.tiff.constants.TiffConstants;
 import org.apache.commons.imaging.util.Debug;
 
 public class TiffRoundtripTest extends TiffBaseTest {
@@ -50,14 +51,20 @@ public class TiffRoundtripTest extends T
             BufferedImage image = Imaging.getBufferedImage(imageFile);
             assertNotNull(image);
 
-            final File tempFile = createTempFile(imageFile.getName() + ".", ".tif");
-            final Map<String,Object> params = new HashMap<String,Object>();
-            Imaging.writeImage(image, tempFile, ImageFormats.TIFF,
-                    params);
-            image = null;
-
-            final BufferedImage image2 = Imaging.getBufferedImage(tempFile);
-            assertNotNull(image2);
+            final int[] compressions = new int[] {
+                    TiffConstants.TIFF_COMPRESSION_UNCOMPRESSED,
+                    TiffConstants.TIFF_COMPRESSION_LZW,
+                    TiffConstants.TIFF_COMPRESSION_PACKBITS
+            };
+            for (final int compression : compressions) {
+                final File tempFile = createTempFile(imageFile.getName() + "-" + compression
+ ".", ".tif");
+                final Map<String,Object> params = new HashMap<String,Object>();
+                params.put(TiffConstants.PARAM_KEY_COMPRESSION, compression);
+                Imaging.writeImage(image, tempFile, ImageFormats.TIFF,
+                        params);
+                final BufferedImage image2 = Imaging.getBufferedImage(tempFile);
+                assertNotNull(image2);
+            }
         }
     }
 

Added: commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/palette/PaletteQuantizationTest.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/palette/PaletteQuantizationTest.java?rev=1540252&view=auto
==============================================================================
--- commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/palette/PaletteQuantizationTest.java
(added)
+++ commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/palette/PaletteQuantizationTest.java
Sat Nov  9 03:18:33 2013
@@ -0,0 +1,119 @@
+/*
+ * 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.palette;
+
+import java.awt.image.BufferedImage;
+
+import org.apache.commons.imaging.ImageWriteException;
+import org.apache.commons.imaging.ImagingTest;
+
+public class PaletteQuantizationTest extends ImagingTest {
+    public void testPaletteQuantization() throws ImageWriteException {
+        BufferedImage whiteImage = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
+        for (int y = 0; y < whiteImage.getHeight(); y++) {
+            for (int x = 0; x < whiteImage.getWidth(); x++) {
+                whiteImage.setRGB(x, y, 0xFFFFFF);
+            }
+        }
+        checkPaletteDetails(whiteImage, 10, 1);
+        
+        BufferedImage whiteAndBlackImage = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
+        for (int y = 0; y < whiteImage.getHeight(); y++) {
+            for (int x = 0; x < 5; x++) {
+                whiteAndBlackImage.setRGB(x, y, 0xFFFFFF);
+            }
+            for (int x = 5; x < 10; x++) {
+                whiteAndBlackImage.setRGB(x, y, 0x000000);
+            }
+        }
+        checkPaletteDetails(whiteAndBlackImage, 10, 2);
+        
+        BufferedImage rainbowImage = new BufferedImage(9, 10, BufferedImage.TYPE_INT_RGB);
+        for (int y = 0; y < whiteImage.getHeight(); y++) {
+            for (int x = 0; x < 3; x++) {
+                rainbowImage.setRGB(x, y, 0xFF0000);
+            }
+            for (int x = 3; x < 6; x++) {
+                rainbowImage.setRGB(x, y, 0x00FF00);
+            }
+            for (int x = 6; x < 9; x++) {
+                rainbowImage.setRGB(x, y, 0x0000FF);
+            }
+        }
+        checkPaletteDetails(rainbowImage, 10, 3);
+        checkPaletteDetails(rainbowImage, 2, 2);
+    }
+    
+    private void checkPaletteDetails(BufferedImage image, final int limit, final int expectedSize)
throws ImageWriteException {
+        PaletteFactory paletteFactory = new PaletteFactory();
+        Palette palette;
+
+        palette = paletteFactory.makeExactRgbPaletteSimple(image, limit);
+        // too small palettes are null
+        boolean exact = false;
+        if (palette != null) {
+            assertEquals(expectedSize, palette.length());
+            //checkUniqueColors(image, palette);
+            exact = true;
+        }
+        if (exact) {
+            //checkDithering(image, palette);
+        }
+
+        palette = paletteFactory.makeQuantizedRgbaPalette(image, false, limit);
+        assertEquals(expectedSize, palette.length());
+        checkUniqueColors(image, palette);
+        if (exact) {
+            checkPixelsAreIdentical(image, palette);
+        }
+        
+        palette = paletteFactory.makeQuantizedRgbPalette(image, limit);
+        assertEquals(expectedSize, palette.length());
+        //checkUniqueColors(image, palette);
+        if (exact) {
+            //checkDithering(image, palette);
+        }
+        
+        MedianCutQuantizer medianCutQuantizer = new MedianCutQuantizer(true);
+        palette = medianCutQuantizer.process(
+                image, limit, new MedianCutMostPopulatedBoxesImplementation(), false);
+        assertEquals(expectedSize, palette.length());
+        checkUniqueColors(image, palette);
+        if (exact) {
+            checkPixelsAreIdentical(image, palette);
+        }
+    }
+    
+    private void checkUniqueColors(BufferedImage src, Palette palette) throws ImageWriteException
{
+        BufferedImage dst = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB);
+        dst.getGraphics().drawImage(src, 0, 0, src.getWidth(), src.getHeight(), null);
+        Dithering.applyFloydSteinbergDithering(dst, palette);
+        Palette ditheredPalette = new PaletteFactory().makeExactRgbPaletteSimple(dst, palette.length()
* 2);
+        assertEquals(palette.length(), ditheredPalette.length());
+    }
+    
+    private void checkPixelsAreIdentical(BufferedImage src, Palette palette) throws ImageWriteException
{
+        BufferedImage dst = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB);
+        dst.getGraphics().drawImage(src, 0, 0, src.getWidth(), src.getHeight(), null);
+        Dithering.applyFloydSteinbergDithering(dst, palette);
+        for (int y = 0; y < src.getHeight(); y++) {
+            for (int x = 0; x < src.getWidth(); x++) {
+                assertEquals(src.getRGB(x, y), dst.getRGB(x, y));
+            }
+        }
+    }
+}

Propchange: commons/proper/imaging/trunk/src/test/java/org/apache/commons/imaging/palette/PaletteQuantizationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message