Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/vncdll/winvnc/libjpeg/jcmaster.c
Views: 11784
/*1* jcmaster.c2*3* Copyright (C) 1991-1997, Thomas G. Lane.4* This file is part of the Independent JPEG Group's software.5* For conditions of distribution and use, see the accompanying README file.6*7* This file contains master control logic for the JPEG compressor.8* These routines are concerned with parameter validation, initial setup,9* and inter-pass control (determining the number of passes and the work10* to be done in each pass).11*/1213#define JPEG_INTERNALS14#include "jinclude.h"15#include "jpeglib.h"161718/* Private state */1920typedef enum {21main_pass, /* input data, also do first output step */22huff_opt_pass, /* Huffman code optimization pass */23output_pass /* data output pass */24} c_pass_type;2526typedef struct {27struct jpeg_comp_master pub; /* public fields */2829c_pass_type pass_type; /* the type of the current pass */3031int pass_number; /* # of passes completed */32int total_passes; /* total # of passes needed */3334int scan_number; /* current index in scan_info[] */35} my_comp_master;3637typedef my_comp_master * my_master_ptr;383940/*41* Support routines that do various essential calculations.42*/4344LOCAL(void)45initial_setup (j_compress_ptr cinfo)46/* Do computations that are needed before master selection phase */47{48int ci;49jpeg_component_info *compptr;50long samplesperrow;51JDIMENSION jd_samplesperrow;5253/* Sanity check on image dimensions */54if (cinfo->image_height <= 0 || cinfo->image_width <= 055|| cinfo->num_components <= 0 || cinfo->input_components <= 0)56ERREXIT(cinfo, JERR_EMPTY_IMAGE);5758/* Make sure image isn't bigger than I can handle */59if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||60(long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)61ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);6263/* Width of an input scanline must be representable as JDIMENSION. */64samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;65jd_samplesperrow = (JDIMENSION) samplesperrow;66if ((long) jd_samplesperrow != samplesperrow)67ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);6869/* For now, precision must match compiled-in value... */70if (cinfo->data_precision != BITS_IN_JSAMPLE)71ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);7273/* Check that number of components won't exceed internal array sizes */74if (cinfo->num_components > MAX_COMPONENTS)75ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,76MAX_COMPONENTS);7778/* Compute maximum sampling factors; check factor validity */79cinfo->max_h_samp_factor = 1;80cinfo->max_v_samp_factor = 1;81for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;82ci++, compptr++) {83if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||84compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)85ERREXIT(cinfo, JERR_BAD_SAMPLING);86cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,87compptr->h_samp_factor);88cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,89compptr->v_samp_factor);90}9192/* Compute dimensions of components */93for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;94ci++, compptr++) {95/* Fill in the correct component_index value; don't rely on application */96compptr->component_index = ci;97/* For compression, we never do DCT scaling. */98compptr->DCT_scaled_size = DCTSIZE;99/* Size in DCT blocks */100compptr->width_in_blocks = (JDIMENSION)101jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,102(long) (cinfo->max_h_samp_factor * DCTSIZE));103compptr->height_in_blocks = (JDIMENSION)104jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,105(long) (cinfo->max_v_samp_factor * DCTSIZE));106/* Size in samples */107compptr->downsampled_width = (JDIMENSION)108jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,109(long) cinfo->max_h_samp_factor);110compptr->downsampled_height = (JDIMENSION)111jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,112(long) cinfo->max_v_samp_factor);113/* Mark component needed (this flag isn't actually used for compression) */114compptr->component_needed = TRUE;115}116117/* Compute number of fully interleaved MCU rows (number of times that118* main controller will call coefficient controller).119*/120cinfo->total_iMCU_rows = (JDIMENSION)121jdiv_round_up((long) cinfo->image_height,122(long) (cinfo->max_v_samp_factor*DCTSIZE));123}124125126#ifdef C_MULTISCAN_FILES_SUPPORTED127128LOCAL(void)129validate_script (j_compress_ptr cinfo)130/* Verify that the scan script in cinfo->scan_info[] is valid; also131* determine whether it uses progressive JPEG, and set cinfo->progressive_mode.132*/133{134const jpeg_scan_info * scanptr;135int scanno, ncomps, ci, coefi, thisi;136int Ss, Se, Ah, Al;137boolean component_sent[MAX_COMPONENTS];138#ifdef C_PROGRESSIVE_SUPPORTED139int * last_bitpos_ptr;140int last_bitpos[MAX_COMPONENTS][DCTSIZE2];141/* -1 until that coefficient has been seen; then last Al for it */142#endif143144if (cinfo->num_scans <= 0)145ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);146147/* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;148* for progressive JPEG, no scan can have this.149*/150scanptr = cinfo->scan_info;151if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {152#ifdef C_PROGRESSIVE_SUPPORTED153cinfo->progressive_mode = TRUE;154last_bitpos_ptr = & last_bitpos[0][0];155for (ci = 0; ci < cinfo->num_components; ci++)156for (coefi = 0; coefi < DCTSIZE2; coefi++)157*last_bitpos_ptr++ = -1;158#else159ERREXIT(cinfo, JERR_NOT_COMPILED);160#endif161} else {162cinfo->progressive_mode = FALSE;163for (ci = 0; ci < cinfo->num_components; ci++)164component_sent[ci] = FALSE;165}166167for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {168/* Validate component indexes */169ncomps = scanptr->comps_in_scan;170if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)171ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);172for (ci = 0; ci < ncomps; ci++) {173thisi = scanptr->component_index[ci];174if (thisi < 0 || thisi >= cinfo->num_components)175ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);176/* Components must appear in SOF order within each scan */177if (ci > 0 && thisi <= scanptr->component_index[ci-1])178ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);179}180/* Validate progression parameters */181Ss = scanptr->Ss;182Se = scanptr->Se;183Ah = scanptr->Ah;184Al = scanptr->Al;185if (cinfo->progressive_mode) {186#ifdef C_PROGRESSIVE_SUPPORTED187/* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that188* seems wrong: the upper bound ought to depend on data precision.189* Perhaps they really meant 0..N+1 for N-bit precision.190* Here we allow 0..10 for 8-bit data; Al larger than 10 results in191* out-of-range reconstructed DC values during the first DC scan,192* which might cause problems for some decoders.193*/194#if BITS_IN_JSAMPLE == 8195#define MAX_AH_AL 10196#else197#define MAX_AH_AL 13198#endif199if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||200Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)201ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);202if (Ss == 0) {203if (Se != 0) /* DC and AC together not OK */204ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);205} else {206if (ncomps != 1) /* AC scans must be for only one component */207ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);208}209for (ci = 0; ci < ncomps; ci++) {210last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];211if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */212ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);213for (coefi = Ss; coefi <= Se; coefi++) {214if (last_bitpos_ptr[coefi] < 0) {215/* first scan of this coefficient */216if (Ah != 0)217ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);218} else {219/* not first scan */220if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)221ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);222}223last_bitpos_ptr[coefi] = Al;224}225}226#endif227} else {228/* For sequential JPEG, all progression parameters must be these: */229if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)230ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);231/* Make sure components are not sent twice */232for (ci = 0; ci < ncomps; ci++) {233thisi = scanptr->component_index[ci];234if (component_sent[thisi])235ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);236component_sent[thisi] = TRUE;237}238}239}240241/* Now verify that everything got sent. */242if (cinfo->progressive_mode) {243#ifdef C_PROGRESSIVE_SUPPORTED244/* For progressive mode, we only check that at least some DC data245* got sent for each component; the spec does not require that all bits246* of all coefficients be transmitted. Would it be wiser to enforce247* transmission of all coefficient bits??248*/249for (ci = 0; ci < cinfo->num_components; ci++) {250if (last_bitpos[ci][0] < 0)251ERREXIT(cinfo, JERR_MISSING_DATA);252}253#endif254} else {255for (ci = 0; ci < cinfo->num_components; ci++) {256if (! component_sent[ci])257ERREXIT(cinfo, JERR_MISSING_DATA);258}259}260}261262#endif /* C_MULTISCAN_FILES_SUPPORTED */263264265LOCAL(void)266select_scan_parameters (j_compress_ptr cinfo)267/* Set up the scan parameters for the current scan */268{269int ci;270271#ifdef C_MULTISCAN_FILES_SUPPORTED272if (cinfo->scan_info != NULL) {273/* Prepare for current scan --- the script is already validated */274my_master_ptr master = (my_master_ptr) cinfo->master;275const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;276277cinfo->comps_in_scan = scanptr->comps_in_scan;278for (ci = 0; ci < scanptr->comps_in_scan; ci++) {279cinfo->cur_comp_info[ci] =280&cinfo->comp_info[scanptr->component_index[ci]];281}282cinfo->Ss = scanptr->Ss;283cinfo->Se = scanptr->Se;284cinfo->Ah = scanptr->Ah;285cinfo->Al = scanptr->Al;286}287else288#endif289{290/* Prepare for single sequential-JPEG scan containing all components */291if (cinfo->num_components > MAX_COMPS_IN_SCAN)292ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,293MAX_COMPS_IN_SCAN);294cinfo->comps_in_scan = cinfo->num_components;295for (ci = 0; ci < cinfo->num_components; ci++) {296cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];297}298cinfo->Ss = 0;299cinfo->Se = DCTSIZE2-1;300cinfo->Ah = 0;301cinfo->Al = 0;302}303}304305306LOCAL(void)307per_scan_setup (j_compress_ptr cinfo)308/* Do computations that are needed before processing a JPEG scan */309/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */310{311int ci, mcublks, tmp;312jpeg_component_info *compptr;313314if (cinfo->comps_in_scan == 1) {315316/* Noninterleaved (single-component) scan */317compptr = cinfo->cur_comp_info[0];318319/* Overall image size in MCUs */320cinfo->MCUs_per_row = compptr->width_in_blocks;321cinfo->MCU_rows_in_scan = compptr->height_in_blocks;322323/* For noninterleaved scan, always one block per MCU */324compptr->MCU_width = 1;325compptr->MCU_height = 1;326compptr->MCU_blocks = 1;327compptr->MCU_sample_width = DCTSIZE;328compptr->last_col_width = 1;329/* For noninterleaved scans, it is convenient to define last_row_height330* as the number of block rows present in the last iMCU row.331*/332tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);333if (tmp == 0) tmp = compptr->v_samp_factor;334compptr->last_row_height = tmp;335336/* Prepare array describing MCU composition */337cinfo->blocks_in_MCU = 1;338cinfo->MCU_membership[0] = 0;339340} else {341342/* Interleaved (multi-component) scan */343if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)344ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,345MAX_COMPS_IN_SCAN);346347/* Overall image size in MCUs */348cinfo->MCUs_per_row = (JDIMENSION)349jdiv_round_up((long) cinfo->image_width,350(long) (cinfo->max_h_samp_factor*DCTSIZE));351cinfo->MCU_rows_in_scan = (JDIMENSION)352jdiv_round_up((long) cinfo->image_height,353(long) (cinfo->max_v_samp_factor*DCTSIZE));354355cinfo->blocks_in_MCU = 0;356357for (ci = 0; ci < cinfo->comps_in_scan; ci++) {358compptr = cinfo->cur_comp_info[ci];359/* Sampling factors give # of blocks of component in each MCU */360compptr->MCU_width = compptr->h_samp_factor;361compptr->MCU_height = compptr->v_samp_factor;362compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;363compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;364/* Figure number of non-dummy blocks in last MCU column & row */365tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);366if (tmp == 0) tmp = compptr->MCU_width;367compptr->last_col_width = tmp;368tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);369if (tmp == 0) tmp = compptr->MCU_height;370compptr->last_row_height = tmp;371/* Prepare array describing MCU composition */372mcublks = compptr->MCU_blocks;373if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)374ERREXIT(cinfo, JERR_BAD_MCU_SIZE);375while (mcublks-- > 0) {376cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;377}378}379380}381382/* Convert restart specified in rows to actual MCU count. */383/* Note that count must fit in 16 bits, so we provide limiting. */384if (cinfo->restart_in_rows > 0) {385long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;386cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);387}388}389390391/*392* Per-pass setup.393* This is called at the beginning of each pass. We determine which modules394* will be active during this pass and give them appropriate start_pass calls.395* We also set is_last_pass to indicate whether any more passes will be396* required.397*/398399METHODDEF(void)400prepare_for_pass (j_compress_ptr cinfo)401{402my_master_ptr master = (my_master_ptr) cinfo->master;403404switch (master->pass_type) {405case main_pass:406/* Initial pass: will collect input data, and do either Huffman407* optimization or data output for the first scan.408*/409select_scan_parameters(cinfo);410per_scan_setup(cinfo);411if (! cinfo->raw_data_in) {412(*cinfo->cconvert->start_pass) (cinfo);413(*cinfo->downsample->start_pass) (cinfo);414(*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);415}416(*cinfo->fdct->start_pass) (cinfo);417(*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);418(*cinfo->coef->start_pass) (cinfo,419(master->total_passes > 1 ?420JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));421(*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);422if (cinfo->optimize_coding) {423/* No immediate data output; postpone writing frame/scan headers */424master->pub.call_pass_startup = FALSE;425} else {426/* Will write frame/scan headers at first jpeg_write_scanlines call */427master->pub.call_pass_startup = TRUE;428}429break;430#ifdef ENTROPY_OPT_SUPPORTED431case huff_opt_pass:432/* Do Huffman optimization for a scan after the first one. */433select_scan_parameters(cinfo);434per_scan_setup(cinfo);435if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {436(*cinfo->entropy->start_pass) (cinfo, TRUE);437(*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);438master->pub.call_pass_startup = FALSE;439break;440}441/* Special case: Huffman DC refinement scans need no Huffman table442* and therefore we can skip the optimization pass for them.443*/444master->pass_type = output_pass;445master->pass_number++;446/*FALLTHROUGH*/447#endif448case output_pass:449/* Do a data-output pass. */450/* We need not repeat per-scan setup if prior optimization pass did it. */451if (! cinfo->optimize_coding) {452select_scan_parameters(cinfo);453per_scan_setup(cinfo);454}455(*cinfo->entropy->start_pass) (cinfo, FALSE);456(*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);457/* We emit frame/scan headers now */458if (master->scan_number == 0)459(*cinfo->marker->write_frame_header) (cinfo);460(*cinfo->marker->write_scan_header) (cinfo);461master->pub.call_pass_startup = FALSE;462break;463default:464ERREXIT(cinfo, JERR_NOT_COMPILED);465}466467master->pub.is_last_pass = (master->pass_number == master->total_passes-1);468469/* Set up progress monitor's pass info if present */470if (cinfo->progress != NULL) {471cinfo->progress->completed_passes = master->pass_number;472cinfo->progress->total_passes = master->total_passes;473}474}475476477/*478* Special start-of-pass hook.479* This is called by jpeg_write_scanlines if call_pass_startup is TRUE.480* In single-pass processing, we need this hook because we don't want to481* write frame/scan headers during jpeg_start_compress; we want to let the482* application write COM markers etc. between jpeg_start_compress and the483* jpeg_write_scanlines loop.484* In multi-pass processing, this routine is not used.485*/486487METHODDEF(void)488pass_startup (j_compress_ptr cinfo)489{490cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */491492(*cinfo->marker->write_frame_header) (cinfo);493(*cinfo->marker->write_scan_header) (cinfo);494}495496497/*498* Finish up at end of pass.499*/500501METHODDEF(void)502finish_pass_master (j_compress_ptr cinfo)503{504my_master_ptr master = (my_master_ptr) cinfo->master;505506/* The entropy coder always needs an end-of-pass call,507* either to analyze statistics or to flush its output buffer.508*/509(*cinfo->entropy->finish_pass) (cinfo);510511/* Update state for next pass */512switch (master->pass_type) {513case main_pass:514/* next pass is either output of scan 0 (after optimization)515* or output of scan 1 (if no optimization).516*/517master->pass_type = output_pass;518if (! cinfo->optimize_coding)519master->scan_number++;520break;521case huff_opt_pass:522/* next pass is always output of current scan */523master->pass_type = output_pass;524break;525case output_pass:526/* next pass is either optimization or output of next scan */527if (cinfo->optimize_coding)528master->pass_type = huff_opt_pass;529master->scan_number++;530break;531}532533master->pass_number++;534}535536537/*538* Initialize master compression control.539*/540541GLOBAL(void)542jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)543{544my_master_ptr master;545546master = (my_master_ptr)547(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,548SIZEOF(my_comp_master));549cinfo->master = (struct jpeg_comp_master *) master;550master->pub.prepare_for_pass = prepare_for_pass;551master->pub.pass_startup = pass_startup;552master->pub.finish_pass = finish_pass_master;553master->pub.is_last_pass = FALSE;554555/* Validate parameters, determine derived values */556initial_setup(cinfo);557558if (cinfo->scan_info != NULL) {559#ifdef C_MULTISCAN_FILES_SUPPORTED560validate_script(cinfo);561#else562ERREXIT(cinfo, JERR_NOT_COMPILED);563#endif564} else {565cinfo->progressive_mode = FALSE;566cinfo->num_scans = 1;567}568569if (cinfo->progressive_mode) /* TEMPORARY HACK ??? */570cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */571572/* Initialize my private state */573if (transcode_only) {574/* no main pass in transcoding */575if (cinfo->optimize_coding)576master->pass_type = huff_opt_pass;577else578master->pass_type = output_pass;579} else {580/* for normal compression, first pass is always this type: */581master->pass_type = main_pass;582}583master->scan_number = 0;584master->pass_number = 0;585if (cinfo->optimize_coding)586master->total_passes = cinfo->num_scans * 2;587else588master->total_passes = cinfo->num_scans;589}590591592