Shoaib Ahmed / Mbed 2 deprecated uzairkhan

Dependencies:   uzair Camera_LS_Y201 F7_Ethernet LCD_DISCO_F746NG NetworkAPI SDFileSystem mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers jdapimin.c Source File

jdapimin.c

00001 /*
00002  * jdapimin.c
00003  *
00004  * Copyright (C) 1994-1998, Thomas G. Lane.
00005  * Modified 2009-2013 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   int cid0, cid1, cid2;
00118 
00119   /* Guess the input colorspace, and set output colorspace accordingly. */
00120   /* Note application may override our guesses. */
00121   switch (cinfo->num_components) {
00122   case 1:
00123     cinfo->jpeg_color_space = JCS_GRAYSCALE;
00124     cinfo->out_color_space = JCS_GRAYSCALE;
00125     break;
00126     
00127   case 3:
00128     cid0 = cinfo->comp_info[0].component_id;
00129     cid1 = cinfo->comp_info[1].component_id;
00130     cid2 = cinfo->comp_info[2].component_id;
00131 
00132     /* First try to guess from the component IDs */
00133     if      (cid0 == 0x01 && cid1 == 0x02 && cid2 == 0x03)
00134       cinfo->jpeg_color_space = JCS_YCbCr;
00135     else if (cid0 == 0x01 && cid1 == 0x22 && cid2 == 0x23)
00136       cinfo->jpeg_color_space = JCS_BG_YCC;
00137     else if (cid0 == 0x52 && cid1 == 0x47 && cid2 == 0x42)
00138       cinfo->jpeg_color_space = JCS_RGB;    /* ASCII 'R', 'G', 'B' */
00139     else if (cid0 == 0x72 && cid1 == 0x67 && cid2 == 0x62)
00140       cinfo->jpeg_color_space = JCS_BG_RGB; /* ASCII 'r', 'g', 'b' */
00141     else if (cinfo->saw_JFIF_marker)
00142       cinfo->jpeg_color_space = JCS_YCbCr;  /* assume it's YCbCr */
00143     else if (cinfo->saw_Adobe_marker) {
00144       switch (cinfo->Adobe_transform) {
00145       case 0:
00146     cinfo->jpeg_color_space = JCS_RGB;
00147     break;
00148       case 1:
00149     cinfo->jpeg_color_space = JCS_YCbCr;
00150     break;
00151       default:
00152     WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
00153     cinfo->jpeg_color_space = JCS_YCbCr;    /* assume it's YCbCr */
00154     break;
00155       }
00156     } else {
00157       TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
00158       cinfo->jpeg_color_space = JCS_YCbCr;  /* assume it's YCbCr */
00159     }
00160     /* Always guess RGB is proper output colorspace. */
00161     cinfo->out_color_space = JCS_RGB;
00162     break;
00163     
00164   case 4:
00165     if (cinfo->saw_Adobe_marker) {
00166       switch (cinfo->Adobe_transform) {
00167       case 0:
00168     cinfo->jpeg_color_space = JCS_CMYK;
00169     break;
00170       case 2:
00171     cinfo->jpeg_color_space = JCS_YCCK;
00172     break;
00173       default:
00174     WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
00175     cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
00176     break;
00177       }
00178     } else {
00179       /* No special markers, assume straight CMYK. */
00180       cinfo->jpeg_color_space = JCS_CMYK;
00181     }
00182     cinfo->out_color_space = JCS_CMYK;
00183     break;
00184     
00185   default:
00186     cinfo->jpeg_color_space = JCS_UNKNOWN;
00187     cinfo->out_color_space = JCS_UNKNOWN;
00188     break;
00189   }
00190 
00191   /* Set defaults for other decompression parameters. */
00192   cinfo->scale_num = cinfo->block_size;     /* 1:1 scaling */
00193   cinfo->scale_denom = cinfo->block_size;
00194   cinfo->output_gamma = 1.0;
00195   cinfo->buffered_image = FALSE;
00196   cinfo->raw_data_out = FALSE;
00197   cinfo->dct_method = JDCT_DEFAULT;
00198   cinfo->do_fancy_upsampling = TRUE;
00199   cinfo->do_block_smoothing = TRUE;
00200   cinfo->quantize_colors = FALSE;
00201   /* We set these in case application only sets quantize_colors. */
00202   cinfo->dither_mode = JDITHER_FS;
00203 #ifdef QUANT_2PASS_SUPPORTED
00204   cinfo->two_pass_quantize = TRUE;
00205 #else
00206   cinfo->two_pass_quantize = FALSE;
00207 #endif
00208   cinfo->desired_number_of_colors = 256;
00209   cinfo->colormap = NULL;
00210   /* Initialize for no mode change in buffered-image mode. */
00211   cinfo->enable_1pass_quant = FALSE;
00212   cinfo->enable_external_quant = FALSE;
00213   cinfo->enable_2pass_quant = FALSE;
00214 }
00215 
00216 
00217 /*
00218  * Decompression startup: read start of JPEG datastream to see what's there.
00219  * Need only initialize JPEG object and supply a data source before calling.
00220  *
00221  * This routine will read as far as the first SOS marker (ie, actual start of
00222  * compressed data), and will save all tables and parameters in the JPEG
00223  * object.  It will also initialize the decompression parameters to default
00224  * values, and finally return JPEG_HEADER_OK.  On return, the application may
00225  * adjust the decompression parameters and then call jpeg_start_decompress.
00226  * (Or, if the application only wanted to determine the image parameters,
00227  * the data need not be decompressed.  In that case, call jpeg_abort or
00228  * jpeg_destroy to release any temporary space.)
00229  * If an abbreviated (tables only) datastream is presented, the routine will
00230  * return JPEG_HEADER_TABLES_ONLY upon reaching EOI.  The application may then
00231  * re-use the JPEG object to read the abbreviated image datastream(s).
00232  * It is unnecessary (but OK) to call jpeg_abort in this case.
00233  * The JPEG_SUSPENDED return code only occurs if the data source module
00234  * requests suspension of the decompressor.  In this case the application
00235  * should load more source data and then re-call jpeg_read_header to resume
00236  * processing.
00237  * If a non-suspending data source is used and require_image is TRUE, then the
00238  * return code need not be inspected since only JPEG_HEADER_OK is possible.
00239  *
00240  * This routine is now just a front end to jpeg_consume_input, with some
00241  * extra error checking.
00242  */
00243 
00244 GLOBAL(int)
00245 jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
00246 {
00247   int retcode;
00248 
00249   if (cinfo->global_state != DSTATE_START &&
00250       cinfo->global_state != DSTATE_INHEADER)
00251     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00252 
00253   retcode = jpeg_consume_input(cinfo);
00254 
00255   switch (retcode) {
00256   case JPEG_REACHED_SOS:
00257     retcode = JPEG_HEADER_OK;
00258     break;
00259   case JPEG_REACHED_EOI:
00260     if (require_image)      /* Complain if application wanted an image */
00261       ERREXIT(cinfo, JERR_NO_IMAGE);
00262     /* Reset to start state; it would be safer to require the application to
00263      * call jpeg_abort, but we can't change it now for compatibility reasons.
00264      * A side effect is to free any temporary memory (there shouldn't be any).
00265      */
00266     jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
00267     retcode = JPEG_HEADER_TABLES_ONLY;
00268     break;
00269   case JPEG_SUSPENDED:
00270     /* no work */
00271     break;
00272   }
00273 
00274   return retcode;
00275 }
00276 
00277 
00278 /*
00279  * Consume data in advance of what the decompressor requires.
00280  * This can be called at any time once the decompressor object has
00281  * been created and a data source has been set up.
00282  *
00283  * This routine is essentially a state machine that handles a couple
00284  * of critical state-transition actions, namely initial setup and
00285  * transition from header scanning to ready-for-start_decompress.
00286  * All the actual input is done via the input controller's consume_input
00287  * method.
00288  */
00289 
00290 GLOBAL(int)
00291 jpeg_consume_input (j_decompress_ptr cinfo)
00292 {
00293   int retcode = JPEG_SUSPENDED;
00294 
00295   /* NB: every possible DSTATE value should be listed in this switch */
00296   switch (cinfo->global_state) {
00297   case DSTATE_START:
00298     /* Start-of-datastream actions: reset appropriate modules */
00299     (*cinfo->inputctl->reset_input_controller) (cinfo);
00300     /* Initialize application's data source module */
00301     (*cinfo->src->init_source) (cinfo);
00302     cinfo->global_state = DSTATE_INHEADER;
00303     /*FALLTHROUGH*/
00304   case DSTATE_INHEADER:
00305     retcode = (*cinfo->inputctl->consume_input) (cinfo);
00306     if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
00307       /* Set up default parameters based on header data */
00308       default_decompress_parms(cinfo);
00309       /* Set global state: ready for start_decompress */
00310       cinfo->global_state = DSTATE_READY;
00311     }
00312     break;
00313   case DSTATE_READY:
00314     /* Can't advance past first SOS until start_decompress is called */
00315     retcode = JPEG_REACHED_SOS;
00316     break;
00317   case DSTATE_PRELOAD:
00318   case DSTATE_PRESCAN:
00319   case DSTATE_SCANNING:
00320   case DSTATE_RAW_OK:
00321   case DSTATE_BUFIMAGE:
00322   case DSTATE_BUFPOST:
00323   case DSTATE_STOPPING:
00324     retcode = (*cinfo->inputctl->consume_input) (cinfo);
00325     break;
00326   default:
00327     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00328   }
00329   return retcode;
00330 }
00331 
00332 
00333 /*
00334  * Have we finished reading the input file?
00335  */
00336 
00337 GLOBAL(boolean)
00338 jpeg_input_complete (j_decompress_ptr cinfo)
00339 {
00340   /* Check for valid jpeg object */
00341   if (cinfo->global_state < DSTATE_START ||
00342       cinfo->global_state > DSTATE_STOPPING)
00343     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00344   return cinfo->inputctl->eoi_reached;
00345 }
00346 
00347 
00348 /*
00349  * Is there more than one scan?
00350  */
00351 
00352 GLOBAL(boolean)
00353 jpeg_has_multiple_scans (j_decompress_ptr cinfo)
00354 {
00355   /* Only valid after jpeg_read_header completes */
00356   if (cinfo->global_state < DSTATE_READY ||
00357       cinfo->global_state > DSTATE_STOPPING)
00358     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00359   return cinfo->inputctl->has_multiple_scans;
00360 }
00361 
00362 
00363 /*
00364  * Finish JPEG decompression.
00365  *
00366  * This will normally just verify the file trailer and release temp storage.
00367  *
00368  * Returns FALSE if suspended.  The return value need be inspected only if
00369  * a suspending data source is used.
00370  */
00371 
00372 GLOBAL(boolean)
00373 jpeg_finish_decompress (j_decompress_ptr cinfo)
00374 {
00375   if ((cinfo->global_state == DSTATE_SCANNING ||
00376        cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
00377     /* Terminate final pass of non-buffered mode */
00378     if (cinfo->output_scanline < cinfo->output_height)
00379       ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
00380     (*cinfo->master->finish_output_pass) (cinfo);
00381     cinfo->global_state = DSTATE_STOPPING;
00382   } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
00383     /* Finishing after a buffered-image operation */
00384     cinfo->global_state = DSTATE_STOPPING;
00385   } else if (cinfo->global_state != DSTATE_STOPPING) {
00386     /* STOPPING = repeat call after a suspension, anything else is error */
00387     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00388   }
00389   /* Read until EOI */
00390   while (! cinfo->inputctl->eoi_reached) {
00391     if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
00392       return FALSE;     /* Suspend, come back later */
00393   }
00394   /* Do final cleanup */
00395   (*cinfo->src->term_source) (cinfo);
00396   /* We can use jpeg_abort to release memory and reset global_state */
00397   jpeg_abort((j_common_ptr) cinfo);
00398   return TRUE;
00399 }