iOS/buildlibs/armv7/allegro-4.4.2/addons/jpgalleg/src/decode.c
/* decode_progressive_block
 *  Decodes some coefficients for an 8x8 block from the input stream. Used in
 *  progressive mode decoding.
 */
decode_progressive_block
Decodes some coefficients for an 8x8 block from the input stream. Used in
progressive mode decoding.
decode_progressive_block Decodes some coefficients for an 8x8 block from the input stream . Used in progressive mode decoding .
static int
decode_progressive_block(intptr_t addr, int type, int *old_dc)
{
	HUFFMAN_TABLE *dc_table, *ac_table;
	short *block = (short *)addr;
	int data, index, value;
	int num_zeroes, category;
	int p_bit, n_bit;
	if (type == LUMINANCE) {
		dc_table = dc_luminance_table;
		ac_table = ac_luminance_table;
	}
	else {
		dc_table = dc_chrominance_table;
		ac_table = ac_chrominance_table;
	}
	if (spectrum_start == 0) {
		if (successive_high == 0) {
			data = huffman_decode(dc_table);
			if (data < 0) {
				TRACE("Bad dc data");
				jpgalleg_error = JPG_ERROR_BAD_IMAGE;
				return -1;
			}
			if ((data = get_value(data & 0xf)) == (int)0x80000000)
				return -1;
			*old_dc += data;
			block[0] = *old_dc << successive_low;
		}
		else {
			if ((data = _jpeg_get_bit()) < 0) {
				TRACE("Failed to get bit from input stream");
				jpgalleg_error = JPG_ERROR_BAD_IMAGE;
				return -1;
			}
			if (data)
				block[0] |= (1 << successive_low);
		}
	}
	else {
		if (successive_high == 0) {
			if (skip_count) {
				skip_count--;
				return 0;
			}
			index = spectrum_start;
			do {
				data = huffman_decode(ac_table);
				if (data < 0) {
					TRACE("Bad ac data (first scan)");
					jpgalleg_error = JPG_ERROR_BAD_IMAGE;
					return -1;
				}
				num_zeroes = data >> 4;
				category = data & 0xf;
				if (category == 0) {
					if (num_zeroes == 15)
						index += 16;
					else {
						index++;
						skip_count = 0;
						if (num_zeroes) {
							value = get_bits(num_zeroes);
							if (value < 0) {
								TRACE("Failed to get bit from input stream");
								jpgalleg_error = JPG_ERROR_BAD_IMAGE;
								return -1;
							}
							skip_count = (1 << num_zeroes) + value - 1;
						}
						break;
					}
				}
				else {
					index += num_zeroes;
					if ((data = get_value(category)) == (int)0x80000000)
						return -1;
					block[index++] = data << successive_low;
				}
			} while (index <= spectrum_end);
		}
		else {
			index = spectrum_start;
			p_bit = 1 << successive_low;
			n_bit = (-1) << successive_low;
			if (skip_count == 0) {
				do {
					data = huffman_decode(ac_table);
					if (data < 0) {
						TRACE("Bad ac data");
						jpgalleg_error = JPG_ERROR_BAD_IMAGE;
						return -1;
					}
					num_zeroes = data >> 4;
					category = data & 0xf;
					if (category == 0) {
						if (num_zeroes < 15) {
							skip_count = 1 << num_zeroes;
							if (num_zeroes) {
								value = get_bits(num_zeroes);
								if (value < 0) {
									TRACE("Failed to get bit from input stream");
									jpgalleg_error = JPG_ERROR_BAD_IMAGE;
									return -1;
								}
								skip_count += value;
							}
							break;
						}
					}
					else if (category == 1) {
						if ((data = _jpeg_get_bit()) < 0) {
							TRACE("Failed to get bit from input stream");
							jpgalleg_error = JPG_ERROR_BAD_IMAGE;
							return -1;
						}
						if (data)
							category = p_bit;
						else
							category = n_bit;
					}
					else {
						TRACE("Unexpected ac value category");
						jpgalleg_error = JPG_ERROR_BAD_IMAGE;
						return -1;
					}
					do {
						if (block[index]) {
							if ((data = _jpeg_get_bit()) < 0) {
								TRACE("Failed to get bit from input stream");
								jpgalleg_error = JPG_ERROR_BAD_IMAGE;
								return -1;
							}
							if ((data) && (!(block[index] & p_bit))) {
								if (block[index] >= 0)
									block[index] += p_bit;
								else
									block[index] += n_bit;
							}
						}
						else {
							num_zeroes--;
							if (num_zeroes < 0)
								break;
						}
						index++;
					} while (index <= spectrum_end);
					if ((category) && (index < 64))
						block[index] = category;
					index++;
				} while (index <= spectrum_end);
			}
			if (skip_count > 0) {
				while (index <= spectrum_end) {
					if (block[index]) {
						if ((data = _jpeg_get_bit()) < 0) {
							TRACE("Failed to get bit from input stream");
							jpgalleg_error = JPG_ERROR_BAD_IMAGE;
							return -1;
						}
						if ((data) && (!(block[index] & p_bit))) {
							if (block[index] >= 0)
								block[index] += p_bit;
							else
								block[index] += n_bit;
						}
					}
					index++;
				}
				skip_count--;
			}
		}
	}
	return 0;
}
static int decode_progressive_block ( intptr_t addr , int type , int * old_dc ) { HUFFMAN_TABLE * dc_table , * ac_table ; short * block = ( short * ) addr ; int data , index , value ; int num_zeroes , category ; int p_bit , n_bit ; if ( type == LUMINANCE ) { dc_table = dc_luminance_table ; ac_table = ac_luminance_table ; } else { dc_table = dc_chrominance_table ; ac_table = ac_chrominance_table ; } if ( spectrum_start == 0 ) { if ( successive_high == 0 ) { data = huffman_decode ( dc_table ) ; if ( data < 0 ) { TRACE ( " " ) ; jpgalleg_error = JPG_ERROR_BAD_IMAGE ; return -1 ; } if ( ( data = get_value ( data & 0xf ) ) == ( int ) 0x80000000 ) return -1 ; * old_dc += data ; block [ 0 ] = * old_dc << successive_low ; } else { if ( ( data = _jpeg_get_bit ( ) ) < 0 ) { TRACE ( " " ) ; jpgalleg_error = JPG_ERROR_BAD_IMAGE ; return -1 ; } if ( data ) block [ 0 ] |= ( 1 << successive_low ) ; } } else { if ( successive_high == 0 ) { if ( skip_count ) { skip_count -- ; return 0 ; } index = spectrum_start ; do { data = huffman_decode ( ac_table ) ; if ( data < 0 ) { TRACE ( " " ) ; jpgalleg_error = JPG_ERROR_BAD_IMAGE ; return -1 ; } num_zeroes = data >> 4 ; category = data & 0xf ; if ( category == 0 ) { if ( num_zeroes == 15 ) index += 16 ; else { index ++ ; skip_count = 0 ; if ( num_zeroes ) { value = get_bits ( num_zeroes ) ; if ( value < 0 ) { TRACE ( " " ) ; jpgalleg_error = JPG_ERROR_BAD_IMAGE ; return -1 ; } skip_count = ( 1 << num_zeroes ) + value - 1 ; } break ; } } else { index += num_zeroes ; if ( ( data = get_value ( category ) ) == ( int ) 0x80000000 ) return -1 ; block [ index ++ ] = data << successive_low ; } } while ( index <= spectrum_end ) ; } else { index = spectrum_start ; p_bit = 1 << successive_low ; n_bit = ( -1 ) << successive_low ; if ( skip_count == 0 ) { do { data = huffman_decode ( ac_table ) ; if ( data < 0 ) { TRACE ( " " ) ; jpgalleg_error = JPG_ERROR_BAD_IMAGE ; return -1 ; } num_zeroes = data >> 4 ; category = data & 0xf ; if ( category == 0 ) { if ( num_zeroes < 15 ) { skip_count = 1 << num_zeroes ; if ( num_zeroes ) { value = get_bits ( num_zeroes ) ; if ( value < 0 ) { TRACE ( " " ) ; jpgalleg_error = JPG_ERROR_BAD_IMAGE ; return -1 ; } skip_count += value ; } break ; } } else if ( category == 1 ) { if ( ( data = _jpeg_get_bit ( ) ) < 0 ) { TRACE ( " " ) ; jpgalleg_error = JPG_ERROR_BAD_IMAGE ; return -1 ; } if ( data ) category = p_bit ; else category = n_bit ; } else { TRACE ( " " ) ; jpgalleg_error = JPG_ERROR_BAD_IMAGE ; return -1 ; } do { if ( block [ index ] ) { if ( ( data = _jpeg_get_bit ( ) ) < 0 ) { TRACE ( " " ) ; jpgalleg_error = JPG_ERROR_BAD_IMAGE ; return -1 ; } if ( ( data ) && ( ! ( block [ index ] & p_bit ) ) ) { if ( block [ index ] >= 0 ) block [ index ] += p_bit ; else block [ index ] += n_bit ; } } else { num_zeroes -- ; if ( num_zeroes < 0 ) break ; } index ++ ; } while ( index <= spectrum_end ) ; if ( ( category ) && ( index < 64 ) ) block [ index ] = category ; index ++ ; } while ( index <= spectrum_end ) ; } if ( skip_count > 0 ) { while ( index <= spectrum_end ) { if ( block [ index ] ) { if ( ( data = _jpeg_get_bit ( ) ) < 0 ) { TRACE ( " " ) ; jpgalleg_error = JPG_ERROR_BAD_IMAGE ; return -1 ; } if ( ( data ) && ( ! ( block [ index ] & p_bit ) ) ) { if ( block [ index ] >= 0 ) block [ index ] += p_bit ; else block [ index ] += n_bit ; } } index ++ ; } skip_count -- ; } } } return 0 ; }
decode_progressive_block
  Decodes some coefficients for an 8x8 block from the input stream.
decode_progressive_block Decodes some coefficients for an 8x8 block from the input stream .
/* DC scan */ /* First DC scan */ /* DC successive approximation */ /* AC scan */ /* First AC scan */ /* AC successive approximation */