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