You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by eb...@apache.org on 2013/11/28 15:00:24 UTC

svn commit: r1546364 - in /commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png: ./ chunks/

Author: ebourg
Date: Thu Nov 28 14:00:24 2013
New Revision: 1546364

URL: http://svn.apache.org/r1546364
Log:
Added an enum for the PNG color types

Added:
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/ColorType.java   (with props)
Modified:
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngConstants.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngImageParser.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngWriter.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/ScanExpediter.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/ScanExpediterInterlaced.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/ScanExpediterSimple.java
    commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkIhdr.java

Added: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/ColorType.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/ColorType.java?rev=1546364&view=auto
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/ColorType.java (added)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/ColorType.java Thu Nov 28 14:00:24 2013
@@ -0,0 +1,87 @@
+/*
+ * 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.png;
+
+import java.util.Arrays;
+
+public enum ColorType {
+
+    GREYSCALE(0, true, false, 1, new int[]{1, 2, 4, 8, 16}),
+    TRUE_COLOR(2, false, false, 3, new int[]{8, 16}),
+    INDEXED_COLOR(3, false, false, 1, new int[]{1, 2, 4, 8}),
+    GREYSCALE_WITH_ALPHA(4, true, true, 2, new int[]{8, 16}),
+    TRUE_COLOR_WITH_ALPHA(6, false, true, 4, new int[]{8, 16});
+
+    private final int value;
+    private final boolean greyscale;
+    private final boolean alpha;
+    private final int samplesPerPixel;
+    private final int[] allowedBitDepths;
+
+    ColorType(int value, boolean greyscale, boolean alpha, int samplesPerPixel, int[] allowedBitDepths) {
+        this.value = value;
+        this.greyscale = greyscale;
+        this.alpha = alpha;
+        this.samplesPerPixel = samplesPerPixel;
+        this.allowedBitDepths = allowedBitDepths;
+    }
+
+    int getValue() {
+        return value;
+    }
+
+    boolean isGreyscale() {
+        return greyscale;
+    }
+
+    boolean hasAlpha() {
+        return alpha;
+    }
+
+    int getSamplesPerPixel() {
+        return samplesPerPixel;
+    }
+
+    boolean isBitDepthAllowed(int bitDepth) {
+        return Arrays.binarySearch(allowedBitDepths, bitDepth) >= 0;
+    }
+
+    public static ColorType getColorType(int value) {
+        for (ColorType type : values()) {
+            if (type.value == value) {
+                return type;
+            }
+        }
+        
+        return null;
+    }
+
+    static ColorType getColorType(boolean alpha, boolean grayscale) {
+        if (grayscale) {
+            if (alpha) {
+                return ColorType.GREYSCALE_WITH_ALPHA;
+            } else {
+                return ColorType.GREYSCALE;
+            }
+        } else if (alpha) {
+            return ColorType.TRUE_COLOR_WITH_ALPHA;
+        } else {
+            return ColorType.TRUE_COLOR;
+        }
+    }
+}

Propchange: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/ColorType.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/ColorType.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngConstants.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngConstants.java?rev=1546364&r1=1546363&r2=1546364&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngConstants.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngConstants.java Thu Nov 28 14:00:24 2013
@@ -32,12 +32,6 @@ public interface PngConstants {
     // public static final Object PARAM_KEY_PNG_BIT_DEPTH_YES = "YES";
     // public static final Object PARAM_KEY_PNG_BIT_DEPTH_NO = "NO";
 
-    public static final int COLOR_TYPE_GREYSCALE = 0;
-    public static final int COLOR_TYPE_TRUE_COLOR = 2;
-    public static final int COLOR_TYPE_INDEXED_COLOR = 3;
-    public static final int COLOR_TYPE_GREYSCALE_WITH_ALPHA = 4;
-    public static final int COLOR_TYPE_TRUE_COLOR_WITH_ALPHA = 6;
-
     public static final byte COMPRESSION_TYPE_INFLATE_DEFLATE = 0;
     public static final byte FILTER_METHOD_ADAPTIVE = 0;
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngImageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngImageParser.java?rev=1546364&r1=1546363&r2=1546364&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngImageParser.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngImageParser.java Thu Nov 28 14:00:24 2013
@@ -299,98 +299,6 @@ public class PngImageParser extends Imag
         return result;
     }
 
-    private boolean isGrayscale(final int colorType) throws ImageReadException {
-        // Color type is a single-byte integer that describes the interpretation
-        // of the
-        // image data. Color type codes represent sums of the following values:
-        // 1 (palette used), 2 (color used), and 4 (alpha channel used).
-        // Valid values are 0, 2, 3, 4, and 6.
-        //
-        // Bit depth restrictions for each color type are imposed to simplify
-        // implementations
-        // and to prohibit combinations that do not compress well. Decoders must
-        // support all
-        // valid combinations of bit depth and color type. The allowed
-        // combinations are:
-        //
-        // Color Allowed Interpretation
-        // Type Bit Depths
-        //
-        // 0 1,2,4,8,16 Each pixel is a grayscale sample.
-        //
-        // 2 8,16 Each pixel is an R,G,B triple.
-        //
-        // 3 1,2,4,8 Each pixel is a palette index;
-        // a PLTE chunk must appear.
-        //
-        // 4 8,16 Each pixel is a grayscale sample,
-        // followed by an alpha sample.
-        //
-        // 6 8,16 Each pixel is an R,G,B triple,
-        // followed by an alpha sample.
-        switch (colorType) {
-        case PngConstants.COLOR_TYPE_GREYSCALE:
-            return true;
-        case PngConstants.COLOR_TYPE_TRUE_COLOR:
-            return false;
-        case PngConstants.COLOR_TYPE_INDEXED_COLOR:
-            return false;
-        case PngConstants.COLOR_TYPE_GREYSCALE_WITH_ALPHA:
-            return true;
-        case PngConstants.COLOR_TYPE_TRUE_COLOR_WITH_ALPHA:
-            return false;
-        }
-
-        // return -1;
-        throw new ImageReadException("PNG: unknown color type: " + colorType);
-    }
-
-    private int samplesPerPixel(final int colorType) throws ImageReadException {
-        // Color type is a single-byte integer that describes the interpretation
-        // of the
-        // image data. Color type codes represent sums of the following values:
-        // 1 (palette used), 2 (color used), and 4 (alpha channel used).
-        // Valid values are 0, 2, 3, 4, and 6.
-        //
-        // Bit depth restrictions for each color type are imposed to simplify
-        // implementations
-        // and to prohibit combinations that do not compress well. Decoders must
-        // support all
-        // valid combinations of bit depth and color type. The allowed
-        // combinations are:
-        //
-        // Color Allowed Interpretation
-        // Type Bit Depths
-        //
-        // 0 1,2,4,8,16 Each pixel is a grayscale sample.
-        //
-        // 2 8,16 Each pixel is an R,G,B triple.
-        //
-        // 3 1,2,4,8 Each pixel is a palette index;
-        // a PLTE chunk must appear.
-        //
-        // 4 8,16 Each pixel is a grayscale sample,
-        // followed by an alpha sample.
-        //
-        // 6 8,16 Each pixel is an R,G,B triple,
-        // followed by an alpha sample.
-        switch (colorType) {
-        case PngConstants.COLOR_TYPE_GREYSCALE:
-            return 1;
-        case PngConstants.COLOR_TYPE_TRUE_COLOR:
-            return 3;
-        case PngConstants.COLOR_TYPE_INDEXED_COLOR:
-            return 1; // is this accurate ? how may bits per index?
-        case PngConstants.COLOR_TYPE_GREYSCALE_WITH_ALPHA:
-            return 2;
-        case PngConstants.COLOR_TYPE_TRUE_COLOR_WITH_ALPHA:
-            return 4;
-        }
-
-        // return -1;
-        throw new ImageReadException("PNG: unknown color type: " + colorType);
-    }
-
     private List<PngChunk> filterChunks(final List<PngChunk> chunks, final ChunkType type) {
         final List<PngChunk> result = new ArrayList<PngChunk>();
 
@@ -403,65 +311,21 @@ public class PngImageParser extends Imag
         return result;
     }
 
-    private boolean hasAlphaChannel(final int colorType) throws ImageReadException {
-        switch (colorType) {
-            case PngConstants.COLOR_TYPE_GREYSCALE: // 1,2,4,8,16 Each pixel is a grayscale
-                // sample.
-            case PngConstants.COLOR_TYPE_TRUE_COLOR: // 8,16 Each pixel is an R,G,B triple.
-            case PngConstants.COLOR_TYPE_INDEXED_COLOR: // 1,2,4,8 Each pixel is a palette index;
-                return false;
-            case PngConstants.COLOR_TYPE_GREYSCALE_WITH_ALPHA: // 8,16 Each pixel is a grayscale
-                // sample,
-                // followed by an alpha sample.
-            case PngConstants.COLOR_TYPE_TRUE_COLOR_WITH_ALPHA: // 8,16 Each pixel is an R,G,B
-                // triple,
-                // followed by an alpha sample.
-                return true;
-            default:
-                throw new ImageReadException("PNG: unknown color type: " + colorType);
-        }
-    }
-
-    private String getColorTypeDescription(final int colorType) {
-        switch (colorType) {
-            case PngConstants.COLOR_TYPE_GREYSCALE: // 1,2,4,8,16 Each pixel is a grayscale
-                // sample.
-                return "grayscale";
-            case PngConstants.COLOR_TYPE_TRUE_COLOR: // 8,16 Each pixel is an R,G,B triple.
-                return "rgb";
-            case PngConstants.COLOR_TYPE_INDEXED_COLOR: // 1,2,4,8 Each pixel is a palette index;
-                return "indexed rgb";
-            case PngConstants.COLOR_TYPE_GREYSCALE_WITH_ALPHA: // 8,16 Each pixel is a grayscale
-                // sample,
-                // followed by an alpha sample.
-                return "grayscale w/ alpha";
-            case PngConstants.COLOR_TYPE_TRUE_COLOR_WITH_ALPHA: // 8,16 Each pixel is an R,G,B
-                // triple,
-                // followed by an alpha sample.
-                return "RGB w/ alpha";
-            default:
-                return "Unknown Color Type";
-        }
-    }
-
     // TODO: I have been too casual about making inner classes subclass of
     // BinaryFileParser
     // I may not have always preserved byte order correctly.
 
-    private TransparencyFilter getTransparencyFilter(int colorType,
-                                                     PngChunk pngChunktRNS) throws ImageReadException, IOException {
+    private TransparencyFilter getTransparencyFilter(ColorType colorType, PngChunk pngChunktRNS)
+            throws ImageReadException, IOException {
         switch (colorType) {
-            case PngConstants.COLOR_TYPE_GREYSCALE: // 1,2,4,8,16 Each pixel is a grayscale
-                // sample.
+            case GREYSCALE: // 1,2,4,8,16 Each pixel is a grayscale sample.
                 return new TransparencyFilterGrayscale(pngChunktRNS.getBytes());
-            case PngConstants.COLOR_TYPE_TRUE_COLOR: // 8,16 Each pixel is an R,G,B triple.
+            case TRUE_COLOR: // 8,16 Each pixel is an R,G,B triple.
                 return new TransparencyFilterTrueColor(pngChunktRNS.getBytes());
-            case PngConstants.COLOR_TYPE_INDEXED_COLOR: // 1,2,4,8 Each pixel is a palette index;
+            case INDEXED_COLOR: // 1,2,4,8 Each pixel is a palette index;
                 return new TransparencyFilterIndexedColor(pngChunktRNS.getBytes());
-            case PngConstants.COLOR_TYPE_GREYSCALE_WITH_ALPHA: // 8,16 Each pixel is a grayscale
-                // sample,
-            case PngConstants.COLOR_TYPE_TRUE_COLOR_WITH_ALPHA: // 8,16 Each pixel is an R,G,B
-                // triple,
+            case GREYSCALE_WITH_ALPHA: // 8,16 Each pixel is a grayscale sample,
+            case TRUE_COLOR_WITH_ALPHA: // 8,16 Each pixel is an R,G,B triple,
             default:
                 throw new ImageReadException("Simple Transparency not compatible with ColorType: " + colorType);
         }
@@ -501,7 +365,7 @@ public class PngImageParser extends Imag
             transparent = true;
         } else {
             // CE - Fix Alpha.
-            transparent = hasAlphaChannel(pngChunkIHDR.colorType);
+            transparent = pngChunkIHDR.colorType.hasAlpha();
             // END FIX
         }
 
@@ -538,7 +402,7 @@ public class PngImageParser extends Imag
             textChunks.add(pngChunkiTXt.getContents());
         }
 
-        final int bitsPerPixel = pngChunkIHDR.bitDepth * samplesPerPixel(pngChunkIHDR.colorType);
+        final int bitsPerPixel = pngChunkIHDR.bitDepth * pngChunkIHDR.colorType.getSamplesPerPixel();
         final ImageFormat format = ImageFormats.PNG;
         final String formatName = "PNG Portable Network Graphics";
         final int height = pngChunkIHDR.height;
@@ -581,19 +445,13 @@ public class PngImageParser extends Imag
 
         int colorType;
         switch (pngChunkIHDR.colorType) {
-            case PngConstants.COLOR_TYPE_GREYSCALE: // 1,2,4,8,16 Each pixel is a grayscale
-                // sample.
-            case PngConstants.COLOR_TYPE_GREYSCALE_WITH_ALPHA: // 8,16 Each pixel is a
-                // grayscale sample,
-                // followed by an alpha sample.
+            case GREYSCALE:
+            case GREYSCALE_WITH_ALPHA:
                 colorType = ImageInfo.COLOR_TYPE_GRAYSCALE;
                 break;
-            case PngConstants.COLOR_TYPE_TRUE_COLOR: // 8,16 Each pixel is an R,G,B triple.
-            case PngConstants.COLOR_TYPE_INDEXED_COLOR: // 1,2,4,8 Each pixel is a palette
-                // index;
-            case PngConstants.COLOR_TYPE_TRUE_COLOR_WITH_ALPHA: // 8,16 Each pixel is an
-                // R,G,B triple,
-                // followed by an alpha sample.
+            case TRUE_COLOR:
+            case INDEXED_COLOR:
+            case TRUE_COLOR_WITH_ALPHA:
                 colorType = ImageInfo.COLOR_TYPE_RGB;
                 break;
             default:
@@ -677,8 +535,7 @@ public class PngImageParser extends Imag
         final List<PngChunk> tRNSs = filterChunks(chunks, ChunkType.tRNS);
         if (!tRNSs.isEmpty()) {
             final PngChunk pngChunktRNS = tRNSs.get(0);
-            transparencyFilter = getTransparencyFilter(pngChunkIHDR.colorType,
-                    pngChunktRNS);
+            transparencyFilter = getTransparencyFilter(pngChunkIHDR.colorType, pngChunktRNS);
         }
 
         ICC_Profile iccProfile = null;
@@ -735,32 +592,22 @@ public class PngImageParser extends Imag
         {
             final int width = pngChunkIHDR.width;
             final int height = pngChunkIHDR.height;
-            final int colorType = pngChunkIHDR.colorType;
+            final ColorType colorType = pngChunkIHDR.colorType;
             final int bitDepth = pngChunkIHDR.bitDepth;
 
-            final int bitsPerSample = bitDepth;
-
             if (pngChunkIHDR.filterMethod != 0) {
-                throw new ImageReadException("PNG: unknown FilterMethod: "
-                        + pngChunkIHDR.filterMethod);
+                throw new ImageReadException("PNG: unknown FilterMethod: " + pngChunkIHDR.filterMethod);
             }
 
-            final int samplesPerPixel = samplesPerPixel(pngChunkIHDR.colorType);
-            final boolean isGrayscale = isGrayscale(pngChunkIHDR.colorType);
-
-            final int bitsPerPixel = bitsPerSample * samplesPerPixel;
+            final int bitsPerPixel = bitDepth * colorType.getSamplesPerPixel();
 
-            final boolean hasAlpha = colorType == PngConstants.COLOR_TYPE_GREYSCALE_WITH_ALPHA
-                    || colorType == PngConstants.COLOR_TYPE_TRUE_COLOR_WITH_ALPHA
-                    || transparencyFilter != null;
+            final boolean hasAlpha = colorType.hasAlpha() || transparencyFilter != null;
 
             BufferedImage result;
-            if (isGrayscale) {
-                result = getBufferedImageFactory(params)
-                        .getGrayscaleBufferedImage(width, height, hasAlpha);
+            if (colorType.isGreyscale()) {
+                result = getBufferedImageFactory(params).getGrayscaleBufferedImage(width, height, hasAlpha);
             } else {
-                result = getBufferedImageFactory(params).getColorBufferedImage(
-                        width, height, hasAlpha);
+                result = getBufferedImageFactory(params).getColorBufferedImage(width, height, hasAlpha);
             }
 
             final ByteArrayInputStream bais = new ByteArrayInputStream(compressed);
@@ -821,8 +668,7 @@ public class PngImageParser extends Imag
             return false;
         }
         final PngChunkIhdr pngChunkIHDR = (PngChunkIhdr) IHDRs.get(0);
-        pw.println("Color: "
-                + getColorTypeDescription(pngChunkIHDR.colorType));
+        pw.println("Color: " + pngChunkIHDR.colorType.name());
 
         pw.println("chunks: " + chunks.size());
 
@@ -848,16 +694,6 @@ public class PngImageParser extends Imag
         new PngWriter(params).writeImage(src, os, params);
     }
 
-    /**
-     * Extracts embedded XML metadata as XML string.
-     * <p>
-     * 
-     * @param byteSource
-     *            File containing image data.
-     * @param params
-     *            Map of optional parameters, defined in ImagingConstants.
-     * @return Xmp Xml as String, if present. Otherwise, returns null.
-     */
     @Override
     public String getXmpXml(final ByteSource byteSource, final Map<String, Object> params)
             throws ImageReadException, IOException {

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngWriter.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngWriter.java?rev=1546364&r1=1546363&r2=1546364&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngWriter.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/PngWriter.java Thu Nov 28 14:00:24 2013
@@ -91,13 +91,13 @@ class PngWriter {
         public final int width;
         public final int height;
         public final byte bitDepth;
-        public final byte colorType;
+        public final ColorType colorType;
         public final byte compressionMethod;
         public final byte filterMethod;
         public final byte interlaceMethod;
 
         public ImageHeader(final int width, final int height, final byte bitDepth,
-                final byte colorType, final byte compressionMethod, final byte filterMethod,
+                final ColorType colorType, final byte compressionMethod, final byte filterMethod,
                 final byte interlaceMethod) {
             this.width = width;
             this.height = height;
@@ -110,19 +110,16 @@ class PngWriter {
 
     }
 
-    private void writeChunkIHDR(final OutputStream os, final ImageHeader value)
-            throws IOException {
+    private void writeChunkIHDR(final OutputStream os, final ImageHeader value) throws IOException {
         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
         writeInt(baos, value.width);
         writeInt(baos, value.height);
         baos.write(0xff & value.bitDepth);
-        baos.write(0xff & value.colorType);
+        baos.write(0xff & value.colorType.getValue());
         baos.write(0xff & value.compressionMethod);
         baos.write(0xff & value.filterMethod);
         baos.write(0xff & value.interlaceMethod);
 
-        // Debug.debug("baos", baos.toByteArray());
-
         writeChunk(os, ChunkType.IHDR, baos.toByteArray());
     }
 
@@ -300,62 +297,15 @@ class PngWriter {
         writeChunk(os, ChunkType.pHYs, bytes);
     }
 
-    private byte getColourType(final boolean hasAlpha, final boolean isGrayscale) {
-        byte result;
+    private byte getBitDepth(final ColorType colorType, final Map<String, Object> params) {
+        byte depth = 8;
 
-        final boolean index = false; // charles
-
-        if (index) {
-            result = PngConstants.COLOR_TYPE_INDEXED_COLOR;
-        } else if (isGrayscale) {
-            if (hasAlpha) {
-                result = PngConstants.COLOR_TYPE_GREYSCALE_WITH_ALPHA;
-            } else {
-                result = PngConstants.COLOR_TYPE_GREYSCALE;
-            }
-        } else if (hasAlpha) {
-            result = PngConstants.COLOR_TYPE_TRUE_COLOR_WITH_ALPHA;
-        } else {
-            result = PngConstants.COLOR_TYPE_TRUE_COLOR;
-        }
-
-        return result;
-    }
-
-    private byte getBitDepth(final byte colorType, final Map<String, Object> params) {
-        byte result = 8;
-
-        final Object o = params.get(PngConstants.PARAM_KEY_PNG_BIT_DEPTH);
+        Object o = params.get(PngConstants.PARAM_KEY_PNG_BIT_DEPTH);
         if (o instanceof Number) {
-            final int value = ((Number) o).intValue();
-            switch (value) {
-            case 1:
-            case 2:
-            case 4:
-            case 8:
-            case 16:
-                result = (byte) value;
-                break;
-            default:
-                result = 8;
-            }
-            switch (colorType) {
-            case PngConstants.COLOR_TYPE_GREYSCALE:
-                break;
-            case PngConstants.COLOR_TYPE_INDEXED_COLOR:
-                result = (byte) Math.min(8, result);
-                break;
-            case PngConstants.COLOR_TYPE_GREYSCALE_WITH_ALPHA:
-            case PngConstants.COLOR_TYPE_TRUE_COLOR:
-            case PngConstants.COLOR_TYPE_TRUE_COLOR_WITH_ALPHA:
-                result = (byte) Math.max(8, result);
-                break;
-            default:
-                result = 8;
-            }
+            depth = ((Number) o).byteValue();
         }
 
-        return result;
+        return colorType.isBitDepthAllowed(depth) ? depth : 8;
     }
 
     /// Wraps a palette by adding a single transparent entry at index 0.
@@ -470,7 +420,7 @@ class PngWriter {
             Debug.debug("isGrayscale: " + isGrayscale);
         }
 
-        byte colorType;
+        ColorType colorType;
         {
             final boolean forceIndexedColor =  Boolean.TRUE.equals(params.get(PngConstants.PARAM_KEY_PNG_FORCE_INDEXED_COLOR));
             final boolean forceTrueColor = Boolean.TRUE.equals(params.get(PngConstants.PARAM_KEY_PNG_FORCE_TRUE_COLOR));
@@ -479,13 +429,12 @@ class PngWriter {
                 throw new ImageWriteException(
                         "Params: Cannot force both indexed and true color modes");
             } else if (forceIndexedColor) {
-                colorType = PngConstants.COLOR_TYPE_INDEXED_COLOR;
+                colorType = ColorType.INDEXED_COLOR;
             } else if (forceTrueColor) {
-                colorType = (byte) (hasAlpha ? PngConstants.COLOR_TYPE_TRUE_COLOR_WITH_ALPHA
-                        : PngConstants.COLOR_TYPE_TRUE_COLOR);
+                colorType = (hasAlpha ? ColorType.TRUE_COLOR_WITH_ALPHA : ColorType.TRUE_COLOR);
                 isGrayscale = false;
             } else {
-                colorType = getColourType(hasAlpha, isGrayscale);
+                colorType = ColorType.getColorType(hasAlpha, isGrayscale);
             }
             if (verbose) {
                 Debug.debug("colorType: " + colorType);
@@ -498,7 +447,7 @@ class PngWriter {
         }
 
         int sampleDepth;
-        if (colorType == PngConstants.COLOR_TYPE_INDEXED_COLOR) {
+        if (colorType == ColorType.INDEXED_COLOR) {
             sampleDepth = 8;
         } else {
             sampleDepth = bitDepth;
@@ -531,7 +480,7 @@ class PngWriter {
         //}
 
         Palette palette = null;
-        if (colorType == PngConstants.COLOR_TYPE_INDEXED_COLOR) {
+        if (colorType == ColorType.INDEXED_COLOR) {
             // PLTE No Before first IDAT
 
             final int maxColors = hasAlpha ? 255 : 256;
@@ -599,8 +548,8 @@ class PngWriter {
             {
                 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
-                final boolean useAlpha = colorType == PngConstants.COLOR_TYPE_GREYSCALE_WITH_ALPHA
-                        || colorType == PngConstants.COLOR_TYPE_TRUE_COLOR_WITH_ALPHA;
+                final boolean useAlpha = colorType == ColorType.GREYSCALE_WITH_ALPHA
+                        || colorType == ColorType.TRUE_COLOR_WITH_ALPHA;
 
                 final int[] row = new int[width];
                 for (int y = 0; y < height; y++) {

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/ScanExpediter.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/ScanExpediter.java?rev=1546364&r1=1546363&r2=1546364&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/ScanExpediter.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/ScanExpediter.java Thu Nov 28 14:00:24 2013
@@ -37,7 +37,7 @@ abstract class ScanExpediter {
     protected final int height;
     protected final InputStream is;
     protected final BufferedImage bi;
-    protected final int colorType;
+    protected final ColorType colorType;
     protected final int bitDepth;
     protected final int bytesPerPixel;
     protected final int bitsPerPixel;
@@ -46,7 +46,7 @@ abstract class ScanExpediter {
     protected final TransparencyFilter transparencyFilter;
 
     public ScanExpediter(final int width, final int height, final InputStream is,
-            final BufferedImage bi, final int colorType, final int bitDepth, final int bitsPerPixel,
+            final BufferedImage bi, final ColorType colorType, final int bitDepth, final int bitsPerPixel,
             final PngChunkPlte pngChunkPLTE, final GammaCorrection gammaCorrection,
             final TransparencyFilter transparencyFilter)
 
@@ -81,11 +81,11 @@ abstract class ScanExpediter {
 
     public abstract void drive() throws ImageReadException, IOException;
 
-    protected int getRGB(final BitParser bitParser, final int pixelIndexInScanline)
+    protected int getRGB(final BitParser bitParser, final int pixelIndexInScanline) 
             throws ImageReadException, IOException {
 
         switch (colorType) {
-        case 0: {
+        case GREYSCALE: {
             // 1,2,4,8,16 Each pixel is a grayscale sample.
             int sample = bitParser.getSampleAsByte(pixelIndexInScanline, 0);
 
@@ -102,7 +102,7 @@ abstract class ScanExpediter {
             return rgb;
 
         }
-        case 2: {
+        case TRUE_COLOR: {
             // 8,16 Each pixel is an R,G,B triple.
             int red = bitParser.getSampleAsByte(pixelIndexInScanline, 0);
             int green = bitParser.getSampleAsByte(pixelIndexInScanline, 1);
@@ -126,7 +126,7 @@ abstract class ScanExpediter {
             return rgb;
         }
         //
-        case 3: {
+        case INDEXED_COLOR: {
             // 1,2,4,8 Each pixel is a palette index;
             // a PLTE chunk must appear.
             final int index = bitParser.getSample(pixelIndexInScanline, 0);
@@ -139,7 +139,7 @@ abstract class ScanExpediter {
 
             return rgb;
         }
-        case 4: {
+        case GREYSCALE_WITH_ALPHA: {
             // 8,16 Each pixel is a grayscale sample,
             // followed by an alpha sample.
             int sample = bitParser.getSampleAsByte(pixelIndexInScanline, 0);
@@ -151,7 +151,7 @@ abstract class ScanExpediter {
 
             return getPixelARGB(alpha, sample, sample, sample);
         }
-        case 6: {
+        case TRUE_COLOR_WITH_ALPHA: {
             // 8,16 Each pixel is an R,G,B triple,
             int red = bitParser.getSampleAsByte(pixelIndexInScanline, 0);
             int green = bitParser.getSampleAsByte(pixelIndexInScanline, 1);

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/ScanExpediterInterlaced.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/ScanExpediterInterlaced.java?rev=1546364&r1=1546363&r2=1546364&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/ScanExpediterInterlaced.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/ScanExpediterInterlaced.java Thu Nov 28 14:00:24 2013
@@ -34,7 +34,7 @@ class ScanExpediterInterlaced extends Sc
 
     public ScanExpediterInterlaced(int width, int height, InputStream is,
             BufferedImage bi,
-            int colorType, int bitDepth, int bitsPerPixel,
+            ColorType colorType, int bitDepth, int bitsPerPixel,
             PngChunkPlte fPNGChunkPLTE,
             GammaCorrection gammaCorrection,
             TransparencyFilter transparencyFilter)

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/ScanExpediterSimple.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/ScanExpediterSimple.java?rev=1546364&r1=1546363&r2=1546364&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/ScanExpediterSimple.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/ScanExpediterSimple.java Thu Nov 28 14:00:24 2013
@@ -26,7 +26,7 @@ import org.apache.commons.imaging.format
 
 class ScanExpediterSimple extends ScanExpediter {
     public ScanExpediterSimple(final int width, final int height, final InputStream is,
-            final BufferedImage bi, final int colorType, final int bitDepth, final int bitsPerPixel,
+            final BufferedImage bi, final ColorType colorType, final int bitDepth, final int bitsPerPixel,
             final PngChunkPlte pngChunkPLTE, final GammaCorrection gammaCorrection,
             final TransparencyFilter transparencyFilter)
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkIhdr.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkIhdr.java?rev=1546364&r1=1546363&r2=1546364&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkIhdr.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkIhdr.java Thu Nov 28 14:00:24 2013
@@ -19,25 +19,32 @@ package org.apache.commons.imaging.forma
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 
+import org.apache.commons.imaging.ImageReadException;
+import org.apache.commons.imaging.formats.png.ColorType;
+
 import static org.apache.commons.imaging.common.BinaryFunctions.*;
 
 public class PngChunkIhdr extends PngChunk {
     public final int width;
     public final int height;
     public final int bitDepth;
-    public final int colorType;
+    public final ColorType colorType;
     public final int compressionMethod;
     public final int filterMethod;
     public final int interlaceMethod;
 
-    public PngChunkIhdr(int length, int chunkType, int crc, byte[] bytes) throws IOException {
+    public PngChunkIhdr(int length, int chunkType, int crc, byte[] bytes) throws ImageReadException, IOException {
         super(length, chunkType, crc, bytes);
 
         final ByteArrayInputStream is = new ByteArrayInputStream(bytes);
         width = read4Bytes("Width", is, "Not a Valid Png File: IHDR Corrupt", getByteOrder());
         height = read4Bytes("Height", is, "Not a Valid Png File: IHDR Corrupt", getByteOrder());
         bitDepth = readByte("BitDepth", is, "Not a Valid Png File: IHDR Corrupt");
-        colorType = readByte("ColorType", is, "Not a Valid Png File: IHDR Corrupt");
+        int type = readByte("ColorType", is, "Not a Valid Png File: IHDR Corrupt");
+        colorType = ColorType.getColorType(type);
+        if (colorType == null) {
+            throw new ImageReadException("PNG: unknown color type: " + type);
+        }
         compressionMethod = readByte("CompressionMethod", is, "Not a Valid Png File: IHDR Corrupt");
         filterMethod = readByte("FilterMethod", is, "Not a Valid Png File: IHDR Corrupt");
         interlaceMethod = readByte("InterlaceMethod", is, "Not a Valid Png File: IHDR Corrupt");