001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.swt.internal.image;
011:
012: import java.io.*;
013:
014: public class PngHuffmanTables {
015: PngHuffmanTable literalTable;
016: PngHuffmanTable distanceTable;
017:
018: static PngHuffmanTable FixedLiteralTable;
019: static PngHuffmanTable FixedDistanceTable;
020:
021: static final int LiteralTableSize = 288;
022: static final int[] FixedLiteralLengths = { 8, 8, 8, 8, 8, 8, 8, 8,
023: 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
024: 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
025: 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
026: 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
027: 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
028: 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
029: 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9,
030: 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
031: 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
032: 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
033: 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
034: 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
035: 9, 9, 9, 9, 9, 9, 9, 9, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
036: 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, };
037:
038: static final int DistanceTableSize = 32;
039: static final int[] FixedDistanceLengths = { 5, 5, 5, 5, 5, 5, 5, 5,
040: 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
041: 5, 5, 5, 5, };
042:
043: static final int LengthCodeTableSize = 19;
044: static final int[] LengthCodeOrder = { 16, 17, 18, 0, 8, 7, 9, 6,
045: 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
046:
047: static PngHuffmanTables getDynamicTables(
048: PngDecodingDataStream stream) throws IOException {
049: return new PngHuffmanTables(stream);
050: }
051:
052: static PngHuffmanTables getFixedTables() {
053: return new PngHuffmanTables();
054: }
055:
056: private PngHuffmanTable getFixedLiteralTable() {
057: if (FixedLiteralTable == null) {
058: FixedLiteralTable = new PngHuffmanTable(FixedLiteralLengths);
059: }
060: return FixedLiteralTable;
061: }
062:
063: private PngHuffmanTable getFixedDistanceTable() {
064: if (FixedDistanceTable == null) {
065: FixedDistanceTable = new PngHuffmanTable(
066: FixedDistanceLengths);
067: }
068: return FixedDistanceTable;
069: }
070:
071: private PngHuffmanTables() {
072: literalTable = getFixedLiteralTable();
073: distanceTable = getFixedDistanceTable();
074: }
075:
076: private PngHuffmanTables(PngDecodingDataStream stream)
077: throws IOException {
078: int literals = PngLzBlockReader.FIRST_LENGTH_CODE
079: + stream.getNextIdatBits(5);
080: int distances = PngLzBlockReader.FIRST_DISTANCE_CODE
081: + stream.getNextIdatBits(5);
082: int codeLengthCodes = PngLzBlockReader.FIRST_CODE_LENGTH_CODE
083: + stream.getNextIdatBits(4);
084:
085: if (codeLengthCodes > PngLzBlockReader.LAST_CODE_LENGTH_CODE) {
086: stream.error();
087: }
088:
089: /* Tricky, tricky, tricky. The length codes are stored in
090: * a very odd order. (For the order, see the definition of
091: * the static field lengthCodeOrder.) Also, the data may
092: * not contain values for all the codes. It may just contain
093: * values for the first X number of codes. The table should
094: * be of size <LengthCodeTableSize> regardless of the number
095: * of values actually given in the table.
096: */
097: int[] lengthCodes = new int[LengthCodeTableSize];
098: for (int i = 0; i < codeLengthCodes; i++) {
099: lengthCodes[LengthCodeOrder[i]] = stream.getNextIdatBits(3);
100: }
101: PngHuffmanTable codeLengthsTable = new PngHuffmanTable(
102: lengthCodes);
103:
104: int[] literalLengths = readLengths(stream, literals,
105: codeLengthsTable, LiteralTableSize);
106: int[] distanceLengths = readLengths(stream, distances,
107: codeLengthsTable, DistanceTableSize);
108:
109: literalTable = new PngHuffmanTable(literalLengths);
110: distanceTable = new PngHuffmanTable(distanceLengths);
111: }
112:
113: private int[] readLengths(PngDecodingDataStream stream,
114: int numLengths, PngHuffmanTable lengthsTable, int tableSize)
115: throws IOException {
116: int[] lengths = new int[tableSize];
117:
118: for (int index = 0; index < numLengths;) {
119: int value = lengthsTable.getNextValue(stream);
120: if (value < 16) {
121: // Literal value
122: lengths[index] = value;
123: index++;
124: } else if (value == 16) {
125: // Repeat the previous code 3-6 times.
126: int count = stream.getNextIdatBits(2) + 3;
127: for (int i = 0; i < count; i++) {
128: lengths[index] = lengths[index - 1];
129: index++;
130: }
131: } else if (value == 17) {
132: // Repeat 0 3-10 times.
133: int count = stream.getNextIdatBits(3) + 3;
134: for (int i = 0; i < count; i++) {
135: lengths[index] = 0;
136: index++;
137: }
138: } else if (value == 18) {
139: // Repeat 0 11-138 times.
140: int count = stream.getNextIdatBits(7) + 11;
141: for (int i = 0; i < count; i++) {
142: lengths[index] = 0;
143: index++;
144: }
145: } else {
146: stream.error();
147: }
148: }
149: return lengths;
150: }
151:
152: int getNextLiteralValue(PngDecodingDataStream stream)
153: throws IOException {
154: return literalTable.getNextValue(stream);
155: }
156:
157: int getNextDistanceValue(PngDecodingDataStream stream)
158: throws IOException {
159: return distanceTable.getNextValue(stream);
160: }
161:
162: }
|