Path: blob/master/SLICK_HOME/src/org/newdawn/slick/imageout/ImageOut.java
1461 views
package org.newdawn.slick.imageout;12import java.io.FileOutputStream;3import java.io.IOException;4import java.io.OutputStream;56import org.newdawn.slick.Image;7import org.newdawn.slick.SlickException;89/**10* A static hook to access all the Image output utilities. The list of format strings11* provided is not the limit of capability. These are provided for utility, use @see {@link #getSupportedFormats()}12* for a full list of supported formats.13*14* @author kevin15*/16public class ImageOut {17/** The default setting for writing out the alpha channel */18private static final boolean DEFAULT_ALPHA_WRITE = false;1920/** The format string for TGA */21public static String TGA = "tga";22/** The format string for PNG */23public static String PNG = "png";24/** The format string for JPG */25public static String JPG = "jpg";2627/**28* Get a list of supported formats29*30* @see ImageWriterFactory#getSupportedFormats()31* @return The list of supported format strings32*/33public static String[] getSupportedFormats() {34return ImageWriterFactory.getSupportedFormats();35}3637/**38* Write an image out to a specified output stream39*40* @param image The image to write out to41* @param format The format to write the image out in42* @param out The output stream to which the image should be written43* @throws SlickException Indicates a failure to write the image in the specified format44*/45public static void write(Image image, String format, OutputStream out) throws SlickException {46write(image, format, out, DEFAULT_ALPHA_WRITE);47}4849/**50* Write an image out to a specified output stream51*52* @param image The image to write out to53* @param format The format to write the image out in54* @param out The output stream to which the image should be written55* @param writeAlpha True if we should write the alpha channel out (some formats don't support this, like JPG)56* @throws SlickException Indicates a failure to write the image in the specified format57*/58public static void write(Image image, String format, OutputStream out, boolean writeAlpha) throws SlickException {59try {60ImageWriter writer = ImageWriterFactory.getWriterForFormat(format);61writer.saveImage(image, format, out, writeAlpha);62} catch (IOException e) {63throw new SlickException("Unable to write out the image in format: "+format, e);64}65}6667/**68* Write an image out to a file on the local file system. The format of the output69* is determined based on the file name extension70*71* @param image The image to be written out72* @param dest The destination path to write to73* @throws SlickException Indicates a failure to write the image in the determined format74*/75public static void write(Image image, String dest) throws SlickException {76write(image, dest, DEFAULT_ALPHA_WRITE);77}7879/**80* Write an image out to a file on the local file system. The format of the output81* is determined based on the file name extension82*83* @param image The image to be written out84* @param dest The destination path to write to85* @param writeAlpha True if we should write the alpha channel out (some formats don't support this, like JPG)86* @throws SlickException Indicates a failure to write the image in the determined format87*/88public static void write(Image image, String dest, boolean writeAlpha) throws SlickException {89try {90int ext = dest.lastIndexOf('.');91if (ext < 0) {92throw new SlickException("Unable to determine format from: "+dest);93}9495String format = dest.substring(ext+1);96write(image, format, new FileOutputStream(dest), writeAlpha);97} catch (IOException e) {98throw new SlickException("Unable to write to the destination: "+dest, e);99}100}101102/**103* Write an image out to a file on the local file system.104*105* @param image The image to be written out106* @param format The format to write the image out in107* @param dest The destination path to write to108* @throws SlickException Indicates a failure to write the image in the determined format109*/110public static void write(Image image, String format, String dest) throws SlickException {111write(image, format, dest, DEFAULT_ALPHA_WRITE);112}113114/**115* Write an image out to a file on the local file system.116*117* @param image The image to be written out118* @param format The format to write the image out in119* @param dest The destination path to write to120* @param writeAlpha True if we should write the alpha channel out (some formats don't support this, like JPG)121* @throws SlickException Indicates a failure to write the image in the determined format122*/123public static void write(Image image, String format, String dest, boolean writeAlpha) throws SlickException {124try {125write(image, format, new FileOutputStream(dest), writeAlpha);126} catch (IOException e) {127throw new SlickException("Unable to write to the destination: "+dest, e);128}129}130}131132133