181 int[] sourceBands = null;
182 int[] destinationBands = null;
183 Point destinationOffset = new Point(0, 0);
184
185 PNGMetadata metadata = new PNGMetadata();
186
187 DataInputStream pixelStream = null;
188
189 BufferedImage theImage = null;
190
191 // The number of source pixels processed
192 int pixelsDone = 0;
193
194 // The total number of pixels in the source image
195 int totalPixels;
196
197 public PNGImageReader(ImageReaderSpi originatingProvider) {
198 super(originatingProvider);
199 }
200
201 public void setInput(Object input,
202 boolean seekForwardOnly,
203 boolean ignoreMetadata) {
204 super.setInput(input, seekForwardOnly, ignoreMetadata);
205 this.stream = (ImageInputStream)input; // Always works
206
207 // Clear all values based on the previous stream contents
208 resetStreamSettings();
209 }
210
211 private String readNullTerminatedString(String charset, int maxLen) throws IOException {
212 ByteArrayOutputStream baos = new ByteArrayOutputStream();
213 int b = 0;
214 int count = 0;
215 while ((maxLen > count++) && ((b = stream.read()) != 0)) {
216 if (b == -1) throw new EOFException();
217 baos.write(b);
218 }
219 if (b != 0) {
220 throw new IIOException("Found non null terminated string");
1503 if (abortRequested()) {
1504 processReadAborted();
1505 } else {
1506 decodeImage();
1507 if (abortRequested()) {
1508 processReadAborted();
1509 } else {
1510 processImageComplete();
1511 }
1512 }
1513
1514 } catch (IOException e) {
1515 throw new IIOException("Error reading PNG image data", e);
1516 } finally {
1517 if (inf != null) {
1518 inf.end();
1519 }
1520 }
1521 }
1522
1523 public int getNumImages(boolean allowSearch) throws IIOException {
1524 if (stream == null) {
1525 throw new IllegalStateException("No input source set!");
1526 }
1527 if (seekForwardOnly && allowSearch) {
1528 throw new IllegalStateException
1529 ("seekForwardOnly and allowSearch can't both be true!");
1530 }
1531 return 1;
1532 }
1533
1534 public int getWidth(int imageIndex) throws IIOException {
1535 if (imageIndex != 0) {
1536 throw new IndexOutOfBoundsException("imageIndex != 0!");
1537 }
1538
1539 readHeader();
1540
1541 return metadata.IHDR_width;
1542 }
1543
1544 public int getHeight(int imageIndex) throws IIOException {
1545 if (imageIndex != 0) {
1546 throw new IndexOutOfBoundsException("imageIndex != 0!");
1547 }
1548
1549 readHeader();
1550
1551 return metadata.IHDR_height;
1552 }
1553
1554 public Iterator<ImageTypeSpecifier> getImageTypes(int imageIndex)
1555 throws IIOException
1556 {
1557 if (imageIndex != 0) {
1558 throw new IndexOutOfBoundsException("imageIndex != 0!");
1559 }
1560
1561 readHeader();
1562
1563 ArrayList<ImageTypeSpecifier> l =
1564 new ArrayList<ImageTypeSpecifier>(1);
1565
1566 ColorSpace rgb;
1567 ColorSpace gray;
1568 int[] bandOffsets;
1569
1570 int bitDepth = metadata.IHDR_bitDepth;
1571 int colorType = metadata.IHDR_colorType;
1572
1573 int dataType;
1768 /*
1769 * Super class implementation uses first element
1770 * of image types list as raw image type.
1771 *
1772 * Also, super implementation uses first element of this list
1773 * as default destination type image read param does not specify
1774 * anything other.
1775 *
1776 * However, in case of RGB and RGBA color types, raw image type
1777 * produces buffered image of custom type. It causes some
1778 * performance degradation of subsequent rendering operations.
1779 *
1780 * To resolve this contradiction we put standard image types
1781 * at the first positions of image types list (to produce standard
1782 * images by default) and put raw image type (which is custom)
1783 * at the last position of this list.
1784 *
1785 * After this changes we should override getRawImageType()
1786 * to return last element of image types list.
1787 */
1788 public ImageTypeSpecifier getRawImageType(int imageIndex)
1789 throws IOException {
1790
1791 Iterator<ImageTypeSpecifier> types = getImageTypes(imageIndex);
1792 ImageTypeSpecifier raw = null;
1793 do {
1794 raw = types.next();
1795 } while (types.hasNext());
1796 return raw;
1797 }
1798
1799 public ImageReadParam getDefaultReadParam() {
1800 return new ImageReadParam();
1801 }
1802
1803 public IIOMetadata getStreamMetadata()
1804 throws IIOException {
1805 return null;
1806 }
1807
1808 public IIOMetadata getImageMetadata(int imageIndex) throws IIOException {
1809 if (imageIndex != 0) {
1810 throw new IndexOutOfBoundsException("imageIndex != 0!");
1811 }
1812 readMetadata();
1813 return metadata;
1814 }
1815
1816 public BufferedImage read(int imageIndex, ImageReadParam param)
1817 throws IIOException {
1818 if (imageIndex != 0) {
1819 throw new IndexOutOfBoundsException("imageIndex != 0!");
1820 }
1821
1822 try {
1823 readImage(param);
1824 } catch (IOException |
1825 IllegalStateException |
1826 IllegalArgumentException e)
1827 {
1828 throw e;
1829 } catch (Throwable e) {
1830 throw new IIOException("Caught exception during read: ", e);
1831 }
1832 return theImage;
1833 }
1834
1835 public void reset() {
1836 super.reset();
1837 resetStreamSettings();
1838 }
1839
1840 private void resetStreamSettings() {
1841 gotHeader = false;
1842 gotMetadata = false;
1843 metadata = null;
1844 pixelStream = null;
1845 imageStartPosition = -1L;
1846 }
1847 }
|
181 int[] sourceBands = null;
182 int[] destinationBands = null;
183 Point destinationOffset = new Point(0, 0);
184
185 PNGMetadata metadata = new PNGMetadata();
186
187 DataInputStream pixelStream = null;
188
189 BufferedImage theImage = null;
190
191 // The number of source pixels processed
192 int pixelsDone = 0;
193
194 // The total number of pixels in the source image
195 int totalPixels;
196
197 public PNGImageReader(ImageReaderSpi originatingProvider) {
198 super(originatingProvider);
199 }
200
201 @Override
202 public void setInput(Object input,
203 boolean seekForwardOnly,
204 boolean ignoreMetadata) {
205 super.setInput(input, seekForwardOnly, ignoreMetadata);
206 this.stream = (ImageInputStream)input; // Always works
207
208 // Clear all values based on the previous stream contents
209 resetStreamSettings();
210 }
211
212 private String readNullTerminatedString(String charset, int maxLen) throws IOException {
213 ByteArrayOutputStream baos = new ByteArrayOutputStream();
214 int b = 0;
215 int count = 0;
216 while ((maxLen > count++) && ((b = stream.read()) != 0)) {
217 if (b == -1) throw new EOFException();
218 baos.write(b);
219 }
220 if (b != 0) {
221 throw new IIOException("Found non null terminated string");
1504 if (abortRequested()) {
1505 processReadAborted();
1506 } else {
1507 decodeImage();
1508 if (abortRequested()) {
1509 processReadAborted();
1510 } else {
1511 processImageComplete();
1512 }
1513 }
1514
1515 } catch (IOException e) {
1516 throw new IIOException("Error reading PNG image data", e);
1517 } finally {
1518 if (inf != null) {
1519 inf.end();
1520 }
1521 }
1522 }
1523
1524 @Override
1525 public int getNumImages(boolean allowSearch) throws IIOException {
1526 if (stream == null) {
1527 throw new IllegalStateException("No input source set!");
1528 }
1529 if (seekForwardOnly && allowSearch) {
1530 throw new IllegalStateException
1531 ("seekForwardOnly and allowSearch can't both be true!");
1532 }
1533 return 1;
1534 }
1535
1536 @Override
1537 public int getWidth(int imageIndex) throws IIOException {
1538 if (imageIndex != 0) {
1539 throw new IndexOutOfBoundsException("imageIndex != 0!");
1540 }
1541
1542 readHeader();
1543
1544 return metadata.IHDR_width;
1545 }
1546
1547 @Override
1548 public int getHeight(int imageIndex) throws IIOException {
1549 if (imageIndex != 0) {
1550 throw new IndexOutOfBoundsException("imageIndex != 0!");
1551 }
1552
1553 readHeader();
1554
1555 return metadata.IHDR_height;
1556 }
1557
1558 @Override
1559 public Iterator<ImageTypeSpecifier> getImageTypes(int imageIndex)
1560 throws IIOException
1561 {
1562 if (imageIndex != 0) {
1563 throw new IndexOutOfBoundsException("imageIndex != 0!");
1564 }
1565
1566 readHeader();
1567
1568 ArrayList<ImageTypeSpecifier> l =
1569 new ArrayList<ImageTypeSpecifier>(1);
1570
1571 ColorSpace rgb;
1572 ColorSpace gray;
1573 int[] bandOffsets;
1574
1575 int bitDepth = metadata.IHDR_bitDepth;
1576 int colorType = metadata.IHDR_colorType;
1577
1578 int dataType;
1773 /*
1774 * Super class implementation uses first element
1775 * of image types list as raw image type.
1776 *
1777 * Also, super implementation uses first element of this list
1778 * as default destination type image read param does not specify
1779 * anything other.
1780 *
1781 * However, in case of RGB and RGBA color types, raw image type
1782 * produces buffered image of custom type. It causes some
1783 * performance degradation of subsequent rendering operations.
1784 *
1785 * To resolve this contradiction we put standard image types
1786 * at the first positions of image types list (to produce standard
1787 * images by default) and put raw image type (which is custom)
1788 * at the last position of this list.
1789 *
1790 * After this changes we should override getRawImageType()
1791 * to return last element of image types list.
1792 */
1793 @Override
1794 public ImageTypeSpecifier getRawImageType(int imageIndex)
1795 throws IOException {
1796
1797 Iterator<ImageTypeSpecifier> types = getImageTypes(imageIndex);
1798 ImageTypeSpecifier raw = null;
1799 do {
1800 raw = types.next();
1801 } while (types.hasNext());
1802 return raw;
1803 }
1804
1805 @Override
1806 public ImageReadParam getDefaultReadParam() {
1807 return new ImageReadParam();
1808 }
1809
1810 @Override
1811 public IIOMetadata getStreamMetadata()
1812 throws IIOException {
1813 return null;
1814 }
1815
1816 @Override
1817 public IIOMetadata getImageMetadata(int imageIndex) throws IIOException {
1818 if (imageIndex != 0) {
1819 throw new IndexOutOfBoundsException("imageIndex != 0!");
1820 }
1821 readMetadata();
1822 return metadata;
1823 }
1824
1825 @Override
1826 public BufferedImage read(int imageIndex, ImageReadParam param)
1827 throws IIOException {
1828 if (imageIndex != 0) {
1829 throw new IndexOutOfBoundsException("imageIndex != 0!");
1830 }
1831
1832 try {
1833 readImage(param);
1834 } catch (IOException |
1835 IllegalStateException |
1836 IllegalArgumentException e)
1837 {
1838 throw e;
1839 } catch (Throwable e) {
1840 throw new IIOException("Caught exception during read: ", e);
1841 }
1842 return theImage;
1843 }
1844
1845 @Override
1846 public void reset() {
1847 super.reset();
1848 resetStreamSettings();
1849 }
1850
1851 private void resetStreamSettings() {
1852 gotHeader = false;
1853 gotMetadata = false;
1854 metadata = null;
1855 pixelStream = null;
1856 imageStartPosition = -1L;
1857 }
1858 }
|