1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 package org.tensorflow.lite.support.image.ops; 17 18 import android.graphics.Bitmap; 19 import android.graphics.PointF; 20 import org.checkerframework.checker.nullness.qual.NonNull; 21 import org.tensorflow.lite.support.image.ImageOperator; 22 import org.tensorflow.lite.support.image.TensorImage; 23 24 /** 25 * As a computation unit for processing images, it can resize an image to user-specified size. 26 * 27 * <p>It interpolates pixels when image is stretched, and discards pixels when image is compressed. 28 * 29 * @see ResizeWithCropOrPadOp for resizing without content distortion. 30 */ 31 public class ResizeOp implements ImageOperator { 32 33 /** Algorithms for resizing. */ 34 public enum ResizeMethod { 35 BILINEAR, 36 NEAREST_NEIGHBOR 37 } 38 39 private final int targetHeight; 40 private final int targetWidth; 41 private final boolean useBilinear; 42 43 /** 44 * Creates a ResizeOp which can resize images to specified size in specified method. 45 * 46 * @param targetHeight: The expected height of resized image. 47 * @param targetWidth: The expected width of resized image. 48 * @param resizeMethod: The algorithm to use for resizing. Options: {@link ResizeMethod} 49 */ ResizeOp(int targetHeight, int targetWidth, ResizeMethod resizeMethod)50 public ResizeOp(int targetHeight, int targetWidth, ResizeMethod resizeMethod) { 51 this.targetHeight = targetHeight; 52 this.targetWidth = targetWidth; 53 useBilinear = (resizeMethod == ResizeMethod.BILINEAR); 54 } 55 56 /** 57 * Applies the defined resizing on given image and returns the result. 58 * 59 * <p>Note: the content of input {@code image} will change, and {@code image} is the same instance 60 * with the output. 61 * 62 * @param image input image. 63 * @return output image. 64 */ 65 @Override 66 @NonNull apply(@onNull TensorImage image)67 public TensorImage apply(@NonNull TensorImage image) { 68 Bitmap scaled = 69 Bitmap.createScaledBitmap(image.getBitmap(), targetWidth, targetHeight, useBilinear); 70 image.load(scaled); 71 return image; 72 } 73 74 @Override getOutputImageHeight(int inputImageHeight, int inputImageWidth)75 public int getOutputImageHeight(int inputImageHeight, int inputImageWidth) { 76 return targetHeight; 77 } 78 79 @Override getOutputImageWidth(int inputImageHeight, int inputImageWidth)80 public int getOutputImageWidth(int inputImageHeight, int inputImageWidth) { 81 return targetWidth; 82 } 83 84 @Override inverseTransform(PointF point, int inputImageHeight, int inputImageWidth)85 public PointF inverseTransform(PointF point, int inputImageHeight, int inputImageWidth) { 86 return new PointF( 87 point.x * inputImageWidth / targetWidth, point.y * inputImageHeight / targetHeight); 88 } 89 } 90