home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / GRAPHICS / jpeglib_5a.lzh / JPEG_5A / jcmaster.c < prev    next >
Text File  |  1995-01-14  |  13KB  |  388 lines

  1. /*
  2.  * jcmaster.c
  3.  *
  4.  * Copyright (C) 1991-1994, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains master control logic for the JPEG compressor.
  9.  * These routines are concerned with selecting the modules to be executed
  10.  * and with determining the number of passes and the work to be done in each
  11.  * pass.
  12.  */
  13.  
  14. #define JPEG_INTERNALS
  15. #include "jinclude.h"
  16. #include "jpeglib.h"
  17.  
  18.  
  19. /* Private state */
  20.  
  21. typedef struct {
  22.   struct jpeg_comp_master pub;    /* public fields */
  23.  
  24.   int pass_number;        /* eventually need more complex state... */
  25. } my_comp_master;
  26.  
  27. typedef my_comp_master * my_master_ptr;
  28.  
  29.  
  30. /*
  31.  * Support routines that do various essential calculations.
  32.  */
  33.  
  34. LOCAL void
  35. initial_setup (j_compress_ptr cinfo)
  36. /* Do computations that are needed before master selection phase */
  37. {
  38.   int ci;
  39.   jpeg_component_info *compptr;
  40.   long samplesperrow;
  41.   JDIMENSION jd_samplesperrow;
  42.  
  43.   /* Sanity check on image dimensions */
  44.   if (cinfo->image_height <= 0 || cinfo->image_width <= 0
  45.       || cinfo->num_components <= 0 || cinfo->input_components <= 0)
  46.     ERREXIT(cinfo, JERR_EMPTY_IMAGE);
  47.  
  48.   /* Make sure image isn't bigger than I can handle */
  49.   if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
  50.       (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
  51.     ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
  52.  
  53.   /* Width of an input scanline must be representable as JDIMENSION. */
  54.   samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
  55.   jd_samplesperrow = (JDIMENSION) samplesperrow;
  56.   if ((long) jd_samplesperrow != samplesperrow)
  57.     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  58.  
  59.   /* For now, precision must match compiled-in value... */
  60.   if (cinfo->data_precision != BITS_IN_JSAMPLE)
  61.     ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
  62.  
  63.   /* Check that number of components won't exceed internal array sizes */
  64.   if (cinfo->num_components > MAX_COMPONENTS)
  65.     ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  66.          MAX_COMPONENTS);
  67.  
  68.   /* Compute maximum sampling factors; check factor validity */
  69.   cinfo->max_h_samp_factor = 1;
  70.   cinfo->max_v_samp_factor = 1;
  71.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  72.        ci++, compptr++) {
  73.     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
  74.     compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
  75.       ERREXIT(cinfo, JERR_BAD_SAMPLING);
  76.     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
  77.                    compptr->h_samp_factor);
  78.     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
  79.                    compptr->v_samp_factor);
  80.   }
  81.  
  82.   /* Compute dimensions of components */
  83.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  84.        ci++, compptr++) {
  85.     /* For compression, we never do DCT scaling. */
  86.     compptr->DCT_scaled_size = DCTSIZE;
  87.     /* Size in DCT blocks */
  88.     compptr->width_in_blocks = (JDIMENSION)
  89.       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  90.             (long) (cinfo->max_h_samp_factor * DCTSIZE));
  91.     compptr->height_in_blocks = (JDIMENSION)
  92.       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  93.             (long) (cinfo->max_v_samp_factor * DCTSIZE));
  94.     /* Size in samples */
  95.     compptr->downsampled_width = (JDIMENSION)
  96.       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  97.             (long) cinfo->max_h_samp_factor);
  98.     compptr->downsampled_height = (JDIMENSION)
  99.       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  100.             (long) cinfo->max_v_samp_factor);
  101.     /* Mark component needed (this flag isn't actually used for compression) */
  102.     compptr->component_needed = TRUE;
  103.   }
  104.  
  105.   /* Compute number of fully interleaved MCU rows (number of times that
  106.    * main controller will call coefficient controller).
  107.    */
  108.   cinfo->total_iMCU_rows = (JDIMENSION)
  109.     jdiv_round_up((long) cinfo->image_height,
  110.           (long) (cinfo->max_v_samp_factor*DCTSIZE));
  111. }
  112.  
  113.  
  114. LOCAL void
  115. per_scan_setup (j_compress_ptr cinfo)
  116. /* Do computations that are needed before processing a JPEG scan */
  117. /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
  118. {
  119.   int ci, mcublks, tmp;
  120.   jpeg_component_info *compptr;
  121.   
  122.   if (cinfo->comps_in_scan == 1) {
  123.     
  124.     /* Noninterleaved (single-component) scan */
  125.     compptr = cinfo->cur_comp_info[0];
  126.     
  127.     /* Overall image size in MCUs */
  128.     cinfo->MCUs_per_row = compptr->width_in_blocks;
  129.     cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
  130.     
  131.     /* For noninterleaved scan, always one block per MCU */
  132.     compptr->MCU_width = 1;
  133.     compptr->MCU_height = 1;
  134.     compptr->MCU_blocks = 1;
  135.     compptr->MCU_sample_width = DCTSIZE;
  136.     compptr->last_col_width = 1;
  137.     compptr->last_row_height = 1;
  138.     
  139.     /* Prepare array describing MCU composition */
  140.     cinfo->blocks_in_MCU = 1;
  141.     cinfo->MCU_membership[0] = 0;
  142.     
  143.   } else {
  144.     
  145.     /* Interleaved (multi-component) scan */
  146.     if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  147.       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
  148.            MAX_COMPS_IN_SCAN);
  149.     
  150.     /* Overall image size in MCUs */
  151.     cinfo->MCUs_per_row = (JDIMENSION)
  152.       jdiv_round_up((long) cinfo->image_width,
  153.             (long) (cinfo->max_h_samp_factor*DCTSIZE));
  154.     cinfo->MCU_rows_in_scan = (JDIMENSION)
  155.       jdiv_round_up((long) cinfo->image_height,
  156.             (long) (cinfo->max_v_samp_factor*DCTSIZE));
  157.     
  158.     cinfo->blocks_in_MCU = 0;
  159.     
  160.     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  161.       compptr = cinfo->cur_comp_info[ci];
  162.       /* Sampling factors give # of blocks of component in each MCU */
  163.       compptr->MCU_width = compptr->h_samp_factor;
  164.       compptr->MCU_height = compptr->v_samp_factor;
  165.       compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  166.       compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
  167.       /* Figure number of non-dummy blocks in last MCU column & row */
  168.       tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
  169.       if (tmp == 0) tmp = compptr->MCU_width;
  170.       compptr->last_col_width = tmp;
  171.       tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
  172.       if (tmp == 0) tmp = compptr->MCU_height;
  173.       compptr->last_row_height = tmp;
  174.       /* Prepare array describing MCU composition */
  175.       mcublks = compptr->MCU_blocks;
  176.       if (cinfo->blocks_in_MCU + mcublks > MAX_BLOCKS_IN_MCU)
  177.     ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
  178.       while (mcublks-- > 0) {
  179.     cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  180.       }
  181.     }
  182.     
  183.   }
  184.  
  185.   /* Convert restart specified in rows to actual MCU count. */
  186.   /* Note that count must fit in 16 bits, so we provide limiting. */
  187.   if (cinfo->restart_in_rows > 0) {
  188.     long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
  189.     cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
  190.   }
  191. }
  192.  
  193.  
  194. /*
  195.  * Master selection of compression modules.
  196.  * This is done once at the start of processing an image.  We determine
  197.  * which modules will be used and give them appropriate initialization calls.
  198.  */
  199.  
  200. LOCAL void
  201. master_selection (j_compress_ptr cinfo)
  202. {
  203.   my_master_ptr master = (my_master_ptr) cinfo->master;
  204.  
  205.   initial_setup(cinfo);
  206.   master->pass_number = 0;
  207.  
  208.   /* There's not a lot of smarts here right now, but it'll get more
  209.    * complicated when we have multiple implementations available...
  210.    */
  211.  
  212.   /* Preprocessing */
  213.   if (! cinfo->raw_data_in) {
  214.     jinit_color_converter(cinfo);
  215.     jinit_downsampler(cinfo);
  216.     jinit_c_prep_controller(cinfo, FALSE /* never need full buffer here */);
  217.   }
  218.   /* Forward DCT */
  219.   jinit_forward_dct(cinfo);
  220.   /* Entropy encoding: either Huffman or arithmetic coding. */
  221.   if (cinfo->arith_code) {
  222. #ifdef C_ARITH_CODING_SUPPORTED
  223.     jinit_arith_encoder(cinfo);
  224. #else
  225.     ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
  226. #endif
  227.   } else
  228.     jinit_huff_encoder(cinfo);
  229.  
  230.   /* For now, a full buffer is needed only for Huffman optimization. */
  231.   jinit_c_coef_controller(cinfo, cinfo->optimize_coding);
  232.   jinit_c_main_controller(cinfo, FALSE /* never need full buffer here */);
  233.  
  234.   jinit_marker_writer(cinfo);
  235.  
  236.   /* We can now tell the memory manager to allocate virtual arrays. */
  237.   (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
  238.  
  239.   /* Write the datastream header (SOI) immediately.
  240.    * Frame and scan headers are postponed till later.
  241.    * This lets application insert special markers after the SOI.
  242.    */
  243.   (*cinfo->marker->write_file_header) (cinfo);
  244. }
  245.  
  246.  
  247. /*
  248.  * Per-pass setup.
  249.  * This is called at the beginning of each pass.  We determine which modules
  250.  * will be active during this pass and give them appropriate start_pass calls.
  251.  * We also set is_last_pass to indicate whether any more passes will be
  252.  * required.
  253.  */
  254.  
  255. METHODDEF void
  256. prepare_for_pass (j_compress_ptr cinfo)
  257. {
  258.   my_master_ptr master = (my_master_ptr) cinfo->master;
  259.   int ci;
  260.   int npasses;
  261.  
  262.   /* ???? JUST A QUICK CROCK FOR NOW ??? */
  263.  
  264.   /* For now, handle only single interleaved output scan; */
  265.   /* we support two passes for Huffman optimization. */
  266.  
  267.   /* Prepare for single scan containing all components */
  268.   if (cinfo->num_components > MAX_COMPS_IN_SCAN)
  269.     ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  270.          MAX_COMPS_IN_SCAN);
  271.   cinfo->comps_in_scan = cinfo->num_components;
  272.   for (ci = 0; ci < cinfo->num_components; ci++) {
  273.     cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
  274.   }
  275.  
  276.   per_scan_setup(cinfo);
  277.  
  278.   if (! cinfo->optimize_coding) {
  279.     /* Standard single-pass case */
  280.     npasses = 1;
  281.     master->pub.call_pass_startup = TRUE;
  282.     master->pub.is_last_pass = TRUE;
  283.     if (! cinfo->raw_data_in) {
  284.       (*cinfo->cconvert->start_pass) (cinfo);
  285.       (*cinfo->downsample->start_pass) (cinfo);
  286.       (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
  287.     }
  288.     (*cinfo->fdct->start_pass) (cinfo);
  289.     (*cinfo->entropy->start_pass) (cinfo, FALSE);
  290.     (*cinfo->coef->start_pass) (cinfo, JBUF_PASS_THRU);
  291.     (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
  292.   } else {
  293.     npasses = 2;
  294.     switch (master->pass_number) {
  295.     case 0:
  296.       /* Huffman optimization: run all modules, gather statistics */
  297.       master->pub.call_pass_startup = FALSE;
  298.       master->pub.is_last_pass = FALSE;
  299.       if (! cinfo->raw_data_in) {
  300.     (*cinfo->cconvert->start_pass) (cinfo);
  301.     (*cinfo->downsample->start_pass) (cinfo);
  302.     (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
  303.       }
  304.       (*cinfo->fdct->start_pass) (cinfo);
  305.       (*cinfo->entropy->start_pass) (cinfo, TRUE);
  306.       (*cinfo->coef->start_pass) (cinfo, JBUF_SAVE_AND_PASS);
  307.       (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
  308.       break;
  309.     case 1:
  310.       /* Second pass: reread data from coefficient buffer */
  311.       master->pub.is_last_pass = TRUE;
  312.       (*cinfo->entropy->start_pass) (cinfo, FALSE);
  313.       (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
  314.       /* We emit frame/scan headers now */
  315.       (*cinfo->marker->write_frame_header) (cinfo);
  316.       (*cinfo->marker->write_scan_header) (cinfo);
  317.       break;
  318.     }
  319.   }
  320.  
  321.   /* Set up progress monitor's pass info if present */
  322.   if (cinfo->progress != NULL) {
  323.     cinfo->progress->completed_passes = master->pass_number;
  324.     cinfo->progress->total_passes = npasses;
  325.   }
  326.  
  327.   master->pass_number++;
  328. }
  329.  
  330.  
  331. /*
  332.  * Special start-of-pass hook.
  333.  * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
  334.  * In single-pass processing, we need this hook because we don't want to
  335.  * write frame/scan headers during jpeg_start_compress; we want to let the
  336.  * application write COM markers etc. between jpeg_start_compress and the
  337.  * jpeg_write_scanlines loop.
  338.  * In multi-pass processing, this routine is not used.
  339.  */
  340.  
  341. METHODDEF void
  342. pass_startup (j_compress_ptr cinfo)
  343. {
  344.   cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
  345.  
  346.   (*cinfo->marker->write_frame_header) (cinfo);
  347.   (*cinfo->marker->write_scan_header) (cinfo);
  348. }
  349.  
  350.  
  351. /*
  352.  * Finish up at end of pass.
  353.  */
  354.  
  355. METHODDEF void
  356. finish_pass_master (j_compress_ptr cinfo)
  357. {
  358.   /* More complex logic later ??? */
  359.  
  360.   /* The entropy coder needs an end-of-pass call, either to analyze
  361.    * statistics or to flush its output buffer.
  362.    */
  363.   (*cinfo->entropy->finish_pass) (cinfo);
  364. }
  365.  
  366.  
  367. /*
  368.  * Initialize master compression control.
  369.  * This creates my own subrecord and also performs the master selection phase,
  370.  * which causes other modules to create their subrecords.
  371.  */
  372.  
  373. GLOBAL void
  374. jinit_master_compress (j_compress_ptr cinfo)
  375. {
  376.   my_master_ptr master;
  377.  
  378.   master = (my_master_ptr)
  379.       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  380.                   SIZEOF(my_comp_master));
  381.   cinfo->master = (struct jpeg_comp_master *) master;
  382.   master->pub.prepare_for_pass = prepare_for_pass;
  383.   master->pub.pass_startup = pass_startup;
  384.   master->pub.finish_pass = finish_pass_master;
  385.  
  386.   master_selection(cinfo);
  387. }
  388.