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 jdapistd.c Source File

jdapistd.c

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