private void add_huff_table(ref JHUFF_TBL htblptr, byte[] bits, byte[] val)
{
if (htblptr == null)
htblptr = new JHUFF_TBL();
/* Copy the number-of-symbols-of-each-code-length counts */
Buffer.BlockCopy(bits, 0, htblptr.Bits, 0, htblptr.Bits.Length);
/* Validate the counts. We do this here mainly so we can copy the right
* number of symbols from the val[] array, without risking marching off
* the end of memory. huff_entropy_encoder will do a more thorough test later.
*/
int nsymbols = 0;
for (int len = 1; len <= 16; len++)
nsymbols += bits[len];
if (nsymbols < 1 || nsymbols > 256)
ERREXIT(J_MESSAGE_CODE.JERR_BAD_HUFF_TABLE);
Buffer.BlockCopy(val, 0, htblptr.Huffval, 0, nsymbols);
/* Initialize sent_table false so table will be written to JPEG file. */
htblptr.Sent_table = false;
}