ParamsUtils.java revision 7ee78d1ee3ee068897b9313af2ed6446675c1be0
1/*
2 * Copyright (C) 2014 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.hardware.camera2.utils;
18
19import android.graphics.Matrix;
20import android.graphics.Rect;
21import android.graphics.RectF;
22import android.util.Size;
23
24import static com.android.internal.util.Preconditions.*;
25
26/**
27 * Various assortment of params utilities.
28 */
29public class ParamsUtils {
30
31    /**
32     * Create a {@link Rect} from a {@code Size} by creating a new rectangle with
33     * left, top = {@code (0, 0)} and right, bottom = {@code (width, height)}
34     *
35     * @param size a non-{@code null} size
36     *
37     * @return a {@code non-null} rectangle
38     *
39     * @throws NullPointerException if {@code size} was {@code null}
40     */
41    public static Rect createRect(Size size) {
42        checkNotNull(size, "size must not be null");
43
44        return new Rect(/*left*/0, /*top*/0, size.getWidth(), size.getHeight());
45    }
46
47    /**
48     * Create a {@link Rect} from a {@code RectF} by creating a new rectangle with
49     * each corner (left, top, right, bottom) rounded towards the nearest integer bounding box.
50     *
51     * <p>In particular (left, top) is floored, and (right, bottom) is ceiled.</p>
52     *
53     * @param size a non-{@code null} rect
54     *
55     * @return a {@code non-null} rectangle
56     *
57     * @throws NullPointerException if {@code rect} was {@code null}
58     */
59    public static Rect createRect(RectF rect) {
60        checkNotNull(rect, "rect must not be null");
61
62        Rect r = new Rect();
63        rect.roundOut(r);
64
65        return r;
66    }
67
68    /**
69     * Map the rectangle in {@code rect} with the transform in {@code transform} into
70     * a new rectangle, with each corner (left, top, right, bottom) rounded towards the nearest
71     * integer bounding box.
72     *
73     * <p>None of the arguments are mutated.</p>
74     *
75     * @param transform a non-{@code null} transformation matrix
76     * @param rect a non-{@code null} rectangle
77     * @return a new rectangle that was transformed by {@code transform}
78     *
79     * @throws NullPointerException if any of the args were {@code null}
80     */
81    public static Rect mapRect(Matrix transform, Rect rect) {
82        checkNotNull(transform, "transform must not be null");
83        checkNotNull(rect, "rect must not be null");
84
85        RectF rectF = new RectF(rect);
86        transform.mapRect(rectF);
87        return createRect(rectF);
88    }
89
90    /**
91     * Create a {@link Size} from a {@code Rect} by creating a new size whose width
92     * and height are the same as the rectangle's width and heights.
93     *
94     * @param rect a non-{@code null} rectangle
95     *
96     * @return a {@code non-null} size
97     *
98     * @throws NullPointerException if {@code rect} was {@code null}
99     */
100    public static Size createSize(Rect rect) {
101        checkNotNull(rect, "rect must not be null");
102
103        return new Size(rect.width(), rect.height());
104    }
105
106    /**
107     * Convert an integral rectangle ({@code source}) to a floating point rectangle
108     * ({@code destination}) in-place.
109     *
110     * @param source the originating integer rectangle will be read from here
111     * @param destination the resulting floating point rectangle will be written out to here
112     *
113     * @throws NullPointerException if {@code rect} was {@code null}
114     */
115    public static void convertRectF(Rect source, RectF destination) {
116        checkNotNull(source, "source must not be null");
117        checkNotNull(destination, "destination must not be null");
118
119        destination.left = source.left;
120        destination.right = source.right;
121        destination.bottom = source.bottom;
122        destination.top = source.top;
123    }
124
125    private ParamsUtils() {
126        throw new AssertionError();
127    }
128}
129