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, R_8})
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     /** @hide */
97     public static final int R_8 = 0x38;
98 
99     /**
100      * @deprecated use {@link android.graphics.ImageFormat#JPEG
101      * ImageFormat.JPEG} instead.
102      */
103     @Deprecated
104     public static final int JPEG        = 0x100;
105 
106     public int bytesPerPixel;
107     public int bitsPerPixel;
108 
getPixelFormatInfo(@ormat int format, PixelFormat info)109     public static void getPixelFormatInfo(@Format int format, PixelFormat info) {
110         switch (format) {
111             case RGBA_8888:
112             case RGBX_8888:
113             case RGBA_1010102:
114                 info.bitsPerPixel = 32;
115                 info.bytesPerPixel = 4;
116                 break;
117             case RGB_888:
118             case HSV_888:
119                 info.bitsPerPixel = 24;
120                 info.bytesPerPixel = 3;
121                 break;
122             case RGB_565:
123             case RGBA_5551:
124             case RGBA_4444:
125             case LA_88:
126                 info.bitsPerPixel = 16;
127                 info.bytesPerPixel = 2;
128                 break;
129             case A_8:
130             case L_8:
131             case RGB_332:
132                 info.bitsPerPixel = 8;
133                 info.bytesPerPixel = 1;
134                 break;
135             case YCbCr_422_SP:
136             case YCbCr_422_I:
137                 info.bitsPerPixel = 16;
138                 info.bytesPerPixel = 1;
139                 break;
140             case YCbCr_420_SP:
141                 info.bitsPerPixel = 12;
142                 info.bytesPerPixel = 1;
143                 break;
144             case RGBA_F16:
145                 info.bitsPerPixel = 64;
146                 info.bytesPerPixel = 8;
147                 break;
148             case R_8:
149                 info.bitsPerPixel = 8;
150                 info.bytesPerPixel = 1;
151                 break;
152             default:
153                 throw new IllegalArgumentException("unknown pixel format " + format);
154         }
155     }
156 
formatHasAlpha(@ormat int format)157     public static boolean formatHasAlpha(@Format int format) {
158         switch (format) {
159             case PixelFormat.A_8:
160             case PixelFormat.LA_88:
161             case PixelFormat.RGBA_4444:
162             case PixelFormat.RGBA_5551:
163             case PixelFormat.RGBA_8888:
164             case PixelFormat.RGBA_F16:
165             case PixelFormat.RGBA_1010102:
166             case PixelFormat.TRANSLUCENT:
167             case PixelFormat.TRANSPARENT:
168                 return true;
169         }
170         return false;
171     }
172 
173     /**
174      * Determine whether or not this is a public-visible and non-deprecated {@code format}.
175      *
176      * <p>In particular, {@code @hide} formats will return {@code false}.</p>
177      *
178      * <p>Any other indirect formats (such as {@code TRANSPARENT} or {@code TRANSLUCENT})
179      * will return {@code false}.</p>
180      *
181      * @param format an integer format
182      * @return a boolean
183      *
184      * @hide
185      */
isPublicFormat(@ormat int format)186     public static boolean isPublicFormat(@Format int format) {
187         switch (format) {
188             case RGBA_8888:
189             case RGBX_8888:
190             case RGB_888:
191             case RGB_565:
192             case RGBA_F16:
193             case RGBA_1010102:
194                 return true;
195         }
196 
197         return false;
198     }
199 
200     /**
201      * @hide
202      */
formatToString(@ormat int format)203     public static String formatToString(@Format int format) {
204         switch (format) {
205             case UNKNOWN:
206                 return "UNKNOWN";
207             case TRANSLUCENT:
208                 return "TRANSLUCENT";
209             case TRANSPARENT:
210                 return "TRANSPARENT";
211             case RGBA_8888:
212                 return "RGBA_8888";
213             case RGBX_8888:
214                 return "RGBX_8888";
215             case RGB_888:
216                 return "RGB_888";
217             case RGB_565:
218                 return "RGB_565";
219             case RGBA_5551:
220                 return "RGBA_5551";
221             case RGBA_4444:
222                 return "RGBA_4444";
223             case A_8:
224                 return "A_8";
225             case L_8:
226                 return "L_8";
227             case LA_88:
228                 return "LA_88";
229             case RGB_332:
230                 return "RGB_332";
231             case YCbCr_422_SP:
232                 return "YCbCr_422_SP";
233             case YCbCr_420_SP:
234                 return "YCbCr_420_SP";
235             case YCbCr_422_I:
236                 return "YCbCr_422_I";
237             case RGBA_F16:
238                 return "RGBA_F16";
239             case RGBA_1010102:
240                 return "RGBA_1010102";
241             case HSV_888:
242                 return "HSV_888";
243             case JPEG:
244                 return "JPEG";
245             case R_8:
246                 return "R_8";
247             default:
248                 return Integer.toString(format);
249         }
250     }
251 }
252