LCOV - code coverage report
Current view: directory - media/libjpeg - jdapimin.c (source / functions) Found Hit Coverage
Test: app.info Lines: 149 85 57.0 %
Date: 2012-06-02 Functions: 9 7 77.8 %

       1                 : /*
       2                 :  * jdapimin.c
       3                 :  *
       4                 :  * Copyright (C) 1994-1998, 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 decompression half
       9                 :  * of the JPEG library.  These are the "minimum" API routines that may be
      10                 :  * needed in either the normal full-decompression case or the
      11                 :  * transcoding-only case.
      12                 :  *
      13                 :  * Most of the routines intended to be called directly by an application
      14                 :  * are in this file or in jdapistd.c.  But also see jcomapi.c for routines
      15                 :  * shared by compression and decompression, and jdtrans.c for the transcoding
      16                 :  * case.
      17                 :  */
      18                 : 
      19                 : #define JPEG_INTERNALS
      20                 : #include "jinclude.h"
      21                 : #include "jpeglib.h"
      22                 : 
      23                 : 
      24                 : /*
      25                 :  * Initialization of a JPEG decompression object.
      26                 :  * The error manager must already be set up (in case memory manager fails).
      27                 :  */
      28                 : 
      29                 : GLOBAL(void)
      30               5 : jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
      31                 : {
      32                 :   int i;
      33                 : 
      34                 :   /* Guard against version mismatches between library and caller. */
      35               5 :   cinfo->mem = NULL;         /* so jpeg_destroy knows mem mgr not called */
      36               5 :   if (version != JPEG_LIB_VERSION)
      37               0 :     ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
      38               5 :   if (structsize != SIZEOF(struct jpeg_decompress_struct))
      39               0 :     ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 
      40                 :              (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);
      41                 : 
      42                 :   /* For debugging purposes, we zero the whole master structure.
      43                 :    * But the application has already set the err pointer, and may have set
      44                 :    * client_data, so we have to save and restore those fields.
      45                 :    * Note: if application hasn't set client_data, tools like Purify may
      46                 :    * complain here.
      47                 :    */
      48                 :   {
      49               5 :     struct jpeg_error_mgr * err = cinfo->err;
      50               5 :     void * client_data = cinfo->client_data; /* ignore Purify complaint here */
      51               5 :     MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
      52               5 :     cinfo->err = err;
      53               5 :     cinfo->client_data = client_data;
      54                 :   }
      55               5 :   cinfo->is_decompressor = TRUE;
      56                 : 
      57                 :   /* Initialize a memory manager instance for this object */
      58               5 :   jinit_memory_mgr((j_common_ptr) cinfo);
      59                 : 
      60                 :   /* Zero out pointers to permanent structures. */
      61               5 :   cinfo->progress = NULL;
      62               5 :   cinfo->src = NULL;
      63                 : 
      64              25 :   for (i = 0; i < NUM_QUANT_TBLS; i++)
      65              20 :     cinfo->quant_tbl_ptrs[i] = NULL;
      66                 : 
      67              25 :   for (i = 0; i < NUM_HUFF_TBLS; i++) {
      68              20 :     cinfo->dc_huff_tbl_ptrs[i] = NULL;
      69              20 :     cinfo->ac_huff_tbl_ptrs[i] = NULL;
      70                 :   }
      71                 : 
      72                 :   /* Initialize marker processor so application can override methods
      73                 :    * for COM, APPn markers before calling jpeg_read_header.
      74                 :    */
      75               5 :   cinfo->marker_list = NULL;
      76               5 :   jinit_marker_reader(cinfo);
      77                 : 
      78                 :   /* And initialize the overall input controller. */
      79               5 :   jinit_input_controller(cinfo);
      80                 : 
      81                 :   /* OK, I'm ready */
      82               5 :   cinfo->global_state = DSTATE_START;
      83               5 : }
      84                 : 
      85                 : 
      86                 : /*
      87                 :  * Destruction of a JPEG decompression object
      88                 :  */
      89                 : 
      90                 : GLOBAL(void)
      91               5 : jpeg_destroy_decompress (j_decompress_ptr cinfo)
      92                 : {
      93               5 :   jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
      94               5 : }
      95                 : 
      96                 : 
      97                 : /*
      98                 :  * Abort processing of a JPEG decompression operation,
      99                 :  * but don't destroy the object itself.
     100                 :  */
     101                 : 
     102                 : GLOBAL(void)
     103               0 : jpeg_abort_decompress (j_decompress_ptr cinfo)
     104                 : {
     105               0 :   jpeg_abort((j_common_ptr) cinfo); /* use common routine */
     106               0 : }
     107                 : 
     108                 : 
     109                 : /*
     110                 :  * Set default decompression parameters.
     111                 :  */
     112                 : 
     113                 : LOCAL(void)
     114               5 : default_decompress_parms (j_decompress_ptr cinfo)
     115                 : {
     116                 :   /* Guess the input colorspace, and set output colorspace accordingly. */
     117                 :   /* (Wish JPEG committee had provided a real way to specify this...) */
     118                 :   /* Note application may override our guesses. */
     119               5 :   switch (cinfo->num_components) {
     120                 :   case 1:
     121               0 :     cinfo->jpeg_color_space = JCS_GRAYSCALE;
     122               0 :     cinfo->out_color_space = JCS_GRAYSCALE;
     123               0 :     break;
     124                 :     
     125                 :   case 3:
     126               5 :     if (cinfo->saw_JFIF_marker) {
     127               5 :       cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
     128               0 :     } else if (cinfo->saw_Adobe_marker) {
     129               0 :       switch (cinfo->Adobe_transform) {
     130                 :       case 0:
     131               0 :         cinfo->jpeg_color_space = JCS_RGB;
     132               0 :         break;
     133                 :       case 1:
     134               0 :         cinfo->jpeg_color_space = JCS_YCbCr;
     135               0 :         break;
     136                 :       default:
     137               0 :         WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
     138               0 :         cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
     139               0 :         break;
     140                 :       }
     141                 :     } else {
     142                 :       /* Saw no special markers, try to guess from the component IDs */
     143               0 :       int cid0 = cinfo->comp_info[0].component_id;
     144               0 :       int cid1 = cinfo->comp_info[1].component_id;
     145               0 :       int cid2 = cinfo->comp_info[2].component_id;
     146                 : 
     147               0 :       if (cid0 == 1 && cid1 == 2 && cid2 == 3)
     148               0 :         cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
     149               0 :       else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
     150               0 :         cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
     151                 :       else {
     152               0 :         TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
     153               0 :         cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
     154                 :       }
     155                 :     }
     156                 :     /* Always guess RGB is proper output colorspace. */
     157               5 :     cinfo->out_color_space = JCS_RGB;
     158               5 :     break;
     159                 :     
     160                 :   case 4:
     161               0 :     if (cinfo->saw_Adobe_marker) {
     162               0 :       switch (cinfo->Adobe_transform) {
     163                 :       case 0:
     164               0 :         cinfo->jpeg_color_space = JCS_CMYK;
     165               0 :         break;
     166                 :       case 2:
     167               0 :         cinfo->jpeg_color_space = JCS_YCCK;
     168               0 :         break;
     169                 :       default:
     170               0 :         WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
     171               0 :         cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
     172               0 :         break;
     173                 :       }
     174                 :     } else {
     175                 :       /* No special markers, assume straight CMYK. */
     176               0 :       cinfo->jpeg_color_space = JCS_CMYK;
     177                 :     }
     178               0 :     cinfo->out_color_space = JCS_CMYK;
     179               0 :     break;
     180                 :     
     181                 :   default:
     182               0 :     cinfo->jpeg_color_space = JCS_UNKNOWN;
     183               0 :     cinfo->out_color_space = JCS_UNKNOWN;
     184               0 :     break;
     185                 :   }
     186                 : 
     187                 :   /* Set defaults for other decompression parameters. */
     188               5 :   cinfo->scale_num = 1;              /* 1:1 scaling */
     189               5 :   cinfo->scale_denom = 1;
     190               5 :   cinfo->output_gamma = 1.0;
     191               5 :   cinfo->buffered_image = FALSE;
     192               5 :   cinfo->raw_data_out = FALSE;
     193               5 :   cinfo->dct_method = JDCT_DEFAULT;
     194               5 :   cinfo->do_fancy_upsampling = TRUE;
     195               5 :   cinfo->do_block_smoothing = TRUE;
     196               5 :   cinfo->quantize_colors = FALSE;
     197                 :   /* We set these in case application only sets quantize_colors. */
     198               5 :   cinfo->dither_mode = JDITHER_FS;
     199                 : #ifdef QUANT_2PASS_SUPPORTED
     200               5 :   cinfo->two_pass_quantize = TRUE;
     201                 : #else
     202                 :   cinfo->two_pass_quantize = FALSE;
     203                 : #endif
     204               5 :   cinfo->desired_number_of_colors = 256;
     205               5 :   cinfo->colormap = NULL;
     206                 :   /* Initialize for no mode change in buffered-image mode. */
     207               5 :   cinfo->enable_1pass_quant = FALSE;
     208               5 :   cinfo->enable_external_quant = FALSE;
     209               5 :   cinfo->enable_2pass_quant = FALSE;
     210               5 : }
     211                 : 
     212                 : 
     213                 : /*
     214                 :  * Decompression startup: read start of JPEG datastream to see what's there.
     215                 :  * Need only initialize JPEG object and supply a data source before calling.
     216                 :  *
     217                 :  * This routine will read as far as the first SOS marker (ie, actual start of
     218                 :  * compressed data), and will save all tables and parameters in the JPEG
     219                 :  * object.  It will also initialize the decompression parameters to default
     220                 :  * values, and finally return JPEG_HEADER_OK.  On return, the application may
     221                 :  * adjust the decompression parameters and then call jpeg_start_decompress.
     222                 :  * (Or, if the application only wanted to determine the image parameters,
     223                 :  * the data need not be decompressed.  In that case, call jpeg_abort or
     224                 :  * jpeg_destroy to release any temporary space.)
     225                 :  * If an abbreviated (tables only) datastream is presented, the routine will
     226                 :  * return JPEG_HEADER_TABLES_ONLY upon reaching EOI.  The application may then
     227                 :  * re-use the JPEG object to read the abbreviated image datastream(s).
     228                 :  * It is unnecessary (but OK) to call jpeg_abort in this case.
     229                 :  * The JPEG_SUSPENDED return code only occurs if the data source module
     230                 :  * requests suspension of the decompressor.  In this case the application
     231                 :  * should load more source data and then re-call jpeg_read_header to resume
     232                 :  * processing.
     233                 :  * If a non-suspending data source is used and require_image is TRUE, then the
     234                 :  * return code need not be inspected since only JPEG_HEADER_OK is possible.
     235                 :  *
     236                 :  * This routine is now just a front end to jpeg_consume_input, with some
     237                 :  * extra error checking.
     238                 :  */
     239                 : 
     240                 : GLOBAL(int)
     241               6 : jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
     242                 : {
     243                 :   int retcode;
     244                 : 
     245               7 :   if (cinfo->global_state != DSTATE_START &&
     246               1 :       cinfo->global_state != DSTATE_INHEADER)
     247               0 :     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
     248                 : 
     249               6 :   retcode = jpeg_consume_input(cinfo);
     250                 : 
     251               6 :   switch (retcode) {
     252                 :   case JPEG_REACHED_SOS:
     253               5 :     retcode = JPEG_HEADER_OK;
     254               5 :     break;
     255                 :   case JPEG_REACHED_EOI:
     256               0 :     if (require_image)          /* Complain if application wanted an image */
     257               0 :       ERREXIT(cinfo, JERR_NO_IMAGE);
     258                 :     /* Reset to start state; it would be safer to require the application to
     259                 :      * call jpeg_abort, but we can't change it now for compatibility reasons.
     260                 :      * A side effect is to free any temporary memory (there shouldn't be any).
     261                 :      */
     262               0 :     jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
     263               0 :     retcode = JPEG_HEADER_TABLES_ONLY;
     264               0 :     break;
     265                 :   case JPEG_SUSPENDED:
     266                 :     /* no work */
     267               1 :     break;
     268                 :   }
     269                 : 
     270               6 :   return retcode;
     271                 : }
     272                 : 
     273                 : 
     274                 : /*
     275                 :  * Consume data in advance of what the decompressor requires.
     276                 :  * This can be called at any time once the decompressor object has
     277                 :  * been created and a data source has been set up.
     278                 :  *
     279                 :  * This routine is essentially a state machine that handles a couple
     280                 :  * of critical state-transition actions, namely initial setup and
     281                 :  * transition from header scanning to ready-for-start_decompress.
     282                 :  * All the actual input is done via the input controller's consume_input
     283                 :  * method.
     284                 :  */
     285                 : 
     286                 : GLOBAL(int)
     287               6 : jpeg_consume_input (j_decompress_ptr cinfo)
     288                 : {
     289               6 :   int retcode = JPEG_SUSPENDED;
     290                 : 
     291                 :   /* NB: every possible DSTATE value should be listed in this switch */
     292               6 :   switch (cinfo->global_state) {
     293                 :   case DSTATE_START:
     294                 :     /* Start-of-datastream actions: reset appropriate modules */
     295               5 :     (*cinfo->inputctl->reset_input_controller) (cinfo);
     296                 :     /* Initialize application's data source module */
     297               5 :     (*cinfo->src->init_source) (cinfo);
     298               5 :     cinfo->global_state = DSTATE_INHEADER;
     299                 :     /*FALLTHROUGH*/
     300                 :   case DSTATE_INHEADER:
     301               6 :     retcode = (*cinfo->inputctl->consume_input) (cinfo);
     302               6 :     if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
     303                 :       /* Set up default parameters based on header data */
     304               5 :       default_decompress_parms(cinfo);
     305                 :       /* Set global state: ready for start_decompress */
     306               5 :       cinfo->global_state = DSTATE_READY;
     307                 :     }
     308               6 :     break;
     309                 :   case DSTATE_READY:
     310                 :     /* Can't advance past first SOS until start_decompress is called */
     311               0 :     retcode = JPEG_REACHED_SOS;
     312               0 :     break;
     313                 :   case DSTATE_PRELOAD:
     314                 :   case DSTATE_PRESCAN:
     315                 :   case DSTATE_SCANNING:
     316                 :   case DSTATE_RAW_OK:
     317                 :   case DSTATE_BUFIMAGE:
     318                 :   case DSTATE_BUFPOST:
     319                 :   case DSTATE_STOPPING:
     320               0 :     retcode = (*cinfo->inputctl->consume_input) (cinfo);
     321               0 :     break;
     322                 :   default:
     323               0 :     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
     324                 :   }
     325               6 :   return retcode;
     326                 : }
     327                 : 
     328                 : 
     329                 : /*
     330                 :  * Have we finished reading the input file?
     331                 :  */
     332                 : 
     333                 : GLOBAL(boolean)
     334               0 : jpeg_input_complete (j_decompress_ptr cinfo)
     335                 : {
     336                 :   /* Check for valid jpeg object */
     337               0 :   if (cinfo->global_state < DSTATE_START ||
     338               0 :       cinfo->global_state > DSTATE_STOPPING)
     339               0 :     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
     340               0 :   return cinfo->inputctl->eoi_reached;
     341                 : }
     342                 : 
     343                 : 
     344                 : /*
     345                 :  * Is there more than one scan?
     346                 :  */
     347                 : 
     348                 : GLOBAL(boolean)
     349               5 : jpeg_has_multiple_scans (j_decompress_ptr cinfo)
     350                 : {
     351                 :   /* Only valid after jpeg_read_header completes */
     352              10 :   if (cinfo->global_state < DSTATE_READY ||
     353               5 :       cinfo->global_state > DSTATE_STOPPING)
     354               0 :     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
     355               5 :   return cinfo->inputctl->has_multiple_scans;
     356                 : }
     357                 : 
     358                 : 
     359                 : /*
     360                 :  * Finish JPEG decompression.
     361                 :  *
     362                 :  * This will normally just verify the file trailer and release temp storage.
     363                 :  *
     364                 :  * Returns FALSE if suspended.  The return value need be inspected only if
     365                 :  * a suspending data source is used.
     366                 :  */
     367                 : 
     368                 : GLOBAL(boolean)
     369               5 : jpeg_finish_decompress (j_decompress_ptr cinfo)
     370                 : {
     371               5 :   if ((cinfo->global_state == DSTATE_SCANNING ||
     372               5 :        cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
     373                 :     /* Terminate final pass of non-buffered mode */
     374               5 :     if (cinfo->output_scanline < cinfo->output_height)
     375               0 :       ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
     376               5 :     (*cinfo->master->finish_output_pass) (cinfo);
     377               5 :     cinfo->global_state = DSTATE_STOPPING;
     378               0 :   } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
     379                 :     /* Finishing after a buffered-image operation */
     380               0 :     cinfo->global_state = DSTATE_STOPPING;
     381               0 :   } else if (cinfo->global_state != DSTATE_STOPPING) {
     382                 :     /* STOPPING = repeat call after a suspension, anything else is error */
     383               0 :     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
     384                 :   }
     385                 :   /* Read until EOI */
     386              15 :   while (! cinfo->inputctl->eoi_reached) {
     387               5 :     if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
     388               0 :       return FALSE;             /* Suspend, come back later */
     389                 :   }
     390                 :   /* Do final cleanup */
     391               5 :   (*cinfo->src->term_source) (cinfo);
     392                 :   /* We can use jpeg_abort to release memory and reset global_state */
     393               5 :   jpeg_abort((j_common_ptr) cinfo);
     394               5 :   return TRUE;
     395                 : }

Generated by: LCOV version 1.7