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}),
27      TRUE_COLOR(2, false, false, 3, new int[]{8, 16}),
28      INDEXED_COLOR(3, false, false, 1, new int[]{1, 2, 4, 8}),
29      GREYSCALE_WITH_ALPHA(4, true, true, 2, new int[]{8, 16}),
30      TRUE_COLOR_WITH_ALPHA(6, false, true, 4, new int[]{8, 16});
31  
32      private final int value;
33      private final boolean greyscale;
34      private final boolean alpha;
35      private final int samplesPerPixel;
36      private final int[] allowedBitDepths;
37  
38      PngColorType(final int value, final boolean greyscale, final boolean alpha, final int samplesPerPixel, final int[] allowedBitDepths) {
39          this.value = value;
40          this.greyscale = greyscale;
41          this.alpha = alpha;
42          this.samplesPerPixel = samplesPerPixel;
43          this.allowedBitDepths = allowedBitDepths;
44      }
45  
46      int getValue() {
47          return value;
48      }
49  
50      boolean isGreyscale() {
51          return greyscale;
52      }
53  
54      boolean hasAlpha() {
55          return alpha;
56      }
57  
58      int getSamplesPerPixel() {
59          return samplesPerPixel;
60      }
61  
62      boolean isBitDepthAllowed(final int bitDepth) {
63          return Arrays.binarySearch(allowedBitDepths, bitDepth) >= 0;
64      }
65  
66      public static PngColorType getColorType(final int value) {
67          for (final PngColorType type : values()) {
68              if (type.value == value) {
69                  return type;
70              }
71          }
72          
73          return null;
74      }
75  
76      static PngColorType getColorType(final boolean alpha, final boolean grayscale) {
77          if (grayscale) {
78              if (alpha) {
79                  return PngColorType.GREYSCALE_WITH_ALPHA;
80              } else {
81                  return PngColorType.GREYSCALE;
82              }
83          } else if (alpha) {
84              return PngColorType.TRUE_COLOR_WITH_ALPHA;
85          } else {
86              return PngColorType.TRUE_COLOR;
87          }
88      }
89  }