ImageFormat.java revision 7a600ff65fa79696a2668c7d33101b6ac6696f04
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.photography.CameraDevice} 94 * through a {@link android.media.ImageReader} object if this format is 95 * supported by {@link android.hardware.photography.CameraDevice}.</p> 96 * 97 * @see android.media.Image 98 * @see android.media.ImageReader 99 * @see android.hardware.photography.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.photography.CameraDevice} 124 * through a {@link android.media.ImageReader} object if this format is 125 * supported by {@link android.hardware.photography.CameraDevice}.</p> 126 * 127 * @see android.media.Image 128 * @see android.media.ImageReader 129 * @see android.hardware.photography.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.photography.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.photography.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.photography.CameraDevice} which produced the 209 * image.</p> 210 */ 211 public static final int RAW_SENSOR = 0x20; 212 213 /** 214 * Raw bayer format used for images, which is 10 bit precision samples 215 * stored in 16 bit words. The filter pattern is RGGB. Whether this format 216 * is supported by the camera hardware can be determined by 217 * {@link android.hardware.Camera.Parameters#getSupportedPreviewFormats()}. 218 * 219 * @hide 220 */ 221 public static final int BAYER_RGGB = 0x200; 222 223 /** 224 * Use this function to retrieve the number of bits per pixel of an 225 * ImageFormat. 226 * 227 * @param format 228 * @return the number of bits per pixel of the given format or -1 if the 229 * format doesn't exist or is not supported. 230 */ 231 public static int getBitsPerPixel(int format) { 232 switch (format) { 233 case RGB_565: 234 return 16; 235 case NV16: 236 return 16; 237 case YUY2: 238 return 16; 239 case YV12: 240 return 12; 241 case Y8: 242 return 8; 243 case Y16: 244 return 16; 245 case NV21: 246 return 12; 247 case YUV_420_888: 248 return 12; 249 case RAW_SENSOR: 250 return 16; 251 case BAYER_RGGB: 252 return 16; 253 } 254 return -1; 255 } 256} 257