/* * Moonlight|3D Copyright (C) 2005 The Moonlight|3D team * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Created on Dec 9, 2005 */ package ml.image; import java.util.ArrayList; import java.util.List; import ml.core.exceptions.Exception; import ml.core.helper.NamePool; /** * This class keeps track of all supported image file formats and * wraps loading and saving images through the registered ImageFormat * handlers. * * @author gregor */ public class FormatManager { private ArrayList imageFormats; private NamePool formatPool; /** * Simple constructor. */ public FormatManager() { imageFormats=new ArrayList(); formatPool=new NamePool(); } /** * Register a new image file format. * * @param format the image file format to register * @throws Exception */ public void registerImageFormat(ImageFormat format) throws Exception { if(!formatPool.isNameUnique(format.getName())) { throw new Exception("image format name is not unique"); } imageFormats.add(format); formatPool.add(format); } /** * Unregister the image file format handle for the given file * format name. * * @param formatName the unique name of the image file format * @throws Exception */ public void unregisterImageFormat(String formatName) throws Exception { for(int i=0;i getFormats() { return (ArrayList) imageFormats.clone(); } /** * Load an image from the file with the given filename through the * image file format handler for the given image file format name. * * @param filename the file name to load the image from * @param formatName the image file format to expect * @return the framebuffer with the loaded image * @throws Exception */ public Framebuffer loadImage(String filename, String formatName) throws Exception { Framebuffer image = null; ImageFormat neededFormat=null; for (ImageFormat format : imageFormats) { if (format.getName().equals(formatName)) { neededFormat = format; } } if (neededFormat == null) { throw new Exception("speficfied image format not found"); } image = neededFormat.loadFile(filename); return image; } /** * Load an image from the given file. Try to guess the file format * from the file extension. * * @param filename the name of the file to load * @return the framebuffer with the loaded image * @throws Exception */ public Framebuffer loadImage(String filename) throws Exception { Framebuffer image = null; for (ImageFormat format : imageFormats) { if(format.getFileType().matches(filename)) { try { image=format.loadFile(filename); return image; } catch(Exception e) { // do nothing here, the catch statement is here to skip the return above } } } throw new Exception("unable to detect image format"); } /** * Save the given framebuffer as an image to the file with the given name * using the given image file format. * * @param image the framebuffer containing the image to be saved * @param filename the name of the file the image is to be saved to * @param formatName the name of the file format to use * @throws Exception */ public void saveImage(Framebuffer image, String filename, String formatName) throws Exception { ImageFormat neededFormat=null; for(ImageFormat format : imageFormats) { if(format.getName().equals(formatName)) { neededFormat=format; } } if(neededFormat==null) { throw new Exception("speficfied image format not found"); } neededFormat.saveFile(filename,image); } }