1/*
2 * Copyright 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 */
16package androidx.media.filterfw;
17
18import java.nio.ByteBuffer;
19
20/**
21 * A collection of utilities to deal with pixel operations on ByteBuffers.
22 */
23public class PixelUtils {
24
25    /**
26     * Copy pixels from one buffer to another, applying a transformation.
27     *
28     * <p>The transformation is specified by specifying the initial offset in the output buffer, the
29     * stride (in pixels) between each pixel, and the stride (in pixels) between each row. The row
30     * stride is measured as the number of pixels between the start of each row.</p>
31     *
32     * <p>Note that this method is native for efficiency reasons. It does NOT do any bounds checking
33     * other than making sure the buffers are of sufficient size. This means that you can corrupt
34     * memory if specifying incorrect stride values!</p>
35     *
36     * @param input The input buffer containing pixel data.
37     * @param output The output buffer to hold the transformed pixel data.
38     * @param width The width of the input image.
39     * @param height The height of the input image.
40     * @param offset The start offset in the output (in pixels)
41     * @param pixStride The stride between each pixel (in pixels)
42     * @param rowStride The stride between the start of each row (in pixels)
43     */
44    public static void copyPixels(ByteBuffer input,
45            ByteBuffer output,
46            int width,
47            int height,
48            int offset,
49            int pixStride,
50            int rowStride) {
51        if (input.remaining() != output.remaining()) {
52            throw new IllegalArgumentException("Input and output buffers must have the same size!");
53        } else if (input.remaining() % 4 != 0) {
54            throw new IllegalArgumentException("Input buffer size must be a multiple of 4!");
55        } else if (output.remaining() % 4 != 0) {
56            throw new IllegalArgumentException("Output buffer size must be a multiple of 4!");
57        } else if ((width * height * 4) != input.remaining()) {
58            throw new IllegalArgumentException(
59                    "Input buffer size does not match given dimensions!");
60        } else if ((width * height * 4) != output.remaining()) {
61            throw new IllegalArgumentException(
62                    "Output buffer size does not match given dimensions!");
63        }
64        nativeCopyPixels(input, output, width, height, offset, pixStride, rowStride);
65    }
66
67    private static native void nativeCopyPixels(ByteBuffer input,
68            ByteBuffer output,
69            int width,
70            int height,
71            int offset,
72            int pixStride,
73            int rowStride);
74
75    static {
76        System.loadLibrary("smartcamera_jni");
77    }
78
79}
80