Image.java revision e3351f1942bfe86682389b278e7ff128a72ea671
1/*
2 * Copyright (C) 2013 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.media;
18
19import java.nio.ByteBuffer;
20import java.lang.AutoCloseable;
21
22/**
23 * <p>A single complete image buffer to use with a media source such as a
24 * {@link MediaCodec} or a
25 * {@link android.hardware.camera2.CameraDevice CameraDevice}.</p>
26 *
27 * <p>This class allows for efficient direct application access to the pixel
28 * data of the Image through one or more
29 * {@link java.nio.ByteBuffer ByteBuffers}. Each buffer is encapsulated in a
30 * {@link Plane} that describes the layout of the pixel data in that plane. Due
31 * to this direct access, and unlike the {@link android.graphics.Bitmap Bitmap} class,
32 * Images are not directly usable as as UI resources.</p>
33 *
34 * <p>Since Images are often directly produced or consumed by hardware
35 * components, they are a limited resource shared across the system, and should
36 * be closed as soon as they are no longer needed.</p>
37 *
38 * <p>For example, when using the {@link ImageReader} class to read out Images
39 * from various media sources, not closing old Image objects will prevent the
40 * availability of new Images once
41 * {@link ImageReader#getMaxImages the maximum outstanding image count} is
42 * reached. When this happens, the function acquiring new Images will typically
43 * throw an {@link IllegalStateException}.</p>
44 *
45 * @see ImageReader
46 */
47public abstract class Image implements AutoCloseable {
48    /**
49     * @hide
50     */
51    protected Image() {
52    }
53
54    /**
55     * Get the format for this image. This format determines the number of
56     * ByteBuffers needed to represent the image, and the general layout of the
57     * pixel data in each in ByteBuffer.
58     *
59     * <p>
60     * The format is one of the values from
61     * {@link android.graphics.ImageFormat ImageFormat}. The mapping between the
62     * formats and the planes is as follows:
63     * </p>
64     *
65     * <table>
66     * <tr>
67     *   <th>Format</th>
68     *   <th>Plane count</th>
69     *   <th>Layout details</th>
70     * </tr>
71     * <tr>
72     *   <td>{@link android.graphics.ImageFormat#JPEG JPEG}</td>
73     *   <td>1</td>
74     *   <td>Compressed data, so row and pixel strides are 0. To uncompress, use
75     *      {@link android.graphics.BitmapFactory#decodeByteArray BitmapFactory#decodeByteArray}.
76     *   </td>
77     * </tr>
78     * <tr>
79     *   <td>{@link android.graphics.ImageFormat#YUV_420_888 YUV_420_888}</td>
80     *   <td>3</td>
81     *   <td>A luminance plane followed by the Cb and Cr chroma planes.
82     *     The chroma planes have half the width and height of the luminance
83     *     plane (4:2:0 subsampling). Each pixel sample in each plane has 8 bits.
84     *     Each plane has its own row stride and pixel stride.</td>
85     * </tr>
86     * <tr>
87     *   <td>{@link android.graphics.ImageFormat#RAW_SENSOR RAW_SENSOR}</td>
88     *   <td>1</td>
89     *   <td>A single plane of raw sensor image data, with 16 bits per color
90     *     sample. The details of the layout need to be queried from the source of
91     *     the raw sensor data, such as
92     *     {@link android.hardware.camera2.CameraDevice CameraDevice}.
93     *   </td>
94     * </tr>
95     * </table>
96     *
97     * @see android.graphics.ImageFormat
98     */
99    public abstract int getFormat();
100
101    /**
102     * The width of the image in pixels. For formats where some color channels
103     * are subsampled, this is the width of the largest-resolution plane.
104     */
105    public abstract int getWidth();
106
107    /**
108     * The height of the image in pixels. For formats where some color channels
109     * are subsampled, this is the height of the largest-resolution plane.
110     */
111    public abstract int getHeight();
112
113    /**
114     * Get the timestamp associated with this frame.
115     * <p>
116     * The timestamp is measured in nanoseconds, and is monotonically
117     * increasing. However, the zero point and whether the timestamp can be
118     * compared against other sources of time or images depend on the source of
119     * this image.
120     * </p>
121     */
122    public abstract long getTimestamp();
123
124    /**
125     * Get the array of pixel planes for this Image. The number of planes is
126     * determined by the format of the Image.
127     */
128    public abstract Plane[] getPlanes();
129
130    /**
131     * Free up this frame for reuse.
132     * <p>
133     * After calling this method, calling any methods on this {@code Image} will
134     * result in an {@link IllegalStateException}, and attempting to read from
135     * {@link ByteBuffer ByteBuffers} returned by an earlier
136     * {@link Plane#getBuffer} call will have undefined behavior.
137     * </p>
138     */
139    @Override
140    public abstract void close();
141
142    /**
143     * <p>A single color plane of image data.</p>
144     *
145     * <p>The number and meaning of the planes in an Image are determined by the
146     * format of the Image.</p>
147     *
148     * <p>Once the Image has been closed, any access to the the plane's
149     * ByteBuffer will fail.</p>
150     *
151     * @see #getFormat
152     */
153    public static abstract class Plane {
154        /**
155         * @hide
156         */
157        protected Plane() {
158        }
159
160        /**
161         * <p>The row stride for this color plane, in bytes.</p>
162         *
163         * <p>This is the distance between the start of two consecutive rows of
164         * pixels in the image. The row stride is always greater than 0.</p>
165         */
166        public abstract int getRowStride();
167        /**
168         * <p>The distance between adjacent pixel samples, in bytes.</p>
169         *
170         * <p>This is the distance between two consecutive pixel values in a row
171         * of pixels. It may be larger than the size of a single pixel to
172         * account for interleaved image data or padded formats.
173         * The pixel stride is always greater than 0.</p>
174         */
175        public abstract int getPixelStride();
176        /**
177         * <p>Get a direct {@link java.nio.ByteBuffer ByteBuffer}
178         * containing the frame data.</p>
179         *
180         * <p>In particular, the buffer returned will always have
181         * {@link java.nio.ByteBuffer#isDirect isDirect} return {@code true}, so
182         * the underlying data could be mapped as a pointer in JNI without doing
183         * any copies with {@code GetDirectBufferAddress}.</p>
184         *
185         * @return the byte buffer containing the image data for this plane.
186         */
187        public abstract ByteBuffer getBuffer();
188    }
189
190}
191