View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.imaging.formats.png;
19  
20  import java.util.Arrays;
21  
22  public enum PngColorType {
23  
24      // FIXME can this be merged with ImageInfo.ColorType?
25  
26      GREYSCALE(0, true, false, 1, new int[] { 1, 2, 4, 8, 16 }), TRUE_COLOR(2, false, false, 3, new int[] { 8, 16 }),
27      INDEXED_COLOR(3, false, false, 1, new int[] { 1, 2, 4, 8 }), GREYSCALE_WITH_ALPHA(4, true, true, 2, new int[] { 8, 16 }),
28      TRUE_COLOR_WITH_ALPHA(6, false, true, 4, new int[] { 8, 16 });
29  
30      static PngColorType getColorType(final boolean alpha, final boolean grayscale) {
31          if (grayscale) {
32              if (alpha) {
33                  return PngColorType.GREYSCALE_WITH_ALPHA;
34              }
35              return PngColorType.GREYSCALE;
36          }
37          if (alpha) {
38              return PngColorType.TRUE_COLOR_WITH_ALPHA;
39          }
40          return PngColorType.TRUE_COLOR;
41      }
42  
43      public static PngColorType getColorType(final int value) {
44          for (final PngColorType type : values()) {
45              if (type.value == value) {
46                  return type;
47              }
48          }
49  
50          return null;
51      }
52  
53      private final int value;
54      private final boolean greyscale;
55      private final boolean alpha;
56  
57      private final int samplesPerPixel;
58  
59      private final int[] allowedBitDepths;
60  
61      PngColorType(final int value, final boolean greyscale, final boolean alpha, final int samplesPerPixel, final int[] allowedBitDepths) {
62          this.value = value;
63          this.greyscale = greyscale;
64          this.alpha = alpha;
65          this.samplesPerPixel = samplesPerPixel;
66          this.allowedBitDepths = allowedBitDepths;
67      }
68  
69      int getSamplesPerPixel() {
70          return samplesPerPixel;
71      }
72  
73      int getValue() {
74          return value;
75      }
76  
77      boolean hasAlpha() {
78          return alpha;
79      }
80  
81      boolean isBitDepthAllowed(final int bitDepth) {
82          return Arrays.binarySearch(allowedBitDepths, bitDepth) >= 0;
83      }
84  
85      boolean isGreyscale() {
86          return greyscale;
87      }
88  }