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