Source for javax.imageio.ImageWriteParam

   1: /* ImageWriteParam.java --
   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.util.Locale;
  43: 
  44: /**
  45:  * DOCUMENT ME
  46:  */
  47: public class ImageWriteParam extends IIOParam
  48: {
  49:   public static final int MODE_DISABLED = 0;
  50:   public static final int MODE_DEFAULT = 1;
  51:   public static final int MODE_EXPLICIT = 2;
  52:   public static final int MODE_COPY_FROM_METADATA = 3;
  53:   
  54:   protected boolean canOffsetTiles;
  55:   protected boolean canWriteCompressed;
  56:   protected boolean canWriteProgressive;
  57:   protected boolean canWriteTiles;
  58:   protected int compressionMode = MODE_COPY_FROM_METADATA;
  59:   protected float compressionQuality;
  60:   protected String compressionType;
  61:   protected String[] compressionTypes;
  62:   protected Locale locale;
  63:   protected Dimension[] preferredTileSizes;
  64:   protected int progressiveMode = MODE_COPY_FROM_METADATA;
  65:   protected int tileGridXOffset;
  66:   protected int tileGridYOffset;
  67:   protected int tileHeight;
  68:   protected int tileWidth;
  69:   protected int tilingMode;
  70:   protected boolean tilingSet;
  71: 
  72:   /**
  73:    * Creates an empty <code>ImageWriteParam</code> object.
  74:    * The subclass is responsible to initialize all fields.
  75:    */
  76:   protected ImageWriteParam()
  77:   {
  78:     // Do nothing here.
  79:   }
  80: 
  81:   /**
  82:    * Creates an <code>ImageWriteParam</code> object with the given locale.
  83:    *
  84:    * @param locale the locale to use for user visible strings
  85:    */
  86:   public ImageWriteParam(Locale locale)
  87:   {
  88:     this.locale = locale;
  89:   }
  90: 
  91:   public float getBitRate(float quality)
  92:   {
  93:     checkNotExplicitCompression();
  94:     checkCompressionTypesSet();
  95: 
  96:     return -1.0f;
  97:   }
  98: 
  99:   private void checkSupportsCompression()
 100:   {
 101:     if (! canWriteCompressed())
 102:       throw new UnsupportedOperationException("compression not supported");
 103:   }
 104: 
 105:   private void checkNotExplicitCompression()
 106:   {
 107:     if (getCompressionMode() != MODE_EXPLICIT)
 108:       throw new IllegalStateException("compression mode is not MODE_EXPLICIT");
 109:   }
 110:   
 111:   private void checkCompressionTypesSet()
 112:   {
 113:     if (getCompressionType() == null
 114:     && getCompressionTypes() != null)
 115:       throw new IllegalStateException("no compression type set");
 116:   }
 117: 
 118:   private void checkSupportsProgressiveEncoding()
 119:   {
 120:     if (! canWriteProgressive())
 121:       throw new UnsupportedOperationException
 122:     ("progressive output not supported");
 123:   }
 124:   
 125:   private void checkSupportsTiling()
 126:   {
 127:     if (! canWriteTiles())
 128:       throw new UnsupportedOperationException("tiling not supported");
 129:   }
 130: 
 131:   private void checkNotExplicitTiling()
 132:   {
 133:     if (getTilingMode() != MODE_EXPLICIT)
 134:       throw new IllegalStateException("tiling mode not MODE_EXPLICIT");
 135:   }
 136: 
 137:   private void checkTilingInitialized()
 138:   {
 139:     if (! tilingSet)
 140:       throw new IllegalStateException("tiling parameters not set");
 141:   }
 142: 
 143:   private void checkMode(int mode)
 144:   {
 145:     if (mode < MODE_DISABLED || mode > MODE_COPY_FROM_METADATA)
 146:       throw new IllegalArgumentException("mode not supported");
 147:   }
 148: 
 149:   public boolean canOffsetTiles()
 150:   {
 151:     return canOffsetTiles;
 152:   }
 153: 
 154:   public boolean canWriteCompressed()
 155:   {
 156:     return canWriteCompressed;
 157:   }
 158: 
 159:   public boolean canWriteProgressive()
 160:   {
 161:     return canWriteProgressive;
 162:   }
 163: 
 164:   public boolean canWriteTiles()
 165:   {
 166:     return canWriteTiles;
 167:   }
 168: 
 169:   public int getCompressionMode()
 170:   {
 171:     checkSupportsCompression();
 172: 
 173:     return compressionMode;
 174:   }
 175: 
 176:   public float getCompressionQuality()
 177:   {
 178:     checkNotExplicitCompression();
 179:     checkCompressionTypesSet();
 180: 
 181:     return compressionQuality;
 182:   }
 183: 
 184:   public String[] getCompressionQualityDescriptions()
 185:   {
 186:     checkNotExplicitCompression();
 187:     checkCompressionTypesSet();;
 188:     
 189:     return null;
 190:   }
 191: 
 192:   public float[] getCompressionQualityValues()
 193:   {
 194:     checkNotExplicitCompression();
 195:     checkCompressionTypesSet();;
 196:     
 197:     return null;
 198:   }
 199: 
 200:   public String getCompressionType()
 201:   {
 202:     checkNotExplicitCompression();
 203: 
 204:     return compressionType;
 205:   }
 206: 
 207:   public String[] getCompressionTypes()
 208:   {
 209:     checkSupportsCompression();
 210: 
 211:     return compressionTypes != null ? (String[]) compressionTypes.clone() : null;
 212:   }
 213: 
 214:   public Locale getLocale()
 215:   {
 216:     return locale;
 217:   }
 218: 
 219:   public String getLocalizedCompressionTypeName()
 220:   {
 221:     checkNotExplicitCompression();
 222:     checkCompressionTypesSet();
 223: 
 224:     return getCompressionType();
 225:   }
 226: 
 227:   public Dimension[] getPreferredTileSizes()
 228:   {
 229:     checkSupportsTiling();
 230: 
 231:     return preferredTileSizes;
 232:   }
 233: 
 234:   public int getProgressiveMode()
 235:   {
 236:     checkSupportsProgressiveEncoding();
 237: 
 238:     return progressiveMode;
 239:   }
 240: 
 241:   public int getTileGridXOffset()
 242:   {
 243:     checkNotExplicitTiling();
 244:     checkTilingInitialized();
 245: 
 246:     return tileGridXOffset;
 247:   }
 248: 
 249:   public int getTileGridYOffset()
 250:   {
 251:     checkNotExplicitTiling();
 252:     checkTilingInitialized();
 253: 
 254:     return tileGridYOffset;
 255:   }
 256: 
 257:   public int getTileHeight()
 258:   {
 259:     checkNotExplicitTiling();
 260:     checkTilingInitialized();
 261: 
 262:     return tileHeight;
 263:   }
 264: 
 265:   public int getTileWidth()
 266:   {
 267:     checkNotExplicitTiling();
 268:     checkTilingInitialized();
 269: 
 270:     return tileWidth;
 271:   }
 272: 
 273:   public int getTilingMode()
 274:   {
 275:     checkSupportsTiling();
 276: 
 277:     return tilingMode;
 278:   }
 279: 
 280:   public boolean isCompressionLossless()
 281:   {
 282:     checkNotExplicitCompression();
 283:     checkCompressionTypesSet();
 284: 
 285:     return true;
 286:   }
 287: 
 288:   public void setCompressionMode(int mode)
 289:   {
 290:     checkSupportsCompression();
 291:     checkMode(mode);
 292:     
 293:     compressionMode = mode;
 294:     
 295:     if (mode == MODE_EXPLICIT)
 296:       unsetCompression();
 297:   }
 298: 
 299:   public void setCompressionQuality(float quality)
 300:   {
 301:     checkNotExplicitCompression();
 302:     checkCompressionTypesSet();
 303: 
 304:     if (quality < 0.0f || quality > 1.0f)
 305:       throw new IllegalArgumentException("quality out of range");
 306: 
 307:     compressionQuality = quality;
 308:   }
 309: 
 310:   public void setCompressionType(String compressionType)
 311:   {
 312:     checkNotExplicitCompression();
 313: 
 314:     String[] types = getCompressionTypes();
 315: 
 316:     if (types == null)
 317:       throw new UnsupportedOperationException("no settable compression types");
 318:     
 319:     if (compressionType == null)
 320:       this.compressionType = null;
 321: 
 322:     for (int i = types.length - 1; i >= 0; --i)
 323:       if (types[i].equals(compressionType))
 324:     {
 325:       this.compressionType = compressionType;
 326:       return;
 327:     }
 328:     
 329:     throw new IllegalArgumentException("unknown compression type");
 330:   }
 331: 
 332:   public void setProgressiveMode(int mode)
 333:   {
 334:     checkSupportsProgressiveEncoding();
 335:     checkMode(mode);
 336:     
 337:     progressiveMode = mode;
 338:   }
 339: 
 340:   public void setTiling(int tileWidth, int tileHeight,
 341:                 int tileGridXOffset, int tileGridYOffset)
 342:   {
 343:     checkNotExplicitTiling();
 344: 
 345:     if (! canOffsetTiles
 346:     && tileGridXOffset != 0
 347:     && tileGridYOffset != 0)
 348:       throw new UnsupportedOperationException("tile offsets not supported");
 349: 
 350:     if (tileWidth < 0 || tileHeight < 0)
 351:       throw new IllegalArgumentException("negative tile dimension");
 352: 
 353:     if (preferredTileSizes != null)
 354:       {
 355:     boolean found = false;
 356: 
 357:     for (int i = 0; i < preferredTileSizes.length; i += 2)
 358:       {
 359:         if (tileWidth >= preferredTileSizes[i].width
 360:         && tileWidth <= preferredTileSizes[i + 1].width
 361:         && tileHeight >= preferredTileSizes[i].height
 362:         && tileHeight <= preferredTileSizes[i + 1].height)
 363:           found = true;
 364:       }
 365: 
 366:     if (! found)
 367:           throw new IllegalArgumentException("illegal tile size");
 368:       }
 369: 
 370:     this.tilingSet = true;
 371:     this.tileWidth = tileWidth;
 372:     this.tileHeight = tileHeight;
 373:     this.tileGridXOffset = tileGridXOffset;
 374:     this.tileGridYOffset = tileGridYOffset;
 375:   }
 376: 
 377:   public void setTilingMode(int mode)
 378:   {
 379:     checkSupportsTiling();
 380:     checkMode(mode);
 381:     tilingMode = mode;
 382:   }
 383: 
 384:   public void unsetCompression()
 385:   {
 386:     checkNotExplicitCompression();
 387:     
 388:     compressionType = null;
 389:     compressionQuality = 1.0F;
 390:   }
 391: 
 392:   public void unsetTiling()
 393:   {
 394:     checkNotExplicitTiling();
 395:     
 396:     tileWidth = 0;
 397:     tileHeight = 0;
 398:     tileGridXOffset = 0;
 399:     tileGridYOffset = 0;
 400:   }
 401: }