Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / imageio / ImageWriter.java
blob389ca4360a703ef718f0815c6e4c6dd90083a88f
1 /* ImageWriter.java -- Encodes raster images.
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.imageio;
41 import java.awt.Dimension;
42 import java.io.IOException;
43 import java.util.ArrayList;
44 import java.util.Iterator;
45 import java.util.List;
46 import java.util.Locale;
48 import javax.imageio.event.IIOWriteProgressListener;
49 import javax.imageio.event.IIOWriteWarningListener;
50 import javax.imageio.metadata.IIOMetadata;
52 import javax.imageio.spi.ImageWriterSpi;
54 public abstract class ImageWriter
55 implements ImageTranscoder
57 private boolean aborted;
59 protected Locale[] availableLocales;
60 protected Locale locale;
61 protected ImageWriterSpi originatingProvider;
62 protected Object output;
63 protected List progressListeners = new ArrayList();
64 protected List warningListeners = new ArrayList();
65 protected List warningLocales = new ArrayList();
67 protected ImageWriter(ImageWriterSpi originatingProvider)
69 this.originatingProvider = originatingProvider;
72 private void checkOutputSet()
74 if (output == null)
75 throw new IllegalStateException("no output set");
78 public void abort()
80 aborted = true;
83 protected boolean abortRequested()
85 return aborted;
88 public void addIIOWriteProgressListener(IIOWriteProgressListener listener)
90 if (listener == null)
91 return;
93 progressListeners.add(listener);
96 public void addIIOWriteWarningListener (IIOWriteWarningListener listener)
98 if (listener == null)
99 return;
101 warningListeners.add(listener);
104 public boolean canInsertEmpty(int imageIndex)
105 throws IOException
107 checkOutputSet();
108 return false;
111 public boolean canInsertImage(int imageIndex)
112 throws IOException
114 checkOutputSet();
115 return false;
118 public boolean canRemoveImage(int imageIndex)
119 throws IOException
121 checkOutputSet();
122 return false;
125 public boolean canReplaceImageMetadata(int imageIndex)
126 throws IOException
128 checkOutputSet();
129 return false;
132 public boolean canReplacePixels(int imageIndex)
133 throws IOException
135 checkOutputSet();
136 return false;
139 public boolean canReplaceStreamMetadata()
140 throws IOException
142 checkOutputSet();
143 return false;
146 public boolean canWriteEmpty()
147 throws IOException
149 checkOutputSet();
150 return false;
153 public boolean canWriteRasters()
155 return false;
158 public boolean canWriteSequence()
160 return false;
163 protected void clearAbortRequest()
165 aborted = false;
168 public abstract IIOMetadata convertImageMetadata (IIOMetadata inData,
169 ImageTypeSpecifier imageType,
170 ImageWriteParam param);
172 public abstract IIOMetadata convertStreamMetadata (IIOMetadata inData,
173 ImageWriteParam param);
175 public void dispose()
177 // The default implementation is empty. Subclasses have to overwrite it.
180 public Locale[] getAvailableLocales()
182 return availableLocales;
185 public abstract IIOMetadata getDefaultImageMetadata (ImageTypeSpecifier imageType, ImageWriteParam param);
187 public abstract IIOMetadata getDefaultStreamMetadata (ImageWriteParam param);
189 public ImageWriteParam getDefaultWriteParam()
191 return new ImageWriteParam(getLocale());
194 public Locale getLocale()
196 return locale;
199 public int getNumThumbnailsSupported (ImageTypeSpecifier imageType, ImageWriteParam param,
200 IIOMetadata streamMetadata, IIOMetadata imageMetadata)
202 return 0;
205 public ImageWriterSpi getOriginatingProvider()
207 return originatingProvider;
210 public Object getOutput()
212 return output;
215 public Dimension[] getPreferredThumbnailSizes (ImageTypeSpecifier imageType,
216 ImageWriteParam param,
217 IIOMetadata streamMetadata,
218 IIOMetadata imageMetadata)
220 return null;
223 protected void processImageComplete()
225 Iterator it = progressListeners.iterator();
227 while (it.hasNext())
229 IIOWriteProgressListener listener = (IIOWriteProgressListener) it.next();
230 listener.imageComplete(this);
234 protected void processImageProgress(float percentageDone)
236 Iterator it = progressListeners.iterator();
238 while (it.hasNext())
240 IIOWriteProgressListener listener = (IIOWriteProgressListener) it.next();
241 listener.imageProgress(this, percentageDone);
245 protected void processImageStarted(int imageIndex)
247 Iterator it = progressListeners.iterator();
249 while (it.hasNext())
251 IIOWriteProgressListener listener = (IIOWriteProgressListener) it.next();
252 listener.imageStarted(this, imageIndex);
256 protected void processThumbnailComplete()
258 Iterator it = progressListeners.iterator();
260 while (it.hasNext())
262 IIOWriteProgressListener listener = (IIOWriteProgressListener) it.next();
263 listener.thumbnailComplete(this);
267 protected void processThumbnailProgress(float percentageDone)
269 Iterator it = progressListeners.iterator();
271 while (it.hasNext())
273 IIOWriteProgressListener listener = (IIOWriteProgressListener) it.next();
274 listener.thumbnailProgress(this, percentageDone);
278 protected void processThumbnailStarted(int imageIndex, int thumbnailIndex)
280 Iterator it = progressListeners.iterator();
282 while (it.hasNext())
284 IIOWriteProgressListener listener = (IIOWriteProgressListener) it.next();
285 listener.thumbnailStarted(this, imageIndex, thumbnailIndex);
289 protected void processWarningOccurred(int imageIndex, String warning)
291 Iterator it = warningListeners.iterator();
293 while (it.hasNext())
295 IIOWriteWarningListener listener = (IIOWriteWarningListener) it.next();
296 listener.warningOccurred(this, imageIndex, warning);
300 protected void processWriteAborted()
302 Iterator it = progressListeners.iterator();
304 while (it.hasNext())
306 IIOWriteProgressListener listener = (IIOWriteProgressListener) it.next();
307 listener.writeAborted(this);
311 public void removeAllIIOWriteProgressListeners()
313 progressListeners.clear();
316 public void removeAllIIOWriteWarningListeners()
318 progressListeners.clear();
321 public void removeIIOWriteProgressListener (IIOWriteProgressListener listener)
323 if (listener == null)
324 return;
326 progressListeners.remove(listener);
329 public void removeIIOWriteWarningListener (IIOWriteWarningListener listener)
331 if (listener == null)
332 return;
334 warningListeners.remove(listener);
337 public void reset()
339 setOutput(null);
340 setLocale(null);
341 removeAllIIOWriteWarningListeners();
342 removeAllIIOWriteProgressListeners();
343 clearAbortRequest();
346 public void setLocale(Locale locale)
348 if (locale != null)
350 // Check if its a valid locale.
351 boolean found = false;
353 if (availableLocales != null)
354 for (int i = availableLocales.length - 1; i >= 0; --i)
355 if (availableLocales[i].equals(locale))
356 found = true;
358 if (! found)
359 throw new IllegalArgumentException("looale not available");
362 this.locale = locale;
365 public void setOutput(Object output)
367 if (output != null)
369 // Check if its a valid output object.
370 boolean found = false;
371 Class[] types = null;
373 if (originatingProvider != null)
374 types = originatingProvider.getOutputTypes();
376 if (types != null)
377 for (int i = types.length - 1; i >= 0; --i)
378 if (types[i].isInstance(output))
379 found = true;
381 if (! found)
382 throw new IllegalArgumentException("output type not available");
385 this.output = output;
388 public abstract void write (IIOMetadata streamMetadata, IIOImage image, ImageWriteParam param)
389 throws IOException;