001 /*
002 * Created on Jan 25, 2011
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005 * the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011 * specific language governing permissions and limitations under the License.
012 *
013 * Copyright @2011 the original author or authors.
014 */
015 package org.fest.assertions.util;
016
017 import java.awt.image.BufferedImage;
018 import java.io.*;
019
020 import javax.imageio.ImageIO;
021
022 /**
023 * Reads an image from a file.
024 *
025 * @author Yvonne Wang
026 */
027 public final class ImageReader {
028
029 /**
030 * Decodes the image in the file at the specified path.
031 * @param path the path of the image to read.
032 * @return the read image.
033 * @throws NullPointerException if the given path is {@code null}.
034 * @throws IllegalArgumentException if the given path does not belong to a file.
035 * @throws IOException if an error occurs during reading.
036 */
037 public static BufferedImage readImageFrom(String path) throws IOException {
038 if (path == null) throw new NullPointerException("The path of the image to read should not be null");
039 File file = new File(path);
040 if (!file.isFile())
041 throw new IllegalArgumentException(String.format("The path '%s' does not belong to a file", path));
042 return ImageIO.read(file);
043 }
044
045 private ImageReader() {}
046 }