    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.imaging.formats.png;
    019
    020import java.util.Arrays;
    021
    022public enum PngColorType {
    023
    024    // FIXME can this be merged with ImageInfo.ColorType?
    025
    026    GREYSCALE(0, true, false, 1, new int[]{1, 2, 4, 8, 16}),
    027    TRUE_COLOR(2, false, false, 3, new int[]{8, 16}),
    028    INDEXED_COLOR(3, false, false, 1, new int[]{1, 2, 4, 8}),
    029    GREYSCALE_WITH_ALPHA(4, true, true, 2, new int[]{8, 16}),
    030    TRUE_COLOR_WITH_ALPHA(6, false, true, 4, new int[]{8, 16});
    031
    032    private final int value;
    033    private final boolean greyscale;
    034    private final boolean alpha;
    035    private final int samplesPerPixel;
    036    private final int[] allowedBitDepths;
    037
    038    PngColorType(final int value, final boolean greyscale, final boolean alpha, final int samplesPerPixel, final int[] allowedBitDepths) {
    039        this.value = value;
    040        this.greyscale = greyscale;
    041        this.alpha = alpha;
    042        this.samplesPerPixel = samplesPerPixel;
    043        this.allowedBitDepths = allowedBitDepths;
    044    }
    045
    046    int getValue() {
    047        return value;
    048    }
    049
    050    boolean isGreyscale() {
    051        return greyscale;
    052    }
    053
    054    boolean hasAlpha() {
    055        return alpha;
    056    }
    057
    058    int getSamplesPerPixel() {
    059        return samplesPerPixel;
    060    }
    061
    062    boolean isBitDepthAllowed(final int bitDepth) {
    063        return Arrays.binarySearch(allowedBitDepths, bitDepth) >= 0;
    064    }
    065
    066    public static PngColorType getColorType(final int value) {
    067        for (final PngColorType type : values()) {
    068            if (type.value == value) {
    069                return type;
    070            }
    071        }
    072
    073        return null;
    074    }
    075
    076    static PngColorType getColorType(final boolean alpha, final boolean grayscale) {
    077        if (grayscale) {
    078            if (alpha) {
    079                return PngColorType.GREYSCALE_WITH_ALPHA;
    080            } else {
    081                return PngColorType.GREYSCALE;
    082            }
    083        } else if (alpha) {
    084            return PngColorType.TRUE_COLOR_WITH_ALPHA;
    085        } else {
    086            return PngColorType.TRUE_COLOR;
    087        }
    088    }
    089}