jcapimin.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:7k
源码类别:

浏览器

开发平台:

Unix_Linux

  1. /*
  2.  * jcapimin.c
  3.  *
  4.  * Copyright (C) 1994-1995, 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
  9.  * of the JPEG library.  These are the "minimum" API routines that may be
  10.  * needed in either the normal full-compression case or the transcoding-only
  11.  * case.
  12.  *
  13.  * Most of the routines intended to be called directly by an application
  14.  * are in this file or in jcapistd.c.  But also see jcparam.c for
  15.  * parameter-setup helper routines, jcomapi.c for routines shared by
  16.  * compression and decompression, and jctrans.c for the transcoding case.
  17.  */
  18. #define JPEG_INTERNALS
  19. #include "jinclude.h"
  20. #include "jpeglib.h"
  21. /*
  22.  * Initialization of a JPEG compression object.
  23.  * The error manager must already be set up (in case memory manager fails).
  24.  */
  25. GLOBAL void
  26. jpeg_create_compress (j_compress_ptr cinfo)
  27. {
  28.   int i;
  29.   /* For debugging purposes, zero the whole master structure.
  30.    * But error manager pointer is already there, so save and restore it.
  31.    */
  32.   {
  33.     struct jpeg_error_mgr * err = cinfo->err;
  34.     MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
  35.     cinfo->err = err;
  36.   }
  37.   cinfo->is_decompressor = FALSE;
  38.   /* Initialize a memory manager instance for this object */
  39.   jinit_memory_mgr((j_common_ptr) cinfo);
  40.   /* Zero out pointers to permanent structures. */
  41.   cinfo->progress = NULL;
  42.   cinfo->dest = NULL;
  43.   cinfo->comp_info = NULL;
  44.   for (i = 0; i < NUM_QUANT_TBLS; i++)
  45.     cinfo->quant_tbl_ptrs[i] = NULL;
  46.   for (i = 0; i < NUM_HUFF_TBLS; i++) {
  47.     cinfo->dc_huff_tbl_ptrs[i] = NULL;
  48.     cinfo->ac_huff_tbl_ptrs[i] = NULL;
  49.   }
  50.   cinfo->input_gamma = 1.0; /* in case application forgets */
  51.   /* OK, I'm ready */
  52.   cinfo->global_state = CSTATE_START;
  53. }
  54. /*
  55.  * Destruction of a JPEG compression object
  56.  */
  57. GLOBAL void
  58. jpeg_destroy_compress (j_compress_ptr cinfo)
  59. {
  60.   jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
  61. }
  62. /*
  63.  * Abort processing of a JPEG compression operation,
  64.  * but don't destroy the object itself.
  65.  */
  66. GLOBAL void
  67. jpeg_abort_compress (j_compress_ptr cinfo)
  68. {
  69.   jpeg_abort((j_common_ptr) cinfo); /* use common routine */
  70. }
  71. /*
  72.  * Forcibly suppress or un-suppress all quantization and Huffman tables.
  73.  * Marks all currently defined tables as already written (if suppress)
  74.  * or not written (if !suppress).  This will control whether they get emitted
  75.  * by a subsequent jpeg_start_compress call.
  76.  *
  77.  * This routine is exported for use by applications that want to produce
  78.  * abbreviated JPEG datastreams.  It logically belongs in jcparam.c, but
  79.  * since it is called by jpeg_start_compress, we put it here --- otherwise
  80.  * jcparam.o would be linked whether the application used it or not.
  81.  */
  82. GLOBAL void
  83. jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
  84. {
  85.   int i;
  86.   JQUANT_TBL * qtbl;
  87.   JHUFF_TBL * htbl;
  88.   for (i = 0; i < NUM_QUANT_TBLS; i++) {
  89.     if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
  90.       qtbl->sent_table = suppress;
  91.   }
  92.   for (i = 0; i < NUM_HUFF_TBLS; i++) {
  93.     if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
  94.       htbl->sent_table = suppress;
  95.     if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
  96.       htbl->sent_table = suppress;
  97.   }
  98. }
  99. /*
  100.  * Finish JPEG compression.
  101.  *
  102.  * If a multipass operating mode was selected, this may do a great deal of
  103.  * work including most of the actual output.
  104.  */
  105. GLOBAL void
  106. jpeg_finish_compress (j_compress_ptr cinfo)
  107. {
  108.   JDIMENSION iMCU_row;
  109.   if (cinfo->global_state == CSTATE_SCANNING ||
  110.       cinfo->global_state == CSTATE_RAW_OK) {
  111.     /* Terminate first pass */
  112.     if (cinfo->next_scanline < cinfo->image_height)
  113.       ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
  114.     (*cinfo->master->finish_pass) (cinfo);
  115.   } else if (cinfo->global_state != CSTATE_WRCOEFS)
  116.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  117.   /* Perform any remaining passes */
  118.   while (! cinfo->master->is_last_pass) {
  119.     (*cinfo->master->prepare_for_pass) (cinfo);
  120.     for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
  121.       if (cinfo->progress != NULL) {
  122. cinfo->progress->pass_counter = (long) iMCU_row;
  123. cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
  124. (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  125.       }
  126.       /* We bypass the main controller and invoke coef controller directly;
  127.        * all work is being done from the coefficient buffer.
  128.        */
  129.       if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
  130. ERREXIT(cinfo, JERR_CANT_SUSPEND);
  131.     }
  132.     (*cinfo->master->finish_pass) (cinfo);
  133.   }
  134.   /* Write EOI, do final cleanup */
  135.   (*cinfo->marker->write_file_trailer) (cinfo);
  136.   (*cinfo->dest->term_destination) (cinfo);
  137.   /* We can use jpeg_abort to release memory and reset global_state */
  138.   jpeg_abort((j_common_ptr) cinfo);
  139. }
  140. /*
  141.  * Write a special marker.
  142.  * This is only recommended for writing COM or APPn markers.
  143.  * Must be called after jpeg_start_compress() and before
  144.  * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
  145.  */
  146. GLOBAL void
  147. jpeg_write_marker (j_compress_ptr cinfo, int marker,
  148.    const JOCTET *dataptr, unsigned int datalen)
  149. {
  150.   if (cinfo->next_scanline != 0 ||
  151.       (cinfo->global_state != CSTATE_SCANNING &&
  152.        cinfo->global_state != CSTATE_RAW_OK &&
  153.        cinfo->global_state != CSTATE_WRCOEFS))
  154.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  155.   (*cinfo->marker->write_any_marker) (cinfo, marker, dataptr, datalen);
  156. }
  157. /*
  158.  * Alternate compression function: just write an abbreviated table file.
  159.  * Before calling this, all parameters and a data destination must be set up.
  160.  *
  161.  * To produce a pair of files containing abbreviated tables and abbreviated
  162.  * image data, one would proceed as follows:
  163.  *
  164.  * initialize JPEG object
  165.  * set JPEG parameters
  166.  * set destination to table file
  167.  * jpeg_write_tables(cinfo);
  168.  * set destination to image file
  169.  * jpeg_start_compress(cinfo, FALSE);
  170.  * write data...
  171.  * jpeg_finish_compress(cinfo);
  172.  *
  173.  * jpeg_write_tables has the side effect of marking all tables written
  174.  * (same as jpeg_suppress_tables(..., TRUE)).  Thus a subsequent start_compress
  175.  * will not re-emit the tables unless it is passed write_all_tables=TRUE.
  176.  */
  177. GLOBAL void
  178. jpeg_write_tables (j_compress_ptr cinfo)
  179. {
  180.   if (cinfo->global_state != CSTATE_START)
  181.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  182.   /* (Re)initialize error mgr and destination modules */
  183.   (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  184.   (*cinfo->dest->init_destination) (cinfo);
  185.   /* Initialize the marker writer ... bit of a crock to do it here. */
  186.   jinit_marker_writer(cinfo);
  187.   /* Write them tables! */
  188.   (*cinfo->marker->write_tables_only) (cinfo);
  189.   /* And clean up. */
  190.   (*cinfo->dest->term_destination) (cinfo);
  191.   /* We can use jpeg_abort to release memory. */
  192.   jpeg_abort((j_common_ptr) cinfo);
  193. }