ImageFormat.java revision d1988a98ed69db8c33b77b5c085ab91d22ef3bbc
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
17package android.graphics;
18
19public 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     * <p>For example, the {@link android.media.Image} object can provide data
191     * in this format from a {@link android.hardware.camera2.CameraDevice}
192     * through a {@link android.media.ImageReader} object.</p>
193     *
194     * @see android.media.Image
195     * @see android.media.ImageReader
196     * @see android.hardware.camera2.CameraDevice
197     */
198    public static final int YUV_420_888 = 0x23;
199
200    /**
201     * <p>General raw camera sensor image format, usually representing a
202     * single-channel Bayer-mosaic image. Each pixel color sample is stored with
203     * 16 bits of precision.</p>
204     *
205     * <p>The layout of the color mosaic, the maximum and minimum encoding
206     * values of the raw pixel data, the color space of the image, and all other
207     * needed information to interpret a raw sensor image must be queried from
208     * the {@link android.hardware.camera2.CameraDevice} which produced the
209     * image.</p>
210     */
211    public static final int RAW_SENSOR = 0x20;
212
213    /**
214     * <p>
215     * Android 10-bit raw format
216     * </p>
217     * <p>
218     * This is a single-plane, 10-bit per pixel, densely packed, unprocessed
219     * format, usually representing raw Bayer-pattern images coming from an image
220     * sensor.
221     * </p>
222     * <p>
223     * In an image buffer with this format, starting from the first pixel, each
224     * 4 consecutive pixels are packed into 5 bytes (40 bits). Each one of the
225     * first 4 bytes contains the top 8 bits of each pixel, The fifth byte
226     * contains the 2 least significant bits of the 4 pixels, the exact layout
227     * data for each 4 consecutive pixels is illustrated below (Pi[j] stands for
228     * the jth bit of the ith pixel):
229     * </p>
230     * <table>
231     * <thead>
232     * <tr>
233     * <th align="center"></th>
234     * <th align="center">bit 7</th>
235     * <th align="center">bit 6</th>
236     * <th align="center">bit 5</th>
237     * <th align="center">bit 4</th>
238     * <th align="center">bit 3</th>
239     * <th align="center">bit 2</th>
240     * <th align="center">bit 1</th>
241     * <th align="center">bit 0</th>
242     * </tr>
243     * </thead> <tbody>
244     * <tr>
245     * <td align="center">Byte 0:</td>
246     * <td align="center">P0[9]</td>
247     * <td align="center">P0[8]</td>
248     * <td align="center">P0[7]</td>
249     * <td align="center">P0[6]</td>
250     * <td align="center">P0[5]</td>
251     * <td align="center">P0[4]</td>
252     * <td align="center">P0[3]</td>
253     * <td align="center">P0[2]</td>
254     * </tr>
255     * <tr>
256     * <td align="center">Byte 1:</td>
257     * <td align="center">P1[9]</td>
258     * <td align="center">P1[8]</td>
259     * <td align="center">P1[7]</td>
260     * <td align="center">P1[6]</td>
261     * <td align="center">P1[5]</td>
262     * <td align="center">P1[4]</td>
263     * <td align="center">P1[3]</td>
264     * <td align="center">P1[2]</td>
265     * </tr>
266     * <tr>
267     * <td align="center">Byte 2:</td>
268     * <td align="center">P2[9]</td>
269     * <td align="center">P2[8]</td>
270     * <td align="center">P2[7]</td>
271     * <td align="center">P2[6]</td>
272     * <td align="center">P2[5]</td>
273     * <td align="center">P2[4]</td>
274     * <td align="center">P2[3]</td>
275     * <td align="center">P2[2]</td>
276     * </tr>
277     * <tr>
278     * <td align="center">Byte 3:</td>
279     * <td align="center">P3[9]</td>
280     * <td align="center">P3[8]</td>
281     * <td align="center">P3[7]</td>
282     * <td align="center">P3[6]</td>
283     * <td align="center">P3[5]</td>
284     * <td align="center">P3[4]</td>
285     * <td align="center">P3[3]</td>
286     * <td align="center">P3[2]</td>
287     * </tr>
288     * <tr>
289     * <td align="center">Byte 4:</td>
290     * <td align="center">P3[1]</td>
291     * <td align="center">P3[0]</td>
292     * <td align="center">P2[1]</td>
293     * <td align="center">P2[0]</td>
294     * <td align="center">P1[1]</td>
295     * <td align="center">P1[0]</td>
296     * <td align="center">P0[1]</td>
297     * <td align="center">P0[0]</td>
298     * </tr>
299     * </tbody>
300     * </table>
301     * <p>
302     * This format assumes
303     * <ul>
304     * <li>a width multiple of 4 pixels</li>
305     * <li>an even height</li>
306     * </ul>
307     * </p>
308     *
309     * <pre>
310     * size = width * height * 10 / 8
311     * </pre>
312     * <p>
313     * Since this is a densely packed format, the pixel and row stride are always
314     * 0. The application must use the pixel data layout defined in above table
315     * to access data.
316     * </p>
317     *
318     * <p>
319     * For example, the {@link android.media.Image} object can provide data in
320     * this format from a {@link android.hardware.camera2.CameraDevice} (if supported)
321     * through a {@link android.media.ImageReader} object. The
322     * {@link android.media.Image#getPlanes() Image#getPlanes()} will return a
323     * single plane containing the pixel data. The pixel stride and row stride
324     * are always 0 in {@link android.media.Image.Plane#getPixelStride()} and
325     * {@link android.media.Image.Plane#getRowStride()} respectively.
326     * </p>
327     *
328     * @see android.media.Image
329     * @see android.media.ImageReader
330     * @see android.hardware.camera2.CameraDevice
331     */
332    public static final int RAW10 = 0x25;
333
334    /**
335     * Raw bayer format used for images, which is 10 bit precision samples
336     * stored in 16 bit words. The filter pattern is RGGB. Whether this format
337     * is supported by the camera hardware can be determined by
338     * {@link android.hardware.Camera.Parameters#getSupportedPreviewFormats()}.
339     *
340     * @hide
341     */
342    public static final int BAYER_RGGB = 0x200;
343
344    /**
345     * Use this function to retrieve the number of bits per pixel of an
346     * ImageFormat.
347     *
348     * @param format
349     * @return the number of bits per pixel of the given format or -1 if the
350     *         format doesn't exist or is not supported.
351     */
352    public static int getBitsPerPixel(int format) {
353        switch (format) {
354            case RGB_565:
355                return 16;
356            case NV16:
357                return 16;
358            case YUY2:
359                return 16;
360            case YV12:
361                return 12;
362            case Y8:
363                return 8;
364            case Y16:
365                return 16;
366            case NV21:
367                return 12;
368            case YUV_420_888:
369                return 12;
370            case RAW_SENSOR:
371                return 16;
372            case BAYER_RGGB:
373                return 16;
374            case RAW10:
375                return 10;
376        }
377        return -1;
378    }
379
380    /**
381     * Determine whether or not this is a public-visible {@code format}.
382     *
383     * <p>In particular, {@code @hide} formats will return {@code false}.</p>
384     *
385     * <p>Any other formats (including UNKNOWN) will return {@code false}.</p>
386     *
387     * @param format an integer format
388     * @return a boolean
389     *
390     * @hide
391     */
392    public static boolean isPublicFormat(int format) {
393        switch (format) {
394            case RGB_565:
395            case NV16:
396            case YUY2:
397            case YV12:
398            case JPEG:
399            case NV21:
400            case YUV_420_888:
401            case RAW_SENSOR:
402            case RAW10:
403                return true;
404        }
405
406        return false;
407    }
408}
409