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