Home | History | Annotate | Download | only in libjpeg-turbo
      1 /*
      2  * jdtrans.c
      3  *
      4  * This file was part of the Independent JPEG Group's software:
      5  * Copyright (C) 1995-1997, Thomas G. Lane.
      6  * It was modified by The libjpeg-turbo Project to include only code relevant
      7  * to libjpeg-turbo.
      8  * For conditions of distribution and use, see the accompanying README file.
      9  *
     10  * This file contains library routines for transcoding decompression,
     11  * that is, reading raw DCT coefficient arrays from an input JPEG file.
     12  * The routines in jdapimin.c will also be needed by a transcoder.
     13  */
     14 
     15 #define JPEG_INTERNALS
     16 #include "jinclude.h"
     17 #include "jpeglib.h"
     18 
     19 
     20 /* Forward declarations */
     21 LOCAL(void) transdecode_master_selection (j_decompress_ptr cinfo);
     22 
     23 
     24 /*
     25  * Read the coefficient arrays from a JPEG file.
     26  * jpeg_read_header must be completed before calling this.
     27  *
     28  * The entire image is read into a set of virtual coefficient-block arrays,
     29  * one per component.  The return value is a pointer to the array of
     30  * virtual-array descriptors.  These can be manipulated directly via the
     31  * JPEG memory manager, or handed off to jpeg_write_coefficients().
     32  * To release the memory occupied by the virtual arrays, call
     33  * jpeg_finish_decompress() when done with the data.
     34  *
     35  * An alternative usage is to simply obtain access to the coefficient arrays
     36  * during a buffered-image-mode decompression operation.  This is allowed
     37  * after any jpeg_finish_output() call.  The arrays can be accessed until
     38  * jpeg_finish_decompress() is called.  (Note that any call to the library
     39  * may reposition the arrays, so don't rely on access_virt_barray() results
     40  * to stay valid across library calls.)
     41  *
     42  * Returns NULL if suspended.  This case need be checked only if
     43  * a suspending data source is used.
     44  */
     45 
     46 GLOBAL(jvirt_barray_ptr *)
     47 jpeg_read_coefficients (j_decompress_ptr cinfo)
     48 {
     49   if (cinfo->global_state == DSTATE_READY) {
     50     /* First call: initialize active modules */
     51     transdecode_master_selection(cinfo);
     52     cinfo->global_state = DSTATE_RDCOEFS;
     53   }
     54   if (cinfo->global_state == DSTATE_RDCOEFS) {
     55     /* Absorb whole file into the coef buffer */
     56     for (;;) {
     57       int retcode;
     58       /* Call progress monitor hook if present */
     59       if (cinfo->progress != NULL)
     60         (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
     61       /* Absorb some more input */
     62       retcode = (*cinfo->inputctl->consume_input) (cinfo);
     63       if (retcode == JPEG_SUSPENDED)
     64         return NULL;
     65       if (retcode == JPEG_REACHED_EOI)
     66         break;
     67       /* Advance progress counter if appropriate */
     68       if (cinfo->progress != NULL &&
     69           (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
     70         if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
     71           /* startup underestimated number of scans; ratchet up one scan */
     72           cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
     73         }
     74       }
     75     }
     76     /* Set state so that jpeg_finish_decompress does the right thing */
     77     cinfo->global_state = DSTATE_STOPPING;
     78   }
     79   /* At this point we should be in state DSTATE_STOPPING if being used
     80    * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access
     81    * to the coefficients during a full buffered-image-mode decompression.
     82    */
     83   if ((cinfo->global_state == DSTATE_STOPPING ||
     84        cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {
     85     return cinfo->coef->coef_arrays;
     86   }
     87   /* Oops, improper usage */
     88   ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
     89   return NULL;                  /* keep compiler happy */
     90 }
     91 
     92 
     93 /*
     94  * Master selection of decompression modules for transcoding.
     95  * This substitutes for jdmaster.c's initialization of the full decompressor.
     96  */
     97 
     98 LOCAL(void)
     99 transdecode_master_selection (j_decompress_ptr cinfo)
    100 {
    101   /* This is effectively a buffered-image operation. */
    102   cinfo->buffered_image = TRUE;
    103 
    104 #if JPEG_LIB_VERSION >= 80
    105   /* Compute output image dimensions and related values. */
    106   jpeg_core_output_dimensions(cinfo);
    107 #endif
    108 
    109   /* Entropy decoding: either Huffman or arithmetic coding. */
    110   if (cinfo->arith_code) {
    111 #ifdef D_ARITH_CODING_SUPPORTED
    112     jinit_arith_decoder(cinfo);
    113 #else
    114     ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
    115 #endif
    116   } else {
    117     if (cinfo->progressive_mode) {
    118 #ifdef D_PROGRESSIVE_SUPPORTED
    119       jinit_phuff_decoder(cinfo);
    120 #else
    121       ERREXIT(cinfo, JERR_NOT_COMPILED);
    122 #endif
    123     } else
    124       jinit_huff_decoder(cinfo);
    125   }
    126 
    127   /* Always get a full-image coefficient buffer. */
    128   jinit_d_coef_controller(cinfo, TRUE);
    129 
    130   /* We can now tell the memory manager to allocate virtual arrays. */
    131   (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
    132 
    133   /* Initialize input side of decompressor to consume first scan. */
    134   (*cinfo->inputctl->start_input_pass) (cinfo);
    135 
    136   /* Initialize progress monitoring. */
    137   if (cinfo->progress != NULL) {
    138     int nscans;
    139     /* Estimate number of scans to set pass_limit. */
    140     if (cinfo->progressive_mode) {
    141       /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
    142       nscans = 2 + 3 * cinfo->num_components;
    143     } else if (cinfo->inputctl->has_multiple_scans) {
    144       /* For a nonprogressive multiscan file, estimate 1 scan per component. */
    145       nscans = cinfo->num_components;
    146     } else {
    147       nscans = 1;
    148     }
    149     cinfo->progress->pass_counter = 0L;
    150     cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
    151     cinfo->progress->completed_passes = 0;
    152     cinfo->progress->total_passes = 1;
    153   }
    154 }
    155