| 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 |
* Port to the D programming language: |
|---|
| 11 |
* Frank Benoit <benoit@tionex.de> |
|---|
| 12 |
*******************************************************************************/ |
|---|
| 13 |
module dwt.internal.image.PngChunkReader; |
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
import dwt.DWT; |
|---|
| 17 |
import dwt.internal.image.LEDataInputStream; |
|---|
| 18 |
import dwt.internal.image.PngFileReadState; |
|---|
| 19 |
import dwt.internal.image.PngIhdrChunk; |
|---|
| 20 |
import dwt.internal.image.PngPlteChunk; |
|---|
| 21 |
import dwt.internal.image.PngTrnsChunk; |
|---|
| 22 |
import dwt.internal.image.PngChunk; |
|---|
| 23 |
|
|---|
| 24 |
public class PngChunkReader { |
|---|
| 25 |
LEDataInputStream inputStream; |
|---|
| 26 |
PngFileReadState readState; |
|---|
| 27 |
PngIhdrChunk headerChunk; |
|---|
| 28 |
PngPlteChunk paletteChunk; |
|---|
| 29 |
|
|---|
| 30 |
this(LEDataInputStream inputStream) { |
|---|
| 31 |
this.inputStream = inputStream; |
|---|
| 32 |
readState = new PngFileReadState(); |
|---|
| 33 |
headerChunk = null; |
|---|
| 34 |
} |
|---|
| 35 |
|
|---|
| 36 |
PngIhdrChunk getIhdrChunk() { |
|---|
| 37 |
if (headerChunk is null) { |
|---|
| 38 |
PngChunk chunk = PngChunk.readNextFromStream(inputStream); |
|---|
| 39 |
if (chunk is null) DWT.error(DWT.ERROR_INVALID_IMAGE); |
|---|
| 40 |
if(( headerChunk = cast(PngIhdrChunk) chunk ) !is null ){ |
|---|
| 41 |
headerChunk.validate(readState, null); |
|---|
| 42 |
} |
|---|
| 43 |
else{ |
|---|
| 44 |
DWT.error(DWT.ERROR_INVALID_IMAGE); |
|---|
| 45 |
} |
|---|
| 46 |
} |
|---|
| 47 |
return headerChunk; |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
PngChunk readNextChunk() { |
|---|
| 51 |
if (headerChunk is null) return getIhdrChunk(); |
|---|
| 52 |
|
|---|
| 53 |
PngChunk chunk = PngChunk.readNextFromStream(inputStream); |
|---|
| 54 |
if (chunk is null) DWT.error(DWT.ERROR_INVALID_IMAGE); |
|---|
| 55 |
switch (chunk.getChunkType()) { |
|---|
| 56 |
case PngChunk.CHUNK_tRNS: |
|---|
| 57 |
(cast(PngTrnsChunk) chunk).validate(readState, headerChunk, paletteChunk); |
|---|
| 58 |
break; |
|---|
| 59 |
case PngChunk.CHUNK_PLTE: |
|---|
| 60 |
chunk.validate(readState, headerChunk); |
|---|
| 61 |
paletteChunk = cast(PngPlteChunk) chunk; |
|---|
| 62 |
break; |
|---|
| 63 |
default: |
|---|
| 64 |
chunk.validate(readState, headerChunk); |
|---|
| 65 |
} |
|---|
| 66 |
if (readState.readIDAT && !(chunk.getChunkType() is PngChunk.CHUNK_IDAT)) { |
|---|
| 67 |
readState.readPixelData = true; |
|---|
| 68 |
} |
|---|
| 69 |
return chunk; |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
bool readPixelData() { |
|---|
| 73 |
return readState.readPixelData; |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
bool hasMoreChunks() { |
|---|
| 77 |
return !readState.readIEND; |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
} |
|---|