1 /*
2  * Copyright (C) 2017 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 package com.android.wallpaper.asset;
17 
18 import android.graphics.Point;
19 import android.graphics.Rect;
20 import android.media.ExifInterface;
21 import android.util.Log;
22 
23 /**
24  * Rotates crop rectangles for bitmap region operations on rotated images (i.e., with non-normal
25  * EXIF orientation).
26  */
27 public class CropRectRotator {
28 
29     private static final String TAG = "CropRectRotator";
30 
31     /**
32      * Rotates and returns a new crop Rect which is adjusted for the provided EXIF orientation value.
33      */
rotateCropRectForExifOrientation(Point dimensions, Rect srcRect, int exifOrientation)34     public static Rect rotateCropRectForExifOrientation(Point dimensions, Rect srcRect,
35                                                         int exifOrientation) {
36 
37         switch (exifOrientation) {
38             case ExifInterface.ORIENTATION_NORMAL:
39                 return new Rect(srcRect);
40             case ExifInterface.ORIENTATION_ROTATE_90:
41                 return new Rect(srcRect.top, dimensions.x - srcRect.right, srcRect.bottom,
42                         dimensions.x - srcRect.left);
43             case ExifInterface.ORIENTATION_ROTATE_180:
44                 return new Rect(dimensions.x - srcRect.right, dimensions.y - srcRect.bottom,
45                         dimensions.x - srcRect.left, dimensions.y - srcRect.top);
46             case ExifInterface.ORIENTATION_ROTATE_270:
47                 return new Rect(dimensions.y - srcRect.bottom, srcRect.left, dimensions.y - srcRect.top,
48                         srcRect.right);
49             default:
50                 Log.w(TAG, "Unsupported EXIF orientation " + exifOrientation);
51                 return new Rect(srcRect);
52         }
53     }
54 }
55