| 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.PngInputStream; |
|---|
| 14 |
|
|---|
| 15 |
import dwt.dwthelper.InputStream; |
|---|
| 16 |
import dwt.dwthelper.System; |
|---|
| 17 |
import dwt.internal.image.PngIdatChunk; |
|---|
| 18 |
import dwt.internal.image.PngChunkReader; |
|---|
| 19 |
import dwt.internal.image.PngChunk; |
|---|
| 20 |
|
|---|
| 21 |
import tango.core.Exception; |
|---|
| 22 |
import Math = tango.math.Math; |
|---|
| 23 |
|
|---|
| 24 |
public class PngInputStream : InputStream { |
|---|
| 25 |
|
|---|
| 26 |
alias InputStream.read read; |
|---|
| 27 |
|
|---|
| 28 |
PngChunkReader reader; |
|---|
| 29 |
PngChunk chunk; |
|---|
| 30 |
int offset, length; |
|---|
| 31 |
|
|---|
| 32 |
final static int DATA_OFFSET = 8; |
|---|
| 33 |
|
|---|
| 34 |
public this(PngIdatChunk chunk, PngChunkReader reader) { |
|---|
| 35 |
this.chunk = chunk; |
|---|
| 36 |
this.reader = reader; |
|---|
| 37 |
length = chunk.getLength(); |
|---|
| 38 |
offset = 0; |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
private bool checkChunk() { |
|---|
| 42 |
while (offset is length) { |
|---|
| 43 |
chunk = reader.readNextChunk(); |
|---|
| 44 |
if (chunk is null) throw new IOException("no data"); |
|---|
| 45 |
if (chunk.getChunkType() is PngChunk.CHUNK_IEND) return false; |
|---|
| 46 |
if (chunk.getChunkType() !is PngChunk.CHUNK_IDAT) throw new IOException(""); |
|---|
| 47 |
length = chunk.getLength(); |
|---|
| 48 |
offset = 0; |
|---|
| 49 |
} |
|---|
| 50 |
return true; |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
public override void close() { |
|---|
| 54 |
chunk = null; |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
public override int read() { |
|---|
| 58 |
if (chunk is null) throw new IOException(""); |
|---|
| 59 |
if (offset is length && !checkChunk()) return -1; |
|---|
| 60 |
int b = chunk.reference[DATA_OFFSET + offset] & 0xFF; |
|---|
| 61 |
offset++; |
|---|
| 62 |
return b; |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
public override int read(byte[] b, int off, int len) { |
|---|
| 66 |
if (chunk is null) throw new IOException(""); |
|---|
| 67 |
if (offset is length && !checkChunk()) return -1; |
|---|
| 68 |
len = Math.min(len, length - offset); |
|---|
| 69 |
System.arraycopy(chunk.reference, DATA_OFFSET + offset, b, off, len); |
|---|
| 70 |
offset += len; |
|---|
| 71 |
return len; |
|---|
| 72 |
} |
|---|
| 73 |
} |
|---|