Path: blob/master/src/java.desktop/share/native/libjavajpeg/jcmaster.c
41152 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jcmaster.c6*7* Copyright (C) 1991-1997, Thomas G. Lane.8* This file is part of the Independent JPEG Group's software.9* For conditions of distribution and use, see the accompanying README file.10*11* This file contains master control logic for the JPEG compressor.12* These routines are concerned with parameter validation, initial setup,13* and inter-pass control (determining the number of passes and the work14* to be done in each pass).15*/1617#define JPEG_INTERNALS18#include "jinclude.h"19#include "jpeglib.h"202122/* Private state */2324typedef enum {25main_pass, /* input data, also do first output step */26huff_opt_pass, /* Huffman code optimization pass */27output_pass /* data output pass */28} c_pass_type;2930typedef struct {31struct jpeg_comp_master pub; /* public fields */3233c_pass_type pass_type; /* the type of the current pass */3435int pass_number; /* # of passes completed */36int total_passes; /* total # of passes needed */3738int scan_number; /* current index in scan_info[] */39} my_comp_master;4041typedef my_comp_master * my_master_ptr;424344/*45* Support routines that do various essential calculations.46*/4748LOCAL(void)49initial_setup (j_compress_ptr cinfo)50/* Do computations that are needed before master selection phase */51{52int ci;53jpeg_component_info *compptr;54long samplesperrow;55JDIMENSION jd_samplesperrow;5657/* Sanity check on image dimensions */58if (cinfo->image_height <= 0 || cinfo->image_width <= 059|| cinfo->num_components <= 0 || cinfo->input_components <= 0)60ERREXIT(cinfo, JERR_EMPTY_IMAGE);6162/* Make sure image isn't bigger than I can handle */63if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||64(long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)65ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);6667/* Width of an input scanline must be representable as JDIMENSION. */68samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;69jd_samplesperrow = (JDIMENSION) samplesperrow;70if ((long) jd_samplesperrow != samplesperrow)71ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);7273/* For now, precision must match compiled-in value... */74if (cinfo->data_precision != BITS_IN_JSAMPLE)75ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);7677/* Check that number of components won't exceed internal array sizes */78if (cinfo->num_components > MAX_COMPONENTS)79ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,80MAX_COMPONENTS);8182/* Compute maximum sampling factors; check factor validity */83cinfo->max_h_samp_factor = 1;84cinfo->max_v_samp_factor = 1;85for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;86ci++, compptr++) {87if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||88compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)89ERREXIT(cinfo, JERR_BAD_SAMPLING);90cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,91compptr->h_samp_factor);92cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,93compptr->v_samp_factor);94}9596/* Compute dimensions of components */97for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;98ci++, compptr++) {99/* Fill in the correct component_index value; don't rely on application */100compptr->component_index = ci;101/* For compression, we never do DCT scaling. */102compptr->DCT_scaled_size = DCTSIZE;103/* Size in DCT blocks */104compptr->width_in_blocks = (JDIMENSION)105jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,106(long) (cinfo->max_h_samp_factor * DCTSIZE));107compptr->height_in_blocks = (JDIMENSION)108jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,109(long) (cinfo->max_v_samp_factor * DCTSIZE));110/* Size in samples */111compptr->downsampled_width = (JDIMENSION)112jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,113(long) cinfo->max_h_samp_factor);114compptr->downsampled_height = (JDIMENSION)115jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,116(long) cinfo->max_v_samp_factor);117/* Mark component needed (this flag isn't actually used for compression) */118compptr->component_needed = TRUE;119}120121/* Compute number of fully interleaved MCU rows (number of times that122* main controller will call coefficient controller).123*/124cinfo->total_iMCU_rows = (JDIMENSION)125jdiv_round_up((long) cinfo->image_height,126(long) (cinfo->max_v_samp_factor*DCTSIZE));127}128129130#ifdef C_MULTISCAN_FILES_SUPPORTED131132LOCAL(void)133validate_script (j_compress_ptr cinfo)134/* Verify that the scan script in cinfo->scan_info[] is valid; also135* determine whether it uses progressive JPEG, and set cinfo->progressive_mode.136*/137{138const jpeg_scan_info * scanptr;139int scanno, ncomps, ci, coefi, thisi;140int Ss, Se, Ah, Al;141boolean component_sent[MAX_COMPONENTS];142#ifdef C_PROGRESSIVE_SUPPORTED143int * last_bitpos_ptr;144int last_bitpos[MAX_COMPONENTS][DCTSIZE2];145/* -1 until that coefficient has been seen; then last Al for it */146#endif147148if (cinfo->num_scans <= 0)149ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);150151/* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;152* for progressive JPEG, no scan can have this.153*/154scanptr = cinfo->scan_info;155if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {156#ifdef C_PROGRESSIVE_SUPPORTED157cinfo->progressive_mode = TRUE;158last_bitpos_ptr = & last_bitpos[0][0];159for (ci = 0; ci < cinfo->num_components; ci++)160for (coefi = 0; coefi < DCTSIZE2; coefi++)161*last_bitpos_ptr++ = -1;162#else163ERREXIT(cinfo, JERR_NOT_COMPILED);164#endif165} else {166cinfo->progressive_mode = FALSE;167for (ci = 0; ci < cinfo->num_components; ci++)168component_sent[ci] = FALSE;169}170171for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {172/* Validate component indexes */173ncomps = scanptr->comps_in_scan;174if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)175ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);176for (ci = 0; ci < ncomps; ci++) {177thisi = scanptr->component_index[ci];178if (thisi < 0 || thisi >= cinfo->num_components)179ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);180/* Components must appear in SOF order within each scan */181if (ci > 0 && thisi <= scanptr->component_index[ci-1])182ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);183}184/* Validate progression parameters */185Ss = scanptr->Ss;186Se = scanptr->Se;187Ah = scanptr->Ah;188Al = scanptr->Al;189if (cinfo->progressive_mode) {190#ifdef C_PROGRESSIVE_SUPPORTED191/* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that192* seems wrong: the upper bound ought to depend on data precision.193* Perhaps they really meant 0..N+1 for N-bit precision.194* Here we allow 0..10 for 8-bit data; Al larger than 10 results in195* out-of-range reconstructed DC values during the first DC scan,196* which might cause problems for some decoders.197*/198#if BITS_IN_JSAMPLE == 8199#define MAX_AH_AL 10200#else201#define MAX_AH_AL 13202#endif203if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||204Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)205ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);206if (Ss == 0) {207if (Se != 0) /* DC and AC together not OK */208ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);209} else {210if (ncomps != 1) /* AC scans must be for only one component */211ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);212}213for (ci = 0; ci < ncomps; ci++) {214last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];215if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */216ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);217for (coefi = Ss; coefi <= Se; coefi++) {218if (last_bitpos_ptr[coefi] < 0) {219/* first scan of this coefficient */220if (Ah != 0)221ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);222} else {223/* not first scan */224if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)225ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);226}227last_bitpos_ptr[coefi] = Al;228}229}230#endif231} else {232/* For sequential JPEG, all progression parameters must be these: */233if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)234ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);235/* Make sure components are not sent twice */236for (ci = 0; ci < ncomps; ci++) {237thisi = scanptr->component_index[ci];238if (component_sent[thisi])239ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);240component_sent[thisi] = TRUE;241}242}243}244245/* Now verify that everything got sent. */246if (cinfo->progressive_mode) {247#ifdef C_PROGRESSIVE_SUPPORTED248/* For progressive mode, we only check that at least some DC data249* got sent for each component; the spec does not require that all bits250* of all coefficients be transmitted. Would it be wiser to enforce251* transmission of all coefficient bits??252*/253for (ci = 0; ci < cinfo->num_components; ci++) {254if (last_bitpos[ci][0] < 0)255ERREXIT(cinfo, JERR_MISSING_DATA);256}257#endif258} else {259for (ci = 0; ci < cinfo->num_components; ci++) {260if (! component_sent[ci])261ERREXIT(cinfo, JERR_MISSING_DATA);262}263}264}265266#endif /* C_MULTISCAN_FILES_SUPPORTED */267268269LOCAL(void)270select_scan_parameters (j_compress_ptr cinfo)271/* Set up the scan parameters for the current scan */272{273int ci;274275#ifdef C_MULTISCAN_FILES_SUPPORTED276if (cinfo->scan_info != NULL) {277/* Prepare for current scan --- the script is already validated */278my_master_ptr master = (my_master_ptr) cinfo->master;279const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;280281cinfo->comps_in_scan = scanptr->comps_in_scan;282for (ci = 0; ci < scanptr->comps_in_scan; ci++) {283cinfo->cur_comp_info[ci] =284&cinfo->comp_info[scanptr->component_index[ci]];285}286cinfo->Ss = scanptr->Ss;287cinfo->Se = scanptr->Se;288cinfo->Ah = scanptr->Ah;289cinfo->Al = scanptr->Al;290}291else292#endif293{294/* Prepare for single sequential-JPEG scan containing all components */295if (cinfo->num_components > MAX_COMPS_IN_SCAN)296ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,297MAX_COMPS_IN_SCAN);298cinfo->comps_in_scan = cinfo->num_components;299for (ci = 0; ci < cinfo->num_components; ci++) {300cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];301}302cinfo->Ss = 0;303cinfo->Se = DCTSIZE2-1;304cinfo->Ah = 0;305cinfo->Al = 0;306}307}308309310LOCAL(void)311per_scan_setup (j_compress_ptr cinfo)312/* Do computations that are needed before processing a JPEG scan */313/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */314{315int ci, mcublks, tmp;316jpeg_component_info *compptr;317318if (cinfo->comps_in_scan == 1) {319320/* Noninterleaved (single-component) scan */321compptr = cinfo->cur_comp_info[0];322323/* Overall image size in MCUs */324cinfo->MCUs_per_row = compptr->width_in_blocks;325cinfo->MCU_rows_in_scan = compptr->height_in_blocks;326327/* For noninterleaved scan, always one block per MCU */328compptr->MCU_width = 1;329compptr->MCU_height = 1;330compptr->MCU_blocks = 1;331compptr->MCU_sample_width = DCTSIZE;332compptr->last_col_width = 1;333/* For noninterleaved scans, it is convenient to define last_row_height334* as the number of block rows present in the last iMCU row.335*/336tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);337if (tmp == 0) tmp = compptr->v_samp_factor;338compptr->last_row_height = tmp;339340/* Prepare array describing MCU composition */341cinfo->blocks_in_MCU = 1;342cinfo->MCU_membership[0] = 0;343344} else {345346/* Interleaved (multi-component) scan */347if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)348ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,349MAX_COMPS_IN_SCAN);350351/* Overall image size in MCUs */352cinfo->MCUs_per_row = (JDIMENSION)353jdiv_round_up((long) cinfo->image_width,354(long) (cinfo->max_h_samp_factor*DCTSIZE));355cinfo->MCU_rows_in_scan = (JDIMENSION)356jdiv_round_up((long) cinfo->image_height,357(long) (cinfo->max_v_samp_factor*DCTSIZE));358359cinfo->blocks_in_MCU = 0;360361for (ci = 0; ci < cinfo->comps_in_scan; ci++) {362compptr = cinfo->cur_comp_info[ci];363/* Sampling factors give # of blocks of component in each MCU */364compptr->MCU_width = compptr->h_samp_factor;365compptr->MCU_height = compptr->v_samp_factor;366compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;367compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;368/* Figure number of non-dummy blocks in last MCU column & row */369tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);370if (tmp == 0) tmp = compptr->MCU_width;371compptr->last_col_width = tmp;372tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);373if (tmp == 0) tmp = compptr->MCU_height;374compptr->last_row_height = tmp;375/* Prepare array describing MCU composition */376mcublks = compptr->MCU_blocks;377if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)378ERREXIT(cinfo, JERR_BAD_MCU_SIZE);379while (mcublks-- > 0) {380cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;381}382}383384}385386/* Convert restart specified in rows to actual MCU count. */387/* Note that count must fit in 16 bits, so we provide limiting. */388if (cinfo->restart_in_rows > 0) {389long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;390cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);391}392}393394395/*396* Per-pass setup.397* This is called at the beginning of each pass. We determine which modules398* will be active during this pass and give them appropriate start_pass calls.399* We also set is_last_pass to indicate whether any more passes will be400* required.401*/402403METHODDEF(void)404prepare_for_pass (j_compress_ptr cinfo)405{406my_master_ptr master = (my_master_ptr) cinfo->master;407408switch (master->pass_type) {409case main_pass:410/* Initial pass: will collect input data, and do either Huffman411* optimization or data output for the first scan.412*/413select_scan_parameters(cinfo);414per_scan_setup(cinfo);415if (! cinfo->raw_data_in) {416(*cinfo->cconvert->start_pass) (cinfo);417(*cinfo->downsample->start_pass) (cinfo);418(*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);419}420(*cinfo->fdct->start_pass) (cinfo);421(*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);422(*cinfo->coef->start_pass) (cinfo,423(master->total_passes > 1 ?424JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));425(*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);426if (cinfo->optimize_coding) {427/* No immediate data output; postpone writing frame/scan headers */428master->pub.call_pass_startup = FALSE;429} else {430/* Will write frame/scan headers at first jpeg_write_scanlines call */431master->pub.call_pass_startup = TRUE;432}433break;434#ifdef ENTROPY_OPT_SUPPORTED435case huff_opt_pass:436/* Do Huffman optimization for a scan after the first one. */437select_scan_parameters(cinfo);438per_scan_setup(cinfo);439if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {440(*cinfo->entropy->start_pass) (cinfo, TRUE);441(*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);442master->pub.call_pass_startup = FALSE;443break;444}445/* Special case: Huffman DC refinement scans need no Huffman table446* and therefore we can skip the optimization pass for them.447*/448master->pass_type = output_pass;449master->pass_number++;450/*FALLTHROUGH*/451#endif452case output_pass:453/* Do a data-output pass. */454/* We need not repeat per-scan setup if prior optimization pass did it. */455if (! cinfo->optimize_coding) {456select_scan_parameters(cinfo);457per_scan_setup(cinfo);458}459(*cinfo->entropy->start_pass) (cinfo, FALSE);460(*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);461/* We emit frame/scan headers now */462if (master->scan_number == 0)463(*cinfo->marker->write_frame_header) (cinfo);464(*cinfo->marker->write_scan_header) (cinfo);465master->pub.call_pass_startup = FALSE;466break;467default:468ERREXIT(cinfo, JERR_NOT_COMPILED);469}470471master->pub.is_last_pass = (master->pass_number == master->total_passes-1);472473/* Set up progress monitor's pass info if present */474if (cinfo->progress != NULL) {475cinfo->progress->completed_passes = master->pass_number;476cinfo->progress->total_passes = master->total_passes;477}478}479480481/*482* Special start-of-pass hook.483* This is called by jpeg_write_scanlines if call_pass_startup is TRUE.484* In single-pass processing, we need this hook because we don't want to485* write frame/scan headers during jpeg_start_compress; we want to let the486* application write COM markers etc. between jpeg_start_compress and the487* jpeg_write_scanlines loop.488* In multi-pass processing, this routine is not used.489*/490491METHODDEF(void)492pass_startup (j_compress_ptr cinfo)493{494cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */495496(*cinfo->marker->write_frame_header) (cinfo);497(*cinfo->marker->write_scan_header) (cinfo);498}499500501/*502* Finish up at end of pass.503*/504505METHODDEF(void)506finish_pass_master (j_compress_ptr cinfo)507{508my_master_ptr master = (my_master_ptr) cinfo->master;509510/* The entropy coder always needs an end-of-pass call,511* either to analyze statistics or to flush its output buffer.512*/513(*cinfo->entropy->finish_pass) (cinfo);514515/* Update state for next pass */516switch (master->pass_type) {517case main_pass:518/* next pass is either output of scan 0 (after optimization)519* or output of scan 1 (if no optimization).520*/521master->pass_type = output_pass;522if (! cinfo->optimize_coding)523master->scan_number++;524break;525case huff_opt_pass:526/* next pass is always output of current scan */527master->pass_type = output_pass;528break;529case output_pass:530/* next pass is either optimization or output of next scan */531if (cinfo->optimize_coding)532master->pass_type = huff_opt_pass;533master->scan_number++;534break;535}536537master->pass_number++;538}539540541/*542* Initialize master compression control.543*/544545GLOBAL(void)546jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)547{548my_master_ptr master;549550master = (my_master_ptr)551(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,552SIZEOF(my_comp_master));553cinfo->master = (struct jpeg_comp_master *) master;554master->pub.prepare_for_pass = prepare_for_pass;555master->pub.pass_startup = pass_startup;556master->pub.finish_pass = finish_pass_master;557master->pub.is_last_pass = FALSE;558559/* Validate parameters, determine derived values */560initial_setup(cinfo);561562if (cinfo->scan_info != NULL) {563#ifdef C_MULTISCAN_FILES_SUPPORTED564validate_script(cinfo);565#else566ERREXIT(cinfo, JERR_NOT_COMPILED);567#endif568} else {569cinfo->progressive_mode = FALSE;570cinfo->num_scans = 1;571}572573if (cinfo->progressive_mode) /* TEMPORARY HACK ??? */574cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */575576/* Initialize my private state */577if (transcode_only) {578/* no main pass in transcoding */579if (cinfo->optimize_coding)580master->pass_type = huff_opt_pass;581else582master->pass_type = output_pass;583} else {584/* for normal compression, first pass is always this type: */585master->pass_type = main_pass;586}587master->scan_number = 0;588master->pass_number = 0;589if (cinfo->optimize_coding)590master->total_passes = cinfo->num_scans * 2;591else592master->total_passes = cinfo->num_scans;593}594595596