Back to index

plt-scheme  4.2.1
jcapimin.c
Go to the documentation of this file.
00001 /*
00002  * jcapimin.c
00003  *
00004  * Copyright (C) 1994-1998, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains application interface code for the compression half
00009  * of the JPEG library.  These are the "minimum" API routines that may be
00010  * needed in either the normal full-compression case or the transcoding-only
00011  * case.
00012  *
00013  * Most of the routines intended to be called directly by an application
00014  * are in this file or in jcapistd.c.  But also see jcparam.c for
00015  * parameter-setup helper routines, jcomapi.c for routines shared by
00016  * compression and decompression, and jctrans.c for the transcoding case.
00017  */
00018 
00019 #define JPEG_INTERNALS
00020 #include "jinclude.h"
00021 #include "jpeglib.h"
00022 
00023 
00024 /*
00025  * Initialization of a JPEG compression object.
00026  * The error manager must already be set up (in case memory manager fails).
00027  */
00028 
00029 GLOBAL(void)
00030 jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)
00031 {
00032   int i;
00033 
00034   /* Guard against version mismatches between library and caller. */
00035   cinfo->mem = NULL;        /* so jpeg_destroy knows mem mgr not called */
00036   if (version != JPEG_LIB_VERSION)
00037     ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
00038   if (structsize != SIZEOF(struct jpeg_compress_struct))
00039     ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 
00040             (int) SIZEOF(struct jpeg_compress_struct), (int) structsize);
00041 
00042   /* For debugging purposes, we zero the whole master structure.
00043    * But the application has already set the err pointer, and may have set
00044    * client_data, so we have to save and restore those fields.
00045    * Note: if application hasn't set client_data, tools like Purify may
00046    * complain here.
00047    */
00048   {
00049     struct jpeg_error_mgr * err = cinfo->err;
00050     void * client_data = cinfo->client_data; /* ignore Purify complaint here */
00051     MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
00052     cinfo->err = err;
00053     cinfo->client_data = client_data;
00054   }
00055   cinfo->is_decompressor = FALSE;
00056 
00057   /* Initialize a memory manager instance for this object */
00058   jinit_memory_mgr((j_common_ptr) cinfo);
00059 
00060   /* Zero out pointers to permanent structures. */
00061   cinfo->progress = NULL;
00062   cinfo->dest = NULL;
00063 
00064   cinfo->comp_info = NULL;
00065 
00066   for (i = 0; i < NUM_QUANT_TBLS; i++)
00067     cinfo->quant_tbl_ptrs[i] = NULL;
00068 
00069   for (i = 0; i < NUM_HUFF_TBLS; i++) {
00070     cinfo->dc_huff_tbl_ptrs[i] = NULL;
00071     cinfo->ac_huff_tbl_ptrs[i] = NULL;
00072   }
00073 
00074   cinfo->script_space = NULL;
00075 
00076   cinfo->input_gamma = 1.0; /* in case application forgets */
00077 
00078   /* OK, I'm ready */
00079   cinfo->global_state = CSTATE_START;
00080 }
00081 
00082 
00083 /*
00084  * Destruction of a JPEG compression object
00085  */
00086 
00087 GLOBAL(void)
00088 jpeg_destroy_compress (j_compress_ptr cinfo)
00089 {
00090   jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
00091 }
00092 
00093 
00094 /*
00095  * Abort processing of a JPEG compression operation,
00096  * but don't destroy the object itself.
00097  */
00098 
00099 GLOBAL(void)
00100 jpeg_abort_compress (j_compress_ptr cinfo)
00101 {
00102   jpeg_abort((j_common_ptr) cinfo); /* use common routine */
00103 }
00104 
00105 
00106 /*
00107  * Forcibly suppress or un-suppress all quantization and Huffman tables.
00108  * Marks all currently defined tables as already written (if suppress)
00109  * or not written (if !suppress).  This will control whether they get emitted
00110  * by a subsequent jpeg_start_compress call.
00111  *
00112  * This routine is exported for use by applications that want to produce
00113  * abbreviated JPEG datastreams.  It logically belongs in jcparam.c, but
00114  * since it is called by jpeg_start_compress, we put it here --- otherwise
00115  * jcparam.o would be linked whether the application used it or not.
00116  */
00117 
00118 GLOBAL(void)
00119 jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
00120 {
00121   int i;
00122   JQUANT_TBL * qtbl;
00123   JHUFF_TBL * htbl;
00124 
00125   for (i = 0; i < NUM_QUANT_TBLS; i++) {
00126     if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
00127       qtbl->sent_table = suppress;
00128   }
00129 
00130   for (i = 0; i < NUM_HUFF_TBLS; i++) {
00131     if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
00132       htbl->sent_table = suppress;
00133     if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
00134       htbl->sent_table = suppress;
00135   }
00136 }
00137 
00138 
00139 /*
00140  * Finish JPEG compression.
00141  *
00142  * If a multipass operating mode was selected, this may do a great deal of
00143  * work including most of the actual output.
00144  */
00145 
00146 GLOBAL(void)
00147 jpeg_finish_compress (j_compress_ptr cinfo)
00148 {
00149   JDIMENSION iMCU_row;
00150 
00151   if (cinfo->global_state == CSTATE_SCANNING ||
00152       cinfo->global_state == CSTATE_RAW_OK) {
00153     /* Terminate first pass */
00154     if (cinfo->next_scanline < cinfo->image_height)
00155       ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
00156     (*cinfo->master->finish_pass) (cinfo);
00157   } else if (cinfo->global_state != CSTATE_WRCOEFS)
00158     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00159   /* Perform any remaining passes */
00160   while (! cinfo->master->is_last_pass) {
00161     (*cinfo->master->prepare_for_pass) (cinfo);
00162     for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
00163       if (cinfo->progress != NULL) {
00164        cinfo->progress->pass_counter = (long) iMCU_row;
00165        cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
00166        (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
00167       }
00168       /* We bypass the main controller and invoke coef controller directly;
00169        * all work is being done from the coefficient buffer.
00170        */
00171       if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
00172        ERREXIT(cinfo, JERR_CANT_SUSPEND);
00173     }
00174     (*cinfo->master->finish_pass) (cinfo);
00175   }
00176   /* Write EOI, do final cleanup */
00177   (*cinfo->marker->write_file_trailer) (cinfo);
00178   (*cinfo->dest->term_destination) (cinfo);
00179   /* We can use jpeg_abort to release memory and reset global_state */
00180   jpeg_abort((j_common_ptr) cinfo);
00181 }
00182 
00183 
00184 /*
00185  * Write a special marker.
00186  * This is only recommended for writing COM or APPn markers.
00187  * Must be called after jpeg_start_compress() and before
00188  * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
00189  */
00190 
00191 GLOBAL(void)
00192 jpeg_write_marker (j_compress_ptr cinfo, int marker,
00193                  const JOCTET *dataptr, unsigned int datalen)
00194 {
00195   JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val));
00196 
00197   if (cinfo->next_scanline != 0 ||
00198       (cinfo->global_state != CSTATE_SCANNING &&
00199        cinfo->global_state != CSTATE_RAW_OK &&
00200        cinfo->global_state != CSTATE_WRCOEFS))
00201     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00202 
00203   (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
00204   write_marker_byte = cinfo->marker->write_marker_byte; /* copy for speed */
00205   while (datalen--) {
00206     (*write_marker_byte) (cinfo, *dataptr);
00207     dataptr++;
00208   }
00209 }
00210 
00211 /* Same, but piecemeal. */
00212 
00213 GLOBAL(void)
00214 jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
00215 {
00216   if (cinfo->next_scanline != 0 ||
00217       (cinfo->global_state != CSTATE_SCANNING &&
00218        cinfo->global_state != CSTATE_RAW_OK &&
00219        cinfo->global_state != CSTATE_WRCOEFS))
00220     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00221 
00222   (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
00223 }
00224 
00225 GLOBAL(void)
00226 jpeg_write_m_byte (j_compress_ptr cinfo, int val)
00227 {
00228   (*cinfo->marker->write_marker_byte) (cinfo, val);
00229 }
00230 
00231 
00232 /*
00233  * Alternate compression function: just write an abbreviated table file.
00234  * Before calling this, all parameters and a data destination must be set up.
00235  *
00236  * To produce a pair of files containing abbreviated tables and abbreviated
00237  * image data, one would proceed as follows:
00238  *
00239  *            initialize JPEG object
00240  *            set JPEG parameters
00241  *            set destination to table file
00242  *            jpeg_write_tables(cinfo);
00243  *            set destination to image file
00244  *            jpeg_start_compress(cinfo, FALSE);
00245  *            write data...
00246  *            jpeg_finish_compress(cinfo);
00247  *
00248  * jpeg_write_tables has the side effect of marking all tables written
00249  * (same as jpeg_suppress_tables(..., TRUE)).  Thus a subsequent start_compress
00250  * will not re-emit the tables unless it is passed write_all_tables=TRUE.
00251  */
00252 
00253 GLOBAL(void)
00254 jpeg_write_tables (j_compress_ptr cinfo)
00255 {
00256   if (cinfo->global_state != CSTATE_START)
00257     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00258 
00259   /* (Re)initialize error mgr and destination modules */
00260   (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
00261   (*cinfo->dest->init_destination) (cinfo);
00262   /* Initialize the marker writer ... bit of a crock to do it here. */
00263   jinit_marker_writer(cinfo);
00264   /* Write them tables! */
00265   (*cinfo->marker->write_tables_only) (cinfo);
00266   /* And clean up. */
00267   (*cinfo->dest->term_destination) (cinfo);
00268   /*
00269    * In library releases up through v6a, we called jpeg_abort() here to free
00270    * any working memory allocated by the destination manager and marker
00271    * writer.  Some applications had a problem with that: they allocated space
00272    * of their own from the library memory manager, and didn't want it to go
00273    * away during write_tables.  So now we do nothing.  This will cause a
00274    * memory leak if an app calls write_tables repeatedly without doing a full
00275    * compression cycle or otherwise resetting the JPEG object.  However, that
00276    * seems less bad than unexpectedly freeing memory in the normal case.
00277    * An app that prefers the old behavior can call jpeg_abort for itself after
00278    * each call to jpeg_write_tables().
00279    */
00280 }