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