001/**
002 * 
003 */
004package icy.file;
005
006import loci.formats.gui.ExtensionFileFilter;
007
008/**
009 * Define some default image file format for Icy.
010 * 
011 * @author Stephane
012 */
013public enum ImageFileFormat
014{
015    TIFF
016    {
017        @Override
018        public String[] getExtensions()
019        {
020            return new String[] {"tif", "tiff"};
021        }
022
023        @Override
024        public String getDescription()
025        {
026            return "TIFF images";
027        }
028
029        @Override
030        public boolean canRead()
031        {
032            return true;
033        }
034
035        @Override
036        public boolean canWrite()
037        {
038            return true;
039        }
040
041        @SuppressWarnings("deprecation")
042        @Override
043        public FileFormat toFileFormat()
044        {
045            return FileFormat.TIFF;
046        }
047    },
048    PNG
049    {
050        @Override
051        public String[] getExtensions()
052        {
053            return new String[] {"png"};
054        }
055
056        @Override
057        public String getDescription()
058        {
059            return "PNG images";
060        }
061
062        @Override
063        public boolean canRead()
064        {
065            return true;
066        }
067
068        @Override
069        public boolean canWrite()
070        {
071            return true;
072        }
073
074        @SuppressWarnings("deprecation")
075        @Override
076        public FileFormat toFileFormat()
077        {
078            return FileFormat.PNG;
079        }
080    },
081    LSM
082    {
083        @Override
084        public String[] getExtensions()
085        {
086            return new String[] {"lsm"};
087        }
088
089        @Override
090        public String getDescription()
091        {
092            return "LSM images";
093        }
094
095        @Override
096        public boolean canRead()
097        {
098            return true;
099        }
100
101        @Override
102        public boolean canWrite()
103        {
104            return false;
105        }
106
107        @SuppressWarnings("deprecation")
108        @Override
109        public FileFormat toFileFormat()
110        {
111            return FileFormat.LSM;
112        }
113    },
114    JPG
115    {
116        @Override
117        public String[] getExtensions()
118        {
119            return new String[] {"jpg", "jpeg"};
120        }
121
122        @Override
123        public String getDescription()
124        {
125            return "JPG images";
126        }
127
128        @Override
129        public boolean canRead()
130        {
131            return true;
132        }
133
134        @Override
135        public boolean canWrite()
136        {
137            return true;
138        }
139
140        @SuppressWarnings("deprecation")
141        @Override
142        public FileFormat toFileFormat()
143        {
144            return FileFormat.JPG;
145        }
146    },
147    AVI
148    {
149        @Override
150        public String[] getExtensions()
151        {
152            return new String[] {"avi"};
153        }
154
155        @Override
156        public String getDescription()
157        {
158            return "AVI sequences";
159        }
160
161        @Override
162        public boolean canRead()
163        {
164            return true;
165        }
166
167        @Override
168        public boolean canWrite()
169        {
170            return true;
171        }
172
173        @SuppressWarnings("deprecation")
174        @Override
175        public FileFormat toFileFormat()
176        {
177            return FileFormat.AVI;
178        }
179    };
180
181    /**
182     * Returns true if the image file format supports read operation.
183     */
184    public abstract boolean canRead();
185
186    /**
187     * Returns true if the image file format supports write operation.
188     */
189    public abstract boolean canWrite();
190
191    /**
192     * Returns the image file format description.
193     */
194    public String getDescription()
195    {
196        return "unknow";
197    }
198
199    /**
200     * Returns the image file format extensions.
201     */
202    public String[] getExtensions()
203    {
204        return new String[] {""};
205    }
206
207    /**
208     * For backward compatibility with {@link FileFormat}.
209     */
210    @SuppressWarnings({"deprecation", "javadoc"})
211    public abstract FileFormat toFileFormat();
212
213    /**
214     * Returns the associated {@link ExtensionFileFilter}
215     */
216    public ExtensionFileFilter getExtensionFileFilter()
217    {
218        return new ExtensionFileFilter(getExtensions(), getDescription());
219    }
220
221    /**
222     * Return true if the specified extension matches this format.<br>
223     * <code>defaultValue</code> is returned if no matching format is found (it can be null).
224     */
225    public boolean matches(String ext)
226    {
227        if (ext == null)
228            return false;
229
230        // always consider lower case extension
231        final String extLC = ext.toLowerCase();
232
233        for (String e : getExtensions())
234            if (e.equals(extLC))
235                return true;
236
237        return false;
238    }
239
240    /**
241     * Returns the FileFormat corresponding to specified extension.<br>
242     * <code>defaultValue</code> is returned if no matching format is found.
243     */
244    public static ImageFileFormat getFormat(String ext, ImageFileFormat defaultValue)
245    {
246        for (ImageFileFormat iff : values())
247            if (iff.matches(ext))
248                return iff;
249
250        return defaultValue;
251    }
252
253    /**
254     * Returns the {@link ImageFileFormat} corresponding to the specified extension and which
255     * support read operation.<br>
256     * <code>defaultValue</code> is returned if no matching format is found.
257     */
258    public static ImageFileFormat getReadFormat(String ext, ImageFileFormat defaultValue)
259    {
260        for (ImageFileFormat iff : values())
261            if (iff.canRead() && iff.matches(ext))
262                return iff;
263
264        return defaultValue;
265    }
266
267    /**
268     * Returns the {@link ImageFileFormat} corresponding to the specified extension and which
269     * support write operation.<br>
270     * <code>defaultValue</code> is returned if no matching format is found.
271     */
272    public static ImageFileFormat getWriteFormat(String ext, ImageFileFormat defaultValue)
273    {
274        for (ImageFileFormat iff : values())
275            if (iff.canWrite() && iff.matches(ext))
276                return iff;
277
278        return defaultValue;
279    }
280
281    /**
282     * For backward compatibility with {@link FileFormat}.
283     */
284    @SuppressWarnings({"deprecation", "javadoc"})
285    public static ImageFileFormat getFormat(FileFormat format)
286    {
287        switch (format)
288        {
289            case AVI:
290                return AVI;
291            case JPG:
292                return JPG;
293            case LSM:
294                return LSM;
295            case PNG:
296                return PNG;
297            default:
298                return TIFF;
299        }
300    }
301}