001package org.kohsuke.args4j;
002
003import java.io.File;
004import java.io.IOException;
005
006import org.kohsuke.args4j.spi.OptionHandler;
007import org.kohsuke.args4j.spi.Parameters;
008import org.kohsuke.args4j.spi.Setter;
009import org.openimaj.image.FImage;
010import org.openimaj.image.ImageUtilities;
011
012
013/**
014 * An {@link OptionHandler} that can provide a {@link FImage} from
015 * a file name.
016 * 
017 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
018 */
019public class FImageOptionHandler extends OptionHandler<FImage> {
020        /**
021         * Default constructor.
022         * @param parser the parser
023         * @param option the option definition
024         * @param setter the setter
025         */
026        public FImageOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super FImage> setter) {
027                super(parser, option, setter);
028        }
029
030        @Override
031        public String getDefaultMetaVariable() {
032                return "imageFile";
033        }
034
035        @Override
036        public int parseArguments(Parameters params) throws CmdLineException {
037                File file = new File(params.getParameter(0));
038                try {
039                        setter.addValue(ImageUtilities.readF(file));
040                } catch (IOException e) {
041                        throw new CmdLineException(owner, "Error opening image file " + file, e);
042                }
043                
044                return 1;
045        }
046}