comparison dwt/internal/image/PngChunkReader.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 1a8b3cb347e0
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
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 module dwt.internal.image;
12
13
14 import dwt.DWT;
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 is null) {
30 try {
31 PngChunk chunk = PngChunk.readNextFromStream(inputStream);
32 if (chunk is null) DWT.error(DWT.ERROR_INVALID_IMAGE);
33 headerChunk = (PngIhdrChunk) chunk;
34 headerChunk.validate(readState, null);
35 } catch (ClassCastException e) {
36 DWT.error(DWT.ERROR_INVALID_IMAGE);
37 }
38 }
39 return headerChunk;
40 }
41
42 PngChunk readNextChunk() {
43 if (headerChunk is null) return getIhdrChunk();
44
45 PngChunk chunk = PngChunk.readNextFromStream(inputStream);
46 if (chunk is null) DWT.error(DWT.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() is PngChunk.CHUNK_IDAT)) {
59 readState.readPixelData = true;
60 }
61 return chunk;
62 }
63
64 bool readPixelData() {
65 return readState.readPixelData;
66 }
67
68 bool hasMoreChunks() {
69 return !readState.readIEND;
70 }
71
72 }