jcmaster.c

Go to the documentation of this file.
00001 /*
00002  * jcmaster.c
00003  *
00004  * Copyright (C) 1991-1997, Thomas G. Lane.
00005  * Modified 2003-2010 by Guido Vollbeding.
00006  * This file is part of the Independent JPEG Group's software.
00007  * For conditions of distribution and use, see the accompanying README file.
00008  *
00009  * This file contains master control logic for the JPEG compressor.
00010  * These routines are concerned with parameter validation, initial setup,
00011  * and inter-pass control (determining the number of passes and the work 
00012  * to be done in each pass).
00013  */
00014 
00015 #define JPEG_INTERNALS
00016 #include "jinclude.h"
00017 #include "jpeglib.h"
00018 
00019 
00020 /* Private state */
00021 
00022 typedef enum {
00023         main_pass,              /* input data, also do first output step */
00024         huff_opt_pass,          /* Huffman code optimization pass */
00025         output_pass             /* data output pass */
00026 } c_pass_type;
00027 
00028 typedef struct {
00029   struct jpeg_comp_master pub;  /* public fields */
00030 
00031   c_pass_type pass_type;        /* the type of the current pass */
00032 
00033   int pass_number;              /* # of passes completed */
00034   int total_passes;             /* total # of passes needed */
00035 
00036   int scan_number;              /* current index in scan_info[] */
00037 } my_comp_master;
00038 
00039 typedef my_comp_master * my_master_ptr;
00040 
00041 
00042 /*
00043  * Support routines that do various essential calculations.
00044  */
00045 
00046 /*
00047  * Compute JPEG image dimensions and related values.
00048  * NOTE: this is exported for possible use by application.
00049  * Hence it mustn't do anything that can't be done twice.
00050  */
00051 
00052 GLOBAL(void)
00053 jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo)
00054 /* Do computations that are needed before master selection phase */
00055 {
00056 #ifdef DCT_SCALING_SUPPORTED
00057 
00058   /* Compute actual JPEG image dimensions and DCT scaling choices. */
00059   if (cinfo->scale_num >= cinfo->scale_denom * 8) {
00060     /* Provide 8/1 scaling */
00061     cinfo->jpeg_width = cinfo->image_width << 3;
00062     cinfo->jpeg_height = cinfo->image_height << 3;
00063     cinfo->min_DCT_h_scaled_size = 1;
00064     cinfo->min_DCT_v_scaled_size = 1;
00065   } else if (cinfo->scale_num >= cinfo->scale_denom * 4) {
00066     /* Provide 4/1 scaling */
00067     cinfo->jpeg_width = cinfo->image_width << 2;
00068     cinfo->jpeg_height = cinfo->image_height << 2;
00069     cinfo->min_DCT_h_scaled_size = 2;
00070     cinfo->min_DCT_v_scaled_size = 2;
00071   } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 8) {
00072     /* Provide 8/3 scaling */
00073     cinfo->jpeg_width = (cinfo->image_width << 1) + (JDIMENSION)
00074       jdiv_round_up((long) cinfo->image_width * 2, 3L);
00075     cinfo->jpeg_height = (cinfo->image_height << 1) + (JDIMENSION)
00076       jdiv_round_up((long) cinfo->image_height * 2, 3L);
00077     cinfo->min_DCT_h_scaled_size = 3;
00078     cinfo->min_DCT_v_scaled_size = 3;
00079   } else if (cinfo->scale_num >= cinfo->scale_denom * 2) {
00080     /* Provide 2/1 scaling */
00081     cinfo->jpeg_width = cinfo->image_width << 1;
00082     cinfo->jpeg_height = cinfo->image_height << 1;
00083     cinfo->min_DCT_h_scaled_size = 4;
00084     cinfo->min_DCT_v_scaled_size = 4;
00085   } else if (cinfo->scale_num * 5 >= cinfo->scale_denom * 8) {
00086     /* Provide 8/5 scaling */
00087     cinfo->jpeg_width = cinfo->image_width + (JDIMENSION)
00088       jdiv_round_up((long) cinfo->image_width * 3, 5L);
00089     cinfo->jpeg_height = cinfo->image_height + (JDIMENSION)
00090       jdiv_round_up((long) cinfo->image_height * 3, 5L);
00091     cinfo->min_DCT_h_scaled_size = 5;
00092     cinfo->min_DCT_v_scaled_size = 5;
00093   } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 4) {
00094     /* Provide 4/3 scaling */
00095     cinfo->jpeg_width = cinfo->image_width + (JDIMENSION)
00096       jdiv_round_up((long) cinfo->image_width, 3L);
00097     cinfo->jpeg_height = cinfo->image_height + (JDIMENSION)
00098       jdiv_round_up((long) cinfo->image_height, 3L);
00099     cinfo->min_DCT_h_scaled_size = 6;
00100     cinfo->min_DCT_v_scaled_size = 6;
00101   } else if (cinfo->scale_num * 7 >= cinfo->scale_denom * 8) {
00102     /* Provide 8/7 scaling */
00103     cinfo->jpeg_width = cinfo->image_width + (JDIMENSION)
00104       jdiv_round_up((long) cinfo->image_width, 7L);
00105     cinfo->jpeg_height = cinfo->image_height + (JDIMENSION)
00106       jdiv_round_up((long) cinfo->image_height, 7L);
00107     cinfo->min_DCT_h_scaled_size = 7;
00108     cinfo->min_DCT_v_scaled_size = 7;
00109   } else if (cinfo->scale_num >= cinfo->scale_denom) {
00110     /* Provide 1/1 scaling */
00111     cinfo->jpeg_width = cinfo->image_width;
00112     cinfo->jpeg_height = cinfo->image_height;
00113     cinfo->min_DCT_h_scaled_size = 8;
00114     cinfo->min_DCT_v_scaled_size = 8;
00115   } else if (cinfo->scale_num * 9 >= cinfo->scale_denom * 8) {
00116     /* Provide 8/9 scaling */
00117     cinfo->jpeg_width = (JDIMENSION)
00118       jdiv_round_up((long) cinfo->image_width * 8, 9L);
00119     cinfo->jpeg_height = (JDIMENSION)
00120       jdiv_round_up((long) cinfo->image_height * 8, 9L);
00121     cinfo->min_DCT_h_scaled_size = 9;
00122     cinfo->min_DCT_v_scaled_size = 9;
00123   } else if (cinfo->scale_num * 5 >= cinfo->scale_denom * 4) {
00124     /* Provide 4/5 scaling */
00125     cinfo->jpeg_width = (JDIMENSION)
00126       jdiv_round_up((long) cinfo->image_width * 4, 5L);
00127     cinfo->jpeg_height = (JDIMENSION)
00128       jdiv_round_up((long) cinfo->image_height * 4, 5L);
00129     cinfo->min_DCT_h_scaled_size = 10;
00130     cinfo->min_DCT_v_scaled_size = 10;
00131   } else if (cinfo->scale_num * 11 >= cinfo->scale_denom * 8) {
00132     /* Provide 8/11 scaling */
00133     cinfo->jpeg_width = (JDIMENSION)
00134       jdiv_round_up((long) cinfo->image_width * 8, 11L);
00135     cinfo->jpeg_height = (JDIMENSION)
00136       jdiv_round_up((long) cinfo->image_height * 8, 11L);
00137     cinfo->min_DCT_h_scaled_size = 11;
00138     cinfo->min_DCT_v_scaled_size = 11;
00139   } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 2) {
00140     /* Provide 2/3 scaling */
00141     cinfo->jpeg_width = (JDIMENSION)
00142       jdiv_round_up((long) cinfo->image_width * 2, 3L);
00143     cinfo->jpeg_height = (JDIMENSION)
00144       jdiv_round_up((long) cinfo->image_height * 2, 3L);
00145     cinfo->min_DCT_h_scaled_size = 12;
00146     cinfo->min_DCT_v_scaled_size = 12;
00147   } else if (cinfo->scale_num * 13 >= cinfo->scale_denom * 8) {
00148     /* Provide 8/13 scaling */
00149     cinfo->jpeg_width = (JDIMENSION)
00150       jdiv_round_up((long) cinfo->image_width * 8, 13L);
00151     cinfo->jpeg_height = (JDIMENSION)
00152       jdiv_round_up((long) cinfo->image_height * 8, 13L);
00153     cinfo->min_DCT_h_scaled_size = 13;
00154     cinfo->min_DCT_v_scaled_size = 13;
00155   } else if (cinfo->scale_num * 7 >= cinfo->scale_denom * 4) {
00156     /* Provide 4/7 scaling */
00157     cinfo->jpeg_width = (JDIMENSION)
00158       jdiv_round_up((long) cinfo->image_width * 4, 7L);
00159     cinfo->jpeg_height = (JDIMENSION)
00160       jdiv_round_up((long) cinfo->image_height * 4, 7L);
00161     cinfo->min_DCT_h_scaled_size = 14;
00162     cinfo->min_DCT_v_scaled_size = 14;
00163   } else if (cinfo->scale_num * 15 >= cinfo->scale_denom * 8) {
00164     /* Provide 8/15 scaling */
00165     cinfo->jpeg_width = (JDIMENSION)
00166       jdiv_round_up((long) cinfo->image_width * 8, 15L);
00167     cinfo->jpeg_height = (JDIMENSION)
00168       jdiv_round_up((long) cinfo->image_height * 8, 15L);
00169     cinfo->min_DCT_h_scaled_size = 15;
00170     cinfo->min_DCT_v_scaled_size = 15;
00171   } else {
00172     /* Provide 1/2 scaling */
00173     cinfo->jpeg_width = (JDIMENSION)
00174       jdiv_round_up((long) cinfo->image_width, 2L);
00175     cinfo->jpeg_height = (JDIMENSION)
00176       jdiv_round_up((long) cinfo->image_height, 2L);
00177     cinfo->min_DCT_h_scaled_size = 16;
00178     cinfo->min_DCT_v_scaled_size = 16;
00179   }
00180 
00181 #else /* !DCT_SCALING_SUPPORTED */
00182 
00183   /* Hardwire it to "no scaling" */
00184   cinfo->jpeg_width = cinfo->image_width;
00185   cinfo->jpeg_height = cinfo->image_height;
00186   cinfo->min_DCT_h_scaled_size = DCTSIZE;
00187   cinfo->min_DCT_v_scaled_size = DCTSIZE;
00188 
00189 #endif /* DCT_SCALING_SUPPORTED */
00190 }
00191 
00192 
00193 LOCAL(void)
00194 jpeg_calc_trans_dimensions (j_compress_ptr cinfo)
00195 {
00196   if (cinfo->min_DCT_h_scaled_size < 1 || cinfo->min_DCT_h_scaled_size > 16
00197       || cinfo->min_DCT_h_scaled_size != cinfo->min_DCT_v_scaled_size)
00198     ERREXIT2(cinfo, JERR_BAD_DCTSIZE,
00199              cinfo->min_DCT_h_scaled_size, cinfo->min_DCT_v_scaled_size);
00200 
00201   cinfo->block_size = cinfo->min_DCT_h_scaled_size;
00202 
00203   switch (cinfo->block_size) {
00204   case 2: cinfo->natural_order = jpeg_natural_order2; break;
00205   case 3: cinfo->natural_order = jpeg_natural_order3; break;
00206   case 4: cinfo->natural_order = jpeg_natural_order4; break;
00207   case 5: cinfo->natural_order = jpeg_natural_order5; break;
00208   case 6: cinfo->natural_order = jpeg_natural_order6; break;
00209   case 7: cinfo->natural_order = jpeg_natural_order7; break;
00210   default: cinfo->natural_order = jpeg_natural_order; break;
00211   }
00212 
00213   cinfo->lim_Se = cinfo->block_size < DCTSIZE ?
00214     cinfo->block_size * cinfo->block_size - 1 : DCTSIZE2-1;
00215 }
00216 
00217 
00218 LOCAL(void)
00219 initial_setup (j_compress_ptr cinfo, boolean transcode_only)
00220 /* Do computations that are needed before master selection phase */
00221 {
00222   int ci, ssize;
00223   jpeg_component_info *compptr;
00224   long samplesperrow;
00225   JDIMENSION jd_samplesperrow;
00226 
00227   if (transcode_only)
00228     jpeg_calc_trans_dimensions(cinfo);
00229   else
00230     jpeg_calc_jpeg_dimensions(cinfo);
00231 
00232   /* Sanity check on image dimensions */
00233   if (cinfo->jpeg_height <= 0 || cinfo->jpeg_width <= 0 ||
00234       cinfo->num_components <= 0 || cinfo->input_components <= 0)
00235     ERREXIT(cinfo, JERR_EMPTY_IMAGE);
00236 
00237   /* Make sure image isn't bigger than I can handle */
00238   if ((long) cinfo->jpeg_height > (long) JPEG_MAX_DIMENSION ||
00239       (long) cinfo->jpeg_width > (long) JPEG_MAX_DIMENSION)
00240     ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
00241 
00242   /* Width of an input scanline must be representable as JDIMENSION. */
00243   samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
00244   jd_samplesperrow = (JDIMENSION) samplesperrow;
00245   if ((long) jd_samplesperrow != samplesperrow)
00246     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
00247 
00248   /* For now, precision must match compiled-in value... */
00249   if (cinfo->data_precision != BITS_IN_JSAMPLE)
00250     ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
00251 
00252   /* Check that number of components won't exceed internal array sizes */
00253   if (cinfo->num_components > MAX_COMPONENTS)
00254     ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
00255              MAX_COMPONENTS);
00256 
00257   /* Compute maximum sampling factors; check factor validity */
00258   cinfo->max_h_samp_factor = 1;
00259   cinfo->max_v_samp_factor = 1;
00260   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00261        ci++, compptr++) {
00262     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
00263         compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
00264       ERREXIT(cinfo, JERR_BAD_SAMPLING);
00265     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
00266                                    compptr->h_samp_factor);
00267     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
00268                                    compptr->v_samp_factor);
00269   }
00270 
00271   /* Compute dimensions of components */
00272   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00273        ci++, compptr++) {
00274     /* Fill in the correct component_index value; don't rely on application */
00275     compptr->component_index = ci;
00276     /* In selecting the actual DCT scaling for each component, we try to
00277      * scale down the chroma components via DCT scaling rather than downsampling.
00278      * This saves time if the downsampler gets to use 1:1 scaling.
00279      * Note this code adapts subsampling ratios which are powers of 2.
00280      */
00281     ssize = 1;
00282 #ifdef DCT_SCALING_SUPPORTED
00283     while (cinfo->min_DCT_h_scaled_size * ssize <=
00284            (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
00285            (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) {
00286       ssize = ssize * 2;
00287     }
00288 #endif
00289     compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize;
00290     ssize = 1;
00291 #ifdef DCT_SCALING_SUPPORTED
00292     while (cinfo->min_DCT_v_scaled_size * ssize <=
00293            (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
00294            (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) {
00295       ssize = ssize * 2;
00296     }
00297 #endif
00298     compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize;
00299 
00300     /* We don't support DCT ratios larger than 2. */
00301     if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2)
00302         compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;
00303     else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2)
00304         compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;
00305 
00306     /* Size in DCT blocks */
00307     compptr->width_in_blocks = (JDIMENSION)
00308       jdiv_round_up((long) cinfo->jpeg_width * (long) compptr->h_samp_factor,
00309                     (long) (cinfo->max_h_samp_factor * cinfo->block_size));
00310     compptr->height_in_blocks = (JDIMENSION)
00311       jdiv_round_up((long) cinfo->jpeg_height * (long) compptr->v_samp_factor,
00312                     (long) (cinfo->max_v_samp_factor * cinfo->block_size));
00313     /* Size in samples */
00314     compptr->downsampled_width = (JDIMENSION)
00315       jdiv_round_up((long) cinfo->jpeg_width *
00316                     (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size),
00317                     (long) (cinfo->max_h_samp_factor * cinfo->block_size));
00318     compptr->downsampled_height = (JDIMENSION)
00319       jdiv_round_up((long) cinfo->jpeg_height *
00320                     (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size),
00321                     (long) (cinfo->max_v_samp_factor * cinfo->block_size));
00322     /* Mark component needed (this flag isn't actually used for compression) */
00323     compptr->component_needed = TRUE;
00324   }
00325 
00326   /* Compute number of fully interleaved MCU rows (number of times that
00327    * main controller will call coefficient controller).
00328    */
00329   cinfo->total_iMCU_rows = (JDIMENSION)
00330     jdiv_round_up((long) cinfo->jpeg_height,
00331                   (long) (cinfo->max_v_samp_factor * cinfo->block_size));
00332 }
00333 
00334 
00335 #ifdef C_MULTISCAN_FILES_SUPPORTED
00336 
00337 LOCAL(void)
00338 validate_script (j_compress_ptr cinfo)
00339 /* Verify that the scan script in cinfo->scan_info[] is valid; also
00340  * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
00341  */
00342 {
00343   const jpeg_scan_info * scanptr;
00344   int scanno, ncomps, ci, coefi, thisi;
00345   int Ss, Se, Ah, Al;
00346   boolean component_sent[MAX_COMPONENTS];
00347 #ifdef C_PROGRESSIVE_SUPPORTED
00348   int * last_bitpos_ptr;
00349   int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
00350   /* -1 until that coefficient has been seen; then last Al for it */
00351 #endif
00352 
00353   if (cinfo->num_scans <= 0)
00354     ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
00355 
00356   /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
00357    * for progressive JPEG, no scan can have this.
00358    */
00359   scanptr = cinfo->scan_info;
00360   if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
00361 #ifdef C_PROGRESSIVE_SUPPORTED
00362     cinfo->progressive_mode = TRUE;
00363     last_bitpos_ptr = & last_bitpos[0][0];
00364     for (ci = 0; ci < cinfo->num_components; ci++) 
00365       for (coefi = 0; coefi < DCTSIZE2; coefi++)
00366         *last_bitpos_ptr++ = -1;
00367 #else
00368     ERREXIT(cinfo, JERR_NOT_COMPILED);
00369 #endif
00370   } else {
00371     cinfo->progressive_mode = FALSE;
00372     for (ci = 0; ci < cinfo->num_components; ci++) 
00373       component_sent[ci] = FALSE;
00374   }
00375 
00376   for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
00377     /* Validate component indexes */
00378     ncomps = scanptr->comps_in_scan;
00379     if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
00380       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
00381     for (ci = 0; ci < ncomps; ci++) {
00382       thisi = scanptr->component_index[ci];
00383       if (thisi < 0 || thisi >= cinfo->num_components)
00384         ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
00385       /* Components must appear in SOF order within each scan */
00386       if (ci > 0 && thisi <= scanptr->component_index[ci-1])
00387         ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
00388     }
00389     /* Validate progression parameters */
00390     Ss = scanptr->Ss;
00391     Se = scanptr->Se;
00392     Ah = scanptr->Ah;
00393     Al = scanptr->Al;
00394     if (cinfo->progressive_mode) {
00395 #ifdef C_PROGRESSIVE_SUPPORTED
00396       /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that
00397        * seems wrong: the upper bound ought to depend on data precision.
00398        * Perhaps they really meant 0..N+1 for N-bit precision.
00399        * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
00400        * out-of-range reconstructed DC values during the first DC scan,
00401        * which might cause problems for some decoders.
00402        */
00403 #if BITS_IN_JSAMPLE == 8
00404 #define MAX_AH_AL 10
00405 #else
00406 #define MAX_AH_AL 13
00407 #endif
00408       if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
00409           Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
00410         ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
00411       if (Ss == 0) {
00412         if (Se != 0)            /* DC and AC together not OK */
00413           ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
00414       } else {
00415         if (ncomps != 1)        /* AC scans must be for only one component */
00416           ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
00417       }
00418       for (ci = 0; ci < ncomps; ci++) {
00419         last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
00420         if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
00421           ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
00422         for (coefi = Ss; coefi <= Se; coefi++) {
00423           if (last_bitpos_ptr[coefi] < 0) {
00424             /* first scan of this coefficient */
00425             if (Ah != 0)
00426               ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
00427           } else {
00428             /* not first scan */
00429             if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
00430               ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
00431           }
00432           last_bitpos_ptr[coefi] = Al;
00433         }
00434       }
00435 #endif
00436     } else {
00437       /* For sequential JPEG, all progression parameters must be these: */
00438       if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
00439         ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
00440       /* Make sure components are not sent twice */
00441       for (ci = 0; ci < ncomps; ci++) {
00442         thisi = scanptr->component_index[ci];
00443         if (component_sent[thisi])
00444           ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
00445         component_sent[thisi] = TRUE;
00446       }
00447     }
00448   }
00449 
00450   /* Now verify that everything got sent. */
00451   if (cinfo->progressive_mode) {
00452 #ifdef C_PROGRESSIVE_SUPPORTED
00453     /* For progressive mode, we only check that at least some DC data
00454      * got sent for each component; the spec does not require that all bits
00455      * of all coefficients be transmitted.  Would it be wiser to enforce
00456      * transmission of all coefficient bits??
00457      */
00458     for (ci = 0; ci < cinfo->num_components; ci++) {
00459       if (last_bitpos[ci][0] < 0)
00460         ERREXIT(cinfo, JERR_MISSING_DATA);
00461     }
00462 #endif
00463   } else {
00464     for (ci = 0; ci < cinfo->num_components; ci++) {
00465       if (! component_sent[ci])
00466         ERREXIT(cinfo, JERR_MISSING_DATA);
00467     }
00468   }
00469 }
00470 
00471 
00472 LOCAL(void)
00473 reduce_script (j_compress_ptr cinfo)
00474 /* Adapt scan script for use with reduced block size;
00475  * assume that script has been validated before.
00476  */
00477 {
00478   jpeg_scan_info * scanptr;
00479   int idxout, idxin;
00480 
00481   /* Circumvent const declaration for this function */
00482   scanptr = (jpeg_scan_info *) cinfo->scan_info;
00483   idxout = 0;
00484 
00485   for (idxin = 0; idxin < cinfo->num_scans; idxin++) {
00486     /* After skipping, idxout becomes smaller than idxin */
00487     if (idxin != idxout)
00488       /* Copy rest of data;
00489        * note we stay in given chunk of allocated memory.
00490        */
00491       scanptr[idxout] = scanptr[idxin];
00492     if (scanptr[idxout].Ss > cinfo->lim_Se)
00493       /* Entire scan out of range - skip this entry */
00494       continue;
00495     if (scanptr[idxout].Se > cinfo->lim_Se)
00496       /* Limit scan to end of block */
00497       scanptr[idxout].Se = cinfo->lim_Se;
00498     idxout++;
00499   }
00500 
00501   cinfo->num_scans = idxout;
00502 }
00503 
00504 #endif /* C_MULTISCAN_FILES_SUPPORTED */
00505 
00506 
00507 LOCAL(void)
00508 select_scan_parameters (j_compress_ptr cinfo)
00509 /* Set up the scan parameters for the current scan */
00510 {
00511   int ci;
00512 
00513 #ifdef C_MULTISCAN_FILES_SUPPORTED
00514   if (cinfo->scan_info != NULL) {
00515     /* Prepare for current scan --- the script is already validated */
00516     my_master_ptr master = (my_master_ptr) cinfo->master;
00517     const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
00518 
00519     cinfo->comps_in_scan = scanptr->comps_in_scan;
00520     for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
00521       cinfo->cur_comp_info[ci] =
00522         &cinfo->comp_info[scanptr->component_index[ci]];
00523     }
00524     if (cinfo->progressive_mode) {
00525       cinfo->Ss = scanptr->Ss;
00526       cinfo->Se = scanptr->Se;
00527       cinfo->Ah = scanptr->Ah;
00528       cinfo->Al = scanptr->Al;
00529       return;
00530     }
00531   }
00532   else
00533 #endif
00534   {
00535     /* Prepare for single sequential-JPEG scan containing all components */
00536     if (cinfo->num_components > MAX_COMPS_IN_SCAN)
00537       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
00538                MAX_COMPS_IN_SCAN);
00539     cinfo->comps_in_scan = cinfo->num_components;
00540     for (ci = 0; ci < cinfo->num_components; ci++) {
00541       cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
00542     }
00543   }
00544   cinfo->Ss = 0;
00545   cinfo->Se = cinfo->block_size * cinfo->block_size - 1;
00546   cinfo->Ah = 0;
00547   cinfo->Al = 0;
00548 }
00549 
00550 
00551 LOCAL(void)
00552 per_scan_setup (j_compress_ptr cinfo)
00553 /* Do computations that are needed before processing a JPEG scan */
00554 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
00555 {
00556   int ci, mcublks, tmp;
00557   jpeg_component_info *compptr;
00558   
00559   if (cinfo->comps_in_scan == 1) {
00560     
00561     /* Noninterleaved (single-component) scan */
00562     compptr = cinfo->cur_comp_info[0];
00563     
00564     /* Overall image size in MCUs */
00565     cinfo->MCUs_per_row = compptr->width_in_blocks;
00566     cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
00567     
00568     /* For noninterleaved scan, always one block per MCU */
00569     compptr->MCU_width = 1;
00570     compptr->MCU_height = 1;
00571     compptr->MCU_blocks = 1;
00572     compptr->MCU_sample_width = compptr->DCT_h_scaled_size;
00573     compptr->last_col_width = 1;
00574     /* For noninterleaved scans, it is convenient to define last_row_height
00575      * as the number of block rows present in the last iMCU row.
00576      */
00577     tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
00578     if (tmp == 0) tmp = compptr->v_samp_factor;
00579     compptr->last_row_height = tmp;
00580     
00581     /* Prepare array describing MCU composition */
00582     cinfo->blocks_in_MCU = 1;
00583     cinfo->MCU_membership[0] = 0;
00584     
00585   } else {
00586     
00587     /* Interleaved (multi-component) scan */
00588     if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
00589       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
00590                MAX_COMPS_IN_SCAN);
00591     
00592     /* Overall image size in MCUs */
00593     cinfo->MCUs_per_row = (JDIMENSION)
00594       jdiv_round_up((long) cinfo->jpeg_width,
00595                     (long) (cinfo->max_h_samp_factor * cinfo->block_size));
00596     cinfo->MCU_rows_in_scan = (JDIMENSION)
00597       jdiv_round_up((long) cinfo->jpeg_height,
00598                     (long) (cinfo->max_v_samp_factor * cinfo->block_size));
00599     
00600     cinfo->blocks_in_MCU = 0;
00601     
00602     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00603       compptr = cinfo->cur_comp_info[ci];
00604       /* Sampling factors give # of blocks of component in each MCU */
00605       compptr->MCU_width = compptr->h_samp_factor;
00606       compptr->MCU_height = compptr->v_samp_factor;
00607       compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
00608       compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size;
00609       /* Figure number of non-dummy blocks in last MCU column & row */
00610       tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
00611       if (tmp == 0) tmp = compptr->MCU_width;
00612       compptr->last_col_width = tmp;
00613       tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
00614       if (tmp == 0) tmp = compptr->MCU_height;
00615       compptr->last_row_height = tmp;
00616       /* Prepare array describing MCU composition */
00617       mcublks = compptr->MCU_blocks;
00618       if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
00619         ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
00620       while (mcublks-- > 0) {
00621         cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
00622       }
00623     }
00624     
00625   }
00626 
00627   /* Convert restart specified in rows to actual MCU count. */
00628   /* Note that count must fit in 16 bits, so we provide limiting. */
00629   if (cinfo->restart_in_rows > 0) {
00630     long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
00631     cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
00632   }
00633 }
00634 
00635 
00636 /*
00637  * Per-pass setup.
00638  * This is called at the beginning of each pass.  We determine which modules
00639  * will be active during this pass and give them appropriate start_pass calls.
00640  * We also set is_last_pass to indicate whether any more passes will be
00641  * required.
00642  */
00643 
00644 METHODDEF(void)
00645 prepare_for_pass (j_compress_ptr cinfo)
00646 {
00647   my_master_ptr master = (my_master_ptr) cinfo->master;
00648 
00649   switch (master->pass_type) {
00650   case main_pass:
00651     /* Initial pass: will collect input data, and do either Huffman
00652      * optimization or data output for the first scan.
00653      */
00654     select_scan_parameters(cinfo);
00655     per_scan_setup(cinfo);
00656     if (! cinfo->raw_data_in) {
00657       (*cinfo->cconvert->start_pass) (cinfo);
00658       (*cinfo->downsample->start_pass) (cinfo);
00659       (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
00660     }
00661     (*cinfo->fdct->start_pass) (cinfo);
00662     (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
00663     (*cinfo->coef->start_pass) (cinfo,
00664                                 (master->total_passes > 1 ?
00665                                  JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
00666     (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
00667     if (cinfo->optimize_coding) {
00668       /* No immediate data output; postpone writing frame/scan headers */
00669       master->pub.call_pass_startup = FALSE;
00670     } else {
00671       /* Will write frame/scan headers at first jpeg_write_scanlines call */
00672       master->pub.call_pass_startup = TRUE;
00673     }
00674     break;
00675 #ifdef ENTROPY_OPT_SUPPORTED
00676   case huff_opt_pass:
00677     /* Do Huffman optimization for a scan after the first one. */
00678     select_scan_parameters(cinfo);
00679     per_scan_setup(cinfo);
00680     if (cinfo->Ss != 0 || cinfo->Ah == 0) {
00681       (*cinfo->entropy->start_pass) (cinfo, TRUE);
00682       (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
00683       master->pub.call_pass_startup = FALSE;
00684       break;
00685     }
00686     /* Special case: Huffman DC refinement scans need no Huffman table
00687      * and therefore we can skip the optimization pass for them.
00688      */
00689     master->pass_type = output_pass;
00690     master->pass_number++;
00691     /*FALLTHROUGH*/
00692 #endif
00693   case output_pass:
00694     /* Do a data-output pass. */
00695     /* We need not repeat per-scan setup if prior optimization pass did it. */
00696     if (! cinfo->optimize_coding) {
00697       select_scan_parameters(cinfo);
00698       per_scan_setup(cinfo);
00699     }
00700     (*cinfo->entropy->start_pass) (cinfo, FALSE);
00701     (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
00702     /* We emit frame/scan headers now */
00703     if (master->scan_number == 0)
00704       (*cinfo->marker->write_frame_header) (cinfo);
00705     (*cinfo->marker->write_scan_header) (cinfo);
00706     master->pub.call_pass_startup = FALSE;
00707     break;
00708   default:
00709     ERREXIT(cinfo, JERR_NOT_COMPILED);
00710   }
00711 
00712   master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
00713 
00714   /* Set up progress monitor's pass info if present */
00715   if (cinfo->progress != NULL) {
00716     cinfo->progress->completed_passes = master->pass_number;
00717     cinfo->progress->total_passes = master->total_passes;
00718   }
00719 }
00720 
00721 
00722 /*
00723  * Special start-of-pass hook.
00724  * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
00725  * In single-pass processing, we need this hook because we don't want to
00726  * write frame/scan headers during jpeg_start_compress; we want to let the
00727  * application write COM markers etc. between jpeg_start_compress and the
00728  * jpeg_write_scanlines loop.
00729  * In multi-pass processing, this routine is not used.
00730  */
00731 
00732 METHODDEF(void)
00733 pass_startup (j_compress_ptr cinfo)
00734 {
00735   cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
00736 
00737   (*cinfo->marker->write_frame_header) (cinfo);
00738   (*cinfo->marker->write_scan_header) (cinfo);
00739 }
00740 
00741 
00742 /*
00743  * Finish up at end of pass.
00744  */
00745 
00746 METHODDEF(void)
00747 finish_pass_master (j_compress_ptr cinfo)
00748 {
00749   my_master_ptr master = (my_master_ptr) cinfo->master;
00750 
00751   /* The entropy coder always needs an end-of-pass call,
00752    * either to analyze statistics or to flush its output buffer.
00753    */
00754   (*cinfo->entropy->finish_pass) (cinfo);
00755 
00756   /* Update state for next pass */
00757   switch (master->pass_type) {
00758   case main_pass:
00759     /* next pass is either output of scan 0 (after optimization)
00760      * or output of scan 1 (if no optimization).
00761      */
00762     master->pass_type = output_pass;
00763     if (! cinfo->optimize_coding)
00764       master->scan_number++;
00765     break;
00766   case huff_opt_pass:
00767     /* next pass is always output of current scan */
00768     master->pass_type = output_pass;
00769     break;
00770   case output_pass:
00771     /* next pass is either optimization or output of next scan */
00772     if (cinfo->optimize_coding)
00773       master->pass_type = huff_opt_pass;
00774     master->scan_number++;
00775     break;
00776   }
00777 
00778   master->pass_number++;
00779 }
00780 
00781 
00782 /*
00783  * Initialize master compression control.
00784  */
00785 
00786 GLOBAL(void)
00787 jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
00788 {
00789   my_master_ptr master;
00790 
00791   master = (my_master_ptr)
00792       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00793                                   SIZEOF(my_comp_master));
00794   cinfo->master = (struct jpeg_comp_master *) master;
00795   master->pub.prepare_for_pass = prepare_for_pass;
00796   master->pub.pass_startup = pass_startup;
00797   master->pub.finish_pass = finish_pass_master;
00798   master->pub.is_last_pass = FALSE;
00799 
00800   /* Validate parameters, determine derived values */
00801   initial_setup(cinfo, transcode_only);
00802 
00803   if (cinfo->scan_info != NULL) {
00804 #ifdef C_MULTISCAN_FILES_SUPPORTED
00805     validate_script(cinfo);
00806     if (cinfo->block_size < DCTSIZE)
00807       reduce_script(cinfo);
00808 #else
00809     ERREXIT(cinfo, JERR_NOT_COMPILED);
00810 #endif
00811   } else {
00812     cinfo->progressive_mode = FALSE;
00813     cinfo->num_scans = 1;
00814   }
00815 
00816   if ((cinfo->progressive_mode || cinfo->block_size < DCTSIZE) &&
00817       !cinfo->arith_code)                       /*  TEMPORARY HACK ??? */
00818     /* assume default tables no good for progressive or downscale mode */
00819     cinfo->optimize_coding = TRUE;
00820 
00821   /* Initialize my private state */
00822   if (transcode_only) {
00823     /* no main pass in transcoding */
00824     if (cinfo->optimize_coding)
00825       master->pass_type = huff_opt_pass;
00826     else
00827       master->pass_type = output_pass;
00828   } else {
00829     /* for normal compression, first pass is always this type: */
00830     master->pass_type = main_pass;
00831   }
00832   master->scan_number = 0;
00833   master->pass_number = 0;
00834   if (cinfo->optimize_coding)
00835     master->total_passes = cinfo->num_scans * 2;
00836   else
00837     master->total_passes = cinfo->num_scans;
00838 }

Generated on Tue Jul 5 14:13:28 2011 for ROOT_528-00b_version by  doxygen 1.5.1