1 7 8 package javax.imageio; 9 10 import java.awt.color.ICC_Profile ; 11 import java.awt.image.LookupTable ; 12 import java.awt.image.RenderedImage ; 13 import java.awt.Dimension ; 14 import java.util.Locale ; 15 16 72 public class ImageWriteParam extends IIOParam { 73 74 93 public static final int MODE_DISABLED = 0; 94 95 116 public static final int MODE_DEFAULT = 1; 117 118 138 public static final int MODE_EXPLICIT = 2; 139 140 164 public static final int MODE_COPY_FROM_METADATA = 3; 165 166 private static final int MAX_MODE = MODE_COPY_FROM_METADATA; 168 169 178 protected boolean canWriteTiles = false; 179 180 194 protected int tilingMode = MODE_COPY_FROM_METADATA; 195 196 207 protected Dimension [] preferredTileSizes = null; 208 209 216 protected boolean tilingSet = false; 217 218 224 protected int tileWidth = 0; 225 226 233 protected int tileHeight = 0; 234 235 245 protected boolean canOffsetTiles = false; 246 247 255 protected int tileGridXOffset = 0; 256 257 265 protected int tileGridYOffset = 0; 266 267 277 protected boolean canWriteProgressive = false; 278 279 295 protected int progressiveMode = MODE_COPY_FROM_METADATA; 296 297 305 protected boolean canWriteCompressed = false; 306 307 322 protected int compressionMode = MODE_COPY_FROM_METADATA; 323 324 332 protected String [] compressionTypes = null; 333 334 341 protected String compressionType = null; 342 343 350 protected float compressionQuality = 1.0F; 351 352 358 protected Locale locale = null; 359 360 364 protected ImageWriteParam() {} 365 366 374 public ImageWriteParam(Locale locale) { 375 this.locale = locale; 376 } 377 378 private static Dimension [] clonePreferredTileSizes(Dimension [] sizes) { 380 if (sizes == null) { 381 return null; 382 } 383 Dimension [] temp = new Dimension [sizes.length]; 384 for (int i = 0; i < sizes.length; i++) { 385 temp[i] = new Dimension (sizes[i]); 386 } 387 return temp; 388 } 389 390 397 public Locale getLocale() { 398 return locale; 399 } 400 401 412 public boolean canWriteTiles() { 413 return canWriteTiles; 414 } 415 416 431 public boolean canOffsetTiles() { 432 return canOffsetTiles; 433 } 434 435 472 public void setTilingMode(int mode) { 473 if (canWriteTiles() == false) { 474 throw new UnsupportedOperationException ("Tiling not supported!"); 475 } 476 if (mode < MODE_DISABLED || mode > MAX_MODE) { 477 throw new IllegalArgumentException ("Illegal value for mode!"); 478 } 479 this.tilingMode = mode; 480 if (mode == MODE_EXPLICIT) { 481 unsetTiling(); 482 } 483 } 484 485 496 public int getTilingMode() { 497 if (!canWriteTiles()) { 498 throw new UnsupportedOperationException ("Tiling not supported"); 499 } 500 return tilingMode; 501 } 502 503 526 public Dimension [] getPreferredTileSizes() { 527 if (!canWriteTiles()) { 528 throw new UnsupportedOperationException ("Tiling not supported"); 529 } 530 return clonePreferredTileSizes(preferredTileSizes); 531 } 532 533 568 public void setTiling(int tileWidth, 569 int tileHeight, 570 int tileGridXOffset, 571 int tileGridYOffset) { 572 if (!canWriteTiles()) { 573 throw new UnsupportedOperationException ("Tiling not supported!"); 574 } 575 if (getTilingMode() != MODE_EXPLICIT) { 576 throw new IllegalStateException ("Tiling mode not MODE_EXPLICIT!"); 577 } 578 if (tileWidth <= 0 || tileHeight <= 0) { 579 throw new IllegalArgumentException 580 ("tile dimensions are non-positive!"); 581 } 582 boolean tilesOffset = (tileGridXOffset != 0) || (tileGridYOffset != 0); 583 if (!canOffsetTiles() && tilesOffset) { 584 throw new UnsupportedOperationException ("Can't offset tiles!"); 585 } 586 if (preferredTileSizes != null) { 587 boolean ok = true; 588 for (int i = 0; i < preferredTileSizes.length; i += 2) { 589 Dimension min = preferredTileSizes[i]; 590 Dimension max = preferredTileSizes[i+1]; 591 if ((tileWidth < min.width) || 592 (tileWidth > max.width) || 593 (tileHeight < min.height) || 594 (tileHeight > max.height)) { 595 ok = false; 596 break; 597 } 598 } 599 if (!ok) { 600 throw new IllegalArgumentException ("Illegal tile size!"); 601 } 602 } 603 604 this.tilingSet = true; 605 this.tileWidth = tileWidth; 606 this.tileHeight = tileHeight; 607 this.tileGridXOffset = tileGridXOffset; 608 this.tileGridYOffset = tileGridYOffset; 609 } 610 611 627 public void unsetTiling() { 628 if (!canWriteTiles()) { 629 throw new UnsupportedOperationException ("Tiling not supported!"); 630 } 631 if (getTilingMode() != MODE_EXPLICIT) { 632 throw new IllegalStateException ("Tiling mode not MODE_EXPLICIT!"); 633 } 634 this.tilingSet = false; 635 this.tileWidth = 0; 636 this.tileHeight = 0; 637 this.tileGridXOffset = 0; 638 this.tileGridYOffset = 0; 639 } 640 641 658 public int getTileWidth() { 659 if (!canWriteTiles()) { 660 throw new UnsupportedOperationException ("Tiling not supported!"); 661 } 662 if (getTilingMode() != MODE_EXPLICIT) { 663 throw new IllegalStateException ("Tiling mode not MODE_EXPLICIT!"); 664 } 665 if (!tilingSet) { 666 throw new IllegalStateException ("Tiling parameters not set!"); 667 } 668 return tileWidth; 669 } 670 671 688 public int getTileHeight() { 689 if (!canWriteTiles()) { 690 throw new UnsupportedOperationException ("Tiling not supported!"); 691 } 692 if (getTilingMode() != MODE_EXPLICIT) { 693 throw new IllegalStateException ("Tiling mode not MODE_EXPLICIT!"); 694 } 695 if (!tilingSet) { 696 throw new IllegalStateException ("Tiling parameters not set!"); 697 } 698 return tileHeight; 699 } 700 701 718 public int getTileGridXOffset() { 719 if (!canWriteTiles()) { 720 throw new UnsupportedOperationException ("Tiling not supported!"); 721 } 722 if (getTilingMode() != MODE_EXPLICIT) { 723 throw new IllegalStateException ("Tiling mode not MODE_EXPLICIT!"); 724 } 725 if (!tilingSet) { 726 throw new IllegalStateException ("Tiling parameters not set!"); 727 } 728 return tileGridXOffset; 729 } 730 731 748 public int getTileGridYOffset() { 749 if (!canWriteTiles()) { 750 throw new UnsupportedOperationException ("Tiling not supported!"); 751 } 752 if (getTilingMode() != MODE_EXPLICIT) { 753 throw new IllegalStateException ("Tiling mode not MODE_EXPLICIT!"); 754 } 755 if (!tilingSet) { 756 throw new IllegalStateException ("Tiling parameters not set!"); 757 } 758 return tileGridYOffset; 759 } 760 761 771 public boolean canWriteProgressive() { 772 return canWriteProgressive; 773 } 774 775 815 public void setProgressiveMode(int mode) { 816 if (!canWriteProgressive()) { 817 throw new UnsupportedOperationException ( 818 "Progressive output not supported"); 819 } 820 if (mode < MODE_DISABLED || mode > MAX_MODE) { 821 throw new IllegalArgumentException ("Illegal value for mode!"); 822 } 823 if (mode == MODE_EXPLICIT) { 824 throw new IllegalArgumentException ( 825 "MODE_EXPLICIT not supported for progressive output"); 826 } 827 this.progressiveMode = mode; 828 } 829 830 841 public int getProgressiveMode() { 842 if (!canWriteProgressive()) { 843 throw new UnsupportedOperationException 844 ("Progressive output not supported"); 845 } 846 return progressiveMode; 847 } 848 849 854 public boolean canWriteCompressed() { 855 return canWriteCompressed; 856 } 857 858 895 public void setCompressionMode(int mode) { 896 if (!canWriteCompressed()) { 897 throw new UnsupportedOperationException ( 898 "Compression not supported."); 899 } 900 if (mode < MODE_DISABLED || mode > MAX_MODE) { 901 throw new IllegalArgumentException ("Illegal value for mode!"); 902 } 903 this.compressionMode = mode; 904 if (mode == MODE_EXPLICIT) { 905 unsetCompression(); 906 } 907 } 908 909 920 public int getCompressionMode() { 921 if (!canWriteCompressed()) { 922 throw new UnsupportedOperationException ( 923 "Compression not supported."); 924 } 925 return compressionMode; 926 } 927 928 954 public String [] getCompressionTypes() { 955 if (!canWriteCompressed()) { 956 throw new UnsupportedOperationException ( 957 "Compression not supported"); 958 } 959 if (compressionTypes == null) { 960 return null; 961 } 962 return (String [])compressionTypes.clone(); 963 } 964 965 998 public void setCompressionType(String compressionType) { 999 if (!canWriteCompressed()) { 1000 throw new UnsupportedOperationException ( 1001 "Compression not supported"); 1002 } 1003 if (getCompressionMode() != MODE_EXPLICIT) { 1004 throw new IllegalStateException 1005 ("Compression mode not MODE_EXPLICIT!"); 1006 } 1007 String [] legalTypes = getCompressionTypes(); 1008 if (legalTypes == null) { 1009 throw new UnsupportedOperationException ( 1010 "No settable compression types"); 1011 } 1012 if (compressionType != null) { 1013 boolean found = false; 1014 if (legalTypes != null) { 1015 for (int i = 0; i < legalTypes.length; i++) { 1016 if (compressionType.equals(legalTypes[i])) { 1017 found = true; 1018 break; 1019 } 1020 } 1021 } 1022 if (!found) { 1023 throw new IllegalArgumentException ("Unknown compression type!"); 1024 } 1025 } 1026 this.compressionType = compressionType; 1027 } 1028 1029 1052 public String getCompressionType() { 1053 if (!canWriteCompressed()) { 1054 throw new UnsupportedOperationException ( 1055 "Compression not supported."); 1056 } 1057 if (getCompressionMode() != MODE_EXPLICIT) { 1058 throw new IllegalStateException 1059 ("Compression mode not MODE_EXPLICIT!"); 1060 } 1061 return compressionType; 1062 } 1063 1064 1080 public void unsetCompression() { 1081 if (!canWriteCompressed()) { 1082 throw new UnsupportedOperationException ( 1083 "Compression not supported"); 1084 } 1085 if (getCompressionMode() != MODE_EXPLICIT) { 1086 throw new IllegalStateException 1087 ("Compression mode not MODE_EXPLICIT!"); 1088 } 1089 this.compressionType = null; 1090 this.compressionQuality = 1.0F; 1091 } 1092 1093 1114 public String getLocalizedCompressionTypeName() { 1115 if (!canWriteCompressed()) { 1116 throw new UnsupportedOperationException ( 1117 "Compression not supported."); 1118 } 1119 if (getCompressionMode() != MODE_EXPLICIT) { 1120 throw new IllegalStateException 1121 ("Compression mode not MODE_EXPLICIT!"); 1122 } 1123 if (getCompressionType() == null) { 1124 throw new IllegalStateException ("No compression type set!"); 1125 } 1126 return getCompressionType(); 1127 } 1128 1129 1156 public boolean isCompressionLossless() { 1157 if (!canWriteCompressed()) { 1158 throw new UnsupportedOperationException ( 1159 "Compression not supported"); 1160 } 1161 if (getCompressionMode() != MODE_EXPLICIT) { 1162 throw new IllegalStateException 1163 ("Compression mode not MODE_EXPLICIT!"); 1164 } 1165 if ((getCompressionTypes() != null) && 1166 (getCompressionType() == null)) { 1167 throw new IllegalStateException ("No compression type set!"); 1168 } 1169 return true; 1170 } 1171 1172 1216 public void setCompressionQuality(float quality) { 1217 if (!canWriteCompressed()) { 1218 throw new UnsupportedOperationException ( 1219 "Compression not supported"); 1220 } 1221 if (getCompressionMode() != MODE_EXPLICIT) { 1222 throw new IllegalStateException 1223 ("Compression mode not MODE_EXPLICIT!"); 1224 } 1225 if (getCompressionTypes() != null && getCompressionType() == null) { 1226 throw new IllegalStateException ("No compression type set!"); 1227 } 1228 if (quality < 0.0F || quality > 1.0F) { 1229 throw new IllegalArgumentException ("Quality out-of-bounds!"); 1230 } 1231 this.compressionQuality = quality; 1232 } 1233 1234 1260 public float getCompressionQuality() { 1261 if (!canWriteCompressed()) { 1262 throw new UnsupportedOperationException ( 1263 "Compression not supported."); 1264 } 1265 if (getCompressionMode() != MODE_EXPLICIT) { 1266 throw new IllegalStateException 1267 ("Compression mode not MODE_EXPLICIT!"); 1268 } 1269 if ((getCompressionTypes() != null) && 1270 (getCompressionType() == null)) { 1271 throw new IllegalStateException ("No compression type set!"); 1272 } 1273 return compressionQuality; 1274 } 1275 1276 1277 1313 public float getBitRate(float quality) { 1314 if (!canWriteCompressed()) { 1315 throw new UnsupportedOperationException ( 1316 "Compression not supported."); 1317 } 1318 if (getCompressionMode() != MODE_EXPLICIT) { 1319 throw new IllegalStateException 1320 ("Compression mode not MODE_EXPLICIT!"); 1321 } 1322 if ((getCompressionTypes() != null) && 1323 (getCompressionType() == null)) { 1324 throw new IllegalStateException ("No compression type set!"); 1325 } 1326 if (quality < 0.0F || quality > 1.0F) { 1327 throw new IllegalArgumentException ("Quality out-of-bounds!"); 1328 } 1329 return -1.0F; 1330 } 1331 1332 1385 public String [] getCompressionQualityDescriptions() { 1386 if (!canWriteCompressed()) { 1387 throw new UnsupportedOperationException ( 1388 "Compression not supported."); 1389 } 1390 if (getCompressionMode() != MODE_EXPLICIT) { 1391 throw new IllegalStateException 1392 ("Compression mode not MODE_EXPLICIT!"); 1393 } 1394 if ((getCompressionTypes() != null) && 1395 (getCompressionType() == null)) { 1396 throw new IllegalStateException ("No compression type set!"); 1397 } 1398 return null; 1399 } 1400 1401 1438 public float[] getCompressionQualityValues() { 1439 if (!canWriteCompressed()) { 1440 throw new UnsupportedOperationException ( 1441 "Compression not supported."); 1442 } 1443 if (getCompressionMode() != MODE_EXPLICIT) { 1444 throw new IllegalStateException 1445 ("Compression mode not MODE_EXPLICIT!"); 1446 } 1447 if ((getCompressionTypes() != null) && 1448 (getCompressionType() == null)) { 1449 throw new IllegalStateException ("No compression type set!"); 1450 } 1451 return null; 1452 } 1453} 1454 | Popular Tags |