Home | History | Annotate | Download | only in utils
      1 /**
      2  * Copyright (c) 2013, Google Inc.
      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 
     17 package com.android.mail.utils;
     18 
     19 import android.graphics.Matrix;
     20 import android.graphics.Rect;
     21 import android.graphics.RectF;
     22 
     23 public class RectUtils {
     24 
     25     /**
     26      * Transform the upright full rectangle so that it bounds the original rotated image,
     27      * given by the orientation. Transform the upright partial rectangle such that it would apply
     28      * to the same region of the transformed full rectangle.
     29      *
     30      * The top-left of the transformed full rectangle will always be placed at (0, 0).
     31      * @param orientation The exif orientation (0, 90, 180, 270) of the original image. The
     32      *                    transformed full and partial rectangles will be in this orientation's
     33      *                    coordinate space.
     34      * @param fullRect    The upright full rectangle. This rectangle will be modified.
     35      * @param partialRect The upright partial rectangle. This rectangle will be modified.
     36      */
     37     public static void rotateRectForOrientation(final int orientation, final Rect fullRect,
     38             final Rect partialRect) {
     39         final Matrix matrix = new Matrix();
     40         // Exif orientation specifies how the camera is rotated relative to the actual subject.
     41         // First rotate in the opposite direction.
     42         matrix.setRotate(-orientation);
     43         final RectF fullRectF = new RectF(fullRect);
     44         final RectF partialRectF = new RectF(partialRect);
     45         matrix.mapRect(fullRectF);
     46         matrix.mapRect(partialRectF);
     47         // Then translate so that the upper left corner of the rotated full rect is at (0,0).
     48         matrix.reset();
     49         matrix.setTranslate(-fullRectF.left, -fullRectF.top);
     50         matrix.mapRect(fullRectF);
     51         matrix.mapRect(partialRectF);
     52         // Orientation transformation is complete.
     53         fullRect.set((int) fullRectF.left, (int) fullRectF.top, (int) fullRectF.right,
     54                 (int) fullRectF.bottom);
     55         partialRect.set((int) partialRectF.left, (int) partialRectF.top, (int) partialRectF.right,
     56                 (int) partialRectF.bottom);
     57     }
     58 
     59     public static void rotateRect(final int degrees, final int px, final int py, final Rect rect) {
     60         final RectF rectF = new RectF(rect);
     61         final Matrix matrix = new Matrix();
     62         matrix.setRotate(degrees, px, py);
     63         matrix.mapRect(rectF);
     64         rect.set((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom);
     65     }
     66 }
     67