01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.swt.internal.image;
11:
12: import org.eclipse.swt.*;
13:
14: public class PngChunkReader {
15: LEDataInputStream inputStream;
16: PngFileReadState readState;
17: PngIhdrChunk headerChunk;
18: PngPlteChunk paletteChunk;
19:
20: PngChunkReader(LEDataInputStream inputStream) {
21: this .inputStream = inputStream;
22: readState = new PngFileReadState();
23: headerChunk = null;
24: }
25:
26: PngIhdrChunk getIhdrChunk() {
27: if (headerChunk == null) {
28: try {
29: PngChunk chunk = PngChunk
30: .readNextFromStream(inputStream);
31: if (chunk == null)
32: 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)
44: return getIhdrChunk();
45:
46: PngChunk chunk = PngChunk.readNextFromStream(inputStream);
47: if (chunk == null)
48: SWT.error(SWT.ERROR_INVALID_IMAGE);
49: switch (chunk.getChunkType()) {
50: case PngChunk.CHUNK_tRNS:
51: ((PngTrnsChunk) chunk).validate(readState, headerChunk,
52: paletteChunk);
53: break;
54: case PngChunk.CHUNK_PLTE:
55: chunk.validate(readState, headerChunk);
56: paletteChunk = (PngPlteChunk) chunk;
57: break;
58: default:
59: chunk.validate(readState, headerChunk);
60: }
61: if (readState.readIDAT
62: && !(chunk.getChunkType() == PngChunk.CHUNK_IDAT)) {
63: readState.readPixelData = true;
64: }
65: return chunk;
66: }
67:
68: boolean readPixelData() {
69: return readState.readPixelData;
70: }
71:
72: boolean hasMoreChunks() {
73: return !readState.readIEND;
74: }
75:
76: }
|