1 /*
2  * Copyright (C) 2006 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.graphics;
18 
19 import android.annotation.IntDef;
20 
21 import java.lang.annotation.Retention;
22 import java.lang.annotation.RetentionPolicy;
23 
24 public class PixelFormat {
25     /** @hide */
26     @IntDef({UNKNOWN, TRANSLUCENT, TRANSPARENT, OPAQUE})
27     @Retention(RetentionPolicy.SOURCE)
28     public @interface Opacity {}
29 
30     /** @hide */
31     @Retention(RetentionPolicy.SOURCE)
32     @IntDef({RGBA_8888, RGBX_8888, RGBA_F16, RGBA_1010102, RGB_888, RGB_565})
33     public @interface Format { }
34 
35     // NOTE: these constants must match the values from graphics/common/x.x/types.hal
36 
37     public static final int UNKNOWN      = 0;
38 
39     /** System chooses a format that supports translucency (many alpha bits) */
40     public static final int TRANSLUCENT  = -3;
41 
42     /**
43      * System chooses a format that supports transparency
44      * (at least 1 alpha bit)
45      */
46     public static final int TRANSPARENT  = -2;
47 
48     /** System chooses an opaque format (no alpha bits required) */
49     public static final int OPAQUE       = -1;
50 
51     public static final int RGBA_8888    = 1;
52     public static final int RGBX_8888    = 2;
53     public static final int RGB_888      = 3;
54     public static final int RGB_565      = 4;
55 
56     @Deprecated
57     public static final int RGBA_5551    = 6;
58     @Deprecated
59     public static final int RGBA_4444    = 7;
60     @Deprecated
61     public static final int A_8          = 8;
62     @Deprecated
63     public static final int L_8          = 9;
64     @Deprecated
65     public static final int LA_88        = 0xA;
66     @Deprecated
67     public static final int RGB_332      = 0xB;
68 
69     /**
70      * @deprecated use {@link android.graphics.ImageFormat#NV16
71      * ImageFormat.NV16} instead.
72      */
73     @Deprecated
74     public static final int YCbCr_422_SP = 0x10;
75 
76     /**
77      * @deprecated use {@link android.graphics.ImageFormat#NV21
78      * ImageFormat.NV21} instead.
79      */
80     @Deprecated
81     public static final int YCbCr_420_SP = 0x11;
82 
83     /**
84      * @deprecated use {@link android.graphics.ImageFormat#YUY2
85      * ImageFormat.YUY2} instead.
86      */
87     @Deprecated
88     public static final int YCbCr_422_I  = 0x14;
89 
90     public static final int RGBA_F16     = 0x16;
91     public static final int RGBA_1010102 = 0x2B;
92 
93     /** @hide */
94     public static final int HSV_888 = 0x37;
95 
96     /**
97      * @deprecated use {@link android.graphics.ImageFormat#JPEG
98      * ImageFormat.JPEG} instead.
99      */
100     @Deprecated
101     public static final int JPEG        = 0x100;
102 
103     public int bytesPerPixel;
104     public int bitsPerPixel;
105 
getPixelFormatInfo(@ormat int format, PixelFormat info)106     public static void getPixelFormatInfo(@Format int format, PixelFormat info) {
107         switch (format) {
108             case RGBA_8888:
109             case RGBX_8888:
110             case RGBA_1010102:
111                 info.bitsPerPixel = 32;
112                 info.bytesPerPixel = 4;
113                 break;
114             case RGB_888:
115             case HSV_888:
116                 info.bitsPerPixel = 24;
117                 info.bytesPerPixel = 3;
118                 break;
119             case RGB_565:
120             case RGBA_5551:
121             case RGBA_4444:
122             case LA_88:
123                 info.bitsPerPixel = 16;
124                 info.bytesPerPixel = 2;
125                 break;
126             case A_8:
127             case L_8:
128             case RGB_332:
129                 info.bitsPerPixel = 8;
130                 info.bytesPerPixel = 1;
131                 break;
132             case YCbCr_422_SP:
133             case YCbCr_422_I:
134                 info.bitsPerPixel = 16;
135                 info.bytesPerPixel = 1;
136                 break;
137             case YCbCr_420_SP:
138                 info.bitsPerPixel = 12;
139                 info.bytesPerPixel = 1;
140                 break;
141             case RGBA_F16:
142                 info.bitsPerPixel = 64;
143                 info.bytesPerPixel = 8;
144                 break;
145             default:
146                 throw new IllegalArgumentException("unknown pixel format " + format);
147         }
148     }
149 
formatHasAlpha(@ormat int format)150     public static boolean formatHasAlpha(@Format int format) {
151         switch (format) {
152             case PixelFormat.A_8:
153             case PixelFormat.LA_88:
154             case PixelFormat.RGBA_4444:
155             case PixelFormat.RGBA_5551:
156             case PixelFormat.RGBA_8888:
157             case PixelFormat.RGBA_F16:
158             case PixelFormat.RGBA_1010102:
159             case PixelFormat.TRANSLUCENT:
160             case PixelFormat.TRANSPARENT:
161                 return true;
162         }
163         return false;
164     }
165 
166     /**
167      * Determine whether or not this is a public-visible and non-deprecated {@code format}.
168      *
169      * <p>In particular, {@code @hide} formats will return {@code false}.</p>
170      *
171      * <p>Any other indirect formats (such as {@code TRANSPARENT} or {@code TRANSLUCENT})
172      * will return {@code false}.</p>
173      *
174      * @param format an integer format
175      * @return a boolean
176      *
177      * @hide
178      */
isPublicFormat(@ormat int format)179     public static boolean isPublicFormat(@Format int format) {
180         switch (format) {
181             case RGBA_8888:
182             case RGBX_8888:
183             case RGB_888:
184             case RGB_565:
185             case RGBA_F16:
186             case RGBA_1010102:
187                 return true;
188         }
189 
190         return false;
191     }
192 
193     /**
194      * @hide
195      */
formatToString(@ormat int format)196     public static String formatToString(@Format int format) {
197         switch (format) {
198             case UNKNOWN:
199                 return "UNKNOWN";
200             case TRANSLUCENT:
201                 return "TRANSLUCENT";
202             case TRANSPARENT:
203                 return "TRANSPARENT";
204             case RGBA_8888:
205                 return "RGBA_8888";
206             case RGBX_8888:
207                 return "RGBX_8888";
208             case RGB_888:
209                 return "RGB_888";
210             case RGB_565:
211                 return "RGB_565";
212             case RGBA_5551:
213                 return "RGBA_5551";
214             case RGBA_4444:
215                 return "RGBA_4444";
216             case A_8:
217                 return "A_8";
218             case L_8:
219                 return "L_8";
220             case LA_88:
221                 return "LA_88";
222             case RGB_332:
223                 return "RGB_332";
224             case YCbCr_422_SP:
225                 return "YCbCr_422_SP";
226             case YCbCr_420_SP:
227                 return "YCbCr_420_SP";
228             case YCbCr_422_I:
229                 return "YCbCr_422_I";
230             case RGBA_F16:
231                 return "RGBA_F16";
232             case RGBA_1010102:
233                 return "RGBA_1010102";
234             case HSV_888:
235                 return "HSV_888";
236             case JPEG:
237                 return "JPEG";
238             default:
239                 return Integer.toString(format);
240         }
241     }
242 }
243