home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 5 / ctrom5b.zip / ctrom5b / DOS / GRAFISCH / JPGSRC5A / JCAPI.C < prev    next >
C/C++ Source or Header  |  1994-07-16  |  12KB  |  370 lines

  1. /*
  2.  * jcapi.c
  3.  *
  4.  * Copyright (C) 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 application interface code for the compression half of
  9.  * the JPEG library.  Most of the routines intended to be called directly by
  10.  * an application are in this file.  But also see jcparam.c for
  11.  * parameter-setup helper routines, and jcomapi.c for routines shared by
  12.  * compression and decompression.
  13.  */
  14.  
  15. #define JPEG_INTERNALS
  16. #include "jinclude.h"
  17. #include "jpeglib.h"
  18.  
  19.  
  20. /*
  21.  * Initialization of a JPEG compression object.
  22.  * The error manager must already be set up (in case memory manager fails).
  23.  */
  24.  
  25. GLOBAL void
  26. jpeg_create_compress (j_compress_ptr cinfo)
  27. {
  28.   int i;
  29.  
  30.   /* For debugging purposes, zero the whole master structure.
  31.    * But error manager pointer is already there, so save and restore it.
  32.    */
  33.   {
  34.     struct jpeg_error_mgr * err = cinfo->err;
  35.     MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
  36.     cinfo->err = err;
  37.   }
  38.   cinfo->is_decompressor = FALSE;
  39.  
  40.   /* Initialize a memory manager instance for this object */
  41.   jinit_memory_mgr((j_common_ptr) cinfo);
  42.  
  43.   /* Zero out pointers to permanent structures. */
  44.   cinfo->progress = NULL;
  45.   cinfo->dest = NULL;
  46.  
  47.   cinfo->comp_info = NULL;
  48.  
  49.   for (i = 0; i < NUM_QUANT_TBLS; i++)
  50.     cinfo->quant_tbl_ptrs[i] = NULL;
  51.  
  52.   for (i = 0; i < NUM_HUFF_TBLS; i++) {
  53.     cinfo->dc_huff_tbl_ptrs[i] = NULL;
  54.     cinfo->ac_huff_tbl_ptrs[i] = NULL;
  55.   }
  56.  
  57.   cinfo->input_gamma = 1.0;    /* in case application forgets */
  58.  
  59.   /* OK, I'm ready */
  60.   cinfo->global_state = CSTATE_START;
  61. }
  62.  
  63.  
  64. /*
  65.  * Destruction of a JPEG compression object
  66.  */
  67.  
  68. GLOBAL void
  69. jpeg_destroy_compress (j_compress_ptr cinfo)
  70. {
  71.   jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
  72. }
  73.  
  74.  
  75. /*
  76.  * Forcibly suppress or un-suppress all quantization and Huffman tables.
  77.  * Marks all currently defined tables as already written (if suppress)
  78.  * or not written (if !suppress).  This will control whether they get emitted
  79.  * by a subsequent jpeg_start_compress call.
  80.  *
  81.  * This routine is exported for use by applications that want to produce
  82.  * abbreviated JPEG datastreams.  It logically belongs in jcparam.c, but
  83.  * since it is called by jpeg_start_compress, we put it here --- otherwise
  84.  * jcparam.o would be linked whether the application used it or not.
  85.  */
  86.  
  87. GLOBAL void
  88. jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
  89. {
  90.   int i;
  91.   JQUANT_TBL * qtbl;
  92.   JHUFF_TBL * htbl;
  93.  
  94.   for (i = 0; i < NUM_QUANT_TBLS; i++) {
  95.     if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
  96.       qtbl->sent_table = suppress;
  97.   }
  98.  
  99.   for (i = 0; i < NUM_HUFF_TBLS; i++) {
  100.     if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
  101.       htbl->sent_table = suppress;
  102.     if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
  103.       htbl->sent_table = suppress;
  104.   }
  105. }
  106.  
  107.  
  108. /*
  109.  * Compression initialization.
  110.  * Before calling this, all parameters and a data destination must be set up.
  111.  *
  112.  * We require a write_all_tables parameter as a failsafe check when writing
  113.  * multiple datastreams from the same compression object.  Since prior runs
  114.  * will have left all the tables marked sent_table=TRUE, a subsequent run
  115.  * would emit an abbreviated stream (no tables) by default.  This may be what
  116.  * is wanted, but for safety's sake it should not be the default behavior:
  117.  * programmers should have to make a deliberate choice to emit abbreviated
  118.  * images.  Therefore the documentation and examples should encourage people
  119.  * to pass write_all_tables=TRUE; then it will take active thought to do the
  120.  * wrong thing.
  121.  */
  122.  
  123. GLOBAL void
  124. jpeg_start_compress (j_compress_ptr cinfo, boolean write_all_tables)
  125. {
  126.   if (cinfo->global_state != CSTATE_START)
  127.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  128.  
  129.   if (write_all_tables)
  130.     jpeg_suppress_tables(cinfo, FALSE);    /* mark all tables to be written */
  131.  
  132.   /* (Re)initialize error mgr and destination modules */
  133.   (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  134.   (*cinfo->dest->init_destination) (cinfo);
  135.   /* Perform master selection of active modules */
  136.   jinit_master_compress(cinfo);
  137.   /* Set up for the first pass */
  138.   (*cinfo->master->prepare_for_pass) (cinfo);
  139.   /* Ready for application to drive first pass through jpeg_write_scanlines
  140.    * or jpeg_write_raw_data.
  141.    */
  142.   cinfo->next_scanline = 0;
  143.   cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);
  144. }
  145.  
  146.  
  147. /*
  148.  * Write some scanlines of data to the JPEG compressor.
  149.  *
  150.  * The return value will be the number of lines actually written.
  151.  * This should be less than the supplied num_lines only in case that
  152.  * the data destination module has requested suspension of the compressor,
  153.  * or if more than image_height scanlines are passed in.
  154.  *
  155.  * Note: we warn about excess calls to jpeg_write_scanlines() since
  156.  * this likely signals an application programmer error.  However,
  157.  * excess scanlines passed in the last valid call are *silently* ignored,
  158.  * so that the application need not adjust num_lines for end-of-image
  159.  * when using a multiple-scanline buffer.
  160.  */
  161.  
  162. GLOBAL JDIMENSION
  163. jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines,
  164.               JDIMENSION num_lines)
  165. {
  166.   JDIMENSION row_ctr, rows_left;
  167.  
  168.   if (cinfo->global_state != CSTATE_SCANNING)
  169.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  170.   if (cinfo->next_scanline >= cinfo->image_height)
  171.     WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  172.  
  173.   /* Call progress monitor hook if present */
  174.   if (cinfo->progress != NULL) {
  175.     cinfo->progress->pass_counter = (long) cinfo->next_scanline;
  176.     cinfo->progress->pass_limit = (long) cinfo->image_height;
  177.     (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  178.   }
  179.  
  180.   /* Give master control module another chance if this is first call to
  181.    * jpeg_write_scanlines.  This lets output of the frame/scan headers be
  182.    * delayed so that application can write COM, etc, markers between
  183.    * jpeg_start_compress and jpeg_write_scanlines.
  184.    */
  185.   if (cinfo->master->call_pass_startup)
  186.     (*cinfo->master->pass_startup) (cinfo);
  187.  
  188.   /* Ignore any extra scanlines at bottom of image. */
  189.   rows_left = cinfo->image_height - cinfo->next_scanline;
  190.   if (num_lines > rows_left)
  191.     num_lines = rows_left;
  192.  
  193.   row_ctr = 0;
  194.   (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);
  195.   cinfo->next_scanline += row_ctr;
  196.   return row_ctr;
  197. }
  198.  
  199.  
  200. /*
  201.  * Alternate entry point to write raw data.
  202.  * Processes exactly one iMCU row per call.
  203.  */
  204.  
  205. GLOBAL JDIMENSION
  206. jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data,
  207.              JDIMENSION num_lines)
  208. {
  209.   JDIMENSION mcu_ctr, lines_per_MCU_row;
  210.  
  211.   if (cinfo->global_state != CSTATE_RAW_OK)
  212.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  213.   if (cinfo->next_scanline >= cinfo->image_height) {
  214.     WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  215.     return 0;
  216.   }
  217.  
  218.   /* Call progress monitor hook if present */
  219.   if (cinfo->progress != NULL) {
  220.     cinfo->progress->pass_counter = (long) cinfo->next_scanline;
  221.     cinfo->progress->pass_limit = (long) cinfo->image_height;
  222.     (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  223.   }
  224.  
  225.   /* Give master control module another chance if this is first call to
  226.    * jpeg_write_raw_data.  This lets output of the frame/scan headers be
  227.    * delayed so that application can write COM, etc, markers between
  228.    * jpeg_start_compress and jpeg_write_raw_data.
  229.    */
  230.   if (cinfo->master->call_pass_startup)
  231.     (*cinfo->master->pass_startup) (cinfo);
  232.  
  233.   /* Verify that at least one iMCU row has been passed. */
  234.   lines_per_MCU_row = cinfo->max_v_samp_factor * DCTSIZE;
  235.   if (num_lines < lines_per_MCU_row)
  236.     ERREXIT(cinfo, JERR_BUFFER_SIZE);
  237.  
  238.   /* Directly compress the row. */
  239.   mcu_ctr = 0;
  240.   (*cinfo->coef->compress_data) (cinfo, data, &mcu_ctr);
  241.   /* If compressor did not consume the whole row, then we must need to
  242.    * suspend processing; this is not currently supported.
  243.    */
  244.   if (mcu_ctr != cinfo->MCUs_per_row)
  245.     ERREXIT(cinfo, JERR_CANT_SUSPEND);
  246.  
  247.   /* OK, we processed one iMCU row. */
  248.   cinfo->next_scanline += lines_per_MCU_row;
  249.   return lines_per_MCU_row;
  250. }
  251.  
  252.  
  253. /*
  254.  * Finish JPEG compression.
  255.  *
  256.  * If a multipass operating mode was selected, this may do a great deal of
  257.  * work including most of the actual output.
  258.  */
  259.  
  260. GLOBAL void
  261. jpeg_finish_compress (j_compress_ptr cinfo)
  262. {
  263.   JDIMENSION iMCU_row, mcu_ctr;
  264.  
  265.   if (cinfo->global_state != CSTATE_SCANNING && 
  266.       cinfo->global_state != CSTATE_RAW_OK)
  267.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  268.   if (cinfo->next_scanline < cinfo->image_height)
  269.     ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
  270.   /* Terminate first pass */
  271.   (*cinfo->master->finish_pass) (cinfo);
  272.   /* Perform any remaining passes */
  273.   while (! cinfo->master->is_last_pass) {
  274.     (*cinfo->master->prepare_for_pass) (cinfo);
  275.     for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
  276.       if (cinfo->progress != NULL) {
  277.     cinfo->progress->pass_counter = (long) iMCU_row;
  278.     cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
  279.     (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  280.       }
  281.       /* We bypass the main controller and invoke coef controller directly;
  282.        * all work is being done from the coefficient buffer.
  283.        */
  284.       mcu_ctr = 0;
  285.       (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL, &mcu_ctr);
  286.       if (mcu_ctr != cinfo->MCUs_per_row)
  287.     ERREXIT(cinfo, JERR_CANT_SUSPEND);
  288.     }
  289.     (*cinfo->master->finish_pass) (cinfo);
  290.   }
  291.   /* Write EOI, do final cleanup */
  292.   (*cinfo->marker->write_file_trailer) (cinfo);
  293.   (*cinfo->dest->term_destination) (cinfo);
  294.   /* We can use jpeg_abort to release memory and reset global_state */
  295.   jpeg_abort((j_common_ptr) cinfo);
  296. }
  297.  
  298.  
  299. /*
  300.  * Write a special marker.
  301.  * This is only recommended for writing COM or APPn markers.
  302.  * Must be called after jpeg_start_compress() and before
  303.  * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
  304.  */
  305.  
  306. GLOBAL void
  307. jpeg_write_marker (j_compress_ptr cinfo, int marker,
  308.            const JOCTET *dataptr, unsigned int datalen)
  309. {
  310.   if (cinfo->next_scanline != 0 ||
  311.       (cinfo->global_state != CSTATE_SCANNING &&
  312.        cinfo->global_state != CSTATE_RAW_OK))
  313.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  314.  
  315.   (*cinfo->marker->write_any_marker) (cinfo, marker, dataptr, datalen);
  316. }
  317.  
  318.  
  319. /*
  320.  * Alternate compression function: just write an abbreviated table file.
  321.  * Before calling this, all parameters and a data destination must be set up.
  322.  *
  323.  * To produce a pair of files containing abbreviated tables and abbreviated
  324.  * image data, one would proceed as follows:
  325.  *
  326.  *        initialize JPEG object
  327.  *        set JPEG parameters
  328.  *        set destination to table file
  329.  *        jpeg_write_tables(cinfo);
  330.  *        set destination to image file
  331.  *        jpeg_start_compress(cinfo, FALSE);
  332.  *        write data...
  333.  *        jpeg_finish_compress(cinfo);
  334.  *
  335.  * jpeg_write_tables has the side effect of marking all tables written
  336.  * (same as jpeg_suppress_tables(..., TRUE)).  Thus a subsequent start_compress
  337.  * will not re-emit the tables unless it is passed write_all_tables=TRUE.
  338.  */
  339.  
  340. GLOBAL void
  341. jpeg_write_tables (j_compress_ptr cinfo)
  342. {
  343.   if (cinfo->global_state != CSTATE_START)
  344.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  345.  
  346.   /* (Re)initialize error mgr and destination modules */
  347.   (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  348.   (*cinfo->dest->init_destination) (cinfo);
  349.   /* Initialize the marker writer ... bit of a crock to do it here. */
  350.   jinit_marker_writer(cinfo);
  351.   /* Write them tables! */
  352.   (*cinfo->marker->write_tables_only) (cinfo);
  353.   /* And clean up. */
  354.   (*cinfo->dest->term_destination) (cinfo);
  355.   /* We can use jpeg_abort to release memory ... is this necessary? */
  356.   jpeg_abort((j_common_ptr) cinfo);
  357. }
  358.  
  359.  
  360. /*
  361.  * Abort processing of a JPEG compression operation,
  362.  * but don't destroy the object itself.
  363.  */
  364.  
  365. GLOBAL void
  366. jpeg_abort_compress (j_compress_ptr cinfo)
  367. {
  368.   jpeg_abort((j_common_ptr) cinfo); /* use common routine */
  369. }
  370.