/*
 * @author Ranides Atterwim {@literal <ranides@gmail.com>}
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.ui.helpers;

import lombok.experimental.UtilityClass;

import net.ranides.assira.io.ResourceUtils;
import net.ranides.assira.trace.LoggerUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.UncheckedIOException;

/**
 * Static methods for loading images
 */
@UtilityClass
public final class ImageLoader {

    /**
     * Loads image from resource in classpath.
     * It uses default classloader, resolved by {@link net.ranides.assira.reflection.util.ClassLoaderUtils#getDefault()} ()}
     *
     * @param path resource path
     * @return Image or null
     */
    public static BufferedImage fromResource(String path) {
        try {
            return ImageIO.read(ResourceUtils.url(path));
        } catch (IOException cause) {
            throw new UncheckedIOException(cause);
        }
    }

    /**
     * Loads image from resource in classpath.
     * It uses classloader assigned to specified class.
     *
     * @param loader class used for resolve classloader
     * @param path resource path
     * @return Image or null
     */
    public static Image fromResource(Class<?> loader, String path) {
        try {
            return ImageIO.read(ResourceUtils.url(loader, path));
        } catch (IOException cause) {
            throw new UncheckedIOException(cause);
        }
    }

}
