Back to index

lightning-sunbird  0.9+nobinonly
jdapistd.c
Go to the documentation of this file.
00001 /*
00002  * jdapistd.c
00003  *
00004  * Copyright (C) 1994-1996, 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 "standard" API routines that are
00010  * used in the normal full-decompression case.  They are not used by a
00011  * transcoding-only application.  Note that if an application links in
00012  * jpeg_start_decompress, it will end up linking in the entire decompressor.
00013  * We thus must separate this file from jdapimin.c to avoid linking the
00014  * whole decompression library into a transcoder.
00015  */
00016 
00017 #define JPEG_INTERNALS
00018 #include "jinclude.h"
00019 #include "jpeglib.h"
00020 
00021 
00022 /* Forward declarations */
00023 LOCAL(boolean) output_pass_setup JPP((j_decompress_ptr cinfo));
00024 
00025 
00026 /*
00027  * Decompression initialization.
00028  * jpeg_read_header must be completed before calling this.
00029  *
00030  * If a multipass operating mode was selected, this will do all but the
00031  * last pass, and thus may take a great deal of time.
00032  *
00033  * Returns FALSE if suspended.  The return value need be inspected only if
00034  * a suspending data source is used.
00035  */
00036 
00037 GLOBAL(boolean)
00038 jpeg_start_decompress (j_decompress_ptr cinfo)
00039 {
00040   if (cinfo->global_state == DSTATE_READY) {
00041     /* First call: initialize master control, select active modules */
00042     jinit_master_decompress(cinfo);
00043     if (cinfo->buffered_image) {
00044       /* No more work here; expecting jpeg_start_output next */
00045       cinfo->global_state = DSTATE_BUFIMAGE;
00046       return TRUE;
00047     }
00048     cinfo->global_state = DSTATE_PRELOAD;
00049   }
00050   if (cinfo->global_state == DSTATE_PRELOAD) {
00051     /* If file has multiple scans, absorb them all into the coef buffer */
00052     if (cinfo->inputctl->has_multiple_scans) {
00053 #ifdef D_MULTISCAN_FILES_SUPPORTED
00054       for (;;) {
00055        int retcode;
00056        /* Call progress monitor hook if present */
00057        if (cinfo->progress != NULL)
00058          (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
00059        /* Absorb some more input */
00060        retcode = (*cinfo->inputctl->consume_input) (cinfo);
00061        if (retcode == JPEG_SUSPENDED)
00062          return FALSE;
00063        if (retcode == JPEG_REACHED_EOI)
00064          break;
00065        /* Advance progress counter if appropriate */
00066        if (cinfo->progress != NULL &&
00067            (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
00068          if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
00069            /* jdmaster underestimated number of scans; ratchet up one scan */
00070            cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
00071          }
00072        }
00073       }
00074 #else
00075       ERREXIT(cinfo, JERR_NOT_COMPILED);
00076 #endif /* D_MULTISCAN_FILES_SUPPORTED */
00077     }
00078     cinfo->output_scan_number = cinfo->input_scan_number;
00079   } else if (cinfo->global_state != DSTATE_PRESCAN)
00080     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00081   /* Perform any dummy output passes, and set up for the final pass */
00082   return output_pass_setup(cinfo);
00083 }
00084 
00085 
00086 /*
00087  * Set up for an output pass, and perform any dummy pass(es) needed.
00088  * Common subroutine for jpeg_start_decompress and jpeg_start_output.
00089  * Entry: global_state = DSTATE_PRESCAN only if previously suspended.
00090  * Exit: If done, returns TRUE and sets global_state for proper output mode.
00091  *       If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.
00092  */
00093 
00094 LOCAL(boolean)
00095 output_pass_setup (j_decompress_ptr cinfo)
00096 {
00097   if (cinfo->global_state != DSTATE_PRESCAN) {
00098     /* First call: do pass setup */
00099     (*cinfo->master->prepare_for_output_pass) (cinfo);
00100     cinfo->output_scanline = 0;
00101     cinfo->global_state = DSTATE_PRESCAN;
00102   }
00103   /* Loop over any required dummy passes */
00104   while (cinfo->master->is_dummy_pass) {
00105 #ifdef QUANT_2PASS_SUPPORTED
00106     /* Crank through the dummy pass */
00107     while (cinfo->output_scanline < cinfo->output_height) {
00108       JDIMENSION last_scanline;
00109       /* Call progress monitor hook if present */
00110       if (cinfo->progress != NULL) {
00111        cinfo->progress->pass_counter = (long) cinfo->output_scanline;
00112        cinfo->progress->pass_limit = (long) cinfo->output_height;
00113        (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
00114       }
00115       /* Process some data */
00116       last_scanline = cinfo->output_scanline;
00117       (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,
00118                                 &cinfo->output_scanline, (JDIMENSION) 0);
00119       if (cinfo->output_scanline == last_scanline)
00120        return FALSE;        /* No progress made, must suspend */
00121     }
00122     /* Finish up dummy pass, and set up for another one */
00123     (*cinfo->master->finish_output_pass) (cinfo);
00124     (*cinfo->master->prepare_for_output_pass) (cinfo);
00125     cinfo->output_scanline = 0;
00126 #else
00127     ERREXIT(cinfo, JERR_NOT_COMPILED);
00128 #endif /* QUANT_2PASS_SUPPORTED */
00129   }
00130   /* Ready for application to drive output pass through
00131    * jpeg_read_scanlines or jpeg_read_raw_data.
00132    */
00133   cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
00134   return TRUE;
00135 }
00136 
00137 
00138 /*
00139  * Read some scanlines of data from the JPEG decompressor.
00140  *
00141  * The return value will be the number of lines actually read.
00142  * This may be less than the number requested in several cases,
00143  * including bottom of image, data source suspension, and operating
00144  * modes that emit multiple scanlines at a time.
00145  *
00146  * Note: we warn about excess calls to jpeg_read_scanlines() since
00147  * this likely signals an application programmer error.  However,
00148  * an oversize buffer (max_lines > scanlines remaining) is not an error.
00149  */
00150 
00151 GLOBAL(JDIMENSION)
00152 jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines,
00153                    JDIMENSION max_lines)
00154 {
00155   JDIMENSION row_ctr;
00156 
00157   if (cinfo->global_state != DSTATE_SCANNING)
00158     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00159   if (cinfo->output_scanline >= cinfo->output_height) {
00160     WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
00161     return 0;
00162   }
00163 
00164   /* Call progress monitor hook if present */
00165   if (cinfo->progress != NULL) {
00166     cinfo->progress->pass_counter = (long) cinfo->output_scanline;
00167     cinfo->progress->pass_limit = (long) cinfo->output_height;
00168     (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
00169   }
00170 
00171   /* Process some data */
00172   row_ctr = 0;
00173   (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
00174   cinfo->output_scanline += row_ctr;
00175   return row_ctr;
00176 }
00177 
00178 
00179 /*
00180  * Alternate entry point to read raw data.
00181  * Processes exactly one iMCU row per call, unless suspended.
00182  */
00183 
00184 GLOBAL(JDIMENSION)
00185 jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data,
00186                   JDIMENSION max_lines)
00187 {
00188   JDIMENSION lines_per_iMCU_row;
00189 
00190   if (cinfo->global_state != DSTATE_RAW_OK)
00191     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00192   if (cinfo->output_scanline >= cinfo->output_height) {
00193     WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
00194     return 0;
00195   }
00196 
00197   /* Call progress monitor hook if present */
00198   if (cinfo->progress != NULL) {
00199     cinfo->progress->pass_counter = (long) cinfo->output_scanline;
00200     cinfo->progress->pass_limit = (long) cinfo->output_height;
00201     (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
00202   }
00203 
00204   /* Verify that at least one iMCU row can be returned. */
00205   lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size;
00206   if (max_lines < lines_per_iMCU_row)
00207     ERREXIT(cinfo, JERR_BUFFER_SIZE);
00208 
00209   /* Decompress directly into user's buffer. */
00210   if (! (*cinfo->coef->decompress_data) (cinfo, data))
00211     return 0;               /* suspension forced, can do nothing more */
00212 
00213   /* OK, we processed one iMCU row. */
00214   cinfo->output_scanline += lines_per_iMCU_row;
00215   return lines_per_iMCU_row;
00216 }
00217 
00218 
00219 /* Additional entry points for buffered-image mode. */
00220 
00221 #ifdef D_MULTISCAN_FILES_SUPPORTED
00222 
00223 /*
00224  * Initialize for an output pass in buffered-image mode.
00225  */
00226 
00227 GLOBAL(boolean)
00228 jpeg_start_output (j_decompress_ptr cinfo, int scan_number)
00229 {
00230   if (cinfo->global_state != DSTATE_BUFIMAGE &&
00231       cinfo->global_state != DSTATE_PRESCAN)
00232     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00233   /* Limit scan number to valid range */
00234   if (scan_number <= 0)
00235     scan_number = 1;
00236   if (cinfo->inputctl->eoi_reached &&
00237       scan_number > cinfo->input_scan_number)
00238     scan_number = cinfo->input_scan_number;
00239   cinfo->output_scan_number = scan_number;
00240   /* Perform any dummy output passes, and set up for the real pass */
00241   return output_pass_setup(cinfo);
00242 }
00243 
00244 
00245 /*
00246  * Finish up after an output pass in buffered-image mode.
00247  *
00248  * Returns FALSE if suspended.  The return value need be inspected only if
00249  * a suspending data source is used.
00250  */
00251 
00252 GLOBAL(boolean)
00253 jpeg_finish_output (j_decompress_ptr cinfo)
00254 {
00255   if ((cinfo->global_state == DSTATE_SCANNING ||
00256        cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
00257     /* Terminate this pass. */
00258     /* We do not require the whole pass to have been completed. */
00259     (*cinfo->master->finish_output_pass) (cinfo);
00260     cinfo->global_state = DSTATE_BUFPOST;
00261   } else if (cinfo->global_state != DSTATE_BUFPOST) {
00262     /* BUFPOST = repeat call after a suspension, anything else is error */
00263     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00264   }
00265   /* Read markers looking for SOS or EOI */
00266   while (cinfo->input_scan_number <= cinfo->output_scan_number &&
00267         ! cinfo->inputctl->eoi_reached) {
00268     if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
00269       return FALSE;         /* Suspend, come back later */
00270   }
00271   cinfo->global_state = DSTATE_BUFIMAGE;
00272   return TRUE;
00273 }
00274 
00275 #endif /* D_MULTISCAN_FILES_SUPPORTED */