Path: blob/master/thirdparty/libjpeg-turbo/src/jcmaster.c
9904 views
/*1* jcmaster.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1991-1997, Thomas G. Lane.5* Modified 2003-2010 by Guido Vollbeding.6* Lossless JPEG Modifications:7* Copyright (C) 1999, Ken Murchison.8* libjpeg-turbo Modifications:9* Copyright (C) 2010, 2016, 2018, 2022-2024, D. R. Commander.10* For conditions of distribution and use, see the accompanying README.ijg11* file.12*13* This file contains master control logic for the JPEG compressor.14* These routines are concerned with parameter validation, initial setup,15* and inter-pass control (determining the number of passes and the work16* to be done in each pass).17*/1819#define JPEG_INTERNALS20#include "jinclude.h"21#include "jpeglib.h"22#include "jpegapicomp.h"23#include "jcmaster.h"242526/*27* Support routines that do various essential calculations.28*/2930#if JPEG_LIB_VERSION >= 7031/*32* Compute JPEG image dimensions and related values.33* NOTE: this is exported for possible use by application.34* Hence it mustn't do anything that can't be done twice.35*/3637GLOBAL(void)38jpeg_calc_jpeg_dimensions(j_compress_ptr cinfo)39/* Do computations that are needed before master selection phase */40{41int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;4243/* Hardwire it to "no scaling" */44cinfo->jpeg_width = cinfo->image_width;45cinfo->jpeg_height = cinfo->image_height;46cinfo->min_DCT_h_scaled_size = data_unit;47cinfo->min_DCT_v_scaled_size = data_unit;48}49#endif505152LOCAL(boolean)53using_std_huff_tables(j_compress_ptr cinfo)54{55int i;5657static const UINT8 bits_dc_luminance[17] = {58/* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 059};60static const UINT8 val_dc_luminance[] = {610, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1162};6364static const UINT8 bits_dc_chrominance[17] = {65/* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 066};67static const UINT8 val_dc_chrominance[] = {680, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1169};7071static const UINT8 bits_ac_luminance[17] = {72/* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d73};74static const UINT8 val_ac_luminance[] = {750x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,760x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,770x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,780x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,790x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,800x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,810x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,820x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,830x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,840x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,850x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,860x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,870x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,880x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,890xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,900xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,910xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,920xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,930xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,940xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,950xf9, 0xfa96};9798static const UINT8 bits_ac_chrominance[17] = {99/* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77100};101static const UINT8 val_ac_chrominance[] = {1020x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,1030x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,1040x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,1050xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,1060x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,1070xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,1080x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,1090x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,1100x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,1110x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,1120x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,1130x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,1140x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,1150x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,1160xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,1170xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,1180xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,1190xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,1200xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,1210xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,1220xf9, 0xfa123};124125if (cinfo->dc_huff_tbl_ptrs[0] == NULL ||126cinfo->ac_huff_tbl_ptrs[0] == NULL ||127cinfo->dc_huff_tbl_ptrs[1] == NULL ||128cinfo->ac_huff_tbl_ptrs[1] == NULL)129return FALSE;130131for (i = 2; i < NUM_HUFF_TBLS; i++) {132if (cinfo->dc_huff_tbl_ptrs[i] != NULL ||133cinfo->ac_huff_tbl_ptrs[i] != NULL)134return FALSE;135}136137if (memcmp(cinfo->dc_huff_tbl_ptrs[0]->bits, bits_dc_luminance,138sizeof(bits_dc_luminance)) ||139memcmp(cinfo->dc_huff_tbl_ptrs[0]->huffval, val_dc_luminance,140sizeof(val_dc_luminance)) ||141memcmp(cinfo->ac_huff_tbl_ptrs[0]->bits, bits_ac_luminance,142sizeof(bits_ac_luminance)) ||143memcmp(cinfo->ac_huff_tbl_ptrs[0]->huffval, val_ac_luminance,144sizeof(val_ac_luminance)) ||145memcmp(cinfo->dc_huff_tbl_ptrs[1]->bits, bits_dc_chrominance,146sizeof(bits_dc_chrominance)) ||147memcmp(cinfo->dc_huff_tbl_ptrs[1]->huffval, val_dc_chrominance,148sizeof(val_dc_chrominance)) ||149memcmp(cinfo->ac_huff_tbl_ptrs[1]->bits, bits_ac_chrominance,150sizeof(bits_ac_chrominance)) ||151memcmp(cinfo->ac_huff_tbl_ptrs[1]->huffval, val_ac_chrominance,152sizeof(val_ac_chrominance)))153return FALSE;154155return TRUE;156}157158159LOCAL(void)160initial_setup(j_compress_ptr cinfo, boolean transcode_only)161/* Do computations that are needed before master selection phase */162{163int ci;164jpeg_component_info *compptr;165long samplesperrow;166JDIMENSION jd_samplesperrow;167int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;168169#if JPEG_LIB_VERSION >= 70170#if JPEG_LIB_VERSION >= 80171if (!transcode_only)172#endif173jpeg_calc_jpeg_dimensions(cinfo);174#endif175176/* Sanity check on image dimensions */177if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0 ||178cinfo->num_components <= 0 || cinfo->input_components <= 0)179ERREXIT(cinfo, JERR_EMPTY_IMAGE);180181/* Make sure image isn't bigger than I can handle */182if ((long)cinfo->_jpeg_height > (long)JPEG_MAX_DIMENSION ||183(long)cinfo->_jpeg_width > (long)JPEG_MAX_DIMENSION)184ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int)JPEG_MAX_DIMENSION);185186/* Width of an input scanline must be representable as JDIMENSION. */187samplesperrow = (long)cinfo->image_width * (long)cinfo->input_components;188jd_samplesperrow = (JDIMENSION)samplesperrow;189if ((long)jd_samplesperrow != samplesperrow)190ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);191192/* Lossy JPEG images must have 8 or 12 bits per sample. Lossless JPEG images193* can have 2 to 16 bits per sample.194*/195#ifdef C_LOSSLESS_SUPPORTED196if (cinfo->master->lossless) {197if (cinfo->data_precision < 2 || cinfo->data_precision > 16)198ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);199} else200#endif201{202if (cinfo->data_precision != 8 && cinfo->data_precision != 12)203ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);204}205206/* Check that number of components won't exceed internal array sizes */207if (cinfo->num_components > MAX_COMPONENTS)208ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,209MAX_COMPONENTS);210211/* Compute maximum sampling factors; check factor validity */212cinfo->max_h_samp_factor = 1;213cinfo->max_v_samp_factor = 1;214for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;215ci++, compptr++) {216if (compptr->h_samp_factor <= 0 ||217compptr->h_samp_factor > MAX_SAMP_FACTOR ||218compptr->v_samp_factor <= 0 ||219compptr->v_samp_factor > MAX_SAMP_FACTOR)220ERREXIT(cinfo, JERR_BAD_SAMPLING);221cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,222compptr->h_samp_factor);223cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,224compptr->v_samp_factor);225}226227/* Compute dimensions of components */228for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;229ci++, compptr++) {230/* Fill in the correct component_index value; don't rely on application */231compptr->component_index = ci;232/* For compression, we never do DCT scaling. */233#if JPEG_LIB_VERSION >= 70234compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = data_unit;235#else236compptr->DCT_scaled_size = data_unit;237#endif238/* Size in data units */239compptr->width_in_blocks = (JDIMENSION)240jdiv_round_up((long)cinfo->_jpeg_width * (long)compptr->h_samp_factor,241(long)(cinfo->max_h_samp_factor * data_unit));242compptr->height_in_blocks = (JDIMENSION)243jdiv_round_up((long)cinfo->_jpeg_height * (long)compptr->v_samp_factor,244(long)(cinfo->max_v_samp_factor * data_unit));245/* Size in samples */246compptr->downsampled_width = (JDIMENSION)247jdiv_round_up((long)cinfo->_jpeg_width * (long)compptr->h_samp_factor,248(long)cinfo->max_h_samp_factor);249compptr->downsampled_height = (JDIMENSION)250jdiv_round_up((long)cinfo->_jpeg_height * (long)compptr->v_samp_factor,251(long)cinfo->max_v_samp_factor);252/* Mark component needed (this flag isn't actually used for compression) */253compptr->component_needed = TRUE;254}255256/* Compute number of fully interleaved MCU rows (number of times that257* main controller will call coefficient or difference controller).258*/259cinfo->total_iMCU_rows = (JDIMENSION)260jdiv_round_up((long)cinfo->_jpeg_height,261(long)(cinfo->max_v_samp_factor * data_unit));262}263264265#if defined(C_MULTISCAN_FILES_SUPPORTED) || defined(C_LOSSLESS_SUPPORTED)266#define NEED_SCAN_SCRIPT267#endif268269#ifdef NEED_SCAN_SCRIPT270271LOCAL(void)272validate_script(j_compress_ptr cinfo)273/* Verify that the scan script in cinfo->scan_info[] is valid; also274* determine whether it uses progressive JPEG, and set cinfo->progressive_mode.275*/276{277const jpeg_scan_info *scanptr;278int scanno, ncomps, ci, coefi, thisi;279int Ss, Se, Ah, Al;280boolean component_sent[MAX_COMPONENTS];281#ifdef C_PROGRESSIVE_SUPPORTED282int *last_bitpos_ptr;283int last_bitpos[MAX_COMPONENTS][DCTSIZE2];284/* -1 until that coefficient has been seen; then last Al for it */285#endif286287if (cinfo->num_scans <= 0)288ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);289290#ifndef C_MULTISCAN_FILES_SUPPORTED291if (cinfo->num_scans > 1)292ERREXIT(cinfo, JERR_NOT_COMPILED);293#endif294295scanptr = cinfo->scan_info;296if (scanptr->Ss != 0 && scanptr->Se == 0) {297#ifdef C_LOSSLESS_SUPPORTED298cinfo->master->lossless = TRUE;299cinfo->progressive_mode = FALSE;300for (ci = 0; ci < cinfo->num_components; ci++)301component_sent[ci] = FALSE;302#else303ERREXIT(cinfo, JERR_NOT_COMPILED);304#endif305}306/* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;307* for progressive JPEG, no scan can have this.308*/309else if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2 - 1) {310#ifdef C_PROGRESSIVE_SUPPORTED311cinfo->progressive_mode = TRUE;312cinfo->master->lossless = FALSE;313last_bitpos_ptr = &last_bitpos[0][0];314for (ci = 0; ci < cinfo->num_components; ci++)315for (coefi = 0; coefi < DCTSIZE2; coefi++)316*last_bitpos_ptr++ = -1;317#else318ERREXIT(cinfo, JERR_NOT_COMPILED);319#endif320} else {321cinfo->progressive_mode = cinfo->master->lossless = FALSE;322for (ci = 0; ci < cinfo->num_components; ci++)323component_sent[ci] = FALSE;324}325326for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {327/* Validate component indexes */328ncomps = scanptr->comps_in_scan;329if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)330ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);331for (ci = 0; ci < ncomps; ci++) {332thisi = scanptr->component_index[ci];333if (thisi < 0 || thisi >= cinfo->num_components)334ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);335/* Components must appear in SOF order within each scan */336if (ci > 0 && thisi <= scanptr->component_index[ci - 1])337ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);338}339/* Validate progression parameters */340Ss = scanptr->Ss;341Se = scanptr->Se;342Ah = scanptr->Ah;343Al = scanptr->Al;344if (cinfo->progressive_mode) {345#ifdef C_PROGRESSIVE_SUPPORTED346/* Rec. ITU-T T.81 | ISO/IEC 10918-1 simply gives the ranges 0..13 for Ah347* and Al, but that seems wrong: the upper bound ought to depend on data348* precision. Perhaps they really meant 0..N+1 for N-bit precision.349* Here we allow 0..10 for 8-bit data; Al larger than 10 results in350* out-of-range reconstructed DC values during the first DC scan,351* which might cause problems for some decoders.352*/353int max_Ah_Al = cinfo->data_precision == 12 ? 13 : 10;354355if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||356Ah < 0 || Ah > max_Ah_Al || Al < 0 || Al > max_Ah_Al)357ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);358if (Ss == 0) {359if (Se != 0) /* DC and AC together not OK */360ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);361} else {362if (ncomps != 1) /* AC scans must be for only one component */363ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);364}365for (ci = 0; ci < ncomps; ci++) {366last_bitpos_ptr = &last_bitpos[scanptr->component_index[ci]][0];367if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */368ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);369for (coefi = Ss; coefi <= Se; coefi++) {370if (last_bitpos_ptr[coefi] < 0) {371/* first scan of this coefficient */372if (Ah != 0)373ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);374} else {375/* not first scan */376if (Ah != last_bitpos_ptr[coefi] || Al != Ah - 1)377ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);378}379last_bitpos_ptr[coefi] = Al;380}381}382#endif383} else {384#ifdef C_LOSSLESS_SUPPORTED385if (cinfo->master->lossless) {386/* The JPEG spec simply gives the range 0..15 for Al (Pt), but that387* seems wrong: the upper bound ought to depend on data precision.388* Perhaps they really meant 0..N-1 for N-bit precision, which is what389* we allow here. Values greater than or equal to the data precision390* will result in a blank image.391*/392if (Ss < 1 || Ss > 7 || /* predictor selection value */393Se != 0 || Ah != 0 ||394Al < 0 || Al >= cinfo->data_precision) /* point transform */395ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);396} else397#endif398{399/* For sequential JPEG, all progression parameters must be these: */400if (Ss != 0 || Se != DCTSIZE2 - 1 || Ah != 0 || Al != 0)401ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);402}403/* Make sure components are not sent twice */404for (ci = 0; ci < ncomps; ci++) {405thisi = scanptr->component_index[ci];406if (component_sent[thisi])407ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);408component_sent[thisi] = TRUE;409}410}411}412413/* Now verify that everything got sent. */414if (cinfo->progressive_mode) {415#ifdef C_PROGRESSIVE_SUPPORTED416/* For progressive mode, we only check that at least some DC data417* got sent for each component; the spec does not require that all bits418* of all coefficients be transmitted. Would it be wiser to enforce419* transmission of all coefficient bits??420*/421for (ci = 0; ci < cinfo->num_components; ci++) {422if (last_bitpos[ci][0] < 0)423ERREXIT(cinfo, JERR_MISSING_DATA);424}425#endif426} else {427for (ci = 0; ci < cinfo->num_components; ci++) {428if (!component_sent[ci])429ERREXIT(cinfo, JERR_MISSING_DATA);430}431}432}433434#endif /* NEED_SCAN_SCRIPT */435436437LOCAL(void)438select_scan_parameters(j_compress_ptr cinfo)439/* Set up the scan parameters for the current scan */440{441int ci;442443#ifdef NEED_SCAN_SCRIPT444if (cinfo->scan_info != NULL) {445/* Prepare for current scan --- the script is already validated */446my_master_ptr master = (my_master_ptr)cinfo->master;447const jpeg_scan_info *scanptr = cinfo->scan_info + master->scan_number;448449cinfo->comps_in_scan = scanptr->comps_in_scan;450for (ci = 0; ci < scanptr->comps_in_scan; ci++) {451cinfo->cur_comp_info[ci] =452&cinfo->comp_info[scanptr->component_index[ci]];453}454cinfo->Ss = scanptr->Ss;455cinfo->Se = scanptr->Se;456cinfo->Ah = scanptr->Ah;457cinfo->Al = scanptr->Al;458} else459#endif460{461/* Prepare for single sequential-JPEG scan containing all components */462if (cinfo->num_components > MAX_COMPS_IN_SCAN)463ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,464MAX_COMPS_IN_SCAN);465cinfo->comps_in_scan = cinfo->num_components;466for (ci = 0; ci < cinfo->num_components; ci++) {467cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];468}469if (!cinfo->master->lossless) {470cinfo->Ss = 0;471cinfo->Se = DCTSIZE2 - 1;472cinfo->Ah = 0;473cinfo->Al = 0;474}475}476}477478479LOCAL(void)480per_scan_setup(j_compress_ptr cinfo)481/* Do computations that are needed before processing a JPEG scan */482/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */483{484int ci, mcublks, tmp;485jpeg_component_info *compptr;486int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;487488if (cinfo->comps_in_scan == 1) {489490/* Noninterleaved (single-component) scan */491compptr = cinfo->cur_comp_info[0];492493/* Overall image size in MCUs */494cinfo->MCUs_per_row = compptr->width_in_blocks;495cinfo->MCU_rows_in_scan = compptr->height_in_blocks;496497/* For noninterleaved scan, always one block per MCU */498compptr->MCU_width = 1;499compptr->MCU_height = 1;500compptr->MCU_blocks = 1;501compptr->MCU_sample_width = data_unit;502compptr->last_col_width = 1;503/* For noninterleaved scans, it is convenient to define last_row_height504* as the number of block rows present in the last iMCU row.505*/506tmp = (int)(compptr->height_in_blocks % compptr->v_samp_factor);507if (tmp == 0) tmp = compptr->v_samp_factor;508compptr->last_row_height = tmp;509510/* Prepare array describing MCU composition */511cinfo->blocks_in_MCU = 1;512cinfo->MCU_membership[0] = 0;513514} else {515516/* Interleaved (multi-component) scan */517if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)518ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,519MAX_COMPS_IN_SCAN);520521/* Overall image size in MCUs */522cinfo->MCUs_per_row = (JDIMENSION)523jdiv_round_up((long)cinfo->_jpeg_width,524(long)(cinfo->max_h_samp_factor * data_unit));525cinfo->MCU_rows_in_scan = (JDIMENSION)526jdiv_round_up((long)cinfo->_jpeg_height,527(long)(cinfo->max_v_samp_factor * data_unit));528529cinfo->blocks_in_MCU = 0;530531for (ci = 0; ci < cinfo->comps_in_scan; ci++) {532compptr = cinfo->cur_comp_info[ci];533/* Sampling factors give # of blocks of component in each MCU */534compptr->MCU_width = compptr->h_samp_factor;535compptr->MCU_height = compptr->v_samp_factor;536compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;537compptr->MCU_sample_width = compptr->MCU_width * data_unit;538/* Figure number of non-dummy blocks in last MCU column & row */539tmp = (int)(compptr->width_in_blocks % compptr->MCU_width);540if (tmp == 0) tmp = compptr->MCU_width;541compptr->last_col_width = tmp;542tmp = (int)(compptr->height_in_blocks % compptr->MCU_height);543if (tmp == 0) tmp = compptr->MCU_height;544compptr->last_row_height = tmp;545/* Prepare array describing MCU composition */546mcublks = compptr->MCU_blocks;547if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)548ERREXIT(cinfo, JERR_BAD_MCU_SIZE);549while (mcublks-- > 0) {550cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;551}552}553554}555556/* Convert restart specified in rows to actual MCU count. */557/* Note that count must fit in 16 bits, so we provide limiting. */558if (cinfo->restart_in_rows > 0) {559long nominal = (long)cinfo->restart_in_rows * (long)cinfo->MCUs_per_row;560cinfo->restart_interval = (unsigned int)MIN(nominal, 65535L);561}562}563564565/*566* Per-pass setup.567* This is called at the beginning of each pass. We determine which modules568* will be active during this pass and give them appropriate start_pass calls.569* We also set is_last_pass to indicate whether any more passes will be570* required.571*/572573METHODDEF(void)574prepare_for_pass(j_compress_ptr cinfo)575{576my_master_ptr master = (my_master_ptr)cinfo->master;577578switch (master->pass_type) {579case main_pass:580/* Initial pass: will collect input data, and do either Huffman581* optimization or data output for the first scan.582*/583select_scan_parameters(cinfo);584per_scan_setup(cinfo);585if (!cinfo->raw_data_in) {586(*cinfo->cconvert->start_pass) (cinfo);587(*cinfo->downsample->start_pass) (cinfo);588(*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);589}590(*cinfo->fdct->start_pass) (cinfo);591(*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);592(*cinfo->coef->start_pass) (cinfo,593(master->total_passes > 1 ?594JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));595(*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);596if (cinfo->optimize_coding) {597/* No immediate data output; postpone writing frame/scan headers */598master->pub.call_pass_startup = FALSE;599} else {600/* Will write frame/scan headers at first jpeg_write_scanlines call */601master->pub.call_pass_startup = TRUE;602}603break;604#ifdef ENTROPY_OPT_SUPPORTED605case huff_opt_pass:606/* Do Huffman optimization for a scan after the first one. */607select_scan_parameters(cinfo);608per_scan_setup(cinfo);609if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code ||610cinfo->master->lossless) {611(*cinfo->entropy->start_pass) (cinfo, TRUE);612(*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);613master->pub.call_pass_startup = FALSE;614break;615}616/* Special case: Huffman DC refinement scans need no Huffman table617* and therefore we can skip the optimization pass for them.618*/619master->pass_type = output_pass;620master->pass_number++;621#endif622FALLTHROUGH /*FALLTHROUGH*/623case output_pass:624/* Do a data-output pass. */625/* We need not repeat per-scan setup if prior optimization pass did it. */626if (!cinfo->optimize_coding) {627select_scan_parameters(cinfo);628per_scan_setup(cinfo);629}630(*cinfo->entropy->start_pass) (cinfo, FALSE);631(*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);632/* We emit frame/scan headers now */633if (master->scan_number == 0)634(*cinfo->marker->write_frame_header) (cinfo);635(*cinfo->marker->write_scan_header) (cinfo);636master->pub.call_pass_startup = FALSE;637break;638default:639ERREXIT(cinfo, JERR_NOT_COMPILED);640}641642master->pub.is_last_pass = (master->pass_number == master->total_passes - 1);643644/* Set up progress monitor's pass info if present */645if (cinfo->progress != NULL) {646cinfo->progress->completed_passes = master->pass_number;647cinfo->progress->total_passes = master->total_passes;648}649}650651652/*653* Special start-of-pass hook.654* This is called by jpeg_write_scanlines if call_pass_startup is TRUE.655* In single-pass processing, we need this hook because we don't want to656* write frame/scan headers during jpeg_start_compress; we want to let the657* application write COM markers etc. between jpeg_start_compress and the658* jpeg_write_scanlines loop.659* In multi-pass processing, this routine is not used.660*/661662METHODDEF(void)663pass_startup(j_compress_ptr cinfo)664{665cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */666667(*cinfo->marker->write_frame_header) (cinfo);668(*cinfo->marker->write_scan_header) (cinfo);669}670671672/*673* Finish up at end of pass.674*/675676METHODDEF(void)677finish_pass_master(j_compress_ptr cinfo)678{679my_master_ptr master = (my_master_ptr)cinfo->master;680681/* The entropy coder always needs an end-of-pass call,682* either to analyze statistics or to flush its output buffer.683*/684(*cinfo->entropy->finish_pass) (cinfo);685686/* Update state for next pass */687switch (master->pass_type) {688case main_pass:689/* next pass is either output of scan 0 (after optimization)690* or output of scan 1 (if no optimization).691*/692master->pass_type = output_pass;693if (!cinfo->optimize_coding)694master->scan_number++;695break;696case huff_opt_pass:697/* next pass is always output of current scan */698master->pass_type = output_pass;699break;700case output_pass:701/* next pass is either optimization or output of next scan */702if (cinfo->optimize_coding)703master->pass_type = huff_opt_pass;704master->scan_number++;705break;706}707708master->pass_number++;709}710711712/*713* Initialize master compression control.714*/715716GLOBAL(void)717jinit_c_master_control(j_compress_ptr cinfo, boolean transcode_only)718{719my_master_ptr master = (my_master_ptr)cinfo->master;720boolean empty_huff_tables = TRUE;721int i;722723master->pub.prepare_for_pass = prepare_for_pass;724master->pub.pass_startup = pass_startup;725master->pub.finish_pass = finish_pass_master;726master->pub.is_last_pass = FALSE;727728if (cinfo->scan_info != NULL) {729#ifdef NEED_SCAN_SCRIPT730validate_script(cinfo);731#else732ERREXIT(cinfo, JERR_NOT_COMPILED);733#endif734} else {735cinfo->progressive_mode = FALSE;736cinfo->num_scans = 1;737}738739#ifdef C_LOSSLESS_SUPPORTED740/* Disable smoothing and subsampling in lossless mode, since those are lossy741* algorithms. Set the JPEG colorspace to the input colorspace. Disable raw742* (downsampled) data input, because it isn't particularly useful without743* subsampling and has not been tested in lossless mode.744*/745if (cinfo->master->lossless) {746int ci;747jpeg_component_info *compptr;748749cinfo->raw_data_in = FALSE;750cinfo->smoothing_factor = 0;751jpeg_default_colorspace(cinfo);752for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;753ci++, compptr++)754compptr->h_samp_factor = compptr->v_samp_factor = 1;755}756#endif757758/* Validate parameters, determine derived values */759initial_setup(cinfo, transcode_only);760761if (cinfo->arith_code)762cinfo->optimize_coding = FALSE;763else {764if (cinfo->master->lossless || /* TEMPORARY HACK ??? */765cinfo->progressive_mode)766cinfo->optimize_coding = TRUE; /* assume default tables no good for767progressive mode or lossless mode */768for (i = 0; i < NUM_HUFF_TBLS; i++) {769if (cinfo->dc_huff_tbl_ptrs[i] != NULL ||770cinfo->ac_huff_tbl_ptrs[i] != NULL) {771empty_huff_tables = FALSE;772break;773}774}775if (cinfo->data_precision == 12 && !cinfo->optimize_coding &&776(empty_huff_tables || using_std_huff_tables(cinfo)))777cinfo->optimize_coding = TRUE; /* assume default tables no good for77812-bit data precision */779}780781/* Initialize my private state */782if (transcode_only) {783/* no main pass in transcoding */784if (cinfo->optimize_coding)785master->pass_type = huff_opt_pass;786else787master->pass_type = output_pass;788} else {789/* for normal compression, first pass is always this type: */790master->pass_type = main_pass;791}792master->scan_number = 0;793master->pass_number = 0;794if (cinfo->optimize_coding)795master->total_passes = cinfo->num_scans * 2;796else797master->total_passes = cinfo->num_scans;798799master->jpeg_version = PACKAGE_NAME " version " VERSION " (build " BUILD ")";800}801802803