ImageFormat.java revision 951516358e2841d2425f610bcd0175d9960135d2
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     * YCbCr format, used for video. Whether this format is supported by the
77     * camera hardware can be determined by
78     * {@link android.hardware.Camera.Parameters#getSupportedPreviewFormats()}.
79     */
80    public static final int NV16 = 0x10;
81
82    /**
83     * YCrCb format used for images, which uses the NV21 encoding format. This
84     * is the default format for camera preview images, when not otherwise set
85     * with {@link android.hardware.Camera.Parameters#setPreviewFormat(int)}.
86     */
87    public static final int NV21 = 0x11;
88
89    /**
90     * YCbCr format used for images, which uses YUYV (YUY2) encoding format.
91     * This is an alternative format for camera preview images. Whether this
92     * format is supported by the camera hardware can be determined by
93     * {@link android.hardware.Camera.Parameters#getSupportedPreviewFormats()}.
94     */
95    public static final int YUY2 = 0x14;
96
97    /**
98     * Encoded formats. These are not necessarily supported by the hardware.
99     */
100    public static final int JPEG = 0x100;
101
102    /**
103     * Raw bayer format used for images, which is 10 bit precision samples
104     * stored in 16 bit words. The filter pattern is RGGB. Whether this format
105     * is supported by the camera hardware can be determined by
106     * {@link android.hardware.Camera.Parameters#getSupportedPreviewFormats()}.
107     *
108     * @hide
109     */
110    public static final int BAYER_RGGB = 0x200;
111
112    /**
113     * Use this function to retrieve the number of bits per pixel of an
114     * ImageFormat.
115     *
116     * @param format
117     * @return the number of bits per pixel of the given format or -1 if the
118     *         format doesn't exist or is not supported.
119     */
120    public static int getBitsPerPixel(int format) {
121        switch (format) {
122            case RGB_565:
123                return 16;
124            case NV16:
125                return 16;
126            case YUY2:
127                return 16;
128            case YV12:
129                return 12;
130            case NV21:
131                return 12;
132            case BAYER_RGGB:
133                return 16;
134        }
135        return -1;
136    }
137}
138