KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > internal > image > PngChunkReader


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 JavaDoc 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