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

jcmainct.c

00001 /*
00002  * jcmainct.c
00003  *
00004  * Copyright (C) 1994-1996, Thomas G. Lane.
00005  * Modified 2003-2012 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 the main buffer controller for compression.
00010  * The main buffer lies between the pre-processor and the JPEG
00011  * compressor proper; it holds downsampled data in the JPEG colorspace.
00012  */
00013 
00014 #define JPEG_INTERNALS
00015 #include "jinclude.h"
00016 #include "jpeglib.h"
00017 
00018 
00019 /* Note: currently, there is no operating mode in which a full-image buffer
00020  * is needed at this step.  If there were, that mode could not be used with
00021  * "raw data" input, since this module is bypassed in that case.  However,
00022  * we've left the code here for possible use in special applications.
00023  */
00024 #undef FULL_MAIN_BUFFER_SUPPORTED
00025 
00026 
00027 /* Private buffer controller object */
00028 
00029 typedef struct {
00030   struct jpeg_c_main_controller pub; /* public fields */
00031 
00032   JDIMENSION cur_iMCU_row;  /* number of current iMCU row */
00033   JDIMENSION rowgroup_ctr;  /* counts row groups received in iMCU row */
00034   boolean suspended;        /* remember if we suspended output */
00035   J_BUF_MODE pass_mode;     /* current operating mode */
00036 
00037   /* If using just a strip buffer, this points to the entire set of buffers
00038    * (we allocate one for each component).  In the full-image case, this
00039    * points to the currently accessible strips of the virtual arrays.
00040    */
00041   JSAMPARRAY buffer[MAX_COMPONENTS];
00042 
00043 #ifdef FULL_MAIN_BUFFER_SUPPORTED
00044   /* If using full-image storage, this array holds pointers to virtual-array
00045    * control blocks for each component.  Unused if not full-image storage.
00046    */
00047   jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
00048 #endif
00049 } my_main_controller;
00050 
00051 typedef my_main_controller * my_main_ptr;
00052 
00053 
00054 /* Forward declarations */
00055 METHODDEF(void) process_data_simple_main
00056     JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
00057          JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
00058 #ifdef FULL_MAIN_BUFFER_SUPPORTED
00059 METHODDEF(void) process_data_buffer_main
00060     JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
00061          JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
00062 #endif
00063 
00064 
00065 /*
00066  * Initialize for a processing pass.
00067  */
00068 
00069 METHODDEF(void)
00070 start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
00071 {
00072   my_main_ptr mainp = (my_main_ptr) cinfo->main;
00073 
00074   /* Do nothing in raw-data mode. */
00075   if (cinfo->raw_data_in)
00076     return;
00077 
00078   mainp->cur_iMCU_row = 0;  /* initialize counters */
00079   mainp->rowgroup_ctr = 0;
00080   mainp->suspended = FALSE;
00081   mainp->pass_mode = pass_mode; /* save mode for use by process_data */
00082 
00083   switch (pass_mode) {
00084   case JBUF_PASS_THRU:
00085 #ifdef FULL_MAIN_BUFFER_SUPPORTED
00086     if (mainp->whole_image[0] != NULL)
00087       ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00088 #endif
00089     mainp->pub.process_data = process_data_simple_main;
00090     break;
00091 #ifdef FULL_MAIN_BUFFER_SUPPORTED
00092   case JBUF_SAVE_SOURCE:
00093   case JBUF_CRANK_DEST:
00094   case JBUF_SAVE_AND_PASS:
00095     if (mainp->whole_image[0] == NULL)
00096       ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00097     mainp->pub.process_data = process_data_buffer_main;
00098     break;
00099 #endif
00100   default:
00101     ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00102     break;
00103   }
00104 }
00105 
00106 
00107 /*
00108  * Process some data.
00109  * This routine handles the simple pass-through mode,
00110  * where we have only a strip buffer.
00111  */
00112 
00113 METHODDEF(void)
00114 process_data_simple_main (j_compress_ptr cinfo,
00115               JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
00116               JDIMENSION in_rows_avail)
00117 {
00118   my_main_ptr mainp = (my_main_ptr) cinfo->main;
00119 
00120   while (mainp->cur_iMCU_row < cinfo->total_iMCU_rows) {
00121     /* Read input data if we haven't filled the main buffer yet */
00122     if (mainp->rowgroup_ctr < (JDIMENSION) cinfo->min_DCT_v_scaled_size)
00123       (*cinfo->prep->pre_process_data) (cinfo,
00124                     input_buf, in_row_ctr, in_rows_avail,
00125                     mainp->buffer, &mainp->rowgroup_ctr,
00126                     (JDIMENSION) cinfo->min_DCT_v_scaled_size);
00127 
00128     /* If we don't have a full iMCU row buffered, return to application for
00129      * more data.  Note that preprocessor will always pad to fill the iMCU row
00130      * at the bottom of the image.
00131      */
00132     if (mainp->rowgroup_ctr != (JDIMENSION) cinfo->min_DCT_v_scaled_size)
00133       return;
00134 
00135     /* Send the completed row to the compressor */
00136     if (! (*cinfo->coef->compress_data) (cinfo, mainp->buffer)) {
00137       /* If compressor did not consume the whole row, then we must need to
00138        * suspend processing and return to the application.  In this situation
00139        * we pretend we didn't yet consume the last input row; otherwise, if
00140        * it happened to be the last row of the image, the application would
00141        * think we were done.
00142        */
00143       if (! mainp->suspended) {
00144     (*in_row_ctr)--;
00145     mainp->suspended = TRUE;
00146       }
00147       return;
00148     }
00149     /* We did finish the row.  Undo our little suspension hack if a previous
00150      * call suspended; then mark the main buffer empty.
00151      */
00152     if (mainp->suspended) {
00153       (*in_row_ctr)++;
00154       mainp->suspended = FALSE;
00155     }
00156     mainp->rowgroup_ctr = 0;
00157     mainp->cur_iMCU_row++;
00158   }
00159 }
00160 
00161 
00162 #ifdef FULL_MAIN_BUFFER_SUPPORTED
00163 
00164 /*
00165  * Process some data.
00166  * This routine handles all of the modes that use a full-size buffer.
00167  */
00168 
00169 METHODDEF(void)
00170 process_data_buffer_main (j_compress_ptr cinfo,
00171               JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
00172               JDIMENSION in_rows_avail)
00173 {
00174   my_main_ptr mainp = (my_main_ptr) cinfo->main;
00175   int ci;
00176   jpeg_component_info *compptr;
00177   boolean writing = (mainp->pass_mode != JBUF_CRANK_DEST);
00178 
00179   while (mainp->cur_iMCU_row < cinfo->total_iMCU_rows) {
00180     /* Realign the virtual buffers if at the start of an iMCU row. */
00181     if (mainp->rowgroup_ctr == 0) {
00182       for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00183        ci++, compptr++) {
00184     mainp->buffer[ci] = (*cinfo->mem->access_virt_sarray)
00185       ((j_common_ptr) cinfo, mainp->whole_image[ci], mainp->cur_iMCU_row *
00186        ((JDIMENSION) (compptr->v_samp_factor * cinfo->min_DCT_v_scaled_size)),
00187        (JDIMENSION) (compptr->v_samp_factor * cinfo->min_DCT_v_scaled_size),
00188        writing);
00189       }
00190       /* In a read pass, pretend we just read some source data. */
00191       if (! writing) {
00192     *in_row_ctr += (JDIMENSION)
00193       (cinfo->max_v_samp_factor * cinfo->min_DCT_v_scaled_size);
00194     mainp->rowgroup_ctr = (JDIMENSION) cinfo->min_DCT_v_scaled_size;
00195       }
00196     }
00197 
00198     /* If a write pass, read input data until the current iMCU row is full. */
00199     /* Note: preprocessor will pad if necessary to fill the last iMCU row. */
00200     if (writing) {
00201       (*cinfo->prep->pre_process_data) (cinfo,
00202                     input_buf, in_row_ctr, in_rows_avail,
00203                     mainp->buffer, &mainp->rowgroup_ctr,
00204                     (JDIMENSION) cinfo->min_DCT_v_scaled_size);
00205       /* Return to application if we need more data to fill the iMCU row. */
00206       if (mainp->rowgroup_ctr < (JDIMENSION) cinfo->min_DCT_v_scaled_size)
00207     return;
00208     }
00209 
00210     /* Emit data, unless this is a sink-only pass. */
00211     if (mainp->pass_mode != JBUF_SAVE_SOURCE) {
00212       if (! (*cinfo->coef->compress_data) (cinfo, mainp->buffer)) {
00213     /* If compressor did not consume the whole row, then we must need to
00214      * suspend processing and return to the application.  In this situation
00215      * we pretend we didn't yet consume the last input row; otherwise, if
00216      * it happened to be the last row of the image, the application would
00217      * think we were done.
00218      */
00219     if (! mainp->suspended) {
00220       (*in_row_ctr)--;
00221       mainp->suspended = TRUE;
00222     }
00223     return;
00224       }
00225       /* We did finish the row.  Undo our little suspension hack if a previous
00226        * call suspended; then mark the main buffer empty.
00227        */
00228       if (mainp->suspended) {
00229     (*in_row_ctr)++;
00230     mainp->suspended = FALSE;
00231       }
00232     }
00233 
00234     /* If get here, we are done with this iMCU row.  Mark buffer empty. */
00235     mainp->rowgroup_ctr = 0;
00236     mainp->cur_iMCU_row++;
00237   }
00238 }
00239 
00240 #endif /* FULL_MAIN_BUFFER_SUPPORTED */
00241 
00242 
00243 /*
00244  * Initialize main buffer controller.
00245  */
00246 
00247 GLOBAL(void)
00248 jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
00249 {
00250   my_main_ptr mainp;
00251   int ci;
00252   jpeg_component_info *compptr;
00253 
00254   mainp = (my_main_ptr)
00255     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00256                 SIZEOF(my_main_controller));
00257   cinfo->main = &mainp->pub;
00258   mainp->pub.start_pass = start_pass_main;
00259 
00260   /* We don't need to create a buffer in raw-data mode. */
00261   if (cinfo->raw_data_in)
00262     return;
00263 
00264   /* Create the buffer.  It holds downsampled data, so each component
00265    * may be of a different size.
00266    */
00267   if (need_full_buffer) {
00268 #ifdef FULL_MAIN_BUFFER_SUPPORTED
00269     /* Allocate a full-image virtual array for each component */
00270     /* Note we pad the bottom to a multiple of the iMCU height */
00271     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00272      ci++, compptr++) {
00273       mainp->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
00274     ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
00275      compptr->width_in_blocks * ((JDIMENSION) compptr->DCT_h_scaled_size),
00276      ((JDIMENSION) jround_up((long) compptr->height_in_blocks,
00277                  (long) compptr->v_samp_factor)) *
00278      ((JDIMENSION) cinfo->min_DCT_v_scaled_size),
00279      (JDIMENSION) (compptr->v_samp_factor * compptr->DCT_v_scaled_size));
00280     }
00281 #else
00282     ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00283 #endif
00284   } else {
00285 #ifdef FULL_MAIN_BUFFER_SUPPORTED
00286     mainp->whole_image[0] = NULL; /* flag for no virtual arrays */
00287 #endif
00288     /* Allocate a strip buffer for each component */
00289     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00290      ci++, compptr++) {
00291       mainp->buffer[ci] = (*cinfo->mem->alloc_sarray)
00292     ((j_common_ptr) cinfo, JPOOL_IMAGE,
00293      compptr->width_in_blocks * ((JDIMENSION) compptr->DCT_h_scaled_size),
00294      (JDIMENSION) (compptr->v_samp_factor * compptr->DCT_v_scaled_size));
00295     }
00296   }
00297 }