comparison dwt/internal/image/TIFFRandomFileAccess.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
1 /******************************************************************************* 1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others. 2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials 3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0 4 * are made available under the terms of the Eclipse Public License v1.0
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.TIFFRandomFileAccess;
12 14
13 import java.io.IOException; 15 import dwt.internal.image.LEDataInputStream;
16 import Math = tango.math.Math;
17 import tango.core.Exception;
18 import dwt.dwthelper.System;
14 19
15 final class TIFFRandomFileAccess { 20 final class TIFFRandomFileAccess {
16 21
17 LEDataInputStream inputStream; 22 LEDataInputStream inputStream;
18 int start, current, next; 23 int start, current, next;
22 static final int LIST_SIZE = 128; 27 static final int LIST_SIZE = 128;
23 28
24 public this(LEDataInputStream stream) { 29 public this(LEDataInputStream stream) {
25 inputStream = stream; 30 inputStream = stream;
26 start = current = next = inputStream.getPosition(); 31 start = current = next = inputStream.getPosition();
27 buffers = new byte[LIST_SIZE][]; 32 buffers = new byte[][](LIST_SIZE);
28 } 33 }
29 34
30 void seek(int pos) { 35 void seek(int pos) {
31 if (pos is current) return; 36 if (pos is current) return;
32 if (pos < start) throw new IOException(); 37 if (pos < start) throw new IOException( "pos < start" );
33 current = pos; 38 current = pos;
34 if (current > next) { 39 if (current > next) {
35 int n = current - next; 40 int n = current - next;
36 /* store required bytes */ 41 /* store required bytes */
37 int index = next / CHUNK_SIZE; 42 int index = next / CHUNK_SIZE;
38 int offset = next % CHUNK_SIZE; 43 int offset = next % CHUNK_SIZE;
39 while (n > 0) { 44 while (n > 0) {
40 if (index >= buffers.length) { 45 if (index >= buffers.length) {
41 byte[][] oldBuffers = buffers; 46 byte[][] oldBuffers = buffers;
42 buffers = new byte[Math.max(index + 1, oldBuffers.length + LIST_SIZE)][]; 47 buffers = new byte[][]( Math.max(index + 1, oldBuffers.length + LIST_SIZE) );
43 System.arraycopy(oldBuffers, 0, buffers, 0, oldBuffers.length); 48 System.arraycopy(oldBuffers, 0, buffers, 0, oldBuffers.length);
44 } 49 }
45 if (buffers[index] is null) buffers[index] = new byte[CHUNK_SIZE]; 50 if (buffers[index] is null) buffers[index] = new byte[CHUNK_SIZE];
46 int cnt = inputStream.read(buffers[index], offset, Math.min(n, CHUNK_SIZE - offset)); 51 int cnt = inputStream.read(buffers[index], offset, Math.min(n, CHUNK_SIZE - offset));
47 n -= cnt; 52 n -= cnt;
58 int nMissing = size - next + current; 63 int nMissing = size - next + current;
59 int destNext = 0; 64 int destNext = 0;
60 if (nCached > 0) { 65 if (nCached > 0) {
61 /* Get cached bytes */ 66 /* Get cached bytes */
62 int index = current / CHUNK_SIZE; 67 int index = current / CHUNK_SIZE;
63 int offset = current % CHUNK_SIZE; 68 int offset = current % CHUNK_SIZE;
64 while (nCached > 0) { 69 while (nCached > 0) {
65 int cnt = Math.min(nCached, CHUNK_SIZE - offset); 70 int cnt = Math.min(nCached, CHUNK_SIZE - offset);
66 System.arraycopy(buffers[index], offset, b, destNext, cnt); 71 System.arraycopy(buffers[index], offset, b, destNext, cnt);
67 nCached -= cnt; 72 nCached -= cnt;
68 destNext += cnt; 73 destNext += cnt;
69 index++; 74 index++;
70 offset = 0; 75 offset = 0;
71 } 76 }
72 } 77 }
75 int index = next / CHUNK_SIZE; 80 int index = next / CHUNK_SIZE;
76 int offset = next % CHUNK_SIZE; 81 int offset = next % CHUNK_SIZE;
77 while (nMissing > 0) { 82 while (nMissing > 0) {
78 if (index >= buffers.length) { 83 if (index >= buffers.length) {
79 byte[][] oldBuffers = buffers; 84 byte[][] oldBuffers = buffers;
80 buffers = new byte[Math.max(index, oldBuffers.length + LIST_SIZE)][]; 85 buffers = new byte[][](Math.max(index, oldBuffers.length + LIST_SIZE));
81 System.arraycopy(oldBuffers, 0, buffers, 0, oldBuffers.length); 86 System.arraycopy(oldBuffers, 0, buffers, 0, oldBuffers.length);
82 } 87 }
83 if (buffers[index] is null) buffers[index] = new byte[CHUNK_SIZE]; 88 if (buffers[index] is null) buffers[index] = new byte[CHUNK_SIZE];
84 int cnt = inputStream.read(buffers[index], offset, Math.min(nMissing, CHUNK_SIZE - offset)); 89 int cnt = inputStream.read(buffers[index], offset, Math.min(nMissing, CHUNK_SIZE - offset));
85 System.arraycopy(buffers[index], offset, b, destNext, cnt); 90 System.arraycopy(buffers[index], offset, b, destNext, cnt);