iOS/buildlibs/armv7/allegro-4.4.2/addons/jpgalleg/src/encode.c
/* write_header:
 *  Writes the complete header of a baseline JPG image to the output stream.
 *  This is made of the following chunks (in order): APP0, COM, DQT, SOF0,
 *  DHT, SOS.
 */
Writes the complete header of a baseline JPG image to the output stream.
Writes the complete header of a baseline JPG image to the output stream .
static int
write_header(int sampling, int greyscale, int quality, int width, int height)
{
	unsigned char *comment = (unsigned char *)"Generated using " JPGALLEG_VERSION_STRING;
	unsigned char app0[] = { 'J', 'F', 'I', 'F', 0, 1, 1, 0, 0, 1, 0, 1, 0, 0 };
	unsigned char sof0[] = { 8, (height >> 8) & 0xff, height & 0xff, (width >> 8) & 0xff, width & 0xff, greyscale ? 1 : 3, 1, 0, 0, 2, 0x11, 1, 3, 0x11, 1 };
	unsigned char sos_greyscale[] = { 1, 1, 0x00, 0, 63, 0 };
	unsigned char sos_color[] = { 3, 1, 0x00, 2, 0x11, 3, 0x11, 0, 63, 0 };
	_jpeg_putw(CHUNK_SOI);
	_jpeg_new_chunk(CHUNK_APP0);
	_jpeg_chunk_puts(app0, 14);
	_jpeg_write_chunk();
	_jpeg_new_chunk(CHUNK_COM);
	_jpeg_chunk_puts(comment, (int) strlen((char*)comment));
	_jpeg_write_chunk();
	_jpeg_new_chunk(CHUNK_DQT);
	_jpeg_chunk_putc(0);
	write_quantization_table(luminance_quant_table, default_luminance_quant_table, quality);
	if (!greyscale) {
		_jpeg_chunk_putc(1);
		write_quantization_table(chrominance_quant_table, default_chrominance_quant_table, quality);
	}
	_jpeg_write_chunk();
	if (greyscale)
		sof0[7] = 0x11;
	else {
		switch (sampling) {
			case JPG_SAMPLING_411: sof0[7] = 0x22; break;
			case JPG_SAMPLING_422: sof0[7] = 0x21; break;
			case JPG_SAMPLING_444: sof0[7] = 0x11; break;
		}
	}
	_jpeg_new_chunk(CHUNK_SOF0);
	_jpeg_chunk_puts(sof0, greyscale ? 9 : 15);
	_jpeg_write_chunk();
	_jpeg_new_chunk(CHUNK_DHT);
	_jpeg_chunk_putc(0x00);		
	write_huffman_table(&_jpeg_huffman_dc_table[0], num_codes_dc_luminance, val_dc_luminance);
	_jpeg_chunk_putc(0x10);		
	write_huffman_table(&_jpeg_huffman_ac_table[0], num_codes_ac_luminance, val_ac_luminance);
	if (!greyscale) {
		_jpeg_chunk_putc(0x01);		
		write_huffman_table(&_jpeg_huffman_dc_table[1], num_codes_dc_chrominance, val_dc_chrominance);
		_jpeg_chunk_putc(0x11);		
		write_huffman_table(&_jpeg_huffman_ac_table[1], num_codes_ac_chrominance, val_ac_chrominance);
	}
	_jpeg_write_chunk();
	_jpeg_new_chunk(CHUNK_SOS);
	if (greyscale)
		_jpeg_chunk_puts(sos_greyscale, 6);
	else
		_jpeg_chunk_puts(sos_color, 10);
	_jpeg_write_chunk();
	return 0;
}
static int write_header ( int sampling , int greyscale , int quality , int width , int height ) { unsigned char * comment = ( unsigned char * ) " " JPGALLEG_VERSION_STRING ; unsigned char app0 [ ] = { ' ' , ' ' , ' ' , ' ' , 0 , 1 , 1 , 0 , 0 , 1 , 0 , 1 , 0 , 0 } ; unsigned char sof0 [ ] = { 8 , ( height >> 8 ) & 0xff , height & 0xff , ( width >> 8 ) & 0xff , width & 0xff , greyscale ? 1 : 3 , 1 , 0 , 0 , 2 , 0x11 , 1 , 3 , 0x11 , 1 } ; unsigned char sos_greyscale [ ] = { 1 , 1 , 0x00 , 0 , 63 , 0 } ; unsigned char sos_color [ ] = { 3 , 1 , 0x00 , 2 , 0x11 , 3 , 0x11 , 0 , 63 , 0 } ; _jpeg_putw ( CHUNK_SOI ) ; _jpeg_new_chunk ( CHUNK_APP0 ) ; _jpeg_chunk_puts ( app0 , 14 ) ; _jpeg_write_chunk ( ) ; _jpeg_new_chunk ( CHUNK_COM ) ; _jpeg_chunk_puts ( comment , ( int ) strlen ( ( char * ) comment ) ) ; _jpeg_write_chunk ( ) ; _jpeg_new_chunk ( CHUNK_DQT ) ; _jpeg_chunk_putc ( 0 ) ; write_quantization_table ( luminance_quant_table , default_luminance_quant_table , quality ) ; if ( ! greyscale ) { _jpeg_chunk_putc ( 1 ) ; write_quantization_table ( chrominance_quant_table , default_chrominance_quant_table , quality ) ; } _jpeg_write_chunk ( ) ; if ( greyscale ) sof0 [ 7 ] = 0x11 ; else { switch ( sampling ) { case JPG_SAMPLING_411 : sof0 [ 7 ] = 0x22 ; break ; case JPG_SAMPLING_422 : sof0 [ 7 ] = 0x21 ; break ; case JPG_SAMPLING_444 : sof0 [ 7 ] = 0x11 ; break ; } } _jpeg_new_chunk ( CHUNK_SOF0 ) ; _jpeg_chunk_puts ( sof0 , greyscale ? 9 : 15 ) ; _jpeg_write_chunk ( ) ; _jpeg_new_chunk ( CHUNK_DHT ) ; _jpeg_chunk_putc ( 0x00 ) ; write_huffman_table ( & _jpeg_huffman_dc_table [ 0 ] , num_codes_dc_luminance , val_dc_luminance ) ; _jpeg_chunk_putc ( 0x10 ) ; write_huffman_table ( & _jpeg_huffman_ac_table [ 0 ] , num_codes_ac_luminance , val_ac_luminance ) ; if ( ! greyscale ) { _jpeg_chunk_putc ( 0x01 ) ; write_huffman_table ( & _jpeg_huffman_dc_table [ 1 ] , num_codes_dc_chrominance , val_dc_chrominance ) ; _jpeg_chunk_putc ( 0x11 ) ; write_huffman_table ( & _jpeg_huffman_ac_table [ 1 ] , num_codes_ac_chrominance , val_ac_chrominance ) ; } _jpeg_write_chunk ( ) ; _jpeg_new_chunk ( CHUNK_SOS ) ; if ( greyscale ) _jpeg_chunk_puts ( sos_greyscale , 6 ) ; else _jpeg_chunk_puts ( sos_color , 10 ) ; _jpeg_write_chunk ( ) ; return 0 ; }
write_header:
  Writes the complete header of a baseline JPG image to the output stream.
write_header : Writes the complete header of a baseline JPG image to the output stream .
/* JFIF 1.1, no units, 1:1 aspect ratio, no thumbnail */ /* 8 bits data precision, Y uses quantization table 0, Cb and Cr table 1 */ /* Y uses DC table 0 and AC table 0, Cb and Cr use DC table 1 and AC table 1 */ /* APP0 chunk */ /* COM chunk ;) */ /* DQT chunk */ /* SOF0 chunk */ /* DHT chunk */ /* DC huffman table 0 (used for luminance) */ /* AC huffman table 0 (used for luminance) */ /* DC huffman table 1 (used for chrominance) */ /* AC huffman table 1 (used for chrominance) */ /* SOS chunk */