123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521 |
- #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 jmain = (my_main_ptr) cinfo->main;
- int ci, rgroup;
- int M = cinfo->min_DCT_scaled_size;
- jpeg_component_info *compptr;
- JSAMPARRAY xbuf;
-
- jmain->xbuffer[0] = (JSAMPIMAGE)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- cinfo->num_components * 2 * SIZEOF(JSAMPARRAY));
- jmain->xbuffer[1] = jmain->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;
- jmain->xbuffer[0][ci] = xbuf;
- xbuf += rgroup * (M + 4);
- jmain->xbuffer[1][ci] = xbuf;
- }
- }
- LOCAL void
- make_funny_pointers (j_decompress_ptr cinfo)
- {
-
- my_main_ptr jmain = (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 = jmain->xbuffer[0][ci];
- xbuf1 = jmain->xbuffer[1][ci];
-
- buf = jmain->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 jmain = (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 = jmain->xbuffer[0][ci];
- xbuf1 = jmain->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 jmain = (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) {
- jmain->rowgroups_avail = (JDIMENSION) ((rows_left-1) / rgroup + 1);
- }
-
- xbuf = jmain->xbuffer[jmain->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 jmain = (my_main_ptr) cinfo->main;
- switch (pass_mode) {
- case JBUF_PASS_THRU:
- if (cinfo->upsample->need_context_rows) {
- jmain->pub.process_data = process_data_context_main;
- make_funny_pointers(cinfo);
- jmain->whichptr = 0;
- jmain->context_state = CTX_PREPARE_FOR_IMCU;
- jmain->iMCU_row_ctr = 0;
- } else {
-
- jmain->pub.process_data = process_data_simple_main;
- }
- jmain->buffer_full = FALSE;
- jmain->rowgroup_ctr = 0;
- break;
- #ifdef QUANT_2PASS_SUPPORTED
- case JBUF_CRANK_DEST:
-
- jmain->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 jmain = (my_main_ptr) cinfo->main;
- JDIMENSION rowgroups_avail;
-
- if (! jmain->buffer_full) {
- if (! (*cinfo->coef->decompress_data) (cinfo, jmain->buffer))
- return;
- jmain->buffer_full = TRUE;
- }
-
- rowgroups_avail = (JDIMENSION) cinfo->min_DCT_scaled_size;
-
-
- (*cinfo->post->post_process_data) (cinfo, jmain->buffer,
- &jmain->rowgroup_ctr, rowgroups_avail,
- output_buf, out_row_ctr, out_rows_avail);
-
- if (jmain->rowgroup_ctr >= rowgroups_avail) {
- jmain->buffer_full = FALSE;
- jmain->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 jmain = (my_main_ptr) cinfo->main;
-
- if (! jmain->buffer_full) {
- if (! (*cinfo->coef->decompress_data) (cinfo,
- jmain->xbuffer[jmain->whichptr]))
- return;
- jmain->buffer_full = TRUE;
- jmain->iMCU_row_ctr++;
- }
-
- switch (jmain->context_state) {
- case CTX_POSTPONED_ROW:
-
- (*cinfo->post->post_process_data) (cinfo, jmain->xbuffer[jmain->whichptr],
- &jmain->rowgroup_ctr, jmain->rowgroups_avail,
- output_buf, out_row_ctr, out_rows_avail);
- if (jmain->rowgroup_ctr < jmain->rowgroups_avail)
- return;
- jmain->context_state = CTX_PREPARE_FOR_IMCU;
- if (*out_row_ctr >= out_rows_avail)
- return;
-
- case CTX_PREPARE_FOR_IMCU:
-
- jmain->rowgroup_ctr = 0;
- jmain->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size - 1);
-
- if (jmain->iMCU_row_ctr == cinfo->total_iMCU_rows)
- set_bottom_pointers(cinfo);
- jmain->context_state = CTX_PROCESS_IMCU;
-
- case CTX_PROCESS_IMCU:
-
- (*cinfo->post->post_process_data) (cinfo, jmain->xbuffer[jmain->whichptr],
- &jmain->rowgroup_ctr, jmain->rowgroups_avail,
- output_buf, out_row_ctr, out_rows_avail);
- if (jmain->rowgroup_ctr < jmain->rowgroups_avail)
- return;
-
- if (jmain->iMCU_row_ctr == 1)
- set_wraparound_pointers(cinfo);
-
- jmain->whichptr ^= 1;
- jmain->buffer_full = FALSE;
-
-
- jmain->rowgroup_ctr = (JDIMENSION) (cinfo->min_DCT_scaled_size + 1);
- jmain->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size + 2);
- jmain->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 jmain;
- int ci, rgroup, ngroups;
- jpeg_component_info *compptr;
- jmain = (my_main_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(my_main_controller));
- cinfo->main = (struct jpeg_d_main_controller *) jmain;
- jmain->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;
- jmain->buffer[ci] = (*cinfo->mem->alloc_sarray)
- ((j_common_ptr) cinfo, JPOOL_IMAGE,
- compptr->width_in_blocks * compptr->DCT_scaled_size,
- (JDIMENSION) (rgroup * ngroups));
- }
- }
|