annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
1 /*******************************************************************************
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2000, 2006 IBM Corporation and others.
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
3 * All rights reserved. This program and the accompanying materials
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
4 * are made available under the terms of the Eclipse Public License v1.0
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
5 * which accompanies this distribution, and is available at
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
6 * http://www.eclipse.org/legal/epl-v10.html
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
7 *
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
8 * Contributors:
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
9 * IBM Corporation - initial API and implementation
34
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
10 * Port to the D programming language:
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
11 * Frank Benoit <benoit@tionex.de>
0
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
12 *******************************************************************************/
34
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
13 module dwt.internal.image.PngInputStream;
0
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
14
34
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
15 import dwt.dwthelper.InputStream;
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
16 import dwt.dwthelper.System;
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
17 import dwt.internal.image.PngIdatChunk;
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
18 import dwt.internal.image.PngChunkReader;
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
19 import dwt.internal.image.PngChunk;
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
20
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
21 import tango.core.Exception;
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
22 import Math = tango.math.Math;
0
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
23
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
24 public class PngInputStream : InputStream {
34
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
25
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
26 alias InputStream.read read;
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
27
0
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
28 PngChunkReader reader;
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
29 PngChunk chunk;
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
30 int offset, length;
34
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
31
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
32 final static int DATA_OFFSET = 8;
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
33
5
1a8b3cb347e0 Fix Ctors to 'this'
Frank Benoit <benoit@tionex.de>
parents: 0
diff changeset
34 public this(PngIdatChunk chunk, PngChunkReader reader) {
0
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
35 this.chunk = chunk;
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
36 this.reader = reader;
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
37 length = chunk.getLength();
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
38 offset = 0;
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
39 }
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
40
34
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
41 private bool checkChunk() {
0
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
42 while (offset is length) {
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
43 chunk = reader.readNextChunk();
34
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
44 if (chunk is null) throw new IOException("no data");
0
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
45 if (chunk.getChunkType() is PngChunk.CHUNK_IEND) return false;
34
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
46 if (chunk.getChunkType() !is PngChunk.CHUNK_IDAT) throw new IOException("");
0
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
47 length = chunk.getLength();
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
48 offset = 0;
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
49 }
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
50 return true;
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
51 }
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
52
34
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
53 public override void close() {
0
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
54 chunk = null;
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
55 }
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
56
34
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
57 public override int read() {
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
58 if (chunk is null) throw new IOException("");
0
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
59 if (offset is length && !checkChunk()) return -1;
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
60 int b = chunk.reference[DATA_OFFSET + offset] & 0xFF;
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
61 offset++;
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
62 return b;
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
63 }
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
64
34
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
65 public override int read(byte[] b, int off, int len) {
5123b17c98ef Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents: 6
diff changeset
66 if (chunk is null) throw new IOException("");
0
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
67 if (offset is length && !checkChunk()) return -1;
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
68 len = Math.min(len, length - offset);
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
69 System.arraycopy(chunk.reference, DATA_OFFSET + offset, b, off, len);
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
70 offset += len;
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
71 return len;
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
72 }
380af2bdd8e5 Upload of whole dwt tree
Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
parents:
diff changeset
73 }