ParamsUtils.java revision df6242e374b81e802a38cb891477f05d3e4b3cbc
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.Rect; 20import android.graphics.RectF; 21import android.util.Size; 22 23import static com.android.internal.util.Preconditions.*; 24 25/** 26 * Various assortment of params utilities. 27 */ 28public class ParamsUtils { 29 30 /** 31 * Create a {@link Rect} from a {@code Size} by creating a new rectangle with 32 * left, top = {@code (0, 0)} and right, bottom = {@code (width, height)} 33 * 34 * @param size a non-{@code null} size 35 * 36 * @return a {@code non-null} rectangle 37 * 38 * @throws NullPointerException if {@code size} was {@code null} 39 */ 40 public static Rect createRect(Size size) { 41 checkNotNull(size, "size must not be null"); 42 43 return new Rect(/*left*/0, /*top*/0, size.getWidth(), size.getHeight()); 44 } 45 46 /** 47 * Create a {@link Rect} from a {@code RectF} by creating a new rectangle with 48 * each corner (left, top, right, bottom) rounded towards the nearest integer value. 49 * 50 * @param size a non-{@code null} rect 51 * 52 * @return a {@code non-null} rectangle 53 * 54 * @throws NullPointerException if {@code rect} was {@code null} 55 */ 56 public static Rect createRect(RectF rect) { 57 checkNotNull(rect, "rect must not be null"); 58 59 Rect r = new Rect(); 60 rect.round(r); 61 62 return r; 63 } 64 65 /** 66 * Create a {@link Size} from a {@code Rect} by creating a new size whose width 67 * and height are the same as the rectangle's width and heights. 68 * 69 * @param rect a non-{@code null} rectangle 70 * 71 * @return a {@code non-null} size 72 * 73 * @throws NullPointerException if {@code rect} was {@code null} 74 */ 75 public static Size createSize(Rect rect) { 76 checkNotNull(rect, "rect must not be null"); 77 78 return new Size(rect.width(), rect.height()); 79 } 80 81 /** 82 * Convert an integral rectangle ({@code size}) to a floating point rectangle 83 * ({@code destination}) in-place. 84 * 85 * @param size the originating integer rectangle will be read from here 86 * @param destination the resulting floating point rectangle will be written out to here 87 * 88 * @throws NullPointerException if {@code rect} was {@code null} 89 */ 90 public static void convertRectF(Rect source, RectF destination) { 91 checkNotNull(source, "source must not be null"); 92 checkNotNull(destination, "destination must not be null"); 93 94 destination.left = source.left; 95 destination.right = source.right; 96 destination.bottom = source.bottom; 97 destination.top = source.top; 98 } 99 100 private ParamsUtils() { 101 throw new AssertionError(); 102 } 103} 104