1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.imaging.formats.png;
19
20 import java.util.Arrays;
21
22 public enum PngColorType {
23
24
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 }