ImageFormat.java revision 4c4064fa66f551f49b7e1017b1ebe65a05f9df21
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 (in each row), 240 * unprocessed format, usually representing raw Bayer-pattern images coming 241 * from an image sensor. 242 * </p> 243 * <p> 244 * In an image buffer with this format, starting from the first pixel of 245 * each row, each 4 consecutive pixels are packed into 5 bytes (40 bits). 246 * Each one of the first 4 bytes contains the top 8 bits of each pixel, The 247 * fifth byte contains the 2 least significant bits of the 4 pixels, the 248 * exact layout data for each 4 consecutive pixels is illustrated below 249 * ({@code Pi[j]} stands for 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>size = row stride * height</pre> where the row stride is in <em>bytes</em>, 331 * not pixels. 332 * 333 * <p> 334 * Since this is a densely packed format, the pixel stride is always 0. The 335 * application must use the pixel data layout defined in above table to 336 * access each row data. When row stride is equal to {@code width * (10 / 8)}, there 337 * will be no padding bytes at the end of each row, the entire image data is 338 * densely packed. When stride is larger than {@code width * (10 / 8)}, padding 339 * bytes will be present at the end of each row. 340 * </p> 341 * <p> 342 * For example, the {@link android.media.Image} object can provide data in 343 * this format from a {@link android.hardware.camera2.CameraDevice} (if 344 * supported) through a {@link android.media.ImageReader} object. The 345 * {@link android.media.Image#getPlanes() Image#getPlanes()} will return a 346 * single plane containing the pixel data. The pixel stride is always 0 in 347 * {@link android.media.Image.Plane#getPixelStride()}, and the 348 * {@link android.media.Image.Plane#getRowStride()} describes the vertical 349 * neighboring pixel distance (in bytes) between adjacent rows. 350 * </p> 351 * 352 * @see android.media.Image 353 * @see android.media.ImageReader 354 * @see android.hardware.camera2.CameraDevice 355 */ 356 public static final int RAW10 = 0x25; 357 358 /** 359 * Use this function to retrieve the number of bits per pixel of an 360 * ImageFormat. 361 * 362 * @param format 363 * @return the number of bits per pixel of the given format or -1 if the 364 * format doesn't exist or is not supported. 365 */ 366 public static int getBitsPerPixel(int format) { 367 switch (format) { 368 case RGB_565: 369 return 16; 370 case NV16: 371 return 16; 372 case YUY2: 373 return 16; 374 case YV12: 375 return 12; 376 case Y8: 377 return 8; 378 case Y16: 379 return 16; 380 case NV21: 381 return 12; 382 case YUV_420_888: 383 return 12; 384 case RAW_SENSOR: 385 return 16; 386 case RAW10: 387 return 10; 388 } 389 return -1; 390 } 391 392 /** 393 * Determine whether or not this is a public-visible {@code format}. 394 * 395 * <p>In particular, {@code @hide} formats will return {@code false}.</p> 396 * 397 * <p>Any other formats (including UNKNOWN) will return {@code false}.</p> 398 * 399 * @param format an integer format 400 * @return a boolean 401 * 402 * @hide 403 */ 404 public static boolean isPublicFormat(int format) { 405 switch (format) { 406 case RGB_565: 407 case NV16: 408 case YUY2: 409 case YV12: 410 case JPEG: 411 case NV21: 412 case YUV_420_888: 413 case RAW_SENSOR: 414 case RAW10: 415 return true; 416 } 417 418 return false; 419 } 420} 421