Back to index

plt-scheme  4.2.1
jdtrans.c
Go to the documentation of this file.
00001 /*
00002  * jdtrans.c
00003  *
00004  * Copyright (C) 1995-1997, 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 library routines for transcoding decompression,
00009  * that is, reading raw DCT coefficient arrays from an input JPEG file.
00010  * The routines in jdapimin.c will also be needed by a transcoder.
00011  */
00012 
00013 #define JPEG_INTERNALS
00014 #include "jinclude.h"
00015 #include "jpeglib.h"
00016 
00017 
00018 /* Forward declarations */
00019 LOCAL(void) transdecode_master_selection JPP((j_decompress_ptr cinfo));
00020 
00021 
00022 /*
00023  * Read the coefficient arrays from a JPEG file.
00024  * jpeg_read_header must be completed before calling this.
00025  *
00026  * The entire image is read into a set of virtual coefficient-block arrays,
00027  * one per component.  The return value is a pointer to the array of
00028  * virtual-array descriptors.  These can be manipulated directly via the
00029  * JPEG memory manager, or handed off to jpeg_write_coefficients().
00030  * To release the memory occupied by the virtual arrays, call
00031  * jpeg_finish_decompress() when done with the data.
00032  *
00033  * An alternative usage is to simply obtain access to the coefficient arrays
00034  * during a buffered-image-mode decompression operation.  This is allowed
00035  * after any jpeg_finish_output() call.  The arrays can be accessed until
00036  * jpeg_finish_decompress() is called.  (Note that any call to the library
00037  * may reposition the arrays, so don't rely on access_virt_barray() results
00038  * to stay valid across library calls.)
00039  *
00040  * Returns NULL if suspended.  This case need be checked only if
00041  * a suspending data source is used.
00042  */
00043 
00044 GLOBAL(jvirt_barray_ptr *)
00045 jpeg_read_coefficients (j_decompress_ptr cinfo)
00046 {
00047   if (cinfo->global_state == DSTATE_READY) {
00048     /* First call: initialize active modules */
00049     transdecode_master_selection(cinfo);
00050     cinfo->global_state = DSTATE_RDCOEFS;
00051   }
00052   if (cinfo->global_state == DSTATE_RDCOEFS) {
00053     /* Absorb whole file into the coef buffer */
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 NULL;
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          /* startup underestimated number of scans; ratchet up one scan */
00070          cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
00071        }
00072       }
00073     }
00074     /* Set state so that jpeg_finish_decompress does the right thing */
00075     cinfo->global_state = DSTATE_STOPPING;
00076   }
00077   /* At this point we should be in state DSTATE_STOPPING if being used
00078    * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access
00079    * to the coefficients during a full buffered-image-mode decompression.
00080    */
00081   if ((cinfo->global_state == DSTATE_STOPPING ||
00082        cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {
00083     return cinfo->coef->coef_arrays;
00084   }
00085   /* Oops, improper usage */
00086   ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00087   return NULL;                     /* keep compiler happy */
00088 }
00089 
00090 
00091 /*
00092  * Master selection of decompression modules for transcoding.
00093  * This substitutes for jdmaster.c's initialization of the full decompressor.
00094  */
00095 
00096 LOCAL(void)
00097 transdecode_master_selection (j_decompress_ptr cinfo)
00098 {
00099   /* This is effectively a buffered-image operation. */
00100   cinfo->buffered_image = TRUE;
00101 
00102   /* Entropy decoding: either Huffman or arithmetic coding. */
00103   if (cinfo->arith_code) {
00104     ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
00105   } else {
00106     if (cinfo->progressive_mode) {
00107 #ifdef D_PROGRESSIVE_SUPPORTED
00108       jinit_phuff_decoder(cinfo);
00109 #else
00110       ERREXIT(cinfo, JERR_NOT_COMPILED);
00111 #endif
00112     } else
00113       jinit_huff_decoder(cinfo);
00114   }
00115 
00116   /* Always get a full-image coefficient buffer. */
00117   jinit_d_coef_controller(cinfo, TRUE);
00118 
00119   /* We can now tell the memory manager to allocate virtual arrays. */
00120   (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
00121 
00122   /* Initialize input side of decompressor to consume first scan. */
00123   (*cinfo->inputctl->start_input_pass) (cinfo);
00124 
00125   /* Initialize progress monitoring. */
00126   if (cinfo->progress != NULL) {
00127     int nscans;
00128     /* Estimate number of scans to set pass_limit. */
00129     if (cinfo->progressive_mode) {
00130       /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
00131       nscans = 2 + 3 * cinfo->num_components;
00132     } else if (cinfo->inputctl->has_multiple_scans) {
00133       /* For a nonprogressive multiscan file, estimate 1 scan per component. */
00134       nscans = cinfo->num_components;
00135     } else {
00136       nscans = 1;
00137     }
00138     cinfo->progress->pass_counter = 0L;
00139     cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
00140     cinfo->progress->completed_passes = 0;
00141     cinfo->progress->total_passes = 1;
00142   }
00143 }