jdapimin.c
Go to the documentation of this file.
00001 /*
00002  * jdapimin.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 decompression half
00009  * of the JPEG library.  These are the "minimum" API routines that may be
00010  * needed in either the normal full-decompression case or the
00011  * transcoding-only case.
00012  *
00013  * Most of the routines intended to be called directly by an application
00014  * are in this file or in jdapistd.c.  But also see jcomapi.c for routines
00015  * shared by compression and decompression, and jdtrans.c for the transcoding
00016  * case.
00017  */
00018 
00019 #define JPEG_INTERNALS
00020 #include "jinclude.h"
00021 #include "jpeglib.h"
00022 
00023 
00024 /*
00025  * Initialization of a JPEG decompression object.
00026  * The error manager must already be set up (in case memory manager fails).
00027  */
00028 
00029 GLOBAL(void)
00030 jpeg_CreateDecompress (j_decompress_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_decompress_struct))
00039     ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 
00040              (int) SIZEOF(struct jpeg_decompress_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_decompress_struct));
00052     cinfo->err = err;
00053     cinfo->client_data = client_data;
00054   }
00055   cinfo->is_decompressor = TRUE;
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->src = NULL;
00063 
00064   for (i = 0; i < NUM_QUANT_TBLS; i++)
00065     cinfo->quant_tbl_ptrs[i] = NULL;
00066 
00067   for (i = 0; i < NUM_HUFF_TBLS; i++) {
00068     cinfo->dc_huff_tbl_ptrs[i] = NULL;
00069     cinfo->ac_huff_tbl_ptrs[i] = NULL;
00070   }
00071 
00072   /* Initialize marker processor so application can override methods
00073    * for COM, APPn markers before calling jpeg_read_header.
00074    */
00075   cinfo->marker_list = NULL;
00076   jinit_marker_reader(cinfo);
00077 
00078   /* And initialize the overall input controller. */
00079   jinit_input_controller(cinfo);
00080 
00081   /* OK, I'm ready */
00082   cinfo->global_state = DSTATE_START;
00083 }
00084 
00085 
00086 /*
00087  * Destruction of a JPEG decompression object
00088  */
00089 
00090 GLOBAL(void)
00091 jpeg_destroy_decompress (j_decompress_ptr cinfo)
00092 {
00093   jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
00094 }
00095 
00096 
00097 /*
00098  * Abort processing of a JPEG decompression operation,
00099  * but don't destroy the object itself.
00100  */
00101 
00102 GLOBAL(void)
00103 jpeg_abort_decompress (j_decompress_ptr cinfo)
00104 {
00105   jpeg_abort((j_common_ptr) cinfo); /* use common routine */
00106 }
00107 
00108 
00109 /*
00110  * Set default decompression parameters.
00111  */
00112 
00113 LOCAL(void)
00114 default_decompress_parms (j_decompress_ptr cinfo)
00115 {
00116   /* Guess the input colorspace, and set output colorspace accordingly. */
00117   /* (Wish JPEG committee had provided a real way to specify this...) */
00118   /* Note application may override our guesses. */
00119   switch (cinfo->num_components) {
00120   case 1:
00121     cinfo->jpeg_color_space = JCS_GRAYSCALE;
00122     cinfo->out_color_space = JCS_GRAYSCALE;
00123     break;
00124     
00125   case 3:
00126     if (cinfo->saw_JFIF_marker) {
00127       cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
00128     } else if (cinfo->saw_Adobe_marker) {
00129       switch (cinfo->Adobe_transform) {
00130       case 0:
00131         cinfo->jpeg_color_space = JCS_RGB;
00132         break;
00133       case 1:
00134         cinfo->jpeg_color_space = JCS_YCbCr;
00135         break;
00136       default:
00137         WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
00138         cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
00139         break;
00140       }
00141     } else {
00142       /* Saw no special markers, try to guess from the component IDs */
00143       int cid0 = cinfo->comp_info[0].component_id;
00144       int cid1 = cinfo->comp_info[1].component_id;
00145       int cid2 = cinfo->comp_info[2].component_id;
00146 
00147       if (cid0 == 1 && cid1 == 2 && cid2 == 3)
00148         cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
00149       else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
00150         cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
00151       else {
00152         TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
00153         cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
00154       }
00155     }
00156     /* Always guess RGB is proper output colorspace. */
00157     cinfo->out_color_space = JCS_RGB;
00158     break;
00159     
00160   case 4:
00161     if (cinfo->saw_Adobe_marker) {
00162       switch (cinfo->Adobe_transform) {
00163       case 0:
00164         cinfo->jpeg_color_space = JCS_CMYK;
00165         break;
00166       case 2:
00167         cinfo->jpeg_color_space = JCS_YCCK;
00168         break;
00169       default:
00170         WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
00171         cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
00172         break;
00173       }
00174     } else {
00175       /* No special markers, assume straight CMYK. */
00176       cinfo->jpeg_color_space = JCS_CMYK;
00177     }
00178     cinfo->out_color_space = JCS_CMYK;
00179     break;
00180     
00181   default:
00182     cinfo->jpeg_color_space = JCS_UNKNOWN;
00183     cinfo->out_color_space = JCS_UNKNOWN;
00184     break;
00185   }
00186 
00187   /* Set defaults for other decompression parameters. */
00188   cinfo->scale_num = 1;         /* 1:1 scaling */
00189   cinfo->scale_denom = 1;
00190   cinfo->output_gamma = 1.0;
00191   cinfo->buffered_image = FALSE;
00192   cinfo->raw_data_out = FALSE;
00193   cinfo->dct_method = JDCT_DEFAULT;
00194   cinfo->do_fancy_upsampling = TRUE;
00195   cinfo->do_block_smoothing = TRUE;
00196   cinfo->quantize_colors = FALSE;
00197   /* We set these in case application only sets quantize_colors. */
00198   cinfo->dither_mode = JDITHER_FS;
00199 #ifdef QUANT_2PASS_SUPPORTED
00200   cinfo->two_pass_quantize = TRUE;
00201 #else
00202   cinfo->two_pass_quantize = FALSE;
00203 #endif
00204   cinfo->desired_number_of_colors = 256;
00205   cinfo->colormap = NULL;
00206   /* Initialize for no mode change in buffered-image mode. */
00207   cinfo->enable_1pass_quant = FALSE;
00208   cinfo->enable_external_quant = FALSE;
00209   cinfo->enable_2pass_quant = FALSE;
00210 }
00211 
00212 
00213 /*
00214  * Decompression startup: read start of JPEG datastream to see what's there.
00215  * Need only initialize JPEG object and supply a data source before calling.
00216  *
00217  * This routine will read as far as the first SOS marker (ie, actual start of
00218  * compressed data), and will save all tables and parameters in the JPEG
00219  * object.  It will also initialize the decompression parameters to default
00220  * values, and finally return JPEG_HEADER_OK.  On return, the application may
00221  * adjust the decompression parameters and then call jpeg_start_decompress.
00222  * (Or, if the application only wanted to determine the image parameters,
00223  * the data need not be decompressed.  In that case, call jpeg_abort or
00224  * jpeg_destroy to release any temporary space.)
00225  * If an abbreviated (tables only) datastream is presented, the routine will
00226  * return JPEG_HEADER_TABLES_ONLY upon reaching EOI.  The application may then
00227  * re-use the JPEG object to read the abbreviated image datastream(s).
00228  * It is unnecessary (but OK) to call jpeg_abort in this case.
00229  * The JPEG_SUSPENDED return code only occurs if the data source module
00230  * requests suspension of the decompressor.  In this case the application
00231  * should load more source data and then re-call jpeg_read_header to resume
00232  * processing.
00233  * If a non-suspending data source is used and require_image is TRUE, then the
00234  * return code need not be inspected since only JPEG_HEADER_OK is possible.
00235  *
00236  * This routine is now just a front end to jpeg_consume_input, with some
00237  * extra error checking.
00238  */
00239 
00240 GLOBAL(int)
00241 jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
00242 {
00243   int retcode;
00244 
00245   if (cinfo->global_state != DSTATE_START &&
00246       cinfo->global_state != DSTATE_INHEADER)
00247     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00248 
00249   retcode = jpeg_consume_input(cinfo);
00250 
00251   switch (retcode) {
00252   case JPEG_REACHED_SOS:
00253     retcode = JPEG_HEADER_OK;
00254     break;
00255   case JPEG_REACHED_EOI:
00256     if (require_image)          /* Complain if application wanted an image */
00257       ERREXIT(cinfo, JERR_NO_IMAGE);
00258     /* Reset to start state; it would be safer to require the application to
00259      * call jpeg_abort, but we can't change it now for compatibility reasons.
00260      * A side effect is to free any temporary memory (there shouldn't be any).
00261      */
00262     jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
00263     retcode = JPEG_HEADER_TABLES_ONLY;
00264     break;
00265   case JPEG_SUSPENDED:
00266     /* no work */
00267     break;
00268   }
00269 
00270   return retcode;
00271 }
00272 
00273 
00274 /*
00275  * Consume data in advance of what the decompressor requires.
00276  * This can be called at any time once the decompressor object has
00277  * been created and a data source has been set up.
00278  *
00279  * This routine is essentially a state machine that handles a couple
00280  * of critical state-transition actions, namely initial setup and
00281  * transition from header scanning to ready-for-start_decompress.
00282  * All the actual input is done via the input controller's consume_input
00283  * method.
00284  */
00285 
00286 GLOBAL(int)
00287 jpeg_consume_input (j_decompress_ptr cinfo)
00288 {
00289   int retcode = JPEG_SUSPENDED;
00290 
00291   /* NB: every possible DSTATE value should be listed in this switch */
00292   switch (cinfo->global_state) {
00293   case DSTATE_START:
00294     /* Start-of-datastream actions: reset appropriate modules */
00295     (*cinfo->inputctl->reset_input_controller) (cinfo);
00296     /* Initialize application's data source module */
00297     (*cinfo->src->init_source) (cinfo);
00298     cinfo->global_state = DSTATE_INHEADER;
00299     /*FALLTHROUGH*/
00300   case DSTATE_INHEADER:
00301     retcode = (*cinfo->inputctl->consume_input) (cinfo);
00302     if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
00303       /* Set up default parameters based on header data */
00304       default_decompress_parms(cinfo);
00305       /* Set global state: ready for start_decompress */
00306       cinfo->global_state = DSTATE_READY;
00307     }
00308     break;
00309   case DSTATE_READY:
00310     /* Can't advance past first SOS until start_decompress is called */
00311     retcode = JPEG_REACHED_SOS;
00312     break;
00313   case DSTATE_PRELOAD:
00314   case DSTATE_PRESCAN:
00315   case DSTATE_SCANNING:
00316   case DSTATE_RAW_OK:
00317   case DSTATE_BUFIMAGE:
00318   case DSTATE_BUFPOST:
00319   case DSTATE_STOPPING:
00320     retcode = (*cinfo->inputctl->consume_input) (cinfo);
00321     break;
00322   default:
00323     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00324   }
00325   return retcode;
00326 }
00327 
00328 
00329 /*
00330  * Have we finished reading the input file?
00331  */
00332 
00333 GLOBAL(boolean)
00334 jpeg_input_complete (j_decompress_ptr cinfo)
00335 {
00336   /* Check for valid jpeg object */
00337   if (cinfo->global_state < DSTATE_START ||
00338       cinfo->global_state > DSTATE_STOPPING)
00339     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00340   return cinfo->inputctl->eoi_reached;
00341 }
00342 
00343 
00344 /*
00345  * Is there more than one scan?
00346  */
00347 
00348 GLOBAL(boolean)
00349 jpeg_has_multiple_scans (j_decompress_ptr cinfo)
00350 {
00351   /* Only valid after jpeg_read_header completes */
00352   if (cinfo->global_state < DSTATE_READY ||
00353       cinfo->global_state > DSTATE_STOPPING)
00354     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00355   return cinfo->inputctl->has_multiple_scans;
00356 }
00357 
00358 
00359 /*
00360  * Finish JPEG decompression.
00361  *
00362  * This will normally just verify the file trailer and release temp storage.
00363  *
00364  * Returns FALSE if suspended.  The return value need be inspected only if
00365  * a suspending data source is used.
00366  */
00367 
00368 GLOBAL(boolean)
00369 jpeg_finish_decompress (j_decompress_ptr cinfo)
00370 {
00371   if ((cinfo->global_state == DSTATE_SCANNING ||
00372        cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
00373     /* Terminate final pass of non-buffered mode */
00374     if (cinfo->output_scanline < cinfo->output_height)
00375       ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
00376     (*cinfo->master->finish_output_pass) (cinfo);
00377     cinfo->global_state = DSTATE_STOPPING;
00378   } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
00379     /* Finishing after a buffered-image operation */
00380     cinfo->global_state = DSTATE_STOPPING;
00381   } else if (cinfo->global_state != DSTATE_STOPPING) {
00382     /* STOPPING = repeat call after a suspension, anything else is error */
00383     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00384   }
00385   /* Read until EOI */
00386   while (! cinfo->inputctl->eoi_reached) {
00387     if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
00388       return FALSE;             /* Suspend, come back later */
00389   }
00390   /* Do final cleanup */
00391   (*cinfo->src->term_source) (cinfo);
00392   /* We can use jpeg_abort to release memory and reset global_state */
00393   jpeg_abort((j_common_ptr) cinfo);
00394   return TRUE;
00395 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sun Apr 2 2017 03:43:55