Quake-III-Arena

Quake III Arena GPL Source Release
Log | Files | Refs

jcapimin.c (6942B)


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