private int HuffmanCoderCount1(int ch, int gr, HuffCodeTab ht, int v, int w, int x, int y) { int huffbits; int p; int signv, signw, signx, signy; int len; int totalBits = 0; signv = GetSign(ref v); signw = GetSign(ref w); signx = GetSign(ref x); signy = GetSign(ref y); p = v + (w << 1) + (x << 2) + (y << 3); huffbits = ht.Table[p]; len = ht.Hlen[p]; codedDataPH[ch, gr].Add(huffbits, len); totalBits += len; if (v != 0) { codedDataPH[ch, gr].Add(signv, 1); totalBits += 1; } if (w != 0) { codedDataPH[ch, gr].Add(signw, 1); totalBits += 1; } if (x != 0) { codedDataPH[ch, gr].Add(signx, 1); totalBits += 1; } if (y != 0) { codedDataPH[ch, gr].Add(signy, 1); totalBits += 1; } return(totalBits); }
public int ReadHuffCodeTab() { HT = new HuffCodeTab[34]; int index = 0; String[] strbuf; string line, command = " ", huffdata; int t = -1, i, j, k, nn, x, y, a = 0, n = 0; int xl = -1, yl = -1, len; int h; int hsize; hsize = 4 * 8; do { line = Tables.AHuffCode[index++]; }while ((line[0] == '#') || (line[0] < ' ')); do { while ((line[0] == '#') || (line[0] < ' ')) { line = Tables.AHuffCode[index++]; } strbuf = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); command = strbuf[0]; if (command == ".end") { return(n); } HT[n] = new HuffCodeTab(); HT[n].TableName = strbuf[1]; xl = Int32.Parse(strbuf[2]); yl = Int32.Parse(strbuf[3]); HT[n].LinBits = Int32.Parse(strbuf[4]); if (command != ".table") { throw new Exception("Неправильный номер таблицы"); } HT[n].LinMax = (1 << HT[n].LinBits) - 1; nn = Int32.Parse(HT[n].TableName.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[0]); if (nn != n) { throw new Exception("Неправильный номер таблицы"); } HT[n].Xlen = xl; HT[n].Ylen = yl; do { line = Tables.AHuffCode[index++]; }while ((line[0] == '#') || (line[0] < ' ')); if (line != " ") { strbuf = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); command = strbuf[0]; t = Int32.Parse(strbuf[1]); } if (command == ".reference") { HT[n].Ref = t; HT[n].Table = HT[t].Table; HT[n].Hlen = HT[t].Hlen; if ((xl != HT[t].Xlen) || (yl != HT[t].Ylen)) { throw new Exception("Неправильная ссылка на таблицу"); } do { line = Tables.AHuffCode[index++]; }while ((line[0] == '#') || (line[0] < ' ')); } else { HT[n].Ref = -1; HT[n].Table = new int[xl * yl]; if (HT[n].Table == null) { throw new Exception("Ошибка"); } HT[n].Hlen = new char[xl * yl];; if (HT[n].Hlen == null) { throw new Exception("Ошибка"); } for (i = 0; i < xl; i++) { for (j = 0; j < yl; j++) { if (xl > 1) { strbuf = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); x = int.Parse(strbuf[0]); y = int.Parse(strbuf[1]); len = int.Parse(strbuf[2]); huffdata = strbuf[3]; } else { strbuf = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); x = int.Parse(strbuf[0]); len = int.Parse(strbuf[1]); huffdata = strbuf[2]; } h = 0; k = 0; while (k < huffdata.Length) { h <<= 1; if (huffdata[k] == '1') { h++; } else if (huffdata[k] != '0') { throw new Exception("Ошибка битов таблицы Хаффмана"); } k++; } if (k != len) { Console.WriteLine("Предупреждение: неправильная длина {0}, позиция: [{1}][{2}]\n", n, i, j); } HT[n].Table[i * xl + j] = h; HT[n].Hlen[i * xl + j] = (char)len; do { line = Tables.AHuffCode[index++]; }while ((line[0] == '#') || (line[0] < ' ')); } } } n++; a++; } while (true); }