Last active
October 25, 2017 07:15
-
-
Save devetude/e90b622aaff95c79a4bfb569e14bf6ef to your computer and use it in GitHub Desktop.
Print huffman code tables.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void CJpeg::FindDHT() { | |
if ((m_pBuf[m_Index] == 0xff) && (m_pBuf[m_Index + 1] == 0xc4)) { | |
WORD SegSize = m_pBuf[m_Index + 2] * 256 + m_pBuf[m_Index + 3]; | |
BYTE *p = &m_pBuf[m_Index + 4]; | |
do { | |
int i, j, k, LASTK; | |
int Num = 0; | |
BYTE BITS[17]; | |
BYTE Th = *p; // Table Number | |
/** | |
* Print huffman table number. | |
*/ | |
switch (Th) { | |
case 0: | |
printf("Luminance-DC (Table Number : %02X)\n", Th); | |
break; | |
case 1: | |
printf("Luminance-AC (Table Number : %02X)\n", Th); | |
break; | |
case 16: | |
printf("Chrominance-DC (Table Number : %02X)\n", Th); | |
break; | |
case 17: | |
printf("Chrominance-AC (Table Number : %02X)\n", Th); | |
break; | |
} | |
memcpy(BITS, p, 17); | |
p = p + 17; | |
for (i = 1; i < 17; i++) | |
Num = Num + BITS[i]; | |
TbH[Th].Flag = TRUE; | |
TbH[Th].HUFFCODE = new WORD[Num + 1]; | |
TbH[Th].HUFFSIZE = new BYTE[Num + 1]; | |
TbH[Th].HUFFVAL = new BYTE[Num + 1]; | |
memcpy(TbH[Th].HUFFVAL, p, Num); | |
p = p + Num; | |
i = 1; j = 1; k = 0; | |
int s = 0; | |
while (i <= 16) { | |
while (j <= BITS[i]) { | |
TbH[Th].HUFFSIZE[k] = (BYTE)i; | |
k++; | |
j++; | |
} | |
/** | |
* Print codes of length bits. | |
*/ | |
printf("Codes of length %02d bits (total %03d) : ", i, BITS[i]); | |
/** | |
* Print codes. | |
*/ | |
for (int z = s; z < s + BITS[i]; z++) { | |
printf("%02X ", TbH[Th].HUFFVAL[z]); | |
} | |
s += BITS[i]; | |
printf("\n"); | |
i++; | |
j = 1; | |
} | |
TbH[Th].HUFFSIZE[k] = 0; | |
LASTK = k; | |
printf("\n"); | |
k = 0; | |
WORD CODE = 0; | |
BYTE SI = TbH[Th].HUFFSIZE[0]; | |
while (TRUE) { | |
do { | |
TbH[Th].HUFFCODE[k] = CODE; | |
CODE++; | |
k++; | |
} while (TbH[Th].HUFFSIZE[k] == SI); | |
if (TbH[Th].HUFFSIZE[k] == 0) | |
break; | |
do { | |
CODE = CODE << 1; | |
SI++; | |
} while (TbH[Th].HUFFSIZE[k] != SI); | |
} | |
i = 0; j = 0; | |
while (TRUE) { | |
do { | |
i++; | |
if (i > 16) | |
break; | |
if (BITS[i] == 0) | |
TbH[Th].MAXCODE[i] = -1; | |
} while (BITS[i] == 0); | |
if (i > 16) | |
break; | |
TbH[Th].VALPTR[i] = j; | |
TbH[Th].MINCODE[i] = TbH[Th].HUFFCODE[j]; | |
j = j + BITS[i] - 1; | |
TbH[Th].MAXCODE[i] = TbH[Th].HUFFCODE[j]; | |
j++; | |
} | |
TbH[Th].Num = Num; | |
} while (*p != 0xff); | |
m_Index = m_Index + SegSize + 2; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment