1 7 8 package javax.imageio; 9 10 import java.awt.Point ; 11 import java.awt.Rectangle ; 12 import java.awt.image.BufferedImage ; 13 import java.awt.image.Raster ; 14 import java.awt.image.RenderedImage ; 15 import java.io.IOException ; 16 import java.util.ArrayList ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Locale ; 20 import java.util.MissingResourceException ; 21 import java.util.ResourceBundle ; 22 import java.util.Set ; 23 import javax.imageio.spi.ImageReaderSpi ; 24 import javax.imageio.event.IIOReadWarningListener ; 25 import javax.imageio.event.IIOReadProgressListener ; 26 import javax.imageio.event.IIOReadUpdateListener ; 27 import javax.imageio.metadata.IIOMetadata ; 28 import javax.imageio.metadata.IIOMetadataFormatImpl ; 29 import javax.imageio.stream.ImageInputStream ; 30 31 56 public abstract class ImageReader { 57 58 63 protected ImageReaderSpi originatingProvider; 64 65 71 protected Object input = null; 72 73 81 protected boolean seekForwardOnly = false; 82 83 90 protected boolean ignoreMetadata = false; 91 92 102 protected int minIndex = 0; 103 104 109 protected Locale [] availableLocales = null; 110 111 115 protected Locale locale = null; 116 117 123 protected List <IIOReadWarningListener > warningListeners = null; 124 125 131 protected List <Locale > warningLocales = null; 132 133 139 protected List <IIOReadProgressListener > progressListeners = null; 140 141 147 protected List <IIOReadUpdateListener > updateListeners = null; 148 149 153 private boolean abortFlag = false; 154 155 168 protected ImageReader(ImageReaderSpi originatingProvider) { 169 this.originatingProvider = originatingProvider; 170 } 171 172 187 public String getFormatName() throws IOException { 188 return originatingProvider.getFormatNames()[0]; 189 } 190 191 199 public ImageReaderSpi getOriginatingProvider() { 200 return originatingProvider; 201 } 202 203 271 public void setInput(Object input, 272 boolean seekForwardOnly, 273 boolean ignoreMetadata) { 274 if (input != null) { 275 boolean found = false; 276 if (originatingProvider != null) { 277 Class [] classes = originatingProvider.getInputTypes(); 278 for (int i = 0; i < classes.length; i++) { 279 if (classes[i].isInstance(input)) { 280 found = true; 281 break; 282 } 283 } 284 } else { 285 if (input instanceof ImageInputStream ) { 286 found = true; 287 } 288 } 289 if (!found) { 290 throw new IllegalArgumentException ("Incorrect input type!"); 291 } 292 293 this.seekForwardOnly = seekForwardOnly; 294 this.ignoreMetadata = ignoreMetadata; 295 this.minIndex = 0; 296 } 297 298 this.input = input; 299 } 300 301 336 public void setInput(Object input, 337 boolean seekForwardOnly) { 338 setInput(input, seekForwardOnly, false); 339 } 340 341 362 public void setInput(Object input) { 363 setInput(input, false, false); 364 } 365 366 377 public Object getInput() { 378 return input; 379 } 380 381 392 public boolean isSeekForwardOnly() { 393 return seekForwardOnly; 394 } 395 396 406 public boolean isIgnoringMetadata() { 407 return ignoreMetadata; 408 } 409 410 420 public int getMinIndex() { 421 return minIndex; 422 } 423 424 426 439 public Locale [] getAvailableLocales() { 440 if (availableLocales == null) { 441 return null; 442 } else { 443 return (Locale [])availableLocales.clone(); 444 } 445 } 446 447 462 public void setLocale(Locale locale) { 463 if (locale != null) { 464 Locale [] locales = getAvailableLocales(); 465 boolean found = false; 466 if (locales != null) { 467 for (int i = 0; i < locales.length; i++) { 468 if (locale.equals(locales[i])) { 469 found = true; 470 break; 471 } 472 } 473 } 474 if (!found) { 475 throw new IllegalArgumentException ("Invalid locale!"); 476 } 477 } 478 this.locale = locale; 479 } 480 481 489 public Locale getLocale() { 490 return locale; 491 } 492 493 495 532 public abstract int getNumImages(boolean allowSearch) throws IOException ; 533 534 551 public abstract int getWidth(int imageIndex) throws IOException ; 552 553 570 public abstract int getHeight(int imageIndex) throws IOException ; 571 572 611 public boolean isRandomAccessEasy(int imageIndex) throws IOException { 612 return false; 613 } 614 615 636 public float getAspectRatio(int imageIndex) throws IOException { 637 return (float)getWidth(imageIndex)/getHeight(imageIndex); 638 } 639 640 663 public ImageTypeSpecifier getRawImageType(int imageIndex) 664 throws IOException { 665 return (ImageTypeSpecifier )getImageTypes(imageIndex).next(); 666 } 667 668 696 public abstract Iterator <ImageTypeSpecifier > 697 getImageTypes(int imageIndex) throws IOException ; 698 699 714 public ImageReadParam getDefaultReadParam() { 715 return new ImageReadParam (); 716 } 717 718 729 public abstract IIOMetadata getStreamMetadata() throws IOException ; 730 731 772 public IIOMetadata getStreamMetadata(String formatName, 773 Set <String > nodeNames) 774 throws IOException 775 { 776 return getMetadata(formatName, nodeNames, true, 0); 777 } 778 779 private IIOMetadata getMetadata(String formatName, 780 Set nodeNames, 781 boolean wantStream, 782 int imageIndex) throws IOException { 783 if (formatName == null) { 784 throw new IllegalArgumentException ("formatName == null!"); 785 } 786 if (nodeNames == null) { 787 throw new IllegalArgumentException ("nodeNames == null!"); 788 } 789 IIOMetadata metadata = 790 wantStream 791 ? getStreamMetadata() 792 : getImageMetadata(imageIndex); 793 if (metadata != null) { 794 if (metadata.isStandardMetadataFormatSupported() && 795 formatName.equals 796 (IIOMetadataFormatImpl.standardMetadataFormatName)) { 797 return metadata; 798 } 799 String nativeName = metadata.getNativeMetadataFormatName(); 800 if (nativeName != null && formatName.equals(nativeName)) { 801 return metadata; 802 } 803 String [] extraNames = metadata.getExtraMetadataFormatNames(); 804 if (extraNames != null) { 805 for (int i = 0; i < extraNames.length; i++) { 806 if (formatName.equals(extraNames[i])) { 807 return metadata; 808 } 809 } 810 } 811 } 812 return null; 813 } 814 815 833 public abstract IIOMetadata getImageMetadata(int imageIndex) 834 throws IOException ; 835 836 883 public IIOMetadata getImageMetadata(int imageIndex, 884 String formatName, 885 Set <String > nodeNames) 886 throws IOException { 887 return getMetadata(formatName, nodeNames, false, imageIndex); 888 } 889 890 922 public BufferedImage read(int imageIndex) throws IOException { 923 return read(imageIndex, null); 924 } 925 926 981 public abstract BufferedImage read(int imageIndex, ImageReadParam param) 982 throws IOException ; 983 984 1044 public IIOImage readAll(int imageIndex, ImageReadParam param) 1045 throws IOException { 1046 if (imageIndex < getMinIndex()) { 1047 throw new IndexOutOfBoundsException ("imageIndex < getMinIndex()!"); 1048 } 1049 1050 BufferedImage im = read(imageIndex, param); 1051 1052 ArrayList thumbnails = null; 1053 int numThumbnails = getNumThumbnails(imageIndex); 1054 if (numThumbnails > 0) { 1055 thumbnails = new ArrayList (); 1056 for (int j = 0; j < numThumbnails; j++) { 1057 thumbnails.add(readThumbnail(imageIndex, j)); 1058 } 1059 } 1060 1061 IIOMetadata metadata = getImageMetadata(imageIndex); 1062 return new IIOImage (im, thumbnails, metadata); 1063 } 1064 1065 1139 public Iterator <IIOImage > 1140 readAll(Iterator <? extends ImageReadParam > params) 1141 throws IOException 1142 { 1143 List output = new ArrayList (); 1144 1145 int imageIndex = getMinIndex(); 1146 1147 processSequenceStarted(imageIndex); 1149 1150 while (true) { 1151 1154 ImageReadParam param = null; 1155 if (params != null && params.hasNext()) { 1156 Object o = params.next(); 1157 if (o != null) { 1158 if (o instanceof ImageReadParam ) { 1159 param = (ImageReadParam )o; 1160 } else { 1161 throw new IllegalArgumentException 1162 ("Non-ImageReadParam supplied as part of params!"); 1163 } 1164 } 1165 } 1166 1167 BufferedImage bi = null; 1168 try { 1169 bi = read(imageIndex, param); 1170 } catch (IndexOutOfBoundsException e) { 1171 break; 1172 } 1173 1174 ArrayList thumbnails = null; 1175 int numThumbnails = getNumThumbnails(imageIndex); 1176 if (numThumbnails > 0) { 1177 thumbnails = new ArrayList (); 1178 for (int j = 0; j < numThumbnails; j++) { 1179 thumbnails.add(readThumbnail(imageIndex, j)); 1180 } 1181 } 1182 1183 IIOMetadata metadata = getImageMetadata(imageIndex); 1184 IIOImage im = new IIOImage (bi, thumbnails, metadata); 1185 output.add(im); 1186 1187 ++imageIndex; 1188 } 1189 1190 processSequenceComplete(); 1192 1193 return output.iterator(); 1194 } 1195 1196 1212 public boolean canReadRaster() { 1213 return false; 1214 } 1215 1216 1269 public Raster readRaster(int imageIndex, ImageReadParam param) 1270 throws IOException { 1271 throw new UnsupportedOperationException ("readRaster not supported!"); 1272 } 1273 1274 1307 public boolean isImageTiled(int imageIndex) throws IOException { 1308 return false; 1309 } 1310 1311 1328 public int getTileWidth(int imageIndex) throws IOException { 1329 return getWidth(imageIndex); 1330 } 1331 1332 1349 public int getTileHeight(int imageIndex) throws IOException { 1350 return getHeight(imageIndex); 1351 } 1352 1353 1379 public int getTileGridXOffset(int imageIndex) throws IOException { 1380 return 0; 1381 } 1382 1383 1409 public int getTileGridYOffset(int imageIndex) throws IOException { 1410 return 0; 1411 } 1412 1413 1453 public BufferedImage readTile(int imageIndex, 1454 int tileX, int tileY) throws IOException { 1455 if ((tileX != 0) || (tileY != 0)) { 1456 throw new IllegalArgumentException ("Invalid tile indices"); 1457 } 1458 return read(imageIndex); 1459 } 1460 1461 1499 public Raster readTileRaster(int imageIndex, 1500 int tileX, int tileY) throws IOException { 1501 if (!canReadRaster()) { 1502 throw new UnsupportedOperationException 1503 ("readTileRaster not supported!"); 1504 } 1505 if ((tileX != 0) || (tileY != 0)) { 1506 throw new IllegalArgumentException ("Invalid tile indices"); 1507 } 1508 return readRaster(imageIndex, null); 1509 } 1510 1511 1513 1557 public RenderedImage readAsRenderedImage(int imageIndex, 1558 ImageReadParam param) 1559 throws IOException { 1560 return read(imageIndex, param); 1561 } 1562 1563 1565 1582 public boolean readerSupportsThumbnails() { 1583 return false; 1584 } 1585 1586 1607 public boolean hasThumbnails(int imageIndex) throws IOException { 1608 return getNumThumbnails(imageIndex) > 0; 1609 } 1610 1611 1633 public int getNumThumbnails(int imageIndex) 1634 throws IOException { 1635 return 0; 1636 } 1637 1638 1666 public int getThumbnailWidth(int imageIndex, int thumbnailIndex) 1667 throws IOException { 1668 return readThumbnail(imageIndex, thumbnailIndex).getWidth(); 1669 } 1670 1671 1699 public int getThumbnailHeight(int imageIndex, int thumbnailIndex) 1700 throws IOException { 1701 return readThumbnail(imageIndex, thumbnailIndex).getHeight(); 1702 } 1703 1704 1735 public BufferedImage readThumbnail(int imageIndex, 1736 int thumbnailIndex) 1737 throws IOException { 1738 throw new UnsupportedOperationException ("Thumbnails not supported!"); 1739 } 1740 1741 1743 1751 public synchronized void abort() { 1752 this.abortFlag = true; 1753 } 1754 1755 1766 protected synchronized boolean abortRequested() { 1767 return this.abortFlag; 1768 } 1769 1770 1778 protected synchronized void clearAbortRequest() { 1779 this.abortFlag = false; 1780 } 1781 1782 1784 static List addToList(List l, Object elt) { 1787 if (l == null) { 1788 l = new ArrayList (); 1789 } 1790 l.add(elt); 1791 return l; 1792 } 1793 1794 1795 static List removeFromList(List l, Object elt) { 1798 if (l == null) { 1799 return l; 1800 } 1801 l.remove(elt); 1802 if (l.size() == 0) { 1803 l = null; 1804 } 1805 return l; 1806 } 1807 1808 1821 public void addIIOReadWarningListener(IIOReadWarningListener listener) { 1822 if (listener == null) { 1823 return; 1824 } 1825 warningListeners = addToList(warningListeners, listener); 1826 warningLocales = addToList(warningLocales, getLocale()); 1827 } 1828 1829 1839 public void removeIIOReadWarningListener(IIOReadWarningListener listener) { 1840 if (listener == null || warningListeners == null) { 1841 return; 1842 } 1843 int index = warningListeners.indexOf(listener); 1844 if (index != -1) { 1845 warningListeners.remove(index); 1846 warningLocales.remove(index); 1847 if (warningListeners.size() == 0) { 1848 warningListeners = null; 1849 warningLocales = null; 1850 } 1851 } 1852 } 1853 1854 1862 public void removeAllIIOReadWarningListeners() { 1863 warningListeners = null; 1864 warningLocales = null; 1865 } 1866 1867 1877 public void addIIOReadProgressListener(IIOReadProgressListener listener) { 1878 if (listener == null) { 1879 return; 1880 } 1881 progressListeners = addToList(progressListeners, listener); 1882 } 1883 1884 1895 public void 1896 removeIIOReadProgressListener (IIOReadProgressListener listener) { 1897 if (listener == null || progressListeners == null) { 1898 return; 1899 } 1900 progressListeners = removeFromList(progressListeners, listener); 1901 } 1902 1903 1911 public void removeAllIIOReadProgressListeners() { 1912 progressListeners = null; 1913 } 1914 1915 1948 public void 1949 addIIOReadUpdateListener(IIOReadUpdateListener listener) { 1950 if (listener == null) { 1951 return; 1952 } 1953 updateListeners = addToList(updateListeners, listener); 1954 } 1955 1956 1967 public void removeIIOReadUpdateListener(IIOReadUpdateListener listener) { 1968 if (listener == null || updateListeners == null) { 1969 return; 1970 } 1971 updateListeners = removeFromList(updateListeners, listener); 1972 } 1973 1974 1982 public void removeAllIIOReadUpdateListeners() { 1983 updateListeners = null; 1984 } 1985 1986 1994 protected void processSequenceStarted(int minIndex) { 1995 if (progressListeners == null) { 1996 return; 1997 } 1998 int numListeners = progressListeners.size(); 1999 for (int i = 0; i < numListeners; i++) { 2000 IIOReadProgressListener listener = 2001 (IIOReadProgressListener )progressListeners.get(i); 2002 listener.sequenceStarted(this, minIndex); 2003 } 2004 } 2005 2006 2012 protected void processSequenceComplete() { 2013 if (progressListeners == null) { 2014 return; 2015 } 2016 int numListeners = progressListeners.size(); 2017 for (int i = 0; i < numListeners; i++) { 2018 IIOReadProgressListener listener = 2019 (IIOReadProgressListener )progressListeners.get(i); 2020 listener.sequenceComplete(this); 2021 } 2022 } 2023 2024 2032 protected void processImageStarted(int imageIndex) { 2033 if (progressListeners == null) { 2034 return; 2035 } 2036 int numListeners = progressListeners.size(); 2037 for (int i = 0; i < numListeners; i++) { 2038 IIOReadProgressListener listener = 2039 (IIOReadProgressListener )progressListeners.get(i); 2040 listener.imageStarted(this, imageIndex); 2041 } 2042 } 2043 2044 2053 protected void processImageProgress(float percentageDone) { 2054 if (progressListeners == null) { 2055 return; 2056 } 2057 int numListeners = progressListeners.size(); 2058 for (int i = 0; i < numListeners; i++) { 2059 IIOReadProgressListener listener = 2060 (IIOReadProgressListener )progressListeners.get(i); 2061 listener.imageProgress(this, percentageDone); 2062 } 2063 } 2064 2065 2071 protected void processImageComplete() { 2072 if (progressListeners == null) { 2073 return; 2074 } 2075 int numListeners = progressListeners.size(); 2076 for (int i = 0; i < numListeners; i++) { 2077 IIOReadProgressListener listener = 2078 (IIOReadProgressListener )progressListeners.get(i); 2079 listener.imageComplete(this); 2080 } 2081 } 2082 2083 2093 protected void processThumbnailStarted(int imageIndex, 2094 int thumbnailIndex) { 2095 if (progressListeners == null) { 2096 return; 2097 } 2098 int numListeners = progressListeners.size(); 2099 for (int i = 0; i < numListeners; i++) { 2100 IIOReadProgressListener listener = 2101 (IIOReadProgressListener )progressListeners.get(i); 2102 listener.thumbnailStarted(this, imageIndex, thumbnailIndex); 2103 } 2104 } 2105 2106 2115 protected void processThumbnailProgress(float percentageDone) { 2116 if (progressListeners == null) { 2117 return; 2118 } 2119 int numListeners = progressListeners.size(); 2120 for (int i = 0; i < numListeners; i++) { 2121 IIOReadProgressListener listener = 2122 (IIOReadProgressListener )progressListeners.get(i); 2123 listener.thumbnailProgress(this, percentageDone); 2124 } 2125 } 2126 2127 2133 protected void processThumbnailComplete() { 2134 if (progressListeners == null) { 2135 return; 2136 } 2137 int numListeners = progressListeners.size(); 2138 for (int i = 0; i < numListeners; i++) { 2139 IIOReadProgressListener listener = 2140 (IIOReadProgressListener )progressListeners.get(i); 2141 listener.thumbnailComplete(this); 2142 } 2143 } 2144 2145 2151 protected void processReadAborted() { 2152 if (progressListeners == null) { 2153 return; 2154 } 2155 int numListeners = progressListeners.size(); 2156 for (int i = 0; i < numListeners; i++) { 2157 IIOReadProgressListener listener = 2158 (IIOReadProgressListener )progressListeners.get(i); 2159 listener.readAborted(this); 2160 } 2161 } 2162 2163 2182 protected void processPassStarted(BufferedImage theImage, 2183 int pass, 2184 int minPass, int maxPass, 2185 int minX, int minY, 2186 int periodX, int periodY, 2187 int[] bands) { 2188 if (updateListeners == null) { 2189 return; 2190 } 2191 int numListeners = updateListeners.size(); 2192 for (int i = 0; i < numListeners; i++) { 2193 IIOReadUpdateListener listener = 2194 (IIOReadUpdateListener )updateListeners.get(i); 2195 listener.passStarted(this, theImage, pass, 2196 minPass, 2197 maxPass, 2198 minX, minY, 2199 periodX, periodY, 2200 bands); 2201 } 2202 } 2203 2204 2224 protected void processImageUpdate(BufferedImage theImage, 2225 int minX, int minY, 2226 int width, int height, 2227 int periodX, int periodY, 2228 int[] bands) { 2229 if (updateListeners == null) { 2230 return; 2231 } 2232 int numListeners = updateListeners.size(); 2233 for (int i = 0; i < numListeners; i++) { 2234 IIOReadUpdateListener listener = 2235 (IIOReadUpdateListener )updateListeners.get(i); 2236 listener.imageUpdate(this, 2237 theImage, 2238 minX, minY, 2239 width, height, 2240 periodX, periodY, 2241 bands); 2242 } 2243 } 2244 2245 2253 protected void processPassComplete(BufferedImage theImage) { 2254 if (updateListeners == null) { 2255 return; 2256 } 2257 int numListeners = updateListeners.size(); 2258 for (int i = 0; i < numListeners; i++) { 2259 IIOReadUpdateListener listener = 2260 (IIOReadUpdateListener )updateListeners.get(i); 2261 listener.passComplete(this, theImage); 2262 } 2263 } 2264 2265 2285 protected void processThumbnailPassStarted(BufferedImage theThumbnail, 2286 int pass, 2287 int minPass, int maxPass, 2288 int minX, int minY, 2289 int periodX, int periodY, 2290 int[] bands) { 2291 if (updateListeners == null) { 2292 return; 2293 } 2294 int numListeners = updateListeners.size(); 2295 for (int i = 0; i < numListeners; i++) { 2296 IIOReadUpdateListener listener = 2297 (IIOReadUpdateListener )updateListeners.get(i); 2298 listener.thumbnailPassStarted(this, theThumbnail, pass, 2299 minPass, 2300 maxPass, 2301 minX, minY, 2302 periodX, periodY, 2303 bands); 2304 } 2305 } 2306 2307 2328 protected void processThumbnailUpdate(BufferedImage theThumbnail, 2329 int minX, int minY, 2330 int width, int height, 2331 int periodX, int periodY, 2332 int[] bands) { 2333 if (updateListeners == null) { 2334 return; 2335 } 2336 int numListeners = updateListeners.size(); 2337 for (int i = 0; i < numListeners; i++) { 2338 IIOReadUpdateListener listener = 2339 (IIOReadUpdateListener )updateListeners.get(i); 2340 listener.thumbnailUpdate(this, 2341 theThumbnail, 2342 minX, minY, 2343 width, height, 2344 periodX, periodY, 2345 bands); 2346 } 2347 } 2348 2349 2358 protected void processThumbnailPassComplete(BufferedImage theThumbnail) { 2359 if (updateListeners == null) { 2360 return; 2361 } 2362 int numListeners = updateListeners.size(); 2363 for (int i = 0; i < numListeners; i++) { 2364 IIOReadUpdateListener listener = 2365 (IIOReadUpdateListener )updateListeners.get(i); 2366 listener.thumbnailPassComplete(this, theThumbnail); 2367 } 2368 } 2369 2370 2381 protected void processWarningOccurred(String warning) { 2382 if (warningListeners == null) { 2383 return; 2384 } 2385 if (warning == null) { 2386 throw new IllegalArgumentException ("warning == null!"); 2387 } 2388 int numListeners = warningListeners.size(); 2389 for (int i = 0; i < numListeners; i++) { 2390 IIOReadWarningListener listener = 2391 (IIOReadWarningListener )warningListeners.get(i); 2392 2393 listener.warningOccurred(this, warning); 2394 } 2395 } 2396 2397 2422 protected void processWarningOccurred(String baseName, 2423 String keyword) { 2424 if (warningListeners == null) { 2425 return; 2426 } 2427 if (baseName == null) { 2428 throw new IllegalArgumentException ("baseName == null!"); 2429 } 2430 if (keyword == null) { 2431 throw new IllegalArgumentException ("keyword == null!"); 2432 } 2433 int numListeners = warningListeners.size(); 2434 for (int i = 0; i < numListeners; i++) { 2435 IIOReadWarningListener listener = 2436 (IIOReadWarningListener )warningListeners.get(i); 2437 Locale locale = (Locale )warningLocales.get(i); 2438 if (locale == null) { 2439 locale = Locale.getDefault(); 2440 } 2441 2442 2450 ClassLoader loader = (ClassLoader ) 2451 java.security.AccessController.doPrivileged( 2452 new java.security.PrivilegedAction () { 2453 public Object run() { 2454 return Thread.currentThread().getContextClassLoader(); 2455 } 2456 }); 2457 2458 ResourceBundle bundle = null; 2459 try { 2460 bundle = ResourceBundle.getBundle(baseName, locale, loader); 2461 } catch (MissingResourceException mre) { 2462 try { 2463 bundle = ResourceBundle.getBundle(baseName, locale); 2464 } catch (MissingResourceException mre1) { 2465 throw new IllegalArgumentException ("Bundle not found!"); 2466 } 2467 } 2468 2469 String warning = null; 2470 try { 2471 warning = bundle.getString(keyword); 2472 } catch (ClassCastException cce) { 2473 throw new IllegalArgumentException ("Resource is not a String!"); 2474 } catch (MissingResourceException mre) { 2475 throw new IllegalArgumentException ("Resource is missing!"); 2476 } 2477 2478 listener.warningOccurred(this, warning); 2479 } 2480 } 2481 2482 2484 2494 public void reset() { 2495 setInput(null, false, false); 2496 setLocale(null); 2497 removeAllIIOReadUpdateListeners(); 2498 removeAllIIOReadProgressListeners(); 2499 removeAllIIOReadWarningListeners(); 2500 clearAbortRequest(); 2501 } 2502 2503 2518 public void dispose() { 2519 } 2520 2521 2523 2541 protected static Rectangle getSourceRegion(ImageReadParam param, 2542 int srcWidth, 2543 int srcHeight) { 2544 Rectangle sourceRegion = new Rectangle (0, 0, srcWidth, srcHeight); 2545 if (param != null) { 2546 Rectangle region = param.getSourceRegion(); 2547 if (region != null) { 2548 sourceRegion = sourceRegion.intersection(region); 2549 } 2550 2551 int subsampleXOffset = param.getSubsamplingXOffset(); 2552 int subsampleYOffset = param.getSubsamplingYOffset(); 2553 sourceRegion.x += subsampleXOffset; 2554 sourceRegion.y += subsampleYOffset; 2555 sourceRegion.width -= subsampleXOffset; 2556 sourceRegion.height -= subsampleYOffset; 2557 } 2558 2559 return sourceRegion; 2560 } 2561 2562 2606 protected static void computeRegions(ImageReadParam param, 2607 int srcWidth, 2608 int srcHeight, 2609 BufferedImage image, 2610 Rectangle srcRegion, 2611 Rectangle destRegion) { 2612 if (srcRegion == null) { 2613 throw new IllegalArgumentException ("srcRegion == null!"); 2614 } 2615 if (destRegion == null) { 2616 throw new IllegalArgumentException ("destRegion == null!"); 2617 } 2618 2619 srcRegion.setBounds(0, 0, srcWidth, srcHeight); 2621 2622 destRegion.setBounds(0, 0, srcWidth, srcHeight); 2625 2626 int periodX = 1; 2628 int periodY = 1; 2629 int gridX = 0; 2630 int gridY = 0; 2631 if (param != null) { 2632 Rectangle paramSrcRegion = param.getSourceRegion(); 2633 if (paramSrcRegion != null) { 2634 srcRegion.setBounds(srcRegion.intersection(paramSrcRegion)); 2635 } 2636 periodX = param.getSourceXSubsampling(); 2637 periodY = param.getSourceYSubsampling(); 2638 gridX = param.getSubsamplingXOffset(); 2639 gridY = param.getSubsamplingYOffset(); 2640 srcRegion.translate(gridX, gridY); 2641 srcRegion.width -= gridX; 2642 srcRegion.height -= gridY; 2643 destRegion.setLocation(param.getDestinationOffset()); 2644 } 2645 2646 if (destRegion.x < 0) { 2649 int delta = -destRegion.x*periodX; 2650 srcRegion.x += delta; 2651 srcRegion.width -= delta; 2652 destRegion.x = 0; 2653 } 2654 if (destRegion.y < 0) { 2655 int delta = -destRegion.y*periodY; 2656 srcRegion.y += delta; 2657 srcRegion.height -= delta; 2658 destRegion.y = 0; 2659 } 2660 2661 int subsampledWidth = (srcRegion.width + periodX - 1)/periodX; 2663 int subsampledHeight = (srcRegion.height + periodY - 1)/periodY; 2664 destRegion.width = subsampledWidth; 2665 destRegion.height = subsampledHeight; 2666 2667 if (image != null) { 2670 Rectangle destImageRect = new Rectangle (0, 0, 2671 image.getWidth(), 2672 image.getHeight()); 2673 destRegion.setBounds(destRegion.intersection(destImageRect)); 2674 if (destRegion.isEmpty()) { 2675 throw new IllegalArgumentException 2676 ("Empty destination region!"); 2677 } 2678 2679 int deltaX = destRegion.x + subsampledWidth - image.getWidth(); 2680 if (deltaX > 0) { 2681 srcRegion.width -= deltaX*periodX; 2682 } 2683 int deltaY = destRegion.y + subsampledHeight - image.getHeight(); 2684 if (deltaY > 0) { 2685 srcRegion.height -= deltaY*periodY; 2686 } 2687 } 2688 if (srcRegion.isEmpty() || destRegion.isEmpty()) { 2689 throw new IllegalArgumentException ("Empty region!"); 2690 } 2691 } 2692 2693 2729 protected static void checkReadParamBandSettings(ImageReadParam param, 2730 int numSrcBands, 2731 int numDstBands) { 2732 int[] srcBands = null; 2734 int[] dstBands = null; 2735 if (param != null) { 2736 srcBands = param.getSourceBands(); 2737 dstBands = param.getDestinationBands(); 2738 } 2739 2740 int paramSrcBandLength = 2741 (srcBands == null) ? numSrcBands : srcBands.length; 2742 int paramDstBandLength = 2743 (dstBands == null) ? numDstBands : dstBands.length; 2744 2745 if (paramSrcBandLength != paramDstBandLength) { 2746 throw new IllegalArgumentException ("ImageReadParam num source & dest bands differ!"); 2747 } 2748 2749 if (srcBands != null) { 2750 for (int i = 0; i < srcBands.length; i++) { 2751 if (srcBands[i] >= numSrcBands) { 2752 throw new IllegalArgumentException ("ImageReadParam source bands contains a value >= the number of source bands!"); 2753 } 2754 } 2755 } 2756 2757 if (dstBands != null) { 2758 for (int i = 0; i < dstBands.length; i++) { 2759 if (dstBands[i] >= numDstBands) { 2760 throw new IllegalArgumentException ("ImageReadParam dest bands contains a value >= the number of dest bands!"); 2761 } 2762 } 2763 } 2764 } 2765 2766 2814 protected static BufferedImage 2815 getDestination(ImageReadParam param, 2816 Iterator <ImageTypeSpecifier > imageTypes, 2817 int width, int height) 2818 throws IIOException { 2819 if (imageTypes == null || !imageTypes.hasNext()) { 2820 throw new IllegalArgumentException ("imageTypes null or empty!"); 2821 } 2822 if ((long)width*height > Integer.MAX_VALUE) { 2823 throw new IllegalArgumentException 2824 ("width*height > Integer.MAX_VALUE!"); 2825 } 2826 2827 BufferedImage dest = null; 2828 ImageTypeSpecifier imageType = null; 2829 2830 if (param != null) { 2832 dest = param.getDestination(); 2834 if (dest != null) { 2835 return dest; 2836 } 2837 2838 imageType = param.getDestinationType(); 2840 } 2841 2842 if (imageType == null) { 2844 Object o = imageTypes.next(); 2845 if (!(o instanceof ImageTypeSpecifier )) { 2846 throw new IllegalArgumentException 2847 ("Non-ImageTypeSpecifier retrieved from imageTypes!"); 2848 } 2849 imageType = (ImageTypeSpecifier )o; 2850 } else { 2851 boolean foundIt = false; 2852 while (imageTypes.hasNext()) { 2853 ImageTypeSpecifier type = 2854 (ImageTypeSpecifier )imageTypes.next(); 2855 if (type.equals(imageType)) { 2856 foundIt = true; 2857 break; 2858 } 2859 } 2860 2861 if (!foundIt) { 2862 throw new IIOException 2863 ("Destination type from ImageReadParam does not match!"); 2864 } 2865 } 2866 2867 Rectangle srcRegion = new Rectangle (0,0,0,0); 2868 Rectangle destRegion = new Rectangle (0,0,0,0); 2869 computeRegions(param, 2870 width, 2871 height, 2872 null, 2873 srcRegion, 2874 destRegion); 2875 2876 int destWidth = destRegion.x + destRegion.width; 2877 int destHeight = destRegion.y + destRegion.height; 2878 return imageType.createBufferedImage(destWidth, destHeight); 2880 } 2881} 2882 | Popular Tags |