Source for javax.imageio.ImageReader

   1: /* ImageReader.java -- Decodes 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.image.BufferedImage;
  42: import java.awt.image.Raster;
  43: import java.io.IOException;
  44: import java.util.ArrayList;
  45: import java.util.Iterator;
  46: import java.util.List;
  47: import java.util.Locale;
  48: 
  49: import javax.imageio.event.IIOReadProgressListener;
  50: import javax.imageio.event.IIOReadUpdateListener;
  51: import javax.imageio.event.IIOReadWarningListener;
  52: import javax.imageio.metadata.IIOMetadata;
  53: import javax.imageio.spi.ImageReaderSpi;
  54: import javax.imageio.stream.ImageInputStream;
  55: 
  56: public abstract class ImageReader
  57: {
  58:   private boolean aborted;
  59:   
  60:   protected Locale[] availableLocales;
  61:   protected boolean ignoreMetadata;
  62:   protected Object input;
  63:   protected Locale locale;
  64:   protected int minIndex;
  65:   protected ImageReaderSpi originatingProvider;
  66:   protected List progressListeners = new ArrayList();
  67:   protected boolean seekForwardOnly;
  68:   protected List updateListeners = new ArrayList();
  69:   protected List warningListeners = new ArrayList();
  70:   protected List warningLocales = new ArrayList();
  71: 
  72:   protected ImageReader(ImageReaderSpi originatingProvider)
  73:   {
  74:     this.originatingProvider = originatingProvider;
  75:   }
  76: 
  77:   public void abort()
  78:   {
  79:     aborted = true;
  80:   }
  81: 
  82:   protected boolean abortRequested()
  83:   {
  84:     return aborted;
  85:   }
  86: 
  87:   public void addIIOReadProgressListener(IIOReadProgressListener listener)
  88:   {
  89:     if (listener == null)
  90:       return;
  91:     
  92:     progressListeners.add(listener);    
  93:   }
  94: 
  95:   public void addIIOReadUpdateListener(IIOReadUpdateListener listener)
  96:   {
  97:     if (listener == null)
  98:       return;
  99:     
 100:     updateListeners.add(listener);    
 101:   }
 102:   
 103:   public void addIIOReadWarningListener(IIOReadWarningListener listener)
 104:   {
 105:     if (listener == null)
 106:       return;
 107:     
 108:     warningListeners.add(listener);    
 109:   }
 110: 
 111:   public boolean canReadRaster()
 112:   {
 113:     return false;
 114:   }
 115: 
 116:   protected void clearAbortRequest()
 117:   {
 118:     aborted = false;
 119:   }
 120:   
 121:   public void dispose()
 122:   {
 123:     // The default implementation does nothing.
 124:   }
 125:   
 126:   public float getAspectRatio(int imageIndex)
 127:     throws IOException
 128:   {
 129:     return (float) (getWidth(imageIndex) / getHeight(imageIndex));
 130:   }
 131: 
 132:   public Locale[] getAvailableLocales()
 133:   {
 134:     if (availableLocales == null)
 135:       return null;
 136:     
 137:     return (Locale[]) availableLocales.clone();
 138:   }
 139: 
 140:   public ImageReadParam getDefaultReadParam()
 141:   {
 142:     return new ImageReadParam();
 143:   }
 144: 
 145:   public String getFormatName()
 146:     throws IOException
 147:   {
 148:     return originatingProvider.getFormatNames()[0];
 149:   }
 150: 
 151:   public abstract int getHeight(int imageIndex)
 152:     throws IOException;
 153: 
 154:   public abstract IIOMetadata getImageMetadata(int imageIndex)
 155:     throws IOException;
 156: 
 157:   public abstract Iterator getImageTypes(int imageIndex)
 158:     throws IOException;
 159: 
 160:   public void setInput(Object input,
 161:                        boolean seekForwardOnly,
 162:                        boolean ignoreMetadata)
 163:   {
 164:     Class[] okClasses = originatingProvider.getInputTypes();
 165:     if (okClasses == null)
 166:       {
 167:         if (!(input instanceof ImageInputStream))
 168:           throw new IllegalArgumentException();
 169:       }
 170:     else
 171:       {
 172:         boolean classOk = false;
 173:         for (int i = 0; i < okClasses.length; ++i)
 174:           if (okClasses[i].isInstance(input))
 175:             classOk = true;
 176:         if (!classOk)
 177:           throw new IllegalArgumentException();
 178:       }
 179: 
 180:     this.input = input;
 181:     this.seekForwardOnly = seekForwardOnly;
 182:     this.ignoreMetadata = ignoreMetadata;
 183:     this.minIndex = 0;
 184:   }
 185: 
 186:   public void setInput(Object in, boolean seekForwardOnly)
 187:   {
 188:     setInput(in, seekForwardOnly, false);
 189:   }
 190: 
 191:   public void setInput(Object in)
 192:   {
 193:     setInput(in, false, false);
 194:   }
 195: 
 196:   public Object getInput()
 197:   {
 198:     return input;
 199:   }
 200: 
 201:   public Locale getLocale()
 202:   {
 203:     return locale;
 204:   }
 205: 
 206:   public abstract int getNumImages(boolean allowSearch)
 207:     throws IOException;
 208: 
 209:   public int getNumThumbnails(int imageIndex)
 210:     throws IOException
 211:   {
 212:     return 0;
 213:   }
 214: 
 215:   public ImageReaderSpi getOriginatingProvider()
 216:   {
 217:     return originatingProvider;
 218:   }
 219: 
 220:   public abstract IIOMetadata getStreamMetadata()
 221:     throws IOException;
 222: 
 223:   public int getThumbnailHeight(int imageIndex, int thumbnailIndex)
 224:     throws IOException
 225:   {
 226:     return readThumbnail(imageIndex, thumbnailIndex).getHeight();
 227:   }
 228: 
 229:   public int getThumbnailWidth(int imageIndex, int thumbnailIndex)
 230:     throws IOException
 231:   {
 232:     return readThumbnail(imageIndex, thumbnailIndex).getWidth();
 233:   }
 234: 
 235:   public int getTileGridXOffset(int imageIndex)
 236:     throws IOException
 237:   {
 238:     return 0;
 239:   }
 240: 
 241:   public int getTileGridYOffset(int imageIndex)
 242:     throws IOException
 243:   {
 244:     return 0;
 245:   }
 246: 
 247:   public int getTileHeight(int imageIndex)
 248:     throws IOException
 249:   {
 250:     return getHeight(imageIndex);
 251:   }
 252: 
 253:   public int getTileWidth(int imageIndex)
 254:     throws IOException
 255:   {
 256:     return getWidth(imageIndex);
 257:   }
 258: 
 259:   public abstract int getWidth(int imageIndex)
 260:     throws IOException;
 261: 
 262:   public boolean hasThumbnails(int imageIndex)
 263:     throws IOException
 264:   {
 265:     return getNumThumbnails(imageIndex) > 0;
 266:   }
 267: 
 268:   public boolean isIgnoringMetadata()
 269:   {
 270:     return ignoreMetadata;
 271:   }
 272: 
 273:   public boolean isImageTiled(int imageIndex)
 274:     throws IOException
 275:   {
 276:     return false;
 277:   }
 278: 
 279:   public boolean isRandomAccessEasy(int imageIndex)
 280:     throws IOException
 281:   {
 282:     return false;
 283:   }
 284: 
 285:   public boolean isSeekForwardOnly()
 286:   {
 287:     return seekForwardOnly;
 288:   }
 289: 
 290:   protected void processImageComplete()
 291:   {
 292:     Iterator it = progressListeners.iterator();
 293: 
 294:     while (it.hasNext())
 295:       {
 296:     IIOReadProgressListener listener = (IIOReadProgressListener) it.next();
 297:     listener.imageComplete (this);
 298:       }
 299:   }
 300: 
 301:   protected void processImageProgress(float percentageDone)
 302:   {
 303:     Iterator it = progressListeners.iterator();
 304: 
 305:     while (it.hasNext())
 306:       {
 307:     IIOReadProgressListener listener = (IIOReadProgressListener) it.next();
 308:     listener.imageProgress(this, percentageDone);
 309:       }
 310:   }
 311: 
 312:   protected void processImageStarted(int imageIndex)
 313:   {
 314:     Iterator it = progressListeners.iterator();
 315: 
 316:     while (it.hasNext())
 317:       {
 318:     IIOReadProgressListener listener = (IIOReadProgressListener) it.next();
 319:     listener.imageStarted(this, imageIndex);
 320:       }
 321:   }
 322: 
 323:   protected void processImageUpdate(BufferedImage image, int minX, int minY,
 324:                     int width, int height, int periodX,
 325:                     int periodY, int[] bands)
 326:   {
 327:     Iterator it = updateListeners.iterator();
 328: 
 329:     while (it.hasNext())
 330:       {
 331:     IIOReadUpdateListener listener = (IIOReadUpdateListener) it.next();
 332:     listener.imageUpdate(this, image, minX, minY, width, height, periodX,
 333:                  periodY, bands);
 334:       }
 335:   }
 336: 
 337:   protected void processPassComplete(BufferedImage image)
 338:   {
 339:     Iterator it = updateListeners.iterator();
 340: 
 341:     while (it.hasNext())
 342:       {
 343:     IIOReadUpdateListener listener = (IIOReadUpdateListener) it.next();
 344:     listener.passComplete(this, image);
 345:       }
 346:   }
 347: 
 348:   protected void processPassStarted(BufferedImage image, int pass, int minPass,
 349:                     int maxPass, int minX, int minY,
 350:                     int periodX, int periodY, int[] bands)
 351:   {
 352:     Iterator it = updateListeners.iterator();
 353: 
 354:     while (it.hasNext())
 355:       {
 356:     IIOReadUpdateListener listener = (IIOReadUpdateListener) it.next();
 357:     listener.passStarted(this, image, pass, minPass, maxPass, minX, minY,
 358:                  periodX, periodY, bands);
 359:       }
 360:   }
 361: 
 362:   protected void processReadAborted()
 363:   {
 364:     Iterator it = progressListeners.iterator();
 365: 
 366:     while (it.hasNext())
 367:       {
 368:     IIOReadProgressListener listener = (IIOReadProgressListener) it.next();
 369:     listener.readAborted(this);
 370:       }
 371:   }
 372: 
 373:   protected void processSequenceComplete()
 374:   {
 375:     Iterator it = progressListeners.iterator();
 376: 
 377:     while (it.hasNext())
 378:       {
 379:     IIOReadProgressListener listener = (IIOReadProgressListener) it.next();
 380:     listener.sequenceComplete(this);
 381:       }
 382:   }
 383: 
 384:   protected void processSequenceStarted(int minIndex)
 385:   {
 386:     Iterator it = progressListeners.iterator();
 387: 
 388:     while (it.hasNext())
 389:       {
 390:     IIOReadProgressListener listener = (IIOReadProgressListener) it.next();
 391:     listener.sequenceStarted(this, minIndex);
 392:       }
 393:   }
 394: 
 395:   protected void processThumbnailComplete()
 396:   {
 397:     Iterator it = progressListeners.iterator();
 398: 
 399:     while (it.hasNext())
 400:       {
 401:     IIOReadProgressListener listener = (IIOReadProgressListener) it.next();
 402:     listener.thumbnailComplete(this);
 403:       }
 404:   }
 405: 
 406:   protected void processThumbnailPassComplete(BufferedImage thumbnail)
 407:   {
 408:     Iterator it = updateListeners.iterator();
 409: 
 410:     while (it.hasNext())
 411:       {
 412:     IIOReadUpdateListener listener = (IIOReadUpdateListener) it.next();
 413:     listener.thumbnailPassComplete(this, thumbnail);
 414:       }
 415:   }
 416: 
 417:   protected void processThumbnailPassStarted(BufferedImage thumbnail, int pass,
 418:                          int minPass, int maxPass, int minX,
 419:                          int minY, int periodX, int periodY,
 420:                          int[] bands)
 421:   {
 422:     Iterator it = updateListeners.iterator();
 423: 
 424:     while (it.hasNext())
 425:       {
 426:     IIOReadUpdateListener listener = (IIOReadUpdateListener) it.next();
 427:     listener.thumbnailPassStarted(this, thumbnail, pass, minPass, maxPass,
 428:                       minX, minY, periodX, periodY, bands);
 429:       }
 430:   }
 431:   
 432:   protected void processThumbnailProgress(float percentageDone)
 433:   {
 434:     Iterator it = progressListeners.iterator();
 435: 
 436:     while (it.hasNext())
 437:       {
 438:     IIOReadProgressListener listener = (IIOReadProgressListener) it.next();
 439:     listener.thumbnailProgress(this, percentageDone);
 440:       }
 441:   }
 442: 
 443:   protected void processThumbnailStarted(int imageIndex, int thumbnailIndex)
 444:   {
 445:     Iterator it = progressListeners.iterator();
 446: 
 447:     while (it.hasNext())
 448:       {
 449:     IIOReadProgressListener listener = (IIOReadProgressListener) it.next();
 450:     listener.thumbnailStarted(this, imageIndex, thumbnailIndex);
 451:       }
 452:   }
 453: 
 454:   protected void processThumbnailUpdate(BufferedImage image, int minX, int minY,
 455:                     int width, int height, int periodX,
 456:                     int periodY, int[] bands)
 457:   {
 458:     Iterator it = updateListeners.iterator();
 459: 
 460:     while (it.hasNext())
 461:       {
 462:     IIOReadUpdateListener listener = (IIOReadUpdateListener) it.next();
 463:     listener.thumbnailUpdate(this, image, minX, minY, width, height,
 464:                  periodX, periodY, bands);
 465:       }
 466:   }
 467: 
 468:   protected void processWarningOccurred(String warning)
 469:   {
 470:     Iterator it = warningListeners.iterator();
 471: 
 472:     while (it.hasNext())
 473:       {
 474:     IIOReadWarningListener listener = (IIOReadWarningListener) it.next();
 475:     listener.warningOccurred(this, warning);
 476:       }
 477:   }
 478: 
 479:   public abstract BufferedImage read(int imageIndex, ImageReadParam param)
 480:     throws IOException;
 481: 
 482:   public boolean readerSupportsThumbnails()
 483:   {
 484:     return false;
 485:   }
 486: 
 487:   public Raster readRaster(int imageIndex, ImageReadParam param)
 488:     throws IOException
 489:   {
 490:     throw new UnsupportedOperationException();
 491:   }
 492: 
 493:   public BufferedImage readThumbnail(int imageIndex, int thumbnailIndex)
 494:     throws IOException
 495:   {
 496:     throw new UnsupportedOperationException();
 497:   }
 498: 
 499:   public void removeAllIIOReadProgressListeners()
 500:   {
 501:     progressListeners.clear();
 502:   }
 503: 
 504:   public void removeAllIIOReadUpdateListeners()
 505:   {
 506:     updateListeners.clear();
 507:   }
 508: 
 509:   public void removeAllIIOReadWarningListeners()
 510:   {
 511:     warningListeners.clear();
 512:   }
 513:   
 514:   public void removeIIOReadProgressListener(IIOReadProgressListener listener) 
 515:   {
 516:     if (listener == null)
 517:       return;
 518:  
 519:     progressListeners.remove(listener);
 520:   }
 521:   
 522:   public void removeIIOReadUpdateListener(IIOReadUpdateListener listener) 
 523:   {
 524:     if (listener == null)
 525:       return;
 526:     
 527:     updateListeners.remove(listener);
 528:   }
 529:   
 530:   public void removeIIOReadWarningListener(IIOReadWarningListener listener)
 531:   {
 532:     if (listener == null)
 533:       return;
 534:     
 535:     warningListeners.remove(listener);
 536:   }
 537:   
 538:   public void setLocale(Locale locale)
 539:   {
 540:     if (locale != null)
 541:       {
 542:     // Check if its a valid locale.
 543:     boolean found = false;
 544: 
 545:     if (availableLocales != null)
 546:       for (int i = availableLocales.length - 1; i >= 0; --i)
 547:         if (availableLocales[i].equals(locale))
 548:           found = true;
 549: 
 550:     if (! found)
 551:       throw new IllegalArgumentException("looale not available");
 552:       }
 553: 
 554:     this.locale = locale;
 555:   }
 556: }