jdapimin.c

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

Generated on Tue Jul 5 14:13:28 2011 for ROOT_528-00b_version by  doxygen 1.5.1