File: ImageLoader.java

package info (click to toggle)
java-imaging-utilities 0.14.3-7
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,556 kB
  • sloc: java: 31,233; python: 71; xml: 31; makefile: 26; sh: 5
file content (402 lines) | stat: -rw-r--r-- 10,637 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
 * ImageLoader
 *
 * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2006 Marco Schmidt.
 * All rights reserved.
 */

package net.sourceforge.jiu.codecs;

import java.awt.Frame;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import net.sourceforge.jiu.codecs.InvalidFileStructureException;
import net.sourceforge.jiu.codecs.InvalidImageIndexException;
import net.sourceforge.jiu.codecs.WrongFileFormatException;
import net.sourceforge.jiu.codecs.BMPCodec;
import net.sourceforge.jiu.codecs.IFFCodec;
import net.sourceforge.jiu.codecs.PCDCodec;
import net.sourceforge.jiu.codecs.PNGCodec;
import net.sourceforge.jiu.codecs.PNMCodec;
import net.sourceforge.jiu.codecs.PSDCodec;
import net.sourceforge.jiu.codecs.RASCodec;
import net.sourceforge.jiu.codecs.tiff.TIFFCodec;
import net.sourceforge.jiu.data.PixelImage;
import net.sourceforge.jiu.gui.awt.ImageCreator;
import net.sourceforge.jiu.ops.MissingParameterException;
import net.sourceforge.jiu.ops.OperationFailedException;

/**
 * A convenience class with static methods to load images from files using JIU codecs.
 * The load methods of this class try to load an image with all codecs  registered with this class.
 * This includes almost every codec that resides in the <code>net.sourceforge.jiu.codecs</code> package.
 * You can register additional codecs with {@link #registerCodecClass} or remove the usage
 * of codecs with {@link #removeCodecClass}.
 * <p>
 * A Codec that cannot safely identify a file to be in the format that it supports must not be used with ImageLoader.
 * The failure to identify typically comes from the lack of magic byte sequences defined for the format.
 * In order to load such a file, use the codec manually.
 * Example: {@link PalmCodec}.
 * <p>
 * In order to load an image via {@link java.awt.Toolkit} (JPEG, PNG or GIF), use
 * {@link net.sourceforge.jiu.gui.awt.ToolkitLoader}.
 * It combines the loading features of java.awt.Toolkit and JIU's ImageLoader.
 * <h3>Usage example</h3>
 * <pre>
 * PixelImage image = null;
 * try
 * {
 *   image = ImageLoader.load("image.tif");
 * }
 * catch (Exception e)
 * {
 *   // handle exception
 * }
 * </pre>
 * @author Marco Schmidt
 */
public class ImageLoader
{
	// all elements of class String
	private static Vector fileExtensions;
	private static Vector imageCodecClasses;

	static
	{
		imageCodecClasses = new Vector();
		registerCodecClass(new BMPCodec());
		registerCodecClass(new IFFCodec());
		registerCodecClass(new PCDCodec());
		registerCodecClass(new PNGCodec());
		registerCodecClass(new PNMCodec());
		registerCodecClass(new PSDCodec());
		registerCodecClass(new RASCodec());
		registerCodecClass(new TIFFCodec());
	}

	private ImageLoader()
	{
	}

	/**
	 * Creates an instance of one of the codec classes known to ImageLoader.
	 * @param index 0-based index of codec number, maximum value is {@link #getNumCodecs}<code> - 1</code>
	 * @return new codec object or <code>null</code> if no object could be instantiated
	 */
	public static ImageCodec createCodec(int index)
	{
		ImageCodec result = null;
		if (index >= 0 && index < getNumCodecs())
		{
			Class c = (Class)imageCodecClasses.elementAt(index);
			try
			{
				Object obj = c.newInstance();
				if (obj != null && obj instanceof ImageCodec)
				{
					result = (ImageCodec)obj;
				}
			}
			catch (IllegalAccessException iae)
			{
				// ignore
			}
			catch (InstantiationException ie)
			{
				// ignore
			}
		}
		return result;
	}

	/**
	 * Returns a filename filter ({@link java.io.FilenameFilter}) that accepts files
	 * with name extensions typical for the image file formats known to ImageLoader.
	 * The filter could then be used in an file dialog like {@link java.awt.FileDialog}.
	 * <p>
	 * Note that this filter does not include file formats supported by the AWT 
	 * {@link java.awt.Toolkit} (GIF and JPEG, also PNG since Java 1.3).
	 * @return filter for image file names
	 */
	public static FilenameFilter createFilenameFilter()
	{
		return new FilenameFilter()
		{
			public boolean accept(File dir, String name)
			{
				if (name == null)
				{
					return false;
				}
				if (fileExtensions == null)
				{
					updateFileExtensions();
				}
				name = name.toLowerCase();
				int index = 0;
				while (index < fileExtensions.size())
				{
					String ext = (String)fileExtensions.elementAt(index++);
					if (name.endsWith(ext))
					{
						return true;
					}
				}
				return false;
			}
		};
	}

	/**
	 * Returns the number of codec classes currently known to ImageLoader.
	 * This number can be changed by registering additional codecs
	 * ({@link #registerCodecClass})
	 * or by removing codec classes ({@link #removeCodecClass}).
	 * @return number of known codec classes
	 */
	public static int getNumCodecs()
	{
		return imageCodecClasses.size();
	}

	/**
	 * Attempts to load an image from a file.
	 * @param file the file from which an image is to be loaded
	 * @return the image on success or <code>null</code> on failure
	 */
	public static PixelImage load(File file) throws
		IOException, 
		InvalidFileStructureException,
		InvalidImageIndexException,
		UnsupportedTypeException
	{
		return load(file, null);
	}

	/**
	 * Attempts to load an image from a file, notifying the
	 * argument progress listeners.
	 * @param file the file to load an image from
	 * @param listeners a Vector of ProgressListener objects to be notified 
	 * @return an instance of a class implementing {@link PixelImage}
	 */
	public static PixelImage load(File file, Vector listeners) throws 
		IOException, 
		InvalidFileStructureException,
		InvalidImageIndexException,
		UnsupportedTypeException
	{
		for (int i = 0; i < getNumCodecs(); i++)
		{
			PixelImage result = null;
			ImageCodec codec = null;
			try
			{
				codec = createCodec(i);
				codec.setFile(file, CodecMode.LOAD);
				codec.addProgressListeners(listeners);
				codec.process();
				result = codec.getImage();
				if (result != null)
				{
					return result;
				}
			}
			catch (MissingParameterException mpe)
			{
				// ignore
			}
			catch (WrongFileFormatException wffe)
			{
				// ignore
			}
			catch (IOException ioe)
			{
				// ignore
			}
			catch (OperationFailedException ofe)
			{
				// ignore
				//System.out.println("codec: " + ofe);
			}
			finally
			{
				if (codec != null)
				{
					codec.close();
				}
			}
		}
		return null;
	}

	/**
	 * Load an image from a file given by its name.
	 * Simply calls load(fileName, null).
	 * @param fileName name of the file from which an image is to be loaded
	 * @return the loaded image on success, null on failure
	 */
	public static PixelImage load(String fileName) throws
		IOException,
		InvalidFileStructureException,
		InvalidImageIndexException,
		UnsupportedTypeException
	{
		return load(fileName, null);
	}

	/**
	 * Attempts to load an image from the file with the given name, 
	 * using the given list of progress listeners.
	 * @param fileName name of the file from which an image is to be loaded
	 * @param listeners a list of objects implementing ProgressListener
	 * @return the loaded image
	 */
	public static PixelImage load(String fileName, Vector listeners) throws
		IOException,
		InvalidFileStructureException,
		InvalidImageIndexException,
		UnsupportedTypeException
	{
		return load(new File(fileName), listeners);
	}

	public static PixelImage loadToolkitImageUri(String uri) throws 
	IOException, 
	InvalidFileStructureException,
	InvalidImageIndexException,
	UnsupportedTypeException
	{
		try
		{
			ImageLoader loader = new ImageLoader();
			InputStream in = loader.getClass().getResourceAsStream(uri);
			if (in == null)
			{
				return null;
			}
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			int b;
			while ((b = in.read()) != -1)
			{
				out.write(b);
			}
			in.close();
			byte[] data = out.toByteArray();
			java.awt.Image awtImage = Toolkit.getDefaultToolkit().createImage(data);
			MediaTracker mediaTracker = new MediaTracker(new Frame());
			mediaTracker.addImage(awtImage, 0);
			try
			{
				mediaTracker.waitForID(0);
			}
			catch (InterruptedException ie)
			{
				return null;
			}
			PixelImage image = ImageCreator.convertImageToRGB24Image(awtImage);
			return image;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * Registers a codec class with ImageLoader.
	 * The argument is an instance of the class to be registered.
	 * Note that the codec class must have an empty constructor.
	 * <p>
	 * Example: let's say you have written a new codec called ACMEImageCodec.
	 * Your codec supports loading images.
	 * Then you could register it like that:
	 * <pre>
	 * ImageLoader.registerCodecClass(new ACMEImageCodec());
	 * </pre>
	 * <p>
	 * @param codec an instance of the codec class to be registered
	 */
	public static void registerCodecClass(ImageCodec codec)
	{
		if (codec == null)
		{
			return;
		}
		if (imageCodecClasses.contains(codec.getClass()))
		{
			return;
		}
		if (!codec.isLoadingSupported())
		{
			throw new IllegalArgumentException("Codec does not support loading.");
		}
		imageCodecClasses.addElement(codec.getClass());
		updateFileExtensions();
	}

	/**
	 * Removes all codec classes from the internal list of codec classes.
	 * After a call to this method, ImageLoader will not load anything unless
	 * new codecs get registered.
	 */
	public static void removeAllCodecClasses()
	{
		imageCodecClasses = new Vector();
		updateFileExtensions();
	}

	/**
	 * Removes a codec class from the internal list of codec classes.
	 * @param codec an instance of the codec class to be removed
	 */
	public static void removeCodecClass(ImageCodec codec)
	{
		if (codec == null)
		{
			return;
		}
		int index = imageCodecClasses.indexOf(codec.getClass());
		if (index != -1)
		{
			imageCodecClasses.remove(index);
			updateFileExtensions();
		}
		else
		{
			throw new IllegalArgumentException("The argument codec's class " +
				"could not be found in the internal list of codec classes.");
		}
	}

	private static void updateFileExtensions()
	{
		fileExtensions = new Vector();
		int index = 0;
		while (index < getNumCodecs())
		{
			try
			{
				ImageCodec codec = createCodec(index++);
				String[] extArray = codec.getFileExtensions();
				if (extArray != null && extArray.length > 0)
				{
					for (int i = 0; i < extArray.length; i++)
					{
						fileExtensions.addElement(extArray[i].toLowerCase());
					}
				}
			}
			catch (Exception e)
			{
			}
		}
	}
}