1 11 package org.eclipse.swt.internal.image; 12 13 14 import org.eclipse.swt.*; 15 16 public class PngChunkReader { 17 LEDataInputStream inputStream; 18 PngFileReadState readState; 19 PngIhdrChunk headerChunk; 20 PngPlteChunk paletteChunk; 21 22 PngChunkReader(LEDataInputStream inputStream) { 23 this.inputStream = inputStream; 24 readState = new PngFileReadState(); 25 headerChunk = null; 26 } 27 28 PngIhdrChunk getIhdrChunk() { 29 if (headerChunk == null) { 30 try { 31 PngChunk chunk = PngChunk.readNextFromStream(inputStream); 32 if (chunk == null) SWT.error(SWT.ERROR_INVALID_IMAGE); 33 headerChunk = (PngIhdrChunk) chunk; 34 headerChunk.validate(readState, null); 35 } catch (ClassCastException e) { 36 SWT.error(SWT.ERROR_INVALID_IMAGE); 37 } 38 } 39 return headerChunk; 40 } 41 42 PngChunk readNextChunk() { 43 if (headerChunk == null) return getIhdrChunk(); 44 45 PngChunk chunk = PngChunk.readNextFromStream(inputStream); 46 if (chunk == null) SWT.error(SWT.ERROR_INVALID_IMAGE); 47 switch (chunk.getChunkType()) { 48 case PngChunk.CHUNK_tRNS: 49 ((PngTrnsChunk) chunk).validate(readState, headerChunk, paletteChunk); 50 break; 51 case PngChunk.CHUNK_PLTE: 52 chunk.validate(readState, headerChunk); 53 paletteChunk = (PngPlteChunk) chunk; 54 break; 55 default: 56 chunk.validate(readState, headerChunk); 57 } 58 if (readState.readIDAT && !(chunk.getChunkType() == PngChunk.CHUNK_IDAT)) { 59 readState.readPixelData = true; 60 } 61 return chunk; 62 } 63 64 boolean readPixelData() { 65 return readState.readPixelData; 66 } 67 68 boolean hasMoreChunks() { 69 return !readState.readIEND; 70 } 71 72 } 73 | Popular Tags |