123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- #define JPEG_INTERNALS
- #include "jinclude.h"
- #include "jpeglib.h"
- typedef struct {
- struct jpeg_d_main_controller pub;
-
- JSAMPARRAY buffer[MAX_COMPONENTS];
- boolean buffer_full;
- JDIMENSION rowgroup_ctr;
-
-
- JSAMPIMAGE xbuffer[2];
- int whichptr;
- int context_state;
- JDIMENSION rowgroups_avail;
- JDIMENSION iMCU_row_ctr;
- } my_main_controller;
- typedef my_main_controller * my_main_ptr;
- #define CTX_PREPARE_FOR_IMCU 0
- #define CTX_PROCESS_IMCU 1
- #define CTX_POSTPONED_ROW 2
- METHODDEF void process_data_simple_main
- JPP( ( j_decompress_ptr cinfo, JSAMPARRAY output_buf,
- JDIMENSION * out_row_ctr, JDIMENSION out_rows_avail ) );
- METHODDEF void process_data_context_main
- JPP( ( j_decompress_ptr cinfo, JSAMPARRAY output_buf,
- JDIMENSION * out_row_ctr, JDIMENSION out_rows_avail ) );
- #ifdef QUANT_2PASS_SUPPORTED
- METHODDEF void process_data_crank_post
- JPP( ( j_decompress_ptr cinfo, JSAMPARRAY output_buf,
- JDIMENSION * out_row_ctr, JDIMENSION out_rows_avail ) );
- #endif
- LOCAL void
- alloc_funny_pointers( j_decompress_ptr cinfo ) {
- my_main_ptr main = (my_main_ptr) cinfo->main;
- int ci, rgroup;
- int M = cinfo->min_DCT_scaled_size;
- jpeg_component_info * compptr;
- JSAMPARRAY xbuf;
-
- main->xbuffer[0] = (JSAMPIMAGE)
- ( *cinfo->mem->alloc_small )( (j_common_ptr) cinfo, JPOOL_IMAGE,
- cinfo->num_components * 2 * SIZEOF( JSAMPARRAY ) );
- main->xbuffer[1] = main->xbuffer[0] + cinfo->num_components;
- for ( ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++ ) {
- rgroup = ( compptr->v_samp_factor * compptr->DCT_scaled_size ) /
- cinfo->min_DCT_scaled_size;
-
- xbuf = (JSAMPARRAY)
- ( *cinfo->mem->alloc_small )( (j_common_ptr) cinfo, JPOOL_IMAGE,
- 2 * ( rgroup * ( M + 4 ) ) * SIZEOF( JSAMPROW ) );
- xbuf += rgroup;
- main->xbuffer[0][ci] = xbuf;
- xbuf += rgroup * ( M + 4 );
- main->xbuffer[1][ci] = xbuf;
- }
- }
- LOCAL void
- make_funny_pointers( j_decompress_ptr cinfo ) {
- my_main_ptr main = (my_main_ptr) cinfo->main;
- int ci, i, rgroup;
- int M = cinfo->min_DCT_scaled_size;
- jpeg_component_info * compptr;
- JSAMPARRAY buf, xbuf0, xbuf1;
- for ( ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++ ) {
- rgroup = ( compptr->v_samp_factor * compptr->DCT_scaled_size ) /
- cinfo->min_DCT_scaled_size;
- xbuf0 = main->xbuffer[0][ci];
- xbuf1 = main->xbuffer[1][ci];
-
- buf = main->buffer[ci];
- for ( i = 0; i < rgroup * ( M + 2 ); i++ ) {
- xbuf0[i] = xbuf1[i] = buf[i];
- }
-
- for ( i = 0; i < rgroup * 2; i++ ) {
- xbuf1[rgroup * ( M - 2 ) + i] = buf[rgroup * M + i];
- xbuf1[rgroup * M + i] = buf[rgroup * ( M - 2 ) + i];
- }
-
- for ( i = 0; i < rgroup; i++ ) {
- xbuf0[i - rgroup] = xbuf0[0];
- }
- }
- }
- LOCAL void
- set_wraparound_pointers( j_decompress_ptr cinfo ) {
- my_main_ptr main = (my_main_ptr) cinfo->main;
- int ci, i, rgroup;
- int M = cinfo->min_DCT_scaled_size;
- jpeg_component_info * compptr;
- JSAMPARRAY xbuf0, xbuf1;
- for ( ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++ ) {
- rgroup = ( compptr->v_samp_factor * compptr->DCT_scaled_size ) /
- cinfo->min_DCT_scaled_size;
- xbuf0 = main->xbuffer[0][ci];
- xbuf1 = main->xbuffer[1][ci];
- for ( i = 0; i < rgroup; i++ ) {
- xbuf0[i - rgroup] = xbuf0[rgroup * ( M + 1 ) + i];
- xbuf1[i - rgroup] = xbuf1[rgroup * ( M + 1 ) + i];
- xbuf0[rgroup * ( M + 2 ) + i] = xbuf0[i];
- xbuf1[rgroup * ( M + 2 ) + i] = xbuf1[i];
- }
- }
- }
- LOCAL void
- set_bottom_pointers( j_decompress_ptr cinfo ) {
- my_main_ptr main = (my_main_ptr) cinfo->main;
- int ci, i, rgroup, iMCUheight, rows_left;
- jpeg_component_info * compptr;
- JSAMPARRAY xbuf;
- for ( ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++ ) {
-
- iMCUheight = compptr->v_samp_factor * compptr->DCT_scaled_size;
- rgroup = iMCUheight / cinfo->min_DCT_scaled_size;
-
- rows_left = (int) ( compptr->downsampled_height % (JDIMENSION) iMCUheight );
- if ( rows_left == 0 ) {
- rows_left = iMCUheight;
- }
-
- if ( ci == 0 ) {
- main->rowgroups_avail = (JDIMENSION) ( ( rows_left - 1 ) / rgroup + 1 );
- }
-
- xbuf = main->xbuffer[main->whichptr][ci];
- for ( i = 0; i < rgroup * 2; i++ ) {
- xbuf[rows_left + i] = xbuf[rows_left - 1];
- }
- }
- }
- METHODDEF void
- start_pass_main( j_decompress_ptr cinfo, J_BUF_MODE pass_mode ) {
- my_main_ptr main = (my_main_ptr) cinfo->main;
- switch ( pass_mode ) {
- case JBUF_PASS_THRU:
- if ( cinfo->upsample->need_context_rows ) {
- main->pub.process_data = process_data_context_main;
- make_funny_pointers( cinfo );
- main->whichptr = 0;
- main->context_state = CTX_PREPARE_FOR_IMCU;
- main->iMCU_row_ctr = 0;
- } else {
-
- main->pub.process_data = process_data_simple_main;
- }
- main->buffer_full = FALSE;
- main->rowgroup_ctr = 0;
- break;
- #ifdef QUANT_2PASS_SUPPORTED
- case JBUF_CRANK_DEST:
-
- main->pub.process_data = process_data_crank_post;
- break;
- #endif
- default:
- ERREXIT( cinfo, JERR_BAD_BUFFER_MODE );
- break;
- }
- }
- METHODDEF void
- process_data_simple_main( j_decompress_ptr cinfo,
- JSAMPARRAY output_buf, JDIMENSION * out_row_ctr,
- JDIMENSION out_rows_avail ) {
- my_main_ptr main = (my_main_ptr) cinfo->main;
- JDIMENSION rowgroups_avail;
-
- if ( !main->buffer_full ) {
- if ( !( *cinfo->coef->decompress_data )( cinfo, main->buffer ) ) {
- return;
- }
- main->buffer_full = TRUE;
- }
-
- rowgroups_avail = (JDIMENSION) cinfo->min_DCT_scaled_size;
-
-
- ( *cinfo->post->post_process_data )( cinfo, main->buffer,
- &main->rowgroup_ctr, rowgroups_avail,
- output_buf, out_row_ctr, out_rows_avail );
-
- if ( main->rowgroup_ctr >= rowgroups_avail ) {
- main->buffer_full = FALSE;
- main->rowgroup_ctr = 0;
- }
- }
- METHODDEF void
- process_data_context_main( j_decompress_ptr cinfo,
- JSAMPARRAY output_buf, JDIMENSION * out_row_ctr,
- JDIMENSION out_rows_avail ) {
- my_main_ptr main = (my_main_ptr) cinfo->main;
-
- if ( !main->buffer_full ) {
- if ( !( *cinfo->coef->decompress_data )( cinfo,
- main->xbuffer[main->whichptr] ) ) {
- return;
- }
- main->buffer_full = TRUE;
- main->iMCU_row_ctr++;
- }
-
- switch ( main->context_state ) {
- case CTX_POSTPONED_ROW:
-
- ( *cinfo->post->post_process_data )( cinfo, main->xbuffer[main->whichptr],
- &main->rowgroup_ctr, main->rowgroups_avail,
- output_buf, out_row_ctr, out_rows_avail );
- if ( main->rowgroup_ctr < main->rowgroups_avail ) {
- return;
- }
- main->context_state = CTX_PREPARE_FOR_IMCU;
- if ( *out_row_ctr >= out_rows_avail ) {
- return;
- }
-
- case CTX_PREPARE_FOR_IMCU:
-
- main->rowgroup_ctr = 0;
- main->rowgroups_avail = (JDIMENSION) ( cinfo->min_DCT_scaled_size - 1 );
-
- if ( main->iMCU_row_ctr == cinfo->total_iMCU_rows ) {
- set_bottom_pointers( cinfo );
- }
- main->context_state = CTX_PROCESS_IMCU;
-
- case CTX_PROCESS_IMCU:
-
- ( *cinfo->post->post_process_data )( cinfo, main->xbuffer[main->whichptr],
- &main->rowgroup_ctr, main->rowgroups_avail,
- output_buf, out_row_ctr, out_rows_avail );
- if ( main->rowgroup_ctr < main->rowgroups_avail ) {
- return;
- }
-
- if ( main->iMCU_row_ctr == 1 ) {
- set_wraparound_pointers( cinfo );
- }
-
- main->whichptr ^= 1;
- main->buffer_full = FALSE;
-
-
- main->rowgroup_ctr = (JDIMENSION) ( cinfo->min_DCT_scaled_size + 1 );
- main->rowgroups_avail = (JDIMENSION) ( cinfo->min_DCT_scaled_size + 2 );
- main->context_state = CTX_POSTPONED_ROW;
- }
- }
- #ifdef QUANT_2PASS_SUPPORTED
- METHODDEF void
- process_data_crank_post( j_decompress_ptr cinfo,
- JSAMPARRAY output_buf, JDIMENSION * out_row_ctr,
- JDIMENSION out_rows_avail ) {
- ( *cinfo->post->post_process_data )( cinfo, (JSAMPIMAGE) NULL,
- (JDIMENSION *) NULL, (JDIMENSION) 0,
- output_buf, out_row_ctr, out_rows_avail );
- }
- #endif
- GLOBAL void
- jinit_d_main_controller( j_decompress_ptr cinfo, boolean need_full_buffer ) {
- my_main_ptr main;
- int ci, rgroup, ngroups;
- jpeg_component_info * compptr;
- main = (my_main_ptr)
- ( *cinfo->mem->alloc_small )( (j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF( my_main_controller ) );
- cinfo->main = (struct jpeg_d_main_controller *) main;
- main->pub.start_pass = start_pass_main;
- if ( need_full_buffer ) {
- ERREXIT( cinfo, JERR_BAD_BUFFER_MODE );
- }
-
- if ( cinfo->upsample->need_context_rows ) {
- if ( cinfo->min_DCT_scaled_size < 2 ) {
- ERREXIT( cinfo, JERR_NOTIMPL );
- }
- alloc_funny_pointers( cinfo );
- ngroups = cinfo->min_DCT_scaled_size + 2;
- } else {
- ngroups = cinfo->min_DCT_scaled_size;
- }
- for ( ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++ ) {
- rgroup = ( compptr->v_samp_factor * compptr->DCT_scaled_size ) /
- cinfo->min_DCT_scaled_size;
- main->buffer[ci] = ( *cinfo->mem->alloc_sarray )
- ( (j_common_ptr) cinfo, JPOOL_IMAGE,
- compptr->width_in_blocks * compptr->DCT_scaled_size,
- (JDIMENSION) ( rgroup * ngroups ) );
- }
- }
|