iOS/buildlibs/armv7/allegro-4.4.2/addons/jpgalleg/src/decode.c
/* read_dht_chunk:
 *  Reads a DHT (Define Huffman Table) chunk from the input stream.
 */
Reads a DHT (Define Huffman Table) chunk from the input stream.
Reads a DHT ( Define Huffman Table ) chunk from the input stream .
static int
read_dht_chunk(void)
{
	int i, j, table_id, num_codes[16];
	int code, value;
	unsigned char data;
	HUFFMAN_TABLE *table;
	HUFFMAN_ENTRY *entry;
	_jpeg_open_chunk();
	do {
		data = _jpeg_getc();
		if (data & 0xe0) {
			TRACE("Invalid DHT information byte");
			jpgalleg_error = JPG_ERROR_BAD_IMAGE;
			return -1;
		}
		table_id = data & 0xf;
		if (table_id > 3) {
			TRACE("Invalid huffman table number");
			jpgalleg_error = JPG_ERROR_BAD_IMAGE;
			return -1;
		}
		if (data & 0x10)
			table = &_jpeg_huffman_ac_table[table_id];
		else
			table = &_jpeg_huffman_dc_table[table_id];
		for (i = 0; i < 16; i++)
			num_codes[i] = _jpeg_getc();
		code = 0;
		for (i = 0; i < 16; i++) {
			if (table->entry_of_length[i])
				free(table->entry_of_length[i]);
			table->entry_of_length[i] = (HUFFMAN_ENTRY *)calloc(1 << (i + 1), sizeof(HUFFMAN_ENTRY));
			if (!table->entry_of_length[i]) {
				TRACE("Out of memory");
				jpgalleg_error = JPG_ERROR_OUT_OF_MEMORY;
				return -1;
			}
			for (j = 0; j < num_codes[i]; j++) {
				value = _jpeg_getc();
				entry = &table->entry_of_length[i][code];
				entry->value = value;
				entry->encoded_value = code;
				entry->bits_length = i + 1;
				code++;
			}
			code <<= 1;
		}
	} while (!_jpeg_eoc());
	_jpeg_close_chunk();
	return 0;
}
static int read_dht_chunk ( void ) { int i , j , table_id , num_codes [ 16 ] ; int code , value ; unsigned char data ; HUFFMAN_TABLE * table ; HUFFMAN_ENTRY * entry ; _jpeg_open_chunk ( ) ; do { data = _jpeg_getc ( ) ; if ( data & 0xe0 ) { TRACE ( " " ) ; jpgalleg_error = JPG_ERROR_BAD_IMAGE ; return -1 ; } table_id = data & 0xf ; if ( table_id > 3 ) { TRACE ( " " ) ; jpgalleg_error = JPG_ERROR_BAD_IMAGE ; return -1 ; } if ( data & 0x10 ) table = & _jpeg_huffman_ac_table [ table_id ] ; else table = & _jpeg_huffman_dc_table [ table_id ] ; for ( i = 0 ; i < 16 ; i ++ ) num_codes [ i ] = _jpeg_getc ( ) ; code = 0 ; for ( i = 0 ; i < 16 ; i ++ ) { if ( table -> entry_of_length [ i ] ) free ( table -> entry_of_length [ i ] ) ; table -> entry_of_length [ i ] = ( HUFFMAN_ENTRY * ) calloc ( 1 << ( i + 1 ) , sizeof ( HUFFMAN_ENTRY ) ) ; if ( ! table -> entry_of_length [ i ] ) { TRACE ( " " ) ; jpgalleg_error = JPG_ERROR_OUT_OF_MEMORY ; return -1 ; } for ( j = 0 ; j < num_codes [ i ] ; j ++ ) { value = _jpeg_getc ( ) ; entry = & table -> entry_of_length [ i ] [ code ] ; entry -> value = value ; entry -> encoded_value = code ; entry -> bits_length = i + 1 ; code ++ ; } code <<= 1 ; } } while ( ! _jpeg_eoc ( ) ) ; _jpeg_close_chunk ( ) ; return 0 ; }
read_dht_chunk:
  Reads a DHT (Define Huffman Table) chunk from the input stream.
read_dht_chunk : Reads a DHT ( Define Huffman Table ) chunk from the input stream .