Index: src/main/java/javax/imageio/ImageWriteParam.java =================================================================== --- src/main/java/javax/imageio/ImageWriteParam.java (revision 468450) +++ src/main/java/javax/imageio/ImageWriteParam.java (working copy) @@ -23,9 +23,6 @@ import java.util.Locale; import java.awt.*; -/** - * TODO: add the methods from the spec - */ public class ImageWriteParam extends IIOParam { public static final int MODE_DISABLED = 0; @@ -77,5 +74,290 @@ } throw new UnsupportedOperationException("progressive mode is not supported"); } + + public boolean canOffsetTiles() { + return canOffsetTiles; + } + + public boolean canWriteCompressed() { + return canWriteCompressed; + } + + public boolean canWriteTiles() { + return canWriteTiles; + } + + private final void checkWriteCompressed() { + if (!canWriteCompressed()) { + throw new UnsupportedOperationException("Compression not supported."); + } + } + + private final void checkCompressionMode() { + if (getCompressionMode() != MODE_EXPLICIT) { + throw new IllegalStateException("Compression mode not MODE_EXPLICIT!"); + } + } + + private final void checkCompressionType() { + if (getCompressionTypes() != null && getCompressionType() == null) { + throw new IllegalStateException("No compression type set!"); + } + } + + public int getCompressionMode() { + checkWriteCompressed(); + return compressionMode; + } + + public String[] getCompressionTypes() { + checkWriteCompressed(); + if (compressionTypes != null) { + return compressionTypes.clone(); + } + return null; + } + + public String getCompressionType() { + checkWriteCompressed(); + checkCompressionMode(); + return compressionType; + } + + public float getBitRate(float quality) { + checkWriteCompressed(); + checkCompressionMode(); + checkCompressionType(); + if (quality < 0 || quality > 1) { + throw new IllegalArgumentException("Quality out-of-bounds!"); + } + return -1.0f; + } + + public float getCompressionQuality() { + checkWriteCompressed(); + checkCompressionMode(); + checkCompressionType(); + return compressionQuality; + } + + public String[] getCompressionQualityDescriptions() { + checkWriteCompressed(); + checkCompressionMode(); + checkCompressionType(); + return null; + } + + public float[] getCompressionQualityValues() { + checkWriteCompressed(); + checkCompressionMode(); + checkCompressionType(); + return null; + } + + public Locale getLocale() { + return locale; + } + + public String getLocalizedCompressionTypeName() { + checkWriteCompressed(); + checkCompressionMode(); + + String compressionType = getCompressionType(); + if (compressionType == null) { + throw new IllegalStateException("No compression type set!"); + } + return compressionType; + + } + + private final void checkTiling() { + if (!canWriteTiles()) { + throw new UnsupportedOperationException("Tiling not supported!"); + } + } + + private final void checkTilingMode() { + if (getTilingMode() != MODE_EXPLICIT) { + throw new IllegalStateException("Tiling mode not MODE_EXPLICIT!"); + } + } + + private final void checkTilingParams() { + if (!tilingSet) { + throw new IllegalStateException("Tiling parameters not set!"); + } + } + + public int getTilingMode() { + checkTiling(); + return tilingMode; + } + + public Dimension[] getPreferredTileSizes() { + checkTiling(); + if (preferredTileSizes == null) { + return null; + } + + Dimension[] retval = new Dimension[preferredTileSizes.length]; + for (int i = 0; i < preferredTileSizes.length; i++) { + retval[i] = new Dimension(retval[i]); + } + return retval; + } + + public int getTileGridXOffset() { + checkTiling(); + checkTilingMode(); + checkTilingParams(); + return tileGridXOffset; + } + + public int getTileGridYOffset() { + checkTiling(); + checkTilingMode(); + checkTilingParams(); + return tileGridYOffset; + } + + public int getTileHeight() { + checkTiling(); + checkTilingMode(); + checkTilingParams(); + return tileHeight; + } + + public int getTileWidth() { + checkTiling(); + checkTilingMode(); + checkTilingParams(); + return tileWidth; + } + + public boolean isCompressionLossless() { + checkWriteCompressed(); + checkCompressionMode(); + checkCompressionType(); + return true; + } + + public void unsetCompression() { + checkWriteCompressed(); + checkCompressionMode(); + compressionType = null; + compressionQuality = 1; + } + + public void setCompressionMode(int mode) { + checkWriteCompressed(); + switch (mode) { + case MODE_EXPLICIT: + unsetCompression(); + case MODE_COPY_FROM_METADATA: + case MODE_DISABLED: + case MODE_DEFAULT: + compressionMode = mode; + break; + default: { + throw new IllegalArgumentException("Illegal value for mode!"); + } + } + } + + public void setCompressionQuality(float quality) { + checkWriteCompressed(); + checkCompressionMode(); + checkCompressionType(); + if (quality < 0 || quality > 1) { + throw new IllegalArgumentException("Quality out-of-bounds!"); + } + compressionQuality = quality; + } + + public void setCompressionType(String compressionType) { + checkWriteCompressed(); + checkCompressionMode(); + + if (compressionType == null) { // Don't check anything + this.compressionType = null; + } else { + String[] compressionTypes = getCompressionTypes(); + if (compressionTypes == null) { + throw new UnsupportedOperationException("No settable compression types"); + } + + for (int i = 0; i < compressionTypes.length; i++) { + if (compressionTypes[i].equals(compressionType)) { + this.compressionType = compressionType; + return; + } + } + + // Compression type is not in the list. + throw new IllegalArgumentException("Unknown compression type!"); + } + } + + public void setTiling(int tileWidth, int tileHeight, int tileGridXOffset, int tileGridYOffset) { + checkTiling(); + checkTilingMode(); + + if (!canOffsetTiles() && (tileGridXOffset != 0 || tileGridYOffset != 0)) { + throw new UnsupportedOperationException("Can't offset tiles!"); + } + + if (tileWidth <=0 || tileHeight <= 0) { + throw new IllegalArgumentException("tile dimensions are non-positive!"); + } + + Dimension preferredTileSizes[] = getPreferredTileSizes(); + if (preferredTileSizes != null) { + for (int i = 0; i < preferredTileSizes.length; i+=2) { + Dimension minSize = preferredTileSizes[i]; + Dimension maxSize = preferredTileSizes[i+1]; + if ( + tileWidth < minSize.width || tileWidth > maxSize.width || + tileHeight < minSize.height || tileHeight > maxSize.height + ) { + throw new IllegalArgumentException("Illegal tile size!"); + } + } + } + + tilingSet = true; + this.tileWidth = tileWidth; + this.tileHeight = tileHeight; + this.tileGridXOffset = tileGridXOffset; + this.tileGridYOffset = tileGridYOffset; + } + + public void unsetTiling() { + checkTiling(); + checkTilingMode(); + + tilingSet = false; + tileWidth = 0; + tileHeight = 0; + tileGridXOffset = 0; + tileGridYOffset = 0; + } + + public void setTilingMode(int mode) { + checkTiling(); + + switch (mode) { + case MODE_EXPLICIT: + unsetTiling(); + case MODE_COPY_FROM_METADATA: + case MODE_DISABLED: + case MODE_DEFAULT: + tilingMode = mode; + break; + default: { + throw new IllegalArgumentException("Illegal value for mode!"); + } + } + } }