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