ImageFormat.java revision b942b05093d2b1cee59ac73196a4b99962f10add
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>For the {@link android.hardware.camera2} API, the {@link #YUV_420_888} format is
58     * recommended for YUV output instead.</p>
59     *
60     * <p>For the older camera API, this format is guaranteed to be supported for
61     * {@link android.hardware.Camera} preview images since API level 12; for earlier API versions,
62     * check {@link android.hardware.Camera.Parameters#getSupportedPreviewFormats()}.
63     *
64     * <p>Note that for camera preview callback use (see
65     * {@link android.hardware.Camera#setPreviewCallback}), the
66     * <var>stride</var> value is the smallest possible; that is, it is equal
67     * to:
68     *
69     * <pre>stride = ALIGN(width, 16)</pre>
70     *
71     * @see android.hardware.Camera.Parameters#setPreviewCallback
72     * @see android.hardware.Camera.Parameters#setPreviewFormat
73     * @see android.hardware.Camera.Parameters#getSupportedPreviewFormats
74     * </p>
75     */
76    public static final int YV12 = 0x32315659;
77
78    /**
79     * <p>Android Y8 format.</p>
80     *
81     * <p>Y8 is a YUV planar format comprised of a WxH Y plane only, with each pixel
82     * being represented by 8 bits. It is equivalent to just the Y plane from {@link #YV12}
83     * format.</p>
84     *
85     * <p>This format assumes
86     * <ul>
87     * <li>an even width</li>
88     * <li>an even height</li>
89     * <li>a horizontal stride multiple of 16 pixels</li>
90     * </ul>
91     * </p>
92     *
93     * <pre> y_size = stride * height </pre>
94     *
95     * <p>For example, the {@link android.media.Image} object can provide data
96     * in this format from a {@link android.hardware.camera2.CameraDevice}
97     * through a {@link android.media.ImageReader} object if this format is
98     * supported by {@link android.hardware.camera2.CameraDevice}.</p>
99     *
100     * @see android.media.Image
101     * @see android.media.ImageReader
102     * @see android.hardware.camera2.CameraDevice
103     *
104     * @hide
105     */
106    public static final int Y8 = 0x20203859;
107
108    /**
109     * <p>Android Y16 format.</p>
110     *
111     * Y16 is a YUV planar format comprised of a WxH Y plane, with each pixel
112     * being represented by 16 bits. It is just like {@link #Y8}, but has 16
113     * bits per pixel (little endian).</p>
114     *
115     * <p>This format assumes
116     * <ul>
117     * <li>an even width</li>
118     * <li>an even height</li>
119     * <li>a horizontal stride multiple of 16 pixels</li>
120     * </ul>
121     * </p>
122     *
123     * <pre> y_size = stride * height </pre>
124     *
125     * <p>For example, the {@link android.media.Image} object can provide data
126     * in this format from a {@link android.hardware.camera2.CameraDevice}
127     * through a {@link android.media.ImageReader} object if this format is
128     * supported by {@link android.hardware.camera2.CameraDevice}.</p>
129     *
130     * @see android.media.Image
131     * @see android.media.ImageReader
132     * @see android.hardware.camera2.CameraDevice
133     *
134     * @hide
135     */
136    public static final int Y16 = 0x20363159;
137
138    /**
139     * YCbCr format, used for video.
140     *
141     * <p>For the {@link android.hardware.camera2} API, the {@link #YUV_420_888} format is
142     * recommended for YUV output instead.</p>
143     *
144     * <p>Whether this format is supported by the old camera API can be determined by
145     * {@link android.hardware.Camera.Parameters#getSupportedPreviewFormats()}.</p>
146     *
147     */
148    public static final int NV16 = 0x10;
149
150    /**
151     * YCrCb format used for images, which uses the NV21 encoding format.
152     *
153     * <p>This is the default format
154     * for {@link android.hardware.Camera} preview images, when not otherwise set with
155     * {@link android.hardware.Camera.Parameters#setPreviewFormat(int)}.</p>
156     *
157     * <p>For the {@link android.hardware.camera2} API, the {@link #YUV_420_888} format is
158     * recommended for YUV output instead.</p>
159     */
160    public static final int NV21 = 0x11;
161
162    /**
163     * YCbCr format used for images, which uses YUYV (YUY2) encoding format.
164     *
165     * <p>For the {@link android.hardware.camera2} API, the {@link #YUV_420_888} format is
166     * recommended for YUV output instead.</p>
167     *
168     * <p>This is an alternative format for {@link android.hardware.Camera} preview images. Whether
169     * this format is supported by the camera hardware can be determined by
170     * {@link android.hardware.Camera.Parameters#getSupportedPreviewFormats()}.</p>
171     */
172    public static final int YUY2 = 0x14;
173
174    /**
175     * Compressed JPEG format.
176     *
177     * <p>This format is always supported as an output format for the
178     * {@link android.hardware.camera2} API, and as a picture format for the older
179     * {@link android.hardware.Camera} API</p>
180     */
181    public static final int JPEG = 0x100;
182
183    /**
184     * <p>Multi-plane Android YUV format</p>
185     *
186     * <p>This format is a generic YCbCr format, capable of describing any 4:2:0
187     * chroma-subsampled planar or semiplanar buffer (but not fully interleaved),
188     * with 8 bits per color sample.</p>
189     *
190     * <p>Images in this format are always represented by three separate buffers
191     * of data, one for each color plane. Additional information always
192     * accompanies the buffers, describing the row stride and the pixel stride
193     * for each plane.</p>
194     *
195     * <p>The order of planes in the array returned by
196     * {@link android.media.Image#getPlanes() Image#getPlanes()} is guaranteed such that
197     * plane #0 is always Y, plane #1 is always U (Cb), and plane #2 is always V (Cr).</p>
198     *
199     * <p>The Y-plane is guaranteed not to be interleaved with the U/V planes
200     * (in particular, pixel stride is always 1 in
201     * {@link android.media.Image.Plane#getPixelStride() yPlane.getPixelStride()}).</p>
202     *
203     * <p>The U/V planes are guaranteed to have the same row stride and pixel stride
204     * (in particular,
205     * {@link android.media.Image.Plane#getRowStride() uPlane.getRowStride()}
206     * == {@link android.media.Image.Plane#getRowStride() vPlane.getRowStride()} and
207     * {@link android.media.Image.Plane#getPixelStride() uPlane.getPixelStride()}
208     * == {@link android.media.Image.Plane#getPixelStride() vPlane.getPixelStride()};
209     * ).</p>
210     *
211     * <p>For example, the {@link android.media.Image} object can provide data
212     * in this format from a {@link android.hardware.camera2.CameraDevice}
213     * through a {@link android.media.ImageReader} object.</p>
214     *
215     * @see android.media.Image
216     * @see android.media.ImageReader
217     * @see android.hardware.camera2.CameraDevice
218     */
219    public static final int YUV_420_888 = 0x23;
220
221    /**
222     * <p>General raw camera sensor image format, usually representing a
223     * single-channel Bayer-mosaic image. Each pixel color sample is stored with
224     * 16 bits of precision.</p>
225     *
226     * <p>The layout of the color mosaic, the maximum and minimum encoding
227     * values of the raw pixel data, the color space of the image, and all other
228     * needed information to interpret a raw sensor image must be queried from
229     * the {@link android.hardware.camera2.CameraDevice} which produced the
230     * image.</p>
231     */
232    public static final int RAW_SENSOR = 0x20;
233
234    /**
235     * <p>
236     * Android 10-bit raw format
237     * </p>
238     * <p>
239     * This is a single-plane, 10-bit per pixel, densely packed, unprocessed
240     * format, usually representing raw Bayer-pattern images coming from an image
241     * sensor.
242     * </p>
243     * <p>
244     * In an image buffer with this format, starting from the first pixel, each
245     * 4 consecutive pixels are packed into 5 bytes (40 bits). Each one of the
246     * first 4 bytes contains the top 8 bits of each pixel, The fifth byte
247     * contains the 2 least significant bits of the 4 pixels, the exact layout
248     * data for each 4 consecutive pixels is illustrated below (Pi[j] stands for
249     * the jth bit of the ith pixel):
250     * </p>
251     * <table>
252     * <thead>
253     * <tr>
254     * <th align="center"></th>
255     * <th align="center">bit 7</th>
256     * <th align="center">bit 6</th>
257     * <th align="center">bit 5</th>
258     * <th align="center">bit 4</th>
259     * <th align="center">bit 3</th>
260     * <th align="center">bit 2</th>
261     * <th align="center">bit 1</th>
262     * <th align="center">bit 0</th>
263     * </tr>
264     * </thead> <tbody>
265     * <tr>
266     * <td align="center">Byte 0:</td>
267     * <td align="center">P0[9]</td>
268     * <td align="center">P0[8]</td>
269     * <td align="center">P0[7]</td>
270     * <td align="center">P0[6]</td>
271     * <td align="center">P0[5]</td>
272     * <td align="center">P0[4]</td>
273     * <td align="center">P0[3]</td>
274     * <td align="center">P0[2]</td>
275     * </tr>
276     * <tr>
277     * <td align="center">Byte 1:</td>
278     * <td align="center">P1[9]</td>
279     * <td align="center">P1[8]</td>
280     * <td align="center">P1[7]</td>
281     * <td align="center">P1[6]</td>
282     * <td align="center">P1[5]</td>
283     * <td align="center">P1[4]</td>
284     * <td align="center">P1[3]</td>
285     * <td align="center">P1[2]</td>
286     * </tr>
287     * <tr>
288     * <td align="center">Byte 2:</td>
289     * <td align="center">P2[9]</td>
290     * <td align="center">P2[8]</td>
291     * <td align="center">P2[7]</td>
292     * <td align="center">P2[6]</td>
293     * <td align="center">P2[5]</td>
294     * <td align="center">P2[4]</td>
295     * <td align="center">P2[3]</td>
296     * <td align="center">P2[2]</td>
297     * </tr>
298     * <tr>
299     * <td align="center">Byte 3:</td>
300     * <td align="center">P3[9]</td>
301     * <td align="center">P3[8]</td>
302     * <td align="center">P3[7]</td>
303     * <td align="center">P3[6]</td>
304     * <td align="center">P3[5]</td>
305     * <td align="center">P3[4]</td>
306     * <td align="center">P3[3]</td>
307     * <td align="center">P3[2]</td>
308     * </tr>
309     * <tr>
310     * <td align="center">Byte 4:</td>
311     * <td align="center">P3[1]</td>
312     * <td align="center">P3[0]</td>
313     * <td align="center">P2[1]</td>
314     * <td align="center">P2[0]</td>
315     * <td align="center">P1[1]</td>
316     * <td align="center">P1[0]</td>
317     * <td align="center">P0[1]</td>
318     * <td align="center">P0[0]</td>
319     * </tr>
320     * </tbody>
321     * </table>
322     * <p>
323     * This format assumes
324     * <ul>
325     * <li>a width multiple of 4 pixels</li>
326     * <li>an even height</li>
327     * </ul>
328     * </p>
329     *
330     * <pre>
331     * size = width * height * 10 / 8
332     * </pre>
333     * <p>
334     * Since this is a densely packed format, the pixel and row stride are always
335     * 0. The application must use the pixel data layout defined in above table
336     * to access data.
337     * </p>
338     *
339     * <p>
340     * For example, the {@link android.media.Image} object can provide data in
341     * this format from a {@link android.hardware.camera2.CameraDevice} (if supported)
342     * through a {@link android.media.ImageReader} object. The
343     * {@link android.media.Image#getPlanes() Image#getPlanes()} will return a
344     * single plane containing the pixel data. The pixel stride and row stride
345     * are always 0 in {@link android.media.Image.Plane#getPixelStride()} and
346     * {@link android.media.Image.Plane#getRowStride()} respectively.
347     * </p>
348     *
349     * @see android.media.Image
350     * @see android.media.ImageReader
351     * @see android.hardware.camera2.CameraDevice
352     */
353    public static final int RAW10 = 0x25;
354
355    /**
356     * Use this function to retrieve the number of bits per pixel of an
357     * ImageFormat.
358     *
359     * @param format
360     * @return the number of bits per pixel of the given format or -1 if the
361     *         format doesn't exist or is not supported.
362     */
363    public static int getBitsPerPixel(int format) {
364        switch (format) {
365            case RGB_565:
366                return 16;
367            case NV16:
368                return 16;
369            case YUY2:
370                return 16;
371            case YV12:
372                return 12;
373            case Y8:
374                return 8;
375            case Y16:
376                return 16;
377            case NV21:
378                return 12;
379            case YUV_420_888:
380                return 12;
381            case RAW_SENSOR:
382                return 16;
383            case RAW10:
384                return 10;
385        }
386        return -1;
387    }
388
389    /**
390     * Determine whether or not this is a public-visible {@code format}.
391     *
392     * <p>In particular, {@code @hide} formats will return {@code false}.</p>
393     *
394     * <p>Any other formats (including UNKNOWN) will return {@code false}.</p>
395     *
396     * @param format an integer format
397     * @return a boolean
398     *
399     * @hide
400     */
401    public static boolean isPublicFormat(int format) {
402        switch (format) {
403            case RGB_565:
404            case NV16:
405            case YUY2:
406            case YV12:
407            case JPEG:
408            case NV21:
409            case YUV_420_888:
410            case RAW_SENSOR:
411            case RAW10:
412                return true;
413        }
414
415        return false;
416    }
417}
418