nuclear@1: /* nuclear@1: * jcmaster.c nuclear@1: * nuclear@1: * Copyright (C) 1991-1997, Thomas G. Lane. nuclear@1: * This file is part of the Independent JPEG Group's software. nuclear@1: * For conditions of distribution and use, see the accompanying README file. nuclear@1: * nuclear@1: * This file contains master control logic for the JPEG compressor. nuclear@1: * These routines are concerned with parameter validation, initial setup, nuclear@1: * and inter-pass control (determining the number of passes and the work nuclear@1: * to be done in each pass). nuclear@1: */ nuclear@1: nuclear@1: #define JPEG_INTERNALS nuclear@1: #include "jinclude.h" nuclear@1: #include "jpeglib.h" nuclear@1: nuclear@1: nuclear@1: /* Private state */ nuclear@1: nuclear@1: typedef enum { nuclear@1: main_pass, /* input data, also do first output step */ nuclear@1: huff_opt_pass, /* Huffman code optimization pass */ nuclear@1: output_pass /* data output pass */ nuclear@1: } c_pass_type; nuclear@1: nuclear@1: typedef struct { nuclear@1: struct jpeg_comp_master pub; /* public fields */ nuclear@1: nuclear@1: c_pass_type pass_type; /* the type of the current pass */ nuclear@1: nuclear@1: int pass_number; /* # of passes completed */ nuclear@1: int total_passes; /* total # of passes needed */ nuclear@1: nuclear@1: int scan_number; /* current index in scan_info[] */ nuclear@1: } my_comp_master; nuclear@1: nuclear@1: typedef my_comp_master * my_master_ptr; nuclear@1: nuclear@1: nuclear@1: /* nuclear@1: * Support routines that do various essential calculations. nuclear@1: */ nuclear@1: nuclear@1: LOCAL(void) nuclear@1: initial_setup (j_compress_ptr cinfo) nuclear@1: /* Do computations that are needed before master selection phase */ nuclear@1: { nuclear@1: int ci; nuclear@1: jpeg_component_info *compptr; nuclear@1: long samplesperrow; nuclear@1: JDIMENSION jd_samplesperrow; nuclear@1: nuclear@1: /* Sanity check on image dimensions */ nuclear@1: if (cinfo->image_height <= 0 || cinfo->image_width <= 0 nuclear@1: || cinfo->num_components <= 0 || cinfo->input_components <= 0) nuclear@1: ERREXIT(cinfo, JERR_EMPTY_IMAGE); nuclear@1: nuclear@1: /* Make sure image isn't bigger than I can handle */ nuclear@1: if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION || nuclear@1: (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION) nuclear@1: ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); nuclear@1: nuclear@1: /* Width of an input scanline must be representable as JDIMENSION. */ nuclear@1: samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components; nuclear@1: jd_samplesperrow = (JDIMENSION) samplesperrow; nuclear@1: if ((long) jd_samplesperrow != samplesperrow) nuclear@1: ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); nuclear@1: nuclear@1: /* For now, precision must match compiled-in value... */ nuclear@1: if (cinfo->data_precision != BITS_IN_JSAMPLE) nuclear@1: ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); nuclear@1: nuclear@1: /* Check that number of components won't exceed internal array sizes */ nuclear@1: if (cinfo->num_components > MAX_COMPONENTS) nuclear@1: ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, nuclear@1: MAX_COMPONENTS); nuclear@1: nuclear@1: /* Compute maximum sampling factors; check factor validity */ nuclear@1: cinfo->max_h_samp_factor = 1; nuclear@1: cinfo->max_v_samp_factor = 1; nuclear@1: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; nuclear@1: ci++, compptr++) { nuclear@1: if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR || nuclear@1: compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR) nuclear@1: ERREXIT(cinfo, JERR_BAD_SAMPLING); nuclear@1: cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor, nuclear@1: compptr->h_samp_factor); nuclear@1: cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor, nuclear@1: compptr->v_samp_factor); nuclear@1: } nuclear@1: nuclear@1: /* Compute dimensions of components */ nuclear@1: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; nuclear@1: ci++, compptr++) { nuclear@1: /* Fill in the correct component_index value; don't rely on application */ nuclear@1: compptr->component_index = ci; nuclear@1: /* For compression, we never do DCT scaling. */ nuclear@1: compptr->DCT_scaled_size = DCTSIZE; nuclear@1: /* Size in DCT blocks */ nuclear@1: compptr->width_in_blocks = (JDIMENSION) nuclear@1: jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor, nuclear@1: (long) (cinfo->max_h_samp_factor * DCTSIZE)); nuclear@1: compptr->height_in_blocks = (JDIMENSION) nuclear@1: jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor, nuclear@1: (long) (cinfo->max_v_samp_factor * DCTSIZE)); nuclear@1: /* Size in samples */ nuclear@1: compptr->downsampled_width = (JDIMENSION) nuclear@1: jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor, nuclear@1: (long) cinfo->max_h_samp_factor); nuclear@1: compptr->downsampled_height = (JDIMENSION) nuclear@1: jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor, nuclear@1: (long) cinfo->max_v_samp_factor); nuclear@1: /* Mark component needed (this flag isn't actually used for compression) */ nuclear@1: compptr->component_needed = TRUE; nuclear@1: } nuclear@1: nuclear@1: /* Compute number of fully interleaved MCU rows (number of times that nuclear@1: * main controller will call coefficient controller). nuclear@1: */ nuclear@1: cinfo->total_iMCU_rows = (JDIMENSION) nuclear@1: jdiv_round_up((long) cinfo->image_height, nuclear@1: (long) (cinfo->max_v_samp_factor*DCTSIZE)); nuclear@1: } nuclear@1: nuclear@1: nuclear@1: #ifdef C_MULTISCAN_FILES_SUPPORTED nuclear@1: nuclear@1: LOCAL(void) nuclear@1: validate_script (j_compress_ptr cinfo) nuclear@1: /* Verify that the scan script in cinfo->scan_info[] is valid; also nuclear@1: * determine whether it uses progressive JPEG, and set cinfo->progressive_mode. nuclear@1: */ nuclear@1: { nuclear@1: const jpeg_scan_info * scanptr; nuclear@1: int scanno, ncomps, ci, coefi, thisi; nuclear@1: int Ss, Se, Ah, Al; nuclear@1: boolean component_sent[MAX_COMPONENTS]; nuclear@1: #ifdef C_PROGRESSIVE_SUPPORTED nuclear@1: int * last_bitpos_ptr; nuclear@1: int last_bitpos[MAX_COMPONENTS][DCTSIZE2]; nuclear@1: /* -1 until that coefficient has been seen; then last Al for it */ nuclear@1: #endif nuclear@1: nuclear@1: if (cinfo->num_scans <= 0) nuclear@1: ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0); nuclear@1: nuclear@1: /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1; nuclear@1: * for progressive JPEG, no scan can have this. nuclear@1: */ nuclear@1: scanptr = cinfo->scan_info; nuclear@1: if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) { nuclear@1: #ifdef C_PROGRESSIVE_SUPPORTED nuclear@1: cinfo->progressive_mode = TRUE; nuclear@1: last_bitpos_ptr = & last_bitpos[0][0]; nuclear@1: for (ci = 0; ci < cinfo->num_components; ci++) nuclear@1: for (coefi = 0; coefi < DCTSIZE2; coefi++) nuclear@1: *last_bitpos_ptr++ = -1; nuclear@1: #else nuclear@1: ERREXIT(cinfo, JERR_NOT_COMPILED); nuclear@1: #endif nuclear@1: } else { nuclear@1: cinfo->progressive_mode = FALSE; nuclear@1: for (ci = 0; ci < cinfo->num_components; ci++) nuclear@1: component_sent[ci] = FALSE; nuclear@1: } nuclear@1: nuclear@1: for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) { nuclear@1: /* Validate component indexes */ nuclear@1: ncomps = scanptr->comps_in_scan; nuclear@1: if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN) nuclear@1: ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN); nuclear@1: for (ci = 0; ci < ncomps; ci++) { nuclear@1: thisi = scanptr->component_index[ci]; nuclear@1: if (thisi < 0 || thisi >= cinfo->num_components) nuclear@1: ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); nuclear@1: /* Components must appear in SOF order within each scan */ nuclear@1: if (ci > 0 && thisi <= scanptr->component_index[ci-1]) nuclear@1: ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); nuclear@1: } nuclear@1: /* Validate progression parameters */ nuclear@1: Ss = scanptr->Ss; nuclear@1: Se = scanptr->Se; nuclear@1: Ah = scanptr->Ah; nuclear@1: Al = scanptr->Al; nuclear@1: if (cinfo->progressive_mode) { nuclear@1: #ifdef C_PROGRESSIVE_SUPPORTED nuclear@1: /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that nuclear@1: * seems wrong: the upper bound ought to depend on data precision. nuclear@1: * Perhaps they really meant 0..N+1 for N-bit precision. nuclear@1: * Here we allow 0..10 for 8-bit data; Al larger than 10 results in nuclear@1: * out-of-range reconstructed DC values during the first DC scan, nuclear@1: * which might cause problems for some decoders. nuclear@1: */ nuclear@1: #if BITS_IN_JSAMPLE == 8 nuclear@1: #define MAX_AH_AL 10 nuclear@1: #else nuclear@1: #define MAX_AH_AL 13 nuclear@1: #endif nuclear@1: if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 || nuclear@1: Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL) nuclear@1: ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); nuclear@1: if (Ss == 0) { nuclear@1: if (Se != 0) /* DC and AC together not OK */ nuclear@1: ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); nuclear@1: } else { nuclear@1: if (ncomps != 1) /* AC scans must be for only one component */ nuclear@1: ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); nuclear@1: } nuclear@1: for (ci = 0; ci < ncomps; ci++) { nuclear@1: last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0]; nuclear@1: if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */ nuclear@1: ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); nuclear@1: for (coefi = Ss; coefi <= Se; coefi++) { nuclear@1: if (last_bitpos_ptr[coefi] < 0) { nuclear@1: /* first scan of this coefficient */ nuclear@1: if (Ah != 0) nuclear@1: ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); nuclear@1: } else { nuclear@1: /* not first scan */ nuclear@1: if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1) nuclear@1: ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); nuclear@1: } nuclear@1: last_bitpos_ptr[coefi] = Al; nuclear@1: } nuclear@1: } nuclear@1: #endif nuclear@1: } else { nuclear@1: /* For sequential JPEG, all progression parameters must be these: */ nuclear@1: if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0) nuclear@1: ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); nuclear@1: /* Make sure components are not sent twice */ nuclear@1: for (ci = 0; ci < ncomps; ci++) { nuclear@1: thisi = scanptr->component_index[ci]; nuclear@1: if (component_sent[thisi]) nuclear@1: ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); nuclear@1: component_sent[thisi] = TRUE; nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* Now verify that everything got sent. */ nuclear@1: if (cinfo->progressive_mode) { nuclear@1: #ifdef C_PROGRESSIVE_SUPPORTED nuclear@1: /* For progressive mode, we only check that at least some DC data nuclear@1: * got sent for each component; the spec does not require that all bits nuclear@1: * of all coefficients be transmitted. Would it be wiser to enforce nuclear@1: * transmission of all coefficient bits?? nuclear@1: */ nuclear@1: for (ci = 0; ci < cinfo->num_components; ci++) { nuclear@1: if (last_bitpos[ci][0] < 0) nuclear@1: ERREXIT(cinfo, JERR_MISSING_DATA); nuclear@1: } nuclear@1: #endif nuclear@1: } else { nuclear@1: for (ci = 0; ci < cinfo->num_components; ci++) { nuclear@1: if (! component_sent[ci]) nuclear@1: ERREXIT(cinfo, JERR_MISSING_DATA); nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: #endif /* C_MULTISCAN_FILES_SUPPORTED */ nuclear@1: nuclear@1: nuclear@1: LOCAL(void) nuclear@1: select_scan_parameters (j_compress_ptr cinfo) nuclear@1: /* Set up the scan parameters for the current scan */ nuclear@1: { nuclear@1: int ci; nuclear@1: nuclear@1: #ifdef C_MULTISCAN_FILES_SUPPORTED nuclear@1: if (cinfo->scan_info != NULL) { nuclear@1: /* Prepare for current scan --- the script is already validated */ nuclear@1: my_master_ptr master = (my_master_ptr) cinfo->master; nuclear@1: const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number; nuclear@1: nuclear@1: cinfo->comps_in_scan = scanptr->comps_in_scan; nuclear@1: for (ci = 0; ci < scanptr->comps_in_scan; ci++) { nuclear@1: cinfo->cur_comp_info[ci] = nuclear@1: &cinfo->comp_info[scanptr->component_index[ci]]; nuclear@1: } nuclear@1: cinfo->Ss = scanptr->Ss; nuclear@1: cinfo->Se = scanptr->Se; nuclear@1: cinfo->Ah = scanptr->Ah; nuclear@1: cinfo->Al = scanptr->Al; nuclear@1: } nuclear@1: else nuclear@1: #endif nuclear@1: { nuclear@1: /* Prepare for single sequential-JPEG scan containing all components */ nuclear@1: if (cinfo->num_components > MAX_COMPS_IN_SCAN) nuclear@1: ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, nuclear@1: MAX_COMPS_IN_SCAN); nuclear@1: cinfo->comps_in_scan = cinfo->num_components; nuclear@1: for (ci = 0; ci < cinfo->num_components; ci++) { nuclear@1: cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci]; nuclear@1: } nuclear@1: cinfo->Ss = 0; nuclear@1: cinfo->Se = DCTSIZE2-1; nuclear@1: cinfo->Ah = 0; nuclear@1: cinfo->Al = 0; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: nuclear@1: LOCAL(void) nuclear@1: per_scan_setup (j_compress_ptr cinfo) nuclear@1: /* Do computations that are needed before processing a JPEG scan */ nuclear@1: /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */ nuclear@1: { nuclear@1: int ci, mcublks, tmp; nuclear@1: jpeg_component_info *compptr; nuclear@1: nuclear@1: if (cinfo->comps_in_scan == 1) { nuclear@1: nuclear@1: /* Noninterleaved (single-component) scan */ nuclear@1: compptr = cinfo->cur_comp_info[0]; nuclear@1: nuclear@1: /* Overall image size in MCUs */ nuclear@1: cinfo->MCUs_per_row = compptr->width_in_blocks; nuclear@1: cinfo->MCU_rows_in_scan = compptr->height_in_blocks; nuclear@1: nuclear@1: /* For noninterleaved scan, always one block per MCU */ nuclear@1: compptr->MCU_width = 1; nuclear@1: compptr->MCU_height = 1; nuclear@1: compptr->MCU_blocks = 1; nuclear@1: compptr->MCU_sample_width = DCTSIZE; nuclear@1: compptr->last_col_width = 1; nuclear@1: /* For noninterleaved scans, it is convenient to define last_row_height nuclear@1: * as the number of block rows present in the last iMCU row. nuclear@1: */ nuclear@1: tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor); nuclear@1: if (tmp == 0) tmp = compptr->v_samp_factor; nuclear@1: compptr->last_row_height = tmp; nuclear@1: nuclear@1: /* Prepare array describing MCU composition */ nuclear@1: cinfo->blocks_in_MCU = 1; nuclear@1: cinfo->MCU_membership[0] = 0; nuclear@1: nuclear@1: } else { nuclear@1: nuclear@1: /* Interleaved (multi-component) scan */ nuclear@1: if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) nuclear@1: ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, nuclear@1: MAX_COMPS_IN_SCAN); nuclear@1: nuclear@1: /* Overall image size in MCUs */ nuclear@1: cinfo->MCUs_per_row = (JDIMENSION) nuclear@1: jdiv_round_up((long) cinfo->image_width, nuclear@1: (long) (cinfo->max_h_samp_factor*DCTSIZE)); nuclear@1: cinfo->MCU_rows_in_scan = (JDIMENSION) nuclear@1: jdiv_round_up((long) cinfo->image_height, nuclear@1: (long) (cinfo->max_v_samp_factor*DCTSIZE)); nuclear@1: nuclear@1: cinfo->blocks_in_MCU = 0; nuclear@1: nuclear@1: for (ci = 0; ci < cinfo->comps_in_scan; ci++) { nuclear@1: compptr = cinfo->cur_comp_info[ci]; nuclear@1: /* Sampling factors give # of blocks of component in each MCU */ nuclear@1: compptr->MCU_width = compptr->h_samp_factor; nuclear@1: compptr->MCU_height = compptr->v_samp_factor; nuclear@1: compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; nuclear@1: compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE; nuclear@1: /* Figure number of non-dummy blocks in last MCU column & row */ nuclear@1: tmp = (int) (compptr->width_in_blocks % compptr->MCU_width); nuclear@1: if (tmp == 0) tmp = compptr->MCU_width; nuclear@1: compptr->last_col_width = tmp; nuclear@1: tmp = (int) (compptr->height_in_blocks % compptr->MCU_height); nuclear@1: if (tmp == 0) tmp = compptr->MCU_height; nuclear@1: compptr->last_row_height = tmp; nuclear@1: /* Prepare array describing MCU composition */ nuclear@1: mcublks = compptr->MCU_blocks; nuclear@1: if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU) nuclear@1: ERREXIT(cinfo, JERR_BAD_MCU_SIZE); nuclear@1: while (mcublks-- > 0) { nuclear@1: cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: } nuclear@1: nuclear@1: /* Convert restart specified in rows to actual MCU count. */ nuclear@1: /* Note that count must fit in 16 bits, so we provide limiting. */ nuclear@1: if (cinfo->restart_in_rows > 0) { nuclear@1: long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row; nuclear@1: cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: nuclear@1: /* nuclear@1: * Per-pass setup. nuclear@1: * This is called at the beginning of each pass. We determine which modules nuclear@1: * will be active during this pass and give them appropriate start_pass calls. nuclear@1: * We also set is_last_pass to indicate whether any more passes will be nuclear@1: * required. nuclear@1: */ nuclear@1: nuclear@1: METHODDEF(void) nuclear@1: prepare_for_pass (j_compress_ptr cinfo) nuclear@1: { nuclear@1: my_master_ptr master = (my_master_ptr) cinfo->master; nuclear@1: nuclear@1: switch (master->pass_type) { nuclear@1: case main_pass: nuclear@1: /* Initial pass: will collect input data, and do either Huffman nuclear@1: * optimization or data output for the first scan. nuclear@1: */ nuclear@1: select_scan_parameters(cinfo); nuclear@1: per_scan_setup(cinfo); nuclear@1: if (! cinfo->raw_data_in) { nuclear@1: (*cinfo->cconvert->start_pass) (cinfo); nuclear@1: (*cinfo->downsample->start_pass) (cinfo); nuclear@1: (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU); nuclear@1: } nuclear@1: (*cinfo->fdct->start_pass) (cinfo); nuclear@1: (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding); nuclear@1: (*cinfo->coef->start_pass) (cinfo, nuclear@1: (master->total_passes > 1 ? nuclear@1: JBUF_SAVE_AND_PASS : JBUF_PASS_THRU)); nuclear@1: (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU); nuclear@1: if (cinfo->optimize_coding) { nuclear@1: /* No immediate data output; postpone writing frame/scan headers */ nuclear@1: master->pub.call_pass_startup = FALSE; nuclear@1: } else { nuclear@1: /* Will write frame/scan headers at first jpeg_write_scanlines call */ nuclear@1: master->pub.call_pass_startup = TRUE; nuclear@1: } nuclear@1: break; nuclear@1: #ifdef ENTROPY_OPT_SUPPORTED nuclear@1: case huff_opt_pass: nuclear@1: /* Do Huffman optimization for a scan after the first one. */ nuclear@1: select_scan_parameters(cinfo); nuclear@1: per_scan_setup(cinfo); nuclear@1: if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) { nuclear@1: (*cinfo->entropy->start_pass) (cinfo, TRUE); nuclear@1: (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); nuclear@1: master->pub.call_pass_startup = FALSE; nuclear@1: break; nuclear@1: } nuclear@1: /* Special case: Huffman DC refinement scans need no Huffman table nuclear@1: * and therefore we can skip the optimization pass for them. nuclear@1: */ nuclear@1: master->pass_type = output_pass; nuclear@1: master->pass_number++; nuclear@1: /*FALLTHROUGH*/ nuclear@1: #endif nuclear@1: case output_pass: nuclear@1: /* Do a data-output pass. */ nuclear@1: /* We need not repeat per-scan setup if prior optimization pass did it. */ nuclear@1: if (! cinfo->optimize_coding) { nuclear@1: select_scan_parameters(cinfo); nuclear@1: per_scan_setup(cinfo); nuclear@1: } nuclear@1: (*cinfo->entropy->start_pass) (cinfo, FALSE); nuclear@1: (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); nuclear@1: /* We emit frame/scan headers now */ nuclear@1: if (master->scan_number == 0) nuclear@1: (*cinfo->marker->write_frame_header) (cinfo); nuclear@1: (*cinfo->marker->write_scan_header) (cinfo); nuclear@1: master->pub.call_pass_startup = FALSE; nuclear@1: break; nuclear@1: default: nuclear@1: ERREXIT(cinfo, JERR_NOT_COMPILED); nuclear@1: } nuclear@1: nuclear@1: master->pub.is_last_pass = (master->pass_number == master->total_passes-1); nuclear@1: nuclear@1: /* Set up progress monitor's pass info if present */ nuclear@1: if (cinfo->progress != NULL) { nuclear@1: cinfo->progress->completed_passes = master->pass_number; nuclear@1: cinfo->progress->total_passes = master->total_passes; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: nuclear@1: /* nuclear@1: * Special start-of-pass hook. nuclear@1: * This is called by jpeg_write_scanlines if call_pass_startup is TRUE. nuclear@1: * In single-pass processing, we need this hook because we don't want to nuclear@1: * write frame/scan headers during jpeg_start_compress; we want to let the nuclear@1: * application write COM markers etc. between jpeg_start_compress and the nuclear@1: * jpeg_write_scanlines loop. nuclear@1: * In multi-pass processing, this routine is not used. nuclear@1: */ nuclear@1: nuclear@1: METHODDEF(void) nuclear@1: pass_startup (j_compress_ptr cinfo) nuclear@1: { nuclear@1: cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */ nuclear@1: nuclear@1: (*cinfo->marker->write_frame_header) (cinfo); nuclear@1: (*cinfo->marker->write_scan_header) (cinfo); nuclear@1: } nuclear@1: nuclear@1: nuclear@1: /* nuclear@1: * Finish up at end of pass. nuclear@1: */ nuclear@1: nuclear@1: METHODDEF(void) nuclear@1: finish_pass_master (j_compress_ptr cinfo) nuclear@1: { nuclear@1: my_master_ptr master = (my_master_ptr) cinfo->master; nuclear@1: nuclear@1: /* The entropy coder always needs an end-of-pass call, nuclear@1: * either to analyze statistics or to flush its output buffer. nuclear@1: */ nuclear@1: (*cinfo->entropy->finish_pass) (cinfo); nuclear@1: nuclear@1: /* Update state for next pass */ nuclear@1: switch (master->pass_type) { nuclear@1: case main_pass: nuclear@1: /* next pass is either output of scan 0 (after optimization) nuclear@1: * or output of scan 1 (if no optimization). nuclear@1: */ nuclear@1: master->pass_type = output_pass; nuclear@1: if (! cinfo->optimize_coding) nuclear@1: master->scan_number++; nuclear@1: break; nuclear@1: case huff_opt_pass: nuclear@1: /* next pass is always output of current scan */ nuclear@1: master->pass_type = output_pass; nuclear@1: break; nuclear@1: case output_pass: nuclear@1: /* next pass is either optimization or output of next scan */ nuclear@1: if (cinfo->optimize_coding) nuclear@1: master->pass_type = huff_opt_pass; nuclear@1: master->scan_number++; nuclear@1: break; nuclear@1: } nuclear@1: nuclear@1: master->pass_number++; nuclear@1: } nuclear@1: nuclear@1: nuclear@1: /* nuclear@1: * Initialize master compression control. nuclear@1: */ nuclear@1: nuclear@1: GLOBAL(void) nuclear@1: jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only) nuclear@1: { nuclear@1: my_master_ptr master; nuclear@1: nuclear@1: master = (my_master_ptr) nuclear@1: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, nuclear@1: SIZEOF(my_comp_master)); nuclear@1: cinfo->master = (struct jpeg_comp_master *) master; nuclear@1: master->pub.prepare_for_pass = prepare_for_pass; nuclear@1: master->pub.pass_startup = pass_startup; nuclear@1: master->pub.finish_pass = finish_pass_master; nuclear@1: master->pub.is_last_pass = FALSE; nuclear@1: nuclear@1: /* Validate parameters, determine derived values */ nuclear@1: initial_setup(cinfo); nuclear@1: nuclear@1: if (cinfo->scan_info != NULL) { nuclear@1: #ifdef C_MULTISCAN_FILES_SUPPORTED nuclear@1: validate_script(cinfo); nuclear@1: #else nuclear@1: ERREXIT(cinfo, JERR_NOT_COMPILED); nuclear@1: #endif nuclear@1: } else { nuclear@1: cinfo->progressive_mode = FALSE; nuclear@1: cinfo->num_scans = 1; nuclear@1: } nuclear@1: nuclear@1: if (cinfo->progressive_mode) /* TEMPORARY HACK ??? */ nuclear@1: cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */ nuclear@1: nuclear@1: /* Initialize my private state */ nuclear@1: if (transcode_only) { nuclear@1: /* no main pass in transcoding */ nuclear@1: if (cinfo->optimize_coding) nuclear@1: master->pass_type = huff_opt_pass; nuclear@1: else nuclear@1: master->pass_type = output_pass; nuclear@1: } else { nuclear@1: /* for normal compression, first pass is always this type: */ nuclear@1: master->pass_type = main_pass; nuclear@1: } nuclear@1: master->scan_number = 0; nuclear@1: master->pass_number = 0; nuclear@1: if (cinfo->optimize_coding) nuclear@1: master->total_passes = cinfo->num_scans * 2; nuclear@1: else nuclear@1: master->total_passes = cinfo->num_scans; nuclear@1: }