• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 public class ImageFormat {
20     /*
21      * these constants are chosen to be binary compatible with their previous
22      * location in PixelFormat.java
23      */
24 
25     public static final int UNKNOWN = 0;
26 
27     /**
28      * RGB format used for pictures encoded as RGB_565. See
29      * {@link android.hardware.Camera.Parameters#setPictureFormat(int)}.
30      */
31     public static final int RGB_565 = 4;
32 
33     /**
34      * <p>Android YUV format.</p>
35      *
36      * <p>This format is exposed to software decoders and applications.</p>
37      *
38      * <p>YV12 is a 4:2:0 YCrCb planar format comprised of a WxH Y plane followed
39      * by (W/2) x (H/2) Cr and Cb planes.</p>
40      *
41      * <p>This format assumes
42      * <ul>
43      * <li>an even width</li>
44      * <li>an even height</li>
45      * <li>a horizontal stride multiple of 16 pixels</li>
46      * <li>a vertical stride equal to the height</li>
47      * </ul>
48      * </p>
49      *
50      * <pre> y_size = stride * height
51      * c_stride = ALIGN(stride/2, 16)
52      * c_size = c_stride * height/2
53      * size = y_size + c_size * 2
54      * cr_offset = y_size
55      * cb_offset = y_size + c_size</pre>
56      *
57      * <p>This format is guaranteed to be supported for camera preview images since
58      * API level 12; for earlier API versions, check
59      * {@link android.hardware.Camera.Parameters#getSupportedPreviewFormats()}.
60      *
61      * <p>Note that for camera preview callback use (see
62      * {@link android.hardware.Camera#setPreviewCallback}), the
63      * <var>stride</var> value is the smallest possible; that is, it is equal
64      * to:
65      *
66      * <pre>stride = ALIGN(width, 16)</pre>
67      *
68      * @see android.hardware.Camera.Parameters#setPreviewCallback
69      * @see android.hardware.Camera.Parameters#setPreviewFormat
70      * @see android.hardware.Camera.Parameters#getSupportedPreviewFormats
71      * </p>
72      */
73     public static final int YV12 = 0x32315659;
74 
75     /**
76      * <p>Android Y8 format.</p>
77      *
78      * <p>Y8 is a YUV planar format comprised of a WxH Y plane only, with each pixel
79      * being represented by 8 bits. It is equivalent to just the Y plane from {@link #YV12}
80      * format.</p>
81      *
82      * <p>This format assumes
83      * <ul>
84      * <li>an even width</li>
85      * <li>an even height</li>
86      * <li>a horizontal stride multiple of 16 pixels</li>
87      * </ul>
88      * </p>
89      *
90      * <pre> y_size = stride * height </pre>
91      *
92      * <p>For example, the {@link android.media.Image} object can provide data
93      * in this format from a {@link android.hardware.camera2.CameraDevice}
94      * through a {@link android.media.ImageReader} object if this format is
95      * supported by {@link android.hardware.camera2.CameraDevice}.</p>
96      *
97      * @see android.media.Image
98      * @see android.media.ImageReader
99      * @see android.hardware.camera2.CameraDevice
100      *
101      * @hide
102      */
103     public static final int Y8 = 0x20203859;
104 
105     /**
106      * <p>Android Y16 format.</p>
107      *
108      * Y16 is a YUV planar format comprised of a WxH Y plane, with each pixel
109      * being represented by 16 bits. It is just like {@link #Y8}, but has 16
110      * bits per pixel (little endian).</p>
111      *
112      * <p>This format assumes
113      * <ul>
114      * <li>an even width</li>
115      * <li>an even height</li>
116      * <li>a horizontal stride multiple of 16 pixels</li>
117      * </ul>
118      * </p>
119      *
120      * <pre> y_size = stride * height </pre>
121      *
122      * <p>For example, the {@link android.media.Image} object can provide data
123      * in this format from a {@link android.hardware.camera2.CameraDevice}
124      * through a {@link android.media.ImageReader} object if this format is
125      * supported by {@link android.hardware.camera2.CameraDevice}.</p>
126      *
127      * @see android.media.Image
128      * @see android.media.ImageReader
129      * @see android.hardware.camera2.CameraDevice
130      *
131      * @hide
132      */
133     public static final int Y16 = 0x20363159;
134 
135     /**
136      * YCbCr format, used for video. Whether this format is supported by the
137      * camera hardware can be determined by
138      * {@link android.hardware.Camera.Parameters#getSupportedPreviewFormats()}.
139      */
140     public static final int NV16 = 0x10;
141 
142     /**
143      * YCrCb format used for images, which uses the NV21 encoding format. This
144      * is the default format for camera preview images, when not otherwise set
145      * with {@link android.hardware.Camera.Parameters#setPreviewFormat(int)}.
146      */
147     public static final int NV21 = 0x11;
148 
149     /**
150      * YCbCr format used for images, which uses YUYV (YUY2) encoding format.
151      * This is an alternative format for camera preview images. Whether this
152      * format is supported by the camera hardware can be determined by
153      * {@link android.hardware.Camera.Parameters#getSupportedPreviewFormats()}.
154      */
155     public static final int YUY2 = 0x14;
156 
157     /**
158      * Encoded formats. These are not necessarily supported by the hardware.
159      */
160     public static final int JPEG = 0x100;
161 
162     /**
163      * <p>Multi-plane Android YUV format</p>
164      *
165      * <p>This format is a generic YCbCr format, capable of describing any 4:2:0
166      * chroma-subsampled planar or semiplanar buffer (but not fully interleaved),
167      * with 8 bits per color sample.</p>
168      *
169      * <p>Images in this format are always represented by three separate buffers
170      * of data, one for each color plane. Additional information always
171      * accompanies the buffers, describing the row stride and the pixel stride
172      * for each plane.</p>
173      *
174      * <p>The order of planes in the array returned by
175      * {@link android.media.Image#getPlanes() Image#getPlanes()} is guaranteed such that
176      * plane #0 is always Y, plane #1 is always U (Cb), and plane #2 is always V (Cr).</p>
177      *
178      * <p>The Y-plane is guaranteed not to be interleaved with the U/V planes
179      * (in particular, pixel stride is always 1 in
180      * {@link android.media.Image.Plane#getPixelStride() yPlane.getPixelStride()}).</p>
181      *
182      * <p>The U/V planes are guaranteed to have the same row stride and pixel stride
183      * (in particular,
184      * {@link android.media.Image.Plane#getRowStride() uPlane.getRowStride()}
185      * == {@link android.media.Image.Plane#getRowStride() vPlane.getRowStride()} and
186      * {@link android.media.Image.Plane#getPixelStride() uPlane.getPixelStride()}
187      * == {@link android.media.Image.Plane#getPixelStride() vPlane.getPixelStride()};
188      * ).</p>
189      *
190      * @see android.media.Image
191      * @see android.media.ImageReader
192      * @see android.hardware.camera2.CameraDevice
193      */
194     public static final int YUV_420_888 = 0x23;
195 
196     /**
197      * <p>General raw camera sensor image format, usually representing a
198      * single-channel Bayer-mosaic image. Each pixel color sample is stored with
199      * 16 bits of precision.</p>
200      *
201      * <p>The layout of the color mosaic, the maximum and minimum encoding
202      * values of the raw pixel data, the color space of the image, and all other
203      * needed information to interpret a raw sensor image must be queried from
204      * the {@link android.hardware.camera2.CameraDevice} which produced the
205      * image.</p>
206      *
207      * @hide
208      */
209     public static final int RAW_SENSOR = 0x20;
210 
211     /**
212      * Raw bayer format used for images, which is 10 bit precision samples
213      * stored in 16 bit words. The filter pattern is RGGB. Whether this format
214      * is supported by the camera hardware can be determined by
215      * {@link android.hardware.Camera.Parameters#getSupportedPreviewFormats()}.
216      *
217      * @hide
218      */
219     public static final int BAYER_RGGB = 0x200;
220 
221     /**
222      * Use this function to retrieve the number of bits per pixel of an
223      * ImageFormat.
224      *
225      * @param format
226      * @return the number of bits per pixel of the given format or -1 if the
227      *         format doesn't exist or is not supported.
228      */
getBitsPerPixel(int format)229     public static int getBitsPerPixel(int format) {
230         switch (format) {
231             case RGB_565:
232                 return 16;
233             case NV16:
234                 return 16;
235             case YUY2:
236                 return 16;
237             case YV12:
238                 return 12;
239             case Y8:
240                 return 8;
241             case Y16:
242                 return 16;
243             case NV21:
244                 return 12;
245             case YUV_420_888:
246                 return 12;
247             case RAW_SENSOR:
248                 return 16;
249             case BAYER_RGGB:
250                 return 16;
251         }
252         return -1;
253     }
254 }
255