iOS/buildlibs/armv7/allegro-4.4.2/addons/jpgalleg/src/encode.c
/* encode_block:
 *  Encodes an 8x8 basic block of coefficients of given type (luminance or
 *  chrominance) and writes it to the output stream.
 */
Encodes an 8x8 basic block of coefficients of given type (luminance or
chrominance) and writes it to the output stream.
Encodes an 8x8 basic block of coefficients of given type ( luminance or chrominance ) and writes it to the output stream .
static int
encode_block(short *block, int type, int *old_dc)
{
	HUFFMAN_TABLE *dc_table, *ac_table;
	int *quant_table;
	short data[64];
	int i, index;
	int value, num_zeroes;
	int category, bits;
	if (type == LUMINANCE) {
		dc_table = &_jpeg_huffman_dc_table[0];
		ac_table = &_jpeg_huffman_ac_table[0];
		quant_table = luminance_quant_table;
	}
	else {
		dc_table = &_jpeg_huffman_dc_table[1];
		ac_table = &_jpeg_huffman_ac_table[1];
		quant_table = chrominance_quant_table;
	}
	apply_fdct(block);
	zigzag_reorder(block, data);
	for (i = 0; i < 64; i++) {
		value = data[i];
		if (value < 0) {
			value = -value;
			value = (value * quant_table[i]) + (quant_table[i] >> 1);
			data[i] = -(value >> 19);
		}
		else
			data[i] = ((value * quant_table[i]) + (quant_table[i] >> 1)) >> 19;
	}
	value = data[0] - *old_dc;
	*old_dc = data[0];
	format_number(value, &category, &bits);
	if (huffman_encode(dc_table, category))
		return -1;
	if (put_bits(bits, category))
		return -1;
	num_zeroes = 0;
	for (index = 1; index < 64; index++) {
		if ((value = data[index]) == 0)
			num_zeroes++;
		else {
			while (num_zeroes > 15) {
				if (huffman_encode(ac_table, 0xf0))
					return -1;
				num_zeroes -= 16;
			}
			format_number(value, &category, &bits);
			value = (num_zeroes << 4) | category;
			if (huffman_encode(ac_table, value))
				return -1;
			if (put_bits(bits, category))
				return -1;
			num_zeroes = 0;
		}
	}
	if (num_zeroes > 0) {
		if (huffman_encode(ac_table, 0x00))
			return -1;
	}
	return 0;
}
static int encode_block ( short * block , int type , int * old_dc ) { HUFFMAN_TABLE * dc_table , * ac_table ; int * quant_table ; short data [ 64 ] ; int i , index ; int value , num_zeroes ; int category , bits ; if ( type == LUMINANCE ) { dc_table = & _jpeg_huffman_dc_table [ 0 ] ; ac_table = & _jpeg_huffman_ac_table [ 0 ] ; quant_table = luminance_quant_table ; } else { dc_table = & _jpeg_huffman_dc_table [ 1 ] ; ac_table = & _jpeg_huffman_ac_table [ 1 ] ; quant_table = chrominance_quant_table ; } apply_fdct ( block ) ; zigzag_reorder ( block , data ) ; for ( i = 0 ; i < 64 ; i ++ ) { value = data [ i ] ; if ( value < 0 ) { value = - value ; value = ( value * quant_table [ i ] ) + ( quant_table [ i ] >> 1 ) ; data [ i ] = - ( value >> 19 ) ; } else data [ i ] = ( ( value * quant_table [ i ] ) + ( quant_table [ i ] >> 1 ) ) >> 19 ; } value = data [ 0 ] - * old_dc ; * old_dc = data [ 0 ] ; format_number ( value , & category , & bits ) ; if ( huffman_encode ( dc_table , category ) ) return -1 ; if ( put_bits ( bits , category ) ) return -1 ; num_zeroes = 0 ; for ( index = 1 ; index < 64 ; index ++ ) { if ( ( value = data [ index ] ) == 0 ) num_zeroes ++ ; else { while ( num_zeroes > 15 ) { if ( huffman_encode ( ac_table , 0xf0 ) ) return -1 ; num_zeroes -= 16 ; } format_number ( value , & category , & bits ) ; value = ( num_zeroes << 4 ) | category ; if ( huffman_encode ( ac_table , value ) ) return -1 ; if ( put_bits ( bits , category ) ) return -1 ; num_zeroes = 0 ; } } if ( num_zeroes > 0 ) { if ( huffman_encode ( ac_table , 0x00 ) ) return -1 ; } return 0 ; }
encode_block:
  Encodes an 8x8 basic block of coefficients of given type (luminance or
  chrominance) and writes it to the output stream.
encode_block : Encodes an 8x8 basic block of coefficients of given type ( luminance or chrominance ) and writes it to the output stream .