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