OSDN Git Service

Initial revision
[pf3gnuchains/gcc-fork.git] / libjava / classpath / javax / imageio / ImageWriter.java
1 /* ImageWriter.java -- Encodes raster images.
2    Copyright (C) 2004  Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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.
10
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.
15
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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
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.
25
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. */
37
38
39 package javax.imageio;
40
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;
47
48 import javax.imageio.event.IIOWriteProgressListener;
49 import javax.imageio.event.IIOWriteWarningListener;
50 import javax.imageio.metadata.IIOMetadata;
51
52 import javax.imageio.spi.ImageWriterSpi;
53
54 public abstract class ImageWriter
55   implements ImageTranscoder
56 {
57   private boolean aborted;
58   
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();
66
67   protected ImageWriter(ImageWriterSpi originatingProvider)
68   {
69     this.originatingProvider = originatingProvider;
70   }
71
72   private void checkOutputSet()
73   {
74     if (output == null)
75       throw new IllegalStateException("no output set");
76   }
77   
78   public void abort()
79   {
80     aborted = true;
81   }
82
83   protected boolean abortRequested()
84   {
85     return aborted;
86   }
87
88   public void addIIOWriteProgressListener(IIOWriteProgressListener listener)
89   {
90     if (listener == null)
91       return;
92     
93     progressListeners.add(listener);
94   }
95   
96   public void addIIOWriteWarningListener (IIOWriteWarningListener listener)
97   {
98     if (listener == null)
99       return;
100     
101     warningListeners.add(listener);
102   }
103
104   public boolean canInsertEmpty(int imageIndex)
105     throws IOException
106   {
107     checkOutputSet();
108     return false;
109   }
110
111   public boolean canInsertImage(int imageIndex)
112     throws IOException
113   {
114     checkOutputSet();
115     return false;
116   }
117
118   public boolean canRemoveImage(int imageIndex)
119     throws IOException
120   {
121     checkOutputSet();
122     return false;
123   }
124
125   public boolean canReplaceImageMetadata(int imageIndex)
126     throws IOException
127   {
128     checkOutputSet();
129     return false;
130   }
131
132   public boolean canReplacePixels(int imageIndex)
133     throws IOException
134   {
135     checkOutputSet();
136     return false;
137   }
138
139   public boolean canReplaceStreamMetadata()
140     throws IOException
141   {
142     checkOutputSet();
143     return false;
144   }
145
146   public boolean canWriteEmpty()
147     throws IOException
148   {
149     checkOutputSet();
150     return false;
151   }
152
153   public boolean canWriteRasters()
154   {
155     return false;
156   }
157
158   public boolean canWriteSequence()
159   {
160     return false;
161   }
162
163   protected void clearAbortRequest()
164   {
165     aborted = false;
166   }
167   
168   public abstract IIOMetadata convertImageMetadata (IIOMetadata inData,
169                                                     ImageTypeSpecifier imageType,
170                                                     ImageWriteParam param);
171
172   public abstract IIOMetadata convertStreamMetadata (IIOMetadata inData,
173                                                      ImageWriteParam param);
174
175   public void dispose()
176   {
177     // The default implementation is empty. Subclasses have to overwrite it.
178   }
179   
180   public Locale[] getAvailableLocales()
181   {
182     return availableLocales;
183   }
184
185   public abstract IIOMetadata getDefaultImageMetadata (ImageTypeSpecifier imageType, ImageWriteParam param);
186
187   public abstract IIOMetadata getDefaultStreamMetadata (ImageWriteParam param);
188
189   public ImageWriteParam getDefaultWriteParam()
190   {
191     return new ImageWriteParam(getLocale());
192   }
193
194   public Locale getLocale()
195   {
196     return locale;
197   }
198
199   public int getNumThumbnailsSupported (ImageTypeSpecifier imageType, ImageWriteParam param,
200                                         IIOMetadata streamMetadata, IIOMetadata imageMetadata)
201   {
202     return 0;
203   }
204
205   public ImageWriterSpi getOriginatingProvider()
206   {
207     return originatingProvider;
208   }
209
210   public Object getOutput()
211   {
212     return output;
213   }
214
215   public Dimension[] getPreferredThumbnailSizes (ImageTypeSpecifier imageType,
216                                                  ImageWriteParam param,
217                                                  IIOMetadata streamMetadata,
218                                                  IIOMetadata imageMetadata)
219   {
220     return null;
221   }
222
223   protected void processImageComplete()
224   {
225     Iterator it = progressListeners.iterator();
226
227     while (it.hasNext())
228       {
229         IIOWriteProgressListener listener = (IIOWriteProgressListener) it.next();
230         listener.imageComplete(this);
231       }
232   }
233
234   protected void processImageProgress(float percentageDone)
235   {
236     Iterator it = progressListeners.iterator();
237
238     while (it.hasNext())
239       {
240         IIOWriteProgressListener listener = (IIOWriteProgressListener) it.next();
241         listener.imageProgress(this, percentageDone);
242       }
243   }
244
245   protected void processImageStarted(int imageIndex)
246   {
247     Iterator it = progressListeners.iterator();
248
249     while (it.hasNext())
250       {
251         IIOWriteProgressListener listener = (IIOWriteProgressListener) it.next();
252         listener.imageStarted(this, imageIndex);
253       }
254   }
255
256   protected void processThumbnailComplete()
257   {
258     Iterator it = progressListeners.iterator();
259
260     while (it.hasNext())
261       {
262         IIOWriteProgressListener listener = (IIOWriteProgressListener) it.next();
263         listener.thumbnailComplete(this);
264       }
265   }
266
267   protected void processThumbnailProgress(float percentageDone)
268   {
269     Iterator it = progressListeners.iterator();
270
271     while (it.hasNext())
272       {
273         IIOWriteProgressListener listener = (IIOWriteProgressListener) it.next();
274         listener.thumbnailProgress(this, percentageDone);
275       }
276   }
277
278   protected void processThumbnailStarted(int imageIndex, int thumbnailIndex)
279   {
280     Iterator it = progressListeners.iterator();
281
282     while (it.hasNext())
283       {
284         IIOWriteProgressListener listener = (IIOWriteProgressListener) it.next();
285         listener.thumbnailStarted(this, imageIndex, thumbnailIndex);
286       }
287   }
288
289   protected void processWarningOccurred(int imageIndex, String warning)
290   {
291     Iterator it = warningListeners.iterator();
292
293     while (it.hasNext())
294       {
295         IIOWriteWarningListener listener = (IIOWriteWarningListener) it.next();
296         listener.warningOccurred(this, imageIndex, warning);
297       }
298   }
299
300   protected void processWriteAborted() 
301   {
302     Iterator it = progressListeners.iterator();
303
304     while (it.hasNext())
305       {
306         IIOWriteProgressListener listener = (IIOWriteProgressListener) it.next();
307         listener.writeAborted(this);
308       }
309   }
310
311   public void removeAllIIOWriteProgressListeners()
312   {
313     progressListeners.clear();
314   }
315
316   public void removeAllIIOWriteWarningListeners()
317   {
318     progressListeners.clear();
319   }
320   
321   public void removeIIOWriteProgressListener (IIOWriteProgressListener listener) 
322   {
323     if (listener == null)
324       return;
325     
326     progressListeners.remove(listener);
327   }
328   
329   public void removeIIOWriteWarningListener (IIOWriteWarningListener listener)
330   {
331     if (listener == null)
332       return;
333     
334     warningListeners.remove(listener);
335   }
336   
337   public void reset()
338   {
339     setOutput(null);
340     setLocale(null);
341     removeAllIIOWriteWarningListeners();
342     removeAllIIOWriteProgressListeners();
343     clearAbortRequest();
344   }
345   
346   public void setLocale(Locale locale)
347   {
348     if (locale != null)
349       {
350         // Check if its a valid locale.
351         boolean found = false;
352
353         if (availableLocales != null)
354           for (int i = availableLocales.length - 1; i >= 0; --i)
355             if (availableLocales[i].equals(locale))
356               found = true;
357
358         if (! found)
359           throw new IllegalArgumentException("looale not available");
360       }
361
362     this.locale = locale;
363   }
364
365   public void setOutput(Object output)
366   {
367     if (output != null)
368       {
369         // Check if its a valid output object.
370         boolean found = false;
371         Class[] types = null;
372
373         if (originatingProvider != null)
374           types = originatingProvider.getOutputTypes();
375         
376         if (types != null)
377           for (int i = types.length - 1; i >= 0; --i)
378             if (types[i].isInstance(output))
379               found = true;
380
381         if (! found)
382           throw new IllegalArgumentException("output type not available");
383       }
384
385     this.output = output;
386   }
387
388   public abstract void write (IIOMetadata streamMetadata, IIOImage image, ImageWriteParam param)
389     throws IOException;
390 }