view dwt/internal/image/PngChunkReader.d @ 14:7b1ca4eb5763

file format png
author Frank Benoit <benoit@tionex.de>
date Mon, 07 Jan 2008 00:11:14 +0100
parents
children 8cec8f536af3
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
module dwt.internal.image.PngChunkReader;


import dwt.SWT;
import dwt.internal.image.LEDataInputStream;
import dwt.internal.image.PngFileReadState;
import dwt.internal.image.PngIhdrChunk;
import dwt.internal.image.PngPlteChunk;
import dwt.internal.image.PngTrnsChunk;
import dwt.internal.image.PngChunk;

public class PngChunkReader {
	LEDataInputStream inputStream;
	PngFileReadState readState;
	PngIhdrChunk headerChunk;
	PngPlteChunk paletteChunk;

this(LEDataInputStream inputStream) {
	this.inputStream = inputStream;
	readState = new PngFileReadState();
	headerChunk = null;
}

PngIhdrChunk getIhdrChunk() {
	if (headerChunk is null) {
        PngChunk chunk = PngChunk.readNextFromStream(inputStream);
        if (chunk is null) SWT.error(SWT.ERROR_INVALID_IMAGE);
        if(( headerChunk = cast(PngIhdrChunk) chunk ) !is null ){
            headerChunk.validate(readState, null);
        }
        else{
            SWT.error(SWT.ERROR_INVALID_IMAGE);
        }
	}
	return headerChunk;
}

PngChunk readNextChunk() {
	if (headerChunk is null) return getIhdrChunk();

	PngChunk chunk = PngChunk.readNextFromStream(inputStream);
	if (chunk is null) SWT.error(SWT.ERROR_INVALID_IMAGE);
	switch (chunk.getChunkType()) {
		case PngChunk.CHUNK_tRNS:
			(cast(PngTrnsChunk) chunk).validate(readState, headerChunk, paletteChunk);
			break;
		case PngChunk.CHUNK_PLTE:
			chunk.validate(readState, headerChunk);
			paletteChunk = cast(PngPlteChunk) chunk;
			break;
		default:
			chunk.validate(readState, headerChunk);
	}
	if (readState.readIDAT && !(chunk.getChunkType() == PngChunk.CHUNK_IDAT)) {
		readState.readPixelData = true;
	}
	return chunk;
}

bool readPixelData() {
	return readState.readPixelData;
}

bool hasMoreChunks() {
	return !readState.readIEND;
}

}