Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / compiler / libjpeg / main / jdtrans.c
blob5c5e0710f535b10b53e1620ab54e42e452ecb7b8
1 /*
2 $Id$
3 */
5 /*
6 * jdtrans.c
8 * Copyright (C) 1995-1998, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains library routines for transcoding decompression,
13 * that is, reading raw DCT coefficient arrays from an input JPEG file.
14 * The routines in jdapimin.c will also be needed by a transcoder.
17 #define JPEG_INTERNALS
18 #include "jinclude.h"
19 #include "jpeglib.h"
20 #include "jlossy.h"
23 /* Forward declarations */
24 LOCAL(void) transdecode_master_selection JPP((j_decompress_ptr cinfo));
28 * Read the coefficient arrays from a JPEG file.
29 * jpeg_read_header must be completed before calling this.
31 * The entire image is read into a set of virtual coefficient-block arrays,
32 * one per component. The return value is a pointer to the array of
33 * virtual-array descriptors. These can be manipulated directly via the
34 * JPEG memory manager, or handed off to jpeg_write_coefficients().
35 * To release the memory occupied by the virtual arrays, call
36 * jpeg_finish_decompress() when done with the data.
38 * An alternative usage is to simply obtain access to the coefficient arrays
39 * during a buffered-image-mode decompression operation. This is allowed
40 * after any jpeg_finish_output() call. The arrays can be accessed until
41 * jpeg_finish_decompress() is called. (Note that any call to the library
42 * may reposition the arrays, so don't rely on access_virt_barray() results
43 * to stay valid across library calls.)
45 * Returns NULL if suspended. This case need be checked only if
46 * a suspending data source is used.
49 JGLOBAL(jvirt_barray_ptr *)
50 jpeg_read_coefficients (j_decompress_ptr cinfo)
52 j_lossy_d_ptr decomp;
54 /* Can't read coefficients from lossless streams */
55 if (cinfo->process == JPROC_LOSSLESS) {
56 ERREXIT(cinfo, JERR_CANT_TRANSCODE);
57 return NULL;
60 if (cinfo->global_state == DSTATE_READY) {
61 /* First call: initialize active modules */
62 transdecode_master_selection(cinfo);
63 cinfo->global_state = DSTATE_RDCOEFS;
65 if (cinfo->global_state == DSTATE_RDCOEFS) {
66 /* Absorb whole file into the coef buffer */
67 for (;;) {
68 int retcode;
69 /* Call progress monitor hook if present */
70 if (cinfo->progress != NULL)
71 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
72 /* Absorb some more input */
73 retcode = (*cinfo->inputctl->consume_input) (cinfo);
74 if (retcode == JPEG_SUSPENDED)
75 return NULL;
76 if (retcode == JPEG_REACHED_EOI)
77 break;
78 /* Advance progress counter if appropriate */
79 if (cinfo->progress != NULL &&
80 (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
81 if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
82 /* startup underestimated number of scans; ratchet up one scan */
83 cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
87 /* Set state so that jpeg_finish_decompress does the right thing */
88 cinfo->global_state = DSTATE_STOPPING;
90 /* At this point we should be in state DSTATE_STOPPING if being used
91 * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access
92 * to the coefficients during a full buffered-image-mode decompression.
94 if ((cinfo->global_state == DSTATE_STOPPING ||
95 cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {
96 return ((j_lossy_d_ptr) cinfo->codec)->coef_arrays;
98 /* Oops, improper usage */
99 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
100 return NULL; /* keep compiler happy */
105 * Master selection of decompression modules for transcoding.
106 * This substitutes for jdmaster.c's initialization of the full decompressor.
109 LOCAL(void)
110 transdecode_master_selection (j_decompress_ptr cinfo)
112 /* This is effectively a buffered-image operation. */
113 cinfo->buffered_image = TRUE;
115 /* Initialize decompression codec */
116 jinit_d_codec(cinfo);
118 /* We can now tell the memory manager to allocate virtual arrays. */
119 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
121 /* Initialize input side of decompressor to consume first scan. */
122 (*cinfo->inputctl->start_input_pass) (cinfo);
124 /* Initialize progress monitoring. */
125 if (cinfo->progress != NULL) {
126 int nscans;
127 /* Estimate number of scans to set pass_limit. */
128 if (cinfo->process == JPROC_PROGRESSIVE) {
129 /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
130 nscans = 2 + 3 * cinfo->num_components;
131 } else if (cinfo->inputctl->has_multiple_scans) {
132 /* For a nonprogressive multiscan file, estimate 1 scan per component. */
133 nscans = cinfo->num_components;
134 } else {
135 nscans = 1;
137 cinfo->progress->pass_counter = 0L;
138 cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
139 cinfo->progress->completed_passes = 0;
140 cinfo->progress->total_passes = 1;