int tc = (reference[ofs] & 0xFF) / 16; int tid = (reference[ofs] & 0xFF) % 16;
int tc = (reference[ofs] & 0xFF) >> 4; int tid = reference[ofs] & 0xF;
void initialize() {	int totalLength = getSegmentLength() - 2;	int ofs = 4;	int[] bits = new int[16];	JPEGHuffmanTable[] huffTables = new JPEGHuffmanTable[8]; // maximum is 4 AC + 4 DC	int huffTableCount = 0;	while (totalLength > 0) {		int tc = (reference[ofs] & 0xFF) / 16; // table class: AC (1) or DC (0)		int tid = (reference[ofs] & 0xFF) % 16; // table id: 0-1 baseline, 0-3 prog/ext		ofs++;				/* Read the 16 count bytes and add them together to get the table size. */		int count = 0;		for (int i = 0; i < bits.length; i++) {			int bCount = reference[ofs + i] & 0xFF;			bits[i] = bCount;			count += bCount;		}		ofs += 16;		totalLength -= 17;				/* Read the table. */		int[] huffVals = new int[count];		for (int i = 0; i < count; i++) {			huffVals[i] = reference[ofs + i] & 0xFF;		}		ofs += count;		totalLength -= count;				/* Calculate the lengths. */		int[] huffCodeLengths = new int[50]; // start with 50 and increment as needed		int huffCodeLengthsIndex = 0;		for (int i = 0; i < 16; i++) {			for (int j = 0; j < bits[i]; j++) {				if (huffCodeLengthsIndex >= huffCodeLengths.length) {					int[] newHuffCodeLengths = new int[huffCodeLengths.length + 50];					System.arraycopy(huffCodeLengths, 0, newHuffCodeLengths, 0, huffCodeLengths.length);					huffCodeLengths = newHuffCodeLengths;				}				huffCodeLengths[huffCodeLengthsIndex] = i + 1;				huffCodeLengthsIndex++;			}		}				/* Truncate huffCodeLengths to the correct size. */		if (huffCodeLengthsIndex < huffCodeLengths.length) {			int[] newHuffCodeLengths = new int[huffCodeLengthsIndex];			System.arraycopy(huffCodeLengths, 0, newHuffCodeLengths, 0, huffCodeLengthsIndex);			huffCodeLengths = newHuffCodeLengths;		}				/* Calculate the Huffman codes. */		int[] huffCodes = new int[50]; // start with 50 and increment as needed		int huffCodesIndex = 0;		int k = 1;		int code = 0;		int si = huffCodeLengths[0];		int p = 0;		while (p < huffCodeLengthsIndex) {			while ((p < huffCodeLengthsIndex) && (huffCodeLengths[p] == si)) {				if (huffCodesIndex >= huffCodes.length) {					int[] newHuffCodes = new int[huffCodes.length + 50];					System.arraycopy(huffCodes, 0, newHuffCodes, 0, huffCodes.length);					huffCodes = newHuffCodes;				}				huffCodes[huffCodesIndex] = code;				huffCodesIndex++;				code++;				p++;			}			code *= 2;			si++;		}				/* Truncate huffCodes to the correct size. */		if (huffCodesIndex < huffCodes.length) {			int[] newHuffCodes = new int[huffCodesIndex];			System.arraycopy(huffCodes, 0, newHuffCodes, 0, huffCodesIndex);			huffCodes = newHuffCodes;		}				/* Calculate the maximum and minimum codes */		k = 0;		int[] maxCodes = new int[16];		int[] minCodes = new int[16];		int[] valPtrs = new int[16];		for (int i = 0; i < 16; i++) {			int bSize = bits[i];			if (bSize == 0) {				maxCodes[i] = -1;			} else {				valPtrs[i] = k;				minCodes[i] = huffCodes[k];				k += bSize;				maxCodes[i] = huffCodes[k - 1];			}		}				/* Calculate the eHuffman codes and lengths. */		int[] eHuffCodes = new int[256];		byte[] eHuffSize = new byte[256];		for (int i = 0; i < huffCodesIndex; i++) {			eHuffCodes[huffVals[i]] = huffCodes[i];			eHuffSize[huffVals[i]] = (byte)huffCodeLengths[i];		}				/* Create the new JPEGHuffmanTable and add it to the allTables array. */		JPEGHuffmanTable dhtTable = new JPEGHuffmanTable(reference);		dhtTable.tableClass = tc;		dhtTable.tableIdentifier = tid;		dhtTable.dhValues = huffVals;		dhtTable.dhMinCodes = minCodes;		dhtTable.dhMaxCodes = maxCodes;		dhtTable.dhValPtrs = valPtrs;		dhtTable.ehCodes = eHuffCodes;		dhtTable.ehCodeLengths = eHuffSize;		huffTables[huffTableCount] = dhtTable;		huffTableCount++;	}	allTables = new JPEGHuffmanTable[huffTableCount];	System.arraycopy(huffTables, 0, allTables, 0, huffTableCount);}