iOS/buildlibs/armv7/allegro-4.4.2/addons/jpgalleg/src/encode.c
/* encode_pass:
 *  Main encoding function. Can be used one time for unoptimized encoding,
 *  or two times for optimized encoding (first pass to gather sample
 *  frequencies to generate optimized huffman tables, second pass to
 *  actually write encoded image).
 */
Main encoding function. Can be used one time for unoptimized encoding,
or two times for optimized encoding (first pass to gather sample
frequencies to generate optimized huffman tables, second pass to
actually write encoded image).
Main encoding function . Can be used one time for unoptimized encoding or two times for optimized encoding ( first pass to gather sample frequencies to generate optimized huffman tables second pass to actually write encoded image ) .
static int
encode_pass(BITMAP *bmp, int quality)
{
	short y_buf[256], cb_buf[256], cr_buf[256];
	short y4[256], cb[64], cr[64], y_blocks_per_mcu;
	short *y_ptr, *cb_ptr, *cr_ptr;
	int dc_y, dc_cb, dc_cr;
	int block_x, block_y, x, y, i;
	intptr_t addr;
	_jpeg_io.buffer = _jpeg_io.buffer_start;
	if (write_header(sampling, greyscale, quality, bmp->w, bmp->h))
		return -1;
	dc_y = dc_cb = dc_cr = 0;
	for (block_y = 0; block_y < bmp->h; block_y += mcu_h) {
		for (block_x = 0; block_x < bmp->w; block_x += mcu_w) {
			addr = (intptr_t)fixed_bmp->line[block_y] + (block_x * 4);
			y_ptr = y_buf;
			cb_ptr = cb_buf;
			cr_ptr = cr_buf;
			for (y = 0; y < mcu_h; y++) {
				for (x = 0; x < mcu_w; x += 2) {
					rgb2ycbcr(addr, y_ptr, cb_ptr, cr_ptr, y_ptr + 1, cb_ptr + 1, cr_ptr + 1);
					y_ptr += 2;
					cb_ptr += 2;
					cr_ptr += 2;
					addr += 8;
				}
				addr += pitch;
			}
			if (mcu_w > 8) {
				if (mcu_h > 8) {
					for (y = 0; y < 8; y++) for (x = 0; x < 8; x++) {
						cb[(y << 3) | x] = (cb_buf[(y << 5) | (x << 1)] + cb_buf[(y << 5) | (x << 1) | 1] +
								   cb_buf[(y << 5) | (x << 1) | 16] + cb_buf[(y << 5) | (x << 1) | 17]) / 4;
						cr[(y << 3) | x] = (cr_buf[(y << 5) | (x << 1)] + cr_buf[(y << 5) | (x << 1) | 1] +
								   cr_buf[(y << 5) | (x << 1) | 16] + cr_buf[(y << 5) | (x << 1) | 17]) / 4;
						y4[(y << 3) | x] = y_buf[(y << 4) | x];
						y4[(y << 3) | x | 64] = y_buf[(y << 4) | x | 8];
						y4[(y << 3) | x | 128] = y_buf[(y << 4) | x | 128];
						y4[(y << 3) | x | 192] = y_buf[(y << 4) | x | 136];
						y_blocks_per_mcu = 4;
					}
				}
				else {
					for (y = 0; y < 8; y++) for (x = 0; x < 8; x++) {
						cb[(y << 3) | x] = (cb_buf[(y << 4) | (x << 1)] + cb_buf[(y << 4) | (x << 1) | 1]) / 2;
						cr[(y << 3) | x] = (cr_buf[(y << 4) | (x << 1)] + cr_buf[(y << 4) | (x << 1) | 1]) / 2;
						y4[(y << 3) | x] = y_buf[(y << 4) | x];
						y4[(y << 3) | x | 64] = y_buf[(y << 4) | x | 8];
						y_blocks_per_mcu = 2;
					}
				}
				y_ptr = y4;
				cb_ptr = cb;
				cr_ptr = cr;
			}
			else {
				y_ptr = y_buf;
				cb_ptr = cb_buf;
				cr_ptr = cr_buf;
				y_blocks_per_mcu = 1;
			}
			for (i = 0; i < y_blocks_per_mcu; i++) {
				if (encode_block(y_ptr, LUMINANCE, &dc_y))
					return -1;
				y_ptr += 64;
			}
			if (!greyscale) {
				if (encode_block(cb_ptr, CHROMINANCE, &dc_cb) ||
				    encode_block(cr_ptr, CHROMINANCE, &dc_cr))
					return -1;
			}
			if (progress_cb)
				progress_cb((progress_counter * 100) / progress_total);
			progress_counter++;
		}
	}
	_jpeg_flush_bits();
	_jpeg_putw(CHUNK_EOI);
	return 0;
}
static int encode_pass ( BITMAP * bmp , int quality ) { short y_buf [ 256 ] , cb_buf [ 256 ] , cr_buf [ 256 ] ; short y4 [ 256 ] , cb [ 64 ] , cr [ 64 ] , y_blocks_per_mcu ; short * y_ptr , * cb_ptr , * cr_ptr ; int dc_y , dc_cb , dc_cr ; int block_x , block_y , x , y , i ; intptr_t addr ; _jpeg_io . buffer = _jpeg_io . buffer_start ; if ( write_header ( sampling , greyscale , quality , bmp -> w , bmp -> h ) ) return -1 ; dc_y = dc_cb = dc_cr = 0 ; for ( block_y = 0 ; block_y < bmp -> h ; block_y += mcu_h ) { for ( block_x = 0 ; block_x < bmp -> w ; block_x += mcu_w ) { addr = ( intptr_t ) fixed_bmp -> line [ block_y ] + ( block_x * 4 ) ; y_ptr = y_buf ; cb_ptr = cb_buf ; cr_ptr = cr_buf ; for ( y = 0 ; y < mcu_h ; y ++ ) { for ( x = 0 ; x < mcu_w ; x += 2 ) { rgb2ycbcr ( addr , y_ptr , cb_ptr , cr_ptr , y_ptr + 1 , cb_ptr + 1 , cr_ptr + 1 ) ; y_ptr += 2 ; cb_ptr += 2 ; cr_ptr += 2 ; addr += 8 ; } addr += pitch ; } if ( mcu_w > 8 ) { if ( mcu_h > 8 ) { for ( y = 0 ; y < 8 ; y ++ ) for ( x = 0 ; x < 8 ; x ++ ) { cb [ ( y << 3 ) | x ] = ( cb_buf [ ( y << 5 ) | ( x << 1 ) ] + cb_buf [ ( y << 5 ) | ( x << 1 ) | 1 ] + cb_buf [ ( y << 5 ) | ( x << 1 ) | 16 ] + cb_buf [ ( y << 5 ) | ( x << 1 ) | 17 ] ) / 4 ; cr [ ( y << 3 ) | x ] = ( cr_buf [ ( y << 5 ) | ( x << 1 ) ] + cr_buf [ ( y << 5 ) | ( x << 1 ) | 1 ] + cr_buf [ ( y << 5 ) | ( x << 1 ) | 16 ] + cr_buf [ ( y << 5 ) | ( x << 1 ) | 17 ] ) / 4 ; y4 [ ( y << 3 ) | x ] = y_buf [ ( y << 4 ) | x ] ; y4 [ ( y << 3 ) | x | 64 ] = y_buf [ ( y << 4 ) | x | 8 ] ; y4 [ ( y << 3 ) | x | 128 ] = y_buf [ ( y << 4 ) | x | 128 ] ; y4 [ ( y << 3 ) | x | 192 ] = y_buf [ ( y << 4 ) | x | 136 ] ; y_blocks_per_mcu = 4 ; } } else { for ( y = 0 ; y < 8 ; y ++ ) for ( x = 0 ; x < 8 ; x ++ ) { cb [ ( y << 3 ) | x ] = ( cb_buf [ ( y << 4 ) | ( x << 1 ) ] + cb_buf [ ( y << 4 ) | ( x << 1 ) | 1 ] ) / 2 ; cr [ ( y << 3 ) | x ] = ( cr_buf [ ( y << 4 ) | ( x << 1 ) ] + cr_buf [ ( y << 4 ) | ( x << 1 ) | 1 ] ) / 2 ; y4 [ ( y << 3 ) | x ] = y_buf [ ( y << 4 ) | x ] ; y4 [ ( y << 3 ) | x | 64 ] = y_buf [ ( y << 4 ) | x | 8 ] ; y_blocks_per_mcu = 2 ; } } y_ptr = y4 ; cb_ptr = cb ; cr_ptr = cr ; } else { y_ptr = y_buf ; cb_ptr = cb_buf ; cr_ptr = cr_buf ; y_blocks_per_mcu = 1 ; } for ( i = 0 ; i < y_blocks_per_mcu ; i ++ ) { if ( encode_block ( y_ptr , LUMINANCE , & dc_y ) ) return -1 ; y_ptr += 64 ; } if ( ! greyscale ) { if ( encode_block ( cb_ptr , CHROMINANCE , & dc_cb ) || encode_block ( cr_ptr , CHROMINANCE , & dc_cr ) ) return -1 ; } if ( progress_cb ) progress_cb ( ( progress_counter * 100 ) / progress_total ) ; progress_counter ++ ; } } _jpeg_flush_bits ( ) ; _jpeg_putw ( CHUNK_EOI ) ; return 0 ; }
encode_pass:
  Main encoding function.
encode_pass : Main encoding function .
/* 411 subsampling */ /* 422 subsampling */ /* 444 subsampling */