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; 17 18 import java.awt.image.BufferedImage; 19 import java.io.File; 20 import java.io.IOException; 21 import java.nio.ByteBuffer; 22 import java.nio.ByteOrder; 23 import java.nio.MappedByteBuffer; 24 import java.nio.channels.FileChannel; 25 import java.nio.file.Files; 26 import java.nio.file.StandardOpenOption; 27 import java.util.EnumSet; 28 import javax.imageio.ImageIO; 29 30 /** Utility for interacting with test-specific data. */ 31 public abstract class TestUtils { 32 33 private static final float DEFAULT_IMAGE_MEAN = 127.5f; 34 private static final float DEFAULT_IMAGE_STD = 127.5f; 35 getTestFileAsBuffer(String path)36 public static MappedByteBuffer getTestFileAsBuffer(String path) { 37 try (FileChannel fileChannel = 38 (FileChannel) 39 Files.newByteChannel(new File(path).toPath(), EnumSet.of(StandardOpenOption.READ))) { 40 return fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size()); 41 } catch (IOException e) { 42 throw new RuntimeException(e); 43 } 44 } 45 supportsFilePaths()46 public static boolean supportsFilePaths() { 47 return true; 48 } 49 getTestImageAsByteBuffer(String path)50 public static ByteBuffer getTestImageAsByteBuffer(String path) { 51 File imageFile = new File(path); 52 try { 53 BufferedImage image = ImageIO.read(imageFile); 54 return toByteBuffer(image); 55 } catch (Exception e) { 56 throw new RuntimeException(e); 57 } 58 } 59 getTestImageAsFloatByteBuffer(String path)60 public static ByteBuffer getTestImageAsFloatByteBuffer(String path) { 61 File imageFile = new File(path); 62 try { 63 BufferedImage image = ImageIO.read(imageFile); 64 return toFloatByteBuffer(image); 65 } catch (Exception e) { 66 throw new RuntimeException(e); 67 } 68 } 69 toByteBuffer(BufferedImage image)70 private static ByteBuffer toByteBuffer(BufferedImage image) { 71 ByteBuffer imgData = 72 ByteBuffer.allocateDirect(image.getHeight() * image.getWidth() * 3) 73 .order(ByteOrder.nativeOrder()); 74 for (int y = 0; y < image.getHeight(); y++) { 75 for (int x = 0; x < image.getWidth(); x++) { 76 int val = image.getRGB(x, y); 77 imgData.put((byte) ((val >> 16) & 0xFF)); 78 imgData.put((byte) ((val >> 8) & 0xFF)); 79 imgData.put((byte) (val & 0xFF)); 80 } 81 } 82 return imgData; 83 } 84 toFloatByteBuffer(BufferedImage image)85 private static ByteBuffer toFloatByteBuffer(BufferedImage image) { 86 return toFloatByteBuffer(image, DEFAULT_IMAGE_MEAN, DEFAULT_IMAGE_STD); 87 } 88 toFloatByteBuffer( BufferedImage image, float imageMean, float imageStd)89 private static ByteBuffer toFloatByteBuffer( 90 BufferedImage image, float imageMean, float imageStd) { 91 ByteBuffer imgData = 92 ByteBuffer.allocateDirect(image.getHeight() * image.getWidth() * 3 * 4) 93 .order(ByteOrder.nativeOrder()); 94 for (int y = 0; y < image.getHeight(); y++) { 95 for (int x = 0; x < image.getWidth(); x++) { 96 int pixelValue = image.getRGB(x, y); 97 imgData.putFloat((((pixelValue >> 16) & 0xFF) - imageMean) / imageStd); 98 imgData.putFloat((((pixelValue >> 8) & 0xFF) - imageMean) / imageStd); 99 imgData.putFloat(((pixelValue & 0xFF) - imageMean) / imageStd); 100 } 101 } 102 return imgData; 103 } 104 TestUtils()105 private TestUtils() {} 106 } 107