1 7 8 package javax.imageio; 9 10 import java.awt.Dimension ; 11 import java.awt.Rectangle ; 12 import java.awt.image.BufferedImage ; 13 import java.awt.image.RenderedImage ; 14 import java.awt.image.Raster ; 15 import java.io.IOException ; 16 import java.util.ArrayList ; 17 import java.util.List ; 18 import java.util.Locale ; 19 import java.util.MissingResourceException ; 20 import java.util.ResourceBundle ; 21 import javax.imageio.event.IIOWriteWarningListener ; 22 import javax.imageio.event.IIOWriteProgressListener ; 23 import javax.imageio.metadata.IIOMetadata ; 24 import javax.imageio.stream.ImageOutputStream ; 25 import javax.imageio.spi.ImageWriterSpi ; 26 27 47 public abstract class ImageWriter implements ImageTranscoder { 48 49 54 protected ImageWriterSpi originatingProvider = null; 55 56 62 protected Object output = null; 63 64 70 protected Locale [] availableLocales = null; 71 72 77 protected Locale locale = null; 78 79 85 protected List <IIOWriteWarningListener > warningListeners = null; 86 87 93 protected List <Locale > warningLocales = null; 94 95 101 protected List <IIOWriteProgressListener > progressListeners = null; 102 103 107 private boolean abortFlag = false; 108 109 123 protected ImageWriter(ImageWriterSpi originatingProvider) { 124 this.originatingProvider = originatingProvider; 125 } 126 127 139 public ImageWriterSpi getOriginatingProvider() { 140 return originatingProvider; 141 } 142 143 189 public void setOutput(Object output) { 190 if (output != null) { 191 ImageWriterSpi provider = getOriginatingProvider(); 192 if (provider != null) { 193 Class [] classes = provider.getOutputTypes(); 194 boolean found = false; 195 for (int i = 0; i < classes.length; i++) { 196 if (classes[i].isInstance(output)) { 197 found = true; 198 break; 199 } 200 } 201 if (!found) { 202 throw new IllegalArgumentException ("Illegal output type!"); 203 } 204 } 205 } 206 207 this.output = output; 208 } 209 210 224 public Object getOutput() { 225 return output; 226 } 227 228 230 243 public Locale [] getAvailableLocales() { 244 return (availableLocales == null) ? 245 null : (Locale [])availableLocales.clone(); 246 } 247 248 270 public void setLocale(Locale locale) { 271 if (locale != null) { 272 Locale [] locales = getAvailableLocales(); 273 boolean found = false; 274 if (locales != null) { 275 for (int i = 0; i < locales.length; i++) { 276 if (locale.equals(locales[i])) { 277 found = true; 278 break; 279 } 280 } 281 } 282 if (!found) { 283 throw new IllegalArgumentException ("Invalid locale!"); 284 } 285 } 286 this.locale = locale; 287 } 288 289 300 public Locale getLocale() { 301 return locale; 302 } 303 304 306 329 public ImageWriteParam getDefaultWriteParam() { 330 return new ImageWriteParam (getLocale()); 331 } 332 333 335 363 public abstract IIOMetadata 364 getDefaultStreamMetadata(ImageWriteParam param); 365 366 392 public abstract IIOMetadata 393 getDefaultImageMetadata(ImageTypeSpecifier imageType, 394 ImageWriteParam param); 395 396 public abstract IIOMetadata convertStreamMetadata(IIOMetadata inData, 398 ImageWriteParam param); 399 400 public abstract IIOMetadata 402 convertImageMetadata(IIOMetadata inData, 403 ImageTypeSpecifier imageType, 404 ImageWriteParam param); 405 406 408 438 public int getNumThumbnailsSupported(ImageTypeSpecifier imageType, 439 ImageWriteParam param, 440 IIOMetadata streamMetadata, 441 IIOMetadata imageMetadata) { 442 return 0; 443 } 444 445 482 public Dimension [] getPreferredThumbnailSizes(ImageTypeSpecifier imageType, 483 ImageWriteParam param, 484 IIOMetadata streamMetadata, 485 IIOMetadata imageMetadata) { 486 return null; 487 } 488 489 503 public boolean canWriteRasters() { 504 return false; 505 } 506 507 558 public abstract void write(IIOMetadata streamMetadata, 559 IIOImage image, 560 ImageWriteParam param) throws IOException ; 561 562 579 public void write(IIOImage image) throws IOException { 580 write(null, image, null); 581 } 582 583 597 public void write(RenderedImage image) throws IOException { 598 write(null, new IIOImage (image, null, null), null); 599 } 600 601 private void unsupported() { 604 if (getOutput() == null) { 605 throw new IllegalStateException ("getOutput() == null!"); 606 } 607 throw new UnsupportedOperationException ("Unsupported write variant!"); 608 } 609 610 612 625 public boolean canWriteSequence() { 626 return false; 627 } 628 629 666 public void prepareWriteSequence(IIOMetadata streamMetadata) 667 throws IOException { 668 unsupported(); 669 } 670 671 731 public void writeToSequence(IIOImage image, ImageWriteParam param) 732 throws IOException { 733 unsupported(); 734 } 735 736 761 public void endWriteSequence() throws IOException { 762 unsupported(); 763 } 764 765 767 782 public boolean canReplaceStreamMetadata() throws IOException { 783 if (getOutput() == null) { 784 throw new IllegalStateException ("getOutput() == null!"); 785 } 786 return false; 787 } 788 789 816 public void replaceStreamMetadata(IIOMetadata streamMetadata) 817 throws IOException { 818 unsupported(); 819 } 820 821 852 public boolean canReplaceImageMetadata(int imageIndex) 853 throws IOException { 854 if (getOutput() == null) { 855 throw new IllegalStateException ("getOutput() == null!"); 856 } 857 return false; 858 } 859 860 886 public void replaceImageMetadata(int imageIndex, 887 IIOMetadata imageMetadata) 888 throws IOException { 889 unsupported(); 890 } 891 892 894 924 public boolean canInsertImage(int imageIndex) throws IOException { 925 if (getOutput() == null) { 926 throw new IllegalStateException ("getOutput() == null!"); 927 } 928 return false; 929 } 930 931 979 public void writeInsert(int imageIndex, 980 IIOImage image, 981 ImageWriteParam param) throws IOException { 982 unsupported(); 983 } 984 985 987 1015 public boolean canRemoveImage(int imageIndex) throws IOException { 1016 if (getOutput() == null) { 1017 throw new IllegalStateException ("getOutput() == null!"); 1018 } 1019 return false; 1020 } 1021 1022 1047 public void removeImage(int imageIndex) throws IOException { 1048 unsupported(); 1049 } 1050 1051 1053 1075 public boolean canWriteEmpty() throws IOException { 1076 if (getOutput() == null) { 1077 throw new IllegalStateException ("getOutput() == null!"); 1078 } 1079 return false; 1080 } 1081 1082 1150 public void prepareWriteEmpty(IIOMetadata streamMetadata, 1151 ImageTypeSpecifier imageType, 1152 int width, int height, 1153 IIOMetadata imageMetadata, 1154 List <? extends BufferedImage > thumbnails, 1155 ImageWriteParam param) throws IOException { 1156 unsupported(); 1157 } 1158 1159 1187 public void endWriteEmpty() throws IOException { 1188 if (getOutput() == null) { 1189 throw new IllegalStateException ("getOutput() == null!"); 1190 } 1191 throw new IllegalStateException ("No call to prepareWriteEmpty!"); 1192 } 1193 1194 1227 public boolean canInsertEmpty(int imageIndex) throws IOException { 1228 if (getOutput() == null) { 1229 throw new IllegalStateException ("getOutput() == null!"); 1230 } 1231 return false; 1232 } 1233 1234 1310 public void prepareInsertEmpty(int imageIndex, 1311 ImageTypeSpecifier imageType, 1312 int width, int height, 1313 IIOMetadata imageMetadata, 1314 List <? extends BufferedImage > thumbnails, 1315 ImageWriteParam param) throws IOException { 1316 unsupported(); 1317 } 1318 1319 1344 public void endInsertEmpty() throws IOException { 1345 unsupported(); 1346 } 1347 1348 1350 1377 public boolean canReplacePixels(int imageIndex) throws IOException { 1378 if (getOutput() == null) { 1379 throw new IllegalStateException ("getOutput() == null!"); 1380 } 1381 return false; 1382 } 1383 1384 1419 public void prepareReplacePixels(int imageIndex, 1420 Rectangle region) throws IOException { 1421 unsupported(); 1422 } 1423 1424 1476 public void replacePixels(RenderedImage image, ImageWriteParam param) 1477 throws IOException { 1478 unsupported(); 1479 } 1480 1481 1536 public void replacePixels(Raster raster, ImageWriteParam param) 1537 throws IOException { 1538 unsupported(); 1539 } 1540 1541 1563 public void endReplacePixels() throws IOException { 1564 unsupported(); 1565 } 1566 1567 1569 1577 public synchronized void abort() { 1578 this.abortFlag = true; 1579 } 1580 1581 1592 protected synchronized boolean abortRequested() { 1593 return this.abortFlag; 1594 } 1595 1596 1604 protected synchronized void clearAbortRequest() { 1605 this.abortFlag = false; 1606 } 1607 1608 1610 1624 public void addIIOWriteWarningListener(IIOWriteWarningListener listener) { 1625 if (listener == null) { 1626 return; 1627 } 1628 warningListeners = ImageReader.addToList(warningListeners, listener); 1629 warningLocales = ImageReader.addToList(warningLocales, getLocale()); 1630 } 1631 1632 1644 public 1645 void removeIIOWriteWarningListener(IIOWriteWarningListener listener) { 1646 if (listener == null || warningListeners == null) { 1647 return; 1648 } 1649 int index = warningListeners.indexOf(listener); 1650 if (index != -1) { 1651 warningListeners.remove(index); 1652 warningLocales.remove(index); 1653 if (warningListeners.size() == 0) { 1654 warningListeners = null; 1655 warningLocales = null; 1656 } 1657 } 1658 } 1659 1660 1668 public void removeAllIIOWriteWarningListeners() { 1669 this.warningListeners = null; 1670 this.warningLocales = null; 1671 } 1672 1673 1684 public void 1685 addIIOWriteProgressListener(IIOWriteProgressListener listener) { 1686 if (listener == null) { 1687 return; 1688 } 1689 progressListeners = ImageReader.addToList(progressListeners, listener); 1690 } 1691 1692 1704 public void 1705 removeIIOWriteProgressListener(IIOWriteProgressListener listener) { 1706 if (listener == null || progressListeners == null) { 1707 return; 1708 } 1709 progressListeners = 1710 ImageReader.removeFromList(progressListeners, listener); 1711 } 1712 1713 1721 public void removeAllIIOWriteProgressListeners() { 1722 this.progressListeners = null; 1723 } 1724 1725 1733 protected void processImageStarted(int imageIndex) { 1734 if (progressListeners == null) { 1735 return; 1736 } 1737 int numListeners = progressListeners.size(); 1738 for (int i = 0; i < numListeners; i++) { 1739 IIOWriteProgressListener listener = 1740 (IIOWriteProgressListener )progressListeners.get(i); 1741 listener.imageStarted(this, imageIndex); 1742 } 1743 } 1744 1745 1754 protected void processImageProgress(float percentageDone) { 1755 if (progressListeners == null) { 1756 return; 1757 } 1758 int numListeners = progressListeners.size(); 1759 for (int i = 0; i < numListeners; i++) { 1760 IIOWriteProgressListener listener = 1761 (IIOWriteProgressListener )progressListeners.get(i); 1762 listener.imageProgress(this, percentageDone); 1763 } 1764 } 1765 1766 1772 protected void processImageComplete() { 1773 if (progressListeners == null) { 1774 return; 1775 } 1776 int numListeners = progressListeners.size(); 1777 for (int i = 0; i < numListeners; i++) { 1778 IIOWriteProgressListener listener = 1779 (IIOWriteProgressListener )progressListeners.get(i); 1780 listener.imageComplete(this); 1781 } 1782 } 1783 1784 1794 protected void processThumbnailStarted(int imageIndex, 1795 int thumbnailIndex) { 1796 if (progressListeners == null) { 1797 return; 1798 } 1799 int numListeners = progressListeners.size(); 1800 for (int i = 0; i < numListeners; i++) { 1801 IIOWriteProgressListener listener = 1802 (IIOWriteProgressListener )progressListeners.get(i); 1803 listener.thumbnailStarted(this, imageIndex, thumbnailIndex); 1804 } 1805 } 1806 1807 1816 protected void processThumbnailProgress(float percentageDone) { 1817 if (progressListeners == null) { 1818 return; 1819 } 1820 int numListeners = progressListeners.size(); 1821 for (int i = 0; i < numListeners; i++) { 1822 IIOWriteProgressListener listener = 1823 (IIOWriteProgressListener )progressListeners.get(i); 1824 listener.thumbnailProgress(this, percentageDone); 1825 } 1826 } 1827 1828 1834 protected void processThumbnailComplete() { 1835 if (progressListeners == null) { 1836 return; 1837 } 1838 int numListeners = progressListeners.size(); 1839 for (int i = 0; i < numListeners; i++) { 1840 IIOWriteProgressListener listener = 1841 (IIOWriteProgressListener )progressListeners.get(i); 1842 listener.thumbnailComplete(this); 1843 } 1844 } 1845 1846 1852 protected void processWriteAborted() { 1853 if (progressListeners == null) { 1854 return; 1855 } 1856 int numListeners = progressListeners.size(); 1857 for (int i = 0; i < numListeners; i++) { 1858 IIOWriteProgressListener listener = 1859 (IIOWriteProgressListener )progressListeners.get(i); 1860 listener.writeAborted(this); 1861 } 1862 } 1863 1864 1877 protected void processWarningOccurred(int imageIndex, 1878 String warning) { 1879 if (warningListeners == null) { 1880 return; 1881 } 1882 if (warning == null) { 1883 throw new IllegalArgumentException ("warning == null!"); 1884 } 1885 int numListeners = warningListeners.size(); 1886 for (int i = 0; i < numListeners; i++) { 1887 IIOWriteWarningListener listener = 1888 (IIOWriteWarningListener )warningListeners.get(i); 1889 1890 listener.warningOccurred(this, imageIndex, warning); 1891 } 1892 } 1893 1894 1921 protected void processWarningOccurred(int imageIndex, 1922 String baseName, 1923 String keyword) { 1924 if (warningListeners == null) { 1925 return; 1926 } 1927 if (baseName == null) { 1928 throw new IllegalArgumentException ("baseName == null!"); 1929 } 1930 if (keyword == null) { 1931 throw new IllegalArgumentException ("keyword == null!"); 1932 } 1933 int numListeners = warningListeners.size(); 1934 for (int i = 0; i < numListeners; i++) { 1935 IIOWriteWarningListener listener = 1936 (IIOWriteWarningListener )warningListeners.get(i); 1937 Locale locale = (Locale )warningLocales.get(i); 1938 if (locale == null) { 1939 locale = Locale.getDefault(); 1940 } 1941 1942 1950 ClassLoader loader = (ClassLoader ) 1951 java.security.AccessController.doPrivileged( 1952 new java.security.PrivilegedAction () { 1953 public Object run() { 1954 return Thread.currentThread().getContextClassLoader(); 1955 } 1956 }); 1957 1958 ResourceBundle bundle = null; 1959 try { 1960 bundle = ResourceBundle.getBundle(baseName, locale, loader); 1961 } catch (MissingResourceException mre) { 1962 try { 1963 bundle = ResourceBundle.getBundle(baseName, locale); 1964 } catch (MissingResourceException mre1) { 1965 throw new IllegalArgumentException ("Bundle not found!"); 1966 } 1967 } 1968 1969 String warning = null; 1970 try { 1971 warning = bundle.getString(keyword); 1972 } catch (ClassCastException cce) { 1973 throw new IllegalArgumentException ("Resource is not a String!"); 1974 } catch (MissingResourceException mre) { 1975 throw new IllegalArgumentException ("Resource is missing!"); 1976 } 1977 1978 listener.warningOccurred(this, imageIndex, warning); 1979 } 1980 } 1981 1982 1984 1993 public void reset() { 1994 setOutput(null); 1995 setLocale(null); 1996 removeAllIIOWriteWarningListeners(); 1997 removeAllIIOWriteProgressListeners(); 1998 clearAbortRequest(); 1999 } 2000 2001 2016 public void dispose() { 2017 } 2018} 2019 | Popular Tags |