Mercurial > projects > dwt-mac
comparison dwt/internal/image/PngInputStream.d @ 34:5123b17c98ef
Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sun, 14 Sep 2008 01:45:57 +0200 |
parents | b903c16b6f48 |
children |
comparison
equal
deleted
inserted
replaced
33:965ac0a77267 | 34:5123b17c98ef |
---|---|
5 * which accompanies this distribution, and is available at | 5 * which accompanies this distribution, and is available at |
6 * http://www.eclipse.org/legal/epl-v10.html | 6 * http://www.eclipse.org/legal/epl-v10.html |
7 * | 7 * |
8 * Contributors: | 8 * Contributors: |
9 * IBM Corporation - initial API and implementation | 9 * IBM Corporation - initial API and implementation |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
10 *******************************************************************************/ | 12 *******************************************************************************/ |
11 module dwt.internal.image; | 13 module dwt.internal.image.PngInputStream; |
12 | 14 |
13 import java.io.IOException; | 15 import dwt.dwthelper.InputStream; |
14 import java.io.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; | |
15 | 23 |
16 public class PngInputStream : InputStream { | 24 public class PngInputStream : InputStream { |
25 | |
26 alias InputStream.read read; | |
27 | |
17 PngChunkReader reader; | 28 PngChunkReader reader; |
18 PngChunk chunk; | 29 PngChunk chunk; |
19 int offset, length; | 30 int offset, length; |
20 | 31 |
21 final static int DATA_OFFSET = 8; | 32 final static int DATA_OFFSET = 8; |
22 | 33 |
23 public this(PngIdatChunk chunk, PngChunkReader reader) { | 34 public this(PngIdatChunk chunk, PngChunkReader reader) { |
24 this.chunk = chunk; | 35 this.chunk = chunk; |
25 this.reader = reader; | 36 this.reader = reader; |
26 length = chunk.getLength(); | 37 length = chunk.getLength(); |
27 offset = 0; | 38 offset = 0; |
28 } | 39 } |
29 | 40 |
30 private bool checkChunk() { | 41 private bool checkChunk() { |
31 while (offset is length) { | 42 while (offset is length) { |
32 chunk = reader.readNextChunk(); | 43 chunk = reader.readNextChunk(); |
33 if (chunk is null) throw new IOException(); | 44 if (chunk is null) throw new IOException("no data"); |
34 if (chunk.getChunkType() is PngChunk.CHUNK_IEND) return false; | 45 if (chunk.getChunkType() is PngChunk.CHUNK_IEND) return false; |
35 if (chunk.getChunkType() !is PngChunk.CHUNK_IDAT) throw new IOException(); | 46 if (chunk.getChunkType() !is PngChunk.CHUNK_IDAT) throw new IOException(""); |
36 length = chunk.getLength(); | 47 length = chunk.getLength(); |
37 offset = 0; | 48 offset = 0; |
38 } | 49 } |
39 return true; | 50 return true; |
40 } | 51 } |
41 | 52 |
42 public void close() { | 53 public override void close() { |
43 chunk = null; | 54 chunk = null; |
44 } | 55 } |
45 | 56 |
46 public int read() { | 57 public override int read() { |
47 if (chunk is null) throw new IOException(); | 58 if (chunk is null) throw new IOException(""); |
48 if (offset is length && !checkChunk()) return -1; | 59 if (offset is length && !checkChunk()) return -1; |
49 int b = chunk.reference[DATA_OFFSET + offset] & 0xFF; | 60 int b = chunk.reference[DATA_OFFSET + offset] & 0xFF; |
50 offset++; | 61 offset++; |
51 return b; | 62 return b; |
52 } | 63 } |
53 | 64 |
54 public int read(byte[] b, int off, int len) { | 65 public override int read(byte[] b, int off, int len) { |
55 if (chunk is null) throw new IOException(); | 66 if (chunk is null) throw new IOException(""); |
56 if (offset is length && !checkChunk()) return -1; | 67 if (offset is length && !checkChunk()) return -1; |
57 len = Math.min(len, length - offset); | 68 len = Math.min(len, length - offset); |
58 System.arraycopy(chunk.reference, DATA_OFFSET + offset, b, off, len); | 69 System.arraycopy(chunk.reference, DATA_OFFSET + offset, b, off, len); |
59 offset += len; | 70 offset += len; |
60 return len; | 71 return len; |