1 7 8 package com.sun.imageio.plugins.common; 9 10 import java.io.PrintStream ; 11 12 20 public class LZWStringTable { 21 22 private final static int RES_CODES = 2; 23 24 private final static short HASH_FREE = (short)0xFFFF; 25 private final static short NEXT_FIRST = (short)0xFFFF; 26 27 private final static int MAXBITS = 12; 28 private final static int MAXSTR = (1 << MAXBITS); 29 30 private final static short HASHSIZE = 9973; 31 private final static short HASHSTEP = 2039; 32 33 byte[] strChr; short[] strNxt; short[] strHsh; short numStrings; 38 42 int[] strLen; 43 44 47 public LZWStringTable() { 48 strChr = new byte[MAXSTR]; 49 strNxt = new short[MAXSTR]; 50 strLen = new int[MAXSTR]; 51 strHsh = new short[HASHSIZE]; 52 } 53 54 61 public int addCharString(short index, byte b) { 62 int hshidx; 63 64 if (numStrings >= MAXSTR) { return 0xFFFF; 66 } 67 68 hshidx = hash(index, b); 69 while (strHsh[hshidx] != HASH_FREE) { 70 hshidx = (hshidx + HASHSTEP) % HASHSIZE; 71 } 72 73 strHsh[hshidx] = numStrings; 74 strChr[numStrings] = b; 75 if (index == HASH_FREE) { 76 strNxt[numStrings] = NEXT_FIRST; 77 strLen[numStrings] = 1; 78 } else { 79 strNxt[numStrings] = index; 80 strLen[numStrings] = strLen[index] + 1; 81 } 82 83 return numStrings++; } 85 86 92 public short findCharString(short index, byte b) { 93 int hshidx, nxtidx; 94 95 if (index == HASH_FREE) { 96 return (short)(b & 0xFF); } 98 99 hshidx = hash(index, b); 100 while ((nxtidx = strHsh[hshidx]) != HASH_FREE) { if (strNxt[nxtidx] == index && strChr[nxtidx] == b) { 102 return (short)nxtidx; 103 } 104 hshidx = (hshidx + HASHSTEP) % HASHSIZE; 105 } 106 107 return (short)0xFFFF; 108 } 109 110 114 public void clearTable(int codesize) { 115 numStrings = 0; 116 117 for (int q = 0; q < HASHSIZE; q++) { 118 strHsh[q] = HASH_FREE; 119 } 120 121 int w = (1 << codesize) + RES_CODES; 122 for (int q = 0; q < w; q++) { 123 addCharString((short)0xFFFF, (byte)q); } 125 } 126 127 static public int hash(short index, byte lastbyte) { 128 return ((int)((short)(lastbyte << 8) ^ index) & 0xFFFF) % HASHSIZE; 129 } 130 131 151 public int expandCode(byte[] buf, int offset, short code, int skipHead) { 152 if (offset == -2) { 153 if (skipHead == 1) { 154 skipHead = 0; 155 } 156 } 157 if (code == (short)0xFFFF || skipHead == strLen[code]) { 160 return 0; 161 } 162 163 int expandLen; int codeLen = strLen[code] - skipHead; int bufSpace = buf.length - offset; if (bufSpace > codeLen) { 167 expandLen = codeLen; } else { 169 expandLen = bufSpace; 170 } 171 172 int skipTail = codeLen - expandLen; 174 int idx = offset + expandLen; 176 while ((idx > offset) && (code != (short)0xFFFF)) { 179 if (--skipTail < 0) { buf[--idx] = strChr[code]; 181 } 182 code = strNxt[code]; } 184 185 if (codeLen > expandLen) { 186 return -expandLen; } else { 188 return expandLen; } 190 } 191 192 public void dump(PrintStream out) { 193 int i; 194 for (i = 258; i < numStrings; ++i) { 195 out.println(" strNxt[" + i + "] = " + strNxt[i] 196 + " strChr " + Integer.toHexString(strChr[i] & 0xFF) 197 + " strLen " + Integer.toHexString(strLen[i])); 198 } 199 } 200 } 201 | Popular Tags |