Source Code Cross Referenced for ImageReader.java in  » Apache-Harmony-Java-SE » javax-package » javax » imageio » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » javax package » javax.imageio 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:        package javax.imageio;
018:
019:        import java.awt.Rectangle;
020:        import java.awt.image.BufferedImage;
021:        import java.awt.image.Raster;
022:        import java.awt.image.RenderedImage;
023:        import java.io.IOException;
024:        import java.util.ArrayList;
025:        import java.util.Iterator;
026:        import java.util.LinkedList;
027:        import java.util.List;
028:        import java.util.Locale;
029:        import java.util.ResourceBundle;
030:        import java.util.Set;
031:
032:        import javax.imageio.event.IIOReadProgressListener;
033:        import javax.imageio.event.IIOReadUpdateListener;
034:        import javax.imageio.event.IIOReadWarningListener;
035:        import javax.imageio.metadata.IIOMetadata;
036:        import javax.imageio.spi.ImageReaderSpi;
037:        import javax.imageio.stream.ImageInputStream;
038:
039:        import org.apache.harmony.x.imageio.internal.nls.Messages;
040:
041:        public abstract class ImageReader {
042:
043:            protected ImageReaderSpi originatingProvider;
044:
045:            protected Object input;
046:
047:            protected boolean seekForwardOnly;
048:
049:            protected boolean ignoreMetadata;
050:
051:            protected int minIndex;
052:
053:            protected Locale[] availableLocales;
054:
055:            protected Locale locale;
056:
057:            protected List<IIOReadWarningListener> warningListeners;
058:
059:            protected List<Locale> warningLocales;
060:
061:            protected List<IIOReadProgressListener> progressListeners;
062:
063:            protected List<IIOReadUpdateListener> updateListeners;
064:
065:            private boolean isAborted;
066:
067:            protected ImageReader(final ImageReaderSpi originatingProvider) {
068:                this .originatingProvider = originatingProvider;
069:            }
070:
071:            public String getFormatName() throws IOException {
072:                return originatingProvider.getFormatNames()[0];
073:            }
074:
075:            public ImageReaderSpi getOriginatingProvider() {
076:                return originatingProvider;
077:            }
078:
079:            public void setInput(final Object input,
080:                    final boolean seekForwardOnly, final boolean ignoreMetadata) {
081:                if (input != null) {
082:                    if (!isSupported(input)
083:                            && !(input instanceof  ImageInputStream)) {
084:                        throw new IllegalArgumentException(Messages.getString(
085:                                "imageio.2", //$NON-NLS-1$
086:                                input));
087:                    }
088:                }
089:                this .minIndex = 0;
090:                this .seekForwardOnly = seekForwardOnly;
091:                this .ignoreMetadata = ignoreMetadata;
092:                this .input = input;
093:            }
094:
095:            private boolean isSupported(final Object input) {
096:                ImageReaderSpi spi = getOriginatingProvider();
097:                if (null != spi) {
098:                    Class<?>[] outTypes = spi.getInputTypes();
099:
100:                    for (Class<?> element : outTypes) {
101:                        if (element.isInstance(input)) {
102:                            return true;
103:                        }
104:                    }
105:                }
106:                return false;
107:            }
108:
109:            public void setInput(final Object input,
110:                    final boolean seekForwardOnly) {
111:                setInput(input, seekForwardOnly, false);
112:            }
113:
114:            public void setInput(final Object input) {
115:                setInput(input, false, false);
116:            }
117:
118:            public Object getInput() {
119:                return input;
120:            }
121:
122:            public boolean isSeekForwardOnly() {
123:                return seekForwardOnly;
124:            }
125:
126:            public boolean isIgnoringMetadata() {
127:                return ignoreMetadata;
128:            }
129:
130:            public int getMinIndex() {
131:                return minIndex;
132:            }
133:
134:            public Locale[] getAvailableLocales() {
135:                return availableLocales;
136:            }
137:
138:            public void setLocale(final Locale locale) {
139:                if (locale != null) {
140:                    final Locale[] locales = getAvailableLocales();
141:
142:                    if ((locales == null) || !arrayContains(locales, locale)) {
143:                        throw new IllegalArgumentException(Messages.getString(
144:                                "imageio.3", //$NON-NLS-1$
145:                                "Locale " + locale)); //$NON-NLS-1$
146:                    }
147:                }
148:
149:                this .locale = locale;
150:            }
151:
152:            public Locale getLocale() {
153:                return locale;
154:            }
155:
156:            public abstract int getNumImages(final boolean allowSearch)
157:                    throws IOException;
158:
159:            public abstract int getWidth(final int imageIndex)
160:                    throws IOException;
161:
162:            public abstract int getHeight(final int imageIndex)
163:                    throws IOException;
164:
165:            public boolean isRandomAccessEasy(final int imageIndex)
166:                    throws IOException {
167:                return false;
168:            }
169:
170:            public float getAspectRatio(final int imageIndex)
171:                    throws IOException {
172:                return (float) getWidth(imageIndex) / getHeight(imageIndex);
173:            }
174:
175:            public ImageTypeSpecifier getRawImageType(final int imageIndex)
176:                    throws IOException {
177:                return getImageTypes(imageIndex).next();
178:            }
179:
180:            public abstract Iterator<ImageTypeSpecifier> getImageTypes(
181:                    final int imageIndex) throws IOException;
182:
183:            public ImageReadParam getDefaultReadParam() {
184:                return new ImageReadParam();
185:            }
186:
187:            public abstract IIOMetadata getStreamMetadata() throws IOException;
188:
189:            public IIOMetadata getStreamMetadata(final String formatName,
190:                    final Set<String> nodeNames) throws IOException {
191:                iaeIfNull("formatName", formatName); //$NON-NLS-1$
192:                iaeIfNull("nodeNames", nodeNames); //$NON-NLS-1$
193:
194:                final IIOMetadata data = getStreamMetadata();
195:                return isSupportedFormat(formatName, data) ? data : null;
196:            }
197:
198:            public abstract IIOMetadata getImageMetadata(final int imageIndex)
199:                    throws IOException;
200:
201:            public IIOMetadata getImageMetadata(final int imageIndex,
202:                    final String formatName, final Set<String> nodeNames)
203:                    throws IOException {
204:                iaeIfNull("formatName", formatName); //$NON-NLS-1$
205:                iaeIfNull("nodeNames", nodeNames); //$NON-NLS-1$
206:
207:                final IIOMetadata data = getImageMetadata(imageIndex);
208:                return isSupportedFormat(formatName, data) ? data : null;
209:            }
210:
211:            public BufferedImage read(final int imageIndex) throws IOException {
212:                return read(imageIndex, null);
213:            }
214:
215:            public abstract BufferedImage read(final int imageIndex,
216:                    final ImageReadParam param) throws IOException;
217:
218:            public IIOImage readAll(final int imageIndex,
219:                    final ImageReadParam param) throws IOException {
220:                List<BufferedImage> th = null;
221:                final BufferedImage img = read(imageIndex, param);
222:                final int num = getNumThumbnails(imageIndex);
223:
224:                if (num > 0) {
225:                    th = new ArrayList<BufferedImage>(num);
226:
227:                    for (int i = 0; i < num; i++) {
228:                        th.add(readThumbnail(imageIndex, i));
229:                    }
230:                }
231:
232:                return new IIOImage(img, th, getImageMetadata(imageIndex));
233:            }
234:
235:            public Iterator<IIOImage> readAll(
236:                    final Iterator<? extends ImageReadParam> params)
237:                    throws IOException {
238:                final int index = getMinIndex();
239:                final List<IIOImage> list = new LinkedList<IIOImage>();
240:
241:                processSequenceStarted(index);
242:
243:                while (params.hasNext()) {
244:                    try {
245:                        list.add(readAll(index, params.next()));
246:                    } catch (final ClassCastException ex) {
247:                        throw new IllegalArgumentException(ex);
248:                    }
249:                }
250:
251:                processSequenceComplete();
252:                return list.iterator();
253:            }
254:
255:            public boolean canReadRaster() {
256:                return false; // def
257:            }
258:
259:            public Raster readRaster(final int imageIndex,
260:                    final ImageReadParam param) throws IOException {
261:                throw new UnsupportedOperationException(Messages.getString(
262:                        "imageio.7", //$NON-NLS-1$
263:                        "readRaster()")); //$NON-NLS-1$
264:            }
265:
266:            public boolean isImageTiled(final int imageIndex)
267:                    throws IOException {
268:                return false; // def
269:            }
270:
271:            public int getTileWidth(final int imageIndex) throws IOException {
272:                return getWidth(imageIndex); // def
273:            }
274:
275:            public int getTileHeight(final int imageIndex) throws IOException {
276:                return getHeight(imageIndex); // def
277:            }
278:
279:            public int getTileGridXOffset(final int imageIndex)
280:                    throws IOException {
281:                return 0; // def
282:            }
283:
284:            public int getTileGridYOffset(final int imageIndex)
285:                    throws IOException {
286:                return 0; // def
287:            }
288:
289:            public BufferedImage readTile(final int imageIndex,
290:                    final int tileX, final int tileY) throws IOException {
291:                if ((tileX != 0) || (tileY != 0)) {
292:                    throw new IllegalArgumentException(Messages.getString(
293:                            "imageio.5", //$NON-NLS-1$
294:                            "0", "tileX & tileY")); //$NON-NLS-1$ //$NON-NLS-2$
295:                }
296:
297:                return read(imageIndex);
298:            }
299:
300:            public Raster readTileRaster(final int imageIndex, final int tileX,
301:                    final int tileY) throws IOException {
302:                if (canReadRaster()) {
303:                    if ((tileX != 0) || (tileY != 0)) {
304:                        throw new IllegalArgumentException(Messages.getString(
305:                                "imageio.5", //$NON-NLS-1$
306:                                "0", "tileX & tileY")); //$NON-NLS-1$ //$NON-NLS-2$
307:                    }
308:                    return readRaster(imageIndex, null);
309:                }
310:
311:                throw new UnsupportedOperationException(Messages.getString(
312:                        "imageio.7", //$NON-NLS-1$
313:                        "readTileRaster()")); //$NON-NLS-1$
314:            }
315:
316:            public RenderedImage readAsRenderedImage(final int imageIndex,
317:                    final ImageReadParam param) throws IOException {
318:                return read(imageIndex, param);
319:            }
320:
321:            public boolean readerSupportsThumbnails() {
322:                return false; // def
323:            }
324:
325:            public boolean hasThumbnails(final int imageIndex)
326:                    throws IOException {
327:                return getNumThumbnails(imageIndex) > 0; // def
328:            }
329:
330:            public int getNumThumbnails(final int imageIndex)
331:                    throws IOException {
332:                return 0; // def
333:            }
334:
335:            public int getThumbnailWidth(final int imageIndex,
336:                    final int thumbnailIndex) throws IOException {
337:                return readThumbnail(imageIndex, thumbnailIndex).getWidth(); // def
338:            }
339:
340:            public int getThumbnailHeight(final int imageIndex,
341:                    final int thumbnailIndex) throws IOException {
342:                return readThumbnail(imageIndex, thumbnailIndex).getHeight(); // def
343:            }
344:
345:            public BufferedImage readThumbnail(final int imageIndex,
346:                    final int thumbnailIndex) throws IOException {
347:                throw new UnsupportedOperationException(Messages.getString(
348:                        "imageio.7", //$NON-NLS-1$
349:                        "readThumbnail()")); //$NON-NLS-1$
350:            }
351:
352:            public void abort() {
353:                isAborted = true;
354:            }
355:
356:            protected boolean abortRequested() {
357:                return isAborted;
358:            }
359:
360:            protected void clearAbortRequest() {
361:                isAborted = false;
362:            }
363:
364:            public void addIIOReadWarningListener(
365:                    final IIOReadWarningListener listener) {
366:                if (listener != null) {
367:                    warningListeners = addToList(warningListeners, listener);
368:                    warningLocales = addToList(warningLocales, getLocale());
369:                }
370:            }
371:
372:            public void removeIIOReadWarningListener(
373:                    final IIOReadWarningListener listener) {
374:                final int ind;
375:
376:                if ((warningListeners != null) && (listener != null)
377:                        && ((ind = warningListeners.indexOf(listener)) != -1)) {
378:                    warningListeners.remove(ind);
379:                    warningLocales.remove(ind);
380:                }
381:            }
382:
383:            public void removeAllIIOReadWarningListeners() {
384:                warningListeners = null;
385:                warningLocales = null;
386:            }
387:
388:            public void addIIOReadProgressListener(
389:                    final IIOReadProgressListener listener) {
390:                if (listener != null) {
391:                    progressListeners = addToList(progressListeners, listener);
392:                }
393:            }
394:
395:            public void removeIIOReadProgressListener(
396:                    final IIOReadProgressListener listener) {
397:                if ((progressListeners != null) && (listener != null)) {
398:                    progressListeners.remove(listener);
399:                }
400:            }
401:
402:            public void removeAllIIOReadProgressListeners() {
403:                progressListeners = null;
404:            }
405:
406:            public void addIIOReadUpdateListener(
407:                    final IIOReadUpdateListener listener) {
408:                if (listener != null) {
409:                    updateListeners = addToList(updateListeners, listener);
410:                }
411:            }
412:
413:            public void removeIIOReadUpdateListener(
414:                    final IIOReadUpdateListener listener) {
415:                if ((updateListeners != null) && (listener != null)) {
416:                    updateListeners.remove(listener);
417:                }
418:            }
419:
420:            public void removeAllIIOReadUpdateListeners() {
421:                updateListeners = null;
422:            }
423:
424:            protected void processSequenceStarted(final int minIndex) {
425:                if (progressListeners != null) {
426:                    for (final IIOReadProgressListener listener : progressListeners) {
427:                        listener.sequenceStarted(this , minIndex);
428:                    }
429:                }
430:            }
431:
432:            protected void processSequenceComplete() {
433:                if (progressListeners != null) {
434:                    for (final IIOReadProgressListener listener : progressListeners) {
435:                        listener.sequenceComplete(this );
436:                    }
437:                }
438:            }
439:
440:            protected void processImageStarted(final int imageIndex) {
441:                if (progressListeners != null) {
442:                    for (final IIOReadProgressListener listener : progressListeners) {
443:                        listener.imageStarted(this , imageIndex);
444:                    }
445:                }
446:            }
447:
448:            protected void processImageProgress(final float percentageDone) {
449:                if (progressListeners != null) {
450:                    for (final IIOReadProgressListener listener : progressListeners) {
451:                        listener.imageProgress(this , percentageDone);
452:                    }
453:                }
454:            }
455:
456:            protected void processImageComplete() {
457:                if (progressListeners != null) {
458:                    for (final IIOReadProgressListener listener : progressListeners) {
459:                        listener.imageComplete(this );
460:                    }
461:                }
462:            }
463:
464:            protected void processThumbnailStarted(final int imageIndex,
465:                    final int thumbnailIndex) {
466:                if (progressListeners != null) {
467:                    for (final IIOReadProgressListener listener : progressListeners) {
468:                        listener.thumbnailStarted(this , imageIndex,
469:                                thumbnailIndex);
470:                    }
471:                }
472:            }
473:
474:            protected void processThumbnailProgress(final float percentageDone) {
475:                if (progressListeners != null) {
476:                    for (final IIOReadProgressListener listener : progressListeners) {
477:                        listener.thumbnailProgress(this , percentageDone);
478:                    }
479:                }
480:            }
481:
482:            protected void processThumbnailComplete() {
483:                if (progressListeners != null) {
484:                    for (final IIOReadProgressListener listener : progressListeners) {
485:                        listener.thumbnailComplete(this );
486:                    }
487:                }
488:            }
489:
490:            protected void processReadAborted() {
491:                if (progressListeners != null) {
492:                    for (final IIOReadProgressListener listener : progressListeners) {
493:                        listener.readAborted(this );
494:                    }
495:                }
496:            }
497:
498:            protected void processPassStarted(final BufferedImage theImage,
499:                    final int pass, final int minPass, final int maxPass,
500:                    final int minX, final int minY, final int periodX,
501:                    final int periodY, final int[] bands) {
502:                if (updateListeners != null) {
503:                    for (final IIOReadUpdateListener listener : updateListeners) {
504:                        listener.passStarted(this , theImage, pass, minPass,
505:                                maxPass, minX, minY, periodX, periodY, bands);
506:                    }
507:                }
508:            }
509:
510:            protected void processImageUpdate(final BufferedImage theImage,
511:                    final int minX, final int minY, final int width,
512:                    final int height, final int periodX, final int periodY,
513:                    final int[] bands) {
514:                if (updateListeners != null) {
515:                    for (final IIOReadUpdateListener listener : updateListeners) {
516:                        listener.imageUpdate(this , theImage, minX, minY, width,
517:                                height, periodX, periodY, bands);
518:                    }
519:                }
520:            }
521:
522:            protected void processPassComplete(final BufferedImage theImage) {
523:                if (updateListeners != null) {
524:                    for (IIOReadUpdateListener listener : updateListeners) {
525:                        listener.passComplete(this , theImage);
526:                    }
527:                }
528:            }
529:
530:            protected void processThumbnailPassStarted(
531:                    final BufferedImage theThumbnail, final int pass,
532:                    final int minPass, final int maxPass, final int minX,
533:                    final int minY, final int periodX, final int periodY,
534:                    final int[] bands) {
535:                if (updateListeners != null) {
536:                    for (final IIOReadUpdateListener listener : updateListeners) {
537:                        listener.thumbnailPassStarted(this , theThumbnail, pass,
538:                                minPass, maxPass, minX, minY, periodX, periodY,
539:                                bands);
540:                    }
541:                }
542:            }
543:
544:            protected void processThumbnailUpdate(
545:                    final BufferedImage theThumbnail, final int minX,
546:                    final int minY, final int width, final int height,
547:                    final int periodX, final int periodY, final int[] bands) {
548:                if (updateListeners != null) {
549:                    for (final IIOReadUpdateListener listener : updateListeners) {
550:                        listener.thumbnailUpdate(this , theThumbnail, minX,
551:                                minY, width, height, periodX, periodY, bands);
552:                    }
553:                }
554:            }
555:
556:            protected void processThumbnailPassComplete(
557:                    final BufferedImage theThumbnail) {
558:                if (updateListeners != null) {
559:                    for (final IIOReadUpdateListener listener : updateListeners) {
560:                        listener.thumbnailPassComplete(this , theThumbnail);
561:                    }
562:                }
563:            }
564:
565:            protected void processWarningOccurred(final String warning) {
566:                if (warningListeners != null) {
567:                    iaeIfNull("warning", warning); //$NON-NLS-1$
568:                    for (final IIOReadWarningListener listener : warningListeners) {
569:                        listener.warningOccurred(this , warning);
570:                    }
571:                }
572:            }
573:
574:            protected void processWarningOccurred(final String baseName,
575:                    final String keyword) {
576:                if (warningListeners != null) {
577:                    int i = 0;
578:
579:                    iaeIfNull("keyword", keyword); //$NON-NLS-1$
580:                    iaeIfNull("baseName", baseName); //$NON-NLS-1$
581:
582:                    for (final IIOReadWarningListener listener : warningListeners) {
583:                        try {
584:                            final Locale locale = warningLocales.get(i);
585:                            final ResourceBundle bundle = (locale != null) ? ResourceBundle
586:                                    .getBundle(baseName, locale)
587:                                    : ResourceBundle.getBundle(baseName);
588:                            listener.warningOccurred(this , bundle
589:                                    .getString(keyword));
590:                        } catch (final RuntimeException ex) {
591:                            throw new IllegalArgumentException(ex.getMessage());
592:                        }
593:
594:                        i++;
595:                    }
596:                }
597:            }
598:
599:            public void reset() {
600:                setInput(null, false);
601:                setLocale(null);
602:                removeAllIIOReadUpdateListeners();
603:                removeAllIIOReadWarningListeners();
604:                removeAllIIOReadProgressListeners();
605:                clearAbortRequest();
606:            }
607:
608:            public void dispose() {
609:                // do nothing by def
610:            }
611:
612:            protected static Rectangle getSourceRegion(
613:                    final ImageReadParam param, final int srcWidth,
614:                    final int srcHeight) {
615:                final Rectangle r = new Rectangle(0, 0, srcWidth, srcHeight);
616:
617:                if (param != null) {
618:                    final int x;
619:                    final int y;
620:                    final Rectangle sr = param.getSourceRegion();
621:
622:                    if (sr != null) {
623:                        r.setBounds(r.intersection(sr));
624:                    }
625:
626:                    x = param.getSubsamplingXOffset();
627:                    y = param.getSubsamplingYOffset();
628:                    r.x += x;
629:                    r.y += y;
630:                    r.width -= x;
631:                    r.height -= y;
632:                }
633:
634:                return r;
635:            }
636:
637:            protected static void computeRegions(final ImageReadParam param,
638:                    final int srcWidth, final int srcHeight,
639:                    final BufferedImage image, final Rectangle srcRegion,
640:                    final Rectangle destRegion) {
641:                int xCols = 1;
642:                int yCols = 1;
643:
644:                iaeIfNull("srcRegion", srcRegion); //$NON-NLS-1$
645:                iaeIfNull("destRegion", destRegion); //$NON-NLS-1$
646:                iaeIfEmpty("srcRegion", srcRegion.isEmpty()); //$NON-NLS-1$
647:                iaeIfEmpty("destRegion", destRegion.isEmpty()); //$NON-NLS-1$
648:
649:                srcRegion
650:                        .setBounds(getSourceRegion(param, srcWidth, srcHeight));
651:
652:                if (param != null) {
653:                    destRegion.setLocation(param.getDestinationOffset());
654:                    xCols = param.getSourceXSubsampling();
655:                    yCols = param.getSourceYSubsampling();
656:                }
657:
658:                if (destRegion.x < 0) {
659:                    final int shift = -destRegion.x * xCols;
660:                    srcRegion.x += shift;
661:                    srcRegion.width -= shift;
662:                    destRegion.x = 0;
663:                }
664:
665:                if (destRegion.y < 0) {
666:                    final int shift = -destRegion.y * yCols;
667:                    srcRegion.y += shift;
668:                    srcRegion.height -= shift;
669:                    destRegion.y = 0;
670:                }
671:
672:                destRegion.width = srcRegion.width / xCols;
673:                destRegion.height = srcRegion.height / yCols;
674:
675:                if (image != null) {
676:                    destRegion.setBounds(destRegion.intersection(new Rectangle(
677:                            0, 0, image.getWidth(), image.getHeight())));
678:                }
679:            }
680:
681:            protected static void checkReadParamBandSettings(
682:                    final ImageReadParam param, final int numSrcBands,
683:                    final int numDstBands) {
684:                final int[] src = (param != null) ? param.getSourceBands()
685:                        : null;
686:                final int[] dst = (param != null) ? param.getDestinationBands()
687:                        : null;
688:                final int srcLen = (src != null) ? src.length : numSrcBands;
689:                final int dstLen = (dst != null) ? dst.length : numDstBands;
690:
691:                if (srcLen != dstLen) {
692:                    throw new IllegalArgumentException("srcLen != dstLen"); //$NON-NLS-1$
693:                }
694:
695:                if (src != null) {
696:                    for (int i = 0; i < srcLen; i++) {
697:                        if (src[i] >= numSrcBands) {
698:                            throw new IllegalArgumentException("src[" + i //$NON-NLS-1$
699:                                    + "] >= numSrcBands"); //$NON-NLS-1$
700:                        }
701:                    }
702:                }
703:
704:                if (dst != null) {
705:                    for (int i = 0; i < dstLen; i++) {
706:                        if (dst[i] >= numDstBands) {
707:                            throw new IllegalArgumentException("dst[" + i //$NON-NLS-1$
708:                                    + "] >= numDstBands"); //$NON-NLS-1$
709:                        }
710:                    }
711:                }
712:            }
713:
714:            protected static BufferedImage getDestination(
715:                    final ImageReadParam param,
716:                    final Iterator<ImageTypeSpecifier> imageTypes,
717:                    final int width, final int height) throws IIOException {
718:                iaeIfNull("imageTypes", imageTypes); //$NON-NLS-1$
719:                iaeIfEmpty("imageTypes", !imageTypes.hasNext()); //$NON-NLS-1$
720:
721:                if ((long) (width * height) > (long) Integer.MAX_VALUE) {
722:                    throw new IllegalArgumentException(
723:                            "width * height > Integer.MAX_VALUE!"); //$NON-NLS-1$
724:                }
725:
726:                final Rectangle dst;
727:                ImageTypeSpecifier its = null;
728:
729:                if (param != null) {
730:                    final BufferedImage img = param.getDestination();
731:
732:                    if (img != null) {
733:                        return img;
734:                    }
735:
736:                    its = param.getDestinationType();
737:                }
738:
739:                try {
740:                    isValid: if (its != null) {
741:                        while (imageTypes.hasNext()) {
742:                            if (its.equals((ImageTypeSpecifier) imageTypes
743:                                    .next())) {
744:                                break isValid;
745:                            }
746:                        }
747:                        throw new IIOException(Messages.getString(
748:                                "imageio.3", its)); //$NON-NLS-1$
749:                    } else {
750:                        its = imageTypes.next();
751:                    }
752:                } catch (final ClassCastException ex) {
753:                    throw new IllegalArgumentException(ex);
754:                }
755:
756:                dst = new Rectangle(0, 0, 0, 0);
757:                computeRegions(param, width, height, null, new Rectangle(0, 0,
758:                        0, 0), dst);
759:                return its.createBufferedImage(dst.width, dst.height);
760:            }
761:
762:            private static <T> List<T> addToList(List<T> list, final T value) {
763:                if (list == null) {
764:                    list = new LinkedList<T>();
765:                }
766:
767:                list.add(value);
768:                return list;
769:            }
770:
771:            private static void iaeIfNull(final String name, final Object value) {
772:                if (value == null) {
773:                    throw new IllegalArgumentException(Messages.getString(
774:                            "imageio.2", //$NON-NLS-1$
775:                            name));
776:                }
777:            }
778:
779:            private static void iaeIfEmpty(final String name,
780:                    final boolean isEmpty) {
781:                if (isEmpty) {
782:                    throw new IllegalArgumentException(Messages.getString(
783:                            "imageio.6", //$NON-NLS-1$
784:                            name));
785:                }
786:            }
787:
788:            private static <T> boolean arrayContains(final T[] array,
789:                    final Object value) {
790:                for (T t : array) {
791:                    if ((t == value) || ((t != null) && t.equals(value))) {
792:                        return true;
793:                    }
794:                }
795:                return false;
796:            }
797:
798:            private static boolean isSupportedFormat(final String formatName,
799:                    final IIOMetadata data) {
800:                final String[] names;
801:                return ((data != null) && ((names = data
802:                        .getMetadataFormatNames()) != null)) ? arrayContains(
803:                        names, formatName) : false;
804:            }
805:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.