Home | History | Annotate | Download | only in libjpeg_turbo
      1 /*
      2  * jcapimin.c
      3  *
      4  * Copyright (C) 1994-1998, Thomas G. Lane.
      5  * Modified 2003-2010 by Guido Vollbeding.
      6  * This file is part of the Independent JPEG Group's software.
      7  * For conditions of distribution and use, see the accompanying README file.
      8  *
      9  * This file contains application interface code for the compression half
     10  * of the JPEG library.  These are the "minimum" API routines that may be
     11  * needed in either the normal full-compression case or the transcoding-only
     12  * case.
     13  *
     14  * Most of the routines intended to be called directly by an application
     15  * are in this file or in jcapistd.c.  But also see jcparam.c for
     16  * parameter-setup helper routines, jcomapi.c for routines shared by
     17  * compression and decompression, and jctrans.c for the transcoding case.
     18  */
     19 
     20 #define JPEG_INTERNALS
     21 #include "jinclude.h"
     22 #include "jpeglib.h"
     23 
     24 
     25 /*
     26  * Initialization of a JPEG compression object.
     27  * The error manager must already be set up (in case memory manager fails).
     28  */
     29 
     30 GLOBAL(void)
     31 jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)
     32 {
     33   int i;
     34 
     35   /* Guard against version mismatches between library and caller. */
     36   cinfo->mem = NULL;		/* so jpeg_destroy knows mem mgr not called */
     37   if (version != JPEG_LIB_VERSION)
     38     ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
     39   if (structsize != SIZEOF(struct jpeg_compress_struct))
     40     ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
     41 	     (int) SIZEOF(struct jpeg_compress_struct), (int) structsize);
     42 
     43   /* For debugging purposes, we zero the whole master structure.
     44    * But the application has already set the err pointer, and may have set
     45    * client_data, so we have to save and restore those fields.
     46    * Note: if application hasn't set client_data, tools like Purify may
     47    * complain here.
     48    */
     49   {
     50     struct jpeg_error_mgr * err = cinfo->err;
     51     void * client_data = cinfo->client_data; /* ignore Purify complaint here */
     52     MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
     53     cinfo->err = err;
     54     cinfo->client_data = client_data;
     55   }
     56   cinfo->is_decompressor = FALSE;
     57 
     58   /* Initialize a memory manager instance for this object */
     59   jinit_memory_mgr((j_common_ptr) cinfo);
     60 
     61   /* Zero out pointers to permanent structures. */
     62   cinfo->progress = NULL;
     63   cinfo->dest = NULL;
     64 
     65   cinfo->comp_info = NULL;
     66 
     67   for (i = 0; i < NUM_QUANT_TBLS; i++) {
     68     cinfo->quant_tbl_ptrs[i] = NULL;
     69 #if JPEG_LIB_VERSION >= 70
     70     cinfo->q_scale_factor[i] = 100;
     71 #endif
     72   }
     73 
     74   for (i = 0; i < NUM_HUFF_TBLS; i++) {
     75     cinfo->dc_huff_tbl_ptrs[i] = NULL;
     76     cinfo->ac_huff_tbl_ptrs[i] = NULL;
     77   }
     78 
     79 #if JPEG_LIB_VERSION >= 80
     80   /* Must do it here for emit_dqt in case jpeg_write_tables is used */
     81   cinfo->block_size = DCTSIZE;
     82   cinfo->natural_order = jpeg_natural_order;
     83   cinfo->lim_Se = DCTSIZE2-1;
     84 #endif
     85 
     86   cinfo->script_space = NULL;
     87 
     88   cinfo->input_gamma = 1.0;	/* in case application forgets */
     89 
     90   /* OK, I'm ready */
     91   cinfo->global_state = CSTATE_START;
     92 }
     93 
     94 
     95 /*
     96  * Destruction of a JPEG compression object
     97  */
     98 
     99 GLOBAL(void)
    100 jpeg_destroy_compress (j_compress_ptr cinfo)
    101 {
    102   jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
    103 }
    104 
    105 
    106 /*
    107  * Abort processing of a JPEG compression operation,
    108  * but don't destroy the object itself.
    109  */
    110 
    111 GLOBAL(void)
    112 jpeg_abort_compress (j_compress_ptr cinfo)
    113 {
    114   jpeg_abort((j_common_ptr) cinfo); /* use common routine */
    115 }
    116 
    117 
    118 /*
    119  * Forcibly suppress or un-suppress all quantization and Huffman tables.
    120  * Marks all currently defined tables as already written (if suppress)
    121  * or not written (if !suppress).  This will control whether they get emitted
    122  * by a subsequent jpeg_start_compress call.
    123  *
    124  * This routine is exported for use by applications that want to produce
    125  * abbreviated JPEG datastreams.  It logically belongs in jcparam.c, but
    126  * since it is called by jpeg_start_compress, we put it here --- otherwise
    127  * jcparam.o would be linked whether the application used it or not.
    128  */
    129 
    130 GLOBAL(void)
    131 jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
    132 {
    133   int i;
    134   JQUANT_TBL * qtbl;
    135   JHUFF_TBL * htbl;
    136 
    137   for (i = 0; i < NUM_QUANT_TBLS; i++) {
    138     if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
    139       qtbl->sent_table = suppress;
    140   }
    141 
    142   for (i = 0; i < NUM_HUFF_TBLS; i++) {
    143     if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
    144       htbl->sent_table = suppress;
    145     if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
    146       htbl->sent_table = suppress;
    147   }
    148 }
    149 
    150 
    151 /*
    152  * Finish JPEG compression.
    153  *
    154  * If a multipass operating mode was selected, this may do a great deal of
    155  * work including most of the actual output.
    156  */
    157 
    158 GLOBAL(void)
    159 jpeg_finish_compress (j_compress_ptr cinfo)
    160 {
    161   JDIMENSION iMCU_row;
    162 
    163   if (cinfo->global_state == CSTATE_SCANNING ||
    164       cinfo->global_state == CSTATE_RAW_OK) {
    165     /* Terminate first pass */
    166     if (cinfo->next_scanline < cinfo->image_height)
    167       ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
    168     (*cinfo->master->finish_pass) (cinfo);
    169   } else if (cinfo->global_state != CSTATE_WRCOEFS)
    170     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
    171   /* Perform any remaining passes */
    172   while (! cinfo->master->is_last_pass) {
    173     (*cinfo->master->prepare_for_pass) (cinfo);
    174     for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
    175       if (cinfo->progress != NULL) {
    176 	cinfo->progress->pass_counter = (long) iMCU_row;
    177 	cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
    178 	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
    179       }
    180       /* We bypass the main controller and invoke coef controller directly;
    181        * all work is being done from the coefficient buffer.
    182        */
    183       if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
    184 	ERREXIT(cinfo, JERR_CANT_SUSPEND);
    185     }
    186     (*cinfo->master->finish_pass) (cinfo);
    187   }
    188   /* Write EOI, do final cleanup */
    189   (*cinfo->marker->write_file_trailer) (cinfo);
    190   (*cinfo->dest->term_destination) (cinfo);
    191   /* We can use jpeg_abort to release memory and reset global_state */
    192   jpeg_abort((j_common_ptr) cinfo);
    193 }
    194 
    195 
    196 /*
    197  * Write a special marker.
    198  * This is only recommended for writing COM or APPn markers.
    199  * Must be called after jpeg_start_compress() and before
    200  * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
    201  */
    202 
    203 GLOBAL(void)
    204 jpeg_write_marker (j_compress_ptr cinfo, int marker,
    205 		   const JOCTET *dataptr, unsigned int datalen)
    206 {
    207   JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val));
    208 
    209   if (cinfo->next_scanline != 0 ||
    210       (cinfo->global_state != CSTATE_SCANNING &&
    211        cinfo->global_state != CSTATE_RAW_OK &&
    212        cinfo->global_state != CSTATE_WRCOEFS))
    213     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
    214 
    215   (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
    216   write_marker_byte = cinfo->marker->write_marker_byte;	/* copy for speed */
    217   while (datalen--) {
    218     (*write_marker_byte) (cinfo, *dataptr);
    219     dataptr++;
    220   }
    221 }
    222 
    223 /* Same, but piecemeal. */
    224 
    225 GLOBAL(void)
    226 jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
    227 {
    228   if (cinfo->next_scanline != 0 ||
    229       (cinfo->global_state != CSTATE_SCANNING &&
    230        cinfo->global_state != CSTATE_RAW_OK &&
    231        cinfo->global_state != CSTATE_WRCOEFS))
    232     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
    233 
    234   (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
    235 }
    236 
    237 GLOBAL(void)
    238 jpeg_write_m_byte (j_compress_ptr cinfo, int val)
    239 {
    240   (*cinfo->marker->write_marker_byte) (cinfo, val);
    241 }
    242 
    243 
    244 /*
    245  * Alternate compression function: just write an abbreviated table file.
    246  * Before calling this, all parameters and a data destination must be set up.
    247  *
    248  * To produce a pair of files containing abbreviated tables and abbreviated
    249  * image data, one would proceed as follows:
    250  *
    251  *		initialize JPEG object
    252  *		set JPEG parameters
    253  *		set destination to table file
    254  *		jpeg_write_tables(cinfo);
    255  *		set destination to image file
    256  *		jpeg_start_compress(cinfo, FALSE);
    257  *		write data...
    258  *		jpeg_finish_compress(cinfo);
    259  *
    260  * jpeg_write_tables has the side effect of marking all tables written
    261  * (same as jpeg_suppress_tables(..., TRUE)).  Thus a subsequent start_compress
    262  * will not re-emit the tables unless it is passed write_all_tables=TRUE.
    263  */
    264 
    265 GLOBAL(void)
    266 jpeg_write_tables (j_compress_ptr cinfo)
    267 {
    268   if (cinfo->global_state != CSTATE_START)
    269     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
    270 
    271   /* (Re)initialize error mgr and destination modules */
    272   (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
    273   (*cinfo->dest->init_destination) (cinfo);
    274   /* Initialize the marker writer ... bit of a crock to do it here. */
    275   jinit_marker_writer(cinfo);
    276   /* Write them tables! */
    277   (*cinfo->marker->write_tables_only) (cinfo);
    278   /* And clean up. */
    279   (*cinfo->dest->term_destination) (cinfo);
    280   /*
    281    * In library releases up through v6a, we called jpeg_abort() here to free
    282    * any working memory allocated by the destination manager and marker
    283    * writer.  Some applications had a problem with that: they allocated space
    284    * of their own from the library memory manager, and didn't want it to go
    285    * away during write_tables.  So now we do nothing.  This will cause a
    286    * memory leak if an app calls write_tables repeatedly without doing a full
    287    * compression cycle or otherwise resetting the JPEG object.  However, that
    288    * seems less bad than unexpectedly freeing memory in the normal case.
    289    * An app that prefers the old behavior can call jpeg_abort for itself after
    290    * each call to jpeg_write_tables().
    291    */
    292 }
    293